mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Markus Wichmann <nullplan@gmx.net>
To: musl@lists.openwall.com
Subject: Re: [musl] Revisiting sigaltstack and implementation-internal signals
Date: Mon, 10 Aug 2020 18:34:48 +0200	[thread overview]
Message-ID: <20200810163448.GB10312@voyager> (raw)
In-Reply-To: <20200810000622.GF3265@brightrain.aerifal.cx>

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

  reply	other threads:[~2020-08-10 16:35 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-09  0:39 Rich Felker
2020-08-09  7:54 ` Markus Wichmann
2020-08-10  0:06   ` Rich Felker
2020-08-10 16:34     ` Markus Wichmann [this message]
2020-08-10  0:10   ` Ariadne Conill
2020-08-10  0:26     ` Rich Felker
2020-08-10  0:28 ` Ariadne Conill
2020-08-10  8:15   ` Olaf Flebbe
2020-08-10 15:41     ` Szabolcs Nagy
2020-08-10 15:45       ` Olaf Flebbe
2020-08-10 16:24         ` Szabolcs Nagy
2020-08-10 16:27         ` Rich Felker
2020-08-10 16:36     ` Rich Felker
2020-08-10 16:57       ` Olaf Flebbe
2020-08-10 17:00         ` Rich Felker
2020-08-10 17:04           ` Olaf Flebbe
2020-08-10 18:32             ` Rich Felker
2020-08-10 19:29               ` Olaf Flebbe

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200810163448.GB10312@voyager \
    --to=nullplan@gmx.net \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).