From: Dmitry Vyukov <dvyukov@google.com>
To: Marco Elver <elver@google.com>
Cc: peterz@infradead.org, tglx@linutronix.de, mingo@kernel.org,
glider@google.com, kasan-dev@googlegroups.com,
linux-kernel@vger.kernel.org, mingo@redhat.com, acme@kernel.org,
mark.rutland@arm.com, alexander.shishkin@linux.intel.com,
jolsa@redhat.com, namhyung@kernel.org,
linux-perf-users@vger.kernel.org, ebiederm@xmission.com,
omosnace@redhat.com, serge@hallyn.com,
linux-security-module@vger.kernel.org
Subject: Re: [PATCH v3 2/2] perf: Refactor permissions check into perf_check_permission()
Date: Tue, 6 Jul 2021 08:16:39 +0200 [thread overview]
Message-ID: <CACT4Y+ZjjuW5wZ-QsYj7btZYYyNEiSnGh6JtV3bmSNx9mY_bZw@mail.gmail.com> (raw)
In-Reply-To: <20210705084453.2151729-2-elver@google.com>
On Mon, Jul 5, 2021 at 10:45 AM Marco Elver <elver@google.com> wrote:
>
> Refactor the permission check in perf_event_open() into a helper
> perf_check_permission(). This makes the permission check logic more
> readable (because we no longer have a negated disjunction). Add a
> comment mentioning the ptrace check also checks the uid.
>
> No functional change intended.
>
> Signed-off-by: Marco Elver <elver@google.com>
Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
> ---
> v3:
> * Introduce this patch to refactor the permissions checking logic to
> make it more readable (reported by Eric W. Biederman).
> ---
> kernel/events/core.c | 58 ++++++++++++++++++++++++--------------------
> 1 file changed, 32 insertions(+), 26 deletions(-)
>
> diff --git a/kernel/events/core.c b/kernel/events/core.c
> index f79ee82e644a..3008b986994b 100644
> --- a/kernel/events/core.c
> +++ b/kernel/events/core.c
> @@ -11912,6 +11912,37 @@ __perf_event_ctx_lock_double(struct perf_event *group_leader,
> return gctx;
> }
>
> +static bool
> +perf_check_permission(struct perf_event_attr *attr, struct task_struct *task)
> +{
> + unsigned int ptrace_mode = PTRACE_MODE_READ_REALCREDS;
> + bool is_capable = perfmon_capable();
> +
> + if (attr->sigtrap) {
> + /*
> + * perf_event_attr::sigtrap sends signals to the other task.
> + * Require the current task to also have CAP_KILL.
> + */
> + rcu_read_lock();
> + is_capable &= ns_capable(__task_cred(task)->user_ns, CAP_KILL);
> + rcu_read_unlock();
> +
> + /*
> + * If the required capabilities aren't available, checks for
> + * ptrace permissions: upgrade to ATTACH, since sending signals
> + * can effectively change the target task.
> + */
> + ptrace_mode = PTRACE_MODE_ATTACH_REALCREDS;
> + }
> +
> + /*
> + * Preserve ptrace permission check for backwards compatibility. The
> + * ptrace check also includes checks that the current task and other
> + * task have matching uids, and is therefore not done here explicitly.
> + */
> + return is_capable || ptrace_may_access(task, ptrace_mode);
> +}
> +
> /**
> * sys_perf_event_open - open a performance event, associate it to a task/cpu
> *
> @@ -12152,43 +12183,18 @@ SYSCALL_DEFINE5(perf_event_open,
> }
>
> if (task) {
> - unsigned int ptrace_mode = PTRACE_MODE_READ_REALCREDS;
> - bool is_capable;
> -
> err = down_read_interruptible(&task->signal->exec_update_lock);
> if (err)
> goto err_file;
>
> - is_capable = perfmon_capable();
> - if (attr.sigtrap) {
> - /*
> - * perf_event_attr::sigtrap sends signals to the other
> - * task. Require the current task to also have
> - * CAP_KILL.
> - */
> - rcu_read_lock();
> - is_capable &= ns_capable(__task_cred(task)->user_ns, CAP_KILL);
> - rcu_read_unlock();
> -
> - /*
> - * If the required capabilities aren't available, checks
> - * for ptrace permissions: upgrade to ATTACH, since
> - * sending signals can effectively change the target
> - * task.
> - */
> - ptrace_mode = PTRACE_MODE_ATTACH_REALCREDS;
> - }
> -
> /*
> - * Preserve ptrace permission check for backwards compatibility.
> - *
> * We must hold exec_update_lock across this and any potential
> * perf_install_in_context() call for this new event to
> * serialize against exec() altering our credentials (and the
> * perf_event_exit_task() that could imply).
> */
> err = -EACCES;
> - if (!is_capable && !ptrace_may_access(task, ptrace_mode))
> + if (!perf_check_permission(&attr, task))
> goto err_cred;
> }
>
> --
> 2.32.0.93.g670b81a890-goog
>
next prev parent reply other threads:[~2021-07-06 6:16 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-07-05 8:44 [PATCH v3 1/2] perf: Fix required permissions if sigtrap is requested Marco Elver
2021-07-05 8:44 ` [PATCH v3 2/2] perf: Refactor permissions check into perf_check_permission() Marco Elver
2021-07-06 6:16 ` Dmitry Vyukov [this message]
2021-07-27 13:58 ` [tip: perf/urgent] " tip-bot2 for Marco Elver
2021-07-06 6:16 ` [PATCH v3 1/2] perf: Fix required permissions if sigtrap is requested Dmitry Vyukov
2021-07-12 10:32 ` Marco Elver
2021-07-13 9:48 ` Peter Zijlstra
2021-07-27 13:58 ` [tip: perf/urgent] " tip-bot2 for Marco Elver
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=CACT4Y+ZjjuW5wZ-QsYj7btZYYyNEiSnGh6JtV3bmSNx9mY_bZw@mail.gmail.com \
--to=dvyukov@google.com \
--cc=acme@kernel.org \
--cc=alexander.shishkin@linux.intel.com \
--cc=ebiederm@xmission.com \
--cc=elver@google.com \
--cc=glider@google.com \
--cc=jolsa@redhat.com \
--cc=kasan-dev@googlegroups.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-perf-users@vger.kernel.org \
--cc=linux-security-module@vger.kernel.org \
--cc=mark.rutland@arm.com \
--cc=mingo@kernel.org \
--cc=mingo@redhat.com \
--cc=namhyung@kernel.org \
--cc=omosnace@redhat.com \
--cc=peterz@infradead.org \
--cc=serge@hallyn.com \
--cc=tglx@linutronix.de \
/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).