Skip to contentSkip to conversation

riscv: signal: fix sigaltstack frame size checking

PATCH Applied Andy Chiu 8 participants 8 messages Aug 22, 2023 – Oct 15, 2023 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

Andy Chiu

author
#0 lore raw

PATCH 1/1 riscv: signal: fix sigaltstack frame size checking

The alternative stack checking in get_sigframe introduced by the Vector support is not needed and has a problem. It is not needed as we have already validate it at the beginning of the function if we are already on an altstack. If not, the size of an altstack is always validated at its allocation stage with sigaltstack_size_valid().

Besides, we must only regard the size of an altstack if the handler of a signal is registered with SA_ONSTACK. So, blindly checking overflow of an altstack if sas_ss_size not equals to zero will check against wrong signal handlers if only a subset of signals are registered with SA_ONSTACK.

Fixes 8ee0b41898fa ("riscv: signal: Add sigcontext save/restore for vector")
---
 arch/riscv/kernel/signal.c | 7 -------
 1 file changed, 7 deletions(-)
arch/riscv/kernel/signal.c +0 -7
index 180d951d3624..21a4d0e111bc 100644
@@ -311,13 +311,6 @@ static inline void __user *get_sigframe(struct ksignal *ksig,
 	/* Align the stack frame. */
 	sp &= ~0xfUL;
 
-	/*
-	 * Fail if the size of the altstack is not large enough for the
-	 * sigframe construction.
-	 */
-	if (current->sas_ss_size && sp < current->sas_ss_sp)
-		return (void __user __force *)-1UL;
-
 	return (void __user *)sp;
 }
 
Signature · 7 lines
2.17.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

A message that is not in the archive (sent off-list or not yet received): CAHdA9BxbLyvxObiQqW=M-Rh3PaGqWsdVHUuDc_x5ZZqBOUzhcQ@mail.gmail.com

#2 lore raw

Per Andy’s request, replying to this thread with my reproduction instructions and some additional data. I’ll ask for forgiveness in advance for the roundabout reproduction method, I’m not familiar enough with this code to know how to intentionally induce this state - in theory if you can (1) cause a get_sigframe to happen on a thread not ON_SASTACK, and (2) you can ensure that the current sp is less than whatever is being held as the alternate stack’s sp (I’m not sure what it means to reference current->sas_ss_sp when you’re not on the alt stack?), this issue should trigger.

My reproduction method (100% rate):

1. Build Android AOSP phone target (lunch aosp_cf_riscv64_phone-userdebug).
2. Launch the emulator:
https://source.android.com/docs/setup/create/cuttlefish-use
3. After boot, turn WiFi on and connect to ‘VirtWifi’. To watch this error, I attach strace to the networkstack PID at this point.
4. Turn WiFi off. Within a few seconds, the networkstack process will crash with a SIGSEGV.

Dumping out the variables at the crash location:

```
regs->sp: 72057560689527184
sigsp: 72057560689527184
framesize: 1088
current->sas_ss_size: 32768
sp: 72057560689526096
current->sas_ss_sp: 72057570417491968
```

As we can see, sp < current->sas_ss_sp and thus triggers the check. What's noticeable here though is that regs->sp and sigsp are identical, which means that when sigsp(sp, ksig) was called, it did not find that it was on the alternate signal stack and simply returned sp as is. If we're not on the alternate signal stack, then this check does not make sense - the relative locations of these two pointers are not guaranteed. We perform a similar check at the top of this function that does validate whether we're on the altstack before attempting the subsequent check.

Quoted 39 lines · Andy Chiu
Quoted 38 lines

The alternative stack checking in get_sigframe introduced by the Vector support is not needed and has a problem. It is not needed as we have already validate it at the beginning of the function if we are already on an altstack. If not, the size of an altstack is always validated at its allocation stage with sigaltstack_size_valid().

Besides, we must only regard the size of an altstack if the handler of a signal is registered with SA_ONSTACK. So, blindly checking overflow of an altstack if sas_ss_size not equals to zero will check against wrong signal handlers if only a subset of signals are registered with SA_ONSTACK.

Fixes 8ee0b41898fa ("riscv: signal: Add sigcontext save/restore for vector")
---
 arch/riscv/kernel/signal.c | 7 -------
 1 file changed, 7 deletions(-)
arch/riscv/kernel/signal.c +0 -7
index 180d951d3624..21a4d0e111bc 100644
@@ -311,13 +311,6 @@ static inline void __user *get_sigframe(struct ksignal *ksig,
        /* Align the stack frame. */
        sp &= ~0xfUL;
 
-       /*
-        * Fail if the size of the altstack is not large enough for the
-        * sigframe construction.
-        */
-       if (current->sas_ss_size && sp < current->sas_ss_sp)
-               return (void __user __force *)-1UL;
-
        return (void __user *)sp;
 }
 
Quoted 3 lines
Quoted 3 lines

--
2.17.1

_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv

#3 lore raw
In reply to Andy Chiu
This patch was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:

Hello:

This patch was applied to riscv/linux.git (for-next) by Palmer Dabbelt <[email protected]>:

Quoted 6 lines · On Tue, 22 Aug 2023 16:49:03 +0000 you wrote:

The alternative stack checking in get_sigframe introduced by the Vector support is not needed and has a problem. It is not needed as we have already validate it at the beginning of the function if we are already on an altstack. If not, the size of an altstack is always validated at its allocation stage with sigaltstack_size_valid().

Quoted 7 lines

Besides, we must only regard the size of an altstack if the handler of a signal is registered with SA_ONSTACK. So, blindly checking overflow of an altstack if sas_ss_size not equals to zero will check against wrong signal handlers if only a subset of signals are registered with SA_ONSTACK.

[...]

Here is the summary with links:
- riscv: signal: fix sigaltstack frame size checking

    https://git.kernel.org/riscv/c/0caa0b447398

You are awesome, thank you!

Signature · 9 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#4 lore raw
In reply to Andy Chiu
Quoted 6 lines · Andy Chiu

The alternative stack checking in get_sigframe introduced by the Vector support is not needed and has a problem. It is not needed as we have already validate it at the beginning of the function if we are already on an altstack. If not, the size of an altstack is always validated at its allocation stage with sigaltstack_size_valid().

Quoted 7 lines

Besides, we must only regard the size of an altstack if the handler of a signal is registered with SA_ONSTACK. So, blindly checking overflow of an altstack if sas_ss_size not equals to zero will check against wrong signal handlers if only a subset of signals are registered with SA_ONSTACK.

[...]

Applied, thanks!

[1/1] riscv: signal: fix sigaltstack frame size checking

      https://git.kernel.org/palmer/c/d77303a57c95

Best regards,

Signature · 7 lines
Palmer Dabbelt <[email protected]>


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
#5 lore raw
Quoted 14 lines · Palmer Dabbelt
Quoted 12 lines · Andy Chiu

The alternative stack checking in get_sigframe introduced by the Vector support is not needed and has a problem. It is not needed as we have already validate it at the beginning of the function if we are already on an altstack. If not, the size of an altstack is always validated at its allocation stage with sigaltstack_size_valid().

Besides, we must only regard the size of an altstack if the handler of a signal is registered with SA_ONSTACK. So, blindly checking overflow of an altstack if sas_ss_size not equals to zero will check against wrong signal handlers if only a subset of signals are registered with SA_ONSTACK.

Quoted 6 lines
Quoted 1 line

[...]

Applied, thanks!

[1/1] riscv: signal: fix sigaltstack frame size checking

      https://git.kernel.org/palmer/c/d77303a57c95

Just wondering: what happened to this patch, which afaics is currently in neither mainline nor next? Because according to
https://bugzilla.kernel.org/show_bug.cgi?id=217923 it fixes rustc userspace crashes with 6.5. Was a different approach found?

Ciao, Thorsten

maintainer
#6 lore raw
Quoted 19 lines · On Mon, 25 Sep 2023 03:07:47 PDT (-0700), [email protected] wrote:
Quoted 18 lines · Palmer Dabbelt
Quoted 6 lines · Andy Chiu

The alternative stack checking in get_sigframe introduced by the Vector support is not needed and has a problem. It is not needed as we have already validate it at the beginning of the function if we are already on an altstack. If not, the size of an altstack is always validated at its allocation stage with sigaltstack_size_valid().

Quoted 7 lines

Besides, we must only regard the size of an altstack if the handler of a signal is registered with SA_ONSTACK. So, blindly checking overflow of an altstack if sas_ss_size not equals to zero will check against wrong signal handlers if only a subset of signals are registered with SA_ONSTACK.

[...]

Applied, thanks!

Quoted 7 lines
Quoted 2 lines

[1/1] riscv: signal: fix sigaltstack frame size checking

      https://git.kernel.org/palmer/c/d77303a57c95

Just wondering: what happened to this patch, which afaics is currently in neither mainline nor next? Because according to
https://bugzilla.kernel.org/show_bug.cgi?id=217923 it fixes rustc userspace crashes with 6.5. Was a different approach found?

We talked about this in the patchwork meeting. I think I just dropped the ball somewhere -- we moved offices and then I was at the cauldron, so things are a bit more hectic than usual.

I got back last night and I'm still a bit out of it. I'm going to try and dig whatever computer I was using out of a moving box and find the actual commit, as I'm kind of worried I might have lost something else as well. Might not be super fast, though, as I've got stuff all over the place and I'm pretty much falling asleep already...

Quoted 2 lines

Ciao, Thorsten

#7 lore raw
In reply to Andy Chiu

Hi,

The patch below is an important fix, as it is necessary to run rustc on riscv. It has been merged as commit 14a270bfab7ab1c4b605c01eeca5557447ad5a2b. I have seen that other commits from the same pull request have already been queued for 6.5, but not this one. Would it be possible to queue it for stable 6.5?

Thanks
Aurelien

Quoted 46 lines · Andy Chiu

The alternative stack checking in get_sigframe introduced by the Vector support is not needed and has a problem. It is not needed as we have already validate it at the beginning of the function if we are already on an altstack. If not, the size of an altstack is always validated at its allocation stage with sigaltstack_size_valid().

Besides, we must only regard the size of an altstack if the handler of a signal is registered with SA_ONSTACK. So, blindly checking overflow of an altstack if sas_ss_size not equals to zero will check against wrong signal handlers if only a subset of signals are registered with SA_ONSTACK.

Fixes 8ee0b41898fa ("riscv: signal: Add sigcontext save/restore for vector")
---
 arch/riscv/kernel/signal.c | 7 -------
 1 file changed, 7 deletions(-)
arch/riscv/kernel/signal.c +0 -7
index 180d951d3624..21a4d0e111bc 100644
@@ -311,13 +311,6 @@ static inline void __user *get_sigframe(struct ksignal *ksig,
 	/* Align the stack frame. */
 	sp &= ~0xfUL;
 
-	/*
-	 * Fail if the size of the altstack is not large enough for the
-	 * sigframe construction.
-	 */
-	if (current->sas_ss_size && sp < current->sas_ss_sp)
-		return (void __user __force *)-1UL;
-
 	return (void __user *)sp;
 }
 
Signature · 7 lines
2.17.1


_______________________________________________
linux-riscv mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-riscv
Signature · 2 lines
Aurelien Jarno                          GPG: 4096R/1DDD8C9B
[email protected]                     http://aurel32.net
maintainer
#8 lore raw
Quoted 6 lines · Aurelien Jarno

Hi,

The patch below is an important fix, as it is necessary to run rustc on riscv. It has been merged as commit 14a270bfab7ab1c4b605c01eeca5557447ad5a2b. I have seen that other commits from the same pull request have already been queued for 6.5, but not this one. Would it be possible to queue it for stable 6.5?

Now queued up, thanks.

greg k-h