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