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