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

TOMOYO Linux Cross Reference
Linux/kernel/sys.c

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

~ [ 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