Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/kernel/sys.c | |
3 | * | |
4 | * Copyright (C) 1991, 1992 Linus Torvalds | |
5 | */ | |
6 | ||
1da177e4 LT |
7 | #include <linux/module.h> |
8 | #include <linux/mm.h> | |
9 | #include <linux/utsname.h> | |
10 | #include <linux/mman.h> | |
1da177e4 LT |
11 | #include <linux/reboot.h> |
12 | #include <linux/prctl.h> | |
1da177e4 LT |
13 | #include <linux/highuid.h> |
14 | #include <linux/fs.h> | |
cdd6c482 | 15 | #include <linux/perf_event.h> |
3e88c553 | 16 | #include <linux/resource.h> |
dc009d92 EB |
17 | #include <linux/kernel.h> |
18 | #include <linux/kexec.h> | |
1da177e4 | 19 | #include <linux/workqueue.h> |
c59ede7b | 20 | #include <linux/capability.h> |
1da177e4 LT |
21 | #include <linux/device.h> |
22 | #include <linux/key.h> | |
23 | #include <linux/times.h> | |
24 | #include <linux/posix-timers.h> | |
25 | #include <linux/security.h> | |
26 | #include <linux/dcookies.h> | |
27 | #include <linux/suspend.h> | |
28 | #include <linux/tty.h> | |
7ed20e1a | 29 | #include <linux/signal.h> |
9f46080c | 30 | #include <linux/cn_proc.h> |
3cfc348b | 31 | #include <linux/getcpu.h> |
6eaeeaba | 32 | #include <linux/task_io_accounting_ops.h> |
1d9d02fe | 33 | #include <linux/seccomp.h> |
4047727e | 34 | #include <linux/cpu.h> |
e28cbf22 | 35 | #include <linux/personality.h> |
e3d5a27d | 36 | #include <linux/ptrace.h> |
5ad4e53b | 37 | #include <linux/fs_struct.h> |
5a0e3ad6 | 38 | #include <linux/gfp.h> |
40dc166c | 39 | #include <linux/syscore_ops.h> |
1da177e4 LT |
40 | |
41 | #include <linux/compat.h> | |
42 | #include <linux/syscalls.h> | |
00d7c05a | 43 | #include <linux/kprobes.h> |
acce292c | 44 | #include <linux/user_namespace.h> |
1da177e4 | 45 | |
04c6862c SA |
46 | #include <linux/kmsg_dump.h> |
47 | ||
1da177e4 LT |
48 | #include <asm/uaccess.h> |
49 | #include <asm/io.h> | |
50 | #include <asm/unistd.h> | |
51 | ||
52 | #ifndef SET_UNALIGN_CTL | |
53 | # define SET_UNALIGN_CTL(a,b) (-EINVAL) | |
54 | #endif | |
55 | #ifndef GET_UNALIGN_CTL | |
56 | # define GET_UNALIGN_CTL(a,b) (-EINVAL) | |
57 | #endif | |
58 | #ifndef SET_FPEMU_CTL | |
59 | # define SET_FPEMU_CTL(a,b) (-EINVAL) | |
60 | #endif | |
61 | #ifndef GET_FPEMU_CTL | |
62 | # define GET_FPEMU_CTL(a,b) (-EINVAL) | |
63 | #endif | |
64 | #ifndef SET_FPEXC_CTL | |
65 | # define SET_FPEXC_CTL(a,b) (-EINVAL) | |
66 | #endif | |
67 | #ifndef GET_FPEXC_CTL | |
68 | # define GET_FPEXC_CTL(a,b) (-EINVAL) | |
69 | #endif | |
651d765d AB |
70 | #ifndef GET_ENDIAN |
71 | # define GET_ENDIAN(a,b) (-EINVAL) | |
72 | #endif | |
73 | #ifndef SET_ENDIAN | |
74 | # define SET_ENDIAN(a,b) (-EINVAL) | |
75 | #endif | |
8fb402bc EB |
76 | #ifndef GET_TSC_CTL |
77 | # define GET_TSC_CTL(a) (-EINVAL) | |
78 | #endif | |
79 | #ifndef SET_TSC_CTL | |
80 | # define SET_TSC_CTL(a) (-EINVAL) | |
81 | #endif | |
1da177e4 LT |
82 | |
83 | /* | |
84 | * this is where the system-wide overflow UID and GID are defined, for | |
85 | * architectures that now have 32-bit UID/GID but didn't in the past | |
86 | */ | |
87 | ||
88 | int overflowuid = DEFAULT_OVERFLOWUID; | |
89 | int overflowgid = DEFAULT_OVERFLOWGID; | |
90 | ||
91 | #ifdef CONFIG_UID16 | |
92 | EXPORT_SYMBOL(overflowuid); | |
93 | EXPORT_SYMBOL(overflowgid); | |
94 | #endif | |
95 | ||
96 | /* | |
97 | * the same as above, but for filesystems which can only store a 16-bit | |
98 | * UID and GID. as such, this is needed on all architectures | |
99 | */ | |
100 | ||
101 | int fs_overflowuid = DEFAULT_FS_OVERFLOWUID; | |
102 | int fs_overflowgid = DEFAULT_FS_OVERFLOWUID; | |
103 | ||
104 | EXPORT_SYMBOL(fs_overflowuid); | |
105 | EXPORT_SYMBOL(fs_overflowgid); | |
106 | ||
107 | /* | |
108 | * this indicates whether you can reboot with ctrl-alt-del: the default is yes | |
109 | */ | |
110 | ||
111 | int C_A_D = 1; | |
9ec52099 CLG |
112 | struct pid *cad_pid; |
113 | EXPORT_SYMBOL(cad_pid); | |
1da177e4 | 114 | |
bd804eba RW |
115 | /* |
116 | * If set, this is used for preparing the system to power off. | |
117 | */ | |
118 | ||
119 | void (*pm_power_off_prepare)(void); | |
bd804eba | 120 | |
fc832ad3 SH |
121 | /* |
122 | * Returns true if current's euid is same as p's uid or euid, | |
123 | * or has CAP_SYS_NICE to p's user_ns. | |
124 | * | |
125 | * Called with rcu_read_lock, creds are safe | |
126 | */ | |
127 | static bool set_one_prio_perm(struct task_struct *p) | |
128 | { | |
129 | const struct cred *cred = current_cred(), *pcred = __task_cred(p); | |
130 | ||
131 | if (pcred->user->user_ns == cred->user->user_ns && | |
132 | (pcred->uid == cred->euid || | |
133 | pcred->euid == cred->euid)) | |
134 | return true; | |
135 | if (ns_capable(pcred->user->user_ns, CAP_SYS_NICE)) | |
136 | return true; | |
137 | return false; | |
138 | } | |
139 | ||
c69e8d9c DH |
140 | /* |
141 | * set the priority of a task | |
142 | * - the caller must hold the RCU read lock | |
143 | */ | |
1da177e4 LT |
144 | static int set_one_prio(struct task_struct *p, int niceval, int error) |
145 | { | |
146 | int no_nice; | |
147 | ||
fc832ad3 | 148 | if (!set_one_prio_perm(p)) { |
1da177e4 LT |
149 | error = -EPERM; |
150 | goto out; | |
151 | } | |
e43379f1 | 152 | if (niceval < task_nice(p) && !can_nice(p, niceval)) { |
1da177e4 LT |
153 | error = -EACCES; |
154 | goto out; | |
155 | } | |
156 | no_nice = security_task_setnice(p, niceval); | |
157 | if (no_nice) { | |
158 | error = no_nice; | |
159 | goto out; | |
160 | } | |
161 | if (error == -ESRCH) | |
162 | error = 0; | |
163 | set_user_nice(p, niceval); | |
164 | out: | |
165 | return error; | |
166 | } | |
167 | ||
754fe8d2 | 168 | SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval) |
1da177e4 LT |
169 | { |
170 | struct task_struct *g, *p; | |
171 | struct user_struct *user; | |
86a264ab | 172 | const struct cred *cred = current_cred(); |
1da177e4 | 173 | int error = -EINVAL; |
41487c65 | 174 | struct pid *pgrp; |
1da177e4 | 175 | |
3e88c553 | 176 | if (which > PRIO_USER || which < PRIO_PROCESS) |
1da177e4 LT |
177 | goto out; |
178 | ||
179 | /* normalize: avoid signed division (rounding problems) */ | |
180 | error = -ESRCH; | |
181 | if (niceval < -20) | |
182 | niceval = -20; | |
183 | if (niceval > 19) | |
184 | niceval = 19; | |
185 | ||
d4581a23 | 186 | rcu_read_lock(); |
1da177e4 LT |
187 | read_lock(&tasklist_lock); |
188 | switch (which) { | |
189 | case PRIO_PROCESS: | |
41487c65 | 190 | if (who) |
228ebcbe | 191 | p = find_task_by_vpid(who); |
41487c65 EB |
192 | else |
193 | p = current; | |
1da177e4 LT |
194 | if (p) |
195 | error = set_one_prio(p, niceval, error); | |
196 | break; | |
197 | case PRIO_PGRP: | |
41487c65 | 198 | if (who) |
b488893a | 199 | pgrp = find_vpid(who); |
41487c65 EB |
200 | else |
201 | pgrp = task_pgrp(current); | |
2d70b68d | 202 | do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { |
1da177e4 | 203 | error = set_one_prio(p, niceval, error); |
2d70b68d | 204 | } while_each_pid_thread(pgrp, PIDTYPE_PGID, p); |
1da177e4 LT |
205 | break; |
206 | case PRIO_USER: | |
d84f4f99 | 207 | user = (struct user_struct *) cred->user; |
1da177e4 | 208 | if (!who) |
86a264ab DH |
209 | who = cred->uid; |
210 | else if ((who != cred->uid) && | |
211 | !(user = find_user(who))) | |
212 | goto out_unlock; /* No processes for this user */ | |
1da177e4 | 213 | |
dfc6a736 | 214 | do_each_thread(g, p) { |
86a264ab | 215 | if (__task_cred(p)->uid == who) |
1da177e4 | 216 | error = set_one_prio(p, niceval, error); |
dfc6a736 | 217 | } while_each_thread(g, p); |
86a264ab | 218 | if (who != cred->uid) |
1da177e4 LT |
219 | free_uid(user); /* For find_user() */ |
220 | break; | |
221 | } | |
222 | out_unlock: | |
223 | read_unlock(&tasklist_lock); | |
d4581a23 | 224 | rcu_read_unlock(); |
1da177e4 LT |
225 | out: |
226 | return error; | |
227 | } | |
228 | ||
229 | /* | |
230 | * Ugh. To avoid negative return values, "getpriority()" will | |
231 | * not return the normal nice-value, but a negated value that | |
232 | * has been offset by 20 (ie it returns 40..1 instead of -20..19) | |
233 | * to stay compatible. | |
234 | */ | |
754fe8d2 | 235 | SYSCALL_DEFINE2(getpriority, int, which, int, who) |
1da177e4 LT |
236 | { |
237 | struct task_struct *g, *p; | |
238 | struct user_struct *user; | |
86a264ab | 239 | const struct cred *cred = current_cred(); |
1da177e4 | 240 | long niceval, retval = -ESRCH; |
41487c65 | 241 | struct pid *pgrp; |
1da177e4 | 242 | |
3e88c553 | 243 | if (which > PRIO_USER || which < PRIO_PROCESS) |
1da177e4 LT |
244 | return -EINVAL; |
245 | ||
70118837 | 246 | rcu_read_lock(); |
1da177e4 LT |
247 | read_lock(&tasklist_lock); |
248 | switch (which) { | |
249 | case PRIO_PROCESS: | |
41487c65 | 250 | if (who) |
228ebcbe | 251 | p = find_task_by_vpid(who); |
41487c65 EB |
252 | else |
253 | p = current; | |
1da177e4 LT |
254 | if (p) { |
255 | niceval = 20 - task_nice(p); | |
256 | if (niceval > retval) | |
257 | retval = niceval; | |
258 | } | |
259 | break; | |
260 | case PRIO_PGRP: | |
41487c65 | 261 | if (who) |
b488893a | 262 | pgrp = find_vpid(who); |
41487c65 EB |
263 | else |
264 | pgrp = task_pgrp(current); | |
2d70b68d | 265 | do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { |
1da177e4 LT |
266 | niceval = 20 - task_nice(p); |
267 | if (niceval > retval) | |
268 | retval = niceval; | |
2d70b68d | 269 | } while_each_pid_thread(pgrp, PIDTYPE_PGID, p); |
1da177e4 LT |
270 | break; |
271 | case PRIO_USER: | |
86a264ab | 272 | user = (struct user_struct *) cred->user; |
1da177e4 | 273 | if (!who) |
86a264ab DH |
274 | who = cred->uid; |
275 | else if ((who != cred->uid) && | |
276 | !(user = find_user(who))) | |
277 | goto out_unlock; /* No processes for this user */ | |
1da177e4 | 278 | |
dfc6a736 | 279 | do_each_thread(g, p) { |
86a264ab | 280 | if (__task_cred(p)->uid == who) { |
1da177e4 LT |
281 | niceval = 20 - task_nice(p); |
282 | if (niceval > retval) | |
283 | retval = niceval; | |
284 | } | |
dfc6a736 | 285 | } while_each_thread(g, p); |
86a264ab | 286 | if (who != cred->uid) |
1da177e4 LT |
287 | free_uid(user); /* for find_user() */ |
288 | break; | |
289 | } | |
290 | out_unlock: | |
291 | read_unlock(&tasklist_lock); | |
70118837 | 292 | rcu_read_unlock(); |
1da177e4 LT |
293 | |
294 | return retval; | |
295 | } | |
296 | ||
e4c94330 EB |
297 | /** |
298 | * emergency_restart - reboot the system | |
299 | * | |
300 | * Without shutting down any hardware or taking any locks | |
301 | * reboot the system. This is called when we know we are in | |
302 | * trouble so this is our best effort to reboot. This is | |
303 | * safe to call in interrupt context. | |
304 | */ | |
7c903473 EB |
305 | void emergency_restart(void) |
306 | { | |
04c6862c | 307 | kmsg_dump(KMSG_DUMP_EMERG); |
7c903473 EB |
308 | machine_emergency_restart(); |
309 | } | |
310 | EXPORT_SYMBOL_GPL(emergency_restart); | |
311 | ||
ca195b7f | 312 | void kernel_restart_prepare(char *cmd) |
4a00ea1e | 313 | { |
e041c683 | 314 | blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd); |
4a00ea1e | 315 | system_state = SYSTEM_RESTART; |
b50fa7c8 | 316 | usermodehelper_disable(); |
4a00ea1e | 317 | device_shutdown(); |
40dc166c | 318 | syscore_shutdown(); |
e4c94330 | 319 | } |
1e5d5331 | 320 | |
c5f41752 AW |
321 | /** |
322 | * register_reboot_notifier - Register function to be called at reboot time | |
323 | * @nb: Info about notifier function to be called | |
324 | * | |
325 | * Registers a function with the list of functions | |
326 | * to be called at reboot time. | |
327 | * | |
328 | * Currently always returns zero, as blocking_notifier_chain_register() | |
329 | * always returns zero. | |
330 | */ | |
331 | int register_reboot_notifier(struct notifier_block *nb) | |
332 | { | |
333 | return blocking_notifier_chain_register(&reboot_notifier_list, nb); | |
334 | } | |
335 | EXPORT_SYMBOL(register_reboot_notifier); | |
336 | ||
337 | /** | |
338 | * unregister_reboot_notifier - Unregister previously registered reboot notifier | |
339 | * @nb: Hook to be unregistered | |
340 | * | |
341 | * Unregisters a previously registered reboot | |
342 | * notifier function. | |
343 | * | |
344 | * Returns zero on success, or %-ENOENT on failure. | |
345 | */ | |
346 | int unregister_reboot_notifier(struct notifier_block *nb) | |
347 | { | |
348 | return blocking_notifier_chain_unregister(&reboot_notifier_list, nb); | |
349 | } | |
350 | EXPORT_SYMBOL(unregister_reboot_notifier); | |
351 | ||
1e5d5331 RD |
352 | /** |
353 | * kernel_restart - reboot the system | |
354 | * @cmd: pointer to buffer containing command to execute for restart | |
b8887e6e | 355 | * or %NULL |
1e5d5331 RD |
356 | * |
357 | * Shutdown everything and perform a clean reboot. | |
358 | * This is not safe to call in interrupt context. | |
359 | */ | |
e4c94330 EB |
360 | void kernel_restart(char *cmd) |
361 | { | |
362 | kernel_restart_prepare(cmd); | |
756184b7 | 363 | if (!cmd) |
4a00ea1e | 364 | printk(KERN_EMERG "Restarting system.\n"); |
756184b7 | 365 | else |
4a00ea1e | 366 | printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd); |
04c6862c | 367 | kmsg_dump(KMSG_DUMP_RESTART); |
4a00ea1e EB |
368 | machine_restart(cmd); |
369 | } | |
370 | EXPORT_SYMBOL_GPL(kernel_restart); | |
371 | ||
4ef7229f | 372 | static void kernel_shutdown_prepare(enum system_states state) |
729b4d4c | 373 | { |
e041c683 | 374 | blocking_notifier_call_chain(&reboot_notifier_list, |
729b4d4c AS |
375 | (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL); |
376 | system_state = state; | |
b50fa7c8 | 377 | usermodehelper_disable(); |
729b4d4c AS |
378 | device_shutdown(); |
379 | } | |
e4c94330 EB |
380 | /** |
381 | * kernel_halt - halt the system | |
382 | * | |
383 | * Shutdown everything and perform a clean system halt. | |
384 | */ | |
e4c94330 EB |
385 | void kernel_halt(void) |
386 | { | |
729b4d4c | 387 | kernel_shutdown_prepare(SYSTEM_HALT); |
40dc166c | 388 | syscore_shutdown(); |
4a00ea1e | 389 | printk(KERN_EMERG "System halted.\n"); |
04c6862c | 390 | kmsg_dump(KMSG_DUMP_HALT); |
4a00ea1e EB |
391 | machine_halt(); |
392 | } | |
729b4d4c | 393 | |
4a00ea1e EB |
394 | EXPORT_SYMBOL_GPL(kernel_halt); |
395 | ||
e4c94330 EB |
396 | /** |
397 | * kernel_power_off - power_off the system | |
398 | * | |
399 | * Shutdown everything and perform a clean system power_off. | |
400 | */ | |
e4c94330 EB |
401 | void kernel_power_off(void) |
402 | { | |
729b4d4c | 403 | kernel_shutdown_prepare(SYSTEM_POWER_OFF); |
bd804eba RW |
404 | if (pm_power_off_prepare) |
405 | pm_power_off_prepare(); | |
4047727e | 406 | disable_nonboot_cpus(); |
40dc166c | 407 | syscore_shutdown(); |
4a00ea1e | 408 | printk(KERN_EMERG "Power down.\n"); |
04c6862c | 409 | kmsg_dump(KMSG_DUMP_POWEROFF); |
4a00ea1e EB |
410 | machine_power_off(); |
411 | } | |
412 | EXPORT_SYMBOL_GPL(kernel_power_off); | |
6f15fa50 TG |
413 | |
414 | static DEFINE_MUTEX(reboot_mutex); | |
415 | ||
1da177e4 LT |
416 | /* |
417 | * Reboot system call: for obvious reasons only root may call it, | |
418 | * and even root needs to set up some magic numbers in the registers | |
419 | * so that some mistake won't make this reboot the whole machine. | |
420 | * You can also set the meaning of the ctrl-alt-del-key here. | |
421 | * | |
422 | * reboot doesn't sync: do that yourself before calling this. | |
423 | */ | |
754fe8d2 HC |
424 | SYSCALL_DEFINE4(reboot, int, magic1, int, magic2, unsigned int, cmd, |
425 | void __user *, arg) | |
1da177e4 LT |
426 | { |
427 | char buffer[256]; | |
3d26dcf7 | 428 | int ret = 0; |
1da177e4 LT |
429 | |
430 | /* We only trust the superuser with rebooting the system. */ | |
431 | if (!capable(CAP_SYS_BOOT)) | |
432 | return -EPERM; | |
433 | ||
434 | /* For safety, we require "magic" arguments. */ | |
435 | if (magic1 != LINUX_REBOOT_MAGIC1 || | |
436 | (magic2 != LINUX_REBOOT_MAGIC2 && | |
437 | magic2 != LINUX_REBOOT_MAGIC2A && | |
438 | magic2 != LINUX_REBOOT_MAGIC2B && | |
439 | magic2 != LINUX_REBOOT_MAGIC2C)) | |
440 | return -EINVAL; | |
441 | ||
5e38291d EB |
442 | /* Instead of trying to make the power_off code look like |
443 | * halt when pm_power_off is not set do it the easy way. | |
444 | */ | |
445 | if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off) | |
446 | cmd = LINUX_REBOOT_CMD_HALT; | |
447 | ||
6f15fa50 | 448 | mutex_lock(&reboot_mutex); |
1da177e4 LT |
449 | switch (cmd) { |
450 | case LINUX_REBOOT_CMD_RESTART: | |
4a00ea1e | 451 | kernel_restart(NULL); |
1da177e4 LT |
452 | break; |
453 | ||
454 | case LINUX_REBOOT_CMD_CAD_ON: | |
455 | C_A_D = 1; | |
456 | break; | |
457 | ||
458 | case LINUX_REBOOT_CMD_CAD_OFF: | |
459 | C_A_D = 0; | |
460 | break; | |
461 | ||
462 | case LINUX_REBOOT_CMD_HALT: | |
4a00ea1e | 463 | kernel_halt(); |
1da177e4 | 464 | do_exit(0); |
3d26dcf7 | 465 | panic("cannot halt"); |
1da177e4 LT |
466 | |
467 | case LINUX_REBOOT_CMD_POWER_OFF: | |
4a00ea1e | 468 | kernel_power_off(); |
1da177e4 LT |
469 | do_exit(0); |
470 | break; | |
471 | ||
472 | case LINUX_REBOOT_CMD_RESTART2: | |
473 | if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) { | |
6f15fa50 TG |
474 | ret = -EFAULT; |
475 | break; | |
1da177e4 LT |
476 | } |
477 | buffer[sizeof(buffer) - 1] = '\0'; | |
478 | ||
4a00ea1e | 479 | kernel_restart(buffer); |
1da177e4 LT |
480 | break; |
481 | ||
3ab83521 | 482 | #ifdef CONFIG_KEXEC |
dc009d92 | 483 | case LINUX_REBOOT_CMD_KEXEC: |
3d26dcf7 AK |
484 | ret = kernel_kexec(); |
485 | break; | |
3ab83521 | 486 | #endif |
4a00ea1e | 487 | |
b0cb1a19 | 488 | #ifdef CONFIG_HIBERNATION |
1da177e4 | 489 | case LINUX_REBOOT_CMD_SW_SUSPEND: |
3d26dcf7 AK |
490 | ret = hibernate(); |
491 | break; | |
1da177e4 LT |
492 | #endif |
493 | ||
494 | default: | |
3d26dcf7 AK |
495 | ret = -EINVAL; |
496 | break; | |
1da177e4 | 497 | } |
6f15fa50 | 498 | mutex_unlock(&reboot_mutex); |
3d26dcf7 | 499 | return ret; |
1da177e4 LT |
500 | } |
501 | ||
65f27f38 | 502 | static void deferred_cad(struct work_struct *dummy) |
1da177e4 | 503 | { |
abcd9e51 | 504 | kernel_restart(NULL); |
1da177e4 LT |
505 | } |
506 | ||
507 | /* | |
508 | * This function gets called by ctrl-alt-del - ie the keyboard interrupt. | |
509 | * As it's called within an interrupt, it may NOT sync: the only choice | |
510 | * is whether to reboot at once, or just ignore the ctrl-alt-del. | |
511 | */ | |
512 | void ctrl_alt_del(void) | |
513 | { | |
65f27f38 | 514 | static DECLARE_WORK(cad_work, deferred_cad); |
1da177e4 LT |
515 | |
516 | if (C_A_D) | |
517 | schedule_work(&cad_work); | |
518 | else | |
9ec52099 | 519 | kill_cad_pid(SIGINT, 1); |
1da177e4 LT |
520 | } |
521 | ||
1da177e4 LT |
522 | /* |
523 | * Unprivileged users may change the real gid to the effective gid | |
524 | * or vice versa. (BSD-style) | |
525 | * | |
526 | * If you set the real gid at all, or set the effective gid to a value not | |
527 | * equal to the real gid, then the saved gid is set to the new effective gid. | |
528 | * | |
529 | * This makes it possible for a setgid program to completely drop its | |
530 | * privileges, which is often a useful assertion to make when you are doing | |
531 | * a security audit over a program. | |
532 | * | |
533 | * The general idea is that a program which uses just setregid() will be | |
534 | * 100% compatible with BSD. A program which uses just setgid() will be | |
535 | * 100% compatible with POSIX with saved IDs. | |
536 | * | |
537 | * SMP: There are not races, the GIDs are checked only by filesystem | |
538 | * operations (as far as semantic preservation is concerned). | |
539 | */ | |
ae1251ab | 540 | SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid) |
1da177e4 | 541 | { |
d84f4f99 DH |
542 | const struct cred *old; |
543 | struct cred *new; | |
1da177e4 LT |
544 | int retval; |
545 | ||
d84f4f99 DH |
546 | new = prepare_creds(); |
547 | if (!new) | |
548 | return -ENOMEM; | |
549 | old = current_cred(); | |
550 | ||
d84f4f99 | 551 | retval = -EPERM; |
1da177e4 | 552 | if (rgid != (gid_t) -1) { |
d84f4f99 DH |
553 | if (old->gid == rgid || |
554 | old->egid == rgid || | |
fc832ad3 | 555 | nsown_capable(CAP_SETGID)) |
d84f4f99 | 556 | new->gid = rgid; |
1da177e4 | 557 | else |
d84f4f99 | 558 | goto error; |
1da177e4 LT |
559 | } |
560 | if (egid != (gid_t) -1) { | |
d84f4f99 DH |
561 | if (old->gid == egid || |
562 | old->egid == egid || | |
563 | old->sgid == egid || | |
fc832ad3 | 564 | nsown_capable(CAP_SETGID)) |
d84f4f99 | 565 | new->egid = egid; |
756184b7 | 566 | else |
d84f4f99 | 567 | goto error; |
1da177e4 | 568 | } |
d84f4f99 | 569 | |
1da177e4 | 570 | if (rgid != (gid_t) -1 || |
d84f4f99 DH |
571 | (egid != (gid_t) -1 && egid != old->gid)) |
572 | new->sgid = new->egid; | |
573 | new->fsgid = new->egid; | |
574 | ||
575 | return commit_creds(new); | |
576 | ||
577 | error: | |
578 | abort_creds(new); | |
579 | return retval; | |
1da177e4 LT |
580 | } |
581 | ||
582 | /* | |
583 | * setgid() is implemented like SysV w/ SAVED_IDS | |
584 | * | |
585 | * SMP: Same implicit races as above. | |
586 | */ | |
ae1251ab | 587 | SYSCALL_DEFINE1(setgid, gid_t, gid) |
1da177e4 | 588 | { |
d84f4f99 DH |
589 | const struct cred *old; |
590 | struct cred *new; | |
1da177e4 LT |
591 | int retval; |
592 | ||
d84f4f99 DH |
593 | new = prepare_creds(); |
594 | if (!new) | |
595 | return -ENOMEM; | |
596 | old = current_cred(); | |
597 | ||
d84f4f99 | 598 | retval = -EPERM; |
fc832ad3 | 599 | if (nsown_capable(CAP_SETGID)) |
d84f4f99 DH |
600 | new->gid = new->egid = new->sgid = new->fsgid = gid; |
601 | else if (gid == old->gid || gid == old->sgid) | |
602 | new->egid = new->fsgid = gid; | |
1da177e4 | 603 | else |
d84f4f99 | 604 | goto error; |
1da177e4 | 605 | |
d84f4f99 DH |
606 | return commit_creds(new); |
607 | ||
608 | error: | |
609 | abort_creds(new); | |
610 | return retval; | |
1da177e4 | 611 | } |
54e99124 | 612 | |
d84f4f99 DH |
613 | /* |
614 | * change the user struct in a credentials set to match the new UID | |
615 | */ | |
616 | static int set_user(struct cred *new) | |
1da177e4 LT |
617 | { |
618 | struct user_struct *new_user; | |
619 | ||
18b6e041 | 620 | new_user = alloc_uid(current_user_ns(), new->uid); |
1da177e4 LT |
621 | if (!new_user) |
622 | return -EAGAIN; | |
623 | ||
78d7d407 | 624 | if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) && |
18b6e041 | 625 | new_user != INIT_USER) { |
1da177e4 LT |
626 | free_uid(new_user); |
627 | return -EAGAIN; | |
628 | } | |
629 | ||
d84f4f99 DH |
630 | free_uid(new->user); |
631 | new->user = new_user; | |
1da177e4 LT |
632 | return 0; |
633 | } | |
634 | ||
635 | /* | |
636 | * Unprivileged users may change the real uid to the effective uid | |
637 | * or vice versa. (BSD-style) | |
638 | * | |
639 | * If you set the real uid at all, or set the effective uid to a value not | |
640 | * equal to the real uid, then the saved uid is set to the new effective uid. | |
641 | * | |
642 | * This makes it possible for a setuid program to completely drop its | |
643 | * privileges, which is often a useful assertion to make when you are doing | |
644 | * a security audit over a program. | |
645 | * | |
646 | * The general idea is that a program which uses just setreuid() will be | |
647 | * 100% compatible with BSD. A program which uses just setuid() will be | |
648 | * 100% compatible with POSIX with saved IDs. | |
649 | */ | |
ae1251ab | 650 | SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid) |
1da177e4 | 651 | { |
d84f4f99 DH |
652 | const struct cred *old; |
653 | struct cred *new; | |
1da177e4 LT |
654 | int retval; |
655 | ||
d84f4f99 DH |
656 | new = prepare_creds(); |
657 | if (!new) | |
658 | return -ENOMEM; | |
659 | old = current_cred(); | |
660 | ||
d84f4f99 | 661 | retval = -EPERM; |
1da177e4 | 662 | if (ruid != (uid_t) -1) { |
d84f4f99 DH |
663 | new->uid = ruid; |
664 | if (old->uid != ruid && | |
665 | old->euid != ruid && | |
fc832ad3 | 666 | !nsown_capable(CAP_SETUID)) |
d84f4f99 | 667 | goto error; |
1da177e4 LT |
668 | } |
669 | ||
670 | if (euid != (uid_t) -1) { | |
d84f4f99 DH |
671 | new->euid = euid; |
672 | if (old->uid != euid && | |
673 | old->euid != euid && | |
674 | old->suid != euid && | |
fc832ad3 | 675 | !nsown_capable(CAP_SETUID)) |
d84f4f99 | 676 | goto error; |
1da177e4 LT |
677 | } |
678 | ||
54e99124 DG |
679 | if (new->uid != old->uid) { |
680 | retval = set_user(new); | |
681 | if (retval < 0) | |
682 | goto error; | |
683 | } | |
1da177e4 | 684 | if (ruid != (uid_t) -1 || |
d84f4f99 DH |
685 | (euid != (uid_t) -1 && euid != old->uid)) |
686 | new->suid = new->euid; | |
687 | new->fsuid = new->euid; | |
1da177e4 | 688 | |
d84f4f99 DH |
689 | retval = security_task_fix_setuid(new, old, LSM_SETID_RE); |
690 | if (retval < 0) | |
691 | goto error; | |
1da177e4 | 692 | |
d84f4f99 | 693 | return commit_creds(new); |
1da177e4 | 694 | |
d84f4f99 DH |
695 | error: |
696 | abort_creds(new); | |
697 | return retval; | |
698 | } | |
1da177e4 LT |
699 | |
700 | /* | |
701 | * setuid() is implemented like SysV with SAVED_IDS | |
702 | * | |
703 | * Note that SAVED_ID's is deficient in that a setuid root program | |
704 | * like sendmail, for example, cannot set its uid to be a normal | |
705 | * user and then switch back, because if you're root, setuid() sets | |
706 | * the saved uid too. If you don't like this, blame the bright people | |
707 | * in the POSIX committee and/or USG. Note that the BSD-style setreuid() | |
708 | * will allow a root program to temporarily drop privileges and be able to | |
709 | * regain them by swapping the real and effective uid. | |
710 | */ | |
ae1251ab | 711 | SYSCALL_DEFINE1(setuid, uid_t, uid) |
1da177e4 | 712 | { |
d84f4f99 DH |
713 | const struct cred *old; |
714 | struct cred *new; | |
1da177e4 LT |
715 | int retval; |
716 | ||
d84f4f99 DH |
717 | new = prepare_creds(); |
718 | if (!new) | |
719 | return -ENOMEM; | |
720 | old = current_cred(); | |
721 | ||
d84f4f99 | 722 | retval = -EPERM; |
fc832ad3 | 723 | if (nsown_capable(CAP_SETUID)) { |
d84f4f99 | 724 | new->suid = new->uid = uid; |
54e99124 DG |
725 | if (uid != old->uid) { |
726 | retval = set_user(new); | |
727 | if (retval < 0) | |
728 | goto error; | |
d84f4f99 DH |
729 | } |
730 | } else if (uid != old->uid && uid != new->suid) { | |
731 | goto error; | |
1da177e4 | 732 | } |
1da177e4 | 733 | |
d84f4f99 DH |
734 | new->fsuid = new->euid = uid; |
735 | ||
736 | retval = security_task_fix_setuid(new, old, LSM_SETID_ID); | |
737 | if (retval < 0) | |
738 | goto error; | |
1da177e4 | 739 | |
d84f4f99 | 740 | return commit_creds(new); |
1da177e4 | 741 | |
d84f4f99 DH |
742 | error: |
743 | abort_creds(new); | |
744 | return retval; | |
1da177e4 LT |
745 | } |
746 | ||
747 | ||
748 | /* | |
749 | * This function implements a generic ability to update ruid, euid, | |
750 | * and suid. This allows you to implement the 4.4 compatible seteuid(). | |
751 | */ | |
ae1251ab | 752 | SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid) |
1da177e4 | 753 | { |
d84f4f99 DH |
754 | const struct cred *old; |
755 | struct cred *new; | |
1da177e4 LT |
756 | int retval; |
757 | ||
d84f4f99 DH |
758 | new = prepare_creds(); |
759 | if (!new) | |
760 | return -ENOMEM; | |
761 | ||
d84f4f99 | 762 | old = current_cred(); |
1da177e4 | 763 | |
d84f4f99 | 764 | retval = -EPERM; |
fc832ad3 | 765 | if (!nsown_capable(CAP_SETUID)) { |
d84f4f99 DH |
766 | if (ruid != (uid_t) -1 && ruid != old->uid && |
767 | ruid != old->euid && ruid != old->suid) | |
768 | goto error; | |
769 | if (euid != (uid_t) -1 && euid != old->uid && | |
770 | euid != old->euid && euid != old->suid) | |
771 | goto error; | |
772 | if (suid != (uid_t) -1 && suid != old->uid && | |
773 | suid != old->euid && suid != old->suid) | |
774 | goto error; | |
1da177e4 | 775 | } |
d84f4f99 | 776 | |
1da177e4 | 777 | if (ruid != (uid_t) -1) { |
d84f4f99 | 778 | new->uid = ruid; |
54e99124 DG |
779 | if (ruid != old->uid) { |
780 | retval = set_user(new); | |
781 | if (retval < 0) | |
782 | goto error; | |
783 | } | |
1da177e4 | 784 | } |
d84f4f99 DH |
785 | if (euid != (uid_t) -1) |
786 | new->euid = euid; | |
1da177e4 | 787 | if (suid != (uid_t) -1) |
d84f4f99 DH |
788 | new->suid = suid; |
789 | new->fsuid = new->euid; | |
1da177e4 | 790 | |
d84f4f99 DH |
791 | retval = security_task_fix_setuid(new, old, LSM_SETID_RES); |
792 | if (retval < 0) | |
793 | goto error; | |
1da177e4 | 794 | |
d84f4f99 | 795 | return commit_creds(new); |
1da177e4 | 796 | |
d84f4f99 DH |
797 | error: |
798 | abort_creds(new); | |
799 | return retval; | |
1da177e4 LT |
800 | } |
801 | ||
dbf040d9 | 802 | SYSCALL_DEFINE3(getresuid, uid_t __user *, ruid, uid_t __user *, euid, uid_t __user *, suid) |
1da177e4 | 803 | { |
86a264ab | 804 | const struct cred *cred = current_cred(); |
1da177e4 LT |
805 | int retval; |
806 | ||
86a264ab DH |
807 | if (!(retval = put_user(cred->uid, ruid)) && |
808 | !(retval = put_user(cred->euid, euid))) | |
b6dff3ec | 809 | retval = put_user(cred->suid, suid); |
1da177e4 LT |
810 | |
811 | return retval; | |
812 | } | |
813 | ||
814 | /* | |
815 | * Same as above, but for rgid, egid, sgid. | |
816 | */ | |
ae1251ab | 817 | SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid) |
1da177e4 | 818 | { |
d84f4f99 DH |
819 | const struct cred *old; |
820 | struct cred *new; | |
1da177e4 LT |
821 | int retval; |
822 | ||
d84f4f99 DH |
823 | new = prepare_creds(); |
824 | if (!new) | |
825 | return -ENOMEM; | |
826 | old = current_cred(); | |
827 | ||
d84f4f99 | 828 | retval = -EPERM; |
fc832ad3 | 829 | if (!nsown_capable(CAP_SETGID)) { |
d84f4f99 DH |
830 | if (rgid != (gid_t) -1 && rgid != old->gid && |
831 | rgid != old->egid && rgid != old->sgid) | |
832 | goto error; | |
833 | if (egid != (gid_t) -1 && egid != old->gid && | |
834 | egid != old->egid && egid != old->sgid) | |
835 | goto error; | |
836 | if (sgid != (gid_t) -1 && sgid != old->gid && | |
837 | sgid != old->egid && sgid != old->sgid) | |
838 | goto error; | |
1da177e4 | 839 | } |
d84f4f99 | 840 | |
1da177e4 | 841 | if (rgid != (gid_t) -1) |
d84f4f99 DH |
842 | new->gid = rgid; |
843 | if (egid != (gid_t) -1) | |
844 | new->egid = egid; | |
1da177e4 | 845 | if (sgid != (gid_t) -1) |
d84f4f99 DH |
846 | new->sgid = sgid; |
847 | new->fsgid = new->egid; | |
1da177e4 | 848 | |
d84f4f99 DH |
849 | return commit_creds(new); |
850 | ||
851 | error: | |
852 | abort_creds(new); | |
853 | return retval; | |
1da177e4 LT |
854 | } |
855 | ||
dbf040d9 | 856 | SYSCALL_DEFINE3(getresgid, gid_t __user *, rgid, gid_t __user *, egid, gid_t __user *, sgid) |
1da177e4 | 857 | { |
86a264ab | 858 | const struct cred *cred = current_cred(); |
1da177e4 LT |
859 | int retval; |
860 | ||
86a264ab DH |
861 | if (!(retval = put_user(cred->gid, rgid)) && |
862 | !(retval = put_user(cred->egid, egid))) | |
b6dff3ec | 863 | retval = put_user(cred->sgid, sgid); |
1da177e4 LT |
864 | |
865 | return retval; | |
866 | } | |
867 | ||
868 | ||
869 | /* | |
870 | * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This | |
871 | * is used for "access()" and for the NFS daemon (letting nfsd stay at | |
872 | * whatever uid it wants to). It normally shadows "euid", except when | |
873 | * explicitly set by setfsuid() or for access.. | |
874 | */ | |
ae1251ab | 875 | SYSCALL_DEFINE1(setfsuid, uid_t, uid) |
1da177e4 | 876 | { |
d84f4f99 DH |
877 | const struct cred *old; |
878 | struct cred *new; | |
879 | uid_t old_fsuid; | |
1da177e4 | 880 | |
d84f4f99 DH |
881 | new = prepare_creds(); |
882 | if (!new) | |
883 | return current_fsuid(); | |
884 | old = current_cred(); | |
885 | old_fsuid = old->fsuid; | |
1da177e4 | 886 | |
d84f4f99 DH |
887 | if (uid == old->uid || uid == old->euid || |
888 | uid == old->suid || uid == old->fsuid || | |
fc832ad3 | 889 | nsown_capable(CAP_SETUID)) { |
756184b7 | 890 | if (uid != old_fsuid) { |
d84f4f99 DH |
891 | new->fsuid = uid; |
892 | if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0) | |
893 | goto change_okay; | |
1da177e4 | 894 | } |
1da177e4 LT |
895 | } |
896 | ||
d84f4f99 DH |
897 | abort_creds(new); |
898 | return old_fsuid; | |
1da177e4 | 899 | |
d84f4f99 DH |
900 | change_okay: |
901 | commit_creds(new); | |
1da177e4 LT |
902 | return old_fsuid; |
903 | } | |
904 | ||
905 | /* | |
f42df9e6 | 906 | * Samma på svenska.. |
1da177e4 | 907 | */ |
ae1251ab | 908 | SYSCALL_DEFINE1(setfsgid, gid_t, gid) |
1da177e4 | 909 | { |
d84f4f99 DH |
910 | const struct cred *old; |
911 | struct cred *new; | |
912 | gid_t old_fsgid; | |
913 | ||
914 | new = prepare_creds(); | |
915 | if (!new) | |
916 | return current_fsgid(); | |
917 | old = current_cred(); | |
918 | old_fsgid = old->fsgid; | |
1da177e4 | 919 | |
d84f4f99 DH |
920 | if (gid == old->gid || gid == old->egid || |
921 | gid == old->sgid || gid == old->fsgid || | |
fc832ad3 | 922 | nsown_capable(CAP_SETGID)) { |
756184b7 | 923 | if (gid != old_fsgid) { |
d84f4f99 DH |
924 | new->fsgid = gid; |
925 | goto change_okay; | |
1da177e4 | 926 | } |
1da177e4 | 927 | } |
d84f4f99 | 928 | |
d84f4f99 DH |
929 | abort_creds(new); |
930 | return old_fsgid; | |
931 | ||
932 | change_okay: | |
933 | commit_creds(new); | |
1da177e4 LT |
934 | return old_fsgid; |
935 | } | |
936 | ||
f06febc9 FM |
937 | void do_sys_times(struct tms *tms) |
938 | { | |
0cf55e1e | 939 | cputime_t tgutime, tgstime, cutime, cstime; |
f06febc9 | 940 | |
2b5fe6de | 941 | spin_lock_irq(¤t->sighand->siglock); |
0cf55e1e | 942 | thread_group_times(current, &tgutime, &tgstime); |
f06febc9 FM |
943 | cutime = current->signal->cutime; |
944 | cstime = current->signal->cstime; | |
945 | spin_unlock_irq(¤t->sighand->siglock); | |
0cf55e1e HS |
946 | tms->tms_utime = cputime_to_clock_t(tgutime); |
947 | tms->tms_stime = cputime_to_clock_t(tgstime); | |
f06febc9 FM |
948 | tms->tms_cutime = cputime_to_clock_t(cutime); |
949 | tms->tms_cstime = cputime_to_clock_t(cstime); | |
950 | } | |
951 | ||
58fd3aa2 | 952 | SYSCALL_DEFINE1(times, struct tms __user *, tbuf) |
1da177e4 | 953 | { |
1da177e4 LT |
954 | if (tbuf) { |
955 | struct tms tmp; | |
f06febc9 FM |
956 | |
957 | do_sys_times(&tmp); | |
1da177e4 LT |
958 | if (copy_to_user(tbuf, &tmp, sizeof(struct tms))) |
959 | return -EFAULT; | |
960 | } | |
e3d5a27d | 961 | force_successful_syscall_return(); |
1da177e4 LT |
962 | return (long) jiffies_64_to_clock_t(get_jiffies_64()); |
963 | } | |
964 | ||
965 | /* | |
966 | * This needs some heavy checking ... | |
967 | * I just haven't the stomach for it. I also don't fully | |
968 | * understand sessions/pgrp etc. Let somebody who does explain it. | |
969 | * | |
970 | * OK, I think I have the protection semantics right.... this is really | |
971 | * only important on a multi-user system anyway, to make sure one user | |
972 | * can't send a signal to a process owned by another. -TYT, 12/12/91 | |
973 | * | |
974 | * Auch. Had to add the 'did_exec' flag to conform completely to POSIX. | |
975 | * LBT 04.03.94 | |
976 | */ | |
b290ebe2 | 977 | SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid) |
1da177e4 LT |
978 | { |
979 | struct task_struct *p; | |
ee0acf90 | 980 | struct task_struct *group_leader = current->group_leader; |
4e021306 ON |
981 | struct pid *pgrp; |
982 | int err; | |
1da177e4 LT |
983 | |
984 | if (!pid) | |
b488893a | 985 | pid = task_pid_vnr(group_leader); |
1da177e4 LT |
986 | if (!pgid) |
987 | pgid = pid; | |
988 | if (pgid < 0) | |
989 | return -EINVAL; | |
950eaaca | 990 | rcu_read_lock(); |
1da177e4 LT |
991 | |
992 | /* From this point forward we keep holding onto the tasklist lock | |
993 | * so that our parent does not change from under us. -DaveM | |
994 | */ | |
995 | write_lock_irq(&tasklist_lock); | |
996 | ||
997 | err = -ESRCH; | |
4e021306 | 998 | p = find_task_by_vpid(pid); |
1da177e4 LT |
999 | if (!p) |
1000 | goto out; | |
1001 | ||
1002 | err = -EINVAL; | |
1003 | if (!thread_group_leader(p)) | |
1004 | goto out; | |
1005 | ||
4e021306 | 1006 | if (same_thread_group(p->real_parent, group_leader)) { |
1da177e4 | 1007 | err = -EPERM; |
41487c65 | 1008 | if (task_session(p) != task_session(group_leader)) |
1da177e4 LT |
1009 | goto out; |
1010 | err = -EACCES; | |
1011 | if (p->did_exec) | |
1012 | goto out; | |
1013 | } else { | |
1014 | err = -ESRCH; | |
ee0acf90 | 1015 | if (p != group_leader) |
1da177e4 LT |
1016 | goto out; |
1017 | } | |
1018 | ||
1019 | err = -EPERM; | |
1020 | if (p->signal->leader) | |
1021 | goto out; | |
1022 | ||
4e021306 | 1023 | pgrp = task_pid(p); |
1da177e4 | 1024 | if (pgid != pid) { |
b488893a | 1025 | struct task_struct *g; |
1da177e4 | 1026 | |
4e021306 ON |
1027 | pgrp = find_vpid(pgid); |
1028 | g = pid_task(pgrp, PIDTYPE_PGID); | |
41487c65 | 1029 | if (!g || task_session(g) != task_session(group_leader)) |
f020bc46 | 1030 | goto out; |
1da177e4 LT |
1031 | } |
1032 | ||
1da177e4 LT |
1033 | err = security_task_setpgid(p, pgid); |
1034 | if (err) | |
1035 | goto out; | |
1036 | ||
1b0f7ffd | 1037 | if (task_pgrp(p) != pgrp) |
83beaf3c | 1038 | change_pid(p, PIDTYPE_PGID, pgrp); |
1da177e4 LT |
1039 | |
1040 | err = 0; | |
1041 | out: | |
1042 | /* All paths lead to here, thus we are safe. -DaveM */ | |
1043 | write_unlock_irq(&tasklist_lock); | |
950eaaca | 1044 | rcu_read_unlock(); |
1da177e4 LT |
1045 | return err; |
1046 | } | |
1047 | ||
dbf040d9 | 1048 | SYSCALL_DEFINE1(getpgid, pid_t, pid) |
1da177e4 | 1049 | { |
12a3de0a ON |
1050 | struct task_struct *p; |
1051 | struct pid *grp; | |
1052 | int retval; | |
1053 | ||
1054 | rcu_read_lock(); | |
756184b7 | 1055 | if (!pid) |
12a3de0a | 1056 | grp = task_pgrp(current); |
756184b7 | 1057 | else { |
1da177e4 | 1058 | retval = -ESRCH; |
12a3de0a ON |
1059 | p = find_task_by_vpid(pid); |
1060 | if (!p) | |
1061 | goto out; | |
1062 | grp = task_pgrp(p); | |
1063 | if (!grp) | |
1064 | goto out; | |
1065 | ||
1066 | retval = security_task_getpgid(p); | |
1067 | if (retval) | |
1068 | goto out; | |
1da177e4 | 1069 | } |
12a3de0a ON |
1070 | retval = pid_vnr(grp); |
1071 | out: | |
1072 | rcu_read_unlock(); | |
1073 | return retval; | |
1da177e4 LT |
1074 | } |
1075 | ||
1076 | #ifdef __ARCH_WANT_SYS_GETPGRP | |
1077 | ||
dbf040d9 | 1078 | SYSCALL_DEFINE0(getpgrp) |
1da177e4 | 1079 | { |
12a3de0a | 1080 | return sys_getpgid(0); |
1da177e4 LT |
1081 | } |
1082 | ||
1083 | #endif | |
1084 | ||
dbf040d9 | 1085 | SYSCALL_DEFINE1(getsid, pid_t, pid) |
1da177e4 | 1086 | { |
1dd768c0 ON |
1087 | struct task_struct *p; |
1088 | struct pid *sid; | |
1089 | int retval; | |
1090 | ||
1091 | rcu_read_lock(); | |
756184b7 | 1092 | if (!pid) |
1dd768c0 | 1093 | sid = task_session(current); |
756184b7 | 1094 | else { |
1da177e4 | 1095 | retval = -ESRCH; |
1dd768c0 ON |
1096 | p = find_task_by_vpid(pid); |
1097 | if (!p) | |
1098 | goto out; | |
1099 | sid = task_session(p); | |
1100 | if (!sid) | |
1101 | goto out; | |
1102 | ||
1103 | retval = security_task_getsid(p); | |
1104 | if (retval) | |
1105 | goto out; | |
1da177e4 | 1106 | } |
1dd768c0 ON |
1107 | retval = pid_vnr(sid); |
1108 | out: | |
1109 | rcu_read_unlock(); | |
1110 | return retval; | |
1da177e4 LT |
1111 | } |
1112 | ||
b290ebe2 | 1113 | SYSCALL_DEFINE0(setsid) |
1da177e4 | 1114 | { |
e19f247a | 1115 | struct task_struct *group_leader = current->group_leader; |
e4cc0a9c ON |
1116 | struct pid *sid = task_pid(group_leader); |
1117 | pid_t session = pid_vnr(sid); | |
1da177e4 LT |
1118 | int err = -EPERM; |
1119 | ||
1da177e4 | 1120 | write_lock_irq(&tasklist_lock); |
390e2ff0 EB |
1121 | /* Fail if I am already a session leader */ |
1122 | if (group_leader->signal->leader) | |
1123 | goto out; | |
1124 | ||
430c6231 ON |
1125 | /* Fail if a process group id already exists that equals the |
1126 | * proposed session id. | |
390e2ff0 | 1127 | */ |
6806aac6 | 1128 | if (pid_task(sid, PIDTYPE_PGID)) |
1da177e4 LT |
1129 | goto out; |
1130 | ||
e19f247a | 1131 | group_leader->signal->leader = 1; |
8520d7c7 | 1132 | __set_special_pids(sid); |
24ec839c | 1133 | |
9c9f4ded | 1134 | proc_clear_tty(group_leader); |
24ec839c | 1135 | |
e4cc0a9c | 1136 | err = session; |
1da177e4 LT |
1137 | out: |
1138 | write_unlock_irq(&tasklist_lock); | |
5091faa4 | 1139 | if (err > 0) { |
0d0df599 | 1140 | proc_sid_connector(group_leader); |
5091faa4 MG |
1141 | sched_autogroup_create_attach(group_leader); |
1142 | } | |
1da177e4 LT |
1143 | return err; |
1144 | } | |
1145 | ||
1da177e4 LT |
1146 | DECLARE_RWSEM(uts_sem); |
1147 | ||
e28cbf22 CH |
1148 | #ifdef COMPAT_UTS_MACHINE |
1149 | #define override_architecture(name) \ | |
46da2766 | 1150 | (personality(current->personality) == PER_LINUX32 && \ |
e28cbf22 CH |
1151 | copy_to_user(name->machine, COMPAT_UTS_MACHINE, \ |
1152 | sizeof(COMPAT_UTS_MACHINE))) | |
1153 | #else | |
1154 | #define override_architecture(name) 0 | |
1155 | #endif | |
1156 | ||
e48fbb69 | 1157 | SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name) |
1da177e4 LT |
1158 | { |
1159 | int errno = 0; | |
1160 | ||
1161 | down_read(&uts_sem); | |
e9ff3990 | 1162 | if (copy_to_user(name, utsname(), sizeof *name)) |
1da177e4 LT |
1163 | errno = -EFAULT; |
1164 | up_read(&uts_sem); | |
e28cbf22 CH |
1165 | |
1166 | if (!errno && override_architecture(name)) | |
1167 | errno = -EFAULT; | |
1da177e4 LT |
1168 | return errno; |
1169 | } | |
1170 | ||
5cacdb4a CH |
1171 | #ifdef __ARCH_WANT_SYS_OLD_UNAME |
1172 | /* | |
1173 | * Old cruft | |
1174 | */ | |
1175 | SYSCALL_DEFINE1(uname, struct old_utsname __user *, name) | |
1176 | { | |
1177 | int error = 0; | |
1178 | ||
1179 | if (!name) | |
1180 | return -EFAULT; | |
1181 | ||
1182 | down_read(&uts_sem); | |
1183 | if (copy_to_user(name, utsname(), sizeof(*name))) | |
1184 | error = -EFAULT; | |
1185 | up_read(&uts_sem); | |
1186 | ||
1187 | if (!error && override_architecture(name)) | |
1188 | error = -EFAULT; | |
1189 | return error; | |
1190 | } | |
1191 | ||
1192 | SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name) | |
1193 | { | |
1194 | int error; | |
1195 | ||
1196 | if (!name) | |
1197 | return -EFAULT; | |
1198 | if (!access_ok(VERIFY_WRITE, name, sizeof(struct oldold_utsname))) | |
1199 | return -EFAULT; | |
1200 | ||
1201 | down_read(&uts_sem); | |
1202 | error = __copy_to_user(&name->sysname, &utsname()->sysname, | |
1203 | __OLD_UTS_LEN); | |
1204 | error |= __put_user(0, name->sysname + __OLD_UTS_LEN); | |
1205 | error |= __copy_to_user(&name->nodename, &utsname()->nodename, | |
1206 | __OLD_UTS_LEN); | |
1207 | error |= __put_user(0, name->nodename + __OLD_UTS_LEN); | |
1208 | error |= __copy_to_user(&name->release, &utsname()->release, | |
1209 | __OLD_UTS_LEN); | |
1210 | error |= __put_user(0, name->release + __OLD_UTS_LEN); | |
1211 | error |= __copy_to_user(&name->version, &utsname()->version, | |
1212 | __OLD_UTS_LEN); | |
1213 | error |= __put_user(0, name->version + __OLD_UTS_LEN); | |
1214 | error |= __copy_to_user(&name->machine, &utsname()->machine, | |
1215 | __OLD_UTS_LEN); | |
1216 | error |= __put_user(0, name->machine + __OLD_UTS_LEN); | |
1217 | up_read(&uts_sem); | |
1218 | ||
1219 | if (!error && override_architecture(name)) | |
1220 | error = -EFAULT; | |
1221 | return error ? -EFAULT : 0; | |
1222 | } | |
1223 | #endif | |
1224 | ||
5a8a82b1 | 1225 | SYSCALL_DEFINE2(sethostname, char __user *, name, int, len) |
1da177e4 LT |
1226 | { |
1227 | int errno; | |
1228 | char tmp[__NEW_UTS_LEN]; | |
1229 | ||
bb96a6f5 | 1230 | if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN)) |
1da177e4 | 1231 | return -EPERM; |
fc832ad3 | 1232 | |
1da177e4 LT |
1233 | if (len < 0 || len > __NEW_UTS_LEN) |
1234 | return -EINVAL; | |
1235 | down_write(&uts_sem); | |
1236 | errno = -EFAULT; | |
1237 | if (!copy_from_user(tmp, name, len)) { | |
9679e4dd AM |
1238 | struct new_utsname *u = utsname(); |
1239 | ||
1240 | memcpy(u->nodename, tmp, len); | |
1241 | memset(u->nodename + len, 0, sizeof(u->nodename) - len); | |
1da177e4 LT |
1242 | errno = 0; |
1243 | } | |
1244 | up_write(&uts_sem); | |
1245 | return errno; | |
1246 | } | |
1247 | ||
1248 | #ifdef __ARCH_WANT_SYS_GETHOSTNAME | |
1249 | ||
5a8a82b1 | 1250 | SYSCALL_DEFINE2(gethostname, char __user *, name, int, len) |
1da177e4 LT |
1251 | { |
1252 | int i, errno; | |
9679e4dd | 1253 | struct new_utsname *u; |
1da177e4 LT |
1254 | |
1255 | if (len < 0) | |
1256 | return -EINVAL; | |
1257 | down_read(&uts_sem); | |
9679e4dd AM |
1258 | u = utsname(); |
1259 | i = 1 + strlen(u->nodename); | |
1da177e4 LT |
1260 | if (i > len) |
1261 | i = len; | |
1262 | errno = 0; | |
9679e4dd | 1263 | if (copy_to_user(name, u->nodename, i)) |
1da177e4 LT |
1264 | errno = -EFAULT; |
1265 | up_read(&uts_sem); | |
1266 | return errno; | |
1267 | } | |
1268 | ||
1269 | #endif | |
1270 | ||
1271 | /* | |
1272 | * Only setdomainname; getdomainname can be implemented by calling | |
1273 | * uname() | |
1274 | */ | |
5a8a82b1 | 1275 | SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len) |
1da177e4 LT |
1276 | { |
1277 | int errno; | |
1278 | char tmp[__NEW_UTS_LEN]; | |
1279 | ||
fc832ad3 | 1280 | if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN)) |
1da177e4 LT |
1281 | return -EPERM; |
1282 | if (len < 0 || len > __NEW_UTS_LEN) | |
1283 | return -EINVAL; | |
1284 | ||
1285 | down_write(&uts_sem); | |
1286 | errno = -EFAULT; | |
1287 | if (!copy_from_user(tmp, name, len)) { | |
9679e4dd AM |
1288 | struct new_utsname *u = utsname(); |
1289 | ||
1290 | memcpy(u->domainname, tmp, len); | |
1291 | memset(u->domainname + len, 0, sizeof(u->domainname) - len); | |
1da177e4 LT |
1292 | errno = 0; |
1293 | } | |
1294 | up_write(&uts_sem); | |
1295 | return errno; | |
1296 | } | |
1297 | ||
e48fbb69 | 1298 | SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim) |
1da177e4 | 1299 | { |
b9518345 JS |
1300 | struct rlimit value; |
1301 | int ret; | |
1302 | ||
1303 | ret = do_prlimit(current, resource, NULL, &value); | |
1304 | if (!ret) | |
1305 | ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0; | |
1306 | ||
1307 | return ret; | |
1da177e4 LT |
1308 | } |
1309 | ||
1310 | #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT | |
1311 | ||
1312 | /* | |
1313 | * Back compatibility for getrlimit. Needed for some apps. | |
1314 | */ | |
1315 | ||
e48fbb69 HC |
1316 | SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource, |
1317 | struct rlimit __user *, rlim) | |
1da177e4 LT |
1318 | { |
1319 | struct rlimit x; | |
1320 | if (resource >= RLIM_NLIMITS) | |
1321 | return -EINVAL; | |
1322 | ||
1323 | task_lock(current->group_leader); | |
1324 | x = current->signal->rlim[resource]; | |
1325 | task_unlock(current->group_leader); | |
756184b7 | 1326 | if (x.rlim_cur > 0x7FFFFFFF) |
1da177e4 | 1327 | x.rlim_cur = 0x7FFFFFFF; |
756184b7 | 1328 | if (x.rlim_max > 0x7FFFFFFF) |
1da177e4 LT |
1329 | x.rlim_max = 0x7FFFFFFF; |
1330 | return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0; | |
1331 | } | |
1332 | ||
1333 | #endif | |
1334 | ||
c022a0ac JS |
1335 | static inline bool rlim64_is_infinity(__u64 rlim64) |
1336 | { | |
1337 | #if BITS_PER_LONG < 64 | |
1338 | return rlim64 >= ULONG_MAX; | |
1339 | #else | |
1340 | return rlim64 == RLIM64_INFINITY; | |
1341 | #endif | |
1342 | } | |
1343 | ||
1344 | static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64) | |
1345 | { | |
1346 | if (rlim->rlim_cur == RLIM_INFINITY) | |
1347 | rlim64->rlim_cur = RLIM64_INFINITY; | |
1348 | else | |
1349 | rlim64->rlim_cur = rlim->rlim_cur; | |
1350 | if (rlim->rlim_max == RLIM_INFINITY) | |
1351 | rlim64->rlim_max = RLIM64_INFINITY; | |
1352 | else | |
1353 | rlim64->rlim_max = rlim->rlim_max; | |
1354 | } | |
1355 | ||
1356 | static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim) | |
1357 | { | |
1358 | if (rlim64_is_infinity(rlim64->rlim_cur)) | |
1359 | rlim->rlim_cur = RLIM_INFINITY; | |
1360 | else | |
1361 | rlim->rlim_cur = (unsigned long)rlim64->rlim_cur; | |
1362 | if (rlim64_is_infinity(rlim64->rlim_max)) | |
1363 | rlim->rlim_max = RLIM_INFINITY; | |
1364 | else | |
1365 | rlim->rlim_max = (unsigned long)rlim64->rlim_max; | |
1366 | } | |
1367 | ||
1c1e618d | 1368 | /* make sure you are allowed to change @tsk limits before calling this */ |
5b41535a JS |
1369 | int do_prlimit(struct task_struct *tsk, unsigned int resource, |
1370 | struct rlimit *new_rlim, struct rlimit *old_rlim) | |
1da177e4 | 1371 | { |
5b41535a | 1372 | struct rlimit *rlim; |
86f162f4 | 1373 | int retval = 0; |
1da177e4 LT |
1374 | |
1375 | if (resource >= RLIM_NLIMITS) | |
1376 | return -EINVAL; | |
5b41535a JS |
1377 | if (new_rlim) { |
1378 | if (new_rlim->rlim_cur > new_rlim->rlim_max) | |
1379 | return -EINVAL; | |
1380 | if (resource == RLIMIT_NOFILE && | |
1381 | new_rlim->rlim_max > sysctl_nr_open) | |
1382 | return -EPERM; | |
1383 | } | |
1da177e4 | 1384 | |
1c1e618d JS |
1385 | /* protect tsk->signal and tsk->sighand from disappearing */ |
1386 | read_lock(&tasklist_lock); | |
1387 | if (!tsk->sighand) { | |
1388 | retval = -ESRCH; | |
1389 | goto out; | |
1390 | } | |
1391 | ||
5b41535a | 1392 | rlim = tsk->signal->rlim + resource; |
86f162f4 | 1393 | task_lock(tsk->group_leader); |
5b41535a | 1394 | if (new_rlim) { |
fc832ad3 SH |
1395 | /* Keep the capable check against init_user_ns until |
1396 | cgroups can contain all limits */ | |
5b41535a JS |
1397 | if (new_rlim->rlim_max > rlim->rlim_max && |
1398 | !capable(CAP_SYS_RESOURCE)) | |
1399 | retval = -EPERM; | |
1400 | if (!retval) | |
1401 | retval = security_task_setrlimit(tsk->group_leader, | |
1402 | resource, new_rlim); | |
1403 | if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) { | |
1404 | /* | |
1405 | * The caller is asking for an immediate RLIMIT_CPU | |
1406 | * expiry. But we use the zero value to mean "it was | |
1407 | * never set". So let's cheat and make it one second | |
1408 | * instead | |
1409 | */ | |
1410 | new_rlim->rlim_cur = 1; | |
1411 | } | |
1412 | } | |
1413 | if (!retval) { | |
1414 | if (old_rlim) | |
1415 | *old_rlim = *rlim; | |
1416 | if (new_rlim) | |
1417 | *rlim = *new_rlim; | |
9926e4c7 | 1418 | } |
7855c35d | 1419 | task_unlock(tsk->group_leader); |
1da177e4 | 1420 | |
d3561f78 AM |
1421 | /* |
1422 | * RLIMIT_CPU handling. Note that the kernel fails to return an error | |
1423 | * code if it rejected the user's attempt to set RLIMIT_CPU. This is a | |
1424 | * very long-standing error, and fixing it now risks breakage of | |
1425 | * applications, so we live with it | |
1426 | */ | |
5b41535a JS |
1427 | if (!retval && new_rlim && resource == RLIMIT_CPU && |
1428 | new_rlim->rlim_cur != RLIM_INFINITY) | |
1429 | update_rlimit_cpu(tsk, new_rlim->rlim_cur); | |
ec9e16ba | 1430 | out: |
1c1e618d | 1431 | read_unlock(&tasklist_lock); |
2fb9d268 | 1432 | return retval; |
1da177e4 LT |
1433 | } |
1434 | ||
c022a0ac JS |
1435 | /* rcu lock must be held */ |
1436 | static int check_prlimit_permission(struct task_struct *task) | |
1437 | { | |
1438 | const struct cred *cred = current_cred(), *tcred; | |
1439 | ||
fc832ad3 SH |
1440 | if (current == task) |
1441 | return 0; | |
c022a0ac | 1442 | |
fc832ad3 SH |
1443 | tcred = __task_cred(task); |
1444 | if (cred->user->user_ns == tcred->user->user_ns && | |
1445 | (cred->uid == tcred->euid && | |
1446 | cred->uid == tcred->suid && | |
1447 | cred->uid == tcred->uid && | |
1448 | cred->gid == tcred->egid && | |
1449 | cred->gid == tcred->sgid && | |
1450 | cred->gid == tcred->gid)) | |
1451 | return 0; | |
1452 | if (ns_capable(tcred->user->user_ns, CAP_SYS_RESOURCE)) | |
1453 | return 0; | |
1454 | ||
1455 | return -EPERM; | |
c022a0ac JS |
1456 | } |
1457 | ||
1458 | SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource, | |
1459 | const struct rlimit64 __user *, new_rlim, | |
1460 | struct rlimit64 __user *, old_rlim) | |
1461 | { | |
1462 | struct rlimit64 old64, new64; | |
1463 | struct rlimit old, new; | |
1464 | struct task_struct *tsk; | |
1465 | int ret; | |
1466 | ||
1467 | if (new_rlim) { | |
1468 | if (copy_from_user(&new64, new_rlim, sizeof(new64))) | |
1469 | return -EFAULT; | |
1470 | rlim64_to_rlim(&new64, &new); | |
1471 | } | |
1472 | ||
1473 | rcu_read_lock(); | |
1474 | tsk = pid ? find_task_by_vpid(pid) : current; | |
1475 | if (!tsk) { | |
1476 | rcu_read_unlock(); | |
1477 | return -ESRCH; | |
1478 | } | |
1479 | ret = check_prlimit_permission(tsk); | |
1480 | if (ret) { | |
1481 | rcu_read_unlock(); | |
1482 | return ret; | |
1483 | } | |
1484 | get_task_struct(tsk); | |
1485 | rcu_read_unlock(); | |
1486 | ||
1487 | ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL, | |
1488 | old_rlim ? &old : NULL); | |
1489 | ||
1490 | if (!ret && old_rlim) { | |
1491 | rlim_to_rlim64(&old, &old64); | |
1492 | if (copy_to_user(old_rlim, &old64, sizeof(old64))) | |
1493 | ret = -EFAULT; | |
1494 | } | |
1495 | ||
1496 | put_task_struct(tsk); | |
1497 | return ret; | |
1498 | } | |
1499 | ||
7855c35d JS |
1500 | SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim) |
1501 | { | |
1502 | struct rlimit new_rlim; | |
1503 | ||
1504 | if (copy_from_user(&new_rlim, rlim, sizeof(*rlim))) | |
1505 | return -EFAULT; | |
5b41535a | 1506 | return do_prlimit(current, resource, &new_rlim, NULL); |
7855c35d JS |
1507 | } |
1508 | ||
1da177e4 LT |
1509 | /* |
1510 | * It would make sense to put struct rusage in the task_struct, | |
1511 | * except that would make the task_struct be *really big*. After | |
1512 | * task_struct gets moved into malloc'ed memory, it would | |
1513 | * make sense to do this. It will make moving the rest of the information | |
1514 | * a lot simpler! (Which we're not doing right now because we're not | |
1515 | * measuring them yet). | |
1516 | * | |
1da177e4 LT |
1517 | * When sampling multiple threads for RUSAGE_SELF, under SMP we might have |
1518 | * races with threads incrementing their own counters. But since word | |
1519 | * reads are atomic, we either get new values or old values and we don't | |
1520 | * care which for the sums. We always take the siglock to protect reading | |
1521 | * the c* fields from p->signal from races with exit.c updating those | |
1522 | * fields when reaping, so a sample either gets all the additions of a | |
1523 | * given child after it's reaped, or none so this sample is before reaping. | |
2dd0ebcd | 1524 | * |
de047c1b RT |
1525 | * Locking: |
1526 | * We need to take the siglock for CHILDEREN, SELF and BOTH | |
1527 | * for the cases current multithreaded, non-current single threaded | |
1528 | * non-current multithreaded. Thread traversal is now safe with | |
1529 | * the siglock held. | |
1530 | * Strictly speaking, we donot need to take the siglock if we are current and | |
1531 | * single threaded, as no one else can take our signal_struct away, no one | |
1532 | * else can reap the children to update signal->c* counters, and no one else | |
1533 | * can race with the signal-> fields. If we do not take any lock, the | |
1534 | * signal-> fields could be read out of order while another thread was just | |
1535 | * exiting. So we should place a read memory barrier when we avoid the lock. | |
1536 | * On the writer side, write memory barrier is implied in __exit_signal | |
1537 | * as __exit_signal releases the siglock spinlock after updating the signal-> | |
1538 | * fields. But we don't do this yet to keep things simple. | |
2dd0ebcd | 1539 | * |
1da177e4 LT |
1540 | */ |
1541 | ||
f06febc9 | 1542 | static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r) |
679c9cd4 | 1543 | { |
679c9cd4 SK |
1544 | r->ru_nvcsw += t->nvcsw; |
1545 | r->ru_nivcsw += t->nivcsw; | |
1546 | r->ru_minflt += t->min_flt; | |
1547 | r->ru_majflt += t->maj_flt; | |
1548 | r->ru_inblock += task_io_get_inblock(t); | |
1549 | r->ru_oublock += task_io_get_oublock(t); | |
1550 | } | |
1551 | ||
1da177e4 LT |
1552 | static void k_getrusage(struct task_struct *p, int who, struct rusage *r) |
1553 | { | |
1554 | struct task_struct *t; | |
1555 | unsigned long flags; | |
0cf55e1e | 1556 | cputime_t tgutime, tgstime, utime, stime; |
1f10206c | 1557 | unsigned long maxrss = 0; |
1da177e4 LT |
1558 | |
1559 | memset((char *) r, 0, sizeof *r); | |
2dd0ebcd | 1560 | utime = stime = cputime_zero; |
1da177e4 | 1561 | |
679c9cd4 | 1562 | if (who == RUSAGE_THREAD) { |
d180c5bc | 1563 | task_times(current, &utime, &stime); |
f06febc9 | 1564 | accumulate_thread_rusage(p, r); |
1f10206c | 1565 | maxrss = p->signal->maxrss; |
679c9cd4 SK |
1566 | goto out; |
1567 | } | |
1568 | ||
d6cf723a | 1569 | if (!lock_task_sighand(p, &flags)) |
de047c1b | 1570 | return; |
0f59cc4a | 1571 | |
1da177e4 | 1572 | switch (who) { |
0f59cc4a | 1573 | case RUSAGE_BOTH: |
1da177e4 | 1574 | case RUSAGE_CHILDREN: |
1da177e4 LT |
1575 | utime = p->signal->cutime; |
1576 | stime = p->signal->cstime; | |
1577 | r->ru_nvcsw = p->signal->cnvcsw; | |
1578 | r->ru_nivcsw = p->signal->cnivcsw; | |
1579 | r->ru_minflt = p->signal->cmin_flt; | |
1580 | r->ru_majflt = p->signal->cmaj_flt; | |
6eaeeaba ED |
1581 | r->ru_inblock = p->signal->cinblock; |
1582 | r->ru_oublock = p->signal->coublock; | |
1f10206c | 1583 | maxrss = p->signal->cmaxrss; |
0f59cc4a ON |
1584 | |
1585 | if (who == RUSAGE_CHILDREN) | |
1586 | break; | |
1587 | ||
1da177e4 | 1588 | case RUSAGE_SELF: |
0cf55e1e HS |
1589 | thread_group_times(p, &tgutime, &tgstime); |
1590 | utime = cputime_add(utime, tgutime); | |
1591 | stime = cputime_add(stime, tgstime); | |
1da177e4 LT |
1592 | r->ru_nvcsw += p->signal->nvcsw; |
1593 | r->ru_nivcsw += p->signal->nivcsw; | |
1594 | r->ru_minflt += p->signal->min_flt; | |
1595 | r->ru_majflt += p->signal->maj_flt; | |
6eaeeaba ED |
1596 | r->ru_inblock += p->signal->inblock; |
1597 | r->ru_oublock += p->signal->oublock; | |
1f10206c JP |
1598 | if (maxrss < p->signal->maxrss) |
1599 | maxrss = p->signal->maxrss; | |
1da177e4 LT |
1600 | t = p; |
1601 | do { | |
f06febc9 | 1602 | accumulate_thread_rusage(t, r); |
1da177e4 LT |
1603 | t = next_thread(t); |
1604 | } while (t != p); | |
1da177e4 | 1605 | break; |
0f59cc4a | 1606 | |
1da177e4 LT |
1607 | default: |
1608 | BUG(); | |
1609 | } | |
de047c1b | 1610 | unlock_task_sighand(p, &flags); |
de047c1b | 1611 | |
679c9cd4 | 1612 | out: |
0f59cc4a ON |
1613 | cputime_to_timeval(utime, &r->ru_utime); |
1614 | cputime_to_timeval(stime, &r->ru_stime); | |
1f10206c JP |
1615 | |
1616 | if (who != RUSAGE_CHILDREN) { | |
1617 | struct mm_struct *mm = get_task_mm(p); | |
1618 | if (mm) { | |
1619 | setmax_mm_hiwater_rss(&maxrss, mm); | |
1620 | mmput(mm); | |
1621 | } | |
1622 | } | |
1623 | r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */ | |
1da177e4 LT |
1624 | } |
1625 | ||
1626 | int getrusage(struct task_struct *p, int who, struct rusage __user *ru) | |
1627 | { | |
1628 | struct rusage r; | |
1da177e4 | 1629 | k_getrusage(p, who, &r); |
1da177e4 LT |
1630 | return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0; |
1631 | } | |
1632 | ||
e48fbb69 | 1633 | SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru) |
1da177e4 | 1634 | { |
679c9cd4 SK |
1635 | if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN && |
1636 | who != RUSAGE_THREAD) | |
1da177e4 LT |
1637 | return -EINVAL; |
1638 | return getrusage(current, who, ru); | |
1639 | } | |
1640 | ||
e48fbb69 | 1641 | SYSCALL_DEFINE1(umask, int, mask) |
1da177e4 LT |
1642 | { |
1643 | mask = xchg(¤t->fs->umask, mask & S_IRWXUGO); | |
1644 | return mask; | |
1645 | } | |
3b7391de | 1646 | |
c4ea37c2 HC |
1647 | SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, |
1648 | unsigned long, arg4, unsigned long, arg5) | |
1da177e4 | 1649 | { |
b6dff3ec DH |
1650 | struct task_struct *me = current; |
1651 | unsigned char comm[sizeof(me->comm)]; | |
1652 | long error; | |
1da177e4 | 1653 | |
d84f4f99 DH |
1654 | error = security_task_prctl(option, arg2, arg3, arg4, arg5); |
1655 | if (error != -ENOSYS) | |
1da177e4 LT |
1656 | return error; |
1657 | ||
d84f4f99 | 1658 | error = 0; |
1da177e4 LT |
1659 | switch (option) { |
1660 | case PR_SET_PDEATHSIG: | |
0730ded5 | 1661 | if (!valid_signal(arg2)) { |
1da177e4 LT |
1662 | error = -EINVAL; |
1663 | break; | |
1664 | } | |
b6dff3ec DH |
1665 | me->pdeath_signal = arg2; |
1666 | error = 0; | |
1da177e4 LT |
1667 | break; |
1668 | case PR_GET_PDEATHSIG: | |
b6dff3ec | 1669 | error = put_user(me->pdeath_signal, (int __user *)arg2); |
1da177e4 LT |
1670 | break; |
1671 | case PR_GET_DUMPABLE: | |
b6dff3ec | 1672 | error = get_dumpable(me->mm); |
1da177e4 LT |
1673 | break; |
1674 | case PR_SET_DUMPABLE: | |
abf75a50 | 1675 | if (arg2 < 0 || arg2 > 1) { |
1da177e4 LT |
1676 | error = -EINVAL; |
1677 | break; | |
1678 | } | |
b6dff3ec DH |
1679 | set_dumpable(me->mm, arg2); |
1680 | error = 0; | |
1da177e4 LT |
1681 | break; |
1682 | ||
1683 | case PR_SET_UNALIGN: | |
b6dff3ec | 1684 | error = SET_UNALIGN_CTL(me, arg2); |
1da177e4 LT |
1685 | break; |
1686 | case PR_GET_UNALIGN: | |
b6dff3ec | 1687 | error = GET_UNALIGN_CTL(me, arg2); |
1da177e4 LT |
1688 | break; |
1689 | case PR_SET_FPEMU: | |
b6dff3ec | 1690 | error = SET_FPEMU_CTL(me, arg2); |
1da177e4 LT |
1691 | break; |
1692 | case PR_GET_FPEMU: | |
b6dff3ec | 1693 | error = GET_FPEMU_CTL(me, arg2); |
1da177e4 LT |
1694 | break; |
1695 | case PR_SET_FPEXC: | |
b6dff3ec | 1696 | error = SET_FPEXC_CTL(me, arg2); |
1da177e4 LT |
1697 | break; |
1698 | case PR_GET_FPEXC: | |
b6dff3ec | 1699 | error = GET_FPEXC_CTL(me, arg2); |
1da177e4 LT |
1700 | break; |
1701 | case PR_GET_TIMING: | |
1702 | error = PR_TIMING_STATISTICAL; | |
1703 | break; | |
1704 | case PR_SET_TIMING: | |
7b26655f | 1705 | if (arg2 != PR_TIMING_STATISTICAL) |
1da177e4 | 1706 | error = -EINVAL; |
b6dff3ec DH |
1707 | else |
1708 | error = 0; | |
1da177e4 LT |
1709 | break; |
1710 | ||
b6dff3ec DH |
1711 | case PR_SET_NAME: |
1712 | comm[sizeof(me->comm)-1] = 0; | |
1713 | if (strncpy_from_user(comm, (char __user *)arg2, | |
1714 | sizeof(me->comm) - 1) < 0) | |
1da177e4 | 1715 | return -EFAULT; |
b6dff3ec | 1716 | set_task_comm(me, comm); |
1da177e4 | 1717 | return 0; |
b6dff3ec DH |
1718 | case PR_GET_NAME: |
1719 | get_task_comm(comm, me); | |
1720 | if (copy_to_user((char __user *)arg2, comm, | |
1721 | sizeof(comm))) | |
1da177e4 LT |
1722 | return -EFAULT; |
1723 | return 0; | |
651d765d | 1724 | case PR_GET_ENDIAN: |
b6dff3ec | 1725 | error = GET_ENDIAN(me, arg2); |
651d765d AB |
1726 | break; |
1727 | case PR_SET_ENDIAN: | |
b6dff3ec | 1728 | error = SET_ENDIAN(me, arg2); |
651d765d AB |
1729 | break; |
1730 | ||
1d9d02fe AA |
1731 | case PR_GET_SECCOMP: |
1732 | error = prctl_get_seccomp(); | |
1733 | break; | |
1734 | case PR_SET_SECCOMP: | |
1735 | error = prctl_set_seccomp(arg2); | |
1736 | break; | |
8fb402bc EB |
1737 | case PR_GET_TSC: |
1738 | error = GET_TSC_CTL(arg2); | |
1739 | break; | |
1740 | case PR_SET_TSC: | |
1741 | error = SET_TSC_CTL(arg2); | |
1742 | break; | |
cdd6c482 IM |
1743 | case PR_TASK_PERF_EVENTS_DISABLE: |
1744 | error = perf_event_task_disable(); | |
1d1c7ddb | 1745 | break; |
cdd6c482 IM |
1746 | case PR_TASK_PERF_EVENTS_ENABLE: |
1747 | error = perf_event_task_enable(); | |
1d1c7ddb | 1748 | break; |
6976675d AV |
1749 | case PR_GET_TIMERSLACK: |
1750 | error = current->timer_slack_ns; | |
1751 | break; | |
1752 | case PR_SET_TIMERSLACK: | |
1753 | if (arg2 <= 0) | |
1754 | current->timer_slack_ns = | |
1755 | current->default_timer_slack_ns; | |
1756 | else | |
1757 | current->timer_slack_ns = arg2; | |
b6dff3ec | 1758 | error = 0; |
6976675d | 1759 | break; |
4db96cf0 AK |
1760 | case PR_MCE_KILL: |
1761 | if (arg4 | arg5) | |
1762 | return -EINVAL; | |
1763 | switch (arg2) { | |
1087e9b4 | 1764 | case PR_MCE_KILL_CLEAR: |
4db96cf0 AK |
1765 | if (arg3 != 0) |
1766 | return -EINVAL; | |
1767 | current->flags &= ~PF_MCE_PROCESS; | |
1768 | break; | |
1087e9b4 | 1769 | case PR_MCE_KILL_SET: |
4db96cf0 | 1770 | current->flags |= PF_MCE_PROCESS; |
1087e9b4 | 1771 | if (arg3 == PR_MCE_KILL_EARLY) |
4db96cf0 | 1772 | current->flags |= PF_MCE_EARLY; |
1087e9b4 | 1773 | else if (arg3 == PR_MCE_KILL_LATE) |
4db96cf0 | 1774 | current->flags &= ~PF_MCE_EARLY; |
1087e9b4 AK |
1775 | else if (arg3 == PR_MCE_KILL_DEFAULT) |
1776 | current->flags &= | |
1777 | ~(PF_MCE_EARLY|PF_MCE_PROCESS); | |
1778 | else | |
1779 | return -EINVAL; | |
4db96cf0 AK |
1780 | break; |
1781 | default: | |
1782 | return -EINVAL; | |
1783 | } | |
1784 | error = 0; | |
1785 | break; | |
1087e9b4 AK |
1786 | case PR_MCE_KILL_GET: |
1787 | if (arg2 | arg3 | arg4 | arg5) | |
1788 | return -EINVAL; | |
1789 | if (current->flags & PF_MCE_PROCESS) | |
1790 | error = (current->flags & PF_MCE_EARLY) ? | |
1791 | PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE; | |
1792 | else | |
1793 | error = PR_MCE_KILL_DEFAULT; | |
1794 | break; | |
1da177e4 LT |
1795 | default: |
1796 | error = -EINVAL; | |
1797 | break; | |
1798 | } | |
1799 | return error; | |
1800 | } | |
3cfc348b | 1801 | |
836f92ad HC |
1802 | SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep, |
1803 | struct getcpu_cache __user *, unused) | |
3cfc348b AK |
1804 | { |
1805 | int err = 0; | |
1806 | int cpu = raw_smp_processor_id(); | |
1807 | if (cpup) | |
1808 | err |= put_user(cpu, cpup); | |
1809 | if (nodep) | |
1810 | err |= put_user(cpu_to_node(cpu), nodep); | |
3cfc348b AK |
1811 | return err ? -EFAULT : 0; |
1812 | } | |
10a0a8d4 JF |
1813 | |
1814 | char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff"; | |
1815 | ||
a06a4dc3 | 1816 | static void argv_cleanup(struct subprocess_info *info) |
10a0a8d4 | 1817 | { |
a06a4dc3 | 1818 | argv_free(info->argv); |
10a0a8d4 JF |
1819 | } |
1820 | ||
1821 | /** | |
1822 | * orderly_poweroff - Trigger an orderly system poweroff | |
1823 | * @force: force poweroff if command execution fails | |
1824 | * | |
1825 | * This may be called from any context to trigger a system shutdown. | |
1826 | * If the orderly shutdown fails, it will force an immediate shutdown. | |
1827 | */ | |
1828 | int orderly_poweroff(bool force) | |
1829 | { | |
1830 | int argc; | |
1831 | char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc); | |
1832 | static char *envp[] = { | |
1833 | "HOME=/", | |
1834 | "PATH=/sbin:/bin:/usr/sbin:/usr/bin", | |
1835 | NULL | |
1836 | }; | |
1837 | int ret = -ENOMEM; | |
1838 | struct subprocess_info *info; | |
1839 | ||
1840 | if (argv == NULL) { | |
1841 | printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n", | |
1842 | __func__, poweroff_cmd); | |
1843 | goto out; | |
1844 | } | |
1845 | ||
ac331d15 | 1846 | info = call_usermodehelper_setup(argv[0], argv, envp, GFP_ATOMIC); |
10a0a8d4 JF |
1847 | if (info == NULL) { |
1848 | argv_free(argv); | |
1849 | goto out; | |
1850 | } | |
1851 | ||
a06a4dc3 | 1852 | call_usermodehelper_setfns(info, NULL, argv_cleanup, NULL); |
10a0a8d4 | 1853 | |
86313c48 | 1854 | ret = call_usermodehelper_exec(info, UMH_NO_WAIT); |
10a0a8d4 JF |
1855 | |
1856 | out: | |
1857 | if (ret && force) { | |
1858 | printk(KERN_WARNING "Failed to start orderly shutdown: " | |
1859 | "forcing the issue\n"); | |
1860 | ||
1861 | /* I guess this should try to kick off some daemon to | |
1862 | sync and poweroff asap. Or not even bother syncing | |
1863 | if we're doing an emergency shutdown? */ | |
1864 | emergency_sync(); | |
1865 | kernel_power_off(); | |
1866 | } | |
1867 | ||
1868 | return ret; | |
1869 | } | |
1870 | EXPORT_SYMBOL_GPL(orderly_poweroff); |