1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * linux/kernel/sys.c 4 * 5 * Copyright (C) 1991, 1992 Linus Torvalds 6 */ 7 8 #include <linux/export.h> 9 #include <linux/mm.h> 10 #include <linux/utsname.h> 11 #include <linux/mman.h> 12 #include <linux/reboot.h> 13 #include <linux/prctl.h> 14 #include <linux/highuid.h> 15 #include <linux/fs.h> 16 #include <linux/kmod.h> 17 #include <linux/perf_event.h> 18 #include <linux/resource.h> 19 #include <linux/kernel.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 #include <linux/personality.h> 37 #include <linux/ptrace.h> 38 #include <linux/fs_struct.h> 39 #include <linux/file.h> 40 #include <linux/mount.h> 41 #include <linux/gfp.h> 42 #include <linux/syscore_ops.h> 43 #include <linux/version.h> 44 #include <linux/ctype.h> 45 46 #include <linux/compat.h> 47 #include <linux/syscalls.h> 48 #include <linux/kprobes.h> 49 #include <linux/user_namespace.h> 50 #include <linux/time_namespace.h> 51 #include <linux/binfmts.h> 52 53 #include <linux/sched.h> 54 #include <linux/sched/autogroup.h> 55 #include <linux/sched/loadavg.h> 56 #include <linux/sched/stat.h> 57 #include <linux/sched/mm.h> 58 #include <linux/sched/coredump.h> 59 #include <linux/sched/task.h> 60 #include <linux/sched/cputime.h> 61 #include <linux/rcupdate.h> 62 #include <linux/uidgid.h> 63 #include <linux/cred.h> 64 65 #include <linux/nospec.h> 66 67 #include <linux/kmsg_dump.h> 68 /* Move somewhere else to avoid recompiling? */ 69 #include <generated/utsrelease.h> 70 71 #include <linux/uaccess.h> 72 #include <asm/io.h> 73 #include <asm/unistd.h> 74 75 #include "uid16.h" 76 77 #ifndef SET_UNALIGN_CTL 78 # define SET_UNALIGN_CTL(a, b) (-EINVAL) 79 #endif 80 #ifndef GET_UNALIGN_CTL 81 # define GET_UNALIGN_CTL(a, b) (-EINVAL) 82 #endif 83 #ifndef SET_FPEMU_CTL 84 # define SET_FPEMU_CTL(a, b) (-EINVAL) 85 #endif 86 #ifndef GET_FPEMU_CTL 87 # define GET_FPEMU_CTL(a, b) (-EINVAL) 88 #endif 89 #ifndef SET_FPEXC_CTL 90 # define SET_FPEXC_CTL(a, b) (-EINVAL) 91 #endif 92 #ifndef GET_FPEXC_CTL 93 # define GET_FPEXC_CTL(a, b) (-EINVAL) 94 #endif 95 #ifndef GET_ENDIAN 96 # define GET_ENDIAN(a, b) (-EINVAL) 97 #endif 98 #ifndef SET_ENDIAN 99 # define SET_ENDIAN(a, b) (-EINVAL) 100 #endif 101 #ifndef GET_TSC_CTL 102 # define GET_TSC_CTL(a) (-EINVAL) 103 #endif 104 #ifndef SET_TSC_CTL 105 # define SET_TSC_CTL(a) (-EINVAL) 106 #endif 107 #ifndef GET_FP_MODE 108 # define GET_FP_MODE(a) (-EINVAL) 109 #endif 110 #ifndef SET_FP_MODE 111 # define SET_FP_MODE(a,b) (-EINVAL) 112 #endif 113 #ifndef SVE_SET_VL 114 # define SVE_SET_VL(a) (-EINVAL) 115 #endif 116 #ifndef SVE_GET_VL 117 # define SVE_GET_VL() (-EINVAL) 118 #endif 119 #ifndef PAC_RESET_KEYS 120 # define PAC_RESET_KEYS(a, b) (-EINVAL) 121 #endif 122 #ifndef SET_TAGGED_ADDR_CTRL 123 # define SET_TAGGED_ADDR_CTRL(a) (-EINVAL) 124 #endif 125 #ifndef GET_TAGGED_ADDR_CTRL 126 # define GET_TAGGED_ADDR_CTRL() (-EINVAL) 127 #endif 128 129 /* 130 * this is where the system-wide overflow UID and GID are defined, for 131 * architectures that now have 32-bit UID/GID but didn't in the past 132 */ 133 134 int overflowuid = DEFAULT_OVERFLOWUID; 135 int overflowgid = DEFAULT_OVERFLOWGID; 136 137 EXPORT_SYMBOL(overflowuid); 138 EXPORT_SYMBOL(overflowgid); 139 140 /* 141 * the same as above, but for filesystems which can only store a 16-bit 142 * UID and GID. as such, this is needed on all architectures 143 */ 144 145 int fs_overflowuid = DEFAULT_FS_OVERFLOWUID; 146 int fs_overflowgid = DEFAULT_FS_OVERFLOWGID; 147 148 EXPORT_SYMBOL(fs_overflowuid); 149 EXPORT_SYMBOL(fs_overflowgid); 150 151 /* 152 * Returns true if current's euid is same as p's uid or euid, 153 * or has CAP_SYS_NICE to p's user_ns. 154 * 155 * Called with rcu_read_lock, creds are safe 156 */ 157 static bool set_one_prio_perm(struct task_struct *p) 158 { 159 const struct cred *cred = current_cred(), *pcred = __task_cred(p); 160 161 if (uid_eq(pcred->uid, cred->euid) || 162 uid_eq(pcred->euid, cred->euid)) 163 return true; 164 if (ns_capable(pcred->user_ns, CAP_SYS_NICE)) 165 return true; 166 return false; 167 } 168 169 /* 170 * set the priority of a task 171 * - the caller must hold the RCU read lock 172 */ 173 static int set_one_prio(struct task_struct *p, int niceval, int error) 174 { 175 int no_nice; 176 177 if (!set_one_prio_perm(p)) { 178 error = -EPERM; 179 goto out; 180 } 181 if (niceval < task_nice(p) && !can_nice(p, niceval)) { 182 error = -EACCES; 183 goto out; 184 } 185 no_nice = security_task_setnice(p, niceval); 186 if (no_nice) { 187 error = no_nice; 188 goto out; 189 } 190 if (error == -ESRCH) 191 error = 0; 192 set_user_nice(p, niceval); 193 out: 194 return error; 195 } 196 197 SYSCALL_DEFINE3(setpriority, int, which, int, who, int, niceval) 198 { 199 struct task_struct *g, *p; 200 struct user_struct *user; 201 const struct cred *cred = current_cred(); 202 int error = -EINVAL; 203 struct pid *pgrp; 204 kuid_t uid; 205 206 if (which > PRIO_USER || which < PRIO_PROCESS) 207 goto out; 208 if (!ccs_capable(CCS_SYS_NICE)) { 209 error = -EPERM; 210 goto out; 211 } 212 213 /* normalize: avoid signed division (rounding problems) */ 214 error = -ESRCH; 215 if (niceval < MIN_NICE) 216 niceval = MIN_NICE; 217 if (niceval > MAX_NICE) 218 niceval = MAX_NICE; 219 220 rcu_read_lock(); 221 read_lock(&tasklist_lock); 222 switch (which) { 223 case PRIO_PROCESS: 224 if (who) 225 p = find_task_by_vpid(who); 226 else 227 p = current; 228 if (p) 229 error = set_one_prio(p, niceval, error); 230 break; 231 case PRIO_PGRP: 232 if (who) 233 pgrp = find_vpid(who); 234 else 235 pgrp = task_pgrp(current); 236 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { 237 error = set_one_prio(p, niceval, error); 238 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p); 239 break; 240 case PRIO_USER: 241 uid = make_kuid(cred->user_ns, who); 242 user = cred->user; 243 if (!who) 244 uid = cred->uid; 245 else if (!uid_eq(uid, cred->uid)) { 246 user = find_user(uid); 247 if (!user) 248 goto out_unlock; /* No processes for this user */ 249 } 250 do_each_thread(g, p) { 251 if (uid_eq(task_uid(p), uid) && task_pid_vnr(p)) 252 error = set_one_prio(p, niceval, error); 253 } while_each_thread(g, p); 254 if (!uid_eq(uid, cred->uid)) 255 free_uid(user); /* For find_user() */ 256 break; 257 } 258 out_unlock: 259 read_unlock(&tasklist_lock); 260 rcu_read_unlock(); 261 out: 262 return error; 263 } 264 265 /* 266 * Ugh. To avoid negative return values, "getpriority()" will 267 * not return the normal nice-value, but a negated value that 268 * has been offset by 20 (ie it returns 40..1 instead of -20..19) 269 * to stay compatible. 270 */ 271 SYSCALL_DEFINE2(getpriority, int, which, int, who) 272 { 273 struct task_struct *g, *p; 274 struct user_struct *user; 275 const struct cred *cred = current_cred(); 276 long niceval, retval = -ESRCH; 277 struct pid *pgrp; 278 kuid_t uid; 279 280 if (which > PRIO_USER || which < PRIO_PROCESS) 281 return -EINVAL; 282 283 rcu_read_lock(); 284 read_lock(&tasklist_lock); 285 switch (which) { 286 case PRIO_PROCESS: 287 if (who) 288 p = find_task_by_vpid(who); 289 else 290 p = current; 291 if (p) { 292 niceval = nice_to_rlimit(task_nice(p)); 293 if (niceval > retval) 294 retval = niceval; 295 } 296 break; 297 case PRIO_PGRP: 298 if (who) 299 pgrp = find_vpid(who); 300 else 301 pgrp = task_pgrp(current); 302 do_each_pid_thread(pgrp, PIDTYPE_PGID, p) { 303 niceval = nice_to_rlimit(task_nice(p)); 304 if (niceval > retval) 305 retval = niceval; 306 } while_each_pid_thread(pgrp, PIDTYPE_PGID, p); 307 break; 308 case PRIO_USER: 309 uid = make_kuid(cred->user_ns, who); 310 user = cred->user; 311 if (!who) 312 uid = cred->uid; 313 else if (!uid_eq(uid, cred->uid)) { 314 user = find_user(uid); 315 if (!user) 316 goto out_unlock; /* No processes for this user */ 317 } 318 do_each_thread(g, p) { 319 if (uid_eq(task_uid(p), uid) && task_pid_vnr(p)) { 320 niceval = nice_to_rlimit(task_nice(p)); 321 if (niceval > retval) 322 retval = niceval; 323 } 324 } while_each_thread(g, p); 325 if (!uid_eq(uid, cred->uid)) 326 free_uid(user); /* for find_user() */ 327 break; 328 } 329 out_unlock: 330 read_unlock(&tasklist_lock); 331 rcu_read_unlock(); 332 333 return retval; 334 } 335 336 /* 337 * Unprivileged users may change the real gid to the effective gid 338 * or vice versa. (BSD-style) 339 * 340 * If you set the real gid at all, or set the effective gid to a value not 341 * equal to the real gid, then the saved gid is set to the new effective gid. 342 * 343 * This makes it possible for a setgid program to completely drop its 344 * privileges, which is often a useful assertion to make when you are doing 345 * a security audit over a program. 346 * 347 * The general idea is that a program which uses just setregid() will be 348 * 100% compatible with BSD. A program which uses just setgid() will be 349 * 100% compatible with POSIX with saved IDs. 350 * 351 * SMP: There are not races, the GIDs are checked only by filesystem 352 * operations (as far as semantic preservation is concerned). 353 */ 354 #ifdef CONFIG_MULTIUSER 355 long __sys_setregid(gid_t rgid, gid_t egid) 356 { 357 struct user_namespace *ns = current_user_ns(); 358 const struct cred *old; 359 struct cred *new; 360 int retval; 361 kgid_t krgid, kegid; 362 363 krgid = make_kgid(ns, rgid); 364 kegid = make_kgid(ns, egid); 365 366 if ((rgid != (gid_t) -1) && !gid_valid(krgid)) 367 return -EINVAL; 368 if ((egid != (gid_t) -1) && !gid_valid(kegid)) 369 return -EINVAL; 370 371 new = prepare_creds(); 372 if (!new) 373 return -ENOMEM; 374 old = current_cred(); 375 376 retval = -EPERM; 377 if (rgid != (gid_t) -1) { 378 if (gid_eq(old->gid, krgid) || 379 gid_eq(old->egid, krgid) || 380 ns_capable(old->user_ns, CAP_SETGID)) 381 new->gid = krgid; 382 else 383 goto error; 384 } 385 if (egid != (gid_t) -1) { 386 if (gid_eq(old->gid, kegid) || 387 gid_eq(old->egid, kegid) || 388 gid_eq(old->sgid, kegid) || 389 ns_capable(old->user_ns, CAP_SETGID)) 390 new->egid = kegid; 391 else 392 goto error; 393 } 394 395 if (rgid != (gid_t) -1 || 396 (egid != (gid_t) -1 && !gid_eq(kegid, old->gid))) 397 new->sgid = new->egid; 398 new->fsgid = new->egid; 399 400 return commit_creds(new); 401 402 error: 403 abort_creds(new); 404 return retval; 405 } 406 407 SYSCALL_DEFINE2(setregid, gid_t, rgid, gid_t, egid) 408 { 409 return __sys_setregid(rgid, egid); 410 } 411 412 /* 413 * setgid() is implemented like SysV w/ SAVED_IDS 414 * 415 * SMP: Same implicit races as above. 416 */ 417 long __sys_setgid(gid_t gid) 418 { 419 struct user_namespace *ns = current_user_ns(); 420 const struct cred *old; 421 struct cred *new; 422 int retval; 423 kgid_t kgid; 424 425 kgid = make_kgid(ns, gid); 426 if (!gid_valid(kgid)) 427 return -EINVAL; 428 429 new = prepare_creds(); 430 if (!new) 431 return -ENOMEM; 432 old = current_cred(); 433 434 retval = -EPERM; 435 if (ns_capable(old->user_ns, CAP_SETGID)) 436 new->gid = new->egid = new->sgid = new->fsgid = kgid; 437 else if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->sgid)) 438 new->egid = new->fsgid = kgid; 439 else 440 goto error; 441 442 return commit_creds(new); 443 444 error: 445 abort_creds(new); 446 return retval; 447 } 448 449 SYSCALL_DEFINE1(setgid, gid_t, gid) 450 { 451 return __sys_setgid(gid); 452 } 453 454 /* 455 * change the user struct in a credentials set to match the new UID 456 */ 457 static int set_user(struct cred *new) 458 { 459 struct user_struct *new_user; 460 461 new_user = alloc_uid(new->uid); 462 if (!new_user) 463 return -EAGAIN; 464 465 /* 466 * We don't fail in case of NPROC limit excess here because too many 467 * poorly written programs don't check set*uid() return code, assuming 468 * it never fails if called by root. We may still enforce NPROC limit 469 * for programs doing set*uid()+execve() by harmlessly deferring the 470 * failure to the execve() stage. 471 */ 472 if (atomic_read(&new_user->processes) >= rlimit(RLIMIT_NPROC) && 473 new_user != INIT_USER) 474 current->flags |= PF_NPROC_EXCEEDED; 475 else 476 current->flags &= ~PF_NPROC_EXCEEDED; 477 478 free_uid(new->user); 479 new->user = new_user; 480 return 0; 481 } 482 483 /* 484 * Unprivileged users may change the real uid to the effective uid 485 * or vice versa. (BSD-style) 486 * 487 * If you set the real uid at all, or set the effective uid to a value not 488 * equal to the real uid, then the saved uid is set to the new effective uid. 489 * 490 * This makes it possible for a setuid program to completely drop its 491 * privileges, which is often a useful assertion to make when you are doing 492 * a security audit over a program. 493 * 494 * The general idea is that a program which uses just setreuid() will be 495 * 100% compatible with BSD. A program which uses just setuid() will be 496 * 100% compatible with POSIX with saved IDs. 497 */ 498 long __sys_setreuid(uid_t ruid, uid_t euid) 499 { 500 struct user_namespace *ns = current_user_ns(); 501 const struct cred *old; 502 struct cred *new; 503 int retval; 504 kuid_t kruid, keuid; 505 506 kruid = make_kuid(ns, ruid); 507 keuid = make_kuid(ns, euid); 508 509 if ((ruid != (uid_t) -1) && !uid_valid(kruid)) 510 return -EINVAL; 511 if ((euid != (uid_t) -1) && !uid_valid(keuid)) 512 return -EINVAL; 513 514 new = prepare_creds(); 515 if (!new) 516 return -ENOMEM; 517 old = current_cred(); 518 519 retval = -EPERM; 520 if (ruid != (uid_t) -1) { 521 new->uid = kruid; 522 if (!uid_eq(old->uid, kruid) && 523 !uid_eq(old->euid, kruid) && 524 !ns_capable_setid(old->user_ns, CAP_SETUID)) 525 goto error; 526 } 527 528 if (euid != (uid_t) -1) { 529 new->euid = keuid; 530 if (!uid_eq(old->uid, keuid) && 531 !uid_eq(old->euid, keuid) && 532 !uid_eq(old->suid, keuid) && 533 !ns_capable_setid(old->user_ns, CAP_SETUID)) 534 goto error; 535 } 536 537 if (!uid_eq(new->uid, old->uid)) { 538 retval = set_user(new); 539 if (retval < 0) 540 goto error; 541 } 542 if (ruid != (uid_t) -1 || 543 (euid != (uid_t) -1 && !uid_eq(keuid, old->uid))) 544 new->suid = new->euid; 545 new->fsuid = new->euid; 546 547 retval = security_task_fix_setuid(new, old, LSM_SETID_RE); 548 if (retval < 0) 549 goto error; 550 551 return commit_creds(new); 552 553 error: 554 abort_creds(new); 555 return retval; 556 } 557 558 SYSCALL_DEFINE2(setreuid, uid_t, ruid, uid_t, euid) 559 { 560 return __sys_setreuid(ruid, euid); 561 } 562 563 /* 564 * setuid() is implemented like SysV with SAVED_IDS 565 * 566 * Note that SAVED_ID's is deficient in that a setuid root program 567 * like sendmail, for example, cannot set its uid to be a normal 568 * user and then switch back, because if you're root, setuid() sets 569 * the saved uid too. If you don't like this, blame the bright people 570 * in the POSIX committee and/or USG. Note that the BSD-style setreuid() 571 * will allow a root program to temporarily drop privileges and be able to 572 * regain them by swapping the real and effective uid. 573 */ 574 long __sys_setuid(uid_t uid) 575 { 576 struct user_namespace *ns = current_user_ns(); 577 const struct cred *old; 578 struct cred *new; 579 int retval; 580 kuid_t kuid; 581 582 kuid = make_kuid(ns, uid); 583 if (!uid_valid(kuid)) 584 return -EINVAL; 585 586 new = prepare_creds(); 587 if (!new) 588 return -ENOMEM; 589 old = current_cred(); 590 591 retval = -EPERM; 592 if (ns_capable_setid(old->user_ns, CAP_SETUID)) { 593 new->suid = new->uid = kuid; 594 if (!uid_eq(kuid, old->uid)) { 595 retval = set_user(new); 596 if (retval < 0) 597 goto error; 598 } 599 } else if (!uid_eq(kuid, old->uid) && !uid_eq(kuid, new->suid)) { 600 goto error; 601 } 602 603 new->fsuid = new->euid = kuid; 604 605 retval = security_task_fix_setuid(new, old, LSM_SETID_ID); 606 if (retval < 0) 607 goto error; 608 609 return commit_creds(new); 610 611 error: 612 abort_creds(new); 613 return retval; 614 } 615 616 SYSCALL_DEFINE1(setuid, uid_t, uid) 617 { 618 return __sys_setuid(uid); 619 } 620 621 622 /* 623 * This function implements a generic ability to update ruid, euid, 624 * and suid. This allows you to implement the 4.4 compatible seteuid(). 625 */ 626 long __sys_setresuid(uid_t ruid, uid_t euid, uid_t suid) 627 { 628 struct user_namespace *ns = current_user_ns(); 629 const struct cred *old; 630 struct cred *new; 631 int retval; 632 kuid_t kruid, keuid, ksuid; 633 634 kruid = make_kuid(ns, ruid); 635 keuid = make_kuid(ns, euid); 636 ksuid = make_kuid(ns, suid); 637 638 if ((ruid != (uid_t) -1) && !uid_valid(kruid)) 639 return -EINVAL; 640 641 if ((euid != (uid_t) -1) && !uid_valid(keuid)) 642 return -EINVAL; 643 644 if ((suid != (uid_t) -1) && !uid_valid(ksuid)) 645 return -EINVAL; 646 647 new = prepare_creds(); 648 if (!new) 649 return -ENOMEM; 650 651 old = current_cred(); 652 653 retval = -EPERM; 654 if (!ns_capable_setid(old->user_ns, CAP_SETUID)) { 655 if (ruid != (uid_t) -1 && !uid_eq(kruid, old->uid) && 656 !uid_eq(kruid, old->euid) && !uid_eq(kruid, old->suid)) 657 goto error; 658 if (euid != (uid_t) -1 && !uid_eq(keuid, old->uid) && 659 !uid_eq(keuid, old->euid) && !uid_eq(keuid, old->suid)) 660 goto error; 661 if (suid != (uid_t) -1 && !uid_eq(ksuid, old->uid) && 662 !uid_eq(ksuid, old->euid) && !uid_eq(ksuid, old->suid)) 663 goto error; 664 } 665 666 if (ruid != (uid_t) -1) { 667 new->uid = kruid; 668 if (!uid_eq(kruid, old->uid)) { 669 retval = set_user(new); 670 if (retval < 0) 671 goto error; 672 } 673 } 674 if (euid != (uid_t) -1) 675 new->euid = keuid; 676 if (suid != (uid_t) -1) 677 new->suid = ksuid; 678 new->fsuid = new->euid; 679 680 retval = security_task_fix_setuid(new, old, LSM_SETID_RES); 681 if (retval < 0) 682 goto error; 683 684 return commit_creds(new); 685 686 error: 687 abort_creds(new); 688 return retval; 689 } 690 691 SYSCALL_DEFINE3(setresuid, uid_t, ruid, uid_t, euid, uid_t, suid) 692 { 693 return __sys_setresuid(ruid, euid, suid); 694 } 695 696 SYSCALL_DEFINE3(getresuid, uid_t __user *, ruidp, uid_t __user *, euidp, uid_t __user *, suidp) 697 { 698 const struct cred *cred = current_cred(); 699 int retval; 700 uid_t ruid, euid, suid; 701 702 ruid = from_kuid_munged(cred->user_ns, cred->uid); 703 euid = from_kuid_munged(cred->user_ns, cred->euid); 704 suid = from_kuid_munged(cred->user_ns, cred->suid); 705 706 retval = put_user(ruid, ruidp); 707 if (!retval) { 708 retval = put_user(euid, euidp); 709 if (!retval) 710 return put_user(suid, suidp); 711 } 712 return retval; 713 } 714 715 /* 716 * Same as above, but for rgid, egid, sgid. 717 */ 718 long __sys_setresgid(gid_t rgid, gid_t egid, gid_t sgid) 719 { 720 struct user_namespace *ns = current_user_ns(); 721 const struct cred *old; 722 struct cred *new; 723 int retval; 724 kgid_t krgid, kegid, ksgid; 725 726 krgid = make_kgid(ns, rgid); 727 kegid = make_kgid(ns, egid); 728 ksgid = make_kgid(ns, sgid); 729 730 if ((rgid != (gid_t) -1) && !gid_valid(krgid)) 731 return -EINVAL; 732 if ((egid != (gid_t) -1) && !gid_valid(kegid)) 733 return -EINVAL; 734 if ((sgid != (gid_t) -1) && !gid_valid(ksgid)) 735 return -EINVAL; 736 737 new = prepare_creds(); 738 if (!new) 739 return -ENOMEM; 740 old = current_cred(); 741 742 retval = -EPERM; 743 if (!ns_capable(old->user_ns, CAP_SETGID)) { 744 if (rgid != (gid_t) -1 && !gid_eq(krgid, old->gid) && 745 !gid_eq(krgid, old->egid) && !gid_eq(krgid, old->sgid)) 746 goto error; 747 if (egid != (gid_t) -1 && !gid_eq(kegid, old->gid) && 748 !gid_eq(kegid, old->egid) && !gid_eq(kegid, old->sgid)) 749 goto error; 750 if (sgid != (gid_t) -1 && !gid_eq(ksgid, old->gid) && 751 !gid_eq(ksgid, old->egid) && !gid_eq(ksgid, old->sgid)) 752 goto error; 753 } 754 755 if (rgid != (gid_t) -1) 756 new->gid = krgid; 757 if (egid != (gid_t) -1) 758 new->egid = kegid; 759 if (sgid != (gid_t) -1) 760 new->sgid = ksgid; 761 new->fsgid = new->egid; 762 763 return commit_creds(new); 764 765 error: 766 abort_creds(new); 767 return retval; 768 } 769 770 SYSCALL_DEFINE3(setresgid, gid_t, rgid, gid_t, egid, gid_t, sgid) 771 { 772 return __sys_setresgid(rgid, egid, sgid); 773 } 774 775 SYSCALL_DEFINE3(getresgid, gid_t __user *, rgidp, gid_t __user *, egidp, gid_t __user *, sgidp) 776 { 777 const struct cred *cred = current_cred(); 778 int retval; 779 gid_t rgid, egid, sgid; 780 781 rgid = from_kgid_munged(cred->user_ns, cred->gid); 782 egid = from_kgid_munged(cred->user_ns, cred->egid); 783 sgid = from_kgid_munged(cred->user_ns, cred->sgid); 784 785 retval = put_user(rgid, rgidp); 786 if (!retval) { 787 retval = put_user(egid, egidp); 788 if (!retval) 789 retval = put_user(sgid, sgidp); 790 } 791 792 return retval; 793 } 794 795 796 /* 797 * "setfsuid()" sets the fsuid - the uid used for filesystem checks. This 798 * is used for "access()" and for the NFS daemon (letting nfsd stay at 799 * whatever uid it wants to). It normally shadows "euid", except when 800 * explicitly set by setfsuid() or for access.. 801 */ 802 long __sys_setfsuid(uid_t uid) 803 { 804 const struct cred *old; 805 struct cred *new; 806 uid_t old_fsuid; 807 kuid_t kuid; 808 809 old = current_cred(); 810 old_fsuid = from_kuid_munged(old->user_ns, old->fsuid); 811 812 kuid = make_kuid(old->user_ns, uid); 813 if (!uid_valid(kuid)) 814 return old_fsuid; 815 816 new = prepare_creds(); 817 if (!new) 818 return old_fsuid; 819 820 if (uid_eq(kuid, old->uid) || uid_eq(kuid, old->euid) || 821 uid_eq(kuid, old->suid) || uid_eq(kuid, old->fsuid) || 822 ns_capable_setid(old->user_ns, CAP_SETUID)) { 823 if (!uid_eq(kuid, old->fsuid)) { 824 new->fsuid = kuid; 825 if (security_task_fix_setuid(new, old, LSM_SETID_FS) == 0) 826 goto change_okay; 827 } 828 } 829 830 abort_creds(new); 831 return old_fsuid; 832 833 change_okay: 834 commit_creds(new); 835 return old_fsuid; 836 } 837 838 SYSCALL_DEFINE1(setfsuid, uid_t, uid) 839 { 840 return __sys_setfsuid(uid); 841 } 842 843 /* 844 * Samma pÃ¥ svenska.. 845 */ 846 long __sys_setfsgid(gid_t gid) 847 { 848 const struct cred *old; 849 struct cred *new; 850 gid_t old_fsgid; 851 kgid_t kgid; 852 853 old = current_cred(); 854 old_fsgid = from_kgid_munged(old->user_ns, old->fsgid); 855 856 kgid = make_kgid(old->user_ns, gid); 857 if (!gid_valid(kgid)) 858 return old_fsgid; 859 860 new = prepare_creds(); 861 if (!new) 862 return old_fsgid; 863 864 if (gid_eq(kgid, old->gid) || gid_eq(kgid, old->egid) || 865 gid_eq(kgid, old->sgid) || gid_eq(kgid, old->fsgid) || 866 ns_capable(old->user_ns, CAP_SETGID)) { 867 if (!gid_eq(kgid, old->fsgid)) { 868 new->fsgid = kgid; 869 goto change_okay; 870 } 871 } 872 873 abort_creds(new); 874 return old_fsgid; 875 876 change_okay: 877 commit_creds(new); 878 return old_fsgid; 879 } 880 881 SYSCALL_DEFINE1(setfsgid, gid_t, gid) 882 { 883 return __sys_setfsgid(gid); 884 } 885 #endif /* CONFIG_MULTIUSER */ 886 887 /** 888 * sys_getpid - return the thread group id of the current process 889 * 890 * Note, despite the name, this returns the tgid not the pid. The tgid and 891 * the pid are identical unless CLONE_THREAD was specified on clone() in 892 * which case the tgid is the same in all threads of the same group. 893 * 894 * This is SMP safe as current->tgid does not change. 895 */ 896 SYSCALL_DEFINE0(getpid) 897 { 898 return task_tgid_vnr(current); 899 } 900 901 /* Thread ID - the internal kernel "pid" */ 902 SYSCALL_DEFINE0(gettid) 903 { 904 return task_pid_vnr(current); 905 } 906 907 /* 908 * Accessing ->real_parent is not SMP-safe, it could 909 * change from under us. However, we can use a stale 910 * value of ->real_parent under rcu_read_lock(), see 911 * release_task()->call_rcu(delayed_put_task_struct). 912 */ 913 SYSCALL_DEFINE0(getppid) 914 { 915 int pid; 916 917 rcu_read_lock(); 918 pid = task_tgid_vnr(rcu_dereference(current->real_parent)); 919 rcu_read_unlock(); 920 921 return pid; 922 } 923 924 SYSCALL_DEFINE0(getuid) 925 { 926 /* Only we change this so SMP safe */ 927 return from_kuid_munged(current_user_ns(), current_uid()); 928 } 929 930 SYSCALL_DEFINE0(geteuid) 931 { 932 /* Only we change this so SMP safe */ 933 return from_kuid_munged(current_user_ns(), current_euid()); 934 } 935 936 SYSCALL_DEFINE0(getgid) 937 { 938 /* Only we change this so SMP safe */ 939 return from_kgid_munged(current_user_ns(), current_gid()); 940 } 941 942 SYSCALL_DEFINE0(getegid) 943 { 944 /* Only we change this so SMP safe */ 945 return from_kgid_munged(current_user_ns(), current_egid()); 946 } 947 948 static void do_sys_times(struct tms *tms) 949 { 950 u64 tgutime, tgstime, cutime, cstime; 951 952 thread_group_cputime_adjusted(current, &tgutime, &tgstime); 953 cutime = current->signal->cutime; 954 cstime = current->signal->cstime; 955 tms->tms_utime = nsec_to_clock_t(tgutime); 956 tms->tms_stime = nsec_to_clock_t(tgstime); 957 tms->tms_cutime = nsec_to_clock_t(cutime); 958 tms->tms_cstime = nsec_to_clock_t(cstime); 959 } 960 961 SYSCALL_DEFINE1(times, struct tms __user *, tbuf) 962 { 963 if (tbuf) { 964 struct tms tmp; 965 966 do_sys_times(&tmp); 967 if (copy_to_user(tbuf, &tmp, sizeof(struct tms))) 968 return -EFAULT; 969 } 970 force_successful_syscall_return(); 971 return (long) jiffies_64_to_clock_t(get_jiffies_64()); 972 } 973 974 #ifdef CONFIG_COMPAT 975 static compat_clock_t clock_t_to_compat_clock_t(clock_t x) 976 { 977 return compat_jiffies_to_clock_t(clock_t_to_jiffies(x)); 978 } 979 980 COMPAT_SYSCALL_DEFINE1(times, struct compat_tms __user *, tbuf) 981 { 982 if (tbuf) { 983 struct tms tms; 984 struct compat_tms tmp; 985 986 do_sys_times(&tms); 987 /* Convert our struct tms to the compat version. */ 988 tmp.tms_utime = clock_t_to_compat_clock_t(tms.tms_utime); 989 tmp.tms_stime = clock_t_to_compat_clock_t(tms.tms_stime); 990 tmp.tms_cutime = clock_t_to_compat_clock_t(tms.tms_cutime); 991 tmp.tms_cstime = clock_t_to_compat_clock_t(tms.tms_cstime); 992 if (copy_to_user(tbuf, &tmp, sizeof(tmp))) 993 return -EFAULT; 994 } 995 force_successful_syscall_return(); 996 return compat_jiffies_to_clock_t(jiffies); 997 } 998 #endif 999 1000 /* 1001 * This needs some heavy checking ... 1002 * I just haven't the stomach for it. I also don't fully 1003 * understand sessions/pgrp etc. Let somebody who does explain it. 1004 * 1005 * OK, I think I have the protection semantics right.... this is really 1006 * only important on a multi-user system anyway, to make sure one user 1007 * can't send a signal to a process owned by another. -TYT, 12/12/91 1008 * 1009 * !PF_FORKNOEXEC check to conform completely to POSIX. 1010 */ 1011 SYSCALL_DEFINE2(setpgid, pid_t, pid, pid_t, pgid) 1012 { 1013 struct task_struct *p; 1014 struct task_struct *group_leader = current->group_leader; 1015 struct pid *pgrp; 1016 int err; 1017 1018 if (!pid) 1019 pid = task_pid_vnr(group_leader); 1020 if (!pgid) 1021 pgid = pid; 1022 if (pgid < 0) 1023 return -EINVAL; 1024 rcu_read_lock(); 1025 1026 /* From this point forward we keep holding onto the tasklist lock 1027 * so that our parent does not change from under us. -DaveM 1028 */ 1029 write_lock_irq(&tasklist_lock); 1030 1031 err = -ESRCH; 1032 p = find_task_by_vpid(pid); 1033 if (!p) 1034 goto out; 1035 1036 err = -EINVAL; 1037 if (!thread_group_leader(p)) 1038 goto out; 1039 1040 if (same_thread_group(p->real_parent, group_leader)) { 1041 err = -EPERM; 1042 if (task_session(p) != task_session(group_leader)) 1043 goto out; 1044 err = -EACCES; 1045 if (!(p->flags & PF_FORKNOEXEC)) 1046 goto out; 1047 } else { 1048 err = -ESRCH; 1049 if (p != group_leader) 1050 goto out; 1051 } 1052 1053 err = -EPERM; 1054 if (p->signal->leader) 1055 goto out; 1056 1057 pgrp = task_pid(p); 1058 if (pgid != pid) { 1059 struct task_struct *g; 1060 1061 pgrp = find_vpid(pgid); 1062 g = pid_task(pgrp, PIDTYPE_PGID); 1063 if (!g || task_session(g) != task_session(group_leader)) 1064 goto out; 1065 } 1066 1067 err = security_task_setpgid(p, pgid); 1068 if (err) 1069 goto out; 1070 1071 if (task_pgrp(p) != pgrp) 1072 change_pid(p, PIDTYPE_PGID, pgrp); 1073 1074 err = 0; 1075 out: 1076 /* All paths lead to here, thus we are safe. -DaveM */ 1077 write_unlock_irq(&tasklist_lock); 1078 rcu_read_unlock(); 1079 return err; 1080 } 1081 1082 static int do_getpgid(pid_t pid) 1083 { 1084 struct task_struct *p; 1085 struct pid *grp; 1086 int retval; 1087 1088 rcu_read_lock(); 1089 if (!pid) 1090 grp = task_pgrp(current); 1091 else { 1092 retval = -ESRCH; 1093 p = find_task_by_vpid(pid); 1094 if (!p) 1095 goto out; 1096 grp = task_pgrp(p); 1097 if (!grp) 1098 goto out; 1099 1100 retval = security_task_getpgid(p); 1101 if (retval) 1102 goto out; 1103 } 1104 retval = pid_vnr(grp); 1105 out: 1106 rcu_read_unlock(); 1107 return retval; 1108 } 1109 1110 SYSCALL_DEFINE1(getpgid, pid_t, pid) 1111 { 1112 return do_getpgid(pid); 1113 } 1114 1115 #ifdef __ARCH_WANT_SYS_GETPGRP 1116 1117 SYSCALL_DEFINE0(getpgrp) 1118 { 1119 return do_getpgid(0); 1120 } 1121 1122 #endif 1123 1124 SYSCALL_DEFINE1(getsid, pid_t, pid) 1125 { 1126 struct task_struct *p; 1127 struct pid *sid; 1128 int retval; 1129 1130 rcu_read_lock(); 1131 if (!pid) 1132 sid = task_session(current); 1133 else { 1134 retval = -ESRCH; 1135 p = find_task_by_vpid(pid); 1136 if (!p) 1137 goto out; 1138 sid = task_session(p); 1139 if (!sid) 1140 goto out; 1141 1142 retval = security_task_getsid(p); 1143 if (retval) 1144 goto out; 1145 } 1146 retval = pid_vnr(sid); 1147 out: 1148 rcu_read_unlock(); 1149 return retval; 1150 } 1151 1152 static void set_special_pids(struct pid *pid) 1153 { 1154 struct task_struct *curr = current->group_leader; 1155 1156 if (task_session(curr) != pid) 1157 change_pid(curr, PIDTYPE_SID, pid); 1158 1159 if (task_pgrp(curr) != pid) 1160 change_pid(curr, PIDTYPE_PGID, pid); 1161 } 1162 1163 int ksys_setsid(void) 1164 { 1165 struct task_struct *group_leader = current->group_leader; 1166 struct pid *sid = task_pid(group_leader); 1167 pid_t session = pid_vnr(sid); 1168 int err = -EPERM; 1169 1170 write_lock_irq(&tasklist_lock); 1171 /* Fail if I am already a session leader */ 1172 if (group_leader->signal->leader) 1173 goto out; 1174 1175 /* Fail if a process group id already exists that equals the 1176 * proposed session id. 1177 */ 1178 if (pid_task(sid, PIDTYPE_PGID)) 1179 goto out; 1180 1181 group_leader->signal->leader = 1; 1182 set_special_pids(sid); 1183 1184 proc_clear_tty(group_leader); 1185 1186 err = session; 1187 out: 1188 write_unlock_irq(&tasklist_lock); 1189 if (err > 0) { 1190 proc_sid_connector(group_leader); 1191 sched_autogroup_create_attach(group_leader); 1192 } 1193 return err; 1194 } 1195 1196 SYSCALL_DEFINE0(setsid) 1197 { 1198 return ksys_setsid(); 1199 } 1200 1201 DECLARE_RWSEM(uts_sem); 1202 1203 #ifdef COMPAT_UTS_MACHINE 1204 #define override_architecture(name) \ 1205 (personality(current->personality) == PER_LINUX32 && \ 1206 copy_to_user(name->machine, COMPAT_UTS_MACHINE, \ 1207 sizeof(COMPAT_UTS_MACHINE))) 1208 #else 1209 #define override_architecture(name) 0 1210 #endif 1211 1212 /* 1213 * Work around broken programs that cannot handle "Linux 3.0". 1214 * Instead we map 3.x to 2.6.40+x, so e.g. 3.0 would be 2.6.40 1215 * And we map 4.x and later versions to 2.6.60+x, so 4.0/5.0/6.0/... would be 1216 * 2.6.60. 1217 */ 1218 static int override_release(char __user *release, size_t len) 1219 { 1220 int ret = 0; 1221 1222 if (current->personality & UNAME26) { 1223 const char *rest = UTS_RELEASE; 1224 char buf[65] = { 0 }; 1225 int ndots = 0; 1226 unsigned v; 1227 size_t copy; 1228 1229 while (*rest) { 1230 if (*rest == '.' && ++ndots >= 3) 1231 break; 1232 if (!isdigit(*rest) && *rest != '.') 1233 break; 1234 rest++; 1235 } 1236 v = ((LINUX_VERSION_CODE >> 8) & 0xff) + 60; 1237 copy = clamp_t(size_t, len, 1, sizeof(buf)); 1238 copy = scnprintf(buf, copy, "2.6.%u%s", v, rest); 1239 ret = copy_to_user(release, buf, copy + 1); 1240 } 1241 return ret; 1242 } 1243 1244 SYSCALL_DEFINE1(newuname, struct new_utsname __user *, name) 1245 { 1246 struct new_utsname tmp; 1247 1248 down_read(&uts_sem); 1249 memcpy(&tmp, utsname(), sizeof(tmp)); 1250 up_read(&uts_sem); 1251 if (copy_to_user(name, &tmp, sizeof(tmp))) 1252 return -EFAULT; 1253 1254 if (override_release(name->release, sizeof(name->release))) 1255 return -EFAULT; 1256 if (override_architecture(name)) 1257 return -EFAULT; 1258 return 0; 1259 } 1260 1261 #ifdef __ARCH_WANT_SYS_OLD_UNAME 1262 /* 1263 * Old cruft 1264 */ 1265 SYSCALL_DEFINE1(uname, struct old_utsname __user *, name) 1266 { 1267 struct old_utsname tmp; 1268 1269 if (!name) 1270 return -EFAULT; 1271 1272 down_read(&uts_sem); 1273 memcpy(&tmp, utsname(), sizeof(tmp)); 1274 up_read(&uts_sem); 1275 if (copy_to_user(name, &tmp, sizeof(tmp))) 1276 return -EFAULT; 1277 1278 if (override_release(name->release, sizeof(name->release))) 1279 return -EFAULT; 1280 if (override_architecture(name)) 1281 return -EFAULT; 1282 return 0; 1283 } 1284 1285 SYSCALL_DEFINE1(olduname, struct oldold_utsname __user *, name) 1286 { 1287 struct oldold_utsname tmp; 1288 1289 if (!name) 1290 return -EFAULT; 1291 1292 memset(&tmp, 0, sizeof(tmp)); 1293 1294 down_read(&uts_sem); 1295 memcpy(&tmp.sysname, &utsname()->sysname, __OLD_UTS_LEN); 1296 memcpy(&tmp.nodename, &utsname()->nodename, __OLD_UTS_LEN); 1297 memcpy(&tmp.release, &utsname()->release, __OLD_UTS_LEN); 1298 memcpy(&tmp.version, &utsname()->version, __OLD_UTS_LEN); 1299 memcpy(&tmp.machine, &utsname()->machine, __OLD_UTS_LEN); 1300 up_read(&uts_sem); 1301 if (copy_to_user(name, &tmp, sizeof(tmp))) 1302 return -EFAULT; 1303 1304 if (override_architecture(name)) 1305 return -EFAULT; 1306 if (override_release(name->release, sizeof(name->release))) 1307 return -EFAULT; 1308 return 0; 1309 } 1310 #endif 1311 1312 SYSCALL_DEFINE2(sethostname, char __user *, name, int, len) 1313 { 1314 int errno; 1315 char tmp[__NEW_UTS_LEN]; 1316 1317 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN)) 1318 return -EPERM; 1319 1320 if (len < 0 || len > __NEW_UTS_LEN) 1321 return -EINVAL; 1322 if (!ccs_capable(CCS_SYS_SETHOSTNAME)) 1323 return -EPERM; 1324 errno = -EFAULT; 1325 if (!copy_from_user(tmp, name, len)) { 1326 struct new_utsname *u; 1327 1328 down_write(&uts_sem); 1329 u = utsname(); 1330 memcpy(u->nodename, tmp, len); 1331 memset(u->nodename + len, 0, sizeof(u->nodename) - len); 1332 errno = 0; 1333 uts_proc_notify(UTS_PROC_HOSTNAME); 1334 up_write(&uts_sem); 1335 } 1336 return errno; 1337 } 1338 1339 #ifdef __ARCH_WANT_SYS_GETHOSTNAME 1340 1341 SYSCALL_DEFINE2(gethostname, char __user *, name, int, len) 1342 { 1343 int i; 1344 struct new_utsname *u; 1345 char tmp[__NEW_UTS_LEN + 1]; 1346 1347 if (len < 0) 1348 return -EINVAL; 1349 down_read(&uts_sem); 1350 u = utsname(); 1351 i = 1 + strlen(u->nodename); 1352 if (i > len) 1353 i = len; 1354 memcpy(tmp, u->nodename, i); 1355 up_read(&uts_sem); 1356 if (copy_to_user(name, tmp, i)) 1357 return -EFAULT; 1358 return 0; 1359 } 1360 1361 #endif 1362 1363 /* 1364 * Only setdomainname; getdomainname can be implemented by calling 1365 * uname() 1366 */ 1367 SYSCALL_DEFINE2(setdomainname, char __user *, name, int, len) 1368 { 1369 int errno; 1370 char tmp[__NEW_UTS_LEN]; 1371 1372 if (!ns_capable(current->nsproxy->uts_ns->user_ns, CAP_SYS_ADMIN)) 1373 return -EPERM; 1374 if (len < 0 || len > __NEW_UTS_LEN) 1375 return -EINVAL; 1376 if (!ccs_capable(CCS_SYS_SETHOSTNAME)) 1377 return -EPERM; 1378 1379 errno = -EFAULT; 1380 if (!copy_from_user(tmp, name, len)) { 1381 struct new_utsname *u; 1382 1383 down_write(&uts_sem); 1384 u = utsname(); 1385 memcpy(u->domainname, tmp, len); 1386 memset(u->domainname + len, 0, sizeof(u->domainname) - len); 1387 errno = 0; 1388 uts_proc_notify(UTS_PROC_DOMAINNAME); 1389 up_write(&uts_sem); 1390 } 1391 return errno; 1392 } 1393 1394 SYSCALL_DEFINE2(getrlimit, unsigned int, resource, struct rlimit __user *, rlim) 1395 { 1396 struct rlimit value; 1397 int ret; 1398 1399 ret = do_prlimit(current, resource, NULL, &value); 1400 if (!ret) 1401 ret = copy_to_user(rlim, &value, sizeof(*rlim)) ? -EFAULT : 0; 1402 1403 return ret; 1404 } 1405 1406 #ifdef CONFIG_COMPAT 1407 1408 COMPAT_SYSCALL_DEFINE2(setrlimit, unsigned int, resource, 1409 struct compat_rlimit __user *, rlim) 1410 { 1411 struct rlimit r; 1412 struct compat_rlimit r32; 1413 1414 if (copy_from_user(&r32, rlim, sizeof(struct compat_rlimit))) 1415 return -EFAULT; 1416 1417 if (r32.rlim_cur == COMPAT_RLIM_INFINITY) 1418 r.rlim_cur = RLIM_INFINITY; 1419 else 1420 r.rlim_cur = r32.rlim_cur; 1421 if (r32.rlim_max == COMPAT_RLIM_INFINITY) 1422 r.rlim_max = RLIM_INFINITY; 1423 else 1424 r.rlim_max = r32.rlim_max; 1425 return do_prlimit(current, resource, &r, NULL); 1426 } 1427 1428 COMPAT_SYSCALL_DEFINE2(getrlimit, unsigned int, resource, 1429 struct compat_rlimit __user *, rlim) 1430 { 1431 struct rlimit r; 1432 int ret; 1433 1434 ret = do_prlimit(current, resource, NULL, &r); 1435 if (!ret) { 1436 struct compat_rlimit r32; 1437 if (r.rlim_cur > COMPAT_RLIM_INFINITY) 1438 r32.rlim_cur = COMPAT_RLIM_INFINITY; 1439 else 1440 r32.rlim_cur = r.rlim_cur; 1441 if (r.rlim_max > COMPAT_RLIM_INFINITY) 1442 r32.rlim_max = COMPAT_RLIM_INFINITY; 1443 else 1444 r32.rlim_max = r.rlim_max; 1445 1446 if (copy_to_user(rlim, &r32, sizeof(struct compat_rlimit))) 1447 return -EFAULT; 1448 } 1449 return ret; 1450 } 1451 1452 #endif 1453 1454 #ifdef __ARCH_WANT_SYS_OLD_GETRLIMIT 1455 1456 /* 1457 * Back compatibility for getrlimit. Needed for some apps. 1458 */ 1459 SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource, 1460 struct rlimit __user *, rlim) 1461 { 1462 struct rlimit x; 1463 if (resource >= RLIM_NLIMITS) 1464 return -EINVAL; 1465 1466 resource = array_index_nospec(resource, RLIM_NLIMITS); 1467 task_lock(current->group_leader); 1468 x = current->signal->rlim[resource]; 1469 task_unlock(current->group_leader); 1470 if (x.rlim_cur > 0x7FFFFFFF) 1471 x.rlim_cur = 0x7FFFFFFF; 1472 if (x.rlim_max > 0x7FFFFFFF) 1473 x.rlim_max = 0x7FFFFFFF; 1474 return copy_to_user(rlim, &x, sizeof(x)) ? -EFAULT : 0; 1475 } 1476 1477 #ifdef CONFIG_COMPAT 1478 COMPAT_SYSCALL_DEFINE2(old_getrlimit, unsigned int, resource, 1479 struct compat_rlimit __user *, rlim) 1480 { 1481 struct rlimit r; 1482 1483 if (resource >= RLIM_NLIMITS) 1484 return -EINVAL; 1485 1486 resource = array_index_nospec(resource, RLIM_NLIMITS); 1487 task_lock(current->group_leader); 1488 r = current->signal->rlim[resource]; 1489 task_unlock(current->group_leader); 1490 if (r.rlim_cur > 0x7FFFFFFF) 1491 r.rlim_cur = 0x7FFFFFFF; 1492 if (r.rlim_max > 0x7FFFFFFF) 1493 r.rlim_max = 0x7FFFFFFF; 1494 1495 if (put_user(r.rlim_cur, &rlim->rlim_cur) || 1496 put_user(r.rlim_max, &rlim->rlim_max)) 1497 return -EFAULT; 1498 return 0; 1499 } 1500 #endif 1501 1502 #endif 1503 1504 static inline bool rlim64_is_infinity(__u64 rlim64) 1505 { 1506 #if BITS_PER_LONG < 64 1507 return rlim64 >= ULONG_MAX; 1508 #else 1509 return rlim64 == RLIM64_INFINITY; 1510 #endif 1511 } 1512 1513 static void rlim_to_rlim64(const struct rlimit *rlim, struct rlimit64 *rlim64) 1514 { 1515 if (rlim->rlim_cur == RLIM_INFINITY) 1516 rlim64->rlim_cur = RLIM64_INFINITY; 1517 else 1518 rlim64->rlim_cur = rlim->rlim_cur; 1519 if (rlim->rlim_max == RLIM_INFINITY) 1520 rlim64->rlim_max = RLIM64_INFINITY; 1521 else 1522 rlim64->rlim_max = rlim->rlim_max; 1523 } 1524 1525 static void rlim64_to_rlim(const struct rlimit64 *rlim64, struct rlimit *rlim) 1526 { 1527 if (rlim64_is_infinity(rlim64->rlim_cur)) 1528 rlim->rlim_cur = RLIM_INFINITY; 1529 else 1530 rlim->rlim_cur = (unsigned long)rlim64->rlim_cur; 1531 if (rlim64_is_infinity(rlim64->rlim_max)) 1532 rlim->rlim_max = RLIM_INFINITY; 1533 else 1534 rlim->rlim_max = (unsigned long)rlim64->rlim_max; 1535 } 1536 1537 /* make sure you are allowed to change @tsk limits before calling this */ 1538 int do_prlimit(struct task_struct *tsk, unsigned int resource, 1539 struct rlimit *new_rlim, struct rlimit *old_rlim) 1540 { 1541 struct rlimit *rlim; 1542 int retval = 0; 1543 1544 if (resource >= RLIM_NLIMITS) 1545 return -EINVAL; 1546 if (new_rlim) { 1547 if (new_rlim->rlim_cur > new_rlim->rlim_max) 1548 return -EINVAL; 1549 if (resource == RLIMIT_NOFILE && 1550 new_rlim->rlim_max > sysctl_nr_open) 1551 return -EPERM; 1552 } 1553 1554 /* protect tsk->signal and tsk->sighand from disappearing */ 1555 read_lock(&tasklist_lock); 1556 if (!tsk->sighand) { 1557 retval = -ESRCH; 1558 goto out; 1559 } 1560 1561 rlim = tsk->signal->rlim + resource; 1562 task_lock(tsk->group_leader); 1563 if (new_rlim) { 1564 /* Keep the capable check against init_user_ns until 1565 cgroups can contain all limits */ 1566 if (new_rlim->rlim_max > rlim->rlim_max && 1567 !capable(CAP_SYS_RESOURCE)) 1568 retval = -EPERM; 1569 if (!retval) 1570 retval = security_task_setrlimit(tsk, resource, new_rlim); 1571 } 1572 if (!retval) { 1573 if (old_rlim) 1574 *old_rlim = *rlim; 1575 if (new_rlim) 1576 *rlim = *new_rlim; 1577 } 1578 task_unlock(tsk->group_leader); 1579 1580 /* 1581 * RLIMIT_CPU handling. Arm the posix CPU timer if the limit is not 1582 * infite. In case of RLIM_INFINITY the posix CPU timer code 1583 * ignores the rlimit. 1584 */ 1585 if (!retval && new_rlim && resource == RLIMIT_CPU && 1586 new_rlim->rlim_cur != RLIM_INFINITY && 1587 IS_ENABLED(CONFIG_POSIX_TIMERS)) 1588 update_rlimit_cpu(tsk, new_rlim->rlim_cur); 1589 out: 1590 read_unlock(&tasklist_lock); 1591 return retval; 1592 } 1593 1594 /* rcu lock must be held */ 1595 static int check_prlimit_permission(struct task_struct *task, 1596 unsigned int flags) 1597 { 1598 const struct cred *cred = current_cred(), *tcred; 1599 bool id_match; 1600 1601 if (current == task) 1602 return 0; 1603 1604 tcred = __task_cred(task); 1605 id_match = (uid_eq(cred->uid, tcred->euid) && 1606 uid_eq(cred->uid, tcred->suid) && 1607 uid_eq(cred->uid, tcred->uid) && 1608 gid_eq(cred->gid, tcred->egid) && 1609 gid_eq(cred->gid, tcred->sgid) && 1610 gid_eq(cred->gid, tcred->gid)); 1611 if (!id_match && !ns_capable(tcred->user_ns, CAP_SYS_RESOURCE)) 1612 return -EPERM; 1613 1614 return security_task_prlimit(cred, tcred, flags); 1615 } 1616 1617 SYSCALL_DEFINE4(prlimit64, pid_t, pid, unsigned int, resource, 1618 const struct rlimit64 __user *, new_rlim, 1619 struct rlimit64 __user *, old_rlim) 1620 { 1621 struct rlimit64 old64, new64; 1622 struct rlimit old, new; 1623 struct task_struct *tsk; 1624 unsigned int checkflags = 0; 1625 int ret; 1626 1627 if (old_rlim) 1628 checkflags |= LSM_PRLIMIT_READ; 1629 1630 if (new_rlim) { 1631 if (copy_from_user(&new64, new_rlim, sizeof(new64))) 1632 return -EFAULT; 1633 rlim64_to_rlim(&new64, &new); 1634 checkflags |= LSM_PRLIMIT_WRITE; 1635 } 1636 1637 rcu_read_lock(); 1638 tsk = pid ? find_task_by_vpid(pid) : current; 1639 if (!tsk) { 1640 rcu_read_unlock(); 1641 return -ESRCH; 1642 } 1643 ret = check_prlimit_permission(tsk, checkflags); 1644 if (ret) { 1645 rcu_read_unlock(); 1646 return ret; 1647 } 1648 get_task_struct(tsk); 1649 rcu_read_unlock(); 1650 1651 ret = do_prlimit(tsk, resource, new_rlim ? &new : NULL, 1652 old_rlim ? &old : NULL); 1653 1654 if (!ret && old_rlim) { 1655 rlim_to_rlim64(&old, &old64); 1656 if (copy_to_user(old_rlim, &old64, sizeof(old64))) 1657 ret = -EFAULT; 1658 } 1659 1660 put_task_struct(tsk); 1661 return ret; 1662 } 1663 1664 SYSCALL_DEFINE2(setrlimit, unsigned int, resource, struct rlimit __user *, rlim) 1665 { 1666 struct rlimit new_rlim; 1667 1668 if (copy_from_user(&new_rlim, rlim, sizeof(*rlim))) 1669 return -EFAULT; 1670 return do_prlimit(current, resource, &new_rlim, NULL); 1671 } 1672 1673 /* 1674 * It would make sense to put struct rusage in the task_struct, 1675 * except that would make the task_struct be *really big*. After 1676 * task_struct gets moved into malloc'ed memory, it would 1677 * make sense to do this. It will make moving the rest of the information 1678 * a lot simpler! (Which we're not doing right now because we're not 1679 * measuring them yet). 1680 * 1681 * When sampling multiple threads for RUSAGE_SELF, under SMP we might have 1682 * races with threads incrementing their own counters. But since word 1683 * reads are atomic, we either get new values or old values and we don't 1684 * care which for the sums. We always take the siglock to protect reading 1685 * the c* fields from p->signal from races with exit.c updating those 1686 * fields when reaping, so a sample either gets all the additions of a 1687 * given child after it's reaped, or none so this sample is before reaping. 1688 * 1689 * Locking: 1690 * We need to take the siglock for CHILDEREN, SELF and BOTH 1691 * for the cases current multithreaded, non-current single threaded 1692 * non-current multithreaded. Thread traversal is now safe with 1693 * the siglock held. 1694 * Strictly speaking, we donot need to take the siglock if we are current and 1695 * single threaded, as no one else can take our signal_struct away, no one 1696 * else can reap the children to update signal->c* counters, and no one else 1697 * can race with the signal-> fields. If we do not take any lock, the 1698 * signal-> fields could be read out of order while another thread was just 1699 * exiting. So we should place a read memory barrier when we avoid the lock. 1700 * On the writer side, write memory barrier is implied in __exit_signal 1701 * as __exit_signal releases the siglock spinlock after updating the signal-> 1702 * fields. But we don't do this yet to keep things simple. 1703 * 1704 */ 1705 1706 static void accumulate_thread_rusage(struct task_struct *t, struct rusage *r) 1707 { 1708 r->ru_nvcsw += t->nvcsw; 1709 r->ru_nivcsw += t->nivcsw; 1710 r->ru_minflt += t->min_flt; 1711 r->ru_majflt += t->maj_flt; 1712 r->ru_inblock += task_io_get_inblock(t); 1713 r->ru_oublock += task_io_get_oublock(t); 1714 } 1715 1716 void getrusage(struct task_struct *p, int who, struct rusage *r) 1717 { 1718 struct task_struct *t; 1719 unsigned long flags; 1720 u64 tgutime, tgstime, utime, stime; 1721 unsigned long maxrss = 0; 1722 1723 memset((char *)r, 0, sizeof (*r)); 1724 utime = stime = 0; 1725 1726 if (who == RUSAGE_THREAD) { 1727 task_cputime_adjusted(current, &utime, &stime); 1728 accumulate_thread_rusage(p, r); 1729 maxrss = p->signal->maxrss; 1730 goto out; 1731 } 1732 1733 if (!lock_task_sighand(p, &flags)) 1734 return; 1735 1736 switch (who) { 1737 case RUSAGE_BOTH: 1738 case RUSAGE_CHILDREN: 1739 utime = p->signal->cutime; 1740 stime = p->signal->cstime; 1741 r->ru_nvcsw = p->signal->cnvcsw; 1742 r->ru_nivcsw = p->signal->cnivcsw; 1743 r->ru_minflt = p->signal->cmin_flt; 1744 r->ru_majflt = p->signal->cmaj_flt; 1745 r->ru_inblock = p->signal->cinblock; 1746 r->ru_oublock = p->signal->coublock; 1747 maxrss = p->signal->cmaxrss; 1748 1749 if (who == RUSAGE_CHILDREN) 1750 break; 1751 /* fall through */ 1752 1753 case RUSAGE_SELF: 1754 thread_group_cputime_adjusted(p, &tgutime, &tgstime); 1755 utime += tgutime; 1756 stime += tgstime; 1757 r->ru_nvcsw += p->signal->nvcsw; 1758 r->ru_nivcsw += p->signal->nivcsw; 1759 r->ru_minflt += p->signal->min_flt; 1760 r->ru_majflt += p->signal->maj_flt; 1761 r->ru_inblock += p->signal->inblock; 1762 r->ru_oublock += p->signal->oublock; 1763 if (maxrss < p->signal->maxrss) 1764 maxrss = p->signal->maxrss; 1765 t = p; 1766 do { 1767 accumulate_thread_rusage(t, r); 1768 } while_each_thread(p, t); 1769 break; 1770 1771 default: 1772 BUG(); 1773 } 1774 unlock_task_sighand(p, &flags); 1775 1776 out: 1777 r->ru_utime = ns_to_kernel_old_timeval(utime); 1778 r->ru_stime = ns_to_kernel_old_timeval(stime); 1779 1780 if (who != RUSAGE_CHILDREN) { 1781 struct mm_struct *mm = get_task_mm(p); 1782 1783 if (mm) { 1784 setmax_mm_hiwater_rss(&maxrss, mm); 1785 mmput(mm); 1786 } 1787 } 1788 r->ru_maxrss = maxrss * (PAGE_SIZE / 1024); /* convert pages to KBs */ 1789 } 1790 1791 SYSCALL_DEFINE2(getrusage, int, who, struct rusage __user *, ru) 1792 { 1793 struct rusage r; 1794 1795 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN && 1796 who != RUSAGE_THREAD) 1797 return -EINVAL; 1798 1799 getrusage(current, who, &r); 1800 return copy_to_user(ru, &r, sizeof(r)) ? -EFAULT : 0; 1801 } 1802 1803 #ifdef CONFIG_COMPAT 1804 COMPAT_SYSCALL_DEFINE2(getrusage, int, who, struct compat_rusage __user *, ru) 1805 { 1806 struct rusage r; 1807 1808 if (who != RUSAGE_SELF && who != RUSAGE_CHILDREN && 1809 who != RUSAGE_THREAD) 1810 return -EINVAL; 1811 1812 getrusage(current, who, &r); 1813 return put_compat_rusage(&r, ru); 1814 } 1815 #endif 1816 1817 SYSCALL_DEFINE1(umask, int, mask) 1818 { 1819 mask = xchg(¤t->fs->umask, mask & S_IRWXUGO); 1820 return mask; 1821 } 1822 1823 static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd) 1824 { 1825 struct fd exe; 1826 struct file *old_exe, *exe_file; 1827 struct inode *inode; 1828 int err; 1829 1830 exe = fdget(fd); 1831 if (!exe.file) 1832 return -EBADF; 1833 1834 inode = file_inode(exe.file); 1835 1836 /* 1837 * Because the original mm->exe_file points to executable file, make 1838 * sure that this one is executable as well, to avoid breaking an 1839 * overall picture. 1840 */ 1841 err = -EACCES; 1842 if (!S_ISREG(inode->i_mode) || path_noexec(&exe.file->f_path)) 1843 goto exit; 1844 1845 err = inode_permission(inode, MAY_EXEC); 1846 if (err) 1847 goto exit; 1848 1849 /* 1850 * Forbid mm->exe_file change if old file still mapped. 1851 */ 1852 exe_file = get_mm_exe_file(mm); 1853 err = -EBUSY; 1854 if (exe_file) { 1855 struct vm_area_struct *vma; 1856 1857 down_read(&mm->mmap_sem); 1858 for (vma = mm->mmap; vma; vma = vma->vm_next) { 1859 if (!vma->vm_file) 1860 continue; 1861 if (path_equal(&vma->vm_file->f_path, 1862 &exe_file->f_path)) 1863 goto exit_err; 1864 } 1865 1866 up_read(&mm->mmap_sem); 1867 fput(exe_file); 1868 } 1869 1870 err = 0; 1871 /* set the new file, lockless */ 1872 get_file(exe.file); 1873 old_exe = xchg(&mm->exe_file, exe.file); 1874 if (old_exe) 1875 fput(old_exe); 1876 exit: 1877 fdput(exe); 1878 return err; 1879 exit_err: 1880 up_read(&mm->mmap_sem); 1881 fput(exe_file); 1882 goto exit; 1883 } 1884 1885 /* 1886 * Check arithmetic relations of passed addresses. 1887 * 1888 * WARNING: we don't require any capability here so be very careful 1889 * in what is allowed for modification from userspace. 1890 */ 1891 static int validate_prctl_map_addr(struct prctl_mm_map *prctl_map) 1892 { 1893 unsigned long mmap_max_addr = TASK_SIZE; 1894 int error = -EINVAL, i; 1895 1896 static const unsigned char offsets[] = { 1897 offsetof(struct prctl_mm_map, start_code), 1898 offsetof(struct prctl_mm_map, end_code), 1899 offsetof(struct prctl_mm_map, start_data), 1900 offsetof(struct prctl_mm_map, end_data), 1901 offsetof(struct prctl_mm_map, start_brk), 1902 offsetof(struct prctl_mm_map, brk), 1903 offsetof(struct prctl_mm_map, start_stack), 1904 offsetof(struct prctl_mm_map, arg_start), 1905 offsetof(struct prctl_mm_map, arg_end), 1906 offsetof(struct prctl_mm_map, env_start), 1907 offsetof(struct prctl_mm_map, env_end), 1908 }; 1909 1910 /* 1911 * Make sure the members are not somewhere outside 1912 * of allowed address space. 1913 */ 1914 for (i = 0; i < ARRAY_SIZE(offsets); i++) { 1915 u64 val = *(u64 *)((char *)prctl_map + offsets[i]); 1916 1917 if ((unsigned long)val >= mmap_max_addr || 1918 (unsigned long)val < mmap_min_addr) 1919 goto out; 1920 } 1921 1922 /* 1923 * Make sure the pairs are ordered. 1924 */ 1925 #define __prctl_check_order(__m1, __op, __m2) \ 1926 ((unsigned long)prctl_map->__m1 __op \ 1927 (unsigned long)prctl_map->__m2) ? 0 : -EINVAL 1928 error = __prctl_check_order(start_code, <, end_code); 1929 error |= __prctl_check_order(start_data,<=, end_data); 1930 error |= __prctl_check_order(start_brk, <=, brk); 1931 error |= __prctl_check_order(arg_start, <=, arg_end); 1932 error |= __prctl_check_order(env_start, <=, env_end); 1933 if (error) 1934 goto out; 1935 #undef __prctl_check_order 1936 1937 error = -EINVAL; 1938 1939 /* 1940 * @brk should be after @end_data in traditional maps. 1941 */ 1942 if (prctl_map->start_brk <= prctl_map->end_data || 1943 prctl_map->brk <= prctl_map->end_data) 1944 goto out; 1945 1946 /* 1947 * Neither we should allow to override limits if they set. 1948 */ 1949 if (check_data_rlimit(rlimit(RLIMIT_DATA), prctl_map->brk, 1950 prctl_map->start_brk, prctl_map->end_data, 1951 prctl_map->start_data)) 1952 goto out; 1953 1954 error = 0; 1955 out: 1956 return error; 1957 } 1958 1959 #ifdef CONFIG_CHECKPOINT_RESTORE 1960 static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data_size) 1961 { 1962 struct prctl_mm_map prctl_map = { .exe_fd = (u32)-1, }; 1963 unsigned long user_auxv[AT_VECTOR_SIZE]; 1964 struct mm_struct *mm = current->mm; 1965 int error; 1966 1967 BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv)); 1968 BUILD_BUG_ON(sizeof(struct prctl_mm_map) > 256); 1969 1970 if (opt == PR_SET_MM_MAP_SIZE) 1971 return put_user((unsigned int)sizeof(prctl_map), 1972 (unsigned int __user *)addr); 1973 1974 if (data_size != sizeof(prctl_map)) 1975 return -EINVAL; 1976 1977 if (copy_from_user(&prctl_map, addr, sizeof(prctl_map))) 1978 return -EFAULT; 1979 1980 error = validate_prctl_map_addr(&prctl_map); 1981 if (error) 1982 return error; 1983 1984 if (prctl_map.auxv_size) { 1985 /* 1986 * Someone is trying to cheat the auxv vector. 1987 */ 1988 if (!prctl_map.auxv || 1989 prctl_map.auxv_size > sizeof(mm->saved_auxv)) 1990 return -EINVAL; 1991 1992 memset(user_auxv, 0, sizeof(user_auxv)); 1993 if (copy_from_user(user_auxv, 1994 (const void __user *)prctl_map.auxv, 1995 prctl_map.auxv_size)) 1996 return -EFAULT; 1997 1998 /* Last entry must be AT_NULL as specification requires */ 1999 user_auxv[AT_VECTOR_SIZE - 2] = AT_NULL; 2000 user_auxv[AT_VECTOR_SIZE - 1] = AT_NULL; 2001 } 2002 2003 if (prctl_map.exe_fd != (u32)-1) { 2004 /* 2005 * Make sure the caller has the rights to 2006 * change /proc/pid/exe link: only local sys admin should 2007 * be allowed to. 2008 */ 2009 if (!ns_capable(current_user_ns(), CAP_SYS_ADMIN)) 2010 return -EINVAL; 2011 2012 error = prctl_set_mm_exe_file(mm, prctl_map.exe_fd); 2013 if (error) 2014 return error; 2015 } 2016 2017 /* 2018 * arg_lock protects concurent updates but we still need mmap_sem for 2019 * read to exclude races with sys_brk. 2020 */ 2021 down_read(&mm->mmap_sem); 2022 2023 /* 2024 * We don't validate if these members are pointing to 2025 * real present VMAs because application may have correspond 2026 * VMAs already unmapped and kernel uses these members for statistics 2027 * output in procfs mostly, except 2028 * 2029 * - @start_brk/@brk which are used in do_brk but kernel lookups 2030 * for VMAs when updating these memvers so anything wrong written 2031 * here cause kernel to swear at userspace program but won't lead 2032 * to any problem in kernel itself 2033 */ 2034 2035 spin_lock(&mm->arg_lock); 2036 mm->start_code = prctl_map.start_code; 2037 mm->end_code = prctl_map.end_code; 2038 mm->start_data = prctl_map.start_data; 2039 mm->end_data = prctl_map.end_data; 2040 mm->start_brk = prctl_map.start_brk; 2041 mm->brk = prctl_map.brk; 2042 mm->start_stack = prctl_map.start_stack; 2043 mm->arg_start = prctl_map.arg_start; 2044 mm->arg_end = prctl_map.arg_end; 2045 mm->env_start = prctl_map.env_start; 2046 mm->env_end = prctl_map.env_end; 2047 spin_unlock(&mm->arg_lock); 2048 2049 /* 2050 * Note this update of @saved_auxv is lockless thus 2051 * if someone reads this member in procfs while we're 2052 * updating -- it may get partly updated results. It's 2053 * known and acceptable trade off: we leave it as is to 2054 * not introduce additional locks here making the kernel 2055 * more complex. 2056 */ 2057 if (prctl_map.auxv_size) 2058 memcpy(mm->saved_auxv, user_auxv, sizeof(user_auxv)); 2059 2060 up_read(&mm->mmap_sem); 2061 return 0; 2062 } 2063 #endif /* CONFIG_CHECKPOINT_RESTORE */ 2064 2065 static int prctl_set_auxv(struct mm_struct *mm, unsigned long addr, 2066 unsigned long len) 2067 { 2068 /* 2069 * This doesn't move the auxiliary vector itself since it's pinned to 2070 * mm_struct, but it permits filling the vector with new values. It's 2071 * up to the caller to provide sane values here, otherwise userspace 2072 * tools which use this vector might be unhappy. 2073 */ 2074 unsigned long user_auxv[AT_VECTOR_SIZE]; 2075 2076 if (len > sizeof(user_auxv)) 2077 return -EINVAL; 2078 2079 if (copy_from_user(user_auxv, (const void __user *)addr, len)) 2080 return -EFAULT; 2081 2082 /* Make sure the last entry is always AT_NULL */ 2083 user_auxv[AT_VECTOR_SIZE - 2] = 0; 2084 user_auxv[AT_VECTOR_SIZE - 1] = 0; 2085 2086 BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv)); 2087 2088 task_lock(current); 2089 memcpy(mm->saved_auxv, user_auxv, len); 2090 task_unlock(current); 2091 2092 return 0; 2093 } 2094 2095 static int prctl_set_mm(int opt, unsigned long addr, 2096 unsigned long arg4, unsigned long arg5) 2097 { 2098 struct mm_struct *mm = current->mm; 2099 struct prctl_mm_map prctl_map = { 2100 .auxv = NULL, 2101 .auxv_size = 0, 2102 .exe_fd = -1, 2103 }; 2104 struct vm_area_struct *vma; 2105 int error; 2106 2107 if (arg5 || (arg4 && (opt != PR_SET_MM_AUXV && 2108 opt != PR_SET_MM_MAP && 2109 opt != PR_SET_MM_MAP_SIZE))) 2110 return -EINVAL; 2111 2112 #ifdef CONFIG_CHECKPOINT_RESTORE 2113 if (opt == PR_SET_MM_MAP || opt == PR_SET_MM_MAP_SIZE) 2114 return prctl_set_mm_map(opt, (const void __user *)addr, arg4); 2115 #endif 2116 2117 if (!capable(CAP_SYS_RESOURCE)) 2118 return -EPERM; 2119 2120 if (opt == PR_SET_MM_EXE_FILE) 2121 return prctl_set_mm_exe_file(mm, (unsigned int)addr); 2122 2123 if (opt == PR_SET_MM_AUXV) 2124 return prctl_set_auxv(mm, addr, arg4); 2125 2126 if (addr >= TASK_SIZE || addr < mmap_min_addr) 2127 return -EINVAL; 2128 2129 error = -EINVAL; 2130 2131 /* 2132 * arg_lock protects concurent updates of arg boundaries, we need 2133 * mmap_sem for a) concurrent sys_brk, b) finding VMA for addr 2134 * validation. 2135 */ 2136 down_read(&mm->mmap_sem); 2137 vma = find_vma(mm, addr); 2138 2139 spin_lock(&mm->arg_lock); 2140 prctl_map.start_code = mm->start_code; 2141 prctl_map.end_code = mm->end_code; 2142 prctl_map.start_data = mm->start_data; 2143 prctl_map.end_data = mm->end_data; 2144 prctl_map.start_brk = mm->start_brk; 2145 prctl_map.brk = mm->brk; 2146 prctl_map.start_stack = mm->start_stack; 2147 prctl_map.arg_start = mm->arg_start; 2148 prctl_map.arg_end = mm->arg_end; 2149 prctl_map.env_start = mm->env_start; 2150 prctl_map.env_end = mm->env_end; 2151 2152 switch (opt) { 2153 case PR_SET_MM_START_CODE: 2154 prctl_map.start_code = addr; 2155 break; 2156 case PR_SET_MM_END_CODE: 2157 prctl_map.end_code = addr; 2158 break; 2159 case PR_SET_MM_START_DATA: 2160 prctl_map.start_data = addr; 2161 break; 2162 case PR_SET_MM_END_DATA: 2163 prctl_map.end_data = addr; 2164 break; 2165 case PR_SET_MM_START_STACK: 2166 prctl_map.start_stack = addr; 2167 break; 2168 case PR_SET_MM_START_BRK: 2169 prctl_map.start_brk = addr; 2170 break; 2171 case PR_SET_MM_BRK: 2172 prctl_map.brk = addr; 2173 break; 2174 case PR_SET_MM_ARG_START: 2175 prctl_map.arg_start = addr; 2176 break; 2177 case PR_SET_MM_ARG_END: 2178 prctl_map.arg_end = addr; 2179 break; 2180 case PR_SET_MM_ENV_START: 2181 prctl_map.env_start = addr; 2182 break; 2183 case PR_SET_MM_ENV_END: 2184 prctl_map.env_end = addr; 2185 break; 2186 default: 2187 goto out; 2188 } 2189 2190 error = validate_prctl_map_addr(&prctl_map); 2191 if (error) 2192 goto out; 2193 2194 switch (opt) { 2195 /* 2196 * If command line arguments and environment 2197 * are placed somewhere else on stack, we can 2198 * set them up here, ARG_START/END to setup 2199 * command line argumets and ENV_START/END 2200 * for environment. 2201 */ 2202 case PR_SET_MM_START_STACK: 2203 case PR_SET_MM_ARG_START: 2204 case PR_SET_MM_ARG_END: 2205 case PR_SET_MM_ENV_START: 2206 case PR_SET_MM_ENV_END: 2207 if (!vma) { 2208 error = -EFAULT; 2209 goto out; 2210 } 2211 } 2212 2213 mm->start_code = prctl_map.start_code; 2214 mm->end_code = prctl_map.end_code; 2215 mm->start_data = prctl_map.start_data; 2216 mm->end_data = prctl_map.end_data; 2217 mm->start_brk = prctl_map.start_brk; 2218 mm->brk = prctl_map.brk; 2219 mm->start_stack = prctl_map.start_stack; 2220 mm->arg_start = prctl_map.arg_start; 2221 mm->arg_end = prctl_map.arg_end; 2222 mm->env_start = prctl_map.env_start; 2223 mm->env_end = prctl_map.env_end; 2224 2225 error = 0; 2226 out: 2227 spin_unlock(&mm->arg_lock); 2228 up_read(&mm->mmap_sem); 2229 return error; 2230 } 2231 2232 #ifdef CONFIG_CHECKPOINT_RESTORE 2233 static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr) 2234 { 2235 return put_user(me->clear_child_tid, tid_addr); 2236 } 2237 #else 2238 static int prctl_get_tid_address(struct task_struct *me, int __user **tid_addr) 2239 { 2240 return -EINVAL; 2241 } 2242 #endif 2243 2244 static int propagate_has_child_subreaper(struct task_struct *p, void *data) 2245 { 2246 /* 2247 * If task has has_child_subreaper - all its decendants 2248 * already have these flag too and new decendants will 2249 * inherit it on fork, skip them. 2250 * 2251 * If we've found child_reaper - skip descendants in 2252 * it's subtree as they will never get out pidns. 2253 */ 2254 if (p->signal->has_child_subreaper || 2255 is_child_reaper(task_pid(p))) 2256 return 0; 2257 2258 p->signal->has_child_subreaper = 1; 2259 return 1; 2260 } 2261 2262 int __weak arch_prctl_spec_ctrl_get(struct task_struct *t, unsigned long which) 2263 { 2264 return -EINVAL; 2265 } 2266 2267 int __weak arch_prctl_spec_ctrl_set(struct task_struct *t, unsigned long which, 2268 unsigned long ctrl) 2269 { 2270 return -EINVAL; 2271 } 2272 2273 #define PR_IO_FLUSHER (PF_MEMALLOC_NOIO | PF_LESS_THROTTLE) 2274 2275 SYSCALL_DEFINE5(prctl, int, option, unsigned long, arg2, unsigned long, arg3, 2276 unsigned long, arg4, unsigned long, arg5) 2277 { 2278 struct task_struct *me = current; 2279 unsigned char comm[sizeof(me->comm)]; 2280 long error; 2281 2282 error = security_task_prctl(option, arg2, arg3, arg4, arg5); 2283 if (error != -ENOSYS) 2284 return error; 2285 2286 error = 0; 2287 switch (option) { 2288 case PR_SET_PDEATHSIG: 2289 if (!valid_signal(arg2)) { 2290 error = -EINVAL; 2291 break; 2292 } 2293 me->pdeath_signal = arg2; 2294 break; 2295 case PR_GET_PDEATHSIG: 2296 error = put_user(me->pdeath_signal, (int __user *)arg2); 2297 break; 2298 case PR_GET_DUMPABLE: 2299 error = get_dumpable(me->mm); 2300 break; 2301 case PR_SET_DUMPABLE: 2302 if (arg2 != SUID_DUMP_DISABLE && arg2 != SUID_DUMP_USER) { 2303 error = -EINVAL; 2304 break; 2305 } 2306 set_dumpable(me->mm, arg2); 2307 break; 2308 2309 case PR_SET_UNALIGN: 2310 error = SET_UNALIGN_CTL(me, arg2); 2311 break; 2312 case PR_GET_UNALIGN: 2313 error = GET_UNALIGN_CTL(me, arg2); 2314 break; 2315 case PR_SET_FPEMU: 2316 error = SET_FPEMU_CTL(me, arg2); 2317 break; 2318 case PR_GET_FPEMU: 2319 error = GET_FPEMU_CTL(me, arg2); 2320 break; 2321 case PR_SET_FPEXC: 2322 error = SET_FPEXC_CTL(me, arg2); 2323 break; 2324 case PR_GET_FPEXC: 2325 error = GET_FPEXC_CTL(me, arg2); 2326 break; 2327 case PR_GET_TIMING: 2328 error = PR_TIMING_STATISTICAL; 2329 break; 2330 case PR_SET_TIMING: 2331 if (arg2 != PR_TIMING_STATISTICAL) 2332 error = -EINVAL; 2333 break; 2334 case PR_SET_NAME: 2335 comm[sizeof(me->comm) - 1] = 0; 2336 if (strncpy_from_user(comm, (char __user *)arg2, 2337 sizeof(me->comm) - 1) < 0) 2338 return -EFAULT; 2339 set_task_comm(me, comm); 2340 proc_comm_connector(me); 2341 break; 2342 case PR_GET_NAME: 2343 get_task_comm(comm, me); 2344 if (copy_to_user((char __user *)arg2, comm, sizeof(comm))) 2345 return -EFAULT; 2346 break; 2347 case PR_GET_ENDIAN: 2348 error = GET_ENDIAN(me, arg2); 2349 break; 2350 case PR_SET_ENDIAN: 2351 error = SET_ENDIAN(me, arg2); 2352 break; 2353 case PR_GET_SECCOMP: 2354 error = prctl_get_seccomp(); 2355 break; 2356 case PR_SET_SECCOMP: 2357 error = prctl_set_seccomp(arg2, (char __user *)arg3); 2358 break; 2359 case PR_GET_TSC: 2360 error = GET_TSC_CTL(arg2); 2361 break; 2362 case PR_SET_TSC: 2363 error = SET_TSC_CTL(arg2); 2364 break; 2365 case PR_TASK_PERF_EVENTS_DISABLE: 2366 error = perf_event_task_disable(); 2367 break; 2368 case PR_TASK_PERF_EVENTS_ENABLE: 2369 error = perf_event_task_enable(); 2370 break; 2371 case PR_GET_TIMERSLACK: 2372 if (current->timer_slack_ns > ULONG_MAX) 2373 error = ULONG_MAX; 2374 else 2375 error = current->timer_slack_ns; 2376 break; 2377 case PR_SET_TIMERSLACK: 2378 if (arg2 <= 0) 2379 current->timer_slack_ns = 2380 current->default_timer_slack_ns; 2381 else 2382 current->timer_slack_ns = arg2; 2383 break; 2384 case PR_MCE_KILL: 2385 if (arg4 | arg5) 2386 return -EINVAL; 2387 switch (arg2) { 2388 case PR_MCE_KILL_CLEAR: 2389 if (arg3 != 0) 2390 return -EINVAL; 2391 current->flags &= ~PF_MCE_PROCESS; 2392 break; 2393 case PR_MCE_KILL_SET: 2394 current->flags |= PF_MCE_PROCESS; 2395 if (arg3 == PR_MCE_KILL_EARLY) 2396 current->flags |= PF_MCE_EARLY; 2397 else if (arg3 == PR_MCE_KILL_LATE) 2398 current->flags &= ~PF_MCE_EARLY; 2399 else if (arg3 == PR_MCE_KILL_DEFAULT) 2400 current->flags &= 2401 ~(PF_MCE_EARLY|PF_MCE_PROCESS); 2402 else 2403 return -EINVAL; 2404 break; 2405 default: 2406 return -EINVAL; 2407 } 2408 break; 2409 case PR_MCE_KILL_GET: 2410 if (arg2 | arg3 | arg4 | arg5) 2411 return -EINVAL; 2412 if (current->flags & PF_MCE_PROCESS) 2413 error = (current->flags & PF_MCE_EARLY) ? 2414 PR_MCE_KILL_EARLY : PR_MCE_KILL_LATE; 2415 else 2416 error = PR_MCE_KILL_DEFAULT; 2417 break; 2418 case PR_SET_MM: 2419 error = prctl_set_mm(arg2, arg3, arg4, arg5); 2420 break; 2421 case PR_GET_TID_ADDRESS: 2422 error = prctl_get_tid_address(me, (int __user **)arg2); 2423 break; 2424 case PR_SET_CHILD_SUBREAPER: 2425 me->signal->is_child_subreaper = !!arg2; 2426 if (!arg2) 2427 break; 2428 2429 walk_process_tree(me, propagate_has_child_subreaper, NULL); 2430 break; 2431 case PR_GET_CHILD_SUBREAPER: 2432 error = put_user(me->signal->is_child_subreaper, 2433 (int __user *)arg2); 2434 break; 2435 case PR_SET_NO_NEW_PRIVS: 2436 if (arg2 != 1 || arg3 || arg4 || arg5) 2437 return -EINVAL; 2438 2439 task_set_no_new_privs(current); 2440 break; 2441 case PR_GET_NO_NEW_PRIVS: 2442 if (arg2 || arg3 || arg4 || arg5) 2443 return -EINVAL; 2444 return task_no_new_privs(current) ? 1 : 0; 2445 case PR_GET_THP_DISABLE: 2446 if (arg2 || arg3 || arg4 || arg5) 2447 return -EINVAL; 2448 error = !!test_bit(MMF_DISABLE_THP, &me->mm->flags); 2449 break; 2450 case PR_SET_THP_DISABLE: 2451 if (arg3 || arg4 || arg5) 2452 return -EINVAL; 2453 if (down_write_killable(&me->mm->mmap_sem)) 2454 return -EINTR; 2455 if (arg2) 2456 set_bit(MMF_DISABLE_THP, &me->mm->flags); 2457 else 2458 clear_bit(MMF_DISABLE_THP, &me->mm->flags); 2459 up_write(&me->mm->mmap_sem); 2460 break; 2461 case PR_MPX_ENABLE_MANAGEMENT: 2462 case PR_MPX_DISABLE_MANAGEMENT: 2463 /* No longer implemented: */ 2464 return -EINVAL; 2465 case PR_SET_FP_MODE: 2466 error = SET_FP_MODE(me, arg2); 2467 break; 2468 case PR_GET_FP_MODE: 2469 error = GET_FP_MODE(me); 2470 break; 2471 case PR_SVE_SET_VL: 2472 error = SVE_SET_VL(arg2); 2473 break; 2474 case PR_SVE_GET_VL: 2475 error = SVE_GET_VL(); 2476 break; 2477 case PR_GET_SPECULATION_CTRL: 2478 if (arg3 || arg4 || arg5) 2479 return -EINVAL; 2480 error = arch_prctl_spec_ctrl_get(me, arg2); 2481 break; 2482 case PR_SET_SPECULATION_CTRL: 2483 if (arg4 || arg5) 2484 return -EINVAL; 2485 error = arch_prctl_spec_ctrl_set(me, arg2, arg3); 2486 break; 2487 case PR_PAC_RESET_KEYS: 2488 if (arg3 || arg4 || arg5) 2489 return -EINVAL; 2490 error = PAC_RESET_KEYS(me, arg2); 2491 break; 2492 case PR_SET_TAGGED_ADDR_CTRL: 2493 if (arg3 || arg4 || arg5) 2494 return -EINVAL; 2495 error = SET_TAGGED_ADDR_CTRL(arg2); 2496 break; 2497 case PR_GET_TAGGED_ADDR_CTRL: 2498 if (arg2 || arg3 || arg4 || arg5) 2499 return -EINVAL; 2500 error = GET_TAGGED_ADDR_CTRL(); 2501 break; 2502 case PR_SET_IO_FLUSHER: 2503 if (!capable(CAP_SYS_RESOURCE)) 2504 return -EPERM; 2505 2506 if (arg3 || arg4 || arg5) 2507 return -EINVAL; 2508 2509 if (arg2 == 1) 2510 current->flags |= PR_IO_FLUSHER; 2511 else if (!arg2) 2512 current->flags &= ~PR_IO_FLUSHER; 2513 else 2514 return -EINVAL; 2515 break; 2516 case PR_GET_IO_FLUSHER: 2517 if (!capable(CAP_SYS_RESOURCE)) 2518 return -EPERM; 2519 2520 if (arg2 || arg3 || arg4 || arg5) 2521 return -EINVAL; 2522 2523 error = (current->flags & PR_IO_FLUSHER) == PR_IO_FLUSHER; 2524 break; 2525 default: 2526 error = -EINVAL; 2527 break; 2528 } 2529 return error; 2530 } 2531 2532 SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep, 2533 struct getcpu_cache __user *, unused) 2534 { 2535 int err = 0; 2536 int cpu = raw_smp_processor_id(); 2537 2538 if (cpup) 2539 err |= put_user(cpu, cpup); 2540 if (nodep) 2541 err |= put_user(cpu_to_node(cpu), nodep); 2542 return err ? -EFAULT : 0; 2543 } 2544 2545 /** 2546 * do_sysinfo - fill in sysinfo struct 2547 * @info: pointer to buffer to fill 2548 */ 2549 static int do_sysinfo(struct sysinfo *info) 2550 { 2551 unsigned long mem_total, sav_total; 2552 unsigned int mem_unit, bitcount; 2553 struct timespec64 tp; 2554 2555 memset(info, 0, sizeof(struct sysinfo)); 2556 2557 ktime_get_boottime_ts64(&tp); 2558 timens_add_boottime(&tp); 2559 info->uptime = tp.tv_sec + (tp.tv_nsec ? 1 : 0); 2560 2561 get_avenrun(info->loads, 0, SI_LOAD_SHIFT - FSHIFT); 2562 2563 info->procs = nr_threads; 2564 2565 si_meminfo(info); 2566 si_swapinfo(info); 2567 2568 /* 2569 * If the sum of all the available memory (i.e. ram + swap) 2570 * is less than can be stored in a 32 bit unsigned long then 2571 * we can be binary compatible with 2.2.x kernels. If not, 2572 * well, in that case 2.2.x was broken anyways... 2573 * 2574 * -Erik Andersen <andersee@debian.org> 2575 */ 2576 2577 mem_total = info->totalram + info->totalswap; 2578 if (mem_total < info->totalram || mem_total < info->totalswap) 2579 goto out; 2580 bitcount = 0; 2581 mem_unit = info->mem_unit; 2582 while (mem_unit > 1) { 2583 bitcount++; 2584 mem_unit >>= 1; 2585 sav_total = mem_total; 2586 mem_total <<= 1; 2587 if (mem_total < sav_total) 2588 goto out; 2589 } 2590 2591 /* 2592 * If mem_total did not overflow, multiply all memory values by 2593 * info->mem_unit and set it to 1. This leaves things compatible 2594 * with 2.2.x, and also retains compatibility with earlier 2.4.x 2595 * kernels... 2596 */ 2597 2598 info->mem_unit = 1; 2599 info->totalram <<= bitcount; 2600 info->freeram <<= bitcount; 2601 info->sharedram <<= bitcount; 2602 info->bufferram <<= bitcount; 2603 info->totalswap <<= bitcount; 2604 info->freeswap <<= bitcount; 2605 info->totalhigh <<= bitcount; 2606 info->freehigh <<= bitcount; 2607 2608 out: 2609 return 0; 2610 } 2611 2612 SYSCALL_DEFINE1(sysinfo, struct sysinfo __user *, info) 2613 { 2614 struct sysinfo val; 2615 2616 do_sysinfo(&val); 2617 2618 if (copy_to_user(info, &val, sizeof(struct sysinfo))) 2619 return -EFAULT; 2620 2621 return 0; 2622 } 2623 2624 #ifdef CONFIG_COMPAT 2625 struct compat_sysinfo { 2626 s32 uptime; 2627 u32 loads[3]; 2628 u32 totalram; 2629 u32 freeram; 2630 u32 sharedram; 2631 u32 bufferram; 2632 u32 totalswap; 2633 u32 freeswap; 2634 u16 procs; 2635 u16 pad; 2636 u32 totalhigh; 2637 u32 freehigh; 2638 u32 mem_unit; 2639 char _f[20-2*sizeof(u32)-sizeof(int)]; 2640 }; 2641 2642 COMPAT_SYSCALL_DEFINE1(sysinfo, struct compat_sysinfo __user *, info) 2643 { 2644 struct sysinfo s; 2645 2646 do_sysinfo(&s); 2647 2648 /* Check to see if any memory value is too large for 32-bit and scale 2649 * down if needed 2650 */ 2651 if (upper_32_bits(s.totalram) || upper_32_bits(s.totalswap)) { 2652 int bitcount = 0; 2653 2654 while (s.mem_unit < PAGE_SIZE) { 2655 s.mem_unit <<= 1; 2656 bitcount++; 2657 } 2658 2659 s.totalram >>= bitcount; 2660 s.freeram >>= bitcount; 2661 s.sharedram >>= bitcount; 2662 s.bufferram >>= bitcount; 2663 s.totalswap >>= bitcount; 2664 s.freeswap >>= bitcount; 2665 s.totalhigh >>= bitcount; 2666 s.freehigh >>= bitcount; 2667 } 2668 2669 if (!access_ok(info, sizeof(struct compat_sysinfo)) || 2670 __put_user(s.uptime, &info->uptime) || 2671 __put_user(s.loads[0], &info->loads[0]) || 2672 __put_user(s.loads[1], &info->loads[1]) || 2673 __put_user(s.loads[2], &info->loads[2]) || 2674 __put_user(s.totalram, &info->totalram) || 2675 __put_user(s.freeram, &info->freeram) || 2676 __put_user(s.sharedram, &info->sharedram) || 2677 __put_user(s.bufferram, &info->bufferram) || 2678 __put_user(s.totalswap, &info->totalswap) || 2679 __put_user(s.freeswap, &info->freeswap) || 2680 __put_user(s.procs, &info->procs) || 2681 __put_user(s.totalhigh, &info->totalhigh) || 2682 __put_user(s.freehigh, &info->freehigh) || 2683 __put_user(s.mem_unit, &info->mem_unit)) 2684 return -EFAULT; 2685 2686 return 0; 2687 } 2688 #endif /* CONFIG_COMPAT */ 2689
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.