Linux Security Modules development
 help / color / mirror / Atom feed
From: Christian Brauner <brauner@kernel.org>
To: Jann Horn <jannh@google.com>
Cc: Linus Torvalds <torvalds@linux-foundation.org>,
	 linux-fsdevel@vger.kernel.org,
	Arnaldo Carvalho de Melo <acme@kernel.org>,
	 linux-perf-users@vger.kernel.org, bpf@vger.kernel.org,
	 selinux@vger.kernel.org, Kees Cook <kees@kernel.org>,
	linux-mm@kvack.org,  linux-security-module@vger.kernel.org,
	apparmor@lists.ubuntu.com,  David Hildenbrand <david@kernel.org>,
	 "Christian Brauner (Amutable)" <brauner@kernel.org>
Subject: [PATCH RFC 13/19] exec: drop exec_update_lock after the credentials are committed
Date: Tue, 24 Nov 2026 23:58:36 +0100	[thread overview]
Message-ID: <20261124-work-exec-update_lock-v1-13-e489035395b8@kernel.org> (raw)
In-Reply-To: <20261124-work-exec-update_lock-v1-0-e489035395b8@kernel.org>

exec_update_lock makes sure that nobody who takes it can see the new mm
together with the old credentials. It is taken in exec_mmap() and
currently held until setup_new_exec().

When it was added in commit eea9673250db ("exec: Add exec_update_mutex
to replace cred_guard_mutex") it was dropped at the end of
install_exec_creds(), right after the new credentials were committed and
the LSMs had been told about it. Back then the personality and the mmap
layout were set up before the credentials were committed. They only
ended up between commit_creds() and the unlock when commit df9e4d2c4a53
("exec: Move most of setup_new_exec into flush_old_exec") moved the
credential commit into flush_old_exec() and left the unlock in
setup_new_exec().

Once the mm, the exec state and the credentials are switched and
perf_event_exit_task() has run every reader is checked against the new
credentials. But we still do a bunch of work with exec_update_lock held:

- security_bprm_committed_creds() which is a sleepable hook for bpf lsm
  programs

- installing the execfd which may have to grow the file descriptor table

- the binfmt setting the personality

- arch_pick_mmap_layout() and arch_setup_new_exec()

So drop it right after perf_event_exit_task(). The LSM hooks don't move
relative to commit_creds() and cred_guard_mutex is still held until
setup_new_exec() so nothing changes for ptrace.

While at it, fix the comments that still point to setup_new_exec(). It's
begin_new_exec() that commits the new credentials, changes whether the
task is dumpable and calls perf_event_exit_task().

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 fs/exec.c            | 39 ++++++++++++++++++++++++---------------
 kernel/events/core.c |  2 +-
 2 files changed, 25 insertions(+), 16 deletions(-)

diff --git a/fs/exec.c b/fs/exec.c
index 855437c352137..7b261b0a5f33c 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -837,8 +837,10 @@ EXPORT_SYMBOL(read_code);
 /*
  * Maps the mm_struct mm into the current task struct.
  * On success, this function returns with exec_update_lock
- * held for writing. The replaced address space is stashed in
- * bprm->old_mm for setup_new_exec() to release outside the lock.
+ * held for writing. begin_new_exec() drops it once the new
+ * credentials are committed. The replaced address space is
+ * stashed in bprm->old_mm for setup_new_exec() to release
+ * outside the exec locks.
  */
 static int exec_mmap(struct linux_binprm *bprm)
 {
@@ -1191,8 +1193,8 @@ int begin_new_exec(struct linux_binprm * bprm)
 
 	/*
 	 * We have to apply CLOEXEC before we change whether the process is
-	 * dumpable (in setup_new_exec) to avoid a race with a process in userspace
-	 * trying to access the should-be-closed file descriptors of a process
+	 * dumpable to avoid a race with a process in userspace trying to
+	 * access the should-be-closed file descriptors of a process
 	 * undergoing exec(2).
 	 *
 	 * This can block on filesystem ->flush() and ->release() handlers,
@@ -1232,7 +1234,7 @@ int begin_new_exec(struct linux_binprm * bprm)
 
 	retval = exec_task_namespaces();
 	if (retval)
-		goto out_unlock;
+		goto out_unlock_exec_update;
 
 	me->flags &= ~(PF_RANDOMIZE | PF_FORKNOEXEC |
 					PF_NOFREEZE | PF_NO_SETAFFINITY);
@@ -1303,7 +1305,7 @@ int begin_new_exec(struct linux_binprm * bprm)
 
 	retval = set_cred_ucounts(bprm->cred);
 	if (retval < 0)
-		goto out_unlock;
+		goto out_unlock_exec_update;
 
 	/*
 	 * install the new credentials for this executable
@@ -1321,6 +1323,10 @@ int begin_new_exec(struct linux_binprm * bprm)
 	 */
 	if (task_exec_state_get_dumpable(me) != TASK_DUMPABLE_OWNER)
 		perf_event_exit_task(me);
+
+	/* The mm, exec state and creds are switched. Let readers back in. */
+	up_write(&me->signal->exec_update_lock);
+
 	/*
 	 * cred_guard_mutex must be held at least to this point to prevent
 	 * ptrace_attach() from altering our determination of the task's
@@ -1339,17 +1345,20 @@ int begin_new_exec(struct linux_binprm * bprm)
 		if (retval < 0) {
 			/* The reference was not consumed. */
 			fput(executable);
-			goto out_unlock;
+			goto out_unlock_cred_guard;
 		}
 		bprm->execfd = retval;
 	}
 	return 0;
 
-out_unlock:
-	up_write(&me->signal->exec_update_lock);
-	if (!bprm->cred)
-		mutex_unlock(&me->signal->cred_guard_mutex);
+out_unlock_cred_guard:
+	/* free_bprm() only unlocks cred_guard_mutex if bprm->cred is set. */
+	mutex_unlock(&me->signal->cred_guard_mutex);
+	return retval;
 
+out_unlock_exec_update:
+	/* bprm->cred is still set so free_bprm() unlocks cred_guard_mutex. */
+	up_write(&me->signal->exec_update_lock);
 out:
 	return retval;
 }
@@ -1391,7 +1400,6 @@ void setup_new_exec(struct linux_binprm * bprm)
 	 * some architectures like powerpc
 	 */
 	me->mm->task_size = TASK_SIZE;
-	up_write(&me->signal->exec_update_lock);
 	mutex_unlock(&me->signal->cred_guard_mutex);
 
 	/* The exec locks are dropped: release the old address space now. */
@@ -1414,9 +1422,10 @@ EXPORT_SYMBOL(finalize_exec);
 
 /*
  * Prepare credentials and lock ->cred_guard_mutex.
- * setup_new_exec() commits the new creds and drops the lock.
- * Or, if exec fails before, free_bprm() should release ->cred
- * and unlock.
+ * begin_new_exec() commits the new creds and setup_new_exec()
+ * drops the lock. If exec fails before the creds are committed
+ * free_bprm() releases ->cred and unlocks. If it fails after
+ * that begin_new_exec() unlocks.
  */
 static int prepare_bprm_creds(struct linux_binprm *bprm)
 {
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 5f648e00ae661..10612214e89cd 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -14871,7 +14871,7 @@ static void perf_event_exit_task_context(struct task_struct *task, bool exit)
  * When a task exits, feed back event values to parent events.
  *
  * Can be called with exec_update_lock held when called from
- * setup_new_exec().
+ * begin_new_exec().
  */
 void perf_event_exit_task(struct task_struct *task)
 {

-- 
2.53.0


  parent reply	other threads:[~2026-11-24 23:00 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 22:58 [PATCH RFC 00/19] exec: move sleeping work out of exec_update_lock Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 01/19] proc: copy namespace link after dropping exec_update_lock Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 02/19] kcmp: look up epoll target without exec_update_lock held Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 03/19] perf: copy counter values after dropping ctx->mutex Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 04/19] perf: copy ioctl arguments without ctx->mutex held Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 05/19] selinux: close inherited files that can't be replaced Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 06/19] exec: move unshare_sighand() before exec_mmap() Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 07/19] selftests/exec: test exec with a task sleeping in a page fault Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 08/19] selftests/exec: test exec with a shared signal table Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 09/19] lsm: add security_bprm_revalidate_files() Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 10/19] bpf: mark bprm_revalidate_files as sleepable Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 11/19] selinux: revalidate inherited files before taking exec_update_lock Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 12/19] apparmor: " Christian Brauner
2026-11-24 22:58 ` Christian Brauner [this message]
2026-11-24 22:58 ` [PATCH RFC 14/19] exec: switch time namespace after dropping exec_update_lock Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 15/19] exec: don't take the mmap lock of the old mm Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 16/19] exit: don't take the mmap lock in exit_mm() Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 17/19] proc: look up map_files vma without exec_update_lock held Christian Brauner
2026-11-24 23:09   ` sashiko-bot
2026-11-24 22:58 ` [PATCH RFC 18/19] vfork: wake up the parent once the child has left the mm Christian Brauner
2026-11-24 22:58 ` [PATCH RFC 19/19] selftests/exec: test when a vfork() parent is woken up Christian Brauner
2026-11-24 23:15 ` [PATCH RFC 00/19] exec: move sleeping work out of exec_update_lock sashiko-bot

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=20261124-work-exec-update_lock-v1-13-e489035395b8@kernel.org \
    --to=brauner@kernel.org \
    --cc=acme@kernel.org \
    --cc=apparmor@lists.ubuntu.com \
    --cc=bpf@vger.kernel.org \
    --cc=david@kernel.org \
    --cc=jannh@google.com \
    --cc=kees@kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=selinux@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    /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