Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH RFC 00/19] exec: move sleeping work out of exec_update_lock
@ 2026-11-24 22:58 Christian Brauner
  2026-11-24 22:58 ` [PATCH RFC 01/19] proc: copy namespace link after dropping exec_update_lock Christian Brauner
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: Christian Brauner @ 2026-11-24 22:58 UTC (permalink / raw)
  To: Jann Horn
  Cc: Linus Torvalds, linux-fsdevel, Arnaldo Carvalho de Melo,
	linux-perf-users, bpf, selinux, Kees Cook, linux-mm,
	linux-security-module, apparmor, David Hildenbrand,
	Christian Brauner (Amutable), stable

I've been massaging exec quite a bit to move a bunch of stuff around
and get rid of a lot of sleeping work out of that path or at least move
it out from precious locks that are used for access checks.

This is another round I plan to upstream. The gist is that we're relying
on exec_update_lock in a bunch more places now and we've been
willy-nilly moving stuff beneath that lock for a while. That's not
sustainable. We need to clean that up and really only do the absolute
minimum of work under that lock to avoid stalling tasks indefinitely
just because stalls an exec. It's also just good hygiene because it's
getting really hard to understand what that lock really protects.

I've spent some time playing with various patches. Some of them went out
independently. This is another core chunk:

* proc_ns_readlink() copies the link to userspace with the lock held.

* kcmp(KCMP_EPOLL_TFD) copies the epoll slot from userspace with the
  lock of both tasks held and then waits for the epoll mutex which
  epoll_wait() holds across a page fault.

* perf_read() and perf_ioctl() copy to and from userspace with the mutex
  of the perf context held. perf_event_exec() takes that mutex with
  exec_update_lock held.

* SELinux ignores a failing replace_fd() when it revokes inherited files
  during exec.

* A vfork() parent is woken up in exec_mmap() but a child that shares
  its signal table with the parent only gets a private one after that.

* SELinux and AppArmor revalidate the inherited files from the
  bprm_committing_creds hook, i.e., with the lock held.

* Move the time namespace switch out of the lock. It doesn't need it.

* Drop the lock right after the new credentials are committed instead of
  in setup_new_exec().

* Don't take the mmap lock of the old mm in exec_mmap().

* Look up the vma for a link in /proc/<pid>/map_files without
  exec_update_lock held.

While at it, don't take the mmap lock in exit_mm() either. It's the same
leftover. A vfork() child that exits without having exec'ed waits on the
mmap lock of the parent it has just woken up.

* A vfork() parent is woken up before the child has left the mm, in exec
  as well as in exit.

What's left under the lock that can block is the hardware breakpoint
flush in flush_thread(), perf_event_exec(), perf_event_exit_task(), the
allocation in set_cred_ucounts() and a sleepable bpf lsm program on the
bprm_committing_creds hook.

The perf ones still take the mutex of the perf context but perf_read()
and perf_ioctl() don't sleep in a page fault with it held anymore.

Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
Christian Brauner (19):
      proc: copy namespace link after dropping exec_update_lock
      kcmp: look up epoll target without exec_update_lock held
      perf: copy counter values after dropping ctx->mutex
      perf: copy ioctl arguments without ctx->mutex held
      selinux: close inherited files that can't be replaced
      exec: move unshare_sighand() before exec_mmap()
      selftests/exec: test exec with a task sleeping in a page fault
      selftests/exec: test exec with a shared signal table
      lsm: add security_bprm_revalidate_files()
      bpf: mark bprm_revalidate_files as sleepable
      selinux: revalidate inherited files before taking exec_update_lock
      apparmor: revalidate inherited files before taking exec_update_lock
      exec: drop exec_update_lock after the credentials are committed
      exec: switch time namespace after dropping exec_update_lock
      exec: don't take the mmap lock of the old mm
      exit: don't take the mmap lock in exit_mm()
      proc: look up map_files vma without exec_update_lock held
      vfork: wake up the parent once the child has left the mm
      selftests/exec: test when a vfork() parent is woken up

 fs/exec.c                                        |  81 ++--
 fs/proc/base.c                                   |  68 ++-
 fs/proc/namespaces.c                             |   7 +-
 include/linux/lsm_hook_defs.h                    |   1 +
 include/linux/sched/mm.h                         |   4 +-
 include/linux/security.h                         |   5 +
 kernel/bpf/bpf_lsm.c                             |   1 +
 kernel/events/core.c                             | 138 +++---
 kernel/exit.c                                    |  10 +-
 kernel/fork.c                                    |  24 +-
 kernel/kcmp.c                                    |  72 ++-
 security/apparmor/lsm.c                          |  23 +-
 security/security.c                              |  27 +-
 security/selinux/hooks.c                         |  22 +-
 tools/testing/selftests/exec/.gitignore          |   3 +
 tools/testing/selftests/exec/Makefile            |   6 +
 tools/testing/selftests/exec/config              |   3 +
 tools/testing/selftests/exec/unshare-sighand.c   | 236 ++++++++++
 tools/testing/selftests/exec/update-lock-fault.c | 547 +++++++++++++++++++++++
 tools/testing/selftests/exec/vfork-mm.c          | 363 +++++++++++++++
 20 files changed, 1480 insertions(+), 161 deletions(-)
---
base-commit: 666539eecdf4db623f5e3c3e55cf9f2dfad24aa4
change-id: 20261120-work-exec-update_lock-7f4bec04bd81


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH RFC 01/19] proc: copy namespace link after dropping exec_update_lock
  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 ` Christian Brauner
  2026-11-24 22:58 ` [PATCH RFC 02/19] kcmp: look up epoll target without exec_update_lock held Christian Brauner
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Christian Brauner @ 2026-11-24 22:58 UTC (permalink / raw)
  To: Jann Horn
  Cc: Linus Torvalds, linux-fsdevel, Arnaldo Carvalho de Melo,
	linux-perf-users, bpf, selinux, Kees Cook, linux-mm,
	linux-security-module, apparmor, David Hildenbrand,
	Christian Brauner (Amutable), stable

proc_ns_readlink() copies the name of the namespace to userspace under
exec_update_lock. If that faults the lock is held for as long as the
fault takes. With a buffer in a mapping of a file on a FUSE filesystem
or in a range registered with userfaultfd it's the caller who decides
how long that is:

  locks held by ns-reader/230:
   #0: (&sig->exec_update_lock){++++}-{4:4}, at: proc_ns_readlink+0x180/0x340

In the meantime an exec of the task waits for exec_update_lock past the
point of no return. And everyone else who needs the exec_update_lock of
the task queues up behind the exec. So a read of /proc/<pid>/stat hangs
as well:

  task:exec-task       state:D pid:229
  Call Trace:
   __schedule+0x1691/0x4b00
   schedule+0xe0/0x2e0
   schedule_preempt_disabled+0x11/0x20
   rwsem_down_write_slowpath+0x472/0x1820
   down_write_killable+0x1ba/0x240
   begin_new_exec+0xa4b/0x2df0
   load_elf_binary+0x340/0x4af0
   bprm_execve+0x772/0x1400
   do_execveat_common.isra.0+0x3d9/0x540
   __x64_sys_execve+0x8c/0xc0
   do_syscall_64+0x110/0x600
   entry_SYSCALL_64_after_hwframe+0x77/0x7f

  task:stat-reader     state:D pid:231
  Call Trace:
   __schedule+0x1691/0x4b00
   schedule+0xe0/0x2e0
   schedule_preempt_disabled+0x11/0x20
   rwsem_down_read_slowpath+0x5cc/0x10c0
   down_read_killable+0xf1/0x5f0
   do_task_stat+0x13b/0x2870
   proc_single_show+0x110/0x1d0
   seq_read_iter+0x2e4/0x1280
   seq_read+0x29b/0x550
   vfs_read+0x171/0xb20
   ksys_read+0x111/0x250
   do_syscall_64+0x110/0x600
   entry_SYSCALL_64_after_hwframe+0x77/0x7f

All it takes is being allowed to read the namespace links of the task.

The name is formatted into a buffer on the stack anyway. So drop the
lock before it's copied out.

Fixes: 6650527444da ("proc: protect ptrace_may_access() with exec_update_lock (part 1)")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 fs/proc/namespaces.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)

diff --git a/fs/proc/namespaces.c b/fs/proc/namespaces.c
index ea6ec61a0430b..4c91ccbed21ef 100644
--- a/fs/proc/namespaces.c
+++ b/fs/proc/namespaces.c
@@ -92,12 +92,11 @@ static int proc_ns_readlink(struct dentry *dentry, char __user *buffer, int bufl
 		goto out_put_task;
 
 	res = -EACCES;
-	if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS)) {
+	if (ptrace_may_access(task, PTRACE_MODE_READ_FSCREDS))
 		res = ns_get_name(name, sizeof(name), task, ns_ops);
-		if (res >= 0)
-			res = readlink_copy(buffer, buflen, name, strlen(name));
-	}
 	up_read(&task->signal->exec_update_lock);
+	if (res >= 0)
+		res = readlink_copy(buffer, buflen, name, strlen(name));
 out_put_task:
 	put_task_struct(task);
 	return res;

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH RFC 02/19] kcmp: look up epoll target without exec_update_lock held
  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 ` Christian Brauner
  2026-11-24 22:58 ` [PATCH RFC 03/19] perf: copy counter values after dropping ctx->mutex Christian Brauner
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Christian Brauner @ 2026-11-24 22:58 UTC (permalink / raw)
  To: Jann Horn
  Cc: Linus Torvalds, linux-fsdevel, Arnaldo Carvalho de Melo,
	linux-perf-users, bpf, selinux, Kees Cook, linux-mm,
	linux-security-module, apparmor, David Hildenbrand,
	Christian Brauner (Amutable), stable

kcmp(KCMP_EPOLL_TFD) holds exec_update_lock and two things both of which
are susceptible to page fault induced, userspace controlled delays:

(1) cpy struct kcmp_epoll_slot from userspace

   locks held by kcmp-reader/233:
    #0: (&sig->exec_update_lock){++++}-{4:4}, at: __do_sys_kcmp+0x2ec/0xb10
    #1: (&sig->exec_update_lock/1){.+.+}-{4:4}, at: __do_sys_kcmp+0x210/0xb10

(2) takes the mutex of the epoll file to look up the target file.

   locks held by epoll-waiter/236:
    #0: (&ep->mtx){+.+.}-{4:4}, at: ep_send_events+0x10a/0x9f0
   locks held by kcmp-caller/237:
    #0: (&sig->exec_update_lock){++++}-{4:4}, at: __do_sys_kcmp+0x2ec/0xb10
    #1: (&sig->exec_update_lock/1){.+.+}-{4:4}, at: __do_sys_kcmp+0x210/0xb10
    #2: (&ep->mtx){+.+.}-{4:4}, at: get_epoll_tfile_raw_ptr+0xa5/0x190

In the meantime an exec of either task waits for exec_update_lock past
the point of no return. And everyone else who needs the exec_update_lock
of that task queues up behind the exec. So a read of /proc/<pid>/stat
hangs as well:

  task:exec-task       state:D pid:235
  Call Trace:
   __schedule+0x1691/0x4b00
   schedule+0xe0/0x2e0
   schedule_preempt_disabled+0x11/0x20
   rwsem_down_write_slowpath+0x472/0x1820
   down_write_killable+0x1ba/0x240
   begin_new_exec+0xa4b/0x2df0
   load_elf_binary+0x340/0x4af0
   bprm_execve+0x772/0x1400
   do_execveat_common.isra.0+0x3d9/0x540
   __x64_sys_execve+0x8c/0xc0
   do_syscall_64+0x110/0x600
   entry_SYSCALL_64_after_hwframe+0x77/0x7f

  task:stat-reader     state:D pid:238
  Call Trace:
   __schedule+0x1691/0x4b00
   schedule+0xe0/0x2e0
   schedule_preempt_disabled+0x11/0x20
   rwsem_down_read_slowpath+0x5cc/0x10c0
   down_read_killable+0xf1/0x5f0
   do_task_stat+0x13b/0x2870
   proc_single_show+0x110/0x1d0
   seq_read_iter+0x2e4/0x1280
   seq_read+0x29b/0x550
   vfs_read+0x171/0xb20
   ksys_read+0x111/0x250
   do_syscall_64+0x110/0x600
   entry_SYSCALL_64_after_hwframe+0x77/0x7f

All it takes is being allowed to kcmp() the two tasks.

Stop that and copy the before the locks are taken. With the locks held
only take a reference to the epoll file and look up the target after
they have been dropped. The epoll file is still taken from the task
after the access check and with exec_update_lock held.

Note that the target is now looked up a moment later. So if the task
execs in between and keeps the epoll file the caller may get to compare
a target that only the new image registered. The caller still only gets
at the epoll file while it's allowed to look at the task and all it
learns is how two files compare.

Fixes: 0791e3644e5e ("kcmp: add KCMP_EPOLL_TFD mode to compare epoll target files")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 kernel/kcmp.c | 72 +++++++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 50 insertions(+), 22 deletions(-)

diff --git a/kernel/kcmp.c b/kernel/kcmp.c
index 76476aeee067b..8102e5825efc5 100644
--- a/kernel/kcmp.c
+++ b/kernel/kcmp.c
@@ -94,6 +94,34 @@ static int kcmp_lock(struct rw_semaphore *l1, struct rw_semaphore *l2)
 	return err;
 }
 
+static void kcmp_unlock_tasks(struct task_struct *task1,
+			      struct task_struct *task2)
+{
+	kcmp_unlock(&task1->signal->exec_update_lock,
+		    &task2->signal->exec_update_lock);
+}
+
+static int kcmp_lock_tasks(struct task_struct *task1,
+			   struct task_struct *task2)
+{
+	int ret;
+
+	ret = kcmp_lock(&task1->signal->exec_update_lock,
+			&task2->signal->exec_update_lock);
+	if (ret)
+		return ret;
+
+	/*
+	 * One should have enough rights to inspect task details.
+	 */
+	if (ptrace_may_access(task1, PTRACE_MODE_READ_REALCREDS) &&
+	    ptrace_may_access(task2, PTRACE_MODE_READ_REALCREDS))
+		return 0;
+
+	kcmp_unlock_tasks(task1, task2);
+	return -EPERM;
+}
+
 #ifdef CONFIG_EPOLL
 static int kcmp_epoll_target(struct task_struct *task1,
 			     struct task_struct *task2,
@@ -102,18 +130,26 @@ static int kcmp_epoll_target(struct task_struct *task1,
 {
 	struct file *filp, *filp_epoll, *filp_tgt;
 	struct kcmp_epoll_slot slot;
+	int ret;
 
+	/* This may fault so do it before exec_update_lock is taken. */
 	if (copy_from_user(&slot, uslot, sizeof(slot)))
 		return -EFAULT;
 
-	filp = get_file_raw_ptr(task1, idx1);
-	if (!filp)
-		return -EBADF;
+	ret = kcmp_lock_tasks(task1, task2);
+	if (ret)
+		return ret;
 
-	filp_epoll = fget_task(task2, slot.efd);
+	filp = get_file_raw_ptr(task1, idx1);
+	filp_epoll = filp ? fget_task(task2, slot.efd) : NULL;
+	kcmp_unlock_tasks(task1, task2);
 	if (!filp_epoll)
 		return -EBADF;
 
+	/*
+	 * This takes the epoll mutex which is held across page faults. So
+	 * do it after exec_update_lock has been dropped.
+	 */
 	filp_tgt = get_epoll_tfile_raw_ptr(filp_epoll, slot.tfd, slot.toff);
 	fput(filp_epoll);
 
@@ -153,19 +189,16 @@ SYSCALL_DEFINE5(kcmp, pid_t, pid1, pid_t, pid2, int, type,
 
 	rcu_read_unlock();
 
-	/*
-	 * One should have enough rights to inspect task details.
-	 */
-	ret = kcmp_lock(&task1->signal->exec_update_lock,
-			&task2->signal->exec_update_lock);
-	if (ret)
-		goto err;
-	if (!ptrace_may_access(task1, PTRACE_MODE_READ_REALCREDS) ||
-	    !ptrace_may_access(task2, PTRACE_MODE_READ_REALCREDS)) {
-		ret = -EPERM;
-		goto err_unlock;
+	/* Takes and drops exec_update_lock itself. */
+	if (type == KCMP_EPOLL_TFD) {
+		ret = kcmp_epoll_target(task1, task2, idx1, (void __user *)idx2);
+		goto out;
 	}
 
+	ret = kcmp_lock_tasks(task1, task2);
+	if (ret)
+		goto out;
+
 	switch (type) {
 	case KCMP_FILE: {
 		struct file *filp1, *filp2;
@@ -203,18 +236,13 @@ SYSCALL_DEFINE5(kcmp, pid_t, pid1, pid_t, pid2, int, type,
 		ret = -EOPNOTSUPP;
 #endif
 		break;
-	case KCMP_EPOLL_TFD:
-		ret = kcmp_epoll_target(task1, task2, idx1, (void *)idx2);
-		break;
 	default:
 		ret = -EINVAL;
 		break;
 	}
 
-err_unlock:
-	kcmp_unlock(&task1->signal->exec_update_lock,
-		    &task2->signal->exec_update_lock);
-err:
+	kcmp_unlock_tasks(task1, task2);
+out:
 	put_task_struct(task1);
 	put_task_struct(task2);
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH RFC 03/19] perf: copy counter values after dropping ctx->mutex
  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 ` Christian Brauner
  2026-11-24 22:58 ` [PATCH RFC 04/19] perf: copy ioctl arguments without ctx->mutex held Christian Brauner
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Christian Brauner @ 2026-11-24 22:58 UTC (permalink / raw)
  To: Jann Horn
  Cc: Linus Torvalds, linux-fsdevel, Arnaldo Carvalho de Melo,
	linux-perf-users, bpf, selinux, Kees Cook, linux-mm,
	linux-security-module, apparmor, David Hildenbrand,
	Christian Brauner (Amutable), stable

perf_read() copies the values to userspace with the mutex of the
event's context held. If that faults the mutex is held for as long as
the fault takes. With a buffer in a mapping of a file on a FUSE
filesystem or in a range registered with userfaultfd it's the caller who
decides how long that is:

  locks held by perf-reader/212:
   #0: (&ctx->mutex){+.+.}-{4:4}, at: perf_event_ctx_lock_nested+0x18f/0x360

Since commit 2e498d0a74e5 ("perf: Add support for event removal on
exec") perf_event_exec() takes that mutex whenever the task has a perf
context. And begin_new_exec() calls it with exec_update_lock held. So an
exec of the task the event counts for hangs past the point of no return
until the fault is resolved. It can't be killed either:

  locks held by exec-task/211:
   #0: (&sig->cred_guard_mutex){+.+.}-{4:4}, at: bprm_execve+0xb7/0x1400
   #1: (&sig->exec_update_lock){++++}-{4:4}, at: begin_new_exec+0xa4b/0x2df0
   #2: (&ctx->mutex){+.+.}-{4:4}, at: perf_event_exec+0x17e/0xe40

  task:exec-task       state:D pid:211
  Call Trace:
   __schedule+0x1691/0x4b00
   schedule+0xe0/0x2e0
   schedule_preempt_disabled+0x11/0x20
   __mutex_lock+0x6d7/0x2700
   perf_event_exec+0x17e/0xe40
   begin_new_exec+0x1c1e/0x2df0
   load_elf_binary+0x340/0x4af0
   bprm_execve+0x772/0x1400
   do_execveat_common.isra.0+0x3d9/0x540
   __x64_sys_execve+0x8c/0xc0
   do_syscall_64+0x110/0x600
   entry_SYSCALL_64_after_hwframe+0x77/0x7f

And everyone who needs the exec_update_lock of the task waits with it.
Since commit 6650527444da ("proc: protect ptrace_may_access() with
exec_update_lock (part 1)") that includes a read of /proc/<pid>/stat:

  task:stat-reader     state:D pid:214
  Call Trace:
   __schedule+0x1691/0x4b00
   schedule+0xe0/0x2e0
   schedule_preempt_disabled+0x11/0x20
   rwsem_down_read_slowpath+0x5cc/0x10c0
   down_read_killable+0xf1/0x5f0
   do_task_stat+0x13b/0x2870
   proc_single_show+0x110/0x1d0
   seq_read_iter+0x2e4/0x1280
   seq_read+0x29b/0x550
   vfs_read+0x171/0xb20
   ksys_read+0x111/0x250
   do_syscall_64+0x110/0x600
   entry_SYSCALL_64_after_hwframe+0x77/0x7f

The values are gathered in a kernel buffer anyway. So hand that buffer
to perf_read() and copy it out after the mutex has been dropped. What a
read returns goes into a struct on the stack of perf_read() that the
helpers fill in.

Fixes: 2e498d0a74e5 ("perf: Add support for event removal on exec")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 kernel/events/core.c | 43 +++++++++++++++++++++++++------------------
 1 file changed, 25 insertions(+), 18 deletions(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index e870f12c3ad81..f00eeded212c4 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -6188,8 +6188,15 @@ static int __perf_read_group_add(struct perf_event *leader,
 	return ret;
 }
 
-static int perf_read_group(struct perf_event *event,
-				   u64 read_format, char __user *buf)
+/* What a read of an event returns, filled in with ctx->mutex held. */
+struct perf_read_buf {
+	size_t	count;		/* room in the buffer of the reader */
+	u64	values[5];	/* a single event */
+	u64	*group_values;	/* PERF_FORMAT_GROUP, allocated */
+};
+
+static int perf_read_group(struct perf_event *event, u64 read_format,
+			   struct perf_read_buf *rb)
 {
 	struct perf_event *leader = event->group_leader, *child;
 	struct perf_event_context *ctx = leader->ctx;
@@ -6218,23 +6225,20 @@ static int perf_read_group(struct perf_event *event,
 
 	mutex_unlock(&leader->child_mutex);
 
-	ret = event->read_size;
-	if (copy_to_user(buf, values, event->read_size))
-		ret = -EFAULT;
-	goto out;
+	rb->group_values = values;
+	return event->read_size;
 
 unlock:
 	mutex_unlock(&leader->child_mutex);
-out:
 	kfree(values);
 	return ret;
 }
 
-static int perf_read_one(struct perf_event *event,
-				 u64 read_format, char __user *buf)
+static int perf_read_one(struct perf_event *event, u64 read_format,
+			 struct perf_read_buf *rb)
 {
 	u64 enabled, running;
-	u64 values[5];
+	u64 *values = rb->values;
 	int n = 0;
 
 	values[n++] = __perf_event_read_value(event, &enabled, &running);
@@ -6247,9 +6251,6 @@ static int perf_read_one(struct perf_event *event,
 	if (read_format & PERF_FORMAT_LOST)
 		values[n++] = atomic64_read(&event->lost_samples);
 
-	if (copy_to_user(buf, values, n * sizeof(u64)))
-		return -EFAULT;
-
 	return n * sizeof(u64);
 }
 
@@ -6270,7 +6271,7 @@ static bool is_event_hup(struct perf_event *event)
  * Read the performance event - simple non blocking version for now
  */
 static ssize_t
-__perf_read(struct perf_event *event, char __user *buf, size_t count)
+__perf_read(struct perf_event *event, struct perf_read_buf *rb)
 {
 	u64 read_format = event->attr.read_format;
 	int ret;
@@ -6283,14 +6284,14 @@ __perf_read(struct perf_event *event, char __user *buf, size_t count)
 	if (event->state == PERF_EVENT_STATE_ERROR)
 		return 0;
 
-	if (count < event->read_size)
+	if (rb->count < event->read_size)
 		return -ENOSPC;
 
 	WARN_ON_ONCE(event->ctx->parent_ctx);
 	if (read_format & PERF_FORMAT_GROUP)
-		ret = perf_read_group(event, read_format, buf);
+		ret = perf_read_group(event, read_format, rb);
 	else
-		ret = perf_read_one(event, read_format, buf);
+		ret = perf_read_one(event, read_format, rb);
 
 	return ret;
 }
@@ -6299,6 +6300,7 @@ static ssize_t
 perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
 {
 	struct perf_event *event = file->private_data;
+	struct perf_read_buf rb = { .count = count };
 	struct perf_event_context *ctx;
 	int ret;
 
@@ -6307,9 +6309,14 @@ perf_read(struct file *file, char __user *buf, size_t count, loff_t *ppos)
 		return ret;
 
 	ctx = perf_event_ctx_lock(event);
-	ret = __perf_read(event, buf, count);
+	ret = __perf_read(event, &rb);
 	perf_event_ctx_unlock(event, ctx);
 
+	/* The copy can sleep in a page fault. Don't hold ctx->mutex for it. */
+	if (ret > 0 && copy_to_user(buf, rb.group_values ?: rb.values, ret))
+		ret = -EFAULT;
+
+	kfree(rb.group_values);
 	return ret;
 }
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH RFC 04/19] perf: copy ioctl arguments without ctx->mutex held
  2026-11-24 22:58 [PATCH RFC 00/19] exec: move sleeping work out of exec_update_lock Christian Brauner
                   ` (2 preceding siblings ...)
  2026-11-24 22:58 ` [PATCH RFC 03/19] perf: copy counter values after dropping ctx->mutex Christian Brauner
@ 2026-11-24 22:58 ` Christian Brauner
  2026-11-24 22:58 ` [PATCH RFC 05/19] selinux: close inherited files that can't be replaced Christian Brauner
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Christian Brauner @ 2026-11-24 22:58 UTC (permalink / raw)
  To: Jann Horn
  Cc: Linus Torvalds, linux-fsdevel, Arnaldo Carvalho de Melo,
	linux-perf-users, bpf, selinux, Kees Cook, linux-mm,
	linux-security-module, apparmor, David Hildenbrand,
	Christian Brauner (Amutable), stable

perf_ioctl() calls _perf_ioctl() with the mutex of the event's context
held and a number of commands access userspace from there:

- PERF_EVENT_IOC_PERIOD copies the period from userspace.

- PERF_EVENT_IOC_ID copies the id to userspace.

- PERF_EVENT_IOC_SET_FILTER copies the filter string from userspace.

- PERF_EVENT_IOC_QUERY_BPF copies the query from userspace and the ids
  of the programs back.

- PERF_EVENT_IOC_MODIFY_ATTRIBUTES copies the attributes from userspace.

This is the same problem as for read(). If one of them faults the mutex
is held for as long as the fault takes and an exec of the task the event
counts for hangs with exec_update_lock held:

  locks held by perf-ioctl/217:
   #0: (&ctx->mutex){+.+.}-{4:4}, at: perf_event_ctx_lock_nested+0x18f/0x360
  locks held by exec-task/216:
   #0: (&sig->cred_guard_mutex){+.+.}-{4:4}, at: bprm_execve+0xb7/0x1400
   #1: (&sig->exec_update_lock){++++}-{4:4}, at: begin_new_exec+0xa4b/0x2df0
   #2: (&ctx->mutex){+.+.}-{4:4}, at: perf_event_exec+0x17e/0xe40
  locks held by stat-reader/218:
   #0: (&p->lock){+.+.}-{4:4}, at: seq_read_iter+0xc5/0x1280
   #1: (&sig->exec_update_lock){++++}-{4:4}, at: do_task_stat+0x13b/0x2870

Copy what comes from userspace before the mutex is taken and what goes
to userspace after it has been dropped.

PERF_EVENT_IOC_QUERY_BPF doesn't need the mutex. The programs of a
tracepoint are protected by bpf_event_mutex. So call
perf_event_query_prog_array() before the mutex is taken.

Fixes: 2e498d0a74e5 ("perf: Add support for event removal on exec")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 kernel/events/core.c | 93 ++++++++++++++++++++++++++++++++--------------------
 1 file changed, 57 insertions(+), 36 deletions(-)

diff --git a/kernel/events/core.c b/kernel/events/core.c
index f00eeded212c4..5f648e00ae661 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -6635,14 +6635,25 @@ static inline bool is_perf_file(struct fd f)
 
 static int perf_event_set_output(struct perf_event *event,
 				 struct perf_event *output_event);
-static int perf_event_set_filter(struct perf_event *event, void __user *arg);
+static int perf_event_set_filter(struct perf_event *event, char *filter_str);
 static int perf_copy_attr(struct perf_event_attr __user *uattr,
 			  struct perf_event_attr *attr);
 static int __perf_event_set_bpf_prog(struct perf_event *event,
 				     struct bpf_prog *prog,
 				     u64 bpf_cookie);
 
-static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned long arg)
+/*
+ * What perf_ioctl() copies from userspace before it takes ctx->mutex and
+ * what it copies back once it dropped it.
+ */
+struct perf_ioctl_data {
+	u64			value;
+	char			*filter;
+	struct perf_event_attr	attr;
+};
+
+static long _perf_ioctl(struct perf_event *event, unsigned int cmd,
+			unsigned long arg, struct perf_ioctl_data *data)
 {
 	void (*func)(struct perf_event *);
 	u32 flags = arg;
@@ -6665,22 +6676,11 @@ static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned lon
 		return _perf_event_refresh(event, arg);
 
 	case PERF_EVENT_IOC_PERIOD:
-	{
-		u64 value;
+		return _perf_event_period(event, data->value);
 
-		if (copy_from_user(&value, (u64 __user *)arg, sizeof(value)))
-			return -EFAULT;
-
-		return _perf_event_period(event, value);
-	}
 	case PERF_EVENT_IOC_ID:
-	{
-		u64 id = primary_event_id(event);
-
-		if (copy_to_user((void __user *)arg, &id, sizeof(id)))
-			return -EFAULT;
+		data->value = primary_event_id(event);
 		return 0;
-	}
 
 	case PERF_EVENT_IOC_SET_OUTPUT:
 	{
@@ -6695,7 +6695,7 @@ static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned lon
 	}
 
 	case PERF_EVENT_IOC_SET_FILTER:
-		return perf_event_set_filter(event, (void __user *)arg);
+		return perf_event_set_filter(event, data->filter);
 
 	case PERF_EVENT_IOC_SET_BPF:
 	{
@@ -6729,19 +6729,9 @@ static long _perf_ioctl(struct perf_event *event, unsigned int cmd, unsigned lon
 		return 0;
 	}
 
-	case PERF_EVENT_IOC_QUERY_BPF:
-		return perf_event_query_prog_array(event, (void __user *)arg);
-
-	case PERF_EVENT_IOC_MODIFY_ATTRIBUTES: {
-		struct perf_event_attr new_attr;
-		int err = perf_copy_attr((struct perf_event_attr __user *)arg,
-					 &new_attr);
+	case PERF_EVENT_IOC_MODIFY_ATTRIBUTES:
+		return perf_event_modify_attr(event, &data->attr);
 
-		if (err)
-			return err;
-
-		return perf_event_modify_attr(event,  &new_attr);
-	}
 	default:
 		return -ENOTTY;
 	}
@@ -6758,6 +6748,7 @@ static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 {
 	struct perf_event *event = file->private_data;
 	struct perf_event_context *ctx;
+	struct perf_ioctl_data data;
 	long ret;
 
 	/* Treat ioctl like writes as it is likely a mutating operation. */
@@ -6765,10 +6756,46 @@ static long perf_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
 	if (ret)
 		return ret;
 
+	/*
+	 * A copy from or to userspace can sleep in a page fault. Don't hold
+	 * ctx->mutex for it.
+	 */
+	data.filter = NULL;
+	switch (cmd) {
+	case PERF_EVENT_IOC_PERIOD:
+		if (copy_from_user(&data.value, (u64 __user *)arg,
+				   sizeof(data.value)))
+			return -EFAULT;
+		break;
+
+	case PERF_EVENT_IOC_SET_FILTER:
+		data.filter = strndup_user((void __user *)arg, PAGE_SIZE);
+		if (IS_ERR(data.filter))
+			return PTR_ERR(data.filter);
+		break;
+
+	case PERF_EVENT_IOC_QUERY_BPF:
+		/* Looks at the tracepoint's programs under bpf_event_mutex. */
+		return perf_event_query_prog_array(event, (void __user *)arg);
+
+	case PERF_EVENT_IOC_MODIFY_ATTRIBUTES:
+		ret = perf_copy_attr((struct perf_event_attr __user *)arg,
+				     &data.attr);
+		if (ret)
+			return ret;
+		break;
+	}
+
 	ctx = perf_event_ctx_lock(event);
-	ret = _perf_ioctl(event, cmd, arg);
+	ret = _perf_ioctl(event, cmd, arg, &data);
 	perf_event_ctx_unlock(event, ctx);
 
+	kfree(data.filter);
+
+	if (!ret && cmd == PERF_EVENT_IOC_ID &&
+	    copy_to_user((void __user *)arg, &data.value, sizeof(data.value)))
+		return -EFAULT;
+
 	return ret;
 }
 
@@ -12367,14 +12394,9 @@ perf_event_set_addr_filter(struct perf_event *event, char *filter_str)
 	return ret;
 }
 
-static int perf_event_set_filter(struct perf_event *event, void __user *arg)
+static int perf_event_set_filter(struct perf_event *event, char *filter_str)
 {
 	int ret = -EINVAL;
-	char *filter_str;
-
-	filter_str = strndup_user(arg, PAGE_SIZE);
-	if (IS_ERR(filter_str))
-		return PTR_ERR(filter_str);
 
 #ifdef CONFIG_EVENT_TRACING
 	if (perf_event_is_tracing(event)) {
@@ -12399,7 +12421,6 @@ static int perf_event_set_filter(struct perf_event *event, void __user *arg)
 	if (has_addr_filter(event))
 		ret = perf_event_set_addr_filter(event, filter_str);
 
-	kfree(filter_str);
 	return ret;
 }
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH RFC 05/19] selinux: close inherited files that can't be replaced
  2026-11-24 22:58 [PATCH RFC 00/19] exec: move sleeping work out of exec_update_lock Christian Brauner
                   ` (3 preceding siblings ...)
  2026-11-24 22:58 ` [PATCH RFC 04/19] perf: copy ioctl arguments without ctx->mutex held Christian Brauner
@ 2026-11-24 22:58 ` Christian Brauner
  2026-11-24 22:58 ` [PATCH RFC 06/19] exec: move unshare_sighand() before exec_mmap() Christian Brauner
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Christian Brauner @ 2026-11-24 22:58 UTC (permalink / raw)
  To: Jann Horn
  Cc: Linus Torvalds, linux-fsdevel, Arnaldo Carvalho de Melo,
	linux-perf-users, bpf, selinux, Kees Cook, linux-mm,
	linux-security-module, apparmor, David Hildenbrand,
	Christian Brauner (Amutable), stable

When an exec transitions to a new SID flush_unauthorized_files()
replaces the inherited files the new SID isn't allowed to use with the
null device. It uses replace_fd() for that and ignores the return value.

replace_fd() refuses a file descriptor at or above RLIMIT_NOFILE. And a
task can lower its own soft limit as it likes. So a task that sets
RLIMIT_NOFILE to the number of a file descriptor right before the exec
keeps that file open in the new image:

	fd = open(path, O_RDONLY);	/* 3 */
	getrlimit(RLIMIT_NOFILE, &rl);
	rl.rlim_cur = fd;
	setrlimit(RLIMIT_NOFILE, &rl);
	execve(...);

The new SID still can't read from the file as that is checked again on
every use. But it holds on to the file and what isn't checked on use
like lseek() works.

Before commit ee97cd872d08 ("switch flush_unauthorized_files() to
replace_fd()") the file was closed first and stayed closed if its file
descriptor couldn't be allocated again. So close the file if it can't be
replaced.

Fixes: ee97cd872d08 ("switch flush_unauthorized_files() to replace_fd()")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 security/selinux/hooks.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 9950f89dfaa23..4ec9ec723cacc 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -2521,7 +2521,9 @@ static inline void flush_unauthorized_files(const struct cred *cred,
 		devnull = NULL;
 	/* replace all the matching ones with this */
 	do {
-		replace_fd(n - 1, devnull, 0);
+		/* replace_fd() fails for an fd at or above RLIMIT_NOFILE. */
+		if (replace_fd(n - 1, devnull, 0) < 0)
+			close_fd(n - 1);
 	} while ((n = iterate_fd(files, n, match_file, cred)) != 0);
 	if (devnull)
 		fput(devnull);

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH RFC 06/19] exec: move unshare_sighand() before exec_mmap()
  2026-11-24 22:58 [PATCH RFC 00/19] exec: move sleeping work out of exec_update_lock Christian Brauner
                   ` (4 preceding siblings ...)
  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 ` Christian Brauner
  2026-11-24 22:58 ` [PATCH RFC 09/19] lsm: add security_bprm_revalidate_files() Christian Brauner
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: Christian Brauner @ 2026-11-24 22:58 UTC (permalink / raw)
  To: Jann Horn
  Cc: Linus Torvalds, linux-fsdevel, Arnaldo Carvalho de Melo,
	linux-perf-users, bpf, selinux, Kees Cook, linux-mm,
	linux-security-module, apparmor, David Hildenbrand,
	Christian Brauner (Amutable), stable

A task that called vfork() sleeps until the child has released the mm.
During exec that happens in exec_mmap(). A child that was created with
CLONE_SIGHAND shares its signal table with the parent and
unshare_sighand() only gives it a private one after exec_mmap(). So the
parent runs again while the child is still on the table they share. If
the parent ignores a signal right after vfork() returned the new image
can end up ignoring it as well.

It used to be the other way around. Commit ccf0fa6be026 ("exec: Move
exec_mmap right after de_thread in flush_old_exec") moved exec_mmap() in
front of it for two reasons. bprm->mm was to tell whether exec is past
the point of no return and everything that can wait for userspace
indefinitely was to sit in front of the lock that became
exec_update_lock.

Move it back to where it used to be, right after de_thread() and the
posix timer cleanup. Nothing in between relies on the signal table still
being shared. With that the table is private before the parent is woken
up. And the new table isn't allocated with exec_update_lock held
anymore.

Fixes: ccf0fa6be026 ("exec: Move exec_mmap right after de_thread in flush_old_exec")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 fs/exec.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/fs/exec.c b/fs/exec.c
index 8d2b14e1d455c..87b78fce609de 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1171,6 +1171,14 @@ int begin_new_exec(struct linux_binprm * bprm)
 	 */
 	posixtimer_exec();
 
+	/*
+	 * Make the signal table private before a vfork() parent that shares
+	 * it is woken up.
+	 */
+	retval = unshare_sighand(me);
+	if (retval)
+		goto out;
+
 	/* see the comment in check_unsafe_exec() */
 	current->fs->in_exec = 0;
 
@@ -1223,13 +1231,6 @@ int begin_new_exec(struct linux_binprm * bprm)
 	if (retval)
 		goto out_unlock;
 
-	/*
-	 * Make the signal table private.
-	 */
-	retval = unshare_sighand(me);
-	if (retval)
-		goto out_unlock;
-
 	me->flags &= ~(PF_RANDOMIZE | PF_FORKNOEXEC |
 					PF_NOFREEZE | PF_NO_SETAFFINITY);
 	flush_thread();

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH RFC 09/19] lsm: add security_bprm_revalidate_files()
  2026-11-24 22:58 [PATCH RFC 00/19] exec: move sleeping work out of exec_update_lock Christian Brauner
                   ` (5 preceding siblings ...)
  2026-11-24 22:58 ` [PATCH RFC 06/19] exec: move unshare_sighand() before exec_mmap() Christian Brauner
@ 2026-11-24 22:58 ` 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
  8 siblings, 0 replies; 10+ messages in thread
From: Christian Brauner @ 2026-11-24 22:58 UTC (permalink / raw)
  To: Jann Horn
  Cc: Linus Torvalds, linux-fsdevel, Arnaldo Carvalho de Melo,
	linux-perf-users, bpf, selinux, Kees Cook, linux-mm,
	linux-security-module, apparmor, David Hildenbrand,
	Christian Brauner (Amutable), stable

SELinux and AppArmor revalidate the controlling terminal and the
inherited files during exec when the security context changes. Files
the new credentials aren't allowed to use are replaced with the null
device or closed and a controlling terminal they aren't allowed to use
is dropped. Currently, they do this from the bprm_committing_creds hook
which begin_new_exec() calls with exec_update_lock held.

Closing a file calls into the filesystem's ->flush() method and that may
block for a long time. For the same reason commit e780259b54e6 ("exec:
do_close_on_exec() before taking exec_update_lock") moved the closing of
close-on-exec files in front of exec_update_lock.

Add a new hook that is called right after the close-on-exec files have
been closed. At that point we're past the point of no return, all other
threads are gone and the file descriptor table isn't shared anymore. So
nobody but the task itself can install new files or acquire a new
controlling terminal until the new credentials are committed. And
nothing between the new hook and the bprm_committing_creds hook touches
the file descriptor table.

The hook has no users yet. SELinux and AppArmor are switched over
separately. Note that bpf lsm programs can attach to it like to any
other LSM hook.

Fixes: 6650527444da ("proc: protect ptrace_may_access() with exec_update_lock (part 1)")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 fs/exec.c                     |  3 +++
 include/linux/lsm_hook_defs.h |  1 +
 include/linux/security.h      |  5 +++++
 security/security.c           | 27 ++++++++++++++++++++++++---
 4 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/fs/exec.c b/fs/exec.c
index 87b78fce609de..855437c352137 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -1203,6 +1203,9 @@ int begin_new_exec(struct linux_binprm * bprm)
 	 */
 	close_cloexec_files(me->files);
 
+	/* Let LSMs revoke inherited files the new creds may not use. */
+	security_bprm_revalidate_files(bprm);
+
 	/*
 	 * Must be called _before_ exec_mmap() as bprm->mm is
 	 * not visible until then. Doing it here also ensures
diff --git a/include/linux/lsm_hook_defs.h b/include/linux/lsm_hook_defs.h
index 87d9d06710cb2..c1bb7501d530d 100644
--- a/include/linux/lsm_hook_defs.h
+++ b/include/linux/lsm_hook_defs.h
@@ -52,6 +52,7 @@ LSM_HOOK(int, 0, vm_enough_memory, struct mm_struct *mm, long pages)
 LSM_HOOK(int, 0, bprm_creds_for_exec, struct linux_binprm *bprm)
 LSM_HOOK(int, 0, bprm_creds_from_file, struct linux_binprm *bprm, const struct file *file)
 LSM_HOOK(int, 0, bprm_check_security, struct linux_binprm *bprm)
+LSM_HOOK(void, LSM_RET_VOID, bprm_revalidate_files, const struct linux_binprm *bprm)
 LSM_HOOK(void, LSM_RET_VOID, bprm_committing_creds, const struct linux_binprm *bprm)
 LSM_HOOK(void, LSM_RET_VOID, bprm_committed_creds, const struct linux_binprm *bprm)
 LSM_HOOK(int, 0, fs_context_submount, struct fs_context *fc, struct super_block *reference)
diff --git a/include/linux/security.h b/include/linux/security.h
index 40831fab54709..e1a85b2f7005e 100644
--- a/include/linux/security.h
+++ b/include/linux/security.h
@@ -360,6 +360,7 @@ int security_vm_enough_memory_mm(struct mm_struct *mm, long pages);
 int security_bprm_creds_for_exec(struct linux_binprm *bprm);
 int security_bprm_creds_from_file(struct linux_binprm *bprm, const struct file *file);
 int security_bprm_check(struct linux_binprm *bprm);
+void security_bprm_revalidate_files(const struct linux_binprm *bprm);
 void security_bprm_committing_creds(const struct linux_binprm *bprm);
 void security_bprm_committed_creds(const struct linux_binprm *bprm);
 int security_fs_context_submount(struct fs_context *fc, struct super_block *reference);
@@ -753,6 +754,10 @@ static inline int security_bprm_check(struct linux_binprm *bprm)
 	return 0;
 }
 
+static inline void security_bprm_revalidate_files(const struct linux_binprm *bprm)
+{
+}
+
 static inline void security_bprm_committing_creds(const struct linux_binprm *bprm)
 {
 }
diff --git a/security/security.c b/security/security.c
index 4727b8347fc4e..f340880ce77ff 100644
--- a/security/security.c
+++ b/security/security.c
@@ -834,6 +834,25 @@ int security_bprm_check(struct linux_binprm *bprm)
 	return call_int_hook(bprm_check_security, bprm);
 }
 
+/**
+ * security_bprm_revalidate_files() - Revalidate open files during exec()
+ * @bprm: binary program information
+ *
+ * Revalidate access to the controlling tty and the open file descriptors of a
+ * process being transformed by an execve operation, based on the old
+ * credentials pointed to by @current->cred and the information set in
+ * @bprm->cred by the bprm_creds_for_exec hook.  Files to which access will no
+ * longer be granted may be closed or replaced.  @bprm points to the
+ * linux_binprm structure.  This is called after the point of no return, once
+ * the file descriptor table has been unshared and close-on-exec files have been
+ * closed, but before exec_update_lock is taken, as closing a file may block in
+ * the filesystem.
+ */
+void security_bprm_revalidate_files(const struct linux_binprm *bprm)
+{
+	call_void_hook(bprm_revalidate_files, bprm);
+}
+
 /**
  * security_bprm_committing_creds() - Install creds for a process during exec()
  * @bprm: binary program information
@@ -842,9 +861,11 @@ int security_bprm_check(struct linux_binprm *bprm)
  * transformed by an execve operation, based on the old credentials pointed to
  * by @current->cred and the information set in @bprm->cred by the
  * bprm_creds_for_exec hook.  @bprm points to the linux_binprm structure.  This
- * hook is a good place to perform state changes on the process such as closing
- * open file descriptors to which access will no longer be granted when the
- * attributes are changed.  This is called immediately before commit_creds().
+ * hook is a good place to perform state changes on the process such as
+ * resetting resource limits.  It is called with exec_update_lock held so it
+ * should not block on a filesystem.  In particular, it must not close files.
+ * Use the bprm_revalidate_files hook for that.  This is called immediately
+ * before commit_creds().
  */
 void security_bprm_committing_creds(const struct linux_binprm *bprm)
 {

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH RFC 11/19] selinux: revalidate inherited files before taking exec_update_lock
  2026-11-24 22:58 [PATCH RFC 00/19] exec: move sleeping work out of exec_update_lock Christian Brauner
                   ` (6 preceding siblings ...)
  2026-11-24 22:58 ` [PATCH RFC 09/19] lsm: add security_bprm_revalidate_files() Christian Brauner
@ 2026-11-24 22:58 ` Christian Brauner
  2026-11-24 22:58 ` [PATCH RFC 12/19] apparmor: " Christian Brauner
  8 siblings, 0 replies; 10+ messages in thread
From: Christian Brauner @ 2026-11-24 22:58 UTC (permalink / raw)
  To: Jann Horn
  Cc: Linus Torvalds, linux-fsdevel, Arnaldo Carvalho de Melo,
	linux-perf-users, bpf, selinux, Kees Cook, linux-mm,
	linux-security-module, apparmor, David Hildenbrand,
	Christian Brauner (Amutable), stable

When an exec transitions to a new SID flush_unauthorized_files()
revalidates the controlling terminal and the inherited files against the
new SID. Files the new SID isn't allowed to use are replaced with the
null device. This is done from selinux_bprm_committing_creds() which
runs with exec_update_lock held for writing.

Replacing a file closes the old one and so calls its ->flush() method
which can block waiting on the filesystem. So a hung filesystem blocks
everyone who needs the exec_update_lock of that task. And a FUSE server
that looks at the task it serves the flush request for deadlocks:

  exec                                 FUSE server
  begin_new_exec()
    exec_mmap()
      down_write(exec_update_lock)
    security_bprm_committing_creds()
      flush_unauthorized_files()
        replace_fd()
          filp_close()
            fuse_flush()
                                       do_task_stat()
                                         down_read(exec_update_lock)

With a FUSE server that reads /proc/<pid>/stat of the task that sent the
flush request the exec hangs until the server is killed:

  task:se-target       state:S pid:143
  Call Trace:
   __schedule+0x53e/0x13f0
   schedule+0x36/0x100
   request_wait_answer+0x1a9/0x320
   fuse_chan_send+0x110/0x250
   fuse_flush+0x1af/0x1f0
   filp_flush+0x33/0x90
   filp_close+0x12/0x30
   do_dup2+0xbf/0x1b0
   replace_fd+0x7e/0xd0
   selinux_bprm_committing_creds+0x1a7/0x2d0
   security_bprm_committing_creds+0x1e/0x30
   begin_new_exec+0x6dc/0xf90
   load_elf_binary+0x12a/0x1990
   bprm_execve+0x39d/0x870
   do_execveat_common.isra.0+0x165/0x180
   __x64_sys_execve+0x38/0x50
   do_syscall_64+0x110/0x600
   entry_SYSCALL_64_after_hwframe+0x77/0x7f

  task:se-fuse         state:D pid:142
  Call Trace:
   __schedule+0x53e/0x13f0
   schedule+0x36/0x100
   schedule_preempt_disabled+0x11/0x20
   rwsem_down_read_slowpath+0x186/0x5f0
   down_read_killable+0x78/0x1b0
   do_task_stat+0x87/0x10c0
   proc_single_show+0x67/0xe0
   seq_read_iter+0x11c/0x480
   seq_read+0x127/0x1c0
   vfs_read+0xd8/0x4a0
   ksys_read+0x7c/0x110
   do_syscall_64+0x110/0x600
   entry_SYSCALL_64_after_hwframe+0x77/0x7f

The flush is a forced request so the exec can't even be killed while it
waits for the reply.

This is the same problem that commit e780259b54e6 ("exec:
do_close_on_exec() before taking exec_update_lock") fixed for
close-on-exec files.

Fix this by moving the revalidation into the new bprm_revalidate_files
hook which is called before exec_update_lock is taken.

Fixes: 6650527444da ("proc: protect ptrace_may_access() with exec_update_lock (part 1)")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 security/selinux/hooks.c | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index 4ec9ec723cacc..71ea94f663772 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -2529,6 +2529,20 @@ static inline void flush_unauthorized_files(const struct cred *cred,
 		fput(devnull);
 }
 
+/*
+ * Close files for which the new task SID is not authorized. Called before
+ * exec_update_lock is taken as closing files may block in the filesystem.
+ */
+static void selinux_bprm_revalidate_files(const struct linux_binprm *bprm)
+{
+	const struct cred_security_struct *new_crsec = selinux_cred(bprm->cred);
+
+	if (new_crsec->sid == new_crsec->osid)
+		return;
+
+	flush_unauthorized_files(bprm->cred, current->files);
+}
+
 /*
  * Prepare a process for imminent new credential changes due to exec
  */
@@ -2542,9 +2556,6 @@ static void selinux_bprm_committing_creds(const struct linux_binprm *bprm)
 	if (new_crsec->sid == new_crsec->osid)
 		return;
 
-	/* Close files for which the new task SID is not authorized. */
-	flush_unauthorized_files(bprm->cred, current->files);
-
 	/* Always clear parent death signal on SID transitions. */
 	current->pdeath_signal = 0;
 
@@ -7690,6 +7701,7 @@ static struct security_hook_list selinux_hooks[] __ro_after_init = {
 	LSM_HOOK_INIT(netlink_send, selinux_netlink_send),
 
 	LSM_HOOK_INIT(bprm_creds_for_exec, selinux_bprm_creds_for_exec),
+	LSM_HOOK_INIT(bprm_revalidate_files, selinux_bprm_revalidate_files),
 	LSM_HOOK_INIT(bprm_committing_creds, selinux_bprm_committing_creds),
 	LSM_HOOK_INIT(bprm_committed_creds, selinux_bprm_committed_creds),
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH RFC 12/19] apparmor: revalidate inherited files before taking exec_update_lock
  2026-11-24 22:58 [PATCH RFC 00/19] exec: move sleeping work out of exec_update_lock Christian Brauner
                   ` (7 preceding siblings ...)
  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 ` Christian Brauner
  8 siblings, 0 replies; 10+ messages in thread
From: Christian Brauner @ 2026-11-24 22:58 UTC (permalink / raw)
  To: Jann Horn
  Cc: Linus Torvalds, linux-fsdevel, Arnaldo Carvalho de Melo,
	linux-perf-users, bpf, selinux, Kees Cook, linux-mm,
	linux-security-module, apparmor, David Hildenbrand,
	Christian Brauner (Amutable), stable

When an exec changes the profile aa_inherit_files() revalidates the
controlling terminal and the inherited files against the new label.
Everything the new label isn't allowed to use is taken away from the
task. This is done from apparmor_bprm_committing_creds() which runs with
exec_update_lock held for writing.

Taking a file away closes it and so calls its ->flush() method which can
block waiting on the filesystem. So a hung filesystem blocks everyone
who needs the exec_update_lock of that task. And a FUSE server that
looks at the task it serves the flush request for deadlocks:

  exec                                 FUSE server
  begin_new_exec()
    exec_mmap()
      down_write(exec_update_lock)
    security_bprm_committing_creds()
      aa_inherit_files()
        replace_fd()
          filp_close()
            fuse_flush()
                                       do_task_stat()
                                         down_read(exec_update_lock)

With a FUSE server that reads /proc/<pid>/stat of the task that sent the
flush request the exec hangs until the server is killed:

  task:aa-target       state:S pid:98
  Call Trace:
   __schedule+0x53e/0x13f0
   schedule+0x36/0x100
   request_wait_answer+0x1a9/0x320
   fuse_chan_send+0x110/0x250
   fuse_flush+0x1af/0x1f0
   filp_flush+0x33/0x90
   filp_close+0x12/0x30
   aa_inherit_files+0x191/0x2a0
   apparmor_bprm_committing_creds+0x61/0x90
   security_bprm_committing_creds+0x19/0x30
   begin_new_exec+0x6dc/0xf90
   load_elf_binary+0x12a/0x1990
   bprm_execve+0x39d/0x870
   do_execveat_common.isra.0+0x165/0x180
   __x64_sys_execve+0x38/0x50
   do_syscall_64+0x110/0x600
   entry_SYSCALL_64_after_hwframe+0x77/0x7f

  task:aa-fuse         state:D pid:97
  Call Trace:
   __schedule+0x53e/0x13f0
   schedule+0x36/0x100
   schedule_preempt_disabled+0x11/0x20
   rwsem_down_read_slowpath+0x186/0x5f0
   down_read_killable+0x78/0x1b0
   do_task_stat+0x87/0x10c0
   proc_single_show+0x67/0xe0
   seq_read_iter+0x11c/0x480
   seq_read+0x127/0x1c0
   vfs_read+0xd8/0x4a0
   ksys_read+0x7c/0x110
   do_syscall_64+0x110/0x600
   entry_SYSCALL_64_after_hwframe+0x77/0x7f

The flush is a forced request so the exec can't even be killed while it
waits for the reply.

This is the same problem that commit e780259b54e6 ("exec:
do_close_on_exec() before taking exec_update_lock") fixed for
close-on-exec files.

Fix this by moving the revalidation into the new bprm_revalidate_files
hook which is called before exec_update_lock is taken.

Fixes: 6650527444da ("proc: protect ptrace_may_access() with exec_update_lock (part 1)")
Cc: stable@vger.kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
---
 security/apparmor/lsm.c | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/security/apparmor/lsm.c b/security/apparmor/lsm.c
index eaebc3c60c5af..e40619b0f4ba9 100644
--- a/security/apparmor/lsm.c
+++ b/security/apparmor/lsm.c
@@ -951,10 +951,13 @@ static int apparmor_setprocattr(const char *name, void *value,
 }
 
 /**
- * apparmor_bprm_committing_creds - do task cleanup on committing new creds
+ * apparmor_bprm_revalidate_files - revalidate inherited files on exec
  * @bprm: binprm for the exec  (NOT NULL)
+ *
+ * Called before exec_update_lock is taken as closing files may block in
+ * the filesystem.
  */
-static void apparmor_bprm_committing_creds(const struct linux_binprm *bprm)
+static void apparmor_bprm_revalidate_files(const struct linux_binprm *bprm)
 {
 	struct aa_label *label = aa_current_raw_label();
 	struct aa_label *new_label = cred_label(bprm->cred);
@@ -965,6 +968,21 @@ static void apparmor_bprm_committing_creds(const struct linux_binprm *bprm)
 		return;
 
 	aa_inherit_files(bprm->cred, current->files);
+}
+
+/**
+ * apparmor_bprm_committing_creds - do task cleanup on committing new creds
+ * @bprm: binprm for the exec  (NOT NULL)
+ */
+static void apparmor_bprm_committing_creds(const struct linux_binprm *bprm)
+{
+	struct aa_label *label = aa_current_raw_label();
+	struct aa_label *new_label = cred_label(bprm->cred);
+
+	/* bail out if unconfined or not changing profile */
+	if ((new_label->proxy == label->proxy) ||
+	    (unconfined(new_label)))
+		return;
 
 	current->pdeath_signal = 0;
 
@@ -1860,6 +1878,7 @@ static struct security_hook_list apparmor_hooks[] __ro_after_init = {
 	LSM_HOOK_INIT(cred_transfer, apparmor_cred_transfer),
 
 	LSM_HOOK_INIT(bprm_creds_for_exec, apparmor_bprm_creds_for_exec),
+	LSM_HOOK_INIT(bprm_revalidate_files, apparmor_bprm_revalidate_files),
 	LSM_HOOK_INIT(bprm_committing_creds, apparmor_bprm_committing_creds),
 	LSM_HOOK_INIT(bprm_committed_creds, apparmor_bprm_committed_creds),
 

-- 
2.53.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2026-11-24 23:00 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 09/19] lsm: add security_bprm_revalidate_files() 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox