Line data Source code
1 : // SPDX-License-Identifier: GPL-2.0
2 : /*
3 : * linux/kernel/sys.c
4 : *
5 : * Copyright (C) 1991, 1992 Linus Torvalds
6 : */
7 :
8 : #include <linux/export.h>
9 : #include <linux/mm.h>
10 : #include <linux/mm_inline.h>
11 : #include <linux/utsname.h>
12 : #include <linux/mman.h>
13 : #include <linux/reboot.h>
14 : #include <linux/prctl.h>
15 : #include <linux/highuid.h>
16 : #include <linux/fs.h>
17 : #include <linux/kmod.h>
18 : #include <linux/perf_event.h>
19 : #include <linux/resource.h>
20 : #include <linux/kernel.h>
21 : #include <linux/workqueue.h>
22 : #include <linux/capability.h>
23 : #include <linux/device.h>
24 : #include <linux/key.h>
25 : #include <linux/times.h>
26 : #include <linux/posix-timers.h>
27 : #include <linux/security.h>
28 : #include <linux/suspend.h>
29 : #include <linux/tty.h>
30 : #include <linux/signal.h>
31 : #include <linux/cn_proc.h>
32 : #include <linux/getcpu.h>
33 : #include <linux/task_io_accounting_ops.h>
34 : #include <linux/seccomp.h>
35 : #include <linux/cpu.h>
36 : #include <linux/personality.h>
37 : #include <linux/ptrace.h>
38 : #include <linux/fs_struct.h>
39 : #include <linux/file.h>
40 : #include <linux/mount.h>
41 : #include <linux/gfp.h>
42 : #include <linux/syscore_ops.h>
43 : #include <linux/version.h>
44 : #include <linux/ctype.h>
45 : #include <linux/syscall_user_dispatch.h>
46 :
47 : #include <linux/compat.h>
48 : #include <linux/syscalls.h>
49 : #include <linux/kprobes.h>
50 : #include <linux/user_namespace.h>
51 : #include <linux/time_namespace.h>
52 : #include <linux/binfmts.h>
53 :
54 : #include <linux/sched.h>
55 : #include <linux/sched/autogroup.h>
56 : #include <linux/sched/loadavg.h>
57 : #include <linux/sched/stat.h>
58 : #include <linux/sched/mm.h>
59 : #include <linux/sched/coredump.h>
60 : #include <linux/sched/task.h>
61 : #include <linux/sched/cputime.h>
62 : #include <linux/rcupdate.h>
63 : #include <linux/uidgid.h>
64 : #include <linux/cred.h>
65 :
66 : #include <linux/nospec.h>
67 :
68 : #include <linux/kmsg_dump.h>
69 : /* Move somewhere else to avoid recompiling? */
70 : #include <generated/utsrelease.h>
71 :
72 : #include <linux/uaccess.h>
73 : #include <asm/io.h>
74 : #include <asm/unistd.h>
75 :
76 : #include "uid16.h"
77 :
78 : #ifndef SET_UNALIGN_CTL
79 : # define SET_UNALIGN_CTL(a, b) (-EINVAL)
80 : #endif
81 : #ifndef GET_UNALIGN_CTL
82 : # define GET_UNALIGN_CTL(a, b) (-EINVAL)
83 : #endif
84 : #ifndef SET_FPEMU_CTL
85 : # define SET_FPEMU_CTL(a, b) (-EINVAL)
86 : #endif
87 : #ifndef GET_FPEMU_CTL
88 : # define GET_FPEMU_CTL(a, b) (-EINVAL)
89 : #endif
90 : #ifndef SET_FPEXC_CTL
91 : # define SET_FPEXC_CTL(a, b) (-EINVAL)
92 : #endif
93 : #ifndef GET_FPEXC_CTL
94 : # define GET_FPEXC_CTL(a, b) (-EINVAL)
95 : #endif
96 : #ifndef GET_ENDIAN
97 : # define GET_ENDIAN(a, b) (-EINVAL)
98 : #endif
99 : #ifndef SET_ENDIAN
100 : # define SET_ENDIAN(a, b) (-EINVAL)
101 : #endif
102 : #ifndef GET_TSC_CTL
103 : # define GET_TSC_CTL(a) (-EINVAL)
104 : #endif
105 : #ifndef SET_TSC_CTL
106 : # define SET_TSC_CTL(a) (-EINVAL)
107 : #endif
108 : #ifndef GET_FP_MODE
109 : # define GET_FP_MODE(a) (-EINVAL)
110 : #endif
111 : #ifndef SET_FP_MODE
112 : # define SET_FP_MODE(a,b) (-EINVAL)
113 : #endif
114 : #ifndef SVE_SET_VL
115 : # define SVE_SET_VL(a) (-EINVAL)
116 : #endif
117 : #ifndef SVE_GET_VL
118 : # define SVE_GET_VL() (-EINVAL)
119 : #endif
120 : #ifndef PAC_RESET_KEYS
121 : # define PAC_RESET_KEYS(a, b) (-EINVAL)
122 : #endif
123 : #ifndef PAC_SET_ENABLED_KEYS
124 : # define PAC_SET_ENABLED_KEYS(a, b, c) (-EINVAL)
125 : #endif
126 : #ifndef PAC_GET_ENABLED_KEYS
127 : # define PAC_GET_ENABLED_KEYS(a) (-EINVAL)
128 : #endif
129 : #ifndef SET_TAGGED_ADDR_CTRL
130 : # define SET_TAGGED_ADDR_CTRL(a) (-EINVAL)
131 : #endif
132 : #ifndef GET_TAGGED_ADDR_CTRL
133 : # define GET_TAGGED_ADDR_CTRL() (-EINVAL)
134 : #endif
135 :
136 : /*
137 : * this is where the system-wide overflow UID and GID are defined, for
138 : * architectures that now have 32-bit UID/GID but didn't in the past
139 : */
140 :
141 : int overflowuid = DEFAULT_OVERFLOWUID;
142 : int overflowgid = DEFAULT_OVERFLOWGID;
143 :
144 : EXPORT_SYMBOL(overflowuid);
145 : EXPORT_SYMBOL(overflowgid);
146 :
147 : /*
148 : * the same as above, but for filesystems which can only store a 16-bit
149 : * UID and GID. as such, this is needed on all architectures
150 : */
151 :
152 : int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
153 : int fs_overflowgid = DEFAULT_FS_OVERFLOWGID;
154 :
155 : EXPORT_SYMBOL(fs_overflowuid);
156 : EXPORT_SYMBOL(fs_overflowgid);
157 :
158 : /*
159 : * Returns true if current's euid is same as p's uid or euid,
160 : * or has CAP_SYS_NICE to p's user_ns.
161 : *
162 : * Called with rcu_read_lock, creds are safe
163 : */
164 0 : static bool set_one_prio_perm(struct task_struct *p)
165 : {
166 0 : const struct cred *cred = current_cred(), *pcred = __task_cred(p);
167 :
168 0 : if (uid_eq(pcred->uid, cred->euid) ||
169 0 : uid_eq(pcred->euid, cred->euid))
170 : return true;
171 0 : if (ns_capable(pcred->user_ns, CAP_SYS_NICE))
172 : return true;
173 0 : return false;
174 : }
175 :
176 : /*
177 : * set the priority of a task
178 : * - the caller must hold the RCU read lock
179 : */
180 0 : static int set_one_prio(struct task_struct *p, int niceval, int error)
181 : {
182 : int no_nice;
183 :
184 0 : if (!set_one_prio_perm(p)) {
185 : error = -EPERM;
186 : goto out;
187 : }
188 0 : if (niceval < task_nice(p) && !can_nice(p, niceval)) {
189 : error = -EACCES;
190 : goto out;
191 : }
192 0 : no_nice = security_task_setnice(p, niceval);
193 0 : if (no_nice) {
194 : error = no_nice;
195 : goto out;
196 : }
197 0 : if (error == -ESRCH)
198 0 : error = 0;
199 0 : set_user_nice(p, niceval);
200 : out:
201 0 : return error;
202 : }
203 :
204 0 : SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval)
205 : {
206 : struct task_struct *g, *p;
207 : struct user_struct *user;
208 0 : const struct cred *cred = current_cred();
209 0 : int error = -EINVAL;
210 : struct pid *pgrp;
211 : kuid_t uid;
212 :
213 0 : if (which > PRIO_USER || which < PRIO_PROCESS)
214 : goto out;
215 :
216 : /* normalize: avoid signed division (rounding problems) */
217 0 : error = -ESRCH;
218 0 : if (niceval < MIN_NICE)
219 0 : niceval = MIN_NICE;
220 0 : if (niceval > MAX_NICE)
221 0 : niceval = MAX_NICE;
222 :
223 : rcu_read_lock();
224 0 : switch (which) {
225 : case PRIO_PROCESS:
226 0 : if (who)
227 0 : p = find_task_by_vpid(who);
228 : else
229 0 : p = current;
230 0 : if (p)
231 0 : error = set_one_prio(p, niceval, error);
232 : break;
233 : case PRIO_PGRP:
234 0 : if (who)
235 0 : pgrp = find_vpid(who);
236 : else
237 0 : pgrp = task_pgrp(current);
238 0 : read_lock(&tasklist_lock);
239 0 : do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
240 0 : error = set_one_prio(p, niceval, error);
241 0 : } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
242 0 : read_unlock(&tasklist_lock);
243 0 : break;
244 : case PRIO_USER:
245 0 : uid = make_kuid(cred->user_ns, who);
246 0 : user = cred->user;
247 0 : if (!who)
248 0 : uid = cred->uid;
249 0 : else if (!uid_eq(uid, cred->uid)) {
250 0 : user = find_user(uid);
251 0 : if (!user)
252 : goto out_unlock; /* No processes for this user */
253 : }
254 0 : for_each_process_thread(g, p) {
255 0 : if (uid_eq(task_uid(p), uid) && task_pid_vnr(p))
256 0 : error = set_one_prio(p, niceval, error);
257 : }
258 0 : if (!uid_eq(uid, cred->uid))
259 0 : free_uid(user); /* For find_user() */
260 : break;
261 : }
262 : out_unlock:
263 : rcu_read_unlock();
264 : out:
265 0 : return error;
266 : }
267 :
268 : /*
269 : * Ugh. To avoid negative return values, "getpriority()" will
270 : * not return the normal nice-value, but a negated value that
271 : * has been offset by 20 (ie it returns 40..1 instead of -20..19)
272 : * to stay compatible.
273 : */
274 0 : SYSCALL_DEFINE2(getpriority, int, which, int, who)
275 : {
276 : struct task_struct *g, *p;
277 : struct user_struct *user;
278 0 : const struct cred *cred = current_cred();
279 0 : long niceval, retval = -ESRCH;
280 : struct pid *pgrp;
281 : kuid_t uid;
282 :
283 0 : if (which > PRIO_USER || which < PRIO_PROCESS)
284 : return -EINVAL;
285 :
286 : rcu_read_lock();
287 0 : switch (which) {
288 : case PRIO_PROCESS:
289 0 : if (who)
290 0 : p = find_task_by_vpid(who);
291 : else
292 0 : p = current;
293 0 : if (p) {
294 0 : niceval = nice_to_rlimit(task_nice(p));
295 0 : if (niceval > retval)
296 0 : retval = niceval;
297 : }
298 : break;
299 : case PRIO_PGRP:
300 0 : if (who)
301 0 : pgrp = find_vpid(who);
302 : else
303 0 : pgrp = task_pgrp(current);
304 0 : read_lock(&tasklist_lock);
305 0 : do_each_pid_thread(pgrp, PIDTYPE_PGID, p) {
306 0 : niceval = nice_to_rlimit(task_nice(p));
307 0 : if (niceval > retval)
308 0 : retval = niceval;
309 0 : } while_each_pid_thread(pgrp, PIDTYPE_PGID, p);
310 0 : read_unlock(&tasklist_lock);
311 0 : break;
312 : case PRIO_USER:
313 0 : uid = make_kuid(cred->user_ns, who);
314 0 : user = cred->user;
315 0 : if (!who)
316 0 : uid = cred->uid;
317 0 : else if (!uid_eq(uid, cred->uid)) {
318 0 : user = find_user(uid);
319 0 : if (!user)
320 : goto out_unlock; /* No processes for this user */
321 : }
322 0 : for_each_process_thread(g, p) {
323 0 : if (uid_eq(task_uid(p), uid) && task_pid_vnr(p)) {
324 0 : niceval = nice_to_rlimit(task_nice(p));
325 0 : if (niceval > retval)
326 0 : retval = niceval;
327 : }
328 : }
329 0 : if (!uid_eq(uid, cred->uid))
330 0 : free_uid(user); /* for find_user() */
331 : break;
332 : }
333 : out_unlock:
334 : rcu_read_unlock();
335 :
336 0 : return retval;
337 : }
338 :
339 : /*
340 : * Unprivileged users may change the real gid to the effective gid
341 : * or vice versa. (BSD-style)
342 : *
343 : * If you set the real gid at all, or set the effective gid to a value not
344 : * equal to the real gid, then the saved gid is set to the new effective gid.
345 : *
346 : * This makes it possible for a setgid program to completely drop its
347 : * privileges, which is often a useful assertion to make when you are doing
348 : * a security audit over a program.
349 : *
350 : * The general idea is that a program which uses just setregid() will be
351 : * 100% compatible with BSD. A program which uses just setgid() will be
352 : * 100% compatible with POSIX with saved IDs.
353 : *
354 : * SMP: There are not races, the GIDs are checked only by filesystem
355 : * operations (as far as semantic preservation is concerned).
356 : */
357 : #ifdef CONFIG_MULTIUSER
358 0 : long __sys_setregid(gid_t rgid, gid_t egid)
359 : {
360 0 : struct user_namespace *ns = current_user_ns();
361 : const struct cred *old;
362 : struct cred *new;
363 : int retval;
364 : kgid_t krgid, kegid;
365 :
366 0 : krgid = make_kgid(ns, rgid);
367 0 : kegid = make_kgid(ns, egid);
368 :
369 : if ((rgid != (gid_t) -1) && !gid_valid(krgid))
370 : return -EINVAL;
371 : if ((egid != (gid_t) -1) && !gid_valid(kegid))
372 : return -EINVAL;
373 :
374 0 : new = prepare_creds();
375 0 : if (!new)
376 : return -ENOMEM;
377 0 : old = current_cred();
378 :
379 0 : retval = -EPERM;
380 0 : if (rgid != (gid_t) -1) {
381 0 : if (gid_eq(old->gid, krgid) ||
382 0 : gid_eq(old->egid, krgid) ||
383 0 : ns_capable_setid(old->user_ns, CAP_SETGID))
384 0 : new->gid = krgid;
385 : else
386 : goto error;
387 : }
388 0 : if (egid != (gid_t) -1) {
389 0 : if (gid_eq(old->gid, kegid) ||
390 0 : gid_eq(old->egid, kegid) ||
391 0 : gid_eq(old->sgid, kegid) ||
392 0 : ns_capable_setid(old->user_ns, CAP_SETGID))
393 0 : new->egid = kegid;
394 : else
395 : goto error;
396 : }
397 :
398 0 : if (rgid != (gid_t) -1 ||
399 0 : (egid != (gid_t) -1 && !gid_eq(kegid, old->gid)))
400 0 : new->sgid = new->egid;
401 0 : new->fsgid = new->egid;
402 :
403 0 : retval = security_task_fix_setgid(new, old, LSM_SETID_RE);
404 : if (retval < 0)
405 : goto error;
406 :
407 0 : return commit_creds(new);
408 :
409 : error:
410 0 : abort_creds(new);
411 0 : return retval;
412 : }
413 :
414 0 : SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid)
415 : {
416 0 : return __sys_setregid(rgid, egid);
417 : }
418 :
419 : /*
420 : * setgid() is implemented like SysV w/ SAVED_IDS
421 : *
422 : * SMP: Same implicit races as above.
423 : */
424 0 : long __sys_setgid(gid_t gid)
425 : {
426 0 : struct user_namespace *ns = current_user_ns();
427 : const struct cred *old;
428 : struct cred *new;
429 : int retval;
430 : kgid_t kgid;
431 :
432 0 : kgid = make_kgid(ns, gid);
433 0 : if (!gid_valid(kgid))
434 : return -EINVAL;
435 :
436 0 : new = prepare_creds();
437 0 : if (!new)
438 : return -ENOMEM;
439 0 : old = current_cred();
440 :
441 0 : retval = -EPERM;
442 0 : if (ns_capable_setid(old->user_ns, CAP_SETGID))
443 0 : new->gid = new->egid = new->sgid = new->fsgid = kgid;
444 0 : else if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->sgid))
445 0 : new->egid = new->fsgid = kgid;
446 : else
447 : goto error;
448 :
449 0 : retval = security_task_fix_setgid(new, old, LSM_SETID_ID);
450 : if (retval < 0)
451 : goto error;
452 :
453 0 : return commit_creds(new);
454 :
455 : error:
456 0 : abort_creds(new);
457 0 : return retval;
458 : }
459 :
460 0 : SYSCALL_DEFINE1(setgid, gid_t, gid)
461 : {
462 0 : return __sys_setgid(gid);
463 : }
464 :
465 : /*
466 : * change the user struct in a credentials set to match the new UID
467 : */
468 : static int set_user(struct cred *new)
469 : {
470 : struct user_struct *new_user;
471 :
472 0 : new_user = alloc_uid(new->uid);
473 0 : if (!new_user)
474 : return -EAGAIN;
475 :
476 0 : free_uid(new->user);
477 0 : new->user = new_user;
478 : return 0;
479 : }
480 :
481 0 : static void flag_nproc_exceeded(struct cred *new)
482 : {
483 0 : if (new->ucounts == current_ucounts())
484 : return;
485 :
486 : /*
487 : * We don't fail in case of NPROC limit excess here because too many
488 : * poorly written programs don't check set*uid() return code, assuming
489 : * it never fails if called by root. We may still enforce NPROC limit
490 : * for programs doing set*uid()+execve() by harmlessly deferring the
491 : * failure to the execve() stage.
492 : */
493 0 : if (is_ucounts_overlimit(new->ucounts, UCOUNT_RLIMIT_NPROC, rlimit(RLIMIT_NPROC)) &&
494 0 : new->user != INIT_USER)
495 0 : current->flags |= PF_NPROC_EXCEEDED;
496 : else
497 0 : current->flags &= ~PF_NPROC_EXCEEDED;
498 : }
499 :
500 : /*
501 : * Unprivileged users may change the real uid to the effective uid
502 : * or vice versa. (BSD-style)
503 : *
504 : * If you set the real uid at all, or set the effective uid to a value not
505 : * equal to the real uid, then the saved uid is set to the new effective uid.
506 : *
507 : * This makes it possible for a setuid program to completely drop its
508 : * privileges, which is often a useful assertion to make when you are doing
509 : * a security audit over a program.
510 : *
511 : * The general idea is that a program which uses just setreuid() will be
512 : * 100% compatible with BSD. A program which uses just setuid() will be
513 : * 100% compatible with POSIX with saved IDs.
514 : */
515 0 : long __sys_setreuid(uid_t ruid, uid_t euid)
516 : {
517 0 : struct user_namespace *ns = current_user_ns();
518 : const struct cred *old;
519 : struct cred *new;
520 : int retval;
521 : kuid_t kruid, keuid;
522 :
523 0 : kruid = make_kuid(ns, ruid);
524 0 : keuid = make_kuid(ns, euid);
525 :
526 : if ((ruid != (uid_t) -1) && !uid_valid(kruid))
527 : return -EINVAL;
528 : if ((euid != (uid_t) -1) && !uid_valid(keuid))
529 : return -EINVAL;
530 :
531 0 : new = prepare_creds();
532 0 : if (!new)
533 : return -ENOMEM;
534 0 : old = current_cred();
535 :
536 0 : retval = -EPERM;
537 0 : if (ruid != (uid_t) -1) {
538 0 : new->uid = kruid;
539 0 : if (!uid_eq(old->uid, kruid) &&
540 0 : !uid_eq(old->euid, kruid) &&
541 0 : !ns_capable_setid(old->user_ns, CAP_SETUID))
542 : goto error;
543 : }
544 :
545 0 : if (euid != (uid_t) -1) {
546 0 : new->euid = keuid;
547 0 : if (!uid_eq(old->uid, keuid) &&
548 0 : !uid_eq(old->euid, keuid) &&
549 0 : !uid_eq(old->suid, keuid) &&
550 0 : !ns_capable_setid(old->user_ns, CAP_SETUID))
551 : goto error;
552 : }
553 :
554 0 : if (!uid_eq(new->uid, old->uid)) {
555 0 : retval = set_user(new);
556 0 : if (retval < 0)
557 : goto error;
558 : }
559 0 : if (ruid != (uid_t) -1 ||
560 0 : (euid != (uid_t) -1 && !uid_eq(keuid, old->uid)))
561 0 : new->suid = new->euid;
562 0 : new->fsuid = new->euid;
563 :
564 0 : retval = security_task_fix_setuid(new, old, LSM_SETID_RE);
565 0 : if (retval < 0)
566 : goto error;
567 :
568 0 : retval = set_cred_ucounts(new);
569 0 : if (retval < 0)
570 : goto error;
571 :
572 0 : flag_nproc_exceeded(new);
573 0 : return commit_creds(new);
574 :
575 : error:
576 0 : abort_creds(new);
577 0 : return retval;
578 : }
579 :
580 0 : SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid)
581 : {
582 0 : return __sys_setreuid(ruid, euid);
583 : }
584 :
585 : /*
586 : * setuid() is implemented like SysV with SAVED_IDS
587 : *
588 : * Note that SAVED_ID's is deficient in that a setuid root program
589 : * like sendmail, for example, cannot set its uid to be a normal
590 : * user and then switch back, because if you're root, setuid() sets
591 : * the saved uid too. If you don't like this, blame the bright people
592 : * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
593 : * will allow a root program to temporarily drop privileges and be able to
594 : * regain them by swapping the real and effective uid.
595 : */
596 0 : long __sys_setuid(uid_t uid)
597 : {
598 0 : struct user_namespace *ns = current_user_ns();
599 : const struct cred *old;
600 : struct cred *new;
601 : int retval;
602 : kuid_t kuid;
603 :
604 0 : kuid = make_kuid(ns, uid);
605 0 : if (!uid_valid(kuid))
606 : return -EINVAL;
607 :
608 0 : new = prepare_creds();
609 0 : if (!new)
610 : return -ENOMEM;
611 0 : old = current_cred();
612 :
613 0 : retval = -EPERM;
614 0 : if (ns_capable_setid(old->user_ns, CAP_SETUID)) {
615 0 : new->suid = new->uid = kuid;
616 0 : if (!uid_eq(kuid, old->uid)) {
617 0 : retval = set_user(new);
618 0 : if (retval < 0)
619 : goto error;
620 : }
621 0 : } else if (!uid_eq(kuid, old->uid) && !uid_eq(kuid, new->suid)) {
622 : goto error;
623 : }
624 :
625 0 : new->fsuid = new->euid = kuid;
626 :
627 0 : retval = security_task_fix_setuid(new, old, LSM_SETID_ID);
628 0 : if (retval < 0)
629 : goto error;
630 :
631 0 : retval = set_cred_ucounts(new);
632 0 : if (retval < 0)
633 : goto error;
634 :
635 0 : flag_nproc_exceeded(new);
636 0 : return commit_creds(new);
637 :
638 : error:
639 0 : abort_creds(new);
640 0 : return retval;
641 : }
642 :
643 0 : SYSCALL_DEFINE1(setuid, uid_t, uid)
644 : {
645 0 : return __sys_setuid(uid);
646 : }
647 :
648 :
649 : /*
650 : * This function implements a generic ability to update ruid, euid,
651 : * and suid. This allows you to implement the 4.4 compatible seteuid().
652 : */
653 0 : long __sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
654 : {
655 0 : struct user_namespace *ns = current_user_ns();
656 : const struct cred *old;
657 : struct cred *new;
658 : int retval;
659 : kuid_t kruid, keuid, ksuid;
660 :
661 0 : kruid = make_kuid(ns, ruid);
662 0 : keuid = make_kuid(ns, euid);
663 0 : ksuid = make_kuid(ns, suid);
664 :
665 : if ((ruid != (uid_t) -1) && !uid_valid(kruid))
666 : return -EINVAL;
667 :
668 : if ((euid != (uid_t) -1) && !uid_valid(keuid))
669 : return -EINVAL;
670 :
671 : if ((suid != (uid_t) -1) && !uid_valid(ksuid))
672 : return -EINVAL;
673 :
674 0 : new = prepare_creds();
675 0 : if (!new)
676 : return -ENOMEM;
677 :
678 0 : old = current_cred();
679 :
680 0 : retval = -EPERM;
681 0 : if (!ns_capable_setid(old->user_ns, CAP_SETUID)) {
682 0 : if (ruid != (uid_t) -1 && !uid_eq(kruid, old->uid) &&
683 0 : !uid_eq(kruid, old->euid) && !uid_eq(kruid, old->suid))
684 : goto error;
685 0 : if (euid != (uid_t) -1 && !uid_eq(keuid, old->uid) &&
686 0 : !uid_eq(keuid, old->euid) && !uid_eq(keuid, old->suid))
687 : goto error;
688 0 : if (suid != (uid_t) -1 && !uid_eq(ksuid, old->uid) &&
689 0 : !uid_eq(ksuid, old->euid) && !uid_eq(ksuid, old->suid))
690 : goto error;
691 : }
692 :
693 0 : if (ruid != (uid_t) -1) {
694 0 : new->uid = kruid;
695 0 : if (!uid_eq(kruid, old->uid)) {
696 0 : retval = set_user(new);
697 0 : if (retval < 0)
698 : goto error;
699 : }
700 : }
701 0 : if (euid != (uid_t) -1)
702 0 : new->euid = keuid;
703 0 : if (suid != (uid_t) -1)
704 0 : new->suid = ksuid;
705 0 : new->fsuid = new->euid;
706 :
707 0 : retval = security_task_fix_setuid(new, old, LSM_SETID_RES);
708 0 : if (retval < 0)
709 : goto error;
710 :
711 0 : retval = set_cred_ucounts(new);
712 0 : if (retval < 0)
713 : goto error;
714 :
715 0 : flag_nproc_exceeded(new);
716 0 : return commit_creds(new);
717 :
718 : error:
719 0 : abort_creds(new);
720 0 : return retval;
721 : }
722 :
723 0 : SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid)
724 : {
725 0 : return __sys_setresuid(ruid, euid, suid);
726 : }
727 :
728 0 : SYSCALL_DEFINE3(getresuid, uid_t __user *, ruidp, uid_t __user *, euidp, uid_t __user *, suidp)
729 : {
730 0 : const struct cred *cred = current_cred();
731 : int retval;
732 : uid_t ruid, euid, suid;
733 :
734 0 : ruid = from_kuid_munged(cred->user_ns, cred->uid);
735 0 : euid = from_kuid_munged(cred->user_ns, cred->euid);
736 0 : suid = from_kuid_munged(cred->user_ns, cred->suid);
737 :
738 0 : retval = put_user(ruid, ruidp);
739 0 : if (!retval) {
740 0 : retval = put_user(euid, euidp);
741 0 : if (!retval)
742 0 : return put_user(suid, suidp);
743 : }
744 0 : return retval;
745 : }
746 :
747 : /*
748 : * Same as above, but for rgid, egid, sgid.
749 : */
750 0 : long __sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
751 : {
752 0 : struct user_namespace *ns = current_user_ns();
753 : const struct cred *old;
754 : struct cred *new;
755 : int retval;
756 : kgid_t krgid, kegid, ksgid;
757 :
758 0 : krgid = make_kgid(ns, rgid);
759 0 : kegid = make_kgid(ns, egid);
760 0 : ksgid = make_kgid(ns, sgid);
761 :
762 : if ((rgid != (gid_t) -1) && !gid_valid(krgid))
763 : return -EINVAL;
764 : if ((egid != (gid_t) -1) && !gid_valid(kegid))
765 : return -EINVAL;
766 : if ((sgid != (gid_t) -1) && !gid_valid(ksgid))
767 : return -EINVAL;
768 :
769 0 : new = prepare_creds();
770 0 : if (!new)
771 : return -ENOMEM;
772 0 : old = current_cred();
773 :
774 0 : retval = -EPERM;
775 0 : if (!ns_capable_setid(old->user_ns, CAP_SETGID)) {
776 0 : if (rgid != (gid_t) -1 && !gid_eq(krgid, old->gid) &&
777 0 : !gid_eq(krgid, old->egid) && !gid_eq(krgid, old->sgid))
778 : goto error;
779 0 : if (egid != (gid_t) -1 && !gid_eq(kegid, old->gid) &&
780 0 : !gid_eq(kegid, old->egid) && !gid_eq(kegid, old->sgid))
781 : goto error;
782 0 : if (sgid != (gid_t) -1 && !gid_eq(ksgid, old->gid) &&
783 0 : !gid_eq(ksgid, old->egid) && !gid_eq(ksgid, old->sgid))
784 : goto error;
785 : }
786 :
787 0 : if (rgid != (gid_t) -1)
788 0 : new->gid = krgid;
789 0 : if (egid != (gid_t) -1)
790 0 : new->egid = kegid;
791 0 : if (sgid != (gid_t) -1)
792 0 : new->sgid = ksgid;
793 0 : new->fsgid = new->egid;
794 :
795 0 : retval = security_task_fix_setgid(new, old, LSM_SETID_RES);
796 : if (retval < 0)
797 : goto error;
798 :
799 0 : return commit_creds(new);
800 :
801 : error:
802 0 : abort_creds(new);
803 0 : return retval;
804 : }
805 :
806 0 : SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid)
807 : {
808 0 : return __sys_setresgid(rgid, egid, sgid);
809 : }
810 :
811 0 : SYSCALL_DEFINE3(getresgid, gid_t __user *, rgidp, gid_t __user *, egidp, gid_t __user *, sgidp)
812 : {
813 0 : const struct cred *cred = current_cred();
814 : int retval;
815 : gid_t rgid, egid, sgid;
816 :
817 0 : rgid = from_kgid_munged(cred->user_ns, cred->gid);
818 0 : egid = from_kgid_munged(cred->user_ns, cred->egid);
819 0 : sgid = from_kgid_munged(cred->user_ns, cred->sgid);
820 :
821 0 : retval = put_user(rgid, rgidp);
822 0 : if (!retval) {
823 0 : retval = put_user(egid, egidp);
824 0 : if (!retval)
825 0 : retval = put_user(sgid, sgidp);
826 : }
827 :
828 0 : return retval;
829 : }
830 :
831 :
832 : /*
833 : * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
834 : * is used for "access()" and for the NFS daemon (letting nfsd stay at
835 : * whatever uid it wants to). It normally shadows "euid", except when
836 : * explicitly set by setfsuid() or for access..
837 : */
838 0 : long __sys_setfsuid(uid_t uid)
839 : {
840 : const struct cred *old;
841 : struct cred *new;
842 : uid_t old_fsuid;
843 : kuid_t kuid;
844 :
845 0 : old = current_cred();
846 0 : old_fsuid = from_kuid_munged(old->user_ns, old->fsuid);
847 :
848 0 : kuid = make_kuid(old->user_ns, uid);
849 0 : if (!uid_valid(kuid))
850 0 : return old_fsuid;
851 :
852 0 : new = prepare_creds();
853 0 : if (!new)
854 0 : return old_fsuid;
855 :
856 0 : if (uid_eq(kuid, old->uid) || uid_eq(kuid, old->euid) ||
857 0 : uid_eq(kuid, old->suid) || uid_eq(kuid, old->fsuid) ||
858 0 : ns_capable_setid(old->user_ns, CAP_SETUID)) {
859 0 : if (!uid_eq(kuid, old->fsuid)) {
860 0 : new->fsuid = kuid;
861 0 : if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0)
862 : goto change_okay;
863 : }
864 : }
865 :
866 0 : abort_creds(new);
867 0 : return old_fsuid;
868 :
869 : change_okay:
870 0 : commit_creds(new);
871 0 : return old_fsuid;
872 : }
873 :
874 0 : SYSCALL_DEFINE1(setfsuid, uid_t, uid)
875 : {
876 0 : return __sys_setfsuid(uid);
877 : }
878 :
879 : /*
880 : * Samma på svenska..
881 : */
882 0 : long __sys_setfsgid(gid_t gid)
883 : {
884 : const struct cred *old;
885 : struct cred *new;
886 : gid_t old_fsgid;
887 : kgid_t kgid;
888 :
889 0 : old = current_cred();
890 0 : old_fsgid = from_kgid_munged(old->user_ns, old->fsgid);
891 :
892 0 : kgid = make_kgid(old->user_ns, gid);
893 0 : if (!gid_valid(kgid))
894 0 : return old_fsgid;
895 :
896 0 : new = prepare_creds();
897 0 : if (!new)
898 0 : return old_fsgid;
899 :
900 0 : if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->egid) ||
901 0 : gid_eq(kgid, old->sgid) || gid_eq(kgid, old->fsgid) ||
902 0 : ns_capable_setid(old->user_ns, CAP_SETGID)) {
903 0 : if (!gid_eq(kgid, old->fsgid)) {
904 0 : new->fsgid = kgid;
905 0 : if (security_task_fix_setgid(new,old,LSM_SETID_FS) == 0)
906 : goto change_okay;
907 : }
908 : }
909 :
910 0 : abort_creds(new);
911 0 : return old_fsgid;
912 :
913 : change_okay:
914 0 : commit_creds(new);
915 0 : return old_fsgid;
916 : }
917 :
918 0 : SYSCALL_DEFINE1(setfsgid, gid_t, gid)
919 : {
920 0 : return __sys_setfsgid(gid);
921 : }
922 : #endif /* CONFIG_MULTIUSER */
923 :
924 : /**
925 : * sys_getpid - return the thread group id of the current process
926 : *
927 : * Note, despite the name, this returns the tgid not the pid. The tgid and
928 : * the pid are identical unless CLONE_THREAD was specified on clone() in
929 : * which case the tgid is the same in all threads of the same group.
930 : *
931 : * This is SMP safe as current->tgid does not change.
932 : */
933 0 : SYSCALL_DEFINE0(getpid)
934 : {
935 0 : return task_tgid_vnr(current);
936 : }
937 :
938 : /* Thread ID - the internal kernel "pid" */
939 0 : SYSCALL_DEFINE0(gettid)
940 : {
941 0 : return task_pid_vnr(current);
942 : }
943 :
944 : /*
945 : * Accessing ->real_parent is not SMP-safe, it could
946 : * change from under us. However, we can use a stale
947 : * value of ->real_parent under rcu_read_lock(), see
948 : * release_task()->call_rcu(delayed_put_task_struct).
949 : */
950 0 : SYSCALL_DEFINE0(getppid)
951 : {
952 : int pid;
953 :
954 : rcu_read_lock();
955 0 : pid = task_tgid_vnr(rcu_dereference(current->real_parent));
956 : rcu_read_unlock();
957 :
958 0 : return pid;
959 : }
960 :
961 0 : SYSCALL_DEFINE0(getuid)
962 : {
963 : /* Only we change this so SMP safe */
964 0 : return from_kuid_munged(current_user_ns(), current_uid());
965 : }
966 :
967 0 : SYSCALL_DEFINE0(geteuid)
968 : {
969 : /* Only we change this so SMP safe */
970 0 : return from_kuid_munged(current_user_ns(), current_euid());
971 : }
972 :
973 0 : SYSCALL_DEFINE0(getgid)
974 : {
975 : /* Only we change this so SMP safe */
976 0 : return from_kgid_munged(current_user_ns(), current_gid());
977 : }
978 :
979 0 : SYSCALL_DEFINE0(getegid)
980 : {
981 : /* Only we change this so SMP safe */
982 0 : return from_kgid_munged(current_user_ns(), current_egid());
983 : }
984 :
985 0 : static void do_sys_times(struct tms *tms)
986 : {
987 : u64 tgutime, tgstime, cutime, cstime;
988 :
989 0 : thread_group_cputime_adjusted(current, &tgutime, &tgstime);
990 0 : cutime = current->signal->cutime;
991 0 : cstime = current->signal->cstime;
992 0 : tms->tms_utime = nsec_to_clock_t(tgutime);
993 0 : tms->tms_stime = nsec_to_clock_t(tgstime);
994 0 : tms->tms_cutime = nsec_to_clock_t(cutime);
995 0 : tms->tms_cstime = nsec_to_clock_t(cstime);
996 0 : }
997 :
998 0 : SYSCALL_DEFINE1(times, struct tms __user *, tbuf)
999 : {
1000 0 : if (tbuf) {
1001 : struct tms tmp;
1002 :
1003 0 : do_sys_times(&tmp);
1004 0 : if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1005 0 : return -EFAULT;
1006 : }
1007 : force_successful_syscall_return();
1008 0 : return (long) jiffies_64_to_clock_t(get_jiffies_64());
1009 : }
1010 :
1011 : #ifdef CONFIG_COMPAT
1012 : static compat_clock_t clock_t_to_compat_clock_t(clock_t x)
1013 : {
1014 : return compat_jiffies_to_clock_t(clock_t_to_jiffies(x));
1015 : }
1016 :
1017 : COMPAT_SYSCALL_DEFINE1(times, struct compat_tms __user *, tbuf)
1018 : {
1019 : if (tbuf) {
1020 : struct tms tms;
1021 : struct compat_tms tmp;
1022 :
1023 : do_sys_times(&tms);
1024 : /* Convert our struct tms to the compat version. */
1025 : tmp.tms_utime = clock_t_to_compat_clock_t(tms.tms_utime);
1026 : tmp.tms_stime = clock_t_to_compat_clock_t(tms.tms_stime);
1027 : tmp.tms_cutime = clock_t_to_compat_clock_t(tms.tms_cutime);
1028 : tmp.tms_cstime = clock_t_to_compat_clock_t(tms.tms_cstime);
1029 : if (copy_to_user(tbuf, &tmp, sizeof(tmp)))
1030 : return -EFAULT;
1031 : }
1032 : force_successful_syscall_return();
1033 : return compat_jiffies_to_clock_t(jiffies);
1034 : }
1035 : #endif
1036 :
1037 : /*
1038 : * This needs some heavy checking ...
1039 : * I just haven't the stomach for it. I also don't fully
1040 : * understand sessions/pgrp etc. Let somebody who does explain it.
1041 : *
1042 : * OK, I think I have the protection semantics right.... this is really
1043 : * only important on a multi-user system anyway, to make sure one user
1044 : * can't send a signal to a process owned by another. -TYT, 12/12/91
1045 : *
1046 : * !PF_FORKNOEXEC check to conform completely to POSIX.
1047 : */
1048 0 : SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid)
1049 : {
1050 : struct task_struct *p;
1051 0 : struct task_struct *group_leader = current->group_leader;
1052 : struct pid *pgrp;
1053 : int err;
1054 :
1055 0 : if (!pid)
1056 0 : pid = task_pid_vnr(group_leader);
1057 0 : if (!pgid)
1058 0 : pgid = pid;
1059 0 : if (pgid < 0)
1060 : return -EINVAL;
1061 : rcu_read_lock();
1062 :
1063 : /* From this point forward we keep holding onto the tasklist lock
1064 : * so that our parent does not change from under us. -DaveM
1065 : */
1066 0 : write_lock_irq(&tasklist_lock);
1067 :
1068 0 : err = -ESRCH;
1069 0 : p = find_task_by_vpid(pid);
1070 0 : if (!p)
1071 : goto out;
1072 :
1073 0 : err = -EINVAL;
1074 0 : if (!thread_group_leader(p))
1075 : goto out;
1076 :
1077 0 : if (same_thread_group(p->real_parent, group_leader)) {
1078 0 : err = -EPERM;
1079 0 : if (task_session(p) != task_session(group_leader))
1080 : goto out;
1081 0 : err = -EACCES;
1082 0 : if (!(p->flags & PF_FORKNOEXEC))
1083 : goto out;
1084 : } else {
1085 0 : err = -ESRCH;
1086 0 : if (p != group_leader)
1087 : goto out;
1088 : }
1089 :
1090 0 : err = -EPERM;
1091 0 : if (p->signal->leader)
1092 : goto out;
1093 :
1094 0 : pgrp = task_pid(p);
1095 0 : if (pgid != pid) {
1096 : struct task_struct *g;
1097 :
1098 0 : pgrp = find_vpid(pgid);
1099 0 : g = pid_task(pgrp, PIDTYPE_PGID);
1100 0 : if (!g || task_session(g) != task_session(group_leader))
1101 : goto out;
1102 : }
1103 :
1104 0 : err = security_task_setpgid(p, pgid);
1105 : if (err)
1106 : goto out;
1107 :
1108 0 : if (task_pgrp(p) != pgrp)
1109 0 : change_pid(p, PIDTYPE_PGID, pgrp);
1110 :
1111 : err = 0;
1112 : out:
1113 : /* All paths lead to here, thus we are safe. -DaveM */
1114 0 : write_unlock_irq(&tasklist_lock);
1115 : rcu_read_unlock();
1116 0 : return err;
1117 : }
1118 :
1119 0 : static int do_getpgid(pid_t pid)
1120 : {
1121 : struct task_struct *p;
1122 : struct pid *grp;
1123 : int retval;
1124 :
1125 : rcu_read_lock();
1126 0 : if (!pid)
1127 0 : grp = task_pgrp(current);
1128 : else {
1129 0 : retval = -ESRCH;
1130 0 : p = find_task_by_vpid(pid);
1131 0 : if (!p)
1132 : goto out;
1133 0 : grp = task_pgrp(p);
1134 0 : if (!grp)
1135 : goto out;
1136 :
1137 : retval = security_task_getpgid(p);
1138 : if (retval)
1139 : goto out;
1140 : }
1141 0 : retval = pid_vnr(grp);
1142 : out:
1143 : rcu_read_unlock();
1144 0 : return retval;
1145 : }
1146 :
1147 0 : SYSCALL_DEFINE1(getpgid, pid_t, pid)
1148 : {
1149 0 : return do_getpgid(pid);
1150 : }
1151 :
1152 : #ifdef __ARCH_WANT_SYS_GETPGRP
1153 :
1154 0 : SYSCALL_DEFINE0(getpgrp)
1155 : {
1156 0 : return do_getpgid(0);
1157 : }
1158 :
1159 : #endif
1160 :
1161 0 : SYSCALL_DEFINE1(getsid, pid_t, pid)
1162 : {
1163 : struct task_struct *p;
1164 : struct pid *sid;
1165 : int retval;
1166 :
1167 : rcu_read_lock();
1168 0 : if (!pid)
1169 0 : sid = task_session(current);
1170 : else {
1171 0 : retval = -ESRCH;
1172 0 : p = find_task_by_vpid(pid);
1173 0 : if (!p)
1174 : goto out;
1175 0 : sid = task_session(p);
1176 0 : if (!sid)
1177 : goto out;
1178 :
1179 : retval = security_task_getsid(p);
1180 : if (retval)
1181 : goto out;
1182 : }
1183 0 : retval = pid_vnr(sid);
1184 : out:
1185 : rcu_read_unlock();
1186 0 : return retval;
1187 : }
1188 :
1189 0 : static void set_special_pids(struct pid *pid)
1190 : {
1191 0 : struct task_struct *curr = current->group_leader;
1192 :
1193 0 : if (task_session(curr) != pid)
1194 0 : change_pid(curr, PIDTYPE_SID, pid);
1195 :
1196 0 : if (task_pgrp(curr) != pid)
1197 0 : change_pid(curr, PIDTYPE_PGID, pid);
1198 0 : }
1199 :
1200 0 : int ksys_setsid(void)
1201 : {
1202 0 : struct task_struct *group_leader = current->group_leader;
1203 0 : struct pid *sid = task_pid(group_leader);
1204 0 : pid_t session = pid_vnr(sid);
1205 0 : int err = -EPERM;
1206 :
1207 0 : write_lock_irq(&tasklist_lock);
1208 : /* Fail if I am already a session leader */
1209 0 : if (group_leader->signal->leader)
1210 : goto out;
1211 :
1212 : /* Fail if a process group id already exists that equals the
1213 : * proposed session id.
1214 : */
1215 0 : if (pid_task(sid, PIDTYPE_PGID))
1216 : goto out;
1217 :
1218 0 : group_leader->signal->leader = 1;
1219 0 : set_special_pids(sid);
1220 :
1221 0 : proc_clear_tty(group_leader);
1222 :
1223 0 : err = session;
1224 : out:
1225 0 : write_unlock_irq(&tasklist_lock);
1226 : if (err > 0) {
1227 : proc_sid_connector(group_leader);
1228 : sched_autogroup_create_attach(group_leader);
1229 : }
1230 0 : return err;
1231 : }
1232 :
1233 0 : SYSCALL_DEFINE0(setsid)
1234 : {
1235 0 : return ksys_setsid();
1236 : }
1237 :
1238 : DECLARE_RWSEM(uts_sem);
1239 :
1240 : #ifdef COMPAT_UTS_MACHINE
1241 : #define override_architecture(name) \
1242 : (personality(current->personality) == PER_LINUX32 && \
1243 : copy_to_user(name->machine, COMPAT_UTS_MACHINE, \
1244 : sizeof(COMPAT_UTS_MACHINE)))
1245 : #else
1246 : #define override_architecture(name) 0
1247 : #endif
1248 :
1249 : /*
1250 : * Work around broken programs that cannot handle "Linux 3.0".
1251 : * Instead we map 3.x to 2.6.40+x, so e.g. 3.0 would be 2.6.40
1252 : * And we map 4.x and later versions to 2.6.60+x, so 4.0/5.0/6.0/... would be
1253 : * 2.6.60.
1254 : */
1255 0 : static int override_release(char __user *release, size_t len)
1256 : {
1257 0 : int ret = 0;
1258 :
1259 0 : if (current->personality & UNAME26) {
1260 0 : const char *rest = UTS_RELEASE;
1261 0 : char buf[65] = { 0 };
1262 0 : int ndots = 0;
1263 : unsigned v;
1264 : size_t copy;
1265 :
1266 0 : while (*rest) {
1267 0 : if (*rest == '.' && ++ndots >= 3)
1268 : break;
1269 0 : if (!isdigit(*rest) && *rest != '.')
1270 : break;
1271 0 : rest++;
1272 : }
1273 0 : v = LINUX_VERSION_PATCHLEVEL + 60;
1274 0 : copy = clamp_t(size_t, len, 1, sizeof(buf));
1275 0 : copy = scnprintf(buf, copy, "2.6.%u%s", v, rest);
1276 0 : ret = copy_to_user(release, buf, copy + 1);
1277 : }
1278 0 : return ret;
1279 : }
1280 :
1281 0 : SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name)
1282 : {
1283 : struct new_utsname tmp;
1284 :
1285 0 : down_read(&uts_sem);
1286 0 : memcpy(&tmp, utsname(), sizeof(tmp));
1287 0 : up_read(&uts_sem);
1288 0 : if (copy_to_user(name, &tmp, sizeof(tmp)))
1289 : return -EFAULT;
1290 :
1291 0 : if (override_release(name->release, sizeof(name->release)))
1292 : return -EFAULT;
1293 : if (override_architecture(name))
1294 : return -EFAULT;
1295 0 : return 0;
1296 : }
1297 :
1298 : #ifdef __ARCH_WANT_SYS_OLD_UNAME
1299 : /*
1300 : * Old cruft
1301 : */
1302 0 : SYSCALL_DEFINE1(uname, struct old_utsname __user *, name)
1303 : {
1304 : struct old_utsname tmp;
1305 :
1306 0 : if (!name)
1307 : return -EFAULT;
1308 :
1309 0 : down_read(&uts_sem);
1310 0 : memcpy(&tmp, utsname(), sizeof(tmp));
1311 0 : up_read(&uts_sem);
1312 0 : if (copy_to_user(name, &tmp, sizeof(tmp)))
1313 : return -EFAULT;
1314 :
1315 0 : if (override_release(name->release, sizeof(name->release)))
1316 : return -EFAULT;
1317 : if (override_architecture(name))
1318 : return -EFAULT;
1319 0 : return 0;
1320 : }
1321 :
1322 0 : SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name)
1323 : {
1324 : struct oldold_utsname tmp;
1325 :
1326 0 : if (!name)
1327 : return -EFAULT;
1328 :
1329 0 : memset(&tmp, 0, sizeof(tmp));
1330 :
1331 0 : down_read(&uts_sem);
1332 0 : memcpy(&tmp.sysname, &utsname()->sysname, __OLD_UTS_LEN);
1333 0 : memcpy(&tmp.nodename, &utsname()->nodename, __OLD_UTS_LEN);
1334 0 : memcpy(&tmp.release, &utsname()->release, __OLD_UTS_LEN);
1335 0 : memcpy(&tmp.version, &utsname()->version, __OLD_UTS_LEN);
1336 0 : memcpy(&tmp.machine, &utsname()->machine, __OLD_UTS_LEN);
1337 0 : up_read(&uts_sem);
1338 0 : if (copy_to_user(name, &tmp, sizeof(tmp)))
1339 : return -EFAULT;
1340 :
1341 : if (override_architecture(name))
1342 : return -EFAULT;
1343 0 : if (override_release(name->release, sizeof(name->release)))
1344 : return -EFAULT;
1345 0 : return 0;
1346 : }
1347 : #endif
1348 :
1349 0 : SYSCALL_DEFINE2(sethostname, char __user *, name, int, len)
1350 : {
1351 : int errno;
1352 : char tmp[__NEW_UTS_LEN];
1353 :
1354 0 : if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
1355 : return -EPERM;
1356 :
1357 0 : if (len < 0 || len > __NEW_UTS_LEN)
1358 : return -EINVAL;
1359 0 : errno = -EFAULT;
1360 0 : if (!copy_from_user(tmp, name, len)) {
1361 : struct new_utsname *u;
1362 :
1363 0 : down_write(&uts_sem);
1364 0 : u = utsname();
1365 0 : memcpy(u->nodename, tmp, len);
1366 0 : memset(u->nodename + len, 0, sizeof(u->nodename) - len);
1367 0 : errno = 0;
1368 0 : uts_proc_notify(UTS_PROC_HOSTNAME);
1369 0 : up_write(&uts_sem);
1370 : }
1371 0 : return errno;
1372 : }
1373 :
1374 : #ifdef __ARCH_WANT_SYS_GETHOSTNAME
1375 :
1376 0 : SYSCALL_DEFINE2(gethostname, char __user *, name, int, len)
1377 : {
1378 : int i;
1379 : struct new_utsname *u;
1380 : char tmp[__NEW_UTS_LEN + 1];
1381 :
1382 0 : if (len < 0)
1383 : return -EINVAL;
1384 0 : down_read(&uts_sem);
1385 0 : u = utsname();
1386 0 : i = 1 + strlen(u->nodename);
1387 0 : if (i > len)
1388 0 : i = len;
1389 0 : memcpy(tmp, u->nodename, i);
1390 0 : up_read(&uts_sem);
1391 0 : if (copy_to_user(name, tmp, i))
1392 : return -EFAULT;
1393 0 : return 0;
1394 : }
1395 :
1396 : #endif
1397 :
1398 : /*
1399 : * Only setdomainname; getdomainname can be implemented by calling
1400 : * uname()
1401 : */
1402 0 : SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len)
1403 : {
1404 : int errno;
1405 : char tmp[__NEW_UTS_LEN];
1406 :
1407 0 : if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN))
1408 : return -EPERM;
1409 0 : if (len < 0 || len > __NEW_UTS_LEN)
1410 : return -EINVAL;
1411 :
1412 0 : errno = -EFAULT;
1413 0 : if (!copy_from_user(tmp, name, len)) {
1414 : struct new_utsname *u;
1415 :
1416 0 : down_write(&uts_sem);
1417 0 : u = utsname();
1418 0 : memcpy(u->domainname, tmp, len);
1419 0 : memset(u->domainname + len, 0, sizeof(u->domainname) - len);
1420 0 : errno = 0;
1421 0 : uts_proc_notify(UTS_PROC_DOMAINNAME);
1422 0 : up_write(&uts_sem);
1423 : }
1424 0 : return errno;
1425 : }
1426 :
1427 : /* make sure you are allowed to change @tsk limits before calling this */
1428 0 : static int do_prlimit(struct task_struct *tsk, unsigned int resource,
1429 : struct rlimit *new_rlim, struct rlimit *old_rlim)
1430 : {
1431 : struct rlimit *rlim;
1432 0 : int retval = 0;
1433 :
1434 0 : if (resource >= RLIM_NLIMITS)
1435 : return -EINVAL;
1436 0 : if (new_rlim) {
1437 0 : if (new_rlim->rlim_cur > new_rlim->rlim_max)
1438 : return -EINVAL;
1439 0 : if (resource == RLIMIT_NOFILE &&
1440 0 : new_rlim->rlim_max > sysctl_nr_open)
1441 : return -EPERM;
1442 : }
1443 :
1444 : /* Holding a refcount on tsk protects tsk->signal from disappearing. */
1445 0 : rlim = tsk->signal->rlim + resource;
1446 0 : task_lock(tsk->group_leader);
1447 0 : if (new_rlim) {
1448 : /*
1449 : * Keep the capable check against init_user_ns until cgroups can
1450 : * contain all limits.
1451 : */
1452 0 : if (new_rlim->rlim_max > rlim->rlim_max &&
1453 0 : !capable(CAP_SYS_RESOURCE))
1454 0 : retval = -EPERM;
1455 0 : if (!retval)
1456 0 : retval = security_task_setrlimit(tsk, resource, new_rlim);
1457 : }
1458 0 : if (!retval) {
1459 0 : if (old_rlim)
1460 0 : *old_rlim = *rlim;
1461 0 : if (new_rlim)
1462 0 : *rlim = *new_rlim;
1463 : }
1464 0 : task_unlock(tsk->group_leader);
1465 :
1466 : /*
1467 : * RLIMIT_CPU handling. Arm the posix CPU timer if the limit is not
1468 : * infinite. In case of RLIM_INFINITY the posix CPU timer code
1469 : * ignores the rlimit.
1470 : */
1471 0 : if (!retval && new_rlim && resource == RLIMIT_CPU &&
1472 0 : new_rlim->rlim_cur != RLIM_INFINITY &&
1473 : IS_ENABLED(CONFIG_POSIX_TIMERS)) {
1474 : /*
1475 : * update_rlimit_cpu can fail if the task is exiting, but there
1476 : * may be other tasks in the thread group that are not exiting,
1477 : * and they need their cpu timers adjusted.
1478 : *
1479 : * The group_leader is the last task to be released, so if we
1480 : * cannot update_rlimit_cpu on it, then the entire process is
1481 : * exiting and we do not need to update at all.
1482 : */
1483 0 : update_rlimit_cpu(tsk->group_leader, new_rlim->rlim_cur);
1484 : }
1485 :
1486 : return retval;
1487 : }
1488 :
1489 0 : SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1490 : {
1491 : struct rlimit value;
1492 : int ret;
1493 :
1494 0 : ret = do_prlimit(current, resource, NULL, &value);
1495 0 : if (!ret)
1496 0 : ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1497 :
1498 0 : return ret;
1499 : }
1500 :
1501 : #ifdef CONFIG_COMPAT
1502 :
1503 : COMPAT_SYSCALL_DEFINE2(setrlimit, unsigned int, resource,
1504 : struct compat_rlimit __user *, rlim)
1505 : {
1506 : struct rlimit r;
1507 : struct compat_rlimit r32;
1508 :
1509 : if (copy_from_user(&r32, rlim, sizeof(struct compat_rlimit)))
1510 : return -EFAULT;
1511 :
1512 : if (r32.rlim_cur == COMPAT_RLIM_INFINITY)
1513 : r.rlim_cur = RLIM_INFINITY;
1514 : else
1515 : r.rlim_cur = r32.rlim_cur;
1516 : if (r32.rlim_max == COMPAT_RLIM_INFINITY)
1517 : r.rlim_max = RLIM_INFINITY;
1518 : else
1519 : r.rlim_max = r32.rlim_max;
1520 : return do_prlimit(current, resource, &r, NULL);
1521 : }
1522 :
1523 : COMPAT_SYSCALL_DEFINE2(getrlimit, unsigned int, resource,
1524 : struct compat_rlimit __user *, rlim)
1525 : {
1526 : struct rlimit r;
1527 : int ret;
1528 :
1529 : ret = do_prlimit(current, resource, NULL, &r);
1530 : if (!ret) {
1531 : struct compat_rlimit r32;
1532 : if (r.rlim_cur > COMPAT_RLIM_INFINITY)
1533 : r32.rlim_cur = COMPAT_RLIM_INFINITY;
1534 : else
1535 : r32.rlim_cur = r.rlim_cur;
1536 : if (r.rlim_max > COMPAT_RLIM_INFINITY)
1537 : r32.rlim_max = COMPAT_RLIM_INFINITY;
1538 : else
1539 : r32.rlim_max = r.rlim_max;
1540 :
1541 : if (copy_to_user(rlim, &r32, sizeof(struct compat_rlimit)))
1542 : return -EFAULT;
1543 : }
1544 : return ret;
1545 : }
1546 :
1547 : #endif
1548 :
1549 : #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1550 :
1551 : /*
1552 : * Back compatibility for getrlimit. Needed for some apps.
1553 : */
1554 0 : SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1555 : struct rlimit __user *, rlim)
1556 : {
1557 : struct rlimit x;
1558 0 : if (resource >= RLIM_NLIMITS)
1559 : return -EINVAL;
1560 :
1561 0 : resource = array_index_nospec(resource, RLIM_NLIMITS);
1562 0 : task_lock(current->group_leader);
1563 0 : x = current->signal->rlim[resource];
1564 0 : task_unlock(current->group_leader);
1565 0 : if (x.rlim_cur > 0x7FFFFFFF)
1566 0 : x.rlim_cur = 0x7FFFFFFF;
1567 0 : if (x.rlim_max > 0x7FFFFFFF)
1568 0 : x.rlim_max = 0x7FFFFFFF;
1569 0 : return copy_to_user(rlim, &x, sizeof(x)) ? -EFAULT : 0;
1570 : }
1571 :
1572 : #ifdef CONFIG_COMPAT
1573 : COMPAT_SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource,
1574 : struct compat_rlimit __user *, rlim)
1575 : {
1576 : struct rlimit r;
1577 :
1578 : if (resource >= RLIM_NLIMITS)
1579 : return -EINVAL;
1580 :
1581 : resource = array_index_nospec(resource, RLIM_NLIMITS);
1582 : task_lock(current->group_leader);
1583 : r = current->signal->rlim[resource];
1584 : task_unlock(current->group_leader);
1585 : if (r.rlim_cur > 0x7FFFFFFF)
1586 : r.rlim_cur = 0x7FFFFFFF;
1587 : if (r.rlim_max > 0x7FFFFFFF)
1588 : r.rlim_max = 0x7FFFFFFF;
1589 :
1590 : if (put_user(r.rlim_cur, &rlim->rlim_cur) ||
1591 : put_user(r.rlim_max, &rlim->rlim_max))
1592 : return -EFAULT;
1593 : return 0;
1594 : }
1595 : #endif
1596 :
1597 : #endif
1598 :
1599 : static inline bool rlim64_is_infinity(__u64 rlim64)
1600 : {
1601 : #if BITS_PER_LONG < 64
1602 : return rlim64 >= ULONG_MAX;
1603 : #else
1604 : return rlim64 == RLIM64_INFINITY;
1605 : #endif
1606 : }
1607 :
1608 : static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64)
1609 : {
1610 0 : if (rlim->rlim_cur == RLIM_INFINITY)
1611 0 : rlim64->rlim_cur = RLIM64_INFINITY;
1612 : else
1613 0 : rlim64->rlim_cur = rlim->rlim_cur;
1614 0 : if (rlim->rlim_max == RLIM_INFINITY)
1615 0 : rlim64->rlim_max = RLIM64_INFINITY;
1616 : else
1617 0 : rlim64->rlim_max = rlim->rlim_max;
1618 : }
1619 :
1620 : static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim)
1621 : {
1622 0 : if (rlim64_is_infinity(rlim64->rlim_cur))
1623 0 : rlim->rlim_cur = RLIM_INFINITY;
1624 : else
1625 0 : rlim->rlim_cur = (unsigned long)rlim64->rlim_cur;
1626 0 : if (rlim64_is_infinity(rlim64->rlim_max))
1627 0 : rlim->rlim_max = RLIM_INFINITY;
1628 : else
1629 0 : rlim->rlim_max = (unsigned long)rlim64->rlim_max;
1630 : }
1631 :
1632 : /* rcu lock must be held */
1633 0 : static int check_prlimit_permission(struct task_struct *task,
1634 : unsigned int flags)
1635 : {
1636 0 : const struct cred *cred = current_cred(), *tcred;
1637 : bool id_match;
1638 :
1639 0 : if (current == task)
1640 : return 0;
1641 :
1642 0 : tcred = __task_cred(task);
1643 0 : id_match = (uid_eq(cred->uid, tcred->euid) &&
1644 0 : uid_eq(cred->uid, tcred->suid) &&
1645 0 : uid_eq(cred->uid, tcred->uid) &&
1646 0 : gid_eq(cred->gid, tcred->egid) &&
1647 0 : gid_eq(cred->gid, tcred->sgid) &&
1648 0 : gid_eq(cred->gid, tcred->gid));
1649 0 : if (!id_match && !ns_capable(tcred->user_ns, CAP_SYS_RESOURCE))
1650 : return -EPERM;
1651 :
1652 : return security_task_prlimit(cred, tcred, flags);
1653 : }
1654 :
1655 0 : SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource,
1656 : const struct rlimit64 __user *, new_rlim,
1657 : struct rlimit64 __user *, old_rlim)
1658 : {
1659 : struct rlimit64 old64, new64;
1660 : struct rlimit old, new;
1661 : struct task_struct *tsk;
1662 0 : unsigned int checkflags = 0;
1663 : int ret;
1664 :
1665 : if (old_rlim)
1666 : checkflags |= LSM_PRLIMIT_READ;
1667 :
1668 0 : if (new_rlim) {
1669 0 : if (copy_from_user(&new64, new_rlim, sizeof(new64)))
1670 : return -EFAULT;
1671 0 : rlim64_to_rlim(&new64, &new);
1672 0 : checkflags |= LSM_PRLIMIT_WRITE;
1673 : }
1674 :
1675 : rcu_read_lock();
1676 0 : tsk = pid ? find_task_by_vpid(pid) : current;
1677 0 : if (!tsk) {
1678 : rcu_read_unlock();
1679 0 : return -ESRCH;
1680 : }
1681 0 : ret = check_prlimit_permission(tsk, checkflags);
1682 0 : if (ret) {
1683 : rcu_read_unlock();
1684 0 : return ret;
1685 : }
1686 0 : get_task_struct(tsk);
1687 : rcu_read_unlock();
1688 :
1689 0 : ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL,
1690 : old_rlim ? &old : NULL);
1691 :
1692 0 : if (!ret && old_rlim) {
1693 0 : rlim_to_rlim64(&old, &old64);
1694 0 : if (copy_to_user(old_rlim, &old64, sizeof(old64)))
1695 0 : ret = -EFAULT;
1696 : }
1697 :
1698 0 : put_task_struct(tsk);
1699 0 : return ret;
1700 : }
1701 :
1702 0 : SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim)
1703 : {
1704 : struct rlimit new_rlim;
1705 :
1706 0 : if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1707 : return -EFAULT;
1708 0 : return do_prlimit(current, resource, &new_rlim, NULL);
1709 : }
1710 :
1711 : /*
1712 : * It would make sense to put struct rusage in the task_struct,
1713 : * except that would make the task_struct be *really big*. After
1714 : * task_struct gets moved into malloc'ed memory, it would
1715 : * make sense to do this. It will make moving the rest of the information
1716 : * a lot simpler! (Which we're not doing right now because we're not
1717 : * measuring them yet).
1718 : *
1719 : * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1720 : * races with threads incrementing their own counters. But since word
1721 : * reads are atomic, we either get new values or old values and we don't
1722 : * care which for the sums. We always take the siglock to protect reading
1723 : * the c* fields from p->signal from races with exit.c updating those
1724 : * fields when reaping, so a sample either gets all the additions of a
1725 : * given child after it's reaped, or none so this sample is before reaping.
1726 : *
1727 : * Locking:
1728 : * We need to take the siglock for CHILDEREN, SELF and BOTH
1729 : * for the cases current multithreaded, non-current single threaded
1730 : * non-current multithreaded. Thread traversal is now safe with
1731 : * the siglock held.
1732 : * Strictly speaking, we donot need to take the siglock if we are current and
1733 : * single threaded, as no one else can take our signal_struct away, no one
1734 : * else can reap the children to update signal->c* counters, and no one else
1735 : * can race with the signal-> fields. If we do not take any lock, the
1736 : * signal-> fields could be read out of order while another thread was just
1737 : * exiting. So we should place a read memory barrier when we avoid the lock.
1738 : * On the writer side, write memory barrier is implied in __exit_signal
1739 : * as __exit_signal releases the siglock spinlock after updating the signal->
1740 : * fields. But we don't do this yet to keep things simple.
1741 : *
1742 : */
1743 :
1744 : static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r)
1745 : {
1746 0 : r->ru_nvcsw += t->nvcsw;
1747 0 : r->ru_nivcsw += t->nivcsw;
1748 0 : r->ru_minflt += t->min_flt;
1749 0 : r->ru_majflt += t->maj_flt;
1750 0 : r->ru_inblock += task_io_get_inblock(t);
1751 0 : r->ru_oublock += task_io_get_oublock(t);
1752 : }
1753 :
1754 0 : void getrusage(struct task_struct *p, int who, struct rusage *r)
1755 : {
1756 : struct task_struct *t;
1757 : unsigned long flags;
1758 : u64 tgutime, tgstime, utime, stime;
1759 0 : unsigned long maxrss = 0;
1760 :
1761 0 : memset((char *)r, 0, sizeof (*r));
1762 0 : utime = stime = 0;
1763 :
1764 0 : if (who == RUSAGE_THREAD) {
1765 0 : task_cputime_adjusted(current, &utime, &stime);
1766 0 : accumulate_thread_rusage(p, r);
1767 0 : maxrss = p->signal->maxrss;
1768 0 : goto out;
1769 : }
1770 :
1771 0 : if (!lock_task_sighand(p, &flags))
1772 0 : return;
1773 :
1774 0 : switch (who) {
1775 : case RUSAGE_BOTH:
1776 : case RUSAGE_CHILDREN:
1777 0 : utime = p->signal->cutime;
1778 0 : stime = p->signal->cstime;
1779 0 : r->ru_nvcsw = p->signal->cnvcsw;
1780 0 : r->ru_nivcsw = p->signal->cnivcsw;
1781 0 : r->ru_minflt = p->signal->cmin_flt;
1782 0 : r->ru_majflt = p->signal->cmaj_flt;
1783 0 : r->ru_inblock = p->signal->cinblock;
1784 0 : r->ru_oublock = p->signal->coublock;
1785 0 : maxrss = p->signal->cmaxrss;
1786 :
1787 0 : if (who == RUSAGE_CHILDREN)
1788 : break;
1789 : fallthrough;
1790 :
1791 : case RUSAGE_SELF:
1792 0 : thread_group_cputime_adjusted(p, &tgutime, &tgstime);
1793 0 : utime += tgutime;
1794 0 : stime += tgstime;
1795 0 : r->ru_nvcsw += p->signal->nvcsw;
1796 0 : r->ru_nivcsw += p->signal->nivcsw;
1797 0 : r->ru_minflt += p->signal->min_flt;
1798 0 : r->ru_majflt += p->signal->maj_flt;
1799 0 : r->ru_inblock += p->signal->inblock;
1800 0 : r->ru_oublock += p->signal->oublock;
1801 0 : if (maxrss < p->signal->maxrss)
1802 0 : maxrss = p->signal->maxrss;
1803 0 : t = p;
1804 : do {
1805 0 : accumulate_thread_rusage(t, r);
1806 0 : } while_each_thread(p, t);
1807 : break;
1808 :
1809 : default:
1810 0 : BUG();
1811 : }
1812 0 : unlock_task_sighand(p, &flags);
1813 :
1814 : out:
1815 0 : r->ru_utime = ns_to_kernel_old_timeval(utime);
1816 0 : r->ru_stime = ns_to_kernel_old_timeval(stime);
1817 :
1818 0 : if (who != RUSAGE_CHILDREN) {
1819 0 : struct mm_struct *mm = get_task_mm(p);
1820 :
1821 0 : if (mm) {
1822 0 : setmax_mm_hiwater_rss(&maxrss, mm);
1823 0 : mmput(mm);
1824 : }
1825 : }
1826 0 : r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */
1827 : }
1828 :
1829 0 : SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru)
1830 : {
1831 : struct rusage r;
1832 :
1833 0 : if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1834 : who != RUSAGE_THREAD)
1835 : return -EINVAL;
1836 :
1837 0 : getrusage(current, who, &r);
1838 0 : return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
1839 : }
1840 :
1841 : #ifdef CONFIG_COMPAT
1842 : COMPAT_SYSCALL_DEFINE2(getrusage, int, who, struct compat_rusage __user *, ru)
1843 : {
1844 : struct rusage r;
1845 :
1846 : if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN &&
1847 : who != RUSAGE_THREAD)
1848 : return -EINVAL;
1849 :
1850 : getrusage(current, who, &r);
1851 : return put_compat_rusage(&r, ru);
1852 : }
1853 : #endif
1854 :
1855 0 : SYSCALL_DEFINE1(umask, int, mask)
1856 : {
1857 0 : mask = xchg(¤t->fs->umask, mask & S_IRWXUGO);
1858 0 : return mask;
1859 : }
1860 :
1861 0 : static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
1862 : {
1863 : struct fd exe;
1864 : struct inode *inode;
1865 : int err;
1866 :
1867 0 : exe = fdget(fd);
1868 0 : if (!exe.file)
1869 : return -EBADF;
1870 :
1871 0 : inode = file_inode(exe.file);
1872 :
1873 : /*
1874 : * Because the original mm->exe_file points to executable file, make
1875 : * sure that this one is executable as well, to avoid breaking an
1876 : * overall picture.
1877 : */
1878 0 : err = -EACCES;
1879 0 : if (!S_ISREG(inode->i_mode) || path_noexec(&exe.file->f_path))
1880 : goto exit;
1881 :
1882 0 : err = file_permission(exe.file, MAY_EXEC);
1883 0 : if (err)
1884 : goto exit;
1885 :
1886 0 : err = replace_mm_exe_file(mm, exe.file);
1887 : exit:
1888 0 : fdput(exe);
1889 : return err;
1890 : }
1891 :
1892 : /*
1893 : * Check arithmetic relations of passed addresses.
1894 : *
1895 : * WARNING: we don't require any capability here so be very careful
1896 : * in what is allowed for modification from userspace.
1897 : */
1898 0 : static int validate_prctl_map_addr(struct prctl_mm_map *prctl_map)
1899 : {
1900 0 : unsigned long mmap_max_addr = TASK_SIZE;
1901 0 : int error = -EINVAL, i;
1902 :
1903 : static const unsigned char offsets[] = {
1904 : offsetof(struct prctl_mm_map, start_code),
1905 : offsetof(struct prctl_mm_map, end_code),
1906 : offsetof(struct prctl_mm_map, start_data),
1907 : offsetof(struct prctl_mm_map, end_data),
1908 : offsetof(struct prctl_mm_map, start_brk),
1909 : offsetof(struct prctl_mm_map, brk),
1910 : offsetof(struct prctl_mm_map, start_stack),
1911 : offsetof(struct prctl_mm_map, arg_start),
1912 : offsetof(struct prctl_mm_map, arg_end),
1913 : offsetof(struct prctl_mm_map, env_start),
1914 : offsetof(struct prctl_mm_map, env_end),
1915 : };
1916 :
1917 : /*
1918 : * Make sure the members are not somewhere outside
1919 : * of allowed address space.
1920 : */
1921 0 : for (i = 0; i < ARRAY_SIZE(offsets); i++) {
1922 0 : u64 val = *(u64 *)((char *)prctl_map + offsets[i]);
1923 :
1924 0 : if ((unsigned long)val >= mmap_max_addr ||
1925 0 : (unsigned long)val < mmap_min_addr)
1926 : goto out;
1927 : }
1928 :
1929 : /*
1930 : * Make sure the pairs are ordered.
1931 : */
1932 : #define __prctl_check_order(__m1, __op, __m2) \
1933 : ((unsigned long)prctl_map->__m1 __op \
1934 : (unsigned long)prctl_map->__m2) ? 0 : -EINVAL
1935 0 : error = __prctl_check_order(start_code, <, end_code);
1936 0 : error |= __prctl_check_order(start_data,<=, end_data);
1937 0 : error |= __prctl_check_order(start_brk, <=, brk);
1938 0 : error |= __prctl_check_order(arg_start, <=, arg_end);
1939 0 : error |= __prctl_check_order(env_start, <=, env_end);
1940 0 : if (error)
1941 : goto out;
1942 : #undef __prctl_check_order
1943 :
1944 0 : error = -EINVAL;
1945 :
1946 : /*
1947 : * Neither we should allow to override limits if they set.
1948 : */
1949 0 : if (check_data_rlimit(rlimit(RLIMIT_DATA), prctl_map->brk,
1950 : prctl_map->start_brk, prctl_map->end_data,
1951 : prctl_map->start_data))
1952 : goto out;
1953 :
1954 0 : error = 0;
1955 : out:
1956 0 : return error;
1957 : }
1958 :
1959 : #ifdef CONFIG_CHECKPOINT_RESTORE
1960 : static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data_size)
1961 : {
1962 : struct prctl_mm_map prctl_map = { .exe_fd = (u32)-1, };
1963 : unsigned long user_auxv[AT_VECTOR_SIZE];
1964 : struct mm_struct *mm = current->mm;
1965 : int error;
1966 :
1967 : BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
1968 : BUILD_BUG_ON(sizeof(struct prctl_mm_map) > 256);
1969 :
1970 : if (opt == PR_SET_MM_MAP_SIZE)
1971 : return put_user((unsigned int)sizeof(prctl_map),
1972 : (unsigned int __user *)addr);
1973 :
1974 : if (data_size != sizeof(prctl_map))
1975 : return -EINVAL;
1976 :
1977 : if (copy_from_user(&prctl_map, addr, sizeof(prctl_map)))
1978 : return -EFAULT;
1979 :
1980 : error = validate_prctl_map_addr(&prctl_map);
1981 : if (error)
1982 : return error;
1983 :
1984 : if (prctl_map.auxv_size) {
1985 : /*
1986 : * Someone is trying to cheat the auxv vector.
1987 : */
1988 : if (!prctl_map.auxv ||
1989 : prctl_map.auxv_size > sizeof(mm->saved_auxv))
1990 : return -EINVAL;
1991 :
1992 : memset(user_auxv, 0, sizeof(user_auxv));
1993 : if (copy_from_user(user_auxv,
1994 : (const void __user *)prctl_map.auxv,
1995 : prctl_map.auxv_size))
1996 : return -EFAULT;
1997 :
1998 : /* Last entry must be AT_NULL as specification requires */
1999 : user_auxv[AT_VECTOR_SIZE - 2] = AT_NULL;
2000 : user_auxv[AT_VECTOR_SIZE - 1] = AT_NULL;
2001 : }
2002 :
2003 : if (prctl_map.exe_fd != (u32)-1) {
2004 : /*
2005 : * Check if the current user is checkpoint/restore capable.
2006 : * At the time of this writing, it checks for CAP_SYS_ADMIN
2007 : * or CAP_CHECKPOINT_RESTORE.
2008 : * Note that a user with access to ptrace can masquerade an
2009 : * arbitrary program as any executable, even setuid ones.
2010 : * This may have implications in the tomoyo subsystem.
2011 : */
2012 : if (!checkpoint_restore_ns_capable(current_user_ns()))
2013 : return -EPERM;
2014 :
2015 : error = prctl_set_mm_exe_file(mm, prctl_map.exe_fd);
2016 : if (error)
2017 : return error;
2018 : }
2019 :
2020 : /*
2021 : * arg_lock protects concurrent updates but we still need mmap_lock for
2022 : * read to exclude races with sys_brk.
2023 : */
2024 : mmap_read_lock(mm);
2025 :
2026 : /*
2027 : * We don't validate if these members are pointing to
2028 : * real present VMAs because application may have correspond
2029 : * VMAs already unmapped and kernel uses these members for statistics
2030 : * output in procfs mostly, except
2031 : *
2032 : * - @start_brk/@brk which are used in do_brk_flags but kernel lookups
2033 : * for VMAs when updating these members so anything wrong written
2034 : * here cause kernel to swear at userspace program but won't lead
2035 : * to any problem in kernel itself
2036 : */
2037 :
2038 : spin_lock(&mm->arg_lock);
2039 : mm->start_code = prctl_map.start_code;
2040 : mm->end_code = prctl_map.end_code;
2041 : mm->start_data = prctl_map.start_data;
2042 : mm->end_data = prctl_map.end_data;
2043 : mm->start_brk = prctl_map.start_brk;
2044 : mm->brk = prctl_map.brk;
2045 : mm->start_stack = prctl_map.start_stack;
2046 : mm->arg_start = prctl_map.arg_start;
2047 : mm->arg_end = prctl_map.arg_end;
2048 : mm->env_start = prctl_map.env_start;
2049 : mm->env_end = prctl_map.env_end;
2050 : spin_unlock(&mm->arg_lock);
2051 :
2052 : /*
2053 : * Note this update of @saved_auxv is lockless thus
2054 : * if someone reads this member in procfs while we're
2055 : * updating -- it may get partly updated results. It's
2056 : * known and acceptable trade off: we leave it as is to
2057 : * not introduce additional locks here making the kernel
2058 : * more complex.
2059 : */
2060 : if (prctl_map.auxv_size)
2061 : memcpy(mm->saved_auxv, user_auxv, sizeof(user_auxv));
2062 :
2063 : mmap_read_unlock(mm);
2064 : return 0;
2065 : }
2066 : #endif /* CONFIG_CHECKPOINT_RESTORE */
2067 :
2068 0 : static int prctl_set_auxv(struct mm_struct *mm, unsigned long addr,
2069 : unsigned long len)
2070 : {
2071 : /*
2072 : * This doesn't move the auxiliary vector itself since it's pinned to
2073 : * mm_struct, but it permits filling the vector with new values. It's
2074 : * up to the caller to provide sane values here, otherwise userspace
2075 : * tools which use this vector might be unhappy.
2076 : */
2077 0 : unsigned long user_auxv[AT_VECTOR_SIZE] = {};
2078 :
2079 0 : if (len > sizeof(user_auxv))
2080 : return -EINVAL;
2081 :
2082 0 : if (copy_from_user(user_auxv, (const void __user *)addr, len))
2083 : return -EFAULT;
2084 :
2085 : /* Make sure the last entry is always AT_NULL */
2086 0 : user_auxv[AT_VECTOR_SIZE - 2] = 0;
2087 0 : user_auxv[AT_VECTOR_SIZE - 1] = 0;
2088 :
2089 : BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
2090 :
2091 0 : task_lock(current);
2092 0 : memcpy(mm->saved_auxv, user_auxv, len);
2093 0 : task_unlock(current);
2094 :
2095 0 : return 0;
2096 : }
2097 :
2098 0 : static int prctl_set_mm(int opt, unsigned long addr,
2099 : unsigned long arg4, unsigned long arg5)
2100 : {
2101 0 : struct mm_struct *mm = current->mm;
2102 0 : struct prctl_mm_map prctl_map = {
2103 : .auxv = NULL,
2104 : .auxv_size = 0,
2105 : .exe_fd = -1,
2106 : };
2107 : struct vm_area_struct *vma;
2108 : int error;
2109 :
2110 0 : if (arg5 || (arg4 && (opt != PR_SET_MM_AUXV &&
2111 0 : opt != PR_SET_MM_MAP &&
2112 : opt != PR_SET_MM_MAP_SIZE)))
2113 : return -EINVAL;
2114 :
2115 : #ifdef CONFIG_CHECKPOINT_RESTORE
2116 : if (opt == PR_SET_MM_MAP || opt == PR_SET_MM_MAP_SIZE)
2117 : return prctl_set_mm_map(opt, (const void __user *)addr, arg4);
2118 : #endif
2119 :
2120 0 : if (!capable(CAP_SYS_RESOURCE))
2121 : return -EPERM;
2122 :
2123 0 : if (opt == PR_SET_MM_EXE_FILE)
2124 0 : return prctl_set_mm_exe_file(mm, (unsigned int)addr);
2125 :
2126 0 : if (opt == PR_SET_MM_AUXV)
2127 0 : return prctl_set_auxv(mm, addr, arg4);
2128 :
2129 0 : if (addr >= TASK_SIZE || addr < mmap_min_addr)
2130 : return -EINVAL;
2131 :
2132 0 : error = -EINVAL;
2133 :
2134 : /*
2135 : * arg_lock protects concurrent updates of arg boundaries, we need
2136 : * mmap_lock for a) concurrent sys_brk, b) finding VMA for addr
2137 : * validation.
2138 : */
2139 0 : mmap_read_lock(mm);
2140 0 : vma = find_vma(mm, addr);
2141 :
2142 0 : spin_lock(&mm->arg_lock);
2143 0 : prctl_map.start_code = mm->start_code;
2144 0 : prctl_map.end_code = mm->end_code;
2145 0 : prctl_map.start_data = mm->start_data;
2146 0 : prctl_map.end_data = mm->end_data;
2147 0 : prctl_map.start_brk = mm->start_brk;
2148 0 : prctl_map.brk = mm->brk;
2149 0 : prctl_map.start_stack = mm->start_stack;
2150 0 : prctl_map.arg_start = mm->arg_start;
2151 0 : prctl_map.arg_end = mm->arg_end;
2152 0 : prctl_map.env_start = mm->env_start;
2153 0 : prctl_map.env_end = mm->env_end;
2154 :
2155 0 : switch (opt) {
2156 : case PR_SET_MM_START_CODE:
2157 0 : prctl_map.start_code = addr;
2158 0 : break;
2159 : case PR_SET_MM_END_CODE:
2160 0 : prctl_map.end_code = addr;
2161 0 : break;
2162 : case PR_SET_MM_START_DATA:
2163 0 : prctl_map.start_data = addr;
2164 0 : break;
2165 : case PR_SET_MM_END_DATA:
2166 0 : prctl_map.end_data = addr;
2167 0 : break;
2168 : case PR_SET_MM_START_STACK:
2169 0 : prctl_map.start_stack = addr;
2170 0 : break;
2171 : case PR_SET_MM_START_BRK:
2172 0 : prctl_map.start_brk = addr;
2173 0 : break;
2174 : case PR_SET_MM_BRK:
2175 0 : prctl_map.brk = addr;
2176 0 : break;
2177 : case PR_SET_MM_ARG_START:
2178 0 : prctl_map.arg_start = addr;
2179 0 : break;
2180 : case PR_SET_MM_ARG_END:
2181 0 : prctl_map.arg_end = addr;
2182 0 : break;
2183 : case PR_SET_MM_ENV_START:
2184 0 : prctl_map.env_start = addr;
2185 0 : break;
2186 : case PR_SET_MM_ENV_END:
2187 0 : prctl_map.env_end = addr;
2188 0 : break;
2189 : default:
2190 : goto out;
2191 : }
2192 :
2193 0 : error = validate_prctl_map_addr(&prctl_map);
2194 0 : if (error)
2195 : goto out;
2196 :
2197 : switch (opt) {
2198 : /*
2199 : * If command line arguments and environment
2200 : * are placed somewhere else on stack, we can
2201 : * set them up here, ARG_START/END to setup
2202 : * command line arguments and ENV_START/END
2203 : * for environment.
2204 : */
2205 : case PR_SET_MM_START_STACK:
2206 : case PR_SET_MM_ARG_START:
2207 : case PR_SET_MM_ARG_END:
2208 : case PR_SET_MM_ENV_START:
2209 : case PR_SET_MM_ENV_END:
2210 0 : if (!vma) {
2211 : error = -EFAULT;
2212 : goto out;
2213 : }
2214 : }
2215 :
2216 0 : mm->start_code = prctl_map.start_code;
2217 0 : mm->end_code = prctl_map.end_code;
2218 0 : mm->start_data = prctl_map.start_data;
2219 0 : mm->end_data = prctl_map.end_data;
2220 0 : mm->start_brk = prctl_map.start_brk;
2221 0 : mm->brk = prctl_map.brk;
2222 0 : mm->start_stack = prctl_map.start_stack;
2223 0 : mm->arg_start = prctl_map.arg_start;
2224 0 : mm->arg_end = prctl_map.arg_end;
2225 0 : mm->env_start = prctl_map.env_start;
2226 0 : mm->env_end = prctl_map.env_end;
2227 :
2228 0 : error = 0;
2229 : out:
2230 0 : spin_unlock(&mm->arg_lock);
2231 0 : mmap_read_unlock(mm);
2232 0 : return error;
2233 : }
2234 :
2235 : #ifdef CONFIG_CHECKPOINT_RESTORE
2236 : static int prctl_get_tid_address(struct task_struct *me, int __user * __user *tid_addr)
2237 : {
2238 : return put_user(me->clear_child_tid, tid_addr);
2239 : }
2240 : #else
2241 : static int prctl_get_tid_address(struct task_struct *me, int __user * __user *tid_addr)
2242 : {
2243 : return -EINVAL;
2244 : }
2245 : #endif
2246 :
2247 0 : static int propagate_has_child_subreaper(struct task_struct *p, void *data)
2248 : {
2249 : /*
2250 : * If task has has_child_subreaper - all its descendants
2251 : * already have these flag too and new descendants will
2252 : * inherit it on fork, skip them.
2253 : *
2254 : * If we've found child_reaper - skip descendants in
2255 : * it's subtree as they will never get out pidns.
2256 : */
2257 0 : if (p->signal->has_child_subreaper ||
2258 0 : is_child_reaper(task_pid(p)))
2259 : return 0;
2260 :
2261 0 : p->signal->has_child_subreaper = 1;
2262 0 : return 1;
2263 : }
2264 :
2265 0 : int __weak arch_prctl_spec_ctrl_get(struct task_struct *t, unsigned long which)
2266 : {
2267 0 : return -EINVAL;
2268 : }
2269 :
2270 0 : int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which,
2271 : unsigned long ctrl)
2272 : {
2273 0 : return -EINVAL;
2274 : }
2275 :
2276 : #define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LOCAL_THROTTLE)
2277 :
2278 : #ifdef CONFIG_ANON_VMA_NAME
2279 :
2280 : #define ANON_VMA_NAME_MAX_LEN 80
2281 : #define ANON_VMA_NAME_INVALID_CHARS "\\`$[]"
2282 :
2283 : static inline bool is_valid_name_char(char ch)
2284 : {
2285 : /* printable ascii characters, excluding ANON_VMA_NAME_INVALID_CHARS */
2286 : return ch > 0x1f && ch < 0x7f &&
2287 : !strchr(ANON_VMA_NAME_INVALID_CHARS, ch);
2288 : }
2289 :
2290 : static int prctl_set_vma(unsigned long opt, unsigned long addr,
2291 : unsigned long size, unsigned long arg)
2292 : {
2293 : struct mm_struct *mm = current->mm;
2294 : const char __user *uname;
2295 : struct anon_vma_name *anon_name = NULL;
2296 : int error;
2297 :
2298 : switch (opt) {
2299 : case PR_SET_VMA_ANON_NAME:
2300 : uname = (const char __user *)arg;
2301 : if (uname) {
2302 : char *name, *pch;
2303 :
2304 : name = strndup_user(uname, ANON_VMA_NAME_MAX_LEN);
2305 : if (IS_ERR(name))
2306 : return PTR_ERR(name);
2307 :
2308 : for (pch = name; *pch != '\0'; pch++) {
2309 : if (!is_valid_name_char(*pch)) {
2310 : kfree(name);
2311 : return -EINVAL;
2312 : }
2313 : }
2314 : /* anon_vma has its own copy */
2315 : anon_name = anon_vma_name_alloc(name);
2316 : kfree(name);
2317 : if (!anon_name)
2318 : return -ENOMEM;
2319 :
2320 : }
2321 :
2322 : mmap_write_lock(mm);
2323 : error = madvise_set_anon_name(mm, addr, size, anon_name);
2324 : mmap_write_unlock(mm);
2325 : anon_vma_name_put(anon_name);
2326 : break;
2327 : default:
2328 : error = -EINVAL;
2329 : }
2330 :
2331 : return error;
2332 : }
2333 :
2334 : #else /* CONFIG_ANON_VMA_NAME */
2335 : static int prctl_set_vma(unsigned long opt, unsigned long start,
2336 : unsigned long size, unsigned long arg)
2337 : {
2338 : return -EINVAL;
2339 : }
2340 : #endif /* CONFIG_ANON_VMA_NAME */
2341 :
2342 0 : SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3,
2343 : unsigned long, arg4, unsigned long, arg5)
2344 : {
2345 0 : struct task_struct *me = current;
2346 : unsigned char comm[sizeof(me->comm)];
2347 : long error;
2348 :
2349 0 : error = security_task_prctl(option, arg2, arg3, arg4, arg5);
2350 0 : if (error != -ENOSYS)
2351 : return error;
2352 :
2353 0 : error = 0;
2354 0 : switch (option) {
2355 : case PR_SET_PDEATHSIG:
2356 0 : if (!valid_signal(arg2)) {
2357 : error = -EINVAL;
2358 : break;
2359 : }
2360 0 : me->pdeath_signal = arg2;
2361 0 : break;
2362 : case PR_GET_PDEATHSIG:
2363 0 : error = put_user(me->pdeath_signal, (int __user *)arg2);
2364 0 : break;
2365 : case PR_GET_DUMPABLE:
2366 0 : error = get_dumpable(me->mm);
2367 0 : break;
2368 : case PR_SET_DUMPABLE:
2369 0 : if (arg2 != SUID_DUMP_DISABLE && arg2 != SUID_DUMP_USER) {
2370 : error = -EINVAL;
2371 : break;
2372 : }
2373 0 : set_dumpable(me->mm, arg2);
2374 0 : break;
2375 :
2376 : case PR_SET_UNALIGN:
2377 : error = SET_UNALIGN_CTL(me, arg2);
2378 : break;
2379 : case PR_GET_UNALIGN:
2380 : error = GET_UNALIGN_CTL(me, arg2);
2381 : break;
2382 : case PR_SET_FPEMU:
2383 : error = SET_FPEMU_CTL(me, arg2);
2384 : break;
2385 : case PR_GET_FPEMU:
2386 : error = GET_FPEMU_CTL(me, arg2);
2387 : break;
2388 : case PR_SET_FPEXC:
2389 : error = SET_FPEXC_CTL(me, arg2);
2390 : break;
2391 : case PR_GET_FPEXC:
2392 : error = GET_FPEXC_CTL(me, arg2);
2393 : break;
2394 : case PR_GET_TIMING:
2395 0 : error = PR_TIMING_STATISTICAL;
2396 0 : break;
2397 : case PR_SET_TIMING:
2398 0 : if (arg2 != PR_TIMING_STATISTICAL)
2399 0 : error = -EINVAL;
2400 : break;
2401 : case PR_SET_NAME:
2402 0 : comm[sizeof(me->comm) - 1] = 0;
2403 0 : if (strncpy_from_user(comm, (char __user *)arg2,
2404 : sizeof(me->comm) - 1) < 0)
2405 : return -EFAULT;
2406 : set_task_comm(me, comm);
2407 : proc_comm_connector(me);
2408 : break;
2409 : case PR_GET_NAME:
2410 0 : get_task_comm(comm, me);
2411 0 : if (copy_to_user((char __user *)arg2, comm, sizeof(comm)))
2412 : return -EFAULT;
2413 : break;
2414 : case PR_GET_ENDIAN:
2415 : error = GET_ENDIAN(me, arg2);
2416 : break;
2417 : case PR_SET_ENDIAN:
2418 : error = SET_ENDIAN(me, arg2);
2419 : break;
2420 : case PR_GET_SECCOMP:
2421 0 : error = prctl_get_seccomp();
2422 0 : break;
2423 : case PR_SET_SECCOMP:
2424 0 : error = prctl_set_seccomp(arg2, (char __user *)arg3);
2425 0 : break;
2426 : case PR_GET_TSC:
2427 : error = GET_TSC_CTL(arg2);
2428 : break;
2429 : case PR_SET_TSC:
2430 : error = SET_TSC_CTL(arg2);
2431 : break;
2432 : case PR_TASK_PERF_EVENTS_DISABLE:
2433 : error = perf_event_task_disable();
2434 : break;
2435 : case PR_TASK_PERF_EVENTS_ENABLE:
2436 : error = perf_event_task_enable();
2437 : break;
2438 : case PR_GET_TIMERSLACK:
2439 0 : if (current->timer_slack_ns > ULONG_MAX)
2440 : error = ULONG_MAX;
2441 : else
2442 0 : error = current->timer_slack_ns;
2443 : break;
2444 : case PR_SET_TIMERSLACK:
2445 0 : if (arg2 <= 0)
2446 0 : current->timer_slack_ns =
2447 0 : current->default_timer_slack_ns;
2448 : else
2449 0 : current->timer_slack_ns = arg2;
2450 : break;
2451 : case PR_MCE_KILL:
2452 0 : if (arg4 | arg5)
2453 : return -EINVAL;
2454 0 : switch (arg2) {
2455 : case PR_MCE_KILL_CLEAR:
2456 0 : if (arg3 != 0)
2457 : return -EINVAL;
2458 0 : current->flags &= ~PF_MCE_PROCESS;
2459 0 : break;
2460 : case PR_MCE_KILL_SET:
2461 0 : current->flags |= PF_MCE_PROCESS;
2462 0 : if (arg3 == PR_MCE_KILL_EARLY)
2463 0 : current->flags |= PF_MCE_EARLY;
2464 0 : else if (arg3 == PR_MCE_KILL_LATE)
2465 0 : current->flags &= ~PF_MCE_EARLY;
2466 0 : else if (arg3 == PR_MCE_KILL_DEFAULT)
2467 0 : current->flags &=
2468 : ~(PF_MCE_EARLY|PF_MCE_PROCESS);
2469 : else
2470 : return -EINVAL;
2471 : break;
2472 : default:
2473 : return -EINVAL;
2474 : }
2475 : break;
2476 : case PR_MCE_KILL_GET:
2477 0 : if (arg2 | arg3 | arg4 | arg5)
2478 : return -EINVAL;
2479 0 : if (current->flags & PF_MCE_PROCESS)
2480 0 : error = (current->flags & PF_MCE_EARLY) ?
2481 0 : PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE;
2482 : else
2483 : error = PR_MCE_KILL_DEFAULT;
2484 : break;
2485 : case PR_SET_MM:
2486 0 : error = prctl_set_mm(arg2, arg3, arg4, arg5);
2487 0 : break;
2488 : case PR_GET_TID_ADDRESS:
2489 : error = prctl_get_tid_address(me, (int __user * __user *)arg2);
2490 : break;
2491 : case PR_SET_CHILD_SUBREAPER:
2492 0 : me->signal->is_child_subreaper = !!arg2;
2493 0 : if (!arg2)
2494 : break;
2495 :
2496 0 : walk_process_tree(me, propagate_has_child_subreaper, NULL);
2497 0 : break;
2498 : case PR_GET_CHILD_SUBREAPER:
2499 0 : error = put_user(me->signal->is_child_subreaper,
2500 : (int __user *)arg2);
2501 0 : break;
2502 : case PR_SET_NO_NEW_PRIVS:
2503 0 : if (arg2 != 1 || arg3 || arg4 || arg5)
2504 : return -EINVAL;
2505 :
2506 0 : task_set_no_new_privs(current);
2507 : break;
2508 : case PR_GET_NO_NEW_PRIVS:
2509 0 : if (arg2 || arg3 || arg4 || arg5)
2510 : return -EINVAL;
2511 0 : return task_no_new_privs(current) ? 1 : 0;
2512 : case PR_GET_THP_DISABLE:
2513 0 : if (arg2 || arg3 || arg4 || arg5)
2514 : return -EINVAL;
2515 0 : error = !!test_bit(MMF_DISABLE_THP, &me->mm->flags);
2516 0 : break;
2517 : case PR_SET_THP_DISABLE:
2518 0 : if (arg3 || arg4 || arg5)
2519 : return -EINVAL;
2520 0 : if (mmap_write_lock_killable(me->mm))
2521 : return -EINTR;
2522 0 : if (arg2)
2523 0 : set_bit(MMF_DISABLE_THP, &me->mm->flags);
2524 : else
2525 0 : clear_bit(MMF_DISABLE_THP, &me->mm->flags);
2526 0 : mmap_write_unlock(me->mm);
2527 : break;
2528 : case PR_MPX_ENABLE_MANAGEMENT:
2529 : case PR_MPX_DISABLE_MANAGEMENT:
2530 : /* No longer implemented: */
2531 : return -EINVAL;
2532 : case PR_SET_FP_MODE:
2533 : error = SET_FP_MODE(me, arg2);
2534 : break;
2535 : case PR_GET_FP_MODE:
2536 : error = GET_FP_MODE(me);
2537 : break;
2538 : case PR_SVE_SET_VL:
2539 : error = SVE_SET_VL(arg2);
2540 : break;
2541 : case PR_SVE_GET_VL:
2542 : error = SVE_GET_VL();
2543 : break;
2544 : case PR_GET_SPECULATION_CTRL:
2545 0 : if (arg3 || arg4 || arg5)
2546 : return -EINVAL;
2547 0 : error = arch_prctl_spec_ctrl_get(me, arg2);
2548 0 : break;
2549 : case PR_SET_SPECULATION_CTRL:
2550 0 : if (arg4 || arg5)
2551 : return -EINVAL;
2552 0 : error = arch_prctl_spec_ctrl_set(me, arg2, arg3);
2553 0 : break;
2554 : case PR_PAC_RESET_KEYS:
2555 0 : if (arg3 || arg4 || arg5)
2556 : return -EINVAL;
2557 : error = PAC_RESET_KEYS(me, arg2);
2558 : break;
2559 : case PR_PAC_SET_ENABLED_KEYS:
2560 0 : if (arg4 || arg5)
2561 : return -EINVAL;
2562 : error = PAC_SET_ENABLED_KEYS(me, arg2, arg3);
2563 : break;
2564 : case PR_PAC_GET_ENABLED_KEYS:
2565 0 : if (arg2 || arg3 || arg4 || arg5)
2566 : return -EINVAL;
2567 : error = PAC_GET_ENABLED_KEYS(me);
2568 : break;
2569 : case PR_SET_TAGGED_ADDR_CTRL:
2570 0 : if (arg3 || arg4 || arg5)
2571 : return -EINVAL;
2572 : error = SET_TAGGED_ADDR_CTRL(arg2);
2573 : break;
2574 : case PR_GET_TAGGED_ADDR_CTRL:
2575 0 : if (arg2 || arg3 || arg4 || arg5)
2576 : return -EINVAL;
2577 : error = GET_TAGGED_ADDR_CTRL();
2578 : break;
2579 : case PR_SET_IO_FLUSHER:
2580 0 : if (!capable(CAP_SYS_RESOURCE))
2581 : return -EPERM;
2582 :
2583 0 : if (arg3 || arg4 || arg5)
2584 : return -EINVAL;
2585 :
2586 0 : if (arg2 == 1)
2587 0 : current->flags |= PR_IO_FLUSHER;
2588 0 : else if (!arg2)
2589 0 : current->flags &= ~PR_IO_FLUSHER;
2590 : else
2591 : return -EINVAL;
2592 : break;
2593 : case PR_GET_IO_FLUSHER:
2594 0 : if (!capable(CAP_SYS_RESOURCE))
2595 : return -EPERM;
2596 :
2597 0 : if (arg2 || arg3 || arg4 || arg5)
2598 : return -EINVAL;
2599 :
2600 0 : error = (current->flags & PR_IO_FLUSHER) == PR_IO_FLUSHER;
2601 0 : break;
2602 : case PR_SET_SYSCALL_USER_DISPATCH:
2603 : error = set_syscall_user_dispatch(arg2, arg3, arg4,
2604 : (char __user *) arg5);
2605 : break;
2606 : #ifdef CONFIG_SCHED_CORE
2607 : case PR_SCHED_CORE:
2608 : error = sched_core_share_pid(arg2, arg3, arg4, arg5);
2609 : break;
2610 : #endif
2611 : case PR_SET_VMA:
2612 : error = prctl_set_vma(arg2, arg3, arg4, arg5);
2613 : break;
2614 : default:
2615 : error = -EINVAL;
2616 : break;
2617 : }
2618 : return error;
2619 : }
2620 :
2621 0 : SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,
2622 : struct getcpu_cache __user *, unused)
2623 : {
2624 0 : int err = 0;
2625 0 : int cpu = raw_smp_processor_id();
2626 :
2627 0 : if (cpup)
2628 0 : err |= put_user(cpu, cpup);
2629 0 : if (nodep)
2630 0 : err |= put_user(cpu_to_node(cpu), nodep);
2631 0 : return err ? -EFAULT : 0;
2632 : }
2633 :
2634 : /**
2635 : * do_sysinfo - fill in sysinfo struct
2636 : * @info: pointer to buffer to fill
2637 : */
2638 0 : static int do_sysinfo(struct sysinfo *info)
2639 : {
2640 : unsigned long mem_total, sav_total;
2641 : unsigned int mem_unit, bitcount;
2642 : struct timespec64 tp;
2643 :
2644 0 : memset(info, 0, sizeof(struct sysinfo));
2645 :
2646 0 : ktime_get_boottime_ts64(&tp);
2647 0 : timens_add_boottime(&tp);
2648 0 : info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0);
2649 :
2650 0 : get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT);
2651 :
2652 0 : info->procs = nr_threads;
2653 :
2654 0 : si_meminfo(info);
2655 0 : si_swapinfo(info);
2656 :
2657 : /*
2658 : * If the sum of all the available memory (i.e. ram + swap)
2659 : * is less than can be stored in a 32 bit unsigned long then
2660 : * we can be binary compatible with 2.2.x kernels. If not,
2661 : * well, in that case 2.2.x was broken anyways...
2662 : *
2663 : * -Erik Andersen <andersee@debian.org>
2664 : */
2665 :
2666 0 : mem_total = info->totalram + info->totalswap;
2667 0 : if (mem_total < info->totalram || mem_total < info->totalswap)
2668 : goto out;
2669 0 : bitcount = 0;
2670 0 : mem_unit = info->mem_unit;
2671 0 : while (mem_unit > 1) {
2672 0 : bitcount++;
2673 0 : mem_unit >>= 1;
2674 0 : sav_total = mem_total;
2675 0 : mem_total <<= 1;
2676 0 : if (mem_total < sav_total)
2677 : goto out;
2678 : }
2679 :
2680 : /*
2681 : * If mem_total did not overflow, multiply all memory values by
2682 : * info->mem_unit and set it to 1. This leaves things compatible
2683 : * with 2.2.x, and also retains compatibility with earlier 2.4.x
2684 : * kernels...
2685 : */
2686 :
2687 0 : info->mem_unit = 1;
2688 0 : info->totalram <<= bitcount;
2689 0 : info->freeram <<= bitcount;
2690 0 : info->sharedram <<= bitcount;
2691 0 : info->bufferram <<= bitcount;
2692 0 : info->totalswap <<= bitcount;
2693 0 : info->freeswap <<= bitcount;
2694 0 : info->totalhigh <<= bitcount;
2695 0 : info->freehigh <<= bitcount;
2696 :
2697 : out:
2698 0 : return 0;
2699 : }
2700 :
2701 0 : SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info)
2702 : {
2703 : struct sysinfo val;
2704 :
2705 0 : do_sysinfo(&val);
2706 :
2707 0 : if (copy_to_user(info, &val, sizeof(struct sysinfo)))
2708 : return -EFAULT;
2709 :
2710 0 : return 0;
2711 : }
2712 :
2713 : #ifdef CONFIG_COMPAT
2714 : struct compat_sysinfo {
2715 : s32 uptime;
2716 : u32 loads[3];
2717 : u32 totalram;
2718 : u32 freeram;
2719 : u32 sharedram;
2720 : u32 bufferram;
2721 : u32 totalswap;
2722 : u32 freeswap;
2723 : u16 procs;
2724 : u16 pad;
2725 : u32 totalhigh;
2726 : u32 freehigh;
2727 : u32 mem_unit;
2728 : char _f[20-2*sizeof(u32)-sizeof(int)];
2729 : };
2730 :
2731 : COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info)
2732 : {
2733 : struct sysinfo s;
2734 : struct compat_sysinfo s_32;
2735 :
2736 : do_sysinfo(&s);
2737 :
2738 : /* Check to see if any memory value is too large for 32-bit and scale
2739 : * down if needed
2740 : */
2741 : if (upper_32_bits(s.totalram) || upper_32_bits(s.totalswap)) {
2742 : int bitcount = 0;
2743 :
2744 : while (s.mem_unit < PAGE_SIZE) {
2745 : s.mem_unit <<= 1;
2746 : bitcount++;
2747 : }
2748 :
2749 : s.totalram >>= bitcount;
2750 : s.freeram >>= bitcount;
2751 : s.sharedram >>= bitcount;
2752 : s.bufferram >>= bitcount;
2753 : s.totalswap >>= bitcount;
2754 : s.freeswap >>= bitcount;
2755 : s.totalhigh >>= bitcount;
2756 : s.freehigh >>= bitcount;
2757 : }
2758 :
2759 : memset(&s_32, 0, sizeof(s_32));
2760 : s_32.uptime = s.uptime;
2761 : s_32.loads[0] = s.loads[0];
2762 : s_32.loads[1] = s.loads[1];
2763 : s_32.loads[2] = s.loads[2];
2764 : s_32.totalram = s.totalram;
2765 : s_32.freeram = s.freeram;
2766 : s_32.sharedram = s.sharedram;
2767 : s_32.bufferram = s.bufferram;
2768 : s_32.totalswap = s.totalswap;
2769 : s_32.freeswap = s.freeswap;
2770 : s_32.procs = s.procs;
2771 : s_32.totalhigh = s.totalhigh;
2772 : s_32.freehigh = s.freehigh;
2773 : s_32.mem_unit = s.mem_unit;
2774 : if (copy_to_user(info, &s_32, sizeof(s_32)))
2775 : return -EFAULT;
2776 : return 0;
2777 : }
2778 : #endif /* CONFIG_COMPAT */
|