| 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 |
/* Shared utility helpers: target inspection, syscall/cap name lookup, |
| 23 |
* output escaping, and common capability aggregation functions. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "cap_audit.h" |
| 27 |
|
| 28 |
#include <errno.h> |
| 29 |
#include <fcntl.h> |
| 30 |
#include <libaudit.h> |
| 31 |
#include <limits.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <string.h> |
| 35 |
#include <sys/stat.h> |
| 36 |
#include <unistd.h> |
| 37 |
|
| 38 |
int resolve_target_exe(pid_t pid, char *exepath, size_t exepath_len) |
| 39 |
{ |
| 40 |
char linkpath[64]; |
| 41 |
char selfpath[PATH_MAX]; |
| 42 |
ssize_t len; |
| 43 |
ssize_t self_len; |
| 44 |
int tries = 50; |
| 45 |
|
| 46 |
if (snprintf(linkpath, sizeof(linkpath), "/proc/%d/exe", pid) < 0) |
| 47 |
return -1; |
| 48 |
|
| 49 |
self_len = readlink("/proc/self/exe", selfpath, sizeof(selfpath) - 1); |
| 50 |
if (self_len >= 0) |
| 51 |
selfpath[self_len] = '\0'; |
| 52 |
|
| 53 |
while (tries--) { |
| 54 |
len = readlink(linkpath, exepath, exepath_len - 1); |
| 55 |
if (len < 0) { |
| 56 |
fprintf(stderr, "Warning: readlink(%s) failed: %s\n", |
| 57 |
linkpath, strerror(errno)); |
| 58 |
return -1; |
| 59 |
} |
| 60 |
exepath[len] = '\0'; |
| 61 |
|
| 62 |
if (self_len < 0 || strcmp(exepath, selfpath) != 0) |
| 63 |
break; |
| 64 |
|
| 65 |
if (tries == 0) |
| 66 |
fprintf(stderr, |
| 67 |
"Warning: %s still points to auditor binary (%s)\n", |
| 68 |
linkpath, exepath); |
| 69 |
usleep(10000); |
| 70 |
} |
| 71 |
|
| 72 |
return 0; |
| 73 |
} |
| 74 |
|
| 75 |
int inspect_target_file_caps(pid_t pid) |
| 76 |
{ |
| 77 |
char exepath[PATH_MAX]; |
| 78 |
int fd; |
| 79 |
struct stat st; |
| 80 |
capng_results_t caps; |
| 81 |
|
| 82 |
state.app.file_caps = 0; |
| 83 |
state.app.file_setpcap = 0; |
| 84 |
|
| 85 |
if (resolve_target_exe(pid, exepath, sizeof(exepath)) < 0) |
| 86 |
return -1; |
| 87 |
|
| 88 |
fd = open(exepath, O_RDONLY | O_CLOEXEC); |
| 89 |
if (fd < 0) { |
| 90 |
fprintf(stderr, "Warning: open(%s) failed: %s\n", |
| 91 |
exepath, strerror(errno)); |
| 92 |
return -1; |
| 93 |
} |
| 94 |
|
| 95 |
if (fstat(fd, &st) < 0) { |
| 96 |
fprintf(stderr, "Warning: fstat(%s) failed: %s\n", |
| 97 |
exepath, strerror(errno)); |
| 98 |
close(fd); |
| 99 |
return -1; |
| 100 |
} |
| 101 |
if (!S_ISREG(st.st_mode)) { |
| 102 |
fprintf(stderr, "Warning: %s is not a regular file\n", |
| 103 |
exepath); |
| 104 |
close(fd); |
| 105 |
return -1; |
| 106 |
} |
| 107 |
|
| 108 |
capng_clear(CAPNG_SELECT_BOTH); |
| 109 |
if (capng_get_caps_fd(fd)) { |
| 110 |
if (errno != ENODATA) |
| 111 |
fprintf(stderr, |
| 112 |
"Warning: capng_get_caps_fd(%s) failed: %s\n", |
| 113 |
exepath, strerror(errno)); |
| 114 |
close(fd); |
| 115 |
if (capng_get_caps_process()) |
| 116 |
fprintf(stderr, |
| 117 |
"Warning: failed to restore process capabilities\n"); |
| 118 |
return -1; |
| 119 |
} |
| 120 |
close(fd); |
| 121 |
|
| 122 |
caps = capng_have_capabilities(CAPNG_SELECT_CAPS); |
| 123 |
if (caps == CAPNG_NONE) |
| 124 |
caps = capng_have_permitted_capabilities(); |
| 125 |
if (caps > CAPNG_NONE) |
| 126 |
state.app.file_caps = 1; |
| 127 |
|
| 128 |
if (capng_have_capability(CAPNG_PERMITTED, CAP_SETPCAP) || |
| 129 |
capng_have_capability(CAPNG_INHERITABLE, CAP_SETPCAP)) |
| 130 |
state.app.file_setpcap = 1; |
| 131 |
|
| 132 |
if (state.verbose) |
| 133 |
printf("[*] File caps source: %s (has_caps=%d setpcap=%d)\n", |
| 134 |
exepath, state.app.file_caps, state.app.file_setpcap); |
| 135 |
|
| 136 |
if (capng_get_caps_process()) |
| 137 |
fprintf(stderr, "Warning: failed to restore process capabilities\n"); |
| 138 |
|
| 139 |
return 0; |
| 140 |
} |
| 141 |
|
| 142 |
const char *cap_name_safe(int cap) |
| 143 |
{ |
| 144 |
const char *name = capng_capability_to_name(cap); |
| 145 |
|
| 146 |
return name ? name : "unknown"; |
| 147 |
} |
| 148 |
|
| 149 |
void read_sysctl(const char *path, int *value) |
| 150 |
{ |
| 151 |
FILE *f; |
| 152 |
|
| 153 |
f = fopen(path, "r"); |
| 154 |
if (f) { |
| 155 |
if (fscanf(f, "%d", value) != 1) |
| 156 |
*value = -1; |
| 157 |
fclose(f); |
| 158 |
} else { |
| 159 |
*value = -1; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
void read_system_state(struct app_caps *app) |
| 164 |
{ |
| 165 |
FILE *f; |
| 166 |
|
| 167 |
read_sysctl("/proc/sys/kernel/yama/ptrace_scope", |
| 168 |
&app->yama_ptrace_scope); |
| 169 |
read_sysctl("/proc/sys/kernel/kptr_restrict", &app->kptr_restrict); |
| 170 |
read_sysctl("/proc/sys/kernel/dmesg_restrict", &app->dmesg_restrict); |
| 171 |
read_sysctl("/proc/sys/kernel/modules_disabled", |
| 172 |
&app->modules_disabled); |
| 173 |
read_sysctl("/proc/sys/kernel/perf_event_paranoid", |
| 174 |
&app->perf_event_paranoid); |
| 175 |
read_sysctl("/proc/sys/kernel/unprivileged_bpf_disabled", |
| 176 |
&app->unprivileged_bpf_disabled); |
| 177 |
read_sysctl("/proc/sys/net/core/bpf_jit_enable", &app->bpf_jit_enable); |
| 178 |
read_sysctl("/proc/sys/net/core/bpf_jit_harden", &app->bpf_jit_harden); |
| 179 |
read_sysctl("/proc/sys/net/core/bpf_jit_kallsyms", |
| 180 |
&app->bpf_jit_kallsyms); |
| 181 |
read_sysctl("/proc/sys/vm/mmap_min_addr", &app->mmap_min_addr); |
| 182 |
read_sysctl("/proc/sys/fs/protected_hardlinks", |
| 183 |
&app->protected_hardlinks); |
| 184 |
read_sysctl("/proc/sys/fs/protected_symlinks", |
| 185 |
&app->protected_symlinks); |
| 186 |
read_sysctl("/proc/sys/fs/suid_dumpable", &app->suid_dumpable); |
| 187 |
|
| 188 |
f = fopen("/proc/sys/kernel/osrelease", "r"); |
| 189 |
if (f) { |
| 190 |
if (!fgets(app->kernel_version, sizeof(app->kernel_version), f)) |
| 191 |
app->kernel_version[0] = 0; |
| 192 |
app->kernel_version[strcspn(app->kernel_version, "\n")] = 0; |
| 193 |
fclose(f); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
const char *syscall_name_from_nr(int nr) |
| 198 |
{ |
| 199 |
if (audit_machine < 0) |
| 200 |
return NULL; |
| 201 |
|
| 202 |
return audit_syscall_to_name(nr, audit_machine); |
| 203 |
} |
| 204 |
|
| 205 |
char *json_escape(const char *input) |
| 206 |
{ |
| 207 |
size_t i; |
| 208 |
size_t needed = 0; |
| 209 |
char *out; |
| 210 |
char *pos; |
| 211 |
|
| 212 |
if (!input) |
| 213 |
return strdup(""); |
| 214 |
|
| 215 |
for (i = 0; input[i]; i++) { |
| 216 |
unsigned char c = input[i]; |
| 217 |
|
| 218 |
switch (c) { |
| 219 |
case '\"': |
| 220 |
case '\\': |
| 221 |
case '\b': |
| 222 |
case '\f': |
| 223 |
case '\n': |
| 224 |
case '\r': |
| 225 |
case '\t': |
| 226 |
needed += 2; |
| 227 |
break; |
| 228 |
default: |
| 229 |
if (c < 0x20) |
| 230 |
needed += 6; |
| 231 |
else |
| 232 |
needed++; |
| 233 |
break; |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
out = malloc(needed + 1); |
| 238 |
if (!out) |
| 239 |
return NULL; |
| 240 |
|
| 241 |
pos = out; |
| 242 |
for (i = 0; input[i]; i++) { |
| 243 |
unsigned char c = input[i]; |
| 244 |
|
| 245 |
switch (c) { |
| 246 |
case '\"': |
| 247 |
*pos++ = '\\'; |
| 248 |
*pos++ = '\"'; |
| 249 |
break; |
| 250 |
case '\\': |
| 251 |
*pos++ = '\\'; |
| 252 |
*pos++ = '\\'; |
| 253 |
break; |
| 254 |
case '\b': |
| 255 |
*pos++ = '\\'; |
| 256 |
*pos++ = 'b'; |
| 257 |
break; |
| 258 |
case '\f': |
| 259 |
*pos++ = '\\'; |
| 260 |
*pos++ = 'f'; |
| 261 |
break; |
| 262 |
case '\n': |
| 263 |
*pos++ = '\\'; |
| 264 |
*pos++ = 'n'; |
| 265 |
break; |
| 266 |
case '\r': |
| 267 |
*pos++ = '\\'; |
| 268 |
*pos++ = 'r'; |
| 269 |
break; |
| 270 |
case '\t': |
| 271 |
*pos++ = '\\'; |
| 272 |
*pos++ = 't'; |
| 273 |
break; |
| 274 |
default: |
| 275 |
if (c < 0x20) { |
| 276 |
snprintf(pos, 7, "\\u%04x", c); |
| 277 |
pos += 6; |
| 278 |
} else { |
| 279 |
*pos++ = c; |
| 280 |
} |
| 281 |
break; |
| 282 |
} |
| 283 |
} |
| 284 |
*pos = '\0'; |
| 285 |
|
| 286 |
return out; |
| 287 |
} |
| 288 |
|
| 289 |
void update_reason_to(char **target, int syscall_nr) |
| 290 |
{ |
| 291 |
const char *syscall_name; |
| 292 |
|
| 293 |
if (*target) |
| 294 |
free(*target); |
| 295 |
|
| 296 |
if (syscall_nr < 0) { |
| 297 |
if (asprintf(target, |
| 298 |
"Used during capability check (syscall unknown)") < 0) |
| 299 |
*target = NULL; |
| 300 |
return; |
| 301 |
} |
| 302 |
|
| 303 |
syscall_name = syscall_name_from_nr(syscall_nr); |
| 304 |
if (asprintf(target, "Used by %s", |
| 305 |
syscall_name ? syscall_name : "unknown") < 0) |
| 306 |
*target = NULL; |
| 307 |
} |
| 308 |
|
| 309 |
void update_reason(struct cap_check *check, int syscall_nr) |
| 310 |
{ |
| 311 |
update_reason_to(&check->reason, syscall_nr); |
| 312 |
} |
| 313 |
|
| 314 |
void update_reason_op(struct cap_check *check, int syscall_nr) |
| 315 |
{ |
| 316 |
update_reason_to(&check->op_reason, syscall_nr); |
| 317 |
} |
| 318 |
|
| 319 |
int cap_required_union(const struct cap_check *check) |
| 320 |
{ |
| 321 |
return check->granted > 0 || check->op_granted > 0; |
| 322 |
} |
| 323 |
|
| 324 |
unsigned long cap_total_checks(const struct cap_check *check) |
| 325 |
{ |
| 326 |
return check->count + check->op_count; |
| 327 |
} |
| 328 |
|
| 329 |
unsigned long cap_total_granted(const struct cap_check *check) |
| 330 |
{ |
| 331 |
return check->granted + check->op_granted; |
| 332 |
} |
| 333 |
|
| 334 |
unsigned long cap_total_denied(const struct cap_check *check) |
| 335 |
{ |
| 336 |
return check->denied + check->op_denied; |
| 337 |
} |
| 338 |
|
| 339 |
const char *cap_union_reason(const struct cap_check *check) |
| 340 |
{ |
| 341 |
if (check->reason) |
| 342 |
return check->reason; |
| 343 |
return check->op_reason; |
| 344 |
} |
| 345 |
|
| 346 |
type_t classify_app(const char *exe) |
| 347 |
{ |
| 348 |
int fd; |
| 349 |
ssize_t rc; |
| 350 |
char buf[257]; |
| 351 |
|
| 352 |
fd = open(exe, O_RDONLY); |
| 353 |
if (fd < 0) { |
| 354 |
fprintf(stderr, "Cannot open %s - %s\n", exe, strerror(errno)); |
| 355 |
exit(1); |
| 356 |
} |
| 357 |
|
| 358 |
rc = read(fd, buf, 256); |
| 359 |
close(fd); |
| 360 |
if (rc > 0) { |
| 361 |
buf[rc] = 0; |
| 362 |
if (buf[0] == '#' && buf[1] == '!') { |
| 363 |
char *ptr = strchr(buf, '\n'); |
| 364 |
|
| 365 |
if (ptr) |
| 366 |
*ptr = 0; |
| 367 |
if (strstr(buf, "python")) |
| 368 |
return PYTHON; |
| 369 |
} else if (strncmp(buf, ELFMAGIC, 4) == 0) |
| 370 |
return ELF; |
| 371 |
} |
| 372 |
|
| 373 |
return UNSUPPORTED; |
| 374 |
} |