blob: ed92e2f033422b8cde3601ada74be31633834ef8 [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>
Daniel Walker3e88c552007-05-10 22:22:53 -070017#include <linux/resource.h>
Eric W. Biedermandc009d92005-06-25 14:57:52 -070018#include <linux/kernel.h>
19#include <linux/kexec.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/workqueue.h>
Randy.Dunlapc59ede72006-01-11 12:17:46 -080021#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/device.h>
23#include <linux/key.h>
24#include <linux/times.h>
25#include <linux/posix-timers.h>
26#include <linux/security.h>
27#include <linux/dcookies.h>
28#include <linux/suspend.h>
29#include <linux/tty.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070030#include <linux/signal.h>
Matt Helsley9f460802005-11-07 00:59:16 -080031#include <linux/cn_proc.h>
Andi Kleen3cfc3482006-09-26 10:52:28 +020032#include <linux/getcpu.h>
Eric Dumazet6eaeeab2007-05-10 22:22:37 -070033#include <linux/task_io_accounting_ops.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include <linux/compat.h>
36#include <linux/syscalls.h>
Keshavamurthy Anil S00d7c052005-12-12 00:37:33 -080037#include <linux/kprobes.h>
Cedric Le Goateracce2922007-07-15 23:40:59 -070038#include <linux/user_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <asm/uaccess.h>
41#include <asm/io.h>
42#include <asm/unistd.h>
43
44#ifndef SET_UNALIGN_CTL
45# define SET_UNALIGN_CTL(a,b) (-EINVAL)
46#endif
47#ifndef GET_UNALIGN_CTL
48# define GET_UNALIGN_CTL(a,b) (-EINVAL)
49#endif
50#ifndef SET_FPEMU_CTL
51# define SET_FPEMU_CTL(a,b) (-EINVAL)
52#endif
53#ifndef GET_FPEMU_CTL
54# define GET_FPEMU_CTL(a,b) (-EINVAL)
55#endif
56#ifndef SET_FPEXC_CTL
57# define SET_FPEXC_CTL(a,b) (-EINVAL)
58#endif
59#ifndef GET_FPEXC_CTL
60# define GET_FPEXC_CTL(a,b) (-EINVAL)
61#endif
Anton Blanchard651d7652006-06-07 16:10:19 +100062#ifndef GET_ENDIAN
63# define GET_ENDIAN(a,b) (-EINVAL)
64#endif
65#ifndef SET_ENDIAN
66# define SET_ENDIAN(a,b) (-EINVAL)
67#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69/*
70 * this is where the system-wide overflow UID and GID are defined, for
71 * architectures that now have 32-bit UID/GID but didn't in the past
72 */
73
74int overflowuid = DEFAULT_OVERFLOWUID;
75int overflowgid = DEFAULT_OVERFLOWGID;
76
77#ifdef CONFIG_UID16
78EXPORT_SYMBOL(overflowuid);
79EXPORT_SYMBOL(overflowgid);
80#endif
81
82/*
83 * the same as above, but for filesystems which can only store a 16-bit
84 * UID and GID. as such, this is needed on all architectures
85 */
86
87int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
88int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
89
90EXPORT_SYMBOL(fs_overflowuid);
91EXPORT_SYMBOL(fs_overflowgid);
92
93/*
94 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
95 */
96
97int C_A_D = 1;
Cedric Le Goater9ec52092006-10-02 02:19:00 -070098struct pid *cad_pid;
99EXPORT_SYMBOL(cad_pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101/*
102 * Notifier list for kernel code which wants to be called
103 * at shutdown. This is used to stop any idling DMA operations
104 * and the like.
105 */
106
Alan Sterne041c682006-03-27 01:16:30 -0800107static BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Alan Sterne041c682006-03-27 01:16:30 -0800109/*
110 * Notifier chain core routines. The exported routines below
111 * are layered on top of these, with appropriate locking added.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 */
Alan Sterne041c682006-03-27 01:16:30 -0800113
114static int notifier_chain_register(struct notifier_block **nl,
115 struct notifier_block *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Alan Sterne041c682006-03-27 01:16:30 -0800117 while ((*nl) != NULL) {
118 if (n->priority > (*nl)->priority)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 break;
Alan Sterne041c682006-03-27 01:16:30 -0800120 nl = &((*nl)->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 }
Alan Sterne041c682006-03-27 01:16:30 -0800122 n->next = *nl;
123 rcu_assign_pointer(*nl, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return 0;
125}
126
Alan Sterne041c682006-03-27 01:16:30 -0800127static int notifier_chain_unregister(struct notifier_block **nl,
128 struct notifier_block *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Alan Sterne041c682006-03-27 01:16:30 -0800130 while ((*nl) != NULL) {
131 if ((*nl) == n) {
132 rcu_assign_pointer(*nl, n->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return 0;
134 }
Alan Sterne041c682006-03-27 01:16:30 -0800135 nl = &((*nl)->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 return -ENOENT;
138}
139
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700140/**
141 * notifier_call_chain - Informs the registered notifiers about an event.
142 * @nl: Pointer to head of the blocking notifier chain
143 * @val: Value passed unmodified to notifier function
144 * @v: Pointer passed unmodified to notifier function
145 * @nr_to_call: Number of notifier functions to be called. Don't care
146 * value of this parameter is -1.
147 * @nr_calls: Records the number of notifications sent. Don't care
148 * value of this field is NULL.
149 * @returns: notifier_call_chain returns the value returned by the
150 * last notifier function called.
151 */
152
Alan Sterne041c682006-03-27 01:16:30 -0800153static int __kprobes notifier_call_chain(struct notifier_block **nl,
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700154 unsigned long val, void *v,
155 int nr_to_call, int *nr_calls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
Alan Sterne041c682006-03-27 01:16:30 -0800157 int ret = NOTIFY_DONE;
Alan Sternbbb17472006-06-25 05:47:15 -0700158 struct notifier_block *nb, *next_nb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Alan Sterne041c682006-03-27 01:16:30 -0800160 nb = rcu_dereference(*nl);
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700161
162 while (nb && nr_to_call) {
Alan Sternbbb17472006-06-25 05:47:15 -0700163 next_nb = rcu_dereference(nb->next);
Alan Sterne041c682006-03-27 01:16:30 -0800164 ret = nb->notifier_call(nb, val, v);
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700165
166 if (nr_calls)
167 (*nr_calls)++;
168
Alan Sterne041c682006-03-27 01:16:30 -0800169 if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
170 break;
Alan Sternbbb17472006-06-25 05:47:15 -0700171 nb = next_nb;
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700172 nr_to_call--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174 return ret;
175}
176
Alan Sterne041c682006-03-27 01:16:30 -0800177/*
178 * Atomic notifier chain routines. Registration and unregistration
Alan Sterneabc0692006-10-04 02:17:04 -0700179 * use a spinlock, and call_chain is synchronized by RCU (no locks).
Alan Sterne041c682006-03-27 01:16:30 -0800180 */
181
182/**
183 * atomic_notifier_chain_register - Add notifier to an atomic notifier chain
184 * @nh: Pointer to head of the atomic notifier chain
185 * @n: New entry in notifier chain
186 *
187 * Adds a notifier to an atomic notifier chain.
188 *
189 * Currently always returns zero.
190 */
191
192int atomic_notifier_chain_register(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_register(&nh->head, n);
200 spin_unlock_irqrestore(&nh->lock, flags);
201 return ret;
202}
203
204EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
205
206/**
207 * atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
208 * @nh: Pointer to head of the atomic notifier chain
209 * @n: Entry to remove from notifier chain
210 *
211 * Removes a notifier from an atomic notifier chain.
212 *
213 * Returns zero on success or %-ENOENT on failure.
214 */
215int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
216 struct notifier_block *n)
217{
218 unsigned long flags;
219 int ret;
220
221 spin_lock_irqsave(&nh->lock, flags);
222 ret = notifier_chain_unregister(&nh->head, n);
223 spin_unlock_irqrestore(&nh->lock, flags);
224 synchronize_rcu();
225 return ret;
226}
227
228EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
229
230/**
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700231 * __atomic_notifier_call_chain - Call functions in an atomic notifier chain
Alan Sterne041c682006-03-27 01:16:30 -0800232 * @nh: Pointer to head of the atomic notifier chain
233 * @val: Value passed unmodified to notifier function
234 * @v: Pointer passed unmodified to notifier function
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700235 * @nr_to_call: See the comment for notifier_call_chain.
236 * @nr_calls: See the comment for notifier_call_chain.
Alan Sterne041c682006-03-27 01:16:30 -0800237 *
238 * Calls each function in a notifier chain in turn. The functions
239 * run in an atomic context, so they must not block.
240 * This routine uses RCU to synchronize with changes to the chain.
241 *
242 * If the return value of the notifier can be and'ed
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800243 * with %NOTIFY_STOP_MASK then atomic_notifier_call_chain()
Alan Sterne041c682006-03-27 01:16:30 -0800244 * will return immediately, with the return value of
245 * the notifier function which halted execution.
246 * Otherwise the return value is the return value
247 * of the last notifier function called.
248 */
249
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700250int __kprobes __atomic_notifier_call_chain(struct atomic_notifier_head *nh,
251 unsigned long val, void *v,
252 int nr_to_call, int *nr_calls)
Alan Sterne041c682006-03-27 01:16:30 -0800253{
254 int ret;
255
256 rcu_read_lock();
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700257 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
Alan Sterne041c682006-03-27 01:16:30 -0800258 rcu_read_unlock();
259 return ret;
260}
261
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700262EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain);
Alan Sterne041c682006-03-27 01:16:30 -0800263
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700264int __kprobes atomic_notifier_call_chain(struct atomic_notifier_head *nh,
265 unsigned long val, void *v)
266{
267 return __atomic_notifier_call_chain(nh, val, v, -1, NULL);
268}
269
270EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
Alan Sterne041c682006-03-27 01:16:30 -0800271/*
272 * Blocking notifier chain routines. All access to the chain is
273 * synchronized by an rwsem.
274 */
275
276/**
277 * blocking_notifier_chain_register - Add notifier to a blocking notifier chain
278 * @nh: Pointer to head of the blocking notifier chain
279 * @n: New entry in notifier chain
280 *
281 * Adds a notifier to a blocking notifier chain.
282 * Must be called in process context.
283 *
284 * Currently always returns zero.
285 */
286
287int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
288 struct notifier_block *n)
289{
290 int ret;
291
292 /*
293 * This code gets used during boot-up, when task switching is
294 * not yet working and interrupts must remain disabled. At
295 * such times we must not call down_write().
296 */
297 if (unlikely(system_state == SYSTEM_BOOTING))
298 return notifier_chain_register(&nh->head, n);
299
300 down_write(&nh->rwsem);
301 ret = notifier_chain_register(&nh->head, n);
302 up_write(&nh->rwsem);
303 return ret;
304}
305
306EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
307
308/**
309 * blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
310 * @nh: Pointer to head of the blocking notifier chain
311 * @n: Entry to remove from notifier chain
312 *
313 * Removes a notifier from a blocking notifier chain.
314 * Must be called from process context.
315 *
316 * Returns zero on success or %-ENOENT on failure.
317 */
318int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
319 struct notifier_block *n)
320{
321 int ret;
322
323 /*
324 * This code gets used during boot-up, when task switching is
325 * not yet working and interrupts must remain disabled. At
326 * such times we must not call down_write().
327 */
328 if (unlikely(system_state == SYSTEM_BOOTING))
329 return notifier_chain_unregister(&nh->head, n);
330
331 down_write(&nh->rwsem);
332 ret = notifier_chain_unregister(&nh->head, n);
333 up_write(&nh->rwsem);
334 return ret;
335}
336
337EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
338
339/**
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700340 * __blocking_notifier_call_chain - Call functions in a blocking notifier chain
Alan Sterne041c682006-03-27 01:16:30 -0800341 * @nh: Pointer to head of the blocking notifier chain
342 * @val: Value passed unmodified to notifier function
343 * @v: Pointer passed unmodified to notifier function
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700344 * @nr_to_call: See comment for notifier_call_chain.
345 * @nr_calls: See comment for notifier_call_chain.
Alan Sterne041c682006-03-27 01:16:30 -0800346 *
347 * Calls each function in a notifier chain in turn. The functions
348 * run in a process context, so they are allowed to block.
349 *
350 * If the return value of the notifier can be and'ed
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800351 * with %NOTIFY_STOP_MASK then blocking_notifier_call_chain()
Alan Sterne041c682006-03-27 01:16:30 -0800352 * will return immediately, with the return value of
353 * the notifier function which halted execution.
354 * Otherwise the return value is the return value
355 * of the last notifier function called.
356 */
357
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700358int __blocking_notifier_call_chain(struct blocking_notifier_head *nh,
359 unsigned long val, void *v,
360 int nr_to_call, int *nr_calls)
Alan Sterne041c682006-03-27 01:16:30 -0800361{
Ingo Molnar1b5180b2007-01-23 10:45:50 +0100362 int ret = NOTIFY_DONE;
Alan Sterne041c682006-03-27 01:16:30 -0800363
Ingo Molnar1b5180b2007-01-23 10:45:50 +0100364 /*
365 * We check the head outside the lock, but if this access is
366 * racy then it does not matter what the result of the test
367 * is, we re-check the list after having taken the lock anyway:
368 */
369 if (rcu_dereference(nh->head)) {
370 down_read(&nh->rwsem);
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700371 ret = notifier_call_chain(&nh->head, val, v, nr_to_call,
372 nr_calls);
Ingo Molnar1b5180b2007-01-23 10:45:50 +0100373 up_read(&nh->rwsem);
374 }
Alan Sterne041c682006-03-27 01:16:30 -0800375 return ret;
376}
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700377EXPORT_SYMBOL_GPL(__blocking_notifier_call_chain);
Alan Sterne041c682006-03-27 01:16:30 -0800378
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700379int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
380 unsigned long val, void *v)
381{
382 return __blocking_notifier_call_chain(nh, val, v, -1, NULL);
383}
Alan Sterne041c682006-03-27 01:16:30 -0800384EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
385
386/*
387 * Raw notifier chain routines. There is no protection;
388 * the caller must provide it. Use at your own risk!
389 */
390
391/**
392 * raw_notifier_chain_register - Add notifier to a raw notifier chain
393 * @nh: Pointer to head of the raw notifier chain
394 * @n: New entry in notifier chain
395 *
396 * Adds a notifier to a raw notifier chain.
397 * All locking must be provided by the caller.
398 *
399 * Currently always returns zero.
400 */
401
402int raw_notifier_chain_register(struct raw_notifier_head *nh,
403 struct notifier_block *n)
404{
405 return notifier_chain_register(&nh->head, n);
406}
407
408EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
409
410/**
411 * raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
412 * @nh: Pointer to head of the raw notifier chain
413 * @n: Entry to remove from notifier chain
414 *
415 * Removes a notifier from a raw notifier chain.
416 * All locking must be provided by the caller.
417 *
418 * Returns zero on success or %-ENOENT on failure.
419 */
420int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
421 struct notifier_block *n)
422{
423 return notifier_chain_unregister(&nh->head, n);
424}
425
426EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
427
428/**
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700429 * __raw_notifier_call_chain - Call functions in a raw notifier chain
Alan Sterne041c682006-03-27 01:16:30 -0800430 * @nh: Pointer to head of the raw notifier chain
431 * @val: Value passed unmodified to notifier function
432 * @v: Pointer passed unmodified to notifier function
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700433 * @nr_to_call: See comment for notifier_call_chain.
434 * @nr_calls: See comment for notifier_call_chain
Alan Sterne041c682006-03-27 01:16:30 -0800435 *
436 * Calls each function in a notifier chain in turn. The functions
437 * run in an undefined context.
438 * All locking must be provided by the caller.
439 *
440 * If the return value of the notifier can be and'ed
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800441 * with %NOTIFY_STOP_MASK then raw_notifier_call_chain()
Alan Sterne041c682006-03-27 01:16:30 -0800442 * will return immediately, with the return value of
443 * the notifier function which halted execution.
444 * Otherwise the return value is the return value
445 * of the last notifier function called.
446 */
447
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700448int __raw_notifier_call_chain(struct raw_notifier_head *nh,
449 unsigned long val, void *v,
450 int nr_to_call, int *nr_calls)
451{
452 return notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
453}
454
455EXPORT_SYMBOL_GPL(__raw_notifier_call_chain);
456
Alan Sterne041c682006-03-27 01:16:30 -0800457int raw_notifier_call_chain(struct raw_notifier_head *nh,
458 unsigned long val, void *v)
459{
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700460 return __raw_notifier_call_chain(nh, val, v, -1, NULL);
Alan Sterne041c682006-03-27 01:16:30 -0800461}
462
463EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Alan Sterneabc0692006-10-04 02:17:04 -0700465/*
466 * SRCU notifier chain routines. Registration and unregistration
467 * use a mutex, and call_chain is synchronized by SRCU (no locks).
468 */
469
470/**
471 * srcu_notifier_chain_register - Add notifier to an SRCU notifier chain
472 * @nh: Pointer to head of the SRCU notifier chain
473 * @n: New entry in notifier chain
474 *
475 * Adds a notifier to an SRCU notifier chain.
476 * Must be called in process context.
477 *
478 * Currently always returns zero.
479 */
480
481int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
482 struct notifier_block *n)
483{
484 int ret;
485
486 /*
487 * This code gets used during boot-up, when task switching is
488 * not yet working and interrupts must remain disabled. At
489 * such times we must not call mutex_lock().
490 */
491 if (unlikely(system_state == SYSTEM_BOOTING))
492 return notifier_chain_register(&nh->head, n);
493
494 mutex_lock(&nh->mutex);
495 ret = notifier_chain_register(&nh->head, n);
496 mutex_unlock(&nh->mutex);
497 return ret;
498}
499
500EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
501
502/**
503 * srcu_notifier_chain_unregister - Remove notifier from an SRCU notifier chain
504 * @nh: Pointer to head of the SRCU notifier chain
505 * @n: Entry to remove from notifier chain
506 *
507 * Removes a notifier from an SRCU notifier chain.
508 * Must be called from process context.
509 *
510 * Returns zero on success or %-ENOENT on failure.
511 */
512int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
513 struct notifier_block *n)
514{
515 int ret;
516
517 /*
518 * This code gets used during boot-up, when task switching is
519 * not yet working and interrupts must remain disabled. At
520 * such times we must not call mutex_lock().
521 */
522 if (unlikely(system_state == SYSTEM_BOOTING))
523 return notifier_chain_unregister(&nh->head, n);
524
525 mutex_lock(&nh->mutex);
526 ret = notifier_chain_unregister(&nh->head, n);
527 mutex_unlock(&nh->mutex);
528 synchronize_srcu(&nh->srcu);
529 return ret;
530}
531
532EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
533
534/**
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700535 * __srcu_notifier_call_chain - Call functions in an SRCU notifier chain
Alan Sterneabc0692006-10-04 02:17:04 -0700536 * @nh: Pointer to head of the SRCU notifier chain
537 * @val: Value passed unmodified to notifier function
538 * @v: Pointer passed unmodified to notifier function
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700539 * @nr_to_call: See comment for notifier_call_chain.
540 * @nr_calls: See comment for notifier_call_chain
Alan Sterneabc0692006-10-04 02:17:04 -0700541 *
542 * Calls each function in a notifier chain in turn. The functions
543 * run in a process context, so they are allowed to block.
544 *
545 * If the return value of the notifier can be and'ed
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800546 * with %NOTIFY_STOP_MASK then srcu_notifier_call_chain()
Alan Sterneabc0692006-10-04 02:17:04 -0700547 * will return immediately, with the return value of
548 * the notifier function which halted execution.
549 * Otherwise the return value is the return value
550 * of the last notifier function called.
551 */
552
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700553int __srcu_notifier_call_chain(struct srcu_notifier_head *nh,
554 unsigned long val, void *v,
555 int nr_to_call, int *nr_calls)
Alan Sterneabc0692006-10-04 02:17:04 -0700556{
557 int ret;
558 int idx;
559
560 idx = srcu_read_lock(&nh->srcu);
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700561 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
Alan Sterneabc0692006-10-04 02:17:04 -0700562 srcu_read_unlock(&nh->srcu, idx);
563 return ret;
564}
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700565EXPORT_SYMBOL_GPL(__srcu_notifier_call_chain);
Alan Sterneabc0692006-10-04 02:17:04 -0700566
Gautham R Shenoy6f7cc112007-05-09 02:34:02 -0700567int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
568 unsigned long val, void *v)
569{
570 return __srcu_notifier_call_chain(nh, val, v, -1, NULL);
571}
Alan Sterneabc0692006-10-04 02:17:04 -0700572EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
573
574/**
575 * srcu_init_notifier_head - Initialize an SRCU notifier head
576 * @nh: Pointer to head of the srcu notifier chain
577 *
578 * Unlike other sorts of notifier heads, SRCU notifier heads require
579 * dynamic initialization. Be sure to call this routine before
580 * calling any of the other SRCU notifier routines for this head.
581 *
582 * If an SRCU notifier head is deallocated, it must first be cleaned
583 * up by calling srcu_cleanup_notifier_head(). Otherwise the head's
584 * per-cpu data (used by the SRCU mechanism) will leak.
585 */
586
587void srcu_init_notifier_head(struct srcu_notifier_head *nh)
588{
589 mutex_init(&nh->mutex);
Alan Sterne6a92012006-10-04 02:17:05 -0700590 if (init_srcu_struct(&nh->srcu) < 0)
591 BUG();
Alan Sterneabc0692006-10-04 02:17:04 -0700592 nh->head = NULL;
593}
594
595EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597/**
598 * register_reboot_notifier - Register function to be called at reboot time
599 * @nb: Info about notifier function to be called
600 *
601 * Registers a function with the list of functions
602 * to be called at reboot time.
603 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800604 * Currently always returns zero, as blocking_notifier_chain_register()
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 * always returns zero.
606 */
607
608int register_reboot_notifier(struct notifier_block * nb)
609{
Alan Sterne041c682006-03-27 01:16:30 -0800610 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
613EXPORT_SYMBOL(register_reboot_notifier);
614
615/**
616 * unregister_reboot_notifier - Unregister previously registered reboot notifier
617 * @nb: Hook to be unregistered
618 *
619 * Unregisters a previously registered reboot
620 * notifier function.
621 *
622 * Returns zero on success, or %-ENOENT on failure.
623 */
624
625int unregister_reboot_notifier(struct notifier_block * nb)
626{
Alan Sterne041c682006-03-27 01:16:30 -0800627 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
630EXPORT_SYMBOL(unregister_reboot_notifier);
631
632static int set_one_prio(struct task_struct *p, int niceval, int error)
633{
634 int no_nice;
635
636 if (p->uid != current->euid &&
637 p->euid != current->euid && !capable(CAP_SYS_NICE)) {
638 error = -EPERM;
639 goto out;
640 }
Matt Mackalle43379f2005-05-01 08:59:00 -0700641 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 error = -EACCES;
643 goto out;
644 }
645 no_nice = security_task_setnice(p, niceval);
646 if (no_nice) {
647 error = no_nice;
648 goto out;
649 }
650 if (error == -ESRCH)
651 error = 0;
652 set_user_nice(p, niceval);
653out:
654 return error;
655}
656
657asmlinkage long sys_setpriority(int which, int who, int niceval)
658{
659 struct task_struct *g, *p;
660 struct user_struct *user;
661 int error = -EINVAL;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800662 struct pid *pgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Daniel Walker3e88c552007-05-10 22:22:53 -0700664 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 goto out;
666
667 /* normalize: avoid signed division (rounding problems) */
668 error = -ESRCH;
669 if (niceval < -20)
670 niceval = -20;
671 if (niceval > 19)
672 niceval = 19;
673
674 read_lock(&tasklist_lock);
675 switch (which) {
676 case PRIO_PROCESS:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800677 if (who)
678 p = find_task_by_pid(who);
679 else
680 p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (p)
682 error = set_one_prio(p, niceval, error);
683 break;
684 case PRIO_PGRP:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800685 if (who)
686 pgrp = find_pid(who);
687 else
688 pgrp = task_pgrp(current);
689 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 error = set_one_prio(p, niceval, error);
Eric W. Biederman41487c62007-02-12 00:53:01 -0800691 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 break;
693 case PRIO_USER:
694 user = current->user;
695 if (!who)
696 who = current->uid;
697 else
698 if ((who != current->uid) && !(user = find_user(who)))
699 goto out_unlock; /* No processes for this user */
700
701 do_each_thread(g, p)
702 if (p->uid == who)
703 error = set_one_prio(p, niceval, error);
704 while_each_thread(g, p);
705 if (who != current->uid)
706 free_uid(user); /* For find_user() */
707 break;
708 }
709out_unlock:
710 read_unlock(&tasklist_lock);
711out:
712 return error;
713}
714
715/*
716 * Ugh. To avoid negative return values, "getpriority()" will
717 * not return the normal nice-value, but a negated value that
718 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
719 * to stay compatible.
720 */
721asmlinkage long sys_getpriority(int which, int who)
722{
723 struct task_struct *g, *p;
724 struct user_struct *user;
725 long niceval, retval = -ESRCH;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800726 struct pid *pgrp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
Daniel Walker3e88c552007-05-10 22:22:53 -0700728 if (which > PRIO_USER || which < PRIO_PROCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 return -EINVAL;
730
731 read_lock(&tasklist_lock);
732 switch (which) {
733 case PRIO_PROCESS:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800734 if (who)
735 p = find_task_by_pid(who);
736 else
737 p = current;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 if (p) {
739 niceval = 20 - task_nice(p);
740 if (niceval > retval)
741 retval = niceval;
742 }
743 break;
744 case PRIO_PGRP:
Eric W. Biederman41487c62007-02-12 00:53:01 -0800745 if (who)
746 pgrp = find_pid(who);
747 else
748 pgrp = task_pgrp(current);
749 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 niceval = 20 - task_nice(p);
751 if (niceval > retval)
752 retval = niceval;
Eric W. Biederman41487c62007-02-12 00:53:01 -0800753 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 break;
755 case PRIO_USER:
756 user = current->user;
757 if (!who)
758 who = current->uid;
759 else
760 if ((who != current->uid) && !(user = find_user(who)))
761 goto out_unlock; /* No processes for this user */
762
763 do_each_thread(g, p)
764 if (p->uid == who) {
765 niceval = 20 - task_nice(p);
766 if (niceval > retval)
767 retval = niceval;
768 }
769 while_each_thread(g, p);
770 if (who != current->uid)
771 free_uid(user); /* for find_user() */
772 break;
773 }
774out_unlock:
775 read_unlock(&tasklist_lock);
776
777 return retval;
778}
779
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700780/**
781 * emergency_restart - reboot the system
782 *
783 * Without shutting down any hardware or taking any locks
784 * reboot the system. This is called when we know we are in
785 * trouble so this is our best effort to reboot. This is
786 * safe to call in interrupt context.
787 */
Eric W. Biederman7c903472005-07-26 11:29:55 -0600788void emergency_restart(void)
789{
790 machine_emergency_restart();
791}
792EXPORT_SYMBOL_GPL(emergency_restart);
793
Adrian Bunk83cc5ed2006-06-25 05:47:41 -0700794static void kernel_restart_prepare(char *cmd)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600795{
Alan Sterne041c682006-03-27 01:16:30 -0800796 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600797 system_state = SYSTEM_RESTART;
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600798 device_shutdown();
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700799}
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800800
801/**
802 * kernel_restart - reboot the system
803 * @cmd: pointer to buffer containing command to execute for restart
Randy Dunlapb8887e62005-11-07 01:01:07 -0800804 * or %NULL
Randy Dunlap1e5d5332005-11-07 01:01:06 -0800805 *
806 * Shutdown everything and perform a clean reboot.
807 * This is not safe to call in interrupt context.
808 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700809void kernel_restart(char *cmd)
810{
811 kernel_restart_prepare(cmd);
Cal Peake756184b2006-09-30 23:27:24 -0700812 if (!cmd)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600813 printk(KERN_EMERG "Restarting system.\n");
Cal Peake756184b2006-09-30 23:27:24 -0700814 else
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600815 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600816 machine_restart(cmd);
817}
818EXPORT_SYMBOL_GPL(kernel_restart);
819
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700820/**
821 * kernel_kexec - reboot the system
822 *
823 * Move into place and start executing a preloaded standalone
824 * executable. If nothing was preloaded return an error.
825 */
Adrian Bunk83cc5ed2006-06-25 05:47:41 -0700826static void kernel_kexec(void)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600827{
828#ifdef CONFIG_KEXEC
829 struct kimage *image;
Al Viro4bb80892006-02-01 05:57:32 -0500830 image = xchg(&kexec_image, NULL);
Cal Peake756184b2006-09-30 23:27:24 -0700831 if (!image)
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600832 return;
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700833 kernel_restart_prepare(NULL);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600834 printk(KERN_EMERG "Starting new kernel\n");
835 machine_shutdown();
836 machine_kexec(image);
837#endif
838}
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600839
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500840void kernel_shutdown_prepare(enum system_states state)
841{
Alan Sterne041c682006-03-27 01:16:30 -0800842 blocking_notifier_call_chain(&reboot_notifier_list,
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500843 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
844 system_state = state;
845 device_shutdown();
846}
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700847/**
848 * kernel_halt - halt the system
849 *
850 * Shutdown everything and perform a clean system halt.
851 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700852void kernel_halt(void)
853{
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500854 kernel_shutdown_prepare(SYSTEM_HALT);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600855 printk(KERN_EMERG "System halted.\n");
856 machine_halt();
857}
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500858
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600859EXPORT_SYMBOL_GPL(kernel_halt);
860
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700861/**
862 * kernel_power_off - power_off the system
863 *
864 * Shutdown everything and perform a clean system power_off.
865 */
Eric W. Biedermane4c94332005-09-22 21:43:45 -0700866void kernel_power_off(void)
867{
Alexey Starikovskiy729b4d42005-12-01 04:29:00 -0500868 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600869 printk(KERN_EMERG "Power down.\n");
870 machine_power_off();
871}
872EXPORT_SYMBOL_GPL(kernel_power_off);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873/*
874 * Reboot system call: for obvious reasons only root may call it,
875 * and even root needs to set up some magic numbers in the registers
876 * so that some mistake won't make this reboot the whole machine.
877 * You can also set the meaning of the ctrl-alt-del-key here.
878 *
879 * reboot doesn't sync: do that yourself before calling this.
880 */
881asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
882{
883 char buffer[256];
884
885 /* We only trust the superuser with rebooting the system. */
886 if (!capable(CAP_SYS_BOOT))
887 return -EPERM;
888
889 /* For safety, we require "magic" arguments. */
890 if (magic1 != LINUX_REBOOT_MAGIC1 ||
891 (magic2 != LINUX_REBOOT_MAGIC2 &&
892 magic2 != LINUX_REBOOT_MAGIC2A &&
893 magic2 != LINUX_REBOOT_MAGIC2B &&
894 magic2 != LINUX_REBOOT_MAGIC2C))
895 return -EINVAL;
896
Eric W. Biederman5e382912006-01-08 01:03:46 -0800897 /* Instead of trying to make the power_off code look like
898 * halt when pm_power_off is not set do it the easy way.
899 */
900 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
901 cmd = LINUX_REBOOT_CMD_HALT;
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 lock_kernel();
904 switch (cmd) {
905 case LINUX_REBOOT_CMD_RESTART:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600906 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 break;
908
909 case LINUX_REBOOT_CMD_CAD_ON:
910 C_A_D = 1;
911 break;
912
913 case LINUX_REBOOT_CMD_CAD_OFF:
914 C_A_D = 0;
915 break;
916
917 case LINUX_REBOOT_CMD_HALT:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600918 kernel_halt();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 unlock_kernel();
920 do_exit(0);
921 break;
922
923 case LINUX_REBOOT_CMD_POWER_OFF:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600924 kernel_power_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 unlock_kernel();
926 do_exit(0);
927 break;
928
929 case LINUX_REBOOT_CMD_RESTART2:
930 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
931 unlock_kernel();
932 return -EFAULT;
933 }
934 buffer[sizeof(buffer) - 1] = '\0';
935
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600936 kernel_restart(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 break;
938
Eric W. Biedermandc009d92005-06-25 14:57:52 -0700939 case LINUX_REBOOT_CMD_KEXEC:
Eric W. Biederman4a00ea12005-07-26 11:24:14 -0600940 kernel_kexec();
941 unlock_kernel();
942 return -EINVAL;
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944#ifdef CONFIG_SOFTWARE_SUSPEND
945 case LINUX_REBOOT_CMD_SW_SUSPEND:
946 {
Rafael J. Wysockia3d25c22007-05-09 02:33:18 -0700947 int ret = hibernate();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 unlock_kernel();
949 return ret;
950 }
951#endif
952
953 default:
954 unlock_kernel();
955 return -EINVAL;
956 }
957 unlock_kernel();
958 return 0;
959}
960
David Howells65f27f32006-11-22 14:55:48 +0000961static void deferred_cad(struct work_struct *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962{
Eric W. Biedermanabcd9e52005-07-26 11:27:34 -0600963 kernel_restart(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964}
965
966/*
967 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
968 * As it's called within an interrupt, it may NOT sync: the only choice
969 * is whether to reboot at once, or just ignore the ctrl-alt-del.
970 */
971void ctrl_alt_del(void)
972{
David Howells65f27f32006-11-22 14:55:48 +0000973 static DECLARE_WORK(cad_work, deferred_cad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 if (C_A_D)
976 schedule_work(&cad_work);
977 else
Cedric Le Goater9ec52092006-10-02 02:19:00 -0700978 kill_cad_pid(SIGINT, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981/*
982 * Unprivileged users may change the real gid to the effective gid
983 * or vice versa. (BSD-style)
984 *
985 * If you set the real gid at all, or set the effective gid to a value not
986 * equal to the real gid, then the saved gid is set to the new effective gid.
987 *
988 * This makes it possible for a setgid program to completely drop its
989 * privileges, which is often a useful assertion to make when you are doing
990 * a security audit over a program.
991 *
992 * The general idea is that a program which uses just setregid() will be
993 * 100% compatible with BSD. A program which uses just setgid() will be
994 * 100% compatible with POSIX with saved IDs.
995 *
996 * SMP: There are not races, the GIDs are checked only by filesystem
997 * operations (as far as semantic preservation is concerned).
998 */
999asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
1000{
1001 int old_rgid = current->gid;
1002 int old_egid = current->egid;
1003 int new_rgid = old_rgid;
1004 int new_egid = old_egid;
1005 int retval;
1006
1007 retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
1008 if (retval)
1009 return retval;
1010
1011 if (rgid != (gid_t) -1) {
1012 if ((old_rgid == rgid) ||
1013 (current->egid==rgid) ||
1014 capable(CAP_SETGID))
1015 new_rgid = rgid;
1016 else
1017 return -EPERM;
1018 }
1019 if (egid != (gid_t) -1) {
1020 if ((old_rgid == egid) ||
1021 (current->egid == egid) ||
1022 (current->sgid == egid) ||
1023 capable(CAP_SETGID))
1024 new_egid = egid;
Cal Peake756184b2006-09-30 23:27:24 -07001025 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 }
Cal Peake756184b2006-09-30 23:27:24 -07001028 if (new_egid != old_egid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001029 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001030 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 }
1032 if (rgid != (gid_t) -1 ||
1033 (egid != (gid_t) -1 && egid != old_rgid))
1034 current->sgid = new_egid;
1035 current->fsgid = new_egid;
1036 current->egid = new_egid;
1037 current->gid = new_rgid;
1038 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001039 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 return 0;
1041}
1042
1043/*
1044 * setgid() is implemented like SysV w/ SAVED_IDS
1045 *
1046 * SMP: Same implicit races as above.
1047 */
1048asmlinkage long sys_setgid(gid_t gid)
1049{
1050 int old_egid = current->egid;
1051 int retval;
1052
1053 retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
1054 if (retval)
1055 return retval;
1056
Cal Peake756184b2006-09-30 23:27:24 -07001057 if (capable(CAP_SETGID)) {
1058 if (old_egid != gid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001059 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001060 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 }
1062 current->gid = current->egid = current->sgid = current->fsgid = gid;
Cal Peake756184b2006-09-30 23:27:24 -07001063 } else if ((gid == current->gid) || (gid == current->sgid)) {
1064 if (old_egid != gid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001065 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001066 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 }
1068 current->egid = current->fsgid = gid;
1069 }
1070 else
1071 return -EPERM;
1072
1073 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001074 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 return 0;
1076}
1077
1078static int set_user(uid_t new_ruid, int dumpclear)
1079{
1080 struct user_struct *new_user;
1081
Cedric Le Goateracce2922007-07-15 23:40:59 -07001082 new_user = alloc_uid(current->nsproxy->user_ns, new_ruid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 if (!new_user)
1084 return -EAGAIN;
1085
1086 if (atomic_read(&new_user->processes) >=
1087 current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
Cedric Le Goateracce2922007-07-15 23:40:59 -07001088 new_user != current->nsproxy->user_ns->root_user) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 free_uid(new_user);
1090 return -EAGAIN;
1091 }
1092
1093 switch_uid(new_user);
1094
Cal Peake756184b2006-09-30 23:27:24 -07001095 if (dumpclear) {
Alan Coxd6e71142005-06-23 00:09:43 -07001096 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001097 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 }
1099 current->uid = new_ruid;
1100 return 0;
1101}
1102
1103/*
1104 * Unprivileged users may change the real uid to the effective uid
1105 * or vice versa. (BSD-style)
1106 *
1107 * If you set the real uid at all, or set the effective uid to a value not
1108 * equal to the real uid, then the saved uid is set to the new effective uid.
1109 *
1110 * This makes it possible for a setuid program to completely drop its
1111 * privileges, which is often a useful assertion to make when you are doing
1112 * a security audit over a program.
1113 *
1114 * The general idea is that a program which uses just setreuid() will be
1115 * 100% compatible with BSD. A program which uses just setuid() will be
1116 * 100% compatible with POSIX with saved IDs.
1117 */
1118asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
1119{
1120 int old_ruid, old_euid, old_suid, new_ruid, new_euid;
1121 int retval;
1122
1123 retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
1124 if (retval)
1125 return retval;
1126
1127 new_ruid = old_ruid = current->uid;
1128 new_euid = old_euid = current->euid;
1129 old_suid = current->suid;
1130
1131 if (ruid != (uid_t) -1) {
1132 new_ruid = ruid;
1133 if ((old_ruid != ruid) &&
1134 (current->euid != ruid) &&
1135 !capable(CAP_SETUID))
1136 return -EPERM;
1137 }
1138
1139 if (euid != (uid_t) -1) {
1140 new_euid = euid;
1141 if ((old_ruid != euid) &&
1142 (current->euid != euid) &&
1143 (current->suid != euid) &&
1144 !capable(CAP_SETUID))
1145 return -EPERM;
1146 }
1147
1148 if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
1149 return -EAGAIN;
1150
Cal Peake756184b2006-09-30 23:27:24 -07001151 if (new_euid != old_euid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001152 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001153 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
1155 current->fsuid = current->euid = new_euid;
1156 if (ruid != (uid_t) -1 ||
1157 (euid != (uid_t) -1 && euid != old_ruid))
1158 current->suid = current->euid;
1159 current->fsuid = current->euid;
1160
1161 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001162 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
1165}
1166
1167
1168
1169/*
1170 * setuid() is implemented like SysV with SAVED_IDS
1171 *
1172 * Note that SAVED_ID's is deficient in that a setuid root program
1173 * like sendmail, for example, cannot set its uid to be a normal
1174 * user and then switch back, because if you're root, setuid() sets
1175 * the saved uid too. If you don't like this, blame the bright people
1176 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
1177 * will allow a root program to temporarily drop privileges and be able to
1178 * regain them by swapping the real and effective uid.
1179 */
1180asmlinkage long sys_setuid(uid_t uid)
1181{
1182 int old_euid = current->euid;
David Rientjesa09c17a2006-12-06 20:40:18 -08001183 int old_ruid, old_suid, new_suid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 int retval;
1185
1186 retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
1187 if (retval)
1188 return retval;
1189
David Rientjesa09c17a2006-12-06 20:40:18 -08001190 old_ruid = current->uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 old_suid = current->suid;
1192 new_suid = old_suid;
1193
1194 if (capable(CAP_SETUID)) {
1195 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
1196 return -EAGAIN;
1197 new_suid = uid;
1198 } else if ((uid != current->uid) && (uid != new_suid))
1199 return -EPERM;
1200
Cal Peake756184b2006-09-30 23:27:24 -07001201 if (old_euid != uid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001202 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001203 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 }
1205 current->fsuid = current->euid = uid;
1206 current->suid = new_suid;
1207
1208 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001209 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
1212}
1213
1214
1215/*
1216 * This function implements a generic ability to update ruid, euid,
1217 * and suid. This allows you to implement the 4.4 compatible seteuid().
1218 */
1219asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
1220{
1221 int old_ruid = current->uid;
1222 int old_euid = current->euid;
1223 int old_suid = current->suid;
1224 int retval;
1225
1226 retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
1227 if (retval)
1228 return retval;
1229
1230 if (!capable(CAP_SETUID)) {
1231 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
1232 (ruid != current->euid) && (ruid != current->suid))
1233 return -EPERM;
1234 if ((euid != (uid_t) -1) && (euid != current->uid) &&
1235 (euid != current->euid) && (euid != current->suid))
1236 return -EPERM;
1237 if ((suid != (uid_t) -1) && (suid != current->uid) &&
1238 (suid != current->euid) && (suid != current->suid))
1239 return -EPERM;
1240 }
1241 if (ruid != (uid_t) -1) {
1242 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
1243 return -EAGAIN;
1244 }
1245 if (euid != (uid_t) -1) {
Cal Peake756184b2006-09-30 23:27:24 -07001246 if (euid != current->euid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001247 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001248 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
1250 current->euid = euid;
1251 }
1252 current->fsuid = current->euid;
1253 if (suid != (uid_t) -1)
1254 current->suid = suid;
1255
1256 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001257 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
1260}
1261
1262asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
1263{
1264 int retval;
1265
1266 if (!(retval = put_user(current->uid, ruid)) &&
1267 !(retval = put_user(current->euid, euid)))
1268 retval = put_user(current->suid, suid);
1269
1270 return retval;
1271}
1272
1273/*
1274 * Same as above, but for rgid, egid, sgid.
1275 */
1276asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
1277{
1278 int retval;
1279
1280 retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
1281 if (retval)
1282 return retval;
1283
1284 if (!capable(CAP_SETGID)) {
1285 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
1286 (rgid != current->egid) && (rgid != current->sgid))
1287 return -EPERM;
1288 if ((egid != (gid_t) -1) && (egid != current->gid) &&
1289 (egid != current->egid) && (egid != current->sgid))
1290 return -EPERM;
1291 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
1292 (sgid != current->egid) && (sgid != current->sgid))
1293 return -EPERM;
1294 }
1295 if (egid != (gid_t) -1) {
Cal Peake756184b2006-09-30 23:27:24 -07001296 if (egid != current->egid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001297 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001298 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
1300 current->egid = egid;
1301 }
1302 current->fsgid = current->egid;
1303 if (rgid != (gid_t) -1)
1304 current->gid = rgid;
1305 if (sgid != (gid_t) -1)
1306 current->sgid = sgid;
1307
1308 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001309 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return 0;
1311}
1312
1313asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
1314{
1315 int retval;
1316
1317 if (!(retval = put_user(current->gid, rgid)) &&
1318 !(retval = put_user(current->egid, egid)))
1319 retval = put_user(current->sgid, sgid);
1320
1321 return retval;
1322}
1323
1324
1325/*
1326 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
1327 * is used for "access()" and for the NFS daemon (letting nfsd stay at
1328 * whatever uid it wants to). It normally shadows "euid", except when
1329 * explicitly set by setfsuid() or for access..
1330 */
1331asmlinkage long sys_setfsuid(uid_t uid)
1332{
1333 int old_fsuid;
1334
1335 old_fsuid = current->fsuid;
1336 if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
1337 return old_fsuid;
1338
1339 if (uid == current->uid || uid == current->euid ||
1340 uid == current->suid || uid == current->fsuid ||
Cal Peake756184b2006-09-30 23:27:24 -07001341 capable(CAP_SETUID)) {
1342 if (uid != old_fsuid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001343 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001344 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346 current->fsuid = uid;
1347 }
1348
1349 key_fsuid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001350 proc_id_connector(current, PROC_EVENT_UID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
1353
1354 return old_fsuid;
1355}
1356
1357/*
John Anthony Kazos Jrf42df9e2007-05-09 08:23:08 +02001358 * Samma på svenska..
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 */
1360asmlinkage long sys_setfsgid(gid_t gid)
1361{
1362 int old_fsgid;
1363
1364 old_fsgid = current->fsgid;
1365 if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
1366 return old_fsgid;
1367
1368 if (gid == current->gid || gid == current->egid ||
1369 gid == current->sgid || gid == current->fsgid ||
Cal Peake756184b2006-09-30 23:27:24 -07001370 capable(CAP_SETGID)) {
1371 if (gid != old_fsgid) {
Alan Coxd6e71142005-06-23 00:09:43 -07001372 current->mm->dumpable = suid_dumpable;
akpm@osdl.orgd59dd462005-05-01 08:58:47 -07001373 smp_wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
1375 current->fsgid = gid;
1376 key_fsgid_changed(current);
Matt Helsley9f460802005-11-07 00:59:16 -08001377 proc_id_connector(current, PROC_EVENT_GID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 }
1379 return old_fsgid;
1380}
1381
1382asmlinkage long sys_times(struct tms __user * tbuf)
1383{
1384 /*
1385 * In the SMP world we might just be unlucky and have one of
1386 * the times increment as we use it. Since the value is an
1387 * atomically safe type this is just fine. Conceptually its
1388 * as if the syscall took an instant longer to occur.
1389 */
1390 if (tbuf) {
1391 struct tms tmp;
Oleg Nesterov35f5cad2006-03-28 16:11:19 -08001392 struct task_struct *tsk = current;
1393 struct task_struct *t;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 cputime_t utime, stime, cutime, cstime;
1395
Oleg Nesterov7d7185c2006-03-28 16:11:21 -08001396 spin_lock_irq(&tsk->sighand->siglock);
Oleg Nesterov35f5cad2006-03-28 16:11:19 -08001397 utime = tsk->signal->utime;
1398 stime = tsk->signal->stime;
1399 t = tsk;
1400 do {
1401 utime = cputime_add(utime, t->utime);
1402 stime = cputime_add(stime, t->stime);
1403 t = next_thread(t);
1404 } while (t != tsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Oleg Nesterov35f5cad2006-03-28 16:11:19 -08001406 cutime = tsk->signal->cutime;
1407 cstime = tsk->signal->cstime;
1408 spin_unlock_irq(&tsk->sighand->siglock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 tmp.tms_utime = cputime_to_clock_t(utime);
1411 tmp.tms_stime = cputime_to_clock_t(stime);
1412 tmp.tms_cutime = cputime_to_clock_t(cutime);
1413 tmp.tms_cstime = cputime_to_clock_t(cstime);
1414 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1415 return -EFAULT;
1416 }
1417 return (long) jiffies_64_to_clock_t(get_jiffies_64());
1418}
1419
1420/*
1421 * This needs some heavy checking ...
1422 * I just haven't the stomach for it. I also don't fully
1423 * understand sessions/pgrp etc. Let somebody who does explain it.
1424 *
1425 * OK, I think I have the protection semantics right.... this is really
1426 * only important on a multi-user system anyway, to make sure one user
1427 * can't send a signal to a process owned by another. -TYT, 12/12/91
1428 *
1429 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1430 * LBT 04.03.94
1431 */
1432
1433asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
1434{
1435 struct task_struct *p;
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001436 struct task_struct *group_leader = current->group_leader;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 int err = -EINVAL;
1438
1439 if (!pid)
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001440 pid = group_leader->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 if (!pgid)
1442 pgid = pid;
1443 if (pgid < 0)
1444 return -EINVAL;
1445
1446 /* From this point forward we keep holding onto the tasklist lock
1447 * so that our parent does not change from under us. -DaveM
1448 */
1449 write_lock_irq(&tasklist_lock);
1450
1451 err = -ESRCH;
1452 p = find_task_by_pid(pid);
1453 if (!p)
1454 goto out;
1455
1456 err = -EINVAL;
1457 if (!thread_group_leader(p))
1458 goto out;
1459
Oleg Nesterovf7dd7952006-01-08 01:03:59 -08001460 if (p->real_parent == group_leader) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 err = -EPERM;
Eric W. Biederman41487c62007-02-12 00:53:01 -08001462 if (task_session(p) != task_session(group_leader))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 goto out;
1464 err = -EACCES;
1465 if (p->did_exec)
1466 goto out;
1467 } else {
1468 err = -ESRCH;
Oleg Nesterovee0acf92006-01-08 01:03:53 -08001469 if (p != group_leader)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 goto out;
1471 }
1472
1473 err = -EPERM;
1474 if (p->signal->leader)
1475 goto out;
1476
1477 if (pgid != pid) {
Oleg Nesterovf020bc42006-12-08 02:38:02 -08001478 struct task_struct *g =
1479 find_task_by_pid_type(PIDTYPE_PGID, pgid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
Eric W. Biederman41487c62007-02-12 00:53:01 -08001481 if (!g || task_session(g) != task_session(group_leader))
Oleg Nesterovf020bc42006-12-08 02:38:02 -08001482 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 }
1484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 err = security_task_setpgid(p, pgid);
1486 if (err)
1487 goto out;
1488
1489 if (process_group(p) != pgid) {
1490 detach_pid(p, PIDTYPE_PGID);
1491 p->signal->pgrp = pgid;
Sukadev Bhattiprolue713d0d2007-05-10 22:22:58 -07001492 attach_pid(p, PIDTYPE_PGID, find_pid(pgid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
1494
1495 err = 0;
1496out:
1497 /* All paths lead to here, thus we are safe. -DaveM */
1498 write_unlock_irq(&tasklist_lock);
1499 return err;
1500}
1501
1502asmlinkage long sys_getpgid(pid_t pid)
1503{
Cal Peake756184b2006-09-30 23:27:24 -07001504 if (!pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return process_group(current);
Cal Peake756184b2006-09-30 23:27:24 -07001506 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 int retval;
1508 struct task_struct *p;
1509
1510 read_lock(&tasklist_lock);
1511 p = find_task_by_pid(pid);
1512
1513 retval = -ESRCH;
1514 if (p) {
1515 retval = security_task_getpgid(p);
1516 if (!retval)
1517 retval = process_group(p);
1518 }
1519 read_unlock(&tasklist_lock);
1520 return retval;
1521 }
1522}
1523
1524#ifdef __ARCH_WANT_SYS_GETPGRP
1525
1526asmlinkage long sys_getpgrp(void)
1527{
1528 /* SMP - assuming writes are word atomic this is fine */
1529 return process_group(current);
1530}
1531
1532#endif
1533
1534asmlinkage long sys_getsid(pid_t pid)
1535{
Cal Peake756184b2006-09-30 23:27:24 -07001536 if (!pid)
Cedric Le Goater937949d2006-12-08 02:37:54 -08001537 return process_session(current);
Cal Peake756184b2006-09-30 23:27:24 -07001538 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 int retval;
1540 struct task_struct *p;
1541
1542 read_lock(&tasklist_lock);
1543 p = find_task_by_pid(pid);
1544
1545 retval = -ESRCH;
Cal Peake756184b2006-09-30 23:27:24 -07001546 if (p) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 retval = security_task_getsid(p);
1548 if (!retval)
Cedric Le Goater937949d2006-12-08 02:37:54 -08001549 retval = process_session(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
1551 read_unlock(&tasklist_lock);
1552 return retval;
1553 }
1554}
1555
1556asmlinkage long sys_setsid(void)
1557{
Oren Laadane19f2472006-01-08 01:03:58 -08001558 struct task_struct *group_leader = current->group_leader;
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001559 pid_t session;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 int err = -EPERM;
1561
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 write_lock_irq(&tasklist_lock);
1563
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001564 /* Fail if I am already a session leader */
1565 if (group_leader->signal->leader)
1566 goto out;
1567
1568 session = group_leader->pid;
1569 /* Fail if a process group id already exists that equals the
1570 * proposed session id.
1571 *
1572 * Don't check if session id == 1 because kernel threads use this
1573 * session id and so the check will always fail and make it so
1574 * init cannot successfully call setsid.
1575 */
1576 if (session > 1 && find_task_by_pid_type(PIDTYPE_PGID, session))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 goto out;
1578
Oren Laadane19f2472006-01-08 01:03:58 -08001579 group_leader->signal->leader = 1;
Eric W. Biederman390e2ff2006-03-31 02:31:33 -08001580 __set_special_pids(session, session);
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001581
1582 spin_lock(&group_leader->sighand->siglock);
Oren Laadane19f2472006-01-08 01:03:58 -08001583 group_leader->signal->tty = NULL;
Peter Zijlstra24ec8392006-12-08 02:36:04 -08001584 spin_unlock(&group_leader->sighand->siglock);
1585
Oren Laadane19f2472006-01-08 01:03:58 -08001586 err = process_group(group_leader);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587out:
1588 write_unlock_irq(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 return err;
1590}
1591
1592/*
1593 * Supplementary group IDs
1594 */
1595
1596/* init to 2 - one for init_task, one to ensure it is never freed */
1597struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
1598
1599struct group_info *groups_alloc(int gidsetsize)
1600{
1601 struct group_info *group_info;
1602 int nblocks;
1603 int i;
1604
1605 nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
1606 /* Make sure we always allocate at least one indirect block pointer */
1607 nblocks = nblocks ? : 1;
1608 group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
1609 if (!group_info)
1610 return NULL;
1611 group_info->ngroups = gidsetsize;
1612 group_info->nblocks = nblocks;
1613 atomic_set(&group_info->usage, 1);
1614
Cal Peake756184b2006-09-30 23:27:24 -07001615 if (gidsetsize <= NGROUPS_SMALL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 group_info->blocks[0] = group_info->small_block;
Cal Peake756184b2006-09-30 23:27:24 -07001617 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 for (i = 0; i < nblocks; i++) {
1619 gid_t *b;
1620 b = (void *)__get_free_page(GFP_USER);
1621 if (!b)
1622 goto out_undo_partial_alloc;
1623 group_info->blocks[i] = b;
1624 }
1625 }
1626 return group_info;
1627
1628out_undo_partial_alloc:
1629 while (--i >= 0) {
1630 free_page((unsigned long)group_info->blocks[i]);
1631 }
1632 kfree(group_info);
1633 return NULL;
1634}
1635
1636EXPORT_SYMBOL(groups_alloc);
1637
1638void groups_free(struct group_info *group_info)
1639{
1640 if (group_info->blocks[0] != group_info->small_block) {
1641 int i;
1642 for (i = 0; i < group_info->nblocks; i++)
1643 free_page((unsigned long)group_info->blocks[i]);
1644 }
1645 kfree(group_info);
1646}
1647
1648EXPORT_SYMBOL(groups_free);
1649
1650/* export the group_info to a user-space array */
1651static int groups_to_user(gid_t __user *grouplist,
1652 struct group_info *group_info)
1653{
1654 int i;
1655 int count = group_info->ngroups;
1656
1657 for (i = 0; i < group_info->nblocks; i++) {
1658 int cp_count = min(NGROUPS_PER_BLOCK, count);
1659 int off = i * NGROUPS_PER_BLOCK;
1660 int len = cp_count * sizeof(*grouplist);
1661
1662 if (copy_to_user(grouplist+off, group_info->blocks[i], len))
1663 return -EFAULT;
1664
1665 count -= cp_count;
1666 }
1667 return 0;
1668}
1669
1670/* fill a group_info from a user-space array - it must be allocated already */
1671static int groups_from_user(struct group_info *group_info,
1672 gid_t __user *grouplist)
Cal Peake756184b2006-09-30 23:27:24 -07001673{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 int i;
1675 int count = group_info->ngroups;
1676
1677 for (i = 0; i < group_info->nblocks; i++) {
1678 int cp_count = min(NGROUPS_PER_BLOCK, count);
1679 int off = i * NGROUPS_PER_BLOCK;
1680 int len = cp_count * sizeof(*grouplist);
1681
1682 if (copy_from_user(group_info->blocks[i], grouplist+off, len))
1683 return -EFAULT;
1684
1685 count -= cp_count;
1686 }
1687 return 0;
1688}
1689
Domen Puncerebe8b542005-05-05 16:16:19 -07001690/* a simple Shell sort */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691static void groups_sort(struct group_info *group_info)
1692{
1693 int base, max, stride;
1694 int gidsetsize = group_info->ngroups;
1695
1696 for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
1697 ; /* nothing */
1698 stride /= 3;
1699
1700 while (stride) {
1701 max = gidsetsize - stride;
1702 for (base = 0; base < max; base++) {
1703 int left = base;
1704 int right = left + stride;
1705 gid_t tmp = GROUP_AT(group_info, right);
1706
1707 while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
1708 GROUP_AT(group_info, right) =
1709 GROUP_AT(group_info, left);
1710 right = left;
1711 left -= stride;
1712 }
1713 GROUP_AT(group_info, right) = tmp;
1714 }
1715 stride /= 3;
1716 }
1717}
1718
1719/* a simple bsearch */
David Howells3e301482005-06-23 22:00:56 -07001720int groups_search(struct group_info *group_info, gid_t grp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721{
Eric Dumazetd74beb9f2006-03-25 03:08:19 -08001722 unsigned int left, right;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 if (!group_info)
1725 return 0;
1726
1727 left = 0;
1728 right = group_info->ngroups;
1729 while (left < right) {
Eric Dumazetd74beb9f2006-03-25 03:08:19 -08001730 unsigned int mid = (left+right)/2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 int cmp = grp - GROUP_AT(group_info, mid);
1732 if (cmp > 0)
1733 left = mid + 1;
1734 else if (cmp < 0)
1735 right = mid;
1736 else
1737 return 1;
1738 }
1739 return 0;
1740}
1741
1742/* validate and set current->group_info */
1743int set_current_groups(struct group_info *group_info)
1744{
1745 int retval;
1746 struct group_info *old_info;
1747
1748 retval = security_task_setgroups(group_info);
1749 if (retval)
1750 return retval;
1751
1752 groups_sort(group_info);
1753 get_group_info(group_info);
1754
1755 task_lock(current);
1756 old_info = current->group_info;
1757 current->group_info = group_info;
1758 task_unlock(current);
1759
1760 put_group_info(old_info);
1761
1762 return 0;
1763}
1764
1765EXPORT_SYMBOL(set_current_groups);
1766
1767asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1768{
1769 int i = 0;
1770
1771 /*
1772 * SMP: Nobody else can change our grouplist. Thus we are
1773 * safe.
1774 */
1775
1776 if (gidsetsize < 0)
1777 return -EINVAL;
1778
1779 /* no need to grab task_lock here; it cannot change */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 i = current->group_info->ngroups;
1781 if (gidsetsize) {
1782 if (i > gidsetsize) {
1783 i = -EINVAL;
1784 goto out;
1785 }
1786 if (groups_to_user(grouplist, current->group_info)) {
1787 i = -EFAULT;
1788 goto out;
1789 }
1790 }
1791out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 return i;
1793}
1794
1795/*
1796 * SMP: Our groups are copy-on-write. We can set them safely
1797 * without another task interfering.
1798 */
1799
1800asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
1801{
1802 struct group_info *group_info;
1803 int retval;
1804
1805 if (!capable(CAP_SETGID))
1806 return -EPERM;
1807 if ((unsigned)gidsetsize > NGROUPS_MAX)
1808 return -EINVAL;
1809
1810 group_info = groups_alloc(gidsetsize);
1811 if (!group_info)
1812 return -ENOMEM;
1813 retval = groups_from_user(group_info, grouplist);
1814 if (retval) {
1815 put_group_info(group_info);
1816 return retval;
1817 }
1818
1819 retval = set_current_groups(group_info);
1820 put_group_info(group_info);
1821
1822 return retval;
1823}
1824
1825/*
1826 * Check whether we're fsgid/egid or in the supplemental group..
1827 */
1828int in_group_p(gid_t grp)
1829{
1830 int retval = 1;
Cal Peake756184b2006-09-30 23:27:24 -07001831 if (grp != current->fsgid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 retval = groups_search(current->group_info, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return retval;
1834}
1835
1836EXPORT_SYMBOL(in_group_p);
1837
1838int in_egroup_p(gid_t grp)
1839{
1840 int retval = 1;
Cal Peake756184b2006-09-30 23:27:24 -07001841 if (grp != current->egid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 retval = groups_search(current->group_info, grp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 return retval;
1844}
1845
1846EXPORT_SYMBOL(in_egroup_p);
1847
1848DECLARE_RWSEM(uts_sem);
1849
David S. Miller393b0722005-11-10 12:47:50 -08001850EXPORT_SYMBOL(uts_sem);
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852asmlinkage long sys_newuname(struct new_utsname __user * name)
1853{
1854 int errno = 0;
1855
1856 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001857 if (copy_to_user(name, utsname(), sizeof *name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 errno = -EFAULT;
1859 up_read(&uts_sem);
1860 return errno;
1861}
1862
1863asmlinkage long sys_sethostname(char __user *name, int len)
1864{
1865 int errno;
1866 char tmp[__NEW_UTS_LEN];
1867
1868 if (!capable(CAP_SYS_ADMIN))
1869 return -EPERM;
1870 if (len < 0 || len > __NEW_UTS_LEN)
1871 return -EINVAL;
1872 down_write(&uts_sem);
1873 errno = -EFAULT;
1874 if (!copy_from_user(tmp, name, len)) {
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001875 memcpy(utsname()->nodename, tmp, len);
1876 utsname()->nodename[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 errno = 0;
1878 }
1879 up_write(&uts_sem);
1880 return errno;
1881}
1882
1883#ifdef __ARCH_WANT_SYS_GETHOSTNAME
1884
1885asmlinkage long sys_gethostname(char __user *name, int len)
1886{
1887 int i, errno;
1888
1889 if (len < 0)
1890 return -EINVAL;
1891 down_read(&uts_sem);
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001892 i = 1 + strlen(utsname()->nodename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (i > len)
1894 i = len;
1895 errno = 0;
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001896 if (copy_to_user(name, utsname()->nodename, i))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 errno = -EFAULT;
1898 up_read(&uts_sem);
1899 return errno;
1900}
1901
1902#endif
1903
1904/*
1905 * Only setdomainname; getdomainname can be implemented by calling
1906 * uname()
1907 */
1908asmlinkage long sys_setdomainname(char __user *name, int len)
1909{
1910 int errno;
1911 char tmp[__NEW_UTS_LEN];
1912
1913 if (!capable(CAP_SYS_ADMIN))
1914 return -EPERM;
1915 if (len < 0 || len > __NEW_UTS_LEN)
1916 return -EINVAL;
1917
1918 down_write(&uts_sem);
1919 errno = -EFAULT;
1920 if (!copy_from_user(tmp, name, len)) {
Serge E. Hallyne9ff3992006-10-02 02:18:11 -07001921 memcpy(utsname()->domainname, tmp, len);
1922 utsname()->domainname[len] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 errno = 0;
1924 }
1925 up_write(&uts_sem);
1926 return errno;
1927}
1928
1929asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1930{
1931 if (resource >= RLIM_NLIMITS)
1932 return -EINVAL;
1933 else {
1934 struct rlimit value;
1935 task_lock(current->group_leader);
1936 value = current->signal->rlim[resource];
1937 task_unlock(current->group_leader);
1938 return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1939 }
1940}
1941
1942#ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1943
1944/*
1945 * Back compatibility for getrlimit. Needed for some apps.
1946 */
1947
1948asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1949{
1950 struct rlimit x;
1951 if (resource >= RLIM_NLIMITS)
1952 return -EINVAL;
1953
1954 task_lock(current->group_leader);
1955 x = current->signal->rlim[resource];
1956 task_unlock(current->group_leader);
Cal Peake756184b2006-09-30 23:27:24 -07001957 if (x.rlim_cur > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 x.rlim_cur = 0x7FFFFFFF;
Cal Peake756184b2006-09-30 23:27:24 -07001959 if (x.rlim_max > 0x7FFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 x.rlim_max = 0x7FFFFFFF;
1961 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1962}
1963
1964#endif
1965
1966asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
1967{
1968 struct rlimit new_rlim, *old_rlim;
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001969 unsigned long it_prof_secs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 int retval;
1971
1972 if (resource >= RLIM_NLIMITS)
1973 return -EINVAL;
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001974 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 return -EFAULT;
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001976 if (new_rlim.rlim_cur > new_rlim.rlim_max)
1977 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 old_rlim = current->signal->rlim + resource;
1979 if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
1980 !capable(CAP_SYS_RESOURCE))
1981 return -EPERM;
1982 if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
Andrew Mortonec9e16b2006-03-24 03:18:34 -08001983 return -EPERM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985 retval = security_task_setrlimit(resource, &new_rlim);
1986 if (retval)
1987 return retval;
1988
Tom Alsberg9926e4c2007-05-08 00:30:31 -07001989 if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
1990 /*
1991 * The caller is asking for an immediate RLIMIT_CPU
1992 * expiry. But we use the zero value to mean "it was
1993 * never set". So let's cheat and make it one second
1994 * instead
1995 */
1996 new_rlim.rlim_cur = 1;
1997 }
1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 task_lock(current->group_leader);
2000 *old_rlim = new_rlim;
2001 task_unlock(current->group_leader);
2002
Andrew Mortonec9e16b2006-03-24 03:18:34 -08002003 if (resource != RLIMIT_CPU)
2004 goto out;
Andrew Mortond3561f72006-03-24 03:18:36 -08002005
2006 /*
2007 * RLIMIT_CPU handling. Note that the kernel fails to return an error
2008 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
2009 * very long-standing error, and fixing it now risks breakage of
2010 * applications, so we live with it
2011 */
Andrew Mortonec9e16b2006-03-24 03:18:34 -08002012 if (new_rlim.rlim_cur == RLIM_INFINITY)
2013 goto out;
2014
2015 it_prof_secs = cputime_to_secs(current->signal->it_prof_expires);
2016 if (it_prof_secs == 0 || new_rlim.rlim_cur <= it_prof_secs) {
Andrew Mortone0661112006-03-24 03:18:35 -08002017 unsigned long rlim_cur = new_rlim.rlim_cur;
2018 cputime_t cputime;
Andrew Mortonec9e16b2006-03-24 03:18:34 -08002019
Andrew Mortone0661112006-03-24 03:18:35 -08002020 cputime = secs_to_cputime(rlim_cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 read_lock(&tasklist_lock);
2022 spin_lock_irq(&current->sighand->siglock);
Andrew Mortonec9e16b2006-03-24 03:18:34 -08002023 set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 spin_unlock_irq(&current->sighand->siglock);
2025 read_unlock(&tasklist_lock);
2026 }
Andrew Mortonec9e16b2006-03-24 03:18:34 -08002027out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 return 0;
2029}
2030
2031/*
2032 * It would make sense to put struct rusage in the task_struct,
2033 * except that would make the task_struct be *really big*. After
2034 * task_struct gets moved into malloc'ed memory, it would
2035 * make sense to do this. It will make moving the rest of the information
2036 * a lot simpler! (Which we're not doing right now because we're not
2037 * measuring them yet).
2038 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
2040 * races with threads incrementing their own counters. But since word
2041 * reads are atomic, we either get new values or old values and we don't
2042 * care which for the sums. We always take the siglock to protect reading
2043 * the c* fields from p->signal from races with exit.c updating those
2044 * fields when reaping, so a sample either gets all the additions of a
2045 * given child after it's reaped, or none so this sample is before reaping.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08002046 *
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07002047 * Locking:
2048 * We need to take the siglock for CHILDEREN, SELF and BOTH
2049 * for the cases current multithreaded, non-current single threaded
2050 * non-current multithreaded. Thread traversal is now safe with
2051 * the siglock held.
2052 * Strictly speaking, we donot need to take the siglock if we are current and
2053 * single threaded, as no one else can take our signal_struct away, no one
2054 * else can reap the children to update signal->c* counters, and no one else
2055 * can race with the signal-> fields. If we do not take any lock, the
2056 * signal-> fields could be read out of order while another thread was just
2057 * exiting. So we should place a read memory barrier when we avoid the lock.
2058 * On the writer side, write memory barrier is implied in __exit_signal
2059 * as __exit_signal releases the siglock spinlock after updating the signal->
2060 * fields. But we don't do this yet to keep things simple.
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08002061 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 */
2063
2064static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
2065{
2066 struct task_struct *t;
2067 unsigned long flags;
2068 cputime_t utime, stime;
2069
2070 memset((char *) r, 0, sizeof *r);
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08002071 utime = stime = cputime_zero;
2072
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07002073 rcu_read_lock();
2074 if (!lock_task_sighand(p, &flags)) {
2075 rcu_read_unlock();
2076 return;
2077 }
Ravikiran G Thirumalai2dd0ebc2006-03-23 03:00:13 -08002078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 switch (who) {
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08002080 case RUSAGE_BOTH:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 case RUSAGE_CHILDREN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 utime = p->signal->cutime;
2083 stime = p->signal->cstime;
2084 r->ru_nvcsw = p->signal->cnvcsw;
2085 r->ru_nivcsw = p->signal->cnivcsw;
2086 r->ru_minflt = p->signal->cmin_flt;
2087 r->ru_majflt = p->signal->cmaj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07002088 r->ru_inblock = p->signal->cinblock;
2089 r->ru_oublock = p->signal->coublock;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08002090
2091 if (who == RUSAGE_CHILDREN)
2092 break;
2093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 case RUSAGE_SELF:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 utime = cputime_add(utime, p->signal->utime);
2096 stime = cputime_add(stime, p->signal->stime);
2097 r->ru_nvcsw += p->signal->nvcsw;
2098 r->ru_nivcsw += p->signal->nivcsw;
2099 r->ru_minflt += p->signal->min_flt;
2100 r->ru_majflt += p->signal->maj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07002101 r->ru_inblock += p->signal->inblock;
2102 r->ru_oublock += p->signal->oublock;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 t = p;
2104 do {
2105 utime = cputime_add(utime, t->utime);
2106 stime = cputime_add(stime, t->stime);
2107 r->ru_nvcsw += t->nvcsw;
2108 r->ru_nivcsw += t->nivcsw;
2109 r->ru_minflt += t->min_flt;
2110 r->ru_majflt += t->maj_flt;
Eric Dumazet6eaeeab2007-05-10 22:22:37 -07002111 r->ru_inblock += task_io_get_inblock(t);
2112 r->ru_oublock += task_io_get_oublock(t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 t = next_thread(t);
2114 } while (t != p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 break;
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08002116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 default:
2118 BUG();
2119 }
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08002120
Ravikiran G Thirumalaide047c12006-06-22 14:47:26 -07002121 unlock_task_sighand(p, &flags);
2122 rcu_read_unlock();
2123
Oleg Nesterov0f59cc42006-01-08 01:05:15 -08002124 cputime_to_timeval(utime, &r->ru_utime);
2125 cputime_to_timeval(stime, &r->ru_stime);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126}
2127
2128int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
2129{
2130 struct rusage r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 k_getrusage(p, who, &r);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
2133}
2134
2135asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
2136{
2137 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
2138 return -EINVAL;
2139 return getrusage(current, who, ru);
2140}
2141
2142asmlinkage long sys_umask(int mask)
2143{
2144 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
2145 return mask;
2146}
2147
2148asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
2149 unsigned long arg4, unsigned long arg5)
2150{
2151 long error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
2153 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
2154 if (error)
2155 return error;
2156
2157 switch (option) {
2158 case PR_SET_PDEATHSIG:
Jesper Juhl0730ded2005-09-06 15:17:37 -07002159 if (!valid_signal(arg2)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 error = -EINVAL;
2161 break;
2162 }
Jesper Juhl0730ded2005-09-06 15:17:37 -07002163 current->pdeath_signal = arg2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 break;
2165 case PR_GET_PDEATHSIG:
2166 error = put_user(current->pdeath_signal, (int __user *)arg2);
2167 break;
2168 case PR_GET_DUMPABLE:
Michael Kerrisk2030c0f2005-09-16 19:28:02 -07002169 error = current->mm->dumpable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 break;
2171 case PR_SET_DUMPABLE:
Marcel Holtmannabf75a52006-07-12 13:12:00 +02002172 if (arg2 < 0 || arg2 > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 error = -EINVAL;
2174 break;
2175 }
2176 current->mm->dumpable = arg2;
2177 break;
2178
2179 case PR_SET_UNALIGN:
2180 error = SET_UNALIGN_CTL(current, arg2);
2181 break;
2182 case PR_GET_UNALIGN:
2183 error = GET_UNALIGN_CTL(current, arg2);
2184 break;
2185 case PR_SET_FPEMU:
2186 error = SET_FPEMU_CTL(current, arg2);
2187 break;
2188 case PR_GET_FPEMU:
2189 error = GET_FPEMU_CTL(current, arg2);
2190 break;
2191 case PR_SET_FPEXC:
2192 error = SET_FPEXC_CTL(current, arg2);
2193 break;
2194 case PR_GET_FPEXC:
2195 error = GET_FPEXC_CTL(current, arg2);
2196 break;
2197 case PR_GET_TIMING:
2198 error = PR_TIMING_STATISTICAL;
2199 break;
2200 case PR_SET_TIMING:
2201 if (arg2 == PR_TIMING_STATISTICAL)
2202 error = 0;
2203 else
2204 error = -EINVAL;
2205 break;
2206
2207 case PR_GET_KEEPCAPS:
2208 if (current->keep_capabilities)
2209 error = 1;
2210 break;
2211 case PR_SET_KEEPCAPS:
2212 if (arg2 != 0 && arg2 != 1) {
2213 error = -EINVAL;
2214 break;
2215 }
2216 current->keep_capabilities = arg2;
2217 break;
2218 case PR_SET_NAME: {
2219 struct task_struct *me = current;
2220 unsigned char ncomm[sizeof(me->comm)];
2221
2222 ncomm[sizeof(me->comm)-1] = 0;
2223 if (strncpy_from_user(ncomm, (char __user *)arg2,
2224 sizeof(me->comm)-1) < 0)
2225 return -EFAULT;
2226 set_task_comm(me, ncomm);
2227 return 0;
2228 }
2229 case PR_GET_NAME: {
2230 struct task_struct *me = current;
2231 unsigned char tcomm[sizeof(me->comm)];
2232
2233 get_task_comm(tcomm, me);
2234 if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))
2235 return -EFAULT;
2236 return 0;
2237 }
Anton Blanchard651d7652006-06-07 16:10:19 +10002238 case PR_GET_ENDIAN:
2239 error = GET_ENDIAN(current, arg2);
2240 break;
2241 case PR_SET_ENDIAN:
2242 error = SET_ENDIAN(current, arg2);
2243 break;
2244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 default:
2246 error = -EINVAL;
2247 break;
2248 }
2249 return error;
2250}
Andi Kleen3cfc3482006-09-26 10:52:28 +02002251
2252asmlinkage long sys_getcpu(unsigned __user *cpup, unsigned __user *nodep,
2253 struct getcpu_cache __user *cache)
2254{
2255 int err = 0;
2256 int cpu = raw_smp_processor_id();
2257 if (cpup)
2258 err |= put_user(cpu, cpup);
2259 if (nodep)
2260 err |= put_user(cpu_to_node(cpu), nodep);
2261 if (cache) {
2262 /*
2263 * The cache is not needed for this implementation,
2264 * but make sure user programs pass something
2265 * valid. vsyscall implementations can instead make
2266 * good use of the cache. Only use t0 and t1 because
2267 * these are available in both 32bit and 64bit ABI (no
2268 * need for a compat_getcpu). 32bit has enough
2269 * padding
2270 */
2271 unsigned long t0, t1;
Andi Kleen34596dc2006-09-30 01:47:55 +02002272 get_user(t0, &cache->blob[0]);
2273 get_user(t1, &cache->blob[1]);
Andi Kleen3cfc3482006-09-26 10:52:28 +02002274 t0++;
2275 t1++;
Andi Kleen34596dc2006-09-30 01:47:55 +02002276 put_user(t0, &cache->blob[0]);
2277 put_user(t1, &cache->blob[1]);
Andi Kleen3cfc3482006-09-26 10:52:28 +02002278 }
2279 return err ? -EFAULT : 0;
2280}