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