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