Skip to contentSkip to conversation

prctl: fix compat handling for prctl

PATCH Superseded Li Bin 2 participants 3 messages Apr 18, 2018

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Li Bin

author
#0 lore raw

PATCH 1/1 prctl: fix compat handling for prctl

The member auxv in prctl_mm_map structure which be shared with userspace is pointer type, but the kernel supporting COMPAT didn't handle it. This patch fix the compat handling for prctl syscall.

---
 kernel/sys.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
kernel/sys.c +41 -0
index ad69218..03b9731 100644
@@ -1968,6 +1968,25 @@ static int validate_prctl_map(struct prctl_mm_map *prctl_map)
 	return error;
 }
 
+#ifdef CONFIG_COMPAT
+struct compat_prctl_mm_map {
+	__u64   start_code;     /* code section bounds */
+	__u64   end_code;
+	__u64   start_data;     /* data section bounds */
+	__u64   end_data;
+	__u64   start_brk;      /* heap for brk() syscall */
+	__u64   brk;
+	__u64   start_stack;        /* stack starts at */
+	__u64   arg_start;      /* command line arguments bounds */
+	__u64   arg_end;
+	__u64   env_start;      /* environment variables bounds */
+	__u64   env_end;
+	compat_uptr_t   auxv;   /* auxiliary vector */
+	__u32   auxv_size;      /* vector size */
+	__u32   exe_fd;         /* /proc/$pid/exe link file */
+};
+#endif
+
 #ifdef CONFIG_CHECKPOINT_RESTORE
 static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data_size)
 {
@@ -1986,6 +2005,28 @@ static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data
 	if (data_size != sizeof(prctl_map))
 		return -EINVAL;
 
+#ifdef CONFIG_COMPAT
+	if (is_compat_task()) {
+		struct compat_prctl_mm_map prctl_map32;
+		if (copy_from_user(&prctl_map32, addr, sizeof(prctl_map32)))
+			return -EFAULT;
+
+		prctl_map.start_code = prctl_map32.start_code;
+		prctl_map.end_code = prctl_map32.end_code;
+		prctl_map.start_data = prctl_map32.start_data;
+		prctl_map.end_data = prctl_map32.end_data;
+		prctl_map.start_brk = prctl_map32.start_brk;
+		prctl_map.brk = prctl_map32.brk;
+		prctl_map.start_stack = prctl_map32.start_stack;
+		prctl_map.arg_start = prctl_map32.arg_start;
+		prctl_map.arg_end = prctl_map32.arg_end;
+		prctl_map.env_start = prctl_map32.env_start;
+		prctl_map.env_end = prctl_map32.env_end;
+		prctl_map.auxv = compat_ptr(prctl_map32.auxv);
+		prctl_map.auxv_size = prctl_map32.auxv_size;
+		prctl_map.exe_fd = prctl_map32.exe_fd;
+	} else
+#endif
 	if (copy_from_user(&prctl_map, addr, sizeof(prctl_map)))
 		return -EFAULT;
 
Signature · 1 line
1.7.12.4
#1 lore raw
Thank you for the patch! Yet something to improve:

Hi Li,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v4.17-rc1 next-20180418]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Li-Bin/prctl-fix-compat-handling-for-prctl/20180419-004502
config: x86_64-randconfig-x002-201815 (attached as .config)
compiler: gcc-7 (Debian 7.3.0-16) 7.3.0
reproduce:

        # save the attached .config to linux build tree
        make ARCH=x86_64 

All errors (new ones prefixed by >>):

kernel/sys.c: In function 'prctl_set_mm_map':

>> kernel/sys.c:2009:6: error: implicit declaration of function 'is_compat_task'; did you mean 'is_idle_task'? [-Werror=implicit-function-declaration]
     if (is_compat_task()) {
         ^~~~~~~~~~~~~~
         is_idle_task

cc1: some warnings being treated as errors

vim +2009 kernel/sys.c

1989
1990 #ifdef CONFIG_CHECKPOINT_RESTORE
1991 static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data_size)
1992 {
1993 struct prctl_mm_map prctl_map = { .exe_fd = (u32)-1, };
1994 unsigned long user_auxv[AT_VECTOR_SIZE];
1995 struct mm_struct *mm = current->mm;
1996 int error;
1997
1998 BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
1999 BUILD_BUG_ON(sizeof(struct prctl_mm_map) > 256);
2000
2001 if (opt == PR_SET_MM_MAP_SIZE)
2002 return put_user((unsigned int)sizeof(prctl_map),
2003 (unsigned int __user *)addr);
2004
2005 if (data_size != sizeof(prctl_map))
2006 return -EINVAL;
2007
2008 #ifdef CONFIG_COMPAT

> 2009		if (is_compat_task()) {
  2010			struct compat_prctl_mm_map prctl_map32;
  2011			if (copy_from_user(&prctl_map32, addr, sizeof(prctl_map32)))
  2012				return -EFAULT;
  2013	
  2014			prctl_map.start_code = prctl_map32.start_code;
  2015			prctl_map.end_code = prctl_map32.end_code;
  2016			prctl_map.start_data = prctl_map32.start_data;
  2017			prctl_map.end_data = prctl_map32.end_data;
  2018			prctl_map.start_brk = prctl_map32.start_brk;
  2019			prctl_map.brk = prctl_map32.brk;
  2020			prctl_map.start_stack = prctl_map32.start_stack;
  2021			prctl_map.arg_start = prctl_map32.arg_start;
  2022			prctl_map.arg_end = prctl_map32.arg_end;
  2023			prctl_map.env_start = prctl_map32.env_start;
  2024			prctl_map.env_end = prctl_map32.env_end;
  2025			prctl_map.auxv = compat_ptr(prctl_map32.auxv);
  2026			prctl_map.auxv_size = prctl_map32.auxv_size;
  2027			prctl_map.exe_fd = prctl_map32.exe_fd;
  2028		} else
  2029	#endif
  2030		if (copy_from_user(&prctl_map, addr, sizeof(prctl_map)))
  2031			return -EFAULT;
  2032	
  2033		error = validate_prctl_map(&prctl_map);
  2034		if (error)
  2035			return error;
  2036	
  2037		if (prctl_map.auxv_size) {
  2038			memset(user_auxv, 0, sizeof(user_auxv));
  2039			if (copy_from_user(user_auxv,
  2040					   (const void __user *)prctl_map.auxv,
  2041					   prctl_map.auxv_size))
  2042				return -EFAULT;
  2043	
  2044			/* Last entry must be AT_NULL as specification requires */
  2045			user_auxv[AT_VECTOR_SIZE - 2] = AT_NULL;
  2046			user_auxv[AT_VECTOR_SIZE - 1] = AT_NULL;
  2047		}
  2048	
  2049		if (prctl_map.exe_fd != (u32)-1) {
  2050			error = prctl_set_mm_exe_file(mm, prctl_map.exe_fd);
  2051			if (error)
  2052				return error;
  2053		}
  2054	
  2055		down_write(&mm->mmap_sem);
  2056	
  2057		/*
  2058		 * We don't validate if these members are pointing to
  2059		 * real present VMAs because application may have correspond
  2060		 * VMAs already unmapped and kernel uses these members for statistics
  2061		 * output in procfs mostly, except
  2062		 *
  2063		 *  - @start_brk/@brk which are used in do_brk but kernel lookups
  2064		 *    for VMAs when updating these memvers so anything wrong written
  2065		 *    here cause kernel to swear at userspace program but won't lead
  2066		 *    to any problem in kernel itself

2067 */
2068
2069 mm->start_code = prctl_map.start_code;
2070 mm->end_code = prctl_map.end_code;
2071 mm->start_data = prctl_map.start_data;
2072 mm->end_data = prctl_map.end_data;
2073 mm->start_brk = prctl_map.start_brk;
2074 mm->brk = prctl_map.brk;
2075 mm->start_stack = prctl_map.start_stack;
2076 mm->arg_start = prctl_map.arg_start;
2077 mm->arg_end = prctl_map.arg_end;
2078 mm->env_start = prctl_map.env_start;
2079 mm->env_end = prctl_map.env_end;
2080
2081 /*
2082 * Note this update of @saved_auxv is lockless thus
2083 * if someone reads this member in procfs while we're
2084 * updating -- it may get partly updated results. It's
2085 * known and acceptable trade off: we leave it as is to
2086 * not introduce additional locks here making the kernel
2087 * more complex.
2088 */
2089 if (prctl_map.auxv_size)
2090 memcpy(mm->saved_auxv, user_auxv, sizeof(user_auxv));
2091
2092 up_write(&mm->mmap_sem);
2093 return 0;
2094 }
2095 #endif /* CONFIG_CHECKPOINT_RESTORE */
2096

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation

#2 lore raw
In reply to Li Bin
Thank you for the patch! Yet something to improve:

Hi Li,

Thank you for the patch! Yet something to improve:

[auto build test ERROR on linus/master]
[also build test ERROR on v4.17-rc1 next-20180418]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url: https://github.com/0day-ci/linux/commits/Li-Bin/prctl-fix-compat-handling-for-prctl/20180419-004502
config: x86_64-randconfig-s1-04190128 (attached as .config)
compiler: gcc-6 (Debian 6.4.0-9) 6.4.0 20171026 reproduce:

        # save the attached .config to linux build tree
        make ARCH=x86_64 

All errors (new ones prefixed by >>):

kernel/sys.c: In function 'prctl_set_mm_map':

>> kernel/sys.c:2009:6: error: implicit declaration of function 'is_compat_task' [-Werror=implicit-function-declaration]
     if (is_compat_task()) {
         ^~~~~~~~~~~~~~

Cyclomatic Complexity 5 include/linux/compiler.h:__read_once_size Cyclomatic Complexity 1 include/linux/kasan-checks.h:kasan_check_read Cyclomatic Complexity 1 include/linux/kasan-checks.h:kasan_check_write Cyclomatic Complexity 2 arch/x86/include/asm/bitops.h:set_bit Cyclomatic Complexity 2 arch/x86/include/asm/bitops.h:clear_bit Cyclomatic Complexity 1 arch/x86/include/asm/bitops.h:constant_test_bit Cyclomatic Complexity 1 arch/x86/include/asm/bitops.h:variable_test_bit Cyclomatic Complexity 1 arch/x86/include/asm/current.h:get_current Cyclomatic Complexity 1 arch/x86/include/asm/atomic.h:arch_atomic_read Cyclomatic Complexity 1 arch/x86/include/asm/atomic.h:arch_atomic_inc Cyclomatic Complexity 1 arch/x86/include/asm/atomic.h:arch_atomic_dec_and_test Cyclomatic Complexity 1 arch/x86/include/asm/atomic64_64.h:arch_atomic64_read Cyclomatic Complexity 1 arch/x86/include/asm/atomic64_64.h:arch_atomic64_inc Cyclomatic Complexity 1 include/asm-generic/atomic-instrumented.h:atomic_read Cyclomatic Complexity 1 include/asm-generic/atomic-instrumented.h:atomic64_read Cyclomatic Complexity 1 include/asm-generic/atomic-instrumented.h:atomic_inc Cyclomatic Complexity 1 include/asm-generic/atomic-instrumented.h:atomic64_inc Cyclomatic Complexity 1 include/asm-generic/atomic-instrumented.h:atomic_dec_and_test Cyclomatic Complexity 1 include/asm-generic/atomic-long.h:atomic_long_read Cyclomatic Complexity 1 include/asm-generic/atomic-long.h:atomic_long_inc Cyclomatic Complexity 1 include/linux/thread_info.h:check_object_size Cyclomatic Complexity 5 include/linux/thread_info.h:check_copy_size Cyclomatic Complexity 1 arch/x86/include/asm/preempt.h:preempt_count Cyclomatic Complexity 5 arch/x86/include/asm/preempt.h:__preempt_count_add Cyclomatic Complexity 5 arch/x86/include/asm/preempt.h:__preempt_count_sub Cyclomatic Complexity 1 include/linux/spinlock.h:spin_lock
Cyclomatic Complexity 1 include/linux/spinlock.h:spin_unlock
Cyclomatic Complexity 1 include/linux/spinlock.h:spin_unlock_irqrestore Cyclomatic Complexity 1 include/linux/rcupdate.h:__rcu_read_lock Cyclomatic Complexity 1 include/linux/rcupdate.h:__rcu_read_unlock Cyclomatic Complexity 1 include/linux/jiffies.h:get_jiffies_64 Cyclomatic Complexity 1 include/linux/rcutiny.h:rcu_is_watching Cyclomatic Complexity 1 include/linux/sched/coredump.h:__get_dumpable Cyclomatic Complexity 1 include/linux/sched/coredump.h:get_dumpable Cyclomatic Complexity 3 include/linux/path.h:path_equal
Cyclomatic Complexity 1 include/linux/uidgid.h:__kuid_val
Cyclomatic Complexity 1 include/linux/uidgid.h:__kgid_val
Cyclomatic Complexity 1 include/linux/uidgid.h:uid_eq
Cyclomatic Complexity 1 include/linux/uidgid.h:gid_eq
Cyclomatic Complexity 1 include/linux/uidgid.h:uid_valid
Cyclomatic Complexity 1 include/linux/uidgid.h:gid_valid
Cyclomatic Complexity 1 include/linux/uidgid.h:make_kuid
Cyclomatic Complexity 1 include/linux/uidgid.h:make_kgid
Cyclomatic Complexity 1 include/linux/uidgid.h:from_kuid
Cyclomatic Complexity 1 include/linux/uidgid.h:from_kgid
Cyclomatic Complexity 2 include/linux/uidgid.h:from_kuid_munged Cyclomatic Complexity 2 include/linux/uidgid.h:from_kgid_munged Cyclomatic Complexity 1 include/linux/pid.h:is_child_reaper
Cyclomatic Complexity 1 include/linux/fs.h:get_file
Cyclomatic Complexity 1 include/linux/fs.h:file_inode
Cyclomatic Complexity 1 include/linux/mm.h:get_mm_counter
Cyclomatic Complexity 1 include/linux/mm.h:get_mm_rss
Cyclomatic Complexity 1 include/linux/mm.h:get_mm_hiwater_rss Cyclomatic Complexity 2 include/linux/mm.h:setmax_mm_hiwater_rss Cyclomatic Complexity 3 include/linux/mm.h:check_data_rlimit
Cyclomatic Complexity 1 include/linux/sched/prio.h:nice_to_rlimit Cyclomatic Complexity 1 include/linux/sched.h:task_pid
Cyclomatic Complexity 1 include/linux/sched.h:task_pgrp
Cyclomatic Complexity 1 include/linux/sched.h:task_session
Cyclomatic Complexity 1 include/linux/sched.h:task_no_new_privs Cyclomatic Complexity 1 include/linux/sched.h:task_set_no_new_privs Cyclomatic Complexity 1 include/linux/sched.h:task_nice
Cyclomatic Complexity 1 include/linux/utsname.h:utsname
Cyclomatic Complexity 1 include/linux/signal.h:valid_signal
Cyclomatic Complexity 1 include/linux/sched/task.h:task_lock
Cyclomatic Complexity 1 include/linux/sched/task.h:task_unlock Cyclomatic Complexity 1 include/linux/cred.h:current_user_ns
Cyclomatic Complexity 1 include/linux/sched/signal.h:thread_group_leader Cyclomatic Complexity 1 include/linux/sched/signal.h:same_thread_group Cyclomatic Complexity 1 include/linux/sched/signal.h:unlock_task_sighand Cyclomatic Complexity 1 include/linux/sched/signal.h:task_rlimit Cyclomatic Complexity 1 include/linux/sched/signal.h:rlimit
Cyclomatic Complexity 1 arch/x86/include/asm/smap.h:clac
Cyclomatic Complexity 1 arch/x86/include/asm/smap.h:stac
Cyclomatic Complexity 1 arch/x86/include/asm/uaccess_64.h:copy_user_generic Cyclomatic Complexity 10 arch/x86/include/asm/uaccess_64.h:raw_copy_to_user Cyclomatic Complexity 1 include/linux/uaccess.h:__copy_to_user Cyclomatic Complexity 2 include/linux/uaccess.h:copy_from_user Cyclomatic Complexity 2 include/linux/uaccess.h:copy_to_user
Cyclomatic Complexity 1 include/linux/cn_proc.h:proc_sid_connector Cyclomatic Complexity 1 include/linux/cn_proc.h:proc_comm_connector Cyclomatic Complexity 1 include/linux/task_io_accounting_ops.h:task_io_get_inblock Cyclomatic Complexity 1 include/linux/task_io_accounting_ops.h:task_io_get_oublock Cyclomatic Complexity 1 include/linux/file.h:__to_fd
Cyclomatic Complexity 1 include/linux/ctype.h:isdigit
Cyclomatic Complexity 1 arch/x86/include/asm/compat.h:compat_ptr Cyclomatic Complexity 5 kernel/sys.c:__do_sys_old_getrlimit
Cyclomatic Complexity 1 kernel/sys.c:__se_sys_old_getrlimit
Cyclomatic Complexity 1 kernel/sys.c:rlim64_is_infinity
Cyclomatic Complexity 3 kernel/sys.c:rlim_to_rlim64
Cyclomatic Complexity 3 kernel/sys.c:rlim64_to_rlim
Cyclomatic Complexity 1 kernel/sys.c:accumulate_thread_rusage Cyclomatic Complexity 1 kernel/sys.c:__do_sys_umask
Cyclomatic Complexity 1 kernel/sys.c:__se_sys_umask
Cyclomatic Complexity 3 kernel/sys.c:propagate_has_child_subreaper Cyclomatic Complexity 2 include/linux/thread_info.h:test_ti_thread_flag Cyclomatic Complexity 1 include/linux/rcupdate.h:rcu_lock_acquire Cyclomatic Complexity 4 include/linux/rcupdate.h:rcu_read_lock

vim +/is_compat_task +2009 kernel/sys.c

1989
1990 #ifdef CONFIG_CHECKPOINT_RESTORE
1991 static int prctl_set_mm_map(int opt, const void __user *addr, unsigned long data_size)
1992 {
1993 struct prctl_mm_map prctl_map = { .exe_fd = (u32)-1, };
1994 unsigned long user_auxv[AT_VECTOR_SIZE];
1995 struct mm_struct *mm = current->mm;
1996 int error;
1997
1998 BUILD_BUG_ON(sizeof(user_auxv) != sizeof(mm->saved_auxv));
1999 BUILD_BUG_ON(sizeof(struct prctl_mm_map) > 256);
2000
2001 if (opt == PR_SET_MM_MAP_SIZE)
2002 return put_user((unsigned int)sizeof(prctl_map),
2003 (unsigned int __user *)addr);
2004
2005 if (data_size != sizeof(prctl_map))
2006 return -EINVAL;
2007
2008 #ifdef CONFIG_COMPAT

> 2009		if (is_compat_task()) {
  2010			struct compat_prctl_mm_map prctl_map32;
  2011			if (copy_from_user(&prctl_map32, addr, sizeof(prctl_map32)))
  2012				return -EFAULT;
  2013	
  2014			prctl_map.start_code = prctl_map32.start_code;
  2015			prctl_map.end_code = prctl_map32.end_code;
  2016			prctl_map.start_data = prctl_map32.start_data;
  2017			prctl_map.end_data = prctl_map32.end_data;
  2018			prctl_map.start_brk = prctl_map32.start_brk;
  2019			prctl_map.brk = prctl_map32.brk;
  2020			prctl_map.start_stack = prctl_map32.start_stack;
  2021			prctl_map.arg_start = prctl_map32.arg_start;
  2022			prctl_map.arg_end = prctl_map32.arg_end;
  2023			prctl_map.env_start = prctl_map32.env_start;
  2024			prctl_map.env_end = prctl_map32.env_end;
  2025			prctl_map.auxv = compat_ptr(prctl_map32.auxv);
  2026			prctl_map.auxv_size = prctl_map32.auxv_size;
  2027			prctl_map.exe_fd = prctl_map32.exe_fd;
  2028		} else
  2029	#endif
  2030		if (copy_from_user(&prctl_map, addr, sizeof(prctl_map)))
  2031			return -EFAULT;
  2032	
  2033		error = validate_prctl_map(&prctl_map);
  2034		if (error)
  2035			return error;
  2036	
  2037		if (prctl_map.auxv_size) {
  2038			memset(user_auxv, 0, sizeof(user_auxv));
  2039			if (copy_from_user(user_auxv,
  2040					   (const void __user *)prctl_map.auxv,
  2041					   prctl_map.auxv_size))
  2042				return -EFAULT;
  2043	
  2044			/* Last entry must be AT_NULL as specification requires */
  2045			user_auxv[AT_VECTOR_SIZE - 2] = AT_NULL;
  2046			user_auxv[AT_VECTOR_SIZE - 1] = AT_NULL;
  2047		}
  2048	
  2049		if (prctl_map.exe_fd != (u32)-1) {
  2050			error = prctl_set_mm_exe_file(mm, prctl_map.exe_fd);
  2051			if (error)
  2052				return error;
  2053		}
  2054	
  2055		down_write(&mm->mmap_sem);
  2056	
  2057		/*
  2058		 * We don't validate if these members are pointing to
  2059		 * real present VMAs because application may have correspond
  2060		 * VMAs already unmapped and kernel uses these members for statistics
  2061		 * output in procfs mostly, except
  2062		 *
  2063		 *  - @start_brk/@brk which are used in do_brk but kernel lookups
  2064		 *    for VMAs when updating these memvers so anything wrong written
  2065		 *    here cause kernel to swear at userspace program but won't lead
  2066		 *    to any problem in kernel itself

2067 */
2068
2069 mm->start_code = prctl_map.start_code;
2070 mm->end_code = prctl_map.end_code;
2071 mm->start_data = prctl_map.start_data;
2072 mm->end_data = prctl_map.end_data;
2073 mm->start_brk = prctl_map.start_brk;
2074 mm->brk = prctl_map.brk;
2075 mm->start_stack = prctl_map.start_stack;
2076 mm->arg_start = prctl_map.arg_start;
2077 mm->arg_end = prctl_map.arg_end;
2078 mm->env_start = prctl_map.env_start;
2079 mm->env_end = prctl_map.env_end;
2080
2081 /*
2082 * Note this update of @saved_auxv is lockless thus
2083 * if someone reads this member in procfs while we're
2084 * updating -- it may get partly updated results. It's
2085 * known and acceptable trade off: we leave it as is to
2086 * not introduce additional locks here making the kernel
2087 * more complex.
2088 */
2089 if (prctl_map.auxv_size)
2090 memcpy(mm->saved_auxv, user_auxv, sizeof(user_auxv));
2091
2092 up_write(&mm->mmap_sem);
2093 return 0;
2094 }
2095 #endif /* CONFIG_CHECKPOINT_RESTORE */
2096

---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation