overhaul implementation-internal signal protections
[musl] / src / thread / __rsyscall.c
1 #include "pthread_impl.h"
2
3 /* "rsyscall" is a mechanism by which a thread can synchronously force all
4  * other threads to perform an arbitrary syscall. It is necessary to work
5  * around the non-conformant implementation of setuid() et al on Linux,
6  * which affect only the calling thread and not the whole process. This
7  * implementation performs some tricks with signal delivery to work around
8  * the fact that it does not keep any list of threads in userspace. */
9
10 static struct {
11         volatile int lock, hold, blocks, cnt;
12         unsigned long arg[6];
13         int nr;
14         int err;
15         int init;
16 } rs;
17
18 static void rsyscall_handler(int sig, siginfo_t *si, void *ctx)
19 {
20         struct pthread *self = __pthread_self();
21         long r;
22
23         if (!rs.hold || rs.cnt == libc.threads_minus_1) return;
24
25         /* Threads which have already decremented themselves from the
26          * thread count must not increment rs.cnt or otherwise act. */
27         if (self->dead) {
28                 sigfillset(&((ucontext_t *)ctx)->uc_sigmask);
29                 return;
30         }
31
32         r = __syscall(rs.nr, rs.arg[0], rs.arg[1],
33                 rs.arg[2], rs.arg[3], rs.arg[4], rs.arg[5]);
34         if (r < 0) rs.err=-r;
35
36         a_inc(&rs.cnt);
37         __wake(&rs.cnt, 1, 1);
38         while(rs.hold)
39                 __wait(&rs.hold, 0, 1, 1);
40         a_dec(&rs.cnt);
41         if (!rs.cnt) __wake(&rs.cnt, 1, 1);
42 }
43
44 int __rsyscall(int nr, long a, long b, long c, long d, long e, long f)
45 {
46         int i, ret;
47         sigset_t set = { 0 };
48         struct pthread *self;
49
50         if (!libc.threads_minus_1)
51                 return syscall(nr, a, b, c, d, e, f);
52
53         self = __pthread_self();
54
55         LOCK(&rs.lock);
56         while ((i=rs.blocks))
57                 __wait(&rs.blocks, 0, i, 1);
58
59         __syscall(SYS_rt_sigprocmask, SIG_BLOCK, (uint64_t[]){-1}, &set, 8);
60
61         if (!rs.init) {
62                 struct sigaction sa = {
63                         .sa_flags = SA_SIGINFO | SA_RESTART,
64                         .sa_sigaction = rsyscall_handler,
65                         .sa_mask = set
66                 };
67                 sigfillset(&sa.sa_mask);
68                 sa.sa_sigaction = rsyscall_handler;
69                 __libc_sigaction(SIGSYSCALL, &sa, 0);
70                 rs.init = 1;
71         }
72
73         rs.nr = nr;
74         rs.arg[0] = a; rs.arg[1] = b;
75         rs.arg[2] = c; rs.arg[3] = d;
76         rs.arg[4] = d; rs.arg[5] = f;
77         rs.err = 0;
78         rs.cnt = 0;
79         rs.hold = 1;
80
81         /* Dispatch signals until all threads respond */
82         for (i=libc.threads_minus_1; i; i--)
83                 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
84         while ((i=rs.cnt) < libc.threads_minus_1) {
85                 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
86                 __wait(&rs.cnt, 0, i, 1);
87         }
88
89         /* Handle any lingering signals with no-op */
90         __syscall(SYS_rt_sigprocmask, SIG_SETMASK, &set, &set, 8);
91
92         /* Resume other threads' signal handlers and wait for them */
93         rs.hold = 0;
94         __wake(&rs.hold, -1, 0);
95         while((i=rs.cnt)) __wait(&rs.cnt, 0, i, 1);
96
97         if (rs.err) errno = rs.err, ret = -1;
98         else ret = syscall(nr, a, b, c, d, e, f);
99
100         UNLOCK(&rs.lock);
101         return ret;
102 }
103
104 void __rsyscall_lock()
105 {
106         a_inc(&rs.blocks);
107         while (rs.lock) __wait(&rs.lock, 0, 1, 1);
108 }
109
110 void __rsyscall_unlock()
111 {
112         a_dec(&rs.blocks);
113         if (rs.lock) __wake(&rs.blocks, 1, 1);
114 }