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