]>
Commit | Line | Data |
---|---|---|
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> | |
dc009d92 EB |
17 | #include <linux/kernel.h> |
18 | #include <linux/kexec.h> | |
1da177e4 | 19 | #include <linux/workqueue.h> |
c59ede7b | 20 | #include <linux/capability.h> |
1da177e4 LT |
21 | #include <linux/device.h> |
22 | #include <linux/key.h> | |
23 | #include <linux/times.h> | |
24 | #include <linux/posix-timers.h> | |
25 | #include <linux/security.h> | |
26 | #include <linux/dcookies.h> | |
27 | #include <linux/suspend.h> | |
28 | #include <linux/tty.h> | |
7ed20e1a | 29 | #include <linux/signal.h> |
9f46080c | 30 | #include <linux/cn_proc.h> |
3cfc348b | 31 | #include <linux/getcpu.h> |
1da177e4 LT |
32 | |
33 | #include <linux/compat.h> | |
34 | #include <linux/syscalls.h> | |
00d7c05a | 35 | #include <linux/kprobes.h> |
1da177e4 LT |
36 | |
37 | #include <asm/uaccess.h> | |
38 | #include <asm/io.h> | |
39 | #include <asm/unistd.h> | |
40 | ||
41 | #ifndef SET_UNALIGN_CTL | |
42 | # define SET_UNALIGN_CTL(a,b) (-EINVAL) | |
43 | #endif | |
44 | #ifndef GET_UNALIGN_CTL | |
45 | # define GET_UNALIGN_CTL(a,b) (-EINVAL) | |
46 | #endif | |
47 | #ifndef SET_FPEMU_CTL | |
48 | # define SET_FPEMU_CTL(a,b) (-EINVAL) | |
49 | #endif | |
50 | #ifndef GET_FPEMU_CTL | |
51 | # define GET_FPEMU_CTL(a,b) (-EINVAL) | |
52 | #endif | |
53 | #ifndef SET_FPEXC_CTL | |
54 | # define SET_FPEXC_CTL(a,b) (-EINVAL) | |
55 | #endif | |
56 | #ifndef GET_FPEXC_CTL | |
57 | # define GET_FPEXC_CTL(a,b) (-EINVAL) | |
58 | #endif | |
651d765d AB |
59 | #ifndef GET_ENDIAN |
60 | # define GET_ENDIAN(a,b) (-EINVAL) | |
61 | #endif | |
62 | #ifndef SET_ENDIAN | |
63 | # define SET_ENDIAN(a,b) (-EINVAL) | |
64 | #endif | |
1da177e4 LT |
65 | |
66 | /* | |
67 | * this is where the system-wide overflow UID and GID are defined, for | |
68 | * architectures that now have 32-bit UID/GID but didn't in the past | |
69 | */ | |
70 | ||
71 | int overflowuid = DEFAULT_OVERFLOWUID; | |
72 | int overflowgid = DEFAULT_OVERFLOWGID; | |
73 | ||
74 | #ifdef CONFIG_UID16 | |
75 | EXPORT_SYMBOL(overflowuid); | |
76 | EXPORT_SYMBOL(overflowgid); | |
77 | #endif | |
78 | ||
79 | /* | |
80 | * the same as above, but for filesystems which can only store a 16-bit | |
81 | * UID and GID. as such, this is needed on all architectures | |
82 | */ | |
83 | ||
84 | int fs_overflowuid = DEFAULT_FS_OVERFLOWUID; | |
85 | int fs_overflowgid = DEFAULT_FS_OVERFLOWUID; | |
86 | ||
87 | EXPORT_SYMBOL(fs_overflowuid); | |
88 | EXPORT_SYMBOL(fs_overflowgid); | |
89 | ||
90 | /* | |
91 | * this indicates whether you can reboot with ctrl-alt-del: the default is yes | |
92 | */ | |
93 | ||
94 | int C_A_D = 1; | |
9ec52099 CLG |
95 | struct pid *cad_pid; |
96 | EXPORT_SYMBOL(cad_pid); | |
1da177e4 LT |
97 | |
98 | /* | |
99 | * Notifier list for kernel code which wants to be called | |
100 | * at shutdown. This is used to stop any idling DMA operations | |
101 | * and the like. | |
102 | */ | |
103 | ||
e041c683 AS |
104 | static BLOCKING_NOTIFIER_HEAD(reboot_notifier_list); |
105 | ||
106 | /* | |
107 | * Notifier chain core routines. The exported routines below | |
108 | * are layered on top of these, with appropriate locking added. | |
109 | */ | |
110 | ||
111 | static int notifier_chain_register(struct notifier_block **nl, | |
112 | struct notifier_block *n) | |
113 | { | |
114 | while ((*nl) != NULL) { | |
115 | if (n->priority > (*nl)->priority) | |
116 | break; | |
117 | nl = &((*nl)->next); | |
118 | } | |
119 | n->next = *nl; | |
120 | rcu_assign_pointer(*nl, n); | |
121 | return 0; | |
122 | } | |
123 | ||
124 | static int notifier_chain_unregister(struct notifier_block **nl, | |
125 | struct notifier_block *n) | |
126 | { | |
127 | while ((*nl) != NULL) { | |
128 | if ((*nl) == n) { | |
129 | rcu_assign_pointer(*nl, n->next); | |
130 | return 0; | |
131 | } | |
132 | nl = &((*nl)->next); | |
133 | } | |
134 | return -ENOENT; | |
135 | } | |
136 | ||
6f7cc11a GS |
137 | /** |
138 | * notifier_call_chain - Informs the registered notifiers about an event. | |
139 | * @nl: Pointer to head of the blocking notifier chain | |
140 | * @val: Value passed unmodified to notifier function | |
141 | * @v: Pointer passed unmodified to notifier function | |
142 | * @nr_to_call: Number of notifier functions to be called. Don't care | |
143 | * value of this parameter is -1. | |
144 | * @nr_calls: Records the number of notifications sent. Don't care | |
145 | * value of this field is NULL. | |
146 | * @returns: notifier_call_chain returns the value returned by the | |
147 | * last notifier function called. | |
148 | */ | |
149 | ||
e041c683 | 150 | static int __kprobes notifier_call_chain(struct notifier_block **nl, |
6f7cc11a GS |
151 | unsigned long val, void *v, |
152 | int nr_to_call, int *nr_calls) | |
e041c683 AS |
153 | { |
154 | int ret = NOTIFY_DONE; | |
bbb1747d | 155 | struct notifier_block *nb, *next_nb; |
e041c683 AS |
156 | |
157 | nb = rcu_dereference(*nl); | |
6f7cc11a GS |
158 | |
159 | while (nb && nr_to_call) { | |
bbb1747d | 160 | next_nb = rcu_dereference(nb->next); |
e041c683 | 161 | ret = nb->notifier_call(nb, val, v); |
6f7cc11a GS |
162 | |
163 | if (nr_calls) | |
164 | (*nr_calls)++; | |
165 | ||
e041c683 AS |
166 | if ((ret & NOTIFY_STOP_MASK) == NOTIFY_STOP_MASK) |
167 | break; | |
bbb1747d | 168 | nb = next_nb; |
6f7cc11a | 169 | nr_to_call--; |
e041c683 AS |
170 | } |
171 | return ret; | |
172 | } | |
173 | ||
174 | /* | |
175 | * Atomic notifier chain routines. Registration and unregistration | |
eabc0694 | 176 | * use a spinlock, and call_chain is synchronized by RCU (no locks). |
e041c683 | 177 | */ |
1da177e4 LT |
178 | |
179 | /** | |
e041c683 AS |
180 | * atomic_notifier_chain_register - Add notifier to an atomic notifier chain |
181 | * @nh: Pointer to head of the atomic notifier chain | |
1da177e4 LT |
182 | * @n: New entry in notifier chain |
183 | * | |
e041c683 | 184 | * Adds a notifier to an atomic notifier chain. |
1da177e4 LT |
185 | * |
186 | * Currently always returns zero. | |
187 | */ | |
e041c683 AS |
188 | |
189 | int atomic_notifier_chain_register(struct atomic_notifier_head *nh, | |
190 | struct notifier_block *n) | |
191 | { | |
192 | unsigned long flags; | |
193 | int ret; | |
194 | ||
195 | spin_lock_irqsave(&nh->lock, flags); | |
196 | ret = notifier_chain_register(&nh->head, n); | |
197 | spin_unlock_irqrestore(&nh->lock, flags); | |
198 | return ret; | |
199 | } | |
200 | ||
201 | EXPORT_SYMBOL_GPL(atomic_notifier_chain_register); | |
202 | ||
203 | /** | |
204 | * atomic_notifier_chain_unregister - Remove notifier from an atomic notifier chain | |
205 | * @nh: Pointer to head of the atomic notifier chain | |
206 | * @n: Entry to remove from notifier chain | |
207 | * | |
208 | * Removes a notifier from an atomic notifier chain. | |
209 | * | |
210 | * Returns zero on success or %-ENOENT on failure. | |
211 | */ | |
212 | int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh, | |
213 | struct notifier_block *n) | |
214 | { | |
215 | unsigned long flags; | |
216 | int ret; | |
217 | ||
218 | spin_lock_irqsave(&nh->lock, flags); | |
219 | ret = notifier_chain_unregister(&nh->head, n); | |
220 | spin_unlock_irqrestore(&nh->lock, flags); | |
221 | synchronize_rcu(); | |
222 | return ret; | |
223 | } | |
224 | ||
225 | EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister); | |
226 | ||
227 | /** | |
6f7cc11a | 228 | * __atomic_notifier_call_chain - Call functions in an atomic notifier chain |
e041c683 AS |
229 | * @nh: Pointer to head of the atomic notifier chain |
230 | * @val: Value passed unmodified to notifier function | |
231 | * @v: Pointer passed unmodified to notifier function | |
6f7cc11a GS |
232 | * @nr_to_call: See the comment for notifier_call_chain. |
233 | * @nr_calls: See the comment for notifier_call_chain. | |
e041c683 AS |
234 | * |
235 | * Calls each function in a notifier chain in turn. The functions | |
236 | * run in an atomic context, so they must not block. | |
237 | * This routine uses RCU to synchronize with changes to the chain. | |
238 | * | |
239 | * If the return value of the notifier can be and'ed | |
72fd4a35 | 240 | * with %NOTIFY_STOP_MASK then atomic_notifier_call_chain() |
e041c683 AS |
241 | * will return immediately, with the return value of |
242 | * the notifier function which halted execution. | |
243 | * Otherwise the return value is the return value | |
244 | * of the last notifier function called. | |
245 | */ | |
1da177e4 | 246 | |
6f7cc11a GS |
247 | int __kprobes __atomic_notifier_call_chain(struct atomic_notifier_head *nh, |
248 | unsigned long val, void *v, | |
249 | int nr_to_call, int *nr_calls) | |
1da177e4 | 250 | { |
e041c683 AS |
251 | int ret; |
252 | ||
253 | rcu_read_lock(); | |
6f7cc11a | 254 | ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls); |
e041c683 AS |
255 | rcu_read_unlock(); |
256 | return ret; | |
1da177e4 LT |
257 | } |
258 | ||
6f7cc11a GS |
259 | EXPORT_SYMBOL_GPL(__atomic_notifier_call_chain); |
260 | ||
261 | int __kprobes atomic_notifier_call_chain(struct atomic_notifier_head *nh, | |
262 | unsigned long val, void *v) | |
263 | { | |
264 | return __atomic_notifier_call_chain(nh, val, v, -1, NULL); | |
265 | } | |
e041c683 | 266 | |
6f7cc11a | 267 | EXPORT_SYMBOL_GPL(atomic_notifier_call_chain); |
e041c683 AS |
268 | /* |
269 | * Blocking notifier chain routines. All access to the chain is | |
270 | * synchronized by an rwsem. | |
271 | */ | |
1da177e4 LT |
272 | |
273 | /** | |
e041c683 AS |
274 | * blocking_notifier_chain_register - Add notifier to a blocking notifier chain |
275 | * @nh: Pointer to head of the blocking notifier chain | |
1da177e4 LT |
276 | * @n: New entry in notifier chain |
277 | * | |
e041c683 AS |
278 | * Adds a notifier to a blocking notifier chain. |
279 | * Must be called in process context. | |
1da177e4 | 280 | * |
e041c683 | 281 | * Currently always returns zero. |
1da177e4 LT |
282 | */ |
283 | ||
e041c683 AS |
284 | int blocking_notifier_chain_register(struct blocking_notifier_head *nh, |
285 | struct notifier_block *n) | |
1da177e4 | 286 | { |
e041c683 AS |
287 | int ret; |
288 | ||
289 | /* | |
290 | * This code gets used during boot-up, when task switching is | |
291 | * not yet working and interrupts must remain disabled. At | |
292 | * such times we must not call down_write(). | |
293 | */ | |
294 | if (unlikely(system_state == SYSTEM_BOOTING)) | |
295 | return notifier_chain_register(&nh->head, n); | |
296 | ||
297 | down_write(&nh->rwsem); | |
298 | ret = notifier_chain_register(&nh->head, n); | |
299 | up_write(&nh->rwsem); | |
300 | return ret; | |
1da177e4 LT |
301 | } |
302 | ||
e041c683 | 303 | EXPORT_SYMBOL_GPL(blocking_notifier_chain_register); |
1da177e4 LT |
304 | |
305 | /** | |
e041c683 AS |
306 | * blocking_notifier_chain_unregister - Remove notifier from a blocking notifier chain |
307 | * @nh: Pointer to head of the blocking notifier chain | |
308 | * @n: Entry to remove from notifier chain | |
309 | * | |
310 | * Removes a notifier from a blocking notifier chain. | |
311 | * Must be called from process context. | |
312 | * | |
313 | * Returns zero on success or %-ENOENT on failure. | |
314 | */ | |
315 | int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh, | |
316 | struct notifier_block *n) | |
317 | { | |
318 | int ret; | |
319 | ||
320 | /* | |
321 | * This code gets used during boot-up, when task switching is | |
322 | * not yet working and interrupts must remain disabled. At | |
323 | * such times we must not call down_write(). | |
324 | */ | |
325 | if (unlikely(system_state == SYSTEM_BOOTING)) | |
326 | return notifier_chain_unregister(&nh->head, n); | |
327 | ||
328 | down_write(&nh->rwsem); | |
329 | ret = notifier_chain_unregister(&nh->head, n); | |
330 | up_write(&nh->rwsem); | |
331 | return ret; | |
332 | } | |
333 | ||
334 | EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister); | |
335 | ||
336 | /** | |
6f7cc11a | 337 | * __blocking_notifier_call_chain - Call functions in a blocking notifier chain |
e041c683 | 338 | * @nh: Pointer to head of the blocking notifier chain |
1da177e4 LT |
339 | * @val: Value passed unmodified to notifier function |
340 | * @v: Pointer passed unmodified to notifier function | |
6f7cc11a GS |
341 | * @nr_to_call: See comment for notifier_call_chain. |
342 | * @nr_calls: See comment for notifier_call_chain. | |
1da177e4 | 343 | * |
e041c683 AS |
344 | * Calls each function in a notifier chain in turn. The functions |
345 | * run in a process context, so they are allowed to block. | |
1da177e4 | 346 | * |
e041c683 | 347 | * If the return value of the notifier can be and'ed |
72fd4a35 | 348 | * with %NOTIFY_STOP_MASK then blocking_notifier_call_chain() |
1da177e4 LT |
349 | * will return immediately, with the return value of |
350 | * the notifier function which halted execution. | |
e041c683 | 351 | * Otherwise the return value is the return value |
1da177e4 LT |
352 | * of the last notifier function called. |
353 | */ | |
354 | ||
6f7cc11a GS |
355 | int __blocking_notifier_call_chain(struct blocking_notifier_head *nh, |
356 | unsigned long val, void *v, | |
357 | int nr_to_call, int *nr_calls) | |
1da177e4 | 358 | { |
1b5180b6 | 359 | int ret = NOTIFY_DONE; |
e041c683 | 360 | |
1b5180b6 IM |
361 | /* |
362 | * We check the head outside the lock, but if this access is | |
363 | * racy then it does not matter what the result of the test | |
364 | * is, we re-check the list after having taken the lock anyway: | |
365 | */ | |
366 | if (rcu_dereference(nh->head)) { | |
367 | down_read(&nh->rwsem); | |
6f7cc11a GS |
368 | ret = notifier_call_chain(&nh->head, val, v, nr_to_call, |
369 | nr_calls); | |
1b5180b6 IM |
370 | up_read(&nh->rwsem); |
371 | } | |
1da177e4 LT |
372 | return ret; |
373 | } | |
6f7cc11a | 374 | EXPORT_SYMBOL_GPL(__blocking_notifier_call_chain); |
1da177e4 | 375 | |
6f7cc11a GS |
376 | int blocking_notifier_call_chain(struct blocking_notifier_head *nh, |
377 | unsigned long val, void *v) | |
378 | { | |
379 | return __blocking_notifier_call_chain(nh, val, v, -1, NULL); | |
380 | } | |
e041c683 AS |
381 | EXPORT_SYMBOL_GPL(blocking_notifier_call_chain); |
382 | ||
383 | /* | |
384 | * Raw notifier chain routines. There is no protection; | |
385 | * the caller must provide it. Use at your own risk! | |
386 | */ | |
387 | ||
388 | /** | |
389 | * raw_notifier_chain_register - Add notifier to a raw notifier chain | |
390 | * @nh: Pointer to head of the raw notifier chain | |
391 | * @n: New entry in notifier chain | |
392 | * | |
393 | * Adds a notifier to a raw notifier chain. | |
394 | * All locking must be provided by the caller. | |
395 | * | |
396 | * Currently always returns zero. | |
397 | */ | |
398 | ||
399 | int raw_notifier_chain_register(struct raw_notifier_head *nh, | |
400 | struct notifier_block *n) | |
401 | { | |
402 | return notifier_chain_register(&nh->head, n); | |
403 | } | |
404 | ||
405 | EXPORT_SYMBOL_GPL(raw_notifier_chain_register); | |
406 | ||
407 | /** | |
408 | * raw_notifier_chain_unregister - Remove notifier from a raw notifier chain | |
409 | * @nh: Pointer to head of the raw notifier chain | |
410 | * @n: Entry to remove from notifier chain | |
411 | * | |
412 | * Removes a notifier from a raw notifier chain. | |
413 | * All locking must be provided by the caller. | |
414 | * | |
415 | * Returns zero on success or %-ENOENT on failure. | |
416 | */ | |
417 | int raw_notifier_chain_unregister(struct raw_notifier_head *nh, | |
418 | struct notifier_block *n) | |
419 | { | |
420 | return notifier_chain_unregister(&nh->head, n); | |
421 | } | |
422 | ||
423 | EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister); | |
424 | ||
425 | /** | |
6f7cc11a | 426 | * __raw_notifier_call_chain - Call functions in a raw notifier chain |
e041c683 AS |
427 | * @nh: Pointer to head of the raw notifier chain |
428 | * @val: Value passed unmodified to notifier function | |
429 | * @v: Pointer passed unmodified to notifier function | |
6f7cc11a GS |
430 | * @nr_to_call: See comment for notifier_call_chain. |
431 | * @nr_calls: See comment for notifier_call_chain | |
e041c683 AS |
432 | * |
433 | * Calls each function in a notifier chain in turn. The functions | |
434 | * run in an undefined context. | |
435 | * All locking must be provided by the caller. | |
436 | * | |
437 | * If the return value of the notifier can be and'ed | |
72fd4a35 | 438 | * with %NOTIFY_STOP_MASK then raw_notifier_call_chain() |
e041c683 AS |
439 | * will return immediately, with the return value of |
440 | * the notifier function which halted execution. | |
441 | * Otherwise the return value is the return value | |
442 | * of the last notifier function called. | |
443 | */ | |
444 | ||
6f7cc11a GS |
445 | int __raw_notifier_call_chain(struct raw_notifier_head *nh, |
446 | unsigned long val, void *v, | |
447 | int nr_to_call, int *nr_calls) | |
448 | { | |
449 | return notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls); | |
450 | } | |
451 | ||
452 | EXPORT_SYMBOL_GPL(__raw_notifier_call_chain); | |
453 | ||
e041c683 AS |
454 | int raw_notifier_call_chain(struct raw_notifier_head *nh, |
455 | unsigned long val, void *v) | |
456 | { | |
6f7cc11a | 457 | return __raw_notifier_call_chain(nh, val, v, -1, NULL); |
e041c683 AS |
458 | } |
459 | ||
460 | EXPORT_SYMBOL_GPL(raw_notifier_call_chain); | |
1da177e4 | 461 | |
eabc0694 AS |
462 | /* |
463 | * SRCU notifier chain routines. Registration and unregistration | |
464 | * use a mutex, and call_chain is synchronized by SRCU (no locks). | |
465 | */ | |
466 | ||
467 | /** | |
468 | * srcu_notifier_chain_register - Add notifier to an SRCU notifier chain | |
469 | * @nh: Pointer to head of the SRCU notifier chain | |
470 | * @n: New entry in notifier chain | |
471 | * | |
472 | * Adds a notifier to an SRCU notifier chain. | |
473 | * Must be called in process context. | |
474 | * | |
475 | * Currently always returns zero. | |
476 | */ | |
477 | ||
478 | int srcu_notifier_chain_register(struct srcu_notifier_head *nh, | |
479 | struct notifier_block *n) | |
480 | { | |
481 | int ret; | |
482 | ||
483 | /* | |
484 | * This code gets used during boot-up, when task switching is | |
485 | * not yet working and interrupts must remain disabled. At | |
486 | * such times we must not call mutex_lock(). | |
487 | */ | |
488 | if (unlikely(system_state == SYSTEM_BOOTING)) | |
489 | return notifier_chain_register(&nh->head, n); | |
490 | ||
491 | mutex_lock(&nh->mutex); | |
492 | ret = notifier_chain_register(&nh->head, n); | |
493 | mutex_unlock(&nh->mutex); | |
494 | return ret; | |
495 | } | |
496 | ||
497 | EXPORT_SYMBOL_GPL(srcu_notifier_chain_register); | |
498 | ||
499 | /** | |
500 | * srcu_notifier_chain_unregister - Remove notifier from an SRCU notifier chain | |
501 | * @nh: Pointer to head of the SRCU notifier chain | |
502 | * @n: Entry to remove from notifier chain | |
503 | * | |
504 | * Removes a notifier from an SRCU notifier chain. | |
505 | * Must be called from process context. | |
506 | * | |
507 | * Returns zero on success or %-ENOENT on failure. | |
508 | */ | |
509 | int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh, | |
510 | struct notifier_block *n) | |
511 | { | |
512 | int ret; | |
513 | ||
514 | /* | |
515 | * This code gets used during boot-up, when task switching is | |
516 | * not yet working and interrupts must remain disabled. At | |
517 | * such times we must not call mutex_lock(). | |
518 | */ | |
519 | if (unlikely(system_state == SYSTEM_BOOTING)) | |
520 | return notifier_chain_unregister(&nh->head, n); | |
521 | ||
522 | mutex_lock(&nh->mutex); | |
523 | ret = notifier_chain_unregister(&nh->head, n); | |
524 | mutex_unlock(&nh->mutex); | |
525 | synchronize_srcu(&nh->srcu); | |
526 | return ret; | |
527 | } | |
528 | ||
529 | EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister); | |
530 | ||
531 | /** | |
6f7cc11a | 532 | * __srcu_notifier_call_chain - Call functions in an SRCU notifier chain |
eabc0694 AS |
533 | * @nh: Pointer to head of the SRCU notifier chain |
534 | * @val: Value passed unmodified to notifier function | |
535 | * @v: Pointer passed unmodified to notifier function | |
6f7cc11a GS |
536 | * @nr_to_call: See comment for notifier_call_chain. |
537 | * @nr_calls: See comment for notifier_call_chain | |
eabc0694 AS |
538 | * |
539 | * Calls each function in a notifier chain in turn. The functions | |
540 | * run in a process context, so they are allowed to block. | |
541 | * | |
542 | * If the return value of the notifier can be and'ed | |
72fd4a35 | 543 | * with %NOTIFY_STOP_MASK then srcu_notifier_call_chain() |
eabc0694 AS |
544 | * will return immediately, with the return value of |
545 | * the notifier function which halted execution. | |
546 | * Otherwise the return value is the return value | |
547 | * of the last notifier function called. | |
548 | */ | |
549 | ||
6f7cc11a GS |
550 | int __srcu_notifier_call_chain(struct srcu_notifier_head *nh, |
551 | unsigned long val, void *v, | |
552 | int nr_to_call, int *nr_calls) | |
eabc0694 AS |
553 | { |
554 | int ret; | |
555 | int idx; | |
556 | ||
557 | idx = srcu_read_lock(&nh->srcu); | |
6f7cc11a | 558 | ret = notifier_call_chain(&nh->head, val, v, nr_to_call, nr_calls); |
eabc0694 AS |
559 | srcu_read_unlock(&nh->srcu, idx); |
560 | return ret; | |
561 | } | |
6f7cc11a | 562 | EXPORT_SYMBOL_GPL(__srcu_notifier_call_chain); |
eabc0694 | 563 | |
6f7cc11a GS |
564 | int srcu_notifier_call_chain(struct srcu_notifier_head *nh, |
565 | unsigned long val, void *v) | |
566 | { | |
567 | return __srcu_notifier_call_chain(nh, val, v, -1, NULL); | |
568 | } | |
eabc0694 AS |
569 | EXPORT_SYMBOL_GPL(srcu_notifier_call_chain); |
570 | ||
571 | /** | |
572 | * srcu_init_notifier_head - Initialize an SRCU notifier head | |
573 | * @nh: Pointer to head of the srcu notifier chain | |
574 | * | |
575 | * Unlike other sorts of notifier heads, SRCU notifier heads require | |
576 | * dynamic initialization. Be sure to call this routine before | |
577 | * calling any of the other SRCU notifier routines for this head. | |
578 | * | |
579 | * If an SRCU notifier head is deallocated, it must first be cleaned | |
580 | * up by calling srcu_cleanup_notifier_head(). Otherwise the head's | |
581 | * per-cpu data (used by the SRCU mechanism) will leak. | |
582 | */ | |
583 | ||
584 | void srcu_init_notifier_head(struct srcu_notifier_head *nh) | |
585 | { | |
586 | mutex_init(&nh->mutex); | |
e6a92013 AS |
587 | if (init_srcu_struct(&nh->srcu) < 0) |
588 | BUG(); | |
eabc0694 AS |
589 | nh->head = NULL; |
590 | } | |
591 | ||
592 | EXPORT_SYMBOL_GPL(srcu_init_notifier_head); | |
593 | ||
1da177e4 LT |
594 | /** |
595 | * register_reboot_notifier - Register function to be called at reboot time | |
596 | * @nb: Info about notifier function to be called | |
597 | * | |
598 | * Registers a function with the list of functions | |
599 | * to be called at reboot time. | |
600 | * | |
72fd4a35 | 601 | * Currently always returns zero, as blocking_notifier_chain_register() |
1da177e4 LT |
602 | * always returns zero. |
603 | */ | |
604 | ||
605 | int register_reboot_notifier(struct notifier_block * nb) | |
606 | { | |
e041c683 | 607 | return blocking_notifier_chain_register(&reboot_notifier_list, nb); |
1da177e4 LT |
608 | } |
609 | ||
610 | EXPORT_SYMBOL(register_reboot_notifier); | |
611 | ||
612 | /** | |
613 | * unregister_reboot_notifier - Unregister previously registered reboot notifier | |
614 | * @nb: Hook to be unregistered | |
615 | * | |
616 | * Unregisters a previously registered reboot | |
617 | * notifier function. | |
618 | * | |
619 | * Returns zero on success, or %-ENOENT on failure. | |
620 | */ | |
621 | ||
622 | int unregister_reboot_notifier(struct notifier_block * nb) | |
623 | { | |
e041c683 | 624 | return blocking_notifier_chain_unregister(&reboot_notifier_list, nb); |
1da177e4 LT |
625 | } |
626 | ||
627 | EXPORT_SYMBOL(unregister_reboot_notifier); | |
628 | ||
629 | static int set_one_prio(struct task_struct *p, int niceval, int error) | |
630 | { | |
631 | int no_nice; | |
632 | ||
633 | if (p->uid != current->euid && | |
634 | p->euid != current->euid && !capable(CAP_SYS_NICE)) { | |
635 | error = -EPERM; | |
636 | goto out; | |
637 | } | |
e43379f1 | 638 | if (niceval < task_nice(p) && !can_nice(p, niceval)) { |
1da177e4 LT |
639 | error = -EACCES; |
640 | goto out; | |
641 | } | |
642 | no_nice = security_task_setnice(p, niceval); | |
643 | if (no_nice) { | |
644 | error = no_nice; | |
645 | goto out; | |
646 | } | |
647 | if (error == -ESRCH) | |
648 | error = 0; | |
649 | set_user_nice(p, niceval); | |
650 | out: | |
651 | return error; | |
652 | } | |
653 | ||
654 | asmlinkage long sys_setpriority(int which, int who, int niceval) | |
655 | { | |
656 | struct task_struct *g, *p; | |
657 | struct user_struct *user; | |
658 | int error = -EINVAL; | |
41487c65 | 659 | struct pid *pgrp; |
1da177e4 LT |
660 | |
661 | if (which > 2 || which < 0) | |
662 | goto out; | |
663 | ||
664 | /* normalize: avoid signed division (rounding problems) */ | |
665 | error = -ESRCH; | |
666 | if (niceval < -20) | |
667 | niceval = -20; | |
668 | if (niceval > 19) | |
669 | niceval = 19; | |
670 | ||
671 | read_lock(&tasklist_lock); | |
672 | switch (which) { | |
673 | case PRIO_PROCESS: | |
41487c65 EB |
674 | if (who) |
675 | p = find_task_by_pid(who); | |
676 | else | |
677 | p = current; | |
1da177e4 LT |
678 | if (p) |
679 | error = set_one_prio(p, niceval, error); | |
680 | break; | |
681 | case PRIO_PGRP: | |
41487c65 EB |
682 | if (who) |
683 | pgrp = find_pid(who); | |
684 | else | |
685 | pgrp = task_pgrp(current); | |
686 | do_each_pid_task(pgrp, PIDTYPE_PGID, p) { | |
1da177e4 | 687 | error = set_one_prio(p, niceval, error); |
41487c65 | 688 | } while_each_pid_task(pgrp, PIDTYPE_PGID, p); |
1da177e4 LT |
689 | break; |
690 | case PRIO_USER: | |
691 | user = current->user; | |
692 | if (!who) | |
693 | who = current->uid; | |
694 | else | |
695 | if ((who != current->uid) && !(user = find_user(who))) | |
696 | goto out_unlock; /* No processes for this user */ | |
697 | ||
698 | do_each_thread(g, p) | |
699 | if (p->uid == who) | |
700 | error = set_one_prio(p, niceval, error); | |
701 | while_each_thread(g, p); | |
702 | if (who != current->uid) | |
703 | free_uid(user); /* For find_user() */ | |
704 | break; | |
705 | } | |
706 | out_unlock: | |
707 | read_unlock(&tasklist_lock); | |
708 | out: | |
709 | return error; | |
710 | } | |
711 | ||
712 | /* | |
713 | * Ugh. To avoid negative return values, "getpriority()" will | |
714 | * not return the normal nice-value, but a negated value that | |
715 | * has been offset by 20 (ie it returns 40..1 instead of -20..19) | |
716 | * to stay compatible. | |
717 | */ | |
718 | asmlinkage long sys_getpriority(int which, int who) | |
719 | { | |
720 | struct task_struct *g, *p; | |
721 | struct user_struct *user; | |
722 | long niceval, retval = -ESRCH; | |
41487c65 | 723 | struct pid *pgrp; |
1da177e4 LT |
724 | |
725 | if (which > 2 || which < 0) | |
726 | return -EINVAL; | |
727 | ||
728 | read_lock(&tasklist_lock); | |
729 | switch (which) { | |
730 | case PRIO_PROCESS: | |
41487c65 EB |
731 | if (who) |
732 | p = find_task_by_pid(who); | |
733 | else | |
734 | p = current; | |
1da177e4 LT |
735 | if (p) { |
736 | niceval = 20 - task_nice(p); | |
737 | if (niceval > retval) | |
738 | retval = niceval; | |
739 | } | |
740 | break; | |
741 | case PRIO_PGRP: | |
41487c65 EB |
742 | if (who) |
743 | pgrp = find_pid(who); | |
744 | else | |
745 | pgrp = task_pgrp(current); | |
746 | do_each_pid_task(pgrp, PIDTYPE_PGID, p) { | |
1da177e4 LT |
747 | niceval = 20 - task_nice(p); |
748 | if (niceval > retval) | |
749 | retval = niceval; | |
41487c65 | 750 | } while_each_pid_task(pgrp, PIDTYPE_PGID, p); |
1da177e4 LT |
751 | break; |
752 | case PRIO_USER: | |
753 | user = current->user; | |
754 | if (!who) | |
755 | who = current->uid; | |
756 | else | |
757 | if ((who != current->uid) && !(user = find_user(who))) | |
758 | goto out_unlock; /* No processes for this user */ | |
759 | ||
760 | do_each_thread(g, p) | |
761 | if (p->uid == who) { | |
762 | niceval = 20 - task_nice(p); | |
763 | if (niceval > retval) | |
764 | retval = niceval; | |
765 | } | |
766 | while_each_thread(g, p); | |
767 | if (who != current->uid) | |
768 | free_uid(user); /* for find_user() */ | |
769 | break; | |
770 | } | |
771 | out_unlock: | |
772 | read_unlock(&tasklist_lock); | |
773 | ||
774 | return retval; | |
775 | } | |
776 | ||
e4c94330 EB |
777 | /** |
778 | * emergency_restart - reboot the system | |
779 | * | |
780 | * Without shutting down any hardware or taking any locks | |
781 | * reboot the system. This is called when we know we are in | |
782 | * trouble so this is our best effort to reboot. This is | |
783 | * safe to call in interrupt context. | |
784 | */ | |
7c903473 EB |
785 | void emergency_restart(void) |
786 | { | |
787 | machine_emergency_restart(); | |
788 | } | |
789 | EXPORT_SYMBOL_GPL(emergency_restart); | |
790 | ||
83cc5ed3 | 791 | static void kernel_restart_prepare(char *cmd) |
4a00ea1e | 792 | { |
e041c683 | 793 | blocking_notifier_call_chain(&reboot_notifier_list, SYS_RESTART, cmd); |
4a00ea1e | 794 | system_state = SYSTEM_RESTART; |
4a00ea1e | 795 | device_shutdown(); |
e4c94330 | 796 | } |
1e5d5331 RD |
797 | |
798 | /** | |
799 | * kernel_restart - reboot the system | |
800 | * @cmd: pointer to buffer containing command to execute for restart | |
b8887e6e | 801 | * or %NULL |
1e5d5331 RD |
802 | * |
803 | * Shutdown everything and perform a clean reboot. | |
804 | * This is not safe to call in interrupt context. | |
805 | */ | |
e4c94330 EB |
806 | void kernel_restart(char *cmd) |
807 | { | |
808 | kernel_restart_prepare(cmd); | |
756184b7 | 809 | if (!cmd) |
4a00ea1e | 810 | printk(KERN_EMERG "Restarting system.\n"); |
756184b7 | 811 | else |
4a00ea1e | 812 | printk(KERN_EMERG "Restarting system with command '%s'.\n", cmd); |
4a00ea1e EB |
813 | machine_restart(cmd); |
814 | } | |
815 | EXPORT_SYMBOL_GPL(kernel_restart); | |
816 | ||
e4c94330 EB |
817 | /** |
818 | * kernel_kexec - reboot the system | |
819 | * | |
820 | * Move into place and start executing a preloaded standalone | |
821 | * executable. If nothing was preloaded return an error. | |
822 | */ | |
83cc5ed3 | 823 | static void kernel_kexec(void) |
4a00ea1e EB |
824 | { |
825 | #ifdef CONFIG_KEXEC | |
826 | struct kimage *image; | |
4bb8089c | 827 | image = xchg(&kexec_image, NULL); |
756184b7 | 828 | if (!image) |
4a00ea1e | 829 | return; |
e4c94330 | 830 | kernel_restart_prepare(NULL); |
4a00ea1e EB |
831 | printk(KERN_EMERG "Starting new kernel\n"); |
832 | machine_shutdown(); | |
833 | machine_kexec(image); | |
834 | #endif | |
835 | } | |
4a00ea1e | 836 | |
729b4d4c AS |
837 | void kernel_shutdown_prepare(enum system_states state) |
838 | { | |
e041c683 | 839 | blocking_notifier_call_chain(&reboot_notifier_list, |
729b4d4c AS |
840 | (state == SYSTEM_HALT)?SYS_HALT:SYS_POWER_OFF, NULL); |
841 | system_state = state; | |
842 | device_shutdown(); | |
843 | } | |
e4c94330 EB |
844 | /** |
845 | * kernel_halt - halt the system | |
846 | * | |
847 | * Shutdown everything and perform a clean system halt. | |
848 | */ | |
e4c94330 EB |
849 | void kernel_halt(void) |
850 | { | |
729b4d4c | 851 | kernel_shutdown_prepare(SYSTEM_HALT); |
4a00ea1e EB |
852 | printk(KERN_EMERG "System halted.\n"); |
853 | machine_halt(); | |
854 | } | |
729b4d4c | 855 | |
4a00ea1e EB |
856 | EXPORT_SYMBOL_GPL(kernel_halt); |
857 | ||
e4c94330 EB |
858 | /** |
859 | * kernel_power_off - power_off the system | |
860 | * | |
861 | * Shutdown everything and perform a clean system power_off. | |
862 | */ | |
e4c94330 EB |
863 | void kernel_power_off(void) |
864 | { | |
729b4d4c | 865 | kernel_shutdown_prepare(SYSTEM_POWER_OFF); |
4a00ea1e EB |
866 | printk(KERN_EMERG "Power down.\n"); |
867 | machine_power_off(); | |
868 | } | |
869 | EXPORT_SYMBOL_GPL(kernel_power_off); | |
1da177e4 LT |
870 | /* |
871 | * Reboot system call: for obvious reasons only root may call it, | |
872 | * and even root needs to set up some magic numbers in the registers | |
873 | * so that some mistake won't make this reboot the whole machine. | |
874 | * You can also set the meaning of the ctrl-alt-del-key here. | |
875 | * | |
876 | * reboot doesn't sync: do that yourself before calling this. | |
877 | */ | |
878 | asmlinkage long sys_reboot(int magic1, int magic2, unsigned int cmd, void __user * arg) | |
879 | { | |
880 | char buffer[256]; | |
881 | ||
882 | /* We only trust the superuser with rebooting the system. */ | |
883 | if (!capable(CAP_SYS_BOOT)) | |
884 | return -EPERM; | |
885 | ||
886 | /* For safety, we require "magic" arguments. */ | |
887 | if (magic1 != LINUX_REBOOT_MAGIC1 || | |
888 | (magic2 != LINUX_REBOOT_MAGIC2 && | |
889 | magic2 != LINUX_REBOOT_MAGIC2A && | |
890 | magic2 != LINUX_REBOOT_MAGIC2B && | |
891 | magic2 != LINUX_REBOOT_MAGIC2C)) | |
892 | return -EINVAL; | |
893 | ||
5e38291d EB |
894 | /* Instead of trying to make the power_off code look like |
895 | * halt when pm_power_off is not set do it the easy way. | |
896 | */ | |
897 | if ((cmd == LINUX_REBOOT_CMD_POWER_OFF) && !pm_power_off) | |
898 | cmd = LINUX_REBOOT_CMD_HALT; | |
899 | ||
1da177e4 LT |
900 | lock_kernel(); |
901 | switch (cmd) { | |
902 | case LINUX_REBOOT_CMD_RESTART: | |
4a00ea1e | 903 | kernel_restart(NULL); |
1da177e4 LT |
904 | break; |
905 | ||
906 | case LINUX_REBOOT_CMD_CAD_ON: | |
907 | C_A_D = 1; | |
908 | break; | |
909 | ||
910 | case LINUX_REBOOT_CMD_CAD_OFF: | |
911 | C_A_D = 0; | |
912 | break; | |
913 | ||
914 | case LINUX_REBOOT_CMD_HALT: | |
4a00ea1e | 915 | kernel_halt(); |
1da177e4 LT |
916 | unlock_kernel(); |
917 | do_exit(0); | |
918 | break; | |
919 | ||
920 | case LINUX_REBOOT_CMD_POWER_OFF: | |
4a00ea1e | 921 | kernel_power_off(); |
1da177e4 LT |
922 | unlock_kernel(); |
923 | do_exit(0); | |
924 | break; | |
925 | ||
926 | case LINUX_REBOOT_CMD_RESTART2: | |
927 | if (strncpy_from_user(&buffer[0], arg, sizeof(buffer) - 1) < 0) { | |
928 | unlock_kernel(); | |
929 | return -EFAULT; | |
930 | } | |
931 | buffer[sizeof(buffer) - 1] = '\0'; | |
932 | ||
4a00ea1e | 933 | kernel_restart(buffer); |
1da177e4 LT |
934 | break; |
935 | ||
dc009d92 | 936 | case LINUX_REBOOT_CMD_KEXEC: |
4a00ea1e EB |
937 | kernel_kexec(); |
938 | unlock_kernel(); | |
939 | return -EINVAL; | |
940 | ||
1da177e4 LT |
941 | #ifdef CONFIG_SOFTWARE_SUSPEND |
942 | case LINUX_REBOOT_CMD_SW_SUSPEND: | |
943 | { | |
a3d25c27 | 944 | int ret = hibernate(); |
1da177e4 LT |
945 | unlock_kernel(); |
946 | return ret; | |
947 | } | |
948 | #endif | |
949 | ||
950 | default: | |
951 | unlock_kernel(); | |
952 | return -EINVAL; | |
953 | } | |
954 | unlock_kernel(); | |
955 | return 0; | |
956 | } | |
957 | ||
65f27f38 | 958 | static void deferred_cad(struct work_struct *dummy) |
1da177e4 | 959 | { |
abcd9e51 | 960 | kernel_restart(NULL); |
1da177e4 LT |
961 | } |
962 | ||
963 | /* | |
964 | * This function gets called by ctrl-alt-del - ie the keyboard interrupt. | |
965 | * As it's called within an interrupt, it may NOT sync: the only choice | |
966 | * is whether to reboot at once, or just ignore the ctrl-alt-del. | |
967 | */ | |
968 | void ctrl_alt_del(void) | |
969 | { | |
65f27f38 | 970 | static DECLARE_WORK(cad_work, deferred_cad); |
1da177e4 LT |
971 | |
972 | if (C_A_D) | |
973 | schedule_work(&cad_work); | |
974 | else | |
9ec52099 | 975 | kill_cad_pid(SIGINT, 1); |
1da177e4 LT |
976 | } |
977 | ||
1da177e4 LT |
978 | /* |
979 | * Unprivileged users may change the real gid to the effective gid | |
980 | * or vice versa. (BSD-style) | |
981 | * | |
982 | * If you set the real gid at all, or set the effective gid to a value not | |
983 | * equal to the real gid, then the saved gid is set to the new effective gid. | |
984 | * | |
985 | * This makes it possible for a setgid program to completely drop its | |
986 | * privileges, which is often a useful assertion to make when you are doing | |
987 | * a security audit over a program. | |
988 | * | |
989 | * The general idea is that a program which uses just setregid() will be | |
990 | * 100% compatible with BSD. A program which uses just setgid() will be | |
991 | * 100% compatible with POSIX with saved IDs. | |
992 | * | |
993 | * SMP: There are not races, the GIDs are checked only by filesystem | |
994 | * operations (as far as semantic preservation is concerned). | |
995 | */ | |
996 | asmlinkage long sys_setregid(gid_t rgid, gid_t egid) | |
997 | { | |
998 | int old_rgid = current->gid; | |
999 | int old_egid = current->egid; | |
1000 | int new_rgid = old_rgid; | |
1001 | int new_egid = old_egid; | |
1002 | int retval; | |
1003 | ||
1004 | retval = security_task_setgid(rgid, egid, (gid_t)-1, LSM_SETID_RE); | |
1005 | if (retval) | |
1006 | return retval; | |
1007 | ||
1008 | if (rgid != (gid_t) -1) { | |
1009 | if ((old_rgid == rgid) || | |
1010 | (current->egid==rgid) || | |
1011 | capable(CAP_SETGID)) | |
1012 | new_rgid = rgid; | |
1013 | else | |
1014 | return -EPERM; | |
1015 | } | |
1016 | if (egid != (gid_t) -1) { | |
1017 | if ((old_rgid == egid) || | |
1018 | (current->egid == egid) || | |
1019 | (current->sgid == egid) || | |
1020 | capable(CAP_SETGID)) | |
1021 | new_egid = egid; | |
756184b7 | 1022 | else |
1da177e4 | 1023 | return -EPERM; |
1da177e4 | 1024 | } |
756184b7 | 1025 | if (new_egid != old_egid) { |
d6e71144 | 1026 | current->mm->dumpable = suid_dumpable; |
d59dd462 | 1027 | smp_wmb(); |
1da177e4 LT |
1028 | } |
1029 | if (rgid != (gid_t) -1 || | |
1030 | (egid != (gid_t) -1 && egid != old_rgid)) | |
1031 | current->sgid = new_egid; | |
1032 | current->fsgid = new_egid; | |
1033 | current->egid = new_egid; | |
1034 | current->gid = new_rgid; | |
1035 | key_fsgid_changed(current); | |
9f46080c | 1036 | proc_id_connector(current, PROC_EVENT_GID); |
1da177e4 LT |
1037 | return 0; |
1038 | } | |
1039 | ||
1040 | /* | |
1041 | * setgid() is implemented like SysV w/ SAVED_IDS | |
1042 | * | |
1043 | * SMP: Same implicit races as above. | |
1044 | */ | |
1045 | asmlinkage long sys_setgid(gid_t gid) | |
1046 | { | |
1047 | int old_egid = current->egid; | |
1048 | int retval; | |
1049 | ||
1050 | retval = security_task_setgid(gid, (gid_t)-1, (gid_t)-1, LSM_SETID_ID); | |
1051 | if (retval) | |
1052 | return retval; | |
1053 | ||
756184b7 CP |
1054 | if (capable(CAP_SETGID)) { |
1055 | if (old_egid != gid) { | |
d6e71144 | 1056 | current->mm->dumpable = suid_dumpable; |
d59dd462 | 1057 | smp_wmb(); |
1da177e4 LT |
1058 | } |
1059 | current->gid = current->egid = current->sgid = current->fsgid = gid; | |
756184b7 CP |
1060 | } else if ((gid == current->gid) || (gid == current->sgid)) { |
1061 | if (old_egid != gid) { | |
d6e71144 | 1062 | current->mm->dumpable = suid_dumpable; |
d59dd462 | 1063 | smp_wmb(); |
1da177e4 LT |
1064 | } |
1065 | current->egid = current->fsgid = gid; | |
1066 | } | |
1067 | else | |
1068 | return -EPERM; | |
1069 | ||
1070 | key_fsgid_changed(current); | |
9f46080c | 1071 | proc_id_connector(current, PROC_EVENT_GID); |
1da177e4 LT |
1072 | return 0; |
1073 | } | |
1074 | ||
1075 | static int set_user(uid_t new_ruid, int dumpclear) | |
1076 | { | |
1077 | struct user_struct *new_user; | |
1078 | ||
1079 | new_user = alloc_uid(new_ruid); | |
1080 | if (!new_user) | |
1081 | return -EAGAIN; | |
1082 | ||
1083 | if (atomic_read(&new_user->processes) >= | |
1084 | current->signal->rlim[RLIMIT_NPROC].rlim_cur && | |
1085 | new_user != &root_user) { | |
1086 | free_uid(new_user); | |
1087 | return -EAGAIN; | |
1088 | } | |
1089 | ||
1090 | switch_uid(new_user); | |
1091 | ||
756184b7 | 1092 | if (dumpclear) { |
d6e71144 | 1093 | current->mm->dumpable = suid_dumpable; |
d59dd462 | 1094 | smp_wmb(); |
1da177e4 LT |
1095 | } |
1096 | current->uid = new_ruid; | |
1097 | return 0; | |
1098 | } | |
1099 | ||
1100 | /* | |
1101 | * Unprivileged users may change the real uid to the effective uid | |
1102 | * or vice versa. (BSD-style) | |
1103 | * | |
1104 | * If you set the real uid at all, or set the effective uid to a value not | |
1105 | * equal to the real uid, then the saved uid is set to the new effective uid. | |
1106 | * | |
1107 | * This makes it possible for a setuid program to completely drop its | |
1108 | * privileges, which is often a useful assertion to make when you are doing | |
1109 | * a security audit over a program. | |
1110 | * | |
1111 | * The general idea is that a program which uses just setreuid() will be | |
1112 | * 100% compatible with BSD. A program which uses just setuid() will be | |
1113 | * 100% compatible with POSIX with saved IDs. | |
1114 | */ | |
1115 | asmlinkage long sys_setreuid(uid_t ruid, uid_t euid) | |
1116 | { | |
1117 | int old_ruid, old_euid, old_suid, new_ruid, new_euid; | |
1118 | int retval; | |
1119 | ||
1120 | retval = security_task_setuid(ruid, euid, (uid_t)-1, LSM_SETID_RE); | |
1121 | if (retval) | |
1122 | return retval; | |
1123 | ||
1124 | new_ruid = old_ruid = current->uid; | |
1125 | new_euid = old_euid = current->euid; | |
1126 | old_suid = current->suid; | |
1127 | ||
1128 | if (ruid != (uid_t) -1) { | |
1129 | new_ruid = ruid; | |
1130 | if ((old_ruid != ruid) && | |
1131 | (current->euid != ruid) && | |
1132 | !capable(CAP_SETUID)) | |
1133 | return -EPERM; | |
1134 | } | |
1135 | ||
1136 | if (euid != (uid_t) -1) { | |
1137 | new_euid = euid; | |
1138 | if ((old_ruid != euid) && | |
1139 | (current->euid != euid) && | |
1140 | (current->suid != euid) && | |
1141 | !capable(CAP_SETUID)) | |
1142 | return -EPERM; | |
1143 | } | |
1144 | ||
1145 | if (new_ruid != old_ruid && set_user(new_ruid, new_euid != old_euid) < 0) | |
1146 | return -EAGAIN; | |
1147 | ||
756184b7 | 1148 | if (new_euid != old_euid) { |
d6e71144 | 1149 | current->mm->dumpable = suid_dumpable; |
d59dd462 | 1150 | smp_wmb(); |
1da177e4 LT |
1151 | } |
1152 | current->fsuid = current->euid = new_euid; | |
1153 | if (ruid != (uid_t) -1 || | |
1154 | (euid != (uid_t) -1 && euid != old_ruid)) | |
1155 | current->suid = current->euid; | |
1156 | current->fsuid = current->euid; | |
1157 | ||
1158 | key_fsuid_changed(current); | |
9f46080c | 1159 | proc_id_connector(current, PROC_EVENT_UID); |
1da177e4 LT |
1160 | |
1161 | return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RE); | |
1162 | } | |
1163 | ||
1164 | ||
1165 | ||
1166 | /* | |
1167 | * setuid() is implemented like SysV with SAVED_IDS | |
1168 | * | |
1169 | * Note that SAVED_ID's is deficient in that a setuid root program | |
1170 | * like sendmail, for example, cannot set its uid to be a normal | |
1171 | * user and then switch back, because if you're root, setuid() sets | |
1172 | * the saved uid too. If you don't like this, blame the bright people | |
1173 | * in the POSIX committee and/or USG. Note that the BSD-style setreuid() | |
1174 | * will allow a root program to temporarily drop privileges and be able to | |
1175 | * regain them by swapping the real and effective uid. | |
1176 | */ | |
1177 | asmlinkage long sys_setuid(uid_t uid) | |
1178 | { | |
1179 | int old_euid = current->euid; | |
a09c17a6 | 1180 | int old_ruid, old_suid, new_suid; |
1da177e4 LT |
1181 | int retval; |
1182 | ||
1183 | retval = security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_ID); | |
1184 | if (retval) | |
1185 | return retval; | |
1186 | ||
a09c17a6 | 1187 | old_ruid = current->uid; |
1da177e4 LT |
1188 | old_suid = current->suid; |
1189 | new_suid = old_suid; | |
1190 | ||
1191 | if (capable(CAP_SETUID)) { | |
1192 | if (uid != old_ruid && set_user(uid, old_euid != uid) < 0) | |
1193 | return -EAGAIN; | |
1194 | new_suid = uid; | |
1195 | } else if ((uid != current->uid) && (uid != new_suid)) | |
1196 | return -EPERM; | |
1197 | ||
756184b7 | 1198 | if (old_euid != uid) { |
d6e71144 | 1199 | current->mm->dumpable = suid_dumpable; |
d59dd462 | 1200 | smp_wmb(); |
1da177e4 LT |
1201 | } |
1202 | current->fsuid = current->euid = uid; | |
1203 | current->suid = new_suid; | |
1204 | ||
1205 | key_fsuid_changed(current); | |
9f46080c | 1206 | proc_id_connector(current, PROC_EVENT_UID); |
1da177e4 LT |
1207 | |
1208 | return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_ID); | |
1209 | } | |
1210 | ||
1211 | ||
1212 | /* | |
1213 | * This function implements a generic ability to update ruid, euid, | |
1214 | * and suid. This allows you to implement the 4.4 compatible seteuid(). | |
1215 | */ | |
1216 | asmlinkage long sys_setresuid(uid_t ruid, uid_t euid, uid_t suid) | |
1217 | { | |
1218 | int old_ruid = current->uid; | |
1219 | int old_euid = current->euid; | |
1220 | int old_suid = current->suid; | |
1221 | int retval; | |
1222 | ||
1223 | retval = security_task_setuid(ruid, euid, suid, LSM_SETID_RES); | |
1224 | if (retval) | |
1225 | return retval; | |
1226 | ||
1227 | if (!capable(CAP_SETUID)) { | |
1228 | if ((ruid != (uid_t) -1) && (ruid != current->uid) && | |
1229 | (ruid != current->euid) && (ruid != current->suid)) | |
1230 | return -EPERM; | |
1231 | if ((euid != (uid_t) -1) && (euid != current->uid) && | |
1232 | (euid != current->euid) && (euid != current->suid)) | |
1233 | return -EPERM; | |
1234 | if ((suid != (uid_t) -1) && (suid != current->uid) && | |
1235 | (suid != current->euid) && (suid != current->suid)) | |
1236 | return -EPERM; | |
1237 | } | |
1238 | if (ruid != (uid_t) -1) { | |
1239 | if (ruid != current->uid && set_user(ruid, euid != current->euid) < 0) | |
1240 | return -EAGAIN; | |
1241 | } | |
1242 | if (euid != (uid_t) -1) { | |
756184b7 | 1243 | if (euid != current->euid) { |
d6e71144 | 1244 | current->mm->dumpable = suid_dumpable; |
d59dd462 | 1245 | smp_wmb(); |
1da177e4 LT |
1246 | } |
1247 | current->euid = euid; | |
1248 | } | |
1249 | current->fsuid = current->euid; | |
1250 | if (suid != (uid_t) -1) | |
1251 | current->suid = suid; | |
1252 | ||
1253 | key_fsuid_changed(current); | |
9f46080c | 1254 | proc_id_connector(current, PROC_EVENT_UID); |
1da177e4 LT |
1255 | |
1256 | return security_task_post_setuid(old_ruid, old_euid, old_suid, LSM_SETID_RES); | |
1257 | } | |
1258 | ||
1259 | asmlinkage long sys_getresuid(uid_t __user *ruid, uid_t __user *euid, uid_t __user *suid) | |
1260 | { | |
1261 | int retval; | |
1262 | ||
1263 | if (!(retval = put_user(current->uid, ruid)) && | |
1264 | !(retval = put_user(current->euid, euid))) | |
1265 | retval = put_user(current->suid, suid); | |
1266 | ||
1267 | return retval; | |
1268 | } | |
1269 | ||
1270 | /* | |
1271 | * Same as above, but for rgid, egid, sgid. | |
1272 | */ | |
1273 | asmlinkage long sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid) | |
1274 | { | |
1275 | int retval; | |
1276 | ||
1277 | retval = security_task_setgid(rgid, egid, sgid, LSM_SETID_RES); | |
1278 | if (retval) | |
1279 | return retval; | |
1280 | ||
1281 | if (!capable(CAP_SETGID)) { | |
1282 | if ((rgid != (gid_t) -1) && (rgid != current->gid) && | |
1283 | (rgid != current->egid) && (rgid != current->sgid)) | |
1284 | return -EPERM; | |
1285 | if ((egid != (gid_t) -1) && (egid != current->gid) && | |
1286 | (egid != current->egid) && (egid != current->sgid)) | |
1287 | return -EPERM; | |
1288 | if ((sgid != (gid_t) -1) && (sgid != current->gid) && | |
1289 | (sgid != current->egid) && (sgid != current->sgid)) | |
1290 | return -EPERM; | |
1291 | } | |
1292 | if (egid != (gid_t) -1) { | |
756184b7 | 1293 | if (egid != current->egid) { |
d6e71144 | 1294 | current->mm->dumpable = suid_dumpable; |
d59dd462 | 1295 | smp_wmb(); |
1da177e4 LT |
1296 | } |
1297 | current->egid = egid; | |
1298 | } | |
1299 | current->fsgid = current->egid; | |
1300 | if (rgid != (gid_t) -1) | |
1301 | current->gid = rgid; | |
1302 | if (sgid != (gid_t) -1) | |
1303 | current->sgid = sgid; | |
1304 | ||
1305 | key_fsgid_changed(current); | |
9f46080c | 1306 | proc_id_connector(current, PROC_EVENT_GID); |
1da177e4 LT |
1307 | return 0; |
1308 | } | |
1309 | ||
1310 | asmlinkage long sys_getresgid(gid_t __user *rgid, gid_t __user *egid, gid_t __user *sgid) | |
1311 | { | |
1312 | int retval; | |
1313 | ||
1314 | if (!(retval = put_user(current->gid, rgid)) && | |
1315 | !(retval = put_user(current->egid, egid))) | |
1316 | retval = put_user(current->sgid, sgid); | |
1317 | ||
1318 | return retval; | |
1319 | } | |
1320 | ||
1321 | ||
1322 | /* | |
1323 | * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This | |
1324 | * is used for "access()" and for the NFS daemon (letting nfsd stay at | |
1325 | * whatever uid it wants to). It normally shadows "euid", except when | |
1326 | * explicitly set by setfsuid() or for access.. | |
1327 | */ | |
1328 | asmlinkage long sys_setfsuid(uid_t uid) | |
1329 | { | |
1330 | int old_fsuid; | |
1331 | ||
1332 | old_fsuid = current->fsuid; | |
1333 | if (security_task_setuid(uid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS)) | |
1334 | return old_fsuid; | |
1335 | ||
1336 | if (uid == current->uid || uid == current->euid || | |
1337 | uid == current->suid || uid == current->fsuid || | |
756184b7 CP |
1338 | capable(CAP_SETUID)) { |
1339 | if (uid != old_fsuid) { | |
d6e71144 | 1340 | current->mm->dumpable = suid_dumpable; |
d59dd462 | 1341 | smp_wmb(); |
1da177e4 LT |
1342 | } |
1343 | current->fsuid = uid; | |
1344 | } | |
1345 | ||
1346 | key_fsuid_changed(current); | |
9f46080c | 1347 | proc_id_connector(current, PROC_EVENT_UID); |
1da177e4 LT |
1348 | |
1349 | security_task_post_setuid(old_fsuid, (uid_t)-1, (uid_t)-1, LSM_SETID_FS); | |
1350 | ||
1351 | return old_fsuid; | |
1352 | } | |
1353 | ||
1354 | /* | |
1355 |