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>,
stable@vger.kernel.org
Subject: [PATCH RFC 04/19] perf: copy ioctl arguments without ctx->mutex held
Date: Tue, 24 Nov 2026 23:58:27 +0100 [thread overview]
Message-ID: <20261124-work-exec-update_lock-v1-4-e489035395b8@kernel.org> (raw)
In-Reply-To: <20261124-work-exec-update_lock-v1-0-e489035395b8@kernel.org>
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
next prev parent reply other threads:[~2026-11-24 22:59 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 ` Christian Brauner [this message]
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 ` [PATCH RFC 13/19] exec: drop exec_update_lock after the credentials are committed Christian Brauner
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-4-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=stable@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