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