From: David Hildenbrand <david@redhat.com>
To: linux-kernel@vger.kernel.org
Cc: David Hildenbrand <david@redhat.com>,
Andrew Morton <akpm@linux-foundation.org>,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, Borislav Petkov <bp@alien8.de>,
"H. Peter Anvin" <hpa@zytor.com>,
Alexander Viro <viro@zeniv.linux.org.uk>,
Alexey Dobriyan <adobriyan@gmail.com>,
Steven Rostedt <rostedt@goodmis.org>,
Peter Zijlstra <peterz@infradead.org>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Mark Rutland <mark.rutland@arm.com>,
Alexander Shishkin <alexander.shishkin@linux.intel.com>,
Jiri Olsa <jolsa@redhat.com>, Namhyung Kim <namhyung@kernel.org>,
Petr Mladek <pmladek@suse.com>,
Sergey Senozhatsky <sergey.senozhatsky@gmail.com>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
Kees Cook <keescook@chromium.org>,
"Eric W. Biederman" <ebiederm@xmission.com>,
Greg Ungerer <gerg@linux-m68k.org>,
Geert Uytterhoeven <geert@linux-m68k.org>,
Mike Rapoport <rppt@kernel.org>, Vlastimil Babka <vbabka@suse.cz>,
Vincenzo Frascino <vincenzo.frascino@arm.com>,
Chinwen Chang <chinwen.chang@mediatek.com>,
Michel Lespinasse <walken@google.com>,
Catalin Marinas <catalin.marinas@arm.com>,
"Matthew Wilcox (Oracle)" <willy@infradead.org>,
Huang Ying <ying.huang@intel.com>, Jann Horn <jannh@google.com>,
Feng Tang <feng.tang@intel.com>,
Kevin Brodsky <Kevin.Brodsky@arm.com>,
Michael Ellerman <mpe@ellerman.id.au>,
Shawn Anastasio <shawn@anastas.io>,
Steven Price <steven.price@arm.com>,
Nicholas Piggin <npiggin@gmail.com>,
Christian Brauner <christian.brauner@ubuntu.com>,
Jens Axboe <axboe@kernel.dk>,
Gabriel Krisman Bertazi <krisman@collabora.com>,
Peter Xu <peterx@redhat.com>,
Suren Baghdasaryan <surenb@google.com>,
Shakeel Butt <shakeelb@google.com>,
Marco Elver <elver@google.com>,
Daniel Jordan <daniel.m.jordan@oracle.com>,
Nicolas Viennot <Nicolas.Viennot@twosigma.com>,
Thomas Cedeno <thomascedeno@google.com>,
Collin Fijalkovich <cfijalkovich@google.com>,
Michal Hocko <mhocko@suse.com>,
linux-api@vger.kernel.org, x86@kernel.org,
linux-fsdevel@vger.kernel.org, linux-mm@kvack.org
Subject: [PATCH RFC 2/7] kernel/fork: factor out atomcially replacing the current MM exe_file
Date: Fri, 23 Apr 2021 15:16:35 +0200 [thread overview]
Message-ID: <20210423131640.20080-3-david@redhat.com> (raw)
In-Reply-To: <20210423131640.20080-1-david@redhat.com>
Let's factor the main logic out into atomic_set_mm_exe_file(), such that
all mm->exe_file logic is contained in kernel/fork.c.
While at it, perform some simple cleanups that are possible now that
we're simplifying the individual functions.
Signed-off-by: David Hildenbrand <david@redhat.com>
---
include/linux/mm.h | 2 ++
kernel/fork.c | 35 +++++++++++++++++++++++++++++++++--
kernel/sys.c | 33 +--------------------------------
3 files changed, 36 insertions(+), 34 deletions(-)
diff --git a/include/linux/mm.h b/include/linux/mm.h
index 8ba434287387..043702972e5f 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -2540,6 +2540,8 @@ extern int mm_take_all_locks(struct mm_struct *mm);
extern void mm_drop_all_locks(struct mm_struct *mm);
extern void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file);
+extern int atomic_set_mm_exe_file(struct mm_struct *mm,
+ struct file *new_exe_file);
extern struct file *get_mm_exe_file(struct mm_struct *mm);
extern struct file *get_task_exe_file(struct task_struct *task);
diff --git a/kernel/fork.c b/kernel/fork.c
index 426cd0c51f9e..199463625adc 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -1138,8 +1138,8 @@ void mmput_async(struct mm_struct *mm)
* Main users are mmput() and sys_execve(). Callers prevent concurrent
* invocations: in mmput() nobody alive left, in execve task is single
* threaded. sys_prctl(PR_SET_MM_MAP/EXE_FILE) also needs to set the
- * mm->exe_file, but does so without using set_mm_exe_file() in order
- * to do avoid the need for any locks.
+ * mm->exe_file, but uses atomic_set_mm_exe_file(), avoiding the need
+ * for any locks.
*/
void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
{
@@ -1159,6 +1159,37 @@ void set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
fput(old_exe_file);
}
+int atomic_set_mm_exe_file(struct mm_struct *mm, struct file *new_exe_file)
+{
+ struct vm_area_struct *vma;
+ struct file *old_exe_file;
+ int ret = 0;
+
+ /* Forbid mm->exe_file change if old file still mapped. */
+ old_exe_file = get_mm_exe_file(mm);
+ if (old_exe_file) {
+ mmap_read_lock(mm);
+ for (vma = mm->mmap; vma && !ret; vma = vma->vm_next) {
+ if (!vma->vm_file)
+ continue;
+ if (path_equal(&vma->vm_file->f_path,
+ &old_exe_file->f_path))
+ ret = -EBUSY;
+ }
+ mmap_read_unlock(mm);
+ fput(old_exe_file);
+ if (ret)
+ return ret;
+ }
+
+ /* set the new file, lockless */
+ get_file(new_exe_file);
+ old_exe_file = xchg(&mm->exe_file, new_exe_file);
+ if (old_exe_file)
+ fput(old_exe_file);
+ return 0;
+}
+
/**
* get_mm_exe_file - acquire a reference to the mm's executable file
*
diff --git a/kernel/sys.c b/kernel/sys.c
index 2e2e3f378d97..7dcd9fb3153c 100644
--- a/kernel/sys.c
+++ b/kernel/sys.c
@@ -1828,7 +1828,6 @@ SYSCALL_DEFINE1(umask, int, mask)
static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
{
struct fd exe;
- struct file *old_exe, *exe_file;
struct inode *inode;
int err;
@@ -1851,40 +1850,10 @@ static int prctl_set_mm_exe_file(struct mm_struct *mm, unsigned int fd)
if (err)
goto exit;
- /*
- * Forbid mm->exe_file change if old file still mapped.
- */
- exe_file = get_mm_exe_file(mm);
- err = -EBUSY;
- if (exe_file) {
- struct vm_area_struct *vma;
-
- mmap_read_lock(mm);
- for (vma = mm->mmap; vma; vma = vma->vm_next) {
- if (!vma->vm_file)
- continue;
- if (path_equal(&vma->vm_file->f_path,
- &exe_file->f_path))
- goto exit_err;
- }
-
- mmap_read_unlock(mm);
- fput(exe_file);
- }
-
- err = 0;
- /* set the new file, lockless */
- get_file(exe.file);
- old_exe = xchg(&mm->exe_file, exe.file);
- if (old_exe)
- fput(old_exe);
+ err = atomic_set_mm_exe_file(mm, exe.file);
exit:
fdput(exe);
return err;
-exit_err:
- mmap_read_unlock(mm);
- fput(exe_file);
- goto exit;
}
/*
--
2.30.2
next prev parent reply other threads:[~2021-04-23 13:17 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-04-23 13:16 [PATCH RFC 0/7] Remove in-tree usage of MAP_DENYWRITE David Hildenbrand
2021-04-23 13:16 ` [PATCH RFC 1/7] binfmt: don't use MAP_DENYWRITE when loading shared libraries via uselib() David Hildenbrand
2021-04-23 13:16 ` David Hildenbrand [this message]
2021-04-23 13:16 ` [PATCH RFC 3/7] kernel/fork: always deny write access to current MM exe_file David Hildenbrand
2021-04-23 13:16 ` [PATCH RFC 4/7] binfmt: remove in-tree usage of MAP_DENYWRITE David Hildenbrand
2021-04-23 13:16 ` [PATCH RFC 5/7] mm: remove VM_DENYWRITE David Hildenbrand
2021-04-23 13:16 ` [PATCH RFC 6/7] mm: ignore MAP_DENYWRITE in ksys_mmap_pgoff() David Hildenbrand
2021-04-23 13:16 ` [PATCH RFC 7/7] fs: update documentation of get_write_access() and friends David Hildenbrand
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=20210423131640.20080-3-david@redhat.com \
--to=david@redhat.com \
--cc=Kevin.Brodsky@arm.com \
--cc=Nicolas.Viennot@twosigma.com \
--cc=acme@kernel.org \
--cc=adobriyan@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=alexander.shishkin@linux.intel.com \
--cc=andriy.shevchenko@linux.intel.com \
--cc=axboe@kernel.dk \
--cc=bp@alien8.de \
--cc=catalin.marinas@arm.com \
--cc=cfijalkovich@google.com \
--cc=chinwen.chang@mediatek.com \
--cc=christian.brauner@ubuntu.com \
--cc=daniel.m.jordan@oracle.com \
--cc=ebiederm@xmission.com \
--cc=elver@google.com \
--cc=feng.tang@intel.com \
--cc=geert@linux-m68k.org \
--cc=gerg@linux-m68k.org \
--cc=hpa@zytor.com \
--cc=jannh@google.com \
--cc=jolsa@redhat.com \
--cc=keescook@chromium.org \
--cc=krisman@collabora.com \
--cc=linux-api@vger.kernel.org \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux@rasmusvillemoes.dk \
--cc=mark.rutland@arm.com \
--cc=mhocko@suse.com \
--cc=mingo@redhat.com \
--cc=mpe@ellerman.id.au \
--cc=namhyung@kernel.org \
--cc=npiggin@gmail.com \
--cc=peterx@redhat.com \
--cc=peterz@infradead.org \
--cc=pmladek@suse.com \
--cc=rostedt@goodmis.org \
--cc=rppt@kernel.org \
--cc=sergey.senozhatsky@gmail.com \
--cc=shakeelb@google.com \
--cc=shawn@anastas.io \
--cc=steven.price@arm.com \
--cc=surenb@google.com \
--cc=tglx@linutronix.de \
--cc=thomascedeno@google.com \
--cc=vbabka@suse.cz \
--cc=vincenzo.frascino@arm.com \
--cc=viro@zeniv.linux.org.uk \
--cc=walken@google.com \
--cc=willy@infradead.org \
--cc=x86@kernel.org \
--cc=ying.huang@intel.com \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).