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