blob: b5370fe5c198f22b0fa637f54bdab5a672ff1fc5 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/kernel/signal.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 *
7 * 1997-11-02 Modified for POSIX.1b signals by Richard Henderson
8 *
9 * 2003-06-02 Jim Houston - Concurrent Computer Corp.
10 * Changes to use preallocated sigqueue structures
11 * to allow signals to be sent reliably.
12 */
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/slab.h>
Paul Gortmaker9984de12011-05-23 14:51:41 -040015#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/init.h>
Ingo Molnar589ee622017-02-04 00:16:44 +010017#include <linux/sched/mm.h>
Ingo Molnar8703e8a2017-02-08 18:51:30 +010018#include <linux/sched/user.h>
Ingo Molnarb17b0152017-02-08 18:51:35 +010019#include <linux/sched/debug.h>
Ingo Molnar29930022017-02-08 18:51:36 +010020#include <linux/sched/task.h>
Ingo Molnar68db0cf2017-02-08 18:51:37 +010021#include <linux/sched/task_stack.h>
Ingo Molnar32ef5512017-02-05 11:48:36 +010022#include <linux/sched/cputime.h>
Christian Brauner3eb39f42018-11-19 00:51:56 +010023#include <linux/file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/fs.h>
Christian Brauner3eb39f42018-11-19 00:51:56 +010025#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/tty.h>
27#include <linux/binfmts.h>
Alex Kelly179899fd2012-10-04 17:15:24 -070028#include <linux/coredump.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/security.h>
30#include <linux/syscalls.h>
31#include <linux/ptrace.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070032#include <linux/signal.h>
Davide Libenzifba2afa2007-05-10 22:23:13 -070033#include <linux/signalfd.h>
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +090034#include <linux/ratelimit.h>
Eric W. Biederman355f8412022-02-09 12:47:08 -060035#include <linux/task_work.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080036#include <linux/capability.h>
Nigel Cunningham7dfb7102006-12-06 20:34:23 -080037#include <linux/freezer.h>
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -080038#include <linux/pid_namespace.h>
39#include <linux/nsproxy.h>
Serge E. Hallyn6b550f92012-01-10 15:11:37 -080040#include <linux/user_namespace.h>
Srikar Dronamraju0326f5a92012-03-13 23:30:11 +053041#include <linux/uprobes.h>
Al Viro90268432012-12-14 14:47:53 -050042#include <linux/compat.h>
Jesper Derehag2b5faa42013-03-19 20:50:05 +000043#include <linux/cn_proc.h>
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -070044#include <linux/compiler.h>
Christoph Hellwig31ea70e2017-06-03 21:01:00 +020045#include <linux/posix-timers.h>
Roman Gushchin76f969e2019-04-19 10:03:04 -070046#include <linux/cgroup.h>
Richard Guy Briggsb48345a2019-05-10 12:21:49 -040047#include <linux/audit.h>
Luis Chamberlain01e6aac2023-05-18 13:37:41 -070048#include <linux/sysctl.h>
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -070049
Masami Hiramatsud1eb6502009-11-24 16:56:45 -050050#define CREATE_TRACE_POINTS
51#include <trace/events/signal.h>
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -080052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <asm/param.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080054#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/unistd.h>
56#include <asm/siginfo.h>
David Howellsd550bbd2012-03-28 18:30:03 +010057#include <asm/cacheflush.h>
Eric W. Biederman307d5222021-06-23 16:44:32 -050058#include <asm/syscall.h> /* for syscall_get_* */
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60/*
61 * SLAB caches for signal bits.
62 */
63
Christoph Lametere18b8902006-12-06 20:33:20 -080064static struct kmem_cache *sigqueue_cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +090066int print_fatal_signals __read_mostly;
67
Roland McGrath35de2542008-07-25 19:45:51 -070068static void __user *sig_handler(struct task_struct *t, int sig)
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070069{
Roland McGrath35de2542008-07-25 19:45:51 -070070 return t->sighand->action[sig - 1].sa.sa_handler;
71}
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070072
Christian Braunere4a8b4e2018-08-21 22:00:15 -070073static inline bool sig_handler_ignored(void __user *handler, int sig)
Roland McGrath35de2542008-07-25 19:45:51 -070074{
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070075 /* Is it explicitly or implicitly ignored? */
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070076 return handler == SIG_IGN ||
Christian Braunere4a8b4e2018-08-21 22:00:15 -070077 (handler == SIG_DFL && sig_kernel_ignore(sig));
Pavel Emelyanov93585ee2008-04-30 00:52:39 -070078}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Christian Brauner41aaa482018-08-21 22:00:19 -070080static bool sig_task_ignored(struct task_struct *t, int sig, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Roland McGrath35de2542008-07-25 19:45:51 -070082 void __user *handler;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Oleg Nesterovf008faf2009-04-02 16:58:02 -070084 handler = sig_handler(t, sig);
85
Eric W. Biederman86989c42018-07-19 19:47:27 -050086 /* SIGKILL and SIGSTOP may not be sent to the global init */
87 if (unlikely(is_global_init(t) && sig_kernel_only(sig)))
88 return true;
89
Oleg Nesterovf008faf2009-04-02 16:58:02 -070090 if (unlikely(t->signal->flags & SIGNAL_UNKILLABLE) &&
Oleg Nesterovac2538502017-11-17 15:30:04 -080091 handler == SIG_DFL && !(force && sig_kernel_only(sig)))
Christian Brauner41aaa482018-08-21 22:00:19 -070092 return true;
Oleg Nesterovf008faf2009-04-02 16:58:02 -070093
Eric W. Biederman33da8e72019-08-16 12:33:54 -050094 /* Only allow kernel generated signals to this kthread */
Jens Axboee8b33b82021-03-25 18:18:59 -060095 if (unlikely((t->flags & PF_KTHREAD) &&
Eric W. Biederman33da8e72019-08-16 12:33:54 -050096 (handler == SIG_KTHREAD_KERNEL) && !force))
97 return true;
98
Oleg Nesterovf008faf2009-04-02 16:58:02 -070099 return sig_handler_ignored(handler, sig);
100}
101
Christian Brauner6a0cdcd2018-08-21 22:00:23 -0700102static bool sig_ignored(struct task_struct *t, int sig, bool force)
Oleg Nesterovf008faf2009-04-02 16:58:02 -0700103{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 /*
105 * Blocked signals are never ignored, since the
106 * signal handler may change by the time it is
107 * unblocked.
108 */
Roland McGrath325d22d2007-11-12 15:41:55 -0800109 if (sigismember(&t->blocked, sig) || sigismember(&t->real_blocked, sig))
Christian Brauner6a0cdcd2018-08-21 22:00:23 -0700110 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Oleg Nesterov628c1bc2017-11-17 15:30:01 -0800112 /*
113 * Tracers may want to know about even ignored signal unless it
114 * is SIGKILL which can't be reported anyway but can be ignored
115 * by SIGNAL_UNKILLABLE task.
116 */
117 if (t->ptrace && sig != SIGKILL)
Christian Brauner6a0cdcd2018-08-21 22:00:23 -0700118 return false;
Roland McGrath35de2542008-07-25 19:45:51 -0700119
Oleg Nesterov628c1bc2017-11-17 15:30:01 -0800120 return sig_task_ignored(t, sig, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123/*
124 * Re-calculate pending state from the set of locally pending
125 * signals, globally pending signals, and blocked signals.
126 */
Christian Brauner938696a2018-08-21 22:00:27 -0700127static inline bool has_pending_signals(sigset_t *signal, sigset_t *blocked)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 unsigned long ready;
130 long i;
131
132 switch (_NSIG_WORDS) {
133 default:
134 for (i = _NSIG_WORDS, ready = 0; --i >= 0 ;)
135 ready |= signal->sig[i] &~ blocked->sig[i];
136 break;
137
138 case 4: ready = signal->sig[3] &~ blocked->sig[3];
139 ready |= signal->sig[2] &~ blocked->sig[2];
140 ready |= signal->sig[1] &~ blocked->sig[1];
141 ready |= signal->sig[0] &~ blocked->sig[0];
142 break;
143
144 case 2: ready = signal->sig[1] &~ blocked->sig[1];
145 ready |= signal->sig[0] &~ blocked->sig[0];
146 break;
147
148 case 1: ready = signal->sig[0] &~ blocked->sig[0];
149 }
150 return ready != 0;
151}
152
153#define PENDING(p,b) has_pending_signals(&(p)->signal, (b))
154
Christian Brauner09ae8542018-08-21 22:00:30 -0700155static bool recalc_sigpending_tsk(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Roman Gushchin76f969e2019-04-19 10:03:04 -0700157 if ((t->jobctl & (JOBCTL_PENDING_MASK | JOBCTL_TRAP_FREEZE)) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 PENDING(&t->pending, &t->blocked) ||
Roman Gushchin76f969e2019-04-19 10:03:04 -0700159 PENDING(&t->signal->shared_pending, &t->blocked) ||
160 cgroup_task_frozen(t)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 set_tsk_thread_flag(t, TIF_SIGPENDING);
Christian Brauner09ae8542018-08-21 22:00:30 -0700162 return true;
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700163 }
Christian Brauner09ae8542018-08-21 22:00:30 -0700164
Roland McGrathb74d0de2007-06-06 03:59:00 -0700165 /*
166 * We must never clear the flag in another thread, or in current
167 * when it's possible the current syscall is returning -ERESTART*.
168 * So we don't clear it here, and only callers who know they should do.
169 */
Christian Brauner09ae8542018-08-21 22:00:30 -0700170 return false;
Roland McGrath7bb44ad2007-05-23 13:57:44 -0700171}
172
173/*
174 * After recalculating TIF_SIGPENDING, we need to make sure the task wakes up.
175 * This is superfluous when called on current, the wakeup is a harmless no-op.
176 */
177void recalc_sigpending_and_wake(struct task_struct *t)
178{
179 if (recalc_sigpending_tsk(t))
180 signal_wake_up(t, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
183void recalc_sigpending(void)
184{
Miroslav Benes8df19472021-03-29 15:28:15 +0200185 if (!recalc_sigpending_tsk(current) && !freezing(current))
Roland McGrathb74d0de2007-06-06 03:59:00 -0700186 clear_thread_flag(TIF_SIGPENDING);
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
Eric W. Biedermanfb50f5a2018-09-13 19:26:35 +0200189EXPORT_SYMBOL(recalc_sigpending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Eric W. Biederman088fe472018-07-23 17:26:49 -0500191void calculate_sigpending(void)
192{
193 /* Have any signals or users of TIF_SIGPENDING been delayed
194 * until after fork?
195 */
196 spin_lock_irq(&current->sighand->siglock);
197 set_tsk_thread_flag(current, TIF_SIGPENDING);
198 recalc_sigpending();
199 spin_unlock_irq(&current->sighand->siglock);
200}
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/* Given the mask, find the first available signal that should be serviced. */
203
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800204#define SYNCHRONOUS_MASK \
205 (sigmask(SIGSEGV) | sigmask(SIGBUS) | sigmask(SIGILL) | \
Will Drewrya0727e82012-04-12 16:48:00 -0500206 sigmask(SIGTRAP) | sigmask(SIGFPE) | sigmask(SIGSYS))
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800207
Davide Libenzifba2afa2007-05-10 22:23:13 -0700208int next_signal(struct sigpending *pending, sigset_t *mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 unsigned long i, *s, *m, x;
211 int sig = 0;
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 s = pending->signal.sig;
214 m = mask->sig;
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800215
216 /*
217 * Handle the first word specially: it contains the
218 * synchronous signals that need to be dequeued first.
219 */
220 x = *s &~ *m;
221 if (x) {
222 if (x & SYNCHRONOUS_MASK)
223 x &= SYNCHRONOUS_MASK;
224 sig = ffz(~x) + 1;
225 return sig;
226 }
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 switch (_NSIG_WORDS) {
229 default:
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800230 for (i = 1; i < _NSIG_WORDS; ++i) {
231 x = *++s &~ *++m;
232 if (!x)
233 continue;
234 sig = ffz(~x) + i*_NSIG_BPW + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 break;
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 break;
238
Linus Torvaldsa27341c2010-03-02 08:36:46 -0800239 case 2:
240 x = s[1] &~ m[1];
241 if (!x)
242 break;
243 sig = ffz(~x) + _NSIG_BPW + 1;
244 break;
245
246 case 1:
247 /* Nothing to do */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 break;
249 }
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return sig;
252}
253
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900254static inline void print_dropped_signal(int sig)
255{
256 static DEFINE_RATELIMIT_STATE(ratelimit_state, 5 * HZ, 10);
257
258 if (!print_fatal_signals)
259 return;
260
261 if (!__ratelimit(&ratelimit_state))
262 return;
263
Wang Xiaoqiang747800ef2016-05-23 16:23:59 -0700264 pr_info("%s/%d: reached RLIMIT_SIGPENDING, dropped signal %d\n",
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900265 current->comm, current->pid, sig);
266}
267
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100268/**
Tejun Heo7dd3db52011-06-02 11:14:00 +0200269 * task_set_jobctl_pending - set jobctl pending bits
270 * @task: target task
271 * @mask: pending bits to set
272 *
273 * Clear @mask from @task->jobctl. @mask must be subset of
274 * %JOBCTL_PENDING_MASK | %JOBCTL_STOP_CONSUME | %JOBCTL_STOP_SIGMASK |
275 * %JOBCTL_TRAPPING. If stop signo is being set, the existing signo is
276 * cleared. If @task is already being killed or exiting, this function
277 * becomes noop.
278 *
279 * CONTEXT:
280 * Must be called with @task->sighand->siglock held.
281 *
282 * RETURNS:
283 * %true if @mask is set, %false if made noop because @task was dying.
284 */
Palmer Dabbeltb76808e2015-04-30 21:19:57 -0700285bool task_set_jobctl_pending(struct task_struct *task, unsigned long mask)
Tejun Heo7dd3db52011-06-02 11:14:00 +0200286{
287 BUG_ON(mask & ~(JOBCTL_PENDING_MASK | JOBCTL_STOP_CONSUME |
288 JOBCTL_STOP_SIGMASK | JOBCTL_TRAPPING));
289 BUG_ON((mask & JOBCTL_TRAPPING) && !(mask & JOBCTL_PENDING_MASK));
290
Jens Axboe1e4cf0d2021-03-25 18:23:44 -0600291 if (unlikely(fatal_signal_pending(task) || (task->flags & PF_EXITING)))
Tejun Heo7dd3db52011-06-02 11:14:00 +0200292 return false;
293
294 if (mask & JOBCTL_STOP_SIGMASK)
295 task->jobctl &= ~JOBCTL_STOP_SIGMASK;
296
297 task->jobctl |= mask;
298 return true;
299}
300
301/**
Tejun Heoa8f072c2011-06-02 11:13:59 +0200302 * task_clear_jobctl_trapping - clear jobctl trapping bit
Tejun Heod79fdd62011-03-23 10:37:00 +0100303 * @task: target task
304 *
Tejun Heoa8f072c2011-06-02 11:13:59 +0200305 * If JOBCTL_TRAPPING is set, a ptracer is waiting for us to enter TRACED.
306 * Clear it and wake up the ptracer. Note that we don't need any further
307 * locking. @task->siglock guarantees that @task->parent points to the
308 * ptracer.
Tejun Heod79fdd62011-03-23 10:37:00 +0100309 *
310 * CONTEXT:
311 * Must be called with @task->sighand->siglock held.
312 */
Tejun Heo73ddff22011-06-14 11:20:14 +0200313void task_clear_jobctl_trapping(struct task_struct *task)
Tejun Heod79fdd62011-03-23 10:37:00 +0100314{
Tejun Heoa8f072c2011-06-02 11:13:59 +0200315 if (unlikely(task->jobctl & JOBCTL_TRAPPING)) {
316 task->jobctl &= ~JOBCTL_TRAPPING;
Oleg Nesterov650226b2014-06-06 14:36:44 -0700317 smp_mb(); /* advised by wake_up_bit() */
Tejun Heo62c124f2011-06-02 11:14:00 +0200318 wake_up_bit(&task->jobctl, JOBCTL_TRAPPING_BIT);
Tejun Heod79fdd62011-03-23 10:37:00 +0100319 }
320}
321
322/**
Tejun Heo3759a0d2011-06-02 11:14:00 +0200323 * task_clear_jobctl_pending - clear jobctl pending bits
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100324 * @task: target task
Tejun Heo3759a0d2011-06-02 11:14:00 +0200325 * @mask: pending bits to clear
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100326 *
Tejun Heo3759a0d2011-06-02 11:14:00 +0200327 * Clear @mask from @task->jobctl. @mask must be subset of
328 * %JOBCTL_PENDING_MASK. If %JOBCTL_STOP_PENDING is being cleared, other
329 * STOP bits are cleared together.
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100330 *
Tejun Heo6dfca322011-06-02 11:14:00 +0200331 * If clearing of @mask leaves no stop or trap pending, this function calls
332 * task_clear_jobctl_trapping().
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100333 *
334 * CONTEXT:
335 * Must be called with @task->sighand->siglock held.
336 */
Palmer Dabbeltb76808e2015-04-30 21:19:57 -0700337void task_clear_jobctl_pending(struct task_struct *task, unsigned long mask)
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100338{
Tejun Heo3759a0d2011-06-02 11:14:00 +0200339 BUG_ON(mask & ~JOBCTL_PENDING_MASK);
340
341 if (mask & JOBCTL_STOP_PENDING)
342 mask |= JOBCTL_STOP_CONSUME | JOBCTL_STOP_DEQUEUED;
343
344 task->jobctl &= ~mask;
Tejun Heo6dfca322011-06-02 11:14:00 +0200345
346 if (!(task->jobctl & JOBCTL_PENDING_MASK))
347 task_clear_jobctl_trapping(task);
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100348}
349
350/**
351 * task_participate_group_stop - participate in a group stop
352 * @task: task participating in a group stop
353 *
Tejun Heoa8f072c2011-06-02 11:13:59 +0200354 * @task has %JOBCTL_STOP_PENDING set and is participating in a group stop.
Tejun Heo39efa3e2011-03-23 10:37:00 +0100355 * Group stop states are cleared and the group stop count is consumed if
Tejun Heoa8f072c2011-06-02 11:13:59 +0200356 * %JOBCTL_STOP_CONSUME was set. If the consumption completes the group
Mauro Carvalho Chehab68d86812019-08-02 21:48:33 -0700357 * stop, the appropriate `SIGNAL_*` flags are set.
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100358 *
359 * CONTEXT:
360 * Must be called with @task->sighand->siglock held.
Tejun Heo244056f2011-03-23 10:37:01 +0100361 *
362 * RETURNS:
363 * %true if group stop completion should be notified to the parent, %false
364 * otherwise.
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100365 */
366static bool task_participate_group_stop(struct task_struct *task)
367{
368 struct signal_struct *sig = task->signal;
Tejun Heoa8f072c2011-06-02 11:13:59 +0200369 bool consume = task->jobctl & JOBCTL_STOP_CONSUME;
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100370
Tejun Heoa8f072c2011-06-02 11:13:59 +0200371 WARN_ON_ONCE(!(task->jobctl & JOBCTL_STOP_PENDING));
Tejun Heo39efa3e2011-03-23 10:37:00 +0100372
Tejun Heo3759a0d2011-06-02 11:14:00 +0200373 task_clear_jobctl_pending(task, JOBCTL_STOP_PENDING);
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100374
375 if (!consume)
376 return false;
377
378 if (!WARN_ON_ONCE(sig->group_stop_count == 0))
379 sig->group_stop_count--;
380
Tejun Heo244056f2011-03-23 10:37:01 +0100381 /*
382 * Tell the caller to notify completion iff we are entering into a
383 * fresh group stop. Read comment in do_signal_stop() for details.
384 */
385 if (!sig->group_stop_count && !(sig->flags & SIGNAL_STOP_STOPPED)) {
Jamie Iles2d39b3c2017-01-10 16:57:54 -0800386 signal_set_stop_flags(sig, SIGNAL_STOP_STOPPED);
Tejun Heoe5c1902e2011-03-23 10:37:00 +0100387 return true;
388 }
389 return false;
390}
391
Eric W. Biederman924de3b2018-07-23 13:38:00 -0500392void task_join_group_stop(struct task_struct *task)
393{
Oleg Nesterov7b3c36f2020-11-01 17:07:44 -0800394 unsigned long mask = current->jobctl & JOBCTL_STOP_SIGMASK;
395 struct signal_struct *sig = current->signal;
396
397 if (sig->group_stop_count) {
398 sig->group_stop_count++;
399 mask |= JOBCTL_STOP_CONSUME;
400 } else if (!(sig->flags & SIGNAL_STOP_STOPPED))
401 return;
402
Eric W. Biederman924de3b2018-07-23 13:38:00 -0500403 /* Have the new thread join an on-going signal group stop */
Oleg Nesterov7b3c36f2020-11-01 17:07:44 -0800404 task_set_jobctl_pending(task, mask | JOBCTL_STOP_PENDING);
Eric W. Biederman924de3b2018-07-23 13:38:00 -0500405}
406
David Howellsc69e8d92008-11-14 10:39:19 +1100407/*
408 * allocate a new signal queue record
409 * - this may be called without locks if and only if t == current, otherwise an
Randy Dunlap5aba0852011-04-04 14:59:31 -0700410 * appropriate lock must be held to stop the target task from exiting
David Howellsc69e8d92008-11-14 10:39:19 +1100411 */
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900412static struct sigqueue *
Thomas Gleixner69995eb2021-03-22 10:19:42 +0100413__sigqueue_alloc(int sig, struct task_struct *t, gfp_t gfp_flags,
414 int override_rlimit, const unsigned int sigqueue_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 struct sigqueue *q = NULL;
Alexey Gladkovd6469692021-04-22 14:27:13 +0200417 struct ucounts *ucounts = NULL;
418 long sigpending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800420 /*
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000421 * Protect access to @t credentials. This can go away when all
422 * callers hold rcu read lock.
Linus Torvaldsfda31c52020-02-24 12:47:14 -0800423 *
424 * NOTE! A pending signal will hold on to the user refcount,
425 * and we get/put the refcount only when the sigpending count
426 * changes from/to zero.
Linus Torvalds10b1fbd2006-11-04 13:03:00 -0800427 */
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000428 rcu_read_lock();
Alexey Gladkovd6469692021-04-22 14:27:13 +0200429 ucounts = task_ucounts(t);
Eric W. Biederman15bc01e2021-10-16 15:59:49 -0500430 sigpending = inc_rlimit_get_ucounts(ucounts, UCOUNT_RLIMIT_SIGPENDING);
Thomas Gleixner7cf7db82009-12-10 00:53:21 +0000431 rcu_read_unlock();
Eric W. Biederman15bc01e2021-10-16 15:59:49 -0500432 if (!sigpending)
433 return NULL;
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900434
Alexey Gladkovf3791f42021-07-08 12:33:01 +0200435 if (override_rlimit || likely(sigpending <= task_rlimit(t, RLIMIT_SIGPENDING))) {
Linus Torvaldsb4b27b92021-06-27 13:32:54 -0700436 q = kmem_cache_alloc(sigqueue_cachep, gfp_flags);
Naohiro Ooiwaf84d49b2009-11-09 00:46:42 +0900437 } else {
438 print_dropped_signal(sig);
439 }
440
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (unlikely(q == NULL)) {
Eric W. Biederman15bc01e2021-10-16 15:59:49 -0500442 dec_rlimit_put_ucounts(ucounts, UCOUNT_RLIMIT_SIGPENDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 } else {
444 INIT_LIST_HEAD(&q->list);
Thomas Gleixner69995eb2021-03-22 10:19:42 +0100445 q->flags = sigqueue_flags;
Alexey Gladkovd6469692021-04-22 14:27:13 +0200446 q->ucounts = ucounts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 }
David Howellsd84f4f92008-11-14 10:39:23 +1100448 return q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
Andrew Morton514a01b2006-02-03 03:04:41 -0800451static void __sigqueue_free(struct sigqueue *q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 if (q->flags & SIGQUEUE_PREALLOC)
454 return;
Eric W. Biederman15bc01e2021-10-16 15:59:49 -0500455 if (q->ucounts) {
456 dec_rlimit_put_ucounts(q->ucounts, UCOUNT_RLIMIT_SIGPENDING);
Alexey Gladkovd6469692021-04-22 14:27:13 +0200457 q->ucounts = NULL;
458 }
Linus Torvaldsb4b27b92021-06-27 13:32:54 -0700459 kmem_cache_free(sigqueue_cachep, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460}
461
Oleg Nesterov6a14c5c2006-03-28 16:11:18 -0800462void flush_sigqueue(struct sigpending *queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
464 struct sigqueue *q;
465
466 sigemptyset(&queue->signal);
467 while (!list_empty(&queue->list)) {
468 q = list_entry(queue->list.next, struct sigqueue , list);
469 list_del_init(&q->list);
470 __sigqueue_free(q);
471 }
472}
473
474/*
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -0400475 * Flush all pending signals for this kthread.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 */
Oleg Nesterovc81addc2006-03-28 16:11:17 -0800477void flush_signals(struct task_struct *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 unsigned long flags;
480
481 spin_lock_irqsave(&t->sighand->siglock, flags);
Oleg Nesterov9e7c8f82015-06-04 16:22:16 -0400482 clear_tsk_thread_flag(t, TIF_SIGPENDING);
483 flush_sigqueue(&t->pending);
484 flush_sigqueue(&t->signal->shared_pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 spin_unlock_irqrestore(&t->sighand->siglock, flags);
486}
Eric W. Biedermanfb50f5a2018-09-13 19:26:35 +0200487EXPORT_SYMBOL(flush_signals);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Nicolas Pitrebaa73d92016-11-11 00:10:10 -0500489#ifdef CONFIG_POSIX_TIMERS
Oleg Nesterovcbaffba2008-05-26 20:55:42 +0400490static void __flush_itimer_signals(struct sigpending *pending)
491{
492 sigset_t signal, retain;
493 struct sigqueue *q, *n;
494
495 signal = pending->signal;
496 sigemptyset(&retain);
497
498 list_for_each_entry_safe(q, n, &pending->list, list) {
499 int sig = q->info.si_signo;
500
501 if (likely(q->info.si_code != SI_TIMER)) {
502 sigaddset(&retain, sig);
503 } else {
504 sigdelset(&signal, sig);
505 list_del_init(&q->list);
506 __sigqueue_free(q);
507 }
508 }
509
510 sigorsets(&pending->signal, &signal, &retain);
511}
512
513void flush_itimer_signals(void)
514{
515 struct task_struct *tsk = current;
516 unsigned long flags;
517
518 spin_lock_irqsave(&tsk->sighand->siglock, flags);
519 __flush_itimer_signals(&tsk->pending);
520 __flush_itimer_signals(&tsk->signal->shared_pending);
521 spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
522}
Nicolas Pitrebaa73d92016-11-11 00:10:10 -0500523#endif
Oleg Nesterovcbaffba2008-05-26 20:55:42 +0400524
Oleg Nesterov10ab8252007-05-09 02:34:37 -0700525void ignore_signals(struct task_struct *t)
526{
527 int i;
528
529 for (i = 0; i < _NSIG; ++i)
530 t->sighand->action[i].sa.sa_handler = SIG_IGN;
531
532 flush_signals(t);
533}
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 * Flush all handlers for a task.
537 */
538
539void
540flush_signal_handlers(struct task_struct *t, int force_default)
541{
542 int i;
543 struct k_sigaction *ka = &t->sighand->action[0];
544 for (i = _NSIG ; i != 0 ; i--) {
545 if (force_default || ka->sa.sa_handler != SIG_IGN)
546 ka->sa.sa_handler = SIG_DFL;
547 ka->sa.sa_flags = 0;
Andrew Morton522cff12013-03-13 14:59:34 -0700548#ifdef __ARCH_HAS_SA_RESTORER
Kees Cook2ca39522013-03-13 14:59:33 -0700549 ka->sa.sa_restorer = NULL;
550#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 sigemptyset(&ka->sa.sa_mask);
552 ka++;
553 }
554}
555
Christian Brauner67a48a22018-08-21 22:00:34 -0700556bool unhandled_signal(struct task_struct *tsk, int sig)
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200557{
Roland McGrath445a91d2008-07-25 19:45:52 -0700558 void __user *handler = tsk->sighand->action[sig-1].sa.sa_handler;
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700559 if (is_global_init(tsk))
Christian Brauner67a48a22018-08-21 22:00:34 -0700560 return true;
561
Roland McGrath445a91d2008-07-25 19:45:52 -0700562 if (handler != SIG_IGN && handler != SIG_DFL)
Christian Brauner67a48a22018-08-21 22:00:34 -0700563 return false;
564
Tejun Heoa288eec2011-06-17 16:50:37 +0200565 /* if ptraced, let the tracer determine */
566 return !tsk->ptrace;
Masoud Asgharifard Sharbianiabd4f752007-07-22 11:12:28 +0200567}
568
Eric W. Biedermanae7795b2018-09-25 11:27:20 +0200569static void collect_signal(int sig, struct sigpending *list, kernel_siginfo_t *info,
Eric W. Biederman57db7e42017-06-13 04:31:16 -0500570 bool *resched_timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 struct sigqueue *q, *first = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 /*
575 * Collect the siginfo appropriate to this signal. Check if
576 * there is another siginfo for the same signal.
577 */
578 list_for_each_entry(q, &list->list, list) {
579 if (q->info.si_signo == sig) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700580 if (first)
581 goto still_pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 first = q;
583 }
584 }
Oleg Nesterovd4434202008-07-25 01:47:28 -0700585
586 sigdelset(&list->signal, sig);
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 if (first) {
Oleg Nesterovd4434202008-07-25 01:47:28 -0700589still_pending:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 list_del_init(&first->list);
591 copy_siginfo(info, &first->info);
Eric W. Biederman57db7e42017-06-13 04:31:16 -0500592
593 *resched_timer =
594 (first->flags & SIGQUEUE_PREALLOC) &&
595 (info->si_code == SI_TIMER) &&
596 (info->si_sys_private);
597
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 __sigqueue_free(first);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 } else {
Randy Dunlap5aba0852011-04-04 14:59:31 -0700600 /*
601 * Ok, it wasn't in the queue. This must be
602 * a fast-pathed signal or we must have been
603 * out of queue space. So zero out the info.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 */
Eric W. Biedermanfaf1f222018-01-05 17:27:42 -0600605 clear_siginfo(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 info->si_signo = sig;
607 info->si_errno = 0;
Oleg Nesterov7486e5d2009-12-15 16:47:24 -0800608 info->si_code = SI_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 info->si_pid = 0;
610 info->si_uid = 0;
611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
614static int __dequeue_signal(struct sigpending *pending, sigset_t *mask,
Eric W. Biedermanae7795b2018-09-25 11:27:20 +0200615 kernel_siginfo_t *info, bool *resched_timer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Roland McGrath27d91e02006-09-29 02:00:31 -0700617 int sig = next_signal(pending, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Oleg Nesterov2e01fab2015-11-06 16:32:19 -0800619 if (sig)
Eric W. Biederman57db7e42017-06-13 04:31:16 -0500620 collect_signal(sig, pending, info, resched_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 return sig;
622}
623
624/*
Randy Dunlap5aba0852011-04-04 14:59:31 -0700625 * Dequeue a signal and return the element to the caller, which is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 * expected to free it.
627 *
628 * All callers have to hold the siglock.
629 */
Eric W. Biederman5768d892021-11-15 13:47:13 -0600630int dequeue_signal(struct task_struct *tsk, sigset_t *mask,
631 kernel_siginfo_t *info, enum pid_type *type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Eric W. Biederman57db7e42017-06-13 04:31:16 -0500633 bool resched_timer = false;
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700634 int signr;
Benjamin Herrenschmidtcaec4e82007-06-12 08:16:18 +1000635
636 /* We only dequeue private signals from ourselves, we don't let
637 * signalfd steal them
638 */
Eric W. Biederman5768d892021-11-15 13:47:13 -0600639 *type = PIDTYPE_PID;
Eric W. Biederman57db7e42017-06-13 04:31:16 -0500640 signr = __dequeue_signal(&tsk->pending, mask, info, &resched_timer);
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800641 if (!signr) {
Eric W. Biederman5768d892021-11-15 13:47:13 -0600642 *type = PIDTYPE_TGID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 signr = __dequeue_signal(&tsk->signal->shared_pending,
Eric W. Biederman57db7e42017-06-13 04:31:16 -0500644 mask, info, &resched_timer);
Nicolas Pitrebaa73d92016-11-11 00:10:10 -0500645#ifdef CONFIG_POSIX_TIMERS
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800646 /*
647 * itimer signal ?
648 *
649 * itimers are process shared and we restart periodic
650 * itimers in the signal delivery path to prevent DoS
651 * attacks in the high resolution timer case. This is
Randy Dunlap5aba0852011-04-04 14:59:31 -0700652 * compliant with the old way of self-restarting
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800653 * itimers, as the SIGALRM is a legacy signal and only
654 * queued once. Changing the restart behaviour to
655 * restart the timer in the signal dequeue path is
656 * reducing the timer noise on heavy loaded !highres
657 * systems too.
658 */
659 if (unlikely(signr == SIGALRM)) {
660 struct hrtimer *tmr = &tsk->signal->real_timer;
661
662 if (!hrtimer_is_queued(tmr) &&
Thomas Gleixner2456e852016-12-25 11:38:40 +0100663 tsk->signal->it_real_incr != 0) {
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800664 hrtimer_forward(tmr, tmr->base->get_time(),
665 tsk->signal->it_real_incr);
666 hrtimer_restart(tmr);
667 }
668 }
Nicolas Pitrebaa73d92016-11-11 00:10:10 -0500669#endif
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800670 }
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700671
Davide Libenzib8fceee2007-09-20 12:40:16 -0700672 recalc_sigpending();
Pavel Emelyanovc5363d02008-04-30 00:52:40 -0700673 if (!signr)
674 return 0;
675
676 if (unlikely(sig_kernel_stop(signr))) {
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800677 /*
678 * Set a marker that we have dequeued a stop signal. Our
679 * caller might release the siglock and then the pending
680 * stop signal it is about to process is no longer in the
681 * pending bitmasks, but must still be cleared by a SIGCONT
682 * (and overruled by a SIGKILL). So those cases clear this
683 * shared flag after we've set it. Note that this flag may
684 * remain set after the signal we return is ignored or
685 * handled. That doesn't matter because its only purpose
686 * is to alert stop-signal processing code when another
687 * processor has come along and cleared the flag.
688 */
Tejun Heoa8f072c2011-06-02 11:13:59 +0200689 current->jobctl |= JOBCTL_STOP_DEQUEUED;
Thomas Gleixner8bfd9a72007-02-16 01:28:12 -0800690 }
Nicolas Pitrebaa73d92016-11-11 00:10:10 -0500691#ifdef CONFIG_POSIX_TIMERS
Eric W. Biederman57db7e42017-06-13 04:31:16 -0500692 if (resched_timer) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /*
694 * Release the siglock to ensure proper locking order
695 * of timer locks outside of siglocks. Note, we leave
696 * irqs disabled here, since the posix-timers code is
697 * about to disable them again anyway.
698 */
699 spin_unlock(&tsk->sighand->siglock);
Thomas Gleixner96fe3b02017-05-30 23:15:46 +0200700 posixtimer_rearm(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 spin_lock(&tsk->sighand->siglock);
Eric W. Biederman9943d3a2017-07-24 14:53:03 -0500702
703 /* Don't expose the si_sys_private value to userspace */
704 info->si_sys_private = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 }
Nicolas Pitrebaa73d92016-11-11 00:10:10 -0500706#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 return signr;
708}
Eric W. Biedermanfb50f5a2018-09-13 19:26:35 +0200709EXPORT_SYMBOL_GPL(dequeue_signal);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Eric W. Biederman7146db32019-02-06 17:51:47 -0600711static int dequeue_synchronous_signal(kernel_siginfo_t *info)
712{
713 struct task_struct *tsk = current;
714 struct sigpending *pending = &tsk->pending;
715 struct sigqueue *q, *sync = NULL;
716
717 /*
718 * Might a synchronous signal be in the queue?
719 */
720 if (!((pending->signal.sig[0] & ~tsk->blocked.sig[0]) & SYNCHRONOUS_MASK))
721 return 0;
722
723 /*
724 * Return the first synchronous signal in the queue.
725 */
726 list_for_each_entry(q, &pending->list, list) {
Pavel Machek7665a472020-07-24 11:05:31 +0200727 /* Synchronous signals have a positive si_code */
Eric W. Biederman7146db32019-02-06 17:51:47 -0600728 if ((q->info.si_code > SI_USER) &&
729 (sigmask(q->info.si_signo) & SYNCHRONOUS_MASK)) {
730 sync = q;
731 goto next;
732 }
733 }
734 return 0;
735next:
736 /*
737 * Check if there is another siginfo for the same signal.
738 */
739 list_for_each_entry_continue(q, &pending->list, list) {
740 if (q->info.si_signo == sync->info.si_signo)
741 goto still_pending;
742 }
743
744 sigdelset(&pending->signal, sync->info.si_signo);
745 recalc_sigpending();
746still_pending:
747 list_del_init(&sync->list);
748 copy_siginfo(info, &sync->info);
749 __sigqueue_free(sync);
750 return info->si_signo;
751}
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753/*
754 * Tell a process that it has a new active signal..
755 *
756 * NOTE! we rely on the previous spin_lock to
757 * lock interrupts for us! We can only be called with
758 * "siglock" held, and the local interrupt must
759 * have been disabled when that got acquired!
760 *
761 * No need to set need_resched since signal event passing
762 * goes through ->blocked
763 */
Oleg Nesterov910ffdb12013-01-21 20:47:41 +0100764void signal_wake_up_state(struct task_struct *t, unsigned int state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
Peter Zijlstra31cae1e2022-05-03 15:57:47 -0500766 lockdep_assert_held(&t->sighand->siglock);
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 set_tsk_thread_flag(t, TIF_SIGPENDING);
Peter Zijlstra31cae1e2022-05-03 15:57:47 -0500769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 /*
Oleg Nesterov910ffdb12013-01-21 20:47:41 +0100771 * TASK_WAKEKILL also means wake it up in the stopped/traced/killable
Matthew Wilcoxf021a3c2007-12-06 11:13:16 -0500772 * case. We don't check t->state here because there is a race with it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 * executing another processor and just now entering stopped state.
774 * By using wake_up_state, we ensure the process will wake up and
775 * handle its death signal.
776 */
Oleg Nesterov910ffdb12013-01-21 20:47:41 +0100777 if (!wake_up_state(t, state | TASK_INTERRUPTIBLE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 kick_process(t);
779}
780
781/*
782 * Remove signals in mask from the pending set and queue.
783 * Returns 1 if any signals were found.
784 *
785 * All callers must be holding the siglock.
George Anzinger71fabd52006-01-08 01:02:48 -0800786 */
Christian Brauner8f113512018-08-21 22:00:38 -0700787static void flush_sigqueue_mask(sigset_t *mask, struct sigpending *s)
George Anzinger71fabd52006-01-08 01:02:48 -0800788{
789 struct sigqueue *q, *n;
790 sigset_t m;
791
792 sigandsets(&m, mask, &s->signal);
793 if (sigisemptyset(&m))
Christian Brauner8f113512018-08-21 22:00:38 -0700794 return;
George Anzinger71fabd52006-01-08 01:02:48 -0800795
Oleg Nesterov702a5072011-04-27 22:01:27 +0200796 sigandnsets(&s->signal, &s->signal, mask);
George Anzinger71fabd52006-01-08 01:02:48 -0800797 list_for_each_entry_safe(q, n, &s->list, list) {
798 if (sigismember(mask, q->info.si_signo)) {
799 list_del_init(&q->list);
800 __sigqueue_free(q);
801 }
802 }
George Anzinger71fabd52006-01-08 01:02:48 -0800803}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
Eric W. Biedermanae7795b2018-09-25 11:27:20 +0200805static inline int is_si_special(const struct kernel_siginfo *info)
Oleg Nesterov614c5172009-12-15 16:47:22 -0800806{
Eric W. Biederman4ff4c312018-09-03 10:39:04 +0200807 return info <= SEND_SIG_PRIV;
Oleg Nesterov614c5172009-12-15 16:47:22 -0800808}
809
Eric W. Biedermanae7795b2018-09-25 11:27:20 +0200810static inline bool si_fromuser(const struct kernel_siginfo *info)
Oleg Nesterov614c5172009-12-15 16:47:22 -0800811{
812 return info == SEND_SIG_NOINFO ||
813 (!is_si_special(info) && SI_FROMUSER(info));
814}
815
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816/*
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700817 * called with RCU read lock from check_kill_permission()
818 */
Christian Brauner2a9b9092018-08-21 22:00:11 -0700819static bool kill_ok_by_cred(struct task_struct *t)
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700820{
821 const struct cred *cred = current_cred();
822 const struct cred *tcred = __task_cred(t);
823
Christian Brauner2a9b9092018-08-21 22:00:11 -0700824 return uid_eq(cred->euid, tcred->suid) ||
825 uid_eq(cred->euid, tcred->uid) ||
826 uid_eq(cred->uid, tcred->suid) ||
827 uid_eq(cred->uid, tcred->uid) ||
828 ns_capable(tcred->user_ns, CAP_KILL);
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700829}
830
831/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 * Bad permissions for sending the signal
David Howells694f6902010-08-04 16:59:14 +0100833 * - the caller must hold the RCU read lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +0200835static int check_kill_permission(int sig, struct kernel_siginfo *info,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 struct task_struct *t)
837{
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700838 struct pid *sid;
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700839 int error;
840
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700841 if (!valid_signal(sig))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700842 return -EINVAL;
843
Oleg Nesterov614c5172009-12-15 16:47:22 -0800844 if (!si_fromuser(info))
Oleg Nesterov3b5e9e52008-04-30 00:52:42 -0700845 return 0;
846
847 error = audit_signal_info(sig, t); /* Let audit system see the signal */
848 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return error;
Amy Griffise54dc242007-03-29 18:01:04 -0400850
Oleg Nesterov065add32010-05-26 14:42:54 -0700851 if (!same_thread_group(current, t) &&
Serge E. Hallyn39fd3392011-03-23 16:43:19 -0700852 !kill_ok_by_cred(t)) {
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700853 switch (sig) {
854 case SIGCONT:
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700855 sid = task_session(t);
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700856 /*
857 * We don't return the error if sid == NULL. The
858 * task was unhashed, the caller must notice this.
859 */
860 if (!sid || sid == task_session(current))
861 break;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500862 fallthrough;
Oleg Nesterov2e2ba222008-04-30 00:53:01 -0700863 default:
864 return -EPERM;
865 }
866 }
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100867
Stephen Smalley6b4f3d02017-09-08 12:40:01 -0400868 return security_task_kill(t, info, sig, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Tejun Heofb1d9102011-06-14 11:20:17 +0200871/**
872 * ptrace_trap_notify - schedule trap to notify ptracer
873 * @t: tracee wanting to notify tracer
874 *
875 * This function schedules sticky ptrace trap which is cleared on the next
876 * TRAP_STOP to notify ptracer of an event. @t must have been seized by
877 * ptracer.
878 *
Tejun Heo544b2c92011-06-14 11:20:18 +0200879 * If @t is running, STOP trap will be taken. If trapped for STOP and
880 * ptracer is listening for events, tracee is woken up so that it can
881 * re-trap for the new event. If trapped otherwise, STOP trap will be
882 * eventually taken without returning to userland after the existing traps
883 * are finished by PTRACE_CONT.
Tejun Heofb1d9102011-06-14 11:20:17 +0200884 *
885 * CONTEXT:
886 * Must be called with @task->sighand->siglock held.
887 */
888static void ptrace_trap_notify(struct task_struct *t)
889{
890 WARN_ON_ONCE(!(t->ptrace & PT_SEIZED));
Eric W. Biedermancb3c19c2022-04-29 09:16:10 -0500891 lockdep_assert_held(&t->sighand->siglock);
Tejun Heofb1d9102011-06-14 11:20:17 +0200892
893 task_set_jobctl_pending(t, JOBCTL_TRAP_NOTIFY);
Oleg Nesterov910ffdb12013-01-21 20:47:41 +0100894 ptrace_signal_wake_up(t, t->jobctl & JOBCTL_LISTENING);
Tejun Heofb1d9102011-06-14 11:20:17 +0200895}
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897/*
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700898 * Handle magic process-wide effects of stop/continue signals. Unlike
899 * the signal actions, these happen immediately at signal-generation
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 * time regardless of blocking, ignoring, or handling. This does the
901 * actual continuing for SIGCONT, but not the actual stopping for stop
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700902 * signals. The process stop is done as a signal action for SIG_DFL.
903 *
904 * Returns true if the signal should be actually delivered, otherwise
905 * it should be dropped.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 */
Oleg Nesterov403bad72013-04-30 15:28:10 -0700907static bool prepare_signal(int sig, struct task_struct *p, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700909 struct signal_struct *signal = p->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 struct task_struct *t;
Oleg Nesterov9490592f2014-06-06 14:36:48 -0700911 sigset_t flush;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Eric W. Biederman2f824d42022-01-08 09:48:31 -0600913 if (signal->flags & SIGNAL_GROUP_EXIT) {
Eric W. Biedermana0287db2022-01-08 09:34:50 -0600914 if (signal->core_state)
Oleg Nesterov403bad72013-04-30 15:28:10 -0700915 return sig == SIGKILL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 /*
Eric W. Biederman9a95f78e2022-01-08 10:37:00 -0600917 * The process is in the middle of dying, drop the signal.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 */
Eric W. Biederman9a95f78e2022-01-08 10:37:00 -0600919 return false;
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700920 } else if (sig_kernel_stop(sig)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 /*
922 * This is a stop signal. Remove SIGCONT from all queues.
923 */
Oleg Nesterov9490592f2014-06-06 14:36:48 -0700924 siginitset(&flush, sigmask(SIGCONT));
Oleg Nesterovc09c1442014-06-06 14:36:50 -0700925 flush_sigqueue_mask(&flush, &signal->shared_pending);
Oleg Nesterov9490592f2014-06-06 14:36:48 -0700926 for_each_thread(p, t)
Oleg Nesterovc09c1442014-06-06 14:36:50 -0700927 flush_sigqueue_mask(&flush, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 } else if (sig == SIGCONT) {
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700929 unsigned int why;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 /*
Oleg Nesterov1deac632011-04-01 20:11:50 +0200931 * Remove all stop signals from all queues, wake all threads.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 */
Oleg Nesterov9490592f2014-06-06 14:36:48 -0700933 siginitset(&flush, SIG_KERNEL_STOP_MASK);
Oleg Nesterovc09c1442014-06-06 14:36:50 -0700934 flush_sigqueue_mask(&flush, &signal->shared_pending);
Oleg Nesterov9490592f2014-06-06 14:36:48 -0700935 for_each_thread(p, t) {
Oleg Nesterovc09c1442014-06-06 14:36:50 -0700936 flush_sigqueue_mask(&flush, &t->pending);
Tejun Heo3759a0d2011-06-02 11:14:00 +0200937 task_clear_jobctl_pending(t, JOBCTL_STOP_PENDING);
Peter Zijlstra31cae1e2022-05-03 15:57:47 -0500938 if (likely(!(t->ptrace & PT_SEIZED))) {
939 t->jobctl &= ~JOBCTL_STOPPED;
Tejun Heofb1d9102011-06-14 11:20:17 +0200940 wake_up_state(t, __TASK_STOPPED);
Peter Zijlstra31cae1e2022-05-03 15:57:47 -0500941 } else
Tejun Heofb1d9102011-06-14 11:20:17 +0200942 ptrace_trap_notify(t);
Oleg Nesterov9490592f2014-06-06 14:36:48 -0700943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700945 /*
946 * Notify the parent with CLD_CONTINUED if we were stopped.
947 *
948 * If we were in the middle of a group stop, we pretend it
949 * was already finished, and then continued. Since SIGCHLD
950 * doesn't queue we report only CLD_STOPPED, as if the next
951 * CLD_CONTINUED was dropped.
952 */
953 why = 0;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700954 if (signal->flags & SIGNAL_STOP_STOPPED)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700955 why |= SIGNAL_CLD_CONTINUED;
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700956 else if (signal->group_stop_count)
Oleg Nesterovfc321d22008-04-30 00:52:46 -0700957 why |= SIGNAL_CLD_STOPPED;
958
959 if (why) {
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700960 /*
Roland McGrathae6d2ed2009-09-23 15:56:53 -0700961 * The first thread which returns from do_signal_stop()
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700962 * will take ->siglock, notice SIGNAL_CLD_MASK, and
Weikang Shi2e58f572018-10-30 15:07:05 -0700963 * notify its parent. See get_signal().
Oleg Nesterov021e1ae2008-04-30 00:53:00 -0700964 */
Jamie Iles2d39b3c2017-01-10 16:57:54 -0800965 signal_set_stop_flags(signal, why | SIGNAL_STOP_CONTINUED);
Oleg Nesterovad16a4602008-04-30 00:52:46 -0700966 signal->group_stop_count = 0;
967 signal->group_exit_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
Oleg Nesterov7e695a52008-04-30 00:52:59 -0700970
Oleg Nesterovdef8cf72012-03-23 15:02:45 -0700971 return !sig_ignored(p, sig, force);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972}
973
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700974/*
975 * Test if P wants to take SIG. After we've checked all threads with this,
976 * it's equivalent to finding no threads not blocking SIG. Any threads not
977 * blocking SIG were ruled out because they are not running and already
978 * have pending signals. Such threads will dequeue from the shared queue
979 * as soon as they're available, so putting the signal on the shared queue
980 * will be equivalent to sending it to one such thread.
981 */
Christian Brauneracd14e62018-08-21 22:00:42 -0700982static inline bool wants_signal(int sig, struct task_struct *p)
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700983{
984 if (sigismember(&p->blocked, sig))
Christian Brauneracd14e62018-08-21 22:00:42 -0700985 return false;
986
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700987 if (p->flags & PF_EXITING)
Christian Brauneracd14e62018-08-21 22:00:42 -0700988 return false;
989
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700990 if (sig == SIGKILL)
Christian Brauneracd14e62018-08-21 22:00:42 -0700991 return true;
992
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700993 if (task_is_stopped_or_traced(p))
Christian Brauneracd14e62018-08-21 22:00:42 -0700994 return false;
995
Jens Axboe5c251e92020-10-26 14:32:27 -0600996 return task_curr(p) || !task_sigpending(p);
Oleg Nesterov71f11dc2008-04-30 00:52:53 -0700997}
998
Eric W. Biederman07296142018-07-13 21:39:13 -0500999static void complete_signal(int sig, struct task_struct *p, enum pid_type type)
Oleg Nesterov71f11dc2008-04-30 00:52:53 -07001000{
1001 struct signal_struct *signal = p->signal;
1002 struct task_struct *t;
1003
1004 /*
1005 * Now find a thread we can wake up to take the signal off the queue.
1006 *
Dmitry Vyukovbcb7ee72023-03-16 13:30:27 +01001007 * Try the suggested task first (may or may not be the main thread).
Oleg Nesterov71f11dc2008-04-30 00:52:53 -07001008 */
1009 if (wants_signal(sig, p))
1010 t = p;
Eric W. Biederman07296142018-07-13 21:39:13 -05001011 else if ((type == PIDTYPE_PID) || thread_group_empty(p))
Oleg Nesterov71f11dc2008-04-30 00:52:53 -07001012 /*
1013 * There is just one thread and it does not need to be woken.
1014 * It will dequeue unblocked signals before it runs again.
1015 */
1016 return;
1017 else {
1018 /*
1019 * Otherwise try to find a suitable thread.
1020 */
1021 t = signal->curr_target;
1022 while (!wants_signal(sig, t)) {
1023 t = next_thread(t);
1024 if (t == signal->curr_target)
1025 /*
1026 * No thread needs to be woken.
1027 * Any eligible threads will see
1028 * the signal in the queue soon.
1029 */
1030 return;
1031 }
1032 signal->curr_target = t;
1033 }
1034
1035 /*
1036 * Found a killable thread. If the signal will be fatal,
1037 * then start taking the whole group down immediately.
1038 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -07001039 if (sig_fatal(p, sig) &&
Eric W. Biederman7ba03472022-01-08 11:01:12 -06001040 (signal->core_state || !(signal->flags & SIGNAL_GROUP_EXIT)) &&
Oleg Nesterov71f11dc2008-04-30 00:52:53 -07001041 !sigismember(&t->real_blocked, sig) &&
Oleg Nesterov42691572017-11-17 15:30:08 -08001042 (sig == SIGKILL || !p->ptrace)) {
Oleg Nesterov71f11dc2008-04-30 00:52:53 -07001043 /*
1044 * This signal will be fatal to the whole group.
1045 */
1046 if (!sig_kernel_coredump(sig)) {
1047 /*
1048 * Start a group exit and wake everybody up.
1049 * This way we don't have other threads
1050 * running and doing things after a slower
1051 * thread has the fatal signal pending.
1052 */
1053 signal->flags = SIGNAL_GROUP_EXIT;
1054 signal->group_exit_code = sig;
1055 signal->group_stop_count = 0;
1056 t = p;
1057 do {
Tejun Heo6dfca322011-06-02 11:14:00 +02001058 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
Oleg Nesterov71f11dc2008-04-30 00:52:53 -07001059 sigaddset(&t->pending.signal, SIGKILL);
1060 signal_wake_up(t, 1);
1061 } while_each_thread(p, t);
1062 return;
1063 }
1064 }
1065
1066 /*
1067 * The signal is already in the shared-pending queue.
1068 * Tell the chosen thread to wake up and dequeue it.
1069 */
1070 signal_wake_up(t, sig == SIGKILL);
1071 return;
1072}
1073
Christian Braunera19e2c02018-08-21 22:00:46 -07001074static inline bool legacy_queue(struct sigpending *signals, int sig)
Pavel Emelyanovaf7fff92008-04-30 00:52:34 -07001075{
1076 return (sig < SIGRTMIN) && sigismember(&signals->signal, sig);
1077}
1078
Eric W. Biederman157cc182022-04-22 09:48:54 -05001079static int __send_signal_locked(int sig, struct kernel_siginfo *info,
1080 struct task_struct *t, enum pid_type type, bool force)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001082 struct sigpending *pending;
Oleg Nesterov6e65acb2008-04-30 00:52:50 -07001083 struct sigqueue *q;
Vegard Nossum7a0aeb12009-05-16 11:28:33 +02001084 int override_rlimit;
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001085 int ret = 0, result;
Mathieu Desnoyers0a16b602008-07-18 12:16:17 -04001086
Eric W. Biedermancb3c19c2022-04-29 09:16:10 -05001087 lockdep_assert_held(&t->sighand->siglock);
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001088
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001089 result = TRACE_SIGNAL_IGNORED;
Eric W. Biederman8ad23de2019-05-15 22:23:32 -05001090 if (!prepare_signal(sig, t, force))
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001091 goto ret;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001092
Eric W. Biederman5a883ce2018-07-13 19:26:27 -05001093 pending = (type != PIDTYPE_PID) ? &t->signal->shared_pending : &t->pending;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 /*
Pavel Emelyanov2acb0242008-04-30 00:52:35 -07001095 * Short-circuit ignored signals and support queuing
1096 * exactly one non-rt signal, so that we can get more
1097 * detailed information about the cause of the signal.
1098 */
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001099 result = TRACE_SIGNAL_ALREADY_PENDING;
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001100 if (legacy_queue(pending, sig))
Oleg Nesterov6c303d32011-11-22 21:13:48 +01001101 goto ret;
1102
1103 result = TRACE_SIGNAL_DELIVERED;
Davide Libenzifba2afa2007-05-10 22:23:13 -07001104 /*
Eric W. Biedermana6929332019-02-05 07:19:11 -06001105 * Skip useless siginfo allocation for SIGKILL and kernel threads.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 */
Jens Axboee8b33b82021-03-25 18:18:59 -06001107 if ((sig == SIGKILL) || (t->flags & PF_KTHREAD))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 goto out_set;
1109
Randy Dunlap5aba0852011-04-04 14:59:31 -07001110 /*
1111 * Real-time signals must be queued if sent by sigqueue, or
1112 * some other real-time mechanism. It is implementation
1113 * defined whether kill() does so. We attempt to do so, on
1114 * the principle of least surprise, but since kill is not
1115 * allowed to fail with EAGAIN when low on memory we just
1116 * make sure at least one signal gets delivered and don't
1117 * pass on the info struct.
1118 */
Vegard Nossum7a0aeb12009-05-16 11:28:33 +02001119 if (sig < SIGRTMIN)
1120 override_rlimit = (is_si_special(info) || info->si_code >= 0);
1121 else
1122 override_rlimit = 0;
1123
Thomas Gleixner69995eb2021-03-22 10:19:42 +01001124 q = __sigqueue_alloc(sig, t, GFP_ATOMIC, override_rlimit, 0);
1125
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (q) {
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001127 list_add_tail(&q->list, &pending->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 switch ((unsigned long) info) {
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001129 case (unsigned long) SEND_SIG_NOINFO:
Eric W. Biedermanfaf1f222018-01-05 17:27:42 -06001130 clear_siginfo(&q->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 q->info.si_signo = sig;
1132 q->info.si_errno = 0;
1133 q->info.si_code = SI_USER;
Sukadev Bhattiprolu9cd4fd12009-01-06 14:42:46 -08001134 q->info.si_pid = task_tgid_nr_ns(current,
Sukadev Bhattiprolu09bca052009-01-06 14:42:45 -08001135 task_active_pid_ns(t));
Eric W. Biederman7a0cf092019-05-15 22:54:56 -05001136 rcu_read_lock();
1137 q->info.si_uid =
1138 from_kuid_munged(task_cred_xxx(t, user_ns),
1139 current_uid());
1140 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 break;
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001142 case (unsigned long) SEND_SIG_PRIV:
Eric W. Biedermanfaf1f222018-01-05 17:27:42 -06001143 clear_siginfo(&q->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 q->info.si_signo = sig;
1145 q->info.si_errno = 0;
1146 q->info.si_code = SI_KERNEL;
1147 q->info.si_pid = 0;
1148 q->info.si_uid = 0;
1149 break;
1150 default:
1151 copy_siginfo(&q->info, info);
1152 break;
1153 }
Eric W. Biederman8917bef2019-05-15 22:56:17 -05001154 } else if (!is_si_special(info) &&
1155 sig >= SIGRTMIN && info->si_code != SI_USER) {
1156 /*
1157 * Queue overflow, abort. We may abort if the
1158 * signal was rt and sent by user using something
1159 * other than kill().
1160 */
1161 result = TRACE_SIGNAL_OVERFLOW_FAIL;
1162 ret = -EAGAIN;
1163 goto ret;
1164 } else {
1165 /*
1166 * This is a silent loss of information. We still
1167 * send the signal, but the *info bits are lost.
1168 */
1169 result = TRACE_SIGNAL_LOSE_INFO;
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. Biederman7a0cf092019-05-15 22:54:56 -05001196static inline bool has_si_pid_and_uid(struct kernel_siginfo *info)
1197{
1198 bool ret = false;
1199 switch (siginfo_layout(info->si_signo, info->si_code)) {
1200 case SIL_KILL:
1201 case SIL_CHLD:
1202 case SIL_RT:
1203 ret = true;
1204 break;
1205 case SIL_TIMER:
1206 case SIL_POLL:
1207 case SIL_FAULT:
Eric W. Biederman9abcabe2021-04-30 17:29:36 -05001208 case SIL_FAULT_TRAPNO:
Eric W. Biederman7a0cf092019-05-15 22:54:56 -05001209 case SIL_FAULT_MCEERR:
1210 case SIL_FAULT_BNDERR:
1211 case SIL_FAULT_PKUERR:
Eric W. Biedermanf4ac7302021-04-30 17:58:56 -05001212 case SIL_FAULT_PERF_EVENT:
Eric W. Biederman7a0cf092019-05-15 22:54:56 -05001213 case SIL_SYS:
1214 ret = false;
1215 break;
1216 }
1217 return ret;
1218}
1219
Eric W. Biederman157cc182022-04-22 09:48:54 -05001220int send_signal_locked(int sig, struct kernel_siginfo *info,
1221 struct task_struct *t, enum pid_type type)
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001222{
Eric W. Biederman8ad23de2019-05-15 22:23:32 -05001223 /* Should SIGKILL or SIGSTOP be received by a pid namespace init? */
1224 bool force = false;
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001225
Eric W. Biederman8ad23de2019-05-15 22:23:32 -05001226 if (info == SEND_SIG_NOINFO) {
1227 /* Force if sent from an ancestor pid namespace */
1228 force = !task_pid_nr_ns(current, task_active_pid_ns(t));
1229 } else if (info == SEND_SIG_PRIV) {
1230 /* Don't ignore kernel generated signals */
1231 force = true;
1232 } else if (has_si_pid_and_uid(info)) {
1233 /* SIGKILL and SIGSTOP is special or has ids */
Eric W. Biederman7a0cf092019-05-15 22:54:56 -05001234 struct user_namespace *t_user_ns;
Sukadev Bhattiprolu921cf9f2009-04-02 16:58:05 -07001235
Eric W. Biederman7a0cf092019-05-15 22:54:56 -05001236 rcu_read_lock();
1237 t_user_ns = task_cred_xxx(t, user_ns);
1238 if (current_user_ns() != t_user_ns) {
1239 kuid_t uid = make_kuid(current_user_ns(), info->si_uid);
1240 info->si_uid = from_kuid_munged(t_user_ns, uid);
1241 }
1242 rcu_read_unlock();
1243
Eric W. Biederman8ad23de2019-05-15 22:23:32 -05001244 /* A kernel generated signal? */
1245 force = (info->si_code == SI_KERNEL);
1246
1247 /* From an ancestor pid namespace? */
1248 if (!task_pid_nr_ns(current, task_active_pid_ns(t))) {
Eric W. Biederman7a0cf092019-05-15 22:54:56 -05001249 info->si_pid = 0;
Eric W. Biederman8ad23de2019-05-15 22:23:32 -05001250 force = true;
1251 }
Eric W. Biederman7a0cf092019-05-15 22:54:56 -05001252 }
Eric W. Biederman157cc182022-04-22 09:48:54 -05001253 return __send_signal_locked(sig, info, t, type, force);
Sukadev Bhattiprolu7978b562009-04-02 16:58:04 -07001254}
1255
Al Viro4aaefee2012-11-05 13:09:56 -05001256static void print_fatal_signal(int signr)
Ingo Molnar45807a12007-07-15 23:40:10 -07001257{
Al Viro6a542d12020-06-08 12:21:07 -04001258 struct pt_regs *regs = task_pt_regs(current);
Wang Xiaoqiang747800ef2016-05-23 16:23:59 -07001259 pr_info("potentially unexpected fatal signal %d.\n", signr);
Ingo Molnar45807a12007-07-15 23:40:10 -07001260
Al Viroca5cd872007-10-29 04:31:16 +00001261#if defined(__i386__) && !defined(__arch_um__)
Wang Xiaoqiang747800ef2016-05-23 16:23:59 -07001262 pr_info("code at %08lx: ", regs->ip);
Ingo Molnar45807a12007-07-15 23:40:10 -07001263 {
1264 int i;
1265 for (i = 0; i < 16; i++) {
1266 unsigned char insn;
1267
Andi Kleenb45c6e72010-01-08 14:42:52 -08001268 if (get_user(insn, (unsigned char *)(regs->ip + i)))
1269 break;
Wang Xiaoqiang747800ef2016-05-23 16:23:59 -07001270 pr_cont("%02x ", insn);
Ingo Molnar45807a12007-07-15 23:40:10 -07001271 }
1272 }
Wang Xiaoqiang747800ef2016-05-23 16:23:59 -07001273 pr_cont("\n");
Ingo Molnar45807a12007-07-15 23:40:10 -07001274#endif
Ed Swierk3a9f84d2009-01-26 15:33:31 -08001275 preempt_disable();
Ingo Molnar45807a12007-07-15 23:40:10 -07001276 show_regs(regs);
Ed Swierk3a9f84d2009-01-26 15:33:31 -08001277 preempt_enable();
Ingo Molnar45807a12007-07-15 23:40:10 -07001278}
1279
1280static int __init setup_print_fatal_signals(char *str)
1281{
1282 get_option (&str, &print_fatal_signals);
1283
1284 return 1;
1285}
1286
1287__setup("print-fatal-signals=", setup_print_fatal_signals);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001289int do_send_sig_info(int sig, struct kernel_siginfo *info, struct task_struct *p,
Eric W. Biederman40b3b022018-07-21 10:45:15 -05001290 enum pid_type type)
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001291{
1292 unsigned long flags;
1293 int ret = -ESRCH;
1294
1295 if (lock_task_sighand(p, &flags)) {
Eric W. Biederman157cc182022-04-22 09:48:54 -05001296 ret = send_signal_locked(sig, info, p, type);
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001297 unlock_task_sighand(p, &flags);
1298 }
1299
1300 return ret;
1301}
1302
Eric W. Biedermane349d942021-11-18 11:11:13 -06001303enum sig_handler {
1304 HANDLER_CURRENT, /* If reachable use the current handler */
1305 HANDLER_SIG_DFL, /* Always use SIG_DFL handler semantics */
1306 HANDLER_EXIT, /* Only visible as the process exit code */
1307};
1308
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309/*
1310 * Force a signal that the process can't ignore: if necessary
1311 * we unblock the signal and change any SIG_IGN to SIG_DFL.
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001312 *
1313 * Note: If we unblock the signal, we always reset it to SIG_DFL,
1314 * since we do not want to have a signal handler that was blocked
1315 * be invoked when user space had explicitly blocked it.
1316 *
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001317 * We don't want to have recursive SIGSEGV's etc, for example,
1318 * that is why we also clear SIGNAL_UNKILLABLE.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 */
Eric W. Biederman59c0e692019-02-07 11:01:20 -06001320static int
Eric W. Biedermane349d942021-11-18 11:11:13 -06001321force_sig_info_to_task(struct kernel_siginfo *info, struct task_struct *t,
1322 enum sig_handler handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323{
1324 unsigned long int flags;
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001325 int ret, blocked, ignored;
1326 struct k_sigaction *action;
Eric W. Biederman59c0e692019-02-07 11:01:20 -06001327 int sig = info->si_signo;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 spin_lock_irqsave(&t->sighand->siglock, flags);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001330 action = &t->sighand->action[sig-1];
1331 ignored = action->sa.sa_handler == SIG_IGN;
1332 blocked = sigismember(&t->blocked, sig);
Eric W. Biedermane349d942021-11-18 11:11:13 -06001333 if (blocked || ignored || (handler != HANDLER_CURRENT)) {
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001334 action->sa.sa_handler = SIG_DFL;
Eric W. Biedermane349d942021-11-18 11:11:13 -06001335 if (handler == HANDLER_EXIT)
1336 action->sa.sa_flags |= SA_IMMUTABLE;
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001337 if (blocked) {
1338 sigdelset(&t->blocked, sig);
Roland McGrath7bb44ad2007-05-23 13:57:44 -07001339 recalc_sigpending_and_wake(t);
Linus Torvaldsae74c3b2006-08-02 20:17:49 -07001340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
Jamie Ileseb61b592017-08-18 15:16:18 -07001342 /*
1343 * Don't clear SIGNAL_UNKILLABLE for traced tasks, users won't expect
Kees Cook5c722632022-02-08 00:57:17 -08001344 * debugging to leave init killable. But HANDLER_EXIT is always fatal.
Jamie Ileseb61b592017-08-18 15:16:18 -07001345 */
Kees Cook5c722632022-02-08 00:57:17 -08001346 if (action->sa.sa_handler == SIG_DFL &&
1347 (!t->ptrace || (handler == HANDLER_EXIT)))
Oleg Nesterov80fe7282008-04-30 00:53:05 -07001348 t->signal->flags &= ~SIGNAL_UNKILLABLE;
Eric W. Biederman157cc182022-04-22 09:48:54 -05001349 ret = send_signal_locked(sig, info, t, PIDTYPE_PID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 spin_unlock_irqrestore(&t->sighand->siglock, flags);
1351
1352 return ret;
1353}
1354
Eric W. Biedermana89e9b82019-05-15 10:11:09 -05001355int force_sig_info(struct kernel_siginfo *info)
Eric W. Biederman59c0e692019-02-07 11:01:20 -06001356{
Eric W. Biedermane349d942021-11-18 11:11:13 -06001357 return force_sig_info_to_task(info, current, HANDLER_CURRENT);
Eric W. Biederman59c0e692019-02-07 11:01:20 -06001358}
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360/*
1361 * Nuke all other threads in the group.
1362 */
Oleg Nesterov09faef12010-05-26 14:43:11 -07001363int zap_other_threads(struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364{
Oleg Nesterov09faef12010-05-26 14:43:11 -07001365 struct task_struct *t = p;
1366 int count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 p->signal->group_stop_count = 0;
1369
Oleg Nesterov09faef12010-05-26 14:43:11 -07001370 while_each_thread(p, t) {
Tejun Heo6dfca322011-06-02 11:14:00 +02001371 task_clear_jobctl_pending(t, JOBCTL_PENDING_MASK);
Mike Christief9010db2023-06-01 13:32:32 -05001372 /* Don't require de_thread to wait for the vhost_worker */
1373 if ((t->flags & (PF_IO_WORKER | PF_USER_WORKER)) != PF_USER_WORKER)
1374 count++;
Oleg Nesterov09faef12010-05-26 14:43:11 -07001375
1376 /* Don't bother with already dead threads */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 if (t->exit_state)
1378 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 sigaddset(&t->pending.signal, SIGKILL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 signal_wake_up(t, 1);
1381 }
Oleg Nesterov09faef12010-05-26 14:43:11 -07001382
1383 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
Namhyung Kimb8ed3742010-10-27 15:34:06 -07001386struct sighand_struct *__lock_task_sighand(struct task_struct *tsk,
1387 unsigned long *flags)
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001388{
1389 struct sighand_struct *sighand;
1390
Anna-Maria Gleixner59dc6f32018-05-25 11:05:07 +02001391 rcu_read_lock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001392 for (;;) {
1393 sighand = rcu_dereference(tsk->sighand);
Anna-Maria Gleixner59dc6f32018-05-25 11:05:07 +02001394 if (unlikely(sighand == NULL))
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001395 break;
Anna-Maria Gleixner59dc6f32018-05-25 11:05:07 +02001396
Oleg Nesterov392809b2014-09-28 23:44:18 +02001397 /*
1398 * This sighand can be already freed and even reused, but
Paul E. McKenney5f0d5a32017-01-18 02:53:44 -08001399 * we rely on SLAB_TYPESAFE_BY_RCU and sighand_ctor() which
Oleg Nesterov392809b2014-09-28 23:44:18 +02001400 * initializes ->siglock: this slab can't go away, it has
1401 * the same object type, ->siglock can't be reinitialized.
1402 *
1403 * We need to ensure that tsk->sighand is still the same
1404 * after we take the lock, we can race with de_thread() or
1405 * __exit_signal(). In the latter case the next iteration
1406 * must see ->sighand == NULL.
1407 */
Anna-Maria Gleixner59dc6f32018-05-25 11:05:07 +02001408 spin_lock_irqsave(&sighand->siglock, *flags);
Madhuparna Bhowmik913292c2020-01-24 10:29:08 +05301409 if (likely(sighand == rcu_access_pointer(tsk->sighand)))
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001410 break;
Anna-Maria Gleixner59dc6f32018-05-25 11:05:07 +02001411 spin_unlock_irqrestore(&sighand->siglock, *flags);
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001412 }
Anna-Maria Gleixner59dc6f32018-05-25 11:05:07 +02001413 rcu_read_unlock();
Oleg Nesterovf63ee722006-03-28 16:11:13 -08001414
1415 return sighand;
1416}
1417
Frederic Weisbeckera5dec9f2021-07-26 14:55:08 +02001418#ifdef CONFIG_LOCKDEP
1419void lockdep_assert_task_sighand_held(struct task_struct *task)
1420{
1421 struct sighand_struct *sighand;
1422
1423 rcu_read_lock();
1424 sighand = rcu_dereference(task->sighand);
1425 if (sighand)
1426 lockdep_assert_held(&sighand->siglock);
1427 else
1428 WARN_ON_ONCE(1);
1429 rcu_read_unlock();
1430}
1431#endif
1432
David Howellsc69e8d92008-11-14 10:39:19 +11001433/*
1434 * send signal info to all the members of a group
David Howellsc69e8d92008-11-14 10:39:19 +11001435 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001436int group_send_sig_info(int sig, struct kernel_siginfo *info,
1437 struct task_struct *p, enum pid_type type)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
David Howells694f6902010-08-04 16:59:14 +01001439 int ret;
1440
1441 rcu_read_lock();
1442 ret = check_kill_permission(sig, info, p);
1443 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07001445 if (!ret && sig)
Eric W. Biederman40b3b022018-07-21 10:45:15 -05001446 ret = do_send_sig_info(sig, info, p, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
1448 return ret;
1449}
1450
1451/*
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001452 * __kill_pgrp_info() sends a signal to a process group: this is what the tty
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 * control characters do (^C, ^Z etc)
David Howellsc69e8d92008-11-14 10:39:19 +11001454 * - the caller must hold at least a readlock on tasklist_lock
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001456int __kill_pgrp_info(int sig, struct kernel_siginfo *info, struct pid *pgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457{
1458 struct task_struct *p = NULL;
1459 int retval, success;
1460
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 success = 0;
1462 retval = -ESRCH;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001463 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Eric W. Biederman01024982018-07-13 18:40:57 -05001464 int err = group_send_sig_info(sig, info, p, PIDTYPE_PGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 success |= !err;
1466 retval = err;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001467 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 return success ? 0 : retval;
1469}
1470
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001471int kill_pid_info(int sig, struct kernel_siginfo *info, struct pid *pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472{
Oleg Nesterovd36174b2008-02-08 04:19:18 -08001473 int error = -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 struct task_struct *p;
1475
Paul E. McKenneyeca1a082014-10-23 11:41:22 -07001476 for (;;) {
1477 rcu_read_lock();
1478 p = pid_task(pid, PIDTYPE_PID);
1479 if (p)
Eric W. Biederman01024982018-07-13 18:40:57 -05001480 error = group_send_sig_info(sig, info, p, PIDTYPE_TGID);
Paul E. McKenneyeca1a082014-10-23 11:41:22 -07001481 rcu_read_unlock();
1482 if (likely(!p || error != -ESRCH))
1483 return error;
Oleg Nesterov6ca25b52008-04-30 00:52:45 -07001484
Paul E. McKenneyeca1a082014-10-23 11:41:22 -07001485 /*
1486 * The task was unhashed in between, try again. If it
1487 * is dead, pid_task() will return NULL, if we race with
1488 * de_thread() it will find the new leader.
1489 */
1490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491}
1492
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001493static int kill_proc_info(int sig, struct kernel_siginfo *info, pid_t pid)
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001494{
1495 int error;
1496 rcu_read_lock();
Pavel Emelyanovb4888932007-10-18 23:40:14 -07001497 error = kill_pid_info(sig, info, find_vpid(pid));
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001498 rcu_read_unlock();
1499 return error;
1500}
1501
Christian Braunerbb17fcc2018-08-21 21:59:55 -07001502static inline bool kill_as_cred_perm(const struct cred *cred,
1503 struct task_struct *target)
Serge Hallynd178bc32011-09-26 10:45:18 -05001504{
1505 const struct cred *pcred = __task_cred(target);
Christian Braunerbb17fcc2018-08-21 21:59:55 -07001506
1507 return uid_eq(cred->euid, pcred->suid) ||
1508 uid_eq(cred->euid, pcred->uid) ||
1509 uid_eq(cred->uid, pcred->suid) ||
1510 uid_eq(cred->uid, pcred->uid);
Serge Hallynd178bc32011-09-26 10:45:18 -05001511}
1512
Eric W. Biederman70f1b0d2019-02-07 19:44:12 -06001513/*
1514 * The usb asyncio usage of siginfo is wrong. The glibc support
1515 * for asyncio which uses SI_ASYNCIO assumes the layout is SIL_RT.
1516 * AKA after the generic fields:
1517 * kernel_pid_t si_pid;
1518 * kernel_uid32_t si_uid;
1519 * sigval_t si_value;
1520 *
1521 * Unfortunately when usb generates SI_ASYNCIO it assumes the layout
1522 * after the generic fields is:
1523 * void __user *si_addr;
1524 *
1525 * This is a practical problem when there is a 64bit big endian kernel
1526 * and a 32bit userspace. As the 32bit address will encoded in the low
1527 * 32bits of the pointer. Those low 32bits will be stored at higher
1528 * address than appear in a 32 bit pointer. So userspace will not
1529 * see the address it was expecting for it's completions.
1530 *
1531 * There is nothing in the encoding that can allow
1532 * copy_siginfo_to_user32 to detect this confusion of formats, so
1533 * handle this by requiring the caller of kill_pid_usb_asyncio to
1534 * notice when this situration takes place and to store the 32bit
1535 * pointer in sival_int, instead of sival_addr of the sigval_t addr
1536 * parameter.
1537 */
1538int kill_pid_usb_asyncio(int sig, int errno, sigval_t addr,
1539 struct pid *pid, const struct cred *cred)
Harald Welte46113832005-10-10 19:44:29 +02001540{
Eric W. Biederman70f1b0d2019-02-07 19:44:12 -06001541 struct kernel_siginfo info;
Harald Welte46113832005-10-10 19:44:29 +02001542 struct task_struct *p;
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001543 unsigned long flags;
Eric W. Biederman70f1b0d2019-02-07 19:44:12 -06001544 int ret = -EINVAL;
1545
Zhiqiang Liueaec2b02020-03-30 10:18:33 +08001546 if (!valid_signal(sig))
1547 return ret;
1548
Eric W. Biederman70f1b0d2019-02-07 19:44:12 -06001549 clear_siginfo(&info);
1550 info.si_signo = sig;
1551 info.si_errno = errno;
1552 info.si_code = SI_ASYNCIO;
1553 *((sigval_t *)&info.si_pid) = addr;
Harald Welte46113832005-10-10 19:44:29 +02001554
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001555 rcu_read_lock();
Eric W. Biederman2425c082006-10-02 02:17:28 -07001556 p = pid_task(pid, PIDTYPE_PID);
Harald Welte46113832005-10-10 19:44:29 +02001557 if (!p) {
1558 ret = -ESRCH;
1559 goto out_unlock;
1560 }
Eric W. Biederman70f1b0d2019-02-07 19:44:12 -06001561 if (!kill_as_cred_perm(cred, p)) {
Harald Welte46113832005-10-10 19:44:29 +02001562 ret = -EPERM;
1563 goto out_unlock;
1564 }
Eric W. Biederman70f1b0d2019-02-07 19:44:12 -06001565 ret = security_task_kill(p, &info, sig, cred);
David Quigley8f95dc52006-06-30 01:55:47 -07001566 if (ret)
1567 goto out_unlock;
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001568
1569 if (sig) {
1570 if (lock_task_sighand(p, &flags)) {
Eric W. Biederman157cc182022-04-22 09:48:54 -05001571 ret = __send_signal_locked(sig, &info, p, PIDTYPE_TGID, false);
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001572 unlock_task_sighand(p, &flags);
1573 } else
1574 ret = -ESRCH;
Harald Welte46113832005-10-10 19:44:29 +02001575 }
1576out_unlock:
Thomas Gleixner14d8c9f2009-12-10 00:53:17 +00001577 rcu_read_unlock();
Harald Welte46113832005-10-10 19:44:29 +02001578 return ret;
1579}
Eric W. Biederman70f1b0d2019-02-07 19:44:12 -06001580EXPORT_SYMBOL_GPL(kill_pid_usb_asyncio);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582/*
1583 * kill_something_info() interprets pid in interesting ways just like kill(2).
1584 *
1585 * POSIX specifies that kill(-1,sig) is unspecified, but what we have
1586 * is probably wrong. Should make it like BSD or SYSV.
1587 */
1588
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001589static int kill_something_info(int sig, struct kernel_siginfo *info, pid_t pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001591 int ret;
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001592
Zhiqiang Liu3075afd2020-03-30 10:44:43 +08001593 if (pid > 0)
1594 return kill_proc_info(sig, info, pid);
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001595
zhongjiang4ea77012017-07-10 15:52:57 -07001596 /* -INT_MIN is undefined. Exclude this case to avoid a UBSAN warning */
1597 if (pid == INT_MIN)
1598 return -ESRCH;
1599
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001600 read_lock(&tasklist_lock);
1601 if (pid != -1) {
1602 ret = __kill_pgrp_info(sig, info,
1603 pid ? find_vpid(-pid) : task_pgrp(current));
1604 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 int retval = 0, count = 0;
1606 struct task_struct * p;
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 for_each_process(p) {
Sukadev Bhattiprolud25141a2008-10-29 14:01:11 -07001609 if (task_pid_vnr(p) > 1 &&
1610 !same_thread_group(p, current)) {
Eric W. Biederman01024982018-07-13 18:40:57 -05001611 int err = group_send_sig_info(sig, info, p,
1612 PIDTYPE_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 ++count;
1614 if (err != -EPERM)
1615 retval = err;
1616 }
1617 }
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001618 ret = count ? retval : -ESRCH;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 }
Pavel Emelyanovd5df7632008-02-08 04:19:22 -08001620 read_unlock(&tasklist_lock);
1621
Eric W. Biederman8d42db182007-02-12 00:52:55 -08001622 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623}
1624
1625/*
1626 * These are for backward compatibility with the rest of the kernel source.
1627 */
1628
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001629int send_sig_info(int sig, struct kernel_siginfo *info, struct task_struct *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 /*
1632 * Make sure legacy kernel users don't send in bad values
1633 * (normal paths check this in check_kill_permission).
1634 */
Jesper Juhl7ed20e12005-05-01 08:59:14 -07001635 if (!valid_signal(sig))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return -EINVAL;
1637
Eric W. Biederman40b3b022018-07-21 10:45:15 -05001638 return do_send_sig_info(sig, info, p, PIDTYPE_PID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
Eric W. Biedermanfb50f5a2018-09-13 19:26:35 +02001640EXPORT_SYMBOL(send_sig_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001642#define __si_special(priv) \
1643 ((priv) ? SEND_SIG_PRIV : SEND_SIG_NOINFO)
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645int
1646send_sig(int sig, struct task_struct *p, int priv)
1647{
Oleg Nesterovb67a1b92005-10-30 15:03:44 -08001648 return send_sig_info(sig, __si_special(priv), p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
Eric W. Biedermanfb50f5a2018-09-13 19:26:35 +02001650EXPORT_SYMBOL(send_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Eric W. Biederman3cf5d072019-05-23 10:17:27 -05001652void force_sig(int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
Eric W. Biedermanffafd232019-05-14 19:17:47 -05001654 struct kernel_siginfo info;
1655
1656 clear_siginfo(&info);
1657 info.si_signo = sig;
1658 info.si_errno = 0;
1659 info.si_code = SI_KERNEL;
1660 info.si_pid = 0;
1661 info.si_uid = 0;
Eric W. Biedermana89e9b82019-05-15 10:11:09 -05001662 force_sig_info(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663}
Eric W. Biedermanfb50f5a2018-09-13 19:26:35 +02001664EXPORT_SYMBOL(force_sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665
Eric W. Biederman26d5bad2021-10-20 12:43:59 -05001666void force_fatal_sig(int sig)
1667{
1668 struct kernel_siginfo info;
1669
1670 clear_siginfo(&info);
1671 info.si_signo = sig;
1672 info.si_errno = 0;
1673 info.si_code = SI_KERNEL;
1674 info.si_pid = 0;
1675 info.si_uid = 0;
Eric W. Biedermane349d942021-11-18 11:11:13 -06001676 force_sig_info_to_task(&info, current, HANDLER_SIG_DFL);
Eric W. Biederman26d5bad2021-10-20 12:43:59 -05001677}
1678
Eric W. Biedermanfcb116b2021-11-18 14:23:21 -06001679void force_exit_sig(int sig)
1680{
1681 struct kernel_siginfo info;
1682
1683 clear_siginfo(&info);
1684 info.si_signo = sig;
1685 info.si_errno = 0;
1686 info.si_code = SI_KERNEL;
1687 info.si_pid = 0;
1688 info.si_uid = 0;
1689 force_sig_info_to_task(&info, current, HANDLER_EXIT);
1690}
1691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692/*
1693 * When things go south during signal handling, we
1694 * will force a SIGSEGV. And if the signal that caused
1695 * the problem was already a SIGSEGV, we'll want to
1696 * make sure we don't even try to deliver the signal..
1697 */
Eric W. Biedermancb44c9a2019-05-21 10:03:48 -05001698void force_sigsegv(int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
Eric W. Biederman26d5bad2021-10-20 12:43:59 -05001700 if (sig == SIGSEGV)
1701 force_fatal_sig(SIGSEGV);
1702 else
1703 force_sig(SIGSEGV);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704}
1705
Eric W. Biederman91ca1802019-02-06 16:39:13 -06001706int force_sig_fault_to_task(int sig, int code, void __user *addr
Eric W. Biedermanf8ec6602018-01-18 14:54:54 -06001707 ___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr)
1708 , struct task_struct *t)
1709{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001710 struct kernel_siginfo info;
Eric W. Biedermanf8ec6602018-01-18 14:54:54 -06001711
1712 clear_siginfo(&info);
1713 info.si_signo = sig;
1714 info.si_errno = 0;
1715 info.si_code = code;
1716 info.si_addr = addr;
Eric W. Biedermanf8ec6602018-01-18 14:54:54 -06001717#ifdef __ia64__
1718 info.si_imm = imm;
1719 info.si_flags = flags;
1720 info.si_isr = isr;
1721#endif
Eric W. Biedermane349d942021-11-18 11:11:13 -06001722 return force_sig_info_to_task(&info, t, HANDLER_CURRENT);
Eric W. Biedermanf8ec6602018-01-18 14:54:54 -06001723}
1724
Eric W. Biederman91ca1802019-02-06 16:39:13 -06001725int force_sig_fault(int sig, int code, void __user *addr
Eric W. Biederman2e1661d22019-05-23 11:04:24 -05001726 ___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr))
Eric W. Biederman91ca1802019-02-06 16:39:13 -06001727{
1728 return force_sig_fault_to_task(sig, code, addr
Eric W. Biederman2e1661d22019-05-23 11:04:24 -05001729 ___ARCH_SI_IA64(imm, flags, isr), current);
Eric W. Biedermanf8ec6602018-01-18 14:54:54 -06001730}
1731
1732int send_sig_fault(int sig, int code, void __user *addr
Eric W. Biedermanf8ec6602018-01-18 14:54:54 -06001733 ___ARCH_SI_IA64(int imm, unsigned int flags, unsigned long isr)
1734 , struct task_struct *t)
1735{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001736 struct kernel_siginfo info;
Eric W. Biedermanf8ec6602018-01-18 14:54:54 -06001737
1738 clear_siginfo(&info);
1739 info.si_signo = sig;
1740 info.si_errno = 0;
1741 info.si_code = code;
1742 info.si_addr = addr;
Eric W. Biedermanf8ec6602018-01-18 14:54:54 -06001743#ifdef __ia64__
1744 info.si_imm = imm;
1745 info.si_flags = flags;
1746 info.si_isr = isr;
1747#endif
1748 return send_sig_info(info.si_signo, &info, t);
1749}
1750
Eric W. Biedermanf8eac902019-02-05 18:14:19 -06001751int force_sig_mceerr(int code, void __user *addr, short lsb)
Eric W. Biederman38246732018-01-18 18:54:31 -06001752{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001753 struct kernel_siginfo info;
Eric W. Biederman38246732018-01-18 18:54:31 -06001754
1755 WARN_ON((code != BUS_MCEERR_AO) && (code != BUS_MCEERR_AR));
1756 clear_siginfo(&info);
1757 info.si_signo = SIGBUS;
1758 info.si_errno = 0;
1759 info.si_code = code;
1760 info.si_addr = addr;
1761 info.si_addr_lsb = lsb;
Eric W. Biedermana89e9b82019-05-15 10:11:09 -05001762 return force_sig_info(&info);
Eric W. Biederman38246732018-01-18 18:54:31 -06001763}
1764
1765int send_sig_mceerr(int code, void __user *addr, short lsb, struct task_struct *t)
1766{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001767 struct kernel_siginfo info;
Eric W. Biederman38246732018-01-18 18:54:31 -06001768
1769 WARN_ON((code != BUS_MCEERR_AO) && (code != BUS_MCEERR_AR));
1770 clear_siginfo(&info);
1771 info.si_signo = SIGBUS;
1772 info.si_errno = 0;
1773 info.si_code = code;
1774 info.si_addr = addr;
1775 info.si_addr_lsb = lsb;
1776 return send_sig_info(info.si_signo, &info, t);
1777}
1778EXPORT_SYMBOL(send_sig_mceerr);
Eric W. Biederman38246732018-01-18 18:54:31 -06001779
Eric W. Biederman38246732018-01-18 18:54:31 -06001780int force_sig_bnderr(void __user *addr, void __user *lower, void __user *upper)
1781{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001782 struct kernel_siginfo info;
Eric W. Biederman38246732018-01-18 18:54:31 -06001783
1784 clear_siginfo(&info);
1785 info.si_signo = SIGSEGV;
1786 info.si_errno = 0;
1787 info.si_code = SEGV_BNDERR;
1788 info.si_addr = addr;
1789 info.si_lower = lower;
1790 info.si_upper = upper;
Eric W. Biedermana89e9b82019-05-15 10:11:09 -05001791 return force_sig_info(&info);
Eric W. Biederman38246732018-01-18 18:54:31 -06001792}
Eric W. Biederman38246732018-01-18 18:54:31 -06001793
1794#ifdef SEGV_PKUERR
1795int force_sig_pkuerr(void __user *addr, u32 pkey)
1796{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001797 struct kernel_siginfo info;
Eric W. Biederman38246732018-01-18 18:54:31 -06001798
1799 clear_siginfo(&info);
1800 info.si_signo = SIGSEGV;
1801 info.si_errno = 0;
1802 info.si_code = SEGV_PKUERR;
1803 info.si_addr = addr;
1804 info.si_pkey = pkey;
Eric W. Biedermana89e9b82019-05-15 10:11:09 -05001805 return force_sig_info(&info);
Eric W. Biederman38246732018-01-18 18:54:31 -06001806}
1807#endif
Eric W. Biedermanf8ec6602018-01-18 14:54:54 -06001808
Marco Elver78ed93d2022-04-04 13:12:04 +02001809int send_sig_perf(void __user *addr, u32 type, u64 sig_data)
Eric W. Biedermanaf5eeab2021-05-02 14:27:24 -05001810{
1811 struct kernel_siginfo info;
1812
1813 clear_siginfo(&info);
Eric W. Biederman0683b532021-05-02 17:28:31 -05001814 info.si_signo = SIGTRAP;
1815 info.si_errno = 0;
1816 info.si_code = TRAP_PERF;
1817 info.si_addr = addr;
1818 info.si_perf_data = sig_data;
1819 info.si_perf_type = type;
1820
Marco Elver78ed93d2022-04-04 13:12:04 +02001821 /*
1822 * Signals generated by perf events should not terminate the whole
1823 * process if SIGTRAP is blocked, however, delivering the signal
1824 * asynchronously is better than not delivering at all. But tell user
1825 * space if the signal was asynchronous, so it can clearly be
1826 * distinguished from normal synchronous ones.
1827 */
1828 info.si_perf_flags = sigismember(&current->blocked, info.si_signo) ?
1829 TRAP_PERF_FLAG_ASYNC :
1830 0;
1831
1832 return send_sig_info(info.si_signo, &info, current);
Eric W. Biedermanaf5eeab2021-05-02 14:27:24 -05001833}
1834
Eric W. Biederman307d5222021-06-23 16:44:32 -05001835/**
1836 * force_sig_seccomp - signals the task to allow in-process syscall emulation
1837 * @syscall: syscall number to send to userland
1838 * @reason: filter-supplied reason code to send to userland (via si_errno)
Randy Dunlap64103492021-12-21 19:10:27 -08001839 * @force_coredump: true to trigger a coredump
Eric W. Biederman307d5222021-06-23 16:44:32 -05001840 *
1841 * Forces a SIGSYS with a code of SYS_SECCOMP and related sigsys info.
1842 */
1843int force_sig_seccomp(int syscall, int reason, bool force_coredump)
1844{
1845 struct kernel_siginfo info;
1846
1847 clear_siginfo(&info);
1848 info.si_signo = SIGSYS;
1849 info.si_code = SYS_SECCOMP;
1850 info.si_call_addr = (void __user *)KSTK_EIP(current);
1851 info.si_errno = reason;
1852 info.si_arch = syscall_get_arch(current);
1853 info.si_syscall = syscall;
Eric W. Biedermane349d942021-11-18 11:11:13 -06001854 return force_sig_info_to_task(&info, current,
1855 force_coredump ? HANDLER_EXIT : HANDLER_CURRENT);
Eric W. Biederman307d5222021-06-23 16:44:32 -05001856}
1857
Eric W. Biedermanf71dd7d2018-01-22 14:37:25 -06001858/* For the crazy architectures that include trap information in
1859 * the errno field, instead of an actual errno value.
1860 */
1861int force_sig_ptrace_errno_trap(int errno, void __user *addr)
1862{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02001863 struct kernel_siginfo info;
Eric W. Biedermanf71dd7d2018-01-22 14:37:25 -06001864
1865 clear_siginfo(&info);
1866 info.si_signo = SIGTRAP;
1867 info.si_errno = errno;
1868 info.si_code = TRAP_HWBKPT;
1869 info.si_addr = addr;
Eric W. Biedermana89e9b82019-05-15 10:11:09 -05001870 return force_sig_info(&info);
Eric W. Biedermanf71dd7d2018-01-22 14:37:25 -06001871}
1872
Eric W. Biederman2c9f7ea2021-05-28 13:38:19 -05001873/* For the rare architectures that include trap information using
1874 * si_trapno.
1875 */
1876int force_sig_fault_trapno(int sig, int code, void __user *addr, int trapno)
1877{
1878 struct kernel_siginfo info;
1879
1880 clear_siginfo(&info);
1881 info.si_signo = sig;
1882 info.si_errno = 0;
1883 info.si_code = code;
1884 info.si_addr = addr;
1885 info.si_trapno = trapno;
1886 return force_sig_info(&info);
1887}
1888
Eric W. Biederman7de5f682021-05-28 14:15:51 -05001889/* For the rare architectures that include trap information using
1890 * si_trapno.
1891 */
1892int send_sig_fault_trapno(int sig, int code, void __user *addr, int trapno,
1893 struct task_struct *t)
1894{
1895 struct kernel_siginfo info;
1896
1897 clear_siginfo(&info);
1898 info.si_signo = sig;
1899 info.si_errno = 0;
1900 info.si_code = code;
1901 info.si_addr = addr;
1902 info.si_trapno = trapno;
1903 return send_sig_info(info.si_signo, &info, t);
1904}
1905
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001906int kill_pgrp(struct pid *pid, int sig, int priv)
1907{
Pavel Emelyanov146a5052008-02-08 04:19:22 -08001908 int ret;
1909
1910 read_lock(&tasklist_lock);
1911 ret = __kill_pgrp_info(sig, __si_special(priv), pid);
1912 read_unlock(&tasklist_lock);
1913
1914 return ret;
Eric W. Biedermanc4b92fc2006-10-02 02:17:10 -07001915}
1916EXPORT_SYMBOL(kill_pgrp);
1917
1918int kill_pid(struct pid *pid, int sig, int priv)
1919{
1920 return kill_pid_info(sig, __si_special(priv), pid);
1921}
1922EXPORT_SYMBOL(kill_pid);
1923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924/*
1925 * These functions support sending signals using preallocated sigqueue
1926 * structures. This is needed "because realtime applications cannot
1927 * afford to lose notifications of asynchronous events, like timer
Randy Dunlap5aba0852011-04-04 14:59:31 -07001928 * expirations or I/O completions". In the case of POSIX Timers
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 * we allocate the sigqueue structure from the timer_create. If this
1930 * allocation fails we are able to report the failure to the application
1931 * with an EAGAIN error.
1932 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933struct sigqueue *sigqueue_alloc(void)
1934{
Thomas Gleixner69995eb2021-03-22 10:19:42 +01001935 return __sigqueue_alloc(-1, current, GFP_KERNEL, 0, SIGQUEUE_PREALLOC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937
1938void sigqueue_free(struct sigqueue *q)
1939{
1940 unsigned long flags;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001941 spinlock_t *lock = &current->sighand->siglock;
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
1944 /*
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001945 * We must hold ->siglock while testing q->list
1946 * to serialize with collect_signal() or with
Oleg Nesterovda7978b2008-05-23 13:04:41 -07001947 * __exit_signal()->flush_sigqueue().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001949 spin_lock_irqsave(lock, flags);
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001950 q->flags &= ~SIGQUEUE_PREALLOC;
1951 /*
1952 * If it is queued it will be freed when dequeued,
1953 * like the "regular" sigqueue.
1954 */
Oleg Nesterov60187d22007-08-30 23:56:35 -07001955 if (!list_empty(&q->list))
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001956 q = NULL;
Oleg Nesterov60187d22007-08-30 23:56:35 -07001957 spin_unlock_irqrestore(lock, flags);
1958
Oleg Nesterovc8e85b4f2008-05-26 20:55:42 +04001959 if (q)
1960 __sigqueue_free(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
1962
Eric W. Biederman24122c72018-07-20 14:30:23 -05001963int send_sigqueue(struct sigqueue *q, struct pid *pid, enum pid_type type)
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07001964{
Oleg Nesterove62e6652008-04-30 00:52:56 -07001965 int sig = q->info.si_signo;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001966 struct sigpending *pending;
Eric W. Biederman24122c72018-07-20 14:30:23 -05001967 struct task_struct *t;
Oleg Nesterove62e6652008-04-30 00:52:56 -07001968 unsigned long flags;
Oleg Nesterov163566f2011-11-22 21:37:41 +01001969 int ret, result;
Oleg Nesterov2ca35152008-04-30 00:52:54 -07001970
Pavel Emelyanov4cd4b6d2008-04-30 00:52:55 -07001971 BUG_ON(!(q->flags & SIGQUEUE_PREALLOC));
Oleg Nesterove62e6652008-04-30 00:52:56 -07001972
1973 ret = -1;
Eric W. Biederman24122c72018-07-20 14:30:23 -05001974 rcu_read_lock();
Dmitry Vyukovbcb7ee72023-03-16 13:30:27 +01001975
1976 /*
1977 * This function is used by POSIX timers to deliver a timer signal.
1978 * Where type is PIDTYPE_PID (such as for timers with SIGEV_THREAD_ID
1979 * set), the signal must be delivered to the specific thread (queues
1980 * into t->pending).
1981 *
1982 * Where type is not PIDTYPE_PID, signals must be delivered to the
1983 * process. In this case, prefer to deliver to current if it is in
1984 * the same thread group as the target process, which avoids
1985 * unnecessarily waking up a potentially idle task.
1986 */
Eric W. Biederman24122c72018-07-20 14:30:23 -05001987 t = pid_task(pid, type);
Dmitry Vyukovbcb7ee72023-03-16 13:30:27 +01001988 if (!t)
1989 goto ret;
1990 if (type != PIDTYPE_PID && same_thread_group(t, current))
1991 t = current;
1992 if (!likely(lock_task_sighand(t, &flags)))
Oleg Nesterove62e6652008-04-30 00:52:56 -07001993 goto ret;
1994
Oleg Nesterov7e695a52008-04-30 00:52:59 -07001995 ret = 1; /* the signal is ignored */
Oleg Nesterov163566f2011-11-22 21:37:41 +01001996 result = TRACE_SIGNAL_IGNORED;
Oleg Nesterovdef8cf72012-03-23 15:02:45 -07001997 if (!prepare_signal(sig, t, false))
Oleg Nesterove62e6652008-04-30 00:52:56 -07001998 goto out;
1999
2000 ret = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07002001 if (unlikely(!list_empty(&q->list))) {
2002 /*
2003 * If an SI_TIMER entry is already queue just increment
2004 * the overrun count.
2005 */
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07002006 BUG_ON(q->info.si_code != SI_TIMER);
2007 q->info.si_overrun++;
Oleg Nesterov163566f2011-11-22 21:37:41 +01002008 result = TRACE_SIGNAL_ALREADY_PENDING;
Oleg Nesterove62e6652008-04-30 00:52:56 -07002009 goto out;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07002010 }
Oleg Nesterovba661292008-07-23 20:52:05 +04002011 q->info.si_overrun = 0;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07002012
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07002013 signalfd_notify(t, sig);
Eric W. Biederman24122c72018-07-20 14:30:23 -05002014 pending = (type != PIDTYPE_PID) ? &t->signal->shared_pending : &t->pending;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07002015 list_add_tail(&q->list, &pending->list);
2016 sigaddset(&pending->signal, sig);
Eric W. Biederman07296142018-07-13 21:39:13 -05002017 complete_signal(sig, t, type);
Oleg Nesterov163566f2011-11-22 21:37:41 +01002018 result = TRACE_SIGNAL_DELIVERED;
Oleg Nesterove62e6652008-04-30 00:52:56 -07002019out:
Eric W. Biederman24122c72018-07-20 14:30:23 -05002020 trace_signal_generate(sig, &q->info, t, type != PIDTYPE_PID, result);
Oleg Nesterove62e6652008-04-30 00:52:56 -07002021 unlock_task_sighand(t, &flags);
2022ret:
Eric W. Biederman24122c72018-07-20 14:30:23 -05002023 rcu_read_unlock();
Oleg Nesterove62e6652008-04-30 00:52:56 -07002024 return ret;
Pavel Emelyanov9e3bd6c2008-04-30 00:52:41 -07002025}
2026
Joel Fernandes (Google)b53b0b92019-04-30 12:21:53 -04002027static void do_notify_pidfd(struct task_struct *task)
2028{
2029 struct pid *pid;
2030
Joel Fernandes (Google)1caf7d52019-07-24 12:48:16 -04002031 WARN_ON(task->exit_state == 0);
Joel Fernandes (Google)b53b0b92019-04-30 12:21:53 -04002032 pid = task_pid(task);
2033 wake_up_all(&pid->wait_pidfd);
2034}
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 * Let a parent know about the death of a child.
2038 * For a stopped/continued status change, use do_notify_parent_cldstop instead.
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07002039 *
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02002040 * Returns true if our parent ignored us and so we've switched to
2041 * self-reaping.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 */
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02002043bool do_notify_parent(struct task_struct *tsk, int sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002045 struct kernel_siginfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 unsigned long flags;
2047 struct sighand_struct *psig;
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02002048 bool autoreap = false;
Frederic Weisbeckerbde82852017-01-31 04:09:31 +01002049 u64 utime, stime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Linus Torvaldsa382f8f2022-07-06 12:20:59 -07002051 WARN_ON_ONCE(sig == -1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Linus Torvaldsa382f8f2022-07-06 12:20:59 -07002053 /* do_notify_parent_cldstop should have been called instead. */
2054 WARN_ON_ONCE(task_is_stopped_or_traced(tsk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Linus Torvaldsa382f8f2022-07-06 12:20:59 -07002056 WARN_ON_ONCE(!tsk->ptrace &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 (tsk->group_leader != tsk || !thread_group_empty(tsk)));
2058
Joel Fernandes (Google)b53b0b92019-04-30 12:21:53 -04002059 /* Wake up all pidfd waiters */
2060 do_notify_pidfd(tsk);
2061
Oleg Nesterovb6e238d2012-03-19 17:03:41 +01002062 if (sig != SIGCHLD) {
2063 /*
2064 * This is only possible if parent == real_parent.
2065 * Check if it has changed security domain.
2066 */
Eric W. Biedermand1e7fd62020-03-30 19:01:04 -05002067 if (tsk->parent_exec_id != READ_ONCE(tsk->parent->self_exec_id))
Oleg Nesterovb6e238d2012-03-19 17:03:41 +01002068 sig = SIGCHLD;
2069 }
2070
Eric W. Biedermanfaf1f222018-01-05 17:27:42 -06002071 clear_siginfo(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 info.si_signo = sig;
2073 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002074 /*
Eric W. Biederman32084502012-05-31 16:26:39 -07002075 * We are under tasklist_lock here so our parent is tied to
2076 * us and cannot change.
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002077 *
Eric W. Biederman32084502012-05-31 16:26:39 -07002078 * task_active_pid_ns will always return the same pid namespace
2079 * until a task passes through release_task.
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002080 *
2081 * write_lock() currently calls preempt_disable() which is the
2082 * same as rcu_read_lock(), but according to Oleg, this is not
2083 * correct to rely on this
2084 */
2085 rcu_read_lock();
Eric W. Biederman32084502012-05-31 16:26:39 -07002086 info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(tsk->parent));
Eric W. Biederman54ba47e2012-03-13 16:04:35 -07002087 info.si_uid = from_kuid_munged(task_cred_xxx(tsk->parent, user_ns),
2088 task_uid(tsk));
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002089 rcu_read_unlock();
2090
Frederic Weisbeckerbde82852017-01-31 04:09:31 +01002091 task_cputime(tsk, &utime, &stime);
2092 info.si_utime = nsec_to_clock_t(utime + tsk->signal->utime);
2093 info.si_stime = nsec_to_clock_t(stime + tsk->signal->stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
2095 info.si_status = tsk->exit_code & 0x7f;
2096 if (tsk->exit_code & 0x80)
2097 info.si_code = CLD_DUMPED;
2098 else if (tsk->exit_code & 0x7f)
2099 info.si_code = CLD_KILLED;
2100 else {
2101 info.si_code = CLD_EXITED;
2102 info.si_status = tsk->exit_code >> 8;
2103 }
2104
2105 psig = tsk->parent->sighand;
2106 spin_lock_irqsave(&psig->siglock, flags);
Tejun Heod21142e2011-06-17 16:50:34 +02002107 if (!tsk->ptrace && sig == SIGCHLD &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN ||
2109 (psig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDWAIT))) {
2110 /*
2111 * We are exiting and our parent doesn't care. POSIX.1
2112 * defines special semantics for setting SIGCHLD to SIG_IGN
2113 * or setting the SA_NOCLDWAIT flag: we should be reaped
2114 * automatically and not left for our parent's wait4 call.
2115 * Rather than having the parent do it as a magic kind of
2116 * signal handler, we just set this to tell do_exit that we
2117 * can be cleaned up without becoming a zombie. Note that
2118 * we still call __wake_up_parent in this case, because a
2119 * blocked sys_wait4 might now return -ECHILD.
2120 *
2121 * Whether we send SIGCHLD or not for SA_NOCLDWAIT
2122 * is implementation-defined: we do (if you don't want
2123 * it, just use SIG_IGN instead).
2124 */
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02002125 autoreap = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 if (psig->action[SIGCHLD-1].sa.sa_handler == SIG_IGN)
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02002127 sig = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 }
Eric W. Biederman61e713b2020-04-20 11:41:50 -05002129 /*
2130 * Send with __send_signal as si_pid and si_uid are in the
2131 * parent's namespaces.
2132 */
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02002133 if (valid_signal(sig) && sig)
Eric W. Biederman157cc182022-04-22 09:48:54 -05002134 __send_signal_locked(sig, &info, tsk->parent, PIDTYPE_TGID, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 __wake_up_parent(tsk, tsk->parent);
2136 spin_unlock_irqrestore(&psig->siglock, flags);
Roland McGrath2b2a1ff2008-07-25 19:45:54 -07002137
Oleg Nesterov53c8f9f2011-06-22 23:08:18 +02002138 return autoreap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
Tejun Heo75b95952011-03-23 10:37:01 +01002141/**
2142 * do_notify_parent_cldstop - notify parent of stopped/continued state change
2143 * @tsk: task reporting the state change
2144 * @for_ptracer: the notification is for ptracer
2145 * @why: CLD_{CONTINUED|STOPPED|TRAPPED} to report
2146 *
2147 * Notify @tsk's parent that the stopped/continued state has changed. If
2148 * @for_ptracer is %false, @tsk's group leader notifies to its real parent.
2149 * If %true, @tsk reports to @tsk->parent which should be the ptracer.
2150 *
2151 * CONTEXT:
2152 * Must be called with tasklist_lock at least read locked.
2153 */
2154static void do_notify_parent_cldstop(struct task_struct *tsk,
2155 bool for_ptracer, int why)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002157 struct kernel_siginfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 unsigned long flags;
Oleg Nesterovbc505a42005-09-06 15:17:32 -07002159 struct task_struct *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 struct sighand_struct *sighand;
Frederic Weisbeckerbde82852017-01-31 04:09:31 +01002161 u64 utime, stime;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Tejun Heo75b95952011-03-23 10:37:01 +01002163 if (for_ptracer) {
Oleg Nesterovbc505a42005-09-06 15:17:32 -07002164 parent = tsk->parent;
Tejun Heo75b95952011-03-23 10:37:01 +01002165 } else {
Oleg Nesterovbc505a42005-09-06 15:17:32 -07002166 tsk = tsk->group_leader;
2167 parent = tsk->real_parent;
2168 }
2169
Eric W. Biedermanfaf1f222018-01-05 17:27:42 -06002170 clear_siginfo(&info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 info.si_signo = SIGCHLD;
2172 info.si_errno = 0;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002173 /*
Randy Dunlap5aba0852011-04-04 14:59:31 -07002174 * see comment in do_notify_parent() about the following 4 lines
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002175 */
2176 rcu_read_lock();
Eric W. Biederman17cf22c2010-03-02 14:51:53 -08002177 info.si_pid = task_pid_nr_ns(tsk, task_active_pid_ns(parent));
Eric W. Biederman54ba47e2012-03-13 16:04:35 -07002178 info.si_uid = from_kuid_munged(task_cred_xxx(parent, user_ns), task_uid(tsk));
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002179 rcu_read_unlock();
2180
Frederic Weisbeckerbde82852017-01-31 04:09:31 +01002181 task_cputime(tsk, &utime, &stime);
2182 info.si_utime = nsec_to_clock_t(utime);
2183 info.si_stime = nsec_to_clock_t(stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185 info.si_code = why;
2186 switch (why) {
2187 case CLD_CONTINUED:
2188 info.si_status = SIGCONT;
2189 break;
2190 case CLD_STOPPED:
2191 info.si_status = tsk->signal->group_exit_code & 0x7f;
2192 break;
2193 case CLD_TRAPPED:
2194 info.si_status = tsk->exit_code & 0x7f;
2195 break;
2196 default:
2197 BUG();
2198 }
2199
2200 sighand = parent->sighand;
2201 spin_lock_irqsave(&sighand->siglock, flags);
2202 if (sighand->action[SIGCHLD-1].sa.sa_handler != SIG_IGN &&
2203 !(sighand->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
Eric W. Biedermane71ba122022-04-22 09:28:50 -05002204 send_signal_locked(SIGCHLD, &info, parent, PIDTYPE_TGID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 /*
2206 * Even if SIGCHLD is not generated, we must wake up wait4 calls.
2207 */
2208 __wake_up_parent(tsk, parent);
2209 spin_unlock_irqrestore(&sighand->siglock, flags);
2210}
2211
Roland McGrath1a669c22008-02-06 01:37:37 -08002212/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 * This must be called with current->sighand->siglock held.
2214 *
2215 * This should be the path for all ptrace stops.
2216 * We always set current->last_siginfo while stopped here.
2217 * That makes it a way to test a stopped process for
2218 * being ptrace-stopped vs being job-control-stopped.
2219 *
Eric W. Biederman6487d1d2022-01-27 12:19:13 -06002220 * Returns the signal the ptracer requested the code resume
2221 * with. If the code did not stop because the tracer is gone,
2222 * the stop signal remains unchanged unless clear_code.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 */
Eric W. Biederman57b6de02022-05-04 13:39:58 -05002224static int ptrace_stop(int exit_code, int why, unsigned long message,
2225 kernel_siginfo_t *info)
Namhyung Kimb8401152010-10-27 15:34:07 -07002226 __releases(&current->sighand->siglock)
2227 __acquires(&current->sighand->siglock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
Tejun Heoceb6bd62011-03-23 10:37:01 +01002229 bool gstop_done = false;
2230
Eric W. Biederman4f627af2021-09-02 16:10:11 -05002231 if (arch_ptrace_stop_needed()) {
Roland McGrath1a669c22008-02-06 01:37:37 -08002232 /*
2233 * The arch code has something special to do before a
2234 * ptrace stop. This is allowed to block, e.g. for faults
2235 * on user stack pages. We can't keep the siglock while
2236 * calling arch_ptrace_stop, so we must release it now.
2237 * To preserve proper semantics, we must do this before
2238 * any signal bookkeeping like checking group_stop_count.
Roland McGrath1a669c22008-02-06 01:37:37 -08002239 */
2240 spin_unlock_irq(&current->sighand->siglock);
Eric W. Biederman4f627af2021-09-02 16:10:11 -05002241 arch_ptrace_stop();
Roland McGrath1a669c22008-02-06 01:37:37 -08002242 spin_lock_irq(&current->sighand->siglock);
Roland McGrath1a669c22008-02-06 01:37:37 -08002243 }
2244
Eric W. Biederman7d613f92021-09-01 13:21:34 -05002245 /*
Eric W. Biederman2500ad12022-04-29 08:43:34 -05002246 * After this point ptrace_signal_wake_up or signal_wake_up
2247 * will clear TASK_TRACED if ptrace_unlink happens or a fatal
2248 * signal comes in. Handle previous ptrace_unlinks and fatal
2249 * signals here to prevent ptrace_stop sleeping in schedule.
Eric W. Biederman7d613f92021-09-01 13:21:34 -05002250 */
Eric W. Biederman2500ad12022-04-29 08:43:34 -05002251 if (!current->ptrace || __fatal_signal_pending(current))
Eric W. Biederman57b6de02022-05-04 13:39:58 -05002252 return exit_code;
2253
Peter Zijlstrab5bf9a92018-04-30 14:51:01 +02002254 set_special_state(TASK_TRACED);
Peter Zijlstra31cae1e2022-05-03 15:57:47 -05002255 current->jobctl |= JOBCTL_TRACED;
Peter Zijlstrab5bf9a92018-04-30 14:51:01 +02002256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 /*
Tejun Heo81be24b2011-06-02 11:13:59 +02002258 * We're committing to trapping. TRACED should be visible before
2259 * TRAPPING is cleared; otherwise, the tracer might fail do_wait().
2260 * Also, transition to TRACED and updates to ->jobctl should be
2261 * atomic with respect to siglock and should be done after the arch
2262 * hook as siglock is released and regrabbed across it.
Peter Zijlstrab5bf9a92018-04-30 14:51:01 +02002263 *
2264 * TRACER TRACEE
2265 *
2266 * ptrace_attach()
2267 * [L] wait_on_bit(JOBCTL_TRAPPING) [S] set_special_state(TRACED)
2268 * do_wait()
2269 * set_current_state() smp_wmb();
2270 * ptrace_do_wait()
2271 * wait_task_stopped()
2272 * task_stopped_code()
2273 * [L] task_is_traced() [S] task_clear_jobctl_trapping();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 */
Peter Zijlstrab5bf9a92018-04-30 14:51:01 +02002275 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Eric W. Biederman336d4b82022-01-27 12:15:32 -06002277 current->ptrace_message = message;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 current->last_siginfo = info;
2279 current->exit_code = exit_code;
2280
Tejun Heod79fdd62011-03-23 10:37:00 +01002281 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 * If @why is CLD_STOPPED, we're trapping to participate in a group
2283 * stop. Do the bookkeeping. Note that if SIGCONT was delievered
Tejun Heo73ddff22011-06-14 11:20:14 +02002284 * across siglock relocks since INTERRUPT was scheduled, PENDING
2285 * could be clear now. We act as if SIGCONT is received after
2286 * TASK_TRACED is entered - ignore it.
Tejun Heod79fdd62011-03-23 10:37:00 +01002287 */
Tejun Heoa8f072c2011-06-02 11:13:59 +02002288 if (why == CLD_STOPPED && (current->jobctl & JOBCTL_STOP_PENDING))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 gstop_done = task_participate_group_stop(current);
Tejun Heod79fdd62011-03-23 10:37:00 +01002290
Tejun Heofb1d9102011-06-14 11:20:17 +02002291 /* any trap clears pending STOP trap, STOP trap clears NOTIFY */
Tejun Heo73ddff22011-06-14 11:20:14 +02002292 task_clear_jobctl_pending(current, JOBCTL_TRAP_STOP);
Tejun Heofb1d9102011-06-14 11:20:17 +02002293 if (info && info->si_code >> 8 == PTRACE_EVENT_STOP)
2294 task_clear_jobctl_pending(current, JOBCTL_TRAP_NOTIFY);
Tejun Heo73ddff22011-06-14 11:20:14 +02002295
Tejun Heo81be24b2011-06-02 11:13:59 +02002296 /* entering a trap, clear TRAPPING */
Tejun Heoa8f072c2011-06-02 11:13:59 +02002297 task_clear_jobctl_trapping(current);
Tejun Heod79fdd62011-03-23 10:37:00 +01002298
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 spin_unlock_irq(&current->sighand->siglock);
2300 read_lock(&tasklist_lock);
Eric W. Biederman57b6de02022-05-04 13:39:58 -05002301 /*
2302 * Notify parents of the stop.
2303 *
2304 * While ptraced, there are two parents - the ptracer and
2305 * the real_parent of the group_leader. The ptracer should
2306 * know about every stop while the real parent is only
2307 * interested in the completion of group stop. The states
2308 * for the two don't interact with each other. Notify
2309 * separately unless they're gonna be duplicates.
2310 */
2311 if (current->ptrace)
Tejun Heoceb6bd62011-03-23 10:37:01 +01002312 do_notify_parent_cldstop(current, true, why);
Eric W. Biederman57b6de02022-05-04 13:39:58 -05002313 if (gstop_done && (!current->ptrace || ptrace_reparented(current)))
2314 do_notify_parent_cldstop(current, false, why);
Tejun Heoceb6bd62011-03-23 10:37:01 +01002315
Eric W. Biederman57b6de02022-05-04 13:39:58 -05002316 /*
2317 * Don't want to allow preemption here, because
2318 * sys_ptrace() needs this task to be inactive.
2319 *
2320 * XXX: implement read_unlock_no_resched().
2321 */
2322 preempt_disable();
2323 read_unlock(&tasklist_lock);
2324 cgroup_enter_frozen();
2325 preempt_enable_no_resched();
Peter Zijlstraf5d39b02022-08-22 13:18:22 +02002326 schedule();
Eric W. Biederman57b6de02022-05-04 13:39:58 -05002327 cgroup_leave_frozen(true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
2329 /*
2330 * We are back. Now reacquire the siglock before touching
2331 * last_siginfo, so that we are sure to have synchronized with
2332 * any signal-sending on another CPU that wants to examine it.
2333 */
2334 spin_lock_irq(&current->sighand->siglock);
Eric W. Biederman57b6de02022-05-04 13:39:58 -05002335 exit_code = current->exit_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 current->last_siginfo = NULL;
Eric W. Biederman336d4b82022-01-27 12:15:32 -06002337 current->ptrace_message = 0;
Eric W. Biederman6487d1d2022-01-27 12:19:13 -06002338 current->exit_code = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Tejun Heo544b2c92011-06-14 11:20:18 +02002340 /* LISTENING can be set only during STOP traps, clear it */
Eric W. Biederman2500ad12022-04-29 08:43:34 -05002341 current->jobctl &= ~(JOBCTL_LISTENING | JOBCTL_PTRACE_FROZEN);
Tejun Heo544b2c92011-06-14 11:20:18 +02002342
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 /*
2344 * Queued signals ignored us while we were stopped for tracing.
2345 * So check for any that we should take before resuming user mode.
Roland McGrathb74d0de2007-06-06 03:59:00 -07002346 * This sets TIF_SIGPENDING, but never clears it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 */
Roland McGrathb74d0de2007-06-06 03:59:00 -07002348 recalc_sigpending_tsk(current);
Eric W. Biederman6487d1d2022-01-27 12:19:13 -06002349 return exit_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350}
2351
Eric W. Biederman6487d1d2022-01-27 12:19:13 -06002352static int ptrace_do_notify(int signr, int exit_code, int why, unsigned long message)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02002354 kernel_siginfo_t info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
Eric W. Biedermanfaf1f222018-01-05 17:27:42 -06002356 clear_siginfo(&info);
Tejun Heo3544d722011-06-14 11:20:15 +02002357 info.si_signo = signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 info.si_code = exit_code;
Pavel Emelyanovb4888932007-10-18 23:40:14 -07002359 info.si_pid = task_pid_vnr(current);
Eric W. Biederman078de5f2012-02-08 07:00:08 -08002360 info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
2362 /* Let the debugger run. */
Eric W. Biederman57b6de02022-05-04 13:39:58 -05002363 return ptrace_stop(exit_code, why, message, &info);
Tejun Heo3544d722011-06-14 11:20:15 +02002364}
2365
Eric W. Biederman6487d1d2022-01-27 12:19:13 -06002366int ptrace_notify(int exit_code, unsigned long message)
Tejun Heo3544d722011-06-14 11:20:15 +02002367{
Eric W. Biederman6487d1d2022-01-27 12:19:13 -06002368 int signr;
2369
Tejun Heo3544d722011-06-14 11:20:15 +02002370 BUG_ON((exit_code & (0x7f | ~0xffff)) != SIGTRAP);
Eric W. Biederman7f62d402022-02-09 08:52:41 -06002371 if (unlikely(task_work_pending(current)))
Oleg Nesterovf784e8a2012-08-26 21:12:17 +02002372 task_work_run();
Tejun Heo3544d722011-06-14 11:20:15 +02002373
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 spin_lock_irq(&current->sighand->siglock);
Eric W. Biederman6487d1d2022-01-27 12:19:13 -06002375 signr = ptrace_do_notify(SIGTRAP, exit_code, CLD_TRAPPED, message);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 spin_unlock_irq(&current->sighand->siglock);
Eric W. Biederman6487d1d2022-01-27 12:19:13 -06002377 return signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378}
2379
Tejun Heo73ddff22011-06-14 11:20:14 +02002380/**
2381 * do_signal_stop - handle group stop for SIGSTOP and other stop signals
2382 * @signr: signr causing group stop if initiating
2383 *
2384 * If %JOBCTL_STOP_PENDING is not set yet, initiate group stop with @signr
2385 * and participate in it. If already set, participate in the existing
2386 * group stop. If participated in a group stop (and thus slept), %true is
2387 * returned with siglock released.
2388 *
2389 * If ptraced, this function doesn't handle stop itself. Instead,
2390 * %JOBCTL_TRAP_STOP is scheduled and %false is returned with siglock
2391 * untouched. The caller must ensure that INTERRUPT trap handling takes
2392 * places afterwards.
2393 *
2394 * CONTEXT:
2395 * Must be called with @current->sighand->siglock held, which is released
2396 * on %true return.
2397 *
2398 * RETURNS:
2399 * %false if group stop is already cancelled or ptrace trap is scheduled.
2400 * %true if participated in group stop.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 */
Tejun Heo73ddff22011-06-14 11:20:14 +02002402static bool do_signal_stop(int signr)
2403 __releases(&current->sighand->siglock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404{
2405 struct signal_struct *sig = current->signal;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
Tejun Heoa8f072c2011-06-02 11:13:59 +02002407 if (!(current->jobctl & JOBCTL_STOP_PENDING)) {
Palmer Dabbeltb76808e2015-04-30 21:19:57 -07002408 unsigned long gstop = JOBCTL_STOP_PENDING | JOBCTL_STOP_CONSUME;
Oleg Nesterovf558b7e2008-02-04 22:27:24 -08002409 struct task_struct *t;
2410
Tejun Heoa8f072c2011-06-02 11:13:59 +02002411 /* signr will be recorded in task->jobctl for retries */
2412 WARN_ON_ONCE(signr & ~JOBCTL_STOP_SIGMASK);
Tejun Heod79fdd62011-03-23 10:37:00 +01002413
Tejun Heoa8f072c2011-06-02 11:13:59 +02002414 if (!likely(current->jobctl & JOBCTL_STOP_DEQUEUED) ||
Eric W. Biederman49697332021-06-24 02:14:30 -05002415 unlikely(sig->flags & SIGNAL_GROUP_EXIT) ||
2416 unlikely(sig->group_exec_task))
Tejun Heo73ddff22011-06-14 11:20:14 +02002417 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 /*
Tejun Heo408a37d2011-03-23 10:37:01 +01002419 * There is no group stop already in progress. We must
2420 * initiate one now.
2421 *
2422 * While ptraced, a task may be resumed while group stop is
2423 * still in effect and then receive a stop signal and
2424 * initiate another group stop. This deviates from the
2425 * usual behavior as two consecutive stop signals can't
Oleg Nesterov780006eac2011-04-01 20:12:16 +02002426 * cause two group stops when !ptraced. That is why we
2427 * also check !task_is_stopped(t) below.
Tejun Heo408a37d2011-03-23 10:37:01 +01002428 *
2429 * The condition can be distinguished by testing whether
2430 * SIGNAL_STOP_STOPPED is already set. Don't generate
2431 * group_exit_code in such case.
2432 *
2433 * This is not necessary for SIGNAL_STOP_CONTINUED because
2434 * an intervening stop signal is required to cause two
2435 * continued events regardless of ptrace.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 */
Tejun Heo408a37d2011-03-23 10:37:01 +01002437 if (!(sig->flags & SIGNAL_STOP_STOPPED))
2438 sig->group_exit_code = signr;
Oleg Nesterova122b342006-03-28 16:11:22 -08002439
Tejun Heo7dd3db52011-06-02 11:14:00 +02002440 sig->group_stop_count = 0;
2441
2442 if (task_set_jobctl_pending(current, signr | gstop))
2443 sig->group_stop_count++;
2444
Oleg Nesterov8d38f202014-01-23 15:55:56 -08002445 t = current;
2446 while_each_thread(current, t) {
Oleg Nesterova122b342006-03-28 16:11:22 -08002447 /*
2448 * Setting state to TASK_STOPPED for a group
2449 * stop is always done with the siglock held,
2450 * so this check has no races.
2451 */
Tejun Heo7dd3db52011-06-02 11:14:00 +02002452 if (!task_is_stopped(t) &&
2453 task_set_jobctl_pending(t, signr | gstop)) {
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002454 sig->group_stop_count++;
Tejun Heofb1d9102011-06-14 11:20:17 +02002455 if (likely(!(t->ptrace & PT_SEIZED)))
2456 signal_wake_up(t, 0);
2457 else
2458 ptrace_trap_notify(t);
Oleg Nesterova122b342006-03-28 16:11:22 -08002459 }
Tejun Heod79fdd62011-03-23 10:37:00 +01002460 }
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002461 }
Tejun Heo73ddff22011-06-14 11:20:14 +02002462
Tejun Heod21142e2011-06-17 16:50:34 +02002463 if (likely(!current->ptrace)) {
Tejun Heo5224fa32011-03-23 10:37:00 +01002464 int notify = 0;
2465
2466 /*
2467 * If there are no other threads in the group, or if there
2468 * is a group stop in progress and we are the last to stop,
2469 * report to the parent.
2470 */
2471 if (task_participate_group_stop(current))
2472 notify = CLD_STOPPED;
2473
Peter Zijlstra31cae1e2022-05-03 15:57:47 -05002474 current->jobctl |= JOBCTL_STOPPED;
Peter Zijlstrab5bf9a92018-04-30 14:51:01 +02002475 set_special_state(TASK_STOPPED);
Tejun Heo5224fa32011-03-23 10:37:00 +01002476 spin_unlock_irq(&current->sighand->siglock);
2477
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002478 /*
2479 * Notify the parent of the group stop completion. Because
2480 * we're not holding either the siglock or tasklist_lock
2481 * here, ptracer may attach inbetween; however, this is for
2482 * group stop and should always be delivered to the real
2483 * parent of the group leader. The new ptracer will get
2484 * its notification when this task transitions into
2485 * TASK_TRACED.
2486 */
Tejun Heo5224fa32011-03-23 10:37:00 +01002487 if (notify) {
2488 read_lock(&tasklist_lock);
Tejun Heo62bcf9d2011-03-23 10:37:01 +01002489 do_notify_parent_cldstop(current, false, notify);
Tejun Heo5224fa32011-03-23 10:37:00 +01002490 read_unlock(&tasklist_lock);
2491 }
2492
2493 /* Now we don't run again until woken by SIGCONT or SIGKILL */
Roman Gushchin76f969e2019-04-19 10:03:04 -07002494 cgroup_enter_frozen();
Peter Zijlstraf5d39b02022-08-22 13:18:22 +02002495 schedule();
Tejun Heo73ddff22011-06-14 11:20:14 +02002496 return true;
Tejun Heod79fdd62011-03-23 10:37:00 +01002497 } else {
Tejun Heo73ddff22011-06-14 11:20:14 +02002498 /*
2499 * While ptraced, group stop is handled by STOP trap.
2500 * Schedule it and let the caller deal with it.
2501 */
2502 task_set_jobctl_pending(current, JOBCTL_TRAP_STOP);
2503 return false;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002504 }
Tejun Heo73ddff22011-06-14 11:20:14 +02002505}
Tejun Heod79fdd62011-03-23 10:37:00 +01002506
Tejun Heo73ddff22011-06-14 11:20:14 +02002507/**
2508 * do_jobctl_trap - take care of ptrace jobctl traps
2509 *
Tejun Heo3544d722011-06-14 11:20:15 +02002510 * When PT_SEIZED, it's used for both group stop and explicit
2511 * SEIZE/INTERRUPT traps. Both generate PTRACE_EVENT_STOP trap with
2512 * accompanying siginfo. If stopped, lower eight bits of exit_code contain
2513 * the stop signal; otherwise, %SIGTRAP.
2514 *
2515 * When !PT_SEIZED, it's used only for group stop trap with stop signal
2516 * number as exit_code and no siginfo.
Tejun Heo73ddff22011-06-14 11:20:14 +02002517 *
2518 * CONTEXT:
2519 * Must be called with @current->sighand->siglock held, which may be
2520 * released and re-acquired before returning with intervening sleep.
2521 */
2522static void do_jobctl_trap(void)
2523{
Tejun Heo3544d722011-06-14 11:20:15 +02002524 struct signal_struct *signal = current->signal;
Tejun Heo73ddff22011-06-14 11:20:14 +02002525 int signr = current->jobctl & JOBCTL_STOP_SIGMASK;
Tejun Heod79fdd62011-03-23 10:37:00 +01002526
Tejun Heo3544d722011-06-14 11:20:15 +02002527 if (current->ptrace & PT_SEIZED) {
2528 if (!signal->group_stop_count &&
2529 !(signal->flags & SIGNAL_STOP_STOPPED))
2530 signr = SIGTRAP;
2531 WARN_ON_ONCE(!signr);
2532 ptrace_do_notify(signr, signr | (PTRACE_EVENT_STOP << 8),
Eric W. Biederman336d4b82022-01-27 12:15:32 -06002533 CLD_STOPPED, 0);
Tejun Heo3544d722011-06-14 11:20:15 +02002534 } else {
2535 WARN_ON_ONCE(!signr);
Eric W. Biederman57b6de02022-05-04 13:39:58 -05002536 ptrace_stop(signr, CLD_STOPPED, 0, NULL);
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538}
2539
Roman Gushchin76f969e2019-04-19 10:03:04 -07002540/**
2541 * do_freezer_trap - handle the freezer jobctl trap
2542 *
2543 * Puts the task into frozen state, if only the task is not about to quit.
2544 * In this case it drops JOBCTL_TRAP_FREEZE.
2545 *
2546 * CONTEXT:
2547 * Must be called with @current->sighand->siglock held,
2548 * which is always released before returning.
2549 */
2550static void do_freezer_trap(void)
2551 __releases(&current->sighand->siglock)
2552{
2553 /*
2554 * If there are other trap bits pending except JOBCTL_TRAP_FREEZE,
2555 * let's make another loop to give it a chance to be handled.
2556 * In any case, we'll return back.
2557 */
2558 if ((current->jobctl & (JOBCTL_PENDING_MASK | JOBCTL_TRAP_FREEZE)) !=
2559 JOBCTL_TRAP_FREEZE) {
2560 spin_unlock_irq(&current->sighand->siglock);
2561 return;
2562 }
2563
2564 /*
2565 * Now we're sure that there is no pending fatal signal and no
2566 * pending traps. Clear TIF_SIGPENDING to not get out of schedule()
2567 * immediately (if there is a non-fatal signal pending), and
2568 * put the task into sleep.
2569 */
Peter Zijlstraf5d39b02022-08-22 13:18:22 +02002570 __set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
Roman Gushchin76f969e2019-04-19 10:03:04 -07002571 clear_thread_flag(TIF_SIGPENDING);
2572 spin_unlock_irq(&current->sighand->siglock);
2573 cgroup_enter_frozen();
Peter Zijlstraf5d39b02022-08-22 13:18:22 +02002574 schedule();
Roman Gushchin76f969e2019-04-19 10:03:04 -07002575}
2576
Eric W. Biederman5768d892021-11-15 13:47:13 -06002577static int ptrace_signal(int signr, kernel_siginfo_t *info, enum pid_type type)
Roland McGrath18c98b62008-04-17 18:44:38 -07002578{
Oleg Nesterov8a352412011-07-21 17:06:53 +02002579 /*
2580 * We do not check sig_kernel_stop(signr) but set this marker
2581 * unconditionally because we do not know whether debugger will
2582 * change signr. This flag has no meaning unless we are going
2583 * to stop after return from ptrace_stop(). In this case it will
2584 * be checked in do_signal_stop(), we should only stop if it was
2585 * not cleared by SIGCONT while we were sleeping. See also the
2586 * comment in dequeue_signal().
2587 */
2588 current->jobctl |= JOBCTL_STOP_DEQUEUED;
Eric W. Biederman57b6de02022-05-04 13:39:58 -05002589 signr = ptrace_stop(signr, CLD_TRAPPED, 0, info);
Roland McGrath18c98b62008-04-17 18:44:38 -07002590
2591 /* We're back. Did the debugger cancel the sig? */
Roland McGrath18c98b62008-04-17 18:44:38 -07002592 if (signr == 0)
2593 return signr;
2594
Randy Dunlap5aba0852011-04-04 14:59:31 -07002595 /*
2596 * Update the siginfo structure if the signal has
2597 * changed. If the debugger wanted something
2598 * specific in the siginfo structure then it should
2599 * have updated *info via PTRACE_SETSIGINFO.
2600 */
Roland McGrath18c98b62008-04-17 18:44:38 -07002601 if (signr != info->si_signo) {
Eric W. Biedermanfaf1f222018-01-05 17:27:42 -06002602 clear_siginfo(info);
Roland McGrath18c98b62008-04-17 18:44:38 -07002603 info->si_signo = signr;
2604 info->si_errno = 0;
2605 info->si_code = SI_USER;
Serge E. Hallyn6b550f92012-01-10 15:11:37 -08002606 rcu_read_lock();
Roland McGrath18c98b62008-04-17 18:44:38 -07002607 info->si_pid = task_pid_vnr(current->parent);
Eric W. Biederman54ba47e2012-03-13 16:04:35 -07002608 info->si_uid = from_kuid_munged(current_user_ns(),
2609 task_uid(current->parent));
Serge E. Hallyn6b550f92012-01-10 15:11:37 -08002610 rcu_read_unlock();
Roland McGrath18c98b62008-04-17 18:44:38 -07002611 }
2612
2613 /* If the (new) signal is now blocked, requeue it. */
Eric W. Biedermanb171f662021-11-15 13:49:45 -06002614 if (sigismember(&current->blocked, signr) ||
2615 fatal_signal_pending(current)) {
Eric W. Biederman157cc182022-04-22 09:48:54 -05002616 send_signal_locked(signr, info, current, type);
Roland McGrath18c98b62008-04-17 18:44:38 -07002617 signr = 0;
2618 }
2619
2620 return signr;
2621}
2622
Peter Collingbourne6ac05e82020-11-20 12:33:45 -08002623static void hide_si_addr_tag_bits(struct ksignal *ksig)
2624{
2625 switch (siginfo_layout(ksig->sig, ksig->info.si_code)) {
2626 case SIL_FAULT:
Eric W. Biederman9abcabe2021-04-30 17:29:36 -05002627 case SIL_FAULT_TRAPNO:
Peter Collingbourne6ac05e82020-11-20 12:33:45 -08002628 case SIL_FAULT_MCEERR:
2629 case SIL_FAULT_BNDERR:
2630 case SIL_FAULT_PKUERR:
Eric W. Biedermanf4ac7302021-04-30 17:58:56 -05002631 case SIL_FAULT_PERF_EVENT:
Peter Collingbourne6ac05e82020-11-20 12:33:45 -08002632 ksig->info.si_addr = arch_untagged_si_addr(
2633 ksig->info.si_addr, ksig->sig, ksig->info.si_code);
2634 break;
2635 case SIL_KILL:
2636 case SIL_TIMER:
2637 case SIL_POLL:
2638 case SIL_CHLD:
2639 case SIL_RT:
2640 case SIL_SYS:
2641 break;
2642 }
2643}
2644
Christian Brauner20ab7212018-08-21 22:00:54 -07002645bool get_signal(struct ksignal *ksig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646{
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002647 struct sighand_struct *sighand = current->sighand;
2648 struct signal_struct *signal = current->signal;
2649 int signr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Eric W. Biederman8ba62d32022-02-09 09:51:14 -06002651 clear_notify_signal();
Eric W. Biederman7f62d402022-02-09 08:52:41 -06002652 if (unlikely(task_work_pending(current)))
Jens Axboe35d0b382021-01-05 11:32:43 -07002653 task_work_run();
2654
Eric W. Biederman8ba62d32022-02-09 09:51:14 -06002655 if (!task_sigpending(current))
2656 return false;
Jens Axboe12db8b62020-10-26 14:32:28 -06002657
Srikar Dronamraju0326f5a92012-03-13 23:30:11 +05302658 if (unlikely(uprobe_deny_signal()))
Christian Brauner20ab7212018-08-21 22:00:54 -07002659 return false;
Srikar Dronamraju0326f5a92012-03-13 23:30:11 +05302660
Roland McGrath13b1c3d2008-03-03 20:22:05 -08002661 /*
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +02002662 * Do this once, we can't return to user-mode if freezing() == T.
2663 * do_signal_stop() and ptrace_stop() do freezable_schedule() and
2664 * thus do not need another check after return.
Roland McGrath13b1c3d2008-03-03 20:22:05 -08002665 */
Rafael J. Wysockifc558a72006-03-23 03:00:05 -08002666 try_to_freeze();
2667
Oleg Nesterov5d8f72b2012-10-26 19:46:06 +02002668relock:
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002669 spin_lock_irq(&sighand->siglock);
Oleg Nesterove91b4812020-06-30 17:32:54 +02002670
Oleg Nesterov021e1ae2008-04-30 00:53:00 -07002671 /*
2672 * Every stopped thread goes here after wakeup. Check to see if
2673 * we should notify the parent, prepare_signal(SIGCONT) encodes
2674 * the CLD_ si_code into SIGNAL_CLD_MASK bits.
2675 */
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002676 if (unlikely(signal->flags & SIGNAL_CLD_MASK)) {
Tejun Heoc672af32011-03-23 10:36:59 +01002677 int why;
2678
2679 if (signal->flags & SIGNAL_CLD_CONTINUED)
2680 why = CLD_CONTINUED;
2681 else
2682 why = CLD_STOPPED;
2683
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002684 signal->flags &= ~SIGNAL_CLD_MASK;
Roland McGrathae6d2ed2009-09-23 15:56:53 -07002685
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002686 spin_unlock_irq(&sighand->siglock);
Oleg Nesterove44205512008-04-30 00:52:44 -07002687
Tejun Heoceb6bd62011-03-23 10:37:01 +01002688 /*
2689 * Notify the parent that we're continuing. This event is
2690 * always per-process and doesn't make whole lot of sense
2691 * for ptracers, who shouldn't consume the state via
2692 * wait(2) either, but, for backward compatibility, notify
2693 * the ptracer of the group leader too unless it's gonna be
2694 * a duplicate.
2695 */
Tejun Heoedf2ed12011-03-23 10:37:00 +01002696 read_lock(&tasklist_lock);
Tejun Heoceb6bd62011-03-23 10:37:01 +01002697 do_notify_parent_cldstop(current, false, why);
2698
Oleg Nesterovbb3696d2011-06-24 17:34:23 +02002699 if (ptrace_reparented(current->group_leader))
2700 do_notify_parent_cldstop(current->group_leader,
2701 true, why);
Tejun Heoedf2ed12011-03-23 10:37:00 +01002702 read_unlock(&tasklist_lock);
Tejun Heoceb6bd62011-03-23 10:37:01 +01002703
Oleg Nesterove44205512008-04-30 00:52:44 -07002704 goto relock;
2705 }
2706
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 for (;;) {
2708 struct k_sigaction *ka;
Eric W. Biederman5768d892021-11-15 13:47:13 -06002709 enum pid_type type;
Tejun Heodd1d6772011-06-02 11:14:00 +02002710
Eric W. Biedermane7f7c992021-11-15 11:55:57 -06002711 /* Has this task already been marked for death? */
Eric W. Biederman49697332021-06-24 02:14:30 -05002712 if ((signal->flags & SIGNAL_GROUP_EXIT) ||
2713 signal->group_exec_task) {
haifeng.xu3a017d62022-11-28 06:56:06 +00002714 clear_siginfo(&ksig->info);
Eric W. Biedermane7f7c992021-11-15 11:55:57 -06002715 ksig->info.si_signo = signr = SIGKILL;
2716 sigdelset(&current->pending.signal, SIGKILL);
2717 trace_signal_deliver(SIGKILL, SEND_SIG_NOINFO,
2718 &sighand->action[SIGKILL - 1]);
2719 recalc_sigpending();
2720 goto fatal;
2721 }
Tejun Heodd1d6772011-06-02 11:14:00 +02002722
2723 if (unlikely(current->jobctl & JOBCTL_STOP_PENDING) &&
2724 do_signal_stop(0))
Roland McGrath7bcf6a22008-07-25 19:45:53 -07002725 goto relock;
Oleg Nesterov1be53962009-12-15 16:47:26 -08002726
Roman Gushchin76f969e2019-04-19 10:03:04 -07002727 if (unlikely(current->jobctl &
2728 (JOBCTL_TRAP_MASK | JOBCTL_TRAP_FREEZE))) {
2729 if (current->jobctl & JOBCTL_TRAP_MASK) {
2730 do_jobctl_trap();
2731 spin_unlock_irq(&sighand->siglock);
2732 } else if (current->jobctl & JOBCTL_TRAP_FREEZE)
2733 do_freezer_trap();
2734
2735 goto relock;
2736 }
2737
2738 /*
2739 * If the task is leaving the frozen state, let's update
2740 * cgroup counters and reset the frozen bit.
2741 */
2742 if (unlikely(cgroup_task_frozen(current))) {
Tejun Heo73ddff22011-06-14 11:20:14 +02002743 spin_unlock_irq(&sighand->siglock);
Roman Gushchincb2c4cd2019-04-26 10:59:44 -07002744 cgroup_leave_frozen(false);
Tejun Heo73ddff22011-06-14 11:20:14 +02002745 goto relock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 }
2747
Eric W. Biederman7146db32019-02-06 17:51:47 -06002748 /*
2749 * Signals generated by the execution of an instruction
2750 * need to be delivered before any other pending signals
2751 * so that the instruction pointer in the signal stack
2752 * frame points to the faulting instruction.
2753 */
Eric W. Biederman5768d892021-11-15 13:47:13 -06002754 type = PIDTYPE_PID;
Eric W. Biederman7146db32019-02-06 17:51:47 -06002755 signr = dequeue_synchronous_signal(&ksig->info);
2756 if (!signr)
Eric W. Biederman5768d892021-11-15 13:47:13 -06002757 signr = dequeue_signal(current, &current->blocked,
2758 &ksig->info, &type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Tejun Heodd1d6772011-06-02 11:14:00 +02002760 if (!signr)
2761 break; /* will return 0 */
2762
Eric W. Biederman00b06da2021-10-29 09:14:19 -05002763 if (unlikely(current->ptrace) && (signr != SIGKILL) &&
2764 !(sighand->action[signr -1].sa.sa_flags & SA_IMMUTABLE)) {
Eric W. Biederman5768d892021-11-15 13:47:13 -06002765 signr = ptrace_signal(signr, &ksig->info, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 if (!signr)
Tejun Heodd1d6772011-06-02 11:14:00 +02002767 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 }
2769
Tejun Heodd1d6772011-06-02 11:14:00 +02002770 ka = &sighand->action[signr-1];
2771
Masami Hiramatsuf9d42572009-11-24 16:56:51 -05002772 /* Trace actually delivered signals. */
Richard Weinberger828b1f62013-10-07 15:26:57 +02002773 trace_signal_deliver(signr, &ksig->info, ka);
Masami Hiramatsuf9d42572009-11-24 16:56:51 -05002774
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 if (ka->sa.sa_handler == SIG_IGN) /* Do nothing. */
2776 continue;
2777 if (ka->sa.sa_handler != SIG_DFL) {
2778 /* Run the handler. */
Richard Weinberger828b1f62013-10-07 15:26:57 +02002779 ksig->ka = *ka;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
2781 if (ka->sa.sa_flags & SA_ONESHOT)
2782 ka->sa.sa_handler = SIG_DFL;
2783
2784 break; /* will return non-zero "signr" value */
2785 }
2786
2787 /*
2788 * Now we are doing the default action for this signal.
2789 */
2790 if (sig_kernel_ignore(signr)) /* Default is nothing. */
2791 continue;
2792
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08002793 /*
Sukadev Bhattiprolu0fbc26a2007-10-18 23:40:13 -07002794 * Global init gets no signals it doesn't want.
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07002795 * Container-init gets no signals it doesn't want from same
2796 * container.
2797 *
2798 * Note that if global/container-init sees a sig_kernel_only()
2799 * signal here, the signal must have been generated internally
2800 * or must have come from an ancestor namespace. In either
2801 * case, the signal cannot be dropped.
Sukadev Bhattiprolu84d73782006-12-08 02:38:01 -08002802 */
Oleg Nesterovfae5fa42008-04-30 00:53:03 -07002803 if (unlikely(signal->flags & SIGNAL_UNKILLABLE) &&
Sukadev Bhattiprolub3bfa0c2009-04-02 16:58:08 -07002804 !sig_kernel_only(signr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 continue;
2806
2807 if (sig_kernel_stop(signr)) {
2808 /*
2809 * The default action is to stop all threads in
2810 * the thread group. The job control signals
2811 * do nothing in an orphaned pgrp, but SIGSTOP
2812 * always works. Note that siglock needs to be
2813 * dropped during the call to is_orphaned_pgrp()
2814 * because of lock ordering with tasklist_lock.
2815 * This allows an intervening SIGCONT to be posted.
2816 * We need to check for that and bail out if necessary.
2817 */
2818 if (signr != SIGSTOP) {
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002819 spin_unlock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
2821 /* signals can be posted during this window */
2822
Eric W. Biederman3e7cd6c2007-02-12 00:52:58 -08002823 if (is_current_pgrp_orphaned())
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 goto relock;
2825
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002826 spin_lock_irq(&sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 }
2828
Richard Weinberger828b1f62013-10-07 15:26:57 +02002829 if (likely(do_signal_stop(ksig->info.si_signo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 /* It released the siglock. */
2831 goto relock;
2832 }
2833
2834 /*
2835 * We didn't actually stop, due to a race
2836 * with SIGCONT or something like that.
2837 */
2838 continue;
2839 }
2840
Eric W. Biederman35634ff2019-02-06 18:39:40 -06002841 fatal:
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002842 spin_unlock_irq(&sighand->siglock);
Roman Gushchinf2b31bb2019-05-08 13:34:20 -07002843 if (unlikely(cgroup_task_frozen(current)))
2844 cgroup_leave_frozen(true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
2846 /*
2847 * Anything else is fatal, maybe with a core dump.
2848 */
2849 current->flags |= PF_SIGNALED;
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07002850
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 if (sig_kernel_coredump(signr)) {
Oleg Nesterov2dce81b2008-04-30 00:52:58 -07002852 if (print_fatal_signals)
Richard Weinberger828b1f62013-10-07 15:26:57 +02002853 print_fatal_signal(ksig->info.si_signo);
Jesper Derehag2b5faa42013-03-19 20:50:05 +00002854 proc_coredump_connector(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 /*
2856 * If it was able to dump core, this kills all
2857 * other threads in the group and synchronizes with
2858 * their demise. If we lost the race with another
2859 * thread getting here, it set group_exit_code
2860 * first and our do_group_exit call below will use
2861 * that value and ignore the one we pass it.
2862 */
Richard Weinberger828b1f62013-10-07 15:26:57 +02002863 do_coredump(&ksig->info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 }
2865
2866 /*
Mike Christief9010db2023-06-01 13:32:32 -05002867 * PF_USER_WORKER threads will catch and exit on fatal signals
Jens Axboe10442992021-03-26 08:57:10 -06002868 * themselves. They have cleanup that must be performed, so
2869 * we cannot call do_exit() on their behalf.
2870 */
Mike Christief9010db2023-06-01 13:32:32 -05002871 if (current->flags & PF_USER_WORKER)
Jens Axboe10442992021-03-26 08:57:10 -06002872 goto out;
2873
2874 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 * Death signals, no core dump.
2876 */
Richard Weinberger828b1f62013-10-07 15:26:57 +02002877 do_group_exit(ksig->info.si_signo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 /* NOTREACHED */
2879 }
Oleg Nesterovf6b76d42008-04-30 00:52:47 -07002880 spin_unlock_irq(&sighand->siglock);
Jens Axboe10442992021-03-26 08:57:10 -06002881out:
Richard Weinberger828b1f62013-10-07 15:26:57 +02002882 ksig->sig = signr;
Peter Collingbourne6ac05e82020-11-20 12:33:45 -08002883
2884 if (!(ksig->ka.sa.sa_flags & SA_EXPOSE_TAGBITS))
2885 hide_si_addr_tag_bits(ksig);
2886
Richard Weinberger828b1f62013-10-07 15:26:57 +02002887 return ksig->sig > 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888}
2889
Matt Fleming5e6292c2012-01-10 15:11:17 -08002890/**
Randy Dunlap64103492021-12-21 19:10:27 -08002891 * signal_delivered - called after signal delivery to update blocked signals
Richard Weinberger10b1c7a2014-07-13 13:36:04 +02002892 * @ksig: kernel signal struct
Al Viroefee9842012-04-28 02:04:15 -04002893 * @stepping: nonzero if debugger single-step or block-step in use
Matt Fleming5e6292c2012-01-10 15:11:17 -08002894 *
Masanari Iidae2278672014-02-18 22:54:36 +09002895 * This function should be called when a signal has successfully been
Richard Weinberger10b1c7a2014-07-13 13:36:04 +02002896 * delivered. It updates the blocked signals accordingly (@ksig->ka.sa.sa_mask
Randy Dunlap64103492021-12-21 19:10:27 -08002897 * is always blocked), and the signal itself is blocked unless %SA_NODEFER
Richard Weinberger10b1c7a2014-07-13 13:36:04 +02002898 * is set in @ksig->ka.sa.sa_flags. Tracing is notified.
Matt Fleming5e6292c2012-01-10 15:11:17 -08002899 */
Richard Weinberger10b1c7a2014-07-13 13:36:04 +02002900static void signal_delivered(struct ksignal *ksig, int stepping)
Matt Fleming5e6292c2012-01-10 15:11:17 -08002901{
2902 sigset_t blocked;
2903
Al Viroa610d6e2012-05-21 23:42:15 -04002904 /* A signal was successfully delivered, and the
2905 saved sigmask was stored on the signal frame,
2906 and will be restored by sigreturn. So we can
2907 simply clear the restore sigmask flag. */
2908 clear_restore_sigmask();
2909
Richard Weinberger10b1c7a2014-07-13 13:36:04 +02002910 sigorsets(&blocked, &current->blocked, &ksig->ka.sa.sa_mask);
2911 if (!(ksig->ka.sa.sa_flags & SA_NODEFER))
2912 sigaddset(&blocked, ksig->sig);
Matt Fleming5e6292c2012-01-10 15:11:17 -08002913 set_current_blocked(&blocked);
Al Viro97c885d2021-06-30 18:56:43 -07002914 if (current->sas_ss_flags & SS_AUTODISARM)
2915 sas_ss_reset(current);
Eric W. Biedermanc1451372022-01-27 12:04:27 -06002916 if (stepping)
Eric W. Biederman336d4b82022-01-27 12:15:32 -06002917 ptrace_notify(SIGTRAP, 0);
Matt Fleming5e6292c2012-01-10 15:11:17 -08002918}
2919
Al Viro2ce5da12012-11-07 15:11:25 -05002920void signal_setup_done(int failed, struct ksignal *ksig, int stepping)
2921{
2922 if (failed)
Eric W. Biedermancb44c9a2019-05-21 10:03:48 -05002923 force_sigsegv(ksig->sig);
Al Viro2ce5da12012-11-07 15:11:25 -05002924 else
Richard Weinberger10b1c7a2014-07-13 13:36:04 +02002925 signal_delivered(ksig, stepping);
Al Viro2ce5da12012-11-07 15:11:25 -05002926}
2927
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002928/*
2929 * It could be that complete_signal() picked us to notify about the
Oleg Nesterovfec99932011-04-27 19:50:21 +02002930 * group-wide signal. Other threads should be notified now to take
2931 * the shared signals in @which since we will not.
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002932 */
Oleg Nesterovf646e222011-04-27 19:18:39 +02002933static void retarget_shared_pending(struct task_struct *tsk, sigset_t *which)
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002934{
Oleg Nesterovf646e222011-04-27 19:18:39 +02002935 sigset_t retarget;
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002936 struct task_struct *t;
2937
Oleg Nesterovf646e222011-04-27 19:18:39 +02002938 sigandsets(&retarget, &tsk->signal->shared_pending.signal, which);
2939 if (sigisemptyset(&retarget))
2940 return;
2941
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002942 t = tsk;
2943 while_each_thread(tsk, t) {
Oleg Nesterovfec99932011-04-27 19:50:21 +02002944 if (t->flags & PF_EXITING)
2945 continue;
2946
2947 if (!has_pending_signals(&retarget, &t->blocked))
2948 continue;
2949 /* Remove the signals this thread can handle. */
2950 sigandsets(&retarget, &retarget, &t->blocked);
2951
Jens Axboe5c251e92020-10-26 14:32:27 -06002952 if (!task_sigpending(t))
Oleg Nesterovfec99932011-04-27 19:50:21 +02002953 signal_wake_up(t, 0);
2954
2955 if (sigisemptyset(&retarget))
2956 break;
Oleg Nesterov0edceb7bc2011-04-27 19:17:37 +02002957 }
2958}
2959
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002960void exit_signals(struct task_struct *tsk)
2961{
2962 int group_stop = 0;
Oleg Nesterovf646e222011-04-27 19:18:39 +02002963 sigset_t unblocked;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002964
Tejun Heo77e4ef92011-12-12 18:12:21 -08002965 /*
2966 * @tsk is about to have PF_EXITING set - lock out users which
2967 * expect stable threadgroup.
2968 */
Ingo Molnar780de9d2017-02-02 11:50:56 +01002969 cgroup_threadgroup_change_begin(tsk);
Tejun Heo77e4ef92011-12-12 18:12:21 -08002970
Eric W. Biederman49697332021-06-24 02:14:30 -05002971 if (thread_group_empty(tsk) || (tsk->signal->flags & SIGNAL_GROUP_EXIT)) {
Mathieu Desnoyersaf7f5882022-11-22 15:39:09 -05002972 sched_mm_cid_exit_signals(tsk);
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002973 tsk->flags |= PF_EXITING;
Ingo Molnar780de9d2017-02-02 11:50:56 +01002974 cgroup_threadgroup_change_end(tsk);
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002975 return;
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002976 }
2977
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002978 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002979 /*
2980 * From now this task is not visible for group-wide signals,
2981 * see wants_signal(), do_signal_stop().
2982 */
Mathieu Desnoyersaf7f5882022-11-22 15:39:09 -05002983 sched_mm_cid_exit_signals(tsk);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002984 tsk->flags |= PF_EXITING;
Tejun Heo77e4ef92011-12-12 18:12:21 -08002985
Ingo Molnar780de9d2017-02-02 11:50:56 +01002986 cgroup_threadgroup_change_end(tsk);
Tejun Heo77e4ef92011-12-12 18:12:21 -08002987
Jens Axboe5c251e92020-10-26 14:32:27 -06002988 if (!task_sigpending(tsk))
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002989 goto out;
2990
Oleg Nesterovf646e222011-04-27 19:18:39 +02002991 unblocked = tsk->blocked;
2992 signotset(&unblocked);
2993 retarget_shared_pending(tsk, &unblocked);
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002994
Tejun Heoa8f072c2011-06-02 11:13:59 +02002995 if (unlikely(tsk->jobctl & JOBCTL_STOP_PENDING) &&
Tejun Heoe5c1902e2011-03-23 10:37:00 +01002996 task_participate_group_stop(tsk))
Tejun Heoedf2ed12011-03-23 10:37:00 +01002997 group_stop = CLD_STOPPED;
Oleg Nesterov5dee1702008-02-08 04:19:13 -08002998out:
Oleg Nesterovd12619b2008-02-08 04:19:12 -08002999 spin_unlock_irq(&tsk->sighand->siglock);
3000
Tejun Heo62bcf9d2011-03-23 10:37:01 +01003001 /*
3002 * If group stop has completed, deliver the notification. This
3003 * should always go to the real parent of the group leader.
3004 */
Roland McGrathae6d2ed2009-09-23 15:56:53 -07003005 if (unlikely(group_stop)) {
Oleg Nesterovd12619b2008-02-08 04:19:12 -08003006 read_lock(&tasklist_lock);
Tejun Heo62bcf9d2011-03-23 10:37:01 +01003007 do_notify_parent_cldstop(tsk, false, group_stop);
Oleg Nesterovd12619b2008-02-08 04:19:12 -08003008 read_unlock(&tasklist_lock);
3009 }
3010}
3011
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012/*
3013 * System call entry points.
3014 */
3015
Randy Dunlap41c57892011-04-04 15:00:26 -07003016/**
3017 * sys_restart_syscall - restart a system call
3018 */
Heiko Carstens754fe8d2009-01-14 14:14:09 +01003019SYSCALL_DEFINE0(restart_syscall)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020{
Andy Lutomirskif56141e2015-02-12 15:01:14 -08003021 struct restart_block *restart = &current->restart_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 return restart->fn(restart);
3023}
3024
3025long do_no_restart_syscall(struct restart_block *param)
3026{
3027 return -EINTR;
3028}
3029
Oleg Nesterovb1828012011-04-27 21:56:14 +02003030static void __set_task_blocked(struct task_struct *tsk, const sigset_t *newset)
3031{
Jens Axboe5c251e92020-10-26 14:32:27 -06003032 if (task_sigpending(tsk) && !thread_group_empty(tsk)) {
Oleg Nesterovb1828012011-04-27 21:56:14 +02003033 sigset_t newblocked;
3034 /* A set of now blocked but previously unblocked signals. */
Oleg Nesterov702a5072011-04-27 22:01:27 +02003035 sigandnsets(&newblocked, newset, &current->blocked);
Oleg Nesterovb1828012011-04-27 21:56:14 +02003036 retarget_shared_pending(tsk, &newblocked);
3037 }
3038 tsk->blocked = *newset;
3039 recalc_sigpending();
3040}
3041
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02003042/**
3043 * set_current_blocked - change current->blocked mask
3044 * @newset: new mask
3045 *
3046 * It is wrong to change ->blocked directly, this helper should be used
3047 * to ensure the process can't miss a shared signal we are going to block.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 */
Al Viro77097ae2012-04-27 13:58:59 -04003049void set_current_blocked(sigset_t *newset)
3050{
Al Viro77097ae2012-04-27 13:58:59 -04003051 sigdelsetmask(newset, sigmask(SIGKILL) | sigmask(SIGSTOP));
Oleg Nesterov0c4a8422013-01-05 19:13:29 +01003052 __set_current_blocked(newset);
Al Viro77097ae2012-04-27 13:58:59 -04003053}
3054
3055void __set_current_blocked(const sigset_t *newset)
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02003056{
3057 struct task_struct *tsk = current;
3058
Waiman Longc7be96a2016-12-14 15:04:10 -08003059 /*
3060 * In case the signal mask hasn't changed, there is nothing we need
3061 * to do. The current->blocked shouldn't be modified by other task.
3062 */
3063 if (sigequalsets(&tsk->blocked, newset))
3064 return;
3065
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02003066 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovb1828012011-04-27 21:56:14 +02003067 __set_task_blocked(tsk, newset);
Oleg Nesterove6fa16a2011-04-27 20:59:41 +02003068 spin_unlock_irq(&tsk->sighand->siglock);
3069}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070
3071/*
3072 * This is also useful for kernel threads that want to temporarily
3073 * (or permanently) block certain signals.
3074 *
3075 * NOTE! Unlike the user-mode sys_sigprocmask(), the kernel
3076 * interface happily blocks "unblockable" signals like SIGKILL
3077 * and friends.
3078 */
3079int sigprocmask(int how, sigset_t *set, sigset_t *oldset)
3080{
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02003081 struct task_struct *tsk = current;
3082 sigset_t newset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02003084 /* Lockless, only current can change ->blocked, never from irq */
Oleg Nesterova26fd332006-03-23 03:00:49 -08003085 if (oldset)
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02003086 *oldset = tsk->blocked;
Oleg Nesterova26fd332006-03-23 03:00:49 -08003087
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 switch (how) {
3089 case SIG_BLOCK:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02003090 sigorsets(&newset, &tsk->blocked, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 break;
3092 case SIG_UNBLOCK:
Oleg Nesterov702a5072011-04-27 22:01:27 +02003093 sigandnsets(&newset, &tsk->blocked, set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094 break;
3095 case SIG_SETMASK:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02003096 newset = *set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 break;
3098 default:
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02003099 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 }
Oleg Nesterova26fd332006-03-23 03:00:49 -08003101
Al Viro77097ae2012-04-27 13:58:59 -04003102 __set_current_blocked(&newset);
Oleg Nesterov73ef4ae2011-04-27 19:54:20 +02003103 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104}
Eric W. Biedermanfb50f5a2018-09-13 19:26:35 +02003105EXPORT_SYMBOL(sigprocmask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
Deepa Dinamanided653c2018-09-19 21:41:04 -07003107/*
3108 * The api helps set app-provided sigmasks.
3109 *
3110 * This is useful for syscalls such as ppoll, pselect, io_pgetevents and
3111 * epoll_pwait where a new sigmask is passed from userland for the syscalls.
Oleg Nesterovb7724342019-07-16 16:29:53 -07003112 *
3113 * Note that it does set_restore_sigmask() in advance, so it must be always
3114 * paired with restore_saved_sigmask_unless() before return from syscall.
Deepa Dinamanided653c2018-09-19 21:41:04 -07003115 */
Oleg Nesterovb7724342019-07-16 16:29:53 -07003116int set_user_sigmask(const sigset_t __user *umask, size_t sigsetsize)
Deepa Dinamanided653c2018-09-19 21:41:04 -07003117{
Oleg Nesterovb7724342019-07-16 16:29:53 -07003118 sigset_t kmask;
Deepa Dinamanided653c2018-09-19 21:41:04 -07003119
Oleg Nesterovb7724342019-07-16 16:29:53 -07003120 if (!umask)
3121 return 0;
Deepa Dinamanided653c2018-09-19 21:41:04 -07003122 if (sigsetsize != sizeof(sigset_t))
3123 return -EINVAL;
Oleg Nesterovb7724342019-07-16 16:29:53 -07003124 if (copy_from_user(&kmask, umask, sizeof(sigset_t)))
Deepa Dinamanided653c2018-09-19 21:41:04 -07003125 return -EFAULT;
3126
Oleg Nesterovb7724342019-07-16 16:29:53 -07003127 set_restore_sigmask();
3128 current->saved_sigmask = current->blocked;
3129 set_current_blocked(&kmask);
Deepa Dinamanided653c2018-09-19 21:41:04 -07003130
3131 return 0;
3132}
Deepa Dinamanided653c2018-09-19 21:41:04 -07003133
3134#ifdef CONFIG_COMPAT
Oleg Nesterovb7724342019-07-16 16:29:53 -07003135int set_compat_user_sigmask(const compat_sigset_t __user *umask,
Deepa Dinamanided653c2018-09-19 21:41:04 -07003136 size_t sigsetsize)
3137{
Oleg Nesterovb7724342019-07-16 16:29:53 -07003138 sigset_t kmask;
Deepa Dinamanided653c2018-09-19 21:41:04 -07003139
Oleg Nesterovb7724342019-07-16 16:29:53 -07003140 if (!umask)
3141 return 0;
Deepa Dinamanided653c2018-09-19 21:41:04 -07003142 if (sigsetsize != sizeof(compat_sigset_t))
3143 return -EINVAL;
Oleg Nesterovb7724342019-07-16 16:29:53 -07003144 if (get_compat_sigset(&kmask, umask))
Deepa Dinamanided653c2018-09-19 21:41:04 -07003145 return -EFAULT;
3146
Oleg Nesterovb7724342019-07-16 16:29:53 -07003147 set_restore_sigmask();
3148 current->saved_sigmask = current->blocked;
3149 set_current_blocked(&kmask);
Deepa Dinamanided653c2018-09-19 21:41:04 -07003150
3151 return 0;
3152}
Deepa Dinamanided653c2018-09-19 21:41:04 -07003153#endif
3154
Randy Dunlap41c57892011-04-04 15:00:26 -07003155/**
3156 * sys_rt_sigprocmask - change the list of currently blocked signals
3157 * @how: whether to add, remove, or set signals
Randy Dunlapada9c932011-06-14 15:50:11 -07003158 * @nset: stores pending signals
Randy Dunlap41c57892011-04-04 15:00:26 -07003159 * @oset: previous value of signal mask if non-null
3160 * @sigsetsize: size of sigset_t type
3161 */
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02003162SYSCALL_DEFINE4(rt_sigprocmask, int, how, sigset_t __user *, nset,
Heiko Carstens17da2bd2009-01-14 14:14:10 +01003163 sigset_t __user *, oset, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 sigset_t old_set, new_set;
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02003166 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
3168 /* XXX: Don't preclude handling different sized sigset_t's. */
3169 if (sigsetsize != sizeof(sigset_t))
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02003170 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02003172 old_set = current->blocked;
3173
3174 if (nset) {
3175 if (copy_from_user(&new_set, nset, sizeof(sigset_t)))
3176 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
3178
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02003179 error = sigprocmask(how, &new_set, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 if (error)
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02003181 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 }
Oleg Nesterovbb7efee2011-04-27 21:18:10 +02003183
3184 if (oset) {
3185 if (copy_to_user(oset, &old_set, sizeof(sigset_t)))
3186 return -EFAULT;
3187 }
3188
3189 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190}
3191
Al Viro322a56c2012-12-25 13:32:58 -05003192#ifdef CONFIG_COMPAT
Al Viro322a56c2012-12-25 13:32:58 -05003193COMPAT_SYSCALL_DEFINE4(rt_sigprocmask, int, how, compat_sigset_t __user *, nset,
3194 compat_sigset_t __user *, oset, compat_size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195{
Al Viro322a56c2012-12-25 13:32:58 -05003196 sigset_t old_set = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197
Al Viro322a56c2012-12-25 13:32:58 -05003198 /* XXX: Don't preclude handling different sized sigset_t's. */
3199 if (sigsetsize != sizeof(sigset_t))
3200 return -EINVAL;
3201
3202 if (nset) {
Al Viro322a56c2012-12-25 13:32:58 -05003203 sigset_t new_set;
3204 int error;
Al Viro3968cf62017-09-03 21:45:17 -04003205 if (get_compat_sigset(&new_set, nset))
Al Viro322a56c2012-12-25 13:32:58 -05003206 return -EFAULT;
Al Viro322a56c2012-12-25 13:32:58 -05003207 sigdelsetmask(&new_set, sigmask(SIGKILL)|sigmask(SIGSTOP));
3208
3209 error = sigprocmask(how, &new_set, NULL);
3210 if (error)
3211 return error;
3212 }
Dmitry V. Levinf4543222017-08-22 02:16:11 +03003213 return oset ? put_compat_sigset(oset, &old_set, sizeof(*oset)) : 0;
Al Viro322a56c2012-12-25 13:32:58 -05003214}
3215#endif
Al Viro322a56c2012-12-25 13:32:58 -05003216
Christian Braunerb1d294c2018-08-21 22:00:02 -07003217static void do_sigpending(sigset_t *set)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 spin_lock_irq(&current->sighand->siglock);
Al Virofe9c1db2012-12-25 14:31:38 -05003220 sigorsets(set, &current->pending.signal,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 &current->signal->shared_pending.signal);
3222 spin_unlock_irq(&current->sighand->siglock);
3223
3224 /* Outside the lock because only this thread touches it. */
Al Virofe9c1db2012-12-25 14:31:38 -05003225 sigandsets(set, &current->blocked, set);
Randy Dunlap5aba0852011-04-04 14:59:31 -07003226}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227
Randy Dunlap41c57892011-04-04 15:00:26 -07003228/**
3229 * sys_rt_sigpending - examine a pending signal that has been raised
3230 * while blocked
Randy Dunlap20f22ab2013-03-04 14:32:59 -08003231 * @uset: stores pending signals
Randy Dunlap41c57892011-04-04 15:00:26 -07003232 * @sigsetsize: size of sigset_t type or larger
3233 */
Al Virofe9c1db2012-12-25 14:31:38 -05003234SYSCALL_DEFINE2(rt_sigpending, sigset_t __user *, uset, size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235{
Al Virofe9c1db2012-12-25 14:31:38 -05003236 sigset_t set;
Dmitry V. Levin176826a2017-08-22 02:16:43 +03003237
3238 if (sigsetsize > sizeof(*uset))
3239 return -EINVAL;
3240
Christian Braunerb1d294c2018-08-21 22:00:02 -07003241 do_sigpending(&set);
3242
3243 if (copy_to_user(uset, &set, sigsetsize))
3244 return -EFAULT;
3245
3246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247}
3248
Al Virofe9c1db2012-12-25 14:31:38 -05003249#ifdef CONFIG_COMPAT
Al Virofe9c1db2012-12-25 14:31:38 -05003250COMPAT_SYSCALL_DEFINE2(rt_sigpending, compat_sigset_t __user *, uset,
3251 compat_size_t, sigsetsize)
3252{
Al Virofe9c1db2012-12-25 14:31:38 -05003253 sigset_t set;
Dmitry V. Levin176826a2017-08-22 02:16:43 +03003254
3255 if (sigsetsize > sizeof(*uset))
3256 return -EINVAL;
3257
Christian Braunerb1d294c2018-08-21 22:00:02 -07003258 do_sigpending(&set);
3259
3260 return put_compat_sigset(uset, &set, sigsetsize);
Al Virofe9c1db2012-12-25 14:31:38 -05003261}
3262#endif
Al Virofe9c1db2012-12-25 14:31:38 -05003263
Eric W. Biederman4ce5f9c2018-09-25 12:59:31 +02003264static const struct {
3265 unsigned char limit, layout;
3266} sig_sicodes[] = {
3267 [SIGILL] = { NSIGILL, SIL_FAULT },
3268 [SIGFPE] = { NSIGFPE, SIL_FAULT },
3269 [SIGSEGV] = { NSIGSEGV, SIL_FAULT },
3270 [SIGBUS] = { NSIGBUS, SIL_FAULT },
3271 [SIGTRAP] = { NSIGTRAP, SIL_FAULT },
3272#if defined(SIGEMT)
3273 [SIGEMT] = { NSIGEMT, SIL_FAULT },
3274#endif
3275 [SIGCHLD] = { NSIGCHLD, SIL_CHLD },
3276 [SIGPOLL] = { NSIGPOLL, SIL_POLL },
3277 [SIGSYS] = { NSIGSYS, SIL_SYS },
3278};
3279
Eric W. Biedermanb2a2ab52018-10-10 20:11:25 -05003280static bool known_siginfo_layout(unsigned sig, int si_code)
Eric W. Biederman4ce5f9c2018-09-25 12:59:31 +02003281{
3282 if (si_code == SI_KERNEL)
3283 return true;
3284 else if ((si_code > SI_USER)) {
3285 if (sig_specific_sicodes(sig)) {
3286 if (si_code <= sig_sicodes[sig].limit)
3287 return true;
3288 }
3289 else if (si_code <= NSIGPOLL)
3290 return true;
3291 }
3292 else if (si_code >= SI_DETHREAD)
3293 return true;
3294 else if (si_code == SI_ASYNCNL)
3295 return true;
3296 return false;
3297}
3298
Eric W. Biedermana3670052018-10-10 20:29:44 -05003299enum siginfo_layout siginfo_layout(unsigned sig, int si_code)
Eric W. Biedermancc731522017-07-16 22:36:59 -05003300{
3301 enum siginfo_layout layout = SIL_KILL;
3302 if ((si_code > SI_USER) && (si_code < SI_KERNEL)) {
Eric W. Biederman4ce5f9c2018-09-25 12:59:31 +02003303 if ((sig < ARRAY_SIZE(sig_sicodes)) &&
3304 (si_code <= sig_sicodes[sig].limit)) {
3305 layout = sig_sicodes[sig].layout;
Eric W. Biederman31931c92018-04-24 20:59:47 -05003306 /* Handle the exceptions */
3307 if ((sig == SIGBUS) &&
3308 (si_code >= BUS_MCEERR_AR) && (si_code <= BUS_MCEERR_AO))
3309 layout = SIL_FAULT_MCEERR;
3310 else if ((sig == SIGSEGV) && (si_code == SEGV_BNDERR))
3311 layout = SIL_FAULT_BNDERR;
3312#ifdef SEGV_PKUERR
3313 else if ((sig == SIGSEGV) && (si_code == SEGV_PKUERR))
3314 layout = SIL_FAULT_PKUERR;
3315#endif
Marco Elvered8e5082021-04-22 21:18:23 +02003316 else if ((sig == SIGTRAP) && (si_code == TRAP_PERF))
Eric W. Biedermanf4ac7302021-04-30 17:58:56 -05003317 layout = SIL_FAULT_PERF_EVENT;
Eric W. Biederman2c9f7ea2021-05-28 13:38:19 -05003318 else if (IS_ENABLED(CONFIG_SPARC) &&
3319 (sig == SIGILL) && (si_code == ILL_ILLTRP))
Eric W. Biederman9abcabe2021-04-30 17:29:36 -05003320 layout = SIL_FAULT_TRAPNO;
Eric W. Biederman7de5f682021-05-28 14:15:51 -05003321 else if (IS_ENABLED(CONFIG_ALPHA) &&
3322 ((sig == SIGFPE) ||
3323 ((sig == SIGTRAP) && (si_code == TRAP_UNK))))
3324 layout = SIL_FAULT_TRAPNO;
Eric W. Biederman31931c92018-04-24 20:59:47 -05003325 }
Eric W. Biedermancc731522017-07-16 22:36:59 -05003326 else if (si_code <= NSIGPOLL)
3327 layout = SIL_POLL;
3328 } else {
3329 if (si_code == SI_TIMER)
3330 layout = SIL_TIMER;
3331 else if (si_code == SI_SIGIO)
3332 layout = SIL_POLL;
3333 else if (si_code < 0)
3334 layout = SIL_RT;
Eric W. Biedermancc731522017-07-16 22:36:59 -05003335 }
3336 return layout;
3337}
3338
Eric W. Biederman4ce5f9c2018-09-25 12:59:31 +02003339static inline char __user *si_expansion(const siginfo_t __user *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340{
Eric W. Biederman4ce5f9c2018-09-25 12:59:31 +02003341 return ((char __user *)info) + sizeof(struct kernel_siginfo);
3342}
3343
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003344int copy_siginfo_to_user(siginfo_t __user *to, const kernel_siginfo_t *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003345{
Eric W. Biederman4ce5f9c2018-09-25 12:59:31 +02003346 char __user *expansion = si_expansion(to);
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003347 if (copy_to_user(to, from , sizeof(struct kernel_siginfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 return -EFAULT;
Eric W. Biederman4ce5f9c2018-09-25 12:59:31 +02003349 if (clear_user(expansion, SI_EXPANSION_SIZE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 return -EFAULT;
Eric W. Biedermanc999b932018-04-14 13:03:25 -05003351 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352}
3353
Eric W. Biederman601d5ab2018-10-05 09:02:48 +02003354static int post_copy_siginfo_from_user(kernel_siginfo_t *info,
3355 const siginfo_t __user *from)
Eric W. Biederman4cd2e0e2018-04-18 17:30:19 -05003356{
Eric W. Biederman601d5ab2018-10-05 09:02:48 +02003357 if (unlikely(!known_siginfo_layout(info->si_signo, info->si_code))) {
Eric W. Biederman4ce5f9c2018-09-25 12:59:31 +02003358 char __user *expansion = si_expansion(from);
3359 char buf[SI_EXPANSION_SIZE];
3360 int i;
3361 /*
3362 * An unknown si_code might need more than
3363 * sizeof(struct kernel_siginfo) bytes. Verify all of the
3364 * extra bytes are 0. This guarantees copy_siginfo_to_user
3365 * will return this data to userspace exactly.
3366 */
3367 if (copy_from_user(&buf, expansion, SI_EXPANSION_SIZE))
3368 return -EFAULT;
3369 for (i = 0; i < SI_EXPANSION_SIZE; i++) {
3370 if (buf[i] != 0)
3371 return -E2BIG;
3372 }
3373 }
Eric W. Biederman4cd2e0e2018-04-18 17:30:19 -05003374 return 0;
3375}
3376
Eric W. Biederman601d5ab2018-10-05 09:02:48 +02003377static int __copy_siginfo_from_user(int signo, kernel_siginfo_t *to,
3378 const siginfo_t __user *from)
3379{
3380 if (copy_from_user(to, from, sizeof(struct kernel_siginfo)))
3381 return -EFAULT;
3382 to->si_signo = signo;
3383 return post_copy_siginfo_from_user(to, from);
3384}
3385
3386int copy_siginfo_from_user(kernel_siginfo_t *to, const siginfo_t __user *from)
3387{
3388 if (copy_from_user(to, from, sizeof(struct kernel_siginfo)))
3389 return -EFAULT;
3390 return post_copy_siginfo_from_user(to, from);
3391}
3392
Eric W. Biederman212a36a2017-07-31 17:15:31 -05003393#ifdef CONFIG_COMPAT
Christoph Hellwigc3b3f522020-05-05 12:12:53 +02003394/**
3395 * copy_siginfo_to_external32 - copy a kernel siginfo into a compat user siginfo
3396 * @to: compat siginfo destination
3397 * @from: kernel siginfo source
3398 *
3399 * Note: This function does not work properly for the SIGCHLD on x32, but
3400 * fortunately it doesn't have to. The only valid callers for this function are
3401 * copy_siginfo_to_user32, which is overriden for x32 and the coredump code.
3402 * The latter does not care because SIGCHLD will never cause a coredump.
3403 */
3404void copy_siginfo_to_external32(struct compat_siginfo *to,
3405 const struct kernel_siginfo *from)
Eric W. Biedermanea64d5a2018-01-15 18:03:33 -06003406{
Christoph Hellwigc3b3f522020-05-05 12:12:53 +02003407 memset(to, 0, sizeof(*to));
Eric W. Biedermanea64d5a2018-01-15 18:03:33 -06003408
Christoph Hellwigc3b3f522020-05-05 12:12:53 +02003409 to->si_signo = from->si_signo;
3410 to->si_errno = from->si_errno;
3411 to->si_code = from->si_code;
Eric W. Biedermanea64d5a2018-01-15 18:03:33 -06003412 switch(siginfo_layout(from->si_signo, from->si_code)) {
3413 case SIL_KILL:
Christoph Hellwigc3b3f522020-05-05 12:12:53 +02003414 to->si_pid = from->si_pid;
3415 to->si_uid = from->si_uid;
Eric W. Biedermanea64d5a2018-01-15 18:03:33 -06003416 break;
3417 case SIL_TIMER:
Christoph Hellwigc3b3f522020-05-05 12:12:53 +02003418 to->si_tid = from->si_tid;
3419 to->si_overrun = from->si_overrun;
3420 to->si_int = from->si_int;
Eric W. Biedermanea64d5a2018-01-15 18:03:33 -06003421 break;
3422 case SIL_POLL:
Christoph Hellwigc3b3f522020-05-05 12:12:53 +02003423 to->si_band = from->si_band;
3424 to->si_fd = from->si_fd;
Eric W. Biedermanea64d5a2018-01-15 18:03:33 -06003425 break;
3426 case SIL_FAULT:
Christoph Hellwigc3b3f522020-05-05 12:12:53 +02003427 to->si_addr = ptr_to_compat(from->si_addr);
Eric W. Biederman9abcabe2021-04-30 17:29:36 -05003428 break;
3429 case SIL_FAULT_TRAPNO:
3430 to->si_addr = ptr_to_compat(from->si_addr);
Christoph Hellwigc3b3f522020-05-05 12:12:53 +02003431 to->si_trapno = from->si_trapno;
Eric W. Biederman31931c92018-04-24 20:59:47 -05003432 break;
3433 case SIL_FAULT_MCEERR:
Christoph Hellwigc3b3f522020-05-05 12:12:53 +02003434 to->si_addr = ptr_to_compat(from->si_addr);
Christoph Hellwigc3b3f522020-05-05 12:12:53 +02003435 to->si_addr_lsb = from->si_addr_lsb;
Eric W. Biederman31931c92018-04-24 20:59:47 -05003436 break;
3437 case SIL_FAULT_BNDERR:
Christoph Hellwigc3b3f522020-05-05 12:12:53 +02003438 to->si_addr = ptr_to_compat(from->si_addr);
Christoph Hellwigc3b3f522020-05-05 12:12:53 +02003439 to->si_lower = ptr_to_compat(from->si_lower);
3440 to->si_upper = ptr_to_compat(from->si_upper);
Eric W. Biederman31931c92018-04-24 20:59:47 -05003441 break;
3442 case SIL_FAULT_PKUERR:
Christoph Hellwigc3b3f522020-05-05 12:12:53 +02003443 to->si_addr = ptr_to_compat(from->si_addr);
Christoph Hellwigc3b3f522020-05-05 12:12:53 +02003444 to->si_pkey = from->si_pkey;
Eric W. Biedermanea64d5a2018-01-15 18:03:33 -06003445 break;
Eric W. Biedermanf4ac7302021-04-30 17:58:56 -05003446 case SIL_FAULT_PERF_EVENT:
Marco Elverfb6cc122021-04-08 12:36:00 +02003447 to->si_addr = ptr_to_compat(from->si_addr);
Eric W. Biederman0683b532021-05-02 17:28:31 -05003448 to->si_perf_data = from->si_perf_data;
3449 to->si_perf_type = from->si_perf_type;
Marco Elver78ed93d2022-04-04 13:12:04 +02003450 to->si_perf_flags = from->si_perf_flags;
Marco Elverfb6cc122021-04-08 12:36:00 +02003451 break;
Eric W. Biedermanea64d5a2018-01-15 18:03:33 -06003452 case SIL_CHLD:
Christoph Hellwigc3b3f522020-05-05 12:12:53 +02003453 to->si_pid = from->si_pid;
3454 to->si_uid = from->si_uid;
3455 to->si_status = from->si_status;
3456 to->si_utime = from->si_utime;
3457 to->si_stime = from->si_stime;
Eric W. Biedermanea64d5a2018-01-15 18:03:33 -06003458 break;
3459 case SIL_RT:
Christoph Hellwigc3b3f522020-05-05 12:12:53 +02003460 to->si_pid = from->si_pid;
3461 to->si_uid = from->si_uid;
3462 to->si_int = from->si_int;
Eric W. Biedermanea64d5a2018-01-15 18:03:33 -06003463 break;
3464 case SIL_SYS:
Christoph Hellwigc3b3f522020-05-05 12:12:53 +02003465 to->si_call_addr = ptr_to_compat(from->si_call_addr);
3466 to->si_syscall = from->si_syscall;
3467 to->si_arch = from->si_arch;
Eric W. Biedermanea64d5a2018-01-15 18:03:33 -06003468 break;
3469 }
Christoph Hellwigc3b3f522020-05-05 12:12:53 +02003470}
Eric W. Biedermanea64d5a2018-01-15 18:03:33 -06003471
Christoph Hellwigc3b3f522020-05-05 12:12:53 +02003472int __copy_siginfo_to_user32(struct compat_siginfo __user *to,
3473 const struct kernel_siginfo *from)
3474{
3475 struct compat_siginfo new;
3476
3477 copy_siginfo_to_external32(&new, from);
Eric W. Biedermanea64d5a2018-01-15 18:03:33 -06003478 if (copy_to_user(to, &new, sizeof(struct compat_siginfo)))
3479 return -EFAULT;
Eric W. Biedermanea64d5a2018-01-15 18:03:33 -06003480 return 0;
3481}
3482
Eric W. Biederman601d5ab2018-10-05 09:02:48 +02003483static int post_copy_siginfo_from_user32(kernel_siginfo_t *to,
3484 const struct compat_siginfo *from)
3485{
3486 clear_siginfo(to);
3487 to->si_signo = from->si_signo;
3488 to->si_errno = from->si_errno;
3489 to->si_code = from->si_code;
3490 switch(siginfo_layout(from->si_signo, from->si_code)) {
3491 case SIL_KILL:
3492 to->si_pid = from->si_pid;
3493 to->si_uid = from->si_uid;
3494 break;
3495 case SIL_TIMER:
3496 to->si_tid = from->si_tid;
3497 to->si_overrun = from->si_overrun;
3498 to->si_int = from->si_int;
3499 break;
3500 case SIL_POLL:
3501 to->si_band = from->si_band;
3502 to->si_fd = from->si_fd;
3503 break;
3504 case SIL_FAULT:
3505 to->si_addr = compat_ptr(from->si_addr);
Eric W. Biederman9abcabe2021-04-30 17:29:36 -05003506 break;
3507 case SIL_FAULT_TRAPNO:
3508 to->si_addr = compat_ptr(from->si_addr);
Eric W. Biederman601d5ab2018-10-05 09:02:48 +02003509 to->si_trapno = from->si_trapno;
Eric W. Biederman601d5ab2018-10-05 09:02:48 +02003510 break;
3511 case SIL_FAULT_MCEERR:
3512 to->si_addr = compat_ptr(from->si_addr);
Eric W. Biederman601d5ab2018-10-05 09:02:48 +02003513 to->si_addr_lsb = from->si_addr_lsb;
3514 break;
3515 case SIL_FAULT_BNDERR:
3516 to->si_addr = compat_ptr(from->si_addr);
Eric W. Biederman601d5ab2018-10-05 09:02:48 +02003517 to->si_lower = compat_ptr(from->si_lower);
3518 to->si_upper = compat_ptr(from->si_upper);
3519 break;
3520 case SIL_FAULT_PKUERR:
3521 to->si_addr = compat_ptr(from->si_addr);
Eric W. Biederman601d5ab2018-10-05 09:02:48 +02003522 to->si_pkey = from->si_pkey;
3523 break;
Eric W. Biedermanf4ac7302021-04-30 17:58:56 -05003524 case SIL_FAULT_PERF_EVENT:
Marco Elverfb6cc122021-04-08 12:36:00 +02003525 to->si_addr = compat_ptr(from->si_addr);
Eric W. Biederman0683b532021-05-02 17:28:31 -05003526 to->si_perf_data = from->si_perf_data;
3527 to->si_perf_type = from->si_perf_type;
Marco Elver78ed93d2022-04-04 13:12:04 +02003528 to->si_perf_flags = from->si_perf_flags;
Marco Elverfb6cc122021-04-08 12:36:00 +02003529 break;
Eric W. Biederman601d5ab2018-10-05 09:02:48 +02003530 case SIL_CHLD:
3531 to->si_pid = from->si_pid;
3532 to->si_uid = from->si_uid;
3533 to->si_status = from->si_status;
3534#ifdef CONFIG_X86_X32_ABI
3535 if (in_x32_syscall()) {
3536 to->si_utime = from->_sifields._sigchld_x32._utime;
3537 to->si_stime = from->_sifields._sigchld_x32._stime;
3538 } else
3539#endif
3540 {
3541 to->si_utime = from->si_utime;
3542 to->si_stime = from->si_stime;
3543 }
3544 break;
3545 case SIL_RT:
3546 to->si_pid = from->si_pid;
3547 to->si_uid = from->si_uid;
3548 to->si_int = from->si_int;
3549 break;
3550 case SIL_SYS:
3551 to->si_call_addr = compat_ptr(from->si_call_addr);
3552 to->si_syscall = from->si_syscall;
3553 to->si_arch = from->si_arch;
3554 break;
3555 }
3556 return 0;
3557}
3558
3559static int __copy_siginfo_from_user32(int signo, struct kernel_siginfo *to,
3560 const struct compat_siginfo __user *ufrom)
3561{
3562 struct compat_siginfo from;
3563
3564 if (copy_from_user(&from, ufrom, sizeof(struct compat_siginfo)))
3565 return -EFAULT;
3566
3567 from.si_signo = signo;
3568 return post_copy_siginfo_from_user32(to, &from);
3569}
3570
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003571int copy_siginfo_from_user32(struct kernel_siginfo *to,
Eric W. Biederman212a36a2017-07-31 17:15:31 -05003572 const struct compat_siginfo __user *ufrom)
3573{
3574 struct compat_siginfo from;
3575
3576 if (copy_from_user(&from, ufrom, sizeof(struct compat_siginfo)))
3577 return -EFAULT;
3578
Eric W. Biederman601d5ab2018-10-05 09:02:48 +02003579 return post_copy_siginfo_from_user32(to, &from);
Eric W. Biederman212a36a2017-07-31 17:15:31 -05003580}
3581#endif /* CONFIG_COMPAT */
3582
Randy Dunlap41c57892011-04-04 15:00:26 -07003583/**
Oleg Nesterov943df142011-04-27 21:44:14 +02003584 * do_sigtimedwait - wait for queued signals specified in @which
3585 * @which: queued signals to wait for
3586 * @info: if non-null, the signal's siginfo is returned here
3587 * @ts: upper bound on process time suspension
3588 */
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003589static int do_sigtimedwait(const sigset_t *which, kernel_siginfo_t *info,
Arnd Bergmann49c39f82018-04-18 15:56:13 +02003590 const struct timespec64 *ts)
Oleg Nesterov943df142011-04-27 21:44:14 +02003591{
Thomas Gleixner2456e852016-12-25 11:38:40 +01003592 ktime_t *to = NULL, timeout = KTIME_MAX;
Oleg Nesterov943df142011-04-27 21:44:14 +02003593 struct task_struct *tsk = current;
Oleg Nesterov943df142011-04-27 21:44:14 +02003594 sigset_t mask = *which;
Eric W. Biederman5768d892021-11-15 13:47:13 -06003595 enum pid_type type;
Thomas Gleixner2b1ecc32016-07-04 09:50:25 +00003596 int sig, ret = 0;
Oleg Nesterov943df142011-04-27 21:44:14 +02003597
3598 if (ts) {
Arnd Bergmann49c39f82018-04-18 15:56:13 +02003599 if (!timespec64_valid(ts))
Oleg Nesterov943df142011-04-27 21:44:14 +02003600 return -EINVAL;
Arnd Bergmann49c39f82018-04-18 15:56:13 +02003601 timeout = timespec64_to_ktime(*ts);
Thomas Gleixner2b1ecc32016-07-04 09:50:25 +00003602 to = &timeout;
Oleg Nesterov943df142011-04-27 21:44:14 +02003603 }
3604
3605 /*
3606 * Invert the set of allowed signals to get those we want to block.
3607 */
3608 sigdelsetmask(&mask, sigmask(SIGKILL) | sigmask(SIGSTOP));
3609 signotset(&mask);
3610
3611 spin_lock_irq(&tsk->sighand->siglock);
Eric W. Biederman5768d892021-11-15 13:47:13 -06003612 sig = dequeue_signal(tsk, &mask, info, &type);
Thomas Gleixner2456e852016-12-25 11:38:40 +01003613 if (!sig && timeout) {
Oleg Nesterov943df142011-04-27 21:44:14 +02003614 /*
3615 * None ready, temporarily unblock those we're interested
3616 * while we are sleeping in so that we'll be awakened when
Oleg Nesterovb1828012011-04-27 21:56:14 +02003617 * they arrive. Unblocking is always fine, we can avoid
3618 * set_current_blocked().
Oleg Nesterov943df142011-04-27 21:44:14 +02003619 */
3620 tsk->real_blocked = tsk->blocked;
3621 sigandsets(&tsk->blocked, &tsk->blocked, &mask);
3622 recalc_sigpending();
3623 spin_unlock_irq(&tsk->sighand->siglock);
3624
Peter Zijlstraf5d39b02022-08-22 13:18:22 +02003625 __set_current_state(TASK_INTERRUPTIBLE|TASK_FREEZABLE);
3626 ret = schedule_hrtimeout_range(to, tsk->timer_slack_ns,
3627 HRTIMER_MODE_REL);
Oleg Nesterov943df142011-04-27 21:44:14 +02003628 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterovb1828012011-04-27 21:56:14 +02003629 __set_task_blocked(tsk, &tsk->real_blocked);
Oleg Nesterov6114041a2014-06-06 14:36:46 -07003630 sigemptyset(&tsk->real_blocked);
Eric W. Biederman5768d892021-11-15 13:47:13 -06003631 sig = dequeue_signal(tsk, &mask, info, &type);
Oleg Nesterov943df142011-04-27 21:44:14 +02003632 }
3633 spin_unlock_irq(&tsk->sighand->siglock);
3634
3635 if (sig)
3636 return sig;
Thomas Gleixner2b1ecc32016-07-04 09:50:25 +00003637 return ret ? -EINTR : -EAGAIN;
Oleg Nesterov943df142011-04-27 21:44:14 +02003638}
3639
3640/**
Randy Dunlap41c57892011-04-04 15:00:26 -07003641 * sys_rt_sigtimedwait - synchronously wait for queued signals specified
3642 * in @uthese
3643 * @uthese: queued signals to wait for
3644 * @uinfo: if non-null, the signal's siginfo is returned here
3645 * @uts: upper bound on process time suspension
3646 * @sigsetsize: size of sigset_t type
3647 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01003648SYSCALL_DEFINE4(rt_sigtimedwait, const sigset_t __user *, uthese,
Arnd Bergmann49c39f82018-04-18 15:56:13 +02003649 siginfo_t __user *, uinfo,
3650 const struct __kernel_timespec __user *, uts,
Heiko Carstens17da2bd2009-01-14 14:14:10 +01003651 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 sigset_t these;
Arnd Bergmann49c39f82018-04-18 15:56:13 +02003654 struct timespec64 ts;
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003655 kernel_siginfo_t info;
Oleg Nesterov943df142011-04-27 21:44:14 +02003656 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657
3658 /* XXX: Don't preclude handling different sized sigset_t's. */
3659 if (sigsetsize != sizeof(sigset_t))
3660 return -EINVAL;
3661
3662 if (copy_from_user(&these, uthese, sizeof(these)))
3663 return -EFAULT;
Randy Dunlap5aba0852011-04-04 14:59:31 -07003664
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 if (uts) {
Arnd Bergmann49c39f82018-04-18 15:56:13 +02003666 if (get_timespec64(&ts, uts))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668 }
3669
Oleg Nesterov943df142011-04-27 21:44:14 +02003670 ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671
Oleg Nesterov943df142011-04-27 21:44:14 +02003672 if (ret > 0 && uinfo) {
3673 if (copy_siginfo_to_user(uinfo, &info))
3674 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 }
3676
3677 return ret;
3678}
3679
Arnd Bergmanndf8522a2018-04-18 16:15:37 +02003680#ifdef CONFIG_COMPAT_32BIT_TIME
3681SYSCALL_DEFINE4(rt_sigtimedwait_time32, const sigset_t __user *, uthese,
3682 siginfo_t __user *, uinfo,
3683 const struct old_timespec32 __user *, uts,
3684 size_t, sigsetsize)
3685{
3686 sigset_t these;
3687 struct timespec64 ts;
3688 kernel_siginfo_t info;
3689 int ret;
3690
3691 if (sigsetsize != sizeof(sigset_t))
3692 return -EINVAL;
3693
3694 if (copy_from_user(&these, uthese, sizeof(these)))
3695 return -EFAULT;
3696
3697 if (uts) {
3698 if (get_old_timespec32(&ts, uts))
3699 return -EFAULT;
3700 }
3701
3702 ret = do_sigtimedwait(&these, &info, uts ? &ts : NULL);
3703
3704 if (ret > 0 && uinfo) {
3705 if (copy_siginfo_to_user(uinfo, &info))
3706 ret = -EFAULT;
3707 }
3708
3709 return ret;
3710}
3711#endif
3712
Al Viro1b3c8722017-05-31 04:46:17 -04003713#ifdef CONFIG_COMPAT
Arnd Bergmann2367c4b2018-04-18 16:18:35 +02003714COMPAT_SYSCALL_DEFINE4(rt_sigtimedwait_time64, compat_sigset_t __user *, uthese,
3715 struct compat_siginfo __user *, uinfo,
3716 struct __kernel_timespec __user *, uts, compat_size_t, sigsetsize)
3717{
3718 sigset_t s;
3719 struct timespec64 t;
3720 kernel_siginfo_t info;
3721 long ret;
3722
3723 if (sigsetsize != sizeof(sigset_t))
3724 return -EINVAL;
3725
3726 if (get_compat_sigset(&s, uthese))
3727 return -EFAULT;
3728
3729 if (uts) {
3730 if (get_timespec64(&t, uts))
3731 return -EFAULT;
3732 }
3733
3734 ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);
3735
3736 if (ret > 0 && uinfo) {
3737 if (copy_siginfo_to_user32(uinfo, &info))
3738 ret = -EFAULT;
3739 }
3740
3741 return ret;
3742}
3743
3744#ifdef CONFIG_COMPAT_32BIT_TIME
Arnd Bergmann8dabe722019-01-07 00:33:08 +01003745COMPAT_SYSCALL_DEFINE4(rt_sigtimedwait_time32, compat_sigset_t __user *, uthese,
Al Viro1b3c8722017-05-31 04:46:17 -04003746 struct compat_siginfo __user *, uinfo,
Arnd Bergmann9afc5ee2018-07-13 12:52:28 +02003747 struct old_timespec32 __user *, uts, compat_size_t, sigsetsize)
Al Viro1b3c8722017-05-31 04:46:17 -04003748{
Al Viro1b3c8722017-05-31 04:46:17 -04003749 sigset_t s;
Arnd Bergmann49c39f82018-04-18 15:56:13 +02003750 struct timespec64 t;
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003751 kernel_siginfo_t info;
Al Viro1b3c8722017-05-31 04:46:17 -04003752 long ret;
3753
3754 if (sigsetsize != sizeof(sigset_t))
3755 return -EINVAL;
3756
Al Viro3968cf62017-09-03 21:45:17 -04003757 if (get_compat_sigset(&s, uthese))
Al Viro1b3c8722017-05-31 04:46:17 -04003758 return -EFAULT;
Al Viro1b3c8722017-05-31 04:46:17 -04003759
3760 if (uts) {
Arnd Bergmann49c39f82018-04-18 15:56:13 +02003761 if (get_old_timespec32(&t, uts))
Al Viro1b3c8722017-05-31 04:46:17 -04003762 return -EFAULT;
3763 }
3764
3765 ret = do_sigtimedwait(&s, &info, uts ? &t : NULL);
3766
3767 if (ret > 0 && uinfo) {
3768 if (copy_siginfo_to_user32(uinfo, &info))
3769 ret = -EFAULT;
3770 }
3771
3772 return ret;
3773}
3774#endif
Arnd Bergmann2367c4b2018-04-18 16:18:35 +02003775#endif
Al Viro1b3c8722017-05-31 04:46:17 -04003776
Christian Brauner3eb39f42018-11-19 00:51:56 +01003777static inline void prepare_kill_siginfo(int sig, struct kernel_siginfo *info)
3778{
3779 clear_siginfo(info);
3780 info->si_signo = sig;
3781 info->si_errno = 0;
3782 info->si_code = SI_USER;
3783 info->si_pid = task_tgid_vnr(current);
3784 info->si_uid = from_kuid_munged(current_user_ns(), current_uid());
3785}
3786
Randy Dunlap41c57892011-04-04 15:00:26 -07003787/**
3788 * sys_kill - send a signal to a process
3789 * @pid: the PID of the process
3790 * @sig: signal to be sent
3791 */
Heiko Carstens17da2bd2009-01-14 14:14:10 +01003792SYSCALL_DEFINE2(kill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003794 struct kernel_siginfo info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795
Christian Brauner3eb39f42018-11-19 00:51:56 +01003796 prepare_kill_siginfo(sig, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
3798 return kill_something_info(sig, &info, pid);
3799}
3800
Christian Brauner3eb39f42018-11-19 00:51:56 +01003801/*
3802 * Verify that the signaler and signalee either are in the same pid namespace
3803 * or that the signaler's pid namespace is an ancestor of the signalee's pid
3804 * namespace.
3805 */
3806static bool access_pidfd_pidns(struct pid *pid)
3807{
3808 struct pid_namespace *active = task_active_pid_ns(current);
3809 struct pid_namespace *p = ns_of_pid(pid);
3810
3811 for (;;) {
3812 if (!p)
3813 return false;
3814 if (p == active)
3815 break;
3816 p = p->parent;
3817 }
3818
3819 return true;
3820}
3821
Jann Hornadc5d872020-12-07 01:02:52 +01003822static int copy_siginfo_from_user_any(kernel_siginfo_t *kinfo,
3823 siginfo_t __user *info)
Christian Brauner3eb39f42018-11-19 00:51:56 +01003824{
3825#ifdef CONFIG_COMPAT
3826 /*
3827 * Avoid hooking up compat syscalls and instead handle necessary
3828 * conversions here. Note, this is a stop-gap measure and should not be
3829 * considered a generic solution.
3830 */
3831 if (in_compat_syscall())
3832 return copy_siginfo_from_user32(
3833 kinfo, (struct compat_siginfo __user *)info);
3834#endif
3835 return copy_siginfo_from_user(kinfo, info);
3836}
3837
Christian Brauner2151ad12019-04-17 22:50:25 +02003838static struct pid *pidfd_to_pid(const struct file *file)
3839{
Christian Brauner3695eae2019-07-28 00:22:29 +02003840 struct pid *pid;
3841
3842 pid = pidfd_pid(file);
3843 if (!IS_ERR(pid))
3844 return pid;
Christian Brauner2151ad12019-04-17 22:50:25 +02003845
3846 return tgid_pidfd_to_pid(file);
3847}
3848
Christian Brauner3eb39f42018-11-19 00:51:56 +01003849/**
Christian Braunerc7323272019-06-04 15:18:43 +02003850 * sys_pidfd_send_signal - Signal a process through a pidfd
3851 * @pidfd: file descriptor of the process
3852 * @sig: signal to send
3853 * @info: signal info
3854 * @flags: future flags
Christian Brauner3eb39f42018-11-19 00:51:56 +01003855 *
3856 * The syscall currently only signals via PIDTYPE_PID which covers
3857 * kill(<positive-pid>, <signal>. It does not signal threads or process
3858 * groups.
3859 * In order to extend the syscall to threads and process groups the @flags
3860 * argument should be used. In essence, the @flags argument will determine
3861 * what is signaled and not the file descriptor itself. Put in other words,
3862 * grouping is a property of the flags argument not a property of the file
3863 * descriptor.
3864 *
3865 * Return: 0 on success, negative errno on failure
3866 */
3867SYSCALL_DEFINE4(pidfd_send_signal, int, pidfd, int, sig,
3868 siginfo_t __user *, info, unsigned int, flags)
3869{
3870 int ret;
3871 struct fd f;
3872 struct pid *pid;
3873 kernel_siginfo_t kinfo;
3874
3875 /* Enforce flags be set to 0 until we add an extension. */
3876 if (flags)
3877 return -EINVAL;
3878
Christian Brauner738a7832019-04-18 12:18:39 +02003879 f = fdget(pidfd);
Christian Brauner3eb39f42018-11-19 00:51:56 +01003880 if (!f.file)
3881 return -EBADF;
3882
3883 /* Is this a pidfd? */
Christian Brauner2151ad12019-04-17 22:50:25 +02003884 pid = pidfd_to_pid(f.file);
Christian Brauner3eb39f42018-11-19 00:51:56 +01003885 if (IS_ERR(pid)) {
3886 ret = PTR_ERR(pid);
3887 goto err;
3888 }
3889
3890 ret = -EINVAL;
3891 if (!access_pidfd_pidns(pid))
3892 goto err;
3893
3894 if (info) {
3895 ret = copy_siginfo_from_user_any(&kinfo, info);
3896 if (unlikely(ret))
3897 goto err;
3898
3899 ret = -EINVAL;
3900 if (unlikely(sig != kinfo.si_signo))
3901 goto err;
3902
Jann Horn556a8882019-03-30 03:12:32 +01003903 /* Only allow sending arbitrary signals to yourself. */
3904 ret = -EPERM;
Christian Brauner3eb39f42018-11-19 00:51:56 +01003905 if ((task_pid(current) != pid) &&
Jann Horn556a8882019-03-30 03:12:32 +01003906 (kinfo.si_code >= 0 || kinfo.si_code == SI_TKILL))
3907 goto err;
Christian Brauner3eb39f42018-11-19 00:51:56 +01003908 } else {
3909 prepare_kill_siginfo(sig, &kinfo);
3910 }
3911
3912 ret = kill_pid_info(sig, &kinfo, pid);
3913
3914err:
3915 fdput(f);
3916 return ret;
3917}
Christian Brauner3eb39f42018-11-19 00:51:56 +01003918
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00003919static int
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003920do_send_specific(pid_t tgid, pid_t pid, int sig, struct kernel_siginfo *info)
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08003921{
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08003922 struct task_struct *p;
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00003923 int error = -ESRCH;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08003924
Oleg Nesterov3547ff32008-04-30 00:52:51 -07003925 rcu_read_lock();
Pavel Emelyanov228ebcb2007-10-18 23:40:16 -07003926 p = find_task_by_vpid(pid);
Pavel Emelyanovb4888932007-10-18 23:40:14 -07003927 if (p && (tgid <= 0 || task_tgid_vnr(p) == tgid)) {
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00003928 error = check_kill_permission(sig, info, p);
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08003929 /*
3930 * The null signal is a permissions and process existence
3931 * probe. No signal is actually delivered.
3932 */
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07003933 if (!error && sig) {
Eric W. Biederman40b3b022018-07-21 10:45:15 -05003934 error = do_send_sig_info(sig, info, p, PIDTYPE_PID);
Oleg Nesterov4a30deb2009-09-23 15:57:00 -07003935 /*
3936 * If lock_task_sighand() failed we pretend the task
3937 * dies after receiving the signal. The window is tiny,
3938 * and the signal is private anyway.
3939 */
3940 if (unlikely(error == -ESRCH))
3941 error = 0;
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08003942 }
3943 }
Oleg Nesterov3547ff32008-04-30 00:52:51 -07003944 rcu_read_unlock();
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08003945
3946 return error;
3947}
3948
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00003949static int do_tkill(pid_t tgid, pid_t pid, int sig)
3950{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003951 struct kernel_siginfo info;
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00003952
Eric W. Biederman5f749722018-01-22 14:58:57 -06003953 clear_siginfo(&info);
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00003954 info.si_signo = sig;
3955 info.si_errno = 0;
3956 info.si_code = SI_TKILL;
3957 info.si_pid = task_tgid_vnr(current);
Eric W. Biederman078de5f2012-02-08 07:00:08 -08003958 info.si_uid = from_kuid_munged(current_user_ns(), current_uid());
Thomas Gleixner30b4ae82009-04-04 21:01:01 +00003959
3960 return do_send_specific(tgid, pid, sig, &info);
3961}
3962
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963/**
3964 * sys_tgkill - send signal to one specific thread
3965 * @tgid: the thread group ID of the thread
3966 * @pid: the PID of the thread
3967 * @sig: signal to be sent
3968 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08003969 * This syscall also checks the @tgid and returns -ESRCH even if the PID
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 * exists but it's not belonging to the target process anymore. This
3971 * method solves the problem of threads exiting and PIDs getting reused.
3972 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003973SYSCALL_DEFINE3(tgkill, pid_t, tgid, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 /* This is only valid for single tasks */
3976 if (pid <= 0 || tgid <= 0)
3977 return -EINVAL;
3978
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08003979 return do_tkill(tgid, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980}
3981
Randy Dunlap41c57892011-04-04 15:00:26 -07003982/**
3983 * sys_tkill - send signal to one specific task
3984 * @pid: the PID of the task
3985 * @sig: signal to be sent
3986 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 * Send a signal to only one task, even if it's a CLONE_THREAD task.
3988 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01003989SYSCALL_DEFINE2(tkill, pid_t, pid, int, sig)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 /* This is only valid for single tasks */
3992 if (pid <= 0)
3993 return -EINVAL;
3994
Vadim Lobanov6dd69f12005-10-30 15:02:18 -08003995 return do_tkill(0, pid, sig);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996}
3997
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02003998static int do_rt_sigqueueinfo(pid_t pid, int sig, kernel_siginfo_t *info)
Al Viro75907d42012-12-25 15:19:12 -05003999{
4000 /* Not even root can pretend to send signals from the kernel.
4001 * Nor can they impersonate a kill()/tgkill(), which adds source info.
4002 */
Andrey Vagin66dd34a2013-02-27 17:03:12 -08004003 if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
Vladimir Davydov69828dc2015-04-16 12:47:35 -07004004 (task_pid_vnr(current) != pid))
Al Viro75907d42012-12-25 15:19:12 -05004005 return -EPERM;
Vladimir Davydov69828dc2015-04-16 12:47:35 -07004006
Al Viro75907d42012-12-25 15:19:12 -05004007 /* POSIX.1b doesn't mention process groups. */
4008 return kill_proc_info(sig, info, pid);
4009}
4010
Randy Dunlap41c57892011-04-04 15:00:26 -07004011/**
4012 * sys_rt_sigqueueinfo - send signal information to a signal
4013 * @pid: the PID of the thread
4014 * @sig: signal to be sent
4015 * @uinfo: signal info to be sent
4016 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01004017SYSCALL_DEFINE3(rt_sigqueueinfo, pid_t, pid, int, sig,
4018 siginfo_t __user *, uinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004020 kernel_siginfo_t info;
Eric W. Biederman601d5ab2018-10-05 09:02:48 +02004021 int ret = __copy_siginfo_from_user(sig, &info, uinfo);
Eric W. Biederman4cd2e0e2018-04-18 17:30:19 -05004022 if (unlikely(ret))
4023 return ret;
Al Viro75907d42012-12-25 15:19:12 -05004024 return do_rt_sigqueueinfo(pid, sig, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025}
4026
Al Viro75907d42012-12-25 15:19:12 -05004027#ifdef CONFIG_COMPAT
Al Viro75907d42012-12-25 15:19:12 -05004028COMPAT_SYSCALL_DEFINE3(rt_sigqueueinfo,
4029 compat_pid_t, pid,
4030 int, sig,
4031 struct compat_siginfo __user *, uinfo)
4032{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004033 kernel_siginfo_t info;
Eric W. Biederman601d5ab2018-10-05 09:02:48 +02004034 int ret = __copy_siginfo_from_user32(sig, &info, uinfo);
Al Viro75907d42012-12-25 15:19:12 -05004035 if (unlikely(ret))
4036 return ret;
4037 return do_rt_sigqueueinfo(pid, sig, &info);
4038}
4039#endif
Al Viro75907d42012-12-25 15:19:12 -05004040
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004041static int do_rt_tgsigqueueinfo(pid_t tgid, pid_t pid, int sig, kernel_siginfo_t *info)
Thomas Gleixner62ab4502009-04-04 21:01:06 +00004042{
4043 /* This is only valid for single tasks */
4044 if (pid <= 0 || tgid <= 0)
4045 return -EINVAL;
4046
4047 /* Not even root can pretend to send signals from the kernel.
Julien Tinnesda485242011-03-18 15:05:21 -07004048 * Nor can they impersonate a kill()/tgkill(), which adds source info.
4049 */
Vladimir Davydov69828dc2015-04-16 12:47:35 -07004050 if ((info->si_code >= 0 || info->si_code == SI_TKILL) &&
4051 (task_pid_vnr(current) != pid))
Thomas Gleixner62ab4502009-04-04 21:01:06 +00004052 return -EPERM;
Vladimir Davydov69828dc2015-04-16 12:47:35 -07004053
Thomas Gleixner62ab4502009-04-04 21:01:06 +00004054 return do_send_specific(tgid, pid, sig, info);
4055}
4056
4057SYSCALL_DEFINE4(rt_tgsigqueueinfo, pid_t, tgid, pid_t, pid, int, sig,
4058 siginfo_t __user *, uinfo)
4059{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004060 kernel_siginfo_t info;
Eric W. Biederman601d5ab2018-10-05 09:02:48 +02004061 int ret = __copy_siginfo_from_user(sig, &info, uinfo);
Eric W. Biederman4cd2e0e2018-04-18 17:30:19 -05004062 if (unlikely(ret))
4063 return ret;
Thomas Gleixner62ab4502009-04-04 21:01:06 +00004064 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
4065}
4066
Al Viro9aae8fc2012-12-24 23:12:04 -05004067#ifdef CONFIG_COMPAT
4068COMPAT_SYSCALL_DEFINE4(rt_tgsigqueueinfo,
4069 compat_pid_t, tgid,
4070 compat_pid_t, pid,
4071 int, sig,
4072 struct compat_siginfo __user *, uinfo)
4073{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004074 kernel_siginfo_t info;
Eric W. Biederman601d5ab2018-10-05 09:02:48 +02004075 int ret = __copy_siginfo_from_user32(sig, &info, uinfo);
Eric W. Biederman4cd2e0e2018-04-18 17:30:19 -05004076 if (unlikely(ret))
4077 return ret;
Al Viro9aae8fc2012-12-24 23:12:04 -05004078 return do_rt_tgsigqueueinfo(tgid, pid, sig, &info);
4079}
4080#endif
4081
Oleg Nesterov03417292014-06-06 14:36:53 -07004082/*
Oleg Nesterovb4e74262014-06-06 14:37:00 -07004083 * For kthreads only, must not be used if cloned with CLONE_SIGHAND
Oleg Nesterov03417292014-06-06 14:36:53 -07004084 */
Oleg Nesterovb4e74262014-06-06 14:37:00 -07004085void kernel_sigaction(int sig, __sighandler_t action)
Oleg Nesterov03417292014-06-06 14:36:53 -07004086{
Oleg Nesterovec5955b2014-06-06 14:36:57 -07004087 spin_lock_irq(&current->sighand->siglock);
Oleg Nesterovb4e74262014-06-06 14:37:00 -07004088 current->sighand->action[sig - 1].sa.sa_handler = action;
4089 if (action == SIG_IGN) {
4090 sigset_t mask;
4091
4092 sigemptyset(&mask);
4093 sigaddset(&mask, sig);
4094
4095 flush_sigqueue_mask(&mask, &current->signal->shared_pending);
4096 flush_sigqueue_mask(&mask, &current->pending);
4097 recalc_sigpending();
4098 }
Oleg Nesterov03417292014-06-06 14:36:53 -07004099 spin_unlock_irq(&current->sighand->siglock);
4100}
Oleg Nesterovb4e74262014-06-06 14:37:00 -07004101EXPORT_SYMBOL(kernel_sigaction);
Oleg Nesterov03417292014-06-06 14:36:53 -07004102
Dmitry Safonov68463512016-09-05 16:33:08 +03004103void __weak sigaction_compat_abi(struct k_sigaction *act,
4104 struct k_sigaction *oact)
4105{
4106}
4107
Oleg Nesterov88531f72006-03-28 16:11:24 -08004108int do_sigaction(int sig, struct k_sigaction *act, struct k_sigaction *oact)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109{
Oleg Nesterovafe2b032014-06-06 14:36:51 -07004110 struct task_struct *p = current, *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 struct k_sigaction *k;
George Anzinger71fabd52006-01-08 01:02:48 -08004112 sigset_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004113
Jesper Juhl7ed20e12005-05-01 08:59:14 -07004114 if (!valid_signal(sig) || sig < 1 || (act && sig_kernel_only(sig)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 return -EINVAL;
4116
Oleg Nesterovafe2b032014-06-06 14:36:51 -07004117 k = &p->sighand->action[sig-1];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118
Oleg Nesterovafe2b032014-06-06 14:36:51 -07004119 spin_lock_irq(&p->sighand->siglock);
Eric W. Biederman00b06da2021-10-29 09:14:19 -05004120 if (k->sa.sa_flags & SA_IMMUTABLE) {
4121 spin_unlock_irq(&p->sighand->siglock);
4122 return -EINVAL;
4123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 if (oact)
4125 *oact = *k;
4126
Peter Collingbourne23acdc72020-11-12 18:53:34 -08004127 /*
Peter Collingbournea54f0df2020-11-16 19:17:25 -08004128 * Make sure that we never accidentally claim to support SA_UNSUPPORTED,
4129 * e.g. by having an architecture use the bit in their uapi.
4130 */
4131 BUILD_BUG_ON(UAPI_SA_FLAGS & SA_UNSUPPORTED);
4132
4133 /*
Peter Collingbourne23acdc72020-11-12 18:53:34 -08004134 * Clear unknown flag bits in order to allow userspace to detect missing
4135 * support for flag bits and to allow the kernel to use non-uapi bits
4136 * internally.
4137 */
4138 if (act)
4139 act->sa.sa_flags &= UAPI_SA_FLAGS;
4140 if (oact)
4141 oact->sa.sa_flags &= UAPI_SA_FLAGS;
4142
Dmitry Safonov68463512016-09-05 16:33:08 +03004143 sigaction_compat_abi(act, oact);
4144
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 if (act) {
Oleg Nesterov9ac95f22006-02-09 22:41:50 +03004146 sigdelsetmask(&act->sa.sa_mask,
4147 sigmask(SIGKILL) | sigmask(SIGSTOP));
Oleg Nesterov88531f72006-03-28 16:11:24 -08004148 *k = *act;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 /*
4150 * POSIX 3.3.1.3:
4151 * "Setting a signal action to SIG_IGN for a signal that is
4152 * pending shall cause the pending signal to be discarded,
4153 * whether or not it is blocked."
4154 *
4155 * "Setting a signal action to SIG_DFL for a signal that is
4156 * pending and whose default action is to ignore the signal
4157 * (for example, SIGCHLD), shall cause the pending signal to
4158 * be discarded, whether or not it is blocked"
4159 */
Oleg Nesterovafe2b032014-06-06 14:36:51 -07004160 if (sig_handler_ignored(sig_handler(p, sig), sig)) {
George Anzinger71fabd52006-01-08 01:02:48 -08004161 sigemptyset(&mask);
4162 sigaddset(&mask, sig);
Oleg Nesterovafe2b032014-06-06 14:36:51 -07004163 flush_sigqueue_mask(&mask, &p->signal->shared_pending);
4164 for_each_thread(p, t)
Oleg Nesterovc09c1442014-06-06 14:36:50 -07004165 flush_sigqueue_mask(&mask, &t->pending);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 }
4168
Oleg Nesterovafe2b032014-06-06 14:36:51 -07004169 spin_unlock_irq(&p->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 return 0;
4171}
4172
Thomas Gleixner1bdda242021-10-21 15:55:05 -07004173#ifdef CONFIG_DYNAMIC_SIGFRAME
4174static inline void sigaltstack_lock(void)
4175 __acquires(&current->sighand->siglock)
4176{
4177 spin_lock_irq(&current->sighand->siglock);
4178}
4179
4180static inline void sigaltstack_unlock(void)
4181 __releases(&current->sighand->siglock)
4182{
4183 spin_unlock_irq(&current->sighand->siglock);
4184}
4185#else
4186static inline void sigaltstack_lock(void) { }
4187static inline void sigaltstack_unlock(void) { }
4188#endif
4189
Oleg Nesterovc09c1442014-06-06 14:36:50 -07004190static int
Will Deacon22839862018-09-05 15:34:42 +01004191do_sigaltstack (const stack_t *ss, stack_t *oss, unsigned long sp,
4192 size_t min_ss_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193{
Al Virobcfe8ad2017-05-27 00:29:34 -04004194 struct task_struct *t = current;
Thomas Gleixner1bdda242021-10-21 15:55:05 -07004195 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
Al Virobcfe8ad2017-05-27 00:29:34 -04004197 if (oss) {
4198 memset(oss, 0, sizeof(stack_t));
4199 oss->ss_sp = (void __user *) t->sas_ss_sp;
4200 oss->ss_size = t->sas_ss_size;
4201 oss->ss_flags = sas_ss_flags(sp) |
4202 (current->sas_ss_flags & SS_FLAG_BITS);
4203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204
Al Virobcfe8ad2017-05-27 00:29:34 -04004205 if (ss) {
4206 void __user *ss_sp = ss->ss_sp;
4207 size_t ss_size = ss->ss_size;
4208 unsigned ss_flags = ss->ss_flags;
Stas Sergeev407bc162016-04-14 23:20:03 +03004209 int ss_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
Al Virobcfe8ad2017-05-27 00:29:34 -04004211 if (unlikely(on_sig_stack(sp)))
4212 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213
Stas Sergeev407bc162016-04-14 23:20:03 +03004214 ss_mode = ss_flags & ~SS_FLAG_BITS;
Al Virobcfe8ad2017-05-27 00:29:34 -04004215 if (unlikely(ss_mode != SS_DISABLE && ss_mode != SS_ONSTACK &&
4216 ss_mode != 0))
4217 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Chang S. Bae6c3118c2021-12-10 14:55:03 -08004219 /*
4220 * Return before taking any locks if no actual
4221 * sigaltstack changes were requested.
4222 */
4223 if (t->sas_ss_sp == (unsigned long)ss_sp &&
4224 t->sas_ss_size == ss_size &&
4225 t->sas_ss_flags == ss_flags)
4226 return 0;
4227
Thomas Gleixner1bdda242021-10-21 15:55:05 -07004228 sigaltstack_lock();
Stas Sergeev407bc162016-04-14 23:20:03 +03004229 if (ss_mode == SS_DISABLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 ss_size = 0;
4231 ss_sp = NULL;
4232 } else {
Will Deacon22839862018-09-05 15:34:42 +01004233 if (unlikely(ss_size < min_ss_size))
Thomas Gleixner1bdda242021-10-21 15:55:05 -07004234 ret = -ENOMEM;
4235 if (!sigaltstack_size_valid(ss_size))
4236 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 }
Thomas Gleixner1bdda242021-10-21 15:55:05 -07004238 if (!ret) {
4239 t->sas_ss_sp = (unsigned long) ss_sp;
4240 t->sas_ss_size = ss_size;
4241 t->sas_ss_flags = ss_flags;
4242 }
4243 sigaltstack_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244 }
Thomas Gleixner1bdda242021-10-21 15:55:05 -07004245 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246}
Al Virobcfe8ad2017-05-27 00:29:34 -04004247
Al Viro6bf9adf2012-12-14 14:09:47 -05004248SYSCALL_DEFINE2(sigaltstack,const stack_t __user *,uss, stack_t __user *,uoss)
4249{
Al Virobcfe8ad2017-05-27 00:29:34 -04004250 stack_t new, old;
4251 int err;
4252 if (uss && copy_from_user(&new, uss, sizeof(stack_t)))
4253 return -EFAULT;
4254 err = do_sigaltstack(uss ? &new : NULL, uoss ? &old : NULL,
Will Deacon22839862018-09-05 15:34:42 +01004255 current_user_stack_pointer(),
4256 MINSIGSTKSZ);
Al Virobcfe8ad2017-05-27 00:29:34 -04004257 if (!err && uoss && copy_to_user(uoss, &old, sizeof(stack_t)))
4258 err = -EFAULT;
4259 return err;
Al Viro6bf9adf2012-12-14 14:09:47 -05004260}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261
Al Viro5c495742012-11-18 15:29:16 -05004262int restore_altstack(const stack_t __user *uss)
4263{
Al Virobcfe8ad2017-05-27 00:29:34 -04004264 stack_t new;
4265 if (copy_from_user(&new, uss, sizeof(stack_t)))
4266 return -EFAULT;
Will Deacon22839862018-09-05 15:34:42 +01004267 (void)do_sigaltstack(&new, NULL, current_user_stack_pointer(),
4268 MINSIGSTKSZ);
Al Viro5c495742012-11-18 15:29:16 -05004269 /* squash all but EFAULT for now */
Al Virobcfe8ad2017-05-27 00:29:34 -04004270 return 0;
Al Viro5c495742012-11-18 15:29:16 -05004271}
4272
Al Viroc40702c2012-11-20 14:24:26 -05004273int __save_altstack(stack_t __user *uss, unsigned long sp)
4274{
4275 struct task_struct *t = current;
Stas Sergeev2a742132016-04-14 23:20:04 +03004276 int err = __put_user((void __user *)t->sas_ss_sp, &uss->ss_sp) |
4277 __put_user(t->sas_ss_flags, &uss->ss_flags) |
Al Viroc40702c2012-11-20 14:24:26 -05004278 __put_user(t->sas_ss_size, &uss->ss_size);
Al Viro97c885d2021-06-30 18:56:43 -07004279 return err;
Al Viroc40702c2012-11-20 14:24:26 -05004280}
4281
Al Viro90268432012-12-14 14:47:53 -05004282#ifdef CONFIG_COMPAT
Dominik Brodowski6203deb2018-03-17 17:11:51 +01004283static int do_compat_sigaltstack(const compat_stack_t __user *uss_ptr,
4284 compat_stack_t __user *uoss_ptr)
Al Viro90268432012-12-14 14:47:53 -05004285{
4286 stack_t uss, uoss;
4287 int ret;
Al Viro90268432012-12-14 14:47:53 -05004288
4289 if (uss_ptr) {
4290 compat_stack_t uss32;
Al Viro90268432012-12-14 14:47:53 -05004291 if (copy_from_user(&uss32, uss_ptr, sizeof(compat_stack_t)))
4292 return -EFAULT;
4293 uss.ss_sp = compat_ptr(uss32.ss_sp);
4294 uss.ss_flags = uss32.ss_flags;
4295 uss.ss_size = uss32.ss_size;
4296 }
Al Virobcfe8ad2017-05-27 00:29:34 -04004297 ret = do_sigaltstack(uss_ptr ? &uss : NULL, &uoss,
Will Deacon22839862018-09-05 15:34:42 +01004298 compat_user_stack_pointer(),
4299 COMPAT_MINSIGSTKSZ);
Al Viro90268432012-12-14 14:47:53 -05004300 if (ret >= 0 && uoss_ptr) {
Al Virobcfe8ad2017-05-27 00:29:34 -04004301 compat_stack_t old;
4302 memset(&old, 0, sizeof(old));
4303 old.ss_sp = ptr_to_compat(uoss.ss_sp);
4304 old.ss_flags = uoss.ss_flags;
4305 old.ss_size = uoss.ss_size;
4306 if (copy_to_user(uoss_ptr, &old, sizeof(compat_stack_t)))
Al Viro90268432012-12-14 14:47:53 -05004307 ret = -EFAULT;
4308 }
4309 return ret;
4310}
4311
Dominik Brodowski6203deb2018-03-17 17:11:51 +01004312COMPAT_SYSCALL_DEFINE2(sigaltstack,
4313 const compat_stack_t __user *, uss_ptr,
4314 compat_stack_t __user *, uoss_ptr)
4315{
4316 return do_compat_sigaltstack(uss_ptr, uoss_ptr);
4317}
4318
Al Viro90268432012-12-14 14:47:53 -05004319int compat_restore_altstack(const compat_stack_t __user *uss)
4320{
Dominik Brodowski6203deb2018-03-17 17:11:51 +01004321 int err = do_compat_sigaltstack(uss, NULL);
Al Viro90268432012-12-14 14:47:53 -05004322 /* squash all but -EFAULT for now */
4323 return err == -EFAULT ? err : 0;
4324}
Al Viroc40702c2012-11-20 14:24:26 -05004325
4326int __compat_save_altstack(compat_stack_t __user *uss, unsigned long sp)
4327{
Stas Sergeev441398d2017-02-27 14:27:25 -08004328 int err;
Al Viroc40702c2012-11-20 14:24:26 -05004329 struct task_struct *t = current;
Stas Sergeev441398d2017-02-27 14:27:25 -08004330 err = __put_user(ptr_to_compat((void __user *)t->sas_ss_sp),
4331 &uss->ss_sp) |
4332 __put_user(t->sas_ss_flags, &uss->ss_flags) |
Al Viroc40702c2012-11-20 14:24:26 -05004333 __put_user(t->sas_ss_size, &uss->ss_size);
Al Viro97c885d2021-06-30 18:56:43 -07004334 return err;
Al Viroc40702c2012-11-20 14:24:26 -05004335}
Al Viro90268432012-12-14 14:47:53 -05004336#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337
4338#ifdef __ARCH_WANT_SYS_SIGPENDING
4339
Randy Dunlap41c57892011-04-04 15:00:26 -07004340/**
4341 * sys_sigpending - examine pending signals
Dominik Brodowskid53238c2018-03-11 11:34:37 +01004342 * @uset: where mask of pending signal is returned
Randy Dunlap41c57892011-04-04 15:00:26 -07004343 */
Dominik Brodowskid53238c2018-03-11 11:34:37 +01004344SYSCALL_DEFINE1(sigpending, old_sigset_t __user *, uset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345{
Dominik Brodowskid53238c2018-03-11 11:34:37 +01004346 sigset_t set;
Dominik Brodowskid53238c2018-03-11 11:34:37 +01004347
4348 if (sizeof(old_sigset_t) > sizeof(*uset))
4349 return -EINVAL;
4350
Christian Braunerb1d294c2018-08-21 22:00:02 -07004351 do_sigpending(&set);
4352
4353 if (copy_to_user(uset, &set, sizeof(old_sigset_t)))
4354 return -EFAULT;
4355
4356 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357}
4358
Al Viro8f136212017-05-31 04:42:07 -04004359#ifdef CONFIG_COMPAT
4360COMPAT_SYSCALL_DEFINE1(sigpending, compat_old_sigset_t __user *, set32)
4361{
4362 sigset_t set;
Christian Braunerb1d294c2018-08-21 22:00:02 -07004363
4364 do_sigpending(&set);
4365
4366 return put_user(set.sig[0], set32);
Al Viro8f136212017-05-31 04:42:07 -04004367}
4368#endif
4369
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370#endif
4371
4372#ifdef __ARCH_WANT_SYS_SIGPROCMASK
Randy Dunlap41c57892011-04-04 15:00:26 -07004373/**
4374 * sys_sigprocmask - examine and change blocked signals
4375 * @how: whether to add, remove, or set signals
Oleg Nesterovb013c392011-04-28 11:36:20 +02004376 * @nset: signals to add or remove (if non-null)
Randy Dunlap41c57892011-04-04 15:00:26 -07004377 * @oset: previous value of signal mask if non-null
4378 *
Randy Dunlap5aba0852011-04-04 14:59:31 -07004379 * Some platforms have their own version with special arguments;
4380 * others support only sys_rt_sigprocmask.
4381 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382
Oleg Nesterovb013c392011-04-28 11:36:20 +02004383SYSCALL_DEFINE3(sigprocmask, int, how, old_sigset_t __user *, nset,
Heiko Carstensb290ebe2009-01-14 14:14:06 +01004384 old_sigset_t __user *, oset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 old_sigset_t old_set, new_set;
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02004387 sigset_t new_blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
Oleg Nesterovb013c392011-04-28 11:36:20 +02004389 old_set = current->blocked.sig[0];
4390
4391 if (nset) {
4392 if (copy_from_user(&new_set, nset, sizeof(*nset)))
4393 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02004395 new_blocked = current->blocked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 switch (how) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 case SIG_BLOCK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02004399 sigaddsetmask(&new_blocked, new_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 break;
4401 case SIG_UNBLOCK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02004402 sigdelsetmask(&new_blocked, new_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 break;
4404 case SIG_SETMASK:
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02004405 new_blocked.sig[0] = new_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004406 break;
Oleg Nesterov2e4f7c72011-05-09 13:48:56 +02004407 default:
4408 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 }
4410
Oleg Nesterov0c4a8422013-01-05 19:13:29 +01004411 set_current_blocked(&new_blocked);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 }
Oleg Nesterovb013c392011-04-28 11:36:20 +02004413
4414 if (oset) {
4415 if (copy_to_user(oset, &old_set, sizeof(*oset)))
4416 return -EFAULT;
4417 }
4418
4419 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420}
4421#endif /* __ARCH_WANT_SYS_SIGPROCMASK */
4422
Al Viroeaca6ea2012-11-25 23:12:10 -05004423#ifndef CONFIG_ODD_RT_SIGACTION
Randy Dunlap41c57892011-04-04 15:00:26 -07004424/**
4425 * sys_rt_sigaction - alter an action taken by a process
4426 * @sig: signal to be sent
Randy Dunlapf9fa0bc2011-04-08 10:53:46 -07004427 * @act: new sigaction
4428 * @oact: used to save the previous sigaction
Randy Dunlap41c57892011-04-04 15:00:26 -07004429 * @sigsetsize: size of sigset_t type
4430 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01004431SYSCALL_DEFINE4(rt_sigaction, int, sig,
4432 const struct sigaction __user *, act,
4433 struct sigaction __user *, oact,
4434 size_t, sigsetsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435{
4436 struct k_sigaction new_sa, old_sa;
Christian Braunerd8f993b2018-08-21 22:00:07 -07004437 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438
4439 /* XXX: Don't preclude handling different sized sigset_t's. */
4440 if (sigsetsize != sizeof(sigset_t))
Christian Braunerd8f993b2018-08-21 22:00:07 -07004441 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442
Christian Braunerd8f993b2018-08-21 22:00:07 -07004443 if (act && copy_from_user(&new_sa.sa, act, sizeof(new_sa.sa)))
4444 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445
4446 ret = do_sigaction(sig, act ? &new_sa : NULL, oact ? &old_sa : NULL);
Christian Braunerd8f993b2018-08-21 22:00:07 -07004447 if (ret)
4448 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004449
Christian Braunerd8f993b2018-08-21 22:00:07 -07004450 if (oact && copy_to_user(oact, &old_sa.sa, sizeof(old_sa.sa)))
4451 return -EFAULT;
4452
4453 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454}
Al Viro08d32fe2012-12-25 18:38:15 -05004455#ifdef CONFIG_COMPAT
Al Viro08d32fe2012-12-25 18:38:15 -05004456COMPAT_SYSCALL_DEFINE4(rt_sigaction, int, sig,
4457 const struct compat_sigaction __user *, act,
4458 struct compat_sigaction __user *, oact,
4459 compat_size_t, sigsetsize)
4460{
4461 struct k_sigaction new_ka, old_ka;
Al Viro08d32fe2012-12-25 18:38:15 -05004462#ifdef __ARCH_HAS_SA_RESTORER
4463 compat_uptr_t restorer;
4464#endif
4465 int ret;
4466
4467 /* XXX: Don't preclude handling different sized sigset_t's. */
4468 if (sigsetsize != sizeof(compat_sigset_t))
4469 return -EINVAL;
4470
4471 if (act) {
4472 compat_uptr_t handler;
4473 ret = get_user(handler, &act->sa_handler);
4474 new_ka.sa.sa_handler = compat_ptr(handler);
4475#ifdef __ARCH_HAS_SA_RESTORER
4476 ret |= get_user(restorer, &act->sa_restorer);
4477 new_ka.sa.sa_restorer = compat_ptr(restorer);
4478#endif
Al Viro3968cf62017-09-03 21:45:17 -04004479 ret |= get_compat_sigset(&new_ka.sa.sa_mask, &act->sa_mask);
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07004480 ret |= get_user(new_ka.sa.sa_flags, &act->sa_flags);
Al Viro08d32fe2012-12-25 18:38:15 -05004481 if (ret)
4482 return -EFAULT;
Al Viro08d32fe2012-12-25 18:38:15 -05004483 }
4484
4485 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
4486 if (!ret && oact) {
Al Viro08d32fe2012-12-25 18:38:15 -05004487 ret = put_user(ptr_to_compat(old_ka.sa.sa_handler),
4488 &oact->sa_handler);
Dmitry V. Levinf4543222017-08-22 02:16:11 +03004489 ret |= put_compat_sigset(&oact->sa_mask, &old_ka.sa.sa_mask,
4490 sizeof(oact->sa_mask));
Mathieu Desnoyers3ddc5b42013-09-11 14:23:18 -07004491 ret |= put_user(old_ka.sa.sa_flags, &oact->sa_flags);
Al Viro08d32fe2012-12-25 18:38:15 -05004492#ifdef __ARCH_HAS_SA_RESTORER
4493 ret |= put_user(ptr_to_compat(old_ka.sa.sa_restorer),
4494 &oact->sa_restorer);
4495#endif
4496 }
4497 return ret;
4498}
4499#endif
Al Viroeaca6ea2012-11-25 23:12:10 -05004500#endif /* !CONFIG_ODD_RT_SIGACTION */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501
Al Viro495dfbf2012-12-25 19:09:45 -05004502#ifdef CONFIG_OLD_SIGACTION
4503SYSCALL_DEFINE3(sigaction, int, sig,
4504 const struct old_sigaction __user *, act,
4505 struct old_sigaction __user *, oact)
4506{
4507 struct k_sigaction new_ka, old_ka;
4508 int ret;
4509
4510 if (act) {
4511 old_sigset_t mask;
Linus Torvalds96d4f262019-01-03 18:57:57 -08004512 if (!access_ok(act, sizeof(*act)) ||
Al Viro495dfbf2012-12-25 19:09:45 -05004513 __get_user(new_ka.sa.sa_handler, &act->sa_handler) ||
4514 __get_user(new_ka.sa.sa_restorer, &act->sa_restorer) ||
4515 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
4516 __get_user(mask, &act->sa_mask))
4517 return -EFAULT;
4518#ifdef __ARCH_HAS_KA_RESTORER
4519 new_ka.ka_restorer = NULL;
4520#endif
4521 siginitset(&new_ka.sa.sa_mask, mask);
4522 }
4523
4524 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
4525
4526 if (!ret && oact) {
Linus Torvalds96d4f262019-01-03 18:57:57 -08004527 if (!access_ok(oact, sizeof(*oact)) ||
Al Viro495dfbf2012-12-25 19:09:45 -05004528 __put_user(old_ka.sa.sa_handler, &oact->sa_handler) ||
4529 __put_user(old_ka.sa.sa_restorer, &oact->sa_restorer) ||
4530 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
4531 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
4532 return -EFAULT;
4533 }
4534
4535 return ret;
4536}
4537#endif
4538#ifdef CONFIG_COMPAT_OLD_SIGACTION
4539COMPAT_SYSCALL_DEFINE3(sigaction, int, sig,
4540 const struct compat_old_sigaction __user *, act,
4541 struct compat_old_sigaction __user *, oact)
4542{
4543 struct k_sigaction new_ka, old_ka;
4544 int ret;
4545 compat_old_sigset_t mask;
4546 compat_uptr_t handler, restorer;
4547
4548 if (act) {
Linus Torvalds96d4f262019-01-03 18:57:57 -08004549 if (!access_ok(act, sizeof(*act)) ||
Al Viro495dfbf2012-12-25 19:09:45 -05004550 __get_user(handler, &act->sa_handler) ||
4551 __get_user(restorer, &act->sa_restorer) ||
4552 __get_user(new_ka.sa.sa_flags, &act->sa_flags) ||
4553 __get_user(mask, &act->sa_mask))
4554 return -EFAULT;
4555
4556#ifdef __ARCH_HAS_KA_RESTORER
4557 new_ka.ka_restorer = NULL;
4558#endif
4559 new_ka.sa.sa_handler = compat_ptr(handler);
4560 new_ka.sa.sa_restorer = compat_ptr(restorer);
4561 siginitset(&new_ka.sa.sa_mask, mask);
4562 }
4563
4564 ret = do_sigaction(sig, act ? &new_ka : NULL, oact ? &old_ka : NULL);
4565
4566 if (!ret && oact) {
Linus Torvalds96d4f262019-01-03 18:57:57 -08004567 if (!access_ok(oact, sizeof(*oact)) ||
Al Viro495dfbf2012-12-25 19:09:45 -05004568 __put_user(ptr_to_compat(old_ka.sa.sa_handler),
4569 &oact->sa_handler) ||
4570 __put_user(ptr_to_compat(old_ka.sa.sa_restorer),
4571 &oact->sa_restorer) ||
4572 __put_user(old_ka.sa.sa_flags, &oact->sa_flags) ||
4573 __put_user(old_ka.sa.sa_mask.sig[0], &oact->sa_mask))
4574 return -EFAULT;
4575 }
4576 return ret;
4577}
4578#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579
Fabian Frederickf6187762014-06-04 16:11:12 -07004580#ifdef CONFIG_SGETMASK_SYSCALL
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581
4582/*
4583 * For backwards compatibility. Functionality superseded by sigprocmask.
4584 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01004585SYSCALL_DEFINE0(sgetmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586{
4587 /* SMP safe */
4588 return current->blocked.sig[0];
4589}
4590
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01004591SYSCALL_DEFINE1(ssetmask, int, newmask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592{
Oleg Nesterovc1095c62011-07-27 12:49:44 -07004593 int old = current->blocked.sig[0];
4594 sigset_t newset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595
Oleg Nesterov5ba53ff2013-01-05 19:13:13 +01004596 siginitset(&newset, newmask);
Oleg Nesterovc1095c62011-07-27 12:49:44 -07004597 set_current_blocked(&newset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
4599 return old;
4600}
Fabian Frederickf6187762014-06-04 16:11:12 -07004601#endif /* CONFIG_SGETMASK_SYSCALL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602
4603#ifdef __ARCH_WANT_SYS_SIGNAL
4604/*
4605 * For backwards compatibility. Functionality superseded by sigaction.
4606 */
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01004607SYSCALL_DEFINE2(signal, int, sig, __sighandler_t, handler)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608{
4609 struct k_sigaction new_sa, old_sa;
4610 int ret;
4611
4612 new_sa.sa.sa_handler = handler;
4613 new_sa.sa.sa_flags = SA_ONESHOT | SA_NOMASK;
Oleg Nesterovc70d3d702006-02-09 22:41:41 +03004614 sigemptyset(&new_sa.sa.sa_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615
4616 ret = do_sigaction(sig, &new_sa, &old_sa);
4617
4618 return ret ? ret : (unsigned long)old_sa.sa.sa_handler;
4619}
4620#endif /* __ARCH_WANT_SYS_SIGNAL */
4621
4622#ifdef __ARCH_WANT_SYS_PAUSE
4623
Heiko Carstensa5f8fa92009-01-14 14:14:11 +01004624SYSCALL_DEFINE0(pause)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625{
Oleg Nesterovd92fcf02011-05-25 19:22:27 +02004626 while (!signal_pending(current)) {
Davidlohr Bueso1df01352015-02-17 13:45:41 -08004627 __set_current_state(TASK_INTERRUPTIBLE);
Oleg Nesterovd92fcf02011-05-25 19:22:27 +02004628 schedule();
4629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 return -ERESTARTNOHAND;
4631}
4632
4633#endif
4634
Richard Weinberger9d8a7652015-11-20 15:57:21 -08004635static int sigsuspend(sigset_t *set)
Al Viro68f3f162012-05-21 21:42:32 -04004636{
Al Viro68f3f162012-05-21 21:42:32 -04004637 current->saved_sigmask = current->blocked;
4638 set_current_blocked(set);
4639
Sasha Levin823dd322016-02-05 15:36:05 -08004640 while (!signal_pending(current)) {
4641 __set_current_state(TASK_INTERRUPTIBLE);
4642 schedule();
4643 }
Al Viro68f3f162012-05-21 21:42:32 -04004644 set_restore_sigmask();
4645 return -ERESTARTNOHAND;
4646}
Al Viro68f3f162012-05-21 21:42:32 -04004647
Randy Dunlap41c57892011-04-04 15:00:26 -07004648/**
4649 * sys_rt_sigsuspend - replace the signal mask for a value with the
4650 * @unewset value until a signal is received
4651 * @unewset: new signal mask value
4652 * @sigsetsize: size of sigset_t type
4653 */
Heiko Carstensd4e82042009-01-14 14:14:34 +01004654SYSCALL_DEFINE2(rt_sigsuspend, sigset_t __user *, unewset, size_t, sigsetsize)
David Woodhouse150256d2006-01-18 17:43:57 -08004655{
4656 sigset_t newset;
4657
4658 /* XXX: Don't preclude handling different sized sigset_t's. */
4659 if (sigsetsize != sizeof(sigset_t))
4660 return -EINVAL;
4661
4662 if (copy_from_user(&newset, unewset, sizeof(newset)))
4663 return -EFAULT;
Al Viro68f3f162012-05-21 21:42:32 -04004664 return sigsuspend(&newset);
David Woodhouse150256d2006-01-18 17:43:57 -08004665}
Al Viroad4b65a2012-12-24 21:43:56 -05004666
4667#ifdef CONFIG_COMPAT
4668COMPAT_SYSCALL_DEFINE2(rt_sigsuspend, compat_sigset_t __user *, unewset, compat_size_t, sigsetsize)
4669{
Al Viroad4b65a2012-12-24 21:43:56 -05004670 sigset_t newset;
Al Viroad4b65a2012-12-24 21:43:56 -05004671
4672 /* XXX: Don't preclude handling different sized sigset_t's. */
4673 if (sigsetsize != sizeof(sigset_t))
4674 return -EINVAL;
4675
Al Viro3968cf62017-09-03 21:45:17 -04004676 if (get_compat_sigset(&newset, unewset))
Al Viroad4b65a2012-12-24 21:43:56 -05004677 return -EFAULT;
Al Viroad4b65a2012-12-24 21:43:56 -05004678 return sigsuspend(&newset);
Al Viroad4b65a2012-12-24 21:43:56 -05004679}
4680#endif
David Woodhouse150256d2006-01-18 17:43:57 -08004681
Al Viro0a0e8cd2012-12-25 16:04:12 -05004682#ifdef CONFIG_OLD_SIGSUSPEND
4683SYSCALL_DEFINE1(sigsuspend, old_sigset_t, mask)
4684{
4685 sigset_t blocked;
4686 siginitset(&blocked, mask);
4687 return sigsuspend(&blocked);
4688}
4689#endif
4690#ifdef CONFIG_OLD_SIGSUSPEND3
4691SYSCALL_DEFINE3(sigsuspend, int, unused1, int, unused2, old_sigset_t, mask)
4692{
4693 sigset_t blocked;
4694 siginitset(&blocked, mask);
4695 return sigsuspend(&blocked);
4696}
4697#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698
Gideon Israel Dsouza52f5684c2014-04-07 15:39:20 -07004699__weak const char *arch_vma_name(struct vm_area_struct *vma)
David Howellsf269fdd2006-09-27 01:50:23 -07004700{
4701 return NULL;
4702}
4703
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004704static inline void siginfo_buildtime_checks(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705{
Eric W. Biedermanaba1be22017-07-19 21:23:15 -05004706 BUILD_BUG_ON(sizeof(struct siginfo) != SI_MAX_SIZE);
Helge Deller41b27152016-03-22 14:27:54 -07004707
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004708 /* Verify the offsets in the two siginfos match */
4709#define CHECK_OFFSET(field) \
4710 BUILD_BUG_ON(offsetof(siginfo_t, field) != offsetof(kernel_siginfo_t, field))
4711
4712 /* kill */
4713 CHECK_OFFSET(si_pid);
4714 CHECK_OFFSET(si_uid);
4715
4716 /* timer */
4717 CHECK_OFFSET(si_tid);
4718 CHECK_OFFSET(si_overrun);
4719 CHECK_OFFSET(si_value);
4720
4721 /* rt */
4722 CHECK_OFFSET(si_pid);
4723 CHECK_OFFSET(si_uid);
4724 CHECK_OFFSET(si_value);
4725
4726 /* sigchld */
4727 CHECK_OFFSET(si_pid);
4728 CHECK_OFFSET(si_uid);
4729 CHECK_OFFSET(si_status);
4730 CHECK_OFFSET(si_utime);
4731 CHECK_OFFSET(si_stime);
4732
4733 /* sigfault */
4734 CHECK_OFFSET(si_addr);
Eric W. Biedermanadd0b322021-04-30 17:06:01 -05004735 CHECK_OFFSET(si_trapno);
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004736 CHECK_OFFSET(si_addr_lsb);
4737 CHECK_OFFSET(si_lower);
4738 CHECK_OFFSET(si_upper);
4739 CHECK_OFFSET(si_pkey);
Eric W. Biederman0683b532021-05-02 17:28:31 -05004740 CHECK_OFFSET(si_perf_data);
4741 CHECK_OFFSET(si_perf_type);
Marco Elver78ed93d2022-04-04 13:12:04 +02004742 CHECK_OFFSET(si_perf_flags);
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004743
4744 /* sigpoll */
4745 CHECK_OFFSET(si_band);
4746 CHECK_OFFSET(si_fd);
4747
4748 /* sigsys */
4749 CHECK_OFFSET(si_call_addr);
4750 CHECK_OFFSET(si_syscall);
4751 CHECK_OFFSET(si_arch);
4752#undef CHECK_OFFSET
Eric W. Biederman70f1b0d2019-02-07 19:44:12 -06004753
4754 /* usb asyncio */
4755 BUILD_BUG_ON(offsetof(struct siginfo, si_pid) !=
4756 offsetof(struct siginfo, si_addr));
4757 if (sizeof(int) == sizeof(void __user *)) {
4758 BUILD_BUG_ON(sizeof_field(struct siginfo, si_pid) !=
4759 sizeof(void __user *));
4760 } else {
4761 BUILD_BUG_ON((sizeof_field(struct siginfo, si_pid) +
4762 sizeof_field(struct siginfo, si_uid)) !=
4763 sizeof(void __user *));
4764 BUILD_BUG_ON(offsetofend(struct siginfo, si_pid) !=
4765 offsetof(struct siginfo, si_uid));
4766 }
4767#ifdef CONFIG_COMPAT
4768 BUILD_BUG_ON(offsetof(struct compat_siginfo, si_pid) !=
4769 offsetof(struct compat_siginfo, si_addr));
4770 BUILD_BUG_ON(sizeof_field(struct compat_siginfo, si_pid) !=
4771 sizeof(compat_uptr_t));
4772 BUILD_BUG_ON(sizeof_field(struct compat_siginfo, si_pid) !=
4773 sizeof_field(struct siginfo, si_pid));
4774#endif
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004775}
4776
Luis Chamberlain01e6aac2023-05-18 13:37:41 -07004777#if defined(CONFIG_SYSCTL)
4778static struct ctl_table signal_debug_table[] = {
4779#ifdef CONFIG_SYSCTL_EXCEPTION_TRACE
4780 {
4781 .procname = "exception-trace",
4782 .data = &show_unhandled_signals,
4783 .maxlen = sizeof(int),
4784 .mode = 0644,
4785 .proc_handler = proc_dointvec
4786 },
4787#endif
4788 { }
4789};
4790
4791static int __init init_signal_sysctls(void)
4792{
4793 register_sysctl_init("debug", signal_debug_table);
4794 return 0;
4795}
4796early_initcall(init_signal_sysctls);
4797#endif /* CONFIG_SYSCTL */
4798
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799void __init signals_init(void)
4800{
Eric W. Biedermanae7795b2018-09-25 11:27:20 +02004801 siginfo_buildtime_checks();
Jason Wessel67fc4e02010-05-20 21:04:21 -05004802
Vasily Averin5f58c392021-09-02 14:55:35 -07004803 sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC | SLAB_ACCOUNT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804}
Jason Wessel67fc4e02010-05-20 21:04:21 -05004805
4806#ifdef CONFIG_KGDB_KDB
4807#include <linux/kdb.h>
4808/*
Eric W. Biederman0b44bf92017-08-17 15:45:38 -05004809 * kdb_send_sig - Allows kdb to send signals without exposing
Jason Wessel67fc4e02010-05-20 21:04:21 -05004810 * signal internals. This function checks if the required locks are
4811 * available before calling the main signal code, to avoid kdb
4812 * deadlocks.
4813 */
Eric W. Biederman0b44bf92017-08-17 15:45:38 -05004814void kdb_send_sig(struct task_struct *t, int sig)
Jason Wessel67fc4e02010-05-20 21:04:21 -05004815{
4816 static struct task_struct *kdb_prev_t;
Eric W. Biederman0b44bf92017-08-17 15:45:38 -05004817 int new_t, ret;
Jason Wessel67fc4e02010-05-20 21:04:21 -05004818 if (!spin_trylock(&t->sighand->siglock)) {
4819 kdb_printf("Can't do kill command now.\n"
4820 "The sigmask lock is held somewhere else in "
4821 "kernel, try again later\n");
4822 return;
4823 }
Jason Wessel67fc4e02010-05-20 21:04:21 -05004824 new_t = kdb_prev_t != t;
4825 kdb_prev_t = t;
Peter Zijlstrab03fbd42021-06-11 10:28:12 +02004826 if (!task_is_running(t) && new_t) {
Eric W. Biederman0b44bf92017-08-17 15:45:38 -05004827 spin_unlock(&t->sighand->siglock);
Jason Wessel67fc4e02010-05-20 21:04:21 -05004828 kdb_printf("Process is not RUNNING, sending a signal from "
4829 "kdb risks deadlock\n"
4830 "on the run queue locks. "
4831 "The signal has _not_ been sent.\n"
4832 "Reissue the kill command if you want to risk "
4833 "the deadlock.\n");
4834 return;
4835 }
Eric W. Biederman157cc182022-04-22 09:48:54 -05004836 ret = send_signal_locked(sig, SEND_SIG_PRIV, t, PIDTYPE_PID);
Eric W. Biederman0b44bf92017-08-17 15:45:38 -05004837 spin_unlock(&t->sighand->siglock);
4838 if (ret)
Jason Wessel67fc4e02010-05-20 21:04:21 -05004839 kdb_printf("Fail to deliver Signal %d to process %d.\n",
4840 sig, t->pid);
4841 else
4842 kdb_printf("Signal %d is sent to process %d.\n", sig, t->pid);
4843}
4844#endif /* CONFIG_KGDB_KDB */