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