]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
exec: Simplify unshare_files
authorEric W. Biederman <ebiederm@xmission.com>
Fri, 20 Nov 2020 23:14:19 +0000 (17:14 -0600)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 21 Jun 2024 12:52:47 +0000 (14:52 +0200)
[ Upstream commit 1f702603e7125a390b5cdf5ce00539781cfcc86a ]

Now that exec no longer needs to return the unshared files to their
previous value there is no reason to return displaced.

Instead when unshare_fd creates a copy of the file table, call
put_files_struct before returning from unshare_files.

Acked-by: Christian Brauner <christian.brauner@ubuntu.com>
v1: https://lkml.kernel.org/r/20200817220425.9389-2-ebiederm@xmission.com
Link: https://lkml.kernel.org/r/20201120231441.29911-2-ebiederm@xmission.com
Signed-off-by: Eric W. Biederman <ebiederm@xmission.com>
Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
fs/coredump.c
fs/exec.c
include/linux/fdtable.h
kernel/fork.c

index 9d91e831ed0b2df24bf4b31763384d5d19b50fab..7b085975ea1638437cb15f443d43eaf78fb857f2 100644 (file)
@@ -590,7 +590,6 @@ void do_coredump(const kernel_siginfo_t *siginfo)
        int ispipe;
        size_t *argv = NULL;
        int argc = 0;
-       struct files_struct *displaced;
        /* require nonrelative corefile path and be extra careful */
        bool need_suid_safe = false;
        bool core_dumped = false;
@@ -797,11 +796,9 @@ void do_coredump(const kernel_siginfo_t *siginfo)
        }
 
        /* get us an unshared descriptor table; almost always a no-op */
-       retval = unshare_files(&displaced);
+       retval = unshare_files();
        if (retval)
                goto close_fail;
-       if (displaced)
-               put_files_struct(displaced);
        if (!dump_interrupted()) {
                /*
                 * umh disabled with CONFIG_STATIC_USERMODEHELPER_PATH="" would
index 42952cf90f4aff7b1ce26712e617f7f742389142..d5c8f085235bcd6cf6f63883c528e49f23890ee8 100644 (file)
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1245,7 +1245,6 @@ void __set_task_comm(struct task_struct *tsk, const char *buf, bool exec)
 int begin_new_exec(struct linux_binprm * bprm)
 {
        struct task_struct *me = current;
-       struct files_struct *displaced;
        int retval;
 
        /* Once we are committed compute the creds */
@@ -1266,11 +1265,9 @@ int begin_new_exec(struct linux_binprm * bprm)
                goto out;
 
        /* Ensure the files table is not shared. */
-       retval = unshare_files(&displaced);
+       retval = unshare_files();
        if (retval)
                goto out;
-       if (displaced)
-               put_files_struct(displaced);
 
        /*
         * Must be called _before_ exec_mmap() as bprm->mm is
index f1a99d3e5570712f4a48b3984e9b178208a460de..b32ab2163dc2d73d62277b1915d972ca7f0eb76c 100644 (file)
@@ -109,7 +109,7 @@ struct task_struct;
 struct files_struct *get_files_struct(struct task_struct *);
 void put_files_struct(struct files_struct *fs);
 void reset_files_struct(struct files_struct *);
-int unshare_files(struct files_struct **);
+int unshare_files(void);
 struct files_struct *dup_fd(struct files_struct *, unsigned, int *) __latent_entropy;
 void do_close_on_exec(struct files_struct *);
 int iterate_fd(struct files_struct *, unsigned,
index 633b0af1d1a73e61257e65b78984dcc24cbaa6fa..8b8a5a172b1586099cb05ed08ce79c04f8a884f2 100644 (file)
@@ -3077,21 +3077,21 @@ SYSCALL_DEFINE1(unshare, unsigned long, unshare_flags)
  *     the exec layer of the kernel.
  */
 
-int unshare_files(struct files_struct **displaced)
+int unshare_files(void)
 {
        struct task_struct *task = current;
-       struct files_struct *copy = NULL;
+       struct files_struct *old, *copy = NULL;
        int error;
 
        error = unshare_fd(CLONE_FILES, NR_OPEN_MAX, &copy);
-       if (error || !copy) {
-               *displaced = NULL;
+       if (error || !copy)
                return error;
-       }
-       *displaced = task->files;
+
+       old = task->files;
        task_lock(task);
        task->files = copy;
        task_unlock(task);
+       put_files_struct(old);
        return 0;
 }