diff options
author | Rich Felker <dalias@aerifal.cx> | 2011-03-09 20:07:24 -0500 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2011-03-09 20:07:24 -0500 |
commit | 0bed7e0acfd34e3fb63ca0e4d99b7592571355a9 (patch) | |
tree | 3fe63a6e5ab2227639ed4154a8449c21819afeac | |
parent | 370f78f2c80c64b7b0780a01e672494a26b5678e (diff) | |
download | musl-0bed7e0acfd34e3fb63ca0e4d99b7592571355a9.tar.gz |
fix race condition in raise - just mask signals
a signal handler could fork after the pid/tid were read, causing the
wrong process to be signalled. i'm not sure if this is supposed to
have UB or not, but raise is async-signal-safe, so it probably is
allowed. the current solution is slightly expensive so this
implementation is likely to be changed in the future.
-rw-r--r-- | src/signal/raise.c | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/src/signal/raise.c b/src/signal/raise.c index f437d23f..cc2b19b7 100644 --- a/src/signal/raise.c +++ b/src/signal/raise.c @@ -2,17 +2,17 @@ #include <errno.h> #include "syscall.h" +int __sigprocmask(int, const sigset_t *, sigset_t *); + int raise(int sig) { int pid, tid, ret; - /* Getting the pid/tid pair is not atomic, and could give wrong - * result if a fork occurs in a signal handler between the two - * syscalls. Use the tgkill syscall's ESRCH semantics to detect - * this condition and retry. */ - do { - tid = syscall0(__NR_gettid); - pid = syscall0(__NR_getpid); - ret = syscall3(__NR_tgkill, pid, tid, sig); - } while (ret<0 && errno == ESRCH); + sigset_t set; + sigfillset(&set); + __sigprocmask(SIG_BLOCK, &set, &set); + tid = syscall0(__NR_gettid); + pid = syscall0(__NR_getpid); + ret = syscall3(__NR_tgkill, pid, tid, sig); + __sigprocmask(SIG_SETMASK, &set, 0); return ret; } |