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