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