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:  Mon Apr 22 08:00:45 +0000 2024
>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.


>Unformatted:

NetBSD Home
NetBSD PR Database Search

(Contact us) $NetBSD: query-full-pr,v 1.47 2022/09/11 19:34:41 kim Exp $
$NetBSD: gnats_config.sh,v 1.9 2014/08/02 14:16:04 spz Exp $
Copyright © 1994-2024 The NetBSD Foundation, Inc. ALL RIGHTS RESERVED.