Linux 6.9-rc4
[sfrench/cifs-2.6.git] / kernel / umh.c
index 7f255b5a8845aef24b048dfabaf07462b04c9fca..1b13c5d34624878edf4a4807d0c8b857d41b97fb 100644 (file)
@@ -14,6 +14,7 @@
 #include <linux/cred.h>
 #include <linux/file.h>
 #include <linux/fdtable.h>
+#include <linux/fs_struct.h>
 #include <linux/workqueue.h>
 #include <linux/security.h>
 #include <linux/mount.h>
 #include <linux/ptrace.h>
 #include <linux/async.h>
 #include <linux/uaccess.h>
-#include <linux/shmem_fs.h>
-#include <linux/pipe_fs_i.h>
+#include <linux/initrd.h>
+#include <linux/freezer.h>
 
 #include <trace/events/module.h>
 
-#define CAP_BSET       (void *)1
-#define CAP_PI         (void *)2
-
 static kernel_cap_t usermodehelper_bset = CAP_FULL_SET;
 static kernel_cap_t usermodehelper_inheritable = CAP_FULL_SET;
 static DEFINE_SPINLOCK(umh_sysctl_lock);
 static DECLARE_RWSEM(umhelper_sem);
-static LIST_HEAD(umh_list);
-static DEFINE_MUTEX(umh_list_lock);
 
 static void call_usermodehelper_freeinfo(struct subprocess_info *info)
 {
@@ -75,6 +71,14 @@ static int call_usermodehelper_exec_async(void *data)
        flush_signal_handlers(current, 1);
        spin_unlock_irq(&current->sighand->siglock);
 
+       /*
+        * Initial kernel threads share ther FS with init, in order to
+        * get the init root directory. But we've now created a new
+        * thread that is going to execve a user process and has its own
+        * 'struct fs_struct'. Reset umask to the default.
+        */
+       current->fs->umask = 0022;
+
        /*
         * Our parent (unbound workqueue) runs with elevated scheduling
         * priority. Avoid propagating that into the userspace child.
@@ -102,16 +106,10 @@ static int call_usermodehelper_exec_async(void *data)
 
        commit_creds(new);
 
-       sub_info->pid = task_pid_nr(current);
-       if (sub_info->file) {
-               retval = do_execve_file(sub_info->file,
-                                       sub_info->argv, sub_info->envp);
-               if (!retval)
-                       current->flags |= PF_UMH;
-       } else
-               retval = do_execve(getname_kernel(sub_info->path),
-                                  (const char __user *const __user *)sub_info->argv,
-                                  (const char __user *const __user *)sub_info->envp);
+       wait_for_initramfs();
+       retval = kernel_execve(sub_info->path,
+                              (const char *const *)sub_info->argv,
+                              (const char *const *)sub_info->envp);
 out:
        sub_info->retval = retval;
        /*
@@ -130,37 +128,16 @@ static void call_usermodehelper_exec_sync(struct subprocess_info *sub_info)
 {
        pid_t pid;
 
-       /* If SIGCLD is ignored kernel_wait4 won't populate the status. */
+       /* If SIGCLD is ignored do_wait won't populate the status. */
        kernel_sigaction(SIGCHLD, SIG_DFL);
-       pid = kernel_thread(call_usermodehelper_exec_async, sub_info, SIGCHLD);
-       if (pid < 0) {
+       pid = user_mode_thread(call_usermodehelper_exec_async, sub_info, SIGCHLD);
+       if (pid < 0)
                sub_info->retval = pid;
-       } else {
-               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.
-                * But call_usermodehelper_exec_sync() always runs as kernel
-                * thread (workqueue) and put_user() to a kernel address works
-                * OK for kernel threads, due to their having an mm_segment_t
-                * which spans the entire address space.
-                *
-                * Thus the __user pointer cast is valid here.
-                */
-               kernel_wait4(pid, (int __user *)&ret, 0, NULL);
-
-               /*
-                * If ret is 0, either call_usermodehelper_exec_async failed and
-                * the real error code is already in sub_info->retval or
-                * sub_info->retval is 0 anyway, so don't mess with it then.
-                */
-               if (ret)
-                       sub_info->retval = ret;
-       }
+       else
+               kernel_wait(pid, &sub_info->retval);
 
        /* Restore default kernel sig handler */
        kernel_sigaction(SIGCHLD, SIG_IGN);
-
        umh_complete(sub_info);
 }
 
@@ -192,8 +169,8 @@ static void call_usermodehelper_exec_work(struct work_struct *work)
                 * want to pollute current->children, and we need a parent
                 * that always ignores SIGCHLD to ensure auto-reaping.
                 */
-               pid = kernel_thread(call_usermodehelper_exec_async, sub_info,
-                                   CLONE_PARENT | SIGCHLD);
+               pid = user_mode_thread(call_usermodehelper_exec_async, sub_info,
+                                      CLONE_PARENT | SIGCHLD);
                if (pid < 0) {
                        sub_info->retval = pid;
                        umh_complete(sub_info);
@@ -359,8 +336,8 @@ static void helper_unlock(void)
  * @argv: arg vector for process
  * @envp: environment for process
  * @gfp_mask: gfp mask for memory allocation
- * @cleanup: a cleanup function
  * @init: an init function
+ * @cleanup: a cleanup function
  * @data: arbitrary context sensitive data
  *
  * Returns either %NULL on allocation failure, or a subprocess_info
@@ -371,7 +348,7 @@ static void helper_unlock(void)
  * exec.  A non-zero return code causes the process to error out, exit,
  * and return the failure to the calling process
  *
- * The cleanup function is just before ethe subprocess_info is about to
+ * The cleanup function is just before the subprocess_info is about to
  * be freed.  This can be used for freeing the argv and envp.  The
  * Function must be runnable in either a process context or the
  * context in which call_usermodehelper_exec is called.
@@ -405,137 +382,9 @@ struct subprocess_info *call_usermodehelper_setup(const char *path, char **argv,
 }
 EXPORT_SYMBOL(call_usermodehelper_setup);
 
-struct subprocess_info *call_usermodehelper_setup_file(struct file *file,
-               int (*init)(struct subprocess_info *info, struct cred *new),
-               void (*cleanup)(struct subprocess_info *info), void *data)
-{
-       struct subprocess_info *sub_info;
-       struct umh_info *info = data;
-       const char *cmdline = (info->cmdline) ? info->cmdline : "usermodehelper";
-
-       sub_info = kzalloc(sizeof(struct subprocess_info), GFP_KERNEL);
-       if (!sub_info)
-               return NULL;
-
-       sub_info->argv = argv_split(GFP_KERNEL, cmdline, NULL);
-       if (!sub_info->argv) {
-               kfree(sub_info);
-               return NULL;
-       }
-
-       INIT_WORK(&sub_info->work, call_usermodehelper_exec_work);
-       sub_info->path = "none";
-       sub_info->file = file;
-       sub_info->init = init;
-       sub_info->cleanup = cleanup;
-       sub_info->data = data;
-       return sub_info;
-}
-
-static int umh_pipe_setup(struct subprocess_info *info, struct cred *new)
-{
-       struct umh_info *umh_info = info->data;
-       struct file *from_umh[2];
-       struct file *to_umh[2];
-       int err;
-
-       /* create pipe to send data to umh */
-       err = create_pipe_files(to_umh, 0);
-       if (err)
-               return err;
-       err = replace_fd(0, to_umh[0], 0);
-       fput(to_umh[0]);
-       if (err < 0) {
-               fput(to_umh[1]);
-               return err;
-       }
-
-       /* create pipe to receive data from umh */
-       err = create_pipe_files(from_umh, 0);
-       if (err) {
-               fput(to_umh[1]);
-               replace_fd(0, NULL, 0);
-               return err;
-       }
-       err = replace_fd(1, from_umh[1], 0);
-       fput(from_umh[1]);
-       if (err < 0) {
-               fput(to_umh[1]);
-               replace_fd(0, NULL, 0);
-               fput(from_umh[0]);
-               return err;
-       }
-
-       umh_info->pipe_to_umh = to_umh[1];
-       umh_info->pipe_from_umh = from_umh[0];
-       return 0;
-}
-
-static void umh_clean_and_save_pid(struct subprocess_info *info)
-{
-       struct umh_info *umh_info = info->data;
-
-       argv_free(info->argv);
-       umh_info->pid = info->pid;
-}
-
-/**
- * fork_usermode_blob - fork a blob of bytes as a usermode process
- * @data: a blob of bytes that can be do_execv-ed as a file
- * @len: length of the blob
- * @info: information about usermode process (shouldn't be NULL)
- *
- * If info->cmdline is set it will be used as command line for the
- * user process, else "usermodehelper" is used.
- *
- * Returns either negative error or zero which indicates success
- * in executing a blob of bytes as a usermode process. In such
- * case 'struct umh_info *info' is populated with two pipes
- * and a pid of the process. The caller is responsible for health
- * check of the user process, killing it via pid, and closing the
- * pipes when user process is no longer needed.
- */
-int fork_usermode_blob(void *data, size_t len, struct umh_info *info)
-{
-       struct subprocess_info *sub_info;
-       struct file *file;
-       ssize_t written;
-       loff_t pos = 0;
-       int err;
-
-       file = shmem_kernel_file_setup("", len, 0);
-       if (IS_ERR(file))
-               return PTR_ERR(file);
-
-       written = kernel_write(file, data, len, &pos);
-       if (written != len) {
-               err = written;
-               if (err >= 0)
-                       err = -ENOMEM;
-               goto out;
-       }
-
-       err = -ENOMEM;
-       sub_info = call_usermodehelper_setup_file(file, umh_pipe_setup,
-                                                 umh_clean_and_save_pid, info);
-       if (!sub_info)
-               goto out;
-
-       err = call_usermodehelper_exec(sub_info, UMH_WAIT_EXEC);
-       if (!err) {
-               mutex_lock(&umh_list_lock);
-               list_add(&info->list, &umh_list);
-               mutex_unlock(&umh_list_lock);
-       }
-out:
-       fput(file);
-       return err;
-}
-EXPORT_SYMBOL_GPL(fork_usermode_blob);
-
 /**
  * call_usermodehelper_exec - start a usermode application
- * @sub_info: information about the subprocessa
+ * @sub_info: information about the subprocess
  * @wait: wait for the application to finish and return status.
  *        when UMH_NO_WAIT don't wait at all, but you get no useful error back
  *        when the program couldn't be exec'ed. This makes it safe to call
@@ -544,9 +393,15 @@ EXPORT_SYMBOL_GPL(fork_usermode_blob);
  * Runs a user-space application.  The application is started
  * asynchronously if wait is not set, and runs as a child of system workqueues.
  * (ie. it runs with full root capabilities and optimized affinity).
+ *
+ * Note: successful return value does not guarantee the helper was called at
+ * all. You can't rely on sub_info->{init,cleanup} being called even for
+ * UMH_WAIT_* wait modes as STATIC_USERMODEHELPER_PATH="" turns all helpers
+ * into a successful no-op.
  */
 int call_usermodehelper_exec(struct subprocess_info *sub_info, int wait)
 {
+       unsigned int state = TASK_UNINTERRUPTIBLE;
        DECLARE_COMPLETION_ONSTACK(done);
        int retval = 0;
 
@@ -580,18 +435,28 @@ int call_usermodehelper_exec(struct subprocess_info *sub_info, int wait)
        if (wait == UMH_NO_WAIT)        /* task has freed sub_info */
                goto unlock;
 
+       if (wait & UMH_FREEZABLE)
+               state |= TASK_FREEZABLE;
+
        if (wait & UMH_KILLABLE) {
-               retval = wait_for_completion_killable(&done);
+               retval = wait_for_completion_state(&done, state | TASK_KILLABLE);
                if (!retval)
                        goto wait_done;
 
                /* umh_complete() will see NULL and free sub_info */
                if (xchg(&sub_info->complete, NULL))
                        goto unlock;
-               /* fallthrough, umh_complete() was already called */
+
+               /*
+                * fallthrough; in case of -ERESTARTSYS now do uninterruptible
+                * wait_for_completion_state(). Since umh_complete() shall call
+                * complete() in a moment if xchg() above returned NULL, this
+                * uninterruptible wait_for_completion_state() will not block
+                * SIGKILL'ed processes for long.
+                */
        }
+       wait_for_completion_state(&done, state);
 
-       wait_for_completion(&done);
 wait_done:
        retval = sub_info->retval;
 out:
@@ -629,13 +494,14 @@ int call_usermodehelper(const char *path, char **argv, char **envp, int wait)
 }
 EXPORT_SYMBOL(call_usermodehelper);
 
+#if defined(CONFIG_SYSCTL)
 static int proc_cap_handler(struct ctl_table *table, int write,
-                        void __user *buffer, size_t *lenp, loff_t *ppos)
+                        void *buffer, size_t *lenp, loff_t *ppos)
 {
        struct ctl_table t;
-       unsigned long cap_array[_KERNEL_CAPABILITY_U32S];
-       kernel_cap_t new_cap;
-       int err, i;
+       unsigned long cap_array[2];
+       kernel_cap_t new_cap, *cap;
+       int err;
 
        if (write && (!capable(CAP_SETPCAP) ||
                      !capable(CAP_SYS_MODULE)))
@@ -644,16 +510,13 @@ static int proc_cap_handler(struct ctl_table *table, int write,
        /*
         * convert from the global kernel_cap_t to the ulong array to print to
         * userspace if this is a read.
+        *
+        * Legacy format: capabilities are exposed as two 32-bit values
         */
+       cap = table->data;
        spin_lock(&umh_sysctl_lock);
-       for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++)  {
-               if (table->data == CAP_BSET)
-                       cap_array[i] = usermodehelper_bset.cap[i];
-               else if (table->data == CAP_PI)
-                       cap_array[i] = usermodehelper_inheritable.cap[i];
-               else
-                       BUG();
-       }
+       cap_array[0] = (u32) cap->val;
+       cap_array[1] = cap->val >> 32;
        spin_unlock(&umh_sysctl_lock);
 
        t = *table;
@@ -667,62 +530,43 @@ static int proc_cap_handler(struct ctl_table *table, int write,
        if (err < 0)
                return err;
 
-       /*
-        * convert from the sysctl array of ulongs to the kernel_cap_t
-        * internal representation
-        */
-       for (i = 0; i < _KERNEL_CAPABILITY_U32S; i++)
-               new_cap.cap[i] = cap_array[i];
+       new_cap.val = (u32)cap_array[0];
+       new_cap.val += (u64)cap_array[1] << 32;
 
        /*
         * Drop everything not in the new_cap (but don't add things)
         */
        if (write) {
                spin_lock(&umh_sysctl_lock);
-               if (table->data == CAP_BSET)
-                       usermodehelper_bset = cap_intersect(usermodehelper_bset, new_cap);
-               if (table->data == CAP_PI)
-                       usermodehelper_inheritable = cap_intersect(usermodehelper_inheritable, new_cap);
+               *cap = cap_intersect(*cap, new_cap);
                spin_unlock(&umh_sysctl_lock);
        }
 
        return 0;
 }
 
-void __exit_umh(struct task_struct *tsk)
-{
-       struct umh_info *info;
-       pid_t pid = tsk->pid;
-
-       mutex_lock(&umh_list_lock);
-       list_for_each_entry(info, &umh_list, list) {
-               if (info->pid == pid) {
-                       list_del(&info->list);
-                       mutex_unlock(&umh_list_lock);
-                       goto out;
-               }
-       }
-       mutex_unlock(&umh_list_lock);
-       return;
-out:
-       if (info->cleanup)
-               info->cleanup(info);
-}
-
-struct ctl_table usermodehelper_table[] = {
+static struct ctl_table usermodehelper_table[] = {
        {
                .procname       = "bset",
-               .data           = CAP_BSET,
-               .maxlen         = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
+               .data           = &usermodehelper_bset,
+               .maxlen         = 2 * sizeof(unsigned long),
                .mode           = 0600,
                .proc_handler   = proc_cap_handler,
        },
        {
                .procname       = "inheritable",
-               .data           = CAP_PI,
-               .maxlen         = _KERNEL_CAPABILITY_U32S * sizeof(unsigned long),
+               .data           = &usermodehelper_inheritable,
+               .maxlen         = 2 * sizeof(unsigned long),
                .mode           = 0600,
                .proc_handler   = proc_cap_handler,
        },
        { }
 };
+
+static int __init init_umh_sysctls(void)
+{
+       register_sysctl_init("kernel/usermodehelper", usermodehelper_table);
+       return 0;
+}
+early_initcall(init_umh_sysctls);
+#endif /* CONFIG_SYSCTL */