Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/kernel/sys.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | */ |
| 6 | |
Paul Gortmaker | 9984de1 | 2011-05-23 14:51:41 -0400 | [diff] [blame] | 7 | #include <linux/export.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/mm.h> |
| 9 | #include <linux/utsname.h> |
| 10 | #include <linux/mman.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/reboot.h> |
| 12 | #include <linux/prctl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/highuid.h> |
| 14 | #include <linux/fs.h> |
Paul Gortmaker | 74da1ff | 2011-05-26 12:48:41 -0400 | [diff] [blame] | 15 | #include <linux/kmod.h> |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 16 | #include <linux/perf_event.h> |
Daniel Walker | 3e88c55 | 2007-05-10 22:22:53 -0700 | [diff] [blame] | 17 | #include <linux/resource.h> |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 18 | #include <linux/kernel.h> |
| 19 | #include <linux/kexec.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/workqueue.h> |
Randy.Dunlap | c59ede7 | 2006-01-11 12:17:46 -0800 | [diff] [blame] | 21 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Jesper Juhl | 7ed20e1 | 2005-05-01 08:59:14 -0700 | [diff] [blame] | 30 | #include <linux/signal.h> |
Matt Helsley | 9f46080 | 2005-11-07 00:59:16 -0800 | [diff] [blame] | 31 | #include <linux/cn_proc.h> |
Andi Kleen | 3cfc348 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 32 | #include <linux/getcpu.h> |
Eric Dumazet | 6eaeeab | 2007-05-10 22:22:37 -0700 | [diff] [blame] | 33 | #include <linux/task_io_accounting_ops.h> |
Andrea Arcangeli | 1d9d02f | 2007-07-15 23:41:32 -0700 | [diff] [blame] | 34 | #include <linux/seccomp.h> |
Mark Lord | 4047727 | 2007-10-01 01:20:10 -0700 | [diff] [blame] | 35 | #include <linux/cpu.h> |
Christoph Hellwig | e28cbf2 | 2010-03-10 15:21:19 -0800 | [diff] [blame] | 36 | #include <linux/personality.h> |
Paul Mackerras | e3d5a27 | 2009-01-06 14:41:02 -0800 | [diff] [blame] | 37 | #include <linux/ptrace.h> |
Al Viro | 5ad4e53 | 2009-03-29 19:50:06 -0400 | [diff] [blame] | 38 | #include <linux/fs_struct.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 39 | #include <linux/gfp.h> |
Rafael J. Wysocki | 40dc166 | 2011-03-15 00:43:46 +0100 | [diff] [blame] | 40 | #include <linux/syscore_ops.h> |
Andi Kleen | be27425 | 2011-08-19 16:15:10 -0700 | [diff] [blame] | 41 | #include <linux/version.h> |
| 42 | #include <linux/ctype.h> |
Colin Cross | 8ad62c2 | 2013-06-26 17:26:01 -0700 | [diff] [blame] | 43 | #include <linux/mm.h> |
| 44 | #include <linux/mempolicy.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
| 46 | #include <linux/compat.h> |
| 47 | #include <linux/syscalls.h> |
Keshavamurthy Anil S | 00d7c05 | 2005-12-12 00:37:33 -0800 | [diff] [blame] | 48 | #include <linux/kprobes.h> |
Cedric Le Goater | acce292 | 2007-07-15 23:40:59 -0700 | [diff] [blame] | 49 | #include <linux/user_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Seiji Aguchi | 04c6862 | 2011-01-12 16:59:30 -0800 | [diff] [blame] | 51 | #include <linux/kmsg_dump.h> |
Andi Kleen | be27425 | 2011-08-19 16:15:10 -0700 | [diff] [blame] | 52 | /* Move somewhere else to avoid recompiling? */ |
| 53 | #include <generated/utsrelease.h> |
Seiji Aguchi | 04c6862 | 2011-01-12 16:59:30 -0800 | [diff] [blame] | 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include <asm/uaccess.h> |
| 56 | #include <asm/io.h> |
| 57 | #include <asm/unistd.h> |
| 58 | |
| 59 | #ifndef SET_UNALIGN_CTL |
| 60 | # define SET_UNALIGN_CTL(a,b) (-EINVAL) |
| 61 | #endif |
| 62 | #ifndef GET_UNALIGN_CTL |
| 63 | # define GET_UNALIGN_CTL(a,b) (-EINVAL) |
| 64 | #endif |
| 65 | #ifndef SET_FPEMU_CTL |
| 66 | # define SET_FPEMU_CTL(a,b) (-EINVAL) |
| 67 | #endif |
| 68 | #ifndef GET_FPEMU_CTL |
| 69 | # define GET_FPEMU_CTL(a,b) (-EINVAL) |
| 70 | #endif |
| 71 | #ifndef SET_FPEXC_CTL |
| 72 | # define SET_FPEXC_CTL(a,b) (-EINVAL) |
| 73 | #endif |
| 74 | #ifndef GET_FPEXC_CTL |
| 75 | # define GET_FPEXC_CTL(a,b) (-EINVAL) |
| 76 | #endif |
Anton Blanchard | 651d765 | 2006-06-07 16:10:19 +1000 | [diff] [blame] | 77 | #ifndef GET_ENDIAN |
| 78 | # define GET_ENDIAN(a,b) (-EINVAL) |
| 79 | #endif |
| 80 | #ifndef SET_ENDIAN |
| 81 | # define SET_ENDIAN(a,b) (-EINVAL) |
| 82 | #endif |
Erik Bosman | 8fb402b | 2008-04-11 18:54:17 +0200 | [diff] [blame] | 83 | #ifndef GET_TSC_CTL |
| 84 | # define GET_TSC_CTL(a) (-EINVAL) |
| 85 | #endif |
| 86 | #ifndef SET_TSC_CTL |
| 87 | # define SET_TSC_CTL(a) (-EINVAL) |
| 88 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * this is where the system-wide overflow UID and GID are defined, for |
| 92 | * architectures that now have 32-bit UID/GID but didn't in the past |
| 93 | */ |
| 94 | |
| 95 | int overflowuid = DEFAULT_OVERFLOWUID; |
| 96 | int overflowgid = DEFAULT_OVERFLOWGID; |
| 97 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | EXPORT_SYMBOL(overflowuid); |
| 99 | EXPORT_SYMBOL(overflowgid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
| 101 | /* |
| 102 | * the same as above, but for filesystems which can only store a 16-bit |
| 103 | * UID and GID. as such, this is needed on all architectures |
| 104 | */ |
| 105 | |
| 106 | int fs_overflowuid = DEFAULT_FS_OVERFLOWUID; |
| 107 | int fs_overflowgid = DEFAULT_FS_OVERFLOWUID; |
| 108 | |
| 109 | EXPORT_SYMBOL(fs_overflowuid); |
| 110 | EXPORT_SYMBOL(fs_overflowgid); |
| 111 | |
| 112 | /* |
| 113 | * this indicates whether you can reboot with ctrl-alt-del: the default is yes |
| 114 | */ |
| 115 | |
| 116 | int C_A_D = 1; |
Cedric Le Goater | 9ec5209 | 2006-10-02 02:19:00 -0700 | [diff] [blame] | 117 | struct pid *cad_pid; |
| 118 | EXPORT_SYMBOL(cad_pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
| 120 | /* |
Rafael J. Wysocki | bd804eb | 2007-07-19 01:47:40 -0700 | [diff] [blame] | 121 | * If set, this is used for preparing the system to power off. |
| 122 | */ |
| 123 | |
| 124 | void (*pm_power_off_prepare)(void); |
Rafael J. Wysocki | bd804eb | 2007-07-19 01:47:40 -0700 | [diff] [blame] | 125 | |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 126 | /* |
Serge E. Hallyn | fc832ad | 2011-03-23 16:43:22 -0700 | [diff] [blame] | 127 | * Returns true if current's euid is same as p's uid or euid, |
| 128 | * or has CAP_SYS_NICE to p's user_ns. |
| 129 | * |
| 130 | * Called with rcu_read_lock, creds are safe |
| 131 | */ |
| 132 | static bool set_one_prio_perm(struct task_struct *p) |
| 133 | { |
| 134 | const struct cred *cred = current_cred(), *pcred = __task_cred(p); |
| 135 | |
Eric W. Biederman | 6f2c0a9 | 2011-11-16 23:15:31 -0800 | [diff] [blame] | 136 | if (pcred->user_ns == cred->user_ns && |
Serge E. Hallyn | fc832ad | 2011-03-23 16:43:22 -0700 | [diff] [blame] | 137 | (pcred->uid == cred->euid || |
| 138 | pcred->euid == cred->euid)) |
| 139 | return true; |
Eric W. Biederman | 6f2c0a9 | 2011-11-16 23:15:31 -0800 | [diff] [blame] | 140 | if (ns_capable(pcred->user_ns, CAP_SYS_NICE)) |
Serge E. Hallyn | fc832ad | 2011-03-23 16:43:22 -0700 | [diff] [blame] | 141 | return true; |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | /* |
David Howells | c69e8d9 | 2008-11-14 10:39:19 +1100 | [diff] [blame] | 146 | * set the priority of a task |
| 147 | * - the caller must hold the RCU read lock |
| 148 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | static int set_one_prio(struct task_struct *p, int niceval, int error) |
| 150 | { |
| 151 | int no_nice; |
| 152 | |
Serge E. Hallyn | fc832ad | 2011-03-23 16:43:22 -0700 | [diff] [blame] | 153 | if (!set_one_prio_perm(p)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | error = -EPERM; |
| 155 | goto out; |
| 156 | } |
Matt Mackall | e43379f | 2005-05-01 08:59:00 -0700 | [diff] [blame] | 157 | if (niceval < task_nice(p) && !can_nice(p, niceval)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | error = -EACCES; |
| 159 | goto out; |
| 160 | } |
| 161 | no_nice = security_task_setnice(p, niceval); |
| 162 | if (no_nice) { |
| 163 | error = no_nice; |
| 164 | goto out; |
| 165 | } |
| 166 | if (error == -ESRCH) |
| 167 | error = 0; |
| 168 | set_user_nice(p, niceval); |
| 169 | out: |
| 170 | return error; |
| 171 | } |
| 172 | |
Heiko Carstens | 754fe8d | 2009-01-14 14:14:09 +0100 | [diff] [blame] | 173 | SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | { |
| 175 | struct task_struct *g, *p; |
| 176 | struct user_struct *user; |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 177 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | int error = -EINVAL; |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 179 | struct pid *pgrp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | |
Daniel Walker | 3e88c55 | 2007-05-10 22:22:53 -0700 | [diff] [blame] | 181 | if (which > PRIO_USER || which < PRIO_PROCESS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | goto out; |
| 183 | |
| 184 | /* normalize: avoid signed division (rounding problems) */ |
| 185 | error = -ESRCH; |
| 186 | if (niceval < -20) |
| 187 | niceval = -20; |
| 188 | if (niceval > 19) |
| 189 | niceval = 19; |
| 190 | |
Thomas Gleixner | d4581a2 | 2009-12-10 00:52:51 +0000 | [diff] [blame] | 191 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | read_lock(&tasklist_lock); |
| 193 | switch (which) { |
| 194 | case PRIO_PROCESS: |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 195 | if (who) |
Pavel Emelyanov | 228ebcb | 2007-10-18 23:40:16 -0700 | [diff] [blame] | 196 | p = find_task_by_vpid(who); |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 197 | else |
| 198 | p = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | if (p) |
| 200 | error = set_one_prio(p, niceval, error); |
| 201 | break; |
| 202 | case PRIO_PGRP: |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 203 | if (who) |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 204 | pgrp = find_vpid(who); |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 205 | else |
| 206 | pgrp = task_pgrp(current); |
Ken Chen | 2d70b68 | 2008-08-20 14:09:17 -0700 | [diff] [blame] | 207 | do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | error = set_one_prio(p, niceval, error); |
Ken Chen | 2d70b68 | 2008-08-20 14:09:17 -0700 | [diff] [blame] | 209 | } while_each_pid_thread(pgrp, PIDTYPE_PGID, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | break; |
| 211 | case PRIO_USER: |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 212 | user = (struct user_struct *) cred->user; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | if (!who) |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 214 | who = cred->uid; |
| 215 | else if ((who != cred->uid) && |
| 216 | !(user = find_user(who))) |
| 217 | goto out_unlock; /* No processes for this user */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | |
H Hartley Sweeten | dfc6a73 | 2009-12-14 18:00:22 -0800 | [diff] [blame] | 219 | do_each_thread(g, p) { |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 220 | if (__task_cred(p)->uid == who) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | error = set_one_prio(p, niceval, error); |
H Hartley Sweeten | dfc6a73 | 2009-12-14 18:00:22 -0800 | [diff] [blame] | 222 | } while_each_thread(g, p); |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 223 | if (who != cred->uid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | free_uid(user); /* For find_user() */ |
| 225 | break; |
| 226 | } |
| 227 | out_unlock: |
| 228 | read_unlock(&tasklist_lock); |
Thomas Gleixner | d4581a2 | 2009-12-10 00:52:51 +0000 | [diff] [blame] | 229 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | out: |
| 231 | return error; |
| 232 | } |
| 233 | |
| 234 | /* |
| 235 | * Ugh. To avoid negative return values, "getpriority()" will |
| 236 | * not return the normal nice-value, but a negated value that |
| 237 | * has been offset by 20 (ie it returns 40..1 instead of -20..19) |
| 238 | * to stay compatible. |
| 239 | */ |
Heiko Carstens | 754fe8d | 2009-01-14 14:14:09 +0100 | [diff] [blame] | 240 | SYSCALL_DEFINE2(getpriority, int, which, int, who) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | { |
| 242 | struct task_struct *g, *p; |
| 243 | struct user_struct *user; |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 244 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | long niceval, retval = -ESRCH; |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 246 | struct pid *pgrp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
Daniel Walker | 3e88c55 | 2007-05-10 22:22:53 -0700 | [diff] [blame] | 248 | if (which > PRIO_USER || which < PRIO_PROCESS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | return -EINVAL; |
| 250 | |
Tetsuo Handa | 7011883 | 2010-02-22 12:44:16 -0800 | [diff] [blame] | 251 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | read_lock(&tasklist_lock); |
| 253 | switch (which) { |
| 254 | case PRIO_PROCESS: |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 255 | if (who) |
Pavel Emelyanov | 228ebcb | 2007-10-18 23:40:16 -0700 | [diff] [blame] | 256 | p = find_task_by_vpid(who); |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 257 | else |
| 258 | p = current; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | if (p) { |
| 260 | niceval = 20 - task_nice(p); |
| 261 | if (niceval > retval) |
| 262 | retval = niceval; |
| 263 | } |
| 264 | break; |
| 265 | case PRIO_PGRP: |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 266 | if (who) |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 267 | pgrp = find_vpid(who); |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 268 | else |
| 269 | pgrp = task_pgrp(current); |
Ken Chen | 2d70b68 | 2008-08-20 14:09:17 -0700 | [diff] [blame] | 270 | do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | niceval = 20 - task_nice(p); |
| 272 | if (niceval > retval) |
| 273 | retval = niceval; |
Ken Chen | 2d70b68 | 2008-08-20 14:09:17 -0700 | [diff] [blame] | 274 | } while_each_pid_thread(pgrp, PIDTYPE_PGID, p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | break; |
| 276 | case PRIO_USER: |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 277 | user = (struct user_struct *) cred->user; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | if (!who) |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 279 | who = cred->uid; |
| 280 | else if ((who != cred->uid) && |
| 281 | !(user = find_user(who))) |
| 282 | goto out_unlock; /* No processes for this user */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
H Hartley Sweeten | dfc6a73 | 2009-12-14 18:00:22 -0800 | [diff] [blame] | 284 | do_each_thread(g, p) { |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 285 | if (__task_cred(p)->uid == who) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | niceval = 20 - task_nice(p); |
| 287 | if (niceval > retval) |
| 288 | retval = niceval; |
| 289 | } |
H Hartley Sweeten | dfc6a73 | 2009-12-14 18:00:22 -0800 | [diff] [blame] | 290 | } while_each_thread(g, p); |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 291 | if (who != cred->uid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | free_uid(user); /* for find_user() */ |
| 293 | break; |
| 294 | } |
| 295 | out_unlock: |
| 296 | read_unlock(&tasklist_lock); |
Tetsuo Handa | 7011883 | 2010-02-22 12:44:16 -0800 | [diff] [blame] | 297 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
| 299 | return retval; |
| 300 | } |
| 301 | |
Eric W. Biederman | e4c9433 | 2005-09-22 21:43:45 -0700 | [diff] [blame] | 302 | /** |
| 303 | * emergency_restart - reboot the system |
| 304 | * |
| 305 | * Without shutting down any hardware or taking any locks |
| 306 | * reboot the system. This is called when we know we are in |
| 307 | * trouble so this is our best effort to reboot. This is |
| 308 | * safe to call in interrupt context. |
| 309 | */ |
Eric W. Biederman | 7c90347 | 2005-07-26 11:29:55 -0600 | [diff] [blame] | 310 | void emergency_restart(void) |
| 311 | { |
Seiji Aguchi | 04c6862 | 2011-01-12 16:59:30 -0800 | [diff] [blame] | 312 | kmsg_dump(KMSG_DUMP_EMERG); |
Eric W. Biederman | 7c90347 | 2005-07-26 11:29:55 -0600 | [diff] [blame] | 313 | machine_emergency_restart(); |
| 314 | } |
| 315 | EXPORT_SYMBOL_GPL(emergency_restart); |
| 316 | |
Huang Ying | ca195b7 | 2008-08-15 00:40:24 -0700 | [diff] [blame] | 317 | void kernel_restart_prepare(char *cmd) |
Eric W. Biederman | 4a00ea1 | 2005-07-26 11:24:14 -0600 | [diff] [blame] | 318 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 319 | blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd); |
Eric W. Biederman | 4a00ea1 | 2005-07-26 11:24:14 -0600 | [diff] [blame] | 320 | system_state = SYSTEM_RESTART; |
Kay Sievers | b50fa7c | 2011-05-05 13:32:05 +0200 | [diff] [blame] | 321 | usermodehelper_disable(); |
Eric W. Biederman | 4a00ea1 | 2005-07-26 11:24:14 -0600 | [diff] [blame] | 322 | device_shutdown(); |
Eric W. Biederman | e4c9433 | 2005-09-22 21:43:45 -0700 | [diff] [blame] | 323 | } |
Randy Dunlap | 1e5d533 | 2005-11-07 01:01:06 -0800 | [diff] [blame] | 324 | |
| 325 | /** |
Amerigo Wang | c5f4175 | 2011-07-25 17:13:10 -0700 | [diff] [blame] | 326 | * register_reboot_notifier - Register function to be called at reboot time |
| 327 | * @nb: Info about notifier function to be called |
| 328 | * |
| 329 | * Registers a function with the list of functions |
| 330 | * to be called at reboot time. |
| 331 | * |
| 332 | * Currently always returns zero, as blocking_notifier_chain_register() |
| 333 | * always returns zero. |
| 334 | */ |
| 335 | int register_reboot_notifier(struct notifier_block *nb) |
| 336 | { |
| 337 | return blocking_notifier_chain_register(&reboot_notifier_list, nb); |
| 338 | } |
| 339 | EXPORT_SYMBOL(register_reboot_notifier); |
| 340 | |
| 341 | /** |
| 342 | * unregister_reboot_notifier - Unregister previously registered reboot notifier |
| 343 | * @nb: Hook to be unregistered |
| 344 | * |
| 345 | * Unregisters a previously registered reboot |
| 346 | * notifier function. |
| 347 | * |
| 348 | * Returns zero on success, or %-ENOENT on failure. |
| 349 | */ |
| 350 | int unregister_reboot_notifier(struct notifier_block *nb) |
| 351 | { |
| 352 | return blocking_notifier_chain_unregister(&reboot_notifier_list, nb); |
| 353 | } |
| 354 | EXPORT_SYMBOL(unregister_reboot_notifier); |
| 355 | |
Robin Holt | fc1cbc7 | 2013-06-12 14:04:37 -0700 | [diff] [blame] | 356 | /* Add backwards compatibility for stable trees. */ |
| 357 | #ifndef PF_NO_SETAFFINITY |
| 358 | #define PF_NO_SETAFFINITY PF_THREAD_BOUND |
| 359 | #endif |
| 360 | |
| 361 | static void migrate_to_reboot_cpu(void) |
| 362 | { |
| 363 | /* The boot cpu is always logical cpu 0 */ |
| 364 | int cpu = 0; |
| 365 | |
| 366 | cpu_hotplug_disable(); |
| 367 | |
| 368 | /* Make certain the cpu I'm about to reboot on is online */ |
| 369 | if (!cpu_online(cpu)) |
| 370 | cpu = cpumask_first(cpu_online_mask); |
| 371 | |
| 372 | /* Prevent races with other tasks migrating this task */ |
| 373 | current->flags |= PF_NO_SETAFFINITY; |
| 374 | |
| 375 | /* Make certain I only run on the appropriate processor */ |
| 376 | set_cpus_allowed_ptr(current, cpumask_of(cpu)); |
| 377 | } |
| 378 | |
Amerigo Wang | c5f4175 | 2011-07-25 17:13:10 -0700 | [diff] [blame] | 379 | /** |
Randy Dunlap | 1e5d533 | 2005-11-07 01:01:06 -0800 | [diff] [blame] | 380 | * kernel_restart - reboot the system |
| 381 | * @cmd: pointer to buffer containing command to execute for restart |
Randy Dunlap | b8887e6 | 2005-11-07 01:01:07 -0800 | [diff] [blame] | 382 | * or %NULL |
Randy Dunlap | 1e5d533 | 2005-11-07 01:01:06 -0800 | [diff] [blame] | 383 | * |
| 384 | * Shutdown everything and perform a clean reboot. |
| 385 | * This is not safe to call in interrupt context. |
| 386 | */ |
Eric W. Biederman | e4c9433 | 2005-09-22 21:43:45 -0700 | [diff] [blame] | 387 | void kernel_restart(char *cmd) |
| 388 | { |
| 389 | kernel_restart_prepare(cmd); |
Robin Holt | fc1cbc7 | 2013-06-12 14:04:37 -0700 | [diff] [blame] | 390 | migrate_to_reboot_cpu(); |
Huacai Chen | e3573b2 | 2013-04-07 02:14:14 +0000 | [diff] [blame] | 391 | syscore_shutdown(); |
Cal Peake | 756184b | 2006-09-30 23:27:24 -0700 | [diff] [blame] | 392 | if (!cmd) |
Eric W. Biederman | 4a00ea1 | 2005-07-26 11:24:14 -0600 | [diff] [blame] | 393 | printk(KERN_EMERG "Restarting system.\n"); |
Cal Peake | 756184b | 2006-09-30 23:27:24 -0700 | [diff] [blame] | 394 | else |
Eric W. Biederman | 4a00ea1 | 2005-07-26 11:24:14 -0600 | [diff] [blame] | 395 | printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd); |
Seiji Aguchi | 04c6862 | 2011-01-12 16:59:30 -0800 | [diff] [blame] | 396 | kmsg_dump(KMSG_DUMP_RESTART); |
Eric W. Biederman | 4a00ea1 | 2005-07-26 11:24:14 -0600 | [diff] [blame] | 397 | machine_restart(cmd); |
| 398 | } |
| 399 | EXPORT_SYMBOL_GPL(kernel_restart); |
| 400 | |
Adrian Bunk | 4ef7229 | 2008-02-04 22:30:06 -0800 | [diff] [blame] | 401 | static void kernel_shutdown_prepare(enum system_states state) |
Alexey Starikovskiy | 729b4d4 | 2005-12-01 04:29:00 -0500 | [diff] [blame] | 402 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 403 | blocking_notifier_call_chain(&reboot_notifier_list, |
Alexey Starikovskiy | 729b4d4 | 2005-12-01 04:29:00 -0500 | [diff] [blame] | 404 | (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL); |
| 405 | system_state = state; |
Kay Sievers | b50fa7c | 2011-05-05 13:32:05 +0200 | [diff] [blame] | 406 | usermodehelper_disable(); |
Alexey Starikovskiy | 729b4d4 | 2005-12-01 04:29:00 -0500 | [diff] [blame] | 407 | device_shutdown(); |
| 408 | } |
Eric W. Biederman | e4c9433 | 2005-09-22 21:43:45 -0700 | [diff] [blame] | 409 | /** |
| 410 | * kernel_halt - halt the system |
| 411 | * |
| 412 | * Shutdown everything and perform a clean system halt. |
| 413 | */ |
Eric W. Biederman | e4c9433 | 2005-09-22 21:43:45 -0700 | [diff] [blame] | 414 | void kernel_halt(void) |
| 415 | { |
Alexey Starikovskiy | 729b4d4 | 2005-12-01 04:29:00 -0500 | [diff] [blame] | 416 | kernel_shutdown_prepare(SYSTEM_HALT); |
Robin Holt | fc1cbc7 | 2013-06-12 14:04:37 -0700 | [diff] [blame] | 417 | migrate_to_reboot_cpu(); |
Rafael J. Wysocki | 40dc166 | 2011-03-15 00:43:46 +0100 | [diff] [blame] | 418 | syscore_shutdown(); |
Eric W. Biederman | 4a00ea1 | 2005-07-26 11:24:14 -0600 | [diff] [blame] | 419 | printk(KERN_EMERG "System halted.\n"); |
Seiji Aguchi | 04c6862 | 2011-01-12 16:59:30 -0800 | [diff] [blame] | 420 | kmsg_dump(KMSG_DUMP_HALT); |
Eric W. Biederman | 4a00ea1 | 2005-07-26 11:24:14 -0600 | [diff] [blame] | 421 | machine_halt(); |
| 422 | } |
Alexey Starikovskiy | 729b4d4 | 2005-12-01 04:29:00 -0500 | [diff] [blame] | 423 | |
Eric W. Biederman | 4a00ea1 | 2005-07-26 11:24:14 -0600 | [diff] [blame] | 424 | EXPORT_SYMBOL_GPL(kernel_halt); |
| 425 | |
Eric W. Biederman | e4c9433 | 2005-09-22 21:43:45 -0700 | [diff] [blame] | 426 | /** |
| 427 | * kernel_power_off - power_off the system |
| 428 | * |
| 429 | * Shutdown everything and perform a clean system power_off. |
| 430 | */ |
Eric W. Biederman | e4c9433 | 2005-09-22 21:43:45 -0700 | [diff] [blame] | 431 | void kernel_power_off(void) |
| 432 | { |
Alexey Starikovskiy | 729b4d4 | 2005-12-01 04:29:00 -0500 | [diff] [blame] | 433 | kernel_shutdown_prepare(SYSTEM_POWER_OFF); |
Rafael J. Wysocki | bd804eb | 2007-07-19 01:47:40 -0700 | [diff] [blame] | 434 | if (pm_power_off_prepare) |
| 435 | pm_power_off_prepare(); |
Robin Holt | fc1cbc7 | 2013-06-12 14:04:37 -0700 | [diff] [blame] | 436 | migrate_to_reboot_cpu(); |
Rafael J. Wysocki | 40dc166 | 2011-03-15 00:43:46 +0100 | [diff] [blame] | 437 | syscore_shutdown(); |
Eric W. Biederman | 4a00ea1 | 2005-07-26 11:24:14 -0600 | [diff] [blame] | 438 | printk(KERN_EMERG "Power down.\n"); |
Seiji Aguchi | 04c6862 | 2011-01-12 16:59:30 -0800 | [diff] [blame] | 439 | kmsg_dump(KMSG_DUMP_POWEROFF); |
Eric W. Biederman | 4a00ea1 | 2005-07-26 11:24:14 -0600 | [diff] [blame] | 440 | machine_power_off(); |
| 441 | } |
| 442 | EXPORT_SYMBOL_GPL(kernel_power_off); |
Thomas Gleixner | 6f15fa5 | 2009-10-09 20:31:33 +0200 | [diff] [blame] | 443 | |
| 444 | static DEFINE_MUTEX(reboot_mutex); |
| 445 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | /* |
| 447 | * Reboot system call: for obvious reasons only root may call it, |
| 448 | * and even root needs to set up some magic numbers in the registers |
| 449 | * so that some mistake won't make this reboot the whole machine. |
| 450 | * You can also set the meaning of the ctrl-alt-del-key here. |
| 451 | * |
| 452 | * reboot doesn't sync: do that yourself before calling this. |
| 453 | */ |
Heiko Carstens | 754fe8d | 2009-01-14 14:14:09 +0100 | [diff] [blame] | 454 | SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd, |
| 455 | void __user *, arg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | { |
| 457 | char buffer[256]; |
Andi Kleen | 3d26dcf | 2009-04-13 14:40:08 -0700 | [diff] [blame] | 458 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | |
| 460 | /* We only trust the superuser with rebooting the system. */ |
| 461 | if (!capable(CAP_SYS_BOOT)) |
| 462 | return -EPERM; |
| 463 | |
| 464 | /* For safety, we require "magic" arguments. */ |
| 465 | if (magic1 != LINUX_REBOOT_MAGIC1 || |
| 466 | (magic2 != LINUX_REBOOT_MAGIC2 && |
| 467 | magic2 != LINUX_REBOOT_MAGIC2A && |
| 468 | magic2 != LINUX_REBOOT_MAGIC2B && |
| 469 | magic2 != LINUX_REBOOT_MAGIC2C)) |
| 470 | return -EINVAL; |
| 471 | |
Daniel Lezcano | cf3f892 | 2012-03-28 14:42:51 -0700 | [diff] [blame] | 472 | /* |
| 473 | * If pid namespaces are enabled and the current task is in a child |
| 474 | * pid_namespace, the command is handled by reboot_pid_ns() which will |
| 475 | * call do_exit(). |
| 476 | */ |
| 477 | ret = reboot_pid_ns(task_active_pid_ns(current), cmd); |
| 478 | if (ret) |
| 479 | return ret; |
| 480 | |
Eric W. Biederman | 5e38291 | 2006-01-08 01:03:46 -0800 | [diff] [blame] | 481 | /* Instead of trying to make the power_off code look like |
| 482 | * halt when pm_power_off is not set do it the easy way. |
| 483 | */ |
| 484 | if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off) |
| 485 | cmd = LINUX_REBOOT_CMD_HALT; |
| 486 | |
Thomas Gleixner | 6f15fa5 | 2009-10-09 20:31:33 +0200 | [diff] [blame] | 487 | mutex_lock(&reboot_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | switch (cmd) { |
| 489 | case LINUX_REBOOT_CMD_RESTART: |
Eric W. Biederman | 4a00ea1 | 2005-07-26 11:24:14 -0600 | [diff] [blame] | 490 | kernel_restart(NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | break; |
| 492 | |
| 493 | case LINUX_REBOOT_CMD_CAD_ON: |
| 494 | C_A_D = 1; |
| 495 | break; |
| 496 | |
| 497 | case LINUX_REBOOT_CMD_CAD_OFF: |
| 498 | C_A_D = 0; |
| 499 | break; |
| 500 | |
| 501 | case LINUX_REBOOT_CMD_HALT: |
Eric W. Biederman | 4a00ea1 | 2005-07-26 11:24:14 -0600 | [diff] [blame] | 502 | kernel_halt(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | do_exit(0); |
Andi Kleen | 3d26dcf | 2009-04-13 14:40:08 -0700 | [diff] [blame] | 504 | panic("cannot halt"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
| 506 | case LINUX_REBOOT_CMD_POWER_OFF: |
Eric W. Biederman | 4a00ea1 | 2005-07-26 11:24:14 -0600 | [diff] [blame] | 507 | kernel_power_off(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | do_exit(0); |
| 509 | break; |
| 510 | |
| 511 | case LINUX_REBOOT_CMD_RESTART2: |
| 512 | if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) { |
Thomas Gleixner | 6f15fa5 | 2009-10-09 20:31:33 +0200 | [diff] [blame] | 513 | ret = -EFAULT; |
| 514 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | } |
| 516 | buffer[sizeof(buffer) - 1] = '\0'; |
| 517 | |
Eric W. Biederman | 4a00ea1 | 2005-07-26 11:24:14 -0600 | [diff] [blame] | 518 | kernel_restart(buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | break; |
| 520 | |
Huang Ying | 3ab8352 | 2008-07-25 19:45:07 -0700 | [diff] [blame] | 521 | #ifdef CONFIG_KEXEC |
Eric W. Biederman | dc009d9 | 2005-06-25 14:57:52 -0700 | [diff] [blame] | 522 | case LINUX_REBOOT_CMD_KEXEC: |
Andi Kleen | 3d26dcf | 2009-04-13 14:40:08 -0700 | [diff] [blame] | 523 | ret = kernel_kexec(); |
| 524 | break; |
Huang Ying | 3ab8352 | 2008-07-25 19:45:07 -0700 | [diff] [blame] | 525 | #endif |
Eric W. Biederman | 4a00ea1 | 2005-07-26 11:24:14 -0600 | [diff] [blame] | 526 | |
Rafael J. Wysocki | b0cb1a1 | 2007-07-29 23:24:36 +0200 | [diff] [blame] | 527 | #ifdef CONFIG_HIBERNATION |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | case LINUX_REBOOT_CMD_SW_SUSPEND: |
Andi Kleen | 3d26dcf | 2009-04-13 14:40:08 -0700 | [diff] [blame] | 529 | ret = hibernate(); |
| 530 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | #endif |
| 532 | |
| 533 | default: |
Andi Kleen | 3d26dcf | 2009-04-13 14:40:08 -0700 | [diff] [blame] | 534 | ret = -EINVAL; |
| 535 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 536 | } |
Thomas Gleixner | 6f15fa5 | 2009-10-09 20:31:33 +0200 | [diff] [blame] | 537 | mutex_unlock(&reboot_mutex); |
Andi Kleen | 3d26dcf | 2009-04-13 14:40:08 -0700 | [diff] [blame] | 538 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | } |
| 540 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 541 | static void deferred_cad(struct work_struct *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | { |
Eric W. Biederman | abcd9e5 | 2005-07-26 11:27:34 -0600 | [diff] [blame] | 543 | kernel_restart(NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | /* |
| 547 | * This function gets called by ctrl-alt-del - ie the keyboard interrupt. |
| 548 | * As it's called within an interrupt, it may NOT sync: the only choice |
| 549 | * is whether to reboot at once, or just ignore the ctrl-alt-del. |
| 550 | */ |
| 551 | void ctrl_alt_del(void) |
| 552 | { |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 553 | static DECLARE_WORK(cad_work, deferred_cad); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | |
| 555 | if (C_A_D) |
| 556 | schedule_work(&cad_work); |
| 557 | else |
Cedric Le Goater | 9ec5209 | 2006-10-02 02:19:00 -0700 | [diff] [blame] | 558 | kill_cad_pid(SIGINT, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | } |
| 560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | /* |
| 562 | * Unprivileged users may change the real gid to the effective gid |
| 563 | * or vice versa. (BSD-style) |
| 564 | * |
| 565 | * If you set the real gid at all, or set the effective gid to a value not |
| 566 | * equal to the real gid, then the saved gid is set to the new effective gid. |
| 567 | * |
| 568 | * This makes it possible for a setgid program to completely drop its |
| 569 | * privileges, which is often a useful assertion to make when you are doing |
| 570 | * a security audit over a program. |
| 571 | * |
| 572 | * The general idea is that a program which uses just setregid() will be |
| 573 | * 100% compatible with BSD. A program which uses just setgid() will be |
| 574 | * 100% compatible with POSIX with saved IDs. |
| 575 | * |
| 576 | * SMP: There are not races, the GIDs are checked only by filesystem |
| 577 | * operations (as far as semantic preservation is concerned). |
| 578 | */ |
Heiko Carstens | ae1251a | 2009-01-14 14:14:05 +0100 | [diff] [blame] | 579 | SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 581 | const struct cred *old; |
| 582 | struct cred *new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | int retval; |
| 584 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 585 | new = prepare_creds(); |
| 586 | if (!new) |
| 587 | return -ENOMEM; |
| 588 | old = current_cred(); |
| 589 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 590 | retval = -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | if (rgid != (gid_t) -1) { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 592 | if (old->gid == rgid || |
| 593 | old->egid == rgid || |
Serge E. Hallyn | fc832ad | 2011-03-23 16:43:22 -0700 | [diff] [blame] | 594 | nsown_capable(CAP_SETGID)) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 595 | new->gid = rgid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | else |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 597 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | } |
| 599 | if (egid != (gid_t) -1) { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 600 | if (old->gid == egid || |
| 601 | old->egid == egid || |
| 602 | old->sgid == egid || |
Serge E. Hallyn | fc832ad | 2011-03-23 16:43:22 -0700 | [diff] [blame] | 603 | nsown_capable(CAP_SETGID)) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 604 | new->egid = egid; |
Cal Peake | 756184b | 2006-09-30 23:27:24 -0700 | [diff] [blame] | 605 | else |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 606 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | } |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 608 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | if (rgid != (gid_t) -1 || |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 610 | (egid != (gid_t) -1 && egid != old->gid)) |
| 611 | new->sgid = new->egid; |
| 612 | new->fsgid = new->egid; |
| 613 | |
| 614 | return commit_creds(new); |
| 615 | |
| 616 | error: |
| 617 | abort_creds(new); |
| 618 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | /* |
| 622 | * setgid() is implemented like SysV w/ SAVED_IDS |
| 623 | * |
| 624 | * SMP: Same implicit races as above. |
| 625 | */ |
Heiko Carstens | ae1251a | 2009-01-14 14:14:05 +0100 | [diff] [blame] | 626 | SYSCALL_DEFINE1(setgid, gid_t, gid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 628 | const struct cred *old; |
| 629 | struct cred *new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | int retval; |
| 631 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 632 | new = prepare_creds(); |
| 633 | if (!new) |
| 634 | return -ENOMEM; |
| 635 | old = current_cred(); |
| 636 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 637 | retval = -EPERM; |
Serge E. Hallyn | fc832ad | 2011-03-23 16:43:22 -0700 | [diff] [blame] | 638 | if (nsown_capable(CAP_SETGID)) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 639 | new->gid = new->egid = new->sgid = new->fsgid = gid; |
| 640 | else if (gid == old->gid || gid == old->sgid) |
| 641 | new->egid = new->fsgid = gid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | else |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 643 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 645 | return commit_creds(new); |
| 646 | |
| 647 | error: |
| 648 | abort_creds(new); |
| 649 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 650 | } |
Dhaval Giani | 54e9912 | 2009-02-27 15:13:54 +0530 | [diff] [blame] | 651 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 652 | /* |
| 653 | * change the user struct in a credentials set to match the new UID |
| 654 | */ |
| 655 | static int set_user(struct cred *new) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | { |
| 657 | struct user_struct *new_user; |
| 658 | |
Serge Hallyn | 18b6e04 | 2008-10-15 16:38:45 -0500 | [diff] [blame] | 659 | new_user = alloc_uid(current_user_ns(), new->uid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | if (!new_user) |
| 661 | return -EAGAIN; |
| 662 | |
Vasiliy Kulikov | 72fa599 | 2011-08-08 19:02:04 +0400 | [diff] [blame] | 663 | /* |
| 664 | * We don't fail in case of NPROC limit excess here because too many |
| 665 | * poorly written programs don't check set*uid() return code, assuming |
| 666 | * it never fails if called by root. We may still enforce NPROC limit |
| 667 | * for programs doing set*uid()+execve() by harmlessly deferring the |
| 668 | * failure to the execve() stage. |
| 669 | */ |
Jiri Slaby | 78d7d40 | 2010-03-05 13:42:54 -0800 | [diff] [blame] | 670 | if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) && |
Vasiliy Kulikov | 72fa599 | 2011-08-08 19:02:04 +0400 | [diff] [blame] | 671 | new_user != INIT_USER) |
| 672 | current->flags |= PF_NPROC_EXCEEDED; |
| 673 | else |
| 674 | current->flags &= ~PF_NPROC_EXCEEDED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 676 | free_uid(new->user); |
| 677 | new->user = new_user; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | return 0; |
| 679 | } |
| 680 | |
| 681 | /* |
| 682 | * Unprivileged users may change the real uid to the effective uid |
| 683 | * or vice versa. (BSD-style) |
| 684 | * |
| 685 | * If you set the real uid at all, or set the effective uid to a value not |
| 686 | * equal to the real uid, then the saved uid is set to the new effective uid. |
| 687 | * |
| 688 | * This makes it possible for a setuid program to completely drop its |
| 689 | * privileges, which is often a useful assertion to make when you are doing |
| 690 | * a security audit over a program. |
| 691 | * |
| 692 | * The general idea is that a program which uses just setreuid() will be |
| 693 | * 100% compatible with BSD. A program which uses just setuid() will be |
| 694 | * 100% compatible with POSIX with saved IDs. |
| 695 | */ |
Heiko Carstens | ae1251a | 2009-01-14 14:14:05 +0100 | [diff] [blame] | 696 | SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 698 | const struct cred *old; |
| 699 | struct cred *new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | int retval; |
| 701 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 702 | new = prepare_creds(); |
| 703 | if (!new) |
| 704 | return -ENOMEM; |
| 705 | old = current_cred(); |
| 706 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 707 | retval = -EPERM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | if (ruid != (uid_t) -1) { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 709 | new->uid = ruid; |
| 710 | if (old->uid != ruid && |
| 711 | old->euid != ruid && |
Serge E. Hallyn | fc832ad | 2011-03-23 16:43:22 -0700 | [diff] [blame] | 712 | !nsown_capable(CAP_SETUID)) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 713 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | if (euid != (uid_t) -1) { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 717 | new->euid = euid; |
| 718 | if (old->uid != euid && |
| 719 | old->euid != euid && |
| 720 | old->suid != euid && |
Serge E. Hallyn | fc832ad | 2011-03-23 16:43:22 -0700 | [diff] [blame] | 721 | !nsown_capable(CAP_SETUID)) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 722 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | } |
| 724 | |
Dhaval Giani | 54e9912 | 2009-02-27 15:13:54 +0530 | [diff] [blame] | 725 | if (new->uid != old->uid) { |
| 726 | retval = set_user(new); |
| 727 | if (retval < 0) |
| 728 | goto error; |
| 729 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | if (ruid != (uid_t) -1 || |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 731 | (euid != (uid_t) -1 && euid != old->uid)) |
| 732 | new->suid = new->euid; |
| 733 | new->fsuid = new->euid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 735 | retval = security_task_fix_setuid(new, old, LSM_SETID_RE); |
| 736 | if (retval < 0) |
| 737 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 739 | return commit_creds(new); |
| 740 | |
| 741 | error: |
| 742 | abort_creds(new); |
| 743 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | |
| 746 | /* |
| 747 | * setuid() is implemented like SysV with SAVED_IDS |
| 748 | * |
| 749 | * Note that SAVED_ID's is deficient in that a setuid root program |
| 750 | * like sendmail, for example, cannot set its uid to be a normal |
| 751 | * user and then switch back, because if you're root, setuid() sets |
| 752 | * the saved uid too. If you don't like this, blame the bright people |
| 753 | * in the POSIX committee and/or USG. Note that the BSD-style setreuid() |
| 754 | * will allow a root program to temporarily drop privileges and be able to |
| 755 | * regain them by swapping the real and effective uid. |
| 756 | */ |
Heiko Carstens | ae1251a | 2009-01-14 14:14:05 +0100 | [diff] [blame] | 757 | SYSCALL_DEFINE1(setuid, uid_t, uid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 759 | const struct cred *old; |
| 760 | struct cred *new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | int retval; |
| 762 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 763 | new = prepare_creds(); |
| 764 | if (!new) |
| 765 | return -ENOMEM; |
| 766 | old = current_cred(); |
| 767 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 768 | retval = -EPERM; |
Serge E. Hallyn | fc832ad | 2011-03-23 16:43:22 -0700 | [diff] [blame] | 769 | if (nsown_capable(CAP_SETUID)) { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 770 | new->suid = new->uid = uid; |
Dhaval Giani | 54e9912 | 2009-02-27 15:13:54 +0530 | [diff] [blame] | 771 | if (uid != old->uid) { |
| 772 | retval = set_user(new); |
| 773 | if (retval < 0) |
| 774 | goto error; |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 775 | } |
| 776 | } else if (uid != old->uid && uid != new->suid) { |
| 777 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 780 | new->fsuid = new->euid = uid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 782 | retval = security_task_fix_setuid(new, old, LSM_SETID_ID); |
| 783 | if (retval < 0) |
| 784 | goto error; |
| 785 | |
| 786 | return commit_creds(new); |
| 787 | |
| 788 | error: |
| 789 | abort_creds(new); |
| 790 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | |
| 794 | /* |
| 795 | * This function implements a generic ability to update ruid, euid, |
| 796 | * and suid. This allows you to implement the 4.4 compatible seteuid(). |
| 797 | */ |
Heiko Carstens | ae1251a | 2009-01-14 14:14:05 +0100 | [diff] [blame] | 798 | SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 800 | const struct cred *old; |
| 801 | struct cred *new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | int retval; |
| 803 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 804 | new = prepare_creds(); |
| 805 | if (!new) |
| 806 | return -ENOMEM; |
| 807 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 808 | old = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 810 | retval = -EPERM; |
Serge E. Hallyn | fc832ad | 2011-03-23 16:43:22 -0700 | [diff] [blame] | 811 | if (!nsown_capable(CAP_SETUID)) { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 812 | if (ruid != (uid_t) -1 && ruid != old->uid && |
| 813 | ruid != old->euid && ruid != old->suid) |
| 814 | goto error; |
| 815 | if (euid != (uid_t) -1 && euid != old->uid && |
| 816 | euid != old->euid && euid != old->suid) |
| 817 | goto error; |
| 818 | if (suid != (uid_t) -1 && suid != old->uid && |
| 819 | suid != old->euid && suid != old->suid) |
| 820 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | } |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 822 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | if (ruid != (uid_t) -1) { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 824 | new->uid = ruid; |
Dhaval Giani | 54e9912 | 2009-02-27 15:13:54 +0530 | [diff] [blame] | 825 | if (ruid != old->uid) { |
| 826 | retval = set_user(new); |
| 827 | if (retval < 0) |
| 828 | goto error; |
| 829 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | } |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 831 | if (euid != (uid_t) -1) |
| 832 | new->euid = euid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | if (suid != (uid_t) -1) |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 834 | new->suid = suid; |
| 835 | new->fsuid = new->euid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 837 | retval = security_task_fix_setuid(new, old, LSM_SETID_RES); |
| 838 | if (retval < 0) |
| 839 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 841 | return commit_creds(new); |
| 842 | |
| 843 | error: |
| 844 | abort_creds(new); |
| 845 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | } |
| 847 | |
Heiko Carstens | dbf040d | 2009-01-14 14:14:04 +0100 | [diff] [blame] | 848 | SYSCALL_DEFINE3(getresuid, uid_t __user *, ruid, uid_t __user *, euid, uid_t __user *, suid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | { |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 850 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | int retval; |
| 852 | |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 853 | if (!(retval = put_user(cred->uid, ruid)) && |
| 854 | !(retval = put_user(cred->euid, euid))) |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 855 | retval = put_user(cred->suid, suid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | |
| 857 | return retval; |
| 858 | } |
| 859 | |
| 860 | /* |
| 861 | * Same as above, but for rgid, egid, sgid. |
| 862 | */ |
Heiko Carstens | ae1251a | 2009-01-14 14:14:05 +0100 | [diff] [blame] | 863 | SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 865 | const struct cred *old; |
| 866 | struct cred *new; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | int retval; |
| 868 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 869 | new = prepare_creds(); |
| 870 | if (!new) |
| 871 | return -ENOMEM; |
| 872 | old = current_cred(); |
| 873 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 874 | retval = -EPERM; |
Serge E. Hallyn | fc832ad | 2011-03-23 16:43:22 -0700 | [diff] [blame] | 875 | if (!nsown_capable(CAP_SETGID)) { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 876 | if (rgid != (gid_t) -1 && rgid != old->gid && |
| 877 | rgid != old->egid && rgid != old->sgid) |
| 878 | goto error; |
| 879 | if (egid != (gid_t) -1 && egid != old->gid && |
| 880 | egid != old->egid && egid != old->sgid) |
| 881 | goto error; |
| 882 | if (sgid != (gid_t) -1 && sgid != old->gid && |
| 883 | sgid != old->egid && sgid != old->sgid) |
| 884 | goto error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 887 | if (rgid != (gid_t) -1) |
| 888 | new->gid = rgid; |
| 889 | if (egid != (gid_t) -1) |
| 890 | new->egid = egid; |
| 891 | if (sgid != (gid_t) -1) |
| 892 | new->sgid = sgid; |
| 893 | new->fsgid = new->egid; |
| 894 | |
| 895 | return commit_creds(new); |
| 896 | |
| 897 | error: |
| 898 | abort_creds(new); |
| 899 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | } |
| 901 | |
Heiko Carstens | dbf040d | 2009-01-14 14:14:04 +0100 | [diff] [blame] | 902 | SYSCALL_DEFINE3(getresgid, gid_t __user *, rgid, gid_t __user *, egid, gid_t __user *, sgid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | { |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 904 | const struct cred *cred = current_cred(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | int retval; |
| 906 | |
David Howells | 86a264a | 2008-11-14 10:39:18 +1100 | [diff] [blame] | 907 | if (!(retval = put_user(cred->gid, rgid)) && |
| 908 | !(retval = put_user(cred->egid, egid))) |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 909 | retval = put_user(cred->sgid, sgid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | |
| 911 | return retval; |
| 912 | } |
| 913 | |
| 914 | |
| 915 | /* |
| 916 | * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This |
| 917 | * is used for "access()" and for the NFS daemon (letting nfsd stay at |
| 918 | * whatever uid it wants to). It normally shadows "euid", except when |
| 919 | * explicitly set by setfsuid() or for access.. |
| 920 | */ |
Heiko Carstens | ae1251a | 2009-01-14 14:14:05 +0100 | [diff] [blame] | 921 | SYSCALL_DEFINE1(setfsuid, uid_t, uid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 923 | const struct cred *old; |
| 924 | struct cred *new; |
| 925 | uid_t old_fsuid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 927 | new = prepare_creds(); |
| 928 | if (!new) |
| 929 | return current_fsuid(); |
| 930 | old = current_cred(); |
| 931 | old_fsuid = old->fsuid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 933 | if (uid == old->uid || uid == old->euid || |
| 934 | uid == old->suid || uid == old->fsuid || |
Serge E. Hallyn | fc832ad | 2011-03-23 16:43:22 -0700 | [diff] [blame] | 935 | nsown_capable(CAP_SETUID)) { |
Cal Peake | 756184b | 2006-09-30 23:27:24 -0700 | [diff] [blame] | 936 | if (uid != old_fsuid) { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 937 | new->fsuid = uid; |
| 938 | if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0) |
| 939 | goto change_okay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | } |
| 942 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 943 | abort_creds(new); |
| 944 | return old_fsuid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 946 | change_okay: |
| 947 | commit_creds(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | return old_fsuid; |
| 949 | } |
| 950 | |
| 951 | /* |
John Anthony Kazos Jr | f42df9e | 2007-05-09 08:23:08 +0200 | [diff] [blame] | 952 | * Samma på svenska.. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | */ |
Heiko Carstens | ae1251a | 2009-01-14 14:14:05 +0100 | [diff] [blame] | 954 | SYSCALL_DEFINE1(setfsgid, gid_t, gid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 956 | const struct cred *old; |
| 957 | struct cred *new; |
| 958 | gid_t old_fsgid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 960 | new = prepare_creds(); |
| 961 | if (!new) |
| 962 | return current_fsgid(); |
| 963 | old = current_cred(); |
| 964 | old_fsgid = old->fsgid; |
| 965 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 966 | if (gid == old->gid || gid == old->egid || |
| 967 | gid == old->sgid || gid == old->fsgid || |
Serge E. Hallyn | fc832ad | 2011-03-23 16:43:22 -0700 | [diff] [blame] | 968 | nsown_capable(CAP_SETGID)) { |
Cal Peake | 756184b | 2006-09-30 23:27:24 -0700 | [diff] [blame] | 969 | if (gid != old_fsgid) { |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 970 | new->fsgid = gid; |
| 971 | goto change_okay; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | } |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 974 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 975 | abort_creds(new); |
| 976 | return old_fsgid; |
| 977 | |
| 978 | change_okay: |
| 979 | commit_creds(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | return old_fsgid; |
| 981 | } |
| 982 | |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 983 | void do_sys_times(struct tms *tms) |
| 984 | { |
Hidetoshi Seto | 0cf55e1 | 2009-12-02 17:28:07 +0900 | [diff] [blame] | 985 | cputime_t tgutime, tgstime, cutime, cstime; |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 986 | |
Oleg Nesterov | 2b5fe6d | 2008-11-17 15:40:08 +0100 | [diff] [blame] | 987 | spin_lock_irq(¤t->sighand->siglock); |
Hidetoshi Seto | 0cf55e1 | 2009-12-02 17:28:07 +0900 | [diff] [blame] | 988 | thread_group_times(current, &tgutime, &tgstime); |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 989 | cutime = current->signal->cutime; |
| 990 | cstime = current->signal->cstime; |
| 991 | spin_unlock_irq(¤t->sighand->siglock); |
Hidetoshi Seto | 0cf55e1 | 2009-12-02 17:28:07 +0900 | [diff] [blame] | 992 | tms->tms_utime = cputime_to_clock_t(tgutime); |
| 993 | tms->tms_stime = cputime_to_clock_t(tgstime); |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 994 | tms->tms_cutime = cputime_to_clock_t(cutime); |
| 995 | tms->tms_cstime = cputime_to_clock_t(cstime); |
| 996 | } |
| 997 | |
Heiko Carstens | 58fd3aa | 2009-01-14 14:14:03 +0100 | [diff] [blame] | 998 | SYSCALL_DEFINE1(times, struct tms __user *, tbuf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | if (tbuf) { |
| 1001 | struct tms tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1003 | do_sys_times(&tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | if (copy_to_user(tbuf, &tmp, sizeof(struct tms))) |
| 1005 | return -EFAULT; |
| 1006 | } |
Paul Mackerras | e3d5a27 | 2009-01-06 14:41:02 -0800 | [diff] [blame] | 1007 | force_successful_syscall_return(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | return (long) jiffies_64_to_clock_t(get_jiffies_64()); |
| 1009 | } |
| 1010 | |
| 1011 | /* |
| 1012 | * This needs some heavy checking ... |
| 1013 | * I just haven't the stomach for it. I also don't fully |
| 1014 | * understand sessions/pgrp etc. Let somebody who does explain it. |
| 1015 | * |
| 1016 | * OK, I think I have the protection semantics right.... this is really |
| 1017 | * only important on a multi-user system anyway, to make sure one user |
| 1018 | * can't send a signal to a process owned by another. -TYT, 12/12/91 |
| 1019 | * |
| 1020 | * Auch. Had to add the 'did_exec' flag to conform completely to POSIX. |
| 1021 | * LBT 04.03.94 |
| 1022 | */ |
Heiko Carstens | b290ebe | 2009-01-14 14:14:06 +0100 | [diff] [blame] | 1023 | SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | { |
| 1025 | struct task_struct *p; |
Oleg Nesterov | ee0acf9 | 2006-01-08 01:03:53 -0800 | [diff] [blame] | 1026 | struct task_struct *group_leader = current->group_leader; |
Oleg Nesterov | 4e02130 | 2008-02-08 04:19:08 -0800 | [diff] [blame] | 1027 | struct pid *pgrp; |
| 1028 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | |
| 1030 | if (!pid) |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 1031 | pid = task_pid_vnr(group_leader); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | if (!pgid) |
| 1033 | pgid = pid; |
| 1034 | if (pgid < 0) |
| 1035 | return -EINVAL; |
Paul E. McKenney | 950eaac | 2010-08-31 17:00:18 -0700 | [diff] [blame] | 1036 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | |
| 1038 | /* From this point forward we keep holding onto the tasklist lock |
| 1039 | * so that our parent does not change from under us. -DaveM |
| 1040 | */ |
| 1041 | write_lock_irq(&tasklist_lock); |
| 1042 | |
| 1043 | err = -ESRCH; |
Oleg Nesterov | 4e02130 | 2008-02-08 04:19:08 -0800 | [diff] [blame] | 1044 | p = find_task_by_vpid(pid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | if (!p) |
| 1046 | goto out; |
| 1047 | |
| 1048 | err = -EINVAL; |
| 1049 | if (!thread_group_leader(p)) |
| 1050 | goto out; |
| 1051 | |
Oleg Nesterov | 4e02130 | 2008-02-08 04:19:08 -0800 | [diff] [blame] | 1052 | if (same_thread_group(p->real_parent, group_leader)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | err = -EPERM; |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 1054 | if (task_session(p) != task_session(group_leader)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | goto out; |
| 1056 | err = -EACCES; |
| 1057 | if (p->did_exec) |
| 1058 | goto out; |
| 1059 | } else { |
| 1060 | err = -ESRCH; |
Oleg Nesterov | ee0acf9 | 2006-01-08 01:03:53 -0800 | [diff] [blame] | 1061 | if (p != group_leader) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | goto out; |
| 1063 | } |
| 1064 | |
| 1065 | err = -EPERM; |
| 1066 | if (p->signal->leader) |
| 1067 | goto out; |
| 1068 | |
Oleg Nesterov | 4e02130 | 2008-02-08 04:19:08 -0800 | [diff] [blame] | 1069 | pgrp = task_pid(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | if (pgid != pid) { |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 1071 | struct task_struct *g; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | |
Oleg Nesterov | 4e02130 | 2008-02-08 04:19:08 -0800 | [diff] [blame] | 1073 | pgrp = find_vpid(pgid); |
| 1074 | g = pid_task(pgrp, PIDTYPE_PGID); |
Eric W. Biederman | 41487c6 | 2007-02-12 00:53:01 -0800 | [diff] [blame] | 1075 | if (!g || task_session(g) != task_session(group_leader)) |
Oleg Nesterov | f020bc4 | 2006-12-08 02:38:02 -0800 | [diff] [blame] | 1076 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | err = security_task_setpgid(p, pgid); |
| 1080 | if (err) |
| 1081 | goto out; |
| 1082 | |
Oleg Nesterov | 1b0f7ff | 2009-04-02 16:58:39 -0700 | [diff] [blame] | 1083 | if (task_pgrp(p) != pgrp) |
Oleg Nesterov | 83beaf3 | 2008-04-30 00:54:27 -0700 | [diff] [blame] | 1084 | change_pid(p, PIDTYPE_PGID, pgrp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | |
| 1086 | err = 0; |
| 1087 | out: |
| 1088 | /* All paths lead to here, thus we are safe. -DaveM */ |
| 1089 | write_unlock_irq(&tasklist_lock); |
Paul E. McKenney | 950eaac | 2010-08-31 17:00:18 -0700 | [diff] [blame] | 1090 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | return err; |
| 1092 | } |
| 1093 | |
Heiko Carstens | dbf040d | 2009-01-14 14:14:04 +0100 | [diff] [blame] | 1094 | SYSCALL_DEFINE1(getpgid, pid_t, pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | { |
Oleg Nesterov | 12a3de0 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 1096 | struct task_struct *p; |
| 1097 | struct pid *grp; |
| 1098 | int retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | |
Oleg Nesterov | 12a3de0 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 1100 | rcu_read_lock(); |
| 1101 | if (!pid) |
| 1102 | grp = task_pgrp(current); |
| 1103 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | retval = -ESRCH; |
Oleg Nesterov | 12a3de0 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 1105 | p = find_task_by_vpid(pid); |
| 1106 | if (!p) |
| 1107 | goto out; |
| 1108 | grp = task_pgrp(p); |
| 1109 | if (!grp) |
| 1110 | goto out; |
| 1111 | |
| 1112 | retval = security_task_getpgid(p); |
| 1113 | if (retval) |
| 1114 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | } |
Oleg Nesterov | 12a3de0 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 1116 | retval = pid_vnr(grp); |
| 1117 | out: |
| 1118 | rcu_read_unlock(); |
| 1119 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | } |
| 1121 | |
| 1122 | #ifdef __ARCH_WANT_SYS_GETPGRP |
| 1123 | |
Heiko Carstens | dbf040d | 2009-01-14 14:14:04 +0100 | [diff] [blame] | 1124 | SYSCALL_DEFINE0(getpgrp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | { |
Oleg Nesterov | 12a3de0 | 2008-04-30 00:54:29 -0700 | [diff] [blame] | 1126 | return sys_getpgid(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | } |
| 1128 | |
| 1129 | #endif |
| 1130 | |
Heiko Carstens | dbf040d | 2009-01-14 14:14:04 +0100 | [diff] [blame] | 1131 | SYSCALL_DEFINE1(getsid, pid_t, pid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | { |
Oleg Nesterov | 1dd768c | 2008-04-30 00:54:28 -0700 | [diff] [blame] | 1133 | struct task_struct *p; |
| 1134 | struct pid *sid; |
| 1135 | int retval; |
Pavel Emelyanov | b488893 | 2007-10-18 23:40:14 -0700 | [diff] [blame] | 1136 | |
Oleg Nesterov | 1dd768c | 2008-04-30 00:54:28 -0700 | [diff] [blame] | 1137 | rcu_read_lock(); |
| 1138 | if (!pid) |
| 1139 | sid = task_session(current); |
| 1140 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | retval = -ESRCH; |
Oleg Nesterov | 1dd768c | 2008-04-30 00:54:28 -0700 | [diff] [blame] | 1142 | p = find_task_by_vpid(pid); |
| 1143 | if (!p) |
| 1144 | goto out; |
| 1145 | sid = task_session(p); |
| 1146 | if (!sid) |
| 1147 | goto out; |
| 1148 | |
| 1149 | retval = security_task_getsid(p); |
| 1150 | if (retval) |
| 1151 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | } |
Oleg Nesterov | 1dd768c | 2008-04-30 00:54:28 -0700 | [diff] [blame] | 1153 | retval = pid_vnr(sid); |
| 1154 | out: |
| 1155 | rcu_read_unlock(); |
| 1156 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
Heiko Carstens | b290ebe | 2009-01-14 14:14:06 +0100 | [diff] [blame] | 1159 | SYSCALL_DEFINE0(setsid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | { |
Oren Laadan | e19f247 | 2006-01-08 01:03:58 -0800 | [diff] [blame] | 1161 | struct task_struct *group_leader = current->group_leader; |
Oleg Nesterov | e4cc0a9 | 2008-02-08 04:19:09 -0800 | [diff] [blame] | 1162 | struct pid *sid = task_pid(group_leader); |
| 1163 | pid_t session = pid_vnr(sid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | int err = -EPERM; |
| 1165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | write_lock_irq(&tasklist_lock); |
Eric W. Biederman | 390e2ff | 2006-03-31 02:31:33 -0800 | [diff] [blame] | 1167 | /* Fail if I am already a session leader */ |
| 1168 | if (group_leader->signal->leader) |
| 1169 | goto out; |
| 1170 | |
Oleg Nesterov | 430c623 | 2008-02-08 04:19:11 -0800 | [diff] [blame] | 1171 | /* Fail if a process group id already exists that equals the |
| 1172 | * proposed session id. |
Eric W. Biederman | 390e2ff | 2006-03-31 02:31:33 -0800 | [diff] [blame] | 1173 | */ |
Oleg Nesterov | 6806aac | 2008-02-08 04:19:12 -0800 | [diff] [blame] | 1174 | if (pid_task(sid, PIDTYPE_PGID)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | goto out; |
| 1176 | |
Oren Laadan | e19f247 | 2006-01-08 01:03:58 -0800 | [diff] [blame] | 1177 | group_leader->signal->leader = 1; |
Oleg Nesterov | 8520d7c | 2008-02-08 04:19:09 -0800 | [diff] [blame] | 1178 | __set_special_pids(sid); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1179 | |
Alan Cox | 9c9f4de | 2008-10-13 10:37:26 +0100 | [diff] [blame] | 1180 | proc_clear_tty(group_leader); |
Peter Zijlstra | 24ec839 | 2006-12-08 02:36:04 -0800 | [diff] [blame] | 1181 | |
Oleg Nesterov | e4cc0a9 | 2008-02-08 04:19:09 -0800 | [diff] [blame] | 1182 | err = session; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | out: |
| 1184 | write_unlock_irq(&tasklist_lock); |
Mike Galbraith | 5091faa | 2010-11-30 14:18:03 +0100 | [diff] [blame] | 1185 | if (err > 0) { |
Christian Borntraeger | 0d0df59 | 2009-10-26 16:49:34 -0700 | [diff] [blame] | 1186 | proc_sid_connector(group_leader); |
Mike Galbraith | 5091faa | 2010-11-30 14:18:03 +0100 | [diff] [blame] | 1187 | sched_autogroup_create_attach(group_leader); |
| 1188 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | return err; |
| 1190 | } |
| 1191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | DECLARE_RWSEM(uts_sem); |
| 1193 | |
Christoph Hellwig | e28cbf2 | 2010-03-10 15:21:19 -0800 | [diff] [blame] | 1194 | #ifdef COMPAT_UTS_MACHINE |
| 1195 | #define override_architecture(name) \ |
Andreas Schwab | 46da276 | 2010-04-23 13:17:44 -0400 | [diff] [blame] | 1196 | (personality(current->personality) == PER_LINUX32 && \ |
Christoph Hellwig | e28cbf2 | 2010-03-10 15:21:19 -0800 | [diff] [blame] | 1197 | copy_to_user(name->machine, COMPAT_UTS_MACHINE, \ |
| 1198 | sizeof(COMPAT_UTS_MACHINE))) |
| 1199 | #else |
| 1200 | #define override_architecture(name) 0 |
| 1201 | #endif |
| 1202 | |
Andi Kleen | be27425 | 2011-08-19 16:15:10 -0700 | [diff] [blame] | 1203 | /* |
| 1204 | * Work around broken programs that cannot handle "Linux 3.0". |
| 1205 | * Instead we map 3.x to 2.6.40+x, so e.g. 3.0 would be 2.6.40 |
| 1206 | */ |
Kees Cook | 643fde8 | 2012-10-19 13:56:51 -0700 | [diff] [blame] | 1207 | static int override_release(char __user *release, size_t len) |
Andi Kleen | be27425 | 2011-08-19 16:15:10 -0700 | [diff] [blame] | 1208 | { |
| 1209 | int ret = 0; |
Andi Kleen | be27425 | 2011-08-19 16:15:10 -0700 | [diff] [blame] | 1210 | |
| 1211 | if (current->personality & UNAME26) { |
Kees Cook | 643fde8 | 2012-10-19 13:56:51 -0700 | [diff] [blame] | 1212 | const char *rest = UTS_RELEASE; |
| 1213 | char buf[65] = { 0 }; |
Andi Kleen | be27425 | 2011-08-19 16:15:10 -0700 | [diff] [blame] | 1214 | int ndots = 0; |
| 1215 | unsigned v; |
Kees Cook | 643fde8 | 2012-10-19 13:56:51 -0700 | [diff] [blame] | 1216 | size_t copy; |
Andi Kleen | be27425 | 2011-08-19 16:15:10 -0700 | [diff] [blame] | 1217 | |
| 1218 | while (*rest) { |
| 1219 | if (*rest == '.' && ++ndots >= 3) |
| 1220 | break; |
| 1221 | if (!isdigit(*rest) && *rest != '.') |
| 1222 | break; |
| 1223 | rest++; |
| 1224 | } |
| 1225 | v = ((LINUX_VERSION_CODE >> 8) & 0xff) + 40; |
Kees Cook | 3680030 | 2012-10-19 18:45:53 -0700 | [diff] [blame] | 1226 | copy = clamp_t(size_t, len, 1, sizeof(buf)); |
Kees Cook | 643fde8 | 2012-10-19 13:56:51 -0700 | [diff] [blame] | 1227 | copy = scnprintf(buf, copy, "2.6.%u%s", v, rest); |
| 1228 | ret = copy_to_user(release, buf, copy + 1); |
Andi Kleen | be27425 | 2011-08-19 16:15:10 -0700 | [diff] [blame] | 1229 | } |
| 1230 | return ret; |
| 1231 | } |
| 1232 | |
Heiko Carstens | e48fbb6 | 2009-01-14 14:14:26 +0100 | [diff] [blame] | 1233 | SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | { |
| 1235 | int errno = 0; |
| 1236 | |
| 1237 | down_read(&uts_sem); |
Serge E. Hallyn | e9ff399 | 2006-10-02 02:18:11 -0700 | [diff] [blame] | 1238 | if (copy_to_user(name, utsname(), sizeof *name)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | errno = -EFAULT; |
| 1240 | up_read(&uts_sem); |
Christoph Hellwig | e28cbf2 | 2010-03-10 15:21:19 -0800 | [diff] [blame] | 1241 | |
Andi Kleen | be27425 | 2011-08-19 16:15:10 -0700 | [diff] [blame] | 1242 | if (!errno && override_release(name->release, sizeof(name->release))) |
| 1243 | errno = -EFAULT; |
Christoph Hellwig | e28cbf2 | 2010-03-10 15:21:19 -0800 | [diff] [blame] | 1244 | if (!errno && override_architecture(name)) |
| 1245 | errno = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | return errno; |
| 1247 | } |
| 1248 | |
Christoph Hellwig | 5cacdb4 | 2010-03-10 15:21:21 -0800 | [diff] [blame] | 1249 | #ifdef __ARCH_WANT_SYS_OLD_UNAME |
| 1250 | /* |
| 1251 | * Old cruft |
| 1252 | */ |
| 1253 | SYSCALL_DEFINE1(uname, struct old_utsname __user *, name) |
| 1254 | { |
| 1255 | int error = 0; |
| 1256 | |
| 1257 | if (!name) |
| 1258 | return -EFAULT; |
| 1259 | |
| 1260 | down_read(&uts_sem); |
| 1261 | if (copy_to_user(name, utsname(), sizeof(*name))) |
| 1262 | error = -EFAULT; |
| 1263 | up_read(&uts_sem); |
| 1264 | |
Andi Kleen | be27425 | 2011-08-19 16:15:10 -0700 | [diff] [blame] | 1265 | if (!error && override_release(name->release, sizeof(name->release))) |
| 1266 | error = -EFAULT; |
Christoph Hellwig | 5cacdb4 | 2010-03-10 15:21:21 -0800 | [diff] [blame] | 1267 | if (!error && override_architecture(name)) |
| 1268 | error = -EFAULT; |
| 1269 | return error; |
| 1270 | } |
| 1271 | |
| 1272 | SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name) |
| 1273 | { |
| 1274 | int error; |
| 1275 | |
| 1276 | if (!name) |
| 1277 | return -EFAULT; |
| 1278 | if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname))) |
| 1279 | return -EFAULT; |
| 1280 | |
| 1281 | down_read(&uts_sem); |
| 1282 | error = __copy_to_user(&name->sysname, &utsname()->sysname, |
| 1283 | __OLD_UTS_LEN); |
| 1284 | error |= __put_user(0, name->sysname + __OLD_UTS_LEN); |
| 1285 | error |= __copy_to_user(&name->nodename, &utsname()->nodename, |
| 1286 | __OLD_UTS_LEN); |
| 1287 | error |= __put_user(0, name->nodename + __OLD_UTS_LEN); |
| 1288 | error |= __copy_to_user(&name->release, &utsname()->release, |
| 1289 | __OLD_UTS_LEN); |
| 1290 | error |= __put_user(0, name->release + __OLD_UTS_LEN); |
| 1291 | error |= __copy_to_user(&name->version, &utsname()->version, |
| 1292 | __OLD_UTS_LEN); |
| 1293 | error |= __put_user(0, name->version + __OLD_UTS_LEN); |
| 1294 | error |= __copy_to_user(&name->machine, &utsname()->machine, |
| 1295 | __OLD_UTS_LEN); |
| 1296 | error |= __put_user(0, name->machine + __OLD_UTS_LEN); |
| 1297 | up_read(&uts_sem); |
| 1298 | |
| 1299 | if (!error && override_architecture(name)) |
| 1300 | error = -EFAULT; |
Andi Kleen | be27425 | 2011-08-19 16:15:10 -0700 | [diff] [blame] | 1301 | if (!error && override_release(name->release, sizeof(name->release))) |
| 1302 | error = -EFAULT; |
Christoph Hellwig | 5cacdb4 | 2010-03-10 15:21:21 -0800 | [diff] [blame] | 1303 | return error ? -EFAULT : 0; |
| 1304 | } |
| 1305 | #endif |
| 1306 | |
Heiko Carstens | 5a8a82b | 2009-01-14 14:14:25 +0100 | [diff] [blame] | 1307 | SYSCALL_DEFINE2(sethostname, char __user *, name, int, len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | { |
| 1309 | int errno; |
| 1310 | char tmp[__NEW_UTS_LEN]; |
| 1311 | |
Serge E. Hallyn | bb96a6f | 2011-03-23 16:43:18 -0700 | [diff] [blame] | 1312 | if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | return -EPERM; |
Serge E. Hallyn | fc832ad | 2011-03-23 16:43:22 -0700 | [diff] [blame] | 1314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | if (len < 0 || len > __NEW_UTS_LEN) |
| 1316 | return -EINVAL; |
| 1317 | down_write(&uts_sem); |
| 1318 | errno = -EFAULT; |
| 1319 | if (!copy_from_user(tmp, name, len)) { |
Andrew Morton | 9679e4d | 2008-10-15 22:01:51 -0700 | [diff] [blame] | 1320 | struct new_utsname *u = utsname(); |
| 1321 | |
| 1322 | memcpy(u->nodename, tmp, len); |
| 1323 | memset(u->nodename + len, 0, sizeof(u->nodename) - len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | errno = 0; |
| 1325 | } |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame] | 1326 | uts_proc_notify(UTS_PROC_HOSTNAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | up_write(&uts_sem); |
| 1328 | return errno; |
| 1329 | } |
| 1330 | |
| 1331 | #ifdef __ARCH_WANT_SYS_GETHOSTNAME |
| 1332 | |
Heiko Carstens | 5a8a82b | 2009-01-14 14:14:25 +0100 | [diff] [blame] | 1333 | SYSCALL_DEFINE2(gethostname, char __user *, name, int, len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | { |
| 1335 | int i, errno; |
Andrew Morton | 9679e4d | 2008-10-15 22:01:51 -0700 | [diff] [blame] | 1336 | struct new_utsname *u; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | |
| 1338 | if (len < 0) |
| 1339 | return -EINVAL; |
| 1340 | down_read(&uts_sem); |
Andrew Morton | 9679e4d | 2008-10-15 22:01:51 -0700 | [diff] [blame] | 1341 | u = utsname(); |
| 1342 | i = 1 + strlen(u->nodename); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | if (i > len) |
| 1344 | i = len; |
| 1345 | errno = 0; |
Andrew Morton | 9679e4d | 2008-10-15 22:01:51 -0700 | [diff] [blame] | 1346 | if (copy_to_user(name, u->nodename, i)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | errno = -EFAULT; |
| 1348 | up_read(&uts_sem); |
| 1349 | return errno; |
| 1350 | } |
| 1351 | |
| 1352 | #endif |
| 1353 | |
| 1354 | /* |
| 1355 | * Only setdomainname; getdomainname can be implemented by calling |
| 1356 | * uname() |
| 1357 | */ |
Heiko Carstens | 5a8a82b | 2009-01-14 14:14:25 +0100 | [diff] [blame] | 1358 | SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | { |
| 1360 | int errno; |
| 1361 | char tmp[__NEW_UTS_LEN]; |
| 1362 | |
Serge E. Hallyn | fc832ad | 2011-03-23 16:43:22 -0700 | [diff] [blame] | 1363 | if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | return -EPERM; |
| 1365 | if (len < 0 || len > __NEW_UTS_LEN) |
| 1366 | return -EINVAL; |
| 1367 | |
| 1368 | down_write(&uts_sem); |
| 1369 | errno = -EFAULT; |
| 1370 | if (!copy_from_user(tmp, name, len)) { |
Andrew Morton | 9679e4d | 2008-10-15 22:01:51 -0700 | [diff] [blame] | 1371 | struct new_utsname *u = utsname(); |
| 1372 | |
| 1373 | memcpy(u->domainname, tmp, len); |
| 1374 | memset(u->domainname + len, 0, sizeof(u->domainname) - len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | errno = 0; |
| 1376 | } |
Lucas De Marchi | f1ecf06 | 2011-11-02 13:39:22 -0700 | [diff] [blame] | 1377 | uts_proc_notify(UTS_PROC_DOMAINNAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | up_write(&uts_sem); |
| 1379 | return errno; |
| 1380 | } |
| 1381 | |
Heiko Carstens | e48fbb6 | 2009-01-14 14:14:26 +0100 | [diff] [blame] | 1382 | SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | { |
Jiri Slaby | b951834 | 2010-05-04 11:28:25 +0200 | [diff] [blame] | 1384 | struct rlimit value; |
| 1385 | int ret; |
| 1386 | |
| 1387 | ret = do_prlimit(current, resource, NULL, &value); |
| 1388 | if (!ret) |
| 1389 | ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0; |
| 1390 | |
| 1391 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT |
| 1395 | |
| 1396 | /* |
| 1397 | * Back compatibility for getrlimit. Needed for some apps. |
| 1398 | */ |
| 1399 | |
Heiko Carstens | e48fbb6 | 2009-01-14 14:14:26 +0100 | [diff] [blame] | 1400 | SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource, |
| 1401 | struct rlimit __user *, rlim) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | { |
| 1403 | struct rlimit x; |
| 1404 | if (resource >= RLIM_NLIMITS) |
| 1405 | return -EINVAL; |
| 1406 | |
| 1407 | task_lock(current->group_leader); |
| 1408 | x = current->signal->rlim[resource]; |
| 1409 | task_unlock(current->group_leader); |
Cal Peake | 756184b | 2006-09-30 23:27:24 -0700 | [diff] [blame] | 1410 | if (x.rlim_cur > 0x7FFFFFFF) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | x.rlim_cur = 0x7FFFFFFF; |
Cal Peake | 756184b | 2006-09-30 23:27:24 -0700 | [diff] [blame] | 1412 | if (x.rlim_max > 0x7FFFFFFF) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | x.rlim_max = 0x7FFFFFFF; |
| 1414 | return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0; |
| 1415 | } |
| 1416 | |
| 1417 | #endif |
| 1418 | |
Jiri Slaby | c022a0a | 2010-05-04 18:03:50 +0200 | [diff] [blame] | 1419 | static inline bool rlim64_is_infinity(__u64 rlim64) |
| 1420 | { |
| 1421 | #if BITS_PER_LONG < 64 |
| 1422 | return rlim64 >= ULONG_MAX; |
| 1423 | #else |
| 1424 | return rlim64 == RLIM64_INFINITY; |
| 1425 | #endif |
| 1426 | } |
| 1427 | |
| 1428 | static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64) |
| 1429 | { |
| 1430 | if (rlim->rlim_cur == RLIM_INFINITY) |
| 1431 | rlim64->rlim_cur = RLIM64_INFINITY; |
| 1432 | else |
| 1433 | rlim64->rlim_cur = rlim->rlim_cur; |
| 1434 | if (rlim->rlim_max == RLIM_INFINITY) |
| 1435 | rlim64->rlim_max = RLIM64_INFINITY; |
| 1436 | else |
| 1437 | rlim64->rlim_max = rlim->rlim_max; |
| 1438 | } |
| 1439 | |
| 1440 | static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim) |
| 1441 | { |
| 1442 | if (rlim64_is_infinity(rlim64->rlim_cur)) |
| 1443 | rlim->rlim_cur = RLIM_INFINITY; |
| 1444 | else |
| 1445 | rlim->rlim_cur = (unsigned long)rlim64->rlim_cur; |
| 1446 | if (rlim64_is_infinity(rlim64->rlim_max)) |
| 1447 | rlim->rlim_max = RLIM_INFINITY; |
| 1448 | else |
| 1449 | rlim->rlim_max = (unsigned long)rlim64->rlim_max; |
| 1450 | } |
| 1451 | |
Jiri Slaby | 1c1e618 | 2009-08-28 14:08:17 +0200 | [diff] [blame] | 1452 | /* make sure you are allowed to change @tsk limits before calling this */ |
Jiri Slaby | 5b41535 | 2010-03-24 16:11:29 +0100 | [diff] [blame] | 1453 | int do_prlimit(struct task_struct *tsk, unsigned int resource, |
| 1454 | struct rlimit *new_rlim, struct rlimit *old_rlim) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | { |
Jiri Slaby | 5b41535 | 2010-03-24 16:11:29 +0100 | [diff] [blame] | 1456 | struct rlimit *rlim; |
Jiri Slaby | 86f162f | 2009-11-14 17:37:04 +0100 | [diff] [blame] | 1457 | int retval = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | |
| 1459 | if (resource >= RLIM_NLIMITS) |
| 1460 | return -EINVAL; |
Jiri Slaby | 5b41535 | 2010-03-24 16:11:29 +0100 | [diff] [blame] | 1461 | if (new_rlim) { |
| 1462 | if (new_rlim->rlim_cur > new_rlim->rlim_max) |
| 1463 | return -EINVAL; |
| 1464 | if (resource == RLIMIT_NOFILE && |
| 1465 | new_rlim->rlim_max > sysctl_nr_open) |
| 1466 | return -EPERM; |
| 1467 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | |
Jiri Slaby | 1c1e618 | 2009-08-28 14:08:17 +0200 | [diff] [blame] | 1469 | /* protect tsk->signal and tsk->sighand from disappearing */ |
| 1470 | read_lock(&tasklist_lock); |
| 1471 | if (!tsk->sighand) { |
| 1472 | retval = -ESRCH; |
| 1473 | goto out; |
| 1474 | } |
| 1475 | |
Jiri Slaby | 5b41535 | 2010-03-24 16:11:29 +0100 | [diff] [blame] | 1476 | rlim = tsk->signal->rlim + resource; |
Jiri Slaby | 86f162f | 2009-11-14 17:37:04 +0100 | [diff] [blame] | 1477 | task_lock(tsk->group_leader); |
Jiri Slaby | 5b41535 | 2010-03-24 16:11:29 +0100 | [diff] [blame] | 1478 | if (new_rlim) { |
Serge E. Hallyn | fc832ad | 2011-03-23 16:43:22 -0700 | [diff] [blame] | 1479 | /* Keep the capable check against init_user_ns until |
| 1480 | cgroups can contain all limits */ |
Jiri Slaby | 5b41535 | 2010-03-24 16:11:29 +0100 | [diff] [blame] | 1481 | if (new_rlim->rlim_max > rlim->rlim_max && |
| 1482 | !capable(CAP_SYS_RESOURCE)) |
| 1483 | retval = -EPERM; |
| 1484 | if (!retval) |
| 1485 | retval = security_task_setrlimit(tsk->group_leader, |
| 1486 | resource, new_rlim); |
| 1487 | if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) { |
| 1488 | /* |
| 1489 | * The caller is asking for an immediate RLIMIT_CPU |
| 1490 | * expiry. But we use the zero value to mean "it was |
| 1491 | * never set". So let's cheat and make it one second |
| 1492 | * instead |
| 1493 | */ |
| 1494 | new_rlim->rlim_cur = 1; |
| 1495 | } |
Tom Alsberg | 9926e4c | 2007-05-08 00:30:31 -0700 | [diff] [blame] | 1496 | } |
Jiri Slaby | 5b41535 | 2010-03-24 16:11:29 +0100 | [diff] [blame] | 1497 | if (!retval) { |
| 1498 | if (old_rlim) |
| 1499 | *old_rlim = *rlim; |
| 1500 | if (new_rlim) |
| 1501 | *rlim = *new_rlim; |
| 1502 | } |
Jiri Slaby | 7855c35 | 2009-08-26 23:45:34 +0200 | [diff] [blame] | 1503 | task_unlock(tsk->group_leader); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | |
Andrew Morton | d3561f7 | 2006-03-24 03:18:36 -0800 | [diff] [blame] | 1505 | /* |
| 1506 | * RLIMIT_CPU handling. Note that the kernel fails to return an error |
| 1507 | * code if it rejected the user's attempt to set RLIMIT_CPU. This is a |
| 1508 | * very long-standing error, and fixing it now risks breakage of |
| 1509 | * applications, so we live with it |
| 1510 | */ |
Jiri Slaby | 5b41535 | 2010-03-24 16:11:29 +0100 | [diff] [blame] | 1511 | if (!retval && new_rlim && resource == RLIMIT_CPU && |
| 1512 | new_rlim->rlim_cur != RLIM_INFINITY) |
| 1513 | update_rlimit_cpu(tsk, new_rlim->rlim_cur); |
Andrew Morton | ec9e16b | 2006-03-24 03:18:34 -0800 | [diff] [blame] | 1514 | out: |
Jiri Slaby | 1c1e618 | 2009-08-28 14:08:17 +0200 | [diff] [blame] | 1515 | read_unlock(&tasklist_lock); |
Oleg Nesterov | 2fb9d26 | 2009-09-03 19:21:45 +0200 | [diff] [blame] | 1516 | return retval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | } |
| 1518 | |
Jiri Slaby | c022a0a | 2010-05-04 18:03:50 +0200 | [diff] [blame] | 1519 | /* rcu lock must be held */ |
| 1520 | static int check_prlimit_permission(struct task_struct *task) |
| 1521 | { |
| 1522 | const struct cred *cred = current_cred(), *tcred; |
| 1523 | |
Serge E. Hallyn | fc832ad | 2011-03-23 16:43:22 -0700 | [diff] [blame] | 1524 | if (current == task) |
| 1525 | return 0; |
Jiri Slaby | c022a0a | 2010-05-04 18:03:50 +0200 | [diff] [blame] | 1526 | |
Serge E. Hallyn | fc832ad | 2011-03-23 16:43:22 -0700 | [diff] [blame] | 1527 | tcred = __task_cred(task); |
Eric W. Biederman | 6f2c0a9 | 2011-11-16 23:15:31 -0800 | [diff] [blame] | 1528 | if (cred->user_ns == tcred->user_ns && |
Serge E. Hallyn | fc832ad | 2011-03-23 16:43:22 -0700 | [diff] [blame] | 1529 | (cred->uid == tcred->euid && |
| 1530 | cred->uid == tcred->suid && |
| 1531 | cred->uid == tcred->uid && |
| 1532 | cred->gid == tcred->egid && |
| 1533 | cred->gid == tcred->sgid && |
| 1534 | cred->gid == tcred->gid)) |
| 1535 | return 0; |
Eric W. Biederman | 6f2c0a9 | 2011-11-16 23:15:31 -0800 | [diff] [blame] | 1536 | if (ns_capable(tcred->user_ns, CAP_SYS_RESOURCE)) |
Serge E. Hallyn | fc832ad | 2011-03-23 16:43:22 -0700 | [diff] [blame] | 1537 | return 0; |
| 1538 | |
| 1539 | return -EPERM; |
Jiri Slaby | c022a0a | 2010-05-04 18:03:50 +0200 | [diff] [blame] | 1540 | } |
| 1541 | |
| 1542 | SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource, |
| 1543 | const struct rlimit64 __user *, new_rlim, |
| 1544 | struct rlimit64 __user *, old_rlim) |
| 1545 | { |
| 1546 | struct rlimit64 old64, new64; |
| 1547 | struct rlimit old, new; |
| 1548 | struct task_struct *tsk; |
| 1549 | int ret; |
| 1550 | |
| 1551 | if (new_rlim) { |
| 1552 | if (copy_from_user(&new64, new_rlim, sizeof(new64))) |
| 1553 | return -EFAULT; |
| 1554 | rlim64_to_rlim(&new64, &new); |
| 1555 | } |
| 1556 | |
| 1557 | rcu_read_lock(); |
| 1558 | tsk = pid ? find_task_by_vpid(pid) : current; |
| 1559 | if (!tsk) { |
| 1560 | rcu_read_unlock(); |
| 1561 | return -ESRCH; |
| 1562 | } |
| 1563 | ret = check_prlimit_permission(tsk); |
| 1564 | if (ret) { |
| 1565 | rcu_read_unlock(); |
| 1566 | return ret; |
| 1567 | } |
| 1568 | get_task_struct(tsk); |
| 1569 | rcu_read_unlock(); |
| 1570 | |
| 1571 | ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL, |
| 1572 | old_rlim ? &old : NULL); |
| 1573 | |
| 1574 | if (!ret && old_rlim) { |
| 1575 | rlim_to_rlim64(&old, &old64); |
| 1576 | if (copy_to_user(old_rlim, &old64, sizeof(old64))) |
| 1577 | ret = -EFAULT; |
| 1578 | } |
| 1579 | |
| 1580 | put_task_struct(tsk); |
| 1581 | return ret; |
| 1582 | } |
| 1583 | |
Jiri Slaby | 7855c35 | 2009-08-26 23:45:34 +0200 | [diff] [blame] | 1584 | SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim) |
| 1585 | { |
| 1586 | struct rlimit new_rlim; |
| 1587 | |
| 1588 | if (copy_from_user(&new_rlim, rlim, sizeof(*rlim))) |
| 1589 | return -EFAULT; |
Jiri Slaby | 5b41535 | 2010-03-24 16:11:29 +0100 | [diff] [blame] | 1590 | return do_prlimit(current, resource, &new_rlim, NULL); |
Jiri Slaby | 7855c35 | 2009-08-26 23:45:34 +0200 | [diff] [blame] | 1591 | } |
| 1592 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | /* |
| 1594 | * It would make sense to put struct rusage in the task_struct, |
| 1595 | * except that would make the task_struct be *really big*. After |
| 1596 | * task_struct gets moved into malloc'ed memory, it would |
| 1597 | * make sense to do this. It will make moving the rest of the information |
| 1598 | * a lot simpler! (Which we're not doing right now because we're not |
| 1599 | * measuring them yet). |
| 1600 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | * When sampling multiple threads for RUSAGE_SELF, under SMP we might have |
| 1602 | * races with threads incrementing their own counters. But since word |
| 1603 | * reads are atomic, we either get new values or old values and we don't |
| 1604 | * care which for the sums. We always take the siglock to protect reading |
| 1605 | * the c* fields from p->signal from races with exit.c updating those |
| 1606 | * fields when reaping, so a sample either gets all the additions of a |
| 1607 | * given child after it's reaped, or none so this sample is before reaping. |
Ravikiran G Thirumalai | 2dd0ebc | 2006-03-23 03:00:13 -0800 | [diff] [blame] | 1608 | * |
Ravikiran G Thirumalai | de047c1 | 2006-06-22 14:47:26 -0700 | [diff] [blame] | 1609 | * Locking: |
| 1610 | * We need to take the siglock for CHILDEREN, SELF and BOTH |
| 1611 | * for the cases current multithreaded, non-current single threaded |
| 1612 | * non-current multithreaded. Thread traversal is now safe with |
| 1613 | * the siglock held. |
| 1614 | * Strictly speaking, we donot need to take the siglock if we are current and |
| 1615 | * single threaded, as no one else can take our signal_struct away, no one |
| 1616 | * else can reap the children to update signal->c* counters, and no one else |
| 1617 | * can race with the signal-> fields. If we do not take any lock, the |
| 1618 | * signal-> fields could be read out of order while another thread was just |
| 1619 | * exiting. So we should place a read memory barrier when we avoid the lock. |
| 1620 | * On the writer side, write memory barrier is implied in __exit_signal |
| 1621 | * as __exit_signal releases the siglock spinlock after updating the signal-> |
| 1622 | * fields. But we don't do this yet to keep things simple. |
Ravikiran G Thirumalai | 2dd0ebc | 2006-03-23 03:00:13 -0800 | [diff] [blame] | 1623 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | */ |
| 1625 | |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1626 | static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r) |
Sripathi Kodi | 679c9cd | 2008-04-29 00:58:42 -0700 | [diff] [blame] | 1627 | { |
Sripathi Kodi | 679c9cd | 2008-04-29 00:58:42 -0700 | [diff] [blame] | 1628 | r->ru_nvcsw += t->nvcsw; |
| 1629 | r->ru_nivcsw += t->nivcsw; |
| 1630 | r->ru_minflt += t->min_flt; |
| 1631 | r->ru_majflt += t->maj_flt; |
| 1632 | r->ru_inblock += task_io_get_inblock(t); |
| 1633 | r->ru_oublock += task_io_get_oublock(t); |
| 1634 | } |
| 1635 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | static void k_getrusage(struct task_struct *p, int who, struct rusage *r) |
| 1637 | { |
| 1638 | struct task_struct *t; |
| 1639 | unsigned long flags; |
Hidetoshi Seto | 0cf55e1 | 2009-12-02 17:28:07 +0900 | [diff] [blame] | 1640 | cputime_t tgutime, tgstime, utime, stime; |
Jiri Pirko | 1f10206 | 2009-09-22 16:44:10 -0700 | [diff] [blame] | 1641 | unsigned long maxrss = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | |
| 1643 | memset((char *) r, 0, sizeof *r); |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 1644 | utime = stime = 0; |
Oleg Nesterov | 0f59cc4 | 2006-01-08 01:05:15 -0800 | [diff] [blame] | 1645 | |
Sripathi Kodi | 679c9cd | 2008-04-29 00:58:42 -0700 | [diff] [blame] | 1646 | if (who == RUSAGE_THREAD) { |
Hidetoshi Seto | d180c5b | 2009-11-26 14:48:30 +0900 | [diff] [blame] | 1647 | task_times(current, &utime, &stime); |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1648 | accumulate_thread_rusage(p, r); |
Jiri Pirko | 1f10206 | 2009-09-22 16:44:10 -0700 | [diff] [blame] | 1649 | maxrss = p->signal->maxrss; |
Sripathi Kodi | 679c9cd | 2008-04-29 00:58:42 -0700 | [diff] [blame] | 1650 | goto out; |
| 1651 | } |
| 1652 | |
Oleg Nesterov | d6cf723 | 2008-04-30 00:52:38 -0700 | [diff] [blame] | 1653 | if (!lock_task_sighand(p, &flags)) |
Ravikiran G Thirumalai | de047c1 | 2006-06-22 14:47:26 -0700 | [diff] [blame] | 1654 | return; |
Ravikiran G Thirumalai | 2dd0ebc | 2006-03-23 03:00:13 -0800 | [diff] [blame] | 1655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | switch (who) { |
Oleg Nesterov | 0f59cc4 | 2006-01-08 01:05:15 -0800 | [diff] [blame] | 1657 | case RUSAGE_BOTH: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | case RUSAGE_CHILDREN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1659 | utime = p->signal->cutime; |
| 1660 | stime = p->signal->cstime; |
| 1661 | r->ru_nvcsw = p->signal->cnvcsw; |
| 1662 | r->ru_nivcsw = p->signal->cnivcsw; |
| 1663 | r->ru_minflt = p->signal->cmin_flt; |
| 1664 | r->ru_majflt = p->signal->cmaj_flt; |
Eric Dumazet | 6eaeeab | 2007-05-10 22:22:37 -0700 | [diff] [blame] | 1665 | r->ru_inblock = p->signal->cinblock; |
| 1666 | r->ru_oublock = p->signal->coublock; |
Jiri Pirko | 1f10206 | 2009-09-22 16:44:10 -0700 | [diff] [blame] | 1667 | maxrss = p->signal->cmaxrss; |
Oleg Nesterov | 0f59cc4 | 2006-01-08 01:05:15 -0800 | [diff] [blame] | 1668 | |
| 1669 | if (who == RUSAGE_CHILDREN) |
| 1670 | break; |
| 1671 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | case RUSAGE_SELF: |
Hidetoshi Seto | 0cf55e1 | 2009-12-02 17:28:07 +0900 | [diff] [blame] | 1673 | thread_group_times(p, &tgutime, &tgstime); |
Martin Schwidefsky | 6486163 | 2011-12-15 14:56:09 +0100 | [diff] [blame] | 1674 | utime += tgutime; |
| 1675 | stime += tgstime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | r->ru_nvcsw += p->signal->nvcsw; |
| 1677 | r->ru_nivcsw += p->signal->nivcsw; |
| 1678 | r->ru_minflt += p->signal->min_flt; |
| 1679 | r->ru_majflt += p->signal->maj_flt; |
Eric Dumazet | 6eaeeab | 2007-05-10 22:22:37 -0700 | [diff] [blame] | 1680 | r->ru_inblock += p->signal->inblock; |
| 1681 | r->ru_oublock += p->signal->oublock; |
Jiri Pirko | 1f10206 | 2009-09-22 16:44:10 -0700 | [diff] [blame] | 1682 | if (maxrss < p->signal->maxrss) |
| 1683 | maxrss = p->signal->maxrss; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | t = p; |
| 1685 | do { |
Frank Mayhar | f06febc | 2008-09-12 09:54:39 -0700 | [diff] [blame] | 1686 | accumulate_thread_rusage(t, r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | t = next_thread(t); |
| 1688 | } while (t != p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | break; |
Oleg Nesterov | 0f59cc4 | 2006-01-08 01:05:15 -0800 | [diff] [blame] | 1690 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | default: |
| 1692 | BUG(); |
| 1693 | } |
Ravikiran G Thirumalai | de047c1 | 2006-06-22 14:47:26 -0700 | [diff] [blame] | 1694 | unlock_task_sighand(p, &flags); |
Ravikiran G Thirumalai | de047c1 | 2006-06-22 14:47:26 -0700 | [diff] [blame] | 1695 | |
Sripathi Kodi | 679c9cd | 2008-04-29 00:58:42 -0700 | [diff] [blame] | 1696 | out: |
Oleg Nesterov | 0f59cc4 | 2006-01-08 01:05:15 -0800 | [diff] [blame] | 1697 | cputime_to_timeval(utime, &r->ru_utime); |
| 1698 | cputime_to_timeval(stime, &r->ru_stime); |
Jiri Pirko | 1f10206 | 2009-09-22 16:44:10 -0700 | [diff] [blame] | 1699 | |
| 1700 | if (who != RUSAGE_CHILDREN) { |
| 1701 | struct mm_struct *mm = get_task_mm(p); |
| 1702 | if (mm) { |
| 1703 | setmax_mm_hiwater_rss(&maxrss, mm); |
| 1704 | mmput(mm); |
| 1705 | } |
| 1706 | } |
| 1707 | r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | } |
| 1709 | |
| 1710 | int getrusage(struct task_struct *p, int who, struct rusage __user *ru) |
| 1711 | { |
| 1712 | struct rusage r; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1713 | k_getrusage(p, who, &r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0; |
| 1715 | } |
| 1716 | |
Heiko Carstens | e48fbb6 | 2009-01-14 14:14:26 +0100 | [diff] [blame] | 1717 | SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | { |
Sripathi Kodi | 679c9cd | 2008-04-29 00:58:42 -0700 | [diff] [blame] | 1719 | if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN && |
| 1720 | who != RUSAGE_THREAD) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | return -EINVAL; |
| 1722 | return getrusage(current, who, ru); |
| 1723 | } |
| 1724 | |
Heiko Carstens | e48fbb6 | 2009-01-14 14:14:26 +0100 | [diff] [blame] | 1725 | SYSCALL_DEFINE1(umask, int, mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1726 | { |
| 1727 | mask = xchg(¤t->fs->umask, mask & S_IRWXUGO); |
| 1728 | return mask; |
| 1729 | } |
Serge E. Hallyn | 3b7391d | 2008-02-04 22:29:45 -0800 | [diff] [blame] | 1730 | |
Cyrill Gorcunov | 028ee4b | 2012-01-12 17:20:55 -0800 | [diff] [blame] | 1731 | #ifdef CONFIG_CHECKPOINT_RESTORE |
| 1732 | static int prctl_set_mm(int opt, unsigned long addr, |
| 1733 | unsigned long arg4, unsigned long arg5) |
| 1734 | { |
| 1735 | unsigned long rlim = rlimit(RLIMIT_DATA); |
| 1736 | unsigned long vm_req_flags; |
| 1737 | unsigned long vm_bad_flags; |
| 1738 | struct vm_area_struct *vma; |
| 1739 | int error = 0; |
| 1740 | struct mm_struct *mm = current->mm; |
| 1741 | |
| 1742 | if (arg4 | arg5) |
| 1743 | return -EINVAL; |
| 1744 | |
Cyrill Gorcunov | 79f0713 | 2012-03-15 15:17:10 -0700 | [diff] [blame] | 1745 | if (!capable(CAP_SYS_RESOURCE)) |
Cyrill Gorcunov | 028ee4b | 2012-01-12 17:20:55 -0800 | [diff] [blame] | 1746 | return -EPERM; |
| 1747 | |
| 1748 | if (addr >= TASK_SIZE) |
| 1749 | return -EINVAL; |
| 1750 | |
| 1751 | down_read(&mm->mmap_sem); |
| 1752 | vma = find_vma(mm, addr); |
| 1753 | |
| 1754 | if (opt != PR_SET_MM_START_BRK && opt != PR_SET_MM_BRK) { |
| 1755 | /* It must be existing VMA */ |
| 1756 | if (!vma || vma->vm_start > addr) |
| 1757 | goto out; |
| 1758 | } |
| 1759 | |
| 1760 | error = -EINVAL; |
| 1761 | switch (opt) { |
| 1762 | case PR_SET_MM_START_CODE: |
| 1763 | case PR_SET_MM_END_CODE: |
| 1764 | vm_req_flags = VM_READ | VM_EXEC; |
| 1765 | vm_bad_flags = VM_WRITE | VM_MAYSHARE; |
| 1766 | |
| 1767 | if ((vma->vm_flags & vm_req_flags) != vm_req_flags || |
| 1768 | (vma->vm_flags & vm_bad_flags)) |
| 1769 | goto out; |
| 1770 | |
| 1771 | if (opt == PR_SET_MM_START_CODE) |
| 1772 | mm->start_code = addr; |
| 1773 | else |
| 1774 | mm->end_code = addr; |
| 1775 | break; |
| 1776 | |
| 1777 | case PR_SET_MM_START_DATA: |
| 1778 | case PR_SET_MM_END_DATA: |
| 1779 | vm_req_flags = VM_READ | VM_WRITE; |
| 1780 | vm_bad_flags = VM_EXEC | VM_MAYSHARE; |
| 1781 | |
| 1782 | if ((vma->vm_flags & vm_req_flags) != vm_req_flags || |
| 1783 | (vma->vm_flags & vm_bad_flags)) |
| 1784 | goto out; |
| 1785 | |
| 1786 | if (opt == PR_SET_MM_START_DATA) |
| 1787 | mm->start_data = addr; |
| 1788 | else |
| 1789 | mm->end_data = addr; |
| 1790 | break; |
| 1791 | |
| 1792 | case PR_SET_MM_START_STACK: |
| 1793 | |
| 1794 | #ifdef CONFIG_STACK_GROWSUP |
| 1795 | vm_req_flags = VM_READ | VM_WRITE | VM_GROWSUP; |
| 1796 | #else |
| 1797 | vm_req_flags = VM_READ | VM_WRITE | VM_GROWSDOWN; |
| 1798 | #endif |
| 1799 | if ((vma->vm_flags & vm_req_flags) != vm_req_flags) |
| 1800 | goto out; |
| 1801 | |
| 1802 | mm->start_stack = addr; |
| 1803 | break; |
| 1804 | |
| 1805 | case PR_SET_MM_START_BRK: |
| 1806 | if (addr <= mm->end_data) |
| 1807 | goto out; |
| 1808 | |
| 1809 | if (rlim < RLIM_INFINITY && |
| 1810 | (mm->brk - addr) + |
| 1811 | (mm->end_data - mm->start_data) > rlim) |
| 1812 | goto out; |
| 1813 | |
| 1814 | mm->start_brk = addr; |
| 1815 | break; |
| 1816 | |
| 1817 | case PR_SET_MM_BRK: |
| 1818 | if (addr <= mm->end_data) |
| 1819 | goto out; |
| 1820 | |
| 1821 | if (rlim < RLIM_INFINITY && |
| 1822 | (addr - mm->start_brk) + |
| 1823 | (mm->end_data - mm->start_data) > rlim) |
| 1824 | goto out; |
| 1825 | |
| 1826 | mm->brk = addr; |
| 1827 | break; |
| 1828 | |
| 1829 | default: |
| 1830 | error = -EINVAL; |
| 1831 | goto out; |
| 1832 | } |
| 1833 | |
| 1834 | error = 0; |
| 1835 | |
| 1836 | out: |
| 1837 | up_read(&mm->mmap_sem); |
| 1838 | |
| 1839 | return error; |
| 1840 | } |
| 1841 | #else /* CONFIG_CHECKPOINT_RESTORE */ |
| 1842 | static int prctl_set_mm(int opt, unsigned long addr, |
| 1843 | unsigned long arg4, unsigned long arg5) |
| 1844 | { |
| 1845 | return -EINVAL; |
| 1846 | } |
| 1847 | #endif |
| 1848 | |
Colin Cross | 8ad62c2 | 2013-06-26 17:26:01 -0700 | [diff] [blame] | 1849 | |
| 1850 | static int prctl_update_vma_anon_name(struct vm_area_struct *vma, |
| 1851 | struct vm_area_struct **prev, |
| 1852 | unsigned long start, unsigned long end, |
| 1853 | const char __user *name_addr) |
| 1854 | { |
| 1855 | struct mm_struct * mm = vma->vm_mm; |
| 1856 | int error = 0; |
| 1857 | pgoff_t pgoff; |
| 1858 | |
| 1859 | if (name_addr == vma_get_anon_name(vma)) { |
| 1860 | *prev = vma; |
| 1861 | goto out; |
| 1862 | } |
| 1863 | |
| 1864 | pgoff = vma->vm_pgoff + ((start - vma->vm_start) >> PAGE_SHIFT); |
| 1865 | *prev = vma_merge(mm, *prev, start, end, vma->vm_flags, vma->anon_vma, |
| 1866 | vma->vm_file, pgoff, vma_policy(vma), |
| 1867 | name_addr); |
| 1868 | if (*prev) { |
| 1869 | vma = *prev; |
| 1870 | goto success; |
| 1871 | } |
| 1872 | |
| 1873 | *prev = vma; |
| 1874 | |
| 1875 | if (start != vma->vm_start) { |
| 1876 | error = split_vma(mm, vma, start, 1); |
| 1877 | if (error) |
| 1878 | goto out; |
| 1879 | } |
| 1880 | |
| 1881 | if (end != vma->vm_end) { |
| 1882 | error = split_vma(mm, vma, end, 0); |
| 1883 | if (error) |
| 1884 | goto out; |
| 1885 | } |
| 1886 | |
| 1887 | success: |
| 1888 | if (!vma->vm_file) |
| 1889 | vma->shared.anon_name = name_addr; |
| 1890 | |
| 1891 | out: |
| 1892 | if (error == -ENOMEM) |
| 1893 | error = -EAGAIN; |
| 1894 | return error; |
| 1895 | } |
| 1896 | |
| 1897 | static int prctl_set_vma_anon_name(unsigned long start, unsigned long end, |
| 1898 | unsigned long arg) |
| 1899 | { |
| 1900 | unsigned long tmp; |
| 1901 | struct vm_area_struct * vma, *prev; |
| 1902 | int unmapped_error = 0; |
| 1903 | int error = -EINVAL; |
| 1904 | |
| 1905 | /* |
| 1906 | * If the interval [start,end) covers some unmapped address |
| 1907 | * ranges, just ignore them, but return -ENOMEM at the end. |
| 1908 | * - this matches the handling in madvise. |
| 1909 | */ |
| 1910 | vma = find_vma_prev(current->mm, start, &prev); |
| 1911 | if (vma && start > vma->vm_start) |
| 1912 | prev = vma; |
| 1913 | |
| 1914 | for (;;) { |
| 1915 | /* Still start < end. */ |
| 1916 | error = -ENOMEM; |
| 1917 | if (!vma) |
| 1918 | return error; |
| 1919 | |
| 1920 | /* Here start < (end|vma->vm_end). */ |
| 1921 | if (start < vma->vm_start) { |
| 1922 | unmapped_error = -ENOMEM; |
| 1923 | start = vma->vm_start; |
| 1924 | if (start >= end) |
| 1925 | return error; |
| 1926 | } |
| 1927 | |
| 1928 | /* Here vma->vm_start <= start < (end|vma->vm_end) */ |
| 1929 | tmp = vma->vm_end; |
| 1930 | if (end < tmp) |
| 1931 | tmp = end; |
| 1932 | |
| 1933 | /* Here vma->vm_start <= start < tmp <= (end|vma->vm_end). */ |
| 1934 | error = prctl_update_vma_anon_name(vma, &prev, start, end, |
| 1935 | (const char __user *)arg); |
| 1936 | if (error) |
| 1937 | return error; |
| 1938 | start = tmp; |
| 1939 | if (prev && start < prev->vm_end) |
| 1940 | start = prev->vm_end; |
| 1941 | error = unmapped_error; |
| 1942 | if (start >= end) |
| 1943 | return error; |
| 1944 | if (prev) |
| 1945 | vma = prev->vm_next; |
| 1946 | else /* madvise_remove dropped mmap_sem */ |
| 1947 | vma = find_vma(current->mm, start); |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | static int prctl_set_vma(unsigned long opt, unsigned long start, |
| 1952 | unsigned long len_in, unsigned long arg) |
| 1953 | { |
| 1954 | struct mm_struct *mm = current->mm; |
| 1955 | int error; |
| 1956 | unsigned long len; |
| 1957 | unsigned long end; |
| 1958 | |
| 1959 | if (start & ~PAGE_MASK) |
| 1960 | return -EINVAL; |
| 1961 | len = (len_in + ~PAGE_MASK) & PAGE_MASK; |
| 1962 | |
| 1963 | /* Check to see whether len was rounded up from small -ve to zero */ |
| 1964 | if (len_in && !len) |
| 1965 | return -EINVAL; |
| 1966 | |
| 1967 | end = start + len; |
| 1968 | if (end < start) |
| 1969 | return -EINVAL; |
| 1970 | |
| 1971 | if (end == start) |
| 1972 | return 0; |
| 1973 | |
| 1974 | down_write(&mm->mmap_sem); |
| 1975 | |
| 1976 | switch (opt) { |
| 1977 | case PR_SET_VMA_ANON_NAME: |
| 1978 | error = prctl_set_vma_anon_name(start, end, arg); |
| 1979 | break; |
| 1980 | default: |
| 1981 | error = -EINVAL; |
| 1982 | } |
| 1983 | |
| 1984 | up_write(&mm->mmap_sem); |
| 1985 | |
| 1986 | return error; |
| 1987 | } |
| 1988 | |
Heiko Carstens | c4ea37c | 2009-01-14 14:14:28 +0100 | [diff] [blame] | 1989 | SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, |
| 1990 | unsigned long, arg4, unsigned long, arg5) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | { |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 1992 | struct task_struct *me = current; |
| 1993 | unsigned char comm[sizeof(me->comm)]; |
| 1994 | long error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 1996 | error = security_task_prctl(option, arg2, arg3, arg4, arg5); |
| 1997 | if (error != -ENOSYS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | return error; |
| 1999 | |
David Howells | d84f4f9 | 2008-11-14 10:39:23 +1100 | [diff] [blame] | 2000 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | switch (option) { |
| 2002 | case PR_SET_PDEATHSIG: |
Jesper Juhl | 0730ded | 2005-09-06 15:17:37 -0700 | [diff] [blame] | 2003 | if (!valid_signal(arg2)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | error = -EINVAL; |
| 2005 | break; |
| 2006 | } |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 2007 | me->pdeath_signal = arg2; |
| 2008 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | break; |
| 2010 | case PR_GET_PDEATHSIG: |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 2011 | error = put_user(me->pdeath_signal, (int __user *)arg2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | break; |
| 2013 | case PR_GET_DUMPABLE: |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 2014 | error = get_dumpable(me->mm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | break; |
| 2016 | case PR_SET_DUMPABLE: |
Marcel Holtmann | abf75a5 | 2006-07-12 13:12:00 +0200 | [diff] [blame] | 2017 | if (arg2 < 0 || arg2 > 1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | error = -EINVAL; |
| 2019 | break; |
| 2020 | } |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 2021 | set_dumpable(me->mm, arg2); |
| 2022 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | break; |
| 2024 | |
| 2025 | case PR_SET_UNALIGN: |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 2026 | error = SET_UNALIGN_CTL(me, arg2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | break; |
| 2028 | case PR_GET_UNALIGN: |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 2029 | error = GET_UNALIGN_CTL(me, arg2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2030 | break; |
| 2031 | case PR_SET_FPEMU: |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 2032 | error = SET_FPEMU_CTL(me, arg2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | break; |
| 2034 | case PR_GET_FPEMU: |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 2035 | error = GET_FPEMU_CTL(me, arg2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | break; |
| 2037 | case PR_SET_FPEXC: |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 2038 | error = SET_FPEXC_CTL(me, arg2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2039 | break; |
| 2040 | case PR_GET_FPEXC: |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 2041 | error = GET_FPEXC_CTL(me, arg2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | break; |
| 2043 | case PR_GET_TIMING: |
| 2044 | error = PR_TIMING_STATISTICAL; |
| 2045 | break; |
| 2046 | case PR_SET_TIMING: |
Shi Weihua | 7b26655 | 2008-05-23 13:04:59 -0700 | [diff] [blame] | 2047 | if (arg2 != PR_TIMING_STATISTICAL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | error = -EINVAL; |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 2049 | else |
| 2050 | error = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | break; |
| 2052 | |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 2053 | case PR_SET_NAME: |
| 2054 | comm[sizeof(me->comm)-1] = 0; |
| 2055 | if (strncpy_from_user(comm, (char __user *)arg2, |
| 2056 | sizeof(me->comm) - 1) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | return -EFAULT; |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 2058 | set_task_comm(me, comm); |
Vladimir Zapolskiy | f786ecb | 2011-09-21 09:26:44 +0000 | [diff] [blame] | 2059 | proc_comm_connector(me); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | return 0; |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 2061 | case PR_GET_NAME: |
| 2062 | get_task_comm(comm, me); |
| 2063 | if (copy_to_user((char __user *)arg2, comm, |
| 2064 | sizeof(comm))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | return -EFAULT; |
| 2066 | return 0; |
Anton Blanchard | 651d765 | 2006-06-07 16:10:19 +1000 | [diff] [blame] | 2067 | case PR_GET_ENDIAN: |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 2068 | error = GET_ENDIAN(me, arg2); |
Anton Blanchard | 651d765 | 2006-06-07 16:10:19 +1000 | [diff] [blame] | 2069 | break; |
| 2070 | case PR_SET_ENDIAN: |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 2071 | error = SET_ENDIAN(me, arg2); |
Anton Blanchard | 651d765 | 2006-06-07 16:10:19 +1000 | [diff] [blame] | 2072 | break; |
| 2073 | |
Andrea Arcangeli | 1d9d02f | 2007-07-15 23:41:32 -0700 | [diff] [blame] | 2074 | case PR_GET_SECCOMP: |
| 2075 | error = prctl_get_seccomp(); |
| 2076 | break; |
| 2077 | case PR_SET_SECCOMP: |
Will Drewry | 980e920 | 2012-02-09 11:50:58 -0600 | [diff] [blame] | 2078 | error = prctl_set_seccomp(arg2, (char __user *)arg3); |
Andrea Arcangeli | 1d9d02f | 2007-07-15 23:41:32 -0700 | [diff] [blame] | 2079 | break; |
Erik Bosman | 8fb402b | 2008-04-11 18:54:17 +0200 | [diff] [blame] | 2080 | case PR_GET_TSC: |
| 2081 | error = GET_TSC_CTL(arg2); |
| 2082 | break; |
| 2083 | case PR_SET_TSC: |
| 2084 | error = SET_TSC_CTL(arg2); |
| 2085 | break; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2086 | case PR_TASK_PERF_EVENTS_DISABLE: |
| 2087 | error = perf_event_task_disable(); |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 2088 | break; |
Ingo Molnar | cdd6c48 | 2009-09-21 12:02:48 +0200 | [diff] [blame] | 2089 | case PR_TASK_PERF_EVENTS_ENABLE: |
| 2090 | error = perf_event_task_enable(); |
Ingo Molnar | 1d1c7dd | 2008-12-11 14:59:31 +0100 | [diff] [blame] | 2091 | break; |
Arjan van de Ven | 6976675 | 2008-09-01 15:52:40 -0700 | [diff] [blame] | 2092 | case PR_GET_TIMERSLACK: |
| 2093 | error = current->timer_slack_ns; |
| 2094 | break; |
| 2095 | case PR_SET_TIMERSLACK: |
| 2096 | if (arg2 <= 0) |
| 2097 | current->timer_slack_ns = |
| 2098 | current->default_timer_slack_ns; |
| 2099 | else |
| 2100 | current->timer_slack_ns = arg2; |
David Howells | b6dff3e | 2008-11-14 10:39:16 +1100 | [diff] [blame] | 2101 | error = 0; |
Arjan van de Ven | 6976675 | 2008-09-01 15:52:40 -0700 | [diff] [blame] | 2102 | break; |
Andi Kleen | 4db96cf | 2009-09-16 11:50:14 +0200 | [diff] [blame] | 2103 | case PR_MCE_KILL: |
| 2104 | if (arg4 | arg5) |
| 2105 | return -EINVAL; |
| 2106 | switch (arg2) { |
Andi Kleen | 1087e9b | 2009-10-04 02:20:11 +0200 | [diff] [blame] | 2107 | case PR_MCE_KILL_CLEAR: |
Andi Kleen | 4db96cf | 2009-09-16 11:50:14 +0200 | [diff] [blame] | 2108 | if (arg3 != 0) |
| 2109 | return -EINVAL; |
| 2110 | current->flags &= ~PF_MCE_PROCESS; |
| 2111 | break; |
Andi Kleen | 1087e9b | 2009-10-04 02:20:11 +0200 | [diff] [blame] | 2112 | case PR_MCE_KILL_SET: |
Andi Kleen | 4db96cf | 2009-09-16 11:50:14 +0200 | [diff] [blame] | 2113 | current->flags |= PF_MCE_PROCESS; |
Andi Kleen | 1087e9b | 2009-10-04 02:20:11 +0200 | [diff] [blame] | 2114 | if (arg3 == PR_MCE_KILL_EARLY) |
Andi Kleen | 4db96cf | 2009-09-16 11:50:14 +0200 | [diff] [blame] | 2115 | current->flags |= PF_MCE_EARLY; |
Andi Kleen | 1087e9b | 2009-10-04 02:20:11 +0200 | [diff] [blame] | 2116 | else if (arg3 == PR_MCE_KILL_LATE) |
Andi Kleen | 4db96cf | 2009-09-16 11:50:14 +0200 | [diff] [blame] | 2117 | current->flags &= ~PF_MCE_EARLY; |
Andi Kleen | 1087e9b | 2009-10-04 02:20:11 +0200 | [diff] [blame] | 2118 | else if (arg3 == PR_MCE_KILL_DEFAULT) |
| 2119 | current->flags &= |
| 2120 | ~(PF_MCE_EARLY|PF_MCE_PROCESS); |
| 2121 | else |
| 2122 | return -EINVAL; |
Andi Kleen | 4db96cf | 2009-09-16 11:50:14 +0200 | [diff] [blame] | 2123 | break; |
| 2124 | default: |
| 2125 | return -EINVAL; |
| 2126 | } |
| 2127 | error = 0; |
| 2128 | break; |
Andi Kleen | 1087e9b | 2009-10-04 02:20:11 +0200 | [diff] [blame] | 2129 | case PR_MCE_KILL_GET: |
| 2130 | if (arg2 | arg3 | arg4 | arg5) |
| 2131 | return -EINVAL; |
| 2132 | if (current->flags & PF_MCE_PROCESS) |
| 2133 | error = (current->flags & PF_MCE_EARLY) ? |
| 2134 | PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE; |
| 2135 | else |
| 2136 | error = PR_MCE_KILL_DEFAULT; |
| 2137 | break; |
Cyrill Gorcunov | 028ee4b | 2012-01-12 17:20:55 -0800 | [diff] [blame] | 2138 | case PR_SET_MM: |
| 2139 | error = prctl_set_mm(arg2, arg3, arg4, arg5); |
| 2140 | break; |
Lennart Poettering | ebec18a | 2012-03-23 15:01:54 -0700 | [diff] [blame] | 2141 | case PR_SET_CHILD_SUBREAPER: |
| 2142 | me->signal->is_child_subreaper = !!arg2; |
| 2143 | error = 0; |
| 2144 | break; |
| 2145 | case PR_GET_CHILD_SUBREAPER: |
| 2146 | error = put_user(me->signal->is_child_subreaper, |
| 2147 | (int __user *) arg2); |
| 2148 | break; |
Colin Cross | 8ad62c2 | 2013-06-26 17:26:01 -0700 | [diff] [blame] | 2149 | case PR_SET_VMA: |
| 2150 | error = prctl_set_vma(arg2, arg3, arg4, arg5); |
| 2151 | break; |
Andy Lutomirski | 9bf75df | 2012-04-12 16:47:50 -0500 | [diff] [blame] | 2152 | case PR_SET_NO_NEW_PRIVS: |
| 2153 | if (arg2 != 1 || arg3 || arg4 || arg5) |
| 2154 | return -EINVAL; |
| 2155 | |
Kees Cook | 5eab130 | 2014-05-21 15:23:46 -0700 | [diff] [blame] | 2156 | task_set_no_new_privs(current); |
Andy Lutomirski | 9bf75df | 2012-04-12 16:47:50 -0500 | [diff] [blame] | 2157 | break; |
| 2158 | case PR_GET_NO_NEW_PRIVS: |
| 2159 | if (arg2 || arg3 || arg4 || arg5) |
| 2160 | return -EINVAL; |
Kees Cook | 5eab130 | 2014-05-21 15:23:46 -0700 | [diff] [blame] | 2161 | return task_no_new_privs(current) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | default: |
| 2163 | error = -EINVAL; |
| 2164 | break; |
| 2165 | } |
| 2166 | return error; |
| 2167 | } |
Andi Kleen | 3cfc348 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 2168 | |
Heiko Carstens | 836f92a | 2009-01-14 14:14:33 +0100 | [diff] [blame] | 2169 | SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep, |
| 2170 | struct getcpu_cache __user *, unused) |
Andi Kleen | 3cfc348 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 2171 | { |
| 2172 | int err = 0; |
| 2173 | int cpu = raw_smp_processor_id(); |
| 2174 | if (cpup) |
| 2175 | err |= put_user(cpu, cpup); |
| 2176 | if (nodep) |
| 2177 | err |= put_user(cpu_to_node(cpu), nodep); |
Andi Kleen | 3cfc348 | 2006-09-26 10:52:28 +0200 | [diff] [blame] | 2178 | return err ? -EFAULT : 0; |
| 2179 | } |
Jeremy Fitzhardinge | 10a0a8d | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 2180 | |
| 2181 | char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff"; |
| 2182 | |
Neil Horman | a06a4dc | 2010-05-26 14:42:58 -0700 | [diff] [blame] | 2183 | static void argv_cleanup(struct subprocess_info *info) |
Jeremy Fitzhardinge | 10a0a8d | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 2184 | { |
Neil Horman | a06a4dc | 2010-05-26 14:42:58 -0700 | [diff] [blame] | 2185 | argv_free(info->argv); |
Jeremy Fitzhardinge | 10a0a8d | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 2186 | } |
| 2187 | |
| 2188 | /** |
| 2189 | * orderly_poweroff - Trigger an orderly system poweroff |
| 2190 | * @force: force poweroff if command execution fails |
| 2191 | * |
| 2192 | * This may be called from any context to trigger a system shutdown. |
| 2193 | * If the orderly shutdown fails, it will force an immediate shutdown. |
| 2194 | */ |
| 2195 | int orderly_poweroff(bool force) |
| 2196 | { |
| 2197 | int argc; |
| 2198 | char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc); |
| 2199 | static char *envp[] = { |
| 2200 | "HOME=/", |
| 2201 | "PATH=/sbin:/bin:/usr/sbin:/usr/bin", |
| 2202 | NULL |
| 2203 | }; |
| 2204 | int ret = -ENOMEM; |
| 2205 | struct subprocess_info *info; |
| 2206 | |
| 2207 | if (argv == NULL) { |
| 2208 | printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n", |
| 2209 | __func__, poweroff_cmd); |
| 2210 | goto out; |
| 2211 | } |
| 2212 | |
KOSAKI Motohiro | ac331d1 | 2008-07-25 01:45:38 -0700 | [diff] [blame] | 2213 | info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC); |
Jeremy Fitzhardinge | 10a0a8d | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 2214 | if (info == NULL) { |
| 2215 | argv_free(argv); |
| 2216 | goto out; |
| 2217 | } |
| 2218 | |
Neil Horman | a06a4dc | 2010-05-26 14:42:58 -0700 | [diff] [blame] | 2219 | call_usermodehelper_setfns(info, NULL, argv_cleanup, NULL); |
Jeremy Fitzhardinge | 10a0a8d | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 2220 | |
Jeremy Fitzhardinge | 86313c4 | 2007-07-17 18:37:03 -0700 | [diff] [blame] | 2221 | ret = call_usermodehelper_exec(info, UMH_NO_WAIT); |
Jeremy Fitzhardinge | 10a0a8d | 2007-07-17 18:37:02 -0700 | [diff] [blame] | 2222 | |
| 2223 | out: |
| 2224 | if (ret && force) { |
| 2225 | printk(KERN_WARNING "Failed to start orderly shutdown: " |
| 2226 | "forcing the issue\n"); |
| 2227 | |
| 2228 | /* I guess this should try to kick off some daemon to |
| 2229 | sync and poweroff asap. Or not even bother syncing |
| 2230 | if we're doing an emergency shutdown? */ |
| 2231 | emergency_sync(); |
| 2232 | kernel_power_off(); |
| 2233 | } |
| 2234 | |
| 2235 | return ret; |
| 2236 | } |
| 2237 | EXPORT_SYMBOL_GPL(orderly_poweroff); |