blob: 13f8cff951567a4241c7251c8979cb3165af4952 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/slab.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040014#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
16#include <linux/sched.h>
Christian Braunercf9f8292018-11-19 00:51:56 +010017#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/fs.h>
Christian Braunercf9f8292018-11-19 00:51:56 +010019#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/tty.h>
21#include <linux/binfmts.h>
Alex Kelly179899f2012-10-04 17:15:24 -070022#include <linux/coredump.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/security.h>
24#include <linux/syscalls.h>
25#include <linux/ptrace.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070026#include <linux/signal.h>
Davide Libenzifba2afa2007-05-10 22:23:13 -070027#include <linux/signalfd.h>
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +090028#include <linux/ratelimit.h>
Roland McGrath35de2542008-07-25 19:45:51 -070029#include <linux/tracehook.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080030#include <linux/capability.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080031#include <linux/freezer.h>
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -080032#include <linux/pid_namespace.h>
33#include <linux/nsproxy.h>
Serge E. Hallyn6b550f92012-01-10 15:11:37 -080034#include <linux/user_namespace.h>
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +053035#include <linux/uprobes.h>
Al Viro90268432012-12-14 14:47:53 -050036#include <linux/compat.h>
Jesper Derehag2b5faa42013-03-19 20:50:05 +000037#include <linux/cn_proc.h>
Gideon Israel Dsouza52f56842014-04-07 15:39:20 -070038#include <linux/compiler.h>
39
Masami Hiramatsud1eb6502009-11-24 16:56:45 -050040#define CREATE_TRACE_POINTS
41#include <trace/events/signal.h>
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -080042
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <asm/param.h>
44#include <asm/uaccess.h>
45#include <asm/unistd.h>
46#include <asm/siginfo.h>
David Howellsd550bbd2012-03-28 18:30:03 +010047#include <asm/cacheflush.h>
Al Viroe1396062006-05-25 10:19:47 -040048#include "audit.h" /* audit_signal_info() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/*
51 * SLAB caches for signal bits.
52 */
53
Christoph Lametere18b8902006-12-06 20:33:20 -080054static struct kmem_cache *sigqueue_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +090056int print_fatal_signals __read_mostly;
57
Roland McGrath35de2542008-07-25 19:45:51 -070058static void __user *sig_handler(struct task_struct *t, int sig)
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070059{
Roland McGrath35de2542008-07-25 19:45:51 -070060 return t->sighand->action[sig - 1].sa.sa_handler;
61}
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070062
Roland McGrath35de2542008-07-25 19:45:51 -070063static int sig_handler_ignored(void __user *handler, int sig)
64{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070065 /* Is it explicitly or implicitly ignored? */
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070066 return handler == SIG_IGN ||
67 (handler == SIG_DFL && sig_kernel_ignore(sig));
68}
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Oleg Nesterovdef8cf72012-03-23 15:02:45 -070070static int sig_task_ignored(struct task_struct *t, int sig, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Roland McGrath35de2542008-07-25 19:45:51 -070072 void __user *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Oleg Nesterovf008faf2009-04-02 16:58:02 -070074 handler = sig_handler(t, sig);
75
Eric W. Biederman1f7d8a22018-07-19 19:47:27 -050076 /* SIGKILL and SIGSTOP may not be sent to the global init */
77 if (unlikely(is_global_init(t) && sig_kernel_only(sig)))
78 return true;
79
Oleg Nesterovf008faf2009-04-02 16:58:02 -070080 if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
Oleg Nesterov794ac8e2017-11-17 15:30:04 -080081 handler == SIG_DFL && !(force && sig_kernel_only(sig)))
Oleg Nesterovf008faf2009-04-02 16:58:02 -070082 return 1;
83
Eric W. Biedermana3714572019-08-16 12:33:54 -050084 /* Only allow kernel generated signals to this kthread */
85 if (unlikely((t->flags & PF_KTHREAD) &&
86 (handler == SIG_KTHREAD_KERNEL) && !force))
87 return true;
88
Oleg Nesterovf008faf2009-04-02 16:58:02 -070089 return sig_handler_ignored(handler, sig);
90}
91
Oleg Nesterovdef8cf72012-03-23 15:02:45 -070092static int sig_ignored(struct task_struct *t, int sig, bool force)
Oleg Nesterovf008faf2009-04-02 16:58:02 -070093{
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 /*
95 * Blocked signals are never ignored, since the
96 * signal handler may change by the time it is
97 * unblocked.
98 */
Roland McGrath325d22d2007-11-12 15:41:55 -080099 if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 return 0;
101
Oleg Nesterov1453b3a2017-11-17 15:30:01 -0800102 /*
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.
106 */
107 if (t->ptrace && sig != SIGKILL)
Roland McGrath35de2542008-07-25 19:45:51 -0700108 return 0;
109
Oleg Nesterov1453b3a2017-11-17 15:30:01 -0800110 return sig_task_ignored(t, sig, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
113/*
114 * Re-calculate pending state from the set of locally pending
115 * signals, globally pending signals, and blocked signals.
116 */
117static inline int has_pending_signals(sigset_t *signal, sigset_t *blocked)
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
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700145static int recalc_sigpending_tsk(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Tejun Heo3759a0d2011-06-02 11:14:00 +0200147 if ((t->jobctl & JOBCTL_PENDING_MASK) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 PENDING(&t->pending, &t->blocked) ||
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700149 PENDING(&t->signal->shared_pending, &t->blocked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 set_tsk_thread_flag(t, TIF_SIGPENDING);
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700151 return 1;
152 }
Roland McGrathb74d0de2007-06-06 03:59:00 -0700153 /*
154 * We must never clear the flag in another thread, or in current
155 * when it's possible the current syscall is returning -ERESTART*.
156 * So we don't clear it here, and only callers who know they should do.
157 */
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700158 return 0;
159}
160
161/*
162 * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
163 * This is superfluous when called on current, the wakeup is a harmless no-op.
164 */
165void recalc_sigpending_and_wake(struct task_struct *t)
166{
167 if (recalc_sigpending_tsk(t))
168 signal_wake_up(t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
171void recalc_sigpending(void)
172{
Tejun Heodd1d6772011-06-02 11:14:00 +0200173 if (!recalc_sigpending_tsk(current) && !freezing(current))
Roland McGrathb74d0de2007-06-06 03:59:00 -0700174 clear_thread_flag(TIF_SIGPENDING);
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
178/* Given the mask, find the first available signal that should be serviced. */
179
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800180#define SYNCHRONOUS_MASK \
181 (sigmask(SIGSEGV) | sigmask(SIGBUS) | sigmask(SIGILL) | \
Will Drewrya0727e82012-04-12 16:48:00 -0500182 sigmask(SIGTRAP) | sigmask(SIGFPE) | sigmask(SIGSYS))
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800183
Davide Libenzifba2afa2007-05-10 22:23:13 -0700184int next_signal(struct sigpending *pending, sigset_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 unsigned long i, *s, *m, x;
187 int sig = 0;
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 s = pending->signal.sig;
190 m = mask->sig;
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800191
192 /*
193 * Handle the first word specially: it contains the
194 * synchronous signals that need to be dequeued first.
195 */
196 x = *s &~ *m;
197 if (x) {
198 if (x & SYNCHRONOUS_MASK)
199 x &= SYNCHRONOUS_MASK;
200 sig = ffz(~x) + 1;
201 return sig;
202 }
203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 switch (_NSIG_WORDS) {
205 default:
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800206 for (i = 1; i < _NSIG_WORDS; ++i) {
207 x = *++s &~ *++m;
208 if (!x)
209 continue;
210 sig = ffz(~x) + i*_NSIG_BPW + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 break;
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 break;
214
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800215 case 2:
216 x = s[1] &~ m[1];
217 if (!x)
218 break;
219 sig = ffz(~x) + _NSIG_BPW + 1;
220 break;
221
222 case 1:
223 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 break;
225 }
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return sig;
228}
229
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900230static inline void print_dropped_signal(int sig)
231{
232 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
233
234 if (!print_fatal_signals)
235 return;
236
237 if (!__ratelimit(&ratelimit_state))
238 return;
239
Wang Xiaoqiang747800e2016-05-23 16:23:59 -0700240 pr_info("%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n",
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900241 current->comm, current->pid, sig);
242}
243
Tejun Heoe5c19022011-03-23 10:37:00 +0100244/**
Tejun Heo7dd3db52011-06-02 11:14:00 +0200245 * task_set_jobctl_pending - set jobctl pending bits
246 * @task: target task
247 * @mask: pending bits to set
248 *
249 * Clear @mask from @task->jobctl. @mask must be subset of
250 * %JOBCTL_PENDING_MASK | %JOBCTL_STOP_CONSUME | %JOBCTL_STOP_SIGMASK |
251 * %JOBCTL_TRAPPING. If stop signo is being set, the existing signo is
252 * cleared. If @task is already being killed or exiting, this function
253 * becomes noop.
254 *
255 * CONTEXT:
256 * Must be called with @task->sighand->siglock held.
257 *
258 * RETURNS:
259 * %true if @mask is set, %false if made noop because @task was dying.
260 */
Palmer Dabbeltb76808e2015-04-30 21:19:57 -0700261bool task_set_jobctl_pending(struct task_struct *task, unsigned long mask)
Tejun Heo7dd3db52011-06-02 11:14:00 +0200262{
263 BUG_ON(mask & ~(JOBCTL_PENDING_MASK | JOBCTL_STOP_CONSUME |
264 JOBCTL_STOP_SIGMASK | JOBCTL_TRAPPING));
265 BUG_ON((mask & JOBCTL_TRAPPING) && !(mask & JOBCTL_PENDING_MASK));
266
267 if (unlikely(fatal_signal_pending(task) || (task->flags & PF_EXITING)))
268 return false;
269
270 if (mask & JOBCTL_STOP_SIGMASK)
271 task->jobctl &= ~JOBCTL_STOP_SIGMASK;
272
273 task->jobctl |= mask;
274 return true;
275}
276
277/**
Tejun Heoa8f072c2011-06-02 11:13:59 +0200278 * task_clear_jobctl_trapping - clear jobctl trapping bit
Tejun Heod79fdd62011-03-23 10:37:00 +0100279 * @task: target task
280 *
Tejun Heoa8f072c2011-06-02 11:13:59 +0200281 * If JOBCTL_TRAPPING is set, a ptracer is waiting for us to enter TRACED.
282 * Clear it and wake up the ptracer. Note that we don't need any further
283 * locking. @task->siglock guarantees that @task->parent points to the
284 * ptracer.
Tejun Heod79fdd62011-03-23 10:37:00 +0100285 *
286 * CONTEXT:
287 * Must be called with @task->sighand->siglock held.
288 */
Tejun Heo73ddff22011-06-14 11:20:14 +0200289void task_clear_jobctl_trapping(struct task_struct *task)
Tejun Heod79fdd62011-03-23 10:37:00 +0100290{
Tejun Heoa8f072c2011-06-02 11:13:59 +0200291 if (unlikely(task->jobctl & JOBCTL_TRAPPING)) {
292 task->jobctl &= ~JOBCTL_TRAPPING;
Oleg Nesterov650226b2014-06-06 14:36:44 -0700293 smp_mb(); /* advised by wake_up_bit() */
Tejun Heo62c124f2011-06-02 11:14:00 +0200294 wake_up_bit(&task->jobctl, JOBCTL_TRAPPING_BIT);
Tejun Heod79fdd62011-03-23 10:37:00 +0100295 }
296}
297
298/**
Tejun Heo3759a0d2011-06-02 11:14:00 +0200299 * task_clear_jobctl_pending - clear jobctl pending bits
Tejun Heoe5c19022011-03-23 10:37:00 +0100300 * @task: target task
Tejun Heo3759a0d2011-06-02 11:14:00 +0200301 * @mask: pending bits to clear
Tejun Heoe5c19022011-03-23 10:37:00 +0100302 *
Tejun Heo3759a0d2011-06-02 11:14:00 +0200303 * Clear @mask from @task->jobctl. @mask must be subset of
304 * %JOBCTL_PENDING_MASK. If %JOBCTL_STOP_PENDING is being cleared, other
305 * STOP bits are cleared together.
Tejun Heoe5c19022011-03-23 10:37:00 +0100306 *
Tejun Heo6dfca322011-06-02 11:14:00 +0200307 * If clearing of @mask leaves no stop or trap pending, this function calls
308 * task_clear_jobctl_trapping().
Tejun Heoe5c19022011-03-23 10:37:00 +0100309 *
310 * CONTEXT:
311 * Must be called with @task->sighand->siglock held.
312 */
Palmer Dabbeltb76808e2015-04-30 21:19:57 -0700313void task_clear_jobctl_pending(struct task_struct *task, unsigned long mask)
Tejun Heoe5c19022011-03-23 10:37:00 +0100314{
Tejun Heo3759a0d2011-06-02 11:14:00 +0200315 BUG_ON(mask & ~JOBCTL_PENDING_MASK);
316
317 if (mask & JOBCTL_STOP_PENDING)
318 mask |= JOBCTL_STOP_CONSUME | JOBCTL_STOP_DEQUEUED;
319
320 task->jobctl &= ~mask;
Tejun Heo6dfca322011-06-02 11:14:00 +0200321
322 if (!(task->jobctl & JOBCTL_PENDING_MASK))
323 task_clear_jobctl_trapping(task);
Tejun Heoe5c19022011-03-23 10:37:00 +0100324}
325
326/**
327 * task_participate_group_stop - participate in a group stop
328 * @task: task participating in a group stop
329 *
Tejun Heoa8f072c2011-06-02 11:13:59 +0200330 * @task has %JOBCTL_STOP_PENDING set and is participating in a group stop.
Tejun Heo39efa3e2011-03-23 10:37:00 +0100331 * Group stop states are cleared and the group stop count is consumed if
Tejun Heoa8f072c2011-06-02 11:13:59 +0200332 * %JOBCTL_STOP_CONSUME was set. If the consumption completes the group
Tejun Heo39efa3e2011-03-23 10:37:00 +0100333 * stop, the appropriate %SIGNAL_* flags are set.
Tejun Heoe5c19022011-03-23 10:37:00 +0100334 *
335 * CONTEXT:
336 * Must be called with @task->sighand->siglock held.
Tejun Heo244056f2011-03-23 10:37:01 +0100337 *
338 * RETURNS:
339 * %true if group stop completion should be notified to the parent, %false
340 * otherwise.
Tejun Heoe5c19022011-03-23 10:37:00 +0100341 */
342static bool task_participate_group_stop(struct task_struct *task)
343{
344 struct signal_struct *sig = task->signal;
Tejun Heoa8f072c2011-06-02 11:13:59 +0200345 bool consume = task->jobctl & JOBCTL_STOP_CONSUME;
Tejun Heoe5c19022011-03-23 10:37:00 +0100346
Tejun Heoa8f072c2011-06-02 11:13:59 +0200347 WARN_ON_ONCE(!(task->jobctl & JOBCTL_STOP_PENDING));
Tejun Heo39efa3e2011-03-23 10:37:00 +0100348
Tejun Heo3759a0d2011-06-02 11:14:00 +0200349 task_clear_jobctl_pending(task, JOBCTL_STOP_PENDING);
Tejun Heoe5c19022011-03-23 10:37:00 +0100350
351 if (!consume)
352 return false;
353
354 if (!WARN_ON_ONCE(sig->group_stop_count == 0))
355 sig->group_stop_count--;
356
Tejun Heo244056f2011-03-23 10:37:01 +0100357 /*
358 * Tell the caller to notify completion iff we are entering into a
359 * fresh group stop. Read comment in do_signal_stop() for details.
360 */
361 if (!sig->group_stop_count && !(sig->flags & SIGNAL_STOP_STOPPED)) {
Jamie Iles916a05b2017-01-10 16:57:54 -0800362 signal_set_stop_flags(sig, SIGNAL_STOP_STOPPED);
Tejun Heoe5c19022011-03-23 10:37:00 +0100363 return true;
364 }
365 return false;
366}
367
David Howellsc69e8d92008-11-14 10:39:19 +1100368/*
369 * allocate a new signal queue record
370 * - this may be called without locks if and only if t == current, otherwise an
Randy Dunlap5aba0852011-04-04 14:59:31 -0700371 * appropriate lock must be held to stop the target task from exiting
David Howellsc69e8d92008-11-14 10:39:19 +1100372 */
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900373static struct sigqueue *
374__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 struct sigqueue *q = NULL;
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800377 struct user_struct *user;
Linus Torvalds43062592020-02-24 12:47:14 -0800378 int sigpending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800380 /*
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000381 * Protect access to @t credentials. This can go away when all
382 * callers hold rcu read lock.
Linus Torvalds43062592020-02-24 12:47:14 -0800383 *
384 * NOTE! A pending signal will hold on to the user refcount,
385 * and we get/put the refcount only when the sigpending count
386 * changes from/to zero.
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800387 */
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000388 rcu_read_lock();
Linus Torvalds43062592020-02-24 12:47:14 -0800389 user = __task_cred(t)->user;
390 sigpending = atomic_inc_return(&user->sigpending);
391 if (sigpending == 1)
392 get_uid(user);
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000393 rcu_read_unlock();
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900394
Linus Torvalds43062592020-02-24 12:47:14 -0800395 if (override_rlimit || likely(sigpending <= task_rlimit(t, RLIMIT_SIGPENDING))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 q = kmem_cache_alloc(sigqueue_cachep, flags);
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900397 } else {
398 print_dropped_signal(sig);
399 }
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (unlikely(q == NULL)) {
Linus Torvalds43062592020-02-24 12:47:14 -0800402 if (atomic_dec_and_test(&user->sigpending))
403 free_uid(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 } else {
405 INIT_LIST_HEAD(&q->list);
406 q->flags = 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100407 q->user = user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 }
David Howellsd84f4f92008-11-14 10:39:23 +1100409
410 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
Andrew Morton514a01b2006-02-03 03:04:41 -0800413static void __sigqueue_free(struct sigqueue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
415 if (q->flags & SIGQUEUE_PREALLOC)
416 return;
Linus Torvalds43062592020-02-24 12:47:14 -0800417 if (atomic_dec_and_test(&q->user->sigpending))
418 free_uid(q->user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 kmem_cache_free(sigqueue_cachep, q);
420}
421
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800422void flush_sigqueue(struct sigpending *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 struct sigqueue *q;
425
426 sigemptyset(&queue->signal);
427 while (!list_empty(&queue->list)) {
428 q = list_entry(queue->list.next, struct sigqueue , list);
429 list_del_init(&q->list);
430 __sigqueue_free(q);
431 }
432}
433
434/*
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -0400435 * Flush all pending signals for this kthread.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 */
Oleg Nesterovc81addc2006-03-28 16:11:17 -0800437void flush_signals(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 unsigned long flags;
440
441 spin_lock_irqsave(&t->sighand->siglock, flags);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -0400442 clear_tsk_thread_flag(t, TIF_SIGPENDING);
443 flush_sigqueue(&t->pending);
444 flush_sigqueue(&t->signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 spin_unlock_irqrestore(&t->sighand->siglock, flags);
446}
447
Oleg Nesterovcbaffba2008-05-26 20:55:42 +0400448static void __flush_itimer_signals(struct sigpending *pending)
449{
450 sigset_t signal, retain;
451 struct sigqueue *q, *n;
452
453 signal = pending->signal;
454 sigemptyset(&retain);
455
456 list_for_each_entry_safe(q, n, &pending->list, list) {
457 int sig = q->info.si_signo;
458
459 if (likely(q->info.si_code != SI_TIMER)) {
460 sigaddset(&retain, sig);
461 } else {
462 sigdelset(&signal, sig);
463 list_del_init(&q->list);
464 __sigqueue_free(q);
465 }
466 }
467
468 sigorsets(&pending->signal, &signal, &retain);
469}
470
471void flush_itimer_signals(void)
472{
473 struct task_struct *tsk = current;
474 unsigned long flags;
475
476 spin_lock_irqsave(&tsk->sighand->siglock, flags);
477 __flush_itimer_signals(&tsk->pending);
478 __flush_itimer_signals(&tsk->signal->shared_pending);
479 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
480}
481
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700482void ignore_signals(struct task_struct *t)
483{
484 int i;
485
486 for (i = 0; i < _NSIG; ++i)
487 t->sighand->action[i].sa.sa_handler = SIG_IGN;
488
489 flush_signals(t);
490}
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 * Flush all handlers for a task.
494 */
495
496void
497flush_signal_handlers(struct task_struct *t, int force_default)
498{
499 int i;
500 struct k_sigaction *ka = &t->sighand->action[0];
501 for (i = _NSIG ; i != 0 ; i--) {
502 if (force_default || ka->sa.sa_handler != SIG_IGN)
503 ka->sa.sa_handler = SIG_DFL;
504 ka->sa.sa_flags = 0;
Andrew Morton522cff12013-03-13 14:59:34 -0700505#ifdef __ARCH_HAS_SA_RESTORER
Kees Cook2ca39522013-03-13 14:59:33 -0700506 ka->sa.sa_restorer = NULL;
507#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 sigemptyset(&ka->sa.sa_mask);
509 ka++;
510 }
511}
512
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200513int unhandled_signal(struct task_struct *tsk, int sig)
514{
Roland McGrath445a91d2008-07-25 19:45:52 -0700515 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700516 if (is_global_init(tsk))
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200517 return 1;
Roland McGrath445a91d2008-07-25 19:45:52 -0700518 if (handler != SIG_IGN && handler != SIG_DFL)
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200519 return 0;
Tejun Heoa288eec2011-06-17 16:50:37 +0200520 /* if ptraced, let the tracer determine */
521 return !tsk->ptrace;
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200522}
523
Eric W. Biedermanf719f202017-06-13 04:31:16 -0500524static void collect_signal(int sig, struct sigpending *list, siginfo_t *info,
525 bool *resched_timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526{
527 struct sigqueue *q, *first = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /*
530 * Collect the siginfo appropriate to this signal. Check if
531 * there is another siginfo for the same signal.
532 */
533 list_for_each_entry(q, &list->list, list) {
534 if (q->info.si_signo == sig) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700535 if (first)
536 goto still_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 first = q;
538 }
539 }
Oleg Nesterovd4434202008-07-25 01:47:28 -0700540
541 sigdelset(&list->signal, sig);
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 if (first) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700544still_pending:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 list_del_init(&first->list);
546 copy_siginfo(info, &first->info);
Eric W. Biedermanf719f202017-06-13 04:31:16 -0500547
548 *resched_timer =
549 (first->flags & SIGQUEUE_PREALLOC) &&
550 (info->si_code == SI_TIMER) &&
551 (info->si_sys_private);
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 __sigqueue_free(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 } else {
Randy Dunlap5aba0852011-04-04 14:59:31 -0700555 /*
556 * Ok, it wasn't in the queue. This must be
557 * a fast-pathed signal or we must have been
558 * out of queue space. So zero out the info.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 info->si_signo = sig;
561 info->si_errno = 0;
Oleg Nesterov7486e5d2009-12-15 16:47:24 -0800562 info->si_code = SI_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 info->si_pid = 0;
564 info->si_uid = 0;
565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
568static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
Eric W. Biedermanf719f202017-06-13 04:31:16 -0500569 siginfo_t *info, bool *resched_timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Roland McGrath27d91e02006-09-29 02:00:31 -0700571 int sig = next_signal(pending, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Oleg Nesterov2e01fab2015-11-06 16:32:19 -0800573 if (sig)
Eric W. Biedermanf719f202017-06-13 04:31:16 -0500574 collect_signal(sig, pending, info, resched_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 return sig;
576}
577
578/*
Randy Dunlap5aba0852011-04-04 14:59:31 -0700579 * Dequeue a signal and return the element to the caller, which is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 * expected to free it.
581 *
582 * All callers have to hold the siglock.
583 */
584int dequeue_signal(struct task_struct *tsk, sigset_t *mask, siginfo_t *info)
585{
Eric W. Biedermanf719f202017-06-13 04:31:16 -0500586 bool resched_timer = false;
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700587 int signr;
Benjamin Herrenschmidtcaec4e82007-06-12 08:16:18 +1000588
589 /* We only dequeue private signals from ourselves, we don't let
590 * signalfd steal them
591 */
Eric W. Biedermanf719f202017-06-13 04:31:16 -0500592 signr = __dequeue_signal(&tsk->pending, mask, info, &resched_timer);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800593 if (!signr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 signr = __dequeue_signal(&tsk->signal->shared_pending,
Eric W. Biedermanf719f202017-06-13 04:31:16 -0500595 mask, info, &resched_timer);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800596 /*
597 * itimer signal ?
598 *
599 * itimers are process shared and we restart periodic
600 * itimers in the signal delivery path to prevent DoS
601 * attacks in the high resolution timer case. This is
Randy Dunlap5aba0852011-04-04 14:59:31 -0700602 * compliant with the old way of self-restarting
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800603 * itimers, as the SIGALRM is a legacy signal and only
604 * queued once. Changing the restart behaviour to
605 * restart the timer in the signal dequeue path is
606 * reducing the timer noise on heavy loaded !highres
607 * systems too.
608 */
609 if (unlikely(signr == SIGALRM)) {
610 struct hrtimer *tmr = &tsk->signal->real_timer;
611
612 if (!hrtimer_is_queued(tmr) &&
613 tsk->signal->it_real_incr.tv64 != 0) {
614 hrtimer_forward(tmr, tmr->base->get_time(),
615 tsk->signal->it_real_incr);
616 hrtimer_restart(tmr);
617 }
618 }
619 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700620
Davide Libenzib8fceee2007-09-20 12:40:16 -0700621 recalc_sigpending();
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700622 if (!signr)
623 return 0;
624
625 if (unlikely(sig_kernel_stop(signr))) {
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800626 /*
627 * Set a marker that we have dequeued a stop signal. Our
628 * caller might release the siglock and then the pending
629 * stop signal it is about to process is no longer in the
630 * pending bitmasks, but must still be cleared by a SIGCONT
631 * (and overruled by a SIGKILL). So those cases clear this
632 * shared flag after we've set it. Note that this flag may
633 * remain set after the signal we return is ignored or
634 * handled. That doesn't matter because its only purpose
635 * is to alert stop-signal processing code when another
636 * processor has come along and cleared the flag.
637 */
Tejun Heoa8f072c2011-06-02 11:13:59 +0200638 current->jobctl |= JOBCTL_STOP_DEQUEUED;
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800639 }
Eric W. Biedermanf719f202017-06-13 04:31:16 -0500640 if (resched_timer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 /*
642 * Release the siglock to ensure proper locking order
643 * of timer locks outside of siglocks. Note, we leave
644 * irqs disabled here, since the posix-timers code is
645 * about to disable them again anyway.
646 */
647 spin_unlock(&tsk->sighand->siglock);
648 do_schedule_next_timer(info);
649 spin_lock(&tsk->sighand->siglock);
650 }
651 return signr;
652}
653
654/*
655 * Tell a process that it has a new active signal..
656 *
657 * NOTE! we rely on the previous spin_lock to
658 * lock interrupts for us! We can only be called with
659 * "siglock" held, and the local interrupt must
660 * have been disabled when that got acquired!
661 *
662 * No need to set need_resched since signal event passing
663 * goes through ->blocked
664 */
Oleg Nesterov910ffdb2013-01-21 20:47:41 +0100665void signal_wake_up_state(struct task_struct *t, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 set_tsk_thread_flag(t, TIF_SIGPENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 /*
Oleg Nesterov910ffdb2013-01-21 20:47:41 +0100669 * TASK_WAKEKILL also means wake it up in the stopped/traced/killable
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500670 * case. We don't check t->state here because there is a race with it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 * executing another processor and just now entering stopped state.
672 * By using wake_up_state, we ensure the process will wake up and
673 * handle its death signal.
674 */
Oleg Nesterov910ffdb2013-01-21 20:47:41 +0100675 if (!wake_up_state(t, state | TASK_INTERRUPTIBLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 kick_process(t);
677}
678
679/*
680 * Remove signals in mask from the pending set and queue.
681 * Returns 1 if any signals were found.
682 *
683 * All callers must be holding the siglock.
George Anzinger71fabd52006-01-08 01:02:48 -0800684 */
Oleg Nesterovc09c1442014-06-06 14:36:50 -0700685static int flush_sigqueue_mask(sigset_t *mask, struct sigpending *s)
George Anzinger71fabd52006-01-08 01:02:48 -0800686{
687 struct sigqueue *q, *n;
688 sigset_t m;
689
690 sigandsets(&m, mask, &s->signal);
691 if (sigisemptyset(&m))
692 return 0;
693
Oleg Nesterov702a5072011-04-27 22:01:27 +0200694 sigandnsets(&s->signal, &s->signal, mask);
George Anzinger71fabd52006-01-08 01:02:48 -0800695 list_for_each_entry_safe(q, n, &s->list, list) {
696 if (sigismember(mask, q->info.si_signo)) {
697 list_del_init(&q->list);
698 __sigqueue_free(q);
699 }
700 }
701 return 1;
702}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Oleg Nesterov614c5172009-12-15 16:47:22 -0800704static inline int is_si_special(const struct siginfo *info)
705{
706 return info <= SEND_SIG_FORCED;
707}
708
709static inline bool si_fromuser(const struct siginfo *info)
710{
711 return info == SEND_SIG_NOINFO ||
712 (!is_si_special(info) && SI_FROMUSER(info));
713}
714
Eric W. Biederman181f1f0d2019-02-06 17:51:47 -0600715static int dequeue_synchronous_signal(siginfo_t *info)
716{
717 struct task_struct *tsk = current;
718 struct sigpending *pending = &tsk->pending;
719 struct sigqueue *q, *sync = NULL;
720
721 /*
722 * Might a synchronous signal be in the queue?
723 */
724 if (!((pending->signal.sig[0] & ~tsk->blocked.sig[0]) & SYNCHRONOUS_MASK))
725 return 0;
726
727 /*
728 * Return the first synchronous signal in the queue.
729 */
730 list_for_each_entry(q, &pending->list, list) {
731 /* Synchronous signals have a postive si_code */
732 if ((q->info.si_code > SI_USER) &&
733 (sigmask(q->info.si_signo) & SYNCHRONOUS_MASK)) {
734 sync = q;
735 goto next;
736 }
737 }
738 return 0;
739next:
740 /*
741 * Check if there is another siginfo for the same signal.
742 */
743 list_for_each_entry_continue(q, &pending->list, list) {
744 if (q->info.si_signo == sync->info.si_signo)
745 goto still_pending;
746 }
747
748 sigdelset(&pending->signal, sync->info.si_signo);
749 recalc_sigpending();
750still_pending:
751 list_del_init(&sync->list);
752 copy_siginfo(info, &sync->info);
753 __sigqueue_free(sync);
754 return info->si_signo;
755}
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757/*
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700758 * called with RCU read lock from check_kill_permission()
759 */
760static int kill_ok_by_cred(struct task_struct *t)
761{
762 const struct cred *cred = current_cred();
763 const struct cred *tcred = __task_cred(t);
764
Eric W. Biederman5af66202012-03-03 20:21:47 -0800765 if (uid_eq(cred->euid, tcred->suid) ||
766 uid_eq(cred->euid, tcred->uid) ||
767 uid_eq(cred->uid, tcred->suid) ||
768 uid_eq(cred->uid, tcred->uid))
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700769 return 1;
770
Eric W. Biedermanc4a4d602011-11-16 23:15:31 -0800771 if (ns_capable(tcred->user_ns, CAP_KILL))
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700772 return 1;
773
774 return 0;
775}
776
777/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 * Bad permissions for sending the signal
David Howells694f6902010-08-04 16:59:14 +0100779 * - the caller must hold the RCU read lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 */
781static int check_kill_permission(int sig, struct siginfo *info,
782 struct task_struct *t)
783{
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700784 struct pid *sid;
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700785 int error;
786
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700787 if (!valid_signal(sig))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700788 return -EINVAL;
789
Oleg Nesterov614c5172009-12-15 16:47:22 -0800790 if (!si_fromuser(info))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700791 return 0;
792
793 error = audit_signal_info(sig, t); /* Let audit system see the signal */
794 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return error;
Amy Griffise54dc242007-03-29 18:01:04 -0400796
Oleg Nesterov065add32010-05-26 14:42:54 -0700797 if (!same_thread_group(current, t) &&
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700798 !kill_ok_by_cred(t)) {
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700799 switch (sig) {
800 case SIGCONT:
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700801 sid = task_session(t);
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700802 /*
803 * We don't return the error if sid == NULL. The
804 * task was unhashed, the caller must notice this.
805 */
806 if (!sid || sid == task_session(current))
807 break;
808 default:
809 return -EPERM;
810 }
811 }
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100812
Amy Griffise54dc242007-03-29 18:01:04 -0400813 return security_task_kill(t, info, sig, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
Tejun Heofb1d9102011-06-14 11:20:17 +0200816/**
817 * ptrace_trap_notify - schedule trap to notify ptracer
818 * @t: tracee wanting to notify tracer
819 *
820 * This function schedules sticky ptrace trap which is cleared on the next
821 * TRAP_STOP to notify ptracer of an event. @t must have been seized by
822 * ptracer.
823 *
Tejun Heo544b2c92011-06-14 11:20:18 +0200824 * If @t is running, STOP trap will be taken. If trapped for STOP and
825 * ptracer is listening for events, tracee is woken up so that it can
826 * re-trap for the new event. If trapped otherwise, STOP trap will be
827 * eventually taken without returning to userland after the existing traps
828 * are finished by PTRACE_CONT.
Tejun Heofb1d9102011-06-14 11:20:17 +0200829 *
830 * CONTEXT:
831 * Must be called with @task->sighand->siglock held.
832 */
833static void ptrace_trap_notify(struct task_struct *t)
834{
835 WARN_ON_ONCE(!(t->ptrace & PT_SEIZED));
836 assert_spin_locked(&t->sighand->siglock);
837
838 task_set_jobctl_pending(t, JOBCTL_TRAP_NOTIFY);
Oleg Nesterov910ffdb2013-01-21 20:47:41 +0100839 ptrace_signal_wake_up(t, t->jobctl & JOBCTL_LISTENING);
Tejun Heofb1d9102011-06-14 11:20:17 +0200840}
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842/*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700843 * Handle magic process-wide effects of stop/continue signals. Unlike
844 * the signal actions, these happen immediately at signal-generation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 * time regardless of blocking, ignoring, or handling. This does the
846 * actual continuing for SIGCONT, but not the actual stopping for stop
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700847 * signals. The process stop is done as a signal action for SIG_DFL.
848 *
849 * Returns true if the signal should be actually delivered, otherwise
850 * it should be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 */
Oleg Nesterov403bad72013-04-30 15:28:10 -0700852static bool prepare_signal(int sig, struct task_struct *p, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853{
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700854 struct signal_struct *signal = p->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 struct task_struct *t;
Oleg Nesterov9490592f2014-06-06 14:36:48 -0700856 sigset_t flush;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
Oleg Nesterov403bad72013-04-30 15:28:10 -0700858 if (signal->flags & (SIGNAL_GROUP_EXIT | SIGNAL_GROUP_COREDUMP)) {
Oleg Nesterov5fa534c2015-11-06 16:32:31 -0800859 if (!(signal->flags & SIGNAL_GROUP_EXIT))
Oleg Nesterov403bad72013-04-30 15:28:10 -0700860 return sig == SIGKILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 /*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700862 * The process is in the middle of dying, nothing to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700864 } else if (sig_kernel_stop(sig)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 /*
866 * This is a stop signal. Remove SIGCONT from all queues.
867 */
Oleg Nesterov9490592f2014-06-06 14:36:48 -0700868 siginitset(&flush, sigmask(SIGCONT));
Oleg Nesterovc09c1442014-06-06 14:36:50 -0700869 flush_sigqueue_mask(&flush, &signal->shared_pending);
Oleg Nesterov9490592f2014-06-06 14:36:48 -0700870 for_each_thread(p, t)
Oleg Nesterovc09c1442014-06-06 14:36:50 -0700871 flush_sigqueue_mask(&flush, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 } else if (sig == SIGCONT) {
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700873 unsigned int why;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 /*
Oleg Nesterov1deac632011-04-01 20:11:50 +0200875 * Remove all stop signals from all queues, wake all threads.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 */
Oleg Nesterov9490592f2014-06-06 14:36:48 -0700877 siginitset(&flush, SIG_KERNEL_STOP_MASK);
Oleg Nesterovc09c1442014-06-06 14:36:50 -0700878 flush_sigqueue_mask(&flush, &signal->shared_pending);
Oleg Nesterov9490592f2014-06-06 14:36:48 -0700879 for_each_thread(p, t) {
Oleg Nesterovc09c1442014-06-06 14:36:50 -0700880 flush_sigqueue_mask(&flush, &t->pending);
Tejun Heo3759a0d2011-06-02 11:14:00 +0200881 task_clear_jobctl_pending(t, JOBCTL_STOP_PENDING);
Tejun Heofb1d9102011-06-14 11:20:17 +0200882 if (likely(!(t->ptrace & PT_SEIZED)))
883 wake_up_state(t, __TASK_STOPPED);
884 else
885 ptrace_trap_notify(t);
Oleg Nesterov9490592f2014-06-06 14:36:48 -0700886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700888 /*
889 * Notify the parent with CLD_CONTINUED if we were stopped.
890 *
891 * If we were in the middle of a group stop, we pretend it
892 * was already finished, and then continued. Since SIGCHLD
893 * doesn't queue we report only CLD_STOPPED, as if the next
894 * CLD_CONTINUED was dropped.
895 */
896 why = 0;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700897 if (signal->flags & SIGNAL_STOP_STOPPED)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700898 why |= SIGNAL_CLD_CONTINUED;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700899 else if (signal->group_stop_count)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700900 why |= SIGNAL_CLD_STOPPED;
901
902 if (why) {
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700903 /*
Roland McGrathae6d2ed2009-09-23 15:56:53 -0700904 * The first thread which returns from do_signal_stop()
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700905 * will take ->siglock, notice SIGNAL_CLD_MASK, and
906 * notify its parent. See get_signal_to_deliver().
907 */
Jamie Iles916a05b2017-01-10 16:57:54 -0800908 signal_set_stop_flags(signal, why | SIGNAL_STOP_CONTINUED);
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700909 signal->group_stop_count = 0;
910 signal->group_exit_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 }
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700913
Oleg Nesterovdef8cf72012-03-23 15:02:45 -0700914 return !sig_ignored(p, sig, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700917/*
918 * Test if P wants to take SIG. After we've checked all threads with this,
919 * it's equivalent to finding no threads not blocking SIG. Any threads not
920 * blocking SIG were ruled out because they are not running and already
921 * have pending signals. Such threads will dequeue from the shared queue
922 * as soon as they're available, so putting the signal on the shared queue
923 * will be equivalent to sending it to one such thread.
924 */
925static inline int wants_signal(int sig, struct task_struct *p)
926{
927 if (sigismember(&p->blocked, sig))
928 return 0;
929 if (p->flags & PF_EXITING)
930 return 0;
931 if (sig == SIGKILL)
932 return 1;
933 if (task_is_stopped_or_traced(p))
934 return 0;
935 return task_curr(p) || !signal_pending(p);
936}
937
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700938static void complete_signal(int sig, struct task_struct *p, int group)
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700939{
940 struct signal_struct *signal = p->signal;
941 struct task_struct *t;
942
943 /*
944 * Now find a thread we can wake up to take the signal off the queue.
945 *
946 * If the main thread wants the signal, it gets first crack.
947 * Probably the least surprising to the average bear.
948 */
949 if (wants_signal(sig, p))
950 t = p;
Oleg Nesterov5fcd8352008-04-30 00:52:55 -0700951 else if (!group || thread_group_empty(p))
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700952 /*
953 * There is just one thread and it does not need to be woken.
954 * It will dequeue unblocked signals before it runs again.
955 */
956 return;
957 else {
958 /*
959 * Otherwise try to find a suitable thread.
960 */
961 t = signal->curr_target;
962 while (!wants_signal(sig, t)) {
963 t = next_thread(t);
964 if (t == signal->curr_target)
965 /*
966 * No thread needs to be woken.
967 * Any eligible threads will see
968 * the signal in the queue soon.
969 */
970 return;
971 }
972 signal->curr_target = t;
973 }
974
975 /*
976 * Found a killable thread. If the signal will be fatal,
977 * then start taking the whole group down immediately.
978 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -0700979 if (sig_fatal(p, sig) &&
Oleg Nesterov4d53eb42017-11-17 15:30:08 -0800980 !(signal->flags & SIGNAL_GROUP_EXIT) &&
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700981 !sigismember(&t->real_blocked, sig) &&
Oleg Nesterov4d53eb42017-11-17 15:30:08 -0800982 (sig == SIGKILL || !p->ptrace)) {
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700983 /*
984 * This signal will be fatal to the whole group.
985 */
986 if (!sig_kernel_coredump(sig)) {
987 /*
988 * Start a group exit and wake everybody up.
989 * This way we don't have other threads
990 * running and doing things after a slower
991 * thread has the fatal signal pending.
992 */
993 signal->flags = SIGNAL_GROUP_EXIT;
994 signal->group_exit_code = sig;
995 signal->group_stop_count = 0;
996 t = p;
997 do {
Tejun Heo6dfca322011-06-02 11:14:00 +0200998 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700999 sigaddset(&t->pending.signal, SIGKILL);
1000 signal_wake_up(t, 1);
1001 } while_each_thread(p, t);
1002 return;
1003 }
1004 }
1005
1006 /*
1007 * The signal is already in the shared-pending queue.
1008 * Tell the chosen thread to wake up and dequeue it.
1009 */
1010 signal_wake_up(t, sig == SIGKILL);
1011 return;
1012}
1013
Pavel Emelyanovaf7fff92008-04-30 00:52:34 -07001014static inline int legacy_queue(struct sigpending *signals, int sig)
1015{
1016 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
1017}
1018
Serge E. Hallyn6b550f92012-01-10 15:11:37 -08001019#ifdef CONFIG_USER_NS
1020static inline void userns_fixup_signal_uid(struct siginfo *info, struct task_struct *t)
1021{
1022 if (current_user_ns() == task_cred_xxx(t, user_ns))
1023 return;
1024
1025 if (SI_FROMKERNEL(info))
1026 return;
1027
Eric W. Biederman078de5f2012-02-08 07:00:08 -08001028 rcu_read_lock();
1029 info->si_uid = from_kuid_munged(task_cred_xxx(t, user_ns),
1030 make_kuid(current_user_ns(), info->si_uid));
1031 rcu_read_unlock();
Serge E. Hallyn6b550f92012-01-10 15:11:37 -08001032}
1033#else
1034static inline void userns_fixup_signal_uid(struct siginfo *info, struct task_struct *t)
1035{
1036 return;
1037}
1038#endif
1039
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001040static int __send_signal(int sig, struct siginfo *info, struct task_struct *t,
1041 int group, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001043 struct sigpending *pending;
Oleg Nesterov6e65acb2008-04-30 00:52:50 -07001044 struct sigqueue *q;
Vegard Nossum7a0aeb12009-05-16 11:28:33 +02001045 int override_rlimit;
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001046 int ret = 0, result;
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04001047
Oleg Nesterov6e65acb2008-04-30 00:52:50 -07001048 assert_spin_locked(&t->sighand->siglock);
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001049
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001050 result = TRACE_SIGNAL_IGNORED;
Oleg Nesterov629d3622012-03-23 15:02:44 -07001051 if (!prepare_signal(sig, t,
Eric W. Biedermanba277fe2018-09-03 20:02:46 +02001052 from_ancestor_ns || (info == SEND_SIG_PRIV) || (info == SEND_SIG_FORCED)))
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001053 goto ret;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001054
1055 pending = group ? &t->signal->shared_pending : &t->pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 /*
Pavel Emelyanov2acb0242008-04-30 00:52:35 -07001057 * Short-circuit ignored signals and support queuing
1058 * exactly one non-rt signal, so that we can get more
1059 * detailed information about the cause of the signal.
1060 */
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001061 result = TRACE_SIGNAL_ALREADY_PENDING;
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001062 if (legacy_queue(pending, sig))
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001063 goto ret;
1064
1065 result = TRACE_SIGNAL_DELIVERED;
Davide Libenzifba2afa2007-05-10 22:23:13 -07001066 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 * fast-pathed signals for kernel-internal things like SIGSTOP
1068 * or SIGKILL.
1069 */
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001070 if (info == SEND_SIG_FORCED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 goto out_set;
1072
Randy Dunlap5aba0852011-04-04 14:59:31 -07001073 /*
1074 * Real-time signals must be queued if sent by sigqueue, or
1075 * some other real-time mechanism. It is implementation
1076 * defined whether kill() does so. We attempt to do so, on
1077 * the principle of least surprise, but since kill is not
1078 * allowed to fail with EAGAIN when low on memory we just
1079 * make sure at least one signal gets delivered and don't
1080 * pass on the info struct.
1081 */
Vegard Nossum7a0aeb12009-05-16 11:28:33 +02001082 if (sig < SIGRTMIN)
1083 override_rlimit = (is_si_special(info) || info->si_code >= 0);
1084 else
1085 override_rlimit = 0;
1086
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001087 q = __sigqueue_alloc(sig, t, GFP_ATOMIC | __GFP_NOTRACK_FALSE_POSITIVE,
Vegard Nossum7a0aeb12009-05-16 11:28:33 +02001088 override_rlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 if (q) {
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001090 list_add_tail(&q->list, &pending->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 switch ((unsigned long) info) {
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001092 case (unsigned long) SEND_SIG_NOINFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 q->info.si_signo = sig;
1094 q->info.si_errno = 0;
1095 q->info.si_code = SI_USER;
Sukadev Bhattiprolu9cd4fd12009-01-06 14:42:46 -08001096 q->info.si_pid = task_tgid_nr_ns(current,
Sukadev Bhattiprolu09bca052009-01-06 14:42:45 -08001097 task_active_pid_ns(t));
Eric W. Biederman078de5f2012-02-08 07:00:08 -08001098 q->info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 break;
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001100 case (unsigned long) SEND_SIG_PRIV:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 q->info.si_signo = sig;
1102 q->info.si_errno = 0;
1103 q->info.si_code = SI_KERNEL;
1104 q->info.si_pid = 0;
1105 q->info.si_uid = 0;
1106 break;
1107 default:
1108 copy_siginfo(&q->info, info);
Sukadev Bhattiprolu6588c1e2009-04-02 16:58:09 -07001109 if (from_ancestor_ns)
1110 q->info.si_pid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 break;
1112 }
Serge E. Hallyn6b550f92012-01-10 15:11:37 -08001113
1114 userns_fixup_signal_uid(&q->info, t);
1115
Oleg Nesterov621d3122005-10-30 15:03:45 -08001116 } else if (!is_si_special(info)) {
Masami Hiramatsuba005e12009-11-24 16:56:58 -05001117 if (sig >= SIGRTMIN && info->si_code != SI_USER) {
1118 /*
1119 * Queue overflow, abort. We may abort if the
1120 * signal was rt and sent by user using something
1121 * other than kill().
1122 */
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001123 result = TRACE_SIGNAL_OVERFLOW_FAIL;
1124 ret = -EAGAIN;
1125 goto ret;
Masami Hiramatsuba005e12009-11-24 16:56:58 -05001126 } else {
1127 /*
1128 * This is a silent loss of information. We still
1129 * send the signal, but the *info bits are lost.
1130 */
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001131 result = TRACE_SIGNAL_LOSE_INFO;
Masami Hiramatsuba005e12009-11-24 16:56:58 -05001132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
1134
1135out_set:
Oleg Nesterov53c30332008-04-30 00:53:00 -07001136 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001137 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001138 complete_signal(sig, t, group);
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001139ret:
1140 trace_signal_generate(sig, info, t, group, result);
1141 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001144static int send_signal(int sig, struct siginfo *info, struct task_struct *t,
1145 int group)
1146{
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001147 int from_ancestor_ns = 0;
1148
1149#ifdef CONFIG_PID_NS
Oleg Nesterovdd342002009-12-15 16:47:24 -08001150 from_ancestor_ns = si_fromuser(info) &&
1151 !task_pid_nr_ns(current, task_active_pid_ns(t));
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001152#endif
1153
1154 return __send_signal(sig, info, t, group, from_ancestor_ns);
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001155}
1156
Al Viro4aaefee2012-11-05 13:09:56 -05001157static void print_fatal_signal(int signr)
Ingo Molnar45807a12007-07-15 23:40:10 -07001158{
Al Viro4aaefee2012-11-05 13:09:56 -05001159 struct pt_regs *regs = signal_pt_regs();
Wang Xiaoqiang747800e2016-05-23 16:23:59 -07001160 pr_info("potentially unexpected fatal signal %d.\n", signr);
Ingo Molnar45807a12007-07-15 23:40:10 -07001161
Al Viroca5cd872007-10-29 04:31:16 +00001162#if defined(__i386__) && !defined(__arch_um__)
Wang Xiaoqiang747800e2016-05-23 16:23:59 -07001163 pr_info("code at %08lx: ", regs->ip);
Ingo Molnar45807a12007-07-15 23:40:10 -07001164 {
1165 int i;
1166 for (i = 0; i < 16; i++) {
1167 unsigned char insn;
1168
Andi Kleenb45c6e72010-01-08 14:42:52 -08001169 if (get_user(insn, (unsigned char *)(regs->ip + i)))
1170 break;
Wang Xiaoqiang747800e2016-05-23 16:23:59 -07001171 pr_cont("%02x ", insn);
Ingo Molnar45807a12007-07-15 23:40:10 -07001172 }
1173 }
Wang Xiaoqiang747800e2016-05-23 16:23:59 -07001174 pr_cont("\n");
Ingo Molnar45807a12007-07-15 23:40:10 -07001175#endif
Ed Swierk3a9f84d2009-01-26 15:33:31 -08001176 preempt_disable();
Ingo Molnar45807a12007-07-15 23:40:10 -07001177 show_regs(regs);
Ed Swierk3a9f84d2009-01-26 15:33:31 -08001178 preempt_enable();
Ingo Molnar45807a12007-07-15 23:40:10 -07001179}
1180
1181static int __init setup_print_fatal_signals(char *str)
1182{
1183 get_option (&str, &print_fatal_signals);
1184
1185 return 1;
1186}
1187
1188__setup("print-fatal-signals=", setup_print_fatal_signals);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001190int
1191__group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1192{
1193 return send_signal(sig, info, p, 1);
1194}
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196static int
1197specific_send_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1198{
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001199 return send_signal(sig, info, t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200}
1201
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001202int do_send_sig_info(int sig, struct siginfo *info, struct task_struct *p,
1203 bool group)
1204{
1205 unsigned long flags;
1206 int ret = -ESRCH;
1207
1208 if (lock_task_sighand(p, &flags)) {
1209 ret = send_signal(sig, info, p, group);
1210 unlock_task_sighand(p, &flags);
1211 }
1212
1213 return ret;
1214}
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216/*
1217 * Force a signal that the process can't ignore: if necessary
1218 * we unblock the signal and change any SIG_IGN to SIG_DFL.
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001219 *
1220 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1221 * since we do not want to have a signal handler that was blocked
1222 * be invoked when user space had explicitly blocked it.
1223 *
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001224 * We don't want to have recursive SIGSEGV's etc, for example,
1225 * that is why we also clear SIGNAL_UNKILLABLE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227int
1228force_sig_info(int sig, struct siginfo *info, struct task_struct *t)
1229{
1230 unsigned long int flags;
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001231 int ret, blocked, ignored;
1232 struct k_sigaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 spin_lock_irqsave(&t->sighand->siglock, flags);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001235 action = &t->sighand->action[sig-1];
1236 ignored = action->sa.sa_handler == SIG_IGN;
1237 blocked = sigismember(&t->blocked, sig);
1238 if (blocked || ignored) {
1239 action->sa.sa_handler = SIG_DFL;
1240 if (blocked) {
1241 sigdelset(&t->blocked, sig);
Roland McGrath7bb44ad2007-05-23 13:57:44 -07001242 recalc_sigpending_and_wake(t);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001245 if (action->sa.sa_handler == SIG_DFL)
1246 t->signal->flags &= ~SIGNAL_UNKILLABLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 ret = specific_send_sig_info(sig, info, t);
1248 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1249
1250 return ret;
1251}
1252
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253/*
1254 * Nuke all other threads in the group.
1255 */
Oleg Nesterov09faef12010-05-26 14:43:11 -07001256int zap_other_threads(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257{
Oleg Nesterov09faef12010-05-26 14:43:11 -07001258 struct task_struct *t = p;
1259 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 p->signal->group_stop_count = 0;
1262
Oleg Nesterov09faef12010-05-26 14:43:11 -07001263 while_each_thread(p, t) {
Tejun Heo6dfca322011-06-02 11:14:00 +02001264 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
Oleg Nesterov09faef12010-05-26 14:43:11 -07001265 count++;
1266
1267 /* Don't bother with already dead threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (t->exit_state)
1269 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 sigaddset(&t->pending.signal, SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 signal_wake_up(t, 1);
1272 }
Oleg Nesterov09faef12010-05-26 14:43:11 -07001273
1274 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
Namhyung Kimb8ed3742010-10-27 15:34:06 -07001277struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
1278 unsigned long *flags)
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001279{
1280 struct sighand_struct *sighand;
1281
1282 for (;;) {
Paul E. McKenneyc41247e2014-05-05 08:18:30 -07001283 /*
1284 * Disable interrupts early to avoid deadlocks.
1285 * See rcu_read_unlock() comment header for details.
1286 */
Paul E. McKenneya8417962011-07-19 03:25:36 -07001287 local_irq_save(*flags);
1288 rcu_read_lock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001289 sighand = rcu_dereference(tsk->sighand);
Paul E. McKenneya8417962011-07-19 03:25:36 -07001290 if (unlikely(sighand == NULL)) {
1291 rcu_read_unlock();
1292 local_irq_restore(*flags);
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001293 break;
Paul E. McKenneya8417962011-07-19 03:25:36 -07001294 }
Oleg Nesterov392809b2014-09-28 23:44:18 +02001295 /*
1296 * This sighand can be already freed and even reused, but
1297 * we rely on SLAB_DESTROY_BY_RCU and sighand_ctor() which
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 */
Paul E. McKenneya8417962011-07-19 03:25:36 -07001306 spin_lock(&sighand->siglock);
1307 if (likely(sighand == tsk->sighand)) {
1308 rcu_read_unlock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001309 break;
Paul E. McKenneya8417962011-07-19 03:25:36 -07001310 }
1311 spin_unlock(&sighand->siglock);
1312 rcu_read_unlock();
1313 local_irq_restore(*flags);
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001314 }
1315
1316 return sighand;
1317}
1318
David Howellsc69e8d92008-11-14 10:39:19 +11001319/*
1320 * send signal info to all the members of a group
David Howellsc69e8d92008-11-14 10:39:19 +11001321 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322int group_send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
1323{
David Howells694f6902010-08-04 16:59:14 +01001324 int ret;
1325
1326 rcu_read_lock();
1327 ret = check_kill_permission(sig, info, p);
1328 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001330 if (!ret && sig)
1331 ret = do_send_sig_info(sig, info, p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332
1333 return ret;
1334}
1335
1336/*
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001337 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 * control characters do (^C, ^Z etc)
David Howellsc69e8d92008-11-14 10:39:19 +11001339 * - the caller must hold at least a readlock on tasklist_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 */
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001341int __kill_pgrp_info(int sig, struct siginfo *info, struct pid *pgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
1343 struct task_struct *p = NULL;
1344 int retval, success;
1345
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 success = 0;
1347 retval = -ESRCH;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001348 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 int err = group_send_sig_info(sig, info, p);
1350 success |= !err;
1351 retval = err;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001352 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return success ? 0 : retval;
1354}
1355
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001356int kill_pid_info(int sig, struct siginfo *info, struct pid *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357{
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001358 int error = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 struct task_struct *p;
1360
Paul E. McKenneyeca1a082014-10-23 11:41:22 -07001361 for (;;) {
1362 rcu_read_lock();
1363 p = pid_task(pid, PIDTYPE_PID);
1364 if (p)
1365 error = group_send_sig_info(sig, info, p);
1366 rcu_read_unlock();
1367 if (likely(!p || error != -ESRCH))
1368 return error;
Oleg Nesterov6ca25b52008-04-30 00:52:45 -07001369
Paul E. McKenneyeca1a082014-10-23 11:41:22 -07001370 /*
1371 * The task was unhashed in between, try again. If it
1372 * is dead, pid_task() will return NULL, if we race with
1373 * de_thread() it will find the new leader.
1374 */
1375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376}
1377
Randy Dunlap5aba0852011-04-04 14:59:31 -07001378int kill_proc_info(int sig, struct siginfo *info, pid_t pid)
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001379{
1380 int error;
1381 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001382 error = kill_pid_info(sig, info, find_vpid(pid));
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001383 rcu_read_unlock();
1384 return error;
1385}
1386
Serge Hallynd178bc32011-09-26 10:45:18 -05001387static int kill_as_cred_perm(const struct cred *cred,
1388 struct task_struct *target)
1389{
1390 const struct cred *pcred = __task_cred(target);
Eric W. Biederman5af66202012-03-03 20:21:47 -08001391 if (!uid_eq(cred->euid, pcred->suid) && !uid_eq(cred->euid, pcred->uid) &&
1392 !uid_eq(cred->uid, pcred->suid) && !uid_eq(cred->uid, pcred->uid))
Serge Hallynd178bc32011-09-26 10:45:18 -05001393 return 0;
1394 return 1;
1395}
1396
Eric W. Biederman2425c082006-10-02 02:17:28 -07001397/* like kill_pid_info(), but doesn't use uid/euid of "current" */
Serge Hallynd178bc32011-09-26 10:45:18 -05001398int kill_pid_info_as_cred(int sig, struct siginfo *info, struct pid *pid,
1399 const struct cred *cred, u32 secid)
Harald Welte46113832005-10-10 19:44:29 +02001400{
1401 int ret = -EINVAL;
1402 struct task_struct *p;
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001403 unsigned long flags;
Harald Welte46113832005-10-10 19:44:29 +02001404
1405 if (!valid_signal(sig))
1406 return ret;
1407
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001408 rcu_read_lock();
Eric W. Biederman2425c082006-10-02 02:17:28 -07001409 p = pid_task(pid, PIDTYPE_PID);
Harald Welte46113832005-10-10 19:44:29 +02001410 if (!p) {
1411 ret = -ESRCH;
1412 goto out_unlock;
1413 }
Serge Hallynd178bc32011-09-26 10:45:18 -05001414 if (si_fromuser(info) && !kill_as_cred_perm(cred, p)) {
Harald Welte46113832005-10-10 19:44:29 +02001415 ret = -EPERM;
1416 goto out_unlock;
1417 }
David Quigley8f95dc52006-06-30 01:55:47 -07001418 ret = security_task_kill(p, info, sig, secid);
1419 if (ret)
1420 goto out_unlock;
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001421
1422 if (sig) {
1423 if (lock_task_sighand(p, &flags)) {
1424 ret = __send_signal(sig, info, p, 1, 0);
1425 unlock_task_sighand(p, &flags);
1426 } else
1427 ret = -ESRCH;
Harald Welte46113832005-10-10 19:44:29 +02001428 }
1429out_unlock:
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001430 rcu_read_unlock();
Harald Welte46113832005-10-10 19:44:29 +02001431 return ret;
1432}
Serge Hallynd178bc32011-09-26 10:45:18 -05001433EXPORT_SYMBOL_GPL(kill_pid_info_as_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
1435/*
1436 * kill_something_info() interprets pid in interesting ways just like kill(2).
1437 *
1438 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1439 * is probably wrong. Should make it like BSD or SYSV.
1440 */
1441
Gustavo Fernando Padovanbc64efd2008-07-25 01:47:33 -07001442static int kill_something_info(int sig, struct siginfo *info, pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443{
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001444 int ret;
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001445
1446 if (pid > 0) {
1447 rcu_read_lock();
1448 ret = kill_pid_info(sig, info, find_vpid(pid));
1449 rcu_read_unlock();
1450 return ret;
1451 }
1452
zhongjiangec1975a2017-07-10 15:52:57 -07001453 /* -INT_MIN is undefined. Exclude this case to avoid a UBSAN warning */
1454 if (pid == INT_MIN)
1455 return -ESRCH;
1456
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001457 read_lock(&tasklist_lock);
1458 if (pid != -1) {
1459 ret = __kill_pgrp_info(sig, info,
1460 pid ? find_vpid(-pid) : task_pgrp(current));
1461 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 int retval = 0, count = 0;
1463 struct task_struct * p;
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 for_each_process(p) {
Sukadev Bhattiprolud25141a2008-10-29 14:01:11 -07001466 if (task_pid_vnr(p) > 1 &&
1467 !same_thread_group(p, current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 int err = group_send_sig_info(sig, info, p);
1469 ++count;
1470 if (err != -EPERM)
1471 retval = err;
1472 }
1473 }
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001474 ret = count ? retval : -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001476 read_unlock(&tasklist_lock);
1477
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001478 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479}
1480
1481/*
1482 * These are for backward compatibility with the rest of the kernel source.
1483 */
1484
Randy Dunlap5aba0852011-04-04 14:59:31 -07001485int send_sig_info(int sig, struct siginfo *info, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 /*
1488 * Make sure legacy kernel users don't send in bad values
1489 * (normal paths check this in check_kill_permission).
1490 */
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001491 if (!valid_signal(sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return -EINVAL;
1493
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001494 return do_send_sig_info(sig, info, p, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495}
1496
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001497#define __si_special(priv) \
1498 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1499
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500int
1501send_sig(int sig, struct task_struct *p, int priv)
1502{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001503 return send_sig_info(sig, __si_special(priv), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504}
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506void
1507force_sig(int sig, struct task_struct *p)
1508{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001509 force_sig_info(sig, SEND_SIG_PRIV, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510}
1511
1512/*
1513 * When things go south during signal handling, we
1514 * will force a SIGSEGV. And if the signal that caused
1515 * the problem was already a SIGSEGV, we'll want to
1516 * make sure we don't even try to deliver the signal..
1517 */
1518int
1519force_sigsegv(int sig, struct task_struct *p)
1520{
1521 if (sig == SIGSEGV) {
1522 unsigned long flags;
1523 spin_lock_irqsave(&p->sighand->siglock, flags);
1524 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1525 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1526 }
1527 force_sig(SIGSEGV, p);
1528 return 0;
1529}
1530
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001531int kill_pgrp(struct pid *pid, int sig, int priv)
1532{
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001533 int ret;
1534
1535 read_lock(&tasklist_lock);
1536 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1537 read_unlock(&tasklist_lock);
1538
1539 return ret;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001540}
1541EXPORT_SYMBOL(kill_pgrp);
1542
1543int kill_pid(struct pid *pid, int sig, int priv)
1544{
1545 return kill_pid_info(sig, __si_special(priv), pid);
1546}
1547EXPORT_SYMBOL(kill_pid);
1548
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549/*
1550 * These functions support sending signals using preallocated sigqueue
1551 * structures. This is needed "because realtime applications cannot
1552 * afford to lose notifications of asynchronous events, like timer
Randy Dunlap5aba0852011-04-04 14:59:31 -07001553 * expirations or I/O completions". In the case of POSIX Timers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 * we allocate the sigqueue structure from the timer_create. If this
1555 * allocation fails we are able to report the failure to the application
1556 * with an EAGAIN error.
1557 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558struct sigqueue *sigqueue_alloc(void)
1559{
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001560 struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001562 if (q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 q->flags |= SIGQUEUE_PREALLOC;
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001564
1565 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
1568void sigqueue_free(struct sigqueue *q)
1569{
1570 unsigned long flags;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001571 spinlock_t *lock = &current->sighand->siglock;
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1574 /*
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001575 * We must hold ->siglock while testing q->list
1576 * to serialize with collect_signal() or with
Oleg Nesterovda7978b2008-05-23 13:04:41 -07001577 * __exit_signal()->flush_sigqueue().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001579 spin_lock_irqsave(lock, flags);
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001580 q->flags &= ~SIGQUEUE_PREALLOC;
1581 /*
1582 * If it is queued it will be freed when dequeued,
1583 * like the "regular" sigqueue.
1584 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001585 if (!list_empty(&q->list))
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001586 q = NULL;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001587 spin_unlock_irqrestore(lock, flags);
1588
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001589 if (q)
1590 __sigqueue_free(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591}
1592
Oleg Nesterovac5c2152008-04-30 00:52:57 -07001593int send_sigqueue(struct sigqueue *q, struct task_struct *t, int group)
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001594{
Oleg Nesterove62e6652008-04-30 00:52:56 -07001595 int sig = q->info.si_signo;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001596 struct sigpending *pending;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001597 unsigned long flags;
Oleg Nesterov163566f2011-11-22 21:37:41 +01001598 int ret, result;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001599
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001600 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
Oleg Nesterove62e6652008-04-30 00:52:56 -07001601
1602 ret = -1;
1603 if (!likely(lock_task_sighand(t, &flags)))
1604 goto ret;
1605
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001606 ret = 1; /* the signal is ignored */
Oleg Nesterov163566f2011-11-22 21:37:41 +01001607 result = TRACE_SIGNAL_IGNORED;
Oleg Nesterovdef8cf72012-03-23 15:02:45 -07001608 if (!prepare_signal(sig, t, false))
Oleg Nesterove62e6652008-04-30 00:52:56 -07001609 goto out;
1610
1611 ret = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001612 if (unlikely(!list_empty(&q->list))) {
1613 /*
1614 * If an SI_TIMER entry is already queue just increment
1615 * the overrun count.
1616 */
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001617 BUG_ON(q->info.si_code != SI_TIMER);
1618 q->info.si_overrun++;
Oleg Nesterov163566f2011-11-22 21:37:41 +01001619 result = TRACE_SIGNAL_ALREADY_PENDING;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001620 goto out;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001621 }
Oleg Nesterovba661292008-07-23 20:52:05 +04001622 q->info.si_overrun = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001623
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001624 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001625 pending = group ? &t->signal->shared_pending : &t->pending;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001626 list_add_tail(&q->list, &pending->list);
1627 sigaddset(&pending->signal, sig);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001628 complete_signal(sig, t, group);
Oleg Nesterov163566f2011-11-22 21:37:41 +01001629 result = TRACE_SIGNAL_DELIVERED;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001630out:
Oleg Nesterov163566f2011-11-22 21:37:41 +01001631 trace_signal_generate(sig, &q->info, t, group, result);
Oleg Nesterove62e6652008-04-30 00:52:56 -07001632 unlock_task_sighand(t, &flags);
1633ret:
1634 return ret;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001635}
1636
Joel Fernandes (Google)af1070f2019-04-30 12:21:53 -04001637static void do_notify_pidfd(struct task_struct *task)
1638{
1639 struct pid *pid;
1640
1641 pid = task_pid(task);
1642 wake_up_all(&pid->wait_pidfd);
1643}
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 * Let a parent know about the death of a child.
1647 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001648 *
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001649 * Returns true if our parent ignored us and so we've switched to
1650 * self-reaping.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 */
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001652bool do_notify_parent(struct task_struct *tsk, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
1654 struct siginfo info;
1655 unsigned long flags;
1656 struct sighand_struct *psig;
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001657 bool autoreap = false;
Frederic Weisbecker6fac4822012-11-13 14:20:55 +01001658 cputime_t utime, stime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
Linus Torvaldsc5a7f952022-07-06 12:20:59 -07001660 WARN_ON_ONCE(sig == -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Linus Torvaldsc5a7f952022-07-06 12:20:59 -07001662 /* do_notify_parent_cldstop should have been called instead. */
1663 WARN_ON_ONCE(task_is_stopped_or_traced(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Linus Torvaldsc5a7f952022-07-06 12:20:59 -07001665 WARN_ON_ONCE(!tsk->ptrace &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1667
Joel Fernandes (Google)af1070f2019-04-30 12:21:53 -04001668 /* Wake up all pidfd waiters */
1669 do_notify_pidfd(tsk);
1670
Oleg Nesterovb6e238d2012-03-19 17:03:41 +01001671 if (sig != SIGCHLD) {
1672 /*
1673 * This is only possible if parent == real_parent.
1674 * Check if it has changed security domain.
1675 */
Eric W. Biederman110012a2020-03-30 19:01:04 -05001676 if (tsk->parent_exec_id != READ_ONCE(tsk->parent->self_exec_id))
Oleg Nesterovb6e238d2012-03-19 17:03:41 +01001677 sig = SIGCHLD;
1678 }
1679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 info.si_signo = sig;
1681 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001682 /*
Eric W. Biederman32084502012-05-31 16:26:39 -07001683 * We are under tasklist_lock here so our parent is tied to
1684 * us and cannot change.
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001685 *
Eric W. Biederman32084502012-05-31 16:26:39 -07001686 * task_active_pid_ns will always return the same pid namespace
1687 * until a task passes through release_task.
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001688 *
1689 * write_lock() currently calls preempt_disable() which is the
1690 * same as rcu_read_lock(), but according to Oleg, this is not
1691 * correct to rely on this
1692 */
1693 rcu_read_lock();
Eric W. Biederman32084502012-05-31 16:26:39 -07001694 info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(tsk->parent));
Eric W. Biederman54ba47e2012-03-13 16:04:35 -07001695 info.si_uid = from_kuid_munged(task_cred_xxx(tsk->parent, user_ns),
1696 task_uid(tsk));
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001697 rcu_read_unlock();
1698
Frederic Weisbecker6fac4822012-11-13 14:20:55 +01001699 task_cputime(tsk, &utime, &stime);
1700 info.si_utime = cputime_to_clock_t(utime + tsk->signal->utime);
1701 info.si_stime = cputime_to_clock_t(stime + tsk->signal->stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
1703 info.si_status = tsk->exit_code & 0x7f;
1704 if (tsk->exit_code & 0x80)
1705 info.si_code = CLD_DUMPED;
1706 else if (tsk->exit_code & 0x7f)
1707 info.si_code = CLD_KILLED;
1708 else {
1709 info.si_code = CLD_EXITED;
1710 info.si_status = tsk->exit_code >> 8;
1711 }
1712
1713 psig = tsk->parent->sighand;
1714 spin_lock_irqsave(&psig->siglock, flags);
Tejun Heod21142e2011-06-17 16:50:34 +02001715 if (!tsk->ptrace && sig == SIGCHLD &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1717 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1718 /*
1719 * We are exiting and our parent doesn't care. POSIX.1
1720 * defines special semantics for setting SIGCHLD to SIG_IGN
1721 * or setting the SA_NOCLDWAIT flag: we should be reaped
1722 * automatically and not left for our parent's wait4 call.
1723 * Rather than having the parent do it as a magic kind of
1724 * signal handler, we just set this to tell do_exit that we
1725 * can be cleaned up without becoming a zombie. Note that
1726 * we still call __wake_up_parent in this case, because a
1727 * blocked sys_wait4 might now return -ECHILD.
1728 *
1729 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1730 * is implementation-defined: we do (if you don't want
1731 * it, just use SIG_IGN instead).
1732 */
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001733 autoreap = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001735 sig = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 }
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001737 if (valid_signal(sig) && sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 __group_send_sig_info(sig, &info, tsk->parent);
1739 __wake_up_parent(tsk, tsk->parent);
1740 spin_unlock_irqrestore(&psig->siglock, flags);
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001741
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001742 return autoreap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743}
1744
Tejun Heo75b95952011-03-23 10:37:01 +01001745/**
1746 * do_notify_parent_cldstop - notify parent of stopped/continued state change
1747 * @tsk: task reporting the state change
1748 * @for_ptracer: the notification is for ptracer
1749 * @why: CLD_{CONTINUED|STOPPED|TRAPPED} to report
1750 *
1751 * Notify @tsk's parent that the stopped/continued state has changed. If
1752 * @for_ptracer is %false, @tsk's group leader notifies to its real parent.
1753 * If %true, @tsk reports to @tsk->parent which should be the ptracer.
1754 *
1755 * CONTEXT:
1756 * Must be called with tasklist_lock at least read locked.
1757 */
1758static void do_notify_parent_cldstop(struct task_struct *tsk,
1759 bool for_ptracer, int why)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
1761 struct siginfo info;
1762 unsigned long flags;
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001763 struct task_struct *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 struct sighand_struct *sighand;
Frederic Weisbecker6fac4822012-11-13 14:20:55 +01001765 cputime_t utime, stime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766
Tejun Heo75b95952011-03-23 10:37:01 +01001767 if (for_ptracer) {
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001768 parent = tsk->parent;
Tejun Heo75b95952011-03-23 10:37:01 +01001769 } else {
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001770 tsk = tsk->group_leader;
1771 parent = tsk->real_parent;
1772 }
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 info.si_signo = SIGCHLD;
1775 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001776 /*
Randy Dunlap5aba0852011-04-04 14:59:31 -07001777 * see comment in do_notify_parent() about the following 4 lines
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001778 */
1779 rcu_read_lock();
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08001780 info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(parent));
Eric W. Biederman54ba47e2012-03-13 16:04:35 -07001781 info.si_uid = from_kuid_munged(task_cred_xxx(parent, user_ns), task_uid(tsk));
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001782 rcu_read_unlock();
1783
Frederic Weisbecker6fac4822012-11-13 14:20:55 +01001784 task_cputime(tsk, &utime, &stime);
1785 info.si_utime = cputime_to_clock_t(utime);
1786 info.si_stime = cputime_to_clock_t(stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
1788 info.si_code = why;
1789 switch (why) {
1790 case CLD_CONTINUED:
1791 info.si_status = SIGCONT;
1792 break;
1793 case CLD_STOPPED:
1794 info.si_status = tsk->signal->group_exit_code & 0x7f;
1795 break;
1796 case CLD_TRAPPED:
1797 info.si_status = tsk->exit_code & 0x7f;
1798 break;
1799 default:
1800 BUG();
1801 }
1802
1803 sighand = parent->sighand;
1804 spin_lock_irqsave(&sighand->siglock, flags);
1805 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1806 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1807 __group_send_sig_info(SIGCHLD, &info, parent);
1808 /*
1809 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1810 */
1811 __wake_up_parent(tsk, parent);
1812 spin_unlock_irqrestore(&sighand->siglock, flags);
1813}
1814
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001815static inline int may_ptrace_stop(void)
1816{
Tejun Heod21142e2011-06-17 16:50:34 +02001817 if (!likely(current->ptrace))
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001818 return 0;
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001819 /*
1820 * Are we in the middle of do_coredump?
1821 * If so and our tracer is also part of the coredump stopping
1822 * is a deadlock situation, and pointless because our tracer
1823 * is dead so don't allow us to stop.
1824 * If SIGKILL was already sent before the caller unlocked
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001825 * ->siglock we must see ->core_state != NULL. Otherwise it
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001826 * is safe to enter schedule().
Oleg Nesterov9899d112013-01-21 20:48:00 +01001827 *
1828 * This is almost outdated, a task with the pending SIGKILL can't
1829 * block in TASK_TRACED. But PTRACE_EVENT_EXIT can be reported
1830 * after SIGKILL was already dequeued.
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001831 */
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001832 if (unlikely(current->mm->core_state) &&
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001833 unlikely(current->mm == current->parent->mm))
1834 return 0;
1835
1836 return 1;
1837}
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839/*
1840 * This must be called with current->sighand->siglock held.
1841 *
1842 * This should be the path for all ptrace stops.
1843 * We always set current->last_siginfo while stopped here.
1844 * That makes it a way to test a stopped process for
1845 * being ptrace-stopped vs being job-control-stopped.
1846 *
Oleg Nesterov20686a32008-02-08 04:19:03 -08001847 * If we actually decide not to stop at all because the tracer
1848 * is gone, we keep current->exit_code unless clear_code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 */
Tejun Heofe1bc6a2011-03-23 10:37:00 +01001850static void ptrace_stop(int exit_code, int why, int clear_code, siginfo_t *info)
Namhyung Kimb8401152010-10-27 15:34:07 -07001851 __releases(&current->sighand->siglock)
1852 __acquires(&current->sighand->siglock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
Tejun Heoceb6bd62011-03-23 10:37:01 +01001854 bool gstop_done = false;
1855
Roland McGrath1a669c22008-02-06 01:37:37 -08001856 if (arch_ptrace_stop_needed(exit_code, info)) {
1857 /*
1858 * The arch code has something special to do before a
1859 * ptrace stop. This is allowed to block, e.g. for faults
1860 * on user stack pages. We can't keep the siglock while
1861 * calling arch_ptrace_stop, so we must release it now.
1862 * To preserve proper semantics, we must do this before
1863 * any signal bookkeeping like checking group_stop_count.
Roland McGrath1a669c22008-02-06 01:37:37 -08001864 */
1865 spin_unlock_irq(&current->sighand->siglock);
1866 arch_ptrace_stop(exit_code, info);
1867 spin_lock_irq(&current->sighand->siglock);
Roland McGrath1a669c22008-02-06 01:37:37 -08001868 }
1869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 /*
Tejun Heo81be24b2011-06-02 11:13:59 +02001871 * We're committing to trapping. TRACED should be visible before
1872 * TRAPPING is cleared; otherwise, the tracer might fail do_wait().
1873 * Also, transition to TRACED and updates to ->jobctl should be
1874 * atomic with respect to siglock and should be done after the arch
1875 * hook as siglock is released and regrabbed across it.
Eric W. Biederman0045dd62021-09-01 13:21:34 -05001876 * schedule() will not sleep if there is a pending signal that
1877 * can awaken the task.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 */
Tejun Heo81be24b2011-06-02 11:13:59 +02001879 set_current_state(TASK_TRACED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
1881 current->last_siginfo = info;
1882 current->exit_code = exit_code;
1883
Tejun Heod79fdd62011-03-23 10:37:00 +01001884 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 * If @why is CLD_STOPPED, we're trapping to participate in a group
1886 * stop. Do the bookkeeping. Note that if SIGCONT was delievered
Tejun Heo73ddff22011-06-14 11:20:14 +02001887 * across siglock relocks since INTERRUPT was scheduled, PENDING
1888 * could be clear now. We act as if SIGCONT is received after
1889 * TASK_TRACED is entered - ignore it.
Tejun Heod79fdd62011-03-23 10:37:00 +01001890 */
Tejun Heoa8f072c2011-06-02 11:13:59 +02001891 if (why == CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 gstop_done = task_participate_group_stop(current);
Tejun Heod79fdd62011-03-23 10:37:00 +01001893
Tejun Heofb1d9102011-06-14 11:20:17 +02001894 /* any trap clears pending STOP trap, STOP trap clears NOTIFY */
Tejun Heo73ddff22011-06-14 11:20:14 +02001895 task_clear_jobctl_pending(current, JOBCTL_TRAP_STOP);
Tejun Heofb1d9102011-06-14 11:20:17 +02001896 if (info && info->si_code >> 8 == PTRACE_EVENT_STOP)
1897 task_clear_jobctl_pending(current, JOBCTL_TRAP_NOTIFY);
Tejun Heo73ddff22011-06-14 11:20:14 +02001898
Tejun Heo81be24b2011-06-02 11:13:59 +02001899 /* entering a trap, clear TRAPPING */
Tejun Heoa8f072c2011-06-02 11:13:59 +02001900 task_clear_jobctl_trapping(current);
Tejun Heod79fdd62011-03-23 10:37:00 +01001901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 spin_unlock_irq(&current->sighand->siglock);
1903 read_lock(&tasklist_lock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07001904 if (may_ptrace_stop()) {
Tejun Heoceb6bd62011-03-23 10:37:01 +01001905 /*
1906 * Notify parents of the stop.
1907 *
1908 * While ptraced, there are two parents - the ptracer and
1909 * the real_parent of the group_leader. The ptracer should
1910 * know about every stop while the real parent is only
1911 * interested in the completion of group stop. The states
1912 * for the two don't interact with each other. Notify
1913 * separately unless they're gonna be duplicates.
1914 */
1915 do_notify_parent_cldstop(current, true, why);
Oleg Nesterovbb3696d2011-06-24 17:34:23 +02001916 if (gstop_done && ptrace_reparented(current))
Tejun Heoceb6bd62011-03-23 10:37:01 +01001917 do_notify_parent_cldstop(current, false, why);
1918
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001919 /*
1920 * Don't want to allow preemption here, because
1921 * sys_ptrace() needs this task to be inactive.
1922 *
1923 * XXX: implement read_unlock_no_resched().
1924 */
1925 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 read_unlock(&tasklist_lock);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01001927 preempt_enable_no_resched();
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +02001928 freezable_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 } else {
1930 /*
1931 * By the time we got the lock, our tracer went away.
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001932 * Don't drop the lock yet, another tracer may come.
Tejun Heoceb6bd62011-03-23 10:37:01 +01001933 *
1934 * If @gstop_done, the ptracer went away between group stop
1935 * completion and here. During detach, it would have set
Tejun Heoa8f072c2011-06-02 11:13:59 +02001936 * JOBCTL_STOP_PENDING on us and we'll re-enter
1937 * TASK_STOPPED in do_signal_stop() on return, so notifying
1938 * the real parent of the group stop completion is enough.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 */
Tejun Heoceb6bd62011-03-23 10:37:01 +01001940 if (gstop_done)
1941 do_notify_parent_cldstop(current, false, why);
1942
Oleg Nesterov9899d112013-01-21 20:48:00 +01001943 /* tasklist protects us from ptrace_freeze_traced() */
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001944 __set_current_state(TASK_RUNNING);
Oleg Nesterov20686a32008-02-08 04:19:03 -08001945 if (clear_code)
1946 current->exit_code = 0;
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08001947 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 }
1949
1950 /*
1951 * We are back. Now reacquire the siglock before touching
1952 * last_siginfo, so that we are sure to have synchronized with
1953 * any signal-sending on another CPU that wants to examine it.
1954 */
1955 spin_lock_irq(&current->sighand->siglock);
1956 current->last_siginfo = NULL;
1957
Tejun Heo544b2c92011-06-14 11:20:18 +02001958 /* LISTENING can be set only during STOP traps, clear it */
1959 current->jobctl &= ~JOBCTL_LISTENING;
1960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 /*
1962 * Queued signals ignored us while we were stopped for tracing.
1963 * So check for any that we should take before resuming user mode.
Roland McGrathb74d0de2007-06-06 03:59:00 -07001964 * This sets TIF_SIGPENDING, but never clears it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 */
Roland McGrathb74d0de2007-06-06 03:59:00 -07001966 recalc_sigpending_tsk(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967}
1968
Tejun Heo3544d722011-06-14 11:20:15 +02001969static void ptrace_do_notify(int signr, int exit_code, int why)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970{
1971 siginfo_t info;
1972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 memset(&info, 0, sizeof info);
Tejun Heo3544d722011-06-14 11:20:15 +02001974 info.si_signo = signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 info.si_code = exit_code;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001976 info.si_pid = task_pid_vnr(current);
Eric W. Biederman078de5f2012-02-08 07:00:08 -08001977 info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 /* Let the debugger run. */
Tejun Heo3544d722011-06-14 11:20:15 +02001980 ptrace_stop(exit_code, why, 1, &info);
1981}
1982
1983void ptrace_notify(int exit_code)
1984{
1985 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
Oleg Nesterovf784e8a2012-08-26 21:12:17 +02001986 if (unlikely(current->task_works))
1987 task_work_run();
Tejun Heo3544d722011-06-14 11:20:15 +02001988
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 spin_lock_irq(&current->sighand->siglock);
Tejun Heo3544d722011-06-14 11:20:15 +02001990 ptrace_do_notify(SIGTRAP, exit_code, CLD_TRAPPED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 spin_unlock_irq(&current->sighand->siglock);
1992}
1993
Tejun Heo73ddff22011-06-14 11:20:14 +02001994/**
1995 * do_signal_stop - handle group stop for SIGSTOP and other stop signals
1996 * @signr: signr causing group stop if initiating
1997 *
1998 * If %JOBCTL_STOP_PENDING is not set yet, initiate group stop with @signr
1999 * and participate in it. If already set, participate in the existing
2000 * group stop. If participated in a group stop (and thus slept), %true is
2001 * returned with siglock released.
2002 *
2003 * If ptraced, this function doesn't handle stop itself. Instead,
2004 * %JOBCTL_TRAP_STOP is scheduled and %false is returned with siglock
2005 * untouched. The caller must ensure that INTERRUPT trap handling takes
2006 * places afterwards.
2007 *
2008 * CONTEXT:
2009 * Must be called with @current->sighand->siglock held, which is released
2010 * on %true return.
2011 *
2012 * RETURNS:
2013 * %false if group stop is already cancelled or ptrace trap is scheduled.
2014 * %true if participated in group stop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 */
Tejun Heo73ddff22011-06-14 11:20:14 +02002016static bool do_signal_stop(int signr)
2017 __releases(&current->sighand->siglock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018{
2019 struct signal_struct *sig = current->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Tejun Heoa8f072c2011-06-02 11:13:59 +02002021 if (!(current->jobctl & JOBCTL_STOP_PENDING)) {
Palmer Dabbeltb76808e2015-04-30 21:19:57 -07002022 unsigned long gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08002023 struct task_struct *t;
2024
Tejun Heoa8f072c2011-06-02 11:13:59 +02002025 /* signr will be recorded in task->jobctl for retries */
2026 WARN_ON_ONCE(signr & ~JOBCTL_STOP_SIGMASK);
Tejun Heod79fdd62011-03-23 10:37:00 +01002027
Tejun Heoa8f072c2011-06-02 11:13:59 +02002028 if (!likely(current->jobctl & JOBCTL_STOP_DEQUEUED) ||
Oleg Nesterov573cf9a2008-04-30 00:52:36 -07002029 unlikely(signal_group_exit(sig)))
Tejun Heo73ddff22011-06-14 11:20:14 +02002030 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 /*
Tejun Heo408a37d2011-03-23 10:37:01 +01002032 * There is no group stop already in progress. We must
2033 * initiate one now.
2034 *
2035 * While ptraced, a task may be resumed while group stop is
2036 * still in effect and then receive a stop signal and
2037 * initiate another group stop. This deviates from the
2038 * usual behavior as two consecutive stop signals can't
Oleg Nesterov780006eac2011-04-01 20:12:16 +02002039 * cause two group stops when !ptraced. That is why we
2040 * also check !task_is_stopped(t) below.
Tejun Heo408a37d2011-03-23 10:37:01 +01002041 *
2042 * The condition can be distinguished by testing whether
2043 * SIGNAL_STOP_STOPPED is already set. Don't generate
2044 * group_exit_code in such case.
2045 *
2046 * This is not necessary for SIGNAL_STOP_CONTINUED because
2047 * an intervening stop signal is required to cause two
2048 * continued events regardless of ptrace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 */
Tejun Heo408a37d2011-03-23 10:37:01 +01002050 if (!(sig->flags & SIGNAL_STOP_STOPPED))
2051 sig->group_exit_code = signr;
Oleg Nesterova122b342006-03-28 16:11:22 -08002052
Tejun Heo7dd3db52011-06-02 11:14:00 +02002053 sig->group_stop_count = 0;
2054
2055 if (task_set_jobctl_pending(current, signr | gstop))
2056 sig->group_stop_count++;
2057
Oleg Nesterov8d38f202014-01-23 15:55:56 -08002058 t = current;
2059 while_each_thread(current, t) {
Oleg Nesterova122b342006-03-28 16:11:22 -08002060 /*
2061 * Setting state to TASK_STOPPED for a group
2062 * stop is always done with the siglock held,
2063 * so this check has no races.
2064 */
Tejun Heo7dd3db52011-06-02 11:14:00 +02002065 if (!task_is_stopped(t) &&
2066 task_set_jobctl_pending(t, signr | gstop)) {
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002067 sig->group_stop_count++;
Tejun Heofb1d9102011-06-14 11:20:17 +02002068 if (likely(!(t->ptrace & PT_SEIZED)))
2069 signal_wake_up(t, 0);
2070 else
2071 ptrace_trap_notify(t);
Oleg Nesterova122b342006-03-28 16:11:22 -08002072 }
Tejun Heod79fdd62011-03-23 10:37:00 +01002073 }
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002074 }
Tejun Heo73ddff22011-06-14 11:20:14 +02002075
Tejun Heod21142e2011-06-17 16:50:34 +02002076 if (likely(!current->ptrace)) {
Tejun Heo5224fa32011-03-23 10:37:00 +01002077 int notify = 0;
2078
2079 /*
2080 * If there are no other threads in the group, or if there
2081 * is a group stop in progress and we are the last to stop,
2082 * report to the parent.
2083 */
2084 if (task_participate_group_stop(current))
2085 notify = CLD_STOPPED;
2086
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002087 __set_current_state(TASK_STOPPED);
Tejun Heo5224fa32011-03-23 10:37:00 +01002088 spin_unlock_irq(&current->sighand->siglock);
2089
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002090 /*
2091 * Notify the parent of the group stop completion. Because
2092 * we're not holding either the siglock or tasklist_lock
2093 * here, ptracer may attach inbetween; however, this is for
2094 * group stop and should always be delivered to the real
2095 * parent of the group leader. The new ptracer will get
2096 * its notification when this task transitions into
2097 * TASK_TRACED.
2098 */
Tejun Heo5224fa32011-03-23 10:37:00 +01002099 if (notify) {
2100 read_lock(&tasklist_lock);
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002101 do_notify_parent_cldstop(current, false, notify);
Tejun Heo5224fa32011-03-23 10:37:00 +01002102 read_unlock(&tasklist_lock);
2103 }
2104
2105 /* Now we don't run again until woken by SIGCONT or SIGKILL */
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +02002106 freezable_schedule();
Tejun Heo73ddff22011-06-14 11:20:14 +02002107 return true;
Tejun Heod79fdd62011-03-23 10:37:00 +01002108 } else {
Tejun Heo73ddff22011-06-14 11:20:14 +02002109 /*
2110 * While ptraced, group stop is handled by STOP trap.
2111 * Schedule it and let the caller deal with it.
2112 */
2113 task_set_jobctl_pending(current, JOBCTL_TRAP_STOP);
2114 return false;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002115 }
Tejun Heo73ddff22011-06-14 11:20:14 +02002116}
Tejun Heod79fdd62011-03-23 10:37:00 +01002117
Tejun Heo73ddff22011-06-14 11:20:14 +02002118/**
2119 * do_jobctl_trap - take care of ptrace jobctl traps
2120 *
Tejun Heo3544d722011-06-14 11:20:15 +02002121 * When PT_SEIZED, it's used for both group stop and explicit
2122 * SEIZE/INTERRUPT traps. Both generate PTRACE_EVENT_STOP trap with
2123 * accompanying siginfo. If stopped, lower eight bits of exit_code contain
2124 * the stop signal; otherwise, %SIGTRAP.
2125 *
2126 * When !PT_SEIZED, it's used only for group stop trap with stop signal
2127 * number as exit_code and no siginfo.
Tejun Heo73ddff22011-06-14 11:20:14 +02002128 *
2129 * CONTEXT:
2130 * Must be called with @current->sighand->siglock held, which may be
2131 * released and re-acquired before returning with intervening sleep.
2132 */
2133static void do_jobctl_trap(void)
2134{
Tejun Heo3544d722011-06-14 11:20:15 +02002135 struct signal_struct *signal = current->signal;
Tejun Heo73ddff22011-06-14 11:20:14 +02002136 int signr = current->jobctl & JOBCTL_STOP_SIGMASK;
Tejun Heod79fdd62011-03-23 10:37:00 +01002137
Tejun Heo3544d722011-06-14 11:20:15 +02002138 if (current->ptrace & PT_SEIZED) {
2139 if (!signal->group_stop_count &&
2140 !(signal->flags & SIGNAL_STOP_STOPPED))
2141 signr = SIGTRAP;
2142 WARN_ON_ONCE(!signr);
2143 ptrace_do_notify(signr, signr | (PTRACE_EVENT_STOP << 8),
2144 CLD_STOPPED);
2145 } else {
2146 WARN_ON_ONCE(!signr);
2147 ptrace_stop(signr, CLD_STOPPED, 0, NULL);
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002148 current->exit_code = 0;
2149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
Al Viro94eb22d2012-11-05 13:08:06 -05002152static int ptrace_signal(int signr, siginfo_t *info)
Roland McGrath18c98b62008-04-17 18:44:38 -07002153{
Al Virob7f95912012-11-05 13:06:22 -05002154 ptrace_signal_deliver();
Oleg Nesterov8a352412011-07-21 17:06:53 +02002155 /*
2156 * We do not check sig_kernel_stop(signr) but set this marker
2157 * unconditionally because we do not know whether debugger will
2158 * change signr. This flag has no meaning unless we are going
2159 * to stop after return from ptrace_stop(). In this case it will
2160 * be checked in do_signal_stop(), we should only stop if it was
2161 * not cleared by SIGCONT while we were sleeping. See also the
2162 * comment in dequeue_signal().
2163 */
2164 current->jobctl |= JOBCTL_STOP_DEQUEUED;
Tejun Heofe1bc6a2011-03-23 10:37:00 +01002165 ptrace_stop(signr, CLD_TRAPPED, 0, info);
Roland McGrath18c98b62008-04-17 18:44:38 -07002166
2167 /* We're back. Did the debugger cancel the sig? */
2168 signr = current->exit_code;
2169 if (signr == 0)
2170 return signr;
2171
2172 current->exit_code = 0;
2173
Randy Dunlap5aba0852011-04-04 14:59:31 -07002174 /*
2175 * Update the siginfo structure if the signal has
2176 * changed. If the debugger wanted something
2177 * specific in the siginfo structure then it should
2178 * have updated *info via PTRACE_SETSIGINFO.
2179 */
Roland McGrath18c98b62008-04-17 18:44:38 -07002180 if (signr != info->si_signo) {
2181 info->si_signo = signr;
2182 info->si_errno = 0;
2183 info->si_code = SI_USER;
Serge E. Hallyn6b550f92012-01-10 15:11:37 -08002184 rcu_read_lock();
Roland McGrath18c98b62008-04-17 18:44:38 -07002185 info->si_pid = task_pid_vnr(current->parent);
Eric W. Biederman54ba47e2012-03-13 16:04:35 -07002186 info->si_uid = from_kuid_munged(current_user_ns(),
2187 task_uid(current->parent));
Serge E. Hallyn6b550f92012-01-10 15:11:37 -08002188 rcu_read_unlock();
Roland McGrath18c98b62008-04-17 18:44:38 -07002189 }
2190
2191 /* If the (new) signal is now blocked, requeue it. */
2192 if (sigismember(&current->blocked, signr)) {
2193 specific_send_sig_info(signr, info, current);
2194 signr = 0;
2195 }
2196
2197 return signr;
2198}
2199
Richard Weinberger828b1f62013-10-07 15:26:57 +02002200int get_signal(struct ksignal *ksig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002202 struct sighand_struct *sighand = current->sighand;
2203 struct signal_struct *signal = current->signal;
2204 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Oleg Nesterovf784e8a2012-08-26 21:12:17 +02002206 if (unlikely(current->task_works))
2207 task_work_run();
Al Viro72667022012-07-15 14:10:52 +04002208
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302209 if (unlikely(uprobe_deny_signal()))
2210 return 0;
2211
Roland McGrath13b1c3d2008-03-03 20:22:05 -08002212 /*
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +02002213 * Do this once, we can't return to user-mode if freezing() == T.
2214 * do_signal_stop() and ptrace_stop() do freezable_schedule() and
2215 * thus do not need another check after return.
Roland McGrath13b1c3d2008-03-03 20:22:05 -08002216 */
Rafael J. Wysockifc558a72006-03-23 03:00:05 -08002217 try_to_freeze();
2218
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +02002219relock:
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002220 spin_lock_irq(&sighand->siglock);
Oleg Nesterov021e1ae2008-04-30 00:53:00 -07002221 /*
2222 * Every stopped thread goes here after wakeup. Check to see if
2223 * we should notify the parent, prepare_signal(SIGCONT) encodes
2224 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
2225 */
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002226 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
Tejun Heoc672af32011-03-23 10:36:59 +01002227 int why;
2228
2229 if (signal->flags & SIGNAL_CLD_CONTINUED)
2230 why = CLD_CONTINUED;
2231 else
2232 why = CLD_STOPPED;
2233
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002234 signal->flags &= ~SIGNAL_CLD_MASK;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002235
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002236 spin_unlock_irq(&sighand->siglock);
Oleg Nesterove4420552008-04-30 00:52:44 -07002237
Tejun Heoceb6bd62011-03-23 10:37:01 +01002238 /*
2239 * Notify the parent that we're continuing. This event is
2240 * always per-process and doesn't make whole lot of sense
2241 * for ptracers, who shouldn't consume the state via
2242 * wait(2) either, but, for backward compatibility, notify
2243 * the ptracer of the group leader too unless it's gonna be
2244 * a duplicate.
2245 */
Tejun Heoedf2ed12011-03-23 10:37:00 +01002246 read_lock(&tasklist_lock);
Tejun Heoceb6bd62011-03-23 10:37:01 +01002247 do_notify_parent_cldstop(current, false, why);
2248
Oleg Nesterovbb3696d2011-06-24 17:34:23 +02002249 if (ptrace_reparented(current->group_leader))
2250 do_notify_parent_cldstop(current->group_leader,
2251 true, why);
Tejun Heoedf2ed12011-03-23 10:37:00 +01002252 read_unlock(&tasklist_lock);
Tejun Heoceb6bd62011-03-23 10:37:01 +01002253
Oleg Nesterove4420552008-04-30 00:52:44 -07002254 goto relock;
2255 }
2256
Eric W. Biederman39beaea2019-02-06 18:39:40 -06002257 /* Has this task already been marked for death? */
Eric W. Biedermanaa74f262019-02-11 23:27:42 -06002258 if (signal_group_exit(signal)) {
2259 ksig->info.si_signo = signr = SIGKILL;
2260 sigdelset(&current->pending.signal, SIGKILL);
Zhenliang Wei9adcdd52019-05-31 22:30:52 -07002261 trace_signal_deliver(SIGKILL, SEND_SIG_NOINFO,
2262 &sighand->action[SIGKILL - 1]);
Eric W. Biedermanaa74f262019-02-11 23:27:42 -06002263 recalc_sigpending();
Eric W. Biederman39beaea2019-02-06 18:39:40 -06002264 goto fatal;
Eric W. Biedermanaa74f262019-02-11 23:27:42 -06002265 }
Eric W. Biederman39beaea2019-02-06 18:39:40 -06002266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 for (;;) {
2268 struct k_sigaction *ka;
Tejun Heodd1d6772011-06-02 11:14:00 +02002269
2270 if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
2271 do_signal_stop(0))
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002272 goto relock;
Oleg Nesterov1be53962009-12-15 16:47:26 -08002273
Tejun Heo73ddff22011-06-14 11:20:14 +02002274 if (unlikely(current->jobctl & JOBCTL_TRAP_MASK)) {
2275 do_jobctl_trap();
2276 spin_unlock_irq(&sighand->siglock);
2277 goto relock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 }
2279
Eric W. Biederman181f1f0d2019-02-06 17:51:47 -06002280 /*
2281 * Signals generated by the execution of an instruction
2282 * need to be delivered before any other pending signals
2283 * so that the instruction pointer in the signal stack
2284 * frame points to the faulting instruction.
2285 */
2286 signr = dequeue_synchronous_signal(&ksig->info);
2287 if (!signr)
2288 signr = dequeue_signal(current, &current->blocked, &ksig->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Tejun Heodd1d6772011-06-02 11:14:00 +02002290 if (!signr)
2291 break; /* will return 0 */
2292
Oleg Nesterov8a352412011-07-21 17:06:53 +02002293 if (unlikely(current->ptrace) && signr != SIGKILL) {
Richard Weinberger828b1f62013-10-07 15:26:57 +02002294 signr = ptrace_signal(signr, &ksig->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 if (!signr)
Tejun Heodd1d6772011-06-02 11:14:00 +02002296 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 }
2298
Tejun Heodd1d6772011-06-02 11:14:00 +02002299 ka = &sighand->action[signr-1];
2300
Masami Hiramatsuf9d42572009-11-24 16:56:51 -05002301 /* Trace actually delivered signals. */
Richard Weinberger828b1f62013-10-07 15:26:57 +02002302 trace_signal_deliver(signr, &ksig->info, ka);
Masami Hiramatsuf9d42572009-11-24 16:56:51 -05002303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
2305 continue;
2306 if (ka->sa.sa_handler != SIG_DFL) {
2307 /* Run the handler. */
Richard Weinberger828b1f62013-10-07 15:26:57 +02002308 ksig->ka = *ka;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
2310 if (ka->sa.sa_flags & SA_ONESHOT)
2311 ka->sa.sa_handler = SIG_DFL;
2312
2313 break; /* will return non-zero "signr" value */
2314 }
2315
2316 /*
2317 * Now we are doing the default action for this signal.
2318 */
2319 if (sig_kernel_ignore(signr)) /* Default is nothing. */
2320 continue;
2321
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08002322 /*
Sukadev Bhattiprolu0fbc26a2007-10-18 23:40:13 -07002323 * Global init gets no signals it doesn't want.
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07002324 * Container-init gets no signals it doesn't want from same
2325 * container.
2326 *
2327 * Note that if global/container-init sees a sig_kernel_only()
2328 * signal here, the signal must have been generated internally
2329 * or must have come from an ancestor namespace. In either
2330 * case, the signal cannot be dropped.
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08002331 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -07002332 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07002333 !sig_kernel_only(signr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 continue;
2335
2336 if (sig_kernel_stop(signr)) {
2337 /*
2338 * The default action is to stop all threads in
2339 * the thread group. The job control signals
2340 * do nothing in an orphaned pgrp, but SIGSTOP
2341 * always works. Note that siglock needs to be
2342 * dropped during the call to is_orphaned_pgrp()
2343 * because of lock ordering with tasklist_lock.
2344 * This allows an intervening SIGCONT to be posted.
2345 * We need to check for that and bail out if necessary.
2346 */
2347 if (signr != SIGSTOP) {
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002348 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350 /* signals can be posted during this window */
2351
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08002352 if (is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 goto relock;
2354
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002355 spin_lock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 }
2357
Richard Weinberger828b1f62013-10-07 15:26:57 +02002358 if (likely(do_signal_stop(ksig->info.si_signo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 /* It released the siglock. */
2360 goto relock;
2361 }
2362
2363 /*
2364 * We didn't actually stop, due to a race
2365 * with SIGCONT or something like that.
2366 */
2367 continue;
2368 }
2369
Eric W. Biederman39beaea2019-02-06 18:39:40 -06002370 fatal:
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002371 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
2373 /*
2374 * Anything else is fatal, maybe with a core dump.
2375 */
2376 current->flags |= PF_SIGNALED;
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07002377
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 if (sig_kernel_coredump(signr)) {
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07002379 if (print_fatal_signals)
Richard Weinberger828b1f62013-10-07 15:26:57 +02002380 print_fatal_signal(ksig->info.si_signo);
Jesper Derehag2b5faa42013-03-19 20:50:05 +00002381 proc_coredump_connector(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 /*
2383 * If it was able to dump core, this kills all
2384 * other threads in the group and synchronizes with
2385 * their demise. If we lost the race with another
2386 * thread getting here, it set group_exit_code
2387 * first and our do_group_exit call below will use
2388 * that value and ignore the one we pass it.
2389 */
Richard Weinberger828b1f62013-10-07 15:26:57 +02002390 do_coredump(&ksig->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 }
2392
2393 /*
2394 * Death signals, no core dump.
2395 */
Richard Weinberger828b1f62013-10-07 15:26:57 +02002396 do_group_exit(ksig->info.si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 /* NOTREACHED */
2398 }
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002399 spin_unlock_irq(&sighand->siglock);
Richard Weinberger828b1f62013-10-07 15:26:57 +02002400
2401 ksig->sig = signr;
2402 return ksig->sig > 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403}
2404
Matt Fleming5e6292c2012-01-10 15:11:17 -08002405/**
Al Viroefee9842012-04-28 02:04:15 -04002406 * signal_delivered -
Richard Weinberger10b1c7a2014-07-13 13:36:04 +02002407 * @ksig: kernel signal struct
Al Viroefee9842012-04-28 02:04:15 -04002408 * @stepping: nonzero if debugger single-step or block-step in use
Matt Fleming5e6292c2012-01-10 15:11:17 -08002409 *
Masanari Iidae2278672014-02-18 22:54:36 +09002410 * This function should be called when a signal has successfully been
Richard Weinberger10b1c7a2014-07-13 13:36:04 +02002411 * delivered. It updates the blocked signals accordingly (@ksig->ka.sa.sa_mask
Al Viroefee9842012-04-28 02:04:15 -04002412 * is always blocked, and the signal itself is blocked unless %SA_NODEFER
Richard Weinberger10b1c7a2014-07-13 13:36:04 +02002413 * is set in @ksig->ka.sa.sa_flags. Tracing is notified.
Matt Fleming5e6292c2012-01-10 15:11:17 -08002414 */
Richard Weinberger10b1c7a2014-07-13 13:36:04 +02002415static void signal_delivered(struct ksignal *ksig, int stepping)
Matt Fleming5e6292c2012-01-10 15:11:17 -08002416{
2417 sigset_t blocked;
2418
Al Viroa610d6e2012-05-21 23:42:15 -04002419 /* A signal was successfully delivered, and the
2420 saved sigmask was stored on the signal frame,
2421 and will be restored by sigreturn. So we can
2422 simply clear the restore sigmask flag. */
2423 clear_restore_sigmask();
2424
Richard Weinberger10b1c7a2014-07-13 13:36:04 +02002425 sigorsets(&blocked, &current->blocked, &ksig->ka.sa.sa_mask);
2426 if (!(ksig->ka.sa.sa_flags & SA_NODEFER))
2427 sigaddset(&blocked, ksig->sig);
Matt Fleming5e6292c2012-01-10 15:11:17 -08002428 set_current_blocked(&blocked);
Richard Weinbergerdf5601f2013-10-07 15:37:19 +02002429 tracehook_signal_handler(stepping);
Matt Fleming5e6292c2012-01-10 15:11:17 -08002430}
2431
Al Viro2ce5da12012-11-07 15:11:25 -05002432void signal_setup_done(int failed, struct ksignal *ksig, int stepping)
2433{
2434 if (failed)
2435 force_sigsegv(ksig->sig, current);
2436 else
Richard Weinberger10b1c7a2014-07-13 13:36:04 +02002437 signal_delivered(ksig, stepping);
Al Viro2ce5da12012-11-07 15:11:25 -05002438}
2439
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002440/*
2441 * It could be that complete_signal() picked us to notify about the
Oleg Nesterovfec99932011-04-27 19:50:21 +02002442 * group-wide signal. Other threads should be notified now to take
2443 * the shared signals in @which since we will not.
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002444 */
Oleg Nesterovf646e222011-04-27 19:18:39 +02002445static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002446{
Oleg Nesterovf646e222011-04-27 19:18:39 +02002447 sigset_t retarget;
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002448 struct task_struct *t;
2449
Oleg Nesterovf646e222011-04-27 19:18:39 +02002450 sigandsets(&retarget, &tsk->signal->shared_pending.signal, which);
2451 if (sigisemptyset(&retarget))
2452 return;
2453
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002454 t = tsk;
2455 while_each_thread(tsk, t) {
Oleg Nesterovfec99932011-04-27 19:50:21 +02002456 if (t->flags & PF_EXITING)
2457 continue;
2458
2459 if (!has_pending_signals(&retarget, &t->blocked))
2460 continue;
2461 /* Remove the signals this thread can handle. */
2462 sigandsets(&retarget, &retarget, &t->blocked);
2463
2464 if (!signal_pending(t))
2465 signal_wake_up(t, 0);
2466
2467 if (sigisemptyset(&retarget))
2468 break;
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002469 }
2470}
2471
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002472void exit_signals(struct task_struct *tsk)
2473{
2474 int group_stop = 0;
Oleg Nesterovf646e222011-04-27 19:18:39 +02002475 sigset_t unblocked;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002476
Tejun Heo77e4ef92011-12-12 18:12:21 -08002477 /*
2478 * @tsk is about to have PF_EXITING set - lock out users which
2479 * expect stable threadgroup.
2480 */
2481 threadgroup_change_begin(tsk);
2482
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002483 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
2484 tsk->flags |= PF_EXITING;
Tejun Heo77e4ef92011-12-12 18:12:21 -08002485 threadgroup_change_end(tsk);
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002486 return;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002487 }
2488
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002489 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002490 /*
2491 * From now this task is not visible for group-wide signals,
2492 * see wants_signal(), do_signal_stop().
2493 */
2494 tsk->flags |= PF_EXITING;
Tejun Heo77e4ef92011-12-12 18:12:21 -08002495
2496 threadgroup_change_end(tsk);
2497
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002498 if (!signal_pending(tsk))
2499 goto out;
2500
Oleg Nesterovf646e222011-04-27 19:18:39 +02002501 unblocked = tsk->blocked;
2502 signotset(&unblocked);
2503 retarget_shared_pending(tsk, &unblocked);
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002504
Tejun Heoa8f072c2011-06-02 11:13:59 +02002505 if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
Tejun Heoe5c19022011-03-23 10:37:00 +01002506 task_participate_group_stop(tsk))
Tejun Heoedf2ed12011-03-23 10:37:00 +01002507 group_stop = CLD_STOPPED;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002508out:
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002509 spin_unlock_irq(&tsk->sighand->siglock);
2510
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002511 /*
2512 * If group stop has completed, deliver the notification. This
2513 * should always go to the real parent of the group leader.
2514 */
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002515 if (unlikely(group_stop)) {
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002516 read_lock(&tasklist_lock);
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002517 do_notify_parent_cldstop(tsk, false, group_stop);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002518 read_unlock(&tasklist_lock);
2519 }
2520}
2521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522EXPORT_SYMBOL(recalc_sigpending);
2523EXPORT_SYMBOL_GPL(dequeue_signal);
2524EXPORT_SYMBOL(flush_signals);
2525EXPORT_SYMBOL(force_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526EXPORT_SYMBOL(send_sig);
2527EXPORT_SYMBOL(send_sig_info);
2528EXPORT_SYMBOL(sigprocmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529
2530/*
2531 * System call entry points.
2532 */
2533
Randy Dunlap41c57892011-04-04 15:00:26 -07002534/**
2535 * sys_restart_syscall - restart a system call
2536 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +01002537SYSCALL_DEFINE0(restart_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538{
Andy Lutomirskif56141e2015-02-12 15:01:14 -08002539 struct restart_block *restart = &current->restart_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 return restart->fn(restart);
2541}
2542
2543long do_no_restart_syscall(struct restart_block *param)
2544{
2545 return -EINTR;
2546}
2547
Oleg Nesterovb1828012011-04-27 21:56:14 +02002548static void __set_task_blocked(struct task_struct *tsk, const sigset_t *newset)
2549{
2550 if (signal_pending(tsk) && !thread_group_empty(tsk)) {
2551 sigset_t newblocked;
2552 /* A set of now blocked but previously unblocked signals. */
Oleg Nesterov702a5072011-04-27 22:01:27 +02002553 sigandnsets(&newblocked, newset, &current->blocked);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002554 retarget_shared_pending(tsk, &newblocked);
2555 }
2556 tsk->blocked = *newset;
2557 recalc_sigpending();
2558}
2559
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002560/**
2561 * set_current_blocked - change current->blocked mask
2562 * @newset: new mask
2563 *
2564 * It is wrong to change ->blocked directly, this helper should be used
2565 * to ensure the process can't miss a shared signal we are going to block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 */
Al Viro77097ae2012-04-27 13:58:59 -04002567void set_current_blocked(sigset_t *newset)
2568{
Al Viro77097ae2012-04-27 13:58:59 -04002569 sigdelsetmask(newset, sigmask(SIGKILL) | sigmask(SIGSTOP));
Oleg Nesterov0c4a8422013-01-05 19:13:29 +01002570 __set_current_blocked(newset);
Al Viro77097ae2012-04-27 13:58:59 -04002571}
2572
2573void __set_current_blocked(const sigset_t *newset)
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002574{
2575 struct task_struct *tsk = current;
2576
Waiman Long20a30612016-12-14 15:04:10 -08002577 /*
2578 * In case the signal mask hasn't changed, there is nothing we need
2579 * to do. The current->blocked shouldn't be modified by other task.
2580 */
2581 if (sigequalsets(&tsk->blocked, newset))
2582 return;
2583
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002584 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002585 __set_task_blocked(tsk, newset);
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002586 spin_unlock_irq(&tsk->sighand->siglock);
2587}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
2589/*
2590 * This is also useful for kernel threads that want to temporarily
2591 * (or permanently) block certain signals.
2592 *
2593 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2594 * interface happily blocks "unblockable" signals like SIGKILL
2595 * and friends.
2596 */
2597int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2598{
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002599 struct task_struct *tsk = current;
2600 sigset_t newset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002602 /* Lockless, only current can change ->blocked, never from irq */
Oleg Nesterova26fd332006-03-23 03:00:49 -08002603 if (oldset)
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002604 *oldset = tsk->blocked;
Oleg Nesterova26fd332006-03-23 03:00:49 -08002605
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 switch (how) {
2607 case SIG_BLOCK:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002608 sigorsets(&newset, &tsk->blocked, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 break;
2610 case SIG_UNBLOCK:
Oleg Nesterov702a5072011-04-27 22:01:27 +02002611 sigandnsets(&newset, &tsk->blocked, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 break;
2613 case SIG_SETMASK:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002614 newset = *set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 break;
2616 default:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002617 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 }
Oleg Nesterova26fd332006-03-23 03:00:49 -08002619
Al Viro77097ae2012-04-27 13:58:59 -04002620 __set_current_blocked(&newset);
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002621 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622}
2623
Randy Dunlap41c57892011-04-04 15:00:26 -07002624/**
2625 * sys_rt_sigprocmask - change the list of currently blocked signals
2626 * @how: whether to add, remove, or set signals
Randy Dunlapada9c932011-06-14 15:50:11 -07002627 * @nset: stores pending signals
Randy Dunlap41c57892011-04-04 15:00:26 -07002628 * @oset: previous value of signal mask if non-null
2629 * @sigsetsize: size of sigset_t type
2630 */
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002631SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset,
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002632 sigset_t __user *, oset, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 sigset_t old_set, new_set;
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002635 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
2637 /* XXX: Don't preclude handling different sized sigset_t's. */
2638 if (sigsetsize != sizeof(sigset_t))
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002639 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002641 old_set = current->blocked;
2642
2643 if (nset) {
2644 if (copy_from_user(&new_set, nset, sizeof(sigset_t)))
2645 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2647
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002648 error = sigprocmask(how, &new_set, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 if (error)
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002650 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 }
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002652
2653 if (oset) {
2654 if (copy_to_user(oset, &old_set, sizeof(sigset_t)))
2655 return -EFAULT;
2656 }
2657
2658 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659}
2660
Al Viro322a56c2012-12-25 13:32:58 -05002661#ifdef CONFIG_COMPAT
Al Viro322a56c2012-12-25 13:32:58 -05002662COMPAT_SYSCALL_DEFINE4(rt_sigprocmask, int, how, compat_sigset_t __user *, nset,
2663 compat_sigset_t __user *, oset, compat_size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664{
Al Viro322a56c2012-12-25 13:32:58 -05002665#ifdef __BIG_ENDIAN
2666 sigset_t old_set = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667
Al Viro322a56c2012-12-25 13:32:58 -05002668 /* XXX: Don't preclude handling different sized sigset_t's. */
2669 if (sigsetsize != sizeof(sigset_t))
2670 return -EINVAL;
2671
2672 if (nset) {
2673 compat_sigset_t new32;
2674 sigset_t new_set;
2675 int error;
2676 if (copy_from_user(&new32, nset, sizeof(compat_sigset_t)))
2677 return -EFAULT;
2678
2679 sigset_from_compat(&new_set, &new32);
2680 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2681
2682 error = sigprocmask(how, &new_set, NULL);
2683 if (error)
2684 return error;
2685 }
2686 if (oset) {
2687 compat_sigset_t old32;
2688 sigset_to_compat(&old32, &old_set);
Al Virodb61ec22013-03-02 20:39:15 -05002689 if (copy_to_user(oset, &old32, sizeof(compat_sigset_t)))
Al Viro322a56c2012-12-25 13:32:58 -05002690 return -EFAULT;
2691 }
2692 return 0;
2693#else
2694 return sys_rt_sigprocmask(how, (sigset_t __user *)nset,
2695 (sigset_t __user *)oset, sigsetsize);
2696#endif
2697}
2698#endif
Al Viro322a56c2012-12-25 13:32:58 -05002699
Al Virofe9c1db2012-12-25 14:31:38 -05002700static int do_sigpending(void *set, unsigned long sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 if (sigsetsize > sizeof(sigset_t))
Al Virofe9c1db2012-12-25 14:31:38 -05002703 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
2705 spin_lock_irq(&current->sighand->siglock);
Al Virofe9c1db2012-12-25 14:31:38 -05002706 sigorsets(set, &current->pending.signal,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 &current->signal->shared_pending.signal);
2708 spin_unlock_irq(&current->sighand->siglock);
2709
2710 /* Outside the lock because only this thread touches it. */
Al Virofe9c1db2012-12-25 14:31:38 -05002711 sigandsets(set, &current->blocked, set);
2712 return 0;
Randy Dunlap5aba0852011-04-04 14:59:31 -07002713}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Randy Dunlap41c57892011-04-04 15:00:26 -07002715/**
2716 * sys_rt_sigpending - examine a pending signal that has been raised
2717 * while blocked
Randy Dunlap20f22ab2013-03-04 14:32:59 -08002718 * @uset: stores pending signals
Randy Dunlap41c57892011-04-04 15:00:26 -07002719 * @sigsetsize: size of sigset_t type or larger
2720 */
Al Virofe9c1db2012-12-25 14:31:38 -05002721SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, uset, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722{
Al Virofe9c1db2012-12-25 14:31:38 -05002723 sigset_t set;
2724 int err = do_sigpending(&set, sigsetsize);
2725 if (!err && copy_to_user(uset, &set, sigsetsize))
2726 err = -EFAULT;
2727 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728}
2729
Al Virofe9c1db2012-12-25 14:31:38 -05002730#ifdef CONFIG_COMPAT
Al Virofe9c1db2012-12-25 14:31:38 -05002731COMPAT_SYSCALL_DEFINE2(rt_sigpending, compat_sigset_t __user *, uset,
2732 compat_size_t, sigsetsize)
2733{
2734#ifdef __BIG_ENDIAN
2735 sigset_t set;
2736 int err = do_sigpending(&set, sigsetsize);
2737 if (!err) {
2738 compat_sigset_t set32;
2739 sigset_to_compat(&set32, &set);
2740 /* we can get here only if sigsetsize <= sizeof(set) */
2741 if (copy_to_user(uset, &set32, sigsetsize))
2742 err = -EFAULT;
2743 }
2744 return err;
2745#else
2746 return sys_rt_sigpending((sigset_t __user *)uset, sigsetsize);
2747#endif
2748}
2749#endif
Al Virofe9c1db2012-12-25 14:31:38 -05002750
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751#ifndef HAVE_ARCH_COPY_SIGINFO_TO_USER
2752
Al Viroce395962013-10-13 17:23:53 -04002753int copy_siginfo_to_user(siginfo_t __user *to, const siginfo_t *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754{
2755 int err;
2756
2757 if (!access_ok (VERIFY_WRITE, to, sizeof(siginfo_t)))
2758 return -EFAULT;
2759 if (from->si_code < 0)
2760 return __copy_to_user(to, from, sizeof(siginfo_t))
2761 ? -EFAULT : 0;
2762 /*
2763 * If you change siginfo_t structure, please be sure
2764 * this code is fixed accordingly.
Davide Libenzifba2afa2007-05-10 22:23:13 -07002765 * Please remember to update the signalfd_copyinfo() function
2766 * inside fs/signalfd.c too, in case siginfo_t changes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 * It should never copy any pad contained in the structure
2768 * to avoid security leaks, but must copy the generic
2769 * 3 ints plus the relevant union member.
2770 */
2771 err = __put_user(from->si_signo, &to->si_signo);
2772 err |= __put_user(from->si_errno, &to->si_errno);
2773 err |= __put_user((short)from->si_code, &to->si_code);
2774 switch (from->si_code & __SI_MASK) {
2775 case __SI_KILL:
2776 err |= __put_user(from->si_pid, &to->si_pid);
2777 err |= __put_user(from->si_uid, &to->si_uid);
2778 break;
2779 case __SI_TIMER:
2780 err |= __put_user(from->si_tid, &to->si_tid);
2781 err |= __put_user(from->si_overrun, &to->si_overrun);
2782 err |= __put_user(from->si_ptr, &to->si_ptr);
2783 break;
2784 case __SI_POLL:
2785 err |= __put_user(from->si_band, &to->si_band);
2786 err |= __put_user(from->si_fd, &to->si_fd);
2787 break;
2788 case __SI_FAULT:
2789 err |= __put_user(from->si_addr, &to->si_addr);
2790#ifdef __ARCH_SI_TRAPNO
2791 err |= __put_user(from->si_trapno, &to->si_trapno);
2792#endif
Andi Kleena337fda2010-09-27 20:32:19 +02002793#ifdef BUS_MCEERR_AO
Randy Dunlap5aba0852011-04-04 14:59:31 -07002794 /*
Andi Kleena337fda2010-09-27 20:32:19 +02002795 * Other callers might not initialize the si_lsb field,
Randy Dunlap5aba0852011-04-04 14:59:31 -07002796 * so check explicitly for the right codes here.
Andi Kleena337fda2010-09-27 20:32:19 +02002797 */
Amanieu d'Antras26135022015-08-06 15:46:29 -07002798 if (from->si_signo == SIGBUS &&
2799 (from->si_code == BUS_MCEERR_AR || from->si_code == BUS_MCEERR_AO))
Andi Kleena337fda2010-09-27 20:32:19 +02002800 err |= __put_user(from->si_addr_lsb, &to->si_addr_lsb);
2801#endif
Qiaowei Renee1b58d2014-11-14 07:18:19 -08002802#ifdef SEGV_BNDERR
Amanieu d'Antras26135022015-08-06 15:46:29 -07002803 if (from->si_signo == SIGSEGV && from->si_code == SEGV_BNDERR) {
2804 err |= __put_user(from->si_lower, &to->si_lower);
2805 err |= __put_user(from->si_upper, &to->si_upper);
2806 }
Qiaowei Renee1b58d2014-11-14 07:18:19 -08002807#endif
Dave Hansencd0ea352016-02-12 13:02:12 -08002808#ifdef SEGV_PKUERR
2809 if (from->si_signo == SIGSEGV && from->si_code == SEGV_PKUERR)
2810 err |= __put_user(from->si_pkey, &to->si_pkey);
2811#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 break;
2813 case __SI_CHLD:
2814 err |= __put_user(from->si_pid, &to->si_pid);
2815 err |= __put_user(from->si_uid, &to->si_uid);
2816 err |= __put_user(from->si_status, &to->si_status);
2817 err |= __put_user(from->si_utime, &to->si_utime);
2818 err |= __put_user(from->si_stime, &to->si_stime);
2819 break;
2820 case __SI_RT: /* This is not generated by the kernel as of now. */
2821 case __SI_MESGQ: /* But this is */
2822 err |= __put_user(from->si_pid, &to->si_pid);
2823 err |= __put_user(from->si_uid, &to->si_uid);
2824 err |= __put_user(from->si_ptr, &to->si_ptr);
2825 break;
Will Drewrya0727e82012-04-12 16:48:00 -05002826#ifdef __ARCH_SIGSYS
2827 case __SI_SYS:
2828 err |= __put_user(from->si_call_addr, &to->si_call_addr);
2829 err |= __put_user(from->si_syscall, &to->si_syscall);
2830 err |= __put_user(from->si_arch, &to->si_arch);
2831 break;
2832#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 default: /* this is just in case for now ... */
2834 err |= __put_user(from->si_pid, &to->si_pid);
2835 err |= __put_user(from->si_uid, &to->si_uid);
2836 break;
2837 }
2838 return err;
2839}
2840
2841#endif
2842
Randy Dunlap41c57892011-04-04 15:00:26 -07002843/**
Oleg Nesterov943df142011-04-27 21:44:14 +02002844 * do_sigtimedwait - wait for queued signals specified in @which
2845 * @which: queued signals to wait for
2846 * @info: if non-null, the signal's siginfo is returned here
2847 * @ts: upper bound on process time suspension
2848 */
2849int do_sigtimedwait(const sigset_t *which, siginfo_t *info,
Thomas Gleixner2b1ecc32016-07-04 09:50:25 +00002850 const struct timespec *ts)
Oleg Nesterov943df142011-04-27 21:44:14 +02002851{
Thomas Gleixner2b1ecc32016-07-04 09:50:25 +00002852 ktime_t *to = NULL, timeout = { .tv64 = KTIME_MAX };
Oleg Nesterov943df142011-04-27 21:44:14 +02002853 struct task_struct *tsk = current;
Oleg Nesterov943df142011-04-27 21:44:14 +02002854 sigset_t mask = *which;
Thomas Gleixner2b1ecc32016-07-04 09:50:25 +00002855 int sig, ret = 0;
Oleg Nesterov943df142011-04-27 21:44:14 +02002856
2857 if (ts) {
2858 if (!timespec_valid(ts))
2859 return -EINVAL;
Thomas Gleixner2b1ecc32016-07-04 09:50:25 +00002860 timeout = timespec_to_ktime(*ts);
2861 to = &timeout;
Oleg Nesterov943df142011-04-27 21:44:14 +02002862 }
2863
2864 /*
2865 * Invert the set of allowed signals to get those we want to block.
2866 */
2867 sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
2868 signotset(&mask);
2869
2870 spin_lock_irq(&tsk->sighand->siglock);
2871 sig = dequeue_signal(tsk, &mask, info);
Thomas Gleixner2b1ecc32016-07-04 09:50:25 +00002872 if (!sig && timeout.tv64) {
Oleg Nesterov943df142011-04-27 21:44:14 +02002873 /*
2874 * None ready, temporarily unblock those we're interested
2875 * while we are sleeping in so that we'll be awakened when
Oleg Nesterovb1828012011-04-27 21:56:14 +02002876 * they arrive. Unblocking is always fine, we can avoid
2877 * set_current_blocked().
Oleg Nesterov943df142011-04-27 21:44:14 +02002878 */
2879 tsk->real_blocked = tsk->blocked;
2880 sigandsets(&tsk->blocked, &tsk->blocked, &mask);
2881 recalc_sigpending();
2882 spin_unlock_irq(&tsk->sighand->siglock);
2883
Thomas Gleixner2b1ecc32016-07-04 09:50:25 +00002884 __set_current_state(TASK_INTERRUPTIBLE);
2885 ret = freezable_schedule_hrtimeout_range(to, tsk->timer_slack_ns,
2886 HRTIMER_MODE_REL);
Oleg Nesterov943df142011-04-27 21:44:14 +02002887 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002888 __set_task_blocked(tsk, &tsk->real_blocked);
Oleg Nesterov61140412014-06-06 14:36:46 -07002889 sigemptyset(&tsk->real_blocked);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002890 sig = dequeue_signal(tsk, &mask, info);
Oleg Nesterov943df142011-04-27 21:44:14 +02002891 }
2892 spin_unlock_irq(&tsk->sighand->siglock);
2893
2894 if (sig)
2895 return sig;
Thomas Gleixner2b1ecc32016-07-04 09:50:25 +00002896 return ret ? -EINTR : -EAGAIN;
Oleg Nesterov943df142011-04-27 21:44:14 +02002897}
2898
2899/**
Randy Dunlap41c57892011-04-04 15:00:26 -07002900 * sys_rt_sigtimedwait - synchronously wait for queued signals specified
2901 * in @uthese
2902 * @uthese: queued signals to wait for
2903 * @uinfo: if non-null, the signal's siginfo is returned here
2904 * @uts: upper bound on process time suspension
2905 * @sigsetsize: size of sigset_t type
2906 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002907SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
2908 siginfo_t __user *, uinfo, const struct timespec __user *, uts,
2909 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 sigset_t these;
2912 struct timespec ts;
2913 siginfo_t info;
Oleg Nesterov943df142011-04-27 21:44:14 +02002914 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
2916 /* XXX: Don't preclude handling different sized sigset_t's. */
2917 if (sigsetsize != sizeof(sigset_t))
2918 return -EINVAL;
2919
2920 if (copy_from_user(&these, uthese, sizeof(these)))
2921 return -EFAULT;
Randy Dunlap5aba0852011-04-04 14:59:31 -07002922
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 if (uts) {
2924 if (copy_from_user(&ts, uts, sizeof(ts)))
2925 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 }
2927
Oleg Nesterov943df142011-04-27 21:44:14 +02002928 ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Oleg Nesterov943df142011-04-27 21:44:14 +02002930 if (ret > 0 && uinfo) {
2931 if (copy_siginfo_to_user(uinfo, &info))
2932 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 }
2934
2935 return ret;
2936}
2937
Christian Braunercf9f8292018-11-19 00:51:56 +01002938static inline void prepare_kill_siginfo(int sig, struct siginfo *info)
2939{
2940 info->si_signo = sig;
2941 info->si_errno = 0;
2942 info->si_code = SI_USER;
2943 info->si_pid = task_tgid_vnr(current);
2944 info->si_uid = from_kuid_munged(current_user_ns(), current_uid());
2945}
2946
Randy Dunlap41c57892011-04-04 15:00:26 -07002947/**
2948 * sys_kill - send a signal to a process
2949 * @pid: the PID of the process
2950 * @sig: signal to be sent
2951 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002952SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953{
2954 struct siginfo info;
2955
Christian Braunercf9f8292018-11-19 00:51:56 +01002956 prepare_kill_siginfo(sig, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
2958 return kill_something_info(sig, &info, pid);
2959}
2960
Christian Braunercf9f8292018-11-19 00:51:56 +01002961/*
2962 * Verify that the signaler and signalee either are in the same pid namespace
2963 * or that the signaler's pid namespace is an ancestor of the signalee's pid
2964 * namespace.
2965 */
2966static bool access_pidfd_pidns(struct pid *pid)
2967{
2968 struct pid_namespace *active = task_active_pid_ns(current);
2969 struct pid_namespace *p = ns_of_pid(pid);
2970
2971 for (;;) {
2972 if (!p)
2973 return false;
2974 if (p == active)
2975 break;
2976 p = p->parent;
2977 }
2978
2979 return true;
2980}
2981
Christian Braunerb3ae5982019-04-17 22:50:25 +02002982static struct pid *pidfd_to_pid(const struct file *file)
2983{
2984 if (file->f_op == &pidfd_fops)
2985 return file->private_data;
2986
2987 return tgid_pidfd_to_pid(file);
2988}
2989
Christian Braunercf9f8292018-11-19 00:51:56 +01002990static int copy_siginfo_from_user_any(siginfo_t *kinfo, siginfo_t __user *info)
2991{
2992#ifdef CONFIG_COMPAT
2993 /*
2994 * Avoid hooking up compat syscalls and instead handle necessary
2995 * conversions here. Note, this is a stop-gap measure and should not be
2996 * considered a generic solution.
2997 */
2998 if (in_compat_syscall())
2999 return copy_siginfo_from_user32(
3000 kinfo, (struct compat_siginfo __user *)info);
3001#endif
3002 return copy_from_user(kinfo, info, sizeof(siginfo_t));
3003}
3004
3005/**
Christian Braunerac937bb2019-06-04 15:18:43 +02003006 * sys_pidfd_send_signal - Signal a process through a pidfd
3007 * @pidfd: file descriptor of the process
3008 * @sig: signal to send
3009 * @info: signal info
3010 * @flags: future flags
Christian Braunercf9f8292018-11-19 00:51:56 +01003011 *
3012 * The syscall currently only signals via PIDTYPE_PID which covers
3013 * kill(<positive-pid>, <signal>. It does not signal threads or process
3014 * groups.
3015 * In order to extend the syscall to threads and process groups the @flags
3016 * argument should be used. In essence, the @flags argument will determine
3017 * what is signaled and not the file descriptor itself. Put in other words,
3018 * grouping is a property of the flags argument not a property of the file
3019 * descriptor.
3020 *
3021 * Return: 0 on success, negative errno on failure
3022 */
3023SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig,
3024 siginfo_t __user *, info, unsigned int, flags)
3025{
3026 int ret;
3027 struct fd f;
3028 struct pid *pid;
3029 siginfo_t kinfo;
3030
3031 /* Enforce flags be set to 0 until we add an extension. */
3032 if (flags)
3033 return -EINVAL;
3034
Christian Brauner68defbc2019-04-18 12:18:39 +02003035 f = fdget(pidfd);
Christian Braunercf9f8292018-11-19 00:51:56 +01003036 if (!f.file)
3037 return -EBADF;
3038
3039 /* Is this a pidfd? */
Christian Braunerb3ae5982019-04-17 22:50:25 +02003040 pid = pidfd_to_pid(f.file);
Christian Braunercf9f8292018-11-19 00:51:56 +01003041 if (IS_ERR(pid)) {
3042 ret = PTR_ERR(pid);
3043 goto err;
3044 }
3045
3046 ret = -EINVAL;
3047 if (!access_pidfd_pidns(pid))
3048 goto err;
3049
3050 if (info) {
3051 ret = copy_siginfo_from_user_any(&kinfo, info);
3052 if (unlikely(ret))
3053 goto err;
3054
3055 ret = -EINVAL;
3056 if (unlikely(sig != kinfo.si_signo))
3057 goto err;
3058
Jann Hornf511d492019-03-30 03:12:32 +01003059 /* Only allow sending arbitrary signals to yourself. */
3060 ret = -EPERM;
Christian Braunercf9f8292018-11-19 00:51:56 +01003061 if ((task_pid(current) != pid) &&
Jann Hornf511d492019-03-30 03:12:32 +01003062 (kinfo.si_code >= 0 || kinfo.si_code == SI_TKILL))
3063 goto err;
Christian Braunercf9f8292018-11-19 00:51:56 +01003064 } else {
3065 prepare_kill_siginfo(sig, &kinfo);
3066 }
3067
3068 ret = kill_pid_info(sig, &kinfo, pid);
3069
3070err:
3071 fdput(f);
3072 return ret;
3073}
Christian Braunercf9f8292018-11-19 00:51:56 +01003074
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00003075static int
3076do_send_specific(pid_t tgid, pid_t pid, int sig, struct siginfo *info)
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08003077{
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08003078 struct task_struct *p;
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00003079 int error = -ESRCH;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08003080
Oleg Nesterov3547ff32008-04-30 00:52:51 -07003081 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07003082 p = find_task_by_vpid(pid);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003083 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00003084 error = check_kill_permission(sig, info, p);
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08003085 /*
3086 * The null signal is a permissions and process existence
3087 * probe. No signal is actually delivered.
3088 */
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07003089 if (!error && sig) {
3090 error = do_send_sig_info(sig, info, p, false);
3091 /*
3092 * If lock_task_sighand() failed we pretend the task
3093 * dies after receiving the signal. The window is tiny,
3094 * and the signal is private anyway.
3095 */
3096 if (unlikely(error == -ESRCH))
3097 error = 0;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08003098 }
3099 }
Oleg Nesterov3547ff32008-04-30 00:52:51 -07003100 rcu_read_unlock();
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08003101
3102 return error;
3103}
3104
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00003105static int do_tkill(pid_t tgid, pid_t pid, int sig)
3106{
Emese Revfyb9e146d2013-04-17 15:58:36 -07003107 struct siginfo info = {};
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00003108
3109 info.si_signo = sig;
3110 info.si_errno = 0;
3111 info.si_code = SI_TKILL;
3112 info.si_pid = task_tgid_vnr(current);
Eric W. Biederman078de5f2012-02-08 07:00:08 -08003113 info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00003114
3115 return do_send_specific(tgid, pid, sig, &info);
3116}
3117
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118/**
3119 * sys_tgkill - send signal to one specific thread
3120 * @tgid: the thread group ID of the thread
3121 * @pid: the PID of the thread
3122 * @sig: signal to be sent
3123 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08003124 * This syscall also checks the @tgid and returns -ESRCH even if the PID
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 * exists but it's not belonging to the target process anymore. This
3126 * method solves the problem of threads exiting and PIDs getting reused.
3127 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003128SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 /* This is only valid for single tasks */
3131 if (pid <= 0 || tgid <= 0)
3132 return -EINVAL;
3133
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08003134 return do_tkill(tgid, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135}
3136
Randy Dunlap41c57892011-04-04 15:00:26 -07003137/**
3138 * sys_tkill - send signal to one specific task
3139 * @pid: the PID of the task
3140 * @sig: signal to be sent
3141 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 * Send a signal to only one task, even if it's a CLONE_THREAD task.
3143 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003144SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 /* This is only valid for single tasks */
3147 if (pid <= 0)
3148 return -EINVAL;
3149
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08003150 return do_tkill(0, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151}
3152
Al Viro75907d42012-12-25 15:19:12 -05003153static int do_rt_sigqueueinfo(pid_t pid, int sig, siginfo_t *info)
3154{
3155 /* Not even root can pretend to send signals from the kernel.
3156 * Nor can they impersonate a kill()/tgkill(), which adds source info.
3157 */
Andrey Vagin66dd34a2013-02-27 17:03:12 -08003158 if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
Vladimir Davydov69828dc2015-04-16 12:47:35 -07003159 (task_pid_vnr(current) != pid))
Al Viro75907d42012-12-25 15:19:12 -05003160 return -EPERM;
Vladimir Davydov69828dc2015-04-16 12:47:35 -07003161
Al Viro75907d42012-12-25 15:19:12 -05003162 info->si_signo = sig;
3163
3164 /* POSIX.1b doesn't mention process groups. */
3165 return kill_proc_info(sig, info, pid);
3166}
3167
Randy Dunlap41c57892011-04-04 15:00:26 -07003168/**
3169 * sys_rt_sigqueueinfo - send signal information to a signal
3170 * @pid: the PID of the thread
3171 * @sig: signal to be sent
3172 * @uinfo: signal info to be sent
3173 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003174SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
3175 siginfo_t __user *, uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176{
3177 siginfo_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
3179 return -EFAULT;
Al Viro75907d42012-12-25 15:19:12 -05003180 return do_rt_sigqueueinfo(pid, sig, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181}
3182
Al Viro75907d42012-12-25 15:19:12 -05003183#ifdef CONFIG_COMPAT
Al Viro75907d42012-12-25 15:19:12 -05003184COMPAT_SYSCALL_DEFINE3(rt_sigqueueinfo,
3185 compat_pid_t, pid,
3186 int, sig,
3187 struct compat_siginfo __user *, uinfo)
3188{
Amanieu d'Antras3c00cb52015-08-06 15:46:26 -07003189 siginfo_t info = {};
Al Viro75907d42012-12-25 15:19:12 -05003190 int ret = copy_siginfo_from_user32(&info, uinfo);
3191 if (unlikely(ret))
3192 return ret;
3193 return do_rt_sigqueueinfo(pid, sig, &info);
3194}
3195#endif
Al Viro75907d42012-12-25 15:19:12 -05003196
Al Viro9aae8fc2012-12-24 23:12:04 -05003197static int do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, siginfo_t *info)
Thomas Gleixner62ab4502009-04-04 21:01:06 +00003198{
3199 /* This is only valid for single tasks */
3200 if (pid <= 0 || tgid <= 0)
3201 return -EINVAL;
3202
3203 /* Not even root can pretend to send signals from the kernel.
Julien Tinnesda485242011-03-18 15:05:21 -07003204 * Nor can they impersonate a kill()/tgkill(), which adds source info.
3205 */
Vladimir Davydov69828dc2015-04-16 12:47:35 -07003206 if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
3207 (task_pid_vnr(current) != pid))
Thomas Gleixner62ab4502009-04-04 21:01:06 +00003208 return -EPERM;
Vladimir Davydov69828dc2015-04-16 12:47:35 -07003209
Thomas Gleixner62ab4502009-04-04 21:01:06 +00003210 info->si_signo = sig;
3211
3212 return do_send_specific(tgid, pid, sig, info);
3213}
3214
3215SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
3216 siginfo_t __user *, uinfo)
3217{
3218 siginfo_t info;
3219
3220 if (copy_from_user(&info, uinfo, sizeof(siginfo_t)))
3221 return -EFAULT;
3222
3223 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
3224}
3225
Al Viro9aae8fc2012-12-24 23:12:04 -05003226#ifdef CONFIG_COMPAT
3227COMPAT_SYSCALL_DEFINE4(rt_tgsigqueueinfo,
3228 compat_pid_t, tgid,
3229 compat_pid_t, pid,
3230 int, sig,
3231 struct compat_siginfo __user *, uinfo)
3232{
Amanieu d'Antras3c00cb52015-08-06 15:46:26 -07003233 siginfo_t info = {};
Al Viro9aae8fc2012-12-24 23:12:04 -05003234
3235 if (copy_siginfo_from_user32(&info, uinfo))
3236 return -EFAULT;
3237 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
3238}
3239#endif
3240
Oleg Nesterov03417292014-06-06 14:36:53 -07003241/*
Oleg Nesterovb4e74262014-06-06 14:37:00 -07003242 * For kthreads only, must not be used if cloned with CLONE_SIGHAND
Oleg Nesterov03417292014-06-06 14:36:53 -07003243 */
Oleg Nesterovb4e74262014-06-06 14:37:00 -07003244void kernel_sigaction(int sig, __sighandler_t action)
Oleg Nesterov03417292014-06-06 14:36:53 -07003245{
Oleg Nesterovec5955b2014-06-06 14:36:57 -07003246 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterovb4e74262014-06-06 14:37:00 -07003247 current->sighand->action[sig - 1].sa.sa_handler = action;
3248 if (action == SIG_IGN) {
3249 sigset_t mask;
3250
3251 sigemptyset(&mask);
3252 sigaddset(&mask, sig);
3253
3254 flush_sigqueue_mask(&mask, &current->signal->shared_pending);
3255 flush_sigqueue_mask(&mask, &current->pending);
3256 recalc_sigpending();
3257 }
Oleg Nesterov03417292014-06-06 14:36:53 -07003258 spin_unlock_irq(&current->sighand->siglock);
3259}
Oleg Nesterovb4e74262014-06-06 14:37:00 -07003260EXPORT_SYMBOL(kernel_sigaction);
Oleg Nesterov03417292014-06-06 14:36:53 -07003261
Dmitry Safonov68463512016-09-05 16:33:08 +03003262void __weak sigaction_compat_abi(struct k_sigaction *act,
3263 struct k_sigaction *oact)
3264{
3265}
3266
Oleg Nesterov88531f72006-03-28 16:11:24 -08003267int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268{
Oleg Nesterovafe2b032014-06-06 14:36:51 -07003269 struct task_struct *p = current, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 struct k_sigaction *k;
George Anzinger71fabd52006-01-08 01:02:48 -08003271 sigset_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272
Jesper Juhl7ed20e12005-05-01 08:59:14 -07003273 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 return -EINVAL;
3275
Oleg Nesterovafe2b032014-06-06 14:36:51 -07003276 k = &p->sighand->action[sig-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277
Oleg Nesterovafe2b032014-06-06 14:36:51 -07003278 spin_lock_irq(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 if (oact)
3280 *oact = *k;
3281
Dmitry Safonov68463512016-09-05 16:33:08 +03003282 sigaction_compat_abi(act, oact);
3283
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 if (act) {
Oleg Nesterov9ac95f22006-02-09 22:41:50 +03003285 sigdelsetmask(&act->sa.sa_mask,
3286 sigmask(SIGKILL) | sigmask(SIGSTOP));
Oleg Nesterov88531f72006-03-28 16:11:24 -08003287 *k = *act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 /*
3289 * POSIX 3.3.1.3:
3290 * "Setting a signal action to SIG_IGN for a signal that is
3291 * pending shall cause the pending signal to be discarded,
3292 * whether or not it is blocked."
3293 *
3294 * "Setting a signal action to SIG_DFL for a signal that is
3295 * pending and whose default action is to ignore the signal
3296 * (for example, SIGCHLD), shall cause the pending signal to
3297 * be discarded, whether or not it is blocked"
3298 */
Oleg Nesterovafe2b032014-06-06 14:36:51 -07003299 if (sig_handler_ignored(sig_handler(p, sig), sig)) {
George Anzinger71fabd52006-01-08 01:02:48 -08003300 sigemptyset(&mask);
3301 sigaddset(&mask, sig);
Oleg Nesterovafe2b032014-06-06 14:36:51 -07003302 flush_sigqueue_mask(&mask, &p->signal->shared_pending);
3303 for_each_thread(p, t)
Oleg Nesterovc09c1442014-06-06 14:36:50 -07003304 flush_sigqueue_mask(&mask, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 }
3307
Oleg Nesterovafe2b032014-06-06 14:36:51 -07003308 spin_unlock_irq(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309 return 0;
3310}
3311
Oleg Nesterovc09c1442014-06-06 14:36:50 -07003312static int
Will Deacon1e7066a2018-09-05 15:34:42 +01003313do_sigaltstack (const stack_t __user *uss, stack_t __user *uoss, unsigned long sp,
3314 size_t min_ss_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315{
3316 stack_t oss;
3317 int error;
3318
Linus Torvalds0083fc22009-08-01 10:34:56 -07003319 oss.ss_sp = (void __user *) current->sas_ss_sp;
3320 oss.ss_size = current->sas_ss_size;
Andy Lutomirski0318bc82016-05-03 10:31:51 -07003321 oss.ss_flags = sas_ss_flags(sp) |
3322 (current->sas_ss_flags & SS_FLAG_BITS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
3324 if (uss) {
3325 void __user *ss_sp;
3326 size_t ss_size;
Stas Sergeev407bc162016-04-14 23:20:03 +03003327 unsigned ss_flags;
3328 int ss_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329
3330 error = -EFAULT;
Linus Torvalds0dd84862009-08-01 11:18:56 -07003331 if (!access_ok(VERIFY_READ, uss, sizeof(*uss)))
3332 goto out;
3333 error = __get_user(ss_sp, &uss->ss_sp) |
3334 __get_user(ss_flags, &uss->ss_flags) |
3335 __get_user(ss_size, &uss->ss_size);
3336 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 goto out;
3338
3339 error = -EPERM;
3340 if (on_sig_stack(sp))
3341 goto out;
3342
Stas Sergeev407bc162016-04-14 23:20:03 +03003343 ss_mode = ss_flags & ~SS_FLAG_BITS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 error = -EINVAL;
Stas Sergeev407bc162016-04-14 23:20:03 +03003345 if (ss_mode != SS_DISABLE && ss_mode != SS_ONSTACK &&
3346 ss_mode != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 goto out;
3348
Stas Sergeev407bc162016-04-14 23:20:03 +03003349 if (ss_mode == SS_DISABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 ss_size = 0;
3351 ss_sp = NULL;
3352 } else {
Will Deacon1e7066a2018-09-05 15:34:42 +01003353 if (unlikely(ss_size < min_ss_size))
3354 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 }
3356
3357 current->sas_ss_sp = (unsigned long) ss_sp;
3358 current->sas_ss_size = ss_size;
Stas Sergeev2a742132016-04-14 23:20:04 +03003359 current->sas_ss_flags = ss_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 }
3361
Linus Torvalds0083fc22009-08-01 10:34:56 -07003362 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 if (uoss) {
3364 error = -EFAULT;
Linus Torvalds0083fc22009-08-01 10:34:56 -07003365 if (!access_ok(VERIFY_WRITE, uoss, sizeof(*uoss)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366 goto out;
Linus Torvalds0083fc22009-08-01 10:34:56 -07003367 error = __put_user(oss.ss_sp, &uoss->ss_sp) |
3368 __put_user(oss.ss_size, &uoss->ss_size) |
3369 __put_user(oss.ss_flags, &uoss->ss_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 }
3371
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372out:
3373 return error;
3374}
Al Viro6bf9adf2012-12-14 14:09:47 -05003375SYSCALL_DEFINE2(sigaltstack,const stack_t __user *,uss, stack_t __user *,uoss)
3376{
Will Deacon1e7066a2018-09-05 15:34:42 +01003377 return do_sigaltstack(uss, uoss, current_user_stack_pointer(),
3378 MINSIGSTKSZ);
Al Viro6bf9adf2012-12-14 14:09:47 -05003379}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
Al Viro5c495742012-11-18 15:29:16 -05003381int restore_altstack(const stack_t __user *uss)
3382{
Will Deacon1e7066a2018-09-05 15:34:42 +01003383 int err = do_sigaltstack(uss, NULL, current_user_stack_pointer(),
3384 MINSIGSTKSZ);
Al Viro5c495742012-11-18 15:29:16 -05003385 /* squash all but EFAULT for now */
3386 return err == -EFAULT ? err : 0;
3387}
3388
Al Viroc40702c2012-11-20 14:24:26 -05003389int __save_altstack(stack_t __user *uss, unsigned long sp)
3390{
3391 struct task_struct *t = current;
Stas Sergeev2a742132016-04-14 23:20:04 +03003392 int err = __put_user((void __user *)t->sas_ss_sp, &uss->ss_sp) |
3393 __put_user(t->sas_ss_flags, &uss->ss_flags) |
Al Viroc40702c2012-11-20 14:24:26 -05003394 __put_user(t->sas_ss_size, &uss->ss_size);
Stas Sergeev2a742132016-04-14 23:20:04 +03003395 if (err)
3396 return err;
3397 if (t->sas_ss_flags & SS_AUTODISARM)
3398 sas_ss_reset(t);
3399 return 0;
Al Viroc40702c2012-11-20 14:24:26 -05003400}
3401
Al Viro90268432012-12-14 14:47:53 -05003402#ifdef CONFIG_COMPAT
Al Viro90228fc2012-12-23 03:33:38 -05003403COMPAT_SYSCALL_DEFINE2(sigaltstack,
3404 const compat_stack_t __user *, uss_ptr,
3405 compat_stack_t __user *, uoss_ptr)
Al Viro90268432012-12-14 14:47:53 -05003406{
3407 stack_t uss, uoss;
3408 int ret;
3409 mm_segment_t seg;
3410
3411 if (uss_ptr) {
3412 compat_stack_t uss32;
3413
3414 memset(&uss, 0, sizeof(stack_t));
3415 if (copy_from_user(&uss32, uss_ptr, sizeof(compat_stack_t)))
3416 return -EFAULT;
3417 uss.ss_sp = compat_ptr(uss32.ss_sp);
3418 uss.ss_flags = uss32.ss_flags;
3419 uss.ss_size = uss32.ss_size;
3420 }
3421 seg = get_fs();
3422 set_fs(KERNEL_DS);
3423 ret = do_sigaltstack((stack_t __force __user *) (uss_ptr ? &uss : NULL),
3424 (stack_t __force __user *) &uoss,
Will Deacon1e7066a2018-09-05 15:34:42 +01003425 compat_user_stack_pointer(),
3426 COMPAT_MINSIGSTKSZ);
Al Viro90268432012-12-14 14:47:53 -05003427 set_fs(seg);
3428 if (ret >= 0 && uoss_ptr) {
3429 if (!access_ok(VERIFY_WRITE, uoss_ptr, sizeof(compat_stack_t)) ||
3430 __put_user(ptr_to_compat(uoss.ss_sp), &uoss_ptr->ss_sp) ||
3431 __put_user(uoss.ss_flags, &uoss_ptr->ss_flags) ||
3432 __put_user(uoss.ss_size, &uoss_ptr->ss_size))
3433 ret = -EFAULT;
3434 }
3435 return ret;
3436}
3437
3438int compat_restore_altstack(const compat_stack_t __user *uss)
3439{
3440 int err = compat_sys_sigaltstack(uss, NULL);
3441 /* squash all but -EFAULT for now */
3442 return err == -EFAULT ? err : 0;
3443}
Al Viroc40702c2012-11-20 14:24:26 -05003444
3445int __compat_save_altstack(compat_stack_t __user *uss, unsigned long sp)
3446{
Stas Sergeev6d94a6b2017-02-27 14:27:25 -08003447 int err;
Al Viroc40702c2012-11-20 14:24:26 -05003448 struct task_struct *t = current;
Stas Sergeev6d94a6b2017-02-27 14:27:25 -08003449 err = __put_user(ptr_to_compat((void __user *)t->sas_ss_sp),
3450 &uss->ss_sp) |
3451 __put_user(t->sas_ss_flags, &uss->ss_flags) |
Al Viroc40702c2012-11-20 14:24:26 -05003452 __put_user(t->sas_ss_size, &uss->ss_size);
Stas Sergeev6d94a6b2017-02-27 14:27:25 -08003453 if (err)
3454 return err;
3455 if (t->sas_ss_flags & SS_AUTODISARM)
3456 sas_ss_reset(t);
3457 return 0;
Al Viroc40702c2012-11-20 14:24:26 -05003458}
Al Viro90268432012-12-14 14:47:53 -05003459#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
3461#ifdef __ARCH_WANT_SYS_SIGPENDING
3462
Randy Dunlap41c57892011-04-04 15:00:26 -07003463/**
3464 * sys_sigpending - examine pending signals
3465 * @set: where mask of pending signal is returned
3466 */
Heiko Carstensb290ebe2009-01-14 14:14:06 +01003467SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468{
Al Virofe9c1db2012-12-25 14:31:38 -05003469 return sys_rt_sigpending((sigset_t __user *)set, sizeof(old_sigset_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470}
3471
3472#endif
3473
3474#ifdef __ARCH_WANT_SYS_SIGPROCMASK
Randy Dunlap41c57892011-04-04 15:00:26 -07003475/**
3476 * sys_sigprocmask - examine and change blocked signals
3477 * @how: whether to add, remove, or set signals
Oleg Nesterovb013c392011-04-28 11:36:20 +02003478 * @nset: signals to add or remove (if non-null)
Randy Dunlap41c57892011-04-04 15:00:26 -07003479 * @oset: previous value of signal mask if non-null
3480 *
Randy Dunlap5aba0852011-04-04 14:59:31 -07003481 * Some platforms have their own version with special arguments;
3482 * others support only sys_rt_sigprocmask.
3483 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
Oleg Nesterovb013c392011-04-28 11:36:20 +02003485SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, nset,
Heiko Carstensb290ebe2009-01-14 14:14:06 +01003486 old_sigset_t __user *, oset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 old_sigset_t old_set, new_set;
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02003489 sigset_t new_blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490
Oleg Nesterovb013c392011-04-28 11:36:20 +02003491 old_set = current->blocked.sig[0];
3492
3493 if (nset) {
3494 if (copy_from_user(&new_set, nset, sizeof(*nset)))
3495 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02003497 new_blocked = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 switch (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 case SIG_BLOCK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02003501 sigaddsetmask(&new_blocked, new_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 break;
3503 case SIG_UNBLOCK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02003504 sigdelsetmask(&new_blocked, new_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 break;
3506 case SIG_SETMASK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02003507 new_blocked.sig[0] = new_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 break;
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02003509 default:
3510 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 }
3512
Oleg Nesterov0c4a8422013-01-05 19:13:29 +01003513 set_current_blocked(&new_blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 }
Oleg Nesterovb013c392011-04-28 11:36:20 +02003515
3516 if (oset) {
3517 if (copy_to_user(oset, &old_set, sizeof(*oset)))
3518 return -EFAULT;
3519 }
3520
3521 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522}
3523#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
3524
Al Viroeaca6ea2012-11-25 23:12:10 -05003525#ifndef CONFIG_ODD_RT_SIGACTION
Randy Dunlap41c57892011-04-04 15:00:26 -07003526/**
3527 * sys_rt_sigaction - alter an action taken by a process
3528 * @sig: signal to be sent
Randy Dunlapf9fa0bc2011-04-08 10:53:46 -07003529 * @act: new sigaction
3530 * @oact: used to save the previous sigaction
Randy Dunlap41c57892011-04-04 15:00:26 -07003531 * @sigsetsize: size of sigset_t type
3532 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01003533SYSCALL_DEFINE4(rt_sigaction, int, sig,
3534 const struct sigaction __user *, act,
3535 struct sigaction __user *, oact,
3536 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537{
3538 struct k_sigaction new_sa, old_sa;
3539 int ret = -EINVAL;
3540
3541 /* XXX: Don't preclude handling different sized sigset_t's. */
3542 if (sigsetsize != sizeof(sigset_t))
3543 goto out;
3544
3545 if (act) {
3546 if (copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
3547 return -EFAULT;
3548 }
3549
3550 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
3551
3552 if (!ret && oact) {
3553 if (copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
3554 return -EFAULT;
3555 }
3556out:
3557 return ret;
3558}
Al Viro08d32fe2012-12-25 18:38:15 -05003559#ifdef CONFIG_COMPAT
Al Viro08d32fe2012-12-25 18:38:15 -05003560COMPAT_SYSCALL_DEFINE4(rt_sigaction, int, sig,
3561 const struct compat_sigaction __user *, act,
3562 struct compat_sigaction __user *, oact,
3563 compat_size_t, sigsetsize)
3564{
3565 struct k_sigaction new_ka, old_ka;
3566 compat_sigset_t mask;
3567#ifdef __ARCH_HAS_SA_RESTORER
3568 compat_uptr_t restorer;
3569#endif
3570 int ret;
3571
3572 /* XXX: Don't preclude handling different sized sigset_t's. */
3573 if (sigsetsize != sizeof(compat_sigset_t))
3574 return -EINVAL;
3575
3576 if (act) {
3577 compat_uptr_t handler;
3578 ret = get_user(handler, &act->sa_handler);
3579 new_ka.sa.sa_handler = compat_ptr(handler);
3580#ifdef __ARCH_HAS_SA_RESTORER
3581 ret |= get_user(restorer, &act->sa_restorer);
3582 new_ka.sa.sa_restorer = compat_ptr(restorer);
3583#endif
3584 ret |= copy_from_user(&mask, &act->sa_mask, sizeof(mask));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003585 ret |= get_user(new_ka.sa.sa_flags, &act->sa_flags);
Al Viro08d32fe2012-12-25 18:38:15 -05003586 if (ret)
3587 return -EFAULT;
3588 sigset_from_compat(&new_ka.sa.sa_mask, &mask);
3589 }
3590
3591 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
3592 if (!ret && oact) {
3593 sigset_to_compat(&mask, &old_ka.sa.sa_mask);
3594 ret = put_user(ptr_to_compat(old_ka.sa.sa_handler),
3595 &oact->sa_handler);
3596 ret |= copy_to_user(&oact->sa_mask, &mask, sizeof(mask));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07003597 ret |= put_user(old_ka.sa.sa_flags, &oact->sa_flags);
Al Viro08d32fe2012-12-25 18:38:15 -05003598#ifdef __ARCH_HAS_SA_RESTORER
3599 ret |= put_user(ptr_to_compat(old_ka.sa.sa_restorer),
3600 &oact->sa_restorer);
3601#endif
3602 }
3603 return ret;
3604}
3605#endif
Al Viroeaca6ea2012-11-25 23:12:10 -05003606#endif /* !CONFIG_ODD_RT_SIGACTION */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607
Al Viro495dfbf2012-12-25 19:09:45 -05003608#ifdef CONFIG_OLD_SIGACTION
3609SYSCALL_DEFINE3(sigaction, int, sig,
3610 const struct old_sigaction __user *, act,
3611 struct old_sigaction __user *, oact)
3612{
3613 struct k_sigaction new_ka, old_ka;
3614 int ret;
3615
3616 if (act) {
3617 old_sigset_t mask;
3618 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
3619 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
3620 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
3621 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
3622 __get_user(mask, &act->sa_mask))
3623 return -EFAULT;
3624#ifdef __ARCH_HAS_KA_RESTORER
3625 new_ka.ka_restorer = NULL;
3626#endif
3627 siginitset(&new_ka.sa.sa_mask, mask);
3628 }
3629
3630 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
3631
3632 if (!ret && oact) {
3633 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
3634 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
3635 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
3636 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
3637 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
3638 return -EFAULT;
3639 }
3640
3641 return ret;
3642}
3643#endif
3644#ifdef CONFIG_COMPAT_OLD_SIGACTION
3645COMPAT_SYSCALL_DEFINE3(sigaction, int, sig,
3646 const struct compat_old_sigaction __user *, act,
3647 struct compat_old_sigaction __user *, oact)
3648{
3649 struct k_sigaction new_ka, old_ka;
3650 int ret;
3651 compat_old_sigset_t mask;
3652 compat_uptr_t handler, restorer;
3653
3654 if (act) {
3655 if (!access_ok(VERIFY_READ, act, sizeof(*act)) ||
3656 __get_user(handler, &act->sa_handler) ||
3657 __get_user(restorer, &act->sa_restorer) ||
3658 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
3659 __get_user(mask, &act->sa_mask))
3660 return -EFAULT;
3661
3662#ifdef __ARCH_HAS_KA_RESTORER
3663 new_ka.ka_restorer = NULL;
3664#endif
3665 new_ka.sa.sa_handler = compat_ptr(handler);
3666 new_ka.sa.sa_restorer = compat_ptr(restorer);
3667 siginitset(&new_ka.sa.sa_mask, mask);
3668 }
3669
3670 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
3671
3672 if (!ret && oact) {
3673 if (!access_ok(VERIFY_WRITE, oact, sizeof(*oact)) ||
3674 __put_user(ptr_to_compat(old_ka.sa.sa_handler),
3675 &oact->sa_handler) ||
3676 __put_user(ptr_to_compat(old_ka.sa.sa_restorer),
3677 &oact->sa_restorer) ||
3678 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
3679 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
3680 return -EFAULT;
3681 }
3682 return ret;
3683}
3684#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685
Fabian Frederickf6187762014-06-04 16:11:12 -07003686#ifdef CONFIG_SGETMASK_SYSCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687
3688/*
3689 * For backwards compatibility. Functionality superseded by sigprocmask.
3690 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003691SYSCALL_DEFINE0(sgetmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692{
3693 /* SMP safe */
3694 return current->blocked.sig[0];
3695}
3696
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003697SYSCALL_DEFINE1(ssetmask, int, newmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698{
Oleg Nesterovc1095c62011-07-27 12:49:44 -07003699 int old = current->blocked.sig[0];
3700 sigset_t newset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701
Oleg Nesterov5ba53ff2013-01-05 19:13:13 +01003702 siginitset(&newset, newmask);
Oleg Nesterovc1095c62011-07-27 12:49:44 -07003703 set_current_blocked(&newset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704
3705 return old;
3706}
Fabian Frederickf6187762014-06-04 16:11:12 -07003707#endif /* CONFIG_SGETMASK_SYSCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708
3709#ifdef __ARCH_WANT_SYS_SIGNAL
3710/*
3711 * For backwards compatibility. Functionality superseded by sigaction.
3712 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003713SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714{
3715 struct k_sigaction new_sa, old_sa;
3716 int ret;
3717
3718 new_sa.sa.sa_handler = handler;
3719 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
Oleg Nesterovc70d3d702006-02-09 22:41:41 +03003720 sigemptyset(&new_sa.sa.sa_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721
3722 ret = do_sigaction(sig, &new_sa, &old_sa);
3723
3724 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
3725}
3726#endif /* __ARCH_WANT_SYS_SIGNAL */
3727
3728#ifdef __ARCH_WANT_SYS_PAUSE
3729
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003730SYSCALL_DEFINE0(pause)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731{
Oleg Nesterovd92fcf02011-05-25 19:22:27 +02003732 while (!signal_pending(current)) {
Davidlohr Bueso1df01352015-02-17 13:45:41 -08003733 __set_current_state(TASK_INTERRUPTIBLE);
Oleg Nesterovd92fcf02011-05-25 19:22:27 +02003734 schedule();
3735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 return -ERESTARTNOHAND;
3737}
3738
3739#endif
3740
Richard Weinberger9d8a7652015-11-20 15:57:21 -08003741static int sigsuspend(sigset_t *set)
Al Viro68f3f162012-05-21 21:42:32 -04003742{
Al Viro68f3f162012-05-21 21:42:32 -04003743 current->saved_sigmask = current->blocked;
3744 set_current_blocked(set);
3745
Sasha Levin823dd322016-02-05 15:36:05 -08003746 while (!signal_pending(current)) {
3747 __set_current_state(TASK_INTERRUPTIBLE);
3748 schedule();
3749 }
Al Viro68f3f162012-05-21 21:42:32 -04003750 set_restore_sigmask();
3751 return -ERESTARTNOHAND;
3752}
Al Viro68f3f162012-05-21 21:42:32 -04003753
Randy Dunlap41c57892011-04-04 15:00:26 -07003754/**
3755 * sys_rt_sigsuspend - replace the signal mask for a value with the
3756 * @unewset value until a signal is received
3757 * @unewset: new signal mask value
3758 * @sigsetsize: size of sigset_t type
3759 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01003760SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
David Woodhouse150256d2006-01-18 17:43:57 -08003761{
3762 sigset_t newset;
3763
3764 /* XXX: Don't preclude handling different sized sigset_t's. */
3765 if (sigsetsize != sizeof(sigset_t))
3766 return -EINVAL;
3767
3768 if (copy_from_user(&newset, unewset, sizeof(newset)))
3769 return -EFAULT;
Al Viro68f3f162012-05-21 21:42:32 -04003770 return sigsuspend(&newset);
David Woodhouse150256d2006-01-18 17:43:57 -08003771}
Al Viroad4b65a2012-12-24 21:43:56 -05003772
3773#ifdef CONFIG_COMPAT
3774COMPAT_SYSCALL_DEFINE2(rt_sigsuspend, compat_sigset_t __user *, unewset, compat_size_t, sigsetsize)
3775{
3776#ifdef __BIG_ENDIAN
3777 sigset_t newset;
3778 compat_sigset_t newset32;
3779
3780 /* XXX: Don't preclude handling different sized sigset_t's. */
3781 if (sigsetsize != sizeof(sigset_t))
3782 return -EINVAL;
3783
3784 if (copy_from_user(&newset32, unewset, sizeof(compat_sigset_t)))
3785 return -EFAULT;
3786 sigset_from_compat(&newset, &newset32);
3787 return sigsuspend(&newset);
3788#else
3789 /* on little-endian bitmaps don't care about granularity */
3790 return sys_rt_sigsuspend((sigset_t __user *)unewset, sigsetsize);
3791#endif
3792}
3793#endif
David Woodhouse150256d2006-01-18 17:43:57 -08003794
Al Viro0a0e8cd2012-12-25 16:04:12 -05003795#ifdef CONFIG_OLD_SIGSUSPEND
3796SYSCALL_DEFINE1(sigsuspend, old_sigset_t, mask)
3797{
3798 sigset_t blocked;
3799 siginitset(&blocked, mask);
3800 return sigsuspend(&blocked);
3801}
3802#endif
3803#ifdef CONFIG_OLD_SIGSUSPEND3
3804SYSCALL_DEFINE3(sigsuspend, int, unused1, int, unused2, old_sigset_t, mask)
3805{
3806 sigset_t blocked;
3807 siginitset(&blocked, mask);
3808 return sigsuspend(&blocked);
3809}
3810#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811
Gideon Israel Dsouza52f56842014-04-07 15:39:20 -07003812__weak const char *arch_vma_name(struct vm_area_struct *vma)
David Howellsf269fdd2006-09-27 01:50:23 -07003813{
3814 return NULL;
3815}
3816
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817void __init signals_init(void)
3818{
Helge Deller41b27152016-03-22 14:27:54 -07003819 /* If this check fails, the __ARCH_SI_PREAMBLE_SIZE value is wrong! */
3820 BUILD_BUG_ON(__ARCH_SI_PREAMBLE_SIZE
3821 != offsetof(struct siginfo, _sifields._pad));
3822
Christoph Lameter0a31bd52007-05-06 14:49:57 -07003823 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824}
Jason Wessel67fc4e02010-05-20 21:04:21 -05003825
3826#ifdef CONFIG_KGDB_KDB
3827#include <linux/kdb.h>
3828/*
3829 * kdb_send_sig_info - Allows kdb to send signals without exposing
3830 * signal internals. This function checks if the required locks are
3831 * available before calling the main signal code, to avoid kdb
3832 * deadlocks.
3833 */
3834void
3835kdb_send_sig_info(struct task_struct *t, struct siginfo *info)
3836{
3837 static struct task_struct *kdb_prev_t;
3838 int sig, new_t;
3839 if (!spin_trylock(&t->sighand->siglock)) {
3840 kdb_printf("Can't do kill command now.\n"
3841 "The sigmask lock is held somewhere else in "
3842 "kernel, try again later\n");
3843 return;
3844 }
3845 spin_unlock(&t->sighand->siglock);
3846 new_t = kdb_prev_t != t;
3847 kdb_prev_t = t;
3848 if (t->state != TASK_RUNNING && new_t) {
3849 kdb_printf("Process is not RUNNING, sending a signal from "
3850 "kdb risks deadlock\n"
3851 "on the run queue locks. "
3852 "The signal has _not_ been sent.\n"
3853 "Reissue the kill command if you want to risk "
3854 "the deadlock.\n");
3855 return;
3856 }
3857 sig = info->si_signo;
3858 if (send_sig_info(sig, info, t))
3859 kdb_printf("Fail to deliver Signal %d to process %d.\n",
3860 sig, t->pid);
3861 else
3862 kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
3863}
3864#endif /* CONFIG_KGDB_KDB */