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