Skip to content

Commit 3356ecb

Browse files
riastradhriastradh
authored andcommitted
x86: Make sure esp is aligned when delivering signal.
While here, use STACK_ALIGNBYTES consistently for the alignment mask (or STACK_ALIGNBYTES32 in amd64 for the compat32 alignment mask). PR kern/59327: user stack pointer is not aligned properly
1 parent 5fdc6ef commit 3356ecb

5 files changed

Lines changed: 17 additions & 12 deletions

File tree

sys/arch/amd64/amd64/machdep.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: machdep.c,v 1.373 2025/03/17 11:39:02 riastradh Exp $ */
1+
/* $NetBSD: machdep.c,v 1.374 2025/04/24 23:51:03 riastradh Exp $ */
22

33
/*
44
* Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
@@ -110,7 +110,7 @@
110110
*/
111111

112112
#include <sys/cdefs.h>
113-
__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.373 2025/03/17 11:39:02 riastradh Exp $");
113+
__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.374 2025/04/24 23:51:03 riastradh Exp $");
114114

115115
#include "opt_modular.h"
116116
#include "opt_user_ldt.h"
@@ -612,7 +612,8 @@ sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
612612

613613
sp -= sizeof(struct sigframe_siginfo);
614614
/* Round down the stackpointer to a multiple of 16 for the ABI. */
615-
fp = (struct sigframe_siginfo *)(((unsigned long)sp & ~15) - 8);
615+
fp = (struct sigframe_siginfo *)(((unsigned long)sp &
616+
~STACK_ALIGNBYTES) - 8);
616617

617618
memset(&frame, 0, sizeof(frame));
618619
frame.sf_ra = (uint64_t)ps->sa_sigdesc[sig].sd_tramp;

sys/arch/amd64/amd64/netbsd32_machdep.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: netbsd32_machdep.c,v 1.141 2022/08/20 23:49:31 riastradh Exp $ */
1+
/* $NetBSD: netbsd32_machdep.c,v 1.142 2025/04/24 23:51:03 riastradh Exp $ */
22

33
/*
44
* Copyright (c) 2001 Wasabi Systems, Inc.
@@ -36,7 +36,7 @@
3636
*/
3737

3838
#include <sys/cdefs.h>
39-
__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.141 2022/08/20 23:49:31 riastradh Exp $");
39+
__KERNEL_RCSID(0, "$NetBSD: netbsd32_machdep.c,v 1.142 2025/04/24 23:51:03 riastradh Exp $");
4040

4141
#ifdef _KERNEL_OPT
4242
#include "opt_compat_netbsd.h"
@@ -233,6 +233,8 @@ netbsd32_sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
233233
fp = (struct netbsd32_sigframe_siginfo *)tf->tf_rsp;
234234

235235
fp--;
236+
fp = (struct netbsd32_sigframe_siginfo *)((uintptr_t)fp &
237+
~STACK_ALIGNBYTES32);
236238

237239
/* Build stack frame for signal trampoline. */
238240
switch (ps->sa_sigdesc[sig].sd_vers) {

sys/arch/amd64/include/param.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: param.h,v 1.40 2025/04/24 09:58:26 kre Exp $ */
1+
/* $NetBSD: param.h,v 1.41 2025/04/24 23:51:03 riastradh Exp $ */
22

33
#ifdef __x86_64__
44

@@ -31,6 +31,7 @@
3131
* (2) rtld in glibc >= 2.23 for Linux/x86_64 requires it.
3232
*/
3333
#define STACK_ALIGNBYTES (16 - 1)
34+
#define STACK_ALIGNBYTES32 (4 - 1)
3435

3536
#define ALIGNBYTES32 (sizeof(int) - 1)
3637
#define ALIGN32(p) (((u_long)(p) + ALIGNBYTES32) &~ALIGNBYTES32)

sys/arch/i386/i386/machdep.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: machdep.c,v 1.845 2025/03/17 11:39:02 riastradh Exp $ */
1+
/* $NetBSD: machdep.c,v 1.846 2025/04/24 23:51:03 riastradh Exp $ */
22

33
/*
44
* Copyright (c) 1996, 1997, 1998, 2000, 2004, 2006, 2008, 2009, 2017
@@ -67,7 +67,7 @@
6767
*/
6868

6969
#include <sys/cdefs.h>
70-
__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.845 2025/03/17 11:39:02 riastradh Exp $");
70+
__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.846 2025/04/24 23:51:03 riastradh Exp $");
7171

7272
#include "opt_beep.h"
7373
#include "opt_compat_freebsd.h"
@@ -676,6 +676,7 @@ sendsig_siginfo(const ksiginfo_t *ksi, const sigset_t *mask)
676676
KASSERT(mutex_owned(p->p_lock));
677677

678678
fp--;
679+
fp = (struct sigframe_siginfo *)((uintptr_t)fp & ~STACK_ALIGNBYTES);
679680

680681
memset(&frame, 0, sizeof(frame));
681682
frame.sf_ra = (int)ps->sa_sigdesc[sig].sd_tramp;

tests/kernel/t_signal_and_sp.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* $NetBSD: t_signal_and_sp.c,v 1.12 2025/04/24 17:00:25 riastradh Exp $ */
1+
/* $NetBSD: t_signal_and_sp.c,v 1.13 2025/04/24 23:51:03 riastradh Exp $ */
22

33
/*
44
* Copyright (c) 2024 The NetBSD Foundation, Inc.
@@ -27,7 +27,7 @@
2727
*/
2828

2929
#include <sys/cdefs.h>
30-
__RCSID("$NetBSD: t_signal_and_sp.c,v 1.12 2025/04/24 17:00:25 riastradh Exp $");
30+
__RCSID("$NetBSD: t_signal_and_sp.c,v 1.13 2025/04/24 23:51:03 riastradh Exp $");
3131

3232
#include <sys/wait.h>
3333

@@ -431,7 +431,7 @@ ATF_TC_BODY(signalsp_sigaltstack, tc)
431431
fprintf(stderr, "stack @ [%p, %p)\n",
432432
stack, stack + SIGSTKSZ + STACK_ALIGNBYTES);
433433

434-
#if defined __alpha__ || defined __i386__ || defined __mips__
434+
#if defined __alpha__ || defined __mips__
435435
atf_tc_expect_fail("PR kern/59327:"
436436
" user stack pointer is not aligned properly");
437437
#endif
@@ -575,7 +575,7 @@ ATF_TC_BODY(misaligned_sp_and_signal, tc)
575575
sa.sa_handler = &signalsphandler;
576576
RL(sigaction(SIGALRM, &sa, NULL));
577577

578-
#if defined __alpha__ || defined __i386__ || defined __mips__
578+
#if defined __alpha__ || defined __mips__
579579
atf_tc_expect_fail("PR kern/58149:"
580580
" Cannot return from a signal handler"
581581
" if SP was misaligned when the signal arrived");

0 commit comments

Comments
 (0)