From: "Serge Hallyn (AMD)" <sergeh@kernel.org>
To: Christian Brauner <brauner@kernel.org>
Cc: Jann Horn <jannh@google.com>,
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>,
stable@vger.kernel.org
Subject: Re: [PATCH RFC 04/19] perf: copy ioctl arguments without ctx->mutex held
Date: Thu, 3 Dec 2026 14:18:58 -0600 [thread overview]
Message-ID: <axHOsq0ohP5JnXEP@shallyn-amd> (raw)
In-Reply-To: <20261124-work-exec-update_lock-v1-4-e489035395b8@kernel.org>
On Tue, Nov 24, 2026 at 11:58:27PM +0100, Christian Brauner wrote:
> 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>
Reviewed-by: Serge Hallyn (AMD) <sergeh@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-12-03 20:19 UTC|newest]
Thread overview: 17+ 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-12-03 20:18 ` Serge Hallyn (AMD)
2026-11-24 22:58 ` [PATCH RFC 02/19] kcmp: look up epoll target without exec_update_lock held Christian Brauner
2026-12-03 20:18 ` Serge Hallyn (AMD)
2026-11-24 22:58 ` [PATCH RFC 03/19] perf: copy counter values after dropping ctx->mutex Christian Brauner
2026-12-03 20:18 ` Serge Hallyn (AMD)
2026-11-24 22:58 ` [PATCH RFC 04/19] perf: copy ioctl arguments without ctx->mutex held Christian Brauner
2026-12-03 20:18 ` Serge Hallyn (AMD) [this message]
2026-11-24 22:58 ` [PATCH RFC 05/19] selinux: close inherited files that can't be replaced Christian Brauner
2026-12-03 20:19 ` Serge Hallyn (AMD)
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 23:49 ` bot+bpf-ci
2026-12-02 0:09 ` Paul Moore
2026-11-24 22:58 ` [PATCH RFC 12/19] apparmor: " Christian Brauner
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=axHOsq0ohP5JnXEP@shallyn-amd \
--to=sergeh@kernel.org \
--cc=acme@kernel.org \
--cc=apparmor@lists.ubuntu.com \
--cc=bpf@vger.kernel.org \
--cc=brauner@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