NetBSD Problem Report #58149
From www@netbsd.org Sun Apr 14 03:39:11 2024
Return-Path: <www@netbsd.org>
Received: from mail.netbsd.org (mail.netbsd.org [199.233.217.200])
(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits))
(Client CN "mail.NetBSD.org", Issuer "mail.NetBSD.org CA" (not verified))
by mollari.NetBSD.org (Postfix) with ESMTPS id 18A7F1A9238
for <gnats-bugs@gnats.NetBSD.org>; Sun, 14 Apr 2024 03:39:11 +0000 (UTC)
Message-Id: <20240414033910.1391F1A9239@mollari.NetBSD.org>
Date: Sun, 14 Apr 2024 03:39:10 +0000 (UTC)
From: pho@cielonegro.org
Reply-To: pho@cielonegro.org
To: gnats-bugs@NetBSD.org
Subject: aarch64: Cannot return from a signal handler if SP was misaligned when the signal arrived
X-Send-Pr-Version: www-1.0
>Number: 58149
>Category: kern
>Synopsis: aarch64: Cannot return from a signal handler if SP was misaligned when the signal arrived
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: kern-bug-people
>State: closed
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Sun Apr 14 03:40:00 +0000 2024
>Closed-Date: Mon Apr 22 08:00:45 +0000 2024
>Last-Modified: Sun Apr 20 22:35:07 +0000 2025
>Originator: PHO
>Release: 10.0
>Organization:
>Environment:
NetBSD yukari.cielonegro.org 10.0 NetBSD 10.0 (GENERIC64) #0: Thu Mar 28 08:33:33 UTC 2024 mkrepro@mkrepro.NetBSD.org:/usr/src/sys/arch/evbarm/compile/GENERIC64 evbarm
>Description:
Copied from the commit message of my patch to fix it:
aarch64: Don't trip up on misaligned SP when a signal is sent to a process
1. sendsig_siginfo() previously assumed that user SP was always aligned to
16 bytes and could call signal handlers with SP misaligned. This is a
wrong assumption because aarch64 demands that SP is aligned *only while*
it's being used to access memory. Now it properly aligns it before
pusing anything on the stack.
2. cpu_mcontext_validate() used to check if _REG_SP was aligned and
considered the ucontext invalid otherwise. This meant if a signal was
sent to a process whose SP was misaligned, the signal handler would fail
to return because the ucontext passed from the kernel was an invalid
one. Now setcontext(2) doesn't complain about misaligned SP.
>How-To-Repeat:
>Fix:
Apply this patch:
https://github.com/NetBSD/src/commit/f672806f26384c093c73b4ee0fb94f1fa309ca4a
>Release-Note:
>Audit-Trail:
From: PHO <pho@cielonegro.org>
To: gnats-bugs@netbsd.org
Cc:
Subject: Re: kern/58149
Date: Sun, 14 Apr 2024 12:46:26 +0900
This was the cause of Xvnc mysteriously exiting that I mentioned in
http://mail-index.netbsd.org/netbsd-users/2024/04/12/msg030915.html
Okay to commit? This would need a pullup.
From: Nick Hudson <nick.hudson@gmx.co.uk>
To: gnats-bugs@netbsd.org, kern-bug-people@netbsd.org,
gnats-admin@netbsd.org, netbsd-bugs@netbsd.org
Cc:
Subject: Re: kern/58149: aarch64: Cannot return from a signal handler if SP
was misaligned when the signal arrived
Date: Sun, 14 Apr 2024 08:31:16 +0100
Hi,
On 14/04/2024 04:40, pho@cielonegro.org wrote:
[...]
>> Synopsis: aarch64: Cannot return from a signal handler if SP was =
misaligned when the signal arrived
Good catch.
>> Fix:
> Apply this patch:
> https://github.com/NetBSD/src/commit/f672806f26384c093c73b4ee0fb94f1fa30=
9ca4a
sp -=3D sizeof(ucontext_t);
sp -=3D roundup(sizeof(ucontext_t), 16);
I'd prefer a compile time assert for the sizeof of ucontext_t... In fact
it already exists in cpu_machdep.c
https://nxr.netbsd.org/xref/src/sys/arch/aarch64/aarch64/cpu_machdep.c#170
Thanks,
Nick
From: PHO <pho@cielonegro.org>
To: gnats-bugs@netbsd.org
Cc:
Subject: Re: kern/58149: aarch64: Cannot return from a signal handler if SP
was misaligned when the signal arrived
Date: Sun, 14 Apr 2024 16:58:44 +0900
On 4/14/24 16:35, Nick Hudson wrote:
> I'd prefer a compile time assert for the sizeof of ucontext_t... In fact
> it already exists in cpu_machdep.c
Agreed. Updated my patch:
https://github.com/NetBSD/src/commit/a8c6eeaee981dddb4b3d9cfca823df2ed857998d
From: Nick Hudson <nick.hudson@gmx.co.uk>
To: gnats-bugs@netbsd.org, kern-bug-people@netbsd.org,
gnats-admin@netbsd.org, netbsd-bugs@netbsd.org, pho@cielonegro.org
Cc:
Subject: Re: kern/58149: aarch64: Cannot return from a signal handler if SP
was misaligned when the signal arrived
Date: Sun, 14 Apr 2024 13:58:32 +0100
On 14/04/2024 09:00, PHO wrote:
> The following reply was made to PR kern/58149; it has been noted by GNAT=
S.
>
> From: PHO <pho@cielonegro.org>
> To: gnats-bugs@netbsd.org
> Cc:
> Subject: Re: kern/58149: aarch64: Cannot return from a signal handler if=
SP
> was misaligned when the signal arrived
> Date: Sun, 14 Apr 2024 16:58:44 +0900
>
> On 4/14/24 16:35, Nick Hudson wrote:
>
> > I'd prefer a compile time assert for the sizeof of ucontext_t... I=
n fact
> > it already exists in cpu_machdep.c
>
> Agreed. Updated my patch:
> https://github.com/NetBSD/src/commit/a8c6eeaee981dddb4b3d9cfca823df2ed=
857998d
>
I committed the kernel side patch.
It'd be good to make the test MI. For example, there is
tests/lib/csu/arch/*/h_initfini_align.S
Nick
From: PHO <pho@cielonegro.org>
To: gnats-bugs@netbsd.org
Cc:
Subject: Re: kern/58149: aarch64: Cannot return from a signal handler if SP
was misaligned when the signal arrived
Date: Tue, 16 Apr 2024 20:57:14 +0900
On 4/14/24 22:00, Nick Hudson wrote:
>
> I committed the kernel side patch.
>
> It'd be good to make the test MI. For example, there is
>
> tests/lib/csu/arch/*/h_initfini_align.S
Done. It's skipped on any architectures other than aarch64 for now:
https://github.com/depressed-pho/netbsd-src/commit/8c4bc1d3bc2f71b76f22cfd98cef7852ec0b16f4
State-Changed-From-To: open->closed
State-Changed-By: pho@NetBSD.org
State-Changed-When: Mon, 22 Apr 2024 08:00:45 +0000
State-Changed-Why:
Test is now comitted. skrll@ requested a pull-up and it's been done.
From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc:
Subject: PR/58149 CVS commit: src
Date: Sun, 20 Apr 2025 22:31:01 +0000
Module Name: src
Committed By: riastradh
Date: Sun Apr 20 22:31:01 UTC 2025
Modified Files:
src/distrib/sets/lists/debug: mi
src/distrib/sets/lists/tests: mi
src/tests/kernel: Makefile t_signal_and_sp.c
src/tests/kernel/arch/aarch64: stack_pointer.h
Added Files:
src/tests/kernel: h_execsp.c h_execsp.h
src/tests/kernel/arch/aarch64: execsp.S signalsphandler.S
src/tests/kernel/arch/x86_64: execsp.S signalsphandler.S
stack_pointer.h
Log Message:
Test stack pointer alignment in various scenarios.
1. elf entry point
2. main function
3. signal handler
Extend the test to amd64 while here -- fortunately both aarch64 and
amd64 pass, but others, such as mips, will fail:
PR kern/59327: user stack pointer is not aligned properly
This extends the test that was previously written for:
PR kern/58149: aarch64: Cannot return from a signal handler if SP was
misaligned when the signal arrived
With any luck, this will help us to systematically eradicate misaligned
stack pointers as hypothesized to be the reason for:
PR port-mips/59236: Multiple segfaults in erlite3 boot
To generate a diff of this commit:
cvs rdiff -u -r1.476 -r1.477 src/distrib/sets/lists/debug/mi
cvs rdiff -u -r1.1369 -r1.1370 src/distrib/sets/lists/tests/mi
cvs rdiff -u -r1.87 -r1.88 src/tests/kernel/Makefile
cvs rdiff -u -r0 -r1.1 src/tests/kernel/h_execsp.c \
src/tests/kernel/h_execsp.h
cvs rdiff -u -r1.1 -r1.2 src/tests/kernel/t_signal_and_sp.c
cvs rdiff -u -r0 -r1.1 src/tests/kernel/arch/aarch64/execsp.S \
src/tests/kernel/arch/aarch64/signalsphandler.S
cvs rdiff -u -r1.1 -r1.2 src/tests/kernel/arch/aarch64/stack_pointer.h
cvs rdiff -u -r0 -r1.1 src/tests/kernel/arch/x86_64/execsp.S \
src/tests/kernel/arch/x86_64/signalsphandler.S \
src/tests/kernel/arch/x86_64/stack_pointer.h
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc:
Subject: PR/58149 CVS commit: src/tests/kernel
Date: Sun, 20 Apr 2025 22:31:58 +0000
Module Name: src
Committed By: riastradh
Date: Sun Apr 20 22:31:58 UTC 2025
Modified Files:
src/tests/kernel: t_signal_and_sp.c
Added Files:
src/tests/kernel/arch/alpha: execsp.S signalsphandler.S stack_pointer.h
Log Message:
t_signal_and_sp: Add alpha support.
Turns out alpha too gets confused by by misaligned sigaltstack, and
by misaligned sp in the interrupted code, when a signal is delivered.
PR kern/59327: user stack pointer is not aligned properly
PR kern/58149: Cannot return from a signal handler if SP was
misaligned when the signal arrived
To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/tests/kernel/t_signal_and_sp.c
cvs rdiff -u -r0 -r1.1 src/tests/kernel/arch/alpha/execsp.S \
src/tests/kernel/arch/alpha/signalsphandler.S \
src/tests/kernel/arch/alpha/stack_pointer.h
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
From: "Taylor R Campbell" <riastradh@netbsd.org>
To: gnats-bugs@gnats.NetBSD.org
Cc:
Subject: PR/58149 CVS commit: src
Date: Sun, 20 Apr 2025 22:32:26 +0000
Module Name: src
Committed By: riastradh
Date: Sun Apr 20 22:32:25 UTC 2025
Modified Files:
src/sys/arch/mips/include: mips_param.h
src/tests/kernel: Makefile t_signal_and_sp.c
Added Files:
src/tests/kernel/arch/mips: execsp.S signalsphandler.S stack_pointer.h
Log Message:
t_signal_and_sp: Add mips support.
PR kern/59327: user stack pointer is not aligned properly
PR kern/58149: Cannot return from a signal handler if SP was
misaligned when the signal arrived
Stack pointer misaligment in some cases hypothesized to be a possible
cause of:
PR port-evbmips/59236: Multiple segfaults in erlite3 boot
To generate a diff of this commit:
cvs rdiff -u -r1.52 -r1.53 src/sys/arch/mips/include/mips_param.h
cvs rdiff -u -r1.88 -r1.89 src/tests/kernel/Makefile
cvs rdiff -u -r1.4 -r1.5 src/tests/kernel/t_signal_and_sp.c
cvs rdiff -u -r0 -r1.1 src/tests/kernel/arch/mips/execsp.S \
src/tests/kernel/arch/mips/signalsphandler.S \
src/tests/kernel/arch/mips/stack_pointer.h
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
>Unformatted:
(Contact us)
$NetBSD: query-full-pr,v 1.51 2026/08/10 02:28:17 riastradh Exp $
$NetBSD: gnats_config.sh,v 1.10 2026/05/13 22:00:09 riastradh Exp $
Copyright © 1994-2026
The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.