From: Jeff Layton <jlayton@kernel.org>
To: viro@zeniv.linux.org.uk
Cc: ebiederm@xmission.com, berrange@redhat.com,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v3 2/3] exec: delay clone(CLONE_FILES) if task associated with current files_struct is exec'ing
Date: Fri, 14 Sep 2018 06:53:09 -0400 [thread overview]
Message-ID: <20180914105310.6454-3-jlayton@kernel.org> (raw)
In-Reply-To: <20180914105310.6454-1-jlayton@kernel.org>
In a later patch, we're going to move the unshare_files call in
__do_execve_file to later in the process, but this opens up a potential
race with clone(CLONE_FILES). We could end up bumping the refcount on
the files_struct after we've checked to see if it was shared. What we
really want to do in that case is have the clone return -EAGAIN, much
like the handling of the similar situation in copy_fs.
Add a new in_exec boolean to files_struct that is protected by the
file_lock. Set it if the files_struct turns out not to be shared, and
clear at the end of __do_execve_file. If it's set when we go to
copy_process, then just return -EAGAIN.
Cc: Eric W. Biederman <ebiederm@xmission.com>
Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
fs/exec.c | 14 ++++++++++++--
fs/file.c | 1 +
include/linux/fdtable.h | 1 +
kernel/fork.c | 21 ++++++++++++++-------
4 files changed, 28 insertions(+), 9 deletions(-)
diff --git a/fs/exec.c b/fs/exec.c
index e71a70d70158..c8a68481d7eb 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1831,8 +1831,13 @@ static int __do_execve_file(int fd, struct filename *filename,
kfree(pathbuf);
if (filename)
putname(filename);
- if (displaced)
+ if (displaced) {
release_files_struct(displaced);
+ } else {
+ spin_lock(¤t->files->file_lock);
+ current->files->in_exec = false;
+ spin_unlock(¤t->files->file_lock);
+ }
return retval;
out:
@@ -1850,8 +1855,13 @@ static int __do_execve_file(int fd, struct filename *filename,
kfree(pathbuf);
out_files:
- if (displaced)
+ if (displaced) {
reset_files_struct(displaced);
+ } else {
+ spin_lock(¤t->files->file_lock);
+ current->files->in_exec = false;
+ spin_unlock(¤t->files->file_lock);
+ }
out_ret:
if (filename)
putname(filename);
diff --git a/fs/file.c b/fs/file.c
index e35eeff4593c..535321846425 100644
--- a/fs/file.c
+++ b/fs/file.c
@@ -286,6 +286,7 @@ struct files_struct *dup_fd(struct files_struct *oldf, int *errorp)
spin_lock_init(&newf->file_lock);
newf->thread_count = 1;
newf->resize_in_progress = false;
+ newf->in_exec = false;
init_waitqueue_head(&newf->resize_wait);
newf->next_fd = 0;
new_fdt = &newf->fdtab;
diff --git a/include/linux/fdtable.h b/include/linux/fdtable.h
index 2cb02f438201..a09ba1b20693 100644
--- a/include/linux/fdtable.h
+++ b/include/linux/fdtable.h
@@ -51,6 +51,7 @@ struct files_struct {
*/
atomic_t count;
bool resize_in_progress;
+ bool in_exec;
wait_queue_head_t resize_wait;
struct fdtable __rcu *fdt;
diff --git a/kernel/fork.c b/kernel/fork.c
index c7eb63e4d5c1..a3786efb39f1 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -1372,10 +1372,15 @@ static int copy_files(unsigned long clone_flags, struct task_struct *tsk)
goto out;
if (clone_flags & CLONE_FILES) {
- atomic_inc(&oldf->count);
spin_lock(&oldf->file_lock);
- oldf->thread_count++;
- spin_unlock(&oldf->file_lock);
+ if (oldf->in_exec) {
+ spin_unlock(&oldf->file_lock);
+ error = -EAGAIN;
+ } else {
+ oldf->thread_count++;
+ spin_unlock(&oldf->file_lock);
+ atomic_inc(&oldf->count);
+ }
goto out;
}
@@ -2419,18 +2424,20 @@ static int unshare_fs(unsigned long unshare_flags, struct fs_struct **new_fsp)
static int unshare_fd(unsigned long unshare_flags, struct files_struct **new_fdp)
{
struct files_struct *fd = current->files;
- int error, count;
+ int error;
if (!(unshare_flags & CLONE_FILES) || !fd)
return 0;
spin_lock(&fd->file_lock);
- count = fd->thread_count;
- spin_unlock(&fd->file_lock);
- if (count > 1) {
+ if (fd->thread_count > 1) {
+ spin_unlock(&fd->file_lock);
*new_fdp = dup_fd(fd, &error);
if (!*new_fdp)
return error;
+ } else {
+ fd->in_exec = true;
+ spin_unlock(&fd->file_lock);
}
return 0;
--
2.17.1
next prev parent reply other threads:[~2018-09-14 16:07 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-09-14 10:53 [PATCH v3 0/3] exec: fix passing of file locks across execve in multithreaded processes Jeff Layton
2018-09-14 10:53 ` [PATCH v3 1/3] exec: separate thread_count for files_struct Jeff Layton
2018-09-15 16:04 ` Oleg Nesterov
2018-09-16 16:10 ` Eric W. Biederman
2018-09-17 15:24 ` Oleg Nesterov
2018-09-17 20:45 ` Eric W. Biederman
2018-09-14 10:53 ` Jeff Layton [this message]
2018-09-14 10:53 ` [PATCH v3 3/3] exec: do unshare_files after de_thread Jeff Layton
2018-09-15 16:37 ` Oleg Nesterov
2018-09-16 16:49 ` Eric W. Biederman
2018-09-17 15:28 ` Oleg Nesterov
2018-09-16 16:59 ` Eric W. Biederman
2018-09-16 17:38 ` [RFC][PATCH 0/3] exec: Moving unshare_files_struct Eric W. Biederman
2018-09-16 17:39 ` [RFC][PATCH 1/3] exec: Move unshare_files down to avoid locks being dropped on exec Eric W. Biederman
2018-09-17 15:49 ` Oleg Nesterov
2018-09-16 17:40 ` [RFC][PATCH 2/3] exec: Simplify unshare_files Eric W. Biederman
2018-09-17 16:23 ` Oleg Nesterov
2018-09-17 20:26 ` Eric W. Biederman
2018-09-16 17:41 ` [RFC][PATCH 3/3] exec: Remove reset_files_struct Eric W. Biederman
2018-09-17 15:59 ` [RFC][PATCH 0/3] exec: Moving unshare_files_struct Oleg Nesterov
2018-09-18 22:18 ` Eric W. Biederman
2018-09-17 16:24 ` Jeff Layton
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20180914105310.6454-3-jlayton@kernel.org \
--to=jlayton@kernel.org \
--cc=berrange@redhat.com \
--cc=ebiederm@xmission.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.