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