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