~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/kernel/sys.c

Version: ~ [ linux-5.4.2 ] ~ [ linux-5.3.15 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.88 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.158 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.206 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.206 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.78 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp