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