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