blob: efcf76e0dadababe9c7d2188628cb47a0ad757b6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/kernel/sys.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/module.h>
8#include <linux/mm.h>
9#include <linux/utsname.h>
10#include <linux/mman.h>
11#include <linux/smp_lock.h>
12#include <linux/notifier.h>
13#include <linux/reboot.h>
14#include <linux/prctl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/highuid.h>
16#include <linux/fs.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070017#include <linux/kernel.h>
18#include <linux/kexec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/workqueue.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080020#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/device.h>
22#include <linux/key.h>
23#include <linux/times.h>
24#include <linux/posix-timers.h>
25#include <linux/security.h>
26#include <linux/dcookies.h>
27#include <linux/suspend.h>
28#include <linux/tty.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070029#include <linux/signal.h>
Matt Helsley9f460802005-11-07 00:59:16 -080030#include <linux/cn_proc.h>
Andi Kleen3cfc3482006-09-26 10:52:28 +020031#include <linux/getcpu.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <linux/compat.h>
34#include <linux/syscalls.h>
Keshavamurthy Anil S00d7c052005-12-12 00:37:33 -080035#include <linux/kprobes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37#include <asm/uaccess.h>
38#include <asm/io.h>
39#include <asm/unistd.h>
40
41#ifndef SET_UNALIGN_CTL
42# define SET_UNALIGN_CTL(a,b) (-EINVAL)
43#endif
44#ifndef GET_UNALIGN_CTL
45# define GET_UNALIGN_CTL(a,b) (-EINVAL)
46#endif
47#ifndef SET_FPEMU_CTL
48# define SET_FPEMU_CTL(a,b) (-EINVAL)
49#endif
50#ifndef GET_FPEMU_CTL
51# define GET_FPEMU_CTL(a,b) (-EINVAL)
52#endif
53#ifndef SET_FPEXC_CTL
54# define SET_FPEXC_CTL(a,b) (-EINVAL)
55#endif
56#ifndef GET_FPEXC_CTL
57# define GET_FPEXC_CTL(a,b) (-EINVAL)
58#endif
Anton Blanchard651d7652006-06-07 16:10:19 +100059#ifndef GET_ENDIAN
60# define GET_ENDIAN(a,b) (-EINVAL)
61#endif
62#ifndef SET_ENDIAN
63# define SET_ENDIAN(a,b) (-EINVAL)
64#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
66/*
67 * this is where the system-wide overflow UID and GID are defined, for
68 * architectures that now have 32-bit UID/GID but didn't in the past
69 */
70
71int overflowuid = DEFAULT_OVERFLOWUID;
72int overflowgid = DEFAULT_OVERFLOWGID;
73
74#ifdef CONFIG_UID16
75EXPORT_SYMBOL(overflowuid);
76EXPORT_SYMBOL(overflowgid);
77#endif
78
79/*
80 * the same as above, but for filesystems which can only store a 16-bit
81 * UID and GID. as such, this is needed on all architectures
82 */
83
84int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
85int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
86
87EXPORT_SYMBOL(fs_overflowuid);
88EXPORT_SYMBOL(fs_overflowgid);
89
90/*
91 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
92 */
93
94int C_A_D = 1;
Cedric Le Goater9ec52092006-10-02 02:19:00 -070095struct pid *cad_pid;
96EXPORT_SYMBOL(cad_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98/*
99 * Notifier list for kernel code which wants to be called
100 * at shutdown. This is used to stop any idling DMA operations
101 * and the like.
102 */
103
Alan Sterne041c682006-03-27 01:16:30 -0800104static BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Alan Sterne041c682006-03-27 01:16:30 -0800106/*
107 * Notifier chain core routines. The exported routines below
108 * are layered on top of these, with appropriate locking added.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 */
Alan Sterne041c682006-03-27 01:16:30 -0800110
111static int notifier_chain_register(struct notifier_block **nl,
112 struct notifier_block *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Alan Sterne041c682006-03-27 01:16:30 -0800114 while ((*nl) != NULL) {
115 if (n->priority > (*nl)->priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 break;
Alan Sterne041c682006-03-27 01:16:30 -0800117 nl = &((*nl)->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 }
Alan Sterne041c682006-03-27 01:16:30 -0800119 n->next = *nl;
120 rcu_assign_pointer(*nl, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return 0;
122}
123
Alan Sterne041c682006-03-27 01:16:30 -0800124static int notifier_chain_unregister(struct notifier_block **nl,
125 struct notifier_block *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Alan Sterne041c682006-03-27 01:16:30 -0800127 while ((*nl) != NULL) {
128 if ((*nl) == n) {
129 rcu_assign_pointer(*nl, n->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return 0;
131 }
Alan Sterne041c682006-03-27 01:16:30 -0800132 nl = &((*nl)->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return -ENOENT;
135}
136
Alan Sterne041c682006-03-27 01:16:30 -0800137static int __kprobes notifier_call_chain(struct notifier_block **nl,
138 unsigned long val, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Alan Sterne041c682006-03-27 01:16:30 -0800140 int ret = NOTIFY_DONE;
Alan Sternbbb17472006-06-25 05:47:15 -0700141 struct notifier_block *nb, *next_nb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Alan Sterne041c682006-03-27 01:16:30 -0800143 nb = rcu_dereference(*nl);
144 while (nb) {
Alan Sternbbb17472006-06-25 05:47:15 -0700145 next_nb = rcu_dereference(nb->next);
Alan Sterne041c682006-03-27 01:16:30 -0800146 ret = nb->notifier_call(nb, val, v);
147 if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
148 break;
Alan Sternbbb17472006-06-25 05:47:15 -0700149 nb = next_nb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151 return ret;
152}
153
Alan Sterne041c682006-03-27 01:16:30 -0800154/*
155 * Atomic notifier chain routines. Registration and unregistration
Alan Sterneabc0692006-10-04 02:17:04 -0700156 * use a spinlock, and call_chain is synchronized by RCU (no locks).
Alan Sterne041c682006-03-27 01:16:30 -0800157 */
158
159/**
160 * atomic_notifier_chain_register - Add notifier to an atomic notifier chain
161 * @nh: Pointer to head of the atomic notifier chain
162 * @n: New entry in notifier chain
163 *
164 * Adds a notifier to an atomic notifier chain.
165 *
166 * Currently always returns zero.
167 */
168
169int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
170 struct notifier_block *n)
171{
172 unsigned long flags;
173 int ret;
174
175 spin_lock_irqsave(&nh->lock, flags);
176 ret = notifier_chain_register(&nh->head, n);
177 spin_unlock_irqrestore(&nh->lock, flags);
178 return ret;
179}
180
181EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
182
183/**
184 * atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
185 * @nh: Pointer to head of the atomic notifier chain
186 * @n: Entry to remove from notifier chain
187 *
188 * Removes a notifier from an atomic notifier chain.
189 *
190 * Returns zero on success or %-ENOENT on failure.
191 */
192int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
193 struct notifier_block *n)
194{
195 unsigned long flags;
196 int ret;
197
198 spin_lock_irqsave(&nh->lock, flags);
199 ret = notifier_chain_unregister(&nh->head, n);
200 spin_unlock_irqrestore(&nh->lock, flags);
201 synchronize_rcu();
202 return ret;
203}
204
205EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
206
207/**
208 * atomic_notifier_call_chain - Call functions in an atomic notifier chain
209 * @nh: Pointer to head of the atomic notifier chain
210 * @val: Value passed unmodified to notifier function
211 * @v: Pointer passed unmodified to notifier function
212 *
213 * Calls each function in a notifier chain in turn. The functions
214 * run in an atomic context, so they must not block.
215 * This routine uses RCU to synchronize with changes to the chain.
216 *
217 * If the return value of the notifier can be and'ed
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800218 * with %NOTIFY_STOP_MASK then atomic_notifier_call_chain()
Alan Sterne041c682006-03-27 01:16:30 -0800219 * will return immediately, with the return value of
220 * the notifier function which halted execution.
221 * Otherwise the return value is the return value
222 * of the last notifier function called.
223 */
224
bibo,maof2aa85a2006-10-02 02:17:34 -0700225int __kprobes atomic_notifier_call_chain(struct atomic_notifier_head *nh,
Alan Sterne041c682006-03-27 01:16:30 -0800226 unsigned long val, void *v)
227{
228 int ret;
229
230 rcu_read_lock();
231 ret = notifier_call_chain(&nh->head, val, v);
232 rcu_read_unlock();
233 return ret;
234}
235
236EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
237
238/*
239 * Blocking notifier chain routines. All access to the chain is
240 * synchronized by an rwsem.
241 */
242
243/**
244 * blocking_notifier_chain_register - Add notifier to a blocking notifier chain
245 * @nh: Pointer to head of the blocking notifier chain
246 * @n: New entry in notifier chain
247 *
248 * Adds a notifier to a blocking notifier chain.
249 * Must be called in process context.
250 *
251 * Currently always returns zero.
252 */
253
254int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
255 struct notifier_block *n)
256{
257 int ret;
258
259 /*
260 * This code gets used during boot-up, when task switching is
261 * not yet working and interrupts must remain disabled. At
262 * such times we must not call down_write().
263 */
264 if (unlikely(system_state == SYSTEM_BOOTING))
265 return notifier_chain_register(&nh->head, n);
266
267 down_write(&nh->rwsem);
268 ret = notifier_chain_register(&nh->head, n);
269 up_write(&nh->rwsem);
270 return ret;
271}
272
273EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
274
275/**
276 * blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
277 * @nh: Pointer to head of the blocking notifier chain
278 * @n: Entry to remove from notifier chain
279 *
280 * Removes a notifier from a blocking notifier chain.
281 * Must be called from process context.
282 *
283 * Returns zero on success or %-ENOENT on failure.
284 */
285int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
286 struct notifier_block *n)
287{
288 int ret;
289
290 /*
291 * This code gets used during boot-up, when task switching is
292 * not yet working and interrupts must remain disabled. At
293 * such times we must not call down_write().
294 */
295 if (unlikely(system_state == SYSTEM_BOOTING))
296 return notifier_chain_unregister(&nh->head, n);
297
298 down_write(&nh->rwsem);
299 ret = notifier_chain_unregister(&nh->head, n);
300 up_write(&nh->rwsem);
301 return ret;
302}
303
304EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
305
306/**
307 * blocking_notifier_call_chain - Call functions in a blocking notifier chain
308 * @nh: Pointer to head of the blocking notifier chain
309 * @val: Value passed unmodified to notifier function
310 * @v: Pointer passed unmodified to notifier function
311 *
312 * Calls each function in a notifier chain in turn. The functions
313 * run in a process context, so they are allowed to block.
314 *
315 * If the return value of the notifier can be and'ed
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800316 * with %NOTIFY_STOP_MASK then blocking_notifier_call_chain()
Alan Sterne041c682006-03-27 01:16:30 -0800317 * will return immediately, with the return value of
318 * the notifier function which halted execution.
319 * Otherwise the return value is the return value
320 * of the last notifier function called.
321 */
322
323int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
324 unsigned long val, void *v)
325{
Ingo Molnar1b5180b2007-01-23 10:45:50 +0100326 int ret = NOTIFY_DONE;
Alan Sterne041c682006-03-27 01:16:30 -0800327
Ingo Molnar1b5180b2007-01-23 10:45:50 +0100328 /*
329 * We check the head outside the lock, but if this access is
330 * racy then it does not matter what the result of the test
331 * is, we re-check the list after having taken the lock anyway:
332 */
333 if (rcu_dereference(nh->head)) {
334 down_read(&nh->rwsem);
335 ret = notifier_call_chain(&nh->head, val, v);
336 up_read(&nh->rwsem);
337 }
Alan Sterne041c682006-03-27 01:16:30 -0800338 return ret;
339}
340
341EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
342
343/*
344 * Raw notifier chain routines. There is no protection;
345 * the caller must provide it. Use at your own risk!
346 */
347
348/**
349 * raw_notifier_chain_register - Add notifier to a raw notifier chain
350 * @nh: Pointer to head of the raw notifier chain
351 * @n: New entry in notifier chain
352 *
353 * Adds a notifier to a raw notifier chain.
354 * All locking must be provided by the caller.
355 *
356 * Currently always returns zero.
357 */
358
359int raw_notifier_chain_register(struct raw_notifier_head *nh,
360 struct notifier_block *n)
361{
362 return notifier_chain_register(&nh->head, n);
363}
364
365EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
366
367/**
368 * raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
369 * @nh: Pointer to head of the raw notifier chain
370 * @n: Entry to remove from notifier chain
371 *
372 * Removes a notifier from a raw notifier chain.
373 * All locking must be provided by the caller.
374 *
375 * Returns zero on success or %-ENOENT on failure.
376 */
377int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
378 struct notifier_block *n)
379{
380 return notifier_chain_unregister(&nh->head, n);
381}
382
383EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
384
385/**
386 * raw_notifier_call_chain - Call functions in a raw notifier chain
387 * @nh: Pointer to head of the raw notifier chain
388 * @val: Value passed unmodified to notifier function
389 * @v: Pointer passed unmodified to notifier function
390 *
391 * Calls each function in a notifier chain in turn. The functions
392 * run in an undefined context.
393 * All locking must be provided by the caller.
394 *
395 * If the return value of the notifier can be and'ed
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800396 * with %NOTIFY_STOP_MASK then raw_notifier_call_chain()
Alan Sterne041c682006-03-27 01:16:30 -0800397 * will return immediately, with the return value of
398 * the notifier function which halted execution.
399 * Otherwise the return value is the return value
400 * of the last notifier function called.
401 */
402
403int raw_notifier_call_chain(struct raw_notifier_head *nh,
404 unsigned long val, void *v)
405{
406 return notifier_call_chain(&nh->head, val, v);
407}
408
409EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Alan Sterneabc0692006-10-04 02:17:04 -0700411/*
412 * SRCU notifier chain routines. Registration and unregistration
413 * use a mutex, and call_chain is synchronized by SRCU (no locks).
414 */
415
416/**
417 * srcu_notifier_chain_register - Add notifier to an SRCU notifier chain
418 * @nh: Pointer to head of the SRCU notifier chain
419 * @n: New entry in notifier chain
420 *
421 * Adds a notifier to an SRCU notifier chain.
422 * Must be called in process context.
423 *
424 * Currently always returns zero.
425 */
426
427int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
428 struct notifier_block *n)
429{
430 int ret;
431
432 /*
433 * This code gets used during boot-up, when task switching is
434 * not yet working and interrupts must remain disabled. At
435 * such times we must not call mutex_lock().
436 */
437 if (unlikely(system_state == SYSTEM_BOOTING))
438 return notifier_chain_register(&nh->head, n);
439
440 mutex_lock(&nh->mutex);
441 ret = notifier_chain_register(&nh->head, n);
442 mutex_unlock(&nh->mutex);
443 return ret;
444}
445
446EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
447
448/**
449 * srcu_notifier_chain_unregister - Remove notifier from an SRCU notifier chain
450 * @nh: Pointer to head of the SRCU notifier chain
451 * @n: Entry to remove from notifier chain
452 *
453 * Removes a notifier from an SRCU notifier chain.
454 * Must be called from process context.
455 *
456 * Returns zero on success or %-ENOENT on failure.
457 */
458int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
459 struct notifier_block *n)
460{
461 int ret;
462
463 /*
464 * This code gets used during boot-up, when task switching is
465 * not yet working and interrupts must remain disabled. At
466 * such times we must not call mutex_lock().
467 */
468 if (unlikely(system_state == SYSTEM_BOOTING))
469 return notifier_chain_unregister(&nh->head, n);
470
471 mutex_lock(&nh->mutex);
472 ret = notifier_chain_unregister(&nh->head, n);
473 mutex_unlock(&nh->mutex);
474 synchronize_srcu(&nh->srcu);
475 return ret;
476}
477
478EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
479
480/**
481 * srcu_notifier_call_chain - Call functions in an SRCU notifier chain
482 * @nh: Pointer to head of the SRCU notifier chain
483 * @val: Value passed unmodified to notifier function
484 * @v: Pointer passed unmodified to notifier function
485 *
486 * Calls each function in a notifier chain in turn. The functions
487 * run in a process context, so they are allowed to block.
488 *
489 * If the return value of the notifier can be and'ed
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800490 * with %NOTIFY_STOP_MASK then srcu_notifier_call_chain()
Alan Sterneabc0692006-10-04 02:17:04 -0700491 * will return immediately, with the return value of
492 * the notifier function which halted execution.
493 * Otherwise the return value is the return value
494 * of the last notifier function called.
495 */
496
497int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
498 unsigned long val, void *v)
499{
500 int ret;
501 int idx;
502
503 idx = srcu_read_lock(&nh->srcu);
504 ret = notifier_call_chain(&nh->head, val, v);
505 srcu_read_unlock(&nh->srcu, idx);
506 return ret;
507}
508
509EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
510
511/**
512 * srcu_init_notifier_head - Initialize an SRCU notifier head
513 * @nh: Pointer to head of the srcu notifier chain
514 *
515 * Unlike other sorts of notifier heads, SRCU notifier heads require
516 * dynamic initialization. Be sure to call this routine before
517 * calling any of the other SRCU notifier routines for this head.
518 *
519 * If an SRCU notifier head is deallocated, it must first be cleaned
520 * up by calling srcu_cleanup_notifier_head(). Otherwise the head's
521 * per-cpu data (used by the SRCU mechanism) will leak.
522 */
523
524void srcu_init_notifier_head(struct srcu_notifier_head *nh)
525{
526 mutex_init(&nh->mutex);
Alan Sterne6a92012006-10-04 02:17:05 -0700527 if (init_srcu_struct(&nh->srcu) < 0)
528 BUG();
Alan Sterneabc0692006-10-04 02:17:04 -0700529 nh->head = NULL;
530}
531
532EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534/**
535 * register_reboot_notifier - Register function to be called at reboot time
536 * @nb: Info about notifier function to be called
537 *
538 * Registers a function with the list of functions
539 * to be called at reboot time.
540 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800541 * Currently always returns zero, as blocking_notifier_chain_register()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 * always returns zero.
543 */
544
545int register_reboot_notifier(struct notifier_block * nb)
546{
Alan Sterne041c682006-03-27 01:16:30 -0800547 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550EXPORT_SYMBOL(register_reboot_notifier);
551
552/**
553 * unregister_reboot_notifier - Unregister previously registered reboot notifier
554 * @nb: Hook to be unregistered
555 *
556 * Unregisters a previously registered reboot
557 * notifier function.
558 *
559 * Returns zero on success, or %-ENOENT on failure.
560 */
561
562int unregister_reboot_notifier(struct notifier_block * nb)
563{
Alan Sterne041c682006-03-27 01:16:30 -0800564 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
567EXPORT_SYMBOL(unregister_reboot_notifier);
568
569static int set_one_prio(struct task_struct *p, int niceval, int error)
570{
571 int no_nice;
572
573 if (p->uid != current->euid &&
574 p->euid != current->euid && !capable(CAP_SYS_NICE)) {
575 error = -EPERM;
576 goto out;
577 }
Matt Mackalle43379f2005-05-01 08:59:00 -0700578 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 error = -EACCES;
580 goto out;
581 }
582 no_nice = security_task_setnice(p, niceval);
583 if (no_nice) {
584 error = no_nice;
585 goto out;
586 }
587 if (error == -ESRCH)
588 error = 0;
589 set_user_nice(p, niceval);
590out:
591 return error;
592}
593
594asmlinkage long sys_setpriority(int which, int who, int niceval)
595{
596 struct task_struct *g, *p;
597 struct user_struct *user;
598 int error = -EINVAL;
599
600 if (which > 2 || which < 0)
601 goto out;
602
603 /* normalize: avoid signed division (rounding problems) */
604 error = -ESRCH;
605 if (niceval < -20)
606 niceval = -20;
607 if (niceval > 19)
608 niceval = 19;
609
610 read_lock(&tasklist_lock);
611 switch (which) {
612 case PRIO_PROCESS:
613 if (!who)
614 who = current->pid;
615 p = find_task_by_pid(who);
616 if (p)
617 error = set_one_prio(p, niceval, error);
618 break;
619 case PRIO_PGRP:
620 if (!who)
621 who = process_group(current);
622 do_each_task_pid(who, PIDTYPE_PGID, p) {
623 error = set_one_prio(p, niceval, error);
624 } while_each_task_pid(who, PIDTYPE_PGID, p);
625 break;
626 case PRIO_USER:
627 user = current->user;
628 if (!who)
629 who = current->uid;
630 else
631 if ((who != current->uid) && !(user = find_user(who)))
632 goto out_unlock; /* No processes for this user */
633
634 do_each_thread(g, p)
635 if (p->uid == who)
636 error = set_one_prio(p, niceval, error);
637 while_each_thread(g, p);
638 if (who != current->uid)
639 free_uid(user); /* For find_user() */
640 break;
641 }
642out_unlock:
643 read_unlock(&tasklist_lock);
644out:
645 return error;
646}
647
648/*
649 * Ugh. To avoid negative return values, "getpriority()" will
650 * not return the normal nice-value, but a negated value that
651 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
652 * to stay compatible.
653 */
654asmlinkage long sys_getpriority(int which, int who)
655{
656 struct task_struct *g, *p;
657 struct user_struct *user;
658 long niceval, retval = -ESRCH;
659
660 if (which > 2 || which < 0)
661 return -EINVAL;
662
663 read_lock(&tasklist_lock);
664 switch (which) {
665 case PRIO_PROCESS:
666 if (!who)
667 who = current->pid;
668 p = find_task_by_pid(who);
669 if (p) {
670 niceval = 20 - task_nice(p);
671 if (niceval > retval)
672 retval = niceval;
673 }
674 break;
675 case PRIO_PGRP:
676 if (!who)
677 who = process_group(current);
678 do_each_task_pid(who, PIDTYPE_PGID, p) {
679 niceval = 20 - task_nice(p);
680 if (niceval > retval)
681 retval = niceval;
682 } while_each_task_pid(who, PIDTYPE_PGID, p);
683 break;
684 case PRIO_USER:
685 user = current->user;
686 if (!who)
687 who = current->uid;
688 else
689 if ((who != current->uid) && !(user = find_user(who)))
690 goto out_unlock; /* No processes for this user */
691
692 do_each_thread(g, p)
693 if (p->uid == who) {
694 niceval = 20 - task_nice(p);
695 if (niceval > retval)
696 retval = niceval;
697 }
698 while_each_thread(g, p);
699 if (who != current->uid)
700 free_uid(user); /* for find_user() */
701 break;
702 }
703out_unlock:
704 read_unlock(&tasklist_lock);
705
706 return retval;
707}
708
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700709/**
710 * emergency_restart - reboot the system
711 *
712 * Without shutting down any hardware or taking any locks
713 * reboot the system. This is called when we know we are in
714 * trouble so this is our best effort to reboot. This is
715 * safe to call in interrupt context.
716 */
Eric W. Biederman7c903472005-07-26 11:29:55 -0600717void emergency_restart(void)
718{
719 machine_emergency_restart();
720}
721EXPORT_SYMBOL_GPL(emergency_restart);
722
Adrian Bunk83cc5ed2006-06-25 05:47:41 -0700723static void kernel_restart_prepare(char *cmd)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600724{
Alan Sterne041c682006-03-27 01:16:30 -0800725 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600726 system_state = SYSTEM_RESTART;
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600727 device_shutdown();
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700728}
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800729
730/**
731 * kernel_restart - reboot the system
732 * @cmd: pointer to buffer containing command to execute for restart
Randy Dunlapb8887e62005-11-07 01:01:07 -0800733 * or %NULL
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800734 *
735 * Shutdown everything and perform a clean reboot.
736 * This is not safe to call in interrupt context.
737 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700738void kernel_restart(char *cmd)
739{
740 kernel_restart_prepare(cmd);
Cal Peake756184b2006-09-30 23:27:24 -0700741 if (!cmd)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600742 printk(KERN_EMERG "Restarting system.\n");
Cal Peake756184b2006-09-30 23:27:24 -0700743 else
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600744 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600745 machine_restart(cmd);
746}
747EXPORT_SYMBOL_GPL(kernel_restart);
748
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700749/**
750 * kernel_kexec - reboot the system
751 *
752 * Move into place and start executing a preloaded standalone
753 * executable. If nothing was preloaded return an error.
754 */
Adrian Bunk83cc5ed2006-06-25 05:47:41 -0700755static void kernel_kexec(void)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600756{
757#ifdef CONFIG_KEXEC
758 struct kimage *image;
Al Viro4bb80892006-02-01 05:57:32 -0500759 image = xchg(&kexec_image, NULL);
Cal Peake756184b2006-09-30 23:27:24 -0700760 if (!image)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600761 return;
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700762 kernel_restart_prepare(NULL);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600763 printk(KERN_EMERG "Starting new kernel\n");
764 machine_shutdown();
765 machine_kexec(image);
766#endif
767}
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600768
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500769void kernel_shutdown_prepare(enum system_states state)
770{
Alan Sterne041c682006-03-27 01:16:30 -0800771 blocking_notifier_call_chain(&reboot_notifier_list,
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500772 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
773 system_state = state;
774 device_shutdown();
775}
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700776/**
777 * kernel_halt - halt the system
778 *
779 * Shutdown everything and perform a clean system halt.
780 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700781void kernel_halt(void)
782{
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500783 kernel_shutdown_prepare(SYSTEM_HALT);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600784 printk(KERN_EMERG "System halted.\n");
785 machine_halt();
786}
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500787
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600788EXPORT_SYMBOL_GPL(kernel_halt);
789
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700790/**
791 * kernel_power_off - power_off the system
792 *
793 * Shutdown everything and perform a clean system power_off.
794 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700795void kernel_power_off(void)
796{
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500797 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600798 printk(KERN_EMERG "Power down.\n");
799 machine_power_off();
800}
801EXPORT_SYMBOL_GPL(kernel_power_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802/*
803 * Reboot system call: for obvious reasons only root may call it,
804 * and even root needs to set up some magic numbers in the registers
805 * so that some mistake won't make this reboot the whole machine.
806 * You can also set the meaning of the ctrl-alt-del-key here.
807 *
808 * reboot doesn't sync: do that yourself before calling this.
809 */
810asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
811{
812 char buffer[256];
813
814 /* We only trust the superuser with rebooting the system. */
815 if (!capable(CAP_SYS_BOOT))
816 return -EPERM;
817
818 /* For safety, we require "magic" arguments. */
819 if (magic1 != LINUX_REBOOT_MAGIC1 ||
820 (magic2 != LINUX_REBOOT_MAGIC2 &&
821 magic2 != LINUX_REBOOT_MAGIC2A &&
822 magic2 != LINUX_REBOOT_MAGIC2B &&
823 magic2 != LINUX_REBOOT_MAGIC2C))
824 return -EINVAL;
825
Eric W. Biederman5e382912006-01-08 01:03:46 -0800826 /* Instead of trying to make the power_off code look like
827 * halt when pm_power_off is not set do it the easy way.
828 */
829 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
830 cmd = LINUX_REBOOT_CMD_HALT;
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 lock_kernel();
833 switch (cmd) {
834 case LINUX_REBOOT_CMD_RESTART:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600835 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 break;
837
838 case LINUX_REBOOT_CMD_CAD_ON:
839 C_A_D = 1;
840 break;
841
842 case LINUX_REBOOT_CMD_CAD_OFF:
843 C_A_D = 0;
844 break;
845
846 case LINUX_REBOOT_CMD_HALT:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600847 kernel_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 unlock_kernel();
849 do_exit(0);
850 break;
851
852 case LINUX_REBOOT_CMD_POWER_OFF:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600853 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 unlock_kernel();
855 do_exit(0);
856 break;
857
858 case LINUX_REBOOT_CMD_RESTART2:
859 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
860 unlock_kernel();
861 return -EFAULT;
862 }
863 buffer[sizeof(buffer) - 1] = '\0';
864
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600865 kernel_restart(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 break;
867
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700868 case LINUX_REBOOT_CMD_KEXEC:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600869 kernel_kexec();
870 unlock_kernel();
871 return -EINVAL;
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873#ifdef CONFIG_SOFTWARE_SUSPEND
874 case LINUX_REBOOT_CMD_SW_SUSPEND:
875 {
876 int ret = software_suspend();
877 unlock_kernel();
878 return ret;
879 }
880#endif
881
882 default:
883 unlock_kernel();
884 return -EINVAL;
885 }
886 unlock_kernel();
887 return 0;
888}
889
David Howells65f27f32006-11-22 14:55:48 +0000890static void deferred_cad(struct work_struct *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Eric W. Biedermanabcd9e52005-07-26 11:27:34 -0600892 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893}
894
895/*
896 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
897 * As it's called within an interrupt, it may NOT sync: the only choice
898 * is whether to reboot at once, or just ignore the ctrl-alt-del.
899 */
900void ctrl_alt_del(void)
901{
David Howells65f27f32006-11-22 14:55:48 +0000902 static DECLARE_WORK(cad_work, deferred_cad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 if (C_A_D)
905 schedule_work(&cad_work);
906 else
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700907 kill_cad_pid(SIGINT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910/*
911 * Unprivileged users may change the real gid to the effective gid
912 * or vice versa. (BSD-style)
913 *
914 * If you set the real gid at all, or set the effective gid to a value not
915 * equal to the real gid, then the saved gid is set to the new effective gid.
916 *
917 * This makes it possible for a setgid program to completely drop its
918 * privileges, which is often a useful assertion to make when you are doing
919 * a security audit over a program.
920 *
921 * The general idea is that a program which uses just setregid() will be
922 * 100% compatible with BSD. A program which uses just setgid() will be
923 * 100% compatible with POSIX with saved IDs.
924 *
925 * SMP: There are not races, the GIDs are checked only by filesystem
926 * operations (as far as semantic preservation is concerned).
927 */
928asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
929{
930 int old_rgid = current->gid;
931 int old_egid = current->egid;
932 int new_rgid = old_rgid;
933 int new_egid = old_egid;
934 int retval;
935
936 retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
937 if (retval)
938 return retval;
939
940 if (rgid != (gid_t) -1) {
941 if ((old_rgid == rgid) ||
942 (current->egid==rgid) ||
943 capable(CAP_SETGID))
944 new_rgid = rgid;
945 else
946 return -EPERM;
947 }
948 if (egid != (gid_t) -1) {
949 if ((old_rgid == egid) ||
950 (current->egid == egid) ||
951 (current->sgid == egid) ||
952 capable(CAP_SETGID))
953 new_egid = egid;
Cal Peake756184b2006-09-30 23:27:24 -0700954 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 }
Cal Peake756184b2006-09-30 23:27:24 -0700957 if (new_egid != old_egid) {
Alan Coxd6e71142005-06-23 00:09:43 -0700958 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700959 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 }
961 if (rgid != (gid_t) -1 ||
962 (egid != (gid_t) -1 && egid != old_rgid))
963 current->sgid = new_egid;
964 current->fsgid = new_egid;
965 current->egid = new_egid;
966 current->gid = new_rgid;
967 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -0800968 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return 0;
970}
971
972/*
973 * setgid() is implemented like SysV w/ SAVED_IDS
974 *
975 * SMP: Same implicit races as above.
976 */
977asmlinkage long sys_setgid(gid_t gid)
978{
979 int old_egid = current->egid;
980 int retval;
981
982 retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
983 if (retval)
984 return retval;
985
Cal Peake756184b2006-09-30 23:27:24 -0700986 if (capable(CAP_SETGID)) {
987 if (old_egid != gid) {
Alan Coxd6e71142005-06-23 00:09:43 -0700988 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700989 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 }
991 current->gid = current->egid = current->sgid = current->fsgid = gid;
Cal Peake756184b2006-09-30 23:27:24 -0700992 } else if ((gid == current->gid) || (gid == current->sgid)) {
993 if (old_egid != gid) {
Alan Coxd6e71142005-06-23 00:09:43 -0700994 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -0700995 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
997 current->egid = current->fsgid = gid;
998 }
999 else
1000 return -EPERM;
1001
1002 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001003 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 return 0;
1005}
1006
1007static int set_user(uid_t new_ruid, int dumpclear)
1008{
1009 struct user_struct *new_user;
1010
1011 new_user = alloc_uid(new_ruid);
1012 if (!new_user)
1013 return -EAGAIN;
1014
1015 if (atomic_read(&new_user->processes) >=
1016 current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
1017 new_user != &root_user) {
1018 free_uid(new_user);
1019 return -EAGAIN;
1020 }
1021
1022 switch_uid(new_user);
1023
Cal Peake756184b2006-09-30 23:27:24 -07001024 if (dumpclear) {
Alan Coxd6e71142005-06-23 00:09:43 -07001025 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001026 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
1028 current->uid = new_ruid;
1029 return 0;
1030}
1031
1032/*
1033 * Unprivileged users may change the real uid to the effective uid
1034 * or vice versa. (BSD-style)
1035 *
1036 * If you set the real uid at all, or set the effective uid to a value not
1037 * equal to the real uid, then the saved uid is set to the new effective uid.
1038 *
1039 * This makes it possible for a setuid program to completely drop its
1040 * privileges, which is often a useful assertion to make when you are doing
1041 * a security audit over a program.
1042 *
1043 * The general idea is that a program which uses just setreuid() will be
1044 * 100% compatible with BSD. A program which uses just setuid() will be
1045 * 100% compatible with POSIX with saved IDs.
1046 */
1047asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
1048{
1049 int old_ruid, old_euid, old_suid, new_ruid, new_euid;
1050 int retval;
1051
1052 retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
1053 if (retval)
1054 return retval;
1055
1056 new_ruid = old_ruid = current->uid;
1057 new_euid = old_euid = current->euid;
1058 old_suid = current->suid;
1059
1060 if (ruid != (uid_t) -1) {
1061 new_ruid = ruid;
1062 if ((old_ruid != ruid) &&
1063 (current->euid != ruid) &&
1064 !capable(CAP_SETUID))
1065 return -EPERM;
1066 }
1067
1068 if (euid != (uid_t) -1) {
1069 new_euid = euid;
1070 if ((old_ruid != euid) &&
1071 (current->euid != euid) &&
1072 (current->suid != euid) &&
1073 !capable(CAP_SETUID))
1074 return -EPERM;
1075 }
1076
1077 if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
1078 return -EAGAIN;
1079
Cal Peake756184b2006-09-30 23:27:24 -07001080 if (new_euid != old_euid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001081 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001082 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084 current->fsuid = current->euid = new_euid;
1085 if (ruid != (uid_t) -1 ||
1086 (euid != (uid_t) -1 && euid != old_ruid))
1087 current->suid = current->euid;
1088 current->fsuid = current->euid;
1089
1090 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001091 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
1093 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
1094}
1095
1096
1097
1098/*
1099 * setuid() is implemented like SysV with SAVED_IDS
1100 *
1101 * Note that SAVED_ID's is deficient in that a setuid root program
1102 * like sendmail, for example, cannot set its uid to be a normal
1103 * user and then switch back, because if you're root, setuid() sets
1104 * the saved uid too. If you don't like this, blame the bright people
1105 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
1106 * will allow a root program to temporarily drop privileges and be able to
1107 * regain them by swapping the real and effective uid.
1108 */
1109asmlinkage long sys_setuid(uid_t uid)
1110{
1111 int old_euid = current->euid;
David Rientjesa09c17a2006-12-06 20:40:18 -08001112 int old_ruid, old_suid, new_suid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 int retval;
1114
1115 retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
1116 if (retval)
1117 return retval;
1118
David Rientjesa09c17a2006-12-06 20:40:18 -08001119 old_ruid = current->uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 old_suid = current->suid;
1121 new_suid = old_suid;
1122
1123 if (capable(CAP_SETUID)) {
1124 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
1125 return -EAGAIN;
1126 new_suid = uid;
1127 } else if ((uid != current->uid) && (uid != new_suid))
1128 return -EPERM;
1129
Cal Peake756184b2006-09-30 23:27:24 -07001130 if (old_euid != uid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001131 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001132 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
1134 current->fsuid = current->euid = uid;
1135 current->suid = new_suid;
1136
1137 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001138 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
1141}
1142
1143
1144/*
1145 * This function implements a generic ability to update ruid, euid,
1146 * and suid. This allows you to implement the 4.4 compatible seteuid().
1147 */
1148asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
1149{
1150 int old_ruid = current->uid;
1151 int old_euid = current->euid;
1152 int old_suid = current->suid;
1153 int retval;
1154
1155 retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
1156 if (retval)
1157 return retval;
1158
1159 if (!capable(CAP_SETUID)) {
1160 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
1161 (ruid != current->euid) && (ruid != current->suid))
1162 return -EPERM;
1163 if ((euid != (uid_t) -1) && (euid != current->uid) &&
1164 (euid != current->euid) && (euid != current->suid))
1165 return -EPERM;
1166 if ((suid != (uid_t) -1) && (suid != current->uid) &&
1167 (suid != current->euid) && (suid != current->suid))
1168 return -EPERM;
1169 }
1170 if (ruid != (uid_t) -1) {
1171 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
1172 return -EAGAIN;
1173 }
1174 if (euid != (uid_t) -1) {
Cal Peake756184b2006-09-30 23:27:24 -07001175 if (euid != current->euid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001176 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001177 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
1179 current->euid = euid;
1180 }
1181 current->fsuid = current->euid;
1182 if (suid != (uid_t) -1)
1183 current->suid = suid;
1184
1185 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001186 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
1189}
1190
1191asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
1192{
1193 int retval;
1194
1195 if (!(retval = put_user(current->uid, ruid)) &&
1196 !(retval = put_user(current->euid, euid)))
1197 retval = put_user(current->suid, suid);
1198
1199 return retval;
1200}
1201
1202/*
1203 * Same as above, but for rgid, egid, sgid.
1204 */
1205asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
1206{
1207 int retval;
1208
1209 retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
1210 if (retval)
1211 return retval;
1212
1213 if (!capable(CAP_SETGID)) {
1214 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
1215 (rgid != current->egid) && (rgid != current->sgid))
1216 return -EPERM;
1217 if ((egid != (gid_t) -1) && (egid != current->gid) &&
1218 (egid != current->egid) && (egid != current->sgid))
1219 return -EPERM;
1220 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
1221 (sgid != current->egid) && (sgid != current->sgid))
1222 return -EPERM;
1223 }
1224 if (egid != (gid_t) -1) {
Cal Peake756184b2006-09-30 23:27:24 -07001225 if (egid != current->egid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001226 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001227 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
1229 current->egid = egid;
1230 }
1231 current->fsgid = current->egid;
1232 if (rgid != (gid_t) -1)
1233 current->gid = rgid;
1234 if (sgid != (gid_t) -1)
1235 current->sgid = sgid;
1236
1237 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001238 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return 0;
1240}
1241
1242asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
1243{
1244 int retval;
1245
1246 if (!(retval = put_user(current->gid, rgid)) &&
1247 !(retval = put_user(current->egid, egid)))
1248 retval = put_user(current->sgid, sgid);
1249
1250 return retval;
1251}
1252
1253
1254/*
1255 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
1256 * is used for "access()" and for the NFS daemon (letting nfsd stay at
1257 * whatever uid it wants to). It normally shadows "euid", except when
1258 * explicitly set by setfsuid() or for access..
1259 */
1260asmlinkage long sys_setfsuid(uid_t uid)
1261{
1262 int old_fsuid;
1263
1264 old_fsuid = current->fsuid;
1265 if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
1266 return old_fsuid;
1267
1268 if (uid == current->uid || uid == current->euid ||
1269 uid == current->suid || uid == current->fsuid ||
Cal Peake756184b2006-09-30 23:27:24 -07001270 capable(CAP_SETUID)) {
1271 if (uid != old_fsuid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001272 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001273 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 }
1275 current->fsuid = uid;
1276 }
1277
1278 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001279 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
1282
1283 return old_fsuid;
1284}
1285
1286/*
1287 * Samma på svenska..
1288 */
1289asmlinkage long sys_setfsgid(gid_t gid)
1290{
1291 int old_fsgid;
1292
1293 old_fsgid = current->fsgid;
1294 if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
1295 return old_fsgid;
1296
1297 if (gid == current->gid || gid == current->egid ||
1298 gid == current->sgid || gid == current->fsgid ||
Cal Peake756184b2006-09-30 23:27:24 -07001299 capable(CAP_SETGID)) {
1300 if (gid != old_fsgid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001301 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001302 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
1304 current->fsgid = gid;
1305 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001306 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
1308 return old_fsgid;
1309}
1310
1311asmlinkage long sys_times(struct tms __user * tbuf)
1312{
1313 /*
1314 * In the SMP world we might just be unlucky and have one of
1315 * the times increment as we use it. Since the value is an
1316 * atomically safe type this is just fine. Conceptually its
1317 * as if the syscall took an instant longer to occur.
1318 */
1319 if (tbuf) {
1320 struct tms tmp;
Oleg Nesterov35f5cad2006-03-28 16:11:19 -08001321 struct task_struct *tsk = current;
1322 struct task_struct *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 cputime_t utime, stime, cutime, cstime;
1324
Oleg Nesterov7d7185c2006-03-28 16:11:21 -08001325 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterov35f5cad2006-03-28 16:11:19 -08001326 utime = tsk->signal->utime;
1327 stime = tsk->signal->stime;
1328 t = tsk;
1329 do {
1330 utime = cputime_add(utime, t->utime);
1331 stime = cputime_add(stime, t->stime);
1332 t = next_thread(t);
1333 } while (t != tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Oleg Nesterov35f5cad2006-03-28 16:11:19 -08001335 cutime = tsk->signal->cutime;
1336 cstime = tsk->signal->cstime;
1337 spin_unlock_irq(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
1339 tmp.tms_utime = cputime_to_clock_t(utime);
1340 tmp.tms_stime = cputime_to_clock_t(stime);
1341 tmp.tms_cutime = cputime_to_clock_t(cutime);
1342 tmp.tms_cstime = cputime_to_clock_t(cstime);
1343 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1344 return -EFAULT;
1345 }
1346 return (long) jiffies_64_to_clock_t(get_jiffies_64());
1347}
1348
1349/*
1350 * This needs some heavy checking ...
1351 * I just haven't the stomach for it. I also don't fully
1352 * understand sessions/pgrp etc. Let somebody who does explain it.
1353 *
1354 * OK, I think I have the protection semantics right.... this is really
1355 * only important on a multi-user system anyway, to make sure one user
1356 * can't send a signal to a process owned by another. -TYT, 12/12/91
1357 *
1358 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1359 * LBT 04.03.94
1360 */
1361
1362asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
1363{
1364 struct task_struct *p;
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001365 struct task_struct *group_leader = current->group_leader;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 int err = -EINVAL;
1367
1368 if (!pid)
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001369 pid = group_leader->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (!pgid)
1371 pgid = pid;
1372 if (pgid < 0)
1373 return -EINVAL;
1374
1375 /* From this point forward we keep holding onto the tasklist lock
1376 * so that our parent does not change from under us. -DaveM
1377 */
1378 write_lock_irq(&tasklist_lock);
1379
1380 err = -ESRCH;
1381 p = find_task_by_pid(pid);
1382 if (!p)
1383 goto out;
1384
1385 err = -EINVAL;
1386 if (!thread_group_leader(p))
1387 goto out;
1388
Oleg Nesterovf7dd7952006-01-08 01:03:59 -08001389 if (p->real_parent == group_leader) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 err = -EPERM;
Cedric Le Goater937949d2006-12-08 02:37:54 -08001391 if (process_session(p) != process_session(group_leader))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 goto out;
1393 err = -EACCES;
1394 if (p->did_exec)
1395 goto out;
1396 } else {
1397 err = -ESRCH;
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001398 if (p != group_leader)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 goto out;
1400 }
1401
1402 err = -EPERM;
1403 if (p->signal->leader)
1404 goto out;
1405
1406 if (pgid != pid) {
Oleg Nesterovf020bc42006-12-08 02:38:02 -08001407 struct task_struct *g =
1408 find_task_by_pid_type(PIDTYPE_PGID, pgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Oleg Nesterovf020bc42006-12-08 02:38:02 -08001410 if (!g || process_session(g) != process_session(group_leader))
1411 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 }
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 err = security_task_setpgid(p, pgid);
1415 if (err)
1416 goto out;
1417
1418 if (process_group(p) != pgid) {
1419 detach_pid(p, PIDTYPE_PGID);
1420 p->signal->pgrp = pgid;
1421 attach_pid(p, PIDTYPE_PGID, pgid);
1422 }
1423
1424 err = 0;
1425out:
1426 /* All paths lead to here, thus we are safe. -DaveM */
1427 write_unlock_irq(&tasklist_lock);
1428 return err;
1429}
1430
1431asmlinkage long sys_getpgid(pid_t pid)
1432{
Cal Peake756184b2006-09-30 23:27:24 -07001433 if (!pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 return process_group(current);
Cal Peake756184b2006-09-30 23:27:24 -07001435 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 int retval;
1437 struct task_struct *p;
1438
1439 read_lock(&tasklist_lock);
1440 p = find_task_by_pid(pid);
1441
1442 retval = -ESRCH;
1443 if (p) {
1444 retval = security_task_getpgid(p);
1445 if (!retval)
1446 retval = process_group(p);
1447 }
1448 read_unlock(&tasklist_lock);
1449 return retval;
1450 }
1451}
1452
1453#ifdef __ARCH_WANT_SYS_GETPGRP
1454
1455asmlinkage long sys_getpgrp(void)
1456{
1457 /* SMP - assuming writes are word atomic this is fine */
1458 return process_group(current);
1459}
1460
1461#endif
1462
1463asmlinkage long sys_getsid(pid_t pid)
1464{
Cal Peake756184b2006-09-30 23:27:24 -07001465 if (!pid)
Cedric Le Goater937949d2006-12-08 02:37:54 -08001466 return process_session(current);
Cal Peake756184b2006-09-30 23:27:24 -07001467 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 int retval;
1469 struct task_struct *p;
1470
1471 read_lock(&tasklist_lock);
1472 p = find_task_by_pid(pid);
1473
1474 retval = -ESRCH;
Cal Peake756184b2006-09-30 23:27:24 -07001475 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 retval = security_task_getsid(p);
1477 if (!retval)
Cedric Le Goater937949d2006-12-08 02:37:54 -08001478 retval = process_session(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 }
1480 read_unlock(&tasklist_lock);
1481 return retval;
1482 }
1483}
1484
1485asmlinkage long sys_setsid(void)
1486{
Oren Laadane19f2472006-01-08 01:03:58 -08001487 struct task_struct *group_leader = current->group_leader;
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001488 pid_t session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 int err = -EPERM;
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 write_lock_irq(&tasklist_lock);
1492
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001493 /* Fail if I am already a session leader */
1494 if (group_leader->signal->leader)
1495 goto out;
1496
1497 session = group_leader->pid;
1498 /* Fail if a process group id already exists that equals the
1499 * proposed session id.
1500 *
1501 * Don't check if session id == 1 because kernel threads use this
1502 * session id and so the check will always fail and make it so
1503 * init cannot successfully call setsid.
1504 */
1505 if (session > 1 && find_task_by_pid_type(PIDTYPE_PGID, session))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 goto out;
1507
Oren Laadane19f2472006-01-08 01:03:58 -08001508 group_leader->signal->leader = 1;
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001509 __set_special_pids(session, session);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001510
1511 spin_lock(&group_leader->sighand->siglock);
Oren Laadane19f2472006-01-08 01:03:58 -08001512 group_leader->signal->tty = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001513 spin_unlock(&group_leader->sighand->siglock);
1514
Oren Laadane19f2472006-01-08 01:03:58 -08001515 err = process_group(group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516out:
1517 write_unlock_irq(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 return err;
1519}
1520
1521/*
1522 * Supplementary group IDs
1523 */
1524
1525/* init to 2 - one for init_task, one to ensure it is never freed */
1526struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
1527
1528struct group_info *groups_alloc(int gidsetsize)
1529{
1530 struct group_info *group_info;
1531 int nblocks;
1532 int i;
1533
1534 nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
1535 /* Make sure we always allocate at least one indirect block pointer */
1536 nblocks = nblocks ? : 1;
1537 group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
1538 if (!group_info)
1539 return NULL;
1540 group_info->ngroups = gidsetsize;
1541 group_info->nblocks = nblocks;
1542 atomic_set(&group_info->usage, 1);
1543
Cal Peake756184b2006-09-30 23:27:24 -07001544 if (gidsetsize <= NGROUPS_SMALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 group_info->blocks[0] = group_info->small_block;
Cal Peake756184b2006-09-30 23:27:24 -07001546 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 for (i = 0; i < nblocks; i++) {
1548 gid_t *b;
1549 b = (void *)__get_free_page(GFP_USER);
1550 if (!b)
1551 goto out_undo_partial_alloc;
1552 group_info->blocks[i] = b;
1553 }
1554 }
1555 return group_info;
1556
1557out_undo_partial_alloc:
1558 while (--i >= 0) {
1559 free_page((unsigned long)group_info->blocks[i]);
1560 }
1561 kfree(group_info);
1562 return NULL;
1563}
1564
1565EXPORT_SYMBOL(groups_alloc);
1566
1567void groups_free(struct group_info *group_info)
1568{
1569 if (group_info->blocks[0] != group_info->small_block) {
1570 int i;
1571 for (i = 0; i < group_info->nblocks; i++)
1572 free_page((unsigned long)group_info->blocks[i]);
1573 }
1574 kfree(group_info);
1575}
1576
1577EXPORT_SYMBOL(groups_free);
1578
1579/* export the group_info to a user-space array */
1580static int groups_to_user(gid_t __user *grouplist,
1581 struct group_info *group_info)
1582{
1583 int i;
1584 int count = group_info->ngroups;
1585
1586 for (i = 0; i < group_info->nblocks; i++) {
1587 int cp_count = min(NGROUPS_PER_BLOCK, count);
1588 int off = i * NGROUPS_PER_BLOCK;
1589 int len = cp_count * sizeof(*grouplist);
1590
1591 if (copy_to_user(grouplist+off, group_info->blocks[i], len))
1592 return -EFAULT;
1593
1594 count -= cp_count;
1595 }
1596 return 0;
1597}
1598
1599/* fill a group_info from a user-space array - it must be allocated already */
1600static int groups_from_user(struct group_info *group_info,
1601 gid_t __user *grouplist)
Cal Peake756184b2006-09-30 23:27:24 -07001602{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 int i;
1604 int count = group_info->ngroups;
1605
1606 for (i = 0; i < group_info->nblocks; i++) {
1607 int cp_count = min(NGROUPS_PER_BLOCK, count);
1608 int off = i * NGROUPS_PER_BLOCK;
1609 int len = cp_count * sizeof(*grouplist);
1610
1611 if (copy_from_user(group_info->blocks[i], grouplist+off, len))
1612 return -EFAULT;
1613
1614 count -= cp_count;
1615 }
1616 return 0;
1617}
1618
Domen Puncerebe8b542005-05-05 16:16:19 -07001619/* a simple Shell sort */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620static void groups_sort(struct group_info *group_info)
1621{
1622 int base, max, stride;
1623 int gidsetsize = group_info->ngroups;
1624
1625 for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
1626 ; /* nothing */
1627 stride /= 3;
1628
1629 while (stride) {
1630 max = gidsetsize - stride;
1631 for (base = 0; base < max; base++) {
1632 int left = base;
1633 int right = left + stride;
1634 gid_t tmp = GROUP_AT(group_info, right);
1635
1636 while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
1637 GROUP_AT(group_info, right) =
1638 GROUP_AT(group_info, left);
1639 right = left;
1640 left -= stride;
1641 }
1642 GROUP_AT(group_info, right) = tmp;
1643 }
1644 stride /= 3;
1645 }
1646}
1647
1648/* a simple bsearch */
David Howells3e301482005-06-23 22:00:56 -07001649int groups_search(struct group_info *group_info, gid_t grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
Eric Dumazetd74beb92006-03-25 03:08:19 -08001651 unsigned int left, right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 if (!group_info)
1654 return 0;
1655
1656 left = 0;
1657 right = group_info->ngroups;
1658 while (left < right) {
Eric Dumazetd74beb92006-03-25 03:08:19 -08001659 unsigned int mid = (left+right)/2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 int cmp = grp - GROUP_AT(group_info, mid);
1661 if (cmp > 0)
1662 left = mid + 1;
1663 else if (cmp < 0)
1664 right = mid;
1665 else
1666 return 1;
1667 }
1668 return 0;
1669}
1670
1671/* validate and set current->group_info */
1672int set_current_groups(struct group_info *group_info)
1673{
1674 int retval;
1675 struct group_info *old_info;
1676
1677 retval = security_task_setgroups(group_info);
1678 if (retval)
1679 return retval;
1680
1681 groups_sort(group_info);
1682 get_group_info(group_info);
1683
1684 task_lock(current);
1685 old_info = current->group_info;
1686 current->group_info = group_info;
1687 task_unlock(current);
1688
1689 put_group_info(old_info);
1690
1691 return 0;
1692}
1693
1694EXPORT_SYMBOL(set_current_groups);
1695
1696asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1697{
1698 int i = 0;
1699
1700 /*
1701 * SMP: Nobody else can change our grouplist. Thus we are
1702 * safe.
1703 */
1704
1705 if (gidsetsize < 0)
1706 return -EINVAL;
1707
1708 /* no need to grab task_lock here; it cannot change */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 i = current->group_info->ngroups;
1710 if (gidsetsize) {
1711 if (i > gidsetsize) {
1712 i = -EINVAL;
1713 goto out;
1714 }
1715 if (groups_to_user(grouplist, current->group_info)) {
1716 i = -EFAULT;
1717 goto out;
1718 }
1719 }
1720out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 return i;
1722}
1723
1724/*
1725 * SMP: Our groups are copy-on-write. We can set them safely
1726 * without another task interfering.
1727 */
1728
1729asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
1730{
1731 struct group_info *group_info;
1732 int retval;
1733
1734 if (!capable(CAP_SETGID))
1735 return -EPERM;
1736 if ((unsigned)gidsetsize > NGROUPS_MAX)
1737 return -EINVAL;
1738
1739 group_info = groups_alloc(gidsetsize);
1740 if (!group_info)
1741 return -ENOMEM;
1742 retval = groups_from_user(group_info, grouplist);
1743 if (retval) {
1744 put_group_info(group_info);
1745 return retval;
1746 }
1747
1748 retval = set_current_groups(group_info);
1749 put_group_info(group_info);
1750
1751 return retval;
1752}
1753
1754/*
1755 * Check whether we're fsgid/egid or in the supplemental group..
1756 */
1757int in_group_p(gid_t grp)
1758{
1759 int retval = 1;
Cal Peake756184b2006-09-30 23:27:24 -07001760 if (grp != current->fsgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 retval = groups_search(current->group_info, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return retval;
1763}
1764
1765EXPORT_SYMBOL(in_group_p);
1766
1767int in_egroup_p(gid_t grp)
1768{
1769 int retval = 1;
Cal Peake756184b2006-09-30 23:27:24 -07001770 if (grp != current->egid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 retval = groups_search(current->group_info, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return retval;
1773}
1774
1775EXPORT_SYMBOL(in_egroup_p);
1776
1777DECLARE_RWSEM(uts_sem);
1778
David S. Miller393b0722005-11-10 12:47:50 -08001779EXPORT_SYMBOL(uts_sem);
1780
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781asmlinkage long sys_newuname(struct new_utsname __user * name)
1782{
1783 int errno = 0;
1784
1785 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001786 if (copy_to_user(name, utsname(), sizeof *name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 errno = -EFAULT;
1788 up_read(&uts_sem);
1789 return errno;
1790}
1791
1792asmlinkage long sys_sethostname(char __user *name, int len)
1793{
1794 int errno;
1795 char tmp[__NEW_UTS_LEN];
1796
1797 if (!capable(CAP_SYS_ADMIN))
1798 return -EPERM;
1799 if (len < 0 || len > __NEW_UTS_LEN)
1800 return -EINVAL;
1801 down_write(&uts_sem);
1802 errno = -EFAULT;
1803 if (!copy_from_user(tmp, name, len)) {
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001804 memcpy(utsname()->nodename, tmp, len);
1805 utsname()->nodename[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 errno = 0;
1807 }
1808 up_write(&uts_sem);
1809 return errno;
1810}
1811
1812#ifdef __ARCH_WANT_SYS_GETHOSTNAME
1813
1814asmlinkage long sys_gethostname(char __user *name, int len)
1815{
1816 int i, errno;
1817
1818 if (len < 0)
1819 return -EINVAL;
1820 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001821 i = 1 + strlen(utsname()->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 if (i > len)
1823 i = len;
1824 errno = 0;
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001825 if (copy_to_user(name, utsname()->nodename, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 errno = -EFAULT;
1827 up_read(&uts_sem);
1828 return errno;
1829}
1830
1831#endif
1832
1833/*
1834 * Only setdomainname; getdomainname can be implemented by calling
1835 * uname()
1836 */
1837asmlinkage long sys_setdomainname(char __user *name, int len)
1838{
1839 int errno;
1840 char tmp[__NEW_UTS_LEN];
1841
1842 if (!capable(CAP_SYS_ADMIN))
1843 return -EPERM;
1844 if (len < 0 || len > __NEW_UTS_LEN)
1845 return -EINVAL;
1846
1847 down_write(&uts_sem);
1848 errno = -EFAULT;
1849 if (!copy_from_user(tmp, name, len)) {
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001850 memcpy(utsname()->domainname, tmp, len);
1851 utsname()->domainname[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 errno = 0;
1853 }
1854 up_write(&uts_sem);
1855 return errno;
1856}
1857
1858asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1859{
1860 if (resource >= RLIM_NLIMITS)
1861 return -EINVAL;
1862 else {
1863 struct rlimit value;
1864 task_lock(current->group_leader);
1865 value = current->signal->rlim[resource];
1866 task_unlock(current->group_leader);
1867 return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1868 }
1869}
1870
1871#ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1872
1873/*
1874 * Back compatibility for getrlimit. Needed for some apps.
1875 */
1876
1877asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1878{
1879 struct rlimit x;
1880 if (resource >= RLIM_NLIMITS)
1881 return -EINVAL;
1882
1883 task_lock(current->group_leader);
1884 x = current->signal->rlim[resource];
1885 task_unlock(current->group_leader);
Cal Peake756184b2006-09-30 23:27:24 -07001886 if (x.rlim_cur > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 x.rlim_cur = 0x7FFFFFFF;
Cal Peake756184b2006-09-30 23:27:24 -07001888 if (x.rlim_max > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 x.rlim_max = 0x7FFFFFFF;
1890 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1891}
1892
1893#endif
1894
1895asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
1896{
1897 struct rlimit new_rlim, *old_rlim;
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001898 unsigned long it_prof_secs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 int retval;
1900
1901 if (resource >= RLIM_NLIMITS)
1902 return -EINVAL;
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001903 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 return -EFAULT;
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001905 if (new_rlim.rlim_cur > new_rlim.rlim_max)
1906 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 old_rlim = current->signal->rlim + resource;
1908 if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
1909 !capable(CAP_SYS_RESOURCE))
1910 return -EPERM;
1911 if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001912 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 retval = security_task_setrlimit(resource, &new_rlim);
1915 if (retval)
1916 return retval;
1917
1918 task_lock(current->group_leader);
1919 *old_rlim = new_rlim;
1920 task_unlock(current->group_leader);
1921
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001922 if (resource != RLIMIT_CPU)
1923 goto out;
Andrew Mortond3561f72006-03-24 03:18:36 -08001924
1925 /*
1926 * RLIMIT_CPU handling. Note that the kernel fails to return an error
1927 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
1928 * very long-standing error, and fixing it now risks breakage of
1929 * applications, so we live with it
1930 */
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001931 if (new_rlim.rlim_cur == RLIM_INFINITY)
1932 goto out;
1933
1934 it_prof_secs = cputime_to_secs(current->signal->it_prof_expires);
1935 if (it_prof_secs == 0 || new_rlim.rlim_cur <= it_prof_secs) {
Andrew Mortone0661112006-03-24 03:18:35 -08001936 unsigned long rlim_cur = new_rlim.rlim_cur;
1937 cputime_t cputime;
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001938
Andrew Mortone0661112006-03-24 03:18:35 -08001939 if (rlim_cur == 0) {
1940 /*
1941 * The caller is asking for an immediate RLIMIT_CPU
1942 * expiry. But we use the zero value to mean "it was
1943 * never set". So let's cheat and make it one second
1944 * instead
1945 */
1946 rlim_cur = 1;
1947 }
1948 cputime = secs_to_cputime(rlim_cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 read_lock(&tasklist_lock);
1950 spin_lock_irq(&current->sighand->siglock);
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001951 set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 spin_unlock_irq(&current->sighand->siglock);
1953 read_unlock(&tasklist_lock);
1954 }
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001955out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 return 0;
1957}
1958
1959/*
1960 * It would make sense to put struct rusage in the task_struct,
1961 * except that would make the task_struct be *really big*. After
1962 * task_struct gets moved into malloc'ed memory, it would
1963 * make sense to do this. It will make moving the rest of the information
1964 * a lot simpler! (Which we're not doing right now because we're not
1965 * measuring them yet).
1966 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
1968 * races with threads incrementing their own counters. But since word
1969 * reads are atomic, we either get new values or old values and we don't
1970 * care which for the sums. We always take the siglock to protect reading
1971 * the c* fields from p->signal from races with exit.c updating those
1972 * fields when reaping, so a sample either gets all the additions of a
1973 * given child after it's reaped, or none so this sample is before reaping.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001974 *
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07001975 * Locking:
1976 * We need to take the siglock for CHILDEREN, SELF and BOTH
1977 * for the cases current multithreaded, non-current single threaded
1978 * non-current multithreaded. Thread traversal is now safe with
1979 * the siglock held.
1980 * Strictly speaking, we donot need to take the siglock if we are current and
1981 * single threaded, as no one else can take our signal_struct away, no one
1982 * else can reap the children to update signal->c* counters, and no one else
1983 * can race with the signal-> fields. If we do not take any lock, the
1984 * signal-> fields could be read out of order while another thread was just
1985 * exiting. So we should place a read memory barrier when we avoid the lock.
1986 * On the writer side, write memory barrier is implied in __exit_signal
1987 * as __exit_signal releases the siglock spinlock after updating the signal->
1988 * fields. But we don't do this yet to keep things simple.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08001989 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 */
1991
1992static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
1993{
1994 struct task_struct *t;
1995 unsigned long flags;
1996 cputime_t utime, stime;
1997
1998 memset((char *) r, 0, sizeof *r);
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08001999 utime = stime = cputime_zero;
2000
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07002001 rcu_read_lock();
2002 if (!lock_task_sighand(p, &flags)) {
2003 rcu_read_unlock();
2004 return;
2005 }
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08002006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 switch (who) {
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08002008 case RUSAGE_BOTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 case RUSAGE_CHILDREN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 utime = p->signal->cutime;
2011 stime = p->signal->cstime;
2012 r->ru_nvcsw = p->signal->cnvcsw;
2013 r->ru_nivcsw = p->signal->cnivcsw;
2014 r->ru_minflt = p->signal->cmin_flt;
2015 r->ru_majflt = p->signal->cmaj_flt;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08002016
2017 if (who == RUSAGE_CHILDREN)
2018 break;
2019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 case RUSAGE_SELF:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 utime = cputime_add(utime, p->signal->utime);
2022 stime = cputime_add(stime, p->signal->stime);
2023 r->ru_nvcsw += p->signal->nvcsw;
2024 r->ru_nivcsw += p->signal->nivcsw;
2025 r->ru_minflt += p->signal->min_flt;
2026 r->ru_majflt += p->signal->maj_flt;
2027 t = p;
2028 do {
2029 utime = cputime_add(utime, t->utime);
2030 stime = cputime_add(stime, t->stime);
2031 r->ru_nvcsw += t->nvcsw;
2032 r->ru_nivcsw += t->nivcsw;
2033 r->ru_minflt += t->min_flt;
2034 r->ru_majflt += t->maj_flt;
2035 t = next_thread(t);
2036 } while (t != p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 break;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08002038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 default:
2040 BUG();
2041 }
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08002042
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07002043 unlock_task_sighand(p, &flags);
2044 rcu_read_unlock();
2045
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08002046 cputime_to_timeval(utime, &r->ru_utime);
2047 cputime_to_timeval(stime, &r->ru_stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
2050int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
2051{
2052 struct rusage r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 k_getrusage(p, who, &r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
2055}
2056
2057asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
2058{
2059 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
2060 return -EINVAL;
2061 return getrusage(current, who, ru);
2062}
2063
2064asmlinkage long sys_umask(int mask)
2065{
2066 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
2067 return mask;
2068}
2069
2070asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
2071 unsigned long arg4, unsigned long arg5)
2072{
2073 long error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
2076 if (error)
2077 return error;
2078
2079 switch (option) {
2080 case PR_SET_PDEATHSIG:
Jesper Juhl0730ded2005-09-06 15:17:37 -07002081 if (!valid_signal(arg2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 error = -EINVAL;
2083 break;
2084 }
Jesper Juhl0730ded2005-09-06 15:17:37 -07002085 current->pdeath_signal = arg2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 break;
2087 case PR_GET_PDEATHSIG:
2088 error = put_user(current->pdeath_signal, (int __user *)arg2);
2089 break;
2090 case PR_GET_DUMPABLE:
Michael Kerrisk2030c0f2005-09-16 19:28:02 -07002091 error = current->mm->dumpable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 break;
2093 case PR_SET_DUMPABLE:
Marcel Holtmannabf75a52006-07-12 13:12:00 +02002094 if (arg2 < 0 || arg2 > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 error = -EINVAL;
2096 break;
2097 }
2098 current->mm->dumpable = arg2;
2099 break;
2100
2101 case PR_SET_UNALIGN:
2102 error = SET_UNALIGN_CTL(current, arg2);
2103 break;
2104 case PR_GET_UNALIGN:
2105 error = GET_UNALIGN_CTL(current, arg2);
2106 break;
2107 case PR_SET_FPEMU:
2108 error = SET_FPEMU_CTL(current, arg2);
2109 break;
2110 case PR_GET_FPEMU:
2111 error = GET_FPEMU_CTL(current, arg2);
2112 break;
2113 case PR_SET_FPEXC:
2114 error = SET_FPEXC_CTL(current, arg2);
2115 break;
2116 case PR_GET_FPEXC:
2117 error = GET_FPEXC_CTL(current, arg2);
2118 break;
2119 case PR_GET_TIMING:
2120 error = PR_TIMING_STATISTICAL;
2121 break;
2122 case PR_SET_TIMING:
2123 if (arg2 == PR_TIMING_STATISTICAL)
2124 error = 0;
2125 else
2126 error = -EINVAL;
2127 break;
2128
2129 case PR_GET_KEEPCAPS:
2130 if (current->keep_capabilities)
2131 error = 1;
2132 break;
2133 case PR_SET_KEEPCAPS:
2134 if (arg2 != 0 && arg2 != 1) {
2135 error = -EINVAL;
2136 break;
2137 }
2138 current->keep_capabilities = arg2;
2139 break;
2140 case PR_SET_NAME: {
2141 struct task_struct *me = current;
2142 unsigned char ncomm[sizeof(me->comm)];
2143
2144 ncomm[sizeof(me->comm)-1] = 0;
2145 if (strncpy_from_user(ncomm, (char __user *)arg2,
2146 sizeof(me->comm)-1) < 0)
2147 return -EFAULT;
2148 set_task_comm(me, ncomm);
2149 return 0;
2150 }
2151 case PR_GET_NAME: {
2152 struct task_struct *me = current;
2153 unsigned char tcomm[sizeof(me->comm)];
2154
2155 get_task_comm(tcomm, me);
2156 if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))
2157 return -EFAULT;
2158 return 0;
2159 }
Anton Blanchard651d7652006-06-07 16:10:19 +10002160 case PR_GET_ENDIAN:
2161 error = GET_ENDIAN(current, arg2);
2162 break;
2163 case PR_SET_ENDIAN:
2164 error = SET_ENDIAN(current, arg2);
2165 break;
2166
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 default:
2168 error = -EINVAL;
2169 break;
2170 }
2171 return error;
2172}
Andi Kleen3cfc3482006-09-26 10:52:28 +02002173
2174asmlinkage long sys_getcpu(unsigned __user *cpup, unsigned __user *nodep,
2175 struct getcpu_cache __user *cache)
2176{
2177 int err = 0;
2178 int cpu = raw_smp_processor_id();
2179 if (cpup)
2180 err |= put_user(cpu, cpup);
2181 if (nodep)
2182 err |= put_user(cpu_to_node(cpu), nodep);
2183 if (cache) {
2184 /*
2185 * The cache is not needed for this implementation,
2186 * but make sure user programs pass something
2187 * valid. vsyscall implementations can instead make
2188 * good use of the cache. Only use t0 and t1 because
2189 * these are available in both 32bit and 64bit ABI (no
2190 * need for a compat_getcpu). 32bit has enough
2191 * padding
2192 */
2193 unsigned long t0, t1;
Andi Kleen34596dc2006-09-30 01:47:55 +02002194 get_user(t0, &cache->blob[0]);
2195 get_user(t1, &cache->blob[1]);
Andi Kleen3cfc3482006-09-26 10:52:28 +02002196 t0++;
2197 t1++;
Andi Kleen34596dc2006-09-30 01:47:55 +02002198 put_user(t0, &cache->blob[0]);
2199 put_user(t1, &cache->blob[1]);
Andi Kleen3cfc3482006-09-26 10:52:28 +02002200 }
2201 return err ? -EFAULT : 0;
2202}