hugetlb: fix clear_user_highpage arguments
[GitHub/LineageOS/android_kernel_samsung_universal7580.git] / kernel / sys.c
CommitLineData
1da177e4
LT
1/*
2 * linux/kernel/sys.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
1da177e4
LT
7#include <linux/module.h>
8#include <linux/mm.h>
9#include <linux/utsname.h>
10#include <linux/mman.h>
11#include <linux/smp_lock.h>
12#include <linux/notifier.h>
13#include <linux/reboot.h>
14#include <linux/prctl.h>
1da177e4
LT
15#include <linux/highuid.h>
16#include <linux/fs.h>
3e88c553 17#include <linux/resource.h>
dc009d92
EB
18#include <linux/kernel.h>
19#include <linux/kexec.h>
1da177e4 20#include <linux/workqueue.h>
c59ede7b 21#include <linux/capability.h>
1da177e4
LT
22#include <linux/device.h>
23#include <linux/key.h>
24#include <linux/times.h>
25#include <linux/posix-timers.h>
26#include <linux/security.h>
27#include <linux/dcookies.h>
28#include <linux/suspend.h>
29#include <linux/tty.h>
7ed20e1a 30#include <linux/signal.h>
9f46080c 31#include <linux/cn_proc.h>
3cfc348b 32#include <linux/getcpu.h>
6eaeeaba 33#include <linux/task_io_accounting_ops.h>
1d9d02fe 34#include <linux/seccomp.h>
1da177e4
LT
35
36#include <linux/compat.h>
37#include <linux/syscalls.h>
00d7c05a 38#include <linux/kprobes.h>
acce292c 39#include <linux/user_namespace.h>
1da177e4
LT
40
41#include <asm/uaccess.h>
42#include <asm/io.h>
43#include <asm/unistd.h>
44
45#ifndef SET_UNALIGN_CTL
46# define SET_UNALIGN_CTL(a,b) (-EINVAL)
47#endif
48#ifndef GET_UNALIGN_CTL
49# define GET_UNALIGN_CTL(a,b) (-EINVAL)
50#endif
51#ifndef SET_FPEMU_CTL
52# define SET_FPEMU_CTL(a,b) (-EINVAL)
53#endif
54#ifndef GET_FPEMU_CTL
55# define GET_FPEMU_CTL(a,b) (-EINVAL)
56#endif
57#ifndef SET_FPEXC_CTL
58# define SET_FPEXC_CTL(a,b) (-EINVAL)
59#endif
60#ifndef GET_FPEXC_CTL
61# define GET_FPEXC_CTL(a,b) (-EINVAL)
62#endif
651d765d
AB
63#ifndef GET_ENDIAN
64# define GET_ENDIAN(a,b) (-EINVAL)
65#endif
66#ifndef SET_ENDIAN
67# define SET_ENDIAN(a,b) (-EINVAL)
68#endif
1da177e4
LT
69
70/*
71 * this is where the system-wide overflow UID and GID are defined, for
72 * architectures that now have 32-bit UID/GID but didn't in the past
73 */
74
75int overflowuid = DEFAULT_OVERFLOWUID;
76int overflowgid = DEFAULT_OVERFLOWGID;
77
78#ifdef CONFIG_UID16
79EXPORT_SYMBOL(overflowuid);
80EXPORT_SYMBOL(overflowgid);
81#endif
82
83/*
84 * the same as above, but for filesystems which can only store a 16-bit
85 * UID and GID. as such, this is needed on all architectures
86 */
87
88int fs_overflowuid = DEFAULT_FS_OVERFLOWUID;
89int fs_overflowgid = DEFAULT_FS_OVERFLOWUID;
90
91EXPORT_SYMBOL(fs_overflowuid);
92EXPORT_SYMBOL(fs_overflowgid);
93
94/*
95 * this indicates whether you can reboot with ctrl-alt-del: the default is yes
96 */
97
98int C_A_D = 1;
9ec52099
CLG
99struct pid *cad_pid;
100EXPORT_SYMBOL(cad_pid);
1da177e4 101
bd804eba
RW
102/*
103 * If set, this is used for preparing the system to power off.
104 */
105
106void (*pm_power_off_prepare)(void);
107EXPORT_SYMBOL(pm_power_off_prepare);
108
1da177e4
LT
109/*
110 * Notifier list for kernel code which wants to be called
111 * at shutdown. This is used to stop any idling DMA operations
112 * and the like.
113 */
114
e041c683
AS
115static BLOCKING_NOTIFIER_HEAD(reboot_notifier_list);
116
117/*
118 * Notifier chain core routines. The exported routines below
119 * are layered on top of these, with appropriate locking added.
120 */
121
122static int notifier_chain_register(struct notifier_block **nl,
123 struct notifier_block *n)
124{
125 while ((*nl) != NULL) {
126 if (n->priority > (*nl)->priority)
127 break;
128 nl = &((*nl)->next);
129 }
130 n->next = *nl;
131 rcu_assign_pointer(*nl, n);
132 return 0;
133}
134
135static int notifier_chain_unregister(struct notifier_block **nl,
136 struct notifier_block *n)
137{
138 while ((*nl) != NULL) {
139 if ((*nl) == n) {
140 rcu_assign_pointer(*nl, n->next);
141 return 0;
142 }
143 nl = &((*nl)->next);
144 }
145 return -ENOENT;
146}
147
6f7cc11a
GS
148/**
149 * notifier_call_chain - Informs the registered notifiers about an event.
150 * @nl: Pointer to head of the blocking notifier chain
151 * @val: Value passed unmodified to notifier function
152 * @v: Pointer passed unmodified to notifier function
153 * @nr_to_call: Number of notifier functions to be called. Don't care
154 * value of this parameter is -1.
155 * @nr_calls: Records the number of notifications sent. Don't care
156 * value of this field is NULL.
157 * @returns: notifier_call_chain returns the value returned by the
158 * last notifier function called.
159 */
160
e041c683 161static int __kprobes notifier_call_chain(struct notifier_block **nl,
6f7cc11a
GS
162 unsigned long val, void *v,
163 int nr_to_call, int *nr_calls)
e041c683
AS
164{
165 int ret = NOTIFY_DONE;
bbb1747d 166 struct notifier_block *nb, *next_nb;
e041c683
AS
167
168 nb = rcu_dereference(*nl);
6f7cc11a
GS
169
170 while (nb && nr_to_call) {
bbb1747d 171 next_nb = rcu_dereference(nb->next);
e041c683 172 ret = nb->notifier_call(nb, val, v);
6f7cc11a
GS
173
174 if (nr_calls)
175 (*nr_calls)++;
176
e041c683
AS
177 if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK)
178 break;
bbb1747d 179 nb = next_nb;
6f7cc11a 180 nr_to_call--;
e041c683
AS
181 }
182 return ret;
183}
184
185/*
186 * Atomic notifier chain routines. Registration and unregistration
eabc0694 187 * use a spinlock, and call_chain is synchronized by RCU (no locks).
e041c683 188 */
1da177e4
LT
189
190/**
e041c683
AS
191 * atomic_notifier_chain_register - Add notifier to an atomic notifier chain
192 * @nh: Pointer to head of the atomic notifier chain
1da177e4
LT
193 * @n: New entry in notifier chain
194 *
e041c683 195 * Adds a notifier to an atomic notifier chain.
1da177e4
LT
196 *
197 * Currently always returns zero.
198 */
e041c683
AS
199
200int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
201 struct notifier_block *n)
202{
203 unsigned long flags;
204 int ret;
205
206 spin_lock_irqsave(&nh->lock, flags);
207 ret = notifier_chain_register(&nh->head, n);
208 spin_unlock_irqrestore(&nh->lock, flags);
209 return ret;
210}
211
212EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
213
214/**
215 * atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain
216 * @nh: Pointer to head of the atomic notifier chain
217 * @n: Entry to remove from notifier chain
218 *
219 * Removes a notifier from an atomic notifier chain.
220 *
221 * Returns zero on success or %-ENOENT on failure.
222 */
223int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
224 struct notifier_block *n)
225{
226 unsigned long flags;
227 int ret;
228
229 spin_lock_irqsave(&nh->lock, flags);
230 ret = notifier_chain_unregister(&nh->head, n);
231 spin_unlock_irqrestore(&nh->lock, flags);
232 synchronize_rcu();
233 return ret;
234}
235
236EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);
237
238/**
6f7cc11a 239 * __atomic_notifier_call_chain - Call functions in an atomic notifier chain
e041c683
AS
240 * @nh: Pointer to head of the atomic notifier chain
241 * @val: Value passed unmodified to notifier function
242 * @v: Pointer passed unmodified to notifier function
6f7cc11a
GS
243 * @nr_to_call: See the comment for notifier_call_chain.
244 * @nr_calls: See the comment for notifier_call_chain.
e041c683
AS
245 *
246 * Calls each function in a notifier chain in turn. The functions
247 * run in an atomic context, so they must not block.
248 * This routine uses RCU to synchronize with changes to the chain.
249 *
250 * If the return value of the notifier can be and'ed
72fd4a35 251 * with %NOTIFY_STOP_MASK then atomic_notifier_call_chain()
e041c683
AS
252 * will return immediately, with the return value of
253 * the notifier function which halted execution.
254 * Otherwise the return value is the return value
255 * of the last notifier function called.
256 */
1da177e4 257
6f7cc11a
GS
258int __kprobes __atomic_notifier_call_chain(struct atomic_notifier_head *nh,
259 unsigned long val, void *v,
260 int nr_to_call, int *nr_calls)
1da177e4 261{
e041c683
AS
262 int ret;
263
264 rcu_read_lock();
6f7cc11a 265 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
e041c683
AS
266 rcu_read_unlock();
267 return ret;
1da177e4
LT
268}
269
6f7cc11a
GS
270EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain);
271
272int __kprobes atomic_notifier_call_chain(struct atomic_notifier_head *nh,
273 unsigned long val, void *v)
274{
275 return __atomic_notifier_call_chain(nh, val, v, -1, NULL);
276}
e041c683 277
6f7cc11a 278EXPORT_SYMBOL_GPL(atomic_notifier_call_chain);
e041c683
AS
279/*
280 * Blocking notifier chain routines. All access to the chain is
281 * synchronized by an rwsem.
282 */
1da177e4
LT
283
284/**
e041c683
AS
285 * blocking_notifier_chain_register - Add notifier to a blocking notifier chain
286 * @nh: Pointer to head of the blocking notifier chain
1da177e4
LT
287 * @n: New entry in notifier chain
288 *
e041c683
AS
289 * Adds a notifier to a blocking notifier chain.
290 * Must be called in process context.
1da177e4 291 *
e041c683 292 * Currently always returns zero.
1da177e4
LT
293 */
294
e041c683
AS
295int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
296 struct notifier_block *n)
1da177e4 297{
e041c683
AS
298 int ret;
299
300 /*
301 * This code gets used during boot-up, when task switching is
302 * not yet working and interrupts must remain disabled. At
303 * such times we must not call down_write().
304 */
305 if (unlikely(system_state == SYSTEM_BOOTING))
306 return notifier_chain_register(&nh->head, n);
307
308 down_write(&nh->rwsem);
309 ret = notifier_chain_register(&nh->head, n);
310 up_write(&nh->rwsem);
311 return ret;
1da177e4
LT
312}
313
e041c683 314EXPORT_SYMBOL_GPL(blocking_notifier_chain_register);
1da177e4
LT
315
316/**
e041c683
AS
317 * blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain
318 * @nh: Pointer to head of the blocking notifier chain
319 * @n: Entry to remove from notifier chain
320 *
321 * Removes a notifier from a blocking notifier chain.
322 * Must be called from process context.
323 *
324 * Returns zero on success or %-ENOENT on failure.
325 */
326int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
327 struct notifier_block *n)
328{
329 int ret;
330
331 /*
332 * This code gets used during boot-up, when task switching is
333 * not yet working and interrupts must remain disabled. At
334 * such times we must not call down_write().
335 */
336 if (unlikely(system_state == SYSTEM_BOOTING))
337 return notifier_chain_unregister(&nh->head, n);
338
339 down_write(&nh->rwsem);
340 ret = notifier_chain_unregister(&nh->head, n);
341 up_write(&nh->rwsem);
342 return ret;
343}
344
345EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);
346
347/**
6f7cc11a 348 * __blocking_notifier_call_chain - Call functions in a blocking notifier chain
e041c683 349 * @nh: Pointer to head of the blocking notifier chain
1da177e4
LT
350 * @val: Value passed unmodified to notifier function
351 * @v: Pointer passed unmodified to notifier function
6f7cc11a
GS
352 * @nr_to_call: See comment for notifier_call_chain.
353 * @nr_calls: See comment for notifier_call_chain.
1da177e4 354 *
e041c683
AS
355 * Calls each function in a notifier chain in turn. The functions
356 * run in a process context, so they are allowed to block.
1da177e4 357 *
e041c683 358 * If the return value of the notifier can be and'ed
72fd4a35 359 * with %NOTIFY_STOP_MASK then blocking_notifier_call_chain()
1da177e4
LT
360 * will return immediately, with the return value of
361 * the notifier function which halted execution.
e041c683 362 * Otherwise the return value is the return value
1da177e4
LT
363 * of the last notifier function called.
364 */
365
6f7cc11a
GS
366int __blocking_notifier_call_chain(struct blocking_notifier_head *nh,
367 unsigned long val, void *v,
368 int nr_to_call, int *nr_calls)
1da177e4 369{
1b5180b6 370 int ret = NOTIFY_DONE;
e041c683 371
1b5180b6
IM
372 /*
373 * We check the head outside the lock, but if this access is
374 * racy then it does not matter what the result of the test
375 * is, we re-check the list after having taken the lock anyway:
376 */
377 if (rcu_dereference(nh->head)) {
378 down_read(&nh->rwsem);
6f7cc11a
GS
379 ret = notifier_call_chain(&nh->head, val, v, nr_to_call,
380 nr_calls);
1b5180b6
IM
381 up_read(&nh->rwsem);
382 }
1da177e4
LT
383 return ret;
384}
6f7cc11a 385EXPORT_SYMBOL_GPL(__blocking_notifier_call_chain);
1da177e4 386
6f7cc11a
GS
387int blocking_notifier_call_chain(struct blocking_notifier_head *nh,
388 unsigned long val, void *v)
389{
390 return __blocking_notifier_call_chain(nh, val, v, -1, NULL);
391}
e041c683
AS
392EXPORT_SYMBOL_GPL(blocking_notifier_call_chain);
393
394/*
395 * Raw notifier chain routines. There is no protection;
396 * the caller must provide it. Use at your own risk!
397 */
398
399/**
400 * raw_notifier_chain_register - Add notifier to a raw notifier chain
401 * @nh: Pointer to head of the raw notifier chain
402 * @n: New entry in notifier chain
403 *
404 * Adds a notifier to a raw notifier chain.
405 * All locking must be provided by the caller.
406 *
407 * Currently always returns zero.
408 */
409
410int raw_notifier_chain_register(struct raw_notifier_head *nh,
411 struct notifier_block *n)
412{
413 return notifier_chain_register(&nh->head, n);
414}
415
416EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
417
418/**
419 * raw_notifier_chain_unregister - Remove notifier from a raw notifier chain
420 * @nh: Pointer to head of the raw notifier chain
421 * @n: Entry to remove from notifier chain
422 *
423 * Removes a notifier from a raw notifier chain.
424 * All locking must be provided by the caller.
425 *
426 * Returns zero on success or %-ENOENT on failure.
427 */
428int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
429 struct notifier_block *n)
430{
431 return notifier_chain_unregister(&nh->head, n);
432}
433
434EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);
435
436/**
6f7cc11a 437 * __raw_notifier_call_chain - Call functions in a raw notifier chain
e041c683
AS
438 * @nh: Pointer to head of the raw notifier chain
439 * @val: Value passed unmodified to notifier function
440 * @v: Pointer passed unmodified to notifier function
6f7cc11a
GS
441 * @nr_to_call: See comment for notifier_call_chain.
442 * @nr_calls: See comment for notifier_call_chain
e041c683
AS
443 *
444 * Calls each function in a notifier chain in turn. The functions
445 * run in an undefined context.
446 * All locking must be provided by the caller.
447 *
448 * If the return value of the notifier can be and'ed
72fd4a35 449 * with %NOTIFY_STOP_MASK then raw_notifier_call_chain()
e041c683
AS
450 * will return immediately, with the return value of
451 * the notifier function which halted execution.
452 * Otherwise the return value is the return value
453 * of the last notifier function called.
454 */
455
6f7cc11a
GS
456int __raw_notifier_call_chain(struct raw_notifier_head *nh,
457 unsigned long val, void *v,
458 int nr_to_call, int *nr_calls)
459{
460 return notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
461}
462
463EXPORT_SYMBOL_GPL(__raw_notifier_call_chain);
464
e041c683
AS
465int raw_notifier_call_chain(struct raw_notifier_head *nh,
466 unsigned long val, void *v)
467{
6f7cc11a 468 return __raw_notifier_call_chain(nh, val, v, -1, NULL);
e041c683
AS
469}
470
471EXPORT_SYMBOL_GPL(raw_notifier_call_chain);
1da177e4 472
eabc0694
AS
473/*
474 * SRCU notifier chain routines. Registration and unregistration
475 * use a mutex, and call_chain is synchronized by SRCU (no locks).
476 */
477
478/**
479 * srcu_notifier_chain_register - Add notifier to an SRCU notifier chain
480 * @nh: Pointer to head of the SRCU notifier chain
481 * @n: New entry in notifier chain
482 *
483 * Adds a notifier to an SRCU notifier chain.
484 * Must be called in process context.
485 *
486 * Currently always returns zero.
487 */
488
489int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
490 struct notifier_block *n)
491{
492 int ret;
493
494 /*
495 * This code gets used during boot-up, when task switching is
496 * not yet working and interrupts must remain disabled. At
497 * such times we must not call mutex_lock().
498 */
499 if (unlikely(system_state == SYSTEM_BOOTING))
500 return notifier_chain_register(&nh->head, n);
501
502 mutex_lock(&nh->mutex);
503 ret = notifier_chain_register(&nh->head, n);
504 mutex_unlock(&nh->mutex);
505 return ret;
506}
507
508EXPORT_SYMBOL_GPL(srcu_notifier_chain_register);
509
510/**
511 * srcu_notifier_chain_unregister - Remove notifier from an SRCU notifier chain
512 * @nh: Pointer to head of the SRCU notifier chain
513 * @n: Entry to remove from notifier chain
514 *
515 * Removes a notifier from an SRCU notifier chain.
516 * Must be called from process context.
517 *
518 * Returns zero on success or %-ENOENT on failure.
519 */
520int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
521 struct notifier_block *n)
522{
523 int ret;
524
525 /*
526 * This code gets used during boot-up, when task switching is
527 * not yet working and interrupts must remain disabled. At
528 * such times we must not call mutex_lock().
529 */
530 if (unlikely(system_state == SYSTEM_BOOTING))
531 return notifier_chain_unregister(&nh->head, n);
532
533 mutex_lock(&nh->mutex);
534 ret = notifier_chain_unregister(&nh->head, n);
535 mutex_unlock(&nh->mutex);
536 synchronize_srcu(&nh->srcu);
537 return ret;
538}
539
540EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);
541
542/**
6f7cc11a 543 * __srcu_notifier_call_chain - Call functions in an SRCU notifier chain
eabc0694
AS
544 * @nh: Pointer to head of the SRCU notifier chain
545 * @val: Value passed unmodified to notifier function
546 * @v: Pointer passed unmodified to notifier function
6f7cc11a
GS
547 * @nr_to_call: See comment for notifier_call_chain.
548 * @nr_calls: See comment for notifier_call_chain
eabc0694
AS
549 *
550 * Calls each function in a notifier chain in turn. The functions
551 * run in a process context, so they are allowed to block.
552 *
553 * If the return value of the notifier can be and'ed
72fd4a35 554 * with %NOTIFY_STOP_MASK then srcu_notifier_call_chain()
eabc0694
AS
555 * will return immediately, with the return value of
556 * the notifier function which halted execution.
557 * Otherwise the return value is the return value
558 * of the last notifier function called.
559 */
560
6f7cc11a
GS
561int __srcu_notifier_call_chain(struct srcu_notifier_head *nh,
562 unsigned long val, void *v,
563 int nr_to_call, int *nr_calls)
eabc0694
AS
564{
565 int ret;
566 int idx;
567
568 idx = srcu_read_lock(&nh->srcu);
6f7cc11a 569 ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls);
eabc0694
AS
570 srcu_read_unlock(&nh->srcu, idx);
571 return ret;
572}
6f7cc11a 573EXPORT_SYMBOL_GPL(__srcu_notifier_call_chain);
eabc0694 574
6f7cc11a
GS
575int srcu_notifier_call_chain(struct srcu_notifier_head *nh,
576 unsigned long val, void *v)
577{
578 return __srcu_notifier_call_chain(nh, val, v, -1, NULL);
579}
eabc0694
AS
580EXPORT_SYMBOL_GPL(srcu_notifier_call_chain);
581
582/**
583 * srcu_init_notifier_head - Initialize an SRCU notifier head
584 * @nh: Pointer to head of the srcu notifier chain
585 *
586 * Unlike other sorts of notifier heads, SRCU notifier heads require
587 * dynamic initialization. Be sure to call this routine before
588 * calling any of the other SRCU notifier routines for this head.
589 *
590 * If an SRCU notifier head is deallocated, it must first be cleaned
591 * up by calling srcu_cleanup_notifier_head(). Otherwise the head's
592 * per-cpu data (used by the SRCU mechanism) will leak.
593 */
594
595void srcu_init_notifier_head(struct srcu_notifier_head *nh)
596{
597 mutex_init(&nh->mutex);
e6a92013
AS
598 if (init_srcu_struct(&nh->srcu) < 0)
599 BUG();
eabc0694
AS
600 nh->head = NULL;
601}
602
603EXPORT_SYMBOL_GPL(srcu_init_notifier_head);
604
1da177e4
LT
605/**
606 * register_reboot_notifier - Register function to be called at reboot time
607 * @nb: Info about notifier function to be called
608 *
609 * Registers a function with the list of functions
610 * to be called at reboot time.
611 *
72fd4a35 612 * Currently always returns zero, as blocking_notifier_chain_register()
1da177e4
LT
613 * always returns zero.
614 */
615
616int register_reboot_notifier(struct notifier_block * nb)
617{
e041c683 618 return blocking_notifier_chain_register(&reboot_notifier_list, nb);
1da177e4
LT
619}
620
621EXPORT_SYMBOL(register_reboot_notifier);
622
623/**
624 * unregister_reboot_notifier - Unregister previously registered reboot notifier
625 * @nb: Hook to be unregistered
626 *
627 * Unregisters a previously registered reboot
628 * notifier function.
629 *
630 * Returns zero on success, or %-ENOENT on failure.
631 */
632
633int unregister_reboot_notifier(struct notifier_block * nb)
634{
e041c683 635 return blocking_notifier_chain_unregister(&reboot_notifier_list, nb);
1da177e4
LT
636}
637
638EXPORT_SYMBOL(unregister_reboot_notifier);
639
640static int set_one_prio(struct task_struct *p, int niceval, int error)
641{
642 int no_nice;
643
644 if (p->uid != current->euid &&
645 p->euid != current->euid && !capable(CAP_SYS_NICE)) {
646 error = -EPERM;
647 goto out;
648 }
e43379f1 649 if (niceval < task_nice(p) && !can_nice(p, niceval)) {
1da177e4
LT
650 error = -EACCES;
651 goto out;
652 }
653 no_nice = security_task_setnice(p, niceval);
654 if (no_nice) {
655 error = no_nice;
656 goto out;
657 }
658 if (error == -ESRCH)
659 error = 0;
660 set_user_nice(p, niceval);
661out:
662 return error;
663}
664
665asmlinkage long sys_setpriority(int which, int who, int niceval)
666{
667 struct task_struct *g, *p;
668 struct user_struct *user;
669 int error = -EINVAL;
41487c65 670 struct pid *pgrp;
1da177e4 671
3e88c553 672 if (which > PRIO_USER || which < PRIO_PROCESS)
1da177e4
LT
673 goto out;
674
675 /* normalize: avoid signed division (rounding problems) */
676 error = -ESRCH;
677 if (niceval < -20)
678 niceval = -20;
679 if (niceval > 19)
680 niceval = 19;
681
682 read_lock(&tasklist_lock);
683 switch (which) {
684 case PRIO_PROCESS:
41487c65
EB
685 if (who)
686 p = find_task_by_pid(who);
687 else
688 p = current;
1da177e4
LT
689 if (p)
690 error = set_one_prio(p, niceval, error);
691 break;
692 case PRIO_PGRP:
41487c65
EB
693 if (who)
694 pgrp = find_pid(who);
695 else
696 pgrp = task_pgrp(current);
697 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
1da177e4 698 error = set_one_prio(p, niceval, error);
41487c65 699 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
1da177e4
LT
700 break;
701 case PRIO_USER:
702 user = current->user;
703 if (!who)
704 who = current->uid;
705 else
706 if ((who != current->uid) && !(user = find_user(who)))
707 goto out_unlock; /* No processes for this user */
708
709 do_each_thread(g, p)
710 if (p->uid == who)
711 error = set_one_prio(p, niceval, error);
712 while_each_thread(g, p);
713 if (who != current->uid)
714 free_uid(user); /* For find_user() */
715 break;
716 }
717out_unlock:
718 read_unlock(&tasklist_lock);
719out:
720 return error;
721}
722
723/*
724 * Ugh. To avoid negative return values, "getpriority()" will
725 * not return the normal nice-value, but a negated value that
726 * has been offset by 20 (ie it returns 40..1 instead of -20..19)
727 * to stay compatible.
728 */
729asmlinkage long sys_getpriority(int which, int who)
730{
731 struct task_struct *g, *p;
732 struct user_struct *user;
733 long niceval, retval = -ESRCH;
41487c65 734 struct pid *pgrp;
1da177e4 735
3e88c553 736 if (which > PRIO_USER || which < PRIO_PROCESS)
1da177e4
LT
737 return -EINVAL;
738
739 read_lock(&tasklist_lock);
740 switch (which) {
741 case PRIO_PROCESS:
41487c65
EB
742 if (who)
743 p = find_task_by_pid(who);
744 else
745 p = current;
1da177e4
LT
746 if (p) {
747 niceval = 20 - task_nice(p);
748 if (niceval > retval)
749 retval = niceval;
750 }
751 break;
752 case PRIO_PGRP:
41487c65
EB
753 if (who)
754 pgrp = find_pid(who);
755 else
756 pgrp = task_pgrp(current);
757 do_each_pid_task(pgrp, PIDTYPE_PGID, p) {
1da177e4
LT
758 niceval = 20 - task_nice(p);
759 if (niceval > retval)
760 retval = niceval;
41487c65 761 } while_each_pid_task(pgrp, PIDTYPE_PGID, p);
1da177e4
LT
762 break;
763 case PRIO_USER:
764 user = current->user;
765 if (!who)
766 who = current->uid;
767 else
768 if ((who != current->uid) && !(user = find_user(who)))
769 goto out_unlock; /* No processes for this user */
770
771 do_each_thread(g, p)
772 if (p->uid == who) {
773 niceval = 20 - task_nice(p);
774 if (niceval > retval)
775 retval = niceval;
776 }
777 while_each_thread(g, p);
778 if (who != current->uid)
779 free_uid(user); /* for find_user() */
780 break;
781 }
782out_unlock:
783 read_unlock(&tasklist_lock);
784
785 return retval;
786}
787
e4c94330
EB
788/**
789 * emergency_restart - reboot the system
790 *
791 * Without shutting down any hardware or taking any locks
792 * reboot the system. This is called when we know we are in
793 * trouble so this is our best effort to reboot. This is
794 * safe to call in interrupt context.
795 */
7c903473
EB
796void emergency_restart(void)
797{
798 machine_emergency_restart();
799}
800EXPORT_SYMBOL_GPL(emergency_restart);
801
83cc5ed3 802static void kernel_restart_prepare(char *cmd)
4a00ea1e 803{
e041c683 804 blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd);
4a00ea1e 805 system_state = SYSTEM_RESTART;
4a00ea1e 806 device_shutdown();
58b3b71d 807 sysdev_shutdown();
e4c94330 808}
1e5d5331
RD
809
810/**
811 * kernel_restart - reboot the system
812 * @cmd: pointer to buffer containing command to execute for restart
b8887e6e 813 * or %NULL
1e5d5331
RD
814 *
815 * Shutdown everything and perform a clean reboot.
816 * This is not safe to call in interrupt context.
817 */
e4c94330
EB
818void kernel_restart(char *cmd)
819{
820 kernel_restart_prepare(cmd);
756184b7 821 if (!cmd)
4a00ea1e 822 printk(KERN_EMERG "Restarting system.\n");
756184b7 823 else
4a00ea1e 824 printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd);
4a00ea1e
EB
825 machine_restart(cmd);
826}
827EXPORT_SYMBOL_GPL(kernel_restart);
828
e4c94330
EB
829/**
830 * kernel_kexec - reboot the system
831 *
832 * Move into place and start executing a preloaded standalone
833 * executable. If nothing was preloaded return an error.
834 */
83cc5ed3 835static void kernel_kexec(void)
4a00ea1e
EB
836{
837#ifdef CONFIG_KEXEC
838 struct kimage *image;
4bb8089c 839 image = xchg(&kexec_image, NULL);
756184b7 840 if (!image)
4a00ea1e 841 return;
e4c94330 842 kernel_restart_prepare(NULL);
4a00ea1e
EB
843 printk(KERN_EMERG "Starting new kernel\n");
844 machine_shutdown();
845 machine_kexec(image);
846#endif
847}
4a00ea1e 848
729b4d4c
AS
849void kernel_shutdown_prepare(enum system_states state)
850{
e041c683 851 blocking_notifier_call_chain(&reboot_notifier_list,
729b4d4c
AS
852 (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL);
853 system_state = state;
854 device_shutdown();
855}
e4c94330
EB
856/**
857 * kernel_halt - halt the system
858 *
859 * Shutdown everything and perform a clean system halt.
860 */
e4c94330
EB
861void kernel_halt(void)
862{
729b4d4c 863 kernel_shutdown_prepare(SYSTEM_HALT);
58b3b71d 864 sysdev_shutdown();
4a00ea1e
EB
865 printk(KERN_EMERG "System halted.\n");
866 machine_halt();
867}
729b4d4c 868
4a00ea1e
EB
869EXPORT_SYMBOL_GPL(kernel_halt);
870
e4c94330
EB
871/**
872 * kernel_power_off - power_off the system
873 *
874 * Shutdown everything and perform a clean system power_off.
875 */
e4c94330
EB
876void kernel_power_off(void)
877{
729b4d4c 878 kernel_shutdown_prepare(SYSTEM_POWER_OFF);
bd804eba
RW
879 if (pm_power_off_prepare)
880 pm_power_off_prepare();
58b3b71d 881 sysdev_shutdown();
4a00ea1e
EB
882 printk(KERN_EMERG "Power down.\n");
883 machine_power_off();
884}
885EXPORT_SYMBOL_GPL(kernel_power_off);
1da177e4
LT
886/*
887 * Reboot system call: for obvious reasons only root may call it,
888 * and even root needs to set up some magic numbers in the registers
889 * so that some mistake won't make this reboot the whole machine.
890 * You can also set the meaning of the ctrl-alt-del-key here.
891 *
892 * reboot doesn't sync: do that yourself before calling this.
893 */
894asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg)
895{
896 char buffer[256];
897
898 /* We only trust the superuser with rebooting the system. */
899 if (!capable(CAP_SYS_BOOT))
900 return -EPERM;
901
902 /* For safety, we require "magic" arguments. */
903 if (magic1 != LINUX_REBOOT_MAGIC1 ||
904 (magic2 != LINUX_REBOOT_MAGIC2 &&
905 magic2 != LINUX_REBOOT_MAGIC2A &&
906 magic2 != LINUX_REBOOT_MAGIC2B &&
907 magic2 != LINUX_REBOOT_MAGIC2C))
908 return -EINVAL;
909
5e38291d
EB
910 /* Instead of trying to make the power_off code look like
911 * halt when pm_power_off is not set do it the easy way.
912 */
913 if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off)
914 cmd = LINUX_REBOOT_CMD_HALT;
915
1da177e4
LT
916 lock_kernel();
917 switch (cmd) {
918 case LINUX_REBOOT_CMD_RESTART:
4a00ea1e 919 kernel_restart(NULL);
1da177e4
LT
920 break;
921
922 case LINUX_REBOOT_CMD_CAD_ON:
923 C_A_D = 1;
924 break;
925
926 case LINUX_REBOOT_CMD_CAD_OFF:
927 C_A_D = 0;
928 break;
929
930 case LINUX_REBOOT_CMD_HALT:
4a00ea1e 931 kernel_halt();
1da177e4
LT
932 unlock_kernel();
933 do_exit(0);
934 break;
935
936 case LINUX_REBOOT_CMD_POWER_OFF:
4a00ea1e 937 kernel_power_off();
1da177e4
LT
938 unlock_kernel();
939 do_exit(0);
940 break;
941
942 case LINUX_REBOOT_CMD_RESTART2:
943 if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) {
944 unlock_kernel();
945 return -EFAULT;
946 }
947 buffer[sizeof(buffer) - 1] = '\0';
948
4a00ea1e 949 kernel_restart(buffer);
1da177e4
LT
950 break;
951
dc009d92 952 case LINUX_REBOOT_CMD_KEXEC:
4a00ea1e
EB
953 kernel_kexec();
954 unlock_kernel();
955 return -EINVAL;
956
b0cb1a19 957#ifdef CONFIG_HIBERNATION
1da177e4
LT
958 case LINUX_REBOOT_CMD_SW_SUSPEND:
959 {
a3d25c27 960 int ret = hibernate();
1da177e4
LT
961 unlock_kernel();
962 return ret;
963 }
964#endif
965
966 default:
967 unlock_kernel();
968 return -EINVAL;
969 }
970 unlock_kernel();
971 return 0;
972}
973
65f27f38 974static void deferred_cad(struct work_struct *dummy)
1da177e4 975{
abcd9e51 976 kernel_restart(NULL);
1da177e4
LT
977}
978
979/*
980 * This function gets called by ctrl-alt-del - ie the keyboard interrupt.
981 * As it's called within an interrupt, it may NOT sync: the only choice
982 * is whether to reboot at once, or just ignore the ctrl-alt-del.
983 */
984void ctrl_alt_del(void)
985{
65f27f38 986 static DECLARE_WORK(cad_work, deferred_cad);
1da177e4
LT
987
988 if (C_A_D)
989 schedule_work(&cad_work);
990 else
9ec52099 991 kill_cad_pid(SIGINT, 1);
1da177e4
LT
992}
993
1da177e4
LT
994/*
995 * Unprivileged users may change the real gid to the effective gid
996 * or vice versa. (BSD-style)
997 *
998 * If you set the real gid at all, or set the effective gid to a value not
999 * equal to the real gid, then the saved gid is set to the new effective gid.
1000 *
1001 * This makes it possible for a setgid program to completely drop its
1002 * privileges, which is often a useful assertion to make when you are doing
1003 * a security audit over a program.
1004 *
1005 * The general idea is that a program which uses just setregid() will be
1006 * 100% compatible with BSD. A program which uses just setgid() will be
1007 * 100% compatible with POSIX with saved IDs.
1008 *
1009 * SMP: There are not races, the GIDs are checked only by filesystem
1010 * operations (as far as semantic preservation is concerned).
1011 */
1012asmlinkage long sys_setregid(gid_t rgid, gid_t egid)
1013{
1014 int old_rgid = current->gid;
1015 int old_egid = current->egid;
1016 int new_rgid = old_rgid;
1017 int new_egid = old_egid;
1018 int retval;
1019
1020 retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE);
1021 if (retval)
1022 return retval;
1023
1024 if (rgid != (gid_t) -1) {
1025 if ((old_rgid == rgid) ||
1026 (current->egid==rgid) ||
1027 capable(CAP_SETGID))
1028 new_rgid = rgid;
1029 else
1030 return -EPERM;
1031 }
1032 if (egid != (gid_t) -1) {
1033 if ((old_rgid == egid) ||
1034 (current->egid == egid) ||
1035 (current->sgid == egid) ||
1036 capable(CAP_SETGID))
1037 new_egid = egid;
756184b7 1038 else
1da177e4 1039 return -EPERM;
1da177e4 1040 }
756184b7 1041 if (new_egid != old_egid) {
6c5d5238 1042 set_dumpable(current->mm, suid_dumpable);
d59dd462 1043 smp_wmb();
1da177e4
LT
1044 }
1045 if (rgid != (gid_t) -1 ||
1046 (egid != (gid_t) -1 && egid != old_rgid))
1047 current->sgid = new_egid;
1048 current->fsgid = new_egid;
1049 current->egid = new_egid;
1050 current->gid = new_rgid;
1051 key_fsgid_changed(current);
9f46080c 1052 proc_id_connector(current, PROC_EVENT_GID);
1da177e4
LT
1053 return 0;
1054}
1055
1056/*
1057 * setgid() is implemented like SysV w/ SAVED_IDS
1058 *
1059 * SMP: Same implicit races as above.
1060 */
1061asmlinkage long sys_setgid(gid_t gid)
1062{
1063 int old_egid = current->egid;
1064 int retval;
1065
1066 retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID);
1067 if (retval)
1068 return retval;
1069
756184b7
CP
1070 if (capable(CAP_SETGID)) {
1071 if (old_egid != gid) {
6c5d5238 1072 set_dumpable(current->mm, suid_dumpable);
d59dd462 1073 smp_wmb();
1da177e4
LT
1074 }
1075 current->gid = current->egid = current->sgid = current->fsgid = gid;
756184b7
CP
1076 } else if ((gid == current->gid) || (gid == current->sgid)) {
1077 if (old_egid != gid) {
6c5d5238 1078 set_dumpable(current->mm, suid_dumpable);
d59dd462 1079 smp_wmb();
1da177e4
LT
1080 }
1081 current->egid = current->fsgid = gid;
1082 }
1083 else
1084 return -EPERM;
1085
1086 key_fsgid_changed(current);
9f46080c 1087 proc_id_connector(current, PROC_EVENT_GID);
1da177e4
LT
1088 return 0;
1089}
1090
1091static int set_user(uid_t new_ruid, int dumpclear)
1092{
1093 struct user_struct *new_user;
1094
acce292c 1095 new_user = alloc_uid(current->nsproxy->user_ns, new_ruid);
1da177e4
LT
1096 if (!new_user)
1097 return -EAGAIN;
1098
1099 if (atomic_read(&new_user->processes) >=
1100 current->signal->rlim[RLIMIT_NPROC].rlim_cur &&
acce292c 1101 new_user != current->nsproxy->user_ns->root_user) {
1da177e4
LT
1102 free_uid(new_user);
1103 return -EAGAIN;
1104 }
1105
1106 switch_uid(new_user);
1107
756184b7 1108 if (dumpclear) {
6c5d5238 1109 set_dumpable(current->mm, suid_dumpable);
d59dd462 1110 smp_wmb();
1da177e4
LT
1111 }
1112 current->uid = new_ruid;
1113 return 0;
1114}
1115
1116/*
1117 * Unprivileged users may change the real uid to the effective uid
1118 * or vice versa. (BSD-style)
1119 *
1120 * If you set the real uid at all, or set the effective uid to a value not
1121 * equal to the real uid, then the saved uid is set to the new effective uid.
1122 *
1123 * This makes it possible for a setuid program to completely drop its
1124 * privileges, which is often a useful assertion to make when you are doing
1125 * a security audit over a program.
1126 *
1127 * The general idea is that a program which uses just setreuid() will be
1128 * 100% compatible with BSD. A program which uses just setuid() will be
1129 * 100% compatible with POSIX with saved IDs.
1130 */
1131asmlinkage long sys_setreuid(uid_t ruid, uid_t euid)
1132{
1133 int old_ruid, old_euid, old_suid, new_ruid, new_euid;
1134 int retval;
1135
1136 retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE);
1137 if (retval)
1138 return retval;
1139
1140 new_ruid = old_ruid = current->uid;
1141 new_euid = old_euid = current->euid;
1142 old_suid = current->suid;
1143
1144 if (ruid != (uid_t) -1) {
1145 new_ruid = ruid;
1146 if ((old_ruid != ruid) &&
1147 (current->euid != ruid) &&
1148 !capable(CAP_SETUID))
1149 return -EPERM;
1150 }
1151
1152 if (euid != (uid_t) -1) {
1153 new_euid = euid;
1154 if ((old_ruid != euid) &&
1155 (current->euid != euid) &&
1156 (current->suid != euid) &&
1157 !capable(CAP_SETUID))
1158 return -EPERM;
1159 }
1160
1161 if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0)
1162 return -EAGAIN;
1163
756184b7 1164 if (new_euid != old_euid) {
6c5d5238 1165 set_dumpable(current->mm, suid_dumpable);
d59dd462 1166 smp_wmb();
1da177e4
LT
1167 }
1168 current->fsuid = current->euid = new_euid;
1169 if (ruid != (uid_t) -1 ||
1170 (euid != (uid_t) -1 && euid != old_ruid))
1171 current->suid = current->euid;
1172 current->fsuid = current->euid;
1173
1174 key_fsuid_changed(current);
9f46080c 1175 proc_id_connector(current, PROC_EVENT_UID);
1da177e4
LT
1176
1177 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE);
1178}
1179
1180
1181
1182/*
1183 * setuid() is implemented like SysV with SAVED_IDS
1184 *
1185 * Note that SAVED_ID's is deficient in that a setuid root program
1186 * like sendmail, for example, cannot set its uid to be a normal
1187 * user and then switch back, because if you're root, setuid() sets
1188 * the saved uid too. If you don't like this, blame the bright people
1189 * in the POSIX committee and/or USG. Note that the BSD-style setreuid()
1190 * will allow a root program to temporarily drop privileges and be able to
1191 * regain them by swapping the real and effective uid.
1192 */
1193asmlinkage long sys_setuid(uid_t uid)
1194{
1195 int old_euid = current->euid;
a09c17a6 1196 int old_ruid, old_suid, new_suid;
1da177e4
LT
1197 int retval;
1198
1199 retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID);
1200 if (retval)
1201 return retval;
1202
a09c17a6 1203 old_ruid = current->uid;
1da177e4
LT
1204 old_suid = current->suid;
1205 new_suid = old_suid;
1206
1207 if (capable(CAP_SETUID)) {
1208 if (uid != old_ruid && set_user(uid, old_euid != uid) < 0)
1209 return -EAGAIN;
1210 new_suid = uid;
1211 } else if ((uid != current->uid) && (uid != new_suid))
1212 return -EPERM;
1213
756184b7 1214 if (old_euid != uid) {
6c5d5238 1215 set_dumpable(current->mm, suid_dumpable);
d59dd462 1216 smp_wmb();
1da177e4
LT
1217 }
1218 current->fsuid = current->euid = uid;
1219 current->suid = new_suid;
1220
1221 key_fsuid_changed(current);
9f46080c 1222 proc_id_connector(current, PROC_EVENT_UID);
1da177e4
LT
1223
1224 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID);
1225}
1226
1227
1228/*
1229 * This function implements a generic ability to update ruid, euid,
1230 * and suid. This allows you to implement the 4.4 compatible seteuid().
1231 */
1232asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid)
1233{
1234 int old_ruid = current->uid;
1235 int old_euid = current->euid;
1236 int old_suid = current->suid;
1237 int retval;
1238
1239 retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES);
1240 if (retval)
1241 return retval;
1242
1243 if (!capable(CAP_SETUID)) {
1244 if ((ruid != (uid_t) -1) && (ruid != current->uid) &&
1245 (ruid != current->euid) && (ruid != current->suid))
1246 return -EPERM;
1247 if ((euid != (uid_t) -1) && (euid != current->uid) &&
1248 (euid != current->euid) && (euid != current->suid))
1249 return -EPERM;
1250 if ((suid != (uid_t) -1) && (suid != current->uid) &&
1251 (suid != current->euid) && (suid != current->suid))
1252 return -EPERM;
1253 }
1254 if (ruid != (uid_t) -1) {
1255 if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0)
1256 return -EAGAIN;
1257 }
1258 if (euid != (uid_t) -1) {
756184b7 1259 if (euid != current->euid) {
6c5d5238 1260 set_dumpable(current->mm, suid_dumpable);
d59dd462 1261 smp_wmb();
1da177e4
LT
1262 }
1263 current->euid = euid;
1264 }
1265 current->fsuid = current->euid;
1266 if (suid != (uid_t) -1)
1267 current->suid = suid;
1268
1269 key_fsuid_changed(current);
9f46080c 1270 proc_id_connector(current, PROC_EVENT_UID);
1da177e4
LT
1271
1272 return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES);
1273}
1274
1275asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid)
1276{
1277 int retval;
1278
1279 if (!(retval = put_user(current->uid, ruid)) &&
1280 !(retval = put_user(current->euid, euid)))
1281 retval = put_user(current->suid, suid);
1282
1283 return retval;
1284}
1285
1286/*
1287 * Same as above, but for rgid, egid, sgid.
1288 */
1289asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid)
1290{
1291 int retval;
1292
1293 retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES);
1294 if (retval)
1295 return retval;
1296
1297 if (!capable(CAP_SETGID)) {
1298 if ((rgid != (gid_t) -1) && (rgid != current->gid) &&
1299 (rgid != current->egid) && (rgid != current->sgid))
1300 return -EPERM;
1301 if ((egid != (gid_t) -1) && (egid != current->gid) &&
1302 (egid != current->egid) && (egid != current->sgid))
1303 return -EPERM;
1304 if ((sgid != (gid_t) -1) && (sgid != current->gid) &&
1305 (sgid != current->egid) && (sgid != current->sgid))
1306 return -EPERM;
1307 }
1308 if (egid != (gid_t) -1) {
756184b7 1309 if (egid != current->egid) {
6c5d5238 1310 set_dumpable(current->mm, suid_dumpable);
d59dd462 1311 smp_wmb();
1da177e4
LT
1312 }
1313 current->egid = egid;
1314 }
1315 current->fsgid = current->egid;
1316 if (rgid != (gid_t) -1)
1317 current->gid = rgid;
1318 if (sgid != (gid_t) -1)
1319 current->sgid = sgid;
1320
1321 key_fsgid_changed(current);
9f46080c 1322 proc_id_connector(current, PROC_EVENT_GID);
1da177e4
LT
1323 return 0;
1324}
1325
1326asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid)
1327{
1328 int retval;
1329
1330 if (!(retval = put_user(current->gid, rgid)) &&
1331 !(retval = put_user(current->egid, egid)))
1332 retval = put_user(current->sgid, sgid);
1333
1334 return retval;
1335}
1336
1337
1338/*
1339 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This
1340 * is used for "access()" and for the NFS daemon (letting nfsd stay at
1341 * whatever uid it wants to). It normally shadows "euid", except when
1342 * explicitly set by setfsuid() or for access..
1343 */
1344asmlinkage long sys_setfsuid(uid_t uid)
1345{
1346 int old_fsuid;
1347
1348 old_fsuid = current->fsuid;
1349 if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS))
1350 return old_fsuid;
1351
1352 if (uid == current->uid || uid == current->euid ||
1353 uid == current->suid || uid == current->fsuid ||
756184b7
CP
1354 capable(CAP_SETUID)) {
1355 if (uid != old_fsuid) {
6c5d5238 1356 set_dumpable(current->mm, suid_dumpable);
d59dd462 1357 smp_wmb();
1da177e4
LT
1358 }
1359 current->fsuid = uid;
1360 }
1361
1362 key_fsuid_changed(current);
9f46080c 1363 proc_id_connector(current, PROC_EVENT_UID);
1da177e4
LT
1364
1365 security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS);
1366
1367 return old_fsuid;
1368}
1369
1370/*
f42df9e6 1371 * Samma på svenska..
1da177e4
LT
1372 */
1373asmlinkage long sys_setfsgid(gid_t gid)
1374{
1375 int old_fsgid;
1376
1377 old_fsgid = current->fsgid;
1378 if (security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_FS))
1379 return old_fsgid;
1380
1381 if (gid == current->gid || gid == current->egid ||
1382 gid == current->sgid || gid == current->fsgid ||
756184b7
CP
1383 capable(CAP_SETGID)) {
1384 if (gid != old_fsgid) {
6c5d5238 1385 set_dumpable(current->mm, suid_dumpable);
d59dd462 1386 smp_wmb();
1da177e4
LT
1387 }
1388 current->fsgid = gid;
1389 key_fsgid_changed(current);
9f46080c 1390 proc_id_connector(current, PROC_EVENT_GID);
1da177e4
LT
1391 }
1392 return old_fsgid;
1393}
1394
1395asmlinkage long sys_times(struct tms __user * tbuf)
1396{
1397 /*
1398 * In the SMP world we might just be unlucky and have one of
1399 * the times increment as we use it. Since the value is an
1400 * atomically safe type this is just fine. Conceptually its
1401 * as if the syscall took an instant longer to occur.
1402 */
1403 if (tbuf) {
1404 struct tms tmp;
35f5cad8
ON
1405 struct task_struct *tsk = current;
1406 struct task_struct *t;
1da177e4
LT
1407 cputime_t utime, stime, cutime, cstime;
1408
7d7185c8 1409 spin_lock_irq(&tsk->sighand->siglock);
35f5cad8
ON
1410 utime = tsk->signal->utime;
1411 stime = tsk->signal->stime;
1412 t = tsk;
1413 do {
1414 utime = cputime_add(utime, t->utime);
1415 stime = cputime_add(stime, t->stime);
1416 t = next_thread(t);
1417 } while (t != tsk);
1418
35f5cad8
ON
1419 cutime = tsk->signal->cutime;
1420 cstime = tsk->signal->cstime;
1421 spin_unlock_irq(&tsk->sighand->siglock);
1da177e4
LT
1422
1423 tmp.tms_utime = cputime_to_clock_t(utime);
1424 tmp.tms_stime = cputime_to_clock_t(stime);
1425 tmp.tms_cutime = cputime_to_clock_t(cutime);
1426 tmp.tms_cstime = cputime_to_clock_t(cstime);
1427 if (copy_to_user(tbuf, &tmp, sizeof(struct tms)))
1428 return -EFAULT;
1429 }
1430 return (long) jiffies_64_to_clock_t(get_jiffies_64());
1431}
1432
1433/*
1434 * This needs some heavy checking ...
1435 * I just haven't the stomach for it. I also don't fully
1436 * understand sessions/pgrp etc. Let somebody who does explain it.
1437 *
1438 * OK, I think I have the protection semantics right.... this is really
1439 * only important on a multi-user system anyway, to make sure one user
1440 * can't send a signal to a process owned by another. -TYT, 12/12/91
1441 *
1442 * Auch. Had to add the 'did_exec' flag to conform completely to POSIX.
1443 * LBT 04.03.94
1444 */
1da177e4
LT
1445asmlinkage long sys_setpgid(pid_t pid, pid_t pgid)
1446{
1447 struct task_struct *p;
ee0acf90 1448 struct task_struct *group_leader = current->group_leader;
1da177e4
LT
1449 int err = -EINVAL;
1450
1451 if (!pid)
ee0acf90 1452 pid = group_leader->pid;
1da177e4
LT
1453 if (!pgid)
1454 pgid = pid;
1455 if (pgid < 0)
1456 return -EINVAL;
1457
1458 /* From this point forward we keep holding onto the tasklist lock
1459 * so that our parent does not change from under us. -DaveM
1460 */
1461 write_lock_irq(&tasklist_lock);
1462
1463 err = -ESRCH;
1464 p = find_task_by_pid(pid);
1465 if (!p)
1466 goto out;
1467
1468 err = -EINVAL;
1469 if (!thread_group_leader(p))
1470 goto out;
1471
b07e35f9 1472 if (p->real_parent->tgid == group_leader->tgid) {
1da177e4 1473 err = -EPERM;
41487c65 1474 if (task_session(p) != task_session(group_leader))
1da177e4
LT
1475 goto out;
1476 err = -EACCES;
1477 if (p->did_exec)
1478 goto out;
1479 } else {
1480 err = -ESRCH;
ee0acf90 1481 if (p != group_leader)
1da177e4
LT
1482 goto out;
1483 }
1484
1485 err = -EPERM;
1486 if (p->signal->leader)
1487 goto out;
1488
1489 if (pgid != pid) {
f020bc46
ON
1490 struct task_struct *g =
1491 find_task_by_pid_type(PIDTYPE_PGID, pgid);
1da177e4 1492
41487c65 1493 if (!g || task_session(g) != task_session(group_leader))
f020bc46 1494 goto out;
1da177e4
LT
1495 }
1496
1da177e4
LT
1497 err = security_task_setpgid(p, pgid);
1498 if (err)
1499 goto out;
1500
1501 if (process_group(p) != pgid) {
1502 detach_pid(p, PIDTYPE_PGID);
1503 p->signal->pgrp = pgid;
e713d0da 1504 attach_pid(p, PIDTYPE_PGID, find_pid(pgid));
1da177e4
LT
1505 }
1506
1507 err = 0;
1508out:
1509 /* All paths lead to here, thus we are safe. -DaveM */
1510 write_unlock_irq(&tasklist_lock);
1511 return err;
1512}
1513
1514asmlinkage long sys_getpgid(pid_t pid)
1515{
756184b7 1516 if (!pid)
1da177e4 1517 return process_group(current);
756184b7 1518 else {
1da177e4
LT
1519 int retval;
1520 struct task_struct *p;
1521
1522 read_lock(&tasklist_lock);
1523 p = find_task_by_pid(pid);
1524
1525 retval = -ESRCH;
1526 if (p) {
1527 retval = security_task_getpgid(p);
1528 if (!retval)
1529 retval = process_group(p);
1530 }
1531 read_unlock(&tasklist_lock);
1532 return retval;
1533 }
1534}
1535
1536#ifdef __ARCH_WANT_SYS_GETPGRP
1537
1538asmlinkage long sys_getpgrp(void)
1539{
1540 /* SMP - assuming writes are word atomic this is fine */
1541 return process_group(current);
1542}
1543
1544#endif
1545
1546asmlinkage long sys_getsid(pid_t pid)
1547{
756184b7 1548 if (!pid)
937949d9 1549 return process_session(current);
756184b7 1550 else {
1da177e4
LT
1551 int retval;
1552 struct task_struct *p;
1553
1554 read_lock(&tasklist_lock);
1555 p = find_task_by_pid(pid);
1556
1557 retval = -ESRCH;
756184b7 1558 if (p) {
1da177e4
LT
1559 retval = security_task_getsid(p);
1560 if (!retval)
937949d9 1561 retval = process_session(p);
1da177e4
LT
1562 }
1563 read_unlock(&tasklist_lock);
1564 return retval;
1565 }
1566}
1567
1568asmlinkage long sys_setsid(void)
1569{
e19f247a 1570 struct task_struct *group_leader = current->group_leader;
390e2ff0 1571 pid_t session;
1da177e4
LT
1572 int err = -EPERM;
1573
1da177e4
LT
1574 write_lock_irq(&tasklist_lock);
1575
390e2ff0
EB
1576 /* Fail if I am already a session leader */
1577 if (group_leader->signal->leader)
1578 goto out;
1579
1580 session = group_leader->pid;
1581 /* Fail if a process group id already exists that equals the
1582 * proposed session id.
1583 *
1584 * Don't check if session id == 1 because kernel threads use this
1585 * session id and so the check will always fail and make it so
1586 * init cannot successfully call setsid.
1587 */
1588 if (session > 1 && find_task_by_pid_type(PIDTYPE_PGID, session))
1da177e4
LT
1589 goto out;
1590
e19f247a 1591 group_leader->signal->leader = 1;
390e2ff0 1592 __set_special_pids(session, session);
24ec839c
PZ
1593
1594 spin_lock(&group_leader->sighand->siglock);
e19f247a 1595 group_leader->signal->tty = NULL;
24ec839c
PZ
1596 spin_unlock(&group_leader->sighand->siglock);
1597
e19f247a 1598 err = process_group(group_leader);
1da177e4
LT
1599out:
1600 write_unlock_irq(&tasklist_lock);
1da177e4
LT
1601 return err;
1602}
1603
1604/*
1605 * Supplementary group IDs
1606 */
1607
1608/* init to 2 - one for init_task, one to ensure it is never freed */
1609struct group_info init_groups = { .usage = ATOMIC_INIT(2) };
1610
1611struct group_info *groups_alloc(int gidsetsize)
1612{
1613 struct group_info *group_info;
1614 int nblocks;
1615 int i;
1616
1617 nblocks = (gidsetsize + NGROUPS_PER_BLOCK - 1) / NGROUPS_PER_BLOCK;
1618 /* Make sure we always allocate at least one indirect block pointer */
1619 nblocks = nblocks ? : 1;
1620 group_info = kmalloc(sizeof(*group_info) + nblocks*sizeof(gid_t *), GFP_USER);
1621 if (!group_info)
1622 return NULL;
1623 group_info->ngroups = gidsetsize;
1624 group_info->nblocks = nblocks;
1625 atomic_set(&group_info->usage, 1);
1626
756184b7 1627 if (gidsetsize <= NGROUPS_SMALL)
1da177e4 1628 group_info->blocks[0] = group_info->small_block;
756184b7 1629 else {
1da177e4
LT
1630 for (i = 0; i < nblocks; i++) {
1631 gid_t *b;
1632 b = (void *)__get_free_page(GFP_USER);
1633 if (!b)
1634 goto out_undo_partial_alloc;
1635 group_info->blocks[i] = b;
1636 }
1637 }
1638 return group_info;
1639
1640out_undo_partial_alloc:
1641 while (--i >= 0) {
1642 free_page((unsigned long)group_info->blocks[i]);
1643 }
1644 kfree(group_info);
1645 return NULL;
1646}
1647
1648EXPORT_SYMBOL(groups_alloc);
1649
1650void groups_free(struct group_info *group_info)
1651{
1652 if (group_info->blocks[0] != group_info->small_block) {
1653 int i;
1654 for (i = 0; i < group_info->nblocks; i++)
1655 free_page((unsigned long)group_info->blocks[i]);
1656 }
1657 kfree(group_info);
1658}
1659
1660EXPORT_SYMBOL(groups_free);
1661
1662/* export the group_info to a user-space array */
1663static int groups_to_user(gid_t __user *grouplist,
1664 struct group_info *group_info)
1665{
1666 int i;
1667 int count = group_info->ngroups;
1668
1669 for (i = 0; i < group_info->nblocks; i++) {
1670 int cp_count = min(NGROUPS_PER_BLOCK, count);
1671 int off = i * NGROUPS_PER_BLOCK;
1672 int len = cp_count * sizeof(*grouplist);
1673
1674 if (copy_to_user(grouplist+off, group_info->blocks[i], len))
1675 return -EFAULT;
1676
1677 count -= cp_count;
1678 }
1679 return 0;
1680}
1681
1682/* fill a group_info from a user-space array - it must be allocated already */
1683static int groups_from_user(struct group_info *group_info,
1684 gid_t __user *grouplist)
756184b7 1685{
1da177e4
LT
1686 int i;
1687 int count = group_info->ngroups;
1688
1689 for (i = 0; i < group_info->nblocks; i++) {
1690 int cp_count = min(NGROUPS_PER_BLOCK, count);
1691 int off = i * NGROUPS_PER_BLOCK;
1692 int len = cp_count * sizeof(*grouplist);
1693
1694 if (copy_from_user(group_info->blocks[i], grouplist+off, len))
1695 return -EFAULT;
1696
1697 count -= cp_count;
1698 }
1699 return 0;
1700}
1701
ebe8b541 1702/* a simple Shell sort */
1da177e4
LT
1703static void groups_sort(struct group_info *group_info)
1704{
1705 int base, max, stride;
1706 int gidsetsize = group_info->ngroups;
1707
1708 for (stride = 1; stride < gidsetsize; stride = 3 * stride + 1)
1709 ; /* nothing */
1710 stride /= 3;
1711
1712 while (stride) {
1713 max = gidsetsize - stride;
1714 for (base = 0; base < max; base++) {
1715 int left = base;
1716 int right = left + stride;
1717 gid_t tmp = GROUP_AT(group_info, right);
1718
1719 while (left >= 0 && GROUP_AT(group_info, left) > tmp) {
1720 GROUP_AT(group_info, right) =
1721 GROUP_AT(group_info, left);
1722 right = left;
1723 left -= stride;
1724 }
1725 GROUP_AT(group_info, right) = tmp;
1726 }
1727 stride /= 3;
1728 }
1729}
1730
1731/* a simple bsearch */
3e30148c 1732int groups_search(struct group_info *group_info, gid_t grp)
1da177e4 1733{
d74beb9f 1734 unsigned int left, right;
1da177e4
LT
1735
1736 if (!group_info)
1737 return 0;
1738
1739 left = 0;
1740 right = group_info->ngroups;
1741 while (left < right) {
d74beb9f 1742 unsigned int mid = (left+right)/2;
1da177e4
LT
1743 int cmp = grp - GROUP_AT(group_info, mid);
1744 if (cmp > 0)
1745 left = mid + 1;
1746 else if (cmp < 0)
1747 right = mid;
1748 else
1749 return 1;
1750 }
1751 return 0;
1752}
1753
1754/* validate and set current->group_info */
1755int set_current_groups(struct group_info *group_info)
1756{
1757 int retval;
1758 struct group_info *old_info;
1759
1760 retval = security_task_setgroups(group_info);
1761 if (retval)
1762 return retval;
1763
1764 groups_sort(group_info);
1765 get_group_info(group_info);
1766
1767 task_lock(current);
1768 old_info = current->group_info;
1769 current->group_info = group_info;
1770 task_unlock(current);
1771
1772 put_group_info(old_info);
1773
1774 return 0;
1775}
1776
1777EXPORT_SYMBOL(set_current_groups);
1778
1779asmlinkage long sys_getgroups(int gidsetsize, gid_t __user *grouplist)
1780{
1781 int i = 0;
1782
1783 /*
1784 * SMP: Nobody else can change our grouplist. Thus we are
1785 * safe.
1786 */
1787
1788 if (gidsetsize < 0)
1789 return -EINVAL;
1790
1791 /* no need to grab task_lock here; it cannot change */
1da177e4
LT
1792 i = current->group_info->ngroups;
1793 if (gidsetsize) {
1794 if (i > gidsetsize) {
1795 i = -EINVAL;
1796 goto out;
1797 }
1798 if (groups_to_user(grouplist, current->group_info)) {
1799 i = -EFAULT;
1800 goto out;
1801 }
1802 }
1803out:
1da177e4
LT
1804 return i;
1805}
1806
1807/*
1808 * SMP: Our groups are copy-on-write. We can set them safely
1809 * without another task interfering.
1810 */
1811
1812asmlinkage long sys_setgroups(int gidsetsize, gid_t __user *grouplist)
1813{
1814 struct group_info *group_info;
1815 int retval;
1816
1817 if (!capable(CAP_SETGID))
1818 return -EPERM;
1819 if ((unsigned)gidsetsize > NGROUPS_MAX)
1820 return -EINVAL;
1821
1822 group_info = groups_alloc(gidsetsize);
1823 if (!group_info)
1824 return -ENOMEM;
1825 retval = groups_from_user(group_info, grouplist);
1826 if (retval) {
1827 put_group_info(group_info);
1828 return retval;
1829 }
1830
1831 retval = set_current_groups(group_info);
1832 put_group_info(group_info);
1833
1834 return retval;
1835}
1836
1837/*
1838 * Check whether we're fsgid/egid or in the supplemental group..
1839 */
1840int in_group_p(gid_t grp)
1841{
1842 int retval = 1;
756184b7 1843 if (grp != current->fsgid)
1da177e4 1844 retval = groups_search(current->group_info, grp);
1da177e4
LT
1845 return retval;
1846}
1847
1848EXPORT_SYMBOL(in_group_p);
1849
1850int in_egroup_p(gid_t grp)
1851{
1852 int retval = 1;
756184b7 1853 if (grp != current->egid)
1da177e4 1854 retval = groups_search(current->group_info, grp);
1da177e4
LT
1855 return retval;
1856}
1857
1858EXPORT_SYMBOL(in_egroup_p);
1859
1860DECLARE_RWSEM(uts_sem);
1861
393b0725
DM
1862EXPORT_SYMBOL(uts_sem);
1863
1da177e4
LT
1864asmlinkage long sys_newuname(struct new_utsname __user * name)
1865{
1866 int errno = 0;
1867
1868 down_read(&uts_sem);
e9ff3990 1869 if (copy_to_user(name, utsname(), sizeof *name))
1da177e4
LT
1870 errno = -EFAULT;
1871 up_read(&uts_sem);
1872 return errno;
1873}
1874
1875asmlinkage long sys_sethostname(char __user *name, int len)
1876{
1877 int errno;
1878 char tmp[__NEW_UTS_LEN];
1879
1880 if (!capable(CAP_SYS_ADMIN))
1881 return -EPERM;
1882 if (len < 0 || len > __NEW_UTS_LEN)
1883 return -EINVAL;
1884 down_write(&uts_sem);
1885 errno = -EFAULT;
1886 if (!copy_from_user(tmp, name, len)) {
e9ff3990
SH
1887 memcpy(utsname()->nodename, tmp, len);
1888 utsname()->nodename[len] = 0;
1da177e4
LT
1889 errno = 0;
1890 }
1891 up_write(&uts_sem);
1892 return errno;
1893}
1894
1895#ifdef __ARCH_WANT_SYS_GETHOSTNAME
1896
1897asmlinkage long sys_gethostname(char __user *name, int len)
1898{
1899 int i, errno;
1900
1901 if (len < 0)
1902 return -EINVAL;
1903 down_read(&uts_sem);
e9ff3990 1904 i = 1 + strlen(utsname()->nodename);
1da177e4
LT
1905 if (i > len)
1906 i = len;
1907 errno = 0;
e9ff3990 1908 if (copy_to_user(name, utsname()->nodename, i))
1da177e4
LT
1909 errno = -EFAULT;
1910 up_read(&uts_sem);
1911 return errno;
1912}
1913
1914#endif
1915
1916/*
1917 * Only setdomainname; getdomainname can be implemented by calling
1918 * uname()
1919 */
1920asmlinkage long sys_setdomainname(char __user *name, int len)
1921{
1922 int errno;
1923 char tmp[__NEW_UTS_LEN];
1924
1925 if (!capable(CAP_SYS_ADMIN))
1926 return -EPERM;
1927 if (len < 0 || len > __NEW_UTS_LEN)
1928 return -EINVAL;
1929
1930 down_write(&uts_sem);
1931 errno = -EFAULT;
1932 if (!copy_from_user(tmp, name, len)) {
e9ff3990
SH
1933 memcpy(utsname()->domainname, tmp, len);
1934 utsname()->domainname[len] = 0;
1da177e4
LT
1935 errno = 0;
1936 }
1937 up_write(&uts_sem);
1938 return errno;
1939}
1940
1941asmlinkage long sys_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1942{
1943 if (resource >= RLIM_NLIMITS)
1944 return -EINVAL;
1945 else {
1946 struct rlimit value;
1947 task_lock(current->group_leader);
1948 value = current->signal->rlim[resource];
1949 task_unlock(current->group_leader);
1950 return copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0;
1951 }
1952}
1953
1954#ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT
1955
1956/*
1957 * Back compatibility for getrlimit. Needed for some apps.
1958 */
1959
1960asmlinkage long sys_old_getrlimit(unsigned int resource, struct rlimit __user *rlim)
1961{
1962 struct rlimit x;
1963 if (resource >= RLIM_NLIMITS)
1964 return -EINVAL;
1965
1966 task_lock(current->group_leader);
1967 x = current->signal->rlim[resource];
1968 task_unlock(current->group_leader);
756184b7 1969 if (x.rlim_cur > 0x7FFFFFFF)
1da177e4 1970 x.rlim_cur = 0x7FFFFFFF;
756184b7 1971 if (x.rlim_max > 0x7FFFFFFF)
1da177e4
LT
1972 x.rlim_max = 0x7FFFFFFF;
1973 return copy_to_user(rlim, &x, sizeof(x))?-EFAULT:0;
1974}
1975
1976#endif
1977
1978asmlinkage long sys_setrlimit(unsigned int resource, struct rlimit __user *rlim)
1979{
1980 struct rlimit new_rlim, *old_rlim;
ec9e16ba 1981 unsigned long it_prof_secs;
1da177e4
LT
1982 int retval;
1983
1984 if (resource >= RLIM_NLIMITS)
1985 return -EINVAL;
ec9e16ba 1986 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim)))
1da177e4 1987 return -EFAULT;
ec9e16ba
AM
1988 if (new_rlim.rlim_cur > new_rlim.rlim_max)
1989 return -EINVAL;
1da177e4
LT
1990 old_rlim = current->signal->rlim + resource;
1991 if ((new_rlim.rlim_max > old_rlim->rlim_max) &&
1992 !capable(CAP_SYS_RESOURCE))
1993 return -EPERM;
1994 if (resource == RLIMIT_NOFILE && new_rlim.rlim_max > NR_OPEN)
ec9e16ba 1995 return -EPERM;
1da177e4
LT
1996
1997 retval = security_task_setrlimit(resource, &new_rlim);
1998 if (retval)
1999 return retval;
2000
9926e4c7
TA
2001 if (resource == RLIMIT_CPU && new_rlim.rlim_cur == 0) {
2002 /*
2003 * The caller is asking for an immediate RLIMIT_CPU
2004 * expiry. But we use the zero value to mean "it was
2005 * never set". So let's cheat and make it one second
2006 * instead
2007 */
2008 new_rlim.rlim_cur = 1;
2009 }
2010
1da177e4
LT
2011 task_lock(current->group_leader);
2012 *old_rlim = new_rlim;
2013 task_unlock(current->group_leader);
2014
ec9e16ba
AM
2015 if (resource != RLIMIT_CPU)
2016 goto out;
d3561f78
AM
2017
2018 /*
2019 * RLIMIT_CPU handling. Note that the kernel fails to return an error
2020 * code if it rejected the user's attempt to set RLIMIT_CPU. This is a
2021 * very long-standing error, and fixing it now risks breakage of
2022 * applications, so we live with it
2023 */
ec9e16ba
AM
2024 if (new_rlim.rlim_cur == RLIM_INFINITY)
2025 goto out;
2026
2027 it_prof_secs = cputime_to_secs(current->signal->it_prof_expires);
2028 if (it_prof_secs == 0 || new_rlim.rlim_cur <= it_prof_secs) {
e0661111
AM
2029 unsigned long rlim_cur = new_rlim.rlim_cur;
2030 cputime_t cputime;
ec9e16ba 2031
e0661111 2032 cputime = secs_to_cputime(rlim_cur);
1da177e4
LT
2033 read_lock(&tasklist_lock);
2034 spin_lock_irq(&current->sighand->siglock);
ec9e16ba 2035 set_process_cpu_timer(current, CPUCLOCK_PROF, &cputime, NULL);
1da177e4
LT
2036 spin_unlock_irq(&current->sighand->siglock);
2037 read_unlock(&tasklist_lock);
2038 }
ec9e16ba 2039out:
1da177e4
LT
2040 return 0;
2041}
2042
2043/*
2044 * It would make sense to put struct rusage in the task_struct,
2045 * except that would make the task_struct be *really big*. After
2046 * task_struct gets moved into malloc'ed memory, it would
2047 * make sense to do this. It will make moving the rest of the information
2048 * a lot simpler! (Which we're not doing right now because we're not
2049 * measuring them yet).
2050 *
1da177e4
LT
2051 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have
2052 * races with threads incrementing their own counters. But since word
2053 * reads are atomic, we either get new values or old values and we don't
2054 * care which for the sums. We always take the siglock to protect reading
2055 * the c* fields from p->signal from races with exit.c updating those
2056 * fields when reaping, so a sample either gets all the additions of a
2057 * given child after it's reaped, or none so this sample is before reaping.
2dd0ebcd 2058 *
de047c1b
RT
2059 * Locking:
2060 * We need to take the siglock for CHILDEREN, SELF and BOTH
2061 * for the cases current multithreaded, non-current single threaded
2062 * non-current multithreaded. Thread traversal is now safe with
2063 * the siglock held.
2064 * Strictly speaking, we donot need to take the siglock if we are current and
2065 * single threaded, as no one else can take our signal_struct away, no one
2066 * else can reap the children to update signal->c* counters, and no one else
2067 * can race with the signal-> fields. If we do not take any lock, the
2068 * signal-> fields could be read out of order while another thread was just
2069 * exiting. So we should place a read memory barrier when we avoid the lock.
2070 * On the writer side, write memory barrier is implied in __exit_signal
2071 * as __exit_signal releases the siglock spinlock after updating the signal->
2072 * fields. But we don't do this yet to keep things simple.
2dd0ebcd 2073 *
1da177e4
LT
2074 */
2075
2076static void k_getrusage(struct task_struct *p, int who, struct rusage *r)
2077{
2078 struct task_struct *t;
2079 unsigned long flags;
2080 cputime_t utime, stime;
2081
2082 memset((char *) r, 0, sizeof *r);
2dd0ebcd 2083 utime = stime = cputime_zero;
1da177e4 2084
de047c1b
RT
2085 rcu_read_lock();
2086 if (!lock_task_sighand(p, &flags)) {
2087 rcu_read_unlock();
2088 return;
2089 }
0f59cc4a 2090
1da177e4 2091 switch (who) {
0f59cc4a 2092 case RUSAGE_BOTH:
1da177e4 2093 case RUSAGE_CHILDREN:
1da177e4
LT
2094 utime = p->signal->cutime;
2095 stime = p->signal->cstime;
2096 r->ru_nvcsw = p->signal->cnvcsw;
2097 r->ru_nivcsw = p->signal->cnivcsw;
2098 r->ru_minflt = p->signal->cmin_flt;
2099 r->ru_majflt = p->signal->cmaj_flt;
6eaeeaba
ED
2100 r->ru_inblock = p->signal->cinblock;
2101 r->ru_oublock = p->signal->coublock;
0f59cc4a
ON
2102
2103 if (who == RUSAGE_CHILDREN)
2104 break;
2105
1da177e4 2106 case RUSAGE_SELF:
1da177e4
LT
2107 utime = cputime_add(utime, p->signal->utime);
2108 stime = cputime_add(stime, p->signal->stime);
2109 r->ru_nvcsw += p->signal->nvcsw;
2110 r->ru_nivcsw += p->signal->nivcsw;
2111 r->ru_minflt += p->signal->min_flt;
2112 r->ru_majflt += p->signal->maj_flt;
6eaeeaba
ED
2113 r->ru_inblock += p->signal->inblock;
2114 r->ru_oublock += p->signal->oublock;
1da177e4
LT
2115 t = p;
2116 do {
2117 utime = cputime_add(utime, t->utime);
2118 stime = cputime_add(stime, t->stime);
2119 r->ru_nvcsw += t->nvcsw;
2120 r->ru_nivcsw += t->nivcsw;
2121 r->ru_minflt += t->min_flt;
2122 r->ru_majflt += t->maj_flt;
6eaeeaba
ED
2123 r->ru_inblock += task_io_get_inblock(t);
2124 r->ru_oublock += task_io_get_oublock(t);
1da177e4
LT
2125 t = next_thread(t);
2126 } while (t != p);
1da177e4 2127 break;
0f59cc4a 2128
1da177e4
LT
2129 default:
2130 BUG();
2131 }
0f59cc4a 2132
de047c1b
RT
2133 unlock_task_sighand(p, &flags);
2134 rcu_read_unlock();
2135
0f59cc4a
ON
2136 cputime_to_timeval(utime, &r->ru_utime);
2137 cputime_to_timeval(stime, &r->ru_stime);
1da177e4
LT
2138}
2139
2140int getrusage(struct task_struct *p, int who, struct rusage __user *ru)
2141{
2142 struct rusage r;
1da177e4 2143 k_getrusage(p, who, &r);
1da177e4
LT
2144 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0;
2145}
2146
2147asmlinkage long sys_getrusage(int who, struct rusage __user *ru)
2148{
2149 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN)
2150 return -EINVAL;
2151 return getrusage(current, who, ru);
2152}
2153
2154asmlinkage long sys_umask(int mask)
2155{
2156 mask = xchg(&current->fs->umask, mask & S_IRWXUGO);
2157 return mask;
2158}
2159
2160asmlinkage long sys_prctl(int option, unsigned long arg2, unsigned long arg3,
2161 unsigned long arg4, unsigned long arg5)
2162{
2163 long error;
1da177e4
LT
2164
2165 error = security_task_prctl(option, arg2, arg3, arg4, arg5);
2166 if (error)
2167 return error;
2168
2169 switch (option) {
2170 case PR_SET_PDEATHSIG:
0730ded5 2171 if (!valid_signal(arg2)) {
1da177e4
LT
2172 error = -EINVAL;
2173 break;
2174 }
0730ded5 2175 current->pdeath_signal = arg2;
1da177e4
LT
2176 break;
2177 case PR_GET_PDEATHSIG:
2178 error = put_user(current->pdeath_signal, (int __user *)arg2);
2179 break;
2180 case PR_GET_DUMPABLE:
6c5d5238 2181 error = get_dumpable(current->mm);
1da177e4
LT
2182 break;
2183 case PR_SET_DUMPABLE:
abf75a50 2184 if (arg2 < 0 || arg2 > 1) {
1da177e4
LT
2185 error = -EINVAL;
2186 break;
2187 }
6c5d5238 2188 set_dumpable(current->mm, arg2);
1da177e4
LT
2189 break;
2190
2191 case PR_SET_UNALIGN:
2192 error = SET_UNALIGN_CTL(current, arg2);
2193 break;
2194 case PR_GET_UNALIGN:
2195 error = GET_UNALIGN_CTL(current, arg2);
2196 break;
2197 case PR_SET_FPEMU:
2198 error = SET_FPEMU_CTL(current, arg2);
2199 break;
2200 case PR_GET_FPEMU:
2201 error = GET_FPEMU_CTL(current, arg2);
2202 break;
2203 case PR_SET_FPEXC:
2204 error = SET_FPEXC_CTL(current, arg2);
2205 break;
2206 case PR_GET_FPEXC:
2207 error = GET_FPEXC_CTL(current, arg2);
2208 break;
2209 case PR_GET_TIMING:
2210 error = PR_TIMING_STATISTICAL;
2211 break;
2212 case PR_SET_TIMING:
2213 if (arg2 == PR_TIMING_STATISTICAL)
2214 error = 0;
2215 else
2216 error = -EINVAL;
2217 break;
2218
2219 case PR_GET_KEEPCAPS:
2220 if (current->keep_capabilities)
2221 error = 1;
2222 break;
2223 case PR_SET_KEEPCAPS:
2224 if (arg2 != 0 && arg2 != 1) {
2225 error = -EINVAL;
2226 break;
2227 }
2228 current->keep_capabilities = arg2;
2229 break;
2230 case PR_SET_NAME: {
2231 struct task_struct *me = current;
2232 unsigned char ncomm[sizeof(me->comm)];
2233
2234 ncomm[sizeof(me->comm)-1] = 0;
2235 if (strncpy_from_user(ncomm, (char __user *)arg2,
2236 sizeof(me->comm)-1) < 0)
2237 return -EFAULT;
2238 set_task_comm(me, ncomm);
2239 return 0;
2240 }
2241 case PR_GET_NAME: {
2242 struct task_struct *me = current;
2243 unsigned char tcomm[sizeof(me->comm)];
2244
2245 get_task_comm(tcomm, me);
2246 if (copy_to_user((char __user *)arg2, tcomm, sizeof(tcomm)))
2247 return -EFAULT;
2248 return 0;
2249 }
651d765d
AB
2250 case PR_GET_ENDIAN:
2251 error = GET_ENDIAN(current, arg2);
2252 break;
2253 case PR_SET_ENDIAN:
2254 error = SET_ENDIAN(current, arg2);
2255 break;
2256
1d9d02fe
AA
2257 case PR_GET_SECCOMP:
2258 error = prctl_get_seccomp();
2259 break;
2260 case PR_SET_SECCOMP:
2261 error = prctl_set_seccomp(arg2);
2262 break;
2263
1da177e4
LT
2264 default:
2265 error = -EINVAL;
2266 break;
2267 }
2268 return error;
2269}
3cfc348b
AK
2270
2271asmlinkage long sys_getcpu(unsigned __user *cpup, unsigned __user *nodep,
2272 struct getcpu_cache __user *cache)
2273{
2274 int err = 0;
2275 int cpu = raw_smp_processor_id();
2276 if (cpup)
2277 err |= put_user(cpu, cpup);
2278 if (nodep)
2279 err |= put_user(cpu_to_node(cpu), nodep);
2280 if (cache) {
2281 /*
2282 * The cache is not needed for this implementation,
2283 * but make sure user programs pass something
2284 * valid. vsyscall implementations can instead make
2285 * good use of the cache. Only use t0 and t1 because
2286 * these are available in both 32bit and 64bit ABI (no
2287 * need for a compat_getcpu). 32bit has enough
2288 * padding
2289 */
2290 unsigned long t0, t1;
34596dc9
AK
2291 get_user(t0, &cache->blob[0]);
2292 get_user(t1, &cache->blob[1]);
3cfc348b
AK
2293 t0++;
2294 t1++;
34596dc9
AK
2295 put_user(t0, &cache->blob[0]);
2296 put_user(t1, &cache->blob[1]);
3cfc348b
AK
2297 }
2298 return err ? -EFAULT : 0;
2299}
10a0a8d4
JF
2300
2301char poweroff_cmd[POWEROFF_CMD_PATH_LEN] = "/sbin/poweroff";
2302
2303static void argv_cleanup(char **argv, char **envp)
2304{
2305 argv_free(argv);
2306}
2307
2308/**
2309 * orderly_poweroff - Trigger an orderly system poweroff
2310 * @force: force poweroff if command execution fails
2311 *
2312 * This may be called from any context to trigger a system shutdown.
2313 * If the orderly shutdown fails, it will force an immediate shutdown.
2314 */
2315int orderly_poweroff(bool force)
2316{
2317 int argc;
2318 char **argv = argv_split(GFP_ATOMIC, poweroff_cmd, &argc);
2319 static char *envp[] = {
2320 "HOME=/",
2321 "PATH=/sbin:/bin:/usr/sbin:/usr/bin",
2322 NULL
2323 };
2324 int ret = -ENOMEM;
2325 struct subprocess_info *info;
2326
2327 if (argv == NULL) {
2328 printk(KERN_WARNING "%s failed to allocate memory for \"%s\"\n",
2329 __func__, poweroff_cmd);
2330 goto out;
2331 }
2332
2333 info = call_usermodehelper_setup(argv[0], argv, envp);
2334 if (info == NULL) {
2335 argv_free(argv);
2336 goto out;
2337 }
2338
2339 call_usermodehelper_setcleanup(info, argv_cleanup);
2340
86313c48 2341 ret = call_usermodehelper_exec(info, UMH_NO_WAIT);
10a0a8d4
JF
2342
2343 out:
2344 if (ret && force) {
2345 printk(KERN_WARNING "Failed to start orderly shutdown: "
2346 "forcing the issue\n");
2347
2348 /* I guess this should try to kick off some daemon to
2349 sync and poweroff asap. Or not even bother syncing
2350 if we're doing an emergency shutdown? */
2351 emergency_sync();
2352 kernel_power_off();
2353 }
2354
2355 return ret;
2356}
2357EXPORT_SYMBOL_GPL(orderly_poweroff);