Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20200810163448.GB10312@voyager>
Date: Mon, 10 Aug 2020 18:34:48 +0200
From: Markus Wichmann <nullplan@....net>
To: musl@...ts.openwall.com
Subject: Re: Revisiting sigaltstack and implementation-internal signals

On Sun, Aug 09, 2020 at 08:06:22PM -0400, Rich Felker wrote:
> My understanding is that SA_ONSTACK is just reported by the kernel if
> the current stack pointer is inside the alternate stack. If the
> application has moved off that stack and a signal arrives, it has
> nowhere to know "where in the alternate stack it was" or that the
> alternate stack was even already in use, and clobbers it from the
> beginning if a new signal arrives that is to execute on the alternate
> stack.
>
> If you think this understanding is incorrect, we should research/test.
>
> Rich

I thought the kernel would set the flag SS_ONSTACK in the sigaltstack
flags when switching to the altstack, and only remove it through
sigaltstack() or sigreturn(). However, this was merely based on what
understanding I managed to absorb from the manpages. So I had a look at
the Linux kernel code, specifically signal delivery. Now, that is
arch-specific code. But I had a look at two architectures and in both of
them you are right, so I guess the other architectures will handle it
the same way.

There is a function called get_sigframe(), that is present both in
arch/x86/kernel/signal.c and arch/powerpc/kernel/signal.c. It determines
where the signal frame is. In the x86 version, we can see that it will
choose the current stack by default, but it will choose the top of the
altstack if SA_ONSTACK is set in the sigaction flags and the current
ss_flags are 0. How are those calculated? Partly by calling
on_sig_stack(), which will test if the current SP is on the signal
stack. So it is not a sticky note in the kernel, it is indeed calculated
from SP.

In the PowerPC version, get_sigframe() calls sigsp(), which basically
does the same thing as above, but streamlined.

So yeah, if you are executing on altstack, and then you call a coroutine
on another stack, you just lost your altstack contents. And if another
signal arrives, the altstack will be clobbered again. Therefore mixing
coroutines and altstack is not safe, unless all signals with SA_ONSTACK
are blocked while the altstack is live but not in active use. Which is
impossible once musl uses altstack as well.

Ciao,
Markus

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.