exec: refactor how call_usermodehelper works, and update the sense of the core_pipe recursion check (v4 rediff)
Subsystems (6)
cover exec: refactor how call_usermodehelper works, and update the sense of the core_pipe recursion check (v4 rediff)
Hey, so I've rediffed and tested the user mode helper bits from my previous patch set that got wrecked when the rcustring bits got pulled. I've got he origional summary below, and the patches follwoing. Note, their just my changes, rediffed to work without Andis patch set. The other umh call sites can still use plain old call_usermodehelper as they did previously. I'll submit patches as needed for other callers to make use of the init/cleanup functions as I have the time/ability to test those changes properly. The changes below I've been able to test and verify myself.
Hey all-
So, about 6 months ago, I made a set of changes to how the
core-dump-to-a-pipe feature in the kernel works. We had reports of several races, including some reports of apps bypassing our recursion check so that a process that was forked as part of a core_pattern setup could infinitely crash and refork until the system crashed.
We fixes those by improving our recursion checks. The new check
basically refuses to fork a process if its core limit is zero, which works well.
Unfortunately, I've been getting grief from maintainer of user space
programs that are inserted as the forked process of core_pattern. They contend that in order for their programs (such as abrt and apport) to work, all the running processes in a system must have their core limits set to a non-zero value, to which I say 'yes'. I did this by design, and think thats the right way to do things.
But I've been asked to ease this burden on user space enough times that
I thought I would take a look at it. The first suggestion was to make the recursion check fail on a non-zero 'special' number, like one. That way the core collector process could set its core size ulimit to 1, and enable the kernel's recursion detection. This isn't a bad idea on the surface, but I don't like it since its opt-in, in that if a program like abrt or apport has a bug and fails to set such a core limit, we're left with a recursively crashing system again.
So I've come up with this. What I've done is modify thecall_usermodehelper api such that an extra parameter is added, a function pointer which will be called by the user helper task, after it forks, but before it exec's the required process. This will give the caller the opportunity to get a call back in the processes context, allowing it to do whatever it needs to to the process in the kernel prior to exec-ing the user space code. In the case of do_coredump, this callback is ues to set the core ulimit of the helper process to 1. This elimnates the opt-in problem that I had above, as it allows the ulimit for core sizes to be set to the value of 1, which is what the recursion check looks for in do_coredump.
This patch has been tested successfully by me.
Re: [PATCH 1/2] kmod: add init function to usermodehelper
Rediff of the usermodehelper changes after all the wreckage. This patch encompasses the patches that were formally in -mm as:
kmod-add-init-function-to-usermodehelper.patch kmod-add-init-function-to-usermodehelper-fix.patch
I also rolled in changes from Oleg, moving the call of the init method down in ____call_usermodehelper, so the init function could override the nice level on the process.
Create new function call_usermodehelper_fns() and allow it to assign both an init and cleanup function, as we'll as arbitrary data.
The init function is called from the context of the forked process and allows for customization of the helper process prior to calling exec. Its return code gates the continuation of the process, or causes its exit. Also add an arbitrary data pointer to the subprocess_info struct allowing for data to be passed from the caller to the new process, and the subsequent cleanup process
Also, use this patch to cleanup the cleanup function. It currently takes an argp and envp pointer for freeing, which is ugly. Lets instead just make the subprocess_info structure public, and pass that to the cleanup and init routines
include/linux/kmod.h | 51 +++++++++++++++++++++++++++++++++++++++++---------- kernel/kmod.c | 51 +++++++++++++++++++++++++++++---------------------- kernel/sys.c | 6 +++--- 3 files changed, 73 insertions(+), 35 deletions(-)
include/linux/kmod.h +41 -10
@@ -23,6 +23,7 @@#include <linux/stddef.h> #include <linux/errno.h> #include <linux/compiler.h> +#include <linux/workqueue.h> #define KMOD_PATH_LEN 256
@@ -45,7 +46,27 @@ static inline int request_module_nowait(const char *name, ...) { return -ENOSYS;struct key ;struct file ; -struct subprocess_info ; + +enum umh_wait { +UMH_NO_WAIT =-1 ,/* don't wait at all */ +UMH_WAIT_EXEC =0 ,/* wait for the exec, but not the process */ +UMH_WAIT_PROC =1 ,/* wait for the process to complete */ +}; + +struct subprocess_info { +struct work_struct work; +struct completion *complete; +struct cred *cred; +char *path; +char **argv; +char **envp; +enum umh_wait wait; +int retval; +struct file *stdin; +int (*init)(struct subprocess_info *info); +void (*cleanup)(struct subprocess_info *info); +void *data; +};/* Allocate a subprocess_info structure */ struct subprocess_info *call_usermodehelper_setup(char *path, char **argv,
@@ -56,14 +77,10 @@ void call_usermodehelper_setkeys(struct subprocess_info *info, struct key *session_keyring);int call_usermodehelper_stdinpipe (struct subprocess_info *sub_info,struct file **filp); -void call_usermodehelper_setcleanup (struct subprocess_info *info, -void (*cleanup)(char **argv,char **envp)); - -enum umh_wait { -UMH_NO_WAIT =-1 ,/* don't wait at all */ -UMH_WAIT_EXEC =0 ,/* wait for the exec, but not the process */ -UMH_WAIT_PROC =1 ,/* wait for the process to complete */ -}; +void call_usermodehelper_setfns (struct subprocess_info *info, +int (*init)(struct subprocess_info *info), +void (*cleanup)(struct subprocess_info *info), +void *data);/* Actually execute the sub-process */ int call_usermodehelper_exec (struct subprocess_info *info,enum umh_wait wait);
@@ -73,18 +90,32 @@ int call_usermodehelper_exec(struct subprocess_info *info, enum umh_wait wait);void call_usermodehelper_freeinfo (struct subprocess_info *info);static inline int -call_usermodehelper (char *path,char **argv,char **envp,enum umh_wait wait) +call_usermodehelper_fns (char *path,char **argv,char **envp, +enum umh_wait wait, +int (*init)(struct subprocess_info *info), +void (*cleanup)(struct subprocess_info *),void *data) {struct subprocess_info *info;gfp_t gfp_mask = (wait ==UMH_NO_WAIT ) ?GFP_ATOMIC :GFP_KERNEL ; info =call_usermodehelper_setup (path, argv, envp, gfp_mask); +if (info ==NULL )return -ENOMEM ; + +call_usermodehelper_setfns (info, init, cleanup, data); +return call_usermodehelper_exec (info, wait); }static inline int +call_usermodehelper (char *path,char **argv,char **envp,enum umh_wait wait) +{ +return call_usermodehelper_fns (path, argv, envp, wait, +NULL ,NULL ,NULL ); +} + +static inline int call_usermodehelper_keys (char *path,char **argv,char **envp,struct key *session_keyring,enum umh_wait wait) {
kernel/kmod.c +29 -22
100 lines not on this page · raw message
kernel/sys.c +3 -3
@@ -1765,9 +1765,9 @@ SYSCALL_DEFINE3(getcpu, unsigned __user *, cpup, unsigned __user *, nodep,char poweroff_cmd[POWEROFF_CMD_PATH_LEN ] ="/sbin/poweroff" ; -static void argv_cleanup (char **argv,char **envp) +static void argv_cleanup (struct subprocess_info *info) { -argv_free (argv); +argv_free (info->argv); }/**
@@ -1801,7 +1801,7 @@ int orderly_poweroff(bool force) goto out; } -call_usermodehelper_setcleanup (info, argv_cleanup); +call_usermodehelper_setfns (info,NULL , argv_cleanup,NULL ); ret =call_usermodehelper_exec (info,UMH_NO_WAIT );
Oleg Nesterov
Reviewed-byRe: [PATCH 1/2] kmod: add init function to usermodehelper
kernel/kmod.c int __request_module(bool wait, const char *fmt, ...) · line 121
Quoted 12 lines · Neil Horman
kernel/kmod.c +3
-2@@ -116,27 +116,16 @@ int __request_module(bool wait, const char *fmt, ...)trace_module_request (module_name, wait, _RET_IP_); - ret =call_usermodehelper (modprobe_path, argv, envp, - wait ?UMH_WAIT_PROC :UMH_WAIT_EXEC ); + ret =call_usermodehelper_fns (modprobe_path, argv, envp, + wait ?UMH_WAIT_PROC :UMH_WAIT_EXEC , +NULL ,NULL ,NULL );
This change looks unnecessary, but doesn't hurt.
Re: [PATCH 1/2] kmod: add init function to usermodehelper
Quoted 8 lines · Oleg Nesterov
Quoted 7 lines · Neil Horman
kernel/kmod.c +0
-0@@ -116,27 +116,16 @@ int __request_module(bool wait, const char *fmt, ...)trace_module_request (module_name, wait, _RET_IP_);
Quoted 8 lines
kernel/kmod.c int __request_module(bool wait, const char *fmt, ...) · line 121
Quoted 5 lines
kernel/kmod.c +3
-2- ret =call_usermodehelper (modprobe_path, argv, envp, - wait ?UMH_WAIT_PROC :UMH_WAIT_EXEC ); + ret =call_usermodehelper_fns (modprobe_path, argv, envp, + wait ?UMH_WAIT_PROC :UMH_WAIT_EXEC , +NULL ,NULL ,NULL );This change looks unnecessary, but doesn't hurt.
Yeah, I concur, it was part of the origional patch, when that call_usermodehelper had already been replaced with call_usermodehelper_cleanup. I made my change anyway, figuring part of the purpose of this longer term would be to convert the call_usermodehelper functions to the init/cleanup enabled variants, which andi's patchset will eventually do here
Neil
Quoted 3 lines
Reviewed-by Oleg Nesterov <[email protected]>
Re: [PATCH 2/2] exec: replace call_usermodehelper_pipe with use of umh init function and resolve limit
In reply to Neil HormanRediff of the usermodehelper changes after all the wreckage. This patch encompasses the patches that were formally in -mm as:
kmod-replace-call_usermodehelper_pipe-with-use-of-umh-init-function-and-resolve-limit.patch kmod-replace-call_usermodehelper_pipe-with-use-of-umh-init-function-and-resolve-limitcleanup.patch
kmod: replace call_usermodehelper_pipe() with use of umh init function and resolve limit
The first patch in this series introduced an init function to the call_usermodehelper api so that processes could be customized by caller. This patch takes advantage of that fact, by customizing the helper in do_coredump to create the pipe and set its core limit to one (for our recusrsion check). This lets us clean up the previous uglyness in the usermodehelper internals and factor call_usermodehelper out entirely. While I'm at it, we can also modify the helper setup to look for a core limit value of 1 rather than zero for our recursion check
fs/exec.c | 63 ++++++++++++++++++++++++++++++++++----- include/linux/kmod.h | 7 ---- kernel/kmod.c | 82 --------------------------------------------------- 3 files changed, 56 insertions(+), 96 deletions(-)
fs/exec.c +56 -7
@@ -1784,6 +1784,50 @@ static void wait_for_dump_helpers(struct file *file) } +/* +* uhm_pipe_setup +* helper function to customize the process used +* to collect the core in userspace. Specifically +* it sets up a pipe and installs it as fd 0 (stdin) +* for the process. Returns 0 on success, or +* PTR_ERR on failure. +* Note that it also sets the core limit to 1. This +* is a special value that we use to trap recursive +* core dumps +*/ +static int umh_pipe_setup (struct subprocess_info *info) +{ +struct file *rp, *wp; +struct fdtable *fdt; +struct coredump_params *cp = (struct coredump_params *)info->data; +struct files_struct *cf = current->files; + + wp =create_write_pipe (0 ); +if (IS_ERR (wp)) +return PTR_ERR (wp); + + rp =create_read_pipe (wp,0 ); +if (IS_ERR (rp)) { +free_write_pipe (wp); +return PTR_ERR (rp); + } + + cp->file = wp; + +sys_close (0 ); +fd_install (0 , rp); +spin_lock (&cf->file_lock); + fdt =files_fdtable (cf); +FD_SET (0 , fdt->open_fds); +FD_CLR (0 , fdt->close_on_exec); +spin_unlock (&cf->file_lock); + +/* and disallow core files too */ + current->signal->rlim[RLIMIT_CORE ] = (struct rlimit ){1 ,1 }; + +return 0 ; +} +void do_coredump (long signr,int exit_code,struct pt_regs *regs) {struct core_state core_state;
@@ -1871,15 +1915,15 @@ void do_coredump(long signr, int exit_code, struct pt_regs *regs) goto fail_unlock;if (ispipe) { -if (cprm.limit ==0 ) { +if (cprm.limit ==1 ) {/* * Normally core limits are irrelevant to pipes, since * we're not writing to the file system, but we use -* cprm.limit of 0 here as a speacial value. Any -* non-zero limit gets set to RLIM_INFINITY below, but +* cprm.limit of 1 here as a speacial value. Any +* non-1 limit gets set to RLIM_INFINITY below, but * a limit of 0 skips the dump. This is a consistent * way to catch recursive crashes. We can still crash -* if the core_pattern binary sets RLIM_CORE = !0 +* if the core_pattern binary sets RLIM_CORE = !1 * but it runs as root, and can do lots of stupid things * Note that we use task_tgid_vnr here to grab the pid * of the process group leader. That way we get the
@@ -1887,7 +1931,7 @@ void do_coredump(long signr, int exit_code, struct pt_regs *regs)* core_pattern process dies. */ printk (KERN_WARNING -"Process %d(%s) has RLIMIT_CORE set to 0\n" , +"Process %d(%s) has RLIMIT_CORE set to 1\n" ,task_tgid_vnr (current), current->comm);printk (KERN_WARNING "Aborting core\n" ); goto fail_unlock;
@@ -1911,8 +1955,13 @@ void do_coredump(long signr, int exit_code, struct pt_regs *regs) cprm.limit =RLIM_INFINITY ;/* SIGPIPE can happen, but it's just never processed */ -if (call_usermodehelper_pipe (helper_argv[0 ], helper_argv,NULL , - &cprm.file)) { + cprm.file =NULL ; +if (call_usermodehelper_fns (helper_argv[0 ], helper_argv,NULL , +UMH_WAIT_EXEC , umh_pipe_setup, +NULL , &cprm)) { +if (cprm.file) +filp_close (cprm.file,NULL ); +printk (KERN_INFO "Core dump to %s pipe failed\n" , corename); goto fail_dropcount;
include/linux/kmod.h +0 -7
@@ -62,7 +62,6 @@ struct subprocess_info {char **envp;enum umh_wait wait;int retval; -struct file *stdin;int (*init)(struct subprocess_info *info);void (*cleanup)(struct subprocess_info *info);void *data;
@@ -75,8 +74,6 @@ struct subprocess_info *call_usermodehelper_setup(char *path, char **argv,/* Set various pieces of state into the subprocess_info structure */ void call_usermodehelper_setkeys (struct subprocess_info *info,struct key *session_keyring); -int call_usermodehelper_stdinpipe (struct subprocess_info *sub_info, -struct file **filp); void call_usermodehelper_setfns(struct subprocess_info *info, int (*init)(struct subprocess_info *info), void (*cleanup)(struct subprocess_info *info),
@@ -132,10 +129,6 @@ call_usermodehelper_keys(char *path, char **argv, char **envp,extern void usermodehelper_init (void ); -struct file ; -extern int call_usermodehelper_pipe (char *path,char *argv[],char *envp[], -struct file **filp); -extern int usermodehelper_disable (void );extern void usermodehelper_enable (void );
kernel/kmod.c +0 -82
100 lines not on this page · raw message
Oleg Nesterov
Reviewed-byRe: [PATCH 2/2] exec: replace call_usermodehelper_pipe with use of umh init function and resolve limit
Quoted 10 lines · Neil Horman
The first patch in this series introduced an init function to the
call_usermodehelperapi so that processes could be customized by caller. This patch takes advantage of that fact, by customizing the helper indo_coredumpto create the pipe and set its core limit to one (for our recusrsion check). This lets us clean up the previous uglyness in the usermodehelper internals and factorcall_usermodehelperout entirely. While I'm at it, we can also modify the helper setup to look for a core limit value of 1 rather than zero for our recursion check
Quoted 3 lines
Signed-off-by Neil Horman <[email protected]>CC Oleg Nesterov <[email protected]>CC Andi Kleen <[email protected]>
cover umh: keys, signals, misc
In reply to Neil HormanQuoted 3 lines · Neil Horman
Hey, so I've rediffed and tested the user mode helper bits from my previous patch set that got wrecked when the rcustring bits got pulled.
More wrecked patches on top of Neil's refactoring.
None was changed, so I keeped the acks.
Oleg.
PATCH 1/6 umh: creds: convert call_usermodehelper_keys() to use subprocess_info->init()
call_usermodehelper_keys() uses call_usermodehelper_setkeys() to change subprocess_info->cred in advance. Now that we have info->init() we can change this code to set tgcred->session_keyring in context of execing kernel thread.
Note: since currently call_usermodehelper_keys() is never called with UMH_NO_WAIT, call_usermodehelper_keys()->key_get() and umh_keys_cleanup() are not really needed, we could rely on install_session_keyring_to_cred() which does key_get() on success.
---
include/linux/kmod.h | 17 ----------------- kernel/kmod.c | 18 ------------------ security/keys/internal.h | 1 + security/keys/process_keys.c | 3 +-- security/keys/request_key.c | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 34 insertions(+), 37 deletions(-)
include/linux/kmod.h renamed from include/linux/kmod.h~1_CONVERT_KEYS +0 -17
@@ -71,8 +71,6 @@ struct subprocess_info *call_usermodehel char **envp, gfp_t gfp_mask);/* Set various pieces of state into the subprocess_info structure */ -void call_usermodehelper_setkeys (struct subprocess_info *info, -struct key *session_keyring); void call_usermodehelper_setfns(struct subprocess_info *info, int (*init)(struct subprocess_info *info), void (*cleanup)(struct subprocess_info *info),
@@ -111,21 +109,6 @@ call_usermodehelper(char *path, char **a NULL, NULL, NULL); } -static inline int -call_usermodehelper_keys (char *path,char **argv,char **envp, -struct key *session_keyring,enum umh_wait wait) -{ -struct subprocess_info *info; -gfp_t gfp_mask = (wait ==UMH_NO_WAIT ) ?GFP_ATOMIC :GFP_KERNEL ; - - info =call_usermodehelper_setup (path, argv, envp, gfp_mask); -if (info ==NULL ) -return -ENOMEM ; - -call_usermodehelper_setkeys (info, session_keyring); -return call_usermodehelper_exec (info, wait); -} -extern void usermodehelper_init (void );extern int usermodehelper_disable (void );
kernel/kmod.c renamed from kernel/kmod.c~1_CONVERT_KEYS +0 -18
@@ -367,24 +367,6 @@ struct subprocess_info *call_usermodehelEXPORT_SYMBOL (call_usermodehelper_setup);/** -* call_usermodehelper_setkeys - set the session keys for usermode helper -* @info: a subprocess_info returned by call_usermodehelper_setup -* @session_keyring: the session keyring for the process -*/ -void call_usermodehelper_setkeys (struct subprocess_info *info, -struct key *session_keyring) -{ -#ifdef CONFIG_KEYS -struct thread_group_cred *tgcred = info->cred->tgcred; -key_put (tgcred->session_keyring); - tgcred->session_keyring =key_get (session_keyring); -#else -BUG (); -#endif -} -EXPORT_SYMBOL (call_usermodehelper_setkeys); - -/** * call_usermodehelper_setfns - set a cleanup/init function * @info: a subprocess_info returned by call_usermodehelper_setup * @cleanup: a cleanup function
security/keys/internal.h renamed from security/keys/internal.h~1_CONVERT_KEYS +1 -0
@@ -115,6 +115,7 @@ extern struct key *find_keyring_by_name(extern int install_user_keyrings (void );extern int install_thread_keyring_to_cred (struct cred *);extern int install_process_keyring_to_cred (struct cred *); +extern int install_session_keyring_to_cred (struct cred *,struct key *); extern struct key *request_key_and_link(struct key_type *type, const char *description,
security/keys/process_keys.c renamed from security/keys/process_keys.c~1_CONVERT_KEYS +1 -2
@@ -217,8 +217,7 @@ static int install_process_keyring(void)/* * install a session keyring directly to a credentials struct */ -static int install_session_keyring_to_cred (struct cred *cred, -struct key *keyring) +int install_session_keyring_to_cred (struct cred *cred,struct key *keyring) {unsigned long flags;struct key *old;
security/keys/request_key.c renamed from security/keys/request_key.c~1_CONVERT_KEYS +32 -0
@@ -58,6 +58,38 @@ void complete_request_key(struct key_con }EXPORT_SYMBOL (complete_request_key); +static int umh_keys_init (struct subprocess_info *info) +{ +struct cred *cred = (struct cred *)current_cred (); +struct key *keyring = info->data; +/* +* This is called in context of freshly forked kthread before +* kernel_execve(), we can just change our ->session_keyring. +*/ +return install_session_keyring_to_cred (cred, keyring); +} + +static void umh_keys_cleanup (struct subprocess_info *info) +{ +struct key *keyring = info->data; +key_put (keyring); +} + +static int call_usermodehelper_keys (char *path,char **argv,char **envp, +struct key *session_keyring,enum umh_wait wait) +{ +gfp_t gfp_mask = (wait ==UMH_NO_WAIT ) ?GFP_ATOMIC :GFP_KERNEL ; +struct subprocess_info *info = +call_usermodehelper_setup (path, argv, envp, gfp_mask); + +if (!info) +return -ENOMEM ; + +call_usermodehelper_setfns (info, umh_keys_init, umh_keys_cleanup, +key_get (session_keyring)); +return call_usermodehelper_exec (info, wait); +} +/* * request userspace finish the construction of a key * - execute "/sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring>"
PATCH 2/6 umh: creds: kill subprocess_info->cred logic
In reply to Oleg NesterovNow that nobody ever changes subprocess_info->cred we can kill this member and related code. ____call_usermodehelper() always runs in the context of freshly forked kernel thread, it has the proper ->cred copied from its parent kthread, keventd.
---
include/linux/cred.h | 1 include/linux/kmod.h | 1 kernel/cred.c | 54 --------------------------------------------------- kernel/kmod.c | 19 ----------------- 4 files changed, 75 deletions(-)
include/linux/cred.h renamed from include/linux/cred.h~2_KILL_INFO_CRED +0 -1
@@ -156,7 +156,6 @@ extern int copy_creds(struct task_structextern struct cred *cred_alloc_blank (void );extern struct cred *prepare_creds (void );extern struct cred *prepare_exec_creds (void ); -extern struct cred *prepare_usermodehelper_creds (void );extern int commit_creds (struct cred *);extern void abort_creds (struct cred *);extern const struct cred *override_creds (const struct cred *);
include/linux/kmod.h renamed from include/linux/kmod.h~2_KILL_INFO_CRED +0 -1
@@ -55,7 +55,6 @@ enum umh_wait {struct subprocess_info {struct work_struct work;struct completion *complete; -struct cred *cred;char *path;char **argv;char **envp;
kernel/cred.c renamed from kernel/cred.c~2_KILL_INFO_CRED +0 -54
60 lines not on this page · raw message
kernel/kmod.c renamed from kernel/kmod.c~2_KILL_INFO_CRED +0 -19
@@ -134,8 +134,6 @@ static int ____call_usermodehelper(voidstruct subprocess_info *sub_info = data;int retval; -BUG_ON (atomic_read (&sub_info->cred->usage) !=1 ); -/* Unblock all signals */ spin_lock_irq (¤t->sighand->siglock);flush_signal_handlers (current,1 );
@@ -143,10 +141,6 @@ static int ____call_usermodehelper(voidrecalc_sigpending ();spin_unlock_irq (¤t->sighand->siglock); -/* Install the credentials */ -commit_creds (sub_info->cred); - sub_info->cred =NULL ; -/* We can run anywhere, unlike our parent keventd(). */ set_cpus_allowed_ptr (current, cpu_all_mask);
@@ -174,8 +168,6 @@ void call_usermodehelper_freeinfo(struct {if (info->cleanup) (*info->cleanup)(info); -if (info->cred) -put_cred (info->cred);kfree (info); }EXPORT_SYMBOL (call_usermodehelper_freeinfo);
@@ -231,8 +223,6 @@ static void __call_usermodehelper(structpid_t pid;enum umh_wait wait = sub_info->wait; -BUG_ON (atomic_read (&sub_info->cred->usage) !=1 ); -/* CLONE_VFORK: wait until the usermode helper has execve'd * successfully We need the data structures to stay around * until that is done. */
@@ -355,12 +345,6 @@ struct subprocess_info *call_usermodehel sub_info->path = path; sub_info->argv = argv; sub_info->envp = envp; - sub_info->cred =prepare_usermodehelper_creds (); -if (!sub_info->cred) { -kfree (sub_info); -return NULL ; - } - out:return sub_info; }
@@ -411,9 +395,6 @@ int call_usermodehelper_exec(struct subpDECLARE_COMPLETION_ONSTACK (done);int retval =0 ; -BUG_ON (atomic_read (&sub_info->cred->usage) !=1 ); -validate_creds (sub_info->cred); -helper_lock ();if (sub_info->path[0 ] =='\0' ) goto out;
PATCH 3/6 call_usermodehelper: no need to unblock signals
In reply to Oleg Nesterov____call_usermodehelper() correctly calls flush_signal_handlers() to set SIG_DFL, but sigemptyset(->blocked) and recalc_sigpending() are not needed.
This kthread was forked by workqueue thread, all signals must be unblocked and ignored, no pending signal is possible.
---
kernel/kmod.c | 3 --- 1 file changed, 3 deletions(-)
kernel/kmod.c renamed from kernel/kmod.c~3_DONT_UNBLOCK +0 -3
@@ -134,11 +134,8 @@ static int ____call_usermodehelper(voidstruct subprocess_info *sub_info = data;int retval; -/* Unblock all signals */ spin_lock_irq (¤t->sighand->siglock);flush_signal_handlers (current,1 ); -sigemptyset (¤t->blocked); -recalc_sigpending ();spin_unlock_irq (¤t->sighand->siglock);/* We can run anywhere, unlike our parent keventd(). */
PATCH 4/6 wait_for_helper: SIGCHLD from user-space can lead to use-after-free
In reply to Oleg Nesterov1. wait_for_helper() calls allow_signal(SIGCHLD) to ensure the child can't autoreap itself.
However, this means that a spurious SIGCHILD from user-space can set TIF_SIGPENDING and:
- kernel_thread() or sys_wait4() can fail due to signal_pending()
- worse, wait4() can fail before ____call_usermodehelper() execs or exits. In this case the caller may kfree(subprocess_info) while the child still uses this memory.
Change the code to use SIG_DFL instead of magic "(void __user *)2" set by allow_signal(). This means that SIGCHLD won't be delivered, yet the child won't autoreap itsefl.
The problem is minor, only root can send a signal to this kthread.
2. If sys_wait4(&ret) fails it doesn't populate "ret", in this case wait_for_helper() reports a random value from uninitialized var.
With this patch sys_wait4() should never fail, but still it makes sense to initialize ret = -ECHILD so that the caller can notice the problem.
---
kernel/kmod.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
kernel/kmod.c renamed from kernel/kmod.c~4_SIGCHLD_RACE +5 -5
@@ -175,16 +175,16 @@ static int wait_for_helper(void *data)struct subprocess_info *sub_info = data;pid_t pid; -/* Install a handler: if SIGCLD isn't handled sys_wait4 won't -* populate the status, but will return -ECHILD. */ -allow_signal (SIGCHLD ); +/* If SIGCLD is ignored sys_wait4 won't populate the status. */ +spin_lock_irq (¤t->sighand->siglock); + current->sighand->action[SIGCHLD -1 ].sa.sa_handler =SIG_DFL ; +spin_unlock_irq (¤t->sighand->siglock); pid =kernel_thread (____call_usermodehelper, sub_info,SIGCHLD );if (pid <0 ) { sub_info->retval = pid; }else { -int ret; - +int ret = -ECHILD ;/* * Normally it is bogus to call wait4() from in-kernel because * wait4() wants to write the exit code to a userspace address.
PATCH 5/6 call_usermodehelper: simplify/fix UMH_NO_WAIT case
In reply to Oleg Nesterov__call_usermodehelper(UMH_NO_WAIT) has 2 problems:
- if kernel_thread() fails, call_usermodehelper_freeinfo() is not called.
- for unknown reason UMH_NO_WAIT has UMH_WAIT_PROC logic, we spawn yet another thread which waits until the user mode application exits.
Change the UMH_NO_WAIT code to use ____call_usermodehelper() instead of wait_for_helper(), and do call_usermodehelper_freeinfo() unconditionally. We can rely on CLONE_VFORK, do_fork(CLONE_VFORK) until the child exits or execs.
With or without this patch UMH_NO_WAIT does not report the error if kernel_thread() fails, this is correct since the caller doesn't wait for result.
---
kernel/kmod.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-)
kernel/kmod.c renamed from kernel/kmod.c~5_UMH_NO_WAIT +4 -6
@@ -205,10 +205,7 @@ static int wait_for_helper(void *data) sub_info->retval = ret; } -if (sub_info->wait ==UMH_NO_WAIT ) -call_usermodehelper_freeinfo (sub_info); -else -complete (sub_info->complete); +complete (sub_info->complete);return 0 ; }
@@ -217,13 +214,13 @@ static void __call_usermodehelper(struct {struct subprocess_info *sub_info = container_of(work, struct subprocess_info, work); -pid_t pid;enum umh_wait wait = sub_info->wait; +pid_t pid;/* CLONE_VFORK: wait until the usermode helper has execve'd * successfully We need the data structures to stay around * until that is done. */ -if (wait ==UMH_WAIT_PROC || wait ==UMH_NO_WAIT ) +if (wait ==UMH_WAIT_PROC ) pid =kernel_thread (wait_for_helper, sub_info,CLONE_FS |CLONE_FILES |SIGCHLD );else
@@ -232,6 +229,7 @@ static void __call_usermodehelper(structswitch (wait) {case UMH_NO_WAIT : +call_usermodehelper_freeinfo (sub_info);break ;case UMH_WAIT_PROC :
PATCH 6/6 call_usermodehelper: UMH_WAIT_EXEC ignores kernel_thread() failure
In reply to Oleg NesterovUMH_WAIT_EXEC should report the error if kernel_thread() fails, like UMH_WAIT_PROC does.
---
kernel/kmod.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/kmod.c renamed from kernel/kmod.c~6_UMH_WAIT_EXEC +2 -2
@@ -235,10 +235,10 @@ static void __call_usermodehelper(structcase UMH_WAIT_PROC :if (pid >0 )break ; - sub_info->retval = pid;/* FALLTHROUGH */ -case UMH_WAIT_EXEC : +if (pid <0 ) + sub_info->retval = pid;complete (sub_info->complete); } }
cover do_coredump: cleanups
In reply to Oleg NesterovQuoted 7 lines · Oleg Nesterov
Quoted 3 lines · Neil Horman
Hey, so I've rediffed and tested the user mode helper bits from my previous patch set that got wrecked when the rcustring bits got pulled.
More wrecked patches on top of Neil's refactoring.
And some more, this time in fs/exec.c.
do_coredump() is unreadable and needs cleanups.
Perhaps we should add the new helper which opens the file or pipe...
Oleg.
PATCH 1/4 coredump: factor out the not-ispipe file checks
do_coredump() does a lot of file checks after it opens the file or calls usermode helper. But all of these checks are only needed in !ispipe case.
Move this code into the "else" branch and kill the ugly repetitive ispipe checks.
---
fs/exec.c | 61 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 30 insertions(+), 31 deletions(-)
fs/exec.c renamed from fs/exec.c~1_ISREG +30 -31
@@ -1834,7 +1834,6 @@ void do_coredump(long signr, int exit_cochar corename[CORENAME_MAX_SIZE +1 ];struct mm_struct *mm = current->mm;struct linux_binfmt * binfmt; -struct inode * inode;const struct cred *old_cred;struct cred *cred;int retval =0 ;
@@ -1911,9 +1910,6 @@ void do_coredump(long signr, int exit_co ispipe =format_corename (corename, signr);unlock_kernel (); -if ((!ispipe) && (cprm.limit < binfmt->min_coredump)) - goto fail_unlock; -if (ispipe) {if (cprm.limit ==1 ) {/*
@@ -1966,39 +1962,42 @@ void do_coredump(long signr, int exit_co corename); goto fail_dropcount; } - }else + }else { +struct inode *inode; + +if (cprm.limit < binfmt->min_coredump) + goto fail_unlock; + cprm.file =filp_open (corename,O_CREAT |2 |O_NOFOLLOW |O_LARGEFILE | flag,0600 ); -if (IS_ERR (cprm.file)) - goto fail_dropcount; - inode = cprm.file->f_path.dentry->d_inode; -if (inode->i_nlink >1 ) - goto close_fail;/* multiple links - don't dump */ -if (!ispipe &&d_unhashed (cprm.file->f_path.dentry)) - goto close_fail; +if (IS_ERR (cprm.file)) + goto fail_unlock; -/* AK: actually i see no reason to not allow this for named pipes etc., -but keep the previous behaviour for now. */ -if (!ispipe && !S_ISREG (inode->i_mode)) - goto close_fail; -/* -* Dont allow local users get cute and trick others to coredump -* into their pre-created files: -* Note, this is not relevant for pipes -*/ -if (!ispipe && (inode->i_uid !=current_fsuid ())) - goto close_fail; -if (!cprm.file->f_op) - goto close_fail; -if (!cprm.file->f_op->write) - goto close_fail; -if (!ispipe && -do_truncate (cprm.file->f_path.dentry,0 ,0 , cprm.file) !=0 ) - goto close_fail; + inode = cprm.file->f_path.dentry->d_inode; +if (inode->i_nlink >1 ) + goto close_fail; +if (d_unhashed (cprm.file->f_path.dentry)) + goto close_fail; +/* +* AK: actually i see no reason to not allow this for named +* pipes etc, but keep the previous behaviour for now. +*/ +if (!S_ISREG (inode->i_mode)) + goto close_fail; +/* +* Dont allow local users get cute and trick others to coredump +* into their pre-created files. +*/ +if (inode->i_uid !=current_fsuid ()) + goto close_fail; +if (!cprm.file->f_op || !cprm.file->f_op->write) + goto close_fail; +if (do_truncate (cprm.file->f_path.dentry,0 ,0 , cprm.file)) + goto close_fail; + } retval = binfmt->core_dump (&cprm); -if (retval) current->signal->group_exit_code |=0x80 ; close_fail:
PATCH 2/4 coredump: cleanup "ispipe" code
In reply to Oleg Nesterov- kill "int dump_count", argv_split(argcp) accepts argcp == NULL.
- move "int dump_count" under " if (ispipe)" branch, fail_dropcount can check ispipe.
- move "char **helper_argv" as well, change the code to do argv_free() right after call_usermodehelper_fns().
- If call_usermodehelper_fns() fails goto close_fail label instead of closing the file by hand.
---
fs/exec.c | 39 +++++++++++++++++---------------------- 1 file changed, 17 insertions(+), 22 deletions(-)
fs/exec.c renamed from fs/exec.c~2_IFIFO +17 -22
@@ -1838,10 +1838,7 @@ void do_coredump(long signr, int exit_costruct cred *cred;int retval =0 ;int flag =0 ; -int ispipe =0 ; -char **helper_argv =NULL ; -int helper_argc =0 ; -int dump_count =0 ; +int ispipe;static atomic_t core_dump_count =ATOMIC_INIT (0 );struct coredump_params cprm = { .signr = signr,
@@ -1911,6 +1908,9 @@ void do_coredump(long signr, int exit_counlock_kernel ();if (ispipe) { +int dump_count; +char **helper_argv; +if (cprm.limit ==1 ) {/* * Normally core limits are irrelevant to pipes, since
@@ -1932,6 +1932,7 @@ void do_coredump(long signr, int exit_coprintk (KERN_WARNING "Aborting core\n" ); goto fail_unlock; } + cprm.limit =RLIM_INFINITY ; dump_count =atomic_inc_return (&core_dump_count);if (core_pipe_limit && (core_pipe_limit < dump_count)) {
@@ -1941,26 +1942,21 @@ void do_coredump(long signr, int exit_co goto fail_dropcount; } - helper_argv =argv_split (GFP_KERNEL , corename+1 , &helper_argc); + helper_argv =argv_split (GFP_KERNEL , corename+1 ,NULL );if (!helper_argv) {printk (KERN_WARNING "%s failed to allocate memory\n" , __func__); goto fail_dropcount; } - cprm.limit =RLIM_INFINITY ; - -/* SIGPIPE can happen, but it's just never processed */ - cprm.file =NULL ; -if (call_usermodehelper_fns (helper_argv[0 ], helper_argv,NULL , -UMH_WAIT_EXEC , umh_pipe_setup, -NULL , &cprm)) { -if (cprm.file) -filp_close (cprm.file,NULL ); - + retval =call_usermodehelper_fns (helper_argv[0 ], helper_argv, +NULL ,UMH_WAIT_EXEC , umh_pipe_setup, +NULL , &cprm); +argv_free (helper_argv); +if (retval) {printk (KERN_INFO "Core dump to %s pipe failed\n" , corename); - goto fail_dropcount; + goto close_fail; } }else {struct inode *inode;
@@ -2000,17 +1996,16 @@ void do_coredump(long signr, int exit_co retval = binfmt->core_dump (&cprm);if (retval) current->signal->group_exit_code |=0x80 ; -close_fail: +if (ispipe && core_pipe_limit)wait_for_dump_helpers (cprm.file); -filp_close (cprm.file,NULL ); +close_fail: +if (cprm.file) +filp_close (cprm.file,NULL ); fail_dropcount: -if (dump_count) +if (ispipe)atomic_dec (&core_dump_count); fail_unlock: -if (helper_argv) -argv_free (helper_argv); -revert_creds (old_cred);put_cred (cred);coredump_finish (mm);
PATCH 3/4 coredump: factor out put_cred() calls
In reply to Oleg NesterovGiven that do_coredump() calls put_cred() on exit path, it is a bit ugly to do put_cred() + "goto fail" twice, just add the new "fail_creds" label.
---
fs/exec.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-)
fs/exec.c renamed from fs/exec.c~3_CREDS +6 -10
@@ -1859,10 +1859,8 @@ void do_coredump(long signr, int exit_co goto fail; cred =prepare_creds (); -if (!cred) { - retval = -ENOMEM ; +if (!cred) goto fail; - }down_write (&mm->mmap_sem);/*
@@ -1870,8 +1868,7 @@ void do_coredump(long signr, int exit_co*/ if (mm->core_state || !__get_dumpable (cprm.mm_flags)) {up_write (&mm->mmap_sem); -put_cred (cred); - goto fail; + goto fail_creds; }/*
@@ -1886,10 +1883,8 @@ void do_coredump(long signr, int exit_co } retval =coredump_wait (exit_code, &core_state); -if (retval <0 ) { -put_cred (cred); - goto fail; - } +if (retval <0 ) + goto fail_creds; old_cred =override_creds (cred);
@@ -2006,9 +2001,10 @@ fail_dropcount:if (ispipe)atomic_dec (&core_dump_count); fail_unlock: +coredump_finish (mm);revert_creds (old_cred); +fail_creds:put_cred (cred); -coredump_finish (mm); fail:return ; }
PATCH 4/4 coredump: shift down_write(mmap_sem) into coredump_wait()
In reply to Oleg Nesterov- move the cprm.mm_flags checks up, before we take mmap_sem
- move down_write(mmap_sem) and ->core_state check from do_coredump() to coredump_wait()
This simplifies the code and makes the locking symmetrical.
---
fs/exec.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-)
fs/exec.c renamed from fs/exec.c~4_MMAP_SEM +7 -12
@@ -1659,12 +1659,15 @@ static int coredump_wait(int exit_code,struct task_struct *tsk = current;struct mm_struct *mm = tsk->mm;struct completion *vfork_done; -int core_waiters; +int core_waiters = -EBUSY ;init_completion (&core_state->startup); core_state->dumper.task = tsk; core_state->dumper.next =NULL ; - core_waiters =zap_threads (tsk, mm, core_state, exit_code); + +down_write (&mm->mmap_sem); +if (!mm->core_state) + core_waiters =zap_threads (tsk, mm, core_state, exit_code);up_write (&mm->mmap_sem);if (unlikely (core_waiters <0 ))
@@ -1857,20 +1860,12 @@ void do_coredump(long signr, int exit_co binfmt = mm->binfmt;if (!binfmt || !binfmt->core_dump) goto fail; +if (!__get_dumpable (cprm.mm_flags)) + goto fail; cred =prepare_creds ();if (!cred) goto fail; - -down_write (&mm->mmap_sem); -/* -* If another thread got here first, or we are not dumpable, bail out. -*/ -if (mm->core_state || !__get_dumpable (cprm.mm_flags)) { -up_write (&mm->mmap_sem); - goto fail_creds; - } -/* * We cannot trust fsuid as being the "true" uid of the * process nor do we know its entire history. We only know it