Skip to content

Commit

Permalink
perf: Fix required permissions if sigtrap is requested
Browse files Browse the repository at this point in the history
If perf_event_open() is called with another task as target and
perf_event_attr::sigtrap is set, and the target task's user does not
match the calling user, also require the CAP_KILL capability or
PTRACE_MODE_ATTACH permissions.

Otherwise, with the CAP_PERFMON capability alone it would be possible
for a user to send SIGTRAP signals via perf events to another user's
tasks. This could potentially result in those tasks being terminated if
they cannot handle SIGTRAP signals.

Note: The check complements the existing capability check, but is not
supposed to supersede the ptrace_may_access() check. At a high level we
now have:

	capable of CAP_PERFMON and (CAP_KILL if sigtrap)
		OR
	ptrace_may_access(...) // also checks for same thread-group and uid

Fixes: 97ba62b ("perf: Add support for SIGTRAP on perf events")
Reported-by: Dmitry Vyukov <[email protected]>
Signed-off-by: Marco Elver <[email protected]>
Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Acked-by: Dmitry Vyukov <[email protected]>
Cc: <[email protected]> # 5.13+
Link: https://lore.kernel.org/r/[email protected]
  • Loading branch information
melver authored and Peter Zijlstra committed Jul 16, 2021
1 parent 7fef2ed commit 9d7a6c9
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion kernel/events/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -12158,10 +12158,33 @@ 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.
*
Expand All @@ -12171,7 +12194,7 @@ SYSCALL_DEFINE5(perf_event_open,
* perf_event_exit_task() that could imply).
*/
err = -EACCES;
if (!perfmon_capable() && !ptrace_may_access(task, PTRACE_MODE_READ_REALCREDS))
if (!is_capable && !ptrace_may_access(task, ptrace_mode))
goto err_cred;
}

Expand Down

0 comments on commit 9d7a6c9

Please sign in to comment.