Skip to content

Commit 65906a9

Browse files
committed
Make a best-effort attempt to pass CAP_PERFMON into tracees if rr has it.
Syscallbuf needs this to open its desched perf counter.
1 parent efa5fe8 commit 65906a9

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

src/Task.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3207,6 +3207,26 @@ static void set_up_process(Session& session, const ScopedFd& err_fd,
32073207
/* TODO tracees can probably undo some of the setup below
32083208
* ... */
32093209

3210+
struct NativeArch::cap_header header = {.version =
3211+
_LINUX_CAPABILITY_VERSION_3,
3212+
.pid = 0 };
3213+
struct NativeArch::cap_data caps[2];
3214+
if (syscall(NativeArch::capget, &header, &caps) != 0) {
3215+
spawned_child_fatal_error(err_fd, "Failed to read capabilities");
3216+
}
3217+
uint32_t perfmon_mask = 1 << (CAP_PERFMON - 32);
3218+
if (caps[1].permitted & perfmon_mask) {
3219+
// Try to pass CAP_PERFMON into our tracees.
3220+
caps[1].inheritable |= perfmon_mask;
3221+
// Ignore any failures here. Capabilities are super complex and I'm not
3222+
// sure this can be trusted to succeed.
3223+
if (syscall(NativeArch::capset, &header, &caps) == 0) {
3224+
// Install CAP_PERFMON as an ambient capabilities.
3225+
// This prctl was only added in 4.3. Ignore failures.
3226+
prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_PERFMON, 0, 0);
3227+
}
3228+
}
3229+
32103230
/* CLOEXEC so that the original fd here will be closed by the exec that's
32113231
* about to happen.
32123232
*/

0 commit comments

Comments
 (0)