]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/kernel/signal.c | |
3 | * | |
4 | * Copyright (C) 1991, 1992 Linus Torvalds | |
5 | * | |
6 | * 1997-11-02 Modified for POSIX.1b signals by Richard Henderson | |
7 | * | |
8 | * 2003-06-02 Jim Houston - Concurrent Computer Corp. | |
9 | * Changes to use preallocated sigqueue structures | |
10 | * to allow signals to be sent reliably. | |
11 | */ | |
12 | ||
1da177e4 | 13 | #include <linux/slab.h> |
9984de1a | 14 | #include <linux/export.h> |
1da177e4 | 15 | #include <linux/init.h> |
589ee628 | 16 | #include <linux/sched/mm.h> |
8703e8a4 | 17 | #include <linux/sched/user.h> |
b17b0153 | 18 | #include <linux/sched/debug.h> |
29930025 | 19 | #include <linux/sched/task.h> |
68db0cf1 | 20 | #include <linux/sched/task_stack.h> |
32ef5517 | 21 | #include <linux/sched/cputime.h> |
1da177e4 LT |
22 | #include <linux/fs.h> |
23 | #include <linux/tty.h> | |
24 | #include <linux/binfmts.h> | |
179899fd | 25 | #include <linux/coredump.h> |
1da177e4 LT |
26 | #include <linux/security.h> |
27 | #include <linux/syscalls.h> | |
28 | #include <linux/ptrace.h> | |
7ed20e1a | 29 | #include <linux/signal.h> |
fba2afaa | 30 | #include <linux/signalfd.h> |
f84d49b2 | 31 | #include <linux/ratelimit.h> |
35de254d | 32 | #include <linux/tracehook.h> |
c59ede7b | 33 | #include <linux/capability.h> |
7dfb7103 | 34 | #include <linux/freezer.h> |
84d73786 SB |
35 | #include <linux/pid_namespace.h> |
36 | #include <linux/nsproxy.h> | |
6b550f94 | 37 | #include <linux/user_namespace.h> |
0326f5a9 | 38 | #include <linux/uprobes.h> |
90268439 | 39 | #include <linux/compat.h> |
2b5faa4c | 40 | #include <linux/cn_proc.h> |
52f5684c | 41 | #include <linux/compiler.h> |
31ea70e0 | 42 | #include <linux/posix-timers.h> |
43347d56 | 43 | #include <linux/livepatch.h> |
52f5684c | 44 | |
d1eb650f MH |
45 | #define CREATE_TRACE_POINTS |
46 | #include <trace/events/signal.h> | |
84d73786 | 47 | |
1da177e4 | 48 | #include <asm/param.h> |
7c0f6ba6 | 49 | #include <linux/uaccess.h> |
1da177e4 LT |
50 | #include <asm/unistd.h> |
51 | #include <asm/siginfo.h> | |
d550bbd4 | 52 | #include <asm/cacheflush.h> |
e1396065 | 53 | #include "audit.h" /* audit_signal_info() */ |
1da177e4 LT |
54 | |
55 | /* | |
56 | * SLAB caches for signal bits. | |
57 | */ | |
58 | ||
e18b890b | 59 | static struct kmem_cache *sigqueue_cachep; |
1da177e4 | 60 | |
f84d49b2 NO |
61 | int print_fatal_signals __read_mostly; |
62 | ||
35de254d | 63 | static void __user *sig_handler(struct task_struct *t, int sig) |
93585eea | 64 | { |
35de254d RM |
65 | return t->sighand->action[sig - 1].sa.sa_handler; |
66 | } | |
93585eea | 67 | |
e4a8b4ef | 68 | static inline bool sig_handler_ignored(void __user *handler, int sig) |
35de254d | 69 | { |
93585eea | 70 | /* Is it explicitly or implicitly ignored? */ |
93585eea | 71 | return handler == SIG_IGN || |
e4a8b4ef | 72 | (handler == SIG_DFL && sig_kernel_ignore(sig)); |
93585eea | 73 | } |
1da177e4 | 74 | |
41aaa481 | 75 | static bool sig_task_ignored(struct task_struct *t, int sig, bool force) |
1da177e4 | 76 | { |
35de254d | 77 | void __user *handler; |
1da177e4 | 78 | |
f008faff ON |
79 | handler = sig_handler(t, sig); |
80 | ||
86989c41 EB |
81 | /* SIGKILL and SIGSTOP may not be sent to the global init */ |
82 | if (unlikely(is_global_init(t) && sig_kernel_only(sig))) | |
83 | return true; | |
84 | ||
f008faff | 85 | if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) && |
ac253850 | 86 | handler == SIG_DFL && !(force && sig_kernel_only(sig))) |
41aaa481 | 87 | return true; |
f008faff ON |
88 | |
89 | return sig_handler_ignored(handler, sig); | |
90 | } | |
91 | ||
6a0cdcd7 | 92 | static bool sig_ignored(struct task_struct *t, int sig, bool force) |
f008faff | 93 | { |
1da177e4 LT |
94 | /* |
95 | * Blocked signals are never ignored, since the | |
96 | * signal handler may change by the time it is | |
97 | * unblocked. | |
98 | */ | |
325d22df | 99 | if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig)) |
6a0cdcd7 | 100 | return false; |
1da177e4 | 101 | |
35de254d | 102 | /* |
628c1bcb ON |
103 | * Tracers may want to know about even ignored signal unless it |
104 | * is SIGKILL which can't be reported anyway but can be ignored | |
105 | * by SIGNAL_UNKILLABLE task. | |
35de254d | 106 | */ |
628c1bcb | 107 | if (t->ptrace && sig != SIGKILL) |
6a0cdcd7 | 108 | return false; |
628c1bcb ON |
109 | |
110 | return sig_task_ignored(t, sig, force); | |
1da177e4 LT |
111 | } |
112 | ||
113 | /* | |
114 | * Re-calculate pending state from the set of locally pending | |
115 | * signals, globally pending signals, and blocked signals. | |
116 | */ | |
938696a8 | 117 | static inline bool has_pending_signals(sigset_t *signal, sigset_t *blocked) |
1da177e4 LT |
118 | { |
119 | unsigned long ready; | |
120 | long i; | |
121 | ||
122 | switch (_NSIG_WORDS) { | |
123 | default: | |
124 | for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;) | |
125 | ready |= signal->sig[i] &~ blocked->sig[i]; | |
126 | break; | |
127 | ||
128 | case 4: ready = signal->sig[3] &~ blocked->sig[3]; | |
129 | ready |= signal->sig[2] &~ blocked->sig[2]; | |
130 | ready |= signal->sig[1] &~ blocked->sig[1]; | |
131 | ready |= signal->sig[0] &~ blocked->sig[0]; | |
132 | break; | |
133 | ||
134 | case 2: ready = signal->sig[1] &~ blocked->sig[1]; | |
135 | ready |= signal->sig[0] &~ blocked->sig[0]; | |
136 | break; | |
137 | ||
138 | case 1: ready = signal->sig[0] &~ blocked->sig[0]; | |
139 | } | |
140 | return ready != 0; | |
141 | } | |
142 | ||
143 | #define PENDING(p,b) has_pending_signals(&(p)->signal, (b)) | |
144 | ||
09ae854e | 145 | static bool recalc_sigpending_tsk(struct task_struct *t) |
1da177e4 | 146 | { |
3759a0d9 | 147 | if ((t->jobctl & JOBCTL_PENDING_MASK) || |
1da177e4 | 148 | PENDING(&t->pending, &t->blocked) || |
7bb44ade | 149 | PENDING(&t->signal->shared_pending, &t->blocked)) { |
1da177e4 | 150 | set_tsk_thread_flag(t, TIF_SIGPENDING); |
09ae854e | 151 | return true; |
7bb44ade | 152 | } |
09ae854e | 153 | |
b74d0deb RM |
154 | /* |
155 | * We must never clear the flag in another thread, or in current | |
156 | * when it's possible the current syscall is returning -ERESTART*. | |
157 | * So we don't clear it here, and only callers who know they should do. | |
158 | */ | |
09ae854e | 159 | return false; |
7bb44ade RM |
160 | } |
161 | ||
162 | /* | |
163 | * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up. | |
164 | * This is superfluous when called on current, the wakeup is a harmless no-op. | |
165 | */ | |
166 | void recalc_sigpending_and_wake(struct task_struct *t) | |
167 | { | |
168 | if (recalc_sigpending_tsk(t)) | |
169 | signal_wake_up(t, 0); | |
1da177e4 LT |
170 | } |
171 | ||
172 | void recalc_sigpending(void) | |
173 | { | |
43347d56 MB |
174 | if (!recalc_sigpending_tsk(current) && !freezing(current) && |
175 | !klp_patch_pending(current)) | |
b74d0deb RM |
176 | clear_thread_flag(TIF_SIGPENDING); |
177 | ||
1da177e4 | 178 | } |
fb50f5a4 | 179 | EXPORT_SYMBOL(recalc_sigpending); |
1da177e4 | 180 | |
088fe47c EB |
181 | void calculate_sigpending(void) |
182 | { | |
183 | /* Have any signals or users of TIF_SIGPENDING been delayed | |
184 | * until after fork? | |
185 | */ | |
186 | spin_lock_irq(¤t->sighand->siglock); | |
187 | set_tsk_thread_flag(current, TIF_SIGPENDING); | |
188 | recalc_sigpending(); | |
189 | spin_unlock_irq(¤t->sighand->siglock); | |
190 | } | |
191 | ||
1da177e4 LT |
192 | /* Given the mask, find the first available signal that should be serviced. */ |
193 | ||
a27341cd LT |
194 | #define SYNCHRONOUS_MASK \ |
195 | (sigmask(SIGSEGV) | sigmask(SIGBUS) | sigmask(SIGILL) | \ | |
a0727e8c | 196 | sigmask(SIGTRAP) | sigmask(SIGFPE) | sigmask(SIGSYS)) |
a27341cd | 197 | |
fba2afaa | 198 | int next_signal(struct sigpending *pending, sigset_t *mask) |
1da177e4 LT |
199 | { |
200 | unsigned long i, *s, *m, x; | |
201 | int sig = 0; | |
f84d49b2 | 202 | |
1da177e4 LT |
203 | s = pending->signal.sig; |
204 | m = mask->sig; | |
a27341cd LT |
205 | |
206 | /* | |
207 | * Handle the first word specially: it contains the | |
208 | * synchronous signals that need to be dequeued first. | |
209 | */ | |
210 | x = *s &~ *m; | |
211 | if (x) { | |
212 | if (x & SYNCHRONOUS_MASK) | |
213 | x &= SYNCHRONOUS_MASK; | |
214 | sig = ffz(~x) + 1; | |
215 | return sig; | |
216 | } | |
217 | ||
1da177e4 LT |
218 | switch (_NSIG_WORDS) { |
219 | default: | |
a27341cd LT |
220 | for (i = 1; i < _NSIG_WORDS; ++i) { |
221 | x = *++s &~ *++m; | |
222 | if (!x) | |
223 | continue; | |
224 | sig = ffz(~x) + i*_NSIG_BPW + 1; | |
225 | break; | |
226 | } | |
1da177e4 LT |
227 | break; |
228 | ||
a27341cd LT |
229 | case 2: |
230 | x = s[1] &~ m[1]; | |
231 | if (!x) | |
1da177e4 | 232 | break; |
a27341cd | 233 | sig = ffz(~x) + _NSIG_BPW + 1; |
1da177e4 LT |
234 | break; |
235 | ||
a27341cd LT |
236 | case 1: |
237 | /* Nothing to do */ | |
1da177e4 LT |
238 | break; |
239 | } | |
f84d49b2 | 240 | |
1da177e4 LT |
241 | return sig; |
242 | } | |
243 | ||
f84d49b2 NO |
244 | static inline void print_dropped_signal(int sig) |
245 | { | |
246 | static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10); | |
247 | ||
248 | if (!print_fatal_signals) | |
249 | return; | |
250 | ||
251 | if (!__ratelimit(&ratelimit_state)) | |
252 | return; | |
253 | ||
747800ef | 254 | pr_info("%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n", |
f84d49b2 NO |
255 | current->comm, current->pid, sig); |
256 | } | |
257 | ||
d79fdd6d | 258 | /** |
7dd3db54 | 259 | * task_set_jobctl_pending - set jobctl pending bits |
d79fdd6d | 260 | * @task: target task |
7dd3db54 | 261 | * @mask: pending bits to set |
d79fdd6d | 262 | * |
7dd3db54 TH |
263 | * Clear @mask from @task->jobctl. @mask must be subset of |
264 | * %JOBCTL_PENDING_MASK | %JOBCTL_STOP_CONSUME | %JOBCTL_STOP_SIGMASK | | |
265 | * %JOBCTL_TRAPPING. If stop signo is being set, the existing signo is | |
266 | * cleared. If @task is already being killed or exiting, this function | |
267 | * becomes noop. | |
268 | * | |
269 | * CONTEXT: | |
270 | * Must be called with @task->sighand->siglock held. | |
271 | * | |
272 | * RETURNS: | |
273 | * %true if @mask is set, %false if made noop because @task was dying. | |
274 | */ | |
b76808e6 | 275 | bool task_set_jobctl_pending(struct task_struct *task, unsigned long mask) |
7dd3db54 TH |
276 | { |
277 | BUG_ON(mask & ~(JOBCTL_PENDING_MASK | JOBCTL_STOP_CONSUME | | |
278 | JOBCTL_STOP_SIGMASK | JOBCTL_TRAPPING)); | |
279 | BUG_ON((mask & JOBCTL_TRAPPING) && !(mask & JOBCTL_PENDING_MASK)); | |
280 | ||
281 | if (unlikely(fatal_signal_pending(task) || (task->flags & PF_EXITING))) | |
282 | return false; | |
283 | ||
284 | if (mask & JOBCTL_STOP_SIGMASK) | |
285 | task->jobctl &= ~JOBCTL_STOP_SIGMASK; | |
286 | ||
287 | task->jobctl |= mask; | |
288 | return true; | |
289 | } | |
290 | ||
d79fdd6d | 291 | /** |
a8f072c1 | 292 | * task_clear_jobctl_trapping - clear jobctl trapping bit |
d79fdd6d TH |
293 | * @task: target task |
294 | * | |
a8f072c1 TH |
295 | * If JOBCTL_TRAPPING is set, a ptracer is waiting for us to enter TRACED. |
296 | * Clear it and wake up the ptracer. Note that we don't need any further | |
297 | * locking. @task->siglock guarantees that @task->parent points to the | |
298 | * ptracer. | |
d79fdd6d TH |
299 | * |
300 | * CONTEXT: | |
301 | * Must be called with @task->sighand->siglock held. | |
302 | */ | |
73ddff2b | 303 | void task_clear_jobctl_trapping(struct task_struct *task) |
d79fdd6d | 304 | { |
a8f072c1 TH |
305 | if (unlikely(task->jobctl & JOBCTL_TRAPPING)) { |
306 | task->jobctl &= ~JOBCTL_TRAPPING; | |
650226bd | 307 | smp_mb(); /* advised by wake_up_bit() */ |
62c124ff | 308 | wake_up_bit(&task->jobctl, JOBCTL_TRAPPING_BIT); |
d79fdd6d TH |
309 | } |
310 | } | |
311 | ||
e5c1902e | 312 | /** |
3759a0d9 | 313 | * task_clear_jobctl_pending - clear jobctl pending bits |
e5c1902e | 314 | * @task: target task |
3759a0d9 | 315 | * @mask: pending bits to clear |
e5c1902e | 316 | * |
3759a0d9 TH |
317 | * Clear @mask from @task->jobctl. @mask must be subset of |
318 | * %JOBCTL_PENDING_MASK. If %JOBCTL_STOP_PENDING is being cleared, other | |
319 | * STOP bits are cleared together. | |
e5c1902e | 320 | * |
6dfca329 TH |
321 | * If clearing of @mask leaves no stop or trap pending, this function calls |
322 | * task_clear_jobctl_trapping(). | |
e5c1902e TH |
323 | * |
324 | * CONTEXT: | |
325 | * Must be called with @task->sighand->siglock held. | |
326 | */ | |
b76808e6 | 327 | void task_clear_jobctl_pending(struct task_struct *task, unsigned long mask) |
e5c1902e | 328 | { |
3759a0d9 TH |
329 | BUG_ON(mask & ~JOBCTL_PENDING_MASK); |
330 | ||
331 | if (mask & JOBCTL_STOP_PENDING) | |
332 | mask |= JOBCTL_STOP_CONSUME | JOBCTL_STOP_DEQUEUED; | |
333 | ||
334 | task->jobctl &= ~mask; | |
6dfca329 TH |
335 | |
336 | if (!(task->jobctl & JOBCTL_PENDING_MASK)) | |
337 | task_clear_jobctl_trapping(task); | |
e5c1902e TH |
338 | } |
339 | ||
340 | /** | |
341 | * task_participate_group_stop - participate in a group stop | |
342 | * @task: task participating in a group stop | |
343 | * | |
a8f072c1 | 344 | * @task has %JOBCTL_STOP_PENDING set and is participating in a group stop. |
39efa3ef | 345 | * Group stop states are cleared and the group stop count is consumed if |
a8f072c1 | 346 | * %JOBCTL_STOP_CONSUME was set. If the consumption completes the group |
39efa3ef | 347 | * stop, the appropriate %SIGNAL_* flags are set. |
e5c1902e TH |
348 | * |
349 | * CONTEXT: | |
350 | * Must be called with @task->sighand->siglock held. | |
244056f9 TH |
351 | * |
352 | * RETURNS: | |
353 | * %true if group stop completion should be notified to the parent, %false | |
354 | * otherwise. | |
e5c1902e TH |
355 | */ |
356 | static bool task_participate_group_stop(struct task_struct *task) | |
357 | { | |
358 | struct signal_struct *sig = task->signal; | |
a8f072c1 | 359 | bool consume = task->jobctl & JOBCTL_STOP_CONSUME; |
e5c1902e | 360 | |
a8f072c1 | 361 | WARN_ON_ONCE(!(task->jobctl & JOBCTL_STOP_PENDING)); |
39efa3ef | 362 | |
3759a0d9 | 363 | task_clear_jobctl_pending(task, JOBCTL_STOP_PENDING); |
e5c1902e TH |
364 | |
365 | if (!consume) | |
366 | return false; | |
367 | ||
368 | if (!WARN_ON_ONCE(sig->group_stop_count == 0)) | |
369 | sig->group_stop_count--; | |
370 | ||
244056f9 TH |
371 | /* |
372 | * Tell the caller to notify completion iff we are entering into a | |
373 | * fresh group stop. Read comment in do_signal_stop() for details. | |
374 | */ | |
375 | if (!sig->group_stop_count && !(sig->flags & SIGNAL_STOP_STOPPED)) { | |
2d39b3cd | 376 | signal_set_stop_flags(sig, SIGNAL_STOP_STOPPED); |
e5c1902e TH |
377 | return true; |
378 | } | |
379 | return false; | |
380 | } | |
381 | ||
924de3b8 EB |
382 | void task_join_group_stop(struct task_struct *task) |
383 | { | |
384 | /* Have the new thread join an on-going signal group stop */ | |
385 | unsigned long jobctl = current->jobctl; | |
386 | if (jobctl & JOBCTL_STOP_PENDING) { | |
387 | struct signal_struct *sig = current->signal; | |
388 | unsigned long signr = jobctl & JOBCTL_STOP_SIGMASK; | |
389 | unsigned long gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME; | |
390 | if (task_set_jobctl_pending(task, signr | gstop)) { | |
391 | sig->group_stop_count++; | |
392 | } | |
393 | } | |
394 | } | |
395 | ||
c69e8d9c DH |
396 | /* |
397 | * allocate a new signal queue record | |
398 | * - this may be called without locks if and only if t == current, otherwise an | |
5aba085e | 399 | * appropriate lock must be held to stop the target task from exiting |
c69e8d9c | 400 | */ |
f84d49b2 NO |
401 | static struct sigqueue * |
402 | __sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit) | |
1da177e4 LT |
403 | { |
404 | struct sigqueue *q = NULL; | |
10b1fbdb | 405 | struct user_struct *user; |
1da177e4 | 406 | |
10b1fbdb | 407 | /* |
7cf7db8d TG |
408 | * Protect access to @t credentials. This can go away when all |
409 | * callers hold rcu read lock. | |
10b1fbdb | 410 | */ |
7cf7db8d | 411 | rcu_read_lock(); |
d84f4f99 | 412 | user = get_uid(__task_cred(t)->user); |
10b1fbdb | 413 | atomic_inc(&user->sigpending); |
7cf7db8d | 414 | rcu_read_unlock(); |
f84d49b2 | 415 | |
1da177e4 | 416 | if (override_rlimit || |
10b1fbdb | 417 | atomic_read(&user->sigpending) <= |
78d7d407 | 418 | task_rlimit(t, RLIMIT_SIGPENDING)) { |
1da177e4 | 419 | q = kmem_cache_alloc(sigqueue_cachep, flags); |
f84d49b2 NO |
420 | } else { |
421 | print_dropped_signal(sig); | |
422 | } | |
423 | ||
1da177e4 | 424 | if (unlikely(q == NULL)) { |
10b1fbdb | 425 | atomic_dec(&user->sigpending); |
d84f4f99 | 426 | free_uid(user); |
1da177e4 LT |
427 | } else { |
428 | INIT_LIST_HEAD(&q->list); | |
429 | q->flags = 0; | |
d84f4f99 | 430 | q->user = user; |
1da177e4 | 431 | } |
d84f4f99 DH |
432 | |
433 | return q; | |
1da177e4 LT |
434 | } |
435 | ||
514a01b8 | 436 | static void __sigqueue_free(struct sigqueue *q) |
1da177e4 LT |
437 | { |
438 | if (q->flags & SIGQUEUE_PREALLOC) | |
439 | return; | |
440 | atomic_dec(&q->user->sigpending); | |
441 | free_uid(q->user); | |
442 | kmem_cache_free(sigqueue_cachep, q); | |
443 | } | |
444 | ||
6a14c5c9 | 445 | void flush_sigqueue(struct sigpending *queue) |
1da177e4 LT |
446 | { |
447 | struct sigqueue *q; | |
448 | ||
449 | sigemptyset(&queue->signal); | |
450 | while (!list_empty(&queue->list)) { | |
451 | q = list_entry(queue->list.next, struct sigqueue , list); | |
452 | list_del_init(&q->list); | |
453 | __sigqueue_free(q); | |
454 | } | |
455 | } | |
456 | ||
457 | /* | |
9e7c8f8c | 458 | * Flush all pending signals for this kthread. |
1da177e4 | 459 | */ |
c81addc9 | 460 | void flush_signals(struct task_struct *t) |
1da177e4 LT |
461 | { |
462 | unsigned long flags; | |
463 | ||
464 | spin_lock_irqsave(&t->sighand->siglock, flags); | |
9e7c8f8c ON |
465 | clear_tsk_thread_flag(t, TIF_SIGPENDING); |
466 | flush_sigqueue(&t->pending); | |
467 | flush_sigqueue(&t->signal->shared_pending); | |
1da177e4 LT |
468 | spin_unlock_irqrestore(&t->sighand->siglock, flags); |
469 | } | |
fb50f5a4 | 470 | EXPORT_SYMBOL(flush_signals); |
1da177e4 | 471 | |
baa73d9e | 472 | #ifdef CONFIG_POSIX_TIMERS |
cbaffba1 ON |
473 | static void __flush_itimer_signals(struct sigpending *pending) |
474 | { | |
475 | sigset_t signal, retain; | |
476 | struct sigqueue *q, *n; | |
477 | ||
478 | signal = pending->signal; | |
479 | sigemptyset(&retain); | |
480 | ||
481 | list_for_each_entry_safe(q, n, &pending->list, list) { | |
482 | int sig = q->info.si_signo; | |
483 | ||
484 | if (likely(q->info.si_code != SI_TIMER)) { | |
485 | sigaddset(&retain, sig); | |
486 | } else { | |
487 | sigdelset(&signal, sig); | |
488 | list_del_init(&q->list); | |
489 | __sigqueue_free(q); | |
490 | } | |
491 | } | |
492 | ||
493 | sigorsets(&pending->signal, &signal, &retain); | |
494 | } | |
495 | ||
496 | void flush_itimer_signals(void) | |
497 | { | |
498 | struct task_struct *tsk = current; | |
499 | unsigned long flags; | |
500 | ||
501 | spin_lock_irqsave(&tsk->sighand->siglock, flags); | |
502 | __flush_itimer_signals(&tsk->pending); | |
503 | __flush_itimer_signals(&tsk->signal->shared_pending); | |
504 | spin_unlock_irqrestore(&tsk->sighand->siglock, flags); | |
505 | } | |
baa73d9e | 506 | #endif |
cbaffba1 | 507 | |
10ab825b ON |
508 | void ignore_signals(struct task_struct *t) |
509 | { | |
510 | int i; | |
511 | ||
512 | for (i = 0; i < _NSIG; ++i) | |
513 | t->sighand->action[i].sa.sa_handler = SIG_IGN; | |
514 | ||
515 | flush_signals(t); | |
516 | } | |
517 | ||
1da177e4 LT |
518 | /* |
519 | * Flush all handlers for a task. | |
520 | */ | |
521 | ||
522 | void | |
523 | flush_signal_handlers(struct task_struct *t, int force_default) | |
524 | { | |
525 | int i; | |
526 | struct k_sigaction *ka = &t->sighand->action[0]; | |
527 | for (i = _NSIG ; i != 0 ; i--) { | |
528 | if (force_default || ka->sa.sa_handler != SIG_IGN) | |
529 | ka->sa.sa_handler = SIG_DFL; | |
530 | ka->sa.sa_flags = 0; | |
522cff14 | 531 | #ifdef __ARCH_HAS_SA_RESTORER |
2ca39528 KC |
532 | ka->sa.sa_restorer = NULL; |
533 | #endif | |
1da177e4 LT |
534 | sigemptyset(&ka->sa.sa_mask); |
535 | ka++; | |
536 | } | |
537 | } | |
538 | ||
67a48a24 | 539 | bool unhandled_signal(struct task_struct *tsk, int sig) |
abd4f750 | 540 | { |
445a91d2 | 541 | void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler; |
b460cbc5 | 542 | if (is_global_init(tsk)) |
67a48a24 CB |
543 | return true; |
544 | ||
445a91d2 | 545 | if (handler != SIG_IGN && handler != SIG_DFL) |
67a48a24 CB |
546 | return false; |
547 | ||
a288eecc TH |
548 | /* if ptraced, let the tracer determine */ |
549 | return !tsk->ptrace; | |
abd4f750 MAS |
550 | } |
551 | ||
57db7e4a EB |
552 | static void collect_signal(int sig, struct sigpending *list, siginfo_t *info, |
553 | bool *resched_timer) | |
1da177e4 LT |
554 | { |
555 | struct sigqueue *q, *first = NULL; | |
1da177e4 | 556 | |
1da177e4 LT |
557 | /* |
558 | * Collect the siginfo appropriate to this signal. Check if | |
559 | * there is another siginfo for the same signal. | |
560 | */ | |
561 | list_for_each_entry(q, &list->list, list) { | |
562 | if (q->info.si_signo == sig) { | |
d4434207 ON |
563 | if (first) |
564 | goto still_pending; | |
1da177e4 LT |
565 | first = q; |
566 | } | |
567 | } | |
d4434207 ON |
568 | |
569 | sigdelset(&list->signal, sig); | |
570 | ||
1da177e4 | 571 | if (first) { |
d4434207 | 572 | still_pending: |
1da177e4 LT |
573 | list_del_init(&first->list); |
574 | copy_siginfo(info, &first->info); | |
57db7e4a EB |
575 | |
576 | *resched_timer = | |
577 | (first->flags & SIGQUEUE_PREALLOC) && | |
578 | (info->si_code == SI_TIMER) && | |
579 | (info->si_sys_private); | |
580 | ||
1da177e4 | 581 | __sigqueue_free(first); |
1da177e4 | 582 | } else { |
5aba085e RD |
583 | /* |
584 | * Ok, it wasn't in the queue. This must be | |
585 | * a fast-pathed signal or we must have been | |
586 | * out of queue space. So zero out the info. | |
1da177e4 | 587 | */ |
faf1f22b | 588 | clear_siginfo(info); |
1da177e4 LT |
589 | info->si_signo = sig; |
590 | info->si_errno = 0; | |
7486e5d9 | 591 | info->si_code = SI_USER; |
1da177e4 LT |
592 | info->si_pid = 0; |
593 | info->si_uid = 0; | |
594 | } | |
1da177e4 LT |
595 | } |
596 | ||
597 | static int __dequeue_signal(struct sigpending *pending, sigset_t *mask, | |
57db7e4a | 598 | siginfo_t *info, bool *resched_timer) |
1da177e4 | 599 | { |
27d91e07 | 600 | int sig = next_signal(pending, mask); |
1da177e4 | 601 | |
2e01fabe | 602 | if (sig) |
57db7e4a | 603 | collect_signal(sig, pending, info, resched_timer); |
1da177e4 LT |
604 | return sig; |
605 | } | |
606 | ||
607 | /* | |
5aba085e | 608 | * Dequeue a signal and return the element to the caller, which is |
1da177e4 LT |
609 | * expected to free it. |
610 | * | |
611 | * All callers have to hold the siglock. | |
612 | */ | |
613 | int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info) | |
614 | { | |
57db7e4a | 615 | bool resched_timer = false; |
c5363d03 | 616 | int signr; |
caec4e8d BH |
617 | |
618 | /* We only dequeue private signals from ourselves, we don't let | |
619 | * signalfd steal them | |
620 | */ | |
57db7e4a | 621 | signr = __dequeue_signal(&tsk->pending, mask, info, &resched_timer); |
8bfd9a7a | 622 | if (!signr) { |
1da177e4 | 623 | signr = __dequeue_signal(&tsk->signal->shared_pending, |
57db7e4a | 624 | mask, info, &resched_timer); |
baa73d9e | 625 | #ifdef CONFIG_POSIX_TIMERS |
8bfd9a7a TG |
626 | /* |
627 | * itimer signal ? | |
628 | * | |
629 | * itimers are process shared and we restart periodic | |
630 | * itimers in the signal delivery path to prevent DoS | |
631 | * attacks in the high resolution timer case. This is | |
5aba085e | 632 | * compliant with the old way of self-restarting |
8bfd9a7a TG |
633 | * itimers, as the SIGALRM is a legacy signal and only |
634 | * queued once. Changing the restart behaviour to | |
635 | * restart the timer in the signal dequeue path is | |
636 | * reducing the timer noise on heavy loaded !highres | |
637 | * systems too. | |
638 | */ | |
639 | if (unlikely(signr == SIGALRM)) { | |
640 | struct hrtimer *tmr = &tsk->signal->real_timer; | |
641 | ||
642 | if (!hrtimer_is_queued(tmr) && | |
2456e855 | 643 | tsk->signal->it_real_incr != 0) { |
8bfd9a7a TG |
644 | hrtimer_forward(tmr, tmr->base->get_time(), |
645 | tsk->signal->it_real_incr); | |
646 | hrtimer_restart(tmr); | |
647 | } | |
648 | } | |
baa73d9e | 649 | #endif |
8bfd9a7a | 650 | } |
c5363d03 | 651 | |
b8fceee1 | 652 | recalc_sigpending(); |
c5363d03 PE |
653 | if (!signr) |
654 | return 0; | |
655 | ||
656 | if (unlikely(sig_kernel_stop(signr))) { | |
8bfd9a7a TG |
657 | /* |
658 | * Set a marker that we have dequeued a stop signal. Our | |
659 | * caller might release the siglock and then the pending | |
660 | * stop signal it is about to process is no longer in the | |
661 | * pending bitmasks, but must still be cleared by a SIGCONT | |
662 | * (and overruled by a SIGKILL). So those cases clear this | |
663 | * shared flag after we've set it. Note that this flag may | |
664 | * remain set after the signal we return is ignored or | |
665 | * handled. That doesn't matter because its only purpose | |
666 | * is to alert stop-signal processing code when another | |
667 | * processor has come along and cleared the flag. | |
668 | */ | |
a8f072c1 | 669 | current->jobctl |= JOBCTL_STOP_DEQUEUED; |
8bfd9a7a | 670 | } |
baa73d9e | 671 | #ifdef CONFIG_POSIX_TIMERS |
57db7e4a | 672 | if (resched_timer) { |
1da177e4 LT |
673 | /* |
674 | * Release the siglock to ensure proper locking order | |
675 | * of timer locks outside of siglocks. Note, we leave | |
676 | * irqs disabled here, since the posix-timers code is | |
677 | * about to disable them again anyway. | |
678 | */ | |
679 | spin_unlock(&tsk->sighand->siglock); | |
96fe3b07 | 680 | posixtimer_rearm(info); |
1da177e4 | 681 | spin_lock(&tsk->sighand->siglock); |
9943d3ac EB |
682 | |
683 | /* Don't expose the si_sys_private value to userspace */ | |
684 | info->si_sys_private = 0; | |
1da177e4 | 685 | } |
baa73d9e | 686 | #endif |
1da177e4 LT |
687 | return signr; |
688 | } | |
fb50f5a4 | 689 | EXPORT_SYMBOL_GPL(dequeue_signal); |
1da177e4 LT |
690 | |
691 | /* | |
692 | * Tell a process that it has a new active signal.. | |
693 | * | |
694 | * NOTE! we rely on the previous spin_lock to | |
695 | * lock interrupts for us! We can only be called with | |
696 | * "siglock" held, and the local interrupt must | |
697 | * have been disabled when that got acquired! | |
698 | * | |
699 | * No need to set need_resched since signal event passing | |
700 | * goes through ->blocked | |
701 | */ | |
910ffdb1 | 702 | void signal_wake_up_state(struct task_struct *t, unsigned int state) |
1da177e4 | 703 | { |
1da177e4 | 704 | set_tsk_thread_flag(t, TIF_SIGPENDING); |
1da177e4 | 705 | /* |
910ffdb1 | 706 | * TASK_WAKEKILL also means wake it up in the stopped/traced/killable |
f021a3c2 | 707 | * case. We don't check t->state here because there is a race with it |
1da177e4 LT |
708 | * executing another processor and just now entering stopped state. |
709 | * By using wake_up_state, we ensure the process will wake up and | |
710 | * handle its death signal. | |
711 | */ | |
910ffdb1 | 712 | if (!wake_up_state(t, state | TASK_INTERRUPTIBLE)) |
1da177e4 LT |
713 | kick_process(t); |
714 | } | |
715 | ||
71fabd5e GA |
716 | /* |
717 | * Remove signals in mask from the pending set and queue. | |
718 | * Returns 1 if any signals were found. | |
719 | * | |
720 | * All callers must be holding the siglock. | |
71fabd5e | 721 | */ |
8f11351e | 722 | static void flush_sigqueue_mask(sigset_t *mask, struct sigpending *s) |
71fabd5e GA |
723 | { |
724 | struct sigqueue *q, *n; | |
725 | sigset_t m; | |
726 | ||
727 | sigandsets(&m, mask, &s->signal); | |
728 | if (sigisemptyset(&m)) | |
8f11351e | 729 | return; |
71fabd5e | 730 | |
702a5073 | 731 | sigandnsets(&s->signal, &s->signal, mask); |
71fabd5e GA |
732 | list_for_each_entry_safe(q, n, &s->list, list) { |
733 | if (sigismember(mask, q->info.si_signo)) { | |
734 | list_del_init(&q->list); | |
735 | __sigqueue_free(q); | |
736 | } | |
737 | } | |
71fabd5e | 738 | } |
1da177e4 | 739 | |
614c517d ON |
740 | static inline int is_si_special(const struct siginfo *info) |
741 | { | |
4ff4c31a | 742 | return info <= SEND_SIG_PRIV; |
614c517d ON |
743 | } |
744 | ||
745 | static inline bool si_fromuser(const struct siginfo *info) | |
746 | { | |
747 | return info == SEND_SIG_NOINFO || | |
748 | (!is_si_special(info) && SI_FROMUSER(info)); | |
749 | } | |
750 | ||
39fd3393 SH |
751 | /* |
752 | * called with RCU read lock from check_kill_permission() | |
753 | */ | |
2a9b9094 | 754 | static bool kill_ok_by_cred(struct task_struct *t) |
39fd3393 SH |
755 | { |
756 | const struct cred *cred = current_cred(); | |
757 | const struct cred *tcred = __task_cred(t); | |
758 | ||
2a9b9094 CB |
759 | return uid_eq(cred->euid, tcred->suid) || |
760 | uid_eq(cred->euid, tcred->uid) || | |
761 | uid_eq(cred->uid, tcred->suid) || | |
762 | uid_eq(cred->uid, tcred->uid) || | |
763 | ns_capable(tcred->user_ns, CAP_KILL); | |
39fd3393 SH |
764 | } |
765 | ||
1da177e4 LT |
766 | /* |
767 | * Bad permissions for sending the signal | |
694f690d | 768 | * - the caller must hold the RCU read lock |
1da177e4 LT |
769 | */ |
770 | static int check_kill_permission(int sig, struct siginfo *info, | |
771 | struct task_struct *t) | |
772 | { | |
2e2ba22e | 773 | struct pid *sid; |
3b5e9e53 ON |
774 | int error; |
775 | ||
7ed20e1a | 776 | if (!valid_signal(sig)) |
3b5e9e53 ON |
777 | return -EINVAL; |
778 | ||
614c517d | 779 | if (!si_fromuser(info)) |
3b5e9e53 | 780 | return 0; |
e54dc243 | 781 | |
3b5e9e53 ON |
782 | error = audit_signal_info(sig, t); /* Let audit system see the signal */ |
783 | if (error) | |
1da177e4 | 784 | return error; |
3b5e9e53 | 785 | |
065add39 | 786 | if (!same_thread_group(current, t) && |
39fd3393 | 787 | !kill_ok_by_cred(t)) { |
2e2ba22e ON |
788 | switch (sig) { |
789 | case SIGCONT: | |
2e2ba22e | 790 | sid = task_session(t); |
2e2ba22e ON |
791 | /* |
792 | * We don't return the error if sid == NULL. The | |
793 | * task was unhashed, the caller must notice this. | |
794 | */ | |
795 | if (!sid || sid == task_session(current)) | |
796 | break; | |
797 | default: | |
798 | return -EPERM; | |
799 | } | |
800 | } | |
c2f0c7c3 | 801 | |
6b4f3d01 | 802 | return security_task_kill(t, info, sig, NULL); |
1da177e4 LT |
803 | } |
804 | ||
fb1d910c TH |
805 | /** |
806 | * ptrace_trap_notify - schedule trap to notify ptracer | |
807 | * @t: tracee wanting to notify tracer | |
808 | * | |
809 | * This function schedules sticky ptrace trap which is cleared on the next | |
810 | * TRAP_STOP to notify ptracer of an event. @t must have been seized by | |
811 | * ptracer. | |
812 | * | |
544b2c91 TH |
813 | * If @t is running, STOP trap will be taken. If trapped for STOP and |
814 | * ptracer is listening for events, tracee is woken up so that it can | |
815 | * re-trap for the new event. If trapped otherwise, STOP trap will be | |
816 | * eventually taken without returning to userland after the existing traps | |
817 | * are finished by PTRACE_CONT. | |
fb1d910c TH |
818 | * |
819 | * CONTEXT: | |
820 | * Must be called with @task->sighand->siglock held. | |
821 | */ | |
822 | static void ptrace_trap_notify(struct task_struct *t) | |
823 | { | |
824 | WARN_ON_ONCE(!(t->ptrace & PT_SEIZED)); | |
825 | assert_spin_locked(&t->sighand->siglock); | |
826 | ||
827 | task_set_jobctl_pending(t, JOBCTL_TRAP_NOTIFY); | |
910ffdb1 | 828 | ptrace_signal_wake_up(t, t->jobctl & JOBCTL_LISTENING); |
fb1d910c TH |
829 | } |
830 | ||
1da177e4 | 831 | /* |
7e695a5e ON |
832 | * Handle magic process-wide effects of stop/continue signals. Unlike |
833 | * the signal actions, these happen immediately at signal-generation | |
1da177e4 LT |
834 | * time regardless of blocking, ignoring, or handling. This does the |
835 | * actual continuing for SIGCONT, but not the actual stopping for stop | |
7e695a5e ON |
836 | * signals. The process stop is done as a signal action for SIG_DFL. |
837 | * | |
838 | * Returns true if the signal should be actually delivered, otherwise | |
839 | * it should be dropped. | |
1da177e4 | 840 | */ |
403bad72 | 841 | static bool prepare_signal(int sig, struct task_struct *p, bool force) |
1da177e4 | 842 | { |
ad16a460 | 843 | struct signal_struct *signal = p->signal; |
1da177e4 | 844 | struct task_struct *t; |
9490592f | 845 | sigset_t flush; |
1da177e4 | 846 | |
403bad72 | 847 | if (signal->flags & (SIGNAL_GROUP_EXIT | SIGNAL_GROUP_COREDUMP)) { |
5fa534c9 | 848 | if (!(signal->flags & SIGNAL_GROUP_EXIT)) |
403bad72 | 849 | return sig == SIGKILL; |
1da177e4 | 850 | /* |
7e695a5e | 851 | * The process is in the middle of dying, nothing to do. |
1da177e4 | 852 | */ |
7e695a5e | 853 | } else if (sig_kernel_stop(sig)) { |
1da177e4 LT |
854 | /* |
855 | * This is a stop signal. Remove SIGCONT from all queues. | |
856 | */ | |
9490592f | 857 | siginitset(&flush, sigmask(SIGCONT)); |
c09c1441 | 858 | flush_sigqueue_mask(&flush, &signal->shared_pending); |
9490592f | 859 | for_each_thread(p, t) |
c09c1441 | 860 | flush_sigqueue_mask(&flush, &t->pending); |
1da177e4 | 861 | } else if (sig == SIGCONT) { |
fc321d2e | 862 | unsigned int why; |
1da177e4 | 863 | /* |
1deac632 | 864 | * Remove all stop signals from all queues, wake all threads. |
1da177e4 | 865 | */ |
9490592f | 866 | siginitset(&flush, SIG_KERNEL_STOP_MASK); |
c09c1441 | 867 | flush_sigqueue_mask(&flush, &signal->shared_pending); |
9490592f | 868 | for_each_thread(p, t) { |
c09c1441 | 869 | flush_sigqueue_mask(&flush, &t->pending); |
3759a0d9 | 870 | task_clear_jobctl_pending(t, JOBCTL_STOP_PENDING); |
fb1d910c TH |
871 | if (likely(!(t->ptrace & PT_SEIZED))) |
872 | wake_up_state(t, __TASK_STOPPED); | |
873 | else | |
874 | ptrace_trap_notify(t); | |
9490592f | 875 | } |
1da177e4 | 876 | |
fc321d2e ON |
877 | /* |
878 | * Notify the parent with CLD_CONTINUED if we were stopped. | |
879 | * | |
880 | * If we were in the middle of a group stop, we pretend it | |
881 | * was already finished, and then continued. Since SIGCHLD | |
882 | * doesn't queue we report only CLD_STOPPED, as if the next | |
883 | * CLD_CONTINUED was dropped. | |
884 | */ | |
885 | why = 0; | |
ad16a460 | 886 | if (signal->flags & SIGNAL_STOP_STOPPED) |
fc321d2e | 887 | why |= SIGNAL_CLD_CONTINUED; |
ad16a460 | 888 | else if (signal->group_stop_count) |
fc321d2e ON |
889 | why |= SIGNAL_CLD_STOPPED; |
890 | ||
891 | if (why) { | |
021e1ae3 | 892 | /* |
ae6d2ed7 | 893 | * The first thread which returns from do_signal_stop() |
021e1ae3 ON |
894 | * will take ->siglock, notice SIGNAL_CLD_MASK, and |
895 | * notify its parent. See get_signal_to_deliver(). | |
896 | */ | |
2d39b3cd | 897 | signal_set_stop_flags(signal, why | SIGNAL_STOP_CONTINUED); |
ad16a460 ON |
898 | signal->group_stop_count = 0; |
899 | signal->group_exit_code = 0; | |
1da177e4 | 900 | } |
1da177e4 | 901 | } |
7e695a5e | 902 | |
def8cf72 | 903 | return !sig_ignored(p, sig, force); |
1da177e4 LT |
904 | } |
905 | ||
71f11dc0 ON |
906 | /* |
907 | * Test if P wants to take SIG. After we've checked all threads with this, | |
908 | * it's equivalent to finding no threads not blocking SIG. Any threads not | |
909 | * blocking SIG were ruled out because they are not running and already | |
910 | * have pending signals. Such threads will dequeue from the shared queue | |
911 | * as soon as they're available, so putting the signal on the shared queue | |
912 | * will be equivalent to sending it to one such thread. | |
913 | */ | |
acd14e62 | 914 | static inline bool wants_signal(int sig, struct task_struct *p) |
71f11dc0 ON |
915 | { |
916 | if (sigismember(&p->blocked, sig)) | |
acd14e62 CB |
917 | return false; |
918 | ||
71f11dc0 | 919 | if (p->flags & PF_EXITING) |
acd14e62 CB |
920 | return false; |
921 | ||
71f11dc0 | 922 | if (sig == SIGKILL) |
acd14e62 CB |
923 | return true; |
924 | ||
71f11dc0 | 925 | if (task_is_stopped_or_traced(p)) |
acd14e62 CB |
926 | return false; |
927 | ||
71f11dc0 ON |
928 | return task_curr(p) || !signal_pending(p); |
929 | } | |
930 | ||
07296149 | 931 | static void complete_signal(int sig, struct task_struct *p, enum pid_type type) |
71f11dc0 ON |
932 | { |
933 | struct signal_struct *signal = p->signal; | |
934 | struct task_struct *t; | |
935 | ||
936 | /* | |
937 | * Now find a thread we can wake up to take the signal off the queue. | |
938 | * | |
939 | * If the main thread wants the signal, it gets first crack. | |
940 | * Probably the least surprising to the average bear. | |
941 | */ | |
942 | if (wants_signal(sig, p)) | |
943 | t = p; | |
07296149 | 944 | else if ((type == PIDTYPE_PID) || thread_group_empty(p)) |
71f11dc0 ON |
945 | /* |
946 | * There is just one thread and it does not need to be woken. | |
947 | * It will dequeue unblocked signals before it runs again. | |
948 | */ | |
949 | return; | |
950 | else { | |
951 | /* | |
952 | * Otherwise try to find a suitable thread. | |
953 | */ | |
954 | t = signal->curr_target; | |
955 | while (!wants_signal(sig, t)) { | |
956 | t = next_thread(t); | |
957 | if (t == signal->curr_target) | |
958 | /* | |
959 | * No thread needs to be woken. | |
960 | * Any eligible threads will see | |
961 | * the signal in the queue soon. | |
962 | */ | |
963 | return; | |
964 | } | |
965 | signal->curr_target = t; | |
966 | } | |
967 | ||
968 | /* | |
969 | * Found a killable thread. If the signal will be fatal, | |
970 | * then start taking the whole group down immediately. | |
971 | */ | |
fae5fa44 | 972 | if (sig_fatal(p, sig) && |
42691579 | 973 | !(signal->flags & SIGNAL_GROUP_EXIT) && |
71f11dc0 | 974 | !sigismember(&t->real_blocked, sig) && |
42691579 | 975 | (sig == SIGKILL || !p->ptrace)) { |
71f11dc0 ON |
976 | /* |
977 | * This signal will be fatal to the whole group. | |
978 | */ | |
979 | if (!sig_kernel_coredump(sig)) { | |
980 | /* | |
981 | * Start a group exit and wake everybody up. | |
982 | * This way we don't have other threads | |
983 | * running and doing things after a slower | |
984 | * thread has the fatal signal pending. | |
985 | */ | |
986 | signal->flags = SIGNAL_GROUP_EXIT; | |
987 | signal->group_exit_code = sig; | |
988 | signal->group_stop_count = 0; | |
989 | t = p; | |
990 | do { | |
6dfca329 | 991 | task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK); |
71f11dc0 ON |
992 | sigaddset(&t->pending.signal, SIGKILL); |
993 | signal_wake_up(t, 1); | |
994 | } while_each_thread(p, t); | |
995 | return; | |
996 | } | |
997 | } | |
998 | ||
999 | /* | |
1000 | * The signal is already in the shared-pending queue. | |
1001 | * Tell the chosen thread to wake up and dequeue it. | |
1002 | */ | |
1003 | signal_wake_up(t, sig == SIGKILL); | |
1004 | return; | |
1005 | } | |
1006 | ||
a19e2c01 | 1007 | static inline bool legacy_queue(struct sigpending *signals, int sig) |
af7fff9c PE |
1008 | { |
1009 | return (sig < SIGRTMIN) && sigismember(&signals->signal, sig); | |
1010 | } | |
1011 | ||
6b550f94 SH |
1012 | #ifdef CONFIG_USER_NS |
1013 | static inline void userns_fixup_signal_uid(struct siginfo *info, struct task_struct *t) | |
1014 | { | |
1015 | if (current_user_ns() == task_cred_xxx(t, user_ns)) | |
1016 | return; | |
1017 | ||
1018 | if (SI_FROMKERNEL(info)) | |
1019 | return; | |
1020 | ||
078de5f7 EB |
1021 | rcu_read_lock(); |
1022 | info->si_uid = from_kuid_munged(task_cred_xxx(t, user_ns), | |
1023 | make_kuid(current_user_ns(), info->si_uid)); | |
1024 | rcu_read_unlock(); | |
6b550f94 SH |
1025 | } |
1026 | #else | |
1027 | static inline void userns_fixup_signal_uid(struct siginfo *info, struct task_struct *t) | |
1028 | { | |
1029 | return; | |
1030 | } | |
1031 | #endif | |
1032 | ||
7978b567 | 1033 | static int __send_signal(int sig, struct siginfo *info, struct task_struct *t, |
5a883cee | 1034 | enum pid_type type, int from_ancestor_ns) |
1da177e4 | 1035 | { |
2ca3515a | 1036 | struct sigpending *pending; |
6e65acba | 1037 | struct sigqueue *q; |
7a0aeb14 | 1038 | int override_rlimit; |
6c303d3a | 1039 | int ret = 0, result; |
0a16b607 | 1040 | |
6e65acba | 1041 | assert_spin_locked(&t->sighand->siglock); |
921cf9f6 | 1042 | |
6c303d3a | 1043 | result = TRACE_SIGNAL_IGNORED; |
629d362b | 1044 | if (!prepare_signal(sig, t, |
4ff4c31a | 1045 | from_ancestor_ns || (info == SEND_SIG_PRIV))) |
6c303d3a | 1046 | goto ret; |
2ca3515a | 1047 | |
5a883cee | 1048 | pending = (type != PIDTYPE_PID) ? &t->signal->shared_pending : &t->pending; |
2acb024d PE |
1049 | /* |
1050 | * Short-circuit ignored signals and support queuing | |
1051 | * exactly one non-rt signal, so that we can get more | |
1052 | * detailed information about the cause of the signal. | |
1053 | */ | |
6c303d3a | 1054 | result = TRACE_SIGNAL_ALREADY_PENDING; |
7e695a5e | 1055 | if (legacy_queue(pending, sig)) |
6c303d3a ON |
1056 | goto ret; |
1057 | ||
1058 | result = TRACE_SIGNAL_DELIVERED; | |
1da177e4 | 1059 | /* |
f149b315 EB |
1060 | * Skip useless siginfo allocation for SIGKILL SIGSTOP, |
1061 | * and kernel threads. | |
1da177e4 | 1062 | */ |
4ff4c31a | 1063 | if (sig_kernel_only(sig) || (t->flags & PF_KTHREAD)) |
1da177e4 LT |
1064 | goto out_set; |
1065 | ||
5aba085e RD |
1066 | /* |
1067 | * Real-time signals must be queued if sent by sigqueue, or | |
1068 | * some other real-time mechanism. It is implementation | |
1069 | * defined whether kill() does so. We attempt to do so, on | |
1070 | * the principle of least surprise, but since kill is not | |
1071 | * allowed to fail with EAGAIN when low on memory we just | |
1072 | * make sure at least one signal gets delivered and don't | |
1073 | * pass on the info struct. | |
1074 | */ | |
7a0aeb14 VN |
1075 | if (sig < SIGRTMIN) |
1076 | override_rlimit = (is_si_special(info) || info->si_code >= 0); | |
1077 | else | |
1078 | override_rlimit = 0; | |
1079 | ||
75f296d9 | 1080 | q = __sigqueue_alloc(sig, t, GFP_ATOMIC, override_rlimit); |
1da177e4 | 1081 | if (q) { |
2ca3515a | 1082 | list_add_tail(&q->list, &pending->list); |
1da177e4 | 1083 | switch ((unsigned long) info) { |
b67a1b9e | 1084 | case (unsigned long) SEND_SIG_NOINFO: |
faf1f22b | 1085 | clear_siginfo(&q->info); |
1da177e4 LT |
1086 | q->info.si_signo = sig; |
1087 | q->info.si_errno = 0; | |
1088 | q->info.si_code = SI_USER; | |
9cd4fd10 | 1089 | q->info.si_pid = task_tgid_nr_ns(current, |
09bca05c | 1090 | task_active_pid_ns(t)); |
078de5f7 | 1091 | q->info.si_uid = from_kuid_munged(current_user_ns(), current_uid()); |
1da177e4 | 1092 | break; |
b67a1b9e | 1093 | case (unsigned long) SEND_SIG_PRIV: |
faf1f22b | 1094 | clear_siginfo(&q->info); |
1da177e4 LT |
1095 | q->info.si_signo = sig; |
1096 | q->info.si_errno = 0; | |
1097 | q->info.si_code = SI_KERNEL; | |
1098 | q->info.si_pid = 0; | |
1099 | q->info.si_uid = 0; | |
1100 | break; | |
1101 | default: | |
1102 | copy_siginfo(&q->info, info); | |
6588c1e3 SB |
1103 | if (from_ancestor_ns) |
1104 | q->info.si_pid = 0; | |
1da177e4 LT |
1105 | break; |
1106 | } | |
6b550f94 SH |
1107 | |
1108 | userns_fixup_signal_uid(&q->info, t); | |
1109 | ||
621d3121 | 1110 | } else if (!is_si_special(info)) { |
ba005e1f MH |
1111 | if (sig >= SIGRTMIN && info->si_code != SI_USER) { |
1112 | /* | |
1113 | * Queue overflow, abort. We may abort if the | |
1114 | * signal was rt and sent by user using something | |
1115 | * other than kill(). | |
1116 | */ | |
6c303d3a ON |
1117 | result = TRACE_SIGNAL_OVERFLOW_FAIL; |
1118 | ret = -EAGAIN; | |
1119 | goto ret; | |
ba005e1f MH |
1120 | } else { |
1121 | /* | |
1122 | * This is a silent loss of information. We still | |
1123 | * send the signal, but the *info bits are lost. | |
1124 | */ | |
6c303d3a | 1125 | result = TRACE_SIGNAL_LOSE_INFO; |
ba005e1f | 1126 | } |
1da177e4 LT |
1127 | } |
1128 | ||
1129 | out_set: | |
53c30337 | 1130 | signalfd_notify(t, sig); |
2ca3515a | 1131 | sigaddset(&pending->signal, sig); |
c3ad2c3b EB |
1132 | |
1133 | /* Let multiprocess signals appear after on-going forks */ | |
1134 | if (type > PIDTYPE_TGID) { | |
1135 | struct multiprocess_signals *delayed; | |
1136 | hlist_for_each_entry(delayed, &t->signal->multiprocess, node) { | |
1137 | sigset_t *signal = &delayed->signal; | |
1138 | /* Can't queue both a stop and a continue signal */ | |
1139 | if (sig == SIGCONT) | |
1140 | sigdelsetmask(signal, SIG_KERNEL_STOP_MASK); | |
1141 | else if (sig_kernel_stop(sig)) | |
1142 | sigdelset(signal, SIGCONT); | |
1143 | sigaddset(signal, sig); | |
1144 | } | |
1145 | } | |
1146 | ||
07296149 | 1147 | complete_signal(sig, t, type); |
6c303d3a | 1148 | ret: |
5a883cee | 1149 | trace_signal_generate(sig, info, t, type != PIDTYPE_PID, result); |
6c303d3a | 1150 | return ret; |
1da177e4 LT |
1151 | } |
1152 | ||
7978b567 | 1153 | static int send_signal(int sig, struct siginfo *info, struct task_struct *t, |
b213984b | 1154 | enum pid_type type) |
7978b567 | 1155 | { |
921cf9f6 SB |
1156 | int from_ancestor_ns = 0; |
1157 | ||
1158 | #ifdef CONFIG_PID_NS | |
dd34200a ON |
1159 | from_ancestor_ns = si_fromuser(info) && |
1160 | !task_pid_nr_ns(current, task_active_pid_ns(t)); | |
921cf9f6 SB |
1161 | #endif |
1162 | ||
5a883cee | 1163 | return __send_signal(sig, info, t, type, from_ancestor_ns); |
7978b567 SB |
1164 | } |
1165 | ||
4aaefee5 | 1166 | static void print_fatal_signal(int signr) |
45807a1d | 1167 | { |
4aaefee5 | 1168 | struct pt_regs *regs = signal_pt_regs(); |
747800ef | 1169 | pr_info("potentially unexpected fatal signal %d.\n", signr); |
45807a1d | 1170 | |
ca5cd877 | 1171 | #if defined(__i386__) && !defined(__arch_um__) |
747800ef | 1172 | pr_info("code at %08lx: ", regs->ip); |
45807a1d IM |
1173 | { |
1174 | int i; | |
1175 | for (i = 0; i < 16; i++) { | |
1176 | unsigned char insn; | |
1177 | ||
b45c6e76 AK |
1178 | if (get_user(insn, (unsigned char *)(regs->ip + i))) |
1179 | break; | |
747800ef | 1180 | pr_cont("%02x ", insn); |
45807a1d IM |
1181 | } |
1182 | } | |
747800ef | 1183 | pr_cont("\n"); |
45807a1d | 1184 | #endif |
3a9f84d3 | 1185 | preempt_disable(); |
45807a1d | 1186 | show_regs(regs); |
3a9f84d3 | 1187 | preempt_enable(); |
45807a1d IM |
1188 | } |
1189 | ||
1190 | static int __init setup_print_fatal_signals(char *str) | |
1191 | { | |
1192 | get_option (&str, &print_fatal_signals); | |
1193 | ||
1194 | return 1; | |
1195 | } | |
1196 | ||
1197 | __setup("print-fatal-signals=", setup_print_fatal_signals); | |
1da177e4 | 1198 | |
4cd4b6d4 PE |
1199 | int |
1200 | __group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p) | |
1201 | { | |
b213984b | 1202 | return send_signal(sig, info, p, PIDTYPE_TGID); |
4cd4b6d4 PE |
1203 | } |
1204 | ||
4a30debf | 1205 | int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p, |
40b3b025 | 1206 | enum pid_type type) |
4a30debf ON |
1207 | { |
1208 | unsigned long flags; | |
1209 | int ret = -ESRCH; | |
1210 | ||
1211 | if (lock_task_sighand(p, &flags)) { | |
b213984b | 1212 | ret = send_signal(sig, info, p, type); |
4a30debf ON |
1213 | unlock_task_sighand(p, &flags); |
1214 | } | |
1215 | ||
1216 | return ret; | |
1217 | } | |
1218 | ||
1da177e4 LT |
1219 | /* |
1220 | * Force a signal that the process can't ignore: if necessary | |
1221 | * we unblock the signal and change any SIG_IGN to SIG_DFL. | |
ae74c3b6 LT |
1222 | * |
1223 | * Note: If we unblock the signal, we always reset it to SIG_DFL, | |
1224 | * since we do not want to have a signal handler that was blocked | |
1225 | * be invoked when user space had explicitly blocked it. | |
1226 | * | |
80fe728d ON |
1227 | * We don't want to have recursive SIGSEGV's etc, for example, |
1228 | * that is why we also clear SIGNAL_UNKILLABLE. | |
1da177e4 | 1229 | */ |
1da177e4 LT |
1230 | int |
1231 | force_sig_info(int sig, struct siginfo *info, struct task_struct *t) | |
1232 | { | |
1233 | unsigned long int flags; | |
ae74c3b6 LT |
1234 | int ret, blocked, ignored; |
1235 | struct k_sigaction *action; | |
1da177e4 LT |
1236 | |
1237 | spin_lock_irqsave(&t->sighand->siglock, flags); | |
ae74c3b6 LT |
1238 | action = &t->sighand->action[sig-1]; |
1239 | ignored = action->sa.sa_handler == SIG_IGN; | |
1240 | blocked = sigismember(&t->blocked, sig); | |
1241 | if (blocked || ignored) { | |
1242 | action->sa.sa_handler = SIG_DFL; | |
1243 | if (blocked) { | |
1244 | sigdelset(&t->blocked, sig); | |
7bb44ade | 1245 | recalc_sigpending_and_wake(t); |
ae74c3b6 | 1246 | } |
1da177e4 | 1247 | } |
eb61b591 JI |
1248 | /* |
1249 | * Don't clear SIGNAL_UNKILLABLE for traced tasks, users won't expect | |
1250 | * debugging to leave init killable. | |
1251 | */ | |
1252 | if (action->sa.sa_handler == SIG_DFL && !t->ptrace) | |
80fe728d | 1253 | t->signal->flags &= ~SIGNAL_UNKILLABLE; |
b21c5bd5 | 1254 | ret = send_signal(sig, info, t, PIDTYPE_PID); |
1da177e4 LT |
1255 | spin_unlock_irqrestore(&t->sighand->siglock, flags); |
1256 | ||
1257 | return ret; | |
1258 | } | |
1259 | ||
1da177e4 LT |
1260 | /* |
1261 | * Nuke all other threads in the group. | |
1262 | */ | |
09faef11 | 1263 | int zap_other_threads(struct task_struct *p) |
1da177e4 | 1264 | { |
09faef11 ON |
1265 | struct task_struct *t = p; |
1266 | int count = 0; | |
1da177e4 | 1267 | |
1da177e4 LT |
1268 | p->signal->group_stop_count = 0; |
1269 | ||
09faef11 | 1270 | while_each_thread(p, t) { |
6dfca329 | 1271 | task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK); |
09faef11 ON |
1272 | count++; |
1273 | ||
1274 | /* Don't bother with already dead threads */ | |
1da177e4 LT |
1275 | if (t->exit_state) |
1276 | continue; | |
1da177e4 | 1277 | sigaddset(&t->pending.signal, SIGKILL); |
1da177e4 LT |
1278 | signal_wake_up(t, 1); |
1279 | } | |
09faef11 ON |
1280 | |
1281 | return count; | |
1da177e4 LT |
1282 | } |
1283 | ||
b8ed374e NK |
1284 | struct sighand_struct *__lock_task_sighand(struct task_struct *tsk, |
1285 | unsigned long *flags) | |
f63ee72e ON |
1286 | { |
1287 | struct sighand_struct *sighand; | |
1288 | ||
59dc6f3c | 1289 | rcu_read_lock(); |
f63ee72e ON |
1290 | for (;;) { |
1291 | sighand = rcu_dereference(tsk->sighand); | |
59dc6f3c | 1292 | if (unlikely(sighand == NULL)) |
f63ee72e | 1293 | break; |
59dc6f3c | 1294 | |
392809b2 ON |
1295 | /* |
1296 | * This sighand can be already freed and even reused, but | |
5f0d5a3a | 1297 | * we rely on SLAB_TYPESAFE_BY_RCU and sighand_ctor() which |
392809b2 ON |
1298 | * initializes ->siglock: this slab can't go away, it has |
1299 | * the same object type, ->siglock can't be reinitialized. | |
1300 | * | |
1301 | * We need to ensure that tsk->sighand is still the same | |
1302 | * after we take the lock, we can race with de_thread() or | |
1303 | * __exit_signal(). In the latter case the next iteration | |
1304 | * must see ->sighand == NULL. | |
1305 | */ | |
59dc6f3c AMG |
1306 | spin_lock_irqsave(&sighand->siglock, *flags); |
1307 | if (likely(sighand == tsk->sighand)) | |
f63ee72e | 1308 | break; |
59dc6f3c | 1309 | spin_unlock_irqrestore(&sighand->siglock, *flags); |
f63ee72e | 1310 | } |
59dc6f3c | 1311 | rcu_read_unlock(); |
f63ee72e ON |
1312 | |
1313 | return sighand; | |
1314 | } | |
1315 | ||
c69e8d9c DH |
1316 | /* |
1317 | * send signal info to all the members of a group | |
c69e8d9c | 1318 | */ |
01024980 EB |
1319 | int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p, |
1320 | enum pid_type type) | |
1da177e4 | 1321 | { |
694f690d DH |
1322 | int ret; |
1323 | ||
1324 | rcu_read_lock(); | |
1325 | ret = check_kill_permission(sig, info, p); | |
1326 | rcu_read_unlock(); | |
f63ee72e | 1327 | |
4a30debf | 1328 | if (!ret && sig) |
40b3b025 | 1329 | ret = do_send_sig_info(sig, info, p, type); |
1da177e4 LT |
1330 | |
1331 | return ret; | |
1332 | } | |
1333 | ||
1334 | /* | |
146a505d | 1335 | * __kill_pgrp_info() sends a signal to a process group: this is what the tty |
1da177e4 | 1336 | * control characters do (^C, ^Z etc) |
c69e8d9c | 1337 | * - the caller must hold at least a readlock on tasklist_lock |
1da177e4 | 1338 | */ |
c4b92fc1 | 1339 | int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp) |
1da177e4 LT |
1340 | { |
1341 | struct task_struct *p = NULL; | |
1342 | int retval, success; | |
1343 | ||
1da177e4 LT |
1344 | success = 0; |
1345 | retval = -ESRCH; | |
c4b92fc1 | 1346 | do_each_pid_task(pgrp, PIDTYPE_PGID, p) { |
01024980 | 1347 | int err = group_send_sig_info(sig, info, p, PIDTYPE_PGID); |
1da177e4 LT |
1348 | success |= !err; |
1349 | retval = err; | |
c4b92fc1 | 1350 | } while_each_pid_task(pgrp, PIDTYPE_PGID, p); |
1da177e4 LT |
1351 | return success ? 0 : retval; |
1352 | } | |
1353 | ||
c4b92fc1 | 1354 | int kill_pid_info(int sig, struct siginfo *info, struct pid *pid) |
1da177e4 | 1355 | { |
d36174bc | 1356 | int error = -ESRCH; |
1da177e4 LT |
1357 | struct task_struct *p; |
1358 | ||
eca1a089 PM |
1359 | for (;;) { |
1360 | rcu_read_lock(); | |
1361 | p = pid_task(pid, PIDTYPE_PID); | |
1362 | if (p) | |
01024980 | 1363 | error = group_send_sig_info(sig, info, p, PIDTYPE_TGID); |
eca1a089 PM |
1364 | rcu_read_unlock(); |
1365 | if (likely(!p || error != -ESRCH)) | |
1366 | return error; | |
6ca25b55 | 1367 | |
eca1a089 PM |
1368 | /* |
1369 | * The task was unhashed in between, try again. If it | |
1370 | * is dead, pid_task() will return NULL, if we race with | |
1371 | * de_thread() it will find the new leader. | |
1372 | */ | |
1373 | } | |
1da177e4 LT |
1374 | } |
1375 | ||
6c478ae9 | 1376 | static int kill_proc_info(int sig, struct siginfo *info, pid_t pid) |
c4b92fc1 EB |
1377 | { |
1378 | int error; | |
1379 | rcu_read_lock(); | |
b488893a | 1380 | error = kill_pid_info(sig, info, find_vpid(pid)); |
c4b92fc1 EB |
1381 | rcu_read_unlock(); |
1382 | return error; | |
1383 | } | |
1384 | ||
bb17fcca CB |
1385 | static inline bool kill_as_cred_perm(const struct cred *cred, |
1386 | struct task_struct *target) | |
d178bc3a SH |
1387 | { |
1388 | const struct cred *pcred = __task_cred(target); | |
bb17fcca CB |
1389 | |
1390 | return uid_eq(cred->euid, pcred->suid) || | |
1391 | uid_eq(cred->euid, pcred->uid) || | |
1392 | uid_eq(cred->uid, pcred->suid) || | |
1393 | uid_eq(cred->uid, pcred->uid); | |
d178bc3a SH |
1394 | } |
1395 | ||
2425c08b | 1396 | /* like kill_pid_info(), but doesn't use uid/euid of "current" */ |
d178bc3a | 1397 | int kill_pid_info_as_cred(int sig, struct siginfo *info, struct pid *pid, |
6b4f3d01 | 1398 | const struct cred *cred) |
46113830 HW |
1399 | { |
1400 | int ret = -EINVAL; | |
1401 | struct task_struct *p; | |
14d8c9f3 | 1402 | unsigned long flags; |
46113830 HW |
1403 | |
1404 | if (!valid_signal(sig)) | |
1405 | return ret; | |
1406 | ||
14d8c9f3 | 1407 | rcu_read_lock(); |
2425c08b | 1408 | p = pid_task(pid, PIDTYPE_PID); |
46113830 HW |
1409 | if (!p) { |
1410 | ret = -ESRCH; | |
1411 | goto out_unlock; | |
1412 | } | |
d178bc3a | 1413 | if (si_fromuser(info) && !kill_as_cred_perm(cred, p)) { |
46113830 HW |
1414 | ret = -EPERM; |
1415 | goto out_unlock; | |
1416 | } | |
6b4f3d01 | 1417 | ret = security_task_kill(p, info, sig, cred); |
8f95dc58 DQ |
1418 | if (ret) |
1419 | goto out_unlock; | |
14d8c9f3 TG |
1420 | |
1421 | if (sig) { | |
1422 | if (lock_task_sighand(p, &flags)) { | |
5a883cee | 1423 | ret = __send_signal(sig, info, p, PIDTYPE_TGID, 0); |
14d8c9f3 TG |
1424 | unlock_task_sighand(p, &flags); |
1425 | } else | |
1426 | ret = -ESRCH; | |
46113830 HW |
1427 | } |
1428 | out_unlock: | |
14d8c9f3 | 1429 | rcu_read_unlock(); |
46113830 HW |
1430 | return ret; |
1431 | } | |
d178bc3a | 1432 | EXPORT_SYMBOL_GPL(kill_pid_info_as_cred); |
1da177e4 LT |
1433 | |
1434 | /* | |
1435 | * kill_something_info() interprets pid in interesting ways just like kill(2). | |
1436 | * | |
1437 | * POSIX specifies that kill(-1,sig) is unspecified, but what we have | |
1438 | * is probably wrong. Should make it like BSD or SYSV. | |
1439 | */ | |
1440 | ||
bc64efd2 | 1441 | static int kill_something_info(int sig, struct siginfo *info, pid_t pid) |
1da177e4 | 1442 | { |
8d42db18 | 1443 | int ret; |
d5df763b PE |
1444 | |
1445 | if (pid > 0) { | |
1446 | rcu_read_lock(); | |
1447 | ret = kill_pid_info(sig, info, find_vpid(pid)); | |
1448 | rcu_read_unlock(); | |
1449 | return ret; | |
1450 | } | |
1451 | ||
4ea77014 | 1452 | /* -INT_MIN is undefined. Exclude this case to avoid a UBSAN warning */ |
1453 | if (pid == INT_MIN) | |
1454 | return -ESRCH; | |
1455 | ||
d5df763b PE |
1456 | read_lock(&tasklist_lock); |
1457 | if (pid != -1) { | |
1458 | ret = __kill_pgrp_info(sig, info, | |
1459 | pid ? find_vpid(-pid) : task_pgrp(current)); | |
1460 | } else { | |
1da177e4 LT |
1461 | int retval = 0, count = 0; |
1462 | struct task_struct * p; | |
1463 | ||
1da177e4 | 1464 | for_each_process(p) { |
d25141a8 SB |
1465 | if (task_pid_vnr(p) > 1 && |
1466 | !same_thread_group(p, current)) { | |
01024980 EB |
1467 | int err = group_send_sig_info(sig, info, p, |
1468 | PIDTYPE_MAX); | |
1da177e4 LT |
1469 | ++count; |
1470 | if (err != -EPERM) | |
1471 | retval = err; | |
1472 | } | |
1473 | } | |
8d42db18 | 1474 | ret = count ? retval : -ESRCH; |
1da177e4 | 1475 | } |
d5df763b PE |
1476 | read_unlock(&tasklist_lock); |
1477 | ||
8d42db18 | 1478 | return ret; |
1da177e4 LT |
1479 | } |
1480 | ||
1481 | /* | |
1482 | * These are for backward compatibility with the rest of the kernel source. | |
1483 | */ | |
1484 | ||
5aba085e | 1485 | int send_sig_info(int sig, struct siginfo *info, struct task_struct *p) |
1da177e4 | 1486 | { |
1da177e4 LT |
1487 | /* |
1488 | * Make sure legacy kernel users don't send in bad values | |
1489 | * (normal paths check this in check_kill_permission). | |
1490 | */ | |
7ed20e1a | 1491 | if (!valid_signal(sig)) |
1da177e4 LT |
1492 | return -EINVAL; |
1493 | ||
40b3b025 | 1494 | return do_send_sig_info(sig, info, p, PIDTYPE_PID); |
1da177e4 | 1495 | } |
fb50f5a4 | 1496 | EXPORT_SYMBOL(send_sig_info); |
1da177e4 | 1497 | |
b67a1b9e ON |
1498 | #define __si_special(priv) \ |
1499 | ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO) | |
1500 | ||
1da177e4 LT |
1501 | int |
1502 | send_sig(int sig, struct task_struct *p, int priv) | |
1503 | { | |
b67a1b9e | 1504 | return send_sig_info(sig, __si_special(priv), p); |
1da177e4 | 1505 | } |
fb50f5a4 | 1506 | EXPORT_SYMBOL(send_sig); |
1da177e4 | 1507 | |
52cba1a2 | 1508 | void force_sig(int sig, struct task_struct *p) |
1da177e4 | 1509 | { |
b67a1b9e | 1510 | force_sig_info(sig, SEND_SIG_PRIV, p); |
1da177e4 | 1511 | } |
fb50f5a4 | 1512 | EXPORT_SYMBOL(force_sig); |
1da177e4 LT |
1513 | |
1514 | /* | |
1515 | * When things go south during signal handling, we | |
1516 | * will force a SIGSEGV. And if the signal that caused | |
1517 | * the problem was already a SIGSEGV, we'll want to | |
1518 | * make sure we don't even try to deliver the signal.. | |
1519 | */ | |
52cba1a2 | 1520 | void force_sigsegv(int sig, struct task_struct *p) |
1da177e4 LT |
1521 | { |
1522 | if (sig == SIGSEGV) { | |
1523 | unsigned long flags; | |
1524 | spin_lock_irqsave(&p->sighand->siglock, flags); | |
1525 | p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL; | |
1526 | spin_unlock_irqrestore(&p->sighand->siglock, flags); | |
1527 | } | |
1528 | force_sig(SIGSEGV, p); | |
1da177e4 LT |
1529 | } |
1530 | ||
f8ec6601 EB |
1531 | int force_sig_fault(int sig, int code, void __user *addr |
1532 | ___ARCH_SI_TRAPNO(int trapno) | |
1533 | ___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr) | |
1534 | , struct task_struct *t) | |
1535 | { | |
1536 | struct siginfo info; | |
1537 | ||
1538 | clear_siginfo(&info); | |
1539 | info.si_signo = sig; | |
1540 | info.si_errno = 0; | |
1541 | info.si_code = code; | |
1542 | info.si_addr = addr; | |
1543 | #ifdef __ARCH_SI_TRAPNO | |
1544 | info.si_trapno = trapno; | |
1545 | #endif | |
1546 | #ifdef __ia64__ | |
1547 | info.si_imm = imm; | |
1548 | info.si_flags = flags; | |
1549 | info.si_isr = isr; | |
1550 | #endif | |
1551 | return force_sig_info(info.si_signo, &info, t); | |
1552 | } | |
1553 | ||
1554 | int send_sig_fault(int sig, int code, void __user *addr | |
1555 | ___ARCH_SI_TRAPNO(int trapno) | |
1556 | ___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr) | |
1557 | , struct task_struct *t) | |
1558 | { | |
1559 | struct siginfo info; | |
1560 | ||
1561 | clear_siginfo(&info); | |
1562 | info.si_signo = sig; | |
1563 | info.si_errno = 0; | |
1564 | info.si_code = code; | |
1565 | info.si_addr = addr; | |
1566 | #ifdef __ARCH_SI_TRAPNO | |
1567 | info.si_trapno = trapno; | |
1568 | #endif | |
1569 | #ifdef __ia64__ | |
1570 | info.si_imm = imm; | |
1571 | info.si_flags = flags; | |
1572 | info.si_isr = isr; | |
1573 | #endif | |
1574 | return send_sig_info(info.si_signo, &info, t); | |
1575 | } | |
1576 | ||
38246735 EB |
1577 | int force_sig_mceerr(int code, void __user *addr, short lsb, struct task_struct *t) |
1578 | { | |
1579 | struct siginfo info; | |
1580 | ||
1581 | WARN_ON((code != BUS_MCEERR_AO) && (code != BUS_MCEERR_AR)); | |
1582 | clear_siginfo(&info); | |
1583 | info.si_signo = SIGBUS; | |
1584 | info.si_errno = 0; | |
1585 | info.si_code = code; | |
1586 | info.si_addr = addr; | |
1587 | info.si_addr_lsb = lsb; | |
1588 | return force_sig_info(info.si_signo, &info, t); | |
1589 | } | |
1590 | ||
1591 | int send_sig_mceerr(int code, void __user *addr, short lsb, struct task_struct *t) | |
1592 | { | |
1593 | struct siginfo info; | |
1594 | ||
1595 | WARN_ON((code != BUS_MCEERR_AO) && (code != BUS_MCEERR_AR)); | |
1596 | clear_siginfo(&info); | |
1597 | info.si_signo = SIGBUS; | |
1598 | info.si_errno = 0; | |
1599 | info.si_code = code; | |
1600 | info.si_addr = addr; | |
1601 | info.si_addr_lsb = lsb; | |
1602 | return send_sig_info(info.si_signo, &info, t); | |
1603 | } | |
1604 | EXPORT_SYMBOL(send_sig_mceerr); | |
38246735 | 1605 | |
38246735 EB |
1606 | int force_sig_bnderr(void __user *addr, void __user *lower, void __user *upper) |
1607 | { | |
1608 | struct siginfo info; | |
1609 | ||
1610 | clear_siginfo(&info); | |
1611 | info.si_signo = SIGSEGV; | |
1612 | info.si_errno = 0; | |
1613 | info.si_code = SEGV_BNDERR; | |
1614 | info.si_addr = addr; | |
1615 | info.si_lower = lower; | |
1616 | info.si_upper = upper; | |
1617 | return force_sig_info(info.si_signo, &info, current); | |
1618 | } | |
38246735 EB |
1619 | |
1620 | #ifdef SEGV_PKUERR | |
1621 | int force_sig_pkuerr(void __user *addr, u32 pkey) | |
1622 | { | |
1623 | struct siginfo info; | |
1624 | ||
1625 | clear_siginfo(&info); | |
1626 | info.si_signo = SIGSEGV; | |
1627 | info.si_errno = 0; | |
1628 | info.si_code = SEGV_PKUERR; | |
1629 | info.si_addr = addr; | |
1630 | info.si_pkey = pkey; | |
1631 | return force_sig_info(info.si_signo, &info, current); | |
1632 | } | |
1633 | #endif | |
f8ec6601 | 1634 | |
f71dd7dc EB |
1635 | /* For the crazy architectures that include trap information in |
1636 | * the errno field, instead of an actual errno value. | |
1637 | */ | |
1638 | int force_sig_ptrace_errno_trap(int errno, void __user *addr) | |
1639 | { | |
1640 | struct siginfo info; | |
1641 | ||
1642 | clear_siginfo(&info); | |
1643 | info.si_signo = SIGTRAP; | |
1644 | info.si_errno = errno; | |
1645 | info.si_code = TRAP_HWBKPT; | |
1646 | info.si_addr = addr; | |
1647 | return force_sig_info(info.si_signo, &info, current); | |
1648 | } | |
1649 | ||
c4b92fc1 EB |
1650 | int kill_pgrp(struct pid *pid, int sig, int priv) |
1651 | { | |
146a505d PE |
1652 | int ret; |
1653 | ||
1654 | read_lock(&tasklist_lock); | |
1655 | ret = __kill_pgrp_info(sig, __si_special(priv), pid); | |
1656 | read_unlock(&tasklist_lock); | |
1657 | ||
1658 | return ret; | |
c4b92fc1 EB |
1659 | } |
1660 | EXPORT_SYMBOL(kill_pgrp); | |
1661 | ||
1662 | int kill_pid(struct pid *pid, int sig, int priv) | |
1663 | { | |
1664 | return kill_pid_info(sig, __si_special(priv), pid); | |
1665 | } | |
1666 | EXPORT_SYMBOL(kill_pid); | |
1667 | ||
1da177e4 LT |
1668 | /* |
1669 | * These functions support sending signals using preallocated sigqueue | |
1670 | * structures. This is needed "because realtime applications cannot | |
1671 | * afford to lose notifications of asynchronous events, like timer | |
5aba085e | 1672 | * expirations or I/O completions". In the case of POSIX Timers |
1da177e4 LT |
1673 | * we allocate the sigqueue structure from the timer_create. If this |
1674 | * allocation fails we are able to report the failure to the application | |
1675 | * with an EAGAIN error. | |
1676 | */ | |
1da177e4 LT |
1677 | struct sigqueue *sigqueue_alloc(void) |
1678 | { | |
f84d49b2 | 1679 | struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0); |
1da177e4 | 1680 | |
f84d49b2 | 1681 | if (q) |
1da177e4 | 1682 | q->flags |= SIGQUEUE_PREALLOC; |
f84d49b2 NO |
1683 | |
1684 | return q; | |
1da177e4 LT |
1685 | } |
1686 | ||
1687 | void sigqueue_free(struct sigqueue *q) | |
1688 | { | |
1689 | unsigned long flags; | |
60187d27 ON |
1690 | spinlock_t *lock = ¤t->sighand->siglock; |
1691 | ||
1da177e4 LT |
1692 | BUG_ON(!(q->flags & SIGQUEUE_PREALLOC)); |
1693 | /* | |
c8e85b4f ON |
1694 | * We must hold ->siglock while testing q->list |
1695 | * to serialize with collect_signal() or with | |
da7978b0 | 1696 | * __exit_signal()->flush_sigqueue(). |
1da177e4 | 1697 | */ |
60187d27 | 1698 | spin_lock_irqsave(lock, flags); |
c8e85b4f ON |
1699 | q->flags &= ~SIGQUEUE_PREALLOC; |
1700 | /* | |
1701 | * If it is queued it will be freed when dequeued, | |
1702 | * like the "regular" sigqueue. | |
1703 | */ | |
60187d27 | 1704 | if (!list_empty(&q->list)) |
c8e85b4f | 1705 | q = NULL; |
60187d27 ON |
1706 | spin_unlock_irqrestore(lock, flags); |
1707 | ||
c8e85b4f ON |
1708 | if (q) |
1709 | __sigqueue_free(q); | |
1da177e4 LT |
1710 | } |
1711 | ||
24122c7f | 1712 | int send_sigqueue(struct sigqueue *q, struct pid *pid, enum pid_type type) |
9e3bd6c3 | 1713 | { |
e62e6650 | 1714 | int sig = q->info.si_signo; |
2ca3515a | 1715 | struct sigpending *pending; |
24122c7f | 1716 | struct task_struct *t; |
e62e6650 | 1717 | unsigned long flags; |
163566f6 | 1718 | int ret, result; |
2ca3515a | 1719 | |
4cd4b6d4 | 1720 | BUG_ON(!(q->flags & SIGQUEUE_PREALLOC)); |
e62e6650 ON |
1721 | |
1722 | ret = -1; | |
24122c7f EB |
1723 | rcu_read_lock(); |
1724 | t = pid_task(pid, type); | |
1725 | if (!t || !likely(lock_task_sighand(t, &flags))) | |
e62e6650 ON |
1726 | goto ret; |
1727 | ||
7e695a5e | 1728 | ret = 1; /* the signal is ignored */ |
163566f6 | 1729 | result = TRACE_SIGNAL_IGNORED; |
def8cf72 | 1730 | if (!prepare_signal(sig, t, false)) |
e62e6650 ON |
1731 | goto out; |
1732 | ||
1733 | ret = 0; | |
9e3bd6c3 PE |
1734 | if (unlikely(!list_empty(&q->list))) { |
1735 | /* | |
1736 | * If an SI_TIMER entry is already queue just increment | |
1737 | * the overrun count. | |
1738 | */ | |
9e3bd6c3 PE |
1739 | BUG_ON(q->info.si_code != SI_TIMER); |
1740 | q->info.si_overrun++; | |
163566f6 | 1741 | result = TRACE_SIGNAL_ALREADY_PENDING; |
e62e6650 | 1742 | goto out; |
9e3bd6c3 | 1743 | } |
ba661292 | 1744 | q->info.si_overrun = 0; |
9e3bd6c3 | 1745 | |
9e3bd6c3 | 1746 | signalfd_notify(t, sig); |
24122c7f | 1747 | pending = (type != PIDTYPE_PID) ? &t->signal->shared_pending : &t->pending; |
9e3bd6c3 PE |
1748 | list_add_tail(&q->list, &pending->list); |
1749 | sigaddset(&pending->signal, sig); | |
07296149 | 1750 | complete_signal(sig, t, type); |
163566f6 | 1751 | result = TRACE_SIGNAL_DELIVERED; |
e62e6650 | 1752 | out: |
24122c7f | 1753 | trace_signal_generate(sig, &q->info, t, type != PIDTYPE_PID, result); |
e62e6650 ON |
1754 | unlock_task_sighand(t, &flags); |
1755 | ret: | |
24122c7f | 1756 | rcu_read_unlock(); |
e62e6650 | 1757 | return ret; |
9e3bd6c3 PE |
1758 | } |
1759 | ||
1da177e4 LT |
1760 | /* |
1761 | * Let a parent know about the death of a child. | |
1762 | * For a stopped/continued status change, use do_notify_parent_cldstop instead. | |
2b2a1ff6 | 1763 | * |
53c8f9f1 ON |
1764 | * Returns true if our parent ignored us and so we've switched to |
1765 | * self-reaping. | |
1da177e4 | 1766 | */ |
53c8f9f1 | 1767 | bool do_notify_parent(struct task_struct *tsk, int sig) |
1da177e4 LT |
1768 | { |
1769 | struct siginfo info; | |
1770 | unsigned long flags; | |
1771 | struct sighand_struct *psig; | |
53c8f9f1 | 1772 | bool autoreap = false; |
bde8285e | 1773 | u64 utime, stime; |
1da177e4 LT |
1774 | |
1775 | BUG_ON(sig == -1); | |
1776 | ||
1777 | /* do_notify_parent_cldstop should have been called instead. */ | |
e1abb39c | 1778 | BUG_ON(task_is_stopped_or_traced(tsk)); |
1da177e4 | 1779 | |
d21142ec | 1780 | BUG_ON(!tsk->ptrace && |
1da177e4 LT |
1781 | (tsk->group_leader != tsk || !thread_group_empty(tsk))); |
1782 | ||
b6e238dc ON |
1783 | if (sig != SIGCHLD) { |
1784 | /* | |
1785 | * This is only possible if parent == real_parent. | |
1786 | * Check if it has changed security domain. | |
1787 | */ | |
1788 | if (tsk->parent_exec_id != tsk->parent->self_exec_id) | |
1789 | sig = SIGCHLD; | |
1790 | } | |
1791 | ||
faf1f22b | 1792 | clear_siginfo(&info); |
1da177e4 LT |
1793 | info.si_signo = sig; |
1794 | info.si_errno = 0; | |
b488893a | 1795 | /* |
32084504 EB |
1796 | * We are under tasklist_lock here so our parent is tied to |
1797 | * us and cannot change. | |
b488893a | 1798 | * |
32084504 EB |
1799 | * task_active_pid_ns will always return the same pid namespace |
1800 | * until a task passes through release_task. | |
b488893a PE |
1801 | * |
1802 | * write_lock() currently calls preempt_disable() which is the | |
1803 | * same as rcu_read_lock(), but according to Oleg, this is not | |
1804 | * correct to rely on this | |
1805 | */ | |
1806 | rcu_read_lock(); | |
32084504 | 1807 | info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(tsk->parent)); |
54ba47ed EB |
1808 | info.si_uid = from_kuid_munged(task_cred_xxx(tsk->parent, user_ns), |
1809 | task_uid(tsk)); | |
b488893a PE |
1810 | rcu_read_unlock(); |
1811 | ||
bde8285e FW |
1812 | task_cputime(tsk, &utime, &stime); |
1813 | info.si_utime = nsec_to_clock_t(utime + tsk->signal->utime); | |
1814 | info.si_stime = nsec_to_clock_t(stime + tsk->signal->stime); | |
1da177e4 LT |
1815 | |
1816 | info.si_status = tsk->exit_code & 0x7f; | |
1817 | if (tsk->exit_code & 0x80) | |
1818 | info.si_code = CLD_DUMPED; | |
1819 | else if (tsk->exit_code & 0x7f) | |
1820 | info.si_code = CLD_KILLED; | |
1821 | else { | |
1822 | info.si_code = CLD_EXITED; | |
1823 | info.si_status = tsk->exit_code >> 8; | |
1824 | } | |
1825 | ||
1826 | psig = tsk->parent->sighand; | |
1827 | spin_lock_irqsave(&psig->siglock, flags); | |
d21142ec | 1828 | if (!tsk->ptrace && sig == SIGCHLD && |
1da177e4 LT |
1829 | (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN || |
1830 | (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) { | |
1831 | /* | |
1832 | * We are exiting and our parent doesn't care. POSIX.1 | |
1833 | * defines special semantics for setting SIGCHLD to SIG_IGN | |
1834 | * or setting the SA_NOCLDWAIT flag: we should be reaped | |
1835 | * automatically and not left for our parent's wait4 call. | |
1836 | * Rather than having the parent do it as a magic kind of | |
1837 | * signal handler, we just set this to tell do_exit that we | |
1838 | * can be cleaned up without becoming a zombie. Note that | |
1839 | * we still call __wake_up_parent in this case, because a | |
1840 | * blocked sys_wait4 might now return -ECHILD. | |
1841 | * | |
1842 | * Whether we send SIGCHLD or not for SA_NOCLDWAIT | |
1843 | * is implementation-defined: we do (if you don't want | |
1844 | * it, just use SIG_IGN instead). | |
1845 | */ | |
53c8f9f1 | 1846 | autoreap = true; |
1da177e4 | 1847 | if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN) |
53c8f9f1 | 1848 | sig = 0; |
1da177e4 | 1849 | } |
53c8f9f1 | 1850 | if (valid_signal(sig) && sig) |
1da177e4 LT |
1851 | __group_send_sig_info(sig, &info, tsk->parent); |
1852 | __wake_up_parent(tsk, tsk->parent); | |
1853 | spin_unlock_irqrestore(&psig->siglock, flags); | |
2b2a1ff6 | 1854 | |
53c8f9f1 | 1855 | return autoreap; |
1da177e4 LT |
1856 | } |
1857 | ||
75b95953 TH |
1858 | /** |
1859 | * do_notify_parent_cldstop - notify parent of stopped/continued state change | |
1860 | * @tsk: task reporting the state change | |
1861 | * @for_ptracer: the notification is for ptracer | |
1862 | * @why: CLD_{CONTINUED|STOPPED|TRAPPED} to report | |
1863 | * | |
1864 | * Notify @tsk's parent that the stopped/continued state has changed. If | |
1865 | * @for_ptracer is %false, @tsk's group leader notifies to its real parent. | |
1866 | * If %true, @tsk reports to @tsk->parent which should be the ptracer. | |
1867 | * | |
1868 | * CONTEXT: | |
1869 | * Must be called with tasklist_lock at least read locked. | |
1870 | */ | |
1871 | static void do_notify_parent_cldstop(struct task_struct *tsk, | |
1872 | bool for_ptracer, int why) | |
1da177e4 LT |
1873 | { |
1874 | struct siginfo info; | |
1875 | unsigned long flags; | |
bc505a47 | 1876 | struct task_struct *parent; |
1da177e4 | 1877 | struct sighand_struct *sighand; |
bde8285e | 1878 | u64 utime, stime; |
1da177e4 | 1879 | |
75b95953 | 1880 | if (for_ptracer) { |
bc505a47 | 1881 | parent = tsk->parent; |
75b95953 | 1882 | } else { |
bc505a47 ON |
1883 | tsk = tsk->group_leader; |
1884 | parent = tsk->real_parent; | |
1885 | } | |
1886 | ||
faf1f22b | 1887 | clear_siginfo(&info); |
1da177e4 LT |
1888 | info.si_signo = SIGCHLD; |
1889 | info.si_errno = 0; | |
b488893a | 1890 | /* |
5aba085e | 1891 | * see comment in do_notify_parent() about the following 4 lines |
b488893a PE |
1892 | */ |
1893 | rcu_read_lock(); | |
17cf22c3 | 1894 | info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(parent)); |
54ba47ed | 1895 | info.si_uid = from_kuid_munged(task_cred_xxx(parent, user_ns), task_uid(tsk)); |
b488893a PE |
1896 | rcu_read_unlock(); |
1897 | ||
bde8285e FW |
1898 | task_cputime(tsk, &utime, &stime); |
1899 | info.si_utime = nsec_to_clock_t(utime); | |
1900 | info.si_stime = nsec_to_clock_t(stime); | |
1da177e4 LT |
1901 | |
1902 | info.si_code = why; | |
1903 | switch (why) { | |
1904 | case CLD_CONTINUED: | |
1905 | info.si_status = SIGCONT; | |
1906 | break; | |
1907 | case CLD_STOPPED: | |
1908 | info.si_status = tsk->signal->group_exit_code & 0x7f; | |
1909 | break; | |
1910 | case CLD_TRAPPED: | |
1911 | info.si_status = tsk->exit_code & 0x7f; | |
1912 | break; | |
1913 | default: | |
1914 | BUG(); | |
1915 | } | |
1916 | ||
1917 | sighand = parent->sighand; | |
1918 | spin_lock_irqsave(&sighand->siglock, flags); | |
1919 | if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN && | |
1920 | !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP)) | |
1921 | __group_send_sig_info(SIGCHLD, &info, parent); | |
1922 | /* | |
1923 | * Even if SIGCHLD is not generated, we must wake up wait4 calls. | |
1924 | */ | |
1925 | __wake_up_parent(tsk, parent); | |
1926 | spin_unlock_irqrestore(&sighand->siglock, flags); | |
1927 | } | |
1928 | ||
6527de95 | 1929 | static inline bool may_ptrace_stop(void) |
d5f70c00 | 1930 | { |
d21142ec | 1931 | if (!likely(current->ptrace)) |
6527de95 | 1932 | return false; |
d5f70c00 ON |
1933 | /* |
1934 | * Are we in the middle of do_coredump? | |
1935 | * If so and our tracer is also part of the coredump stopping | |
1936 | * is a deadlock situation, and pointless because our tracer | |
1937 | * is dead so don't allow us to stop. | |
1938 | * If SIGKILL was already sent before the caller unlocked | |
999d9fc1 | 1939 | * ->siglock we must see ->core_state != NULL. Otherwise it |
d5f70c00 | 1940 | * is safe to enter schedule(). |
9899d11f ON |
1941 | * |
1942 | * This is almost outdated, a task with the pending SIGKILL can't | |
1943 | * block in TASK_TRACED. But PTRACE_EVENT_EXIT can be reported | |
1944 | * after SIGKILL was already dequeued. | |
d5f70c00 | 1945 | */ |
999d9fc1 | 1946 | if (unlikely(current->mm->core_state) && |
d5f70c00 | 1947 | unlikely(current->mm == current->parent->mm)) |
6527de95 | 1948 | return false; |
d5f70c00 | 1949 | |
6527de95 | 1950 | return true; |
d5f70c00 ON |
1951 | } |
1952 | ||
1a669c2f | 1953 | /* |
5aba085e | 1954 | * Return non-zero if there is a SIGKILL that should be waking us up. |
1a669c2f RM |
1955 | * Called with the siglock held. |
1956 | */ | |
f99e9d8c | 1957 | static bool sigkill_pending(struct task_struct *tsk) |
1a669c2f | 1958 | { |
f99e9d8c CB |
1959 | return sigismember(&tsk->pending.signal, SIGKILL) || |
1960 | sigismember(&tsk->signal->shared_pending.signal, SIGKILL); | |
1a669c2f RM |
1961 | } |
1962 | ||
1da177e4 LT |
1963 | /* |
1964 | * This must be called with current->sighand->siglock held. | |
1965 | * | |
1966 | * This should be the path for all ptrace stops. | |
1967 | * We always set current->last_siginfo while stopped here. | |
1968 | * That makes it a way to test a stopped process for | |
1969 | * being ptrace-stopped vs being job-control-stopped. | |
1970 | * | |
20686a30 ON |
1971 | * If we actually decide not to stop at all because the tracer |
1972 | * is gone, we keep current->exit_code unless clear_code. | |
1da177e4 | 1973 | */ |
fe1bc6a0 | 1974 | static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info) |
b8401150 NK |
1975 | __releases(¤t->sighand->siglock) |
1976 | __acquires(¤t->sighand->siglock) | |
1da177e4 | 1977 | { |
ceb6bd67 TH |
1978 | bool gstop_done = false; |
1979 | ||
1a669c2f RM |
1980 | if (arch_ptrace_stop_needed(exit_code, info)) { |
1981 | /* | |
1982 | * The arch code has something special to do before a | |
1983 | * ptrace stop. This is allowed to block, e.g. for faults | |
1984 | * on user stack pages. We can't keep the siglock while | |
1985 | * calling arch_ptrace_stop, so we must release it now. | |
1986 | * To preserve proper semantics, we must do this before | |
1987 | * any signal bookkeeping like checking group_stop_count. | |
1988 | * Meanwhile, a SIGKILL could come in before we retake the | |
1989 | * siglock. That must prevent us from sleeping in TASK_TRACED. | |
1990 | * So after regaining the lock, we must check for SIGKILL. | |
1991 | */ | |
1992 | spin_unlock_irq(¤t->sighand->siglock); | |
1993 | arch_ptrace_stop(exit_code, info); | |
1994 | spin_lock_irq(¤t->sighand->siglock); | |
3d749b9e ON |
1995 | if (sigkill_pending(current)) |
1996 | return; | |
1a669c2f RM |
1997 | } |
1998 | ||
b5bf9a90 PZ |
1999 | set_special_state(TASK_TRACED); |
2000 | ||
1da177e4 | 2001 | /* |
81be24b8 TH |
2002 | * We're committing to trapping. TRACED should be visible before |
2003 | * TRAPPING is cleared; otherwise, the tracer might fail do_wait(). | |
2004 | * Also, transition to TRACED and updates to ->jobctl should be | |
2005 | * atomic with respect to siglock and should be done after the arch | |
2006 | * hook as siglock is released and regrabbed across it. | |
b5bf9a90 PZ |
2007 | * |
2008 | * TRACER TRACEE | |
2009 | * | |
2010 | * ptrace_attach() | |
2011 | * [L] wait_on_bit(JOBCTL_TRAPPING) [S] set_special_state(TRACED) | |
2012 | * do_wait() | |
2013 | * set_current_state() smp_wmb(); | |
2014 | * ptrace_do_wait() | |
2015 | * wait_task_stopped() | |
2016 | * task_stopped_code() | |
2017 | * [L] task_is_traced() [S] task_clear_jobctl_trapping(); | |
1da177e4 | 2018 | */ |
b5bf9a90 | 2019 | smp_wmb(); |
1da177e4 LT |
2020 | |
2021 | current->last_siginfo = info; | |
2022 | current->exit_code = exit_code; | |
2023 | ||
d79fdd6d | 2024 | /* |
0ae8ce1c TH |
2025 | * If @why is CLD_STOPPED, we're trapping to participate in a group |
2026 | * stop. Do the bookkeeping. Note that if SIGCONT was delievered | |
73ddff2b TH |
2027 | * across siglock relocks since INTERRUPT was scheduled, PENDING |
2028 | * could be clear now. We act as if SIGCONT is received after | |
2029 | * TASK_TRACED is entered - ignore it. | |
d79fdd6d | 2030 | */ |
a8f072c1 | 2031 | if (why == CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING)) |
ceb6bd67 | 2032 | gstop_done = task_participate_group_stop(current); |
d79fdd6d | 2033 | |
fb1d910c | 2034 | /* any trap clears pending STOP trap, STOP trap clears NOTIFY */ |
73ddff2b | 2035 | task_clear_jobctl_pending(current, JOBCTL_TRAP_STOP); |
fb1d910c TH |
2036 | if (info && info->si_code >> 8 == PTRACE_EVENT_STOP) |
2037 | task_clear_jobctl_pending(current, JOBCTL_TRAP_NOTIFY); | |
73ddff2b | 2038 | |
81be24b8 | 2039 | /* entering a trap, clear TRAPPING */ |
a8f072c1 | 2040 | task_clear_jobctl_trapping(current); |
d79fdd6d | 2041 | |
1da177e4 LT |
2042 | spin_unlock_irq(¤t->sighand->siglock); |
2043 | read_lock(&tasklist_lock); | |
3d749b9e | 2044 | if (may_ptrace_stop()) { |
ceb6bd67 TH |
2045 | /* |
2046 | * Notify parents of the stop. | |
2047 | * | |
2048 | * While ptraced, there are two parents - the ptracer and | |
2049 | * the real_parent of the group_leader. The ptracer should | |
2050 | * know about every stop while the real parent is only | |
2051 | * interested in the completion of group stop. The states | |
2052 | * for the two don't interact with each other. Notify | |
2053 | * separately unless they're gonna be duplicates. | |
2054 | */ | |
2055 | do_notify_parent_cldstop(current, true, why); | |
bb3696da | 2056 | if (gstop_done && ptrace_reparented(current)) |
ceb6bd67 TH |
2057 | do_notify_parent_cldstop(current, false, why); |
2058 | ||
53da1d94 MS |
2059 | /* |
2060 | * Don't want to allow preemption here, because | |
2061 | * sys_ptrace() needs this task to be inactive. | |
2062 | * | |
2063 | * XXX: implement read_unlock_no_resched(). | |
2064 | */ | |
2065 | preempt_disable(); | |
1da177e4 | 2066 | read_unlock(&tasklist_lock); |
53da1d94 | 2067 | preempt_enable_no_resched(); |
5d8f72b5 | 2068 | freezable_schedule(); |
1da177e4 LT |
2069 | } else { |
2070 | /* | |
2071 | * By the time we got the lock, our tracer went away. | |
6405f7f4 | 2072 | * Don't drop the lock yet, another tracer may come. |
ceb6bd67 TH |
2073 | * |
2074 | * If @gstop_done, the ptracer went away between group stop | |
2075 | * completion and here. During detach, it would have set | |
a8f072c1 TH |
2076 | * JOBCTL_STOP_PENDING on us and we'll re-enter |
2077 | * TASK_STOPPED in do_signal_stop() on return, so notifying | |
2078 | * the real parent of the group stop completion is enough. | |
1da177e4 | 2079 | */ |
ceb6bd67 TH |
2080 | if (gstop_done) |
2081 | do_notify_parent_cldstop(current, false, why); | |
2082 | ||
9899d11f | 2083 | /* tasklist protects us from ptrace_freeze_traced() */ |
6405f7f4 | 2084 | __set_current_state(TASK_RUNNING); |
20686a30 ON |
2085 | if (clear_code) |
2086 | current->exit_code = 0; | |
6405f7f4 | 2087 | read_unlock(&tasklist_lock); |
1da177e4 LT |
2088 | } |
2089 | ||
2090 | /* | |
2091 | * We are back. Now reacquire the siglock before touching | |
2092 | * last_siginfo, so that we are sure to have synchronized with | |
2093 | * any signal-sending on another CPU that wants to examine it. | |
2094 | */ | |
2095 | spin_lock_irq(¤t->sighand->siglock); | |
2096 | current->last_siginfo = NULL; | |
2097 | ||
544b2c91 TH |
2098 | /* LISTENING can be set only during STOP traps, clear it */ |
2099 | current->jobctl &= ~JOBCTL_LISTENING; | |
2100 | ||
1da177e4 LT |
2101 | /* |
2102 | * Queued signals ignored us while we were stopped for tracing. | |
2103 | * So check for any that we should take before resuming user mode. | |
b74d0deb | 2104 | * This sets TIF_SIGPENDING, but never clears it. |
1da177e4 | 2105 | */ |
b74d0deb | 2106 | recalc_sigpending_tsk(current); |
1da177e4 LT |
2107 | } |
2108 | ||
3544d72a | 2109 | static void ptrace_do_notify(int signr, int exit_code, int why) |
1da177e4 LT |
2110 | { |
2111 | siginfo_t info; | |
2112 | ||
faf1f22b | 2113 | clear_siginfo(&info); |
3544d72a | 2114 | info.si_signo = signr; |
1da177e4 | 2115 | info.si_code = exit_code; |
b488893a | 2116 | info.si_pid = task_pid_vnr(current); |
078de5f7 | 2117 | info.si_uid = from_kuid_munged(current_user_ns(), current_uid()); |
1da177e4 LT |
2118 | |
2119 | /* Let the debugger run. */ | |
3544d72a TH |
2120 | ptrace_stop(exit_code, why, 1, &info); |
2121 | } | |
2122 | ||
2123 | void ptrace_notify(int exit_code) | |
2124 | { | |
2125 | BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP); | |
f784e8a7 ON |
2126 | if (unlikely(current->task_works)) |
2127 | task_work_run(); | |
3544d72a | 2128 | |
1da177e4 | 2129 | spin_lock_irq(¤t->sighand->siglock); |
3544d72a | 2130 | ptrace_do_notify(SIGTRAP, exit_code, CLD_TRAPPED); |
1da177e4 LT |
2131 | spin_unlock_irq(¤t->sighand->siglock); |
2132 | } | |
2133 | ||
73ddff2b TH |
2134 | /** |
2135 | * do_signal_stop - handle group stop for SIGSTOP and other stop signals | |
2136 | * @signr: signr causing group stop if initiating | |
2137 | * | |
2138 | * If %JOBCTL_STOP_PENDING is not set yet, initiate group stop with @signr | |
2139 | * and participate in it. If already set, participate in the existing | |
2140 | * group stop. If participated in a group stop (and thus slept), %true is | |
2141 | * returned with siglock released. | |
2142 | * | |
2143 | * If ptraced, this function doesn't handle stop itself. Instead, | |
2144 | * %JOBCTL_TRAP_STOP is scheduled and %false is returned with siglock | |
2145 | * untouched. The caller must ensure that INTERRUPT trap handling takes | |
2146 | * places afterwards. | |
2147 | * | |
2148 | * CONTEXT: | |
2149 | * Must be called with @current->sighand->siglock held, which is released | |
2150 | * on %true return. | |
2151 | * | |
2152 | * RETURNS: | |
2153 | * %false if group stop is already cancelled or ptrace trap is scheduled. | |
2154 | * %true if participated in group stop. | |
1da177e4 | 2155 | */ |
73ddff2b TH |
2156 | static bool do_signal_stop(int signr) |
2157 | __releases(¤t->sighand->siglock) | |
1da177e4 LT |
2158 | { |
2159 | struct signal_struct *sig = current->signal; | |
1da177e4 | 2160 | |
a8f072c1 | 2161 | if (!(current->jobctl & JOBCTL_STOP_PENDING)) { |
b76808e6 | 2162 | unsigned long gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME; |
f558b7e4 ON |
2163 | struct task_struct *t; |
2164 | ||
a8f072c1 TH |
2165 | /* signr will be recorded in task->jobctl for retries */ |
2166 | WARN_ON_ONCE(signr & ~JOBCTL_STOP_SIGMASK); | |
d79fdd6d | 2167 | |
a8f072c1 | 2168 | if (!likely(current->jobctl & JOBCTL_STOP_DEQUEUED) || |
573cf9ad | 2169 | unlikely(signal_group_exit(sig))) |
73ddff2b | 2170 | return false; |
1da177e4 | 2171 | /* |
408a37de TH |
2172 | * There is no group stop already in progress. We must |
2173 | * initiate one now. | |
2174 | * | |
2175 | * While ptraced, a task may be resumed while group stop is | |
2176 | * still in effect and then receive a stop signal and | |
2177 | * initiate another group stop. This deviates from the | |
2178 | * usual behavior as two consecutive stop signals can't | |
780006ea ON |
2179 | * cause two group stops when !ptraced. That is why we |
2180 | * also check !task_is_stopped(t) below. | |
408a37de TH |
2181 | * |
2182 | * The condition can be distinguished by testing whether | |
2183 | * SIGNAL_STOP_STOPPED is already set. Don't generate | |
2184 | * group_exit_code in such case. | |
2185 | * | |
2186 | * This is not necessary for SIGNAL_STOP_CONTINUED because | |
2187 | * an intervening stop signal is required to cause two | |
2188 | * continued events regardless of ptrace. | |
1da177e4 | 2189 | */ |
408a37de TH |
2190 | if (!(sig->flags & SIGNAL_STOP_STOPPED)) |
2191 | sig->group_exit_code = signr; | |
1da177e4 | 2192 | |
7dd3db54 TH |
2193 | sig->group_stop_count = 0; |
2194 | ||
2195 | if (task_set_jobctl_pending(current, signr | gstop)) | |
2196 | sig->group_stop_count++; | |
1da177e4 | 2197 | |
8d38f203 ON |
2198 | t = current; |
2199 | while_each_thread(current, t) { | |
1da177e4 | 2200 | /* |
a122b341 ON |
2201 | * Setting state to TASK_STOPPED for a group |
2202 | * stop is always done with the siglock held, | |
2203 | * so this check has no races. | |
1da177e4 | 2204 | */ |
7dd3db54 TH |
2205 | if (!task_is_stopped(t) && |
2206 | task_set_jobctl_pending(t, signr | gstop)) { | |
ae6d2ed7 | 2207 | sig->group_stop_count++; |
fb1d910c TH |
2208 | if (likely(!(t->ptrace & PT_SEIZED))) |
2209 | signal_wake_up(t, 0); | |
2210 | else | |
2211 | ptrace_trap_notify(t); | |
a122b341 | 2212 | } |
d79fdd6d | 2213 | } |
1da177e4 | 2214 | } |
73ddff2b | 2215 | |
d21142ec | 2216 | if (likely(!current->ptrace)) { |
5224fa36 | 2217 | int notify = 0; |
1da177e4 | 2218 | |
5224fa36 TH |
2219 | /* |
2220 | * If there are no other threads in the group, or if there | |
2221 | * is a group stop in progress and we are the last to stop, | |
2222 | * report to the parent. | |
2223 | */ | |
2224 | if (task_participate_group_stop(current)) | |
2225 | notify = CLD_STOPPED; | |
2226 | ||
b5bf9a90 | 2227 | set_special_state(TASK_STOPPED); |
5224fa36 TH |
2228 | spin_unlock_irq(¤t->sighand->siglock); |
2229 | ||
62bcf9d9 TH |
2230 | /* |
2231 | * Notify the parent of the group stop completion. Because | |
2232 | * we're not holding either the siglock or tasklist_lock | |
2233 | * here, ptracer may attach inbetween; however, this is for | |
2234 | * group stop and should always be delivered to the real | |
2235 | * parent of the group leader. The new ptracer will get | |
2236 | * its notification when this task transitions into | |
2237 | * TASK_TRACED. | |
2238 | */ | |
5224fa36 TH |
2239 | if (notify) { |
2240 | read_lock(&tasklist_lock); | |
62bcf9d9 | 2241 | do_notify_parent_cldstop(current, false, notify); |
5224fa36 TH |
2242 | read_unlock(&tasklist_lock); |
2243 | } | |
2244 | ||
2245 | /* Now we don't run again until woken by SIGCONT or SIGKILL */ | |
5d8f72b5 | 2246 | freezable_schedule(); |
73ddff2b | 2247 | return true; |
d79fdd6d | 2248 | } else { |
73ddff2b TH |
2249 | /* |
2250 | * While ptraced, group stop is handled by STOP trap. | |
2251 | * Schedule it and let the caller deal with it. | |
2252 | */ | |
2253 | task_set_jobctl_pending(current, JOBCTL_TRAP_STOP); | |
2254 | return false; | |
ae6d2ed7 | 2255 | } |
73ddff2b | 2256 | } |
1da177e4 | 2257 | |
73ddff2b TH |
2258 | /** |
2259 | * do_jobctl_trap - take care of ptrace jobctl traps | |
2260 | * | |
3544d72a TH |
2261 | * When PT_SEIZED, it's used for both group stop and explicit |
2262 | * SEIZE/INTERRUPT traps. Both generate PTRACE_EVENT_STOP trap with | |
2263 | * accompanying siginfo. If stopped, lower eight bits of exit_code contain | |
2264 | * the stop signal; otherwise, %SIGTRAP. | |
2265 | * | |
2266 | * When !PT_SEIZED, it's used only for group stop trap with stop signal | |
2267 | * number as exit_code and no siginfo. | |
73ddff2b TH |
2268 | * |
2269 | * CONTEXT: | |
2270 | * Must be called with @current->sighand->siglock held, which may be | |
2271 | * released and re-acquired before returning with intervening sleep. | |
2272 | */ | |
2273 | static void do_jobctl_trap(void) | |
2274 | { | |
3544d72a | 2275 | struct signal_struct *signal = current->signal; |
73ddff2b | 2276 | int signr = current->jobctl & JOBCTL_STOP_SIGMASK; |
ae6d2ed7 | 2277 | |
3544d72a TH |
2278 | if (current->ptrace & PT_SEIZED) { |
2279 | if (!signal->group_stop_count && | |
2280 | !(signal->flags & SIGNAL_STOP_STOPPED)) | |
2281 | signr = SIGTRAP; | |
2282 | WARN_ON_ONCE(!signr); | |
2283 | ptrace_do_notify(signr, signr | (PTRACE_EVENT_STOP << 8), | |
2284 | CLD_STOPPED); | |
2285 | } else { | |
2286 | WARN_ON_ONCE(!signr); | |
2287 | ptrace_stop(signr, CLD_STOPPED, 0, NULL); | |
2288 | current->exit_code = 0; | |
ae6d2ed7 | 2289 | } |
1da177e4 LT |
2290 | } |
2291 | ||
94eb22d5 | 2292 | static int ptrace_signal(int signr, siginfo_t *info) |
18c98b65 | 2293 | { |
8a352418 ON |
2294 | /* |
2295 | * We do not check sig_kernel_stop(signr) but set this marker | |
2296 | * unconditionally because we do not know whether debugger will | |
2297 | * change signr. This flag has no meaning unless we are going | |
2298 | * to stop after return from ptrace_stop(). In this case it will | |
2299 | * be checked in do_signal_stop(), we should only stop if it was | |
2300 | * not cleared by SIGCONT while we were sleeping. See also the | |
2301 | * comment in dequeue_signal(). | |
2302 | */ | |
2303 | current->jobctl |= JOBCTL_STOP_DEQUEUED; | |
fe1bc6a0 | 2304 | ptrace_stop(signr, CLD_TRAPPED, 0, info); |
18c98b65 RM |
2305 | |
2306 | /* We're back. Did the debugger cancel the sig? */ | |
2307 | signr = current->exit_code; | |
2308 | if (signr == 0) | |
2309 | return signr; | |
2310 | ||
2311 | current->exit_code = 0; | |
2312 | ||
5aba085e RD |
2313 | /* |
2314 | * Update the siginfo structure if the signal has | |
2315 | * changed. If the debugger wanted something | |
2316 | * specific in the siginfo structure then it should | |
2317 | * have updated *info via PTRACE_SETSIGINFO. | |
2318 | */ | |
18c98b65 | 2319 | if (signr != info->si_signo) { |
faf1f22b | 2320 | clear_siginfo(info); |
18c98b65 RM |
2321 | info->si_signo = signr; |
2322 | info->si_errno = 0; | |
2323 | info->si_code = SI_USER; | |
6b550f94 | 2324 | rcu_read_lock(); |
18c98b65 | 2325 | info->si_pid = task_pid_vnr(current->parent); |
54ba47ed EB |
2326 | info->si_uid = from_kuid_munged(current_user_ns(), |
2327 | task_uid(current->parent)); | |
6b550f94 | 2328 | rcu_read_unlock(); |
18c98b65 RM |
2329 | } |
2330 | ||
2331 | /* If the (new) signal is now blocked, requeue it. */ | |
2332 | if (sigismember(¤t->blocked, signr)) { | |
b21c5bd5 | 2333 | send_signal(signr, info, current, PIDTYPE_PID); |
18c98b65 RM |
2334 | signr = 0; |
2335 | } | |
2336 | ||
2337 | return signr; | |
2338 | } | |
2339 | ||
20ab7218 | 2340 | bool get_signal(struct ksignal *ksig) |
1da177e4 | 2341 | { |
f6b76d4f ON |
2342 | struct sighand_struct *sighand = current->sighand; |
2343 | struct signal_struct *signal = current->signal; | |
2344 | int signr; | |
1da177e4 | 2345 | |
f784e8a7 ON |
2346 | if (unlikely(current->task_works)) |
2347 | task_work_run(); | |
72667028 | 2348 | |
0326f5a9 | 2349 | if (unlikely(uprobe_deny_signal())) |
20ab7218 | 2350 | return false; |
0326f5a9 | 2351 | |
13b1c3d4 | 2352 | /* |
5d8f72b5 ON |
2353 | * Do this once, we can't return to user-mode if freezing() == T. |
2354 | * do_signal_stop() and ptrace_stop() do freezable_schedule() and | |
2355 | * thus do not need another check after return. | |
13b1c3d4 | 2356 | */ |
fc558a74 RW |
2357 | try_to_freeze(); |
2358 | ||
5d8f72b5 | 2359 | relock: |
f6b76d4f | 2360 | spin_lock_irq(&sighand->siglock); |
021e1ae3 ON |
2361 | /* |
2362 | * Every stopped thread goes here after wakeup. Check to see if | |
2363 | * we should notify the parent, prepare_signal(SIGCONT) encodes | |
2364 | * the CLD_ si_code into SIGNAL_CLD_MASK bits. | |
2365 | */ | |
f6b76d4f | 2366 | if (unlikely(signal->flags & SIGNAL_CLD_MASK)) { |
c672af35 TH |
2367 | int why; |
2368 | ||
2369 | if (signal->flags & SIGNAL_CLD_CONTINUED) | |
2370 | why = CLD_CONTINUED; | |
2371 | else | |
2372 | why = CLD_STOPPED; | |
2373 | ||
f6b76d4f | 2374 | signal->flags &= ~SIGNAL_CLD_MASK; |
e4420551 | 2375 | |
ae6d2ed7 | 2376 | spin_unlock_irq(&sighand->siglock); |
fa00b80b | 2377 | |
ceb6bd67 TH |
2378 | /* |
2379 | * Notify the parent that we're continuing. This event is | |
2380 | * always per-process and doesn't make whole lot of sense | |
2381 | * for ptracers, who shouldn't consume the state via | |
2382 | * wait(2) either, but, for backward compatibility, notify | |
2383 | * the ptracer of the group leader too unless it's gonna be | |
2384 | * a duplicate. | |
2385 | */ | |
edf2ed15 | 2386 | read_lock(&tasklist_lock); |
ceb6bd67 TH |
2387 | do_notify_parent_cldstop(current, false, why); |
2388 | ||
bb3696da ON |
2389 | if (ptrace_reparented(current->group_leader)) |
2390 | do_notify_parent_cldstop(current->group_leader, | |
2391 | true, why); | |
edf2ed15 | 2392 | read_unlock(&tasklist_lock); |
ceb6bd67 | 2393 | |
e4420551 ON |
2394 | goto relock; |
2395 | } | |
2396 | ||
1da177e4 LT |
2397 | for (;;) { |
2398 | struct k_sigaction *ka; | |
1be53963 | 2399 | |
dd1d6772 TH |
2400 | if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) && |
2401 | do_signal_stop(0)) | |
7bcf6a2c | 2402 | goto relock; |
1be53963 | 2403 | |
73ddff2b TH |
2404 | if (unlikely(current->jobctl & JOBCTL_TRAP_MASK)) { |
2405 | do_jobctl_trap(); | |
2406 | spin_unlock_irq(&sighand->siglock); | |
2407 | goto relock; | |
2408 | } | |
1da177e4 | 2409 | |
828b1f65 | 2410 | signr = dequeue_signal(current, ¤t->blocked, &ksig->info); |
7bcf6a2c | 2411 | |
dd1d6772 TH |
2412 | if (!signr) |
2413 | break; /* will return 0 */ | |
7bcf6a2c | 2414 | |
8a352418 | 2415 | if (unlikely(current->ptrace) && signr != SIGKILL) { |
828b1f65 | 2416 | signr = ptrace_signal(signr, &ksig->info); |
dd1d6772 TH |
2417 | if (!signr) |
2418 | continue; | |
1da177e4 LT |
2419 | } |
2420 | ||
dd1d6772 TH |
2421 | ka = &sighand->action[signr-1]; |
2422 | ||
f9d4257e | 2423 | /* Trace actually delivered signals. */ |
828b1f65 | 2424 | trace_signal_deliver(signr, &ksig->info, ka); |
f9d4257e | 2425 | |
1da177e4 LT |
2426 | if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */ |
2427 | continue; | |
2428 | if (ka->sa.sa_handler != SIG_DFL) { | |
2429 | /* Run the handler. */ | |
828b1f65 | 2430 | ksig->ka = *ka; |
1da177e4 LT |
2431 | |
2432 | if (ka->sa.sa_flags & SA_ONESHOT) | |
2433 | ka->sa.sa_handler = SIG_DFL; | |
2434 | ||
2435 | break; /* will return non-zero "signr" value */ | |
2436 | } | |
2437 | ||
2438 | /* | |
2439 | * Now we are doing the default action for this signal. | |
2440 | */ | |
2441 | if (sig_kernel_ignore(signr)) /* Default is nothing. */ | |
2442 | continue; | |
2443 | ||
84d73786 | 2444 | /* |
0fbc26a6 | 2445 | * Global init gets no signals it doesn't want. |
b3bfa0cb SB |
2446 | * Container-init gets no signals it doesn't want from same |
2447 | * container. | |
2448 | * | |
2449 | * Note that if global/container-init sees a sig_kernel_only() | |
2450 | * signal here, the signal must have been generated internally | |
2451 | * or must have come from an ancestor namespace. In either | |
2452 | * case, the signal cannot be dropped. | |
84d73786 | 2453 | */ |
fae5fa44 | 2454 | if (unlikely(signal->flags & SIGNAL_UNKILLABLE) && |
b3bfa0cb | 2455 | !sig_kernel_only(signr)) |
1da177e4 LT |
2456 | continue; |
2457 | ||
2458 | if (sig_kernel_stop(signr)) { | |
2459 | /* | |
2460 | * The default action is to stop all threads in | |
2461 | * the thread group. The job control signals | |
2462 | * do nothing in an orphaned pgrp, but SIGSTOP | |
2463 | * always works. Note that siglock needs to be | |
2464 | * dropped during the call to is_orphaned_pgrp() | |
2465 | * because of lock ordering with tasklist_lock. | |
2466 | * This allows an intervening SIGCONT to be posted. | |
2467 | * We need to check for that and bail out if necessary. | |
2468 | */ | |
2469 | if (signr != SIGSTOP) { | |
f6b76d4f | 2470 | spin_unlock_irq(&sighand->siglock); |
1da177e4 LT |
2471 | |
2472 | /* signals can be posted during this window */ | |
2473 | ||
3e7cd6c4 | 2474 | if (is_current_pgrp_orphaned()) |
1da177e4 LT |
2475 | goto relock; |
2476 | ||
f6b76d4f | 2477 | spin_lock_irq(&sighand->siglock); |
1da177e4 LT |
2478 | } |
2479 | ||
828b1f65 | 2480 | if (likely(do_signal_stop(ksig->info.si_signo))) { |
1da177e4 LT |
2481 | /* It released the siglock. */ |
2482 | goto relock; | |
2483 | } | |
2484 | ||
2485 | /* | |
2486 | * We didn't actually stop, due to a race | |
2487 | * with SIGCONT or something like that. | |
2488 | */ | |
2489 | continue; | |
2490 | } | |
2491 | ||
f6b76d4f | 2492 | spin_unlock_irq(&sighand->siglock); |
1da177e4 LT |
2493 | |
2494 | /* | |
2495 | * Anything else is fatal, maybe with a core dump. | |
2496 | */ | |
2497 | current->flags |= PF_SIGNALED; | |
2dce81bf | 2498 | |
1da177e4 | 2499 | if (sig_kernel_coredump(signr)) { |
2dce81bf | 2500 | if (print_fatal_signals) |
828b1f65 | 2501 | print_fatal_signal(ksig->info.si_signo); |
2b5faa4c | 2502 | proc_coredump_connector(current); |
1da177e4 LT |
2503 | /* |
2504 | * If it was able to dump core, this kills all | |
2505 | * other threads in the group and synchronizes with | |
2506 | * their demise. If we lost the race with another | |
2507 | * thread getting here, it set group_exit_code | |
2508 | * first and our do_group_exit call below will use | |
2509 | * that value and ignore the one we pass it. | |
2510 | */ | |
828b1f65 | 2511 | do_coredump(&ksig->info); |
1da177e4 LT |
2512 | } |
2513 | ||
2514 | /* | |
2515 | * Death signals, no core dump. | |
2516 | */ | |
828b1f65 | 2517 | do_group_exit(ksig->info.si_signo); |
1da177e4 LT |
2518 | /* NOTREACHED */ |
2519 | } | |
f6b76d4f | 2520 | spin_unlock_irq(&sighand->siglock); |
828b1f65 RW |
2521 | |
2522 | ksig->sig = signr; | |
2523 | return ksig->sig > 0; | |
1da177e4 LT |
2524 | } |
2525 | ||
5e6292c0 | 2526 | /** |
efee984c | 2527 | * signal_delivered - |
10b1c7ac | 2528 | * @ksig: kernel signal struct |
efee984c | 2529 | * @stepping: nonzero if debugger single-step or block-step in use |
5e6292c0 | 2530 | * |
e227867f | 2531 | * This function should be called when a signal has successfully been |
10b1c7ac | 2532 | * delivered. It updates the blocked signals accordingly (@ksig->ka.sa.sa_mask |
efee984c | 2533 | * is always blocked, and the signal itself is blocked unless %SA_NODEFER |
10b1c7ac | 2534 | * is set in @ksig->ka.sa.sa_flags. Tracing is notified. |
5e6292c0 | 2535 | */ |
10b1c7ac | 2536 | static void signal_delivered(struct ksignal *ksig, int stepping) |
5e6292c0 MF |
2537 | { |
2538 | sigset_t blocked; | |
2539 | ||
a610d6e6 AV |
2540 | /* A signal was successfully delivered, and the |
2541 | saved sigmask was stored on the signal frame, | |
2542 | and will be restored by sigreturn. So we can | |
2543 | simply clear the restore sigmask flag. */ | |
2544 | clear_restore_sigmask(); | |
2545 | ||
10b1c7ac RW |
2546 | sigorsets(&blocked, ¤t->blocked, &ksig->ka.sa.sa_mask); |
2547 | if (!(ksig->ka.sa.sa_flags & SA_NODEFER)) | |
2548 | sigaddset(&blocked, ksig->sig); | |
5e6292c0 | 2549 | set_current_blocked(&blocked); |
df5601f9 | 2550 | tracehook_signal_handler(stepping); |
5e6292c0 MF |
2551 | } |
2552 | ||
2ce5da17 AV |
2553 | void signal_setup_done(int failed, struct ksignal *ksig, int stepping) |
2554 | { | |
2555 | if (failed) | |
2556 | force_sigsegv(ksig->sig, current); | |
2557 | else | |
10b1c7ac | 2558 | signal_delivered(ksig, stepping); |
2ce5da17 AV |
2559 | } |
2560 | ||
0edceb7b ON |
2561 | /* |
2562 | * It could be that complete_signal() picked us to notify about the | |
fec9993d ON |
2563 | * group-wide signal. Other threads should be notified now to take |
2564 | * the shared signals in @which since we will not. | |
0edceb7b | 2565 | */ |
f646e227 | 2566 | static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which) |
0edceb7b | 2567 | { |
f646e227 | 2568 | sigset_t retarget; |
0edceb7b ON |
2569 | struct task_struct *t; |
2570 | ||
f646e227 ON |
2571 | sigandsets(&retarget, &tsk->signal->shared_pending.signal, which); |
2572 | if (sigisemptyset(&retarget)) | |
2573 | return; | |
2574 | ||
0edceb7b ON |
2575 | t = tsk; |
2576 | while_each_thread(tsk, t) { | |
fec9993d ON |
2577 | if (t->flags & PF_EXITING) |
2578 | continue; | |
2579 | ||
2580 | if (!has_pending_signals(&retarget, &t->blocked)) | |
2581 | continue; | |
2582 | /* Remove the signals this thread can handle. */ | |
2583 | sigandsets(&retarget, &retarget, &t->blocked); | |
2584 | ||
2585 | if (!signal_pending(t)) | |
2586 | signal_wake_up(t, 0); | |
2587 | ||
2588 | if (sigisemptyset(&retarget)) | |
2589 | break; | |
0edceb7b ON |
2590 | } |
2591 | } | |
2592 | ||
d12619b5 ON |
2593 | void exit_signals(struct task_struct *tsk) |
2594 | { | |
2595 | int group_stop = 0; | |
f646e227 | 2596 | sigset_t unblocked; |
d12619b5 | 2597 | |
77e4ef99 TH |
2598 | /* |
2599 | * @tsk is about to have PF_EXITING set - lock out users which | |
2600 | * expect stable threadgroup. | |
2601 | */ | |
780de9dd | 2602 | cgroup_threadgroup_change_begin(tsk); |
77e4ef99 | 2603 | |
5dee1707 ON |
2604 | if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) { |
2605 | tsk->flags |= PF_EXITING; | |
780de9dd | 2606 | cgroup_threadgroup_change_end(tsk); |
5dee1707 | 2607 | return; |
d12619b5 ON |
2608 | } |
2609 | ||
5dee1707 | 2610 | spin_lock_irq(&tsk->sighand->siglock); |
d12619b5 ON |
2611 | /* |
2612 | * From now this task is not visible for group-wide signals, | |
2613 | * see wants_signal(), do_signal_stop(). | |
2614 | */ | |
2615 | tsk->flags |= PF_EXITING; | |
77e4ef99 | 2616 | |
780de9dd | 2617 | cgroup_threadgroup_change_end(tsk); |
77e4ef99 | 2618 | |
5dee1707 ON |
2619 | if (!signal_pending(tsk)) |
2620 | goto out; | |
2621 | ||
f646e227 ON |
2622 | unblocked = tsk->blocked; |
2623 | signotset(&unblocked); | |
2624 | retarget_shared_pending(tsk, &unblocked); | |
5dee1707 | 2625 | |
a8f072c1 | 2626 | if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) && |
e5c1902e | 2627 | task_participate_group_stop(tsk)) |
edf2ed15 | 2628 | group_stop = CLD_STOPPED; |
5dee1707 | 2629 | out: |
d12619b5 ON |
2630 | spin_unlock_irq(&tsk->sighand->siglock); |
2631 | ||
62bcf9d9 TH |
2632 | /* |
2633 | * If group stop has completed, deliver the notification. This | |
2634 | * should always go to the real parent of the group leader. | |
2635 | */ | |
ae6d2ed7 | 2636 | if (unlikely(group_stop)) { |
d12619b5 | 2637 | read_lock(&tasklist_lock); |
62bcf9d9 | 2638 | do_notify_parent_cldstop(tsk, false, group_stop); |
d12619b5 ON |
2639 | read_unlock(&tasklist_lock); |
2640 | } | |
2641 | } | |
2642 | ||
1da177e4 LT |
2643 | /* |
2644 | * System call entry points. | |
2645 | */ | |
2646 | ||
41c57892 RD |
2647 | /** |
2648 | * sys_restart_syscall - restart a system call | |
2649 | */ | |
754fe8d2 | 2650 | SYSCALL_DEFINE0(restart_syscall) |
1da177e4 | 2651 | { |
f56141e3 | 2652 | struct restart_block *restart = ¤t->restart_block; |
1da177e4 LT |
2653 | return restart->fn(restart); |
2654 | } | |
2655 | ||
2656 | long do_no_restart_syscall(struct restart_block *param) | |
2657 | { | |
2658 | return -EINTR; | |
2659 | } | |
2660 | ||
b182801a ON |
2661 | static void __set_task_blocked(struct task_struct *tsk, const sigset_t *newset) |
2662 | { | |
2663 | if (signal_pending(tsk) && !thread_group_empty(tsk)) { | |
2664 | sigset_t newblocked; | |
2665 | /* A set of now blocked but previously unblocked signals. */ | |
702a5073 | 2666 | sigandnsets(&newblocked, newset, ¤t->blocked); |
b182801a ON |
2667 | retarget_shared_pending(tsk, &newblocked); |
2668 | } | |
2669 | tsk->blocked = *newset; | |
2670 | recalc_sigpending(); | |
2671 | } | |
2672 | ||
e6fa16ab ON |
2673 | /** |
2674 | * set_current_blocked - change current->blocked mask | |
2675 | * @newset: new mask | |
2676 | * | |
2677 | * It is wrong to change ->blocked directly, this helper should be used | |
2678 | * to ensure the process can't miss a shared signal we are going to block. | |
1da177e4 | 2679 | */ |
77097ae5 AV |
2680 | void set_current_blocked(sigset_t *newset) |
2681 | { | |
77097ae5 | 2682 | sigdelsetmask(newset, sigmask(SIGKILL) | sigmask(SIGSTOP)); |
0c4a8423 | 2683 | __set_current_blocked(newset); |
77097ae5 AV |
2684 | } |
2685 | ||
2686 | void __set_current_blocked(const sigset_t *newset) | |
e6fa16ab ON |
2687 | { |
2688 | struct task_struct *tsk = current; | |
2689 | ||
c7be96af WL |
2690 | /* |
2691 | * In case the signal mask hasn't changed, there is nothing we need | |
2692 | * to do. The current->blocked shouldn't be modified by other task. | |
2693 | */ | |
2694 | if (sigequalsets(&tsk->blocked, newset)) | |
2695 | return; | |
2696 | ||
e6fa16ab | 2697 | spin_lock_irq(&tsk->sighand->siglock); |
b182801a | 2698 | __set_task_blocked(tsk, newset); |
e6fa16ab ON |
2699 | spin_unlock_irq(&tsk->sighand->siglock); |
2700 | } | |
1da177e4 LT |
2701 | |
2702 | /* | |
2703 | * This is also useful for kernel threads that want to temporarily | |
2704 | * (or permanently) block certain signals. | |
2705 | * | |
2706 | * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel | |
2707 | * interface happily blocks "unblockable" signals like SIGKILL | |
2708 | * and friends. | |
2709 | */ | |
2710 | int sigprocmask(int how, sigset_t *set, sigset_t *oldset) | |
2711 | { | |
73ef4aeb ON |
2712 | struct task_struct *tsk = current; |
2713 | sigset_t newset; | |
1da177e4 | 2714 | |
73ef4aeb | 2715 | /* Lockless, only current can change ->blocked, never from irq */ |
a26fd335 | 2716 | if (oldset) |
73ef4aeb | 2717 | *oldset = tsk->blocked; |
a26fd335 | 2718 | |
1da177e4 LT |
2719 | switch (how) { |
2720 | case SIG_BLOCK: | |
73ef4aeb | 2721 | sigorsets(&newset, &tsk->blocked, set); |
1da177e4 LT |
2722 | break; |
2723 | case SIG_UNBLOCK: | |
702a5073 | 2724 | sigandnsets(&newset, &tsk->blocked, set); |
1da177e4 LT |
2725 | break; |
2726 | case SIG_SETMASK: | |
73ef4aeb | 2727 | newset = *set; |
1da177e4 LT |
2728 | break; |
2729 | default: | |
73ef4aeb | 2730 | return -EINVAL; |
1da177e4 | 2731 | } |
a26fd335 | 2732 | |
77097ae5 | 2733 | __set_current_blocked(&newset); |
73ef4aeb | 2734 | return 0; |
1da177e4 | 2735 | } |
fb50f5a4 | 2736 | EXPORT_SYMBOL(sigprocmask); |
1da177e4 | 2737 | |
41c57892 RD |
2738 | /** |
2739 | * sys_rt_sigprocmask - change the list of currently blocked signals | |
2740 | * @how: whether to add, remove, or set signals | |
ada9c933 | 2741 | * @nset: stores pending signals |
41c57892 RD |
2742 | * @oset: previous value of signal mask if non-null |
2743 | * @sigsetsize: size of sigset_t type | |
2744 | */ | |
bb7efee2 | 2745 | SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset, |
17da2bd9 | 2746 | sigset_t __user *, oset, size_t, sigsetsize) |
1da177e4 | 2747 | { |
1da177e4 | 2748 | sigset_t old_set, new_set; |
bb7efee2 | 2749 | int error; |
1da177e4 LT |
2750 | |
2751 | /* XXX: Don't preclude handling different sized sigset_t's. */ | |
2752 | if (sigsetsize != sizeof(sigset_t)) | |
bb7efee2 | 2753 | return -EINVAL; |
1da177e4 | 2754 | |
bb7efee2 ON |
2755 | old_set = current->blocked; |
2756 | ||
2757 | if (nset) { | |
2758 | if (copy_from_user(&new_set, nset, sizeof(sigset_t))) | |
2759 | return -EFAULT; | |
1da177e4 LT |
2760 | sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP)); |
2761 | ||
bb7efee2 | 2762 | error = sigprocmask(how, &new_set, NULL); |
1da177e4 | 2763 | if (error) |
bb7efee2 ON |
2764 | return error; |
2765 | } | |
1da177e4 | 2766 | |
bb7efee2 ON |
2767 | if (oset) { |
2768 | if (copy_to_user(oset, &old_set, sizeof(sigset_t))) | |
2769 | return -EFAULT; | |
1da177e4 | 2770 | } |
bb7efee2 ON |
2771 | |
2772 | return 0; | |
1da177e4 LT |
2773 | } |
2774 | ||
322a56cb | 2775 | #ifdef CONFIG_COMPAT |
322a56cb AV |
2776 | COMPAT_SYSCALL_DEFINE4(rt_sigprocmask, int, how, compat_sigset_t __user *, nset, |
2777 | compat_sigset_t __user *, oset, compat_size_t, sigsetsize) | |
1da177e4 | 2778 | { |
322a56cb AV |
2779 | sigset_t old_set = current->blocked; |
2780 | ||
2781 | /* XXX: Don't preclude handling different sized sigset_t's. */ | |
2782 | if (sigsetsize != sizeof(sigset_t)) | |
2783 | return -EINVAL; | |
2784 | ||
2785 | if (nset) { | |
322a56cb AV |
2786 | sigset_t new_set; |
2787 | int error; | |
3968cf62 | 2788 | if (get_compat_sigset(&new_set, nset)) |
322a56cb | 2789 | return -EFAULT; |
322a56cb AV |
2790 | sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP)); |
2791 | ||
2792 | error = sigprocmask(how, &new_set, NULL); | |
2793 | if (error) | |
2794 | return error; | |
2795 | } | |
f454322e | 2796 | return oset ? put_compat_sigset(oset, &old_set, sizeof(*oset)) : 0; |
322a56cb AV |
2797 | } |
2798 | #endif | |
1da177e4 | 2799 | |
b1d294c8 | 2800 | static void do_sigpending(sigset_t *set) |
1da177e4 | 2801 | { |
1da177e4 | 2802 | spin_lock_irq(¤t->sighand->siglock); |
fe9c1db2 | 2803 | sigorsets(set, ¤t->pending.signal, |
1da177e4 LT |
2804 | ¤t->signal->shared_pending.signal); |
2805 | spin_unlock_irq(¤t->sighand->siglock); | |
2806 | ||
2807 | /* Outside the lock because only this thread touches it. */ | |
fe9c1db2 | 2808 | sigandsets(set, ¤t->blocked, set); |
5aba085e | 2809 | } |
1da177e4 | 2810 | |
41c57892 RD |
2811 | /** |
2812 | * sys_rt_sigpending - examine a pending signal that has been raised | |
2813 | * while blocked | |
20f22ab4 | 2814 | * @uset: stores pending signals |
41c57892 RD |
2815 | * @sigsetsize: size of sigset_t type or larger |
2816 | */ | |
fe9c1db2 | 2817 | SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, uset, size_t, sigsetsize) |
1da177e4 | 2818 | { |
fe9c1db2 | 2819 | sigset_t set; |
176826af DL |
2820 | |
2821 | if (sigsetsize > sizeof(*uset)) | |
2822 | return -EINVAL; | |
2823 | ||
b1d294c8 CB |
2824 | do_sigpending(&set); |
2825 | ||
2826 | if (copy_to_user(uset, &set, sigsetsize)) | |
2827 | return -EFAULT; | |
2828 | ||
2829 | return 0; | |
fe9c1db2 AV |
2830 | } |
2831 | ||
2832 | #ifdef CONFIG_COMPAT | |
fe9c1db2 AV |
2833 | COMPAT_SYSCALL_DEFINE2(rt_sigpending, compat_sigset_t __user *, uset, |
2834 | compat_size_t, sigsetsize) | |
1da177e4 | 2835 | { |
fe9c1db2 | 2836 | sigset_t set; |
176826af DL |
2837 | |
2838 | if (sigsetsize > sizeof(*uset)) | |
2839 | return -EINVAL; | |
2840 | ||
b1d294c8 CB |
2841 | do_sigpending(&set); |
2842 | ||
2843 | return put_compat_sigset(uset, &set, sigsetsize); | |
1da177e4 | 2844 | } |
fe9c1db2 | 2845 | #endif |
1da177e4 | 2846 | |
cc731525 EB |
2847 | enum siginfo_layout siginfo_layout(int sig, int si_code) |
2848 | { | |
2849 | enum siginfo_layout layout = SIL_KILL; | |
2850 | if ((si_code > SI_USER) && (si_code < SI_KERNEL)) { | |
2851 | static const struct { | |
2852 | unsigned char limit, layout; | |
2853 | } filter[] = { | |
2854 | [SIGILL] = { NSIGILL, SIL_FAULT }, | |
2855 | [SIGFPE] = { NSIGFPE, SIL_FAULT }, | |
2856 | [SIGSEGV] = { NSIGSEGV, SIL_FAULT }, | |
2857 | [SIGBUS] = { NSIGBUS, SIL_FAULT }, | |
2858 | [SIGTRAP] = { NSIGTRAP, SIL_FAULT }, | |
018303a9 | 2859 | #if defined(SIGEMT) |
cc731525 EB |
2860 | [SIGEMT] = { NSIGEMT, SIL_FAULT }, |
2861 | #endif | |
2862 | [SIGCHLD] = { NSIGCHLD, SIL_CHLD }, | |
2863 | [SIGPOLL] = { NSIGPOLL, SIL_POLL }, | |
cc731525 | 2864 | [SIGSYS] = { NSIGSYS, SIL_SYS }, |
cc731525 | 2865 | }; |
31931c93 | 2866 | if ((sig < ARRAY_SIZE(filter)) && (si_code <= filter[sig].limit)) { |
cc731525 | 2867 | layout = filter[sig].layout; |
31931c93 EB |
2868 | /* Handle the exceptions */ |
2869 | if ((sig == SIGBUS) && | |
2870 | (si_code >= BUS_MCEERR_AR) && (si_code <= BUS_MCEERR_AO)) | |
2871 | layout = SIL_FAULT_MCEERR; | |
2872 | else if ((sig == SIGSEGV) && (si_code == SEGV_BNDERR)) | |
2873 | layout = SIL_FAULT_BNDERR; | |
2874 | #ifdef SEGV_PKUERR | |
2875 | else if ((sig == SIGSEGV) && (si_code == SEGV_PKUERR)) | |
2876 | layout = SIL_FAULT_PKUERR; | |
2877 | #endif | |
2878 | } | |
cc731525 EB |
2879 | else if (si_code <= NSIGPOLL) |
2880 | layout = SIL_POLL; | |
2881 | } else { | |
2882 | if (si_code == SI_TIMER) | |
2883 | layout = SIL_TIMER; | |
2884 | else if (si_code == SI_SIGIO) | |
2885 | layout = SIL_POLL; | |
2886 | else if (si_code < 0) | |
2887 | layout = SIL_RT; | |
cc731525 EB |
2888 | } |
2889 | return layout; | |
2890 | } | |
2891 | ||
ce395960 | 2892 | int copy_siginfo_to_user(siginfo_t __user *to, const siginfo_t *from) |
1da177e4 | 2893 | { |
c999b933 | 2894 | if (copy_to_user(to, from , sizeof(struct siginfo))) |
1da177e4 | 2895 | return -EFAULT; |
c999b933 | 2896 | return 0; |
1da177e4 LT |
2897 | } |
2898 | ||
4cd2e0e7 EB |
2899 | int copy_siginfo_from_user(siginfo_t *to, const siginfo_t __user *from) |
2900 | { | |
2901 | if (copy_from_user(to, from, sizeof(struct siginfo))) | |
2902 | return -EFAULT; | |
2903 | return 0; | |
2904 | } | |
2905 | ||
212a36a1 | 2906 | #ifdef CONFIG_COMPAT |
ea64d5ac EB |
2907 | int copy_siginfo_to_user32(struct compat_siginfo __user *to, |
2908 | const struct siginfo *from) | |
2909 | #if defined(CONFIG_X86_X32_ABI) || defined(CONFIG_IA32_EMULATION) | |
2910 | { | |
2911 | return __copy_siginfo_to_user32(to, from, in_x32_syscall()); | |
2912 | } | |
2913 | int __copy_siginfo_to_user32(struct compat_siginfo __user *to, | |
2914 | const struct siginfo *from, bool x32_ABI) | |
2915 | #endif | |
2916 | { | |
2917 | struct compat_siginfo new; | |
2918 | memset(&new, 0, sizeof(new)); | |
2919 | ||
2920 | new.si_signo = from->si_signo; | |
2921 | new.si_errno = from->si_errno; | |
2922 | new.si_code = from->si_code; | |
2923 | switch(siginfo_layout(from->si_signo, from->si_code)) { | |
2924 | case SIL_KILL: | |
2925 | new.si_pid = from->si_pid; | |
2926 | new.si_uid = from->si_uid; | |
2927 | break; | |
2928 | case SIL_TIMER: | |
2929 | new.si_tid = from->si_tid; | |
2930 | new.si_overrun = from->si_overrun; | |
2931 | new.si_int = from->si_int; | |
2932 | break; | |
2933 | case SIL_POLL: | |
2934 | new.si_band = from->si_band; | |
2935 | new.si_fd = from->si_fd; | |
2936 | break; | |
2937 | case SIL_FAULT: | |
2938 | new.si_addr = ptr_to_compat(from->si_addr); | |
2939 | #ifdef __ARCH_SI_TRAPNO | |
2940 | new.si_trapno = from->si_trapno; | |
2941 | #endif | |
31931c93 EB |
2942 | break; |
2943 | case SIL_FAULT_MCEERR: | |
2944 | new.si_addr = ptr_to_compat(from->si_addr); | |
2945 | #ifdef __ARCH_SI_TRAPNO | |
2946 | new.si_trapno = from->si_trapno; | |
ea64d5ac | 2947 | #endif |
31931c93 EB |
2948 | new.si_addr_lsb = from->si_addr_lsb; |
2949 | break; | |
2950 | case SIL_FAULT_BNDERR: | |
2951 | new.si_addr = ptr_to_compat(from->si_addr); | |
2952 | #ifdef __ARCH_SI_TRAPNO | |
2953 | new.si_trapno = from->si_trapno; | |
ea64d5ac | 2954 | #endif |
31931c93 EB |
2955 | new.si_lower = ptr_to_compat(from->si_lower); |
2956 | new.si_upper = ptr_to_compat(from->si_upper); | |
2957 | break; | |
2958 | case SIL_FAULT_PKUERR: | |
2959 | new.si_addr = ptr_to_compat(from->si_addr); | |
2960 | #ifdef __ARCH_SI_TRAPNO | |
2961 | new.si_trapno = from->si_trapno; | |
ea64d5ac | 2962 | #endif |
31931c93 | 2963 | new.si_pkey = from->si_pkey; |
ea64d5ac EB |
2964 | break; |
2965 | case SIL_CHLD: | |
2966 | new.si_pid = from->si_pid; | |
2967 | new.si_uid = from->si_uid; | |
2968 | new.si_status = from->si_status; | |
2969 | #ifdef CONFIG_X86_X32_ABI | |
2970 | if (x32_ABI) { | |
2971 | new._sifields._sigchld_x32._utime = from->si_utime; | |
2972 | new._sifields._sigchld_x32._stime = from->si_stime; | |
2973 | } else | |
2974 | #endif | |
2975 | { | |
2976 | new.si_utime = from->si_utime; | |
2977 | new.si_stime = from->si_stime; | |
2978 | } | |
2979 | break; | |
2980 | case SIL_RT: | |
2981 | new.si_pid = from->si_pid; | |
2982 | new.si_uid = from->si_uid; | |
2983 | new.si_int = from->si_int; | |
2984 | break; | |
2985 | case SIL_SYS: | |
2986 | new.si_call_addr = ptr_to_compat(from->si_call_addr); | |
2987 | new.si_syscall = from->si_syscall; | |
2988 | new.si_arch = from->si_arch; | |
2989 | break; | |
2990 | } | |
2991 | ||
2992 | if (copy_to_user(to, &new, sizeof(struct compat_siginfo))) | |
2993 | return -EFAULT; | |
2994 | ||
2995 | return 0; | |
2996 | } | |
2997 | ||
212a36a1 EB |
2998 | int copy_siginfo_from_user32(struct siginfo *to, |
2999 | const struct compat_siginfo __user *ufrom) | |
3000 | { | |
3001 | struct compat_siginfo from; | |
3002 | ||
3003 | if (copy_from_user(&from, ufrom, sizeof(struct compat_siginfo))) | |
3004 | return -EFAULT; | |
3005 | ||
3006 | clear_siginfo(to); | |
3007 | to->si_signo = from.si_signo; | |
3008 | to->si_errno = from.si_errno; | |
3009 | to->si_code = from.si_code; | |
3010 | switch(siginfo_layout(from.si_signo, from.si_code)) { | |
3011 | case SIL_KILL: | |
3012 | to->si_pid = from.si_pid; | |
3013 | to->si_uid = from.si_uid; | |
3014 | break; | |
3015 | case SIL_TIMER: | |
3016 | to->si_tid = from.si_tid; | |
3017 | to->si_overrun = from.si_overrun; | |
3018 | to->si_int = from.si_int; | |
3019 | break; | |
3020 | case SIL_POLL: | |
3021 | to->si_band = from.si_band; | |
3022 | to->si_fd = from.si_fd; | |
3023 | break; | |
3024 | case SIL_FAULT: | |
3025 | to->si_addr = compat_ptr(from.si_addr); | |
3026 | #ifdef __ARCH_SI_TRAPNO | |
3027 | to->si_trapno = from.si_trapno; | |
3028 | #endif | |
31931c93 EB |
3029 | break; |
3030 | case SIL_FAULT_MCEERR: | |
3031 | to->si_addr = compat_ptr(from.si_addr); | |
3032 | #ifdef __ARCH_SI_TRAPNO | |
3033 | to->si_trapno = from.si_trapno; | |
212a36a1 | 3034 | #endif |
31931c93 EB |
3035 | to->si_addr_lsb = from.si_addr_lsb; |
3036 | break; | |
3037 | case SIL_FAULT_BNDERR: | |
3038 | to->si_addr = compat_ptr(from.si_addr); | |
3039 | #ifdef __ARCH_SI_TRAPNO | |
3040 | to->si_trapno = from.si_trapno; | |
212a36a1 | 3041 | #endif |
31931c93 EB |
3042 | to->si_lower = compat_ptr(from.si_lower); |
3043 | to->si_upper = compat_ptr(from.si_upper); | |
3044 | break; | |
3045 | case SIL_FAULT_PKUERR: | |
3046 | to->si_addr = compat_ptr(from.si_addr); | |
3047 | #ifdef __ARCH_SI_TRAPNO | |
3048 | to->si_trapno = from.si_trapno; | |
212a36a1 | 3049 | #endif |
31931c93 | 3050 | to->si_pkey = from.si_pkey; |
212a36a1 EB |
3051 | break; |
3052 | case SIL_CHLD: | |
3053 | to->si_pid = from.si_pid; | |
3054 | to->si_uid = from.si_uid; | |
3055 | to->si_status = from.si_status; | |
3056 | #ifdef CONFIG_X86_X32_ABI | |
3057 | if (in_x32_syscall()) { | |
3058 | to->si_utime = from._sifields._sigchld_x32._utime; | |
3059 | to->si_stime = from._sifields._sigchld_x32._stime; | |
3060 | } else | |
3061 | #endif | |
3062 | { | |
3063 | to->si_utime = from.si_utime; | |
3064 | to->si_stime = from.si_stime; | |
3065 | } | |
3066 | break; | |
3067 | case SIL_RT: | |
3068 | to->si_pid = from.si_pid; | |
3069 | to->si_uid = from.si_uid; | |
3070 | to->si_int = from.si_int; | |
3071 | break; | |
3072 | case SIL_SYS: | |
3073 | to->si_call_addr = compat_ptr(from.si_call_addr); | |
3074 | to->si_syscall = from.si_syscall; | |
3075 | to->si_arch = from.si_arch; | |
3076 | break; | |
3077 | } | |
3078 | return 0; | |
3079 | } | |
3080 | #endif /* CONFIG_COMPAT */ | |
3081 | ||
943df148 ON |
3082 | /** |
3083 | * do_sigtimedwait - wait for queued signals specified in @which | |
3084 | * @which: queued signals to wait for | |
3085 | * @info: if non-null, the signal's siginfo is returned here | |
3086 | * @ts: upper bound on process time suspension | |
3087 | */ | |
1b3c872c | 3088 | static int do_sigtimedwait(const sigset_t *which, siginfo_t *info, |
2b1ecc3d | 3089 | const struct timespec *ts) |
943df148 | 3090 | { |
2456e855 | 3091 | ktime_t *to = NULL, timeout = KTIME_MAX; |
943df148 | 3092 | struct task_struct *tsk = current; |
943df148 | 3093 | sigset_t mask = *which; |
2b1ecc3d | 3094 | int sig, ret = 0; |
943df148 ON |
3095 | |
3096 | if (ts) { | |
3097 | if (!timespec_valid(ts)) | |
3098 | return -EINVAL; | |
2b1ecc3d TG |
3099 | timeout = timespec_to_ktime(*ts); |
3100 | to = &timeout; | |
943df148 ON |
3101 | } |
3102 | ||
3103 | /* | |
3104 | * Invert the set of allowed signals to get those we want to block. | |
3105 | */ | |
3106 | sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP)); | |
3107 | signotset(&mask); | |
3108 | ||
3109 | spin_lock_irq(&tsk->sighand->siglock); | |
3110 | sig = dequeue_signal(tsk, &mask, info); | |
2456e855 | 3111 | if (!sig && timeout) { |
943df148 ON |
3112 | /* |
3113 | * None ready, temporarily unblock those we're interested | |
3114 | * while we are sleeping in so that we'll be awakened when | |
b182801a ON |
3115 | * they arrive. Unblocking is always fine, we can avoid |
3116 | * set_current_blocked(). | |
943df148 ON |
3117 | */ |
3118 | tsk->real_blocked = tsk->blocked; | |
3119 | sigandsets(&tsk->blocked, &tsk->blocked, &mask); | |
3120 | recalc_sigpending(); | |
3121 | spin_unlock_irq(&tsk->sighand->siglock); | |
3122 | ||
2b1ecc3d TG |
3123 | __set_current_state(TASK_INTERRUPTIBLE); |
3124 | ret = freezable_schedule_hrtimeout_range(to, tsk->timer_slack_ns, | |
3125 | HRTIMER_MODE_REL); | |
943df148 | 3126 | spin_lock_irq(&tsk->sighand->siglock); |
b182801a | 3127 | __set_task_blocked(tsk, &tsk->real_blocked); |
6114041a | 3128 | sigemptyset(&tsk->real_blocked); |
b182801a | 3129 | sig = dequeue_signal(tsk, &mask, info); |
943df148 ON |
3130 | } |
3131 | spin_unlock_irq(&tsk->sighand->siglock); | |
3132 | ||
3133 | if (sig) | |
3134 | return sig; | |
2b1ecc3d | 3135 | return ret ? -EINTR : -EAGAIN; |
943df148 ON |
3136 | } |
3137 | ||
41c57892 RD |
3138 | /** |
3139 | * sys_rt_sigtimedwait - synchronously wait for queued signals specified | |
3140 | * in @uthese | |
3141 | * @uthese: queued signals to wait for | |
3142 | * @uinfo: if non-null, the signal's siginfo is returned here | |
3143 | * @uts: upper bound on process time suspension | |
3144 | * @sigsetsize: size of sigset_t type | |
3145 | */ | |
17da2bd9 HC |
3146 | SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese, |
3147 | siginfo_t __user *, uinfo, const struct timespec __user *, uts, | |
3148 | size_t, sigsetsize) | |
1da177e4 | 3149 | { |
1da177e4 LT |
3150 | sigset_t these; |
3151 | struct timespec ts; | |
3152 | siginfo_t info; | |
943df148 | 3153 | int ret; |
1da177e4 LT |
3154 | |
3155 | /* XXX: Don't preclude handling different sized sigset_t's. */ | |
3156 | if (sigsetsize != sizeof(sigset_t)) | |
3157 | return -EINVAL; | |
3158 | ||
3159 | if (copy_from_user(&these, uthese, sizeof(these))) | |
3160 | return -EFAULT; | |
5aba085e | 3161 | |
1da177e4 LT |
3162 | if (uts) { |
3163 | if (copy_from_user(&ts, uts, sizeof(ts))) | |
3164 | return -EFAULT; | |
1da177e4 LT |
3165 | } |
3166 | ||
943df148 | 3167 | ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL); |
1da177e4 | 3168 | |
943df148 ON |
3169 | if (ret > 0 && uinfo) { |
3170 | if (copy_siginfo_to_user(uinfo, &info)) | |
3171 | ret = -EFAULT; | |
1da177e4 LT |
3172 | } |
3173 | ||
3174 | return ret; | |
3175 | } | |
3176 | ||
1b3c872c AV |
3177 | #ifdef CONFIG_COMPAT |
3178 | COMPAT_SYSCALL_DEFINE4(rt_sigtimedwait, compat_sigset_t __user *, uthese, | |
3179 | struct compat_siginfo __user *, uinfo, | |
3180 | struct compat_timespec __user *, uts, compat_size_t, sigsetsize) | |
3181 | { | |
1b3c872c AV |
3182 | sigset_t s; |
3183 | struct timespec t; | |
3184 | siginfo_t info; | |
3185 | long ret; | |
3186 | ||
3187 | if (sigsetsize != sizeof(sigset_t)) | |
3188 | return -EINVAL; | |
3189 | ||
3968cf62 | 3190 | if (get_compat_sigset(&s, uthese)) |
1b3c872c | 3191 | return -EFAULT; |
1b3c872c AV |
3192 | |
3193 | if (uts) { | |
3194 | if (compat_get_timespec(&t, uts)) | |
3195 | return -EFAULT; | |
3196 | } | |
3197 | ||
3198 | ret = do_sigtimedwait(&s, &info, uts ? &t : NULL); | |
3199 | ||
3200 | if (ret > 0 && uinfo) { | |
3201 | if (copy_siginfo_to_user32(uinfo, &info)) | |
3202 | ret = -EFAULT; | |
3203 | } | |
3204 | ||
3205 | return ret; | |
3206 | } | |
3207 | #endif | |
3208 | ||
41c57892 RD |
3209 | /** |
3210 | * sys_kill - send a signal to a process | |
3211 | * @pid: the PID of the process | |
3212 | * @sig: signal to be sent | |
3213 | */ | |
17da2bd9 | 3214 | SYSCALL_DEFINE2(kill, pid_t, pid, int, sig) |
1da177e4 LT |
3215 | { |
3216 | struct siginfo info; | |
3217 | ||
faf1f22b | 3218 | clear_siginfo(&info); |
1da177e4 LT |
3219 | info.si_signo = sig; |
3220 | info.si_errno = 0; | |
3221 | info.si_code = SI_USER; | |
b488893a | 3222 | info.si_pid = task_tgid_vnr(current); |
078de5f7 | 3223 | info.si_uid = from_kuid_munged(current_user_ns(), current_uid()); |
1da177e4 LT |
3224 | |
3225 | return kill_something_info(sig, &info, pid); | |
3226 | } | |
3227 | ||
30b4ae8a TG |
3228 | static int |
3229 | do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info) | |
1da177e4 | 3230 | { |
1da177e4 | 3231 | struct task_struct *p; |
30b4ae8a | 3232 | int error = -ESRCH; |
1da177e4 | 3233 | |
3547ff3a | 3234 | rcu_read_lock(); |
228ebcbe | 3235 | p = find_task_by_vpid(pid); |
b488893a | 3236 | if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) { |
30b4ae8a | 3237 | error = check_kill_permission(sig, info, p); |
1da177e4 LT |
3238 | /* |
3239 | * The null signal is a permissions and process existence | |
3240 | * probe. No signal is actually delivered. | |
3241 | */ | |
4a30debf | 3242 | if (!error && sig) { |
40b3b025 | 3243 | error = do_send_sig_info(sig, info, p, PIDTYPE_PID); |
4a30debf ON |
3244 | /* |
3245 | * If lock_task_sighand() failed we pretend the task | |
3246 | * dies after receiving the signal. The window is tiny, | |
3247 | * and the signal is private anyway. | |
3248 | */ | |
3249 | if (unlikely(error == -ESRCH)) | |
3250 | error = 0; | |
1da177e4 LT |
3251 | } |
3252 | } | |
3547ff3a | 3253 | rcu_read_unlock(); |
6dd69f10 | 3254 | |
1da177e4 LT |
3255 | return error; |
3256 | } | |
3257 | ||
30b4ae8a TG |
3258 | static int do_tkill(pid_t tgid, pid_t pid, int sig) |
3259 | { | |
5f74972c | 3260 | struct siginfo info; |
30b4ae8a | 3261 | |
5f74972c | 3262 | clear_siginfo(&info); |
30b4ae8a TG |
3263 | info.si_signo = sig; |
3264 | info.si_errno = 0; | |
3265 | info.si_code = SI_TKILL; | |
3266 | info.si_pid = task_tgid_vnr(current); | |
078de5f7 | 3267 | info.si_uid = from_kuid_munged(current_user_ns(), current_uid()); |
30b4ae8a TG |
3268 | |
3269 | return do_send_specific(tgid, pid, sig, &info); | |
3270 | } | |
3271 | ||
6dd69f10 VL |
3272 | /** |
3273 | * sys_tgkill - send signal to one specific thread | |
3274 | * @tgid: the thread group ID of the thread | |
3275 | * @pid: the PID of the thread | |
3276 | * @sig: signal to be sent | |
3277 | * | |
72fd4a35 | 3278 | * This syscall also checks the @tgid and returns -ESRCH even if the PID |
6dd69f10 VL |
3279 | * exists but it's not belonging to the target process anymore. This |
3280 | * method solves the problem of threads exiting and PIDs getting reused. | |
3281 | */ | |
a5f8fa9e | 3282 | SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig) |
6dd69f10 VL |
3283 | { |
3284 | /* This is only valid for single tasks */ | |
3285 | if (pid <= 0 || tgid <= 0) | |
3286 | return -EINVAL; | |
3287 | ||
3288 | return do_tkill(tgid, pid, sig); | |
3289 | } | |
3290 | ||
41c57892 RD |
3291 | /** |
3292 | * sys_tkill - send signal to one specific task | |
3293 | * @pid: the PID of the task | |
3294 | * @sig: signal to be sent | |
3295 | * | |
1da177e4 LT |
3296 | * Send a signal to only one task, even if it's a CLONE_THREAD task. |
3297 | */ | |
a5f8fa9e | 3298 | SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig) |
1da177e4 | 3299 | { |
1da177e4 LT |
3300 | /* This is only valid for single tasks */ |
3301 | if (pid <= 0) | |
3302 | return -EINVAL; | |
3303 | ||
6dd69f10 | 3304 | return do_tkill(0, pid, sig); |
1da177e4 LT |
3305 | } |
3306 | ||
75907d4d AV |
3307 | static int do_rt_sigqueueinfo(pid_t pid, int sig, siginfo_t *info) |
3308 | { | |
3309 | /* Not even root can pretend to send signals from the kernel. | |
3310 | * Nor can they impersonate a kill()/tgkill(), which adds source info. | |
3311 | */ | |
66dd34ad | 3312 | if ((info->si_code >= 0 || info->si_code == SI_TKILL) && |
69828dce | 3313 | (task_pid_vnr(current) != pid)) |
75907d4d | 3314 | return -EPERM; |
69828dce | 3315 | |
e75dc036 EB |
3316 | if (info->si_signo != sig) |
3317 | return -EINVAL; | |
75907d4d AV |
3318 | |
3319 | /* POSIX.1b doesn't mention process groups. */ | |
3320 | return kill_proc_info(sig, info, pid); | |
3321 | } | |
3322 | ||
41c57892 RD |
3323 | /** |
3324 | * sys_rt_sigqueueinfo - send signal information to a signal | |
3325 | * @pid: the PID of the thread | |
3326 | * @sig: signal to be sent | |
3327 | * @uinfo: signal info to be sent | |
3328 | */ | |
a5f8fa9e HC |
3329 | SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig, |
3330 | siginfo_t __user *, uinfo) | |
1da177e4 LT |
3331 | { |
3332 | siginfo_t info; | |
4cd2e0e7 EB |
3333 | int ret = copy_siginfo_from_user(&info, uinfo); |
3334 | if (unlikely(ret)) | |
3335 | return ret; | |
75907d4d AV |
3336 | return do_rt_sigqueueinfo(pid, sig, &info); |
3337 | } | |
1da177e4 | 3338 | |
75907d4d | 3339 | #ifdef CONFIG_COMPAT |
75907d4d AV |
3340 | COMPAT_SYSCALL_DEFINE3(rt_sigqueueinfo, |
3341 | compat_pid_t, pid, | |
3342 | int, sig, | |
3343 | struct compat_siginfo __user *, uinfo) | |
3344 | { | |
eb5346c3 | 3345 | siginfo_t info; |
75907d4d AV |
3346 | int ret = copy_siginfo_from_user32(&info, uinfo); |
3347 | if (unlikely(ret)) | |
3348 | return ret; | |
3349 | return do_rt_sigqueueinfo(pid, sig, &info); | |
1da177e4 | 3350 | } |
75907d4d | 3351 | #endif |
1da177e4 | 3352 | |
9aae8fc0 | 3353 | static int do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info) |
62ab4505 TG |
3354 | { |
3355 | /* This is only valid for single tasks */ | |
3356 | if (pid <= 0 || tgid <= 0) | |
3357 | return -EINVAL; | |
3358 | ||
3359 | /* Not even root can pretend to send signals from the kernel. | |
da48524e JT |
3360 | * Nor can they impersonate a kill()/tgkill(), which adds source info. |
3361 | */ | |
69828dce VD |
3362 | if ((info->si_code >= 0 || info->si_code == SI_TKILL) && |
3363 | (task_pid_vnr(current) != pid)) | |
62ab4505 | 3364 | return -EPERM; |
69828dce | 3365 | |
e75dc036 EB |
3366 | if (info->si_signo != sig) |
3367 | return -EINVAL; | |
62ab4505 TG |
3368 | |
3369 | return do_send_specific(tgid, pid, sig, info); | |
3370 | } | |
3371 | ||
3372 | SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig, | |
3373 | siginfo_t __user *, uinfo) | |
3374 | { | |
3375 | siginfo_t info; | |
4cd2e0e7 EB |
3376 | int ret = copy_siginfo_from_user(&info, uinfo); |
3377 | if (unlikely(ret)) | |
3378 | return ret; | |
62ab4505 TG |
3379 | return do_rt_tgsigqueueinfo(tgid, pid, sig, &info); |
3380 | } | |
3381 | ||
9aae8fc0 AV |
3382 | #ifdef CONFIG_COMPAT |
3383 | COMPAT_SYSCALL_DEFINE4(rt_tgsigqueueinfo, | |
3384 | compat_pid_t, tgid, | |
3385 | compat_pid_t, pid, | |
3386 | int, sig, | |
3387 | struct compat_siginfo __user *, uinfo) | |
3388 | { | |
eb5346c3 | 3389 | siginfo_t info; |
4cd2e0e7 EB |
3390 | int ret = copy_siginfo_from_user32(&info, uinfo); |
3391 | if (unlikely(ret)) | |
3392 | return ret; | |
9aae8fc0 AV |
3393 | return do_rt_tgsigqueueinfo(tgid, pid, sig, &info); |
3394 | } | |
3395 | #endif | |
3396 | ||
0341729b | 3397 | /* |
b4e74264 | 3398 | * For kthreads only, must not be used if cloned with CLONE_SIGHAND |
0341729b | 3399 | */ |
b4e74264 | 3400 | void kernel_sigaction(int sig, __sighandler_t action) |
0341729b | 3401 | { |
ec5955b8 | 3402 | spin_lock_irq(¤t->sighand->siglock); |
b4e74264 ON |
3403 | current->sighand->action[sig - 1].sa.sa_handler = action; |
3404 | if (action == SIG_IGN) { | |
3405 | sigset_t mask; | |
0341729b | 3406 | |
b4e74264 ON |
3407 | sigemptyset(&mask); |
3408 | sigaddset(&mask, sig); | |
580d34e4 | 3409 | |
b4e74264 ON |
3410 | flush_sigqueue_mask(&mask, ¤t->signal->shared_pending); |
3411 | flush_sigqueue_mask(&mask, ¤t->pending); | |
3412 | recalc_sigpending(); | |
3413 | } | |
0341729b ON |
3414 | spin_unlock_irq(¤t->sighand->siglock); |
3415 | } | |
b4e74264 | 3416 | EXPORT_SYMBOL(kernel_sigaction); |
0341729b | 3417 | |
68463510 DS |
3418 | void __weak sigaction_compat_abi(struct k_sigaction *act, |
3419 | struct k_sigaction *oact) | |
3420 | { | |
3421 | } | |
3422 | ||
88531f72 | 3423 | int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact) |
1da177e4 | 3424 | { |
afe2b038 | 3425 | struct task_struct *p = current, *t; |
1da177e4 | 3426 | struct k_sigaction *k; |
71fabd5e | 3427 | sigset_t mask; |
1da177e4 | 3428 | |
7ed20e1a | 3429 | if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig))) |
1da177e4 LT |
3430 | return -EINVAL; |
3431 | ||
afe2b038 | 3432 | k = &p->sighand->action[sig-1]; |
1da177e4 | 3433 | |
afe2b038 | 3434 | spin_lock_irq(&p->sighand->siglock); |
1da177e4 LT |
3435 | if (oact) |
3436 | *oact = *k; | |
3437 | ||
68463510 DS |
3438 | sigaction_compat_abi(act, oact); |
3439 | ||
1da177e4 | 3440 | if (act) { |
9ac95f2f ON |
3441 | sigdelsetmask(&act->sa.sa_mask, |
3442 | sigmask(SIGKILL) | sigmask(SIGSTOP)); | |
88531f72 | 3443 | *k = *act; |
1da177e4 LT |
3444 | /* |
3445 | * POSIX 3.3.1.3: | |
3446 | * "Setting a signal action to SIG_IGN for a signal that is | |
3447 | * pending shall cause the pending signal to be discarded, | |
3448 | * whether or not it is blocked." | |
3449 | * | |
3450 | * "Setting a signal action to SIG_DFL for a signal that is | |
3451 | * pending and whose default action is to ignore the signal | |
3452 | * (for example, SIGCHLD), shall cause the pending signal to | |
3453 | * be discarded, whether or not it is blocked" | |
3454 | */ | |
afe2b038 | 3455 | if (sig_handler_ignored(sig_handler(p, sig), sig)) { |
71fabd5e GA |
3456 | sigemptyset(&mask); |
3457 | sigaddset(&mask, sig); | |
afe2b038 ON |
3458 | flush_sigqueue_mask(&mask, &p->signal->shared_pending); |
3459 | for_each_thread(p, t) | |
c09c1441 | 3460 | flush_sigqueue_mask(&mask, &t->pending); |
1da177e4 | 3461 | } |
1da177e4 LT |
3462 | } |
3463 | ||
afe2b038 | 3464 | spin_unlock_irq(&p->sighand->siglock); |
1da177e4 LT |
3465 | return 0; |
3466 | } | |
3467 | ||
c09c1441 | 3468 | static int |
bcfe8ad8 | 3469 | do_sigaltstack (const stack_t *ss, stack_t *oss, unsigned long sp) |
1da177e4 | 3470 | { |
bcfe8ad8 | 3471 | struct task_struct *t = current; |
1da177e4 | 3472 | |
bcfe8ad8 AV |
3473 | if (oss) { |
3474 | memset(oss, 0, sizeof(stack_t)); | |
3475 | oss->ss_sp = (void __user *) t->sas_ss_sp; | |
3476 | oss->ss_size = t->sas_ss_size; | |
3477 | oss->ss_flags = sas_ss_flags(sp) | | |
3478 | (current->sas_ss_flags & SS_FLAG_BITS); | |
3479 | } | |
1da177e4 | 3480 | |
bcfe8ad8 AV |
3481 | if (ss) { |
3482 | void __user *ss_sp = ss->ss_sp; | |
3483 | size_t ss_size = ss->ss_size; | |
3484 | unsigned ss_flags = ss->ss_flags; | |
407bc16a | 3485 | int ss_mode; |
1da177e4 | 3486 | |
bcfe8ad8 AV |
3487 | if (unlikely(on_sig_stack(sp))) |
3488 | return -EPERM; | |
1da177e4 | 3489 | |
407bc16a | 3490 | ss_mode = ss_flags & ~SS_FLAG_BITS; |
bcfe8ad8 AV |
3491 | if (unlikely(ss_mode != SS_DISABLE && ss_mode != SS_ONSTACK && |
3492 | ss_mode != 0)) | |
3493 | return -EINVAL; | |
1da177e4 | 3494 | |
407bc16a | 3495 | if (ss_mode == SS_DISABLE) { |
1da177e4 LT |
3496 | ss_size = 0; |
3497 | ss_sp = NULL; | |
3498 | } else { | |
bcfe8ad8 AV |
3499 | if (unlikely(ss_size < MINSIGSTKSZ)) |
3500 | return -ENOMEM; | |
1da177e4 LT |
3501 | } |
3502 | ||
bcfe8ad8 AV |
3503 | t->sas_ss_sp = (unsigned long) ss_sp; |
3504 | t->sas_ss_size = ss_size; | |
3505 | t->sas_ss_flags = ss_flags; | |
1da177e4 | 3506 | } |
bcfe8ad8 | 3507 | return 0; |
1da177e4 | 3508 | } |
bcfe8ad8 | 3509 | |
6bf9adfc AV |
3510 | SYSCALL_DEFINE2(sigaltstack,const stack_t __user *,uss, stack_t __user *,uoss) |
3511 | { | |
bcfe8ad8 AV |
3512 | stack_t new, old; |
3513 | int err; | |
3514 | if (uss && copy_from_user(&new, uss, sizeof(stack_t))) | |
3515 | return -EFAULT; | |
3516 | err = do_sigaltstack(uss ? &new : NULL, uoss ? &old : NULL, | |
3517 | current_user_stack_pointer()); | |
3518 | if (!err && uoss && copy_to_user(uoss, &old, sizeof(stack_t))) | |
3519 | err = -EFAULT; | |
3520 | return err; | |
6bf9adfc | 3521 | } |
1da177e4 | 3522 | |
5c49574f AV |
3523 | int restore_altstack(const stack_t __user *uss) |
3524 | { | |
bcfe8ad8 AV |
3525 | stack_t new; |
3526 | if (copy_from_user(&new, uss, sizeof(stack_t))) | |
3527 | return -EFAULT; | |
3528 | (void)do_sigaltstack(&new, NULL, current_user_stack_pointer()); | |
5c49574f | 3529 | /* squash all but EFAULT for now */ |
bcfe8ad8 | 3530 | return 0; |
5c49574f AV |
3531 | } |
3532 | ||
c40702c4 AV |
3533 | int __save_altstack(stack_t __user *uss, unsigned long sp) |
3534 | { | |
3535 | struct task_struct *t = current; | |
2a742138 SS |
3536 | int err = __put_user((void __user *)t->sas_ss_sp, &uss->ss_sp) | |
3537 | __put_user(t->sas_ss_flags, &uss->ss_flags) | | |
c40702c4 | 3538 | __put_user(t->sas_ss_size, &uss->ss_size); |
2a742138 SS |
3539 | if (err) |
3540 | return err; | |
3541 | if (t->sas_ss_flags & SS_AUTODISARM) | |
3542 | sas_ss_reset(t); | |
3543 | return 0; | |
c40702c4 AV |
3544 | } |
3545 | ||
90268439 | 3546 | #ifdef CONFIG_COMPAT |
6203deb0 DB |
3547 | static int do_compat_sigaltstack(const compat_stack_t __user *uss_ptr, |
3548 | compat_stack_t __user *uoss_ptr) | |
90268439 AV |
3549 | { |
3550 | stack_t uss, uoss; | |
3551 | int ret; | |
90268439 AV |
3552 | |
3553 | if (uss_ptr) { | |
3554 | compat_stack_t uss32; | |
90268439 AV |
3555 | if (copy_from_user(&uss32, uss_ptr, sizeof(compat_stack_t))) |
3556 | return -EFAULT; | |
3557 | uss.ss_sp = compat_ptr(uss32.ss_sp); | |
3558 | uss.ss_flags = uss32.ss_flags; | |
3559 | uss.ss_size = uss32.ss_size; | |
3560 | } | |
bcfe8ad8 | 3561 | ret = do_sigaltstack(uss_ptr ? &uss : NULL, &uoss, |
90268439 | 3562 | compat_user_stack_pointer()); |
90268439 | 3563 | if (ret >= 0 && uoss_ptr) { |
bcfe8ad8 AV |
3564 | compat_stack_t old; |
3565 | memset(&old, 0, sizeof(old)); | |
3566 | old.ss_sp = ptr_to_compat(uoss.ss_sp); | |
3567 | old.ss_flags = uoss.ss_flags; | |
3568 | old.ss_size = uoss.ss_size; | |
3569 | if (copy_to_user(uoss_ptr, &old, sizeof(compat_stack_t))) | |
90268439 AV |
3570 | ret = -EFAULT; |
3571 | } | |
3572 | return ret; | |
3573 | } | |
3574 | ||
6203deb0 DB |
3575 | COMPAT_SYSCALL_DEFINE2(sigaltstack, |
3576 | const compat_stack_t __user *, uss_ptr, | |
3577 | compat_stack_t __user *, uoss_ptr) | |
3578 | { | |
3579 | return do_compat_sigaltstack(uss_ptr, uoss_ptr); | |
3580 | } | |
3581 | ||
90268439 AV |
3582 | int compat_restore_altstack(const compat_stack_t __user *uss) |
3583 | { | |
6203deb0 | 3584 | int err = do_compat_sigaltstack(uss, NULL); |
90268439 AV |
3585 | /* squash all but -EFAULT for now */ |
3586 | return err == -EFAULT ? err : 0; | |
3587 | } | |
c40702c4 AV |
3588 | |
3589 | int __compat_save_altstack(compat_stack_t __user *uss, unsigned long sp) | |
3590 | { | |
441398d3 | 3591 | int err; |
c40702c4 | 3592 | struct task_struct *t = current; |
441398d3 SS |
3593 | err = __put_user(ptr_to_compat((void __user *)t->sas_ss_sp), |
3594 | &uss->ss_sp) | | |
3595 | __put_user(t->sas_ss_flags, &uss->ss_flags) | | |
c40702c4 | 3596 | __put_user(t->sas_ss_size, &uss->ss_size); |
441398d3 SS |
3597 | if (err) |
3598 | return err; | |
3599 | if (t->sas_ss_flags & SS_AUTODISARM) | |
3600 | sas_ss_reset(t); | |
3601 | return 0; | |
c40702c4 | 3602 | } |
90268439 | 3603 | #endif |
1da177e4 LT |
3604 | |
3605 | #ifdef __ARCH_WANT_SYS_SIGPENDING | |
3606 | ||
41c57892 RD |
3607 | /** |
3608 | * sys_sigpending - examine pending signals | |
d53238cd | 3609 | * @uset: where mask of pending signal is returned |
41c57892 | 3610 | */ |
d53238cd | 3611 | SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, uset) |
1da177e4 | 3612 | { |
d53238cd | 3613 | sigset_t set; |
d53238cd DB |
3614 | |
3615 | if (sizeof(old_sigset_t) > sizeof(*uset)) | |
3616 | return -EINVAL; | |
3617 | ||
b1d294c8 CB |
3618 | do_sigpending(&set); |
3619 | ||
3620 | if (copy_to_user(uset, &set, sizeof(old_sigset_t))) | |
3621 | return -EFAULT; | |
3622 | ||
3623 | return 0; | |
1da177e4 LT |
3624 | } |
3625 | ||
8f13621a AV |
3626 | #ifdef CONFIG_COMPAT |
3627 | COMPAT_SYSCALL_DEFINE1(sigpending, compat_old_sigset_t __user *, set32) | |
3628 | { | |
3629 | sigset_t set; | |
b1d294c8 CB |
3630 | |
3631 | do_sigpending(&set); | |
3632 | ||
3633 | return put_user(set.sig[0], set32); | |
8f13621a AV |
3634 | } |
3635 | #endif | |
3636 | ||
1da177e4 LT |
3637 | #endif |
3638 | ||
3639 | #ifdef __ARCH_WANT_SYS_SIGPROCMASK | |
41c57892 RD |
3640 | /** |
3641 | * sys_sigprocmask - examine and change blocked signals | |
3642 | * @how: whether to add, remove, or set signals | |
b013c399 | 3643 | * @nset: signals to add or remove (if non-null) |
41c57892 RD |
3644 | * @oset: previous value of signal mask if non-null |
3645 | * | |
5aba085e RD |
3646 | * Some platforms have their own version with special arguments; |
3647 | * others support only sys_rt_sigprocmask. | |
3648 | */ | |
1da177e4 | 3649 | |
b013c399 | 3650 | SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, nset, |
b290ebe2 | 3651 | old_sigset_t __user *, oset) |
1da177e4 | 3652 | { |
1da177e4 | 3653 | old_sigset_t old_set, new_set; |
2e4f7c77 | 3654 | sigset_t new_blocked; |
1da177e4 | 3655 | |
b013c399 | 3656 | old_set = current->blocked.sig[0]; |
1da177e4 | 3657 | |
b013c399 ON |
3658 | if (nset) { |
3659 | if (copy_from_user(&new_set, nset, sizeof(*nset))) | |
3660 | return -EFAULT; | |
1da177e4 | 3661 | |
2e4f7c77 | 3662 | new_blocked = current->blocked; |
1da177e4 | 3663 | |
1da177e4 | 3664 | switch (how) { |
1da177e4 | 3665 | case SIG_BLOCK: |
2e4f7c77 | 3666 | sigaddsetmask(&new_blocked, new_set); |
1da177e4 LT |
3667 | break; |
3668 | case SIG_UNBLOCK: | |
2e4f7c77 | 3669 | sigdelsetmask(&new_blocked, new_set); |
1da177e4 LT |
3670 | break; |
3671 | case SIG_SETMASK: | |
2e4f7c77 | 3672 | new_blocked.sig[0] = new_set; |
1da177e4 | 3673 | break; |
2e4f7c77 ON |
3674 | default: |
3675 | return -EINVAL; | |
1da177e4 LT |
3676 | } |
3677 | ||
0c4a8423 | 3678 | set_current_blocked(&new_blocked); |
b013c399 ON |
3679 | } |
3680 | ||
3681 | if (oset) { | |
1da177e4 | 3682 | if (copy_to_user(oset, &old_set, sizeof(*oset))) |
b013c399 | 3683 | return -EFAULT; |
1da177e4 | 3684 | } |
b013c399 ON |
3685 | |
3686 | return 0; | |
1da177e4 LT |
3687 | } |
3688 | #endif /* __ARCH_WANT_SYS_SIGPROCMASK */ | |
3689 | ||
eaca6eae | 3690 | #ifndef CONFIG_ODD_RT_SIGACTION |
41c57892 RD |
3691 | /** |
3692 | * sys_rt_sigaction - alter an action taken by a process | |
3693 | * @sig: signal to be sent | |
f9fa0bc1 RD |
3694 | * @act: new sigaction |
3695 | * @oact: used to save the previous sigaction | |
41c57892 RD |
3696 | * @sigsetsize: size of sigset_t type |
3697 | */ | |
d4e82042 HC |
3698 | SYSCALL_DEFINE4(rt_sigaction, int, sig, |
3699 | const struct sigaction __user *, act, | |
3700 | struct sigaction __user *, oact, | |
3701 | size_t, sigsetsize) | |
1da177e4 LT |
3702 | { |
3703 | struct k_sigaction new_sa, old_sa; | |
d8f993b3 | 3704 | int ret; |
1da177e4 LT |
3705 | |
3706 | /* XXX: Don't preclude handling different sized sigset_t's. */ | |
3707 | if (sigsetsize != sizeof(sigset_t)) | |
d8f993b3 | 3708 | return -EINVAL; |
1da177e4 | 3709 | |
d8f993b3 CB |
3710 | if (act && copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa))) |
3711 | return -EFAULT; | |
1da177e4 LT |
3712 | |
3713 | ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL); | |
d8f993b3 CB |
3714 | if (ret) |
3715 | return ret; | |
1da177e4 | 3716 | |
d8f993b3 CB |
3717 | if (oact && copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa))) |
3718 | return -EFAULT; | |
3719 | ||
3720 | return 0; | |
1da177e4 | 3721 | } |
08d32fe5 | 3722 | #ifdef CONFIG_COMPAT |
08d32fe5 AV |
3723 | COMPAT_SYSCALL_DEFINE4(rt_sigaction, int, sig, |
3724 | const struct compat_sigaction __user *, act, | |
3725 | struct compat_sigaction __user *, oact, | |
3726 | compat_size_t, sigsetsize) | |
3727 | { | |
3728 | struct k_sigaction new_ka, old_ka; | |
08d32fe5 AV |
3729 | #ifdef __ARCH_HAS_SA_RESTORER |
3730 | compat_uptr_t restorer; | |
3731 | #endif | |
3732 | int ret; | |
3733 | ||
3734 | /* XXX: Don't preclude handling different sized sigset_t's. */ | |
3735 | if (sigsetsize != sizeof(compat_sigset_t)) | |
3736 | return -EINVAL; | |
3737 | ||
3738 | if (act) { | |
3739 | compat_uptr_t handler; | |
3740 | ret = get_user(handler, &act->sa_handler); | |
3741 | new_ka.sa.sa_handler = compat_ptr(handler); | |
3742 | #ifdef __ARCH_HAS_SA_RESTORER | |
3743 | ret |= get_user(restorer, &act->sa_restorer); | |
3744 | new_ka.sa.sa_restorer = compat_ptr(restorer); | |
3745 | #endif | |
3968cf62 | 3746 | ret |= get_compat_sigset(&new_ka.sa.sa_mask, &act->sa_mask); |
3ddc5b46 | 3747 | ret |= get_user(new_ka.sa.sa_flags, &act->sa_flags); |
08d32fe5 AV |
3748 | if (ret) |
3749 | return -EFAULT; | |
08d32fe5 AV |
3750 | } |
3751 | ||
3752 | ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL); | |
3753 | if (!ret && oact) { | |
08d32fe5 AV |
3754 | ret = put_user(ptr_to_compat(old_ka.sa.sa_handler), |
3755 | &oact->sa_handler); | |
f454322e DL |
3756 | ret |= put_compat_sigset(&oact->sa_mask, &old_ka.sa.sa_mask, |
3757 | sizeof(oact->sa_mask)); | |
3ddc5b46 | 3758 | ret |= put_user(old_ka.sa.sa_flags, &oact->sa_flags); |
08d32fe5 AV |
3759 | #ifdef __ARCH_HAS_SA_RESTORER |
3760 | ret |= put_user(ptr_to_compat(old_ka.sa.sa_restorer), | |
3761 | &oact->sa_restorer); | |
3762 | #endif | |
3763 | } | |
3764 | return ret; | |
3765 | } | |
3766 | #endif | |
eaca6eae | 3767 | #endif /* !CONFIG_ODD_RT_SIGACTION */ |
1da177e4 | 3768 | |
495dfbf7 AV |
3769 | #ifdef CONFIG_OLD_SIGACTION |
3770 | SYSCALL_DEFINE3(sigaction, int, sig, | |
3771 | const struct old_sigaction __user *, act, | |
3772 | struct old_sigaction __user *, oact) | |
3773 | { | |
3774 | struct k_sigaction new_ka, old_ka; | |
3775 | int ret; | |
3776 | ||
3777 | if (act) { | |
3778 | old_sigset_t mask; | |
3779 | if (!access_ok(VERIFY_READ, act, sizeof(*act)) || | |
3780 | __get_user(new_ka.sa.sa_handler, &act->sa_handler) || | |
3781 | __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) || | |
3782 | __get_user(new_ka.sa.sa_flags, &act->sa_flags) || | |
3783 | __get_user(mask, &act->sa_mask)) | |
3784 | return -EFAULT; | |
3785 | #ifdef __ARCH_HAS_KA_RESTORER | |
3786 | new_ka.ka_restorer = NULL; | |
3787 | #endif | |
3788 | siginitset(&new_ka.sa.sa_mask, mask); | |
3789 | } | |
3790 | ||
3791 | ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL); | |
3792 | ||
3793 | if (!ret && oact) { | |
3794 | if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) || | |
3795 | __put_user(old_ka.sa.sa_handler, &oact->sa_handler) || | |
3796 | __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) || | |
3797 | __put_user(old_ka.sa.sa_flags, &oact->sa_flags) || | |
3798 | __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask)) | |
3799 | return -EFAULT; | |
3800 | } | |
3801 | ||
3802 | return ret; | |
3803 | } | |
3804 | #endif | |
3805 | #ifdef CONFIG_COMPAT_OLD_SIGACTION | |
3806 | COMPAT_SYSCALL_DEFINE3(sigaction, int, sig, | |
3807 | const struct compat_old_sigaction __user *, act, | |
3808 | struct compat_old_sigaction __user *, oact) | |
3809 | { | |
3810 | struct k_sigaction new_ka, old_ka; | |
3811 | int ret; | |
3812 | compat_old_sigset_t mask; | |
3813 | compat_uptr_t handler, restorer; | |
3814 | ||
3815 | if (act) { | |
3816 | if (!access_ok(VERIFY_READ, act, sizeof(*act)) || | |
3817 | __get_user(handler, &act->sa_handler) || | |
3818 | __get_user(restorer, &act->sa_restorer) || | |
3819 | __get_user(new_ka.sa.sa_flags, &act->sa_flags) || | |
3820 | __get_user(mask, &act->sa_mask)) | |
3821 | return -EFAULT; | |
3822 | ||
3823 | #ifdef __ARCH_HAS_KA_RESTORER | |
3824 | new_ka.ka_restorer = NULL; | |
3825 | #endif | |
3826 | new_ka.sa.sa_handler = compat_ptr(handler); | |
3827 | new_ka.sa.sa_restorer = compat_ptr(restorer); | |
3828 | siginitset(&new_ka.sa.sa_mask, mask); | |
3829 | } | |
3830 | ||
3831 | ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL); | |
3832 | ||
3833 | if (!ret && oact) { | |
3834 | if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) || | |
3835 | __put_user(ptr_to_compat(old_ka.sa.sa_handler), | |
3836 | &oact->sa_handler) || | |
3837 | __put_user(ptr_to_compat(old_ka.sa.sa_restorer), | |
3838 | &oact->sa_restorer) || | |
3839 | __put_user(old_ka.sa.sa_flags, &oact->sa_flags) || | |
3840 | __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask)) | |
3841 | return -EFAULT; | |
3842 | } | |
3843 | return ret; | |
3844 | } | |
3845 | #endif | |
1da177e4 | 3846 | |
f6187769 | 3847 | #ifdef CONFIG_SGETMASK_SYSCALL |
1da177e4 LT |
3848 | |
3849 | /* | |
3850 | * For backwards compatibility. Functionality superseded by sigprocmask. | |
3851 | */ | |
a5f8fa9e | 3852 | SYSCALL_DEFINE0(sgetmask) |
1da177e4 LT |
3853 | { |
3854 | /* SMP safe */ | |
3855 | return current->blocked.sig[0]; | |
3856 | } | |
3857 | ||
a5f8fa9e | 3858 | SYSCALL_DEFINE1(ssetmask, int, newmask) |
1da177e4 | 3859 | { |
c1095c6d ON |
3860 | int old = current->blocked.sig[0]; |
3861 | sigset_t newset; | |
1da177e4 | 3862 | |
5ba53ff6 | 3863 | siginitset(&newset, newmask); |
c1095c6d | 3864 | set_current_blocked(&newset); |
1da177e4 LT |
3865 | |
3866 | return old; | |
3867 | } | |
f6187769 | 3868 | #endif /* CONFIG_SGETMASK_SYSCALL */ |
1da177e4 LT |
3869 | |
3870 | #ifdef __ARCH_WANT_SYS_SIGNAL | |
3871 | /* | |
3872 | * For backwards compatibility. Functionality superseded by sigaction. | |
3873 | */ | |
a5f8fa9e | 3874 | SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler) |
1da177e4 LT |
3875 | { |
3876 | struct k_sigaction new_sa, old_sa; | |
3877 | int ret; | |
3878 | ||
3879 | new_sa.sa.sa_handler = handler; | |
3880 | new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK; | |
c70d3d70 | 3881 | sigemptyset(&new_sa.sa.sa_mask); |
1da177e4 LT |
3882 | |
3883 | ret = do_sigaction(sig, &new_sa, &old_sa); | |
3884 | ||
3885 | return ret ? ret : (unsigned long)old_sa.sa.sa_handler; | |
3886 | } | |
3887 | #endif /* __ARCH_WANT_SYS_SIGNAL */ | |
3888 | ||
3889 | #ifdef __ARCH_WANT_SYS_PAUSE | |
3890 | ||
a5f8fa9e | 3891 | SYSCALL_DEFINE0(pause) |
1da177e4 | 3892 | { |
d92fcf05 | 3893 | while (!signal_pending(current)) { |
1df01355 | 3894 | __set_current_state(TASK_INTERRUPTIBLE); |
d92fcf05 ON |
3895 | schedule(); |
3896 | } | |
1da177e4 LT |
3897 | return -ERESTARTNOHAND; |
3898 | } | |
3899 | ||
3900 | #endif | |
3901 | ||
9d8a7652 | 3902 | static int sigsuspend(sigset_t *set) |
68f3f16d | 3903 | { |
68f3f16d AV |
3904 | current->saved_sigmask = current->blocked; |
3905 | set_current_blocked(set); | |
3906 | ||
823dd322 SL |
3907 | while (!signal_pending(current)) { |
3908 | __set_current_state(TASK_INTERRUPTIBLE); | |
3909 | schedule(); | |
3910 | } | |
68f3f16d AV |
3911 | set_restore_sigmask(); |
3912 | return -ERESTARTNOHAND; | |
3913 | } | |
68f3f16d | 3914 | |
41c57892 RD |
3915 | /** |
3916 | * sys_rt_sigsuspend - replace the signal mask for a value with the | |
3917 | * @unewset value until a signal is received | |
3918 | * @unewset: new signal mask value | |
3919 | * @sigsetsize: size of sigset_t type | |
3920 | */ | |
d4e82042 | 3921 | SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize) |
150256d8 DW |
3922 | { |
3923 | sigset_t newset; | |
3924 | ||
3925 | /* XXX: Don't preclude handling different sized sigset_t's. */ | |
3926 | if (sigsetsize != sizeof(sigset_t)) | |
3927 | return -EINVAL; | |
3928 | ||
3929 | if (copy_from_user(&newset, unewset, sizeof(newset))) | |
3930 | return -EFAULT; | |
68f3f16d | 3931 | return sigsuspend(&newset); |
150256d8 | 3932 | } |
ad4b65a4 AV |
3933 | |
3934 | #ifdef CONFIG_COMPAT | |
3935 | COMPAT_SYSCALL_DEFINE2(rt_sigsuspend, compat_sigset_t __user *, unewset, compat_size_t, sigsetsize) | |
3936 | { | |
ad4b65a4 | 3937 | sigset_t newset; |
ad4b65a4 AV |
3938 | |
3939 | /* XXX: Don't preclude handling different sized sigset_t's. */ | |
3940 | if (sigsetsize != sizeof(sigset_t)) | |
3941 | return -EINVAL; | |
3942 | ||
3968cf62 | 3943 | if (get_compat_sigset(&newset, unewset)) |
ad4b65a4 | 3944 | return -EFAULT; |
ad4b65a4 | 3945 | return sigsuspend(&newset); |
ad4b65a4 AV |
3946 | } |
3947 | #endif | |
150256d8 | 3948 | |
0a0e8cdf AV |
3949 | #ifdef CONFIG_OLD_SIGSUSPEND |
3950 | SYSCALL_DEFINE1(sigsuspend, old_sigset_t, mask) | |
3951 | { | |
3952 | sigset_t blocked; | |
3953 | siginitset(&blocked, mask); | |
3954 | return sigsuspend(&blocked); | |
3955 | } | |
3956 | #endif | |
3957 | #ifdef CONFIG_OLD_SIGSUSPEND3 | |
3958 | SYSCALL_DEFINE3(sigsuspend, int, unused1, int, unused2, old_sigset_t, mask) | |
3959 | { | |
3960 | sigset_t blocked; | |
3961 | siginitset(&blocked, mask); | |
3962 | return sigsuspend(&blocked); | |
3963 | } | |
3964 | #endif | |
150256d8 | 3965 | |
52f5684c | 3966 | __weak const char *arch_vma_name(struct vm_area_struct *vma) |
f269fdd1 DH |
3967 | { |
3968 | return NULL; | |
3969 | } | |
3970 | ||
1da177e4 LT |
3971 | void __init signals_init(void) |
3972 | { | |
aba1be2f | 3973 | BUILD_BUG_ON(sizeof(struct siginfo) != SI_MAX_SIZE); |
41b27154 | 3974 | |
0a31bd5f | 3975 | sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC); |
1da177e4 | 3976 | } |
67fc4e0c JW |
3977 | |
3978 | #ifdef CONFIG_KGDB_KDB | |
3979 | #include <linux/kdb.h> | |
3980 | /* | |
0b44bf9a | 3981 | * kdb_send_sig - Allows kdb to send signals without exposing |
67fc4e0c JW |
3982 | * signal internals. This function checks if the required locks are |
3983 | * available before calling the main signal code, to avoid kdb | |
3984 | * deadlocks. | |
3985 | */ | |
0b44bf9a | 3986 | void kdb_send_sig(struct task_struct *t, int sig) |
67fc4e0c JW |
3987 | { |
3988 | static struct task_struct *kdb_prev_t; | |
0b44bf9a | 3989 | int new_t, ret; |
67fc4e0c JW |
3990 | if (!spin_trylock(&t->sighand->siglock)) { |
3991 | kdb_printf("Can't do kill command now.\n" | |
3992 | "The sigmask lock is held somewhere else in " | |
3993 | "kernel, try again later\n"); | |
3994 | return; | |
3995 | } | |
67fc4e0c JW |
3996 | new_t = kdb_prev_t != t; |
3997 | kdb_prev_t = t; | |
3998 | if (t->state != TASK_RUNNING && new_t) { | |
0b44bf9a | 3999 | spin_unlock(&t->sighand->siglock); |
67fc4e0c JW |
4000 | kdb_printf("Process is not RUNNING, sending a signal from " |
4001 | "kdb risks deadlock\n" | |
4002 | "on the run queue locks. " | |
4003 | "The signal has _not_ been sent.\n" | |
4004 | "Reissue the kill command if you want to risk " | |
4005 | "the deadlock.\n"); | |
4006 | return; | |
4007 | } | |
b213984b | 4008 | ret = send_signal(sig, SEND_SIG_PRIV, t, PIDTYPE_PID); |
0b44bf9a EB |
4009 | spin_unlock(&t->sighand->siglock); |
4010 | if (ret) | |
67fc4e0c JW |
4011 | kdb_printf("Fail to deliver Signal %d to process %d.\n", |
4012 | sig, t->pid); | |
4013 | else | |
4014 | kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid); | |
4015 | } | |
4016 | #endif /* CONFIG_KGDB_KDB */ |