remove set_fs calls from the exec and coredump code v2
Subsystems (6)
Hi all,
this series gets rid of playing with the address limit in the exec and coredump code. Most of this was fairly trivial, the biggest changes are those to the spufs coredump code.
Changes since v1:
- properly spell NUL
- properly handle the compat siginfo case in ELF coredumps
PATCH 1/8 powerpc/spufs: simplify spufs core dumping
Replace the coredump ->read method with a ->dump method that must call dump_emit itself. That way we avoid a buffer allocation an messing with set_fs() to call into code that is intended to deal with user buffers. For the ->get case we can now use a small on-stack buffer and avoid memory allocations as well.
--- arch/powerpc/platforms/cell/spufs/coredump.c | 87 ++---- arch/powerpc/platforms/cell/spufs/file.c | 273 ++++++++++--------- arch/powerpc/platforms/cell/spufs/spufs.h | 3 +- 3 files changed, 170 insertions(+), 193 deletions(-)
arch/powerpc/platforms/cell/spufs/coredump.c +28 -59
@@ -21,22 +21,6 @@#include "spufs.h" -static ssize_t do_coredump_read (int num,struct spu_context *ctx,void *buffer, -size_t size,loff_t *off) -{ -u64 data; -int ret; - -if (spufs_coredump_read[num].read) -return spufs_coredump_read[num].read (ctx, buffer, size, off); - - data = spufs_coredump_read[num].get (ctx); - ret =snprintf (buffer, size,"0x%.16llx" , data); -if (ret >= size) -return size; -return ++ret;/* count trailing NULL */ -} -static int spufs_ctx_note_size (struct spu_context *ctx,int dfd) {int i, sz, total =0 ;
@@ -118,58 +102,43 @@ int spufs_coredump_extra_notes_size(void)static int spufs_arch_write_note (struct spu_context *ctx,int i,struct coredump_params *cprm,int dfd) { -loff_t pos =0 ; -int sz, rc, total =0 ; -const int bufsz =PAGE_SIZE ; -char *name; -char fullname[80 ], *buf; +size_t sz = spufs_coredump_read[i].size; +char fullname[80 ];struct elf_note en; -size_t skip; - - buf = (void *)get_zeroed_page (GFP_KERNEL ); -if (!buf) -return -ENOMEM ; +size_t ret; - name = spufs_coredump_read[i].name; - sz = spufs_coredump_read[i].size; - -sprintf (fullname,"SPU/%d/%s" , dfd, name); +sprintf (fullname,"SPU/%d/%s" , dfd, spufs_coredump_read[i].name); en.n_namesz =strlen (fullname) +1 ; en.n_descsz = sz; en.n_type =NT_SPU ;if (!dump_emit (cprm, &en,sizeof (en))) - goto Eio; - +return -EIO ;if (!dump_emit (cprm, fullname, en.n_namesz)) - goto Eio; - +return -EIO ;if (!dump_align (cprm,4 )) - goto Eio; - -do { - rc =do_coredump_read (i, ctx, buf, bufsz, &pos); -if (rc >0 ) { -if (!dump_emit (cprm, buf, rc)) - goto Eio; - total += rc; - } - }while (rc == bufsz && total < sz); - -if (rc <0 ) - goto out; - - skip =roundup (cprm->pos - total + sz,4 ) - cprm->pos; -if (!dump_skip (cprm, skip)) - goto Eio; - - rc =0 ; -out: -free_page ((unsigned long )buf); -return rc; -Eio: -free_page ((unsigned long )buf); -return -EIO ; +return -EIO ; + +if (spufs_coredump_read[i].dump) { + ret = spufs_coredump_read[i].dump (ctx, cprm); +if (ret <0 ) +return ret; + }else { +char buf[32 ]; + + ret =snprintf (buf,sizeof (buf),"0x%.16llx" , + spufs_coredump_read[i].get (ctx)); +if (ret >=sizeof (buf)) +return sizeof (buf); + +/* count trailing the NULL: */ +if (!dump_emit (cprm, buf, ret +1 )) +return -EIO ; + } + +if (!dump_skip (cprm,roundup (cprm->pos - ret + sz,4 ) - cprm->pos)) +return -EIO ; +return 0 ; }int spufs_coredump_extra_notes_write (struct coredump_params *cprm)
arch/powerpc/platforms/cell/spufs/file.c +141 -132
472 lines not on this page · raw message
arch/powerpc/platforms/cell/spufs/spufs.h +1 -2
@@ -337,8 +337,7 @@ void spufs_dma_callback(struct spu *spu, int type);extern struct spu_coredump_calls spufs_coredump_calls;struct spufs_coredump_reader {char *name; -ssize_t (*read)(struct spu_context *ctx, -char __user *buffer,size_t size,loff_t *pos); +ssize_t (*dump)(struct spu_context *ctx,struct coredump_params *cprm);u64 (*get)(struct spu_context *ctx);size_t size; };
Signature · 1 line
2.25.1
PATCH 2/8 signal: clean up __copy_siginfo_to_user32
In reply to Christoph HellwigInstead of an architecture specific calling convention in common code just pass a flags argument with architecture specific values.
--- arch/x86/ia32/ia32_signal.c | 2 +- arch/x86/include/asm/compat.h | 4 ---- arch/x86/include/asm/signal.h | 3 +++ arch/x86/kernel/signal.c | 3 ++- include/linux/compat.h | 2 ++ kernel/signal.c | 21 ++++++++++++--------- 6 files changed, 20 insertions(+), 15 deletions(-)
arch/x86/ia32/ia32_signal.c +1 -1
@@ -350,7 +350,7 @@ int ia32_setup_rt_frame(int sig, struct ksignal *ksig,unsafe_put_user (*(__u64 *)set, (__u64 *)&frame->uc.uc_sigmask, Efault);user_access_end (); -if (__copy_siginfo_to_user32 (&frame->info, &ksig->info, false)) +if (__copy_siginfo_to_user32 (&frame->info, &ksig->info,SA_IA32_ABI ))return -EFAULT ;/* Set up registers for signal handler */
arch/x86/include/asm/compat.h +0 -4
@@ -213,8 +213,4 @@ static inline bool in_compat_syscall(void)#define in_compat_syscall in_compat_syscall/* override the generic impl */ #endif -struct compat_siginfo ; -int __copy_siginfo_to_user32 (struct compat_siginfo __user *to, -const kernel_siginfo_t *from,bool x32_ABI); -#endif /* _ASM_X86_COMPAT_H */
arch/x86/include/asm/signal.h +3 -0
@@ -28,6 +28,9 @@ typedef struct {#define SA_IA32_ABI 0x02000000u#define SA_X32_ABI 0x01000000u +#define compat_siginfo_flags () \ + (in_x32_syscall() ? SA_X32_ABI : SA_IA32_ABI) +#ifndef CONFIG_COMPAT typedef sigset_t compat_sigset_t ;#endif
arch/x86/kernel/signal.c +2 -1
@@ -543,7 +543,8 @@ static int x32_setup_rt_frame(struct ksignal *ksig,user_access_end ();if (ksig->ka.sa.sa_flags &SA_SIGINFO ) { -if (__copy_siginfo_to_user32 (&frame->info, &ksig->info, true)) +if (__copy_siginfo_to_user32 (&frame->info, &ksig->info, +SA_X32_ABI ))return -EFAULT ; }
include/linux/compat.h +2 -0
@@ -404,6 +404,8 @@ long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask, unsigned long bitmap_size);int copy_siginfo_from_user32 (kernel_siginfo_t *to,const struct compat_siginfo __user *from);int copy_siginfo_to_user32 (struct compat_siginfo __user *to,const kernel_siginfo_t *from); +int __copy_siginfo_to_user32 (struct compat_siginfo __user *to, +const kernel_siginfo_t *from,unsigned int flags);int get_compat_sigevent (struct sigevent *event,const struct compat_sigevent __user *u_event);
kernel/signal.c +12 -9
@@ -3235,15 +3235,8 @@ int copy_siginfo_from_user(kernel_siginfo_t *to, const siginfo_t __user *from) }#ifdef CONFIG_COMPAT -int copy_siginfo_to_user32(struct compat_siginfo __user *to, - const struct kernel_siginfo *from) -#if defined(CONFIG_X86_X32_ABI) || defined(CONFIG_IA32_EMULATION) -{ - return __copy_siginfo_to_user32(to, from, in_x32_syscall()); -}int __copy_siginfo_to_user32 (struct compat_siginfo __user *to, - const struct kernel_siginfo *from, bool x32_ABI) -#endif +const struct kernel_siginfo *from,unsigned int flags) {struct compat_siginfo new;memset (&new,0 ,sizeof (new));
@@ -3298,7 +3291,7 @@ int __copy_siginfo_to_user32(struct compat_siginfo __user *to, new.si_uid = from->si_uid; new.si_status = from->si_status;#ifdef CONFIG_X86_X32_ABI -if (x32_ABI) { +if (flags &SA_X32_ABI ) { new._sifields._sigchld_x32._utime = from->si_utime; new._sifields._sigchld_x32._stime = from->si_stime; }else
@@ -3326,6 +3319,16 @@ int __copy_siginfo_to_user32(struct compat_siginfo __user *to,return 0 ; } +#ifndef compat_siginfo_flags +#define compat_siginfo_flags () 0 +#endif + +int copy_siginfo_to_user32 (struct compat_siginfo __user *to, +const struct kernel_siginfo *from) +{ +return __copy_siginfo_to_user32 (to, from,compat_siginfo_flags ()); +} +static int post_copy_siginfo_from_user32 (kernel_siginfo_t *to,const struct compat_siginfo *from) {
Signature · 1 line
2.25.1
Re: [PATCH 2/8] signal: clean up __copy_siginfo_to_user32
Quoted 2 lines · Christoph Hellwig
Instead of an architecture specific calling convention in common code just pass a flags argument with architecture specific values.
This bothers me because it makes all architectures pay for the sins of x32. Further it starts burying the details of the what is happening in architecture specific helpers. Hiding the fact that there is only one niche architecture that does anything weird.
I am very sensitive to hiding away signal handling details right now because way to much of the signal handling code got hidden in architecture specific files and was quite buggy because as a result.
My general sense is putting all of the weird details up front and center in kernel/signal.c is the only way for this code will be looked at and successfully maintained.
How about these patches to solve set_fs with binfmt_elf instead:
Eric W. Biederman (2):
signal: Factor copy_siginfo_to_external32 from copy_siginfo_to_user32
signal: Remove the set_fs in binfmt_elf.c:fill_siginfo_note
fs/binfmt_elf.c | 5 +---- fs/compat_binfmt_elf.c | 2 +- include/linux/compat.h | 1 + include/linux/signal.h | 7 +++++++ kernel/signal.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------
Eric
PATCH 1/2 signal: Factor copy_siginfo_to_external32 from copy_siginfo_to_user32
To remove the use of set_fs in the coredump code there needs to be a way to convert a kernel siginfo to a userspace compat siginfo.
Call that function copy_siginfo_to_compat and factor it out of copy_siginfo_to_user32.
The existence of x32 complicates this code. On x32 SIGCHLD uses 64bit times for utime and stime. As only SIGCHLD is affected and SIGCHLD never causes a coredump I have avoided handling that case.
--- include/linux/compat.h | 1 + kernel/signal.c | 108 +++++++++++++++++++++++------------------ 2 files changed, 63 insertions(+), 46 deletions(-)
include/linux/compat.h +1 -0
@@ -402,6 +402,7 @@ long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask, unsigned long bitmap_size);long compat_put_bitmap (compat_ulong_t __user *umask,unsigned long *mask,unsigned long bitmap_size); +void copy_siginfo_to_external32 (struct compat_siginfo *to,const struct kernel_siginfo *from);int copy_siginfo_from_user32 (kernel_siginfo_t *to,const struct compat_siginfo __user *from);int copy_siginfo_to_user32 (struct compat_siginfo __user *to,const kernel_siginfo_t *from); int get_compat_sigevent(struct sigevent *event,
kernel/signal.c +62 -46
152 lines not on this page · raw message
Signature · 1 line
2.25.0
Re: [PATCH 1/2] signal: Factor copy_siginfo_to_external32 from copy_siginfo_to_user32
Le 17/04/2020 à 23:09, Eric W. Biederman a écrit :
Quoted 6 lines
To remove the use of
set_fsin the coredump code there needs to be a way to convert a kernel siginfo to a userspace compat siginfo.Call that function
copy_siginfo_to_compatand factor it out ofcopy_siginfo_to_user32.
I find it a pitty to do that.
The existing function could have been easily converted to using user_access_begin() + user_access_end() and use unsafe_put_user() to copy to userspace to avoid copying through a temporary structure on the stack.
With your change, it becomes impossible to do that.
Is that really an issue to use that set_fs() in the coredump code ?
Christophe
Quoted 181 lines
The existence of x32 complicates this code. On x32 SIGCHLD uses 64bit times for utime and stime. As only SIGCHLD is affected and SIGCHLD never causes a coredump I have avoided handling that case.
Signed-off-by "Eric W. Biederman" <[email protected]>--- include/linux/compat.h | 1 + kernel/signal.c | 108 +++++++++++++++++++++++------------------ 2 files changed, 63 insertions(+), 46 deletions(-)include/linux/compat.h +1
-0@@ -402,6 +402,7 @@ long compat_get_bitmap(unsigned long *mask, const compat_ulong_t __user *umask, unsigned long bitmap_size);long compat_put_bitmap (compat_ulong_t __user *umask,unsigned long *mask,unsigned long bitmap_size); +void copy_siginfo_to_external32 (struct compat_siginfo *to,const struct kernel_siginfo *from);int copy_siginfo_from_user32 (kernel_siginfo_t *to,const struct compat_siginfo __user *from);int copy_siginfo_to_user32 (struct compat_siginfo __user *to,const kernel_siginfo_t *from); int get_compat_sigevent(struct sigevent *event,kernel/signal.c +62
-46@@ -3235,90 +3235,106 @@ int copy_siginfo_from_user(kernel_siginfo_t *to, const siginfo_t __user *from) } #ifdef CONFIG_COMPAT -int copy_siginfo_to_user32(struct compat_siginfo __user *to, - const struct kernel_siginfo *from) -#if defined(CONFIG_X86_X32_ABI) || defined(CONFIG_IA32_EMULATION) +void copy_siginfo_to_external32(struct compat_siginfo *to, + const struct kernel_siginfo *from) { - return __copy_siginfo_to_user32(to, from, in_x32_syscall()); -} -int __copy_siginfo_to_user32(struct compat_siginfo __user *to, - const struct kernel_siginfo *from, bool x32_ABI) -#endif -{ - struct compat_siginfo new; - memset(&new, 0, sizeof(new)); + /* + * This function does not work properly for SIGCHLD on x32, + * but it does not need to as SIGCHLD never causes a coredump. + */ + memset(to, 0, sizeof(*to)); - new.si_signo = from->si_signo; - new.si_errno = from->si_errno; - new.si_code = from->si_code; + to->si_signo = from->si_signo; + to->si_errno = from->si_errno; + to->si_code = from->si_code; switch(siginfo_layout(from->si_signo, from->si_code)) { case SIL_KILL: - new.si_pid = from->si_pid; - new.si_uid = from->si_uid; + to->si_pid = from->si_pid; + to->si_uid = from->si_uid; break; case SIL_TIMER: - new.si_tid = from->si_tid; - new.si_overrun = from->si_overrun; - new.si_int = from->si_int; + to->si_tid = from->si_tid; + to->si_overrun = from->si_overrun; + to->si_int = from->si_int; break; case SIL_POLL: - new.si_band = from->si_band; - new.si_fd = from->si_fd; + to->si_band = from->si_band; + to->si_fd = from->si_fd; break; case SIL_FAULT: - new.si_addr = ptr_to_compat(from->si_addr); + to->si_addr = ptr_to_compat(from->si_addr); #ifdef __ARCH_SI_TRAPNO - new.si_trapno = from->si_trapno; + to->si_trapno = from->si_trapno; #endif break; case SIL_FAULT_MCEERR: - new.si_addr = ptr_to_compat(from->si_addr); + to->si_addr = ptr_to_compat(from->si_addr); #ifdef __ARCH_SI_TRAPNO - new.si_trapno = from->si_trapno; + to->si_trapno = from->si_trapno; #endif - new.si_addr_lsb = from->si_addr_lsb; + to->si_addr_lsb = from->si_addr_lsb; break; case SIL_FAULT_BNDERR: - new.si_addr = ptr_to_compat(from->si_addr); + to->si_addr = ptr_to_compat(from->si_addr); #ifdef __ARCH_SI_TRAPNO - new.si_trapno = from->si_trapno; + to->si_trapno = from->si_trapno; #endif - new.si_lower = ptr_to_compat(from->si_lower); - new.si_upper = ptr_to_compat(from->si_upper); + to->si_lower = ptr_to_compat(from->si_lower); + to->si_upper = ptr_to_compat(from->si_upper); break; case SIL_FAULT_PKUERR: - new.si_addr = ptr_to_compat(from->si_addr); + to->si_addr = ptr_to_compat(from->si_addr); #ifdef __ARCH_SI_TRAPNO - new.si_trapno = from->si_trapno; + to->si_trapno = from->si_trapno; #endif - new.si_pkey = from->si_pkey; + to->si_pkey = from->si_pkey; break; case SIL_CHLD: - new.si_pid = from->si_pid; - new.si_uid = from->si_uid; - new.si_status = from->si_status; + to->si_pid = from->si_pid; + to->si_uid = from->si_uid; + to->si_status = from->si_status; + to->si_utime = from->si_utime; + to->si_stime = from->si_stime; #ifdef CONFIG_X86_X32_ABI if (x32_ABI) { - new._sifields._sigchld_x32._utime = from->si_utime; - new._sifields._sigchld_x32._stime = from->si_stime; + to->_sifields._sigchld_x32._utime = from->si_utime; + to->_sifields._sigchld_x32._stime = from->si_stime; } else #endif { - new.si_utime = from->si_utime; - new.si_stime = from->si_stime; } break; case SIL_RT: - new.si_pid = from->si_pid; - new.si_uid = from->si_uid; - new.si_int = from->si_int; + to->si_pid = from->si_pid; + to->si_uid = from->si_uid; + to->si_int = from->si_int; break; case SIL_SYS: - new.si_call_addr = ptr_to_compat(from->si_call_addr); - new.si_syscall = from->si_syscall; - new.si_arch = from->si_arch; + to->si_call_addr = ptr_to_compat(from->si_call_addr); + to->si_syscall = from->si_syscall; + to->si_arch = from->si_arch; break; } +} + +int copy_siginfo_to_user32(struct compat_siginfo __user *to, + const struct kernel_siginfo *from) +#if defined(CONFIG_X86_X32_ABI) || defined(CONFIG_IA32_EMULATION) +{ + return __copy_siginfo_to_user32(to, from, in_x32_syscall()); +} +int __copy_siginfo_to_user32(struct compat_siginfo __user *to, + const struct kernel_siginfo *from, bool x32_ABI) +#endif +{ + struct compat_siginfo new; + copy_siginfo_to_external32(&new, from); +#ifdef CONFIG_X86_X32_ABI + if (x32_ABI && from->si_signo == SIGCHLD) { + new._sifields._sigchld_x32._utime = from->si_utime; + new._sifields._sigchld_x32._stime = from->si_stime; + } +#endif if (copy_to_user(to, &new, sizeof(struct compat_siginfo))) return -EFAULT;
Re: [PATCH 1/2] signal: Factor copy_siginfo_to_external32 from copy_siginfo_to_user32
Quoted 8 lines · Christophe Leroy
Le 17/04/2020 à 23:09, Eric W. Biederman a écrit :
Quoted 6 lines
To remove the use of
set_fsin the coredump code there needs to be a way to convert a kernel siginfo to a userspace compat siginfo.Call that function
copy_siginfo_to_compatand factor it out ofcopy_siginfo_to_user32.
Quoted 7 lines
I find it a pitty to do that.
The existing function could have been easily converted to using
user_access_begin()+user_access_end()and useunsafe_put_user()to copy to userspace to avoid copying through a temporary structure on the stack.With your change, it becomes impossible to do that.
I don't follow. You don't like temporary structures in the coredump code or temporary structures in copy_siginfo_to_user32?
A temporary structure in copy_siginfo_to_user is pretty much required so that it can be zeroed to guarantee we don't pass a structure with holes to userspace.
The implementation of copy_siginfo_to_user32 used to use the equivalent of user_access_begin() and user_access_end() and the code was a mess that was very difficult to reason about. I recall their being holes in the structure that were being copied to userspace.
Meanwhile if you are going to set all of the bytes a cache hot temporary structure is quite cheap.
Quoted 1 line
Is that really an issue to use that
set_fs()in the coredump code ?
Using set_fs() is pretty bad and something that we would like to remove from the kernel entirely. The fewer instances of set_fs() we have the better.
I forget all of the details but set_fs() is both a type violation and an attack point when people are attacking the kernel. The existence of set_fs() requires somethings that should be constants to be variables. Something about that means that our current code is difficult to protect from spectre style vulnerabilities.
There was a very good thread about it all in I think 2018 but unfortunately I can't find it now.
Eric
Re: [PATCH 1/2] signal: Factor copy_siginfo_to_external32 from copy_siginfo_to_user32
Quoted 11 lines · Eric W. Biederman
Quoted 1 line
Is that really an issue to use that
set_fs()in the coredump code ?Using
set_fs()is pretty bad and something that we would like to remove from the kernel entirely. The fewer instances ofset_fs()we have the better.I forget all of the details but
set_fs()is both a type violation and an attack point when people are attacking the kernel. The existence ofset_fs()requires somethings that should be constants to be variables. Something about that means that our current code is difficult to protect from spectre style vulnerabilities.
Yes, set_fs requires variable based address checking in the uaccess routines for architectures with a shared address space, or even entirely different code for architectures with separate kernel and user address spaces. My plan is to hopefully kill set_fs in its current form a few merge windows down the road. We'll probably still need some form of it to e.g. mark a thread as kernel thread vs also being able to execute user code, but it will be much ore limited than before, called from very few places and actually be a no-op for many architectures.
Re: [PATCH 1/2] signal: Factor copy_siginfo_to_external32 from copy_siginfo_to_user32
Le 19/04/2020 à 10:13, Christoph Hellwig a écrit :
Quoted 13 lines · Eric W. Biederman
Quoted 11 lines
Quoted 1 line
Is that really an issue to use that
set_fs()in the coredump code ?Using
set_fs()is pretty bad and something that we would like to remove from the kernel entirely. The fewer instances ofset_fs()we have the better.I forget all of the details but
set_fs()is both a type violation and an attack point when people are attacking the kernel. The existence ofset_fs()requires somethings that should be constants to be variables. Something about that means that our current code is difficult to protect from spectre style vulnerabilities.
Quoted 9 lines
Yes,
set_fsrequires variable based address checking in the uaccess routines for architectures with a shared address space, or even entirely different code for architectures with separate kernel and user address spaces. My plan is to hopefully killset_fsin its current form a few merge windows down the road. We'll probably still need some form of it to e.g. mark a thread as kernel thread vs also being able to execute user code, but it will be much ore limited than before, called from very few places and actually be a no-op for many architectures.
Oh nice. Some time ago I proposed a patch to change set_fs() to a flip/flop flag based logic, see https://patchwork.ozlabs.org/project/linuxppc-dev/patch/dd2876b808ea38eb7b7f760ecd6ce06096c61fb5.1580295551.git.christophe.leroy@c-s.fr/
But if we manage to get rid of it completely, that's even better.
Re: [PATCH 1/2] signal: Factor copy_siginfo_to_external32 from copy_siginfo_to_user32
In reply to Eric W. BiedermanLe 18/04/2020 à 13:55, Eric W. Biederman a écrit :
Quoted 12 lines
Quoted 10 lines · Christophe Leroy
Le 17/04/2020 à 23:09, Eric W. Biederman a écrit :
Quoted 6 lines
To remove the use of
set_fsin the coredump code there needs to be a way to convert a kernel siginfo to a userspace compat siginfo.Call that function
copy_siginfo_to_compatand factor it out ofcopy_siginfo_to_user32.I find it a pitty to do that.
Quoted 8 lines
Quoted 5 lines
The existing function could have been easily converted to using
user_access_begin()+user_access_end()and useunsafe_put_user()to copy to userspace to avoid copying through a temporary structure on the stack.With your change, it becomes impossible to do that.
I don't follow. You don't like temporary structures in the coredump code or temporary structures in
copy_siginfo_to_user32?
In copy_siginfo_to_user32()
Quoted 4 lines
A temporary structure in
copy_siginfo_to_useris pretty much required so that it can be zeroed to guarantee we don't pass a structure with holes to userspace.
Why ? We can zeroize the user structure directly, either with clear_user() or with some not yet existing unsafe_clear_user() or equivalent.
Quoted 8 lines
The implementation of
copy_siginfo_to_user32used to use the equivalent ofuser_access_begin()anduser_access_end()and the code was a mess that was very difficult to reason about. I recall their being holes in the structure that were being copied to userspace.Meanwhile if you are going to set all of the bytes a cache hot temporary structure is quite cheap.
But how can we be sure it is cache hot ? As we are using memset() to zeroize it, it won't be loaded from memory as it will use dcbz instruction, but at some point in time it will get flushed back to memory, that's consuming anyway. Unless we invalidate it after the copy, but that becomes complex.
Christophe
Re: [PATCH 1/2] signal: Factor copy_siginfo_to_external32 from copy_siginfo_to_user32
In reply to Christophe LeroyQuoted 10 lines · Christophe Leroy
Le 17/04/2020 à 23:09, Eric W. Biederman a écrit :
Quoted 6 lines
To remove the use of
set_fsin the coredump code there needs to be a way to convert a kernel siginfo to a userspace compat siginfo.Call that function
copy_siginfo_to_compatand factor it out ofcopy_siginfo_to_user32.
Quoted 7 lines
I find it a pitty to do that.
The existing function could have been easily converted to using
user_access_begin()+user_access_end()and useunsafe_put_user()to copy to userspace to avoid copying through a temporary structure on the stack.With your change, it becomes impossible to do that.
As Eric said we need a struct to clear all padding. Note that I though about converting to unsafe_copy_to_user in my variant as we can pretty easily do that if pre-filling the structure earlier. But I didn't want to throw in such unrelated changes for now - I'll volunteer to do it later, though.
PATCH 2/2 signal: Remove the set_fs in binfmt_elf.c:fill_siginfo_note
In reply to Eric W. BiedermanThe code in binfmt_elf.c is differnt from the rest of the code that processes siginfo, as it sends siginfo from a kernel buffer to a file rather than from kernel memory to userspace buffers. To remove it's use of set_fs the code needs some different siginfo helpers.
Add the helper copy_siginfo_to_external to copy from the kernel's internal siginfo layout to a buffer in the siginfo layout that userspace expects.
Modify fill_siginfo_note to use copy_siginfo_to_external instead of set_fs and copy_siginfo_to_user.
Update compat_binfmt_elf.c to use the previously added copy_siginfo_to_external32 to handle the compat case.
--- fs/binfmt_elf.c | 5 +---- fs/compat_binfmt_elf.c | 2 +- include/linux/signal.h | 7 +++++++ 3 files changed, 9 insertions(+), 5 deletions(-)
fs/binfmt_elf.c +1 -4
@@ -1556,10 +1556,7 @@ static void fill_auxv_note(struct memelfnote *note, struct mm_struct *mm)static void fill_siginfo_note (struct memelfnote *note,user_siginfo_t *csigdata,const kernel_siginfo_t *siginfo) { -mm_segment_t old_fs =get_fs (); -set_fs (KERNEL_DS ); -copy_siginfo_to_user ((user_siginfo_t __user *) csigdata, siginfo); -set_fs (old_fs); +copy_siginfo_to_external (csigdata, siginfo);fill_note (note,"CORE" ,NT_SIGINFO ,sizeof (*csigdata), csigdata); }
fs/compat_binfmt_elf.c +1 -1
@@ -39,7 +39,7 @@*/ #define user_long_t compat_long_t#define user_siginfo_t compat_siginfo_t -#define copy_siginfo_to_user copy_siginfo_to_user32 +#define copy_siginfo_to_external copy_siginfo_to_external32/* * The machine-dependent core note format types are defined in elfcore-compat.h,
include/linux/signal.h +7 -0
@@ -24,6 +24,13 @@ static inline void clear_siginfo(kernel_siginfo_t *info)#define SI_EXPANSION_SIZE (sizeof(struct siginfo) - sizeof(struct kernel_siginfo)) +static inline void copy_siginfo_to_external (siginfo_t *to, +const kernel_siginfo_t *from) +{ +memcpy (to, from,sizeof (*from)); +memset (((char *)to) +sizeof (struct kernel_siginfo ),0 ,SI_EXPANSION_SIZE ); +} +int copy_siginfo_to_user (siginfo_t __user *to,const kernel_siginfo_t *from);int copy_siginfo_from_user (kernel_siginfo_t *to,const siginfo_t __user *from);
Signature · 1 line
2.25.0
Re: [PATCH 2/8] signal: clean up __copy_siginfo_to_user32
In reply to Eric W. BiedermanQuoted 8 lines · Eric W. Biederman
This bothers me because it makes all architectures pay for the sins of x32. Further it starts burying the details of the what is happening in architecture specific helpers. Hiding the fact that there is only one niche architecture that does anything weird.
I am very sensitive to hiding away signal handling details right now because way to much of the signal handling code got hidden in architecture specific files and was quite buggy because as a result.
I much prefer the unconditional flags over the crazy ifdef mess in the current coe and your version. Except for that and the rather strange and confusing copy_siginfo_to_external32 name it pretty much looks the same.
PATCH 3/8 signal: replace __copy_siginfo_to_user32 with to_compat_siginfo
In reply to Christoph HellwigMove copying the siginfo to userspace into the callers, so that the compat_siginfo conversion can be reused by the ELF coredump code without set_fs magic.
--- arch/x86/ia32/ia32_signal.c | 4 +- arch/x86/kernel/signal.c | 5 ++- include/linux/compat.h | 4 +- kernel/signal.c | 89 ++++++++++++++++++------------------- 4 files changed, 52 insertions(+), 50 deletions(-)
arch/x86/ia32/ia32_signal.c +3 -1
@@ -301,6 +301,7 @@ int ia32_setup_rt_frame(int sig, struct ksignal *ksig, compat_sigset_t *set, struct pt_regs *regs) { struct rt_sigframe_ia32 __user *frame; +struct compat_siginfo new;void __user *restorer;void __user *fp =NULL ;
@@ -350,7 +351,8 @@ int ia32_setup_rt_frame(int sig, struct ksignal *ksig,unsafe_put_user (*(__u64 *)set, (__u64 *)&frame->uc.uc_sigmask, Efault);user_access_end (); -if (__copy_siginfo_to_user32 (&frame->info, &ksig->info,SA_IA32_ABI )) +to_compat_siginfo (&new, &ksig->info,SA_IA32_ABI ); +if (copy_to_user (&frame->info, &new,sizeof (frame->info)))return -EFAULT ;/* Set up registers for signal handler */
arch/x86/kernel/signal.c +3 -2
@@ -517,6 +517,7 @@ static int x32_setup_rt_frame(struct ksignal *ksig, {#ifdef CONFIG_X86_X32_ABI struct rt_sigframe_x32 __user *frame; +struct compat_siginfo new;unsigned long uc_flags;void __user *restorer;void __user *fp =NULL ;
@@ -543,8 +544,8 @@ static int x32_setup_rt_frame(struct ksignal *ksig,user_access_end ();if (ksig->ka.sa.sa_flags &SA_SIGINFO ) { -if (__copy_siginfo_to_user32 (&frame->info, &ksig->info, -SA_X32_ABI )) +to_compat_siginfo (&new, &ksig->info,SA_X32_ABI ); +if (copy_to_user (&frame->info, &new,sizeof (frame->info)))return -EFAULT ; }
include/linux/compat.h +2 -2
@@ -404,8 +404,8 @@ long compat_put_bitmap(compat_ulong_t __user *umask, unsigned long *mask, unsigned long bitmap_size);int copy_siginfo_from_user32 (kernel_siginfo_t *to,const struct compat_siginfo __user *from);int copy_siginfo_to_user32 (struct compat_siginfo __user *to,const kernel_siginfo_t *from); -int __copy_siginfo_to_user32 (struct compat_siginfo __user *to, -const kernel_siginfo_t *from,unsigned int flags); +void to_compat_siginfo (struct compat_siginfo *to, +const struct kernel_siginfo *from,unsigned int flags);int get_compat_sigevent (struct sigevent *event,const struct compat_sigevent __user *u_event);
kernel/signal.c +44 -45
139 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH 3/8] signal: replace __copy_siginfo_to_user32 with to_compat_siginfo
Quoted 6 lines · Christoph Hellwig
Move copying the siginfo to userspace into the callers, so that the
compat_siginfoconversion can be reused by the ELF coredump code withoutset_fsmagic.Signed-off-by Christoph Hellwig <[email protected]>
Looks all good to me, but I noticed that the naming is now a bit inconsistent. to_compat_siginfo() is basically the reverse of post_copy_siginfo_from_user32(), but the names are very different.
I suppose this can always be cleaned up later though, as your naming choice is more consistent with how things are in the rest of the kernel these days.
Arnd
PATCH 4/8 binfmt_elf: open code copy_siginfo_to_user to kernelspace buffer
In reply to Christoph HellwigInstead of messing with the address limit just open code the trivial memcpy + memset logic for the native version, and a call to to_compat_siginfo for the compat version.
--- fs/binfmt_elf.c | 9 +++++---- fs/compat_binfmt_elf.c | 6 +++++- 2 files changed, 10 insertions(+), 5 deletions(-)
fs/binfmt_elf.c +5 -4
@@ -1553,15 +1553,16 @@ static void fill_auxv_note(struct memelfnote *note, struct mm_struct *mm)fill_note (note,"CORE" ,NT_AUXV , i *sizeof (elf_addr_t), auxv); } +#ifndef fill_siginfo_notestatic void fill_siginfo_note (struct memelfnote *note,user_siginfo_t *csigdata,const kernel_siginfo_t *siginfo) { -mm_segment_t old_fs =get_fs (); -set_fs (KERNEL_DS ); -copy_siginfo_to_user ((user_siginfo_t __user *) csigdata, siginfo); -set_fs (old_fs); +memcpy (csigdata, siginfo,sizeof (struct kernel_siginfo )); +memset ((char *)csigdata +sizeof (struct kernel_siginfo ),0 , +SI_EXPANSION_SIZE );fill_note (note,"CORE" ,NT_SIGINFO ,sizeof (*csigdata), csigdata); } +#endif #define MAX_FILE_NOTE_SIZE (4*1024*1024)/*
fs/compat_binfmt_elf.c +5 -1
@@ -39,7 +39,11 @@*/ #define user_long_t compat_long_t#define user_siginfo_t compat_siginfo_t -#define copy_siginfo_to_user copy_siginfo_to_user32 +#define fill_siginfo_note (note, csigdata, siginfo) \ +do { \ + to_compat_siginfo(csigdata, siginfo, compat_siginfo_flags()); \ + fill_note(note, "CORE", NT_SIGINFO, sizeof(*csigdata), csigdata); \ +} while (0)/* * The machine-dependent core note format types are defined in elfcore-compat.h,
Signature · 1 line
2.25.1
Re: [PATCH 4/8] binfmt_elf: open code copy_siginfo_to_user to kernelspace buffer
Quoted 6 lines · Christoph Hellwig
Instead of messing with the address limit just open code the trivial memcpy + memset logic for the native version, and a call to
to_compat_siginfofor the compat version.Signed-off-by Christoph Hellwig <[email protected]>
Nice!
fs/compat_binfmt_elf.c line 46
Quoted 9 lines
*/
#defineuser_long_tcompat_long_t
#defineuser_siginfo_tcompat_siginfo_tfs/compat_binfmt_elf.c +5
-1-#define copy_siginfo_to_user copy_siginfo_to_user32 +#define fill_siginfo_note (note, csigdata, siginfo) \ +do { \ + to_compat_siginfo(csigdata, siginfo, compat_siginfo_flags()); \ + fill_note(note, "CORE", NT_SIGINFO, sizeof(*csigdata), csigdata); \ +} while (0)
I don't think you are changing the behavior here, but I still wonder if it is in fact correct for x32: is in_x32_syscall() true here when dumping an x32 compat elf process, or should this rather be set according to which binfmt_elf copy is being used?
Arnd
Re: [PATCH 4/8] binfmt_elf: open code copy_siginfo_to_user to kernelspace buffer
Quoted 4 lines · Arnd Bergmann
I don't think you are changing the behavior here, but I still wonder if it is in fact correct for x32: is
in_x32_syscall()true here when dumping an x32 compat elf process, or should this rather be set according to whichbinfmt_elfcopy is being used?
The infrastructure cold enable that, although it would require more arch hooks I think. I'd rather keep it out of this series and to an interested party. Then again x32 doesn't seem to have a whole lot of interested parties..
Re: [PATCH 4/8] binfmt_elf: open code copy_siginfo_to_user to kernelspace buffer
Quoted 9 lines · Christoph Hellwig
Quoted 4 lines · Arnd Bergmann
I don't think you are changing the behavior here, but I still wonder if it is in fact correct for x32: is
in_x32_syscall()true here when dumping an x32 compat elf process, or should this rather be set according to whichbinfmt_elfcopy is being used?The infrastructure could enable that, although it would require more arch hooks I think.
I was more interested in whether you can tell if it's currently broken or not. If my feeling is right that the current code does the wrong thing here, it would be good to at least put a FIXME comment in there.
Quoted 3 lines
I'd rather keep it out of this series and to
an interested party. Then again x32 doesn't seem to have a whole lot of interested parties..
Fine with me. It's on my mental list of things that we want to kill off eventually as soon as the remaining users stop replying to questions about it.
In fact I should really turn that into a properly maintained list in Documentation/... that contains any options that someone has asked about removing in the past, along with the reasons for keeping it around and a time at which we should ask about it again.
Arnd
Re: [PATCH 4/8] binfmt_elf: open code copy_siginfo_to_user to kernelspace buffer
Quoted 4 lines · Arnd Bergmann
Quoted 3 lines
I'd rather keep it out of this series and to
an interested party. Then again x32 doesn't seem to have a whole lot of interested parties..
Quoted 8 lines
Fine with me. It's on my mental list of things that we want to kill off eventually as soon as the remaining users stop replying to questions about it.
In fact I should really turn that into a properly maintained list in
Documentation/... that contains any options that someone has asked about removing in the past, along with the reasons for keeping it around and a time at which we should ask about it again.
To the newly added x86 maintainers: Arnd brought up the point that elf_core_dump writes the ABI siginfo format into the core dump. That format differs for i386 vs x32. Is there any good way to find out which is the right format when are not in a syscall?
As far a I can tell x32 vs i386 just seems to be based around what syscall table was used for the current syscall, but core dumps aren't always in syscall context.
Re: [PATCH 4/8] binfmt_elf: open code copy_siginfo_to_user to kernelspace buffer
Quoted 14 lines · Christoph Hellwig
Quoted 4 lines · Arnd Bergmann
Quoted 3 lines
I'd rather keep it out of this series and to
an interested party. Then again x32 doesn't seem to have a whole lot of interested parties..Quoted 8 lines
Fine with me. It's on my mental list of things that we want to kill off eventually as soon as the remaining users stop replying to questions about it.
In fact I should really turn that into a properly maintained list in
Documentation/... that contains any options that someone has asked about removing in the past, along with the reasons for keeping it around and a time at which we should ask about it again.
Quoted 8 lines
To the newly added x86 maintainers: Arnd brought up the point that
elf_core_dumpwrites the ABI siginfo format into the core dump. That format differs for i386 vs x32. Is there any good way to find out which is the right format when are not in a syscall?As far a I can tell x32 vs i386 just seems to be based around what syscall table was used for the current syscall, but core dumps aren't always in syscall context.
I don't think this matters. The i386 and x32 signal structures only differ for SIGCHLD. The SIGCHLD signal does cause coredumps. So as long as we get the 32bit vs 64bit distinct correct all should be well.
Eric
Re: [PATCH 4/8] binfmt_elf: open code copy_siginfo_to_user to kernelspace buffer
Quoted 22 lines · Eric W. Biederman
Quoted 19 lines · Christoph Hellwig
Quoted 4 lines · Arnd Bergmann
Quoted 3 lines
I'd rather keep it out of this series and to
an interested party. Then again x32 doesn't seem to have a whole lot of interested parties..Quoted 8 lines
Fine with me. It's on my mental list of things that we want to kill off eventually as soon as the remaining users stop replying to questions about it.
In fact I should really turn that into a properly maintained list in
Documentation/... that contains any options that someone has asked about removing in the past, along with the reasons for keeping it around and a time at which we should ask about it again.To the newly added x86 maintainers: Arnd brought up the point that
elf_core_dumpwrites the ABI siginfo format into the core dump. That format differs for i386 vs x32. Is there any good way to find out which is the right format when are not in a syscall?
Quoted 8 lines
Quoted 3 lines
As far a I can tell x32 vs i386 just seems to be based around what syscall table was used for the current syscall, but core dumps aren't always in syscall context.
I don't think this matters. The i386 and x32 signal structures only differ for SIGCHLD. The SIGCHLD signal does cause coredumps. So as long as we get the 32bit vs 64bit distinct correct all should be well.
Ok, makes sense. Thanks for taking a look into this!
Arnd
Michael Ellerman
reviewerRe: [PATCH 4/8] binfmt_elf: open code copy_siginfo_to_user to kernelspace buffer
In reply to Christoph HellwigQuoted 40 lines · Christoph Hellwig
Instead of messing with the address limit just open code the trivial memcpy + memset logic for the native version, and a call to
to_compat_siginfofor the compat version.Signed-off-by Christoph Hellwig <[email protected]>--- fs/binfmt_elf.c | 9 +++++---- fs/compat_binfmt_elf.c | 6 +++++- 2 files changed, 10 insertions(+), 5 deletions(-)fs/binfmt_elf.c +5
-4@@ -1553,15 +1553,16 @@ static void fill_auxv_note(struct memelfnote *note, struct mm_struct *mm)fill_note (note,"CORE" ,NT_AUXV , i *sizeof (elf_addr_t), auxv); } +#ifndef fill_siginfo_notestatic void fill_siginfo_note (struct memelfnote *note,user_siginfo_t *csigdata,const kernel_siginfo_t *siginfo) { -mm_segment_t old_fs =get_fs (); -set_fs (KERNEL_DS ); -copy_siginfo_to_user ((user_siginfo_t __user *) csigdata, siginfo); -set_fs (old_fs); +memcpy (csigdata, siginfo,sizeof (struct kernel_siginfo )); +memset ((char *)csigdata +sizeof (struct kernel_siginfo ),0 , +SI_EXPANSION_SIZE );fill_note (note,"CORE" ,NT_SIGINFO ,sizeof (*csigdata), csigdata); } +#endif #define MAX_FILE_NOTE_SIZE (4*1024*1024)/* fs/compat_binfmt_elf.c +0
-0
fs/compat_binfmt_elf.c line 46
Quoted 9 lines
fs/compat_binfmt_elf.c +5
-1@@ -39,7 +39,11 @@*/ #define user_long_t compat_long_t#define user_siginfo_t compat_siginfo_t -#define copy_siginfo_to_user copy_siginfo_to_user32 +#define fill_siginfo_note (note, csigdata, siginfo) \ +do { \ + to_compat_siginfo(csigdata, siginfo, compat_siginfo_flags()); \ + fill_note(note, "CORE", NT_SIGINFO, sizeof(*csigdata), csigdata); \ +} while (0)
This doesn't build on ppc (cell_defconfig):
../fs/binfmt_elf.c: In function 'fill_note_info':
../fs/compat_binfmt_elf.c:44:39: error: implicit declaration of function 'compat_siginfo_flags'; did you mean 'to_compat_siginfo'? [-Werror=implicit-function-d eclaration]
to_compat_siginfo(csigdata, siginfo, compat_siginfo_flags()); \
^~~~~~~~~~~~~~~~~~~~
../fs/binfmt_elf.c:1846:2: note: in expansion of macro 'fill_siginfo_note'
fill_siginfo_note(&info->signote, &info->csigdata, siginfo);
^~~~~~~~~~~~~~~~~ cc1: some warnings being treated as errors
make[2]: *** [../scripts/Makefile.build:266: fs/compat_binfmt_elf.o] Error 1
I guess the empty version from kernel/signal.c needs to move into a header somewhere.
cheers
Re: [PATCH 4/8] binfmt_elf: open code copy_siginfo_to_user to kernelspace buffer
Quoted 14 lines · Michael Ellerman
fs/compat_binfmt_elf.c line 46
Quoted 3 lines
fs/compat_binfmt_elf.c +3
-0+to_compat_siginfo (csigdata, siginfo,compat_siginfo_flags ()); \ +fill_note (note,"CORE" ,NT_SIGINFO ,sizeof (*csigdata), csigdata); \ +}while (0 )This doesn't build on ppc (
cell_defconfig):
../fs/binfmt_elf.c: In function 'fill_note_info':
../fs/compat_binfmt_elf.c:44:39: error: implicit declaration of function 'compat_siginfo_flags'; did you mean 'to_compat_siginfo'? [-Werror=implicit-function-d eclaration]to_compat_siginfo(csigdata, siginfo, compat_siginfo_flags()); \ ^~~~~~~~~~~~~~~~~~~~ ../fs/binfmt_elf.c:1846:2: note: in expansion of macro 'fill_siginfo_note' fill_siginfo_note(&info->signote, &info->csigdata, siginfo); ^~~~~~~~~~~~~~~~~
Quoted 6 lines
cc1: some warnings being treated as errors
make[2]: *** [../scripts/Makefile.build:266:fs/compat_binfmt_elf.o] Error 1I guess the empty version from
kernel/signal.cneeds to move into a header somewhere.
Yes, it should. Odd that the buildbut hasn't complained yet so far..
PATCH 5/8 binfmt_elf: remove the set_fs(KERNEL_DS) in elf_core_dump
In reply to Christoph HellwigThere is no logic in elf_core_dump itself, or in the various arch helpers called from it which use uaccess routines on kernel pointers, except for the actual file writes that nicely encapsulated in __kernel_write used by dump_emit.
--- fs/binfmt_elf.c | 40 +++++++++++++--------------------------- 1 file changed, 13 insertions(+), 27 deletions(-)
fs/binfmt_elf.c +13 -27
@@ -1355,7 +1355,6 @@ static unsigned long vma_dump_size(struct vm_area_struct *vma, vma->vm_pgoff == 0 && (vma->vm_flags & VM_READ)) {u32 __user *header = (u32 __user *) vma->vm_start;u32 word; -mm_segment_t fs =get_fs ();/* * Doing it this way gets the constant folded by GCC. */
@@ -1368,14 +1367,8 @@ static unsigned long vma_dump_size(struct vm_area_struct *vma, magic.elfmag[EI_MAG1 ] =ELFMAG1 ; magic.elfmag[EI_MAG2 ] =ELFMAG2 ; magic.elfmag[EI_MAG3 ] =ELFMAG3 ; -/* -* Switch to the user "segment" for get_user(), -* then put back what elf_core_dump() had in place. -*/ -set_fs (USER_DS );if (unlikely (get_user (word, header))) word =0 ; -set_fs (fs);if (word == magic.cmp)return PAGE_SIZE ; }
@@ -2187,7 +2180,6 @@ static void fill_extnum_info(struct elfhdr *elf, struct elf_shdr *shdr4extnum,static int elf_core_dump (struct coredump_params *cprm) {int has_dumped =0 ; -mm_segment_t fs;int segs, i;size_t vma_data_size =0 ;struct vm_area_struct *vma, *gate_vma;
@@ -2240,9 +2232,6 @@ static int elf_core_dump(struct coredump_params *cprm) has_dumped =1 ; - fs =get_fs (); -set_fs (KERNEL_DS ); - offset +=sizeof (elf);/* Elf header */ offset += segs *sizeof (struct elf_phdr );/* Program headers */
@@ -2254,7 +2243,7 @@ static int elf_core_dump(struct coredump_params *cprm) phdr4note =kmalloc (sizeof (*phdr4note),GFP_KERNEL );if (!phdr4note) - goto end_coredump; + goto cleanup;fill_elf_note_phdr (phdr4note, sz, offset); offset += sz;
@@ -2269,7 +2258,7 @@ static int elf_core_dump(struct coredump_params *cprm) vma_filesz =kvmalloc (array_size (sizeof (*vma_filesz), (segs -1 )),GFP_KERNEL );if (!vma_filesz) - goto end_coredump; + goto cleanup;for (i =0 , vma =first_vma (current, gate_vma); vma !=NULL ; vma =next_vma (vma, gate_vma)) {
@@ -2287,17 +2276,17 @@ static int elf_core_dump(struct coredump_params *cprm)if (e_phnum ==PN_XNUM ) { shdr4extnum =kmalloc (sizeof (*shdr4extnum),GFP_KERNEL );if (!shdr4extnum) - goto end_coredump; + goto cleanup;fill_extnum_info (&elf, shdr4extnum, e_shoff, segs); } offset = dataoff;if (!dump_emit (cprm, &elf,sizeof (elf))) - goto end_coredump; + goto cleanup;if (!dump_emit (cprm, phdr4note,sizeof (*phdr4note))) - goto end_coredump; + goto cleanup;/* Write program headers for segments dump */ for (i = 0, vma = first_vma(current, gate_vma); vma != NULL;
@@ -2319,22 +2308,22 @@ static int elf_core_dump(struct coredump_params *cprm) phdr.p_align =ELF_EXEC_PAGESIZE ;if (!dump_emit (cprm, &phdr,sizeof (phdr))) - goto end_coredump; + goto cleanup; }if (!elf_core_write_extra_phdrs (cprm, offset)) - goto end_coredump; + goto cleanup;/* write out the notes section */ if (!write_note_info (&info, cprm)) - goto end_coredump; + goto cleanup;if (elf_coredump_extra_notes_write (cprm)) - goto end_coredump; + goto cleanup;/* Align to page */ if (!dump_skip (cprm, dataoff - cprm->pos)) - goto end_coredump; + goto cleanup;for (i =0 , vma =first_vma (current, gate_vma); vma !=NULL ; vma =next_vma (vma, gate_vma)) {
@@ -2356,22 +2345,19 @@ static int elf_core_dump(struct coredump_params *cprm) }else stop = !dump_skip (cprm,PAGE_SIZE );if (stop) - goto end_coredump; + goto cleanup; } }dump_truncate (cprm);if (!elf_core_write_extra_data (cprm)) - goto end_coredump; + goto cleanup;if (e_phnum ==PN_XNUM ) {if (!dump_emit (cprm, shdr4extnum,sizeof (*shdr4extnum))) - goto end_coredump; + goto cleanup; } -end_coredump: -set_fs (fs); - cleanup:free_note_info (&info);kfree (shdr4extnum);
Signature · 1 line
2.25.1
PATCH 6/8 binfmt_elf_fdpic: remove the set_fs(KERNEL_DS) in elf_fdpic_core_dump
In reply to Christoph HellwigThere is no logic in elf_fdpic_core_dump itself that uses uaccess routines on kernel pointers, the file writes are nicely encapsulated in dump_emit which does its own set_fs.
--- fs/binfmt_elf_fdpic.c | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-)
fs/binfmt_elf_fdpic.c +12 -19
@@ -1549,7 +1549,6 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm) {#define NUM_NOTES 6int has_dumped =0 ; -mm_segment_t fs;int segs;int i;struct vm_area_struct *vma;
@@ -1678,9 +1677,6 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm) "LINUX", ELF_CORE_XFPREG_TYPE, sizeof(*xfpu), xfpu);#endif - fs =get_fs (); -set_fs (KERNEL_DS ); - offset +=sizeof (*elf);/* Elf header */ offset += segs *sizeof (struct elf_phdr );/* Program headers */
@@ -1695,7 +1691,7 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm) phdr4note =kmalloc (sizeof (*phdr4note),GFP_KERNEL );if (!phdr4note) - goto end_coredump; + goto cleanup;fill_elf_note_phdr (phdr4note, sz, offset); offset += sz;
@@ -1711,17 +1707,17 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm)if (e_phnum ==PN_XNUM ) { shdr4extnum =kmalloc (sizeof (*shdr4extnum),GFP_KERNEL );if (!shdr4extnum) - goto end_coredump; + goto cleanup;fill_extnum_info (elf, shdr4extnum, e_shoff, segs); } offset = dataoff;if (!dump_emit (cprm, elf,sizeof (*elf))) - goto end_coredump; + goto cleanup;if (!dump_emit (cprm, phdr4note,sizeof (*phdr4note))) - goto end_coredump; + goto cleanup;/* write program headers for segments dump */ for (vma = current->mm->mmap; vma; vma = vma->vm_next) {
@@ -1745,16 +1741,16 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm) phdr.p_align =ELF_EXEC_PAGESIZE ;if (!dump_emit (cprm, &phdr,sizeof (phdr))) - goto end_coredump; + goto cleanup; }if (!elf_core_write_extra_phdrs (cprm, offset)) - goto end_coredump; + goto cleanup;/* write out the notes section */ for (i =0 ; i < numnote; i++)if (!writenote (notes + i, cprm)) - goto end_coredump; + goto cleanup;/* write out the thread status notes section */ list_for_each (t, &thread_list) {
@@ -1763,21 +1759,21 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm)for (i =0 ; i < tmp->num_notes; i++)if (!writenote (&tmp->notes[i], cprm)) - goto end_coredump; + goto cleanup; }if (!dump_skip (cprm, dataoff - cprm->pos)) - goto end_coredump; + goto cleanup;if (!elf_fdpic_dump_segments (cprm)) - goto end_coredump; + goto cleanup;if (!elf_core_write_extra_data (cprm)) - goto end_coredump; + goto cleanup;if (e_phnum ==PN_XNUM ) {if (!dump_emit (cprm, shdr4extnum,sizeof (*shdr4extnum))) - goto end_coredump; + goto cleanup; }if (cprm->file->f_pos != offset) {
@@ -1787,9 +1783,6 @@ static int elf_fdpic_core_dump(struct coredump_params *cprm) cprm->file->f_pos, offset); } -end_coredump: -set_fs (fs); - cleanup:while (!list_empty (&thread_list)) {struct list_head *tmp = thread_list.next;
Signature · 1 line
2.25.1
PATCH 7/8 exec: simplify the copy_strings_kernel calling convention
In reply to Christoph Hellwigcopy_strings_kernel is always used with a single argument, adjust the calling convention to that.
--- fs/binfmt_em86.c | 6 +++--- fs/binfmt_misc.c | 4 ++-- fs/binfmt_script.c | 6 +++--- fs/exec.c | 13 ++++++------- include/linux/binfmts.h | 3 +-- 5 files changed, 15 insertions(+), 17 deletions(-)
fs/binfmt_em86.c +3 -3
@@ -68,15 +68,15 @@ static int load_em86(struct linux_binprm *bprm)* user environment and arguments are stored. */ remove_arg_zero (bprm); - retval =copy_strings_kernel (1 , &bprm->filename, bprm); + retval =copy_string_kernel (bprm->filename, bprm);if (retval <0 )return retval; bprm->argc++;if (i_arg) { - retval =copy_strings_kernel (1 , &i_arg, bprm); + retval =copy_string_kernel (i_arg, bprm);if (retval <0 )return retval; bprm->argc++; } - retval =copy_strings_kernel (1 , &i_name, bprm); + retval =copy_string_kernel (i_name, bprm);if (retval <0 )return retval; bprm->argc++;
fs/binfmt_misc.c +2 -2
@@ -190,13 +190,13 @@ static int load_misc_binary(struct linux_binprm *bprm) bprm->file =NULL ; }/* make argv[1] be the path to the binary */ - retval =copy_strings_kernel (1 , &bprm->interp, bprm); + retval =copy_string_kernel (bprm->interp, bprm);if (retval <0 ) goto error; bprm->argc++;/* add the interp as argv[0] */ - retval =copy_strings_kernel (1 , &fmt->interpreter, bprm); + retval =copy_string_kernel (fmt->interpreter, bprm);if (retval <0 ) goto error; bprm->argc++;
fs/binfmt_script.c +3 -3
@@ -117,17 +117,17 @@ static int load_script(struct linux_binprm *bprm) retval =remove_arg_zero (bprm);if (retval)return retval; - retval =copy_strings_kernel (1 , &bprm->interp, bprm); + retval =copy_string_kernel (bprm->interp, bprm);if (retval <0 )return retval; bprm->argc++;if (i_arg) { - retval =copy_strings_kernel (1 , &i_arg, bprm); + retval =copy_string_kernel (i_arg, bprm);if (retval <0 )return retval; bprm->argc++; } - retval =copy_strings_kernel (1 , &i_name, bprm); + retval =copy_string_kernel (i_name, bprm);if (retval)return retval; bprm->argc++;
fs/exec.c +6 -7
@@ -588,24 +588,23 @@ static int copy_strings(int argc, struct user_arg_ptr argv, }/* -* Like copy_strings, but get argv and its values from kernel memory. +* Copy and argument/environment string from the kernel to the processes stack. */ -int copy_strings_kernel (int argc,const char *const *__argv, -struct linux_binprm *bprm) +int copy_string_kernel (const char *arg,struct linux_binprm *bprm) {int r;mm_segment_t oldfs =get_fs ();struct user_arg_ptr argv = { - .ptr.native = (const char __user *const __user *)__argv, + .ptr.native = (const char __user *const __user *)&arg, };set_fs (KERNEL_DS ); - r =copy_strings (argc, argv, bprm); + r =copy_strings (1 , argv, bprm);set_fs (oldfs);return r; } -EXPORT_SYMBOL (copy_strings_kernel); +EXPORT_SYMBOL (copy_string_kernel);#ifdef CONFIG_MMU
@@ -1863,7 +1862,7 @@ static int __do_execve_file(int fd, struct filename *filename,if (retval <0 ) goto out; - retval =copy_strings_kernel (1 , &bprm->filename, bprm); + retval =copy_string_kernel (bprm->filename, bprm);if (retval <0 ) goto out;
include/linux/binfmts.h +1 -2
@@ -144,8 +144,7 @@ extern int setup_arg_pages(struct linux_binprm * bprm,extern int transfer_args_to_stack (struct linux_binprm *bprm,unsigned long *sp_location);extern int bprm_change_interp (const char *interp,struct linux_binprm *bprm); -extern int copy_strings_kernel (int argc,const char *const *argv, -struct linux_binprm *bprm); +int copy_string_kernel (const char *arg,struct linux_binprm *bprm);extern void install_exec_creds (struct linux_binprm *bprm);extern void set_binfmt (struct linux_binfmt *new);extern ssize_t read_code (struct file *,unsigned long ,loff_t ,size_t );
Signature · 1 line
2.25.1
PATCH 8/8 exec: open code copy_string_kernel
In reply to Christoph HellwigCurrently copy_string_kernel is just a wrapper around copy_strings that simplifies the calling conventions and uses set_fs to allow passing a kernel pointer. But due to the fact the we only need to handle a single kernel argument pointer, the logic can be sigificantly simplified while getting rid of the set_fs.
--- fs/exec.c | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-)
fs/exec.c +34 -9
@@ -592,17 +592,42 @@ static int copy_strings(int argc, struct user_arg_ptr argv,*/ int copy_string_kernel (const char *arg,struct linux_binprm *bprm) { -int r; -mm_segment_t oldfs =get_fs (); -struct user_arg_ptr argv = { - .ptr.native = (const char __user *const __user *)&arg, - }; +int len =strnlen (arg,MAX_ARG_STRLEN ) +1 /* terminating NUL */ ; +unsigned long pos = bprm->p; + +if (len ==0 ) +return -EFAULT ; +if (!valid_arg_len (bprm, len)) +return -E2BIG ; + +/* We're going to work our way backwards. */ + arg += len; + bprm->p -= len; +if (IS_ENABLED (CONFIG_MMU ) && bprm->p < bprm->argmin) +return -E2BIG ; + +while (len >0 ) { + unsigned int bytes_to_copy = min_t(unsigned int, len, +min_not_zero (offset_in_page (pos),PAGE_SIZE )); +struct page *page; +char *kaddr; -set_fs (KERNEL_DS ); - r =copy_strings (1 , argv, bprm); -set_fs (oldfs); + pos -= bytes_to_copy; + arg -= bytes_to_copy; + len -= bytes_to_copy; -return r; + page =get_arg_page (bprm, pos,1 ); +if (!page) +return -E2BIG ; + kaddr =kmap_atomic (page); +flush_arg_page (bprm, pos &PAGE_MASK , page); +memcpy (kaddr +offset_in_page (pos), arg, bytes_to_copy); +flush_kernel_dcache_page (page); +kunmap_atomic (kaddr); +put_arg_page (page); + } + +return 0 ; }EXPORT_SYMBOL (copy_string_kernel);
Signature · 1 line
2.25.1
Re: [PATCH 8/8] exec: open code copy_string_kernel
Le 14/04/2020 à 09:01, Christoph Hellwig a écrit :
Quoted 5 lines
Currently
copy_string_kernelis just a wrapper aroundcopy_stringsthat simplifies the calling conventions and usesset_fsto allow passing a kernel pointer. But due to the fact the we only need to handle a single kernel argument pointer, the logic can be sigificantly simplified while getting rid of theset_fs.
Instead of duplicating almost identical code, can you write a function that takes whether the source is from user or from kernel, then you just do things like:
if (from_user) len = strnlen_user(str, MAX_ARG_STRLEN); else len = strnlen(str, MAX_ARG_STRLEN); if (from_user) copy_from_user(kaddr+offset, str, bytes_to_copy); else memcpy(kaddr+offset, str, bytes_to_copy);
Quoted 53 lines
Signed-off-by Christoph Hellwig <[email protected]>--- fs/exec.c | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-)fs/exec.c +27
-9@@ -592,17 +592,42 @@ static int copy_strings(int argc, struct user_arg_ptr argv,*/ int copy_string_kernel (const char *arg,struct linux_binprm *bprm) { -int r; -mm_segment_t oldfs =get_fs (); -struct user_arg_ptr argv = { - .ptr.native = (const char __user *const __user *)&arg, - }; +int len =strnlen (arg,MAX_ARG_STRLEN ) +1 /* terminating NUL */ ; +unsigned long pos = bprm->p; + +if (len ==0 ) +return -EFAULT ; +if (!valid_arg_len (bprm, len)) +return -E2BIG ; + +/* We're going to work our way backwards. */ + arg += len; + bprm->p -= len; +if (IS_ENABLED (CONFIG_MMU ) && bprm->p < bprm->argmin) +return -E2BIG ; + +while (len >0 ) { + unsigned int bytes_to_copy = min_t(unsigned int, len, +min_not_zero (offset_in_page (pos),PAGE_SIZE )); +struct page *page; +char *kaddr; -set_fs (KERNEL_DS ); - r =copy_strings (1 , argv, bprm); -set_fs (oldfs); + pos -= bytes_to_copy; + arg -= bytes_to_copy; + len -= bytes_to_copy; -return r; + page =get_arg_page (bprm, pos,1 ); +if (!page) +return -E2BIG ; + kaddr =kmap_atomic (page); +flush_arg_page (bprm, pos &PAGE_MASK , page);
fs/exec.c static int copy_strings(int argc, struct user_arg_ptr argv, · line 633
Quoted 10 lines
fs/exec.c +7
-0+memcpy (kaddr +offset_in_page (pos), arg, bytes_to_copy); +flush_kernel_dcache_page (page); +kunmap_atomic (kaddr); +put_arg_page (page); + } + +return 0 ; }EXPORT_SYMBOL (copy_string_kernel);
Christophe
Re: [PATCH 8/8] exec: open code copy_string_kernel
Quoted 14 lines · Christophe Leroy
Le 14/04/2020 à 09:01, Christoph Hellwig a écrit :
Quoted 5 lines
Currently
copy_string_kernelis just a wrapper aroundcopy_stringsthat simplifies the calling conventions and usesset_fsto allow passing a kernel pointer. But due to the fact the we only need to handle a single kernel argument pointer, the logic can be sigificantly simplified while getting rid of theset_fs.Instead of duplicating almost identical code, can you write a function that takes whether the source is from user or from kernel, then you just do things like:
Quoted 10 lines
if (from_user) len = strnlen_user(str, MAX_ARG_STRLEN); else len = strnlen(str, MAX_ARG_STRLEN); if (from_user) copy_from_user(kaddr+offset, str, bytes_to_copy); else memcpy(kaddr+offset, str, bytes_to_copy);
We'll need two different str variables then with and without __user annotations to keep type safety. And introduce a branch-y and unreadable mess in the exec fast path instead of adding a simple and well understood function for the kernel case that just deals with the much simpler case of just copying a single arg vector from a kernel address.
Re: [PATCH 8/8] exec: open code copy_string_kernel
Le 19/04/2020 à 10:06, Christoph Hellwig a écrit :
Quoted 26 lines · Christophe Leroy
Quoted 14 lines
Le 14/04/2020 à 09:01, Christoph Hellwig a écrit :
Quoted 5 lines
Currently
copy_string_kernelis just a wrapper aroundcopy_stringsthat simplifies the calling conventions and usesset_fsto allow passing a kernel pointer. But due to the fact the we only need to handle a single kernel argument pointer, the logic can be sigificantly simplified while getting rid of theset_fs.Instead of duplicating almost identical code, can you write a function that takes whether the source is from user or from kernel, then you just do things like:
Quoted 10 lines
if (from_user) len = strnlen_user(str, MAX_ARG_STRLEN); else len = strnlen(str, MAX_ARG_STRLEN); if (from_user) copy_from_user(kaddr+offset, str, bytes_to_copy); else memcpy(kaddr+offset, str, bytes_to_copy);
Quoted 6 lines
We'll need two different str variables then with and without
__userannotations to keep type safety. And introduce a branch-y and unreadable mess in the exec fast path instead of adding a simple and well understood function for the kernel case that just deals with the much simpler case of just copying a single arg vector from a kernel address.
About the branch, I was expecting GCC to inline and eliminate the unused branch.
Quoted 9 lines · Christoph Hellwig
Hi all,
this series gets rid of playing with the address limit in the exec and coredump code. Most of this was fairly trivial, the biggest changes are those to the spufs coredump code.
Changes since v1:
- properly spell NUL
- properly handle the compat siginfo case in ELF coredumps
Quick question is exec from a kernel thread within the scope of what you are looking at?
There is a set_fs(USER_DS) in flush_old_exec whose sole purpose appears to be to allow exec from kernel threads. Where the kernel threads run with set_fs(KERNEL_DS) until they call exec.
Eric
Quoted 8 lines · Eric W. Biederman
Quoted 7 lines
this series gets rid of playing with the address limit in the exec and coredump code. Most of this was fairly trivial, the biggest changes are those to the spufs coredump code.
Changes since v1:
- properly spell NUL
- properly handle the compat siginfo case in ELF coredumps
Quoted 6 lines
Quick question is exec from a kernel thread within the scope of what you are looking at?
There is a
set_fs(USER_DS)inflush_old_execwhose sole purpose appears to be to allow exec from kernel threads. Where the kernel threads run withset_fs(KERNEL_DS)until they call exec.
This series doesn't really look at that area. But I don't think exec from a kernel thread makes any sense, and cleaning up how to set the initial USER_DS vs KERNEL_DS state is something I'll eventually get to, it seems like a major mess at the moment.
Quoted 12 lines · Christoph Hellwig
Quoted 11 lines · Eric W. Biederman
Quoted 7 lines
this series gets rid of playing with the address limit in the exec and coredump code. Most of this was fairly trivial, the biggest changes are those to the spufs coredump code.
Changes since v1:
- properly spell NUL
- properly handle the compat siginfo case in ELF coredumpsQuick question is exec from a kernel thread within the scope of what you are looking at?
Quoted 8 lines
Quoted 3 lines
There is a
set_fs(USER_DS)inflush_old_execwhose sole purpose appears to be to allow exec from kernel threads. Where the kernel threads run withset_fs(KERNEL_DS)until they call exec.This series doesn't really look at that area. But I don't think exec from a kernel thread makes any sense, and cleaning up how to set the initial
USER_DSvsKERNEL_DSstate is something I'll eventually get to, it seems like a major mess at the moment.
Fair enough. I just wanted to make certain that it is on people's radar that when the kernel exec's init the arguments are read from kernel memory and the set_fs(USER_DS) in flush_old_exec() that makes that not work later.
It is subtle and easy to miss. So I figured I would mention it since I have been staring at the exec code a lot lately.
Eric