blob: f98448cf2defb5b2f212d005dcfa2a899252ae25 [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>
Ingo Molnar589ee622017-02-04 00:16:44 +010016#include <linux/sched/mm.h>
Ingo Molnar8703e8a2017-02-08 18:51:30 +010017#include <linux/sched/user.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010018#include <linux/sched/debug.h>
Ingo Molnar29930022017-02-08 18:51:36 +010019#include <linux/sched/task.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010020#include <linux/sched/task_stack.h>
Ingo Molnar32ef5512017-02-05 11:48:36 +010021#include <linux/sched/cputime.h>
Christian Brauner3eb39f42018-11-19 00:51:56 +010022#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/fs.h>
Christian Brauner3eb39f42018-11-19 00:51:56 +010024#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/tty.h>
26#include <linux/binfmts.h>
Alex Kelly179899f2012-10-04 17:15:24 -070027#include <linux/coredump.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/security.h>
29#include <linux/syscalls.h>
30#include <linux/ptrace.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070031#include <linux/signal.h>
Davide Libenzifba2afa2007-05-10 22:23:13 -070032#include <linux/signalfd.h>
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +090033#include <linux/ratelimit.h>
Roland McGrath35de2542008-07-25 19:45:51 -070034#include <linux/tracehook.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080035#include <linux/capability.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080036#include <linux/freezer.h>
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -080037#include <linux/pid_namespace.h>
38#include <linux/nsproxy.h>
Serge E. Hallyn6b550f92012-01-10 15:11:37 -080039#include <linux/user_namespace.h>
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +053040#include <linux/uprobes.h>
Al Viro90268432012-12-14 14:47:53 -050041#include <linux/compat.h>
Jesper Derehag2b5faa42013-03-19 20:50:05 +000042#include <linux/cn_proc.h>
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -070043#include <linux/compiler.h>
Christoph Hellwig31ea70e2017-06-03 21:01:00 +020044#include <linux/posix-timers.h>
Miroslav Benes43347d52017-11-15 14:50:13 +010045#include <linux/livepatch.h>
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -070046
Masami Hiramatsud1eb6502009-11-24 16:56:45 -050047#define CREATE_TRACE_POINTS
48#include <trace/events/signal.h>
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -080049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <asm/param.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080051#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <asm/unistd.h>
53#include <asm/siginfo.h>
David Howellsd550bbd2012-03-28 18:30:03 +010054#include <asm/cacheflush.h>
Al Viroe1396062006-05-25 10:19:47 -040055#include "audit.h" /* audit_signal_info() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/*
58 * SLAB caches for signal bits.
59 */
60
Christoph Lametere18b8902006-12-06 20:33:20 -080061static struct kmem_cache *sigqueue_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +090063int print_fatal_signals __read_mostly;
64
Roland McGrath35de2542008-07-25 19:45:51 -070065static void __user *sig_handler(struct task_struct *t, int sig)
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070066{
Roland McGrath35de2542008-07-25 19:45:51 -070067 return t->sighand->action[sig - 1].sa.sa_handler;
68}
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070069
Christian Braunere4a8b4e2018-08-21 22:00:15 -070070static inline bool sig_handler_ignored(void __user *handler, int sig)
Roland McGrath35de2542008-07-25 19:45:51 -070071{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070072 /* Is it explicitly or implicitly ignored? */
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070073 return handler == SIG_IGN ||
Christian Braunere4a8b4e2018-08-21 22:00:15 -070074 (handler == SIG_DFL && sig_kernel_ignore(sig));
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070075}
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Christian Brauner41aaa482018-08-21 22:00:19 -070077static bool sig_task_ignored(struct task_struct *t, int sig, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Roland McGrath35de2542008-07-25 19:45:51 -070079 void __user *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Oleg Nesterovf008faf2009-04-02 16:58:02 -070081 handler = sig_handler(t, sig);
82
Eric W. Biederman86989c42018-07-19 19:47:27 -050083 /* SIGKILL and SIGSTOP may not be sent to the global init */
84 if (unlikely(is_global_init(t) && sig_kernel_only(sig)))
85 return true;
86
Oleg Nesterovf008faf2009-04-02 16:58:02 -070087 if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
Oleg Nesterovac253852017-11-17 15:30:04 -080088 handler == SIG_DFL && !(force && sig_kernel_only(sig)))
Christian Brauner41aaa482018-08-21 22:00:19 -070089 return true;
Oleg Nesterovf008faf2009-04-02 16:58:02 -070090
91 return sig_handler_ignored(handler, sig);
92}
93
Christian Brauner6a0cdcd2018-08-21 22:00:23 -070094static bool sig_ignored(struct task_struct *t, int sig, bool force)
Oleg Nesterovf008faf2009-04-02 16:58:02 -070095{
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 /*
97 * Blocked signals are never ignored, since the
98 * signal handler may change by the time it is
99 * unblocked.
100 */
Roland McGrath325d22d2007-11-12 15:41:55 -0800101 if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
Christian Brauner6a0cdcd2018-08-21 22:00:23 -0700102 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Oleg Nesterov628c1bc2017-11-17 15:30:01 -0800104 /*
105 * Tracers may want to know about even ignored signal unless it
106 * is SIGKILL which can't be reported anyway but can be ignored
107 * by SIGNAL_UNKILLABLE task.
108 */
109 if (t->ptrace && sig != SIGKILL)
Christian Brauner6a0cdcd2018-08-21 22:00:23 -0700110 return false;
Roland McGrath35de2542008-07-25 19:45:51 -0700111
Oleg Nesterov628c1bc2017-11-17 15:30:01 -0800112 return sig_task_ignored(t, sig, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
115/*
116 * Re-calculate pending state from the set of locally pending
117 * signals, globally pending signals, and blocked signals.
118 */
Christian Brauner938696a2018-08-21 22:00:27 -0700119static inline bool has_pending_signals(sigset_t *signal, sigset_t *blocked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 unsigned long ready;
122 long i;
123
124 switch (_NSIG_WORDS) {
125 default:
126 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
127 ready |= signal->sig[i] &~ blocked->sig[i];
128 break;
129
130 case 4: ready = signal->sig[3] &~ blocked->sig[3];
131 ready |= signal->sig[2] &~ blocked->sig[2];
132 ready |= signal->sig[1] &~ blocked->sig[1];
133 ready |= signal->sig[0] &~ blocked->sig[0];
134 break;
135
136 case 2: ready = signal->sig[1] &~ blocked->sig[1];
137 ready |= signal->sig[0] &~ blocked->sig[0];
138 break;
139
140 case 1: ready = signal->sig[0] &~ blocked->sig[0];
141 }
142 return ready != 0;
143}
144
145#define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
146
Christian Brauner09ae8542018-08-21 22:00:30 -0700147static bool recalc_sigpending_tsk(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Tejun Heo3759a0d2011-06-02 11:14:00 +0200149 if ((t->jobctl & JOBCTL_PENDING_MASK) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 PENDING(&t->pending, &t->blocked) ||
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700151 PENDING(&t->signal->shared_pending, &t->blocked)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 set_tsk_thread_flag(t, TIF_SIGPENDING);
Christian Brauner09ae8542018-08-21 22:00:30 -0700153 return true;
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700154 }
Christian Brauner09ae8542018-08-21 22:00:30 -0700155
Roland McGrathb74d0de2007-06-06 03:59:00 -0700156 /*
157 * We must never clear the flag in another thread, or in current
158 * when it's possible the current syscall is returning -ERESTART*.
159 * So we don't clear it here, and only callers who know they should do.
160 */
Christian Brauner09ae8542018-08-21 22:00:30 -0700161 return false;
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700162}
163
164/*
165 * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
166 * This is superfluous when called on current, the wakeup is a harmless no-op.
167 */
168void recalc_sigpending_and_wake(struct task_struct *t)
169{
170 if (recalc_sigpending_tsk(t))
171 signal_wake_up(t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174void recalc_sigpending(void)
175{
Miroslav Benes43347d52017-11-15 14:50:13 +0100176 if (!recalc_sigpending_tsk(current) && !freezing(current) &&
177 !klp_patch_pending(current))
Roland McGrathb74d0de2007-06-06 03:59:00 -0700178 clear_thread_flag(TIF_SIGPENDING);
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
Eric W. Biedermanfb50f5a2018-09-13 19:26:35 +0200181EXPORT_SYMBOL(recalc_sigpending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Eric W. Biederman088fe472018-07-23 17:26:49 -0500183void calculate_sigpending(void)
184{
185 /* Have any signals or users of TIF_SIGPENDING been delayed
186 * until after fork?
187 */
188 spin_lock_irq(&current->sighand->siglock);
189 set_tsk_thread_flag(current, TIF_SIGPENDING);
190 recalc_sigpending();
191 spin_unlock_irq(&current->sighand->siglock);
192}
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194/* Given the mask, find the first available signal that should be serviced. */
195
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800196#define SYNCHRONOUS_MASK \
197 (sigmask(SIGSEGV) | sigmask(SIGBUS) | sigmask(SIGILL) | \
Will Drewrya0727e82012-04-12 16:48:00 -0500198 sigmask(SIGTRAP) | sigmask(SIGFPE) | sigmask(SIGSYS))
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800199
Davide Libenzifba2afa2007-05-10 22:23:13 -0700200int next_signal(struct sigpending *pending, sigset_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 unsigned long i, *s, *m, x;
203 int sig = 0;
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 s = pending->signal.sig;
206 m = mask->sig;
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800207
208 /*
209 * Handle the first word specially: it contains the
210 * synchronous signals that need to be dequeued first.
211 */
212 x = *s &~ *m;
213 if (x) {
214 if (x & SYNCHRONOUS_MASK)
215 x &= SYNCHRONOUS_MASK;
216 sig = ffz(~x) + 1;
217 return sig;
218 }
219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 switch (_NSIG_WORDS) {
221 default:
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800222 for (i = 1; i < _NSIG_WORDS; ++i) {
223 x = *++s &~ *++m;
224 if (!x)
225 continue;
226 sig = ffz(~x) + i*_NSIG_BPW + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 break;
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 break;
230
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800231 case 2:
232 x = s[1] &~ m[1];
233 if (!x)
234 break;
235 sig = ffz(~x) + _NSIG_BPW + 1;
236 break;
237
238 case 1:
239 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 break;
241 }
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 return sig;
244}
245
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900246static inline void print_dropped_signal(int sig)
247{
248 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
249
250 if (!print_fatal_signals)
251 return;
252
253 if (!__ratelimit(&ratelimit_state))
254 return;
255
Wang Xiaoqiang747800e2016-05-23 16:23:59 -0700256 pr_info("%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n",
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900257 current->comm, current->pid, sig);
258}
259
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100260/**
Tejun Heo7dd3db52011-06-02 11:14:00 +0200261 * task_set_jobctl_pending - set jobctl pending bits
262 * @task: target task
263 * @mask: pending bits to set
264 *
265 * Clear @mask from @task->jobctl. @mask must be subset of
266 * %JOBCTL_PENDING_MASK | %JOBCTL_STOP_CONSUME | %JOBCTL_STOP_SIGMASK |
267 * %JOBCTL_TRAPPING. If stop signo is being set, the existing signo is
268 * cleared. If @task is already being killed or exiting, this function
269 * becomes noop.
270 *
271 * CONTEXT:
272 * Must be called with @task->sighand->siglock held.
273 *
274 * RETURNS:
275 * %true if @mask is set, %false if made noop because @task was dying.
276 */
Palmer Dabbeltb76808e2015-04-30 21:19:57 -0700277bool task_set_jobctl_pending(struct task_struct *task, unsigned long mask)
Tejun Heo7dd3db52011-06-02 11:14:00 +0200278{
279 BUG_ON(mask & ~(JOBCTL_PENDING_MASK | JOBCTL_STOP_CONSUME |
280 JOBCTL_STOP_SIGMASK | JOBCTL_TRAPPING));
281 BUG_ON((mask & JOBCTL_TRAPPING) && !(mask & JOBCTL_PENDING_MASK));
282
283 if (unlikely(fatal_signal_pending(task) || (task->flags & PF_EXITING)))
284 return false;
285
286 if (mask & JOBCTL_STOP_SIGMASK)
287 task->jobctl &= ~JOBCTL_STOP_SIGMASK;
288
289 task->jobctl |= mask;
290 return true;
291}
292
293/**
Tejun Heoa8f072c2011-06-02 11:13:59 +0200294 * task_clear_jobctl_trapping - clear jobctl trapping bit
Tejun Heod79fdd62011-03-23 10:37:00 +0100295 * @task: target task
296 *
Tejun Heoa8f072c2011-06-02 11:13:59 +0200297 * If JOBCTL_TRAPPING is set, a ptracer is waiting for us to enter TRACED.
298 * Clear it and wake up the ptracer. Note that we don't need any further
299 * locking. @task->siglock guarantees that @task->parent points to the
300 * ptracer.
Tejun Heod79fdd62011-03-23 10:37:00 +0100301 *
302 * CONTEXT:
303 * Must be called with @task->sighand->siglock held.
304 */
Tejun Heo73ddff22011-06-14 11:20:14 +0200305void task_clear_jobctl_trapping(struct task_struct *task)
Tejun Heod79fdd62011-03-23 10:37:00 +0100306{
Tejun Heoa8f072c2011-06-02 11:13:59 +0200307 if (unlikely(task->jobctl & JOBCTL_TRAPPING)) {
308 task->jobctl &= ~JOBCTL_TRAPPING;
Oleg Nesterov650226b2014-06-06 14:36:44 -0700309 smp_mb(); /* advised by wake_up_bit() */
Tejun Heo62c124f2011-06-02 11:14:00 +0200310 wake_up_bit(&task->jobctl, JOBCTL_TRAPPING_BIT);
Tejun Heod79fdd62011-03-23 10:37:00 +0100311 }
312}
313
314/**
Tejun Heo3759a0d2011-06-02 11:14:00 +0200315 * task_clear_jobctl_pending - clear jobctl pending bits
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100316 * @task: target task
Tejun Heo3759a0d2011-06-02 11:14:00 +0200317 * @mask: pending bits to clear
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100318 *
Tejun Heo3759a0d2011-06-02 11:14:00 +0200319 * Clear @mask from @task->jobctl. @mask must be subset of
320 * %JOBCTL_PENDING_MASK. If %JOBCTL_STOP_PENDING is being cleared, other
321 * STOP bits are cleared together.
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100322 *
Tejun Heo6dfca322011-06-02 11:14:00 +0200323 * If clearing of @mask leaves no stop or trap pending, this function calls
324 * task_clear_jobctl_trapping().
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100325 *
326 * CONTEXT:
327 * Must be called with @task->sighand->siglock held.
328 */
Palmer Dabbeltb76808e2015-04-30 21:19:57 -0700329void task_clear_jobctl_pending(struct task_struct *task, unsigned long mask)
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100330{
Tejun Heo3759a0d2011-06-02 11:14:00 +0200331 BUG_ON(mask & ~JOBCTL_PENDING_MASK);
332
333 if (mask & JOBCTL_STOP_PENDING)
334 mask |= JOBCTL_STOP_CONSUME | JOBCTL_STOP_DEQUEUED;
335
336 task->jobctl &= ~mask;
Tejun Heo6dfca322011-06-02 11:14:00 +0200337
338 if (!(task->jobctl & JOBCTL_PENDING_MASK))
339 task_clear_jobctl_trapping(task);
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100340}
341
342/**
343 * task_participate_group_stop - participate in a group stop
344 * @task: task participating in a group stop
345 *
Tejun Heoa8f072c2011-06-02 11:13:59 +0200346 * @task has %JOBCTL_STOP_PENDING set and is participating in a group stop.
Tejun Heo39efa3e2011-03-23 10:37:00 +0100347 * Group stop states are cleared and the group stop count is consumed if
Tejun Heoa8f072c2011-06-02 11:13:59 +0200348 * %JOBCTL_STOP_CONSUME was set. If the consumption completes the group
Tejun Heo39efa3e2011-03-23 10:37:00 +0100349 * stop, the appropriate %SIGNAL_* flags are set.
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100350 *
351 * CONTEXT:
352 * Must be called with @task->sighand->siglock held.
Tejun Heo244056f2011-03-23 10:37:01 +0100353 *
354 * RETURNS:
355 * %true if group stop completion should be notified to the parent, %false
356 * otherwise.
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100357 */
358static bool task_participate_group_stop(struct task_struct *task)
359{
360 struct signal_struct *sig = task->signal;
Tejun Heoa8f072c2011-06-02 11:13:59 +0200361 bool consume = task->jobctl & JOBCTL_STOP_CONSUME;
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100362
Tejun Heoa8f072c2011-06-02 11:13:59 +0200363 WARN_ON_ONCE(!(task->jobctl & JOBCTL_STOP_PENDING));
Tejun Heo39efa3e2011-03-23 10:37:00 +0100364
Tejun Heo3759a0d2011-06-02 11:14:00 +0200365 task_clear_jobctl_pending(task, JOBCTL_STOP_PENDING);
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100366
367 if (!consume)
368 return false;
369
370 if (!WARN_ON_ONCE(sig->group_stop_count == 0))
371 sig->group_stop_count--;
372
Tejun Heo244056f2011-03-23 10:37:01 +0100373 /*
374 * Tell the caller to notify completion iff we are entering into a
375 * fresh group stop. Read comment in do_signal_stop() for details.
376 */
377 if (!sig->group_stop_count && !(sig->flags & SIGNAL_STOP_STOPPED)) {
Jamie Iles2d39b3c2017-01-10 16:57:54 -0800378 signal_set_stop_flags(sig, SIGNAL_STOP_STOPPED);
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100379 return true;
380 }
381 return false;
382}
383
Eric W. Biederman924de3b2018-07-23 13:38:00 -0500384void task_join_group_stop(struct task_struct *task)
385{
386 /* Have the new thread join an on-going signal group stop */
387 unsigned long jobctl = current->jobctl;
388 if (jobctl & JOBCTL_STOP_PENDING) {
389 struct signal_struct *sig = current->signal;
390 unsigned long signr = jobctl & JOBCTL_STOP_SIGMASK;
391 unsigned long gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
392 if (task_set_jobctl_pending(task, signr | gstop)) {
393 sig->group_stop_count++;
394 }
395 }
396}
397
David Howellsc69e8d92008-11-14 10:39:19 +1100398/*
399 * allocate a new signal queue record
400 * - this may be called without locks if and only if t == current, otherwise an
Randy Dunlap5aba0852011-04-04 14:59:31 -0700401 * appropriate lock must be held to stop the target task from exiting
David Howellsc69e8d92008-11-14 10:39:19 +1100402 */
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900403static struct sigqueue *
404__sigqueue_alloc(int sig, struct task_struct *t, gfp_t flags, int override_rlimit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 struct sigqueue *q = NULL;
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800407 struct user_struct *user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800409 /*
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000410 * Protect access to @t credentials. This can go away when all
411 * callers hold rcu read lock.
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800412 */
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000413 rcu_read_lock();
David Howellsd84f4f92008-11-14 10:39:23 +1100414 user = get_uid(__task_cred(t)->user);
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800415 atomic_inc(&user->sigpending);
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000416 rcu_read_unlock();
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 if (override_rlimit ||
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800419 atomic_read(&user->sigpending) <=
Jiri Slaby78d7d402010-03-05 13:42:54 -0800420 task_rlimit(t, RLIMIT_SIGPENDING)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 q = kmem_cache_alloc(sigqueue_cachep, flags);
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900422 } else {
423 print_dropped_signal(sig);
424 }
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (unlikely(q == NULL)) {
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800427 atomic_dec(&user->sigpending);
David Howellsd84f4f92008-11-14 10:39:23 +1100428 free_uid(user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 } else {
430 INIT_LIST_HEAD(&q->list);
431 q->flags = 0;
David Howellsd84f4f92008-11-14 10:39:23 +1100432 q->user = user;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 }
David Howellsd84f4f92008-11-14 10:39:23 +1100434
435 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436}
437
Andrew Morton514a01b2006-02-03 03:04:41 -0800438static void __sigqueue_free(struct sigqueue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 if (q->flags & SIGQUEUE_PREALLOC)
441 return;
442 atomic_dec(&q->user->sigpending);
443 free_uid(q->user);
444 kmem_cache_free(sigqueue_cachep, q);
445}
446
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800447void flush_sigqueue(struct sigpending *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 struct sigqueue *q;
450
451 sigemptyset(&queue->signal);
452 while (!list_empty(&queue->list)) {
453 q = list_entry(queue->list.next, struct sigqueue , list);
454 list_del_init(&q->list);
455 __sigqueue_free(q);
456 }
457}
458
459/*
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -0400460 * Flush all pending signals for this kthread.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 */
Oleg Nesterovc81addc2006-03-28 16:11:17 -0800462void flush_signals(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 unsigned long flags;
465
466 spin_lock_irqsave(&t->sighand->siglock, flags);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -0400467 clear_tsk_thread_flag(t, TIF_SIGPENDING);
468 flush_sigqueue(&t->pending);
469 flush_sigqueue(&t->signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 spin_unlock_irqrestore(&t->sighand->siglock, flags);
471}
Eric W. Biedermanfb50f5a2018-09-13 19:26:35 +0200472EXPORT_SYMBOL(flush_signals);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Nicolas Pitrebaa73d92016-11-11 00:10:10 -0500474#ifdef CONFIG_POSIX_TIMERS
Oleg Nesterovcbaffba2008-05-26 20:55:42 +0400475static void __flush_itimer_signals(struct sigpending *pending)
476{
477 sigset_t signal, retain;
478 struct sigqueue *q, *n;
479
480 signal = pending->signal;
481 sigemptyset(&retain);
482
483 list_for_each_entry_safe(q, n, &pending->list, list) {
484 int sig = q->info.si_signo;
485
486 if (likely(q->info.si_code != SI_TIMER)) {
487 sigaddset(&retain, sig);
488 } else {
489 sigdelset(&signal, sig);
490 list_del_init(&q->list);
491 __sigqueue_free(q);
492 }
493 }
494
495 sigorsets(&pending->signal, &signal, &retain);
496}
497
498void flush_itimer_signals(void)
499{
500 struct task_struct *tsk = current;
501 unsigned long flags;
502
503 spin_lock_irqsave(&tsk->sighand->siglock, flags);
504 __flush_itimer_signals(&tsk->pending);
505 __flush_itimer_signals(&tsk->signal->shared_pending);
506 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
507}
Nicolas Pitrebaa73d92016-11-11 00:10:10 -0500508#endif
Oleg Nesterovcbaffba2008-05-26 20:55:42 +0400509
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700510void ignore_signals(struct task_struct *t)
511{
512 int i;
513
514 for (i = 0; i < _NSIG; ++i)
515 t->sighand->action[i].sa.sa_handler = SIG_IGN;
516
517 flush_signals(t);
518}
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 * Flush all handlers for a task.
522 */
523
524void
525flush_signal_handlers(struct task_struct *t, int force_default)
526{
527 int i;
528 struct k_sigaction *ka = &t->sighand->action[0];
529 for (i = _NSIG ; i != 0 ; i--) {
530 if (force_default || ka->sa.sa_handler != SIG_IGN)
531 ka->sa.sa_handler = SIG_DFL;
532 ka->sa.sa_flags = 0;
Andrew Morton522cff12013-03-13 14:59:34 -0700533#ifdef __ARCH_HAS_SA_RESTORER
Kees Cook2ca39522013-03-13 14:59:33 -0700534 ka->sa.sa_restorer = NULL;
535#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 sigemptyset(&ka->sa.sa_mask);
537 ka++;
538 }
539}
540
Christian Brauner67a48a22018-08-21 22:00:34 -0700541bool unhandled_signal(struct task_struct *tsk, int sig)
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200542{
Roland McGrath445a91d2008-07-25 19:45:52 -0700543 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700544 if (is_global_init(tsk))
Christian Brauner67a48a22018-08-21 22:00:34 -0700545 return true;
546
Roland McGrath445a91d2008-07-25 19:45:52 -0700547 if (handler != SIG_IGN && handler != SIG_DFL)
Christian Brauner67a48a22018-08-21 22:00:34 -0700548 return false;
549
Tejun Heoa288eec2011-06-17 16:50:37 +0200550 /* if ptraced, let the tracer determine */
551 return !tsk->ptrace;
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200552}
553
Eric W. Biedermanae7795b2018-09-25 11:27:20 +0200554static void collect_signal(int sig, struct sigpending *list, kernel_siginfo_t *info,
Eric W. Biederman57db7e42017-06-13 04:31:16 -0500555 bool *resched_timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 struct sigqueue *q, *first = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 /*
560 * Collect the siginfo appropriate to this signal. Check if
561 * there is another siginfo for the same signal.
562 */
563 list_for_each_entry(q, &list->list, list) {
564 if (q->info.si_signo == sig) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700565 if (first)
566 goto still_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 first = q;
568 }
569 }
Oleg Nesterovd4434202008-07-25 01:47:28 -0700570
571 sigdelset(&list->signal, sig);
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (first) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700574still_pending:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 list_del_init(&first->list);
576 copy_siginfo(info, &first->info);
Eric W. Biederman57db7e42017-06-13 04:31:16 -0500577
578 *resched_timer =
579 (first->flags & SIGQUEUE_PREALLOC) &&
580 (info->si_code == SI_TIMER) &&
581 (info->si_sys_private);
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 __sigqueue_free(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 } else {
Randy Dunlap5aba0852011-04-04 14:59:31 -0700585 /*
586 * Ok, it wasn't in the queue. This must be
587 * a fast-pathed signal or we must have been
588 * out of queue space. So zero out the info.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 */
Eric W. Biedermanfaf1f222018-01-05 17:27:42 -0600590 clear_siginfo(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 info->si_signo = sig;
592 info->si_errno = 0;
Oleg Nesterov7486e5d2009-12-15 16:47:24 -0800593 info->si_code = SI_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 info->si_pid = 0;
595 info->si_uid = 0;
596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597}
598
599static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
Eric W. Biedermanae7795b2018-09-25 11:27:20 +0200600 kernel_siginfo_t *info, bool *resched_timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
Roland McGrath27d91e02006-09-29 02:00:31 -0700602 int sig = next_signal(pending, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603
Oleg Nesterov2e01fab2015-11-06 16:32:19 -0800604 if (sig)
Eric W. Biederman57db7e42017-06-13 04:31:16 -0500605 collect_signal(sig, pending, info, resched_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 return sig;
607}
608
609/*
Randy Dunlap5aba0852011-04-04 14:59:31 -0700610 * Dequeue a signal and return the element to the caller, which is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 * expected to free it.
612 *
613 * All callers have to hold the siglock.
614 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +0200615int dequeue_signal(struct task_struct *tsk, sigset_t *mask, kernel_siginfo_t *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Eric W. Biederman57db7e42017-06-13 04:31:16 -0500617 bool resched_timer = false;
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700618 int signr;
Benjamin Herrenschmidtcaec4e82007-06-12 08:16:18 +1000619
620 /* We only dequeue private signals from ourselves, we don't let
621 * signalfd steal them
622 */
Eric W. Biederman57db7e42017-06-13 04:31:16 -0500623 signr = __dequeue_signal(&tsk->pending, mask, info, &resched_timer);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800624 if (!signr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 signr = __dequeue_signal(&tsk->signal->shared_pending,
Eric W. Biederman57db7e42017-06-13 04:31:16 -0500626 mask, info, &resched_timer);
Nicolas Pitrebaa73d92016-11-11 00:10:10 -0500627#ifdef CONFIG_POSIX_TIMERS
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800628 /*
629 * itimer signal ?
630 *
631 * itimers are process shared and we restart periodic
632 * itimers in the signal delivery path to prevent DoS
633 * attacks in the high resolution timer case. This is
Randy Dunlap5aba0852011-04-04 14:59:31 -0700634 * compliant with the old way of self-restarting
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800635 * itimers, as the SIGALRM is a legacy signal and only
636 * queued once. Changing the restart behaviour to
637 * restart the timer in the signal dequeue path is
638 * reducing the timer noise on heavy loaded !highres
639 * systems too.
640 */
641 if (unlikely(signr == SIGALRM)) {
642 struct hrtimer *tmr = &tsk->signal->real_timer;
643
644 if (!hrtimer_is_queued(tmr) &&
Thomas Gleixner2456e852016-12-25 11:38:40 +0100645 tsk->signal->it_real_incr != 0) {
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800646 hrtimer_forward(tmr, tmr->base->get_time(),
647 tsk->signal->it_real_incr);
648 hrtimer_restart(tmr);
649 }
650 }
Nicolas Pitrebaa73d92016-11-11 00:10:10 -0500651#endif
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800652 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700653
Davide Libenzib8fceee2007-09-20 12:40:16 -0700654 recalc_sigpending();
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700655 if (!signr)
656 return 0;
657
658 if (unlikely(sig_kernel_stop(signr))) {
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800659 /*
660 * Set a marker that we have dequeued a stop signal. Our
661 * caller might release the siglock and then the pending
662 * stop signal it is about to process is no longer in the
663 * pending bitmasks, but must still be cleared by a SIGCONT
664 * (and overruled by a SIGKILL). So those cases clear this
665 * shared flag after we've set it. Note that this flag may
666 * remain set after the signal we return is ignored or
667 * handled. That doesn't matter because its only purpose
668 * is to alert stop-signal processing code when another
669 * processor has come along and cleared the flag.
670 */
Tejun Heoa8f072c2011-06-02 11:13:59 +0200671 current->jobctl |= JOBCTL_STOP_DEQUEUED;
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800672 }
Nicolas Pitrebaa73d92016-11-11 00:10:10 -0500673#ifdef CONFIG_POSIX_TIMERS
Eric W. Biederman57db7e42017-06-13 04:31:16 -0500674 if (resched_timer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 /*
676 * Release the siglock to ensure proper locking order
677 * of timer locks outside of siglocks. Note, we leave
678 * irqs disabled here, since the posix-timers code is
679 * about to disable them again anyway.
680 */
681 spin_unlock(&tsk->sighand->siglock);
Thomas Gleixner96fe3b02017-05-30 23:15:46 +0200682 posixtimer_rearm(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 spin_lock(&tsk->sighand->siglock);
Eric W. Biederman9943d3a2017-07-24 14:53:03 -0500684
685 /* Don't expose the si_sys_private value to userspace */
686 info->si_sys_private = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 }
Nicolas Pitrebaa73d92016-11-11 00:10:10 -0500688#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return signr;
690}
Eric W. Biedermanfb50f5a2018-09-13 19:26:35 +0200691EXPORT_SYMBOL_GPL(dequeue_signal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Eric W. Biederman7146db32019-02-06 17:51:47 -0600693static int dequeue_synchronous_signal(kernel_siginfo_t *info)
694{
695 struct task_struct *tsk = current;
696 struct sigpending *pending = &tsk->pending;
697 struct sigqueue *q, *sync = NULL;
698
699 /*
700 * Might a synchronous signal be in the queue?
701 */
702 if (!((pending->signal.sig[0] & ~tsk->blocked.sig[0]) & SYNCHRONOUS_MASK))
703 return 0;
704
705 /*
706 * Return the first synchronous signal in the queue.
707 */
708 list_for_each_entry(q, &pending->list, list) {
709 /* Synchronous signals have a postive si_code */
710 if ((q->info.si_code > SI_USER) &&
711 (sigmask(q->info.si_signo) & SYNCHRONOUS_MASK)) {
712 sync = q;
713 goto next;
714 }
715 }
716 return 0;
717next:
718 /*
719 * Check if there is another siginfo for the same signal.
720 */
721 list_for_each_entry_continue(q, &pending->list, list) {
722 if (q->info.si_signo == sync->info.si_signo)
723 goto still_pending;
724 }
725
726 sigdelset(&pending->signal, sync->info.si_signo);
727 recalc_sigpending();
728still_pending:
729 list_del_init(&sync->list);
730 copy_siginfo(info, &sync->info);
731 __sigqueue_free(sync);
732 return info->si_signo;
733}
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735/*
736 * Tell a process that it has a new active signal..
737 *
738 * NOTE! we rely on the previous spin_lock to
739 * lock interrupts for us! We can only be called with
740 * "siglock" held, and the local interrupt must
741 * have been disabled when that got acquired!
742 *
743 * No need to set need_resched since signal event passing
744 * goes through ->blocked
745 */
Oleg Nesterov910ffdb2013-01-21 20:47:41 +0100746void signal_wake_up_state(struct task_struct *t, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 set_tsk_thread_flag(t, TIF_SIGPENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 /*
Oleg Nesterov910ffdb2013-01-21 20:47:41 +0100750 * TASK_WAKEKILL also means wake it up in the stopped/traced/killable
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500751 * case. We don't check t->state here because there is a race with it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 * executing another processor and just now entering stopped state.
753 * By using wake_up_state, we ensure the process will wake up and
754 * handle its death signal.
755 */
Oleg Nesterov910ffdb2013-01-21 20:47:41 +0100756 if (!wake_up_state(t, state | TASK_INTERRUPTIBLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 kick_process(t);
758}
759
760/*
761 * Remove signals in mask from the pending set and queue.
762 * Returns 1 if any signals were found.
763 *
764 * All callers must be holding the siglock.
George Anzinger71fabd5e2006-01-08 01:02:48 -0800765 */
Christian Brauner8f113512018-08-21 22:00:38 -0700766static void flush_sigqueue_mask(sigset_t *mask, struct sigpending *s)
George Anzinger71fabd5e2006-01-08 01:02:48 -0800767{
768 struct sigqueue *q, *n;
769 sigset_t m;
770
771 sigandsets(&m, mask, &s->signal);
772 if (sigisemptyset(&m))
Christian Brauner8f113512018-08-21 22:00:38 -0700773 return;
George Anzinger71fabd5e2006-01-08 01:02:48 -0800774
Oleg Nesterov702a5072011-04-27 22:01:27 +0200775 sigandnsets(&s->signal, &s->signal, mask);
George Anzinger71fabd5e2006-01-08 01:02:48 -0800776 list_for_each_entry_safe(q, n, &s->list, list) {
777 if (sigismember(mask, q->info.si_signo)) {
778 list_del_init(&q->list);
779 __sigqueue_free(q);
780 }
781 }
George Anzinger71fabd5e2006-01-08 01:02:48 -0800782}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Eric W. Biedermanae7795b2018-09-25 11:27:20 +0200784static inline int is_si_special(const struct kernel_siginfo *info)
Oleg Nesterov614c5172009-12-15 16:47:22 -0800785{
Eric W. Biederman4ff4c312018-09-03 10:39:04 +0200786 return info <= SEND_SIG_PRIV;
Oleg Nesterov614c5172009-12-15 16:47:22 -0800787}
788
Eric W. Biedermanae7795b2018-09-25 11:27:20 +0200789static inline bool si_fromuser(const struct kernel_siginfo *info)
Oleg Nesterov614c5172009-12-15 16:47:22 -0800790{
791 return info == SEND_SIG_NOINFO ||
792 (!is_si_special(info) && SI_FROMUSER(info));
793}
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795/*
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700796 * called with RCU read lock from check_kill_permission()
797 */
Christian Brauner2a9b9092018-08-21 22:00:11 -0700798static bool kill_ok_by_cred(struct task_struct *t)
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700799{
800 const struct cred *cred = current_cred();
801 const struct cred *tcred = __task_cred(t);
802
Christian Brauner2a9b9092018-08-21 22:00:11 -0700803 return uid_eq(cred->euid, tcred->suid) ||
804 uid_eq(cred->euid, tcred->uid) ||
805 uid_eq(cred->uid, tcred->suid) ||
806 uid_eq(cred->uid, tcred->uid) ||
807 ns_capable(tcred->user_ns, CAP_KILL);
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700808}
809
810/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 * Bad permissions for sending the signal
David Howells694f6902010-08-04 16:59:14 +0100812 * - the caller must hold the RCU read lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +0200814static int check_kill_permission(int sig, struct kernel_siginfo *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 struct task_struct *t)
816{
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700817 struct pid *sid;
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700818 int error;
819
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700820 if (!valid_signal(sig))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700821 return -EINVAL;
822
Oleg Nesterov614c5172009-12-15 16:47:22 -0800823 if (!si_fromuser(info))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700824 return 0;
825
826 error = audit_signal_info(sig, t); /* Let audit system see the signal */
827 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 return error;
Amy Griffise54dc242007-03-29 18:01:04 -0400829
Oleg Nesterov065add32010-05-26 14:42:54 -0700830 if (!same_thread_group(current, t) &&
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700831 !kill_ok_by_cred(t)) {
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700832 switch (sig) {
833 case SIGCONT:
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700834 sid = task_session(t);
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700835 /*
836 * We don't return the error if sid == NULL. The
837 * task was unhashed, the caller must notice this.
838 */
839 if (!sid || sid == task_session(current))
840 break;
841 default:
842 return -EPERM;
843 }
844 }
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100845
Stephen Smalley6b4f3d02017-09-08 12:40:01 -0400846 return security_task_kill(t, info, sig, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
Tejun Heofb1d9102011-06-14 11:20:17 +0200849/**
850 * ptrace_trap_notify - schedule trap to notify ptracer
851 * @t: tracee wanting to notify tracer
852 *
853 * This function schedules sticky ptrace trap which is cleared on the next
854 * TRAP_STOP to notify ptracer of an event. @t must have been seized by
855 * ptracer.
856 *
Tejun Heo544b2c92011-06-14 11:20:18 +0200857 * If @t is running, STOP trap will be taken. If trapped for STOP and
858 * ptracer is listening for events, tracee is woken up so that it can
859 * re-trap for the new event. If trapped otherwise, STOP trap will be
860 * eventually taken without returning to userland after the existing traps
861 * are finished by PTRACE_CONT.
Tejun Heofb1d9102011-06-14 11:20:17 +0200862 *
863 * CONTEXT:
864 * Must be called with @task->sighand->siglock held.
865 */
866static void ptrace_trap_notify(struct task_struct *t)
867{
868 WARN_ON_ONCE(!(t->ptrace & PT_SEIZED));
869 assert_spin_locked(&t->sighand->siglock);
870
871 task_set_jobctl_pending(t, JOBCTL_TRAP_NOTIFY);
Oleg Nesterov910ffdb2013-01-21 20:47:41 +0100872 ptrace_signal_wake_up(t, t->jobctl & JOBCTL_LISTENING);
Tejun Heofb1d9102011-06-14 11:20:17 +0200873}
874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875/*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700876 * Handle magic process-wide effects of stop/continue signals. Unlike
877 * the signal actions, these happen immediately at signal-generation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 * time regardless of blocking, ignoring, or handling. This does the
879 * actual continuing for SIGCONT, but not the actual stopping for stop
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700880 * signals. The process stop is done as a signal action for SIG_DFL.
881 *
882 * Returns true if the signal should be actually delivered, otherwise
883 * it should be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 */
Oleg Nesterov403bad72013-04-30 15:28:10 -0700885static bool prepare_signal(int sig, struct task_struct *p, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700887 struct signal_struct *signal = p->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 struct task_struct *t;
Oleg Nesterov9490592f2014-06-06 14:36:48 -0700889 sigset_t flush;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Oleg Nesterov403bad72013-04-30 15:28:10 -0700891 if (signal->flags & (SIGNAL_GROUP_EXIT | SIGNAL_GROUP_COREDUMP)) {
Oleg Nesterov5fa534c2015-11-06 16:32:31 -0800892 if (!(signal->flags & SIGNAL_GROUP_EXIT))
Oleg Nesterov403bad72013-04-30 15:28:10 -0700893 return sig == SIGKILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 /*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700895 * The process is in the middle of dying, nothing to do.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 */
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700897 } else if (sig_kernel_stop(sig)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 /*
899 * This is a stop signal. Remove SIGCONT from all queues.
900 */
Oleg Nesterov9490592f2014-06-06 14:36:48 -0700901 siginitset(&flush, sigmask(SIGCONT));
Oleg Nesterovc09c1442014-06-06 14:36:50 -0700902 flush_sigqueue_mask(&flush, &signal->shared_pending);
Oleg Nesterov9490592f2014-06-06 14:36:48 -0700903 for_each_thread(p, t)
Oleg Nesterovc09c1442014-06-06 14:36:50 -0700904 flush_sigqueue_mask(&flush, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 } else if (sig == SIGCONT) {
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700906 unsigned int why;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 /*
Oleg Nesterov1deac632011-04-01 20:11:50 +0200908 * Remove all stop signals from all queues, wake all threads.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 */
Oleg Nesterov9490592f2014-06-06 14:36:48 -0700910 siginitset(&flush, SIG_KERNEL_STOP_MASK);
Oleg Nesterovc09c1442014-06-06 14:36:50 -0700911 flush_sigqueue_mask(&flush, &signal->shared_pending);
Oleg Nesterov9490592f2014-06-06 14:36:48 -0700912 for_each_thread(p, t) {
Oleg Nesterovc09c1442014-06-06 14:36:50 -0700913 flush_sigqueue_mask(&flush, &t->pending);
Tejun Heo3759a0d2011-06-02 11:14:00 +0200914 task_clear_jobctl_pending(t, JOBCTL_STOP_PENDING);
Tejun Heofb1d9102011-06-14 11:20:17 +0200915 if (likely(!(t->ptrace & PT_SEIZED)))
916 wake_up_state(t, __TASK_STOPPED);
917 else
918 ptrace_trap_notify(t);
Oleg Nesterov9490592f2014-06-06 14:36:48 -0700919 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700921 /*
922 * Notify the parent with CLD_CONTINUED if we were stopped.
923 *
924 * If we were in the middle of a group stop, we pretend it
925 * was already finished, and then continued. Since SIGCHLD
926 * doesn't queue we report only CLD_STOPPED, as if the next
927 * CLD_CONTINUED was dropped.
928 */
929 why = 0;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700930 if (signal->flags & SIGNAL_STOP_STOPPED)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700931 why |= SIGNAL_CLD_CONTINUED;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700932 else if (signal->group_stop_count)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700933 why |= SIGNAL_CLD_STOPPED;
934
935 if (why) {
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700936 /*
Roland McGrathae6d2ed2009-09-23 15:56:53 -0700937 * The first thread which returns from do_signal_stop()
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700938 * will take ->siglock, notice SIGNAL_CLD_MASK, and
Weikang Shi2e58f572018-10-30 15:07:05 -0700939 * notify its parent. See get_signal().
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700940 */
Jamie Iles2d39b3c2017-01-10 16:57:54 -0800941 signal_set_stop_flags(signal, why | SIGNAL_STOP_CONTINUED);
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700942 signal->group_stop_count = 0;
943 signal->group_exit_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700946
Oleg Nesterovdef8cf72012-03-23 15:02:45 -0700947 return !sig_ignored(p, sig, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700950/*
951 * Test if P wants to take SIG. After we've checked all threads with this,
952 * it's equivalent to finding no threads not blocking SIG. Any threads not
953 * blocking SIG were ruled out because they are not running and already
954 * have pending signals. Such threads will dequeue from the shared queue
955 * as soon as they're available, so putting the signal on the shared queue
956 * will be equivalent to sending it to one such thread.
957 */
Christian Brauneracd14e62018-08-21 22:00:42 -0700958static inline bool wants_signal(int sig, struct task_struct *p)
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700959{
960 if (sigismember(&p->blocked, sig))
Christian Brauneracd14e62018-08-21 22:00:42 -0700961 return false;
962
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700963 if (p->flags & PF_EXITING)
Christian Brauneracd14e62018-08-21 22:00:42 -0700964 return false;
965
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700966 if (sig == SIGKILL)
Christian Brauneracd14e62018-08-21 22:00:42 -0700967 return true;
968
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700969 if (task_is_stopped_or_traced(p))
Christian Brauneracd14e62018-08-21 22:00:42 -0700970 return false;
971
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700972 return task_curr(p) || !signal_pending(p);
973}
974
Eric W. Biederman07296142018-07-13 21:39:13 -0500975static void complete_signal(int sig, struct task_struct *p, enum pid_type type)
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700976{
977 struct signal_struct *signal = p->signal;
978 struct task_struct *t;
979
980 /*
981 * Now find a thread we can wake up to take the signal off the queue.
982 *
983 * If the main thread wants the signal, it gets first crack.
984 * Probably the least surprising to the average bear.
985 */
986 if (wants_signal(sig, p))
987 t = p;
Eric W. Biederman07296142018-07-13 21:39:13 -0500988 else if ((type == PIDTYPE_PID) || thread_group_empty(p))
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700989 /*
990 * There is just one thread and it does not need to be woken.
991 * It will dequeue unblocked signals before it runs again.
992 */
993 return;
994 else {
995 /*
996 * Otherwise try to find a suitable thread.
997 */
998 t = signal->curr_target;
999 while (!wants_signal(sig, t)) {
1000 t = next_thread(t);
1001 if (t == signal->curr_target)
1002 /*
1003 * No thread needs to be woken.
1004 * Any eligible threads will see
1005 * the signal in the queue soon.
1006 */
1007 return;
1008 }
1009 signal->curr_target = t;
1010 }
1011
1012 /*
1013 * Found a killable thread. If the signal will be fatal,
1014 * then start taking the whole group down immediately.
1015 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -07001016 if (sig_fatal(p, sig) &&
Oleg Nesterov42691572017-11-17 15:30:08 -08001017 !(signal->flags & SIGNAL_GROUP_EXIT) &&
Oleg Nesterov71f11dc2008-04-30 00:52:53 -07001018 !sigismember(&t->real_blocked, sig) &&
Oleg Nesterov42691572017-11-17 15:30:08 -08001019 (sig == SIGKILL || !p->ptrace)) {
Oleg Nesterov71f11dc2008-04-30 00:52:53 -07001020 /*
1021 * This signal will be fatal to the whole group.
1022 */
1023 if (!sig_kernel_coredump(sig)) {
1024 /*
1025 * Start a group exit and wake everybody up.
1026 * This way we don't have other threads
1027 * running and doing things after a slower
1028 * thread has the fatal signal pending.
1029 */
1030 signal->flags = SIGNAL_GROUP_EXIT;
1031 signal->group_exit_code = sig;
1032 signal->group_stop_count = 0;
1033 t = p;
1034 do {
Tejun Heo6dfca322011-06-02 11:14:00 +02001035 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
Oleg Nesterov71f11dc2008-04-30 00:52:53 -07001036 sigaddset(&t->pending.signal, SIGKILL);
1037 signal_wake_up(t, 1);
1038 } while_each_thread(p, t);
1039 return;
1040 }
1041 }
1042
1043 /*
1044 * The signal is already in the shared-pending queue.
1045 * Tell the chosen thread to wake up and dequeue it.
1046 */
1047 signal_wake_up(t, sig == SIGKILL);
1048 return;
1049}
1050
Christian Braunera19e2c02018-08-21 22:00:46 -07001051static inline bool legacy_queue(struct sigpending *signals, int sig)
Pavel Emelyanovaf7fff92008-04-30 00:52:34 -07001052{
1053 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
1054}
1055
Serge E. Hallyn6b550f92012-01-10 15:11:37 -08001056#ifdef CONFIG_USER_NS
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001057static inline void userns_fixup_signal_uid(struct kernel_siginfo *info, struct task_struct *t)
Serge E. Hallyn6b550f92012-01-10 15:11:37 -08001058{
1059 if (current_user_ns() == task_cred_xxx(t, user_ns))
1060 return;
1061
1062 if (SI_FROMKERNEL(info))
1063 return;
1064
Eric W. Biederman078de5f2012-02-08 07:00:08 -08001065 rcu_read_lock();
1066 info->si_uid = from_kuid_munged(task_cred_xxx(t, user_ns),
1067 make_kuid(current_user_ns(), info->si_uid));
1068 rcu_read_unlock();
Serge E. Hallyn6b550f92012-01-10 15:11:37 -08001069}
1070#else
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001071static inline void userns_fixup_signal_uid(struct kernel_siginfo *info, struct task_struct *t)
Serge E. Hallyn6b550f92012-01-10 15:11:37 -08001072{
1073 return;
1074}
1075#endif
1076
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001077static int __send_signal(int sig, struct kernel_siginfo *info, struct task_struct *t,
Eric W. Biederman5a883ce2018-07-13 19:26:27 -05001078 enum pid_type type, int from_ancestor_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001080 struct sigpending *pending;
Oleg Nesterov6e65acb2008-04-30 00:52:50 -07001081 struct sigqueue *q;
Vegard Nossum7a0aeb12009-05-16 11:28:33 +02001082 int override_rlimit;
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001083 int ret = 0, result;
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04001084
Oleg Nesterov6e65acb2008-04-30 00:52:50 -07001085 assert_spin_locked(&t->sighand->siglock);
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001086
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001087 result = TRACE_SIGNAL_IGNORED;
Oleg Nesterov629d3622012-03-23 15:02:44 -07001088 if (!prepare_signal(sig, t,
Eric W. Biederman4ff4c312018-09-03 10:39:04 +02001089 from_ancestor_ns || (info == SEND_SIG_PRIV)))
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001090 goto ret;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001091
Eric W. Biederman5a883ce2018-07-13 19:26:27 -05001092 pending = (type != PIDTYPE_PID) ? &t->signal->shared_pending : &t->pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 /*
Pavel Emelyanov2acb0242008-04-30 00:52:35 -07001094 * Short-circuit ignored signals and support queuing
1095 * exactly one non-rt signal, so that we can get more
1096 * detailed information about the cause of the signal.
1097 */
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001098 result = TRACE_SIGNAL_ALREADY_PENDING;
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001099 if (legacy_queue(pending, sig))
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001100 goto ret;
1101
1102 result = TRACE_SIGNAL_DELIVERED;
Davide Libenzifba2afa2007-05-10 22:23:13 -07001103 /*
Eric W. Biedermana6929332019-02-05 07:19:11 -06001104 * Skip useless siginfo allocation for SIGKILL and kernel threads.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 */
Eric W. Biedermana6929332019-02-05 07:19:11 -06001106 if ((sig == SIGKILL) || (t->flags & PF_KTHREAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 goto out_set;
1108
Randy Dunlap5aba0852011-04-04 14:59:31 -07001109 /*
1110 * Real-time signals must be queued if sent by sigqueue, or
1111 * some other real-time mechanism. It is implementation
1112 * defined whether kill() does so. We attempt to do so, on
1113 * the principle of least surprise, but since kill is not
1114 * allowed to fail with EAGAIN when low on memory we just
1115 * make sure at least one signal gets delivered and don't
1116 * pass on the info struct.
1117 */
Vegard Nossum7a0aeb12009-05-16 11:28:33 +02001118 if (sig < SIGRTMIN)
1119 override_rlimit = (is_si_special(info) || info->si_code >= 0);
1120 else
1121 override_rlimit = 0;
1122
Levin, Alexander (Sasha Levin)75f296d2017-11-15 17:35:54 -08001123 q = __sigqueue_alloc(sig, t, GFP_ATOMIC, override_rlimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 if (q) {
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001125 list_add_tail(&q->list, &pending->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 switch ((unsigned long) info) {
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001127 case (unsigned long) SEND_SIG_NOINFO:
Eric W. Biedermanfaf1f222018-01-05 17:27:42 -06001128 clear_siginfo(&q->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 q->info.si_signo = sig;
1130 q->info.si_errno = 0;
1131 q->info.si_code = SI_USER;
Sukadev Bhattiprolu9cd4fd12009-01-06 14:42:46 -08001132 q->info.si_pid = task_tgid_nr_ns(current,
Sukadev Bhattiprolu09bca052009-01-06 14:42:45 -08001133 task_active_pid_ns(t));
Eric W. Biederman078de5f2012-02-08 07:00:08 -08001134 q->info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 break;
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001136 case (unsigned long) SEND_SIG_PRIV:
Eric W. Biedermanfaf1f222018-01-05 17:27:42 -06001137 clear_siginfo(&q->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 q->info.si_signo = sig;
1139 q->info.si_errno = 0;
1140 q->info.si_code = SI_KERNEL;
1141 q->info.si_pid = 0;
1142 q->info.si_uid = 0;
1143 break;
1144 default:
1145 copy_siginfo(&q->info, info);
Sukadev Bhattiprolu6588c1e2009-04-02 16:58:09 -07001146 if (from_ancestor_ns)
1147 q->info.si_pid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 break;
1149 }
Serge E. Hallyn6b550f92012-01-10 15:11:37 -08001150
1151 userns_fixup_signal_uid(&q->info, t);
1152
Oleg Nesterov621d3122005-10-30 15:03:45 -08001153 } else if (!is_si_special(info)) {
Masami Hiramatsuba005e12009-11-24 16:56:58 -05001154 if (sig >= SIGRTMIN && info->si_code != SI_USER) {
1155 /*
1156 * Queue overflow, abort. We may abort if the
1157 * signal was rt and sent by user using something
1158 * other than kill().
1159 */
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001160 result = TRACE_SIGNAL_OVERFLOW_FAIL;
1161 ret = -EAGAIN;
1162 goto ret;
Masami Hiramatsuba005e12009-11-24 16:56:58 -05001163 } else {
1164 /*
1165 * This is a silent loss of information. We still
1166 * send the signal, but the *info bits are lost.
1167 */
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001168 result = TRACE_SIGNAL_LOSE_INFO;
Masami Hiramatsuba005e12009-11-24 16:56:58 -05001169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 }
1171
1172out_set:
Oleg Nesterov53c30332008-04-30 00:53:00 -07001173 signalfd_notify(t, sig);
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001174 sigaddset(&pending->signal, sig);
Eric W. Biedermanc3ad2c32018-07-23 15:20:37 -05001175
1176 /* Let multiprocess signals appear after on-going forks */
1177 if (type > PIDTYPE_TGID) {
1178 struct multiprocess_signals *delayed;
1179 hlist_for_each_entry(delayed, &t->signal->multiprocess, node) {
1180 sigset_t *signal = &delayed->signal;
1181 /* Can't queue both a stop and a continue signal */
1182 if (sig == SIGCONT)
1183 sigdelsetmask(signal, SIG_KERNEL_STOP_MASK);
1184 else if (sig_kernel_stop(sig))
1185 sigdelset(signal, SIGCONT);
1186 sigaddset(signal, sig);
1187 }
1188 }
1189
Eric W. Biederman07296142018-07-13 21:39:13 -05001190 complete_signal(sig, t, type);
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001191ret:
Eric W. Biederman5a883ce2018-07-13 19:26:27 -05001192 trace_signal_generate(sig, info, t, type != PIDTYPE_PID, result);
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001193 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194}
1195
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001196static int send_signal(int sig, struct kernel_siginfo *info, struct task_struct *t,
Eric W. Biedermanb2139842018-07-20 15:49:17 -05001197 enum pid_type type)
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001198{
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001199 int from_ancestor_ns = 0;
1200
1201#ifdef CONFIG_PID_NS
Oleg Nesterovdd342002009-12-15 16:47:24 -08001202 from_ancestor_ns = si_fromuser(info) &&
1203 !task_pid_nr_ns(current, task_active_pid_ns(t));
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001204#endif
1205
Eric W. Biederman5a883ce2018-07-13 19:26:27 -05001206 return __send_signal(sig, info, t, type, from_ancestor_ns);
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001207}
1208
Al Viro4aaefee2012-11-05 13:09:56 -05001209static void print_fatal_signal(int signr)
Ingo Molnar45807a12007-07-15 23:40:10 -07001210{
Al Viro4aaefee2012-11-05 13:09:56 -05001211 struct pt_regs *regs = signal_pt_regs();
Wang Xiaoqiang747800e2016-05-23 16:23:59 -07001212 pr_info("potentially unexpected fatal signal %d.\n", signr);
Ingo Molnar45807a12007-07-15 23:40:10 -07001213
Al Viroca5cd872007-10-29 04:31:16 +00001214#if defined(__i386__) && !defined(__arch_um__)
Wang Xiaoqiang747800e2016-05-23 16:23:59 -07001215 pr_info("code at %08lx: ", regs->ip);
Ingo Molnar45807a12007-07-15 23:40:10 -07001216 {
1217 int i;
1218 for (i = 0; i < 16; i++) {
1219 unsigned char insn;
1220
Andi Kleenb45c6e72010-01-08 14:42:52 -08001221 if (get_user(insn, (unsigned char *)(regs->ip + i)))
1222 break;
Wang Xiaoqiang747800e2016-05-23 16:23:59 -07001223 pr_cont("%02x ", insn);
Ingo Molnar45807a12007-07-15 23:40:10 -07001224 }
1225 }
Wang Xiaoqiang747800e2016-05-23 16:23:59 -07001226 pr_cont("\n");
Ingo Molnar45807a12007-07-15 23:40:10 -07001227#endif
Ed Swierk3a9f84d2009-01-26 15:33:31 -08001228 preempt_disable();
Ingo Molnar45807a12007-07-15 23:40:10 -07001229 show_regs(regs);
Ed Swierk3a9f84d2009-01-26 15:33:31 -08001230 preempt_enable();
Ingo Molnar45807a12007-07-15 23:40:10 -07001231}
1232
1233static int __init setup_print_fatal_signals(char *str)
1234{
1235 get_option (&str, &print_fatal_signals);
1236
1237 return 1;
1238}
1239
1240__setup("print-fatal-signals=", setup_print_fatal_signals);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001242int
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001243__group_send_sig_info(int sig, struct kernel_siginfo *info, struct task_struct *p)
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001244{
Eric W. Biedermanb2139842018-07-20 15:49:17 -05001245 return send_signal(sig, info, p, PIDTYPE_TGID);
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001246}
1247
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001248int do_send_sig_info(int sig, struct kernel_siginfo *info, struct task_struct *p,
Eric W. Biederman40b3b022018-07-21 10:45:15 -05001249 enum pid_type type)
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001250{
1251 unsigned long flags;
1252 int ret = -ESRCH;
1253
1254 if (lock_task_sighand(p, &flags)) {
Eric W. Biedermanb2139842018-07-20 15:49:17 -05001255 ret = send_signal(sig, info, p, type);
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001256 unlock_task_sighand(p, &flags);
1257 }
1258
1259 return ret;
1260}
1261
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262/*
1263 * Force a signal that the process can't ignore: if necessary
1264 * we unblock the signal and change any SIG_IGN to SIG_DFL.
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001265 *
1266 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1267 * since we do not want to have a signal handler that was blocked
1268 * be invoked when user space had explicitly blocked it.
1269 *
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001270 * We don't want to have recursive SIGSEGV's etc, for example,
1271 * that is why we also clear SIGNAL_UNKILLABLE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273int
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001274force_sig_info(int sig, struct kernel_siginfo *info, struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
1276 unsigned long int flags;
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001277 int ret, blocked, ignored;
1278 struct k_sigaction *action;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
1280 spin_lock_irqsave(&t->sighand->siglock, flags);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001281 action = &t->sighand->action[sig-1];
1282 ignored = action->sa.sa_handler == SIG_IGN;
1283 blocked = sigismember(&t->blocked, sig);
1284 if (blocked || ignored) {
1285 action->sa.sa_handler = SIG_DFL;
1286 if (blocked) {
1287 sigdelset(&t->blocked, sig);
Roland McGrath7bb44ad2007-05-23 13:57:44 -07001288 recalc_sigpending_and_wake(t);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
Jamie Ileseb61b592017-08-18 15:16:18 -07001291 /*
1292 * Don't clear SIGNAL_UNKILLABLE for traced tasks, users won't expect
1293 * debugging to leave init killable.
1294 */
1295 if (action->sa.sa_handler == SIG_DFL && !t->ptrace)
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001296 t->signal->flags &= ~SIGNAL_UNKILLABLE;
Eric W. Biedermanb21c5bd2018-07-21 11:34:03 -05001297 ret = send_signal(sig, info, t, PIDTYPE_PID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1299
1300 return ret;
1301}
1302
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303/*
1304 * Nuke all other threads in the group.
1305 */
Oleg Nesterov09faef12010-05-26 14:43:11 -07001306int zap_other_threads(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
Oleg Nesterov09faef12010-05-26 14:43:11 -07001308 struct task_struct *t = p;
1309 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 p->signal->group_stop_count = 0;
1312
Oleg Nesterov09faef12010-05-26 14:43:11 -07001313 while_each_thread(p, t) {
Tejun Heo6dfca322011-06-02 11:14:00 +02001314 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
Oleg Nesterov09faef12010-05-26 14:43:11 -07001315 count++;
1316
1317 /* Don't bother with already dead threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 if (t->exit_state)
1319 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 sigaddset(&t->pending.signal, SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 signal_wake_up(t, 1);
1322 }
Oleg Nesterov09faef12010-05-26 14:43:11 -07001323
1324 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
Namhyung Kimb8ed3742010-10-27 15:34:06 -07001327struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
1328 unsigned long *flags)
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001329{
1330 struct sighand_struct *sighand;
1331
Anna-Maria Gleixner59dc6f32018-05-25 11:05:07 +02001332 rcu_read_lock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001333 for (;;) {
1334 sighand = rcu_dereference(tsk->sighand);
Anna-Maria Gleixner59dc6f32018-05-25 11:05:07 +02001335 if (unlikely(sighand == NULL))
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001336 break;
Anna-Maria Gleixner59dc6f32018-05-25 11:05:07 +02001337
Oleg Nesterov392809b2014-09-28 23:44:18 +02001338 /*
1339 * This sighand can be already freed and even reused, but
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08001340 * we rely on SLAB_TYPESAFE_BY_RCU and sighand_ctor() which
Oleg Nesterov392809b2014-09-28 23:44:18 +02001341 * initializes ->siglock: this slab can't go away, it has
1342 * the same object type, ->siglock can't be reinitialized.
1343 *
1344 * We need to ensure that tsk->sighand is still the same
1345 * after we take the lock, we can race with de_thread() or
1346 * __exit_signal(). In the latter case the next iteration
1347 * must see ->sighand == NULL.
1348 */
Anna-Maria Gleixner59dc6f32018-05-25 11:05:07 +02001349 spin_lock_irqsave(&sighand->siglock, *flags);
1350 if (likely(sighand == tsk->sighand))
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001351 break;
Anna-Maria Gleixner59dc6f32018-05-25 11:05:07 +02001352 spin_unlock_irqrestore(&sighand->siglock, *flags);
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001353 }
Anna-Maria Gleixner59dc6f32018-05-25 11:05:07 +02001354 rcu_read_unlock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001355
1356 return sighand;
1357}
1358
David Howellsc69e8d92008-11-14 10:39:19 +11001359/*
1360 * send signal info to all the members of a group
David Howellsc69e8d92008-11-14 10:39:19 +11001361 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001362int group_send_sig_info(int sig, struct kernel_siginfo *info,
1363 struct task_struct *p, enum pid_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
David Howells694f6902010-08-04 16:59:14 +01001365 int ret;
1366
1367 rcu_read_lock();
1368 ret = check_kill_permission(sig, info, p);
1369 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001371 if (!ret && sig)
Eric W. Biederman40b3b022018-07-21 10:45:15 -05001372 ret = do_send_sig_info(sig, info, p, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
1374 return ret;
1375}
1376
1377/*
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001378 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 * control characters do (^C, ^Z etc)
David Howellsc69e8d92008-11-14 10:39:19 +11001380 * - the caller must hold at least a readlock on tasklist_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001382int __kill_pgrp_info(int sig, struct kernel_siginfo *info, struct pid *pgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383{
1384 struct task_struct *p = NULL;
1385 int retval, success;
1386
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 success = 0;
1388 retval = -ESRCH;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001389 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Eric W. Biederman01024982018-07-13 18:40:57 -05001390 int err = group_send_sig_info(sig, info, p, PIDTYPE_PGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 success |= !err;
1392 retval = err;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001393 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 return success ? 0 : retval;
1395}
1396
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001397int kill_pid_info(int sig, struct kernel_siginfo *info, struct pid *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001399 int error = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 struct task_struct *p;
1401
Paul E. McKenneyeca1a082014-10-23 11:41:22 -07001402 for (;;) {
1403 rcu_read_lock();
1404 p = pid_task(pid, PIDTYPE_PID);
1405 if (p)
Eric W. Biederman01024982018-07-13 18:40:57 -05001406 error = group_send_sig_info(sig, info, p, PIDTYPE_TGID);
Paul E. McKenneyeca1a082014-10-23 11:41:22 -07001407 rcu_read_unlock();
1408 if (likely(!p || error != -ESRCH))
1409 return error;
Oleg Nesterov6ca25b52008-04-30 00:52:45 -07001410
Paul E. McKenneyeca1a082014-10-23 11:41:22 -07001411 /*
1412 * The task was unhashed in between, try again. If it
1413 * is dead, pid_task() will return NULL, if we race with
1414 * de_thread() it will find the new leader.
1415 */
1416 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417}
1418
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001419static int kill_proc_info(int sig, struct kernel_siginfo *info, pid_t pid)
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001420{
1421 int error;
1422 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001423 error = kill_pid_info(sig, info, find_vpid(pid));
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001424 rcu_read_unlock();
1425 return error;
1426}
1427
Christian Braunerbb17fcc2018-08-21 21:59:55 -07001428static inline bool kill_as_cred_perm(const struct cred *cred,
1429 struct task_struct *target)
Serge Hallynd178bc32011-09-26 10:45:18 -05001430{
1431 const struct cred *pcred = __task_cred(target);
Christian Braunerbb17fcc2018-08-21 21:59:55 -07001432
1433 return uid_eq(cred->euid, pcred->suid) ||
1434 uid_eq(cred->euid, pcred->uid) ||
1435 uid_eq(cred->uid, pcred->suid) ||
1436 uid_eq(cred->uid, pcred->uid);
Serge Hallynd178bc32011-09-26 10:45:18 -05001437}
1438
Eric W. Biederman2425c082006-10-02 02:17:28 -07001439/* like kill_pid_info(), but doesn't use uid/euid of "current" */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001440int kill_pid_info_as_cred(int sig, struct kernel_siginfo *info, struct pid *pid,
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04001441 const struct cred *cred)
Harald Welte46113832005-10-10 19:44:29 +02001442{
1443 int ret = -EINVAL;
1444 struct task_struct *p;
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001445 unsigned long flags;
Harald Welte46113832005-10-10 19:44:29 +02001446
1447 if (!valid_signal(sig))
1448 return ret;
1449
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001450 rcu_read_lock();
Eric W. Biederman2425c082006-10-02 02:17:28 -07001451 p = pid_task(pid, PIDTYPE_PID);
Harald Welte46113832005-10-10 19:44:29 +02001452 if (!p) {
1453 ret = -ESRCH;
1454 goto out_unlock;
1455 }
Serge Hallynd178bc32011-09-26 10:45:18 -05001456 if (si_fromuser(info) && !kill_as_cred_perm(cred, p)) {
Harald Welte46113832005-10-10 19:44:29 +02001457 ret = -EPERM;
1458 goto out_unlock;
1459 }
Stephen Smalley6b4f3d02017-09-08 12:40:01 -04001460 ret = security_task_kill(p, info, sig, cred);
David Quigley8f95dc52006-06-30 01:55:47 -07001461 if (ret)
1462 goto out_unlock;
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001463
1464 if (sig) {
1465 if (lock_task_sighand(p, &flags)) {
Eric W. Biederman5a883ce2018-07-13 19:26:27 -05001466 ret = __send_signal(sig, info, p, PIDTYPE_TGID, 0);
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001467 unlock_task_sighand(p, &flags);
1468 } else
1469 ret = -ESRCH;
Harald Welte46113832005-10-10 19:44:29 +02001470 }
1471out_unlock:
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001472 rcu_read_unlock();
Harald Welte46113832005-10-10 19:44:29 +02001473 return ret;
1474}
Serge Hallynd178bc32011-09-26 10:45:18 -05001475EXPORT_SYMBOL_GPL(kill_pid_info_as_cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477/*
1478 * kill_something_info() interprets pid in interesting ways just like kill(2).
1479 *
1480 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1481 * is probably wrong. Should make it like BSD or SYSV.
1482 */
1483
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001484static int kill_something_info(int sig, struct kernel_siginfo *info, pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485{
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001486 int ret;
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001487
1488 if (pid > 0) {
1489 rcu_read_lock();
1490 ret = kill_pid_info(sig, info, find_vpid(pid));
1491 rcu_read_unlock();
1492 return ret;
1493 }
1494
zhongjiang4ea77012017-07-10 15:52:57 -07001495 /* -INT_MIN is undefined. Exclude this case to avoid a UBSAN warning */
1496 if (pid == INT_MIN)
1497 return -ESRCH;
1498
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001499 read_lock(&tasklist_lock);
1500 if (pid != -1) {
1501 ret = __kill_pgrp_info(sig, info,
1502 pid ? find_vpid(-pid) : task_pgrp(current));
1503 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 int retval = 0, count = 0;
1505 struct task_struct * p;
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 for_each_process(p) {
Sukadev Bhattiprolud25141a2008-10-29 14:01:11 -07001508 if (task_pid_vnr(p) > 1 &&
1509 !same_thread_group(p, current)) {
Eric W. Biederman01024982018-07-13 18:40:57 -05001510 int err = group_send_sig_info(sig, info, p,
1511 PIDTYPE_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 ++count;
1513 if (err != -EPERM)
1514 retval = err;
1515 }
1516 }
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001517 ret = count ? retval : -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 }
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001519 read_unlock(&tasklist_lock);
1520
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001521 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522}
1523
1524/*
1525 * These are for backward compatibility with the rest of the kernel source.
1526 */
1527
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001528int send_sig_info(int sig, struct kernel_siginfo *info, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 /*
1531 * Make sure legacy kernel users don't send in bad values
1532 * (normal paths check this in check_kill_permission).
1533 */
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001534 if (!valid_signal(sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return -EINVAL;
1536
Eric W. Biederman40b3b022018-07-21 10:45:15 -05001537 return do_send_sig_info(sig, info, p, PIDTYPE_PID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538}
Eric W. Biedermanfb50f5a2018-09-13 19:26:35 +02001539EXPORT_SYMBOL(send_sig_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001541#define __si_special(priv) \
1542 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544int
1545send_sig(int sig, struct task_struct *p, int priv)
1546{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001547 return send_sig_info(sig, __si_special(priv), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
Eric W. Biedermanfb50f5a2018-09-13 19:26:35 +02001549EXPORT_SYMBOL(send_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
Christian Brauner52cba1a2018-08-21 21:59:51 -07001551void force_sig(int sig, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001553 force_sig_info(sig, SEND_SIG_PRIV, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554}
Eric W. Biedermanfb50f5a2018-09-13 19:26:35 +02001555EXPORT_SYMBOL(force_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
1557/*
1558 * When things go south during signal handling, we
1559 * will force a SIGSEGV. And if the signal that caused
1560 * the problem was already a SIGSEGV, we'll want to
1561 * make sure we don't even try to deliver the signal..
1562 */
Christian Brauner52cba1a2018-08-21 21:59:51 -07001563void force_sigsegv(int sig, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
1565 if (sig == SIGSEGV) {
1566 unsigned long flags;
1567 spin_lock_irqsave(&p->sighand->siglock, flags);
1568 p->sighand->action[sig - 1].sa.sa_handler = SIG_DFL;
1569 spin_unlock_irqrestore(&p->sighand->siglock, flags);
1570 }
1571 force_sig(SIGSEGV, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572}
1573
Eric W. Biedermanf8ec6602018-01-18 14:54:54 -06001574int force_sig_fault(int sig, int code, void __user *addr
1575 ___ARCH_SI_TRAPNO(int trapno)
1576 ___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr)
1577 , struct task_struct *t)
1578{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001579 struct kernel_siginfo info;
Eric W. Biedermanf8ec6602018-01-18 14:54:54 -06001580
1581 clear_siginfo(&info);
1582 info.si_signo = sig;
1583 info.si_errno = 0;
1584 info.si_code = code;
1585 info.si_addr = addr;
1586#ifdef __ARCH_SI_TRAPNO
1587 info.si_trapno = trapno;
1588#endif
1589#ifdef __ia64__
1590 info.si_imm = imm;
1591 info.si_flags = flags;
1592 info.si_isr = isr;
1593#endif
1594 return force_sig_info(info.si_signo, &info, t);
1595}
1596
1597int send_sig_fault(int sig, int code, void __user *addr
1598 ___ARCH_SI_TRAPNO(int trapno)
1599 ___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr)
1600 , struct task_struct *t)
1601{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001602 struct kernel_siginfo info;
Eric W. Biedermanf8ec6602018-01-18 14:54:54 -06001603
1604 clear_siginfo(&info);
1605 info.si_signo = sig;
1606 info.si_errno = 0;
1607 info.si_code = code;
1608 info.si_addr = addr;
1609#ifdef __ARCH_SI_TRAPNO
1610 info.si_trapno = trapno;
1611#endif
1612#ifdef __ia64__
1613 info.si_imm = imm;
1614 info.si_flags = flags;
1615 info.si_isr = isr;
1616#endif
1617 return send_sig_info(info.si_signo, &info, t);
1618}
1619
Eric W. Biederman38246732018-01-18 18:54:31 -06001620int force_sig_mceerr(int code, void __user *addr, short lsb, struct task_struct *t)
1621{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001622 struct kernel_siginfo info;
Eric W. Biederman38246732018-01-18 18:54:31 -06001623
1624 WARN_ON((code != BUS_MCEERR_AO) && (code != BUS_MCEERR_AR));
1625 clear_siginfo(&info);
1626 info.si_signo = SIGBUS;
1627 info.si_errno = 0;
1628 info.si_code = code;
1629 info.si_addr = addr;
1630 info.si_addr_lsb = lsb;
1631 return force_sig_info(info.si_signo, &info, t);
1632}
1633
1634int send_sig_mceerr(int code, void __user *addr, short lsb, struct task_struct *t)
1635{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001636 struct kernel_siginfo info;
Eric W. Biederman38246732018-01-18 18:54:31 -06001637
1638 WARN_ON((code != BUS_MCEERR_AO) && (code != BUS_MCEERR_AR));
1639 clear_siginfo(&info);
1640 info.si_signo = SIGBUS;
1641 info.si_errno = 0;
1642 info.si_code = code;
1643 info.si_addr = addr;
1644 info.si_addr_lsb = lsb;
1645 return send_sig_info(info.si_signo, &info, t);
1646}
1647EXPORT_SYMBOL(send_sig_mceerr);
Eric W. Biederman38246732018-01-18 18:54:31 -06001648
Eric W. Biederman38246732018-01-18 18:54:31 -06001649int force_sig_bnderr(void __user *addr, void __user *lower, void __user *upper)
1650{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001651 struct kernel_siginfo info;
Eric W. Biederman38246732018-01-18 18:54:31 -06001652
1653 clear_siginfo(&info);
1654 info.si_signo = SIGSEGV;
1655 info.si_errno = 0;
1656 info.si_code = SEGV_BNDERR;
1657 info.si_addr = addr;
1658 info.si_lower = lower;
1659 info.si_upper = upper;
1660 return force_sig_info(info.si_signo, &info, current);
1661}
Eric W. Biederman38246732018-01-18 18:54:31 -06001662
1663#ifdef SEGV_PKUERR
1664int force_sig_pkuerr(void __user *addr, u32 pkey)
1665{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001666 struct kernel_siginfo info;
Eric W. Biederman38246732018-01-18 18:54:31 -06001667
1668 clear_siginfo(&info);
1669 info.si_signo = SIGSEGV;
1670 info.si_errno = 0;
1671 info.si_code = SEGV_PKUERR;
1672 info.si_addr = addr;
1673 info.si_pkey = pkey;
1674 return force_sig_info(info.si_signo, &info, current);
1675}
1676#endif
Eric W. Biedermanf8ec6602018-01-18 14:54:54 -06001677
Eric W. Biedermanf71dd7d2018-01-22 14:37:25 -06001678/* For the crazy architectures that include trap information in
1679 * the errno field, instead of an actual errno value.
1680 */
1681int force_sig_ptrace_errno_trap(int errno, void __user *addr)
1682{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001683 struct kernel_siginfo info;
Eric W. Biedermanf71dd7d2018-01-22 14:37:25 -06001684
1685 clear_siginfo(&info);
1686 info.si_signo = SIGTRAP;
1687 info.si_errno = errno;
1688 info.si_code = TRAP_HWBKPT;
1689 info.si_addr = addr;
1690 return force_sig_info(info.si_signo, &info, current);
1691}
1692
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001693int kill_pgrp(struct pid *pid, int sig, int priv)
1694{
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001695 int ret;
1696
1697 read_lock(&tasklist_lock);
1698 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1699 read_unlock(&tasklist_lock);
1700
1701 return ret;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001702}
1703EXPORT_SYMBOL(kill_pgrp);
1704
1705int kill_pid(struct pid *pid, int sig, int priv)
1706{
1707 return kill_pid_info(sig, __si_special(priv), pid);
1708}
1709EXPORT_SYMBOL(kill_pid);
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711/*
1712 * These functions support sending signals using preallocated sigqueue
1713 * structures. This is needed "because realtime applications cannot
1714 * afford to lose notifications of asynchronous events, like timer
Randy Dunlap5aba0852011-04-04 14:59:31 -07001715 * expirations or I/O completions". In the case of POSIX Timers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 * we allocate the sigqueue structure from the timer_create. If this
1717 * allocation fails we are able to report the failure to the application
1718 * with an EAGAIN error.
1719 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720struct sigqueue *sigqueue_alloc(void)
1721{
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001722 struct sigqueue *q = __sigqueue_alloc(-1, current, GFP_KERNEL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001724 if (q)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 q->flags |= SIGQUEUE_PREALLOC;
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +09001726
1727 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
1730void sigqueue_free(struct sigqueue *q)
1731{
1732 unsigned long flags;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001733 spinlock_t *lock = &current->sighand->siglock;
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1736 /*
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001737 * We must hold ->siglock while testing q->list
1738 * to serialize with collect_signal() or with
Oleg Nesterovda7978b2008-05-23 13:04:41 -07001739 * __exit_signal()->flush_sigqueue().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001741 spin_lock_irqsave(lock, flags);
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001742 q->flags &= ~SIGQUEUE_PREALLOC;
1743 /*
1744 * If it is queued it will be freed when dequeued,
1745 * like the "regular" sigqueue.
1746 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001747 if (!list_empty(&q->list))
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001748 q = NULL;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001749 spin_unlock_irqrestore(lock, flags);
1750
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001751 if (q)
1752 __sigqueue_free(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
1754
Eric W. Biederman24122c72018-07-20 14:30:23 -05001755int send_sigqueue(struct sigqueue *q, struct pid *pid, enum pid_type type)
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001756{
Oleg Nesterove62e6652008-04-30 00:52:56 -07001757 int sig = q->info.si_signo;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001758 struct sigpending *pending;
Eric W. Biederman24122c72018-07-20 14:30:23 -05001759 struct task_struct *t;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001760 unsigned long flags;
Oleg Nesterov163566f2011-11-22 21:37:41 +01001761 int ret, result;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001762
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001763 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
Oleg Nesterove62e6652008-04-30 00:52:56 -07001764
1765 ret = -1;
Eric W. Biederman24122c72018-07-20 14:30:23 -05001766 rcu_read_lock();
1767 t = pid_task(pid, type);
1768 if (!t || !likely(lock_task_sighand(t, &flags)))
Oleg Nesterove62e6652008-04-30 00:52:56 -07001769 goto ret;
1770
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001771 ret = 1; /* the signal is ignored */
Oleg Nesterov163566f2011-11-22 21:37:41 +01001772 result = TRACE_SIGNAL_IGNORED;
Oleg Nesterovdef8cf72012-03-23 15:02:45 -07001773 if (!prepare_signal(sig, t, false))
Oleg Nesterove62e6652008-04-30 00:52:56 -07001774 goto out;
1775
1776 ret = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001777 if (unlikely(!list_empty(&q->list))) {
1778 /*
1779 * If an SI_TIMER entry is already queue just increment
1780 * the overrun count.
1781 */
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001782 BUG_ON(q->info.si_code != SI_TIMER);
1783 q->info.si_overrun++;
Oleg Nesterov163566f2011-11-22 21:37:41 +01001784 result = TRACE_SIGNAL_ALREADY_PENDING;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001785 goto out;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001786 }
Oleg Nesterovba661292008-07-23 20:52:05 +04001787 q->info.si_overrun = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001788
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001789 signalfd_notify(t, sig);
Eric W. Biederman24122c72018-07-20 14:30:23 -05001790 pending = (type != PIDTYPE_PID) ? &t->signal->shared_pending : &t->pending;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001791 list_add_tail(&q->list, &pending->list);
1792 sigaddset(&pending->signal, sig);
Eric W. Biederman07296142018-07-13 21:39:13 -05001793 complete_signal(sig, t, type);
Oleg Nesterov163566f2011-11-22 21:37:41 +01001794 result = TRACE_SIGNAL_DELIVERED;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001795out:
Eric W. Biederman24122c72018-07-20 14:30:23 -05001796 trace_signal_generate(sig, &q->info, t, type != PIDTYPE_PID, result);
Oleg Nesterove62e6652008-04-30 00:52:56 -07001797 unlock_task_sighand(t, &flags);
1798ret:
Eric W. Biederman24122c72018-07-20 14:30:23 -05001799 rcu_read_unlock();
Oleg Nesterove62e6652008-04-30 00:52:56 -07001800 return ret;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001801}
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 * Let a parent know about the death of a child.
1805 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001806 *
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001807 * Returns true if our parent ignored us and so we've switched to
1808 * self-reaping.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 */
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001810bool do_notify_parent(struct task_struct *tsk, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001812 struct kernel_siginfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 unsigned long flags;
1814 struct sighand_struct *psig;
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001815 bool autoreap = false;
Frederic Weisbeckerbde82852017-01-31 04:09:31 +01001816 u64 utime, stime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
1818 BUG_ON(sig == -1);
1819
1820 /* do_notify_parent_cldstop should have been called instead. */
Matthew Wilcoxe1abb392007-12-06 11:07:35 -05001821 BUG_ON(task_is_stopped_or_traced(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Tejun Heod21142e2011-06-17 16:50:34 +02001823 BUG_ON(!tsk->ptrace &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
1825
Oleg Nesterovb6e238d2012-03-19 17:03:41 +01001826 if (sig != SIGCHLD) {
1827 /*
1828 * This is only possible if parent == real_parent.
1829 * Check if it has changed security domain.
1830 */
1831 if (tsk->parent_exec_id != tsk->parent->self_exec_id)
1832 sig = SIGCHLD;
1833 }
1834
Eric W. Biedermanfaf1f222018-01-05 17:27:42 -06001835 clear_siginfo(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 info.si_signo = sig;
1837 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001838 /*
Eric W. Biederman32084502012-05-31 16:26:39 -07001839 * We are under tasklist_lock here so our parent is tied to
1840 * us and cannot change.
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001841 *
Eric W. Biederman32084502012-05-31 16:26:39 -07001842 * task_active_pid_ns will always return the same pid namespace
1843 * until a task passes through release_task.
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001844 *
1845 * write_lock() currently calls preempt_disable() which is the
1846 * same as rcu_read_lock(), but according to Oleg, this is not
1847 * correct to rely on this
1848 */
1849 rcu_read_lock();
Eric W. Biederman32084502012-05-31 16:26:39 -07001850 info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(tsk->parent));
Eric W. Biederman54ba47e2012-03-13 16:04:35 -07001851 info.si_uid = from_kuid_munged(task_cred_xxx(tsk->parent, user_ns),
1852 task_uid(tsk));
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001853 rcu_read_unlock();
1854
Frederic Weisbeckerbde82852017-01-31 04:09:31 +01001855 task_cputime(tsk, &utime, &stime);
1856 info.si_utime = nsec_to_clock_t(utime + tsk->signal->utime);
1857 info.si_stime = nsec_to_clock_t(stime + tsk->signal->stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 info.si_status = tsk->exit_code & 0x7f;
1860 if (tsk->exit_code & 0x80)
1861 info.si_code = CLD_DUMPED;
1862 else if (tsk->exit_code & 0x7f)
1863 info.si_code = CLD_KILLED;
1864 else {
1865 info.si_code = CLD_EXITED;
1866 info.si_status = tsk->exit_code >> 8;
1867 }
1868
1869 psig = tsk->parent->sighand;
1870 spin_lock_irqsave(&psig->siglock, flags);
Tejun Heod21142e2011-06-17 16:50:34 +02001871 if (!tsk->ptrace && sig == SIGCHLD &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
1873 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
1874 /*
1875 * We are exiting and our parent doesn't care. POSIX.1
1876 * defines special semantics for setting SIGCHLD to SIG_IGN
1877 * or setting the SA_NOCLDWAIT flag: we should be reaped
1878 * automatically and not left for our parent's wait4 call.
1879 * Rather than having the parent do it as a magic kind of
1880 * signal handler, we just set this to tell do_exit that we
1881 * can be cleaned up without becoming a zombie. Note that
1882 * we still call __wake_up_parent in this case, because a
1883 * blocked sys_wait4 might now return -ECHILD.
1884 *
1885 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
1886 * is implementation-defined: we do (if you don't want
1887 * it, just use SIG_IGN instead).
1888 */
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001889 autoreap = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001891 sig = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 }
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001893 if (valid_signal(sig) && sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 __group_send_sig_info(sig, &info, tsk->parent);
1895 __wake_up_parent(tsk, tsk->parent);
1896 spin_unlock_irqrestore(&psig->siglock, flags);
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07001897
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02001898 return autoreap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
1900
Tejun Heo75b95952011-03-23 10:37:01 +01001901/**
1902 * do_notify_parent_cldstop - notify parent of stopped/continued state change
1903 * @tsk: task reporting the state change
1904 * @for_ptracer: the notification is for ptracer
1905 * @why: CLD_{CONTINUED|STOPPED|TRAPPED} to report
1906 *
1907 * Notify @tsk's parent that the stopped/continued state has changed. If
1908 * @for_ptracer is %false, @tsk's group leader notifies to its real parent.
1909 * If %true, @tsk reports to @tsk->parent which should be the ptracer.
1910 *
1911 * CONTEXT:
1912 * Must be called with tasklist_lock at least read locked.
1913 */
1914static void do_notify_parent_cldstop(struct task_struct *tsk,
1915 bool for_ptracer, int why)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001917 struct kernel_siginfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 unsigned long flags;
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001919 struct task_struct *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 struct sighand_struct *sighand;
Frederic Weisbeckerbde82852017-01-31 04:09:31 +01001921 u64 utime, stime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Tejun Heo75b95952011-03-23 10:37:01 +01001923 if (for_ptracer) {
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001924 parent = tsk->parent;
Tejun Heo75b95952011-03-23 10:37:01 +01001925 } else {
Oleg Nesterovbc505a42005-09-06 15:17:32 -07001926 tsk = tsk->group_leader;
1927 parent = tsk->real_parent;
1928 }
1929
Eric W. Biedermanfaf1f222018-01-05 17:27:42 -06001930 clear_siginfo(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 info.si_signo = SIGCHLD;
1932 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001933 /*
Randy Dunlap5aba0852011-04-04 14:59:31 -07001934 * see comment in do_notify_parent() about the following 4 lines
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001935 */
1936 rcu_read_lock();
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08001937 info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(parent));
Eric W. Biederman54ba47e2012-03-13 16:04:35 -07001938 info.si_uid = from_kuid_munged(task_cred_xxx(parent, user_ns), task_uid(tsk));
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001939 rcu_read_unlock();
1940
Frederic Weisbeckerbde82852017-01-31 04:09:31 +01001941 task_cputime(tsk, &utime, &stime);
1942 info.si_utime = nsec_to_clock_t(utime);
1943 info.si_stime = nsec_to_clock_t(stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
1945 info.si_code = why;
1946 switch (why) {
1947 case CLD_CONTINUED:
1948 info.si_status = SIGCONT;
1949 break;
1950 case CLD_STOPPED:
1951 info.si_status = tsk->signal->group_exit_code & 0x7f;
1952 break;
1953 case CLD_TRAPPED:
1954 info.si_status = tsk->exit_code & 0x7f;
1955 break;
1956 default:
1957 BUG();
1958 }
1959
1960 sighand = parent->sighand;
1961 spin_lock_irqsave(&sighand->siglock, flags);
1962 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
1963 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
1964 __group_send_sig_info(SIGCHLD, &info, parent);
1965 /*
1966 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
1967 */
1968 __wake_up_parent(tsk, parent);
1969 spin_unlock_irqrestore(&sighand->siglock, flags);
1970}
1971
Christian Brauner6527de92018-08-21 21:59:59 -07001972static inline bool may_ptrace_stop(void)
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001973{
Tejun Heod21142e2011-06-17 16:50:34 +02001974 if (!likely(current->ptrace))
Christian Brauner6527de92018-08-21 21:59:59 -07001975 return false;
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001976 /*
1977 * Are we in the middle of do_coredump?
1978 * If so and our tracer is also part of the coredump stopping
1979 * is a deadlock situation, and pointless because our tracer
1980 * is dead so don't allow us to stop.
1981 * If SIGKILL was already sent before the caller unlocked
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001982 * ->siglock we must see ->core_state != NULL. Otherwise it
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001983 * is safe to enter schedule().
Oleg Nesterov9899d112013-01-21 20:48:00 +01001984 *
1985 * This is almost outdated, a task with the pending SIGKILL can't
1986 * block in TASK_TRACED. But PTRACE_EVENT_EXIT can be reported
1987 * after SIGKILL was already dequeued.
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001988 */
Oleg Nesterov999d9fc2008-07-25 01:47:41 -07001989 if (unlikely(current->mm->core_state) &&
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001990 unlikely(current->mm == current->parent->mm))
Christian Brauner6527de92018-08-21 21:59:59 -07001991 return false;
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001992
Christian Brauner6527de92018-08-21 21:59:59 -07001993 return true;
Oleg Nesterovd5f70c02006-06-26 00:26:07 -07001994}
1995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996/*
Randy Dunlap5aba0852011-04-04 14:59:31 -07001997 * Return non-zero if there is a SIGKILL that should be waking us up.
Roland McGrath1a669c22008-02-06 01:37:37 -08001998 * Called with the siglock held.
1999 */
Christian Braunerf99e9d82018-08-21 22:00:50 -07002000static bool sigkill_pending(struct task_struct *tsk)
Roland McGrath1a669c22008-02-06 01:37:37 -08002001{
Christian Braunerf99e9d82018-08-21 22:00:50 -07002002 return sigismember(&tsk->pending.signal, SIGKILL) ||
2003 sigismember(&tsk->signal->shared_pending.signal, SIGKILL);
Roland McGrath1a669c22008-02-06 01:37:37 -08002004}
2005
2006/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 * This must be called with current->sighand->siglock held.
2008 *
2009 * This should be the path for all ptrace stops.
2010 * We always set current->last_siginfo while stopped here.
2011 * That makes it a way to test a stopped process for
2012 * being ptrace-stopped vs being job-control-stopped.
2013 *
Oleg Nesterov20686a32008-02-08 04:19:03 -08002014 * If we actually decide not to stop at all because the tracer
2015 * is gone, we keep current->exit_code unless clear_code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002017static void ptrace_stop(int exit_code, int why, int clear_code, kernel_siginfo_t *info)
Namhyung Kimb8401152010-10-27 15:34:07 -07002018 __releases(&current->sighand->siglock)
2019 __acquires(&current->sighand->siglock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020{
Tejun Heoceb6bd62011-03-23 10:37:01 +01002021 bool gstop_done = false;
2022
Roland McGrath1a669c22008-02-06 01:37:37 -08002023 if (arch_ptrace_stop_needed(exit_code, info)) {
2024 /*
2025 * The arch code has something special to do before a
2026 * ptrace stop. This is allowed to block, e.g. for faults
2027 * on user stack pages. We can't keep the siglock while
2028 * calling arch_ptrace_stop, so we must release it now.
2029 * To preserve proper semantics, we must do this before
2030 * any signal bookkeeping like checking group_stop_count.
2031 * Meanwhile, a SIGKILL could come in before we retake the
2032 * siglock. That must prevent us from sleeping in TASK_TRACED.
2033 * So after regaining the lock, we must check for SIGKILL.
2034 */
2035 spin_unlock_irq(&current->sighand->siglock);
2036 arch_ptrace_stop(exit_code, info);
2037 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07002038 if (sigkill_pending(current))
2039 return;
Roland McGrath1a669c22008-02-06 01:37:37 -08002040 }
2041
Peter Zijlstrab5bf9a92018-04-30 14:51:01 +02002042 set_special_state(TASK_TRACED);
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 /*
Tejun Heo81be24b2011-06-02 11:13:59 +02002045 * We're committing to trapping. TRACED should be visible before
2046 * TRAPPING is cleared; otherwise, the tracer might fail do_wait().
2047 * Also, transition to TRACED and updates to ->jobctl should be
2048 * atomic with respect to siglock and should be done after the arch
2049 * hook as siglock is released and regrabbed across it.
Peter Zijlstrab5bf9a92018-04-30 14:51:01 +02002050 *
2051 * TRACER TRACEE
2052 *
2053 * ptrace_attach()
2054 * [L] wait_on_bit(JOBCTL_TRAPPING) [S] set_special_state(TRACED)
2055 * do_wait()
2056 * set_current_state() smp_wmb();
2057 * ptrace_do_wait()
2058 * wait_task_stopped()
2059 * task_stopped_code()
2060 * [L] task_is_traced() [S] task_clear_jobctl_trapping();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 */
Peter Zijlstrab5bf9a92018-04-30 14:51:01 +02002062 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 current->last_siginfo = info;
2065 current->exit_code = exit_code;
2066
Tejun Heod79fdd62011-03-23 10:37:00 +01002067 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 * If @why is CLD_STOPPED, we're trapping to participate in a group
2069 * stop. Do the bookkeeping. Note that if SIGCONT was delievered
Tejun Heo73ddff22011-06-14 11:20:14 +02002070 * across siglock relocks since INTERRUPT was scheduled, PENDING
2071 * could be clear now. We act as if SIGCONT is received after
2072 * TASK_TRACED is entered - ignore it.
Tejun Heod79fdd62011-03-23 10:37:00 +01002073 */
Tejun Heoa8f072c2011-06-02 11:13:59 +02002074 if (why == CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 gstop_done = task_participate_group_stop(current);
Tejun Heod79fdd62011-03-23 10:37:00 +01002076
Tejun Heofb1d9102011-06-14 11:20:17 +02002077 /* any trap clears pending STOP trap, STOP trap clears NOTIFY */
Tejun Heo73ddff22011-06-14 11:20:14 +02002078 task_clear_jobctl_pending(current, JOBCTL_TRAP_STOP);
Tejun Heofb1d9102011-06-14 11:20:17 +02002079 if (info && info->si_code >> 8 == PTRACE_EVENT_STOP)
2080 task_clear_jobctl_pending(current, JOBCTL_TRAP_NOTIFY);
Tejun Heo73ddff22011-06-14 11:20:14 +02002081
Tejun Heo81be24b2011-06-02 11:13:59 +02002082 /* entering a trap, clear TRAPPING */
Tejun Heoa8f072c2011-06-02 11:13:59 +02002083 task_clear_jobctl_trapping(current);
Tejun Heod79fdd62011-03-23 10:37:00 +01002084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 spin_unlock_irq(&current->sighand->siglock);
2086 read_lock(&tasklist_lock);
Oleg Nesterov3d749b92008-07-25 01:47:37 -07002087 if (may_ptrace_stop()) {
Tejun Heoceb6bd62011-03-23 10:37:01 +01002088 /*
2089 * Notify parents of the stop.
2090 *
2091 * While ptraced, there are two parents - the ptracer and
2092 * the real_parent of the group_leader. The ptracer should
2093 * know about every stop while the real parent is only
2094 * interested in the completion of group stop. The states
2095 * for the two don't interact with each other. Notify
2096 * separately unless they're gonna be duplicates.
2097 */
2098 do_notify_parent_cldstop(current, true, why);
Oleg Nesterovbb3696d2011-06-24 17:34:23 +02002099 if (gstop_done && ptrace_reparented(current))
Tejun Heoceb6bd62011-03-23 10:37:01 +01002100 do_notify_parent_cldstop(current, false, why);
2101
Miklos Szeredi53da1d92009-03-23 16:07:24 +01002102 /*
2103 * Don't want to allow preemption here, because
2104 * sys_ptrace() needs this task to be inactive.
2105 *
2106 * XXX: implement read_unlock_no_resched().
2107 */
2108 preempt_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 read_unlock(&tasklist_lock);
Miklos Szeredi53da1d92009-03-23 16:07:24 +01002110 preempt_enable_no_resched();
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +02002111 freezable_schedule();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 } else {
2113 /*
2114 * By the time we got the lock, our tracer went away.
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08002115 * Don't drop the lock yet, another tracer may come.
Tejun Heoceb6bd62011-03-23 10:37:01 +01002116 *
2117 * If @gstop_done, the ptracer went away between group stop
2118 * completion and here. During detach, it would have set
Tejun Heoa8f072c2011-06-02 11:13:59 +02002119 * JOBCTL_STOP_PENDING on us and we'll re-enter
2120 * TASK_STOPPED in do_signal_stop() on return, so notifying
2121 * the real parent of the group stop completion is enough.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 */
Tejun Heoceb6bd62011-03-23 10:37:01 +01002123 if (gstop_done)
2124 do_notify_parent_cldstop(current, false, why);
2125
Oleg Nesterov9899d112013-01-21 20:48:00 +01002126 /* tasklist protects us from ptrace_freeze_traced() */
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08002127 __set_current_state(TASK_RUNNING);
Oleg Nesterov20686a32008-02-08 04:19:03 -08002128 if (clear_code)
2129 current->exit_code = 0;
Oleg Nesterov6405f7f2008-02-08 04:19:00 -08002130 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 }
2132
2133 /*
2134 * We are back. Now reacquire the siglock before touching
2135 * last_siginfo, so that we are sure to have synchronized with
2136 * any signal-sending on another CPU that wants to examine it.
2137 */
2138 spin_lock_irq(&current->sighand->siglock);
2139 current->last_siginfo = NULL;
2140
Tejun Heo544b2c92011-06-14 11:20:18 +02002141 /* LISTENING can be set only during STOP traps, clear it */
2142 current->jobctl &= ~JOBCTL_LISTENING;
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 /*
2145 * Queued signals ignored us while we were stopped for tracing.
2146 * So check for any that we should take before resuming user mode.
Roland McGrathb74d0de2007-06-06 03:59:00 -07002147 * This sets TIF_SIGPENDING, but never clears it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 */
Roland McGrathb74d0de2007-06-06 03:59:00 -07002149 recalc_sigpending_tsk(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150}
2151
Tejun Heo3544d722011-06-14 11:20:15 +02002152static void ptrace_do_notify(int signr, int exit_code, int why)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002154 kernel_siginfo_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Eric W. Biedermanfaf1f222018-01-05 17:27:42 -06002156 clear_siginfo(&info);
Tejun Heo3544d722011-06-14 11:20:15 +02002157 info.si_signo = signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 info.si_code = exit_code;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002159 info.si_pid = task_pid_vnr(current);
Eric W. Biederman078de5f2012-02-08 07:00:08 -08002160 info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 /* Let the debugger run. */
Tejun Heo3544d722011-06-14 11:20:15 +02002163 ptrace_stop(exit_code, why, 1, &info);
2164}
2165
2166void ptrace_notify(int exit_code)
2167{
2168 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
Oleg Nesterovf784e8a2012-08-26 21:12:17 +02002169 if (unlikely(current->task_works))
2170 task_work_run();
Tejun Heo3544d722011-06-14 11:20:15 +02002171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 spin_lock_irq(&current->sighand->siglock);
Tejun Heo3544d722011-06-14 11:20:15 +02002173 ptrace_do_notify(SIGTRAP, exit_code, CLD_TRAPPED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 spin_unlock_irq(&current->sighand->siglock);
2175}
2176
Tejun Heo73ddff22011-06-14 11:20:14 +02002177/**
2178 * do_signal_stop - handle group stop for SIGSTOP and other stop signals
2179 * @signr: signr causing group stop if initiating
2180 *
2181 * If %JOBCTL_STOP_PENDING is not set yet, initiate group stop with @signr
2182 * and participate in it. If already set, participate in the existing
2183 * group stop. If participated in a group stop (and thus slept), %true is
2184 * returned with siglock released.
2185 *
2186 * If ptraced, this function doesn't handle stop itself. Instead,
2187 * %JOBCTL_TRAP_STOP is scheduled and %false is returned with siglock
2188 * untouched. The caller must ensure that INTERRUPT trap handling takes
2189 * places afterwards.
2190 *
2191 * CONTEXT:
2192 * Must be called with @current->sighand->siglock held, which is released
2193 * on %true return.
2194 *
2195 * RETURNS:
2196 * %false if group stop is already cancelled or ptrace trap is scheduled.
2197 * %true if participated in group stop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 */
Tejun Heo73ddff22011-06-14 11:20:14 +02002199static bool do_signal_stop(int signr)
2200 __releases(&current->sighand->siglock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
2202 struct signal_struct *sig = current->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Tejun Heoa8f072c2011-06-02 11:13:59 +02002204 if (!(current->jobctl & JOBCTL_STOP_PENDING)) {
Palmer Dabbeltb76808e2015-04-30 21:19:57 -07002205 unsigned long gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08002206 struct task_struct *t;
2207
Tejun Heoa8f072c2011-06-02 11:13:59 +02002208 /* signr will be recorded in task->jobctl for retries */
2209 WARN_ON_ONCE(signr & ~JOBCTL_STOP_SIGMASK);
Tejun Heod79fdd62011-03-23 10:37:00 +01002210
Tejun Heoa8f072c2011-06-02 11:13:59 +02002211 if (!likely(current->jobctl & JOBCTL_STOP_DEQUEUED) ||
Oleg Nesterov573cf9a2008-04-30 00:52:36 -07002212 unlikely(signal_group_exit(sig)))
Tejun Heo73ddff22011-06-14 11:20:14 +02002213 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 /*
Tejun Heo408a37d2011-03-23 10:37:01 +01002215 * There is no group stop already in progress. We must
2216 * initiate one now.
2217 *
2218 * While ptraced, a task may be resumed while group stop is
2219 * still in effect and then receive a stop signal and
2220 * initiate another group stop. This deviates from the
2221 * usual behavior as two consecutive stop signals can't
Oleg Nesterov780006eac2011-04-01 20:12:16 +02002222 * cause two group stops when !ptraced. That is why we
2223 * also check !task_is_stopped(t) below.
Tejun Heo408a37d2011-03-23 10:37:01 +01002224 *
2225 * The condition can be distinguished by testing whether
2226 * SIGNAL_STOP_STOPPED is already set. Don't generate
2227 * group_exit_code in such case.
2228 *
2229 * This is not necessary for SIGNAL_STOP_CONTINUED because
2230 * an intervening stop signal is required to cause two
2231 * continued events regardless of ptrace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 */
Tejun Heo408a37d2011-03-23 10:37:01 +01002233 if (!(sig->flags & SIGNAL_STOP_STOPPED))
2234 sig->group_exit_code = signr;
Oleg Nesterova122b342006-03-28 16:11:22 -08002235
Tejun Heo7dd3db52011-06-02 11:14:00 +02002236 sig->group_stop_count = 0;
2237
2238 if (task_set_jobctl_pending(current, signr | gstop))
2239 sig->group_stop_count++;
2240
Oleg Nesterov8d38f202014-01-23 15:55:56 -08002241 t = current;
2242 while_each_thread(current, t) {
Oleg Nesterova122b342006-03-28 16:11:22 -08002243 /*
2244 * Setting state to TASK_STOPPED for a group
2245 * stop is always done with the siglock held,
2246 * so this check has no races.
2247 */
Tejun Heo7dd3db52011-06-02 11:14:00 +02002248 if (!task_is_stopped(t) &&
2249 task_set_jobctl_pending(t, signr | gstop)) {
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002250 sig->group_stop_count++;
Tejun Heofb1d9102011-06-14 11:20:17 +02002251 if (likely(!(t->ptrace & PT_SEIZED)))
2252 signal_wake_up(t, 0);
2253 else
2254 ptrace_trap_notify(t);
Oleg Nesterova122b342006-03-28 16:11:22 -08002255 }
Tejun Heod79fdd62011-03-23 10:37:00 +01002256 }
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002257 }
Tejun Heo73ddff22011-06-14 11:20:14 +02002258
Tejun Heod21142e2011-06-17 16:50:34 +02002259 if (likely(!current->ptrace)) {
Tejun Heo5224fa32011-03-23 10:37:00 +01002260 int notify = 0;
2261
2262 /*
2263 * If there are no other threads in the group, or if there
2264 * is a group stop in progress and we are the last to stop,
2265 * report to the parent.
2266 */
2267 if (task_participate_group_stop(current))
2268 notify = CLD_STOPPED;
2269
Peter Zijlstrab5bf9a92018-04-30 14:51:01 +02002270 set_special_state(TASK_STOPPED);
Tejun Heo5224fa32011-03-23 10:37:00 +01002271 spin_unlock_irq(&current->sighand->siglock);
2272
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002273 /*
2274 * Notify the parent of the group stop completion. Because
2275 * we're not holding either the siglock or tasklist_lock
2276 * here, ptracer may attach inbetween; however, this is for
2277 * group stop and should always be delivered to the real
2278 * parent of the group leader. The new ptracer will get
2279 * its notification when this task transitions into
2280 * TASK_TRACED.
2281 */
Tejun Heo5224fa32011-03-23 10:37:00 +01002282 if (notify) {
2283 read_lock(&tasklist_lock);
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002284 do_notify_parent_cldstop(current, false, notify);
Tejun Heo5224fa32011-03-23 10:37:00 +01002285 read_unlock(&tasklist_lock);
2286 }
2287
2288 /* Now we don't run again until woken by SIGCONT or SIGKILL */
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +02002289 freezable_schedule();
Tejun Heo73ddff22011-06-14 11:20:14 +02002290 return true;
Tejun Heod79fdd62011-03-23 10:37:00 +01002291 } else {
Tejun Heo73ddff22011-06-14 11:20:14 +02002292 /*
2293 * While ptraced, group stop is handled by STOP trap.
2294 * Schedule it and let the caller deal with it.
2295 */
2296 task_set_jobctl_pending(current, JOBCTL_TRAP_STOP);
2297 return false;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002298 }
Tejun Heo73ddff22011-06-14 11:20:14 +02002299}
Tejun Heod79fdd62011-03-23 10:37:00 +01002300
Tejun Heo73ddff22011-06-14 11:20:14 +02002301/**
2302 * do_jobctl_trap - take care of ptrace jobctl traps
2303 *
Tejun Heo3544d722011-06-14 11:20:15 +02002304 * When PT_SEIZED, it's used for both group stop and explicit
2305 * SEIZE/INTERRUPT traps. Both generate PTRACE_EVENT_STOP trap with
2306 * accompanying siginfo. If stopped, lower eight bits of exit_code contain
2307 * the stop signal; otherwise, %SIGTRAP.
2308 *
2309 * When !PT_SEIZED, it's used only for group stop trap with stop signal
2310 * number as exit_code and no siginfo.
Tejun Heo73ddff22011-06-14 11:20:14 +02002311 *
2312 * CONTEXT:
2313 * Must be called with @current->sighand->siglock held, which may be
2314 * released and re-acquired before returning with intervening sleep.
2315 */
2316static void do_jobctl_trap(void)
2317{
Tejun Heo3544d722011-06-14 11:20:15 +02002318 struct signal_struct *signal = current->signal;
Tejun Heo73ddff22011-06-14 11:20:14 +02002319 int signr = current->jobctl & JOBCTL_STOP_SIGMASK;
Tejun Heod79fdd62011-03-23 10:37:00 +01002320
Tejun Heo3544d722011-06-14 11:20:15 +02002321 if (current->ptrace & PT_SEIZED) {
2322 if (!signal->group_stop_count &&
2323 !(signal->flags & SIGNAL_STOP_STOPPED))
2324 signr = SIGTRAP;
2325 WARN_ON_ONCE(!signr);
2326 ptrace_do_notify(signr, signr | (PTRACE_EVENT_STOP << 8),
2327 CLD_STOPPED);
2328 } else {
2329 WARN_ON_ONCE(!signr);
2330 ptrace_stop(signr, CLD_STOPPED, 0, NULL);
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002331 current->exit_code = 0;
2332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333}
2334
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002335static int ptrace_signal(int signr, kernel_siginfo_t *info)
Roland McGrath18c98b62008-04-17 18:44:38 -07002336{
Oleg Nesterov8a352412011-07-21 17:06:53 +02002337 /*
2338 * We do not check sig_kernel_stop(signr) but set this marker
2339 * unconditionally because we do not know whether debugger will
2340 * change signr. This flag has no meaning unless we are going
2341 * to stop after return from ptrace_stop(). In this case it will
2342 * be checked in do_signal_stop(), we should only stop if it was
2343 * not cleared by SIGCONT while we were sleeping. See also the
2344 * comment in dequeue_signal().
2345 */
2346 current->jobctl |= JOBCTL_STOP_DEQUEUED;
Tejun Heofe1bc6a2011-03-23 10:37:00 +01002347 ptrace_stop(signr, CLD_TRAPPED, 0, info);
Roland McGrath18c98b62008-04-17 18:44:38 -07002348
2349 /* We're back. Did the debugger cancel the sig? */
2350 signr = current->exit_code;
2351 if (signr == 0)
2352 return signr;
2353
2354 current->exit_code = 0;
2355
Randy Dunlap5aba0852011-04-04 14:59:31 -07002356 /*
2357 * Update the siginfo structure if the signal has
2358 * changed. If the debugger wanted something
2359 * specific in the siginfo structure then it should
2360 * have updated *info via PTRACE_SETSIGINFO.
2361 */
Roland McGrath18c98b62008-04-17 18:44:38 -07002362 if (signr != info->si_signo) {
Eric W. Biedermanfaf1f222018-01-05 17:27:42 -06002363 clear_siginfo(info);
Roland McGrath18c98b62008-04-17 18:44:38 -07002364 info->si_signo = signr;
2365 info->si_errno = 0;
2366 info->si_code = SI_USER;
Serge E. Hallyn6b550f92012-01-10 15:11:37 -08002367 rcu_read_lock();
Roland McGrath18c98b62008-04-17 18:44:38 -07002368 info->si_pid = task_pid_vnr(current->parent);
Eric W. Biederman54ba47e2012-03-13 16:04:35 -07002369 info->si_uid = from_kuid_munged(current_user_ns(),
2370 task_uid(current->parent));
Serge E. Hallyn6b550f92012-01-10 15:11:37 -08002371 rcu_read_unlock();
Roland McGrath18c98b62008-04-17 18:44:38 -07002372 }
2373
2374 /* If the (new) signal is now blocked, requeue it. */
2375 if (sigismember(&current->blocked, signr)) {
Eric W. Biedermanb21c5bd2018-07-21 11:34:03 -05002376 send_signal(signr, info, current, PIDTYPE_PID);
Roland McGrath18c98b62008-04-17 18:44:38 -07002377 signr = 0;
2378 }
2379
2380 return signr;
2381}
2382
Christian Brauner20ab7212018-08-21 22:00:54 -07002383bool get_signal(struct ksignal *ksig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384{
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002385 struct sighand_struct *sighand = current->sighand;
2386 struct signal_struct *signal = current->signal;
2387 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
Oleg Nesterovf784e8a2012-08-26 21:12:17 +02002389 if (unlikely(current->task_works))
2390 task_work_run();
Al Viro72667022012-07-15 14:10:52 +04002391
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302392 if (unlikely(uprobe_deny_signal()))
Christian Brauner20ab7212018-08-21 22:00:54 -07002393 return false;
Srikar Dronamraju0326f5a2012-03-13 23:30:11 +05302394
Roland McGrath13b1c3d2008-03-03 20:22:05 -08002395 /*
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +02002396 * Do this once, we can't return to user-mode if freezing() == T.
2397 * do_signal_stop() and ptrace_stop() do freezable_schedule() and
2398 * thus do not need another check after return.
Roland McGrath13b1c3d2008-03-03 20:22:05 -08002399 */
Rafael J. Wysockifc558a72006-03-23 03:00:05 -08002400 try_to_freeze();
2401
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +02002402relock:
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002403 spin_lock_irq(&sighand->siglock);
Oleg Nesterov021e1ae2008-04-30 00:53:00 -07002404 /*
2405 * Every stopped thread goes here after wakeup. Check to see if
2406 * we should notify the parent, prepare_signal(SIGCONT) encodes
2407 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
2408 */
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002409 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
Tejun Heoc672af32011-03-23 10:36:59 +01002410 int why;
2411
2412 if (signal->flags & SIGNAL_CLD_CONTINUED)
2413 why = CLD_CONTINUED;
2414 else
2415 why = CLD_STOPPED;
2416
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002417 signal->flags &= ~SIGNAL_CLD_MASK;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002418
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002419 spin_unlock_irq(&sighand->siglock);
Oleg Nesterove4420552008-04-30 00:52:44 -07002420
Tejun Heoceb6bd62011-03-23 10:37:01 +01002421 /*
2422 * Notify the parent that we're continuing. This event is
2423 * always per-process and doesn't make whole lot of sense
2424 * for ptracers, who shouldn't consume the state via
2425 * wait(2) either, but, for backward compatibility, notify
2426 * the ptracer of the group leader too unless it's gonna be
2427 * a duplicate.
2428 */
Tejun Heoedf2ed12011-03-23 10:37:00 +01002429 read_lock(&tasklist_lock);
Tejun Heoceb6bd62011-03-23 10:37:01 +01002430 do_notify_parent_cldstop(current, false, why);
2431
Oleg Nesterovbb3696d2011-06-24 17:34:23 +02002432 if (ptrace_reparented(current->group_leader))
2433 do_notify_parent_cldstop(current->group_leader,
2434 true, why);
Tejun Heoedf2ed12011-03-23 10:37:00 +01002435 read_unlock(&tasklist_lock);
Tejun Heoceb6bd62011-03-23 10:37:01 +01002436
Oleg Nesterove4420552008-04-30 00:52:44 -07002437 goto relock;
2438 }
2439
Eric W. Biederman35634ff2019-02-06 18:39:40 -06002440 /* Has this task already been marked for death? */
Eric W. Biedermancf43a752019-02-11 23:27:42 -06002441 if (signal_group_exit(signal)) {
2442 ksig->info.si_signo = signr = SIGKILL;
2443 sigdelset(&current->pending.signal, SIGKILL);
2444 recalc_sigpending();
Eric W. Biederman35634ff2019-02-06 18:39:40 -06002445 goto fatal;
Eric W. Biedermancf43a752019-02-11 23:27:42 -06002446 }
Eric W. Biederman35634ff2019-02-06 18:39:40 -06002447
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 for (;;) {
2449 struct k_sigaction *ka;
Tejun Heodd1d6772011-06-02 11:14:00 +02002450
2451 if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
2452 do_signal_stop(0))
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002453 goto relock;
Oleg Nesterov1be53962009-12-15 16:47:26 -08002454
Tejun Heo73ddff22011-06-14 11:20:14 +02002455 if (unlikely(current->jobctl & JOBCTL_TRAP_MASK)) {
2456 do_jobctl_trap();
2457 spin_unlock_irq(&sighand->siglock);
2458 goto relock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 }
2460
Eric W. Biederman7146db32019-02-06 17:51:47 -06002461 /*
2462 * Signals generated by the execution of an instruction
2463 * need to be delivered before any other pending signals
2464 * so that the instruction pointer in the signal stack
2465 * frame points to the faulting instruction.
2466 */
2467 signr = dequeue_synchronous_signal(&ksig->info);
2468 if (!signr)
2469 signr = dequeue_signal(current, &current->blocked, &ksig->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470
Tejun Heodd1d6772011-06-02 11:14:00 +02002471 if (!signr)
2472 break; /* will return 0 */
2473
Oleg Nesterov8a352412011-07-21 17:06:53 +02002474 if (unlikely(current->ptrace) && signr != SIGKILL) {
Richard Weinberger828b1f62013-10-07 15:26:57 +02002475 signr = ptrace_signal(signr, &ksig->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 if (!signr)
Tejun Heodd1d6772011-06-02 11:14:00 +02002477 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 }
2479
Tejun Heodd1d6772011-06-02 11:14:00 +02002480 ka = &sighand->action[signr-1];
2481
Masami Hiramatsuf9d42572009-11-24 16:56:51 -05002482 /* Trace actually delivered signals. */
Richard Weinberger828b1f62013-10-07 15:26:57 +02002483 trace_signal_deliver(signr, &ksig->info, ka);
Masami Hiramatsuf9d42572009-11-24 16:56:51 -05002484
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
2486 continue;
2487 if (ka->sa.sa_handler != SIG_DFL) {
2488 /* Run the handler. */
Richard Weinberger828b1f62013-10-07 15:26:57 +02002489 ksig->ka = *ka;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
2491 if (ka->sa.sa_flags & SA_ONESHOT)
2492 ka->sa.sa_handler = SIG_DFL;
2493
2494 break; /* will return non-zero "signr" value */
2495 }
2496
2497 /*
2498 * Now we are doing the default action for this signal.
2499 */
2500 if (sig_kernel_ignore(signr)) /* Default is nothing. */
2501 continue;
2502
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08002503 /*
Sukadev Bhattiprolu0fbc26a2007-10-18 23:40:13 -07002504 * Global init gets no signals it doesn't want.
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07002505 * Container-init gets no signals it doesn't want from same
2506 * container.
2507 *
2508 * Note that if global/container-init sees a sig_kernel_only()
2509 * signal here, the signal must have been generated internally
2510 * or must have come from an ancestor namespace. In either
2511 * case, the signal cannot be dropped.
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08002512 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -07002513 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07002514 !sig_kernel_only(signr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 continue;
2516
2517 if (sig_kernel_stop(signr)) {
2518 /*
2519 * The default action is to stop all threads in
2520 * the thread group. The job control signals
2521 * do nothing in an orphaned pgrp, but SIGSTOP
2522 * always works. Note that siglock needs to be
2523 * dropped during the call to is_orphaned_pgrp()
2524 * because of lock ordering with tasklist_lock.
2525 * This allows an intervening SIGCONT to be posted.
2526 * We need to check for that and bail out if necessary.
2527 */
2528 if (signr != SIGSTOP) {
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002529 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
2531 /* signals can be posted during this window */
2532
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08002533 if (is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 goto relock;
2535
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002536 spin_lock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 }
2538
Richard Weinberger828b1f62013-10-07 15:26:57 +02002539 if (likely(do_signal_stop(ksig->info.si_signo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 /* It released the siglock. */
2541 goto relock;
2542 }
2543
2544 /*
2545 * We didn't actually stop, due to a race
2546 * with SIGCONT or something like that.
2547 */
2548 continue;
2549 }
2550
Eric W. Biederman35634ff2019-02-06 18:39:40 -06002551 fatal:
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002552 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
2554 /*
2555 * Anything else is fatal, maybe with a core dump.
2556 */
2557 current->flags |= PF_SIGNALED;
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07002558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 if (sig_kernel_coredump(signr)) {
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07002560 if (print_fatal_signals)
Richard Weinberger828b1f62013-10-07 15:26:57 +02002561 print_fatal_signal(ksig->info.si_signo);
Jesper Derehag2b5faa42013-03-19 20:50:05 +00002562 proc_coredump_connector(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 /*
2564 * If it was able to dump core, this kills all
2565 * other threads in the group and synchronizes with
2566 * their demise. If we lost the race with another
2567 * thread getting here, it set group_exit_code
2568 * first and our do_group_exit call below will use
2569 * that value and ignore the one we pass it.
2570 */
Richard Weinberger828b1f62013-10-07 15:26:57 +02002571 do_coredump(&ksig->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 }
2573
2574 /*
2575 * Death signals, no core dump.
2576 */
Richard Weinberger828b1f62013-10-07 15:26:57 +02002577 do_group_exit(ksig->info.si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 /* NOTREACHED */
2579 }
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002580 spin_unlock_irq(&sighand->siglock);
Richard Weinberger828b1f62013-10-07 15:26:57 +02002581
2582 ksig->sig = signr;
2583 return ksig->sig > 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
Matt Fleming5e6292c2012-01-10 15:11:17 -08002586/**
Al Viroefee9842012-04-28 02:04:15 -04002587 * signal_delivered -
Richard Weinberger10b1c7a2014-07-13 13:36:04 +02002588 * @ksig: kernel signal struct
Al Viroefee9842012-04-28 02:04:15 -04002589 * @stepping: nonzero if debugger single-step or block-step in use
Matt Fleming5e6292c2012-01-10 15:11:17 -08002590 *
Masanari Iidae2278672014-02-18 22:54:36 +09002591 * This function should be called when a signal has successfully been
Richard Weinberger10b1c7a2014-07-13 13:36:04 +02002592 * delivered. It updates the blocked signals accordingly (@ksig->ka.sa.sa_mask
Al Viroefee9842012-04-28 02:04:15 -04002593 * is always blocked, and the signal itself is blocked unless %SA_NODEFER
Richard Weinberger10b1c7a2014-07-13 13:36:04 +02002594 * is set in @ksig->ka.sa.sa_flags. Tracing is notified.
Matt Fleming5e6292c2012-01-10 15:11:17 -08002595 */
Richard Weinberger10b1c7a2014-07-13 13:36:04 +02002596static void signal_delivered(struct ksignal *ksig, int stepping)
Matt Fleming5e6292c2012-01-10 15:11:17 -08002597{
2598 sigset_t blocked;
2599
Al Viroa610d6e2012-05-21 23:42:15 -04002600 /* A signal was successfully delivered, and the
2601 saved sigmask was stored on the signal frame,
2602 and will be restored by sigreturn. So we can
2603 simply clear the restore sigmask flag. */
2604 clear_restore_sigmask();
2605
Richard Weinberger10b1c7a2014-07-13 13:36:04 +02002606 sigorsets(&blocked, &current->blocked, &ksig->ka.sa.sa_mask);
2607 if (!(ksig->ka.sa.sa_flags & SA_NODEFER))
2608 sigaddset(&blocked, ksig->sig);
Matt Fleming5e6292c2012-01-10 15:11:17 -08002609 set_current_blocked(&blocked);
Richard Weinbergerdf5601f2013-10-07 15:37:19 +02002610 tracehook_signal_handler(stepping);
Matt Fleming5e6292c2012-01-10 15:11:17 -08002611}
2612
Al Viro2ce5da12012-11-07 15:11:25 -05002613void signal_setup_done(int failed, struct ksignal *ksig, int stepping)
2614{
2615 if (failed)
2616 force_sigsegv(ksig->sig, current);
2617 else
Richard Weinberger10b1c7a2014-07-13 13:36:04 +02002618 signal_delivered(ksig, stepping);
Al Viro2ce5da12012-11-07 15:11:25 -05002619}
2620
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002621/*
2622 * It could be that complete_signal() picked us to notify about the
Oleg Nesterovfec99932011-04-27 19:50:21 +02002623 * group-wide signal. Other threads should be notified now to take
2624 * the shared signals in @which since we will not.
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002625 */
Oleg Nesterovf646e222011-04-27 19:18:39 +02002626static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002627{
Oleg Nesterovf646e222011-04-27 19:18:39 +02002628 sigset_t retarget;
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002629 struct task_struct *t;
2630
Oleg Nesterovf646e222011-04-27 19:18:39 +02002631 sigandsets(&retarget, &tsk->signal->shared_pending.signal, which);
2632 if (sigisemptyset(&retarget))
2633 return;
2634
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002635 t = tsk;
2636 while_each_thread(tsk, t) {
Oleg Nesterovfec99932011-04-27 19:50:21 +02002637 if (t->flags & PF_EXITING)
2638 continue;
2639
2640 if (!has_pending_signals(&retarget, &t->blocked))
2641 continue;
2642 /* Remove the signals this thread can handle. */
2643 sigandsets(&retarget, &retarget, &t->blocked);
2644
2645 if (!signal_pending(t))
2646 signal_wake_up(t, 0);
2647
2648 if (sigisemptyset(&retarget))
2649 break;
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002650 }
2651}
2652
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002653void exit_signals(struct task_struct *tsk)
2654{
2655 int group_stop = 0;
Oleg Nesterovf646e222011-04-27 19:18:39 +02002656 sigset_t unblocked;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002657
Tejun Heo77e4ef92011-12-12 18:12:21 -08002658 /*
2659 * @tsk is about to have PF_EXITING set - lock out users which
2660 * expect stable threadgroup.
2661 */
Ingo Molnar780de9d2017-02-02 11:50:56 +01002662 cgroup_threadgroup_change_begin(tsk);
Tejun Heo77e4ef92011-12-12 18:12:21 -08002663
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002664 if (thread_group_empty(tsk) || signal_group_exit(tsk->signal)) {
2665 tsk->flags |= PF_EXITING;
Ingo Molnar780de9d2017-02-02 11:50:56 +01002666 cgroup_threadgroup_change_end(tsk);
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002667 return;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002668 }
2669
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002670 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002671 /*
2672 * From now this task is not visible for group-wide signals,
2673 * see wants_signal(), do_signal_stop().
2674 */
2675 tsk->flags |= PF_EXITING;
Tejun Heo77e4ef92011-12-12 18:12:21 -08002676
Ingo Molnar780de9d2017-02-02 11:50:56 +01002677 cgroup_threadgroup_change_end(tsk);
Tejun Heo77e4ef92011-12-12 18:12:21 -08002678
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002679 if (!signal_pending(tsk))
2680 goto out;
2681
Oleg Nesterovf646e222011-04-27 19:18:39 +02002682 unblocked = tsk->blocked;
2683 signotset(&unblocked);
2684 retarget_shared_pending(tsk, &unblocked);
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002685
Tejun Heoa8f072c2011-06-02 11:13:59 +02002686 if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
Tejun Heoe5c1902e2011-03-23 10:37:00 +01002687 task_participate_group_stop(tsk))
Tejun Heoedf2ed12011-03-23 10:37:00 +01002688 group_stop = CLD_STOPPED;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002689out:
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002690 spin_unlock_irq(&tsk->sighand->siglock);
2691
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002692 /*
2693 * If group stop has completed, deliver the notification. This
2694 * should always go to the real parent of the group leader.
2695 */
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002696 if (unlikely(group_stop)) {
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002697 read_lock(&tasklist_lock);
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002698 do_notify_parent_cldstop(tsk, false, group_stop);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002699 read_unlock(&tasklist_lock);
2700 }
2701}
2702
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703/*
2704 * System call entry points.
2705 */
2706
Randy Dunlap41c57892011-04-04 15:00:26 -07002707/**
2708 * sys_restart_syscall - restart a system call
2709 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +01002710SYSCALL_DEFINE0(restart_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711{
Andy Lutomirskif56141e2015-02-12 15:01:14 -08002712 struct restart_block *restart = &current->restart_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 return restart->fn(restart);
2714}
2715
2716long do_no_restart_syscall(struct restart_block *param)
2717{
2718 return -EINTR;
2719}
2720
Oleg Nesterovb1828012011-04-27 21:56:14 +02002721static void __set_task_blocked(struct task_struct *tsk, const sigset_t *newset)
2722{
2723 if (signal_pending(tsk) && !thread_group_empty(tsk)) {
2724 sigset_t newblocked;
2725 /* A set of now blocked but previously unblocked signals. */
Oleg Nesterov702a5072011-04-27 22:01:27 +02002726 sigandnsets(&newblocked, newset, &current->blocked);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002727 retarget_shared_pending(tsk, &newblocked);
2728 }
2729 tsk->blocked = *newset;
2730 recalc_sigpending();
2731}
2732
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002733/**
2734 * set_current_blocked - change current->blocked mask
2735 * @newset: new mask
2736 *
2737 * It is wrong to change ->blocked directly, this helper should be used
2738 * to ensure the process can't miss a shared signal we are going to block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 */
Al Viro77097ae2012-04-27 13:58:59 -04002740void set_current_blocked(sigset_t *newset)
2741{
Al Viro77097ae2012-04-27 13:58:59 -04002742 sigdelsetmask(newset, sigmask(SIGKILL) | sigmask(SIGSTOP));
Oleg Nesterov0c4a8422013-01-05 19:13:29 +01002743 __set_current_blocked(newset);
Al Viro77097ae2012-04-27 13:58:59 -04002744}
2745
2746void __set_current_blocked(const sigset_t *newset)
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002747{
2748 struct task_struct *tsk = current;
2749
Waiman Longc7be96a2016-12-14 15:04:10 -08002750 /*
2751 * In case the signal mask hasn't changed, there is nothing we need
2752 * to do. The current->blocked shouldn't be modified by other task.
2753 */
2754 if (sigequalsets(&tsk->blocked, newset))
2755 return;
2756
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002757 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovb1828012011-04-27 21:56:14 +02002758 __set_task_blocked(tsk, newset);
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02002759 spin_unlock_irq(&tsk->sighand->siglock);
2760}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
2762/*
2763 * This is also useful for kernel threads that want to temporarily
2764 * (or permanently) block certain signals.
2765 *
2766 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
2767 * interface happily blocks "unblockable" signals like SIGKILL
2768 * and friends.
2769 */
2770int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
2771{
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002772 struct task_struct *tsk = current;
2773 sigset_t newset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002775 /* Lockless, only current can change ->blocked, never from irq */
Oleg Nesterova26fd332006-03-23 03:00:49 -08002776 if (oldset)
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002777 *oldset = tsk->blocked;
Oleg Nesterova26fd332006-03-23 03:00:49 -08002778
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 switch (how) {
2780 case SIG_BLOCK:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002781 sigorsets(&newset, &tsk->blocked, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 break;
2783 case SIG_UNBLOCK:
Oleg Nesterov702a5072011-04-27 22:01:27 +02002784 sigandnsets(&newset, &tsk->blocked, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 break;
2786 case SIG_SETMASK:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002787 newset = *set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 break;
2789 default:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002790 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 }
Oleg Nesterova26fd332006-03-23 03:00:49 -08002792
Al Viro77097ae2012-04-27 13:58:59 -04002793 __set_current_blocked(&newset);
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02002794 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795}
Eric W. Biedermanfb50f5a2018-09-13 19:26:35 +02002796EXPORT_SYMBOL(sigprocmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Deepa Dinamanided653c2018-09-19 21:41:04 -07002798/*
2799 * The api helps set app-provided sigmasks.
2800 *
2801 * This is useful for syscalls such as ppoll, pselect, io_pgetevents and
2802 * epoll_pwait where a new sigmask is passed from userland for the syscalls.
2803 */
2804int set_user_sigmask(const sigset_t __user *usigmask, sigset_t *set,
2805 sigset_t *oldset, size_t sigsetsize)
2806{
2807 if (!usigmask)
2808 return 0;
2809
2810 if (sigsetsize != sizeof(sigset_t))
2811 return -EINVAL;
2812 if (copy_from_user(set, usigmask, sizeof(sigset_t)))
2813 return -EFAULT;
2814
2815 *oldset = current->blocked;
2816 set_current_blocked(set);
2817
2818 return 0;
2819}
2820EXPORT_SYMBOL(set_user_sigmask);
2821
2822#ifdef CONFIG_COMPAT
2823int set_compat_user_sigmask(const compat_sigset_t __user *usigmask,
2824 sigset_t *set, sigset_t *oldset,
2825 size_t sigsetsize)
2826{
2827 if (!usigmask)
2828 return 0;
2829
2830 if (sigsetsize != sizeof(compat_sigset_t))
2831 return -EINVAL;
2832 if (get_compat_sigset(set, usigmask))
2833 return -EFAULT;
2834
2835 *oldset = current->blocked;
2836 set_current_blocked(set);
2837
2838 return 0;
2839}
2840EXPORT_SYMBOL(set_compat_user_sigmask);
2841#endif
2842
Deepa Dinamani854a6ed2018-09-19 21:41:05 -07002843/*
2844 * restore_user_sigmask:
2845 * usigmask: sigmask passed in from userland.
2846 * sigsaved: saved sigmask when the syscall started and changed the sigmask to
2847 * usigmask.
2848 *
2849 * This is useful for syscalls such as ppoll, pselect, io_pgetevents and
2850 * epoll_pwait where a new sigmask is passed in from userland for the syscalls.
2851 */
2852void restore_user_sigmask(const void __user *usigmask, sigset_t *sigsaved)
2853{
2854
2855 if (!usigmask)
2856 return;
2857 /*
2858 * When signals are pending, do not restore them here.
2859 * Restoring sigmask here can lead to delivering signals that the above
2860 * syscalls are intended to block because of the sigmask passed in.
2861 */
2862 if (signal_pending(current)) {
2863 current->saved_sigmask = *sigsaved;
2864 set_restore_sigmask();
2865 return;
2866 }
2867
2868 /*
2869 * This is needed because the fast syscall return path does not restore
2870 * saved_sigmask when signals are not pending.
2871 */
2872 set_current_blocked(sigsaved);
2873}
2874EXPORT_SYMBOL(restore_user_sigmask);
2875
Randy Dunlap41c57892011-04-04 15:00:26 -07002876/**
2877 * sys_rt_sigprocmask - change the list of currently blocked signals
2878 * @how: whether to add, remove, or set signals
Randy Dunlapada9c932011-06-14 15:50:11 -07002879 * @nset: stores pending signals
Randy Dunlap41c57892011-04-04 15:00:26 -07002880 * @oset: previous value of signal mask if non-null
2881 * @sigsetsize: size of sigset_t type
2882 */
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002883SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset,
Heiko Carstens17da2bd2009-01-14 14:14:10 +01002884 sigset_t __user *, oset, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 sigset_t old_set, new_set;
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002887 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
2889 /* XXX: Don't preclude handling different sized sigset_t's. */
2890 if (sigsetsize != sizeof(sigset_t))
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002891 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002893 old_set = current->blocked;
2894
2895 if (nset) {
2896 if (copy_from_user(&new_set, nset, sizeof(sigset_t)))
2897 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2899
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002900 error = sigprocmask(how, &new_set, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 if (error)
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002902 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 }
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02002904
2905 if (oset) {
2906 if (copy_to_user(oset, &old_set, sizeof(sigset_t)))
2907 return -EFAULT;
2908 }
2909
2910 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911}
2912
Al Viro322a56c2012-12-25 13:32:58 -05002913#ifdef CONFIG_COMPAT
Al Viro322a56c2012-12-25 13:32:58 -05002914COMPAT_SYSCALL_DEFINE4(rt_sigprocmask, int, how, compat_sigset_t __user *, nset,
2915 compat_sigset_t __user *, oset, compat_size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916{
Al Viro322a56c2012-12-25 13:32:58 -05002917 sigset_t old_set = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
Al Viro322a56c2012-12-25 13:32:58 -05002919 /* XXX: Don't preclude handling different sized sigset_t's. */
2920 if (sigsetsize != sizeof(sigset_t))
2921 return -EINVAL;
2922
2923 if (nset) {
Al Viro322a56c2012-12-25 13:32:58 -05002924 sigset_t new_set;
2925 int error;
Al Viro3968cf62017-09-03 21:45:17 -04002926 if (get_compat_sigset(&new_set, nset))
Al Viro322a56c2012-12-25 13:32:58 -05002927 return -EFAULT;
Al Viro322a56c2012-12-25 13:32:58 -05002928 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
2929
2930 error = sigprocmask(how, &new_set, NULL);
2931 if (error)
2932 return error;
2933 }
Dmitry V. Levinf4543222017-08-22 02:16:11 +03002934 return oset ? put_compat_sigset(oset, &old_set, sizeof(*oset)) : 0;
Al Viro322a56c2012-12-25 13:32:58 -05002935}
2936#endif
Al Viro322a56c2012-12-25 13:32:58 -05002937
Christian Braunerb1d294c2018-08-21 22:00:02 -07002938static void do_sigpending(sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 spin_lock_irq(&current->sighand->siglock);
Al Virofe9c1db2012-12-25 14:31:38 -05002941 sigorsets(set, &current->pending.signal,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 &current->signal->shared_pending.signal);
2943 spin_unlock_irq(&current->sighand->siglock);
2944
2945 /* Outside the lock because only this thread touches it. */
Al Virofe9c1db2012-12-25 14:31:38 -05002946 sigandsets(set, &current->blocked, set);
Randy Dunlap5aba0852011-04-04 14:59:31 -07002947}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Randy Dunlap41c57892011-04-04 15:00:26 -07002949/**
2950 * sys_rt_sigpending - examine a pending signal that has been raised
2951 * while blocked
Randy Dunlap20f22ab2013-03-04 14:32:59 -08002952 * @uset: stores pending signals
Randy Dunlap41c57892011-04-04 15:00:26 -07002953 * @sigsetsize: size of sigset_t type or larger
2954 */
Al Virofe9c1db2012-12-25 14:31:38 -05002955SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, uset, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
Al Virofe9c1db2012-12-25 14:31:38 -05002957 sigset_t set;
Dmitry V. Levin176826a2017-08-22 02:16:43 +03002958
2959 if (sigsetsize > sizeof(*uset))
2960 return -EINVAL;
2961
Christian Braunerb1d294c2018-08-21 22:00:02 -07002962 do_sigpending(&set);
2963
2964 if (copy_to_user(uset, &set, sigsetsize))
2965 return -EFAULT;
2966
2967 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968}
2969
Al Virofe9c1db2012-12-25 14:31:38 -05002970#ifdef CONFIG_COMPAT
Al Virofe9c1db2012-12-25 14:31:38 -05002971COMPAT_SYSCALL_DEFINE2(rt_sigpending, compat_sigset_t __user *, uset,
2972 compat_size_t, sigsetsize)
2973{
Al Virofe9c1db2012-12-25 14:31:38 -05002974 sigset_t set;
Dmitry V. Levin176826a2017-08-22 02:16:43 +03002975
2976 if (sigsetsize > sizeof(*uset))
2977 return -EINVAL;
2978
Christian Braunerb1d294c2018-08-21 22:00:02 -07002979 do_sigpending(&set);
2980
2981 return put_compat_sigset(uset, &set, sigsetsize);
Al Virofe9c1db2012-12-25 14:31:38 -05002982}
2983#endif
Al Virofe9c1db2012-12-25 14:31:38 -05002984
Eric W. Biederman4ce5f9c2018-09-25 12:59:31 +02002985static const struct {
2986 unsigned char limit, layout;
2987} sig_sicodes[] = {
2988 [SIGILL] = { NSIGILL, SIL_FAULT },
2989 [SIGFPE] = { NSIGFPE, SIL_FAULT },
2990 [SIGSEGV] = { NSIGSEGV, SIL_FAULT },
2991 [SIGBUS] = { NSIGBUS, SIL_FAULT },
2992 [SIGTRAP] = { NSIGTRAP, SIL_FAULT },
2993#if defined(SIGEMT)
2994 [SIGEMT] = { NSIGEMT, SIL_FAULT },
2995#endif
2996 [SIGCHLD] = { NSIGCHLD, SIL_CHLD },
2997 [SIGPOLL] = { NSIGPOLL, SIL_POLL },
2998 [SIGSYS] = { NSIGSYS, SIL_SYS },
2999};
3000
Eric W. Biedermanb2a2ab52018-10-10 20:11:25 -05003001static bool known_siginfo_layout(unsigned sig, int si_code)
Eric W. Biederman4ce5f9c2018-09-25 12:59:31 +02003002{
3003 if (si_code == SI_KERNEL)
3004 return true;
3005 else if ((si_code > SI_USER)) {
3006 if (sig_specific_sicodes(sig)) {
3007 if (si_code <= sig_sicodes[sig].limit)
3008 return true;
3009 }
3010 else if (si_code <= NSIGPOLL)
3011 return true;
3012 }
3013 else if (si_code >= SI_DETHREAD)
3014 return true;
3015 else if (si_code == SI_ASYNCNL)
3016 return true;
3017 return false;
3018}
3019
Eric W. Biedermana3670052018-10-10 20:29:44 -05003020enum siginfo_layout siginfo_layout(unsigned sig, int si_code)
Eric W. Biedermancc731522017-07-16 22:36:59 -05003021{
3022 enum siginfo_layout layout = SIL_KILL;
3023 if ((si_code > SI_USER) && (si_code < SI_KERNEL)) {
Eric W. Biederman4ce5f9c2018-09-25 12:59:31 +02003024 if ((sig < ARRAY_SIZE(sig_sicodes)) &&
3025 (si_code <= sig_sicodes[sig].limit)) {
3026 layout = sig_sicodes[sig].layout;
Eric W. Biederman31931c92018-04-24 20:59:47 -05003027 /* Handle the exceptions */
3028 if ((sig == SIGBUS) &&
3029 (si_code >= BUS_MCEERR_AR) && (si_code <= BUS_MCEERR_AO))
3030 layout = SIL_FAULT_MCEERR;
3031 else if ((sig == SIGSEGV) && (si_code == SEGV_BNDERR))
3032 layout = SIL_FAULT_BNDERR;
3033#ifdef SEGV_PKUERR
3034 else if ((sig == SIGSEGV) && (si_code == SEGV_PKUERR))
3035 layout = SIL_FAULT_PKUERR;
3036#endif
3037 }
Eric W. Biedermancc731522017-07-16 22:36:59 -05003038 else if (si_code <= NSIGPOLL)
3039 layout = SIL_POLL;
3040 } else {
3041 if (si_code == SI_TIMER)
3042 layout = SIL_TIMER;
3043 else if (si_code == SI_SIGIO)
3044 layout = SIL_POLL;
3045 else if (si_code < 0)
3046 layout = SIL_RT;
Eric W. Biedermancc731522017-07-16 22:36:59 -05003047 }
3048 return layout;
3049}
3050
Eric W. Biederman4ce5f9c2018-09-25 12:59:31 +02003051static inline char __user *si_expansion(const siginfo_t __user *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052{
Eric W. Biederman4ce5f9c2018-09-25 12:59:31 +02003053 return ((char __user *)info) + sizeof(struct kernel_siginfo);
3054}
3055
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003056int copy_siginfo_to_user(siginfo_t __user *to, const kernel_siginfo_t *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057{
Eric W. Biederman4ce5f9c2018-09-25 12:59:31 +02003058 char __user *expansion = si_expansion(to);
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003059 if (copy_to_user(to, from , sizeof(struct kernel_siginfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 return -EFAULT;
Eric W. Biederman4ce5f9c2018-09-25 12:59:31 +02003061 if (clear_user(expansion, SI_EXPANSION_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 return -EFAULT;
Eric W. Biedermanc999b932018-04-14 13:03:25 -05003063 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064}
3065
Eric W. Biederman601d5ab2018-10-05 09:02:48 +02003066static int post_copy_siginfo_from_user(kernel_siginfo_t *info,
3067 const siginfo_t __user *from)
Eric W. Biederman4cd2e0e2018-04-18 17:30:19 -05003068{
Eric W. Biederman601d5ab2018-10-05 09:02:48 +02003069 if (unlikely(!known_siginfo_layout(info->si_signo, info->si_code))) {
Eric W. Biederman4ce5f9c2018-09-25 12:59:31 +02003070 char __user *expansion = si_expansion(from);
3071 char buf[SI_EXPANSION_SIZE];
3072 int i;
3073 /*
3074 * An unknown si_code might need more than
3075 * sizeof(struct kernel_siginfo) bytes. Verify all of the
3076 * extra bytes are 0. This guarantees copy_siginfo_to_user
3077 * will return this data to userspace exactly.
3078 */
3079 if (copy_from_user(&buf, expansion, SI_EXPANSION_SIZE))
3080 return -EFAULT;
3081 for (i = 0; i < SI_EXPANSION_SIZE; i++) {
3082 if (buf[i] != 0)
3083 return -E2BIG;
3084 }
3085 }
Eric W. Biederman4cd2e0e2018-04-18 17:30:19 -05003086 return 0;
3087}
3088
Eric W. Biederman601d5ab2018-10-05 09:02:48 +02003089static int __copy_siginfo_from_user(int signo, kernel_siginfo_t *to,
3090 const siginfo_t __user *from)
3091{
3092 if (copy_from_user(to, from, sizeof(struct kernel_siginfo)))
3093 return -EFAULT;
3094 to->si_signo = signo;
3095 return post_copy_siginfo_from_user(to, from);
3096}
3097
3098int copy_siginfo_from_user(kernel_siginfo_t *to, const siginfo_t __user *from)
3099{
3100 if (copy_from_user(to, from, sizeof(struct kernel_siginfo)))
3101 return -EFAULT;
3102 return post_copy_siginfo_from_user(to, from);
3103}
3104
Eric W. Biederman212a36a2017-07-31 17:15:31 -05003105#ifdef CONFIG_COMPAT
Eric W. Biedermanea64d5a2018-01-15 18:03:33 -06003106int copy_siginfo_to_user32(struct compat_siginfo __user *to,
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003107 const struct kernel_siginfo *from)
Eric W. Biedermanea64d5a2018-01-15 18:03:33 -06003108#if defined(CONFIG_X86_X32_ABI) || defined(CONFIG_IA32_EMULATION)
3109{
3110 return __copy_siginfo_to_user32(to, from, in_x32_syscall());
3111}
3112int __copy_siginfo_to_user32(struct compat_siginfo __user *to,
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003113 const struct kernel_siginfo *from, bool x32_ABI)
Eric W. Biedermanea64d5a2018-01-15 18:03:33 -06003114#endif
3115{
3116 struct compat_siginfo new;
3117 memset(&new, 0, sizeof(new));
3118
3119 new.si_signo = from->si_signo;
3120 new.si_errno = from->si_errno;
3121 new.si_code = from->si_code;
3122 switch(siginfo_layout(from->si_signo, from->si_code)) {
3123 case SIL_KILL:
3124 new.si_pid = from->si_pid;
3125 new.si_uid = from->si_uid;
3126 break;
3127 case SIL_TIMER:
3128 new.si_tid = from->si_tid;
3129 new.si_overrun = from->si_overrun;
3130 new.si_int = from->si_int;
3131 break;
3132 case SIL_POLL:
3133 new.si_band = from->si_band;
3134 new.si_fd = from->si_fd;
3135 break;
3136 case SIL_FAULT:
3137 new.si_addr = ptr_to_compat(from->si_addr);
3138#ifdef __ARCH_SI_TRAPNO
3139 new.si_trapno = from->si_trapno;
3140#endif
Eric W. Biederman31931c92018-04-24 20:59:47 -05003141 break;
3142 case SIL_FAULT_MCEERR:
3143 new.si_addr = ptr_to_compat(from->si_addr);
3144#ifdef __ARCH_SI_TRAPNO
3145 new.si_trapno = from->si_trapno;
Eric W. Biedermanea64d5a2018-01-15 18:03:33 -06003146#endif
Eric W. Biederman31931c92018-04-24 20:59:47 -05003147 new.si_addr_lsb = from->si_addr_lsb;
3148 break;
3149 case SIL_FAULT_BNDERR:
3150 new.si_addr = ptr_to_compat(from->si_addr);
3151#ifdef __ARCH_SI_TRAPNO
3152 new.si_trapno = from->si_trapno;
Eric W. Biedermanea64d5a2018-01-15 18:03:33 -06003153#endif
Eric W. Biederman31931c92018-04-24 20:59:47 -05003154 new.si_lower = ptr_to_compat(from->si_lower);
3155 new.si_upper = ptr_to_compat(from->si_upper);
3156 break;
3157 case SIL_FAULT_PKUERR:
3158 new.si_addr = ptr_to_compat(from->si_addr);
3159#ifdef __ARCH_SI_TRAPNO
3160 new.si_trapno = from->si_trapno;
Eric W. Biedermanea64d5a2018-01-15 18:03:33 -06003161#endif
Eric W. Biederman31931c92018-04-24 20:59:47 -05003162 new.si_pkey = from->si_pkey;
Eric W. Biedermanea64d5a2018-01-15 18:03:33 -06003163 break;
3164 case SIL_CHLD:
3165 new.si_pid = from->si_pid;
3166 new.si_uid = from->si_uid;
3167 new.si_status = from->si_status;
3168#ifdef CONFIG_X86_X32_ABI
3169 if (x32_ABI) {
3170 new._sifields._sigchld_x32._utime = from->si_utime;
3171 new._sifields._sigchld_x32._stime = from->si_stime;
3172 } else
3173#endif
3174 {
3175 new.si_utime = from->si_utime;
3176 new.si_stime = from->si_stime;
3177 }
3178 break;
3179 case SIL_RT:
3180 new.si_pid = from->si_pid;
3181 new.si_uid = from->si_uid;
3182 new.si_int = from->si_int;
3183 break;
3184 case SIL_SYS:
3185 new.si_call_addr = ptr_to_compat(from->si_call_addr);
3186 new.si_syscall = from->si_syscall;
3187 new.si_arch = from->si_arch;
3188 break;
3189 }
3190
3191 if (copy_to_user(to, &new, sizeof(struct compat_siginfo)))
3192 return -EFAULT;
3193
3194 return 0;
3195}
3196
Eric W. Biederman601d5ab2018-10-05 09:02:48 +02003197static int post_copy_siginfo_from_user32(kernel_siginfo_t *to,
3198 const struct compat_siginfo *from)
3199{
3200 clear_siginfo(to);
3201 to->si_signo = from->si_signo;
3202 to->si_errno = from->si_errno;
3203 to->si_code = from->si_code;
3204 switch(siginfo_layout(from->si_signo, from->si_code)) {
3205 case SIL_KILL:
3206 to->si_pid = from->si_pid;
3207 to->si_uid = from->si_uid;
3208 break;
3209 case SIL_TIMER:
3210 to->si_tid = from->si_tid;
3211 to->si_overrun = from->si_overrun;
3212 to->si_int = from->si_int;
3213 break;
3214 case SIL_POLL:
3215 to->si_band = from->si_band;
3216 to->si_fd = from->si_fd;
3217 break;
3218 case SIL_FAULT:
3219 to->si_addr = compat_ptr(from->si_addr);
3220#ifdef __ARCH_SI_TRAPNO
3221 to->si_trapno = from->si_trapno;
3222#endif
3223 break;
3224 case SIL_FAULT_MCEERR:
3225 to->si_addr = compat_ptr(from->si_addr);
3226#ifdef __ARCH_SI_TRAPNO
3227 to->si_trapno = from->si_trapno;
3228#endif
3229 to->si_addr_lsb = from->si_addr_lsb;
3230 break;
3231 case SIL_FAULT_BNDERR:
3232 to->si_addr = compat_ptr(from->si_addr);
3233#ifdef __ARCH_SI_TRAPNO
3234 to->si_trapno = from->si_trapno;
3235#endif
3236 to->si_lower = compat_ptr(from->si_lower);
3237 to->si_upper = compat_ptr(from->si_upper);
3238 break;
3239 case SIL_FAULT_PKUERR:
3240 to->si_addr = compat_ptr(from->si_addr);
3241#ifdef __ARCH_SI_TRAPNO
3242 to->si_trapno = from->si_trapno;
3243#endif
3244 to->si_pkey = from->si_pkey;
3245 break;
3246 case SIL_CHLD:
3247 to->si_pid = from->si_pid;
3248 to->si_uid = from->si_uid;
3249 to->si_status = from->si_status;
3250#ifdef CONFIG_X86_X32_ABI
3251 if (in_x32_syscall()) {
3252 to->si_utime = from->_sifields._sigchld_x32._utime;
3253 to->si_stime = from->_sifields._sigchld_x32._stime;
3254 } else
3255#endif
3256 {
3257 to->si_utime = from->si_utime;
3258 to->si_stime = from->si_stime;
3259 }
3260 break;
3261 case SIL_RT:
3262 to->si_pid = from->si_pid;
3263 to->si_uid = from->si_uid;
3264 to->si_int = from->si_int;
3265 break;
3266 case SIL_SYS:
3267 to->si_call_addr = compat_ptr(from->si_call_addr);
3268 to->si_syscall = from->si_syscall;
3269 to->si_arch = from->si_arch;
3270 break;
3271 }
3272 return 0;
3273}
3274
3275static int __copy_siginfo_from_user32(int signo, struct kernel_siginfo *to,
3276 const struct compat_siginfo __user *ufrom)
3277{
3278 struct compat_siginfo from;
3279
3280 if (copy_from_user(&from, ufrom, sizeof(struct compat_siginfo)))
3281 return -EFAULT;
3282
3283 from.si_signo = signo;
3284 return post_copy_siginfo_from_user32(to, &from);
3285}
3286
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003287int copy_siginfo_from_user32(struct kernel_siginfo *to,
Eric W. Biederman212a36a2017-07-31 17:15:31 -05003288 const struct compat_siginfo __user *ufrom)
3289{
3290 struct compat_siginfo from;
3291
3292 if (copy_from_user(&from, ufrom, sizeof(struct compat_siginfo)))
3293 return -EFAULT;
3294
Eric W. Biederman601d5ab2018-10-05 09:02:48 +02003295 return post_copy_siginfo_from_user32(to, &from);
Eric W. Biederman212a36a2017-07-31 17:15:31 -05003296}
3297#endif /* CONFIG_COMPAT */
3298
Randy Dunlap41c57892011-04-04 15:00:26 -07003299/**
Oleg Nesterov943df142011-04-27 21:44:14 +02003300 * do_sigtimedwait - wait for queued signals specified in @which
3301 * @which: queued signals to wait for
3302 * @info: if non-null, the signal's siginfo is returned here
3303 * @ts: upper bound on process time suspension
3304 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003305static int do_sigtimedwait(const sigset_t *which, kernel_siginfo_t *info,
Arnd Bergmann49c39f82018-04-18 15:56:13 +02003306 const struct timespec64 *ts)
Oleg Nesterov943df142011-04-27 21:44:14 +02003307{
Thomas Gleixner2456e852016-12-25 11:38:40 +01003308 ktime_t *to = NULL, timeout = KTIME_MAX;
Oleg Nesterov943df142011-04-27 21:44:14 +02003309 struct task_struct *tsk = current;
Oleg Nesterov943df142011-04-27 21:44:14 +02003310 sigset_t mask = *which;
Thomas Gleixner2b1ecc32016-07-04 09:50:25 +00003311 int sig, ret = 0;
Oleg Nesterov943df142011-04-27 21:44:14 +02003312
3313 if (ts) {
Arnd Bergmann49c39f82018-04-18 15:56:13 +02003314 if (!timespec64_valid(ts))
Oleg Nesterov943df142011-04-27 21:44:14 +02003315 return -EINVAL;
Arnd Bergmann49c39f82018-04-18 15:56:13 +02003316 timeout = timespec64_to_ktime(*ts);
Thomas Gleixner2b1ecc32016-07-04 09:50:25 +00003317 to = &timeout;
Oleg Nesterov943df142011-04-27 21:44:14 +02003318 }
3319
3320 /*
3321 * Invert the set of allowed signals to get those we want to block.
3322 */
3323 sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
3324 signotset(&mask);
3325
3326 spin_lock_irq(&tsk->sighand->siglock);
3327 sig = dequeue_signal(tsk, &mask, info);
Thomas Gleixner2456e852016-12-25 11:38:40 +01003328 if (!sig && timeout) {
Oleg Nesterov943df142011-04-27 21:44:14 +02003329 /*
3330 * None ready, temporarily unblock those we're interested
3331 * while we are sleeping in so that we'll be awakened when
Oleg Nesterovb1828012011-04-27 21:56:14 +02003332 * they arrive. Unblocking is always fine, we can avoid
3333 * set_current_blocked().
Oleg Nesterov943df142011-04-27 21:44:14 +02003334 */
3335 tsk->real_blocked = tsk->blocked;
3336 sigandsets(&tsk->blocked, &tsk->blocked, &mask);
3337 recalc_sigpending();
3338 spin_unlock_irq(&tsk->sighand->siglock);
3339
Thomas Gleixner2b1ecc32016-07-04 09:50:25 +00003340 __set_current_state(TASK_INTERRUPTIBLE);
3341 ret = freezable_schedule_hrtimeout_range(to, tsk->timer_slack_ns,
3342 HRTIMER_MODE_REL);
Oleg Nesterov943df142011-04-27 21:44:14 +02003343 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovb1828012011-04-27 21:56:14 +02003344 __set_task_blocked(tsk, &tsk->real_blocked);
Oleg Nesterov61140412014-06-06 14:36:46 -07003345 sigemptyset(&tsk->real_blocked);
Oleg Nesterovb1828012011-04-27 21:56:14 +02003346 sig = dequeue_signal(tsk, &mask, info);
Oleg Nesterov943df142011-04-27 21:44:14 +02003347 }
3348 spin_unlock_irq(&tsk->sighand->siglock);
3349
3350 if (sig)
3351 return sig;
Thomas Gleixner2b1ecc32016-07-04 09:50:25 +00003352 return ret ? -EINTR : -EAGAIN;
Oleg Nesterov943df142011-04-27 21:44:14 +02003353}
3354
3355/**
Randy Dunlap41c57892011-04-04 15:00:26 -07003356 * sys_rt_sigtimedwait - synchronously wait for queued signals specified
3357 * in @uthese
3358 * @uthese: queued signals to wait for
3359 * @uinfo: if non-null, the signal's siginfo is returned here
3360 * @uts: upper bound on process time suspension
3361 * @sigsetsize: size of sigset_t type
3362 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01003363SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
Arnd Bergmann49c39f82018-04-18 15:56:13 +02003364 siginfo_t __user *, uinfo,
3365 const struct __kernel_timespec __user *, uts,
Heiko Carstens17da2bd2009-01-14 14:14:10 +01003366 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 sigset_t these;
Arnd Bergmann49c39f82018-04-18 15:56:13 +02003369 struct timespec64 ts;
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003370 kernel_siginfo_t info;
Oleg Nesterov943df142011-04-27 21:44:14 +02003371 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372
3373 /* XXX: Don't preclude handling different sized sigset_t's. */
3374 if (sigsetsize != sizeof(sigset_t))
3375 return -EINVAL;
3376
3377 if (copy_from_user(&these, uthese, sizeof(these)))
3378 return -EFAULT;
Randy Dunlap5aba0852011-04-04 14:59:31 -07003379
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380 if (uts) {
Arnd Bergmann49c39f82018-04-18 15:56:13 +02003381 if (get_timespec64(&ts, uts))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 }
3384
Oleg Nesterov943df142011-04-27 21:44:14 +02003385 ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386
Oleg Nesterov943df142011-04-27 21:44:14 +02003387 if (ret > 0 && uinfo) {
3388 if (copy_siginfo_to_user(uinfo, &info))
3389 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 }
3391
3392 return ret;
3393}
3394
Arnd Bergmanndf8522a2018-04-18 16:15:37 +02003395#ifdef CONFIG_COMPAT_32BIT_TIME
3396SYSCALL_DEFINE4(rt_sigtimedwait_time32, const sigset_t __user *, uthese,
3397 siginfo_t __user *, uinfo,
3398 const struct old_timespec32 __user *, uts,
3399 size_t, sigsetsize)
3400{
3401 sigset_t these;
3402 struct timespec64 ts;
3403 kernel_siginfo_t info;
3404 int ret;
3405
3406 if (sigsetsize != sizeof(sigset_t))
3407 return -EINVAL;
3408
3409 if (copy_from_user(&these, uthese, sizeof(these)))
3410 return -EFAULT;
3411
3412 if (uts) {
3413 if (get_old_timespec32(&ts, uts))
3414 return -EFAULT;
3415 }
3416
3417 ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
3418
3419 if (ret > 0 && uinfo) {
3420 if (copy_siginfo_to_user(uinfo, &info))
3421 ret = -EFAULT;
3422 }
3423
3424 return ret;
3425}
3426#endif
3427
Al Viro1b3c8722017-05-31 04:46:17 -04003428#ifdef CONFIG_COMPAT
Arnd Bergmann2367c4b2018-04-18 16:18:35 +02003429COMPAT_SYSCALL_DEFINE4(rt_sigtimedwait_time64, compat_sigset_t __user *, uthese,
3430 struct compat_siginfo __user *, uinfo,
3431 struct __kernel_timespec __user *, uts, compat_size_t, sigsetsize)
3432{
3433 sigset_t s;
3434 struct timespec64 t;
3435 kernel_siginfo_t info;
3436 long ret;
3437
3438 if (sigsetsize != sizeof(sigset_t))
3439 return -EINVAL;
3440
3441 if (get_compat_sigset(&s, uthese))
3442 return -EFAULT;
3443
3444 if (uts) {
3445 if (get_timespec64(&t, uts))
3446 return -EFAULT;
3447 }
3448
3449 ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);
3450
3451 if (ret > 0 && uinfo) {
3452 if (copy_siginfo_to_user32(uinfo, &info))
3453 ret = -EFAULT;
3454 }
3455
3456 return ret;
3457}
3458
3459#ifdef CONFIG_COMPAT_32BIT_TIME
Arnd Bergmann8dabe722019-01-07 00:33:08 +01003460COMPAT_SYSCALL_DEFINE4(rt_sigtimedwait_time32, compat_sigset_t __user *, uthese,
Al Viro1b3c8722017-05-31 04:46:17 -04003461 struct compat_siginfo __user *, uinfo,
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +02003462 struct old_timespec32 __user *, uts, compat_size_t, sigsetsize)
Al Viro1b3c8722017-05-31 04:46:17 -04003463{
Al Viro1b3c8722017-05-31 04:46:17 -04003464 sigset_t s;
Arnd Bergmann49c39f82018-04-18 15:56:13 +02003465 struct timespec64 t;
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003466 kernel_siginfo_t info;
Al Viro1b3c8722017-05-31 04:46:17 -04003467 long ret;
3468
3469 if (sigsetsize != sizeof(sigset_t))
3470 return -EINVAL;
3471
Al Viro3968cf62017-09-03 21:45:17 -04003472 if (get_compat_sigset(&s, uthese))
Al Viro1b3c8722017-05-31 04:46:17 -04003473 return -EFAULT;
Al Viro1b3c8722017-05-31 04:46:17 -04003474
3475 if (uts) {
Arnd Bergmann49c39f82018-04-18 15:56:13 +02003476 if (get_old_timespec32(&t, uts))
Al Viro1b3c8722017-05-31 04:46:17 -04003477 return -EFAULT;
3478 }
3479
3480 ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);
3481
3482 if (ret > 0 && uinfo) {
3483 if (copy_siginfo_to_user32(uinfo, &info))
3484 ret = -EFAULT;
3485 }
3486
3487 return ret;
3488}
3489#endif
Arnd Bergmann2367c4b2018-04-18 16:18:35 +02003490#endif
Al Viro1b3c8722017-05-31 04:46:17 -04003491
Christian Brauner3eb39f42018-11-19 00:51:56 +01003492static inline void prepare_kill_siginfo(int sig, struct kernel_siginfo *info)
3493{
3494 clear_siginfo(info);
3495 info->si_signo = sig;
3496 info->si_errno = 0;
3497 info->si_code = SI_USER;
3498 info->si_pid = task_tgid_vnr(current);
3499 info->si_uid = from_kuid_munged(current_user_ns(), current_uid());
3500}
3501
Randy Dunlap41c57892011-04-04 15:00:26 -07003502/**
3503 * sys_kill - send a signal to a process
3504 * @pid: the PID of the process
3505 * @sig: signal to be sent
3506 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01003507SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003509 struct kernel_siginfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510
Christian Brauner3eb39f42018-11-19 00:51:56 +01003511 prepare_kill_siginfo(sig, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512
3513 return kill_something_info(sig, &info, pid);
3514}
3515
Christian Brauner3eb39f42018-11-19 00:51:56 +01003516#ifdef CONFIG_PROC_FS
3517/*
3518 * Verify that the signaler and signalee either are in the same pid namespace
3519 * or that the signaler's pid namespace is an ancestor of the signalee's pid
3520 * namespace.
3521 */
3522static bool access_pidfd_pidns(struct pid *pid)
3523{
3524 struct pid_namespace *active = task_active_pid_ns(current);
3525 struct pid_namespace *p = ns_of_pid(pid);
3526
3527 for (;;) {
3528 if (!p)
3529 return false;
3530 if (p == active)
3531 break;
3532 p = p->parent;
3533 }
3534
3535 return true;
3536}
3537
3538static int copy_siginfo_from_user_any(kernel_siginfo_t *kinfo, siginfo_t *info)
3539{
3540#ifdef CONFIG_COMPAT
3541 /*
3542 * Avoid hooking up compat syscalls and instead handle necessary
3543 * conversions here. Note, this is a stop-gap measure and should not be
3544 * considered a generic solution.
3545 */
3546 if (in_compat_syscall())
3547 return copy_siginfo_from_user32(
3548 kinfo, (struct compat_siginfo __user *)info);
3549#endif
3550 return copy_siginfo_from_user(kinfo, info);
3551}
3552
3553/**
3554 * sys_pidfd_send_signal - send a signal to a process through a task file
3555 * descriptor
3556 * @pidfd: the file descriptor of the process
3557 * @sig: signal to be sent
3558 * @info: the signal info
3559 * @flags: future flags to be passed
3560 *
3561 * The syscall currently only signals via PIDTYPE_PID which covers
3562 * kill(<positive-pid>, <signal>. It does not signal threads or process
3563 * groups.
3564 * In order to extend the syscall to threads and process groups the @flags
3565 * argument should be used. In essence, the @flags argument will determine
3566 * what is signaled and not the file descriptor itself. Put in other words,
3567 * grouping is a property of the flags argument not a property of the file
3568 * descriptor.
3569 *
3570 * Return: 0 on success, negative errno on failure
3571 */
3572SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig,
3573 siginfo_t __user *, info, unsigned int, flags)
3574{
3575 int ret;
3576 struct fd f;
3577 struct pid *pid;
3578 kernel_siginfo_t kinfo;
3579
3580 /* Enforce flags be set to 0 until we add an extension. */
3581 if (flags)
3582 return -EINVAL;
3583
3584 f = fdget_raw(pidfd);
3585 if (!f.file)
3586 return -EBADF;
3587
3588 /* Is this a pidfd? */
3589 pid = tgid_pidfd_to_pid(f.file);
3590 if (IS_ERR(pid)) {
3591 ret = PTR_ERR(pid);
3592 goto err;
3593 }
3594
3595 ret = -EINVAL;
3596 if (!access_pidfd_pidns(pid))
3597 goto err;
3598
3599 if (info) {
3600 ret = copy_siginfo_from_user_any(&kinfo, info);
3601 if (unlikely(ret))
3602 goto err;
3603
3604 ret = -EINVAL;
3605 if (unlikely(sig != kinfo.si_signo))
3606 goto err;
3607
Jann Horn556a8882019-03-30 03:12:32 +01003608 /* Only allow sending arbitrary signals to yourself. */
3609 ret = -EPERM;
Christian Brauner3eb39f42018-11-19 00:51:56 +01003610 if ((task_pid(current) != pid) &&
Jann Horn556a8882019-03-30 03:12:32 +01003611 (kinfo.si_code >= 0 || kinfo.si_code == SI_TKILL))
3612 goto err;
Christian Brauner3eb39f42018-11-19 00:51:56 +01003613 } else {
3614 prepare_kill_siginfo(sig, &kinfo);
3615 }
3616
3617 ret = kill_pid_info(sig, &kinfo, pid);
3618
3619err:
3620 fdput(f);
3621 return ret;
3622}
3623#endif /* CONFIG_PROC_FS */
3624
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00003625static int
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003626do_send_specific(pid_t tgid, pid_t pid, int sig, struct kernel_siginfo *info)
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08003627{
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08003628 struct task_struct *p;
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00003629 int error = -ESRCH;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08003630
Oleg Nesterov3547ff32008-04-30 00:52:51 -07003631 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07003632 p = find_task_by_vpid(pid);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003633 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00003634 error = check_kill_permission(sig, info, p);
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08003635 /*
3636 * The null signal is a permissions and process existence
3637 * probe. No signal is actually delivered.
3638 */
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07003639 if (!error && sig) {
Eric W. Biederman40b3b022018-07-21 10:45:15 -05003640 error = do_send_sig_info(sig, info, p, PIDTYPE_PID);
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07003641 /*
3642 * If lock_task_sighand() failed we pretend the task
3643 * dies after receiving the signal. The window is tiny,
3644 * and the signal is private anyway.
3645 */
3646 if (unlikely(error == -ESRCH))
3647 error = 0;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08003648 }
3649 }
Oleg Nesterov3547ff32008-04-30 00:52:51 -07003650 rcu_read_unlock();
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08003651
3652 return error;
3653}
3654
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00003655static int do_tkill(pid_t tgid, pid_t pid, int sig)
3656{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003657 struct kernel_siginfo info;
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00003658
Eric W. Biederman5f749722018-01-22 14:58:57 -06003659 clear_siginfo(&info);
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00003660 info.si_signo = sig;
3661 info.si_errno = 0;
3662 info.si_code = SI_TKILL;
3663 info.si_pid = task_tgid_vnr(current);
Eric W. Biederman078de5f2012-02-08 07:00:08 -08003664 info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00003665
3666 return do_send_specific(tgid, pid, sig, &info);
3667}
3668
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669/**
3670 * sys_tgkill - send signal to one specific thread
3671 * @tgid: the thread group ID of the thread
3672 * @pid: the PID of the thread
3673 * @sig: signal to be sent
3674 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08003675 * This syscall also checks the @tgid and returns -ESRCH even if the PID
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 * exists but it's not belonging to the target process anymore. This
3677 * method solves the problem of threads exiting and PIDs getting reused.
3678 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003679SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 /* This is only valid for single tasks */
3682 if (pid <= 0 || tgid <= 0)
3683 return -EINVAL;
3684
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08003685 return do_tkill(tgid, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686}
3687
Randy Dunlap41c57892011-04-04 15:00:26 -07003688/**
3689 * sys_tkill - send signal to one specific task
3690 * @pid: the PID of the task
3691 * @sig: signal to be sent
3692 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 * Send a signal to only one task, even if it's a CLONE_THREAD task.
3694 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003695SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 /* This is only valid for single tasks */
3698 if (pid <= 0)
3699 return -EINVAL;
3700
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08003701 return do_tkill(0, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702}
3703
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003704static int do_rt_sigqueueinfo(pid_t pid, int sig, kernel_siginfo_t *info)
Al Viro75907d42012-12-25 15:19:12 -05003705{
3706 /* Not even root can pretend to send signals from the kernel.
3707 * Nor can they impersonate a kill()/tgkill(), which adds source info.
3708 */
Andrey Vagin66dd34a2013-02-27 17:03:12 -08003709 if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
Vladimir Davydov69828dc2015-04-16 12:47:35 -07003710 (task_pid_vnr(current) != pid))
Al Viro75907d42012-12-25 15:19:12 -05003711 return -EPERM;
Vladimir Davydov69828dc2015-04-16 12:47:35 -07003712
Al Viro75907d42012-12-25 15:19:12 -05003713 /* POSIX.1b doesn't mention process groups. */
3714 return kill_proc_info(sig, info, pid);
3715}
3716
Randy Dunlap41c57892011-04-04 15:00:26 -07003717/**
3718 * sys_rt_sigqueueinfo - send signal information to a signal
3719 * @pid: the PID of the thread
3720 * @sig: signal to be sent
3721 * @uinfo: signal info to be sent
3722 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003723SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
3724 siginfo_t __user *, uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003726 kernel_siginfo_t info;
Eric W. Biederman601d5ab2018-10-05 09:02:48 +02003727 int ret = __copy_siginfo_from_user(sig, &info, uinfo);
Eric W. Biederman4cd2e0e2018-04-18 17:30:19 -05003728 if (unlikely(ret))
3729 return ret;
Al Viro75907d42012-12-25 15:19:12 -05003730 return do_rt_sigqueueinfo(pid, sig, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731}
3732
Al Viro75907d42012-12-25 15:19:12 -05003733#ifdef CONFIG_COMPAT
Al Viro75907d42012-12-25 15:19:12 -05003734COMPAT_SYSCALL_DEFINE3(rt_sigqueueinfo,
3735 compat_pid_t, pid,
3736 int, sig,
3737 struct compat_siginfo __user *, uinfo)
3738{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003739 kernel_siginfo_t info;
Eric W. Biederman601d5ab2018-10-05 09:02:48 +02003740 int ret = __copy_siginfo_from_user32(sig, &info, uinfo);
Al Viro75907d42012-12-25 15:19:12 -05003741 if (unlikely(ret))
3742 return ret;
3743 return do_rt_sigqueueinfo(pid, sig, &info);
3744}
3745#endif
Al Viro75907d42012-12-25 15:19:12 -05003746
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003747static int do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, kernel_siginfo_t *info)
Thomas Gleixner62ab4502009-04-04 21:01:06 +00003748{
3749 /* This is only valid for single tasks */
3750 if (pid <= 0 || tgid <= 0)
3751 return -EINVAL;
3752
3753 /* Not even root can pretend to send signals from the kernel.
Julien Tinnesda485242011-03-18 15:05:21 -07003754 * Nor can they impersonate a kill()/tgkill(), which adds source info.
3755 */
Vladimir Davydov69828dc2015-04-16 12:47:35 -07003756 if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
3757 (task_pid_vnr(current) != pid))
Thomas Gleixner62ab4502009-04-04 21:01:06 +00003758 return -EPERM;
Vladimir Davydov69828dc2015-04-16 12:47:35 -07003759
Thomas Gleixner62ab4502009-04-04 21:01:06 +00003760 return do_send_specific(tgid, pid, sig, info);
3761}
3762
3763SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
3764 siginfo_t __user *, uinfo)
3765{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003766 kernel_siginfo_t info;
Eric W. Biederman601d5ab2018-10-05 09:02:48 +02003767 int ret = __copy_siginfo_from_user(sig, &info, uinfo);
Eric W. Biederman4cd2e0e2018-04-18 17:30:19 -05003768 if (unlikely(ret))
3769 return ret;
Thomas Gleixner62ab4502009-04-04 21:01:06 +00003770 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
3771}
3772
Al Viro9aae8fc2012-12-24 23:12:04 -05003773#ifdef CONFIG_COMPAT
3774COMPAT_SYSCALL_DEFINE4(rt_tgsigqueueinfo,
3775 compat_pid_t, tgid,
3776 compat_pid_t, pid,
3777 int, sig,
3778 struct compat_siginfo __user *, uinfo)
3779{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003780 kernel_siginfo_t info;
Eric W. Biederman601d5ab2018-10-05 09:02:48 +02003781 int ret = __copy_siginfo_from_user32(sig, &info, uinfo);
Eric W. Biederman4cd2e0e2018-04-18 17:30:19 -05003782 if (unlikely(ret))
3783 return ret;
Al Viro9aae8fc2012-12-24 23:12:04 -05003784 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
3785}
3786#endif
3787
Oleg Nesterov03417292014-06-06 14:36:53 -07003788/*
Oleg Nesterovb4e74262014-06-06 14:37:00 -07003789 * For kthreads only, must not be used if cloned with CLONE_SIGHAND
Oleg Nesterov03417292014-06-06 14:36:53 -07003790 */
Oleg Nesterovb4e74262014-06-06 14:37:00 -07003791void kernel_sigaction(int sig, __sighandler_t action)
Oleg Nesterov03417292014-06-06 14:36:53 -07003792{
Oleg Nesterovec5955b2014-06-06 14:36:57 -07003793 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterovb4e74262014-06-06 14:37:00 -07003794 current->sighand->action[sig - 1].sa.sa_handler = action;
3795 if (action == SIG_IGN) {
3796 sigset_t mask;
3797
3798 sigemptyset(&mask);
3799 sigaddset(&mask, sig);
3800
3801 flush_sigqueue_mask(&mask, &current->signal->shared_pending);
3802 flush_sigqueue_mask(&mask, &current->pending);
3803 recalc_sigpending();
3804 }
Oleg Nesterov03417292014-06-06 14:36:53 -07003805 spin_unlock_irq(&current->sighand->siglock);
3806}
Oleg Nesterovb4e74262014-06-06 14:37:00 -07003807EXPORT_SYMBOL(kernel_sigaction);
Oleg Nesterov03417292014-06-06 14:36:53 -07003808
Dmitry Safonov68463512016-09-05 16:33:08 +03003809void __weak sigaction_compat_abi(struct k_sigaction *act,
3810 struct k_sigaction *oact)
3811{
3812}
3813
Oleg Nesterov88531f72006-03-28 16:11:24 -08003814int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815{
Oleg Nesterovafe2b032014-06-06 14:36:51 -07003816 struct task_struct *p = current, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 struct k_sigaction *k;
George Anzinger71fabd5e2006-01-08 01:02:48 -08003818 sigset_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819
Jesper Juhl7ed20e12005-05-01 08:59:14 -07003820 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 return -EINVAL;
3822
Oleg Nesterovafe2b032014-06-06 14:36:51 -07003823 k = &p->sighand->action[sig-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824
Oleg Nesterovafe2b032014-06-06 14:36:51 -07003825 spin_lock_irq(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 if (oact)
3827 *oact = *k;
3828
Dmitry Safonov68463512016-09-05 16:33:08 +03003829 sigaction_compat_abi(act, oact);
3830
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 if (act) {
Oleg Nesterov9ac95f22006-02-09 22:41:50 +03003832 sigdelsetmask(&act->sa.sa_mask,
3833 sigmask(SIGKILL) | sigmask(SIGSTOP));
Oleg Nesterov88531f72006-03-28 16:11:24 -08003834 *k = *act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 /*
3836 * POSIX 3.3.1.3:
3837 * "Setting a signal action to SIG_IGN for a signal that is
3838 * pending shall cause the pending signal to be discarded,
3839 * whether or not it is blocked."
3840 *
3841 * "Setting a signal action to SIG_DFL for a signal that is
3842 * pending and whose default action is to ignore the signal
3843 * (for example, SIGCHLD), shall cause the pending signal to
3844 * be discarded, whether or not it is blocked"
3845 */
Oleg Nesterovafe2b032014-06-06 14:36:51 -07003846 if (sig_handler_ignored(sig_handler(p, sig), sig)) {
George Anzinger71fabd5e2006-01-08 01:02:48 -08003847 sigemptyset(&mask);
3848 sigaddset(&mask, sig);
Oleg Nesterovafe2b032014-06-06 14:36:51 -07003849 flush_sigqueue_mask(&mask, &p->signal->shared_pending);
3850 for_each_thread(p, t)
Oleg Nesterovc09c1442014-06-06 14:36:50 -07003851 flush_sigqueue_mask(&mask, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853 }
3854
Oleg Nesterovafe2b032014-06-06 14:36:51 -07003855 spin_unlock_irq(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 return 0;
3857}
3858
Oleg Nesterovc09c1442014-06-06 14:36:50 -07003859static int
Will Deacon22839862018-09-05 15:34:42 +01003860do_sigaltstack (const stack_t *ss, stack_t *oss, unsigned long sp,
3861 size_t min_ss_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862{
Al Virobcfe8ad2017-05-27 00:29:34 -04003863 struct task_struct *t = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864
Al Virobcfe8ad2017-05-27 00:29:34 -04003865 if (oss) {
3866 memset(oss, 0, sizeof(stack_t));
3867 oss->ss_sp = (void __user *) t->sas_ss_sp;
3868 oss->ss_size = t->sas_ss_size;
3869 oss->ss_flags = sas_ss_flags(sp) |
3870 (current->sas_ss_flags & SS_FLAG_BITS);
3871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872
Al Virobcfe8ad2017-05-27 00:29:34 -04003873 if (ss) {
3874 void __user *ss_sp = ss->ss_sp;
3875 size_t ss_size = ss->ss_size;
3876 unsigned ss_flags = ss->ss_flags;
Stas Sergeev407bc162016-04-14 23:20:03 +03003877 int ss_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878
Al Virobcfe8ad2017-05-27 00:29:34 -04003879 if (unlikely(on_sig_stack(sp)))
3880 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
Stas Sergeev407bc162016-04-14 23:20:03 +03003882 ss_mode = ss_flags & ~SS_FLAG_BITS;
Al Virobcfe8ad2017-05-27 00:29:34 -04003883 if (unlikely(ss_mode != SS_DISABLE && ss_mode != SS_ONSTACK &&
3884 ss_mode != 0))
3885 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886
Stas Sergeev407bc162016-04-14 23:20:03 +03003887 if (ss_mode == SS_DISABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 ss_size = 0;
3889 ss_sp = NULL;
3890 } else {
Will Deacon22839862018-09-05 15:34:42 +01003891 if (unlikely(ss_size < min_ss_size))
Al Virobcfe8ad2017-05-27 00:29:34 -04003892 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 }
3894
Al Virobcfe8ad2017-05-27 00:29:34 -04003895 t->sas_ss_sp = (unsigned long) ss_sp;
3896 t->sas_ss_size = ss_size;
3897 t->sas_ss_flags = ss_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 }
Al Virobcfe8ad2017-05-27 00:29:34 -04003899 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900}
Al Virobcfe8ad2017-05-27 00:29:34 -04003901
Al Viro6bf9adf2012-12-14 14:09:47 -05003902SYSCALL_DEFINE2(sigaltstack,const stack_t __user *,uss, stack_t __user *,uoss)
3903{
Al Virobcfe8ad2017-05-27 00:29:34 -04003904 stack_t new, old;
3905 int err;
3906 if (uss && copy_from_user(&new, uss, sizeof(stack_t)))
3907 return -EFAULT;
3908 err = do_sigaltstack(uss ? &new : NULL, uoss ? &old : NULL,
Will Deacon22839862018-09-05 15:34:42 +01003909 current_user_stack_pointer(),
3910 MINSIGSTKSZ);
Al Virobcfe8ad2017-05-27 00:29:34 -04003911 if (!err && uoss && copy_to_user(uoss, &old, sizeof(stack_t)))
3912 err = -EFAULT;
3913 return err;
Al Viro6bf9adf2012-12-14 14:09:47 -05003914}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915
Al Viro5c495742012-11-18 15:29:16 -05003916int restore_altstack(const stack_t __user *uss)
3917{
Al Virobcfe8ad2017-05-27 00:29:34 -04003918 stack_t new;
3919 if (copy_from_user(&new, uss, sizeof(stack_t)))
3920 return -EFAULT;
Will Deacon22839862018-09-05 15:34:42 +01003921 (void)do_sigaltstack(&new, NULL, current_user_stack_pointer(),
3922 MINSIGSTKSZ);
Al Viro5c495742012-11-18 15:29:16 -05003923 /* squash all but EFAULT for now */
Al Virobcfe8ad2017-05-27 00:29:34 -04003924 return 0;
Al Viro5c495742012-11-18 15:29:16 -05003925}
3926
Al Viroc40702c2012-11-20 14:24:26 -05003927int __save_altstack(stack_t __user *uss, unsigned long sp)
3928{
3929 struct task_struct *t = current;
Stas Sergeev2a742132016-04-14 23:20:04 +03003930 int err = __put_user((void __user *)t->sas_ss_sp, &uss->ss_sp) |
3931 __put_user(t->sas_ss_flags, &uss->ss_flags) |
Al Viroc40702c2012-11-20 14:24:26 -05003932 __put_user(t->sas_ss_size, &uss->ss_size);
Stas Sergeev2a742132016-04-14 23:20:04 +03003933 if (err)
3934 return err;
3935 if (t->sas_ss_flags & SS_AUTODISARM)
3936 sas_ss_reset(t);
3937 return 0;
Al Viroc40702c2012-11-20 14:24:26 -05003938}
3939
Al Viro90268432012-12-14 14:47:53 -05003940#ifdef CONFIG_COMPAT
Dominik Brodowski6203deb2018-03-17 17:11:51 +01003941static int do_compat_sigaltstack(const compat_stack_t __user *uss_ptr,
3942 compat_stack_t __user *uoss_ptr)
Al Viro90268432012-12-14 14:47:53 -05003943{
3944 stack_t uss, uoss;
3945 int ret;
Al Viro90268432012-12-14 14:47:53 -05003946
3947 if (uss_ptr) {
3948 compat_stack_t uss32;
Al Viro90268432012-12-14 14:47:53 -05003949 if (copy_from_user(&uss32, uss_ptr, sizeof(compat_stack_t)))
3950 return -EFAULT;
3951 uss.ss_sp = compat_ptr(uss32.ss_sp);
3952 uss.ss_flags = uss32.ss_flags;
3953 uss.ss_size = uss32.ss_size;
3954 }
Al Virobcfe8ad2017-05-27 00:29:34 -04003955 ret = do_sigaltstack(uss_ptr ? &uss : NULL, &uoss,
Will Deacon22839862018-09-05 15:34:42 +01003956 compat_user_stack_pointer(),
3957 COMPAT_MINSIGSTKSZ);
Al Viro90268432012-12-14 14:47:53 -05003958 if (ret >= 0 && uoss_ptr) {
Al Virobcfe8ad2017-05-27 00:29:34 -04003959 compat_stack_t old;
3960 memset(&old, 0, sizeof(old));
3961 old.ss_sp = ptr_to_compat(uoss.ss_sp);
3962 old.ss_flags = uoss.ss_flags;
3963 old.ss_size = uoss.ss_size;
3964 if (copy_to_user(uoss_ptr, &old, sizeof(compat_stack_t)))
Al Viro90268432012-12-14 14:47:53 -05003965 ret = -EFAULT;
3966 }
3967 return ret;
3968}
3969
Dominik Brodowski6203deb2018-03-17 17:11:51 +01003970COMPAT_SYSCALL_DEFINE2(sigaltstack,
3971 const compat_stack_t __user *, uss_ptr,
3972 compat_stack_t __user *, uoss_ptr)
3973{
3974 return do_compat_sigaltstack(uss_ptr, uoss_ptr);
3975}
3976
Al Viro90268432012-12-14 14:47:53 -05003977int compat_restore_altstack(const compat_stack_t __user *uss)
3978{
Dominik Brodowski6203deb2018-03-17 17:11:51 +01003979 int err = do_compat_sigaltstack(uss, NULL);
Al Viro90268432012-12-14 14:47:53 -05003980 /* squash all but -EFAULT for now */
3981 return err == -EFAULT ? err : 0;
3982}
Al Viroc40702c2012-11-20 14:24:26 -05003983
3984int __compat_save_altstack(compat_stack_t __user *uss, unsigned long sp)
3985{
Stas Sergeev441398d2017-02-27 14:27:25 -08003986 int err;
Al Viroc40702c2012-11-20 14:24:26 -05003987 struct task_struct *t = current;
Stas Sergeev441398d2017-02-27 14:27:25 -08003988 err = __put_user(ptr_to_compat((void __user *)t->sas_ss_sp),
3989 &uss->ss_sp) |
3990 __put_user(t->sas_ss_flags, &uss->ss_flags) |
Al Viroc40702c2012-11-20 14:24:26 -05003991 __put_user(t->sas_ss_size, &uss->ss_size);
Stas Sergeev441398d2017-02-27 14:27:25 -08003992 if (err)
3993 return err;
3994 if (t->sas_ss_flags & SS_AUTODISARM)
3995 sas_ss_reset(t);
3996 return 0;
Al Viroc40702c2012-11-20 14:24:26 -05003997}
Al Viro90268432012-12-14 14:47:53 -05003998#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999
4000#ifdef __ARCH_WANT_SYS_SIGPENDING
4001
Randy Dunlap41c57892011-04-04 15:00:26 -07004002/**
4003 * sys_sigpending - examine pending signals
Dominik Brodowskid53238c2018-03-11 11:34:37 +01004004 * @uset: where mask of pending signal is returned
Randy Dunlap41c57892011-04-04 15:00:26 -07004005 */
Dominik Brodowskid53238c2018-03-11 11:34:37 +01004006SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, uset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007{
Dominik Brodowskid53238c2018-03-11 11:34:37 +01004008 sigset_t set;
Dominik Brodowskid53238c2018-03-11 11:34:37 +01004009
4010 if (sizeof(old_sigset_t) > sizeof(*uset))
4011 return -EINVAL;
4012
Christian Braunerb1d294c2018-08-21 22:00:02 -07004013 do_sigpending(&set);
4014
4015 if (copy_to_user(uset, &set, sizeof(old_sigset_t)))
4016 return -EFAULT;
4017
4018 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019}
4020
Al Viro8f136212017-05-31 04:42:07 -04004021#ifdef CONFIG_COMPAT
4022COMPAT_SYSCALL_DEFINE1(sigpending, compat_old_sigset_t __user *, set32)
4023{
4024 sigset_t set;
Christian Braunerb1d294c2018-08-21 22:00:02 -07004025
4026 do_sigpending(&set);
4027
4028 return put_user(set.sig[0], set32);
Al Viro8f136212017-05-31 04:42:07 -04004029}
4030#endif
4031
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032#endif
4033
4034#ifdef __ARCH_WANT_SYS_SIGPROCMASK
Randy Dunlap41c57892011-04-04 15:00:26 -07004035/**
4036 * sys_sigprocmask - examine and change blocked signals
4037 * @how: whether to add, remove, or set signals
Oleg Nesterovb013c392011-04-28 11:36:20 +02004038 * @nset: signals to add or remove (if non-null)
Randy Dunlap41c57892011-04-04 15:00:26 -07004039 * @oset: previous value of signal mask if non-null
4040 *
Randy Dunlap5aba0852011-04-04 14:59:31 -07004041 * Some platforms have their own version with special arguments;
4042 * others support only sys_rt_sigprocmask.
4043 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
Oleg Nesterovb013c392011-04-28 11:36:20 +02004045SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, nset,
Heiko Carstensb290ebe2009-01-14 14:14:06 +01004046 old_sigset_t __user *, oset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 old_sigset_t old_set, new_set;
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02004049 sigset_t new_blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050
Oleg Nesterovb013c392011-04-28 11:36:20 +02004051 old_set = current->blocked.sig[0];
4052
4053 if (nset) {
4054 if (copy_from_user(&new_set, nset, sizeof(*nset)))
4055 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02004057 new_blocked = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 switch (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 case SIG_BLOCK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02004061 sigaddsetmask(&new_blocked, new_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 break;
4063 case SIG_UNBLOCK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02004064 sigdelsetmask(&new_blocked, new_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 break;
4066 case SIG_SETMASK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02004067 new_blocked.sig[0] = new_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 break;
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02004069 default:
4070 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 }
4072
Oleg Nesterov0c4a8422013-01-05 19:13:29 +01004073 set_current_blocked(&new_blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 }
Oleg Nesterovb013c392011-04-28 11:36:20 +02004075
4076 if (oset) {
4077 if (copy_to_user(oset, &old_set, sizeof(*oset)))
4078 return -EFAULT;
4079 }
4080
4081 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082}
4083#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
4084
Al Viroeaca6ea2012-11-25 23:12:10 -05004085#ifndef CONFIG_ODD_RT_SIGACTION
Randy Dunlap41c57892011-04-04 15:00:26 -07004086/**
4087 * sys_rt_sigaction - alter an action taken by a process
4088 * @sig: signal to be sent
Randy Dunlapf9fa0bc2011-04-08 10:53:46 -07004089 * @act: new sigaction
4090 * @oact: used to save the previous sigaction
Randy Dunlap41c57892011-04-04 15:00:26 -07004091 * @sigsetsize: size of sigset_t type
4092 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01004093SYSCALL_DEFINE4(rt_sigaction, int, sig,
4094 const struct sigaction __user *, act,
4095 struct sigaction __user *, oact,
4096 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097{
4098 struct k_sigaction new_sa, old_sa;
Christian Braunerd8f993b2018-08-21 22:00:07 -07004099 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100
4101 /* XXX: Don't preclude handling different sized sigset_t's. */
4102 if (sigsetsize != sizeof(sigset_t))
Christian Braunerd8f993b2018-08-21 22:00:07 -07004103 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104
Christian Braunerd8f993b2018-08-21 22:00:07 -07004105 if (act && copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
4106 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107
4108 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
Christian Braunerd8f993b2018-08-21 22:00:07 -07004109 if (ret)
4110 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111
Christian Braunerd8f993b2018-08-21 22:00:07 -07004112 if (oact && copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
4113 return -EFAULT;
4114
4115 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116}
Al Viro08d32fe2012-12-25 18:38:15 -05004117#ifdef CONFIG_COMPAT
Al Viro08d32fe2012-12-25 18:38:15 -05004118COMPAT_SYSCALL_DEFINE4(rt_sigaction, int, sig,
4119 const struct compat_sigaction __user *, act,
4120 struct compat_sigaction __user *, oact,
4121 compat_size_t, sigsetsize)
4122{
4123 struct k_sigaction new_ka, old_ka;
Al Viro08d32fe2012-12-25 18:38:15 -05004124#ifdef __ARCH_HAS_SA_RESTORER
4125 compat_uptr_t restorer;
4126#endif
4127 int ret;
4128
4129 /* XXX: Don't preclude handling different sized sigset_t's. */
4130 if (sigsetsize != sizeof(compat_sigset_t))
4131 return -EINVAL;
4132
4133 if (act) {
4134 compat_uptr_t handler;
4135 ret = get_user(handler, &act->sa_handler);
4136 new_ka.sa.sa_handler = compat_ptr(handler);
4137#ifdef __ARCH_HAS_SA_RESTORER
4138 ret |= get_user(restorer, &act->sa_restorer);
4139 new_ka.sa.sa_restorer = compat_ptr(restorer);
4140#endif
Al Viro3968cf62017-09-03 21:45:17 -04004141 ret |= get_compat_sigset(&new_ka.sa.sa_mask, &act->sa_mask);
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07004142 ret |= get_user(new_ka.sa.sa_flags, &act->sa_flags);
Al Viro08d32fe2012-12-25 18:38:15 -05004143 if (ret)
4144 return -EFAULT;
Al Viro08d32fe2012-12-25 18:38:15 -05004145 }
4146
4147 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
4148 if (!ret && oact) {
Al Viro08d32fe2012-12-25 18:38:15 -05004149 ret = put_user(ptr_to_compat(old_ka.sa.sa_handler),
4150 &oact->sa_handler);
Dmitry V. Levinf4543222017-08-22 02:16:11 +03004151 ret |= put_compat_sigset(&oact->sa_mask, &old_ka.sa.sa_mask,
4152 sizeof(oact->sa_mask));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07004153 ret |= put_user(old_ka.sa.sa_flags, &oact->sa_flags);
Al Viro08d32fe2012-12-25 18:38:15 -05004154#ifdef __ARCH_HAS_SA_RESTORER
4155 ret |= put_user(ptr_to_compat(old_ka.sa.sa_restorer),
4156 &oact->sa_restorer);
4157#endif
4158 }
4159 return ret;
4160}
4161#endif
Al Viroeaca6ea2012-11-25 23:12:10 -05004162#endif /* !CONFIG_ODD_RT_SIGACTION */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
Al Viro495dfbf2012-12-25 19:09:45 -05004164#ifdef CONFIG_OLD_SIGACTION
4165SYSCALL_DEFINE3(sigaction, int, sig,
4166 const struct old_sigaction __user *, act,
4167 struct old_sigaction __user *, oact)
4168{
4169 struct k_sigaction new_ka, old_ka;
4170 int ret;
4171
4172 if (act) {
4173 old_sigset_t mask;
Linus Torvalds96d4f262019-01-03 18:57:57 -08004174 if (!access_ok(act, sizeof(*act)) ||
Al Viro495dfbf2012-12-25 19:09:45 -05004175 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
4176 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
4177 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
4178 __get_user(mask, &act->sa_mask))
4179 return -EFAULT;
4180#ifdef __ARCH_HAS_KA_RESTORER
4181 new_ka.ka_restorer = NULL;
4182#endif
4183 siginitset(&new_ka.sa.sa_mask, mask);
4184 }
4185
4186 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
4187
4188 if (!ret && oact) {
Linus Torvalds96d4f262019-01-03 18:57:57 -08004189 if (!access_ok(oact, sizeof(*oact)) ||
Al Viro495dfbf2012-12-25 19:09:45 -05004190 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
4191 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
4192 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
4193 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
4194 return -EFAULT;
4195 }
4196
4197 return ret;
4198}
4199#endif
4200#ifdef CONFIG_COMPAT_OLD_SIGACTION
4201COMPAT_SYSCALL_DEFINE3(sigaction, int, sig,
4202 const struct compat_old_sigaction __user *, act,
4203 struct compat_old_sigaction __user *, oact)
4204{
4205 struct k_sigaction new_ka, old_ka;
4206 int ret;
4207 compat_old_sigset_t mask;
4208 compat_uptr_t handler, restorer;
4209
4210 if (act) {
Linus Torvalds96d4f262019-01-03 18:57:57 -08004211 if (!access_ok(act, sizeof(*act)) ||
Al Viro495dfbf2012-12-25 19:09:45 -05004212 __get_user(handler, &act->sa_handler) ||
4213 __get_user(restorer, &act->sa_restorer) ||
4214 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
4215 __get_user(mask, &act->sa_mask))
4216 return -EFAULT;
4217
4218#ifdef __ARCH_HAS_KA_RESTORER
4219 new_ka.ka_restorer = NULL;
4220#endif
4221 new_ka.sa.sa_handler = compat_ptr(handler);
4222 new_ka.sa.sa_restorer = compat_ptr(restorer);
4223 siginitset(&new_ka.sa.sa_mask, mask);
4224 }
4225
4226 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
4227
4228 if (!ret && oact) {
Linus Torvalds96d4f262019-01-03 18:57:57 -08004229 if (!access_ok(oact, sizeof(*oact)) ||
Al Viro495dfbf2012-12-25 19:09:45 -05004230 __put_user(ptr_to_compat(old_ka.sa.sa_handler),
4231 &oact->sa_handler) ||
4232 __put_user(ptr_to_compat(old_ka.sa.sa_restorer),
4233 &oact->sa_restorer) ||
4234 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
4235 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
4236 return -EFAULT;
4237 }
4238 return ret;
4239}
4240#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
Fabian Frederickf6187762014-06-04 16:11:12 -07004242#ifdef CONFIG_SGETMASK_SYSCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
4244/*
4245 * For backwards compatibility. Functionality superseded by sigprocmask.
4246 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01004247SYSCALL_DEFINE0(sgetmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248{
4249 /* SMP safe */
4250 return current->blocked.sig[0];
4251}
4252
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01004253SYSCALL_DEFINE1(ssetmask, int, newmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254{
Oleg Nesterovc1095c62011-07-27 12:49:44 -07004255 int old = current->blocked.sig[0];
4256 sigset_t newset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257
Oleg Nesterov5ba53ff2013-01-05 19:13:13 +01004258 siginitset(&newset, newmask);
Oleg Nesterovc1095c62011-07-27 12:49:44 -07004259 set_current_blocked(&newset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
4261 return old;
4262}
Fabian Frederickf6187762014-06-04 16:11:12 -07004263#endif /* CONFIG_SGETMASK_SYSCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
4265#ifdef __ARCH_WANT_SYS_SIGNAL
4266/*
4267 * For backwards compatibility. Functionality superseded by sigaction.
4268 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01004269SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270{
4271 struct k_sigaction new_sa, old_sa;
4272 int ret;
4273
4274 new_sa.sa.sa_handler = handler;
4275 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
Oleg Nesterovc70d3d702006-02-09 22:41:41 +03004276 sigemptyset(&new_sa.sa.sa_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
4278 ret = do_sigaction(sig, &new_sa, &old_sa);
4279
4280 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
4281}
4282#endif /* __ARCH_WANT_SYS_SIGNAL */
4283
4284#ifdef __ARCH_WANT_SYS_PAUSE
4285
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01004286SYSCALL_DEFINE0(pause)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004287{
Oleg Nesterovd92fcf02011-05-25 19:22:27 +02004288 while (!signal_pending(current)) {
Davidlohr Bueso1df01352015-02-17 13:45:41 -08004289 __set_current_state(TASK_INTERRUPTIBLE);
Oleg Nesterovd92fcf02011-05-25 19:22:27 +02004290 schedule();
4291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 return -ERESTARTNOHAND;
4293}
4294
4295#endif
4296
Richard Weinberger9d8a7652015-11-20 15:57:21 -08004297static int sigsuspend(sigset_t *set)
Al Viro68f3f162012-05-21 21:42:32 -04004298{
Al Viro68f3f162012-05-21 21:42:32 -04004299 current->saved_sigmask = current->blocked;
4300 set_current_blocked(set);
4301
Sasha Levin823dd322016-02-05 15:36:05 -08004302 while (!signal_pending(current)) {
4303 __set_current_state(TASK_INTERRUPTIBLE);
4304 schedule();
4305 }
Al Viro68f3f162012-05-21 21:42:32 -04004306 set_restore_sigmask();
4307 return -ERESTARTNOHAND;
4308}
Al Viro68f3f162012-05-21 21:42:32 -04004309
Randy Dunlap41c57892011-04-04 15:00:26 -07004310/**
4311 * sys_rt_sigsuspend - replace the signal mask for a value with the
4312 * @unewset value until a signal is received
4313 * @unewset: new signal mask value
4314 * @sigsetsize: size of sigset_t type
4315 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01004316SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
David Woodhouse150256d2006-01-18 17:43:57 -08004317{
4318 sigset_t newset;
4319
4320 /* XXX: Don't preclude handling different sized sigset_t's. */
4321 if (sigsetsize != sizeof(sigset_t))
4322 return -EINVAL;
4323
4324 if (copy_from_user(&newset, unewset, sizeof(newset)))
4325 return -EFAULT;
Al Viro68f3f162012-05-21 21:42:32 -04004326 return sigsuspend(&newset);
David Woodhouse150256d2006-01-18 17:43:57 -08004327}
Al Viroad4b65a2012-12-24 21:43:56 -05004328
4329#ifdef CONFIG_COMPAT
4330COMPAT_SYSCALL_DEFINE2(rt_sigsuspend, compat_sigset_t __user *, unewset, compat_size_t, sigsetsize)
4331{
Al Viroad4b65a2012-12-24 21:43:56 -05004332 sigset_t newset;
Al Viroad4b65a2012-12-24 21:43:56 -05004333
4334 /* XXX: Don't preclude handling different sized sigset_t's. */
4335 if (sigsetsize != sizeof(sigset_t))
4336 return -EINVAL;
4337
Al Viro3968cf62017-09-03 21:45:17 -04004338 if (get_compat_sigset(&newset, unewset))
Al Viroad4b65a2012-12-24 21:43:56 -05004339 return -EFAULT;
Al Viroad4b65a2012-12-24 21:43:56 -05004340 return sigsuspend(&newset);
Al Viroad4b65a2012-12-24 21:43:56 -05004341}
4342#endif
David Woodhouse150256d2006-01-18 17:43:57 -08004343
Al Viro0a0e8cd2012-12-25 16:04:12 -05004344#ifdef CONFIG_OLD_SIGSUSPEND
4345SYSCALL_DEFINE1(sigsuspend, old_sigset_t, mask)
4346{
4347 sigset_t blocked;
4348 siginitset(&blocked, mask);
4349 return sigsuspend(&blocked);
4350}
4351#endif
4352#ifdef CONFIG_OLD_SIGSUSPEND3
4353SYSCALL_DEFINE3(sigsuspend, int, unused1, int, unused2, old_sigset_t, mask)
4354{
4355 sigset_t blocked;
4356 siginitset(&blocked, mask);
4357 return sigsuspend(&blocked);
4358}
4359#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07004361__weak const char *arch_vma_name(struct vm_area_struct *vma)
David Howellsf269fdd2006-09-27 01:50:23 -07004362{
4363 return NULL;
4364}
4365
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004366static inline void siginfo_buildtime_checks(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367{
Eric W. Biedermanaba1be22017-07-19 21:23:15 -05004368 BUILD_BUG_ON(sizeof(struct siginfo) != SI_MAX_SIZE);
Helge Deller41b27152016-03-22 14:27:54 -07004369
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004370 /* Verify the offsets in the two siginfos match */
4371#define CHECK_OFFSET(field) \
4372 BUILD_BUG_ON(offsetof(siginfo_t, field) != offsetof(kernel_siginfo_t, field))
4373
4374 /* kill */
4375 CHECK_OFFSET(si_pid);
4376 CHECK_OFFSET(si_uid);
4377
4378 /* timer */
4379 CHECK_OFFSET(si_tid);
4380 CHECK_OFFSET(si_overrun);
4381 CHECK_OFFSET(si_value);
4382
4383 /* rt */
4384 CHECK_OFFSET(si_pid);
4385 CHECK_OFFSET(si_uid);
4386 CHECK_OFFSET(si_value);
4387
4388 /* sigchld */
4389 CHECK_OFFSET(si_pid);
4390 CHECK_OFFSET(si_uid);
4391 CHECK_OFFSET(si_status);
4392 CHECK_OFFSET(si_utime);
4393 CHECK_OFFSET(si_stime);
4394
4395 /* sigfault */
4396 CHECK_OFFSET(si_addr);
4397 CHECK_OFFSET(si_addr_lsb);
4398 CHECK_OFFSET(si_lower);
4399 CHECK_OFFSET(si_upper);
4400 CHECK_OFFSET(si_pkey);
4401
4402 /* sigpoll */
4403 CHECK_OFFSET(si_band);
4404 CHECK_OFFSET(si_fd);
4405
4406 /* sigsys */
4407 CHECK_OFFSET(si_call_addr);
4408 CHECK_OFFSET(si_syscall);
4409 CHECK_OFFSET(si_arch);
4410#undef CHECK_OFFSET
4411}
4412
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413void __init signals_init(void)
4414{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004415 siginfo_buildtime_checks();
Jason Wessel67fc4e02010-05-20 21:04:21 -05004416
4417 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
4418}
4419
4420#ifdef CONFIG_KGDB_KDB
4421#include <linux/kdb.h>
4422/*
Eric W. Biederman0b44bf92017-08-17 15:45:38 -05004423 * kdb_send_sig - Allows kdb to send signals without exposing
Jason Wessel67fc4e02010-05-20 21:04:21 -05004424 * signal internals. This function checks if the required locks are
4425 * available before calling the main signal code, to avoid kdb
4426 * deadlocks.
4427 */
Eric W. Biederman0b44bf92017-08-17 15:45:38 -05004428void kdb_send_sig(struct task_struct *t, int sig)
Jason Wessel67fc4e02010-05-20 21:04:21 -05004429{
4430 static struct task_struct *kdb_prev_t;
Eric W. Biederman0b44bf92017-08-17 15:45:38 -05004431 int new_t, ret;
Jason Wessel67fc4e02010-05-20 21:04:21 -05004432 if (!spin_trylock(&t->sighand->siglock)) {
4433 kdb_printf("Can't do kill command now.\n"
4434 "The sigmask lock is held somewhere else in "
4435 "kernel, try again later\n");
4436 return;
4437 }
Jason Wessel67fc4e02010-05-20 21:04:21 -05004438 new_t = kdb_prev_t != t;
4439 kdb_prev_t = t;
4440 if (t->state != TASK_RUNNING && new_t) {
Eric W. Biederman0b44bf92017-08-17 15:45:38 -05004441 spin_unlock(&t->sighand->siglock);
Jason Wessel67fc4e02010-05-20 21:04:21 -05004442 kdb_printf("Process is not RUNNING, sending a signal from "
4443 "kdb risks deadlock\n"
4444 "on the run queue locks. "
4445 "The signal has _not_ been sent.\n"
4446 "Reissue the kill command if you want to risk "
4447 "the deadlock.\n");
4448 return;
4449 }
Eric W. Biedermanb2139842018-07-20 15:49:17 -05004450 ret = send_signal(sig, SEND_SIG_PRIV, t, PIDTYPE_PID);
Eric W. Biederman0b44bf92017-08-17 15:45:38 -05004451 spin_unlock(&t->sighand->siglock);
4452 if (ret)
Jason Wessel67fc4e02010-05-20 21:04:21 -05004453 kdb_printf("Fail to deliver Signal %d to process %d.\n",
4454 sig, t->pid);
4455 else
4456 kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
4457}
4458#endif /* CONFIG_KGDB_KDB */