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