Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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