security fix: check that cancel/rsyscall signal was sent by the process itself
[musl] / src / thread / pthread_create.c
1 #include "pthread_impl.h"
2
3 void __pthread_unwind_next(struct __ptcb *cb)
4 {
5         int i, j, not_finished;
6         pthread_t self;
7
8         if (cb->__next) longjmp((void *)cb->__next->__jb, 1);
9
10         self = pthread_self();
11         if (self->cancel) self->result = PTHREAD_CANCELLED;
12
13         LOCK(&self->exitlock);
14
15         not_finished = self->tsd_used;
16         for (j=0; not_finished && j<PTHREAD_DESTRUCTOR_ITERATIONS; j++) {
17                 not_finished = 0;
18                 for (i=0; i<PTHREAD_KEYS_MAX; i++) {
19                         if (self->tsd[i] && libc.tsd_keys[i]) {
20                                 void *tmp = self->tsd[i];
21                                 self->tsd[i] = 0;
22                                 libc.tsd_keys[i](tmp);
23                                 not_finished = 1;
24                         }
25                 }
26         }
27
28         syscall4(__NR_rt_sigprocmask, SIG_BLOCK, (long)(uint64_t[1]){-1},0,8);
29
30         if (!a_fetch_add(&libc.threads_minus_1, -1))
31                 exit(0);
32
33         if (self->detached && self->map_base)
34                 __unmapself(self->map_base, self->map_size);
35
36         __syscall_exit(0);
37 }
38
39 static void docancel(struct pthread *self)
40 {
41         struct __ptcb cb = { .__next = self->cancelbuf };
42         __pthread_unwind_next(&cb);
43 }
44
45 static void cancel_handler(int sig, siginfo_t *si, void *ctx)
46 {
47         struct pthread *self = __pthread_self();
48         if (si->si_code > 0 || si->si_pid != self->pid) return;
49         self->cancel = 1;
50         if (self->canceldisable || (!self->cancelasync && !self->cancelpoint))
51                 return;
52         docancel(self);
53 }
54
55 static void cancelpt(int x)
56 {
57         struct pthread *self = __pthread_self();
58         if (self->canceldisable) return;
59         self->cancelpoint = x;
60         if (self->cancel) docancel(self);
61 }
62
63 /* "rsyscall" is a mechanism by which a thread can synchronously force all
64  * other threads to perform an arbitrary syscall. It is necessary to work
65  * around the non-conformant implementation of setuid() et al on Linux,
66  * which affect only the calling thread and not the whole process. This
67  * implementation performs some tricks with signal delivery to work around
68  * the fact that it does not keep any list of threads in userspace. */
69
70 static struct {
71         volatile int lock, hold, blocks, cnt;
72         unsigned long arg[6];
73         int nr;
74         int err;
75 } rs;
76
77 static void rsyscall_handler(int sig, siginfo_t *si, void *ctx)
78 {
79         if (si->si_code > 0 || si->si_pid != __pthread_self()->pid) return;
80
81         if (rs.cnt == libc.threads_minus_1) return;
82
83         if (syscall6(rs.nr, rs.arg[0], rs.arg[1], rs.arg[2],
84                 rs.arg[3], rs.arg[4], rs.arg[5]) < 0 && !rs.err) rs.err=errno;
85
86         a_inc(&rs.cnt);
87         __wake(&rs.cnt, 1, 1);
88         while(rs.hold)
89                 __wait(&rs.hold, 0, 1, 1);
90         a_dec(&rs.cnt);
91         if (!rs.cnt) __wake(&rs.cnt, 1, 1);
92 }
93
94 static int rsyscall(int nr, long a, long b, long c, long d, long e, long f)
95 {
96         int i, ret;
97         sigset_t set = { 0 };
98         struct pthread *self = __pthread_self();
99         sigaddset(&set, SIGSYSCALL);
100
101         LOCK(&rs.lock);
102         while ((i=rs.blocks))
103                 __wait(&rs.blocks, 0, i, 1);
104
105         __libc_sigprocmask(SIG_BLOCK, &set, 0);
106
107         rs.nr = nr;
108         rs.arg[0] = a; rs.arg[1] = b;
109         rs.arg[2] = c; rs.arg[3] = d;
110         rs.arg[4] = d; rs.arg[5] = f;
111         rs.hold = 1;
112         rs.err = 0;
113         rs.cnt = 0;
114
115         /* Dispatch signals until all threads respond */
116         for (i=libc.threads_minus_1; i; i--)
117                 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
118         while ((i=rs.cnt) < libc.threads_minus_1) {
119                 sigqueue(self->pid, SIGSYSCALL, (union sigval){0});
120                 __wait(&rs.cnt, 0, i, 1);
121         }
122
123         /* Handle any lingering signals with no-op */
124         __libc_sigprocmask(SIG_UNBLOCK, &set, 0);
125
126         /* Resume other threads' signal handlers and wait for them */
127         rs.hold = 0;
128         __wake(&rs.hold, -1, 0);
129         while((i=rs.cnt)) __wait(&rs.cnt, 0, i, 1);
130
131         if (rs.err) errno = rs.err, ret = -1;
132         else ret = syscall6(nr, a, b, c, d, e, f);
133
134         UNLOCK(&rs.lock);
135         return ret;
136 }
137
138 static void init_threads()
139 {
140         struct sigaction sa = { .sa_flags = SA_SIGINFO | SA_RESTART };
141         libc.lock = __lock;
142         libc.cancelpt = cancelpt;
143         libc.rsyscall = rsyscall;
144         sa.sa_sigaction = cancel_handler;
145         __libc_sigaction(SIGCANCEL, &sa, 0);
146         sigaddset(&sa.sa_mask, SIGSYSCALL);
147         sigaddset(&sa.sa_mask, SIGCANCEL);
148         sa.sa_sigaction = rsyscall_handler;
149         __libc_sigaction(SIGSYSCALL, &sa, 0);
150         sigprocmask(SIG_UNBLOCK, &sa.sa_mask, 0);
151 }
152
153 static int start(void *p)
154 {
155         struct pthread *self = p;
156         pthread_exit(self->start(self->start_arg));
157         return 0;
158 }
159
160 int __uniclone(void *, int (*)(), void *);
161
162 #define ROUND(x) (((x)+PAGE_SIZE-1)&-PAGE_SIZE)
163
164 /* pthread_key_create.c overrides this */
165 static const size_t dummy = 0;
166 weak_alias(dummy, __pthread_tsd_size);
167
168 int pthread_create(pthread_t *res, const pthread_attr_t *attr, void *(*entry)(void *), void *arg)
169 {
170         static int init;
171         int ret;
172         size_t size, guard;
173         struct pthread *self = pthread_self(), *new;
174         unsigned char *map, *stack, *tsd;
175         static const pthread_attr_t default_attr;
176
177         if (!self) return errno = ENOSYS;
178         if (!init && ++init) init_threads();
179
180         if (!attr) attr = &default_attr;
181         guard = ROUND(attr->_a_guardsize + DEFAULT_GUARD_SIZE);
182         size = guard + ROUND(attr->_a_stacksize + DEFAULT_STACK_SIZE);
183         size += __pthread_tsd_size;
184         map = mmap(0, size, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE|MAP_ANON, -1, 0);
185         if (!map) return EAGAIN;
186         mprotect(map, guard, PROT_NONE);
187
188         tsd = map + size - __pthread_tsd_size;
189         new = (void *)(tsd - sizeof *new - PAGE_SIZE%sizeof *new);
190         new->map_base = map;
191         new->map_size = size;
192         new->pid = self->pid;
193         new->errno_ptr = &new->errno_val;
194         new->start = entry;
195         new->start_arg = arg;
196         new->self = new;
197         new->tsd = (void *)tsd;
198         new->detached = attr->_a_detach;
199         new->attr = *attr;
200         memcpy(new->tlsdesc, self->tlsdesc, sizeof new->tlsdesc);
201         new->tlsdesc[1] = (uintptr_t)new;
202         stack = (void *)((uintptr_t)new-1 & ~(uintptr_t)15);
203
204         /* We must synchronize new thread creation with rsyscall
205          * delivery. This looks to be the least expensive way: */
206         a_inc(&rs.blocks);
207         while (rs.lock) __wait(&rs.lock, 0, 1, 1);
208
209         a_inc(&libc.threads_minus_1);
210         ret = __uniclone(stack, start, new);
211
212         a_dec(&rs.blocks);
213         if (rs.lock) __wake(&rs.blocks, 1, 1);
214
215         if (ret < 0) {
216                 a_dec(&libc.threads_minus_1);
217                 munmap(map, size);
218                 return EAGAIN;
219         }
220         *res = new;
221         return 0;
222 }
223
224 void pthread_exit(void *result)
225 {
226         struct pthread *self = pthread_self();
227         self->result = result;
228         docancel(self);
229 }