| 1 |
// SPDX-License-Identifier: GPL-2.1-or-later |
| 2 |
/* |
| 3 |
* cap-audit - Trace a target process to discover required capabilities. |
| 4 |
* Copyright (c) 2026 Red Hat Inc. |
| 5 |
* All Rights Reserved. |
| 6 |
* |
| 7 |
* This software may be freely redistributed and/or modified under the |
| 8 |
* terms of the GNU General Public License as published by the Free |
| 9 |
* Software Foundation; either version 2, or (at your option) any |
| 10 |
* later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* Authors: |
| 18 |
* Steve Grubb <[email protected]> |
| 19 |
* Portions of this code were made with codex 5.2 |
| 20 |
*/ |
| 21 |
|
| 22 |
#include "cap_audit.h" |
| 23 |
|
| 24 |
#include <bpf/bpf.h> |
| 25 |
#include <errno.h> |
| 26 |
#include <libaudit.h> |
| 27 |
#include <limits.h> |
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
#include <sys/resource.h> |
| 32 |
#include <sys/wait.h> |
| 33 |
#include <unistd.h> |
| 34 |
|
| 35 |
/* |
| 36 |
* Overview: |
| 37 |
* cap-audit launches a target application, traces that process tree using |
| 38 |
* eBPF hooks, and reports which Linux capabilities were actually exercised. |
| 39 |
* The userspace side performs three major jobs: |
| 40 |
* |
| 41 |
* (1) prepare the runtime environment by checking our own capabilities and |
| 42 |
* raising rlimits; |
| 43 |
* (2) coordinate with the eBPF program by registering the target PID before |
| 44 |
* exec() and consuming capability check events from the ring buffer; and |
| 45 |
* (3) analyze collected data to present required, conditional, and denied |
| 46 |
* capabilities in human and machine-readable formats. |
| 47 |
* |
| 48 |
* When the tool observes a capset syscall from the initial PID, it splits |
| 49 |
* capability accounting into initialization and operational phases. The |
| 50 |
* initialization phase covers all capability checks from process start to |
| 51 |
* the first capset. The operational phase covers everything after. This |
| 52 |
* separation allows the tool to distinguish capabilities needed for one-time |
| 53 |
* setup (binding privileged ports, chroot, loading restricted configuration) |
| 54 |
* from capabilities needed for ongoing operation. Recommendations for |
| 55 |
* programmatic capability dropping use only the operational set, while |
| 56 |
* deployment recommendations (file capabilities, systemd, containers) use |
| 57 |
* the union since the process must start with sufficient capabilities for |
| 58 |
* initialization. Programs that never call capset produce an undifferentiated |
| 59 |
* report identical to previous versions. |
| 60 |
* |
| 61 |
* Core design problem: |
| 62 |
* when tracing as root, many capability checks come from kernel-internal |
| 63 |
* work under the same PID rather than from app logic. The tool uses a |
| 64 |
* two-layer noise filter pipeline, split between BPF and userspace, to |
| 65 |
* separate real requirements from incidental checks. |
| 66 |
* |
| 67 |
* Layer 1 - pre-exec noise (BPF phase gate): |
| 68 |
* after fork() but before execve() completes, the child is still this |
| 69 |
* auditor image. PATH lookup, directory traversal, and exec machinery can |
| 70 |
* trigger checks like DAC_READ_SEARCH/SYS_ADMIN/SETPCAP that are unrelated |
| 71 |
* to the target app. The BPF side tracks PID phases and suppresses all |
| 72 |
* capability events until sched_process_exec transitions the PID from |
| 73 |
* phase 1 (pre-exec) to phase 2 (post-exec). |
| 74 |
* |
| 75 |
* Layer 2 - userspace always-noise filter: |
| 76 |
* handle_cap_event() unconditionally drops two classes of capability |
| 77 |
* checks that never represent real application requirements. First, |
| 78 |
* execve-triggered SYS_ADMIN and SETPCAP checks are kernel-internal |
| 79 |
* credential-transition noise. Second, cap-audit identifies the known |
| 80 |
* advisory memory overcommit probe by combining syscall, capability, and |
| 81 |
* CAP_OPT_NOAUDIT: cap_vm_enough_memory checks CAP_SYS_ADMIN on |
| 82 |
* brk/mmap/mprotect/mremap and the operation succeeds regardless of the |
| 83 |
* result. CAP_OPT_NOAUDIT means "do not audit", not "advisory", so it |
| 84 |
* is only a confirming signal here. Other enforcement checks also use |
| 85 |
* CAP_OPT_NOAUDIT to avoid audit spam, and must still be reported. The |
| 86 |
* syscall + capability match identifies the specific advisory call site, |
| 87 |
* while CAP_OPT_NOAUDIT confirms that the event followed the advisory path |
| 88 |
* rather than a security-gating path on the same syscall. |
| 89 |
* |
| 90 |
* Layer 3 - final drain shutdown backstop: |
| 91 |
* after waitpid() reports that the initial PID has exited, cap-audit does a |
| 92 |
* short final ring-buffer drain before analyzing results. That drain should |
| 93 |
* stay conservative because queued late SYS_ADMIN/SETPCAP checks from the |
| 94 |
* exiting interpreter/runtime are more likely to be teardown chatter than a |
| 95 |
* meaningful application requirement. The shutdown backstop is intentionally |
| 96 |
* limited to the initial PID and only applies during that final drain. |
| 97 |
* |
| 98 |
* PID filtering remains central: parent registers the child immediately after |
| 99 |
* fork(), BPF follows forks/exits to keep the target set precise, and each |
| 100 |
* event carries capability, syscall context, namespace info, and result for |
| 101 |
* per-capability aggregation. |
| 102 |
* |
| 103 |
* Parent/child startup is synchronized with a pipe. The child blocks in |
| 104 |
* read(sync_pipe) until the parent has inserted its PID in target_pids and |
| 105 |
* written a go byte. That ordering prevents missed events and prevents events |
| 106 |
* from arriving before the PID is registered. |
| 107 |
*/ |
| 108 |
|
| 109 |
struct audit_state state; |
| 110 |
int audit_machine = -1; |
| 111 |
|
| 112 |
static void sig_handler(int sig __attribute__((unused))) |
| 113 |
{ |
| 114 |
state.stop = 1; |
| 115 |
} |
| 116 |
|
| 117 |
static int set_memlock_rlimit(void) |
| 118 |
{ |
| 119 |
struct rlimit rlim_new = { |
| 120 |
.rlim_cur = RLIM_INFINITY, |
| 121 |
.rlim_max = RLIM_INFINITY, |
| 122 |
}; |
| 123 |
|
| 124 |
if (setrlimit(RLIMIT_MEMLOCK, &rlim_new) < 0) { |
| 125 |
fprintf(stderr, "Error: Failed to raise memlock rlimit: %s\n", |
| 126 |
errno == EPERM ? |
| 127 |
"insufficient privileges" : strerror(errno)); |
| 128 |
return -1; |
| 129 |
} |
| 130 |
|
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
static int init_capng(void) |
| 135 |
{ |
| 136 |
capng_clear(CAPNG_SELECT_BOTH); |
| 137 |
|
| 138 |
if (capng_get_caps_process() != 0) { |
| 139 |
fprintf(stderr, "Error: Failed to get process capabilities\n"); |
| 140 |
return -1; |
| 141 |
} |
| 142 |
|
| 143 |
return 0; |
| 144 |
} |
| 145 |
|
| 146 |
static int check_auditor_caps(void) |
| 147 |
{ |
| 148 |
if (!capng_have_capability(CAPNG_EFFECTIVE, CAP_BPF) && |
| 149 |
!capng_have_capability(CAPNG_EFFECTIVE, CAP_SYS_ADMIN)) { |
| 150 |
fprintf( |
| 151 |
stderr, |
| 152 |
"Error: Need CAP_BPF or CAP_SYS_ADMIN to run auditor\n"); |
| 153 |
return -1; |
| 154 |
} |
| 155 |
|
| 156 |
if (!capng_have_capability(CAPNG_EFFECTIVE, CAP_PERFMON) && |
| 157 |
!capng_have_capability(CAPNG_EFFECTIVE, CAP_SYS_ADMIN)) { |
| 158 |
fprintf(stderr, "Error: Need CAP_PERFMON or CAP_SYS_ADMIN for " |
| 159 |
"perf events\n"); |
| 160 |
return -1; |
| 161 |
} |
| 162 |
|
| 163 |
if (!capng_have_capability(CAPNG_EFFECTIVE, CAP_SYS_PTRACE)) |
| 164 |
fprintf(stderr, "Warning: CAP_SYS_PTRACE not available, stack " |
| 165 |
"traces may be limited\n"); |
| 166 |
|
| 167 |
return 0; |
| 168 |
} |
| 169 |
|
| 170 |
static int set_target_pid(pid_t pid) |
| 171 |
{ |
| 172 |
int map_fd; |
| 173 |
__u8 val = 1; |
| 174 |
|
| 175 |
map_fd = bpf_map__fd(state.skel->maps.target_pids); |
| 176 |
if (map_fd < 0) { |
| 177 |
fprintf(stderr, "Error: Failed to get target_pids map fd\n"); |
| 178 |
return -1; |
| 179 |
} |
| 180 |
|
| 181 |
if (bpf_map_update_elem(map_fd, &pid, &val, BPF_ANY) != 0) { |
| 182 |
fprintf(stderr, "Error: Failed to register target PID %d: %s\n", |
| 183 |
pid, strerror(errno)); |
| 184 |
return -1; |
| 185 |
} |
| 186 |
|
| 187 |
if (state.verbose) |
| 188 |
printf("[*] Registered PID %d for tracing\n", pid); |
| 189 |
|
| 190 |
return 0; |
| 191 |
} |
| 192 |
|
| 193 |
static void usage(FILE *out, const char *prog) |
| 194 |
{ |
| 195 |
fprintf(out, "Usage: %s [options] -- command [args...]\n", prog); |
| 196 |
fprintf(out, "Options:\n"); |
| 197 |
fprintf(out, " -h, --help Show this help message\n"); |
| 198 |
fprintf(out, " -v, --verbose Verbose output\n"); |
| 199 |
fprintf(out, " -j, --json JSON output\n"); |
| 200 |
fprintf(out, " -y, --yaml YAML output\n"); |
| 201 |
} |
| 202 |
|
| 203 |
int main(int argc, char **argv) |
| 204 |
{ |
| 205 |
int err; |
| 206 |
int arg_idx; |
| 207 |
pid_t child; |
| 208 |
pid_t ret_pid; |
| 209 |
int wstatus; |
| 210 |
|
| 211 |
if (argc < 2) { |
| 212 |
usage(stderr, argv[0]); |
| 213 |
return 1; |
| 214 |
} |
| 215 |
|
| 216 |
arg_idx = 1; |
| 217 |
while (arg_idx < argc && argv[arg_idx][0] == '-') { |
| 218 |
if (!strcmp(argv[arg_idx], "-h") || |
| 219 |
!strcmp(argv[arg_idx], "--help")) { |
| 220 |
usage(stdout, argv[0]); |
| 221 |
return 0; |
| 222 |
} else if (!strcmp(argv[arg_idx], "-v") || |
| 223 |
!strcmp(argv[arg_idx], "--verbose")) |
| 224 |
state.verbose = 1; |
| 225 |
else if (!strcmp(argv[arg_idx], "-j") || |
| 226 |
!strcmp(argv[arg_idx], "--json")) |
| 227 |
state.json_output = 1; |
| 228 |
else if (!strcmp(argv[arg_idx], "-y") || |
| 229 |
!strcmp(argv[arg_idx], "--yaml")) |
| 230 |
state.yaml_output = 1; |
| 231 |
else if (!strcmp(argv[arg_idx], "--")) { |
| 232 |
arg_idx++; |
| 233 |
break; |
| 234 |
} else { |
| 235 |
fprintf(stderr, "Error: Unknown option '%s'\n", |
| 236 |
argv[arg_idx]); |
| 237 |
usage(stderr, argv[0]); |
| 238 |
return 1; |
| 239 |
} |
| 240 |
arg_idx++; |
| 241 |
} |
| 242 |
|
| 243 |
if (arg_idx >= argc) { |
| 244 |
fprintf(stderr, "Error: No command specified\n"); |
| 245 |
usage(stderr, argv[0]); |
| 246 |
return 1; |
| 247 |
} |
| 248 |
|
| 249 |
state.target_argv = &argv[arg_idx]; |
| 250 |
|
| 251 |
if (init_capng() != 0) |
| 252 |
return 1; |
| 253 |
|
| 254 |
if (check_auditor_caps() != 0) |
| 255 |
return 1; |
| 256 |
|
| 257 |
if (set_memlock_rlimit() != 0) |
| 258 |
return 1; |
| 259 |
|
| 260 |
state.app.exe = strdup(state.target_argv[0]); |
| 261 |
state.app.prog_type = UNSUPPORTED; |
| 262 |
if (audit_machine < 0) |
| 263 |
audit_machine = audit_detect_machine(); |
| 264 |
if (audit_machine < 0) { |
| 265 |
fprintf(stderr, |
| 266 |
"Error: unable to determine hardware architecture for syscall lookup. Exiting.\n"); |
| 267 |
return 1; |
| 268 |
} |
| 269 |
state.app.execve_nr = audit_name_to_syscall("execve", audit_machine); |
| 270 |
state.app.mmap_nr = audit_name_to_syscall("mmap", audit_machine); |
| 271 |
state.app.brk_nr = audit_name_to_syscall("brk", audit_machine); |
| 272 |
state.app.mprotect_nr = audit_name_to_syscall("mprotect", |
| 273 |
audit_machine); |
| 274 |
state.app.mremap_nr = audit_name_to_syscall("mremap", audit_machine); |
| 275 |
state.app.capset_nr = audit_name_to_syscall("capset", audit_machine); |
| 276 |
|
| 277 |
state.skel = cap_audit_bpf__open_and_load(); |
| 278 |
if (!state.skel) { |
| 279 |
fprintf(stderr, "Error: Failed to load BPF program: %s\n", |
| 280 |
strerror(errno)); |
| 281 |
return 1; |
| 282 |
} |
| 283 |
|
| 284 |
err = cap_audit_bpf__attach(state.skel); |
| 285 |
if (err) { |
| 286 |
fprintf(stderr, "Error: Failed to attach BPF programs: %s\n", |
| 287 |
strerror(-err)); |
| 288 |
cap_audit_bpf__destroy(state.skel); |
| 289 |
return 1; |
| 290 |
} |
| 291 |
|
| 292 |
state.rb = ring_buffer__new(bpf_map__fd(state.skel->maps.cap_events), |
| 293 |
handle_cap_event, NULL, NULL); |
| 294 |
if (!state.rb) { |
| 295 |
fprintf(stderr, "Error: Failed to create ring buffer: %s\n", |
| 296 |
strerror(errno)); |
| 297 |
cap_audit_bpf__destroy(state.skel); |
| 298 |
return 1; |
| 299 |
} |
| 300 |
|
| 301 |
printf("[*] Capability auditor started\n"); |
| 302 |
|
| 303 |
if (pipe(state.sync_pipe) != 0) { |
| 304 |
fprintf(stderr, "Error: pipe failed: %s\n", strerror(errno)); |
| 305 |
ring_buffer__free(state.rb); |
| 306 |
cap_audit_bpf__destroy(state.skel); |
| 307 |
return 1; |
| 308 |
} |
| 309 |
|
| 310 |
child = fork(); |
| 311 |
if (child == 0) { |
| 312 |
char sync_byte; |
| 313 |
ssize_t bytes; |
| 314 |
|
| 315 |
close(state.sync_pipe[1]); |
| 316 |
bytes = read(state.sync_pipe[0], &sync_byte, 1); |
| 317 |
if (bytes != 1) { |
| 318 |
if (bytes < 0) |
| 319 |
perror("read"); |
| 320 |
else |
| 321 |
fprintf(stderr, |
| 322 |
"Error: failed to sync with parent\n"); |
| 323 |
close(state.sync_pipe[0]); |
| 324 |
exit(1); |
| 325 |
} |
| 326 |
close(state.sync_pipe[0]); |
| 327 |
execvp(state.target_argv[0], state.target_argv); |
| 328 |
perror("execvp"); |
| 329 |
exit(1); |
| 330 |
} else if (child < 0) { |
| 331 |
fprintf(stderr, "Error: fork failed: %s\n", strerror(errno)); |
| 332 |
close(state.sync_pipe[0]); |
| 333 |
close(state.sync_pipe[1]); |
| 334 |
ring_buffer__free(state.rb); |
| 335 |
cap_audit_bpf__destroy(state.skel); |
| 336 |
return 1; |
| 337 |
} |
| 338 |
|
| 339 |
state.app.pid = child; |
| 340 |
|
| 341 |
close(state.sync_pipe[0]); |
| 342 |
if (set_target_pid(child) != 0) { |
| 343 |
close(state.sync_pipe[1]); |
| 344 |
kill(child, SIGKILL); |
| 345 |
waitpid(child, NULL, 0); |
| 346 |
ring_buffer__free(state.rb); |
| 347 |
cap_audit_bpf__destroy(state.skel); |
| 348 |
free(state.app.exe); |
| 349 |
return 1; |
| 350 |
} |
| 351 |
|
| 352 |
if (write(state.sync_pipe[1], "1", 1) != 1) { |
| 353 |
fprintf(stderr, "Error: write failed: %s\n", strerror(errno)); |
| 354 |
close(state.sync_pipe[1]); |
| 355 |
kill(child, SIGKILL); |
| 356 |
waitpid(child, NULL, 0); |
| 357 |
ring_buffer__free(state.rb); |
| 358 |
cap_audit_bpf__destroy(state.skel); |
| 359 |
free(state.app.exe); |
| 360 |
return 1; |
| 361 |
} |
| 362 |
close(state.sync_pipe[1]); |
| 363 |
|
| 364 |
{ |
| 365 |
char resolved[PATH_MAX]; |
| 366 |
|
| 367 |
if (resolve_target_exe(child, resolved, sizeof(resolved)) == 0) { |
| 368 |
char *resolved_dup = strdup(resolved); |
| 369 |
|
| 370 |
if (resolved_dup) { |
| 371 |
free(state.app.exe); |
| 372 |
state.app.exe = resolved_dup; |
| 373 |
} |
| 374 |
state.app.prog_type = classify_app(state.app.exe); |
| 375 |
} else { |
| 376 |
fprintf(stderr, |
| 377 |
"Warning: unable to resolve target path\n"); |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
read_system_state(&state.app); |
| 382 |
inspect_target_file_caps(child); |
| 383 |
if (state.app.prog_type == PYTHON && state.verbose) |
| 384 |
printf("[*] Script interpreter: %s\n", state.app.exe); |
| 385 |
printf("[*] Tracing application: %s (PID %d)\n", state.app.exe, child); |
| 386 |
printf("[*] Press Ctrl-C to stop\n\n"); |
| 387 |
|
| 388 |
signal(SIGINT, sig_handler); |
| 389 |
signal(SIGTERM, sig_handler); |
| 390 |
|
| 391 |
while (!state.stop) { |
| 392 |
err = ring_buffer__poll(state.rb, 100); |
| 393 |
if (err < 0 && err != -EINTR) { |
| 394 |
fprintf(stderr, "Error polling ring buffer: %s\n", |
| 395 |
strerror(-err)); |
| 396 |
break; |
| 397 |
} |
| 398 |
|
| 399 |
ret_pid = waitpid(child, &wstatus, WNOHANG); |
| 400 |
if (ret_pid == child) { |
| 401 |
if (WIFEXITED(wstatus)) |
| 402 |
printf( |
| 403 |
"\n[*] Application exited with status %d\n", |
| 404 |
WEXITSTATUS(wstatus)); |
| 405 |
else if (WIFSIGNALED(wstatus)) |
| 406 |
printf( |
| 407 |
"\n[*] Application terminated by signal %d\n", |
| 408 |
WTERMSIG(wstatus)); |
| 409 |
break; |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
printf("[*] Analyzing results...\n"); |
| 414 |
|
| 415 |
state.shutting_down = 1; |
| 416 |
usleep(100000); |
| 417 |
ring_buffer__poll(state.rb, 0); |
| 418 |
|
| 419 |
if (state.json_output) |
| 420 |
output_json(); |
| 421 |
else if (state.yaml_output) |
| 422 |
output_yaml(); |
| 423 |
else |
| 424 |
analyze_capabilities(); |
| 425 |
|
| 426 |
ring_buffer__free(state.rb); |
| 427 |
cap_audit_bpf__destroy(state.skel); |
| 428 |
free(state.app.exe); |
| 429 |
|
| 430 |
for (int i = 0; i <= CAP_LAST_CAP; i++) { |
| 431 |
if (state.app.checks[i].reason) |
| 432 |
free(state.app.checks[i].reason); |
| 433 |
if (state.app.checks[i].op_reason) |
| 434 |
free(state.app.checks[i].op_reason); |
| 435 |
if (state.app.checks[i].denied_syscalls) |
| 436 |
free(state.app.checks[i].denied_syscalls); |
| 437 |
} |
| 438 |
|
| 439 |
return 0; |
| 440 |
} |