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