linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* sigaltstack breaks swapcontext()
@ 2016-01-06 15:45 Stas Sergeev
  2016-01-06 18:05 ` Andy Lutomirski
  0 siblings, 1 reply; 20+ messages in thread
From: Stas Sergeev @ 2016-01-06 15:45 UTC (permalink / raw)
  To: Linux kernel; +Cc: Andy Lutomirski

Hello.

swapcontext() can be used with signal handlers,
it swaps the signal masks together with the other
parts of the context.
Unfortunately, linux implements the sigaltstack()
in a way that makes it impossible to use with
swapcontext().
Per the man page, sigaltstack is allowed to return
EPERM if the process is altering its sigaltstack while
running on sigaltstack. This is likely needed to
consistently return oss->ss_flags, that indicates
whether the process is being on sigaltstack or not.
Unfortunately, linux takes that permission to return
EPERM too literally: it returns EPERM even if you
don't want to change to another sigaltstack, but
only want to disable sigaltstack with SS_DISABLE.
To my reading of a man page, this is not a desired
behaviour. Moreover, you can't use swapcontext()
without disabling sigaltstack first, or the stack will
be re-used and overwritten by a subsequent signal.

The work-around from this, is not even trivial: I have
to use the shm tricks to duplicate the sigaltstack in
the VA space, and move the stack pointer to another
mirror before calling sigaltstack. Then I use longjmp()
to restore the stack pointer. Then I can finally use
swapcontext(). This is an unpleasant work-around.

The fix on a kernel side looks simple: kernel should
just use ss_flags to determine whether the sigaltstack
is active. I can make a patch for that, but the problem
is that the arch-specific code is not using any helper
function to check for sigaltstack; instead it just uses
"if (ss_size)" checks. So the patch will need to update
all arches... I wonder if maybe someone can fix that
problem and update the arch-specific code. If not,
I'll probably need to update only the x86-specific code
and add an arch-specific define, which is a bit nasty.

Any suggestions?

^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2016-03-07 21:45 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-01-06 15:45 sigaltstack breaks swapcontext() Stas Sergeev
2016-01-06 18:05 ` Andy Lutomirski
2016-01-06 18:42   ` Stas Sergeev
2016-01-06 19:14     ` Andy Lutomirski
2016-01-06 19:31       ` Stas Sergeev
2016-01-06 19:53         ` Andy Lutomirski
2016-01-07 15:33           ` Stas Sergeev
2016-01-07 17:23             ` Andy Lutomirski
2016-01-07 19:10               ` Stas Sergeev
2016-01-08 13:49   ` Stas Sergeev
2016-01-08 23:24     ` Andy Lutomirski
2016-01-08 23:40       ` Stas Sergeev
2016-01-09  1:43       ` Stas Sergeev
2016-01-09  1:48         ` Andy Lutomirski
2016-03-07 20:02           ` Stas Sergeev
2016-03-07 21:10             ` Andy Lutomirski
2016-03-07 21:20               ` Stas Sergeev
2016-03-07 21:32                 ` Andy Lutomirski
2016-03-07 21:38                   ` One Thousand Gnomes
2016-03-07 21:45                   ` Stas Sergeev

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).