| 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 |
/* Human-readable analysis output: capability summaries, deployment |
| 23 |
* recommendations, and wrapped terminal formatting helpers. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "cap_audit.h" |
| 27 |
|
| 28 |
#include <ctype.h> |
| 29 |
#include <limits.h> |
| 30 |
#include <stdarg.h> |
| 31 |
#include <stdio.h> |
| 32 |
#include <stdlib.h> |
| 33 |
#include <string.h> |
| 34 |
#include <sys/ioctl.h> |
| 35 |
#include <unistd.h> |
| 36 |
|
| 37 |
int include_cap_in_recommendations(int cap) |
| 38 |
{ |
| 39 |
if (cap == CAP_SETPCAP && state.app.file_caps && |
| 40 |
!state.app.file_setpcap) |
| 41 |
return 0; |
| 42 |
|
| 43 |
return 1; |
| 44 |
} |
| 45 |
|
| 46 |
static void cap_name_upper_buf(int cap, char *buf, size_t buf_len) |
| 47 |
{ |
| 48 |
const char *name = cap_name_safe(cap); |
| 49 |
size_t i; |
| 50 |
|
| 51 |
if (buf_len == 0) |
| 52 |
return; |
| 53 |
|
| 54 |
for (i = 0; name[i] && i + 1 < buf_len; i++) |
| 55 |
buf[i] = toupper((unsigned char)name[i]); |
| 56 |
buf[i] = '\0'; |
| 57 |
} |
| 58 |
|
| 59 |
static int get_output_width(void) |
| 60 |
{ |
| 61 |
struct winsize ws; |
| 62 |
const char *columns; |
| 63 |
long env_width; |
| 64 |
|
| 65 |
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) == 0 && ws.ws_col > 0) |
| 66 |
return ws.ws_col; |
| 67 |
|
| 68 |
columns = getenv("COLUMNS"); |
| 69 |
if (columns) { |
| 70 |
env_width = strtol(columns, NULL, 10); |
| 71 |
if (env_width >= 40 && env_width <= 400) |
| 72 |
return (int)env_width; |
| 73 |
} |
| 74 |
|
| 75 |
return 80; |
| 76 |
} |
| 77 |
|
| 78 |
static void print_rule(char ch) |
| 79 |
{ |
| 80 |
int i; |
| 81 |
int width = get_output_width(); |
| 82 |
|
| 83 |
if (width < 40) |
| 84 |
width = 40; |
| 85 |
|
| 86 |
for (i = 0; i < width; i++) |
| 87 |
putchar(ch); |
| 88 |
putchar('\n'); |
| 89 |
} |
| 90 |
|
| 91 |
static void print_wrapped_text(const char *indent, const char *text) |
| 92 |
{ |
| 93 |
size_t indent_len; |
| 94 |
char *cont_indent; |
| 95 |
int width; |
| 96 |
int content_width; |
| 97 |
const char *p; |
| 98 |
int line_len = 0; |
| 99 |
int first_line = 1; |
| 100 |
|
| 101 |
if (!text) { |
| 102 |
printf("%s\n", indent); |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
indent_len = strlen(indent); |
| 107 |
cont_indent = malloc(indent_len + 1); |
| 108 |
if (!cont_indent) { |
| 109 |
printf("%s%s\n", indent, text); |
| 110 |
return; |
| 111 |
} |
| 112 |
memset(cont_indent, ' ', indent_len); |
| 113 |
cont_indent[indent_len] = '\0'; |
| 114 |
|
| 115 |
width = get_output_width(); |
| 116 |
if (width < 40) |
| 117 |
width = 40; |
| 118 |
content_width = width - (int)indent_len; |
| 119 |
if (content_width < 16) |
| 120 |
content_width = 16; |
| 121 |
|
| 122 |
printf("%s", indent); |
| 123 |
p = text; |
| 124 |
while (*p) { |
| 125 |
size_t word_len; |
| 126 |
int need_space = line_len > 0; |
| 127 |
|
| 128 |
while (*p == ' ') |
| 129 |
p++; |
| 130 |
|
| 131 |
if (*p == '\n') { |
| 132 |
putchar('\n'); |
| 133 |
printf("%s", first_line ? cont_indent : cont_indent); |
| 134 |
line_len = 0; |
| 135 |
first_line = 0; |
| 136 |
p++; |
| 137 |
continue; |
| 138 |
} |
| 139 |
if (*p == '\0') |
| 140 |
break; |
| 141 |
|
| 142 |
word_len = strcspn(p, " \n"); |
| 143 |
if (need_space && |
| 144 |
line_len + 1 + (int)word_len > content_width) { |
| 145 |
putchar('\n'); |
| 146 |
printf("%s", cont_indent); |
| 147 |
line_len = 0; |
| 148 |
need_space = 0; |
| 149 |
first_line = 0; |
| 150 |
} |
| 151 |
if (need_space) { |
| 152 |
putchar(' '); |
| 153 |
line_len++; |
| 154 |
} |
| 155 |
fwrite(p, 1, word_len, stdout); |
| 156 |
line_len += word_len; |
| 157 |
p += word_len; |
| 158 |
} |
| 159 |
putchar('\n'); |
| 160 |
free(cont_indent); |
| 161 |
} |
| 162 |
|
| 163 |
static void print_wrappedf(const char *indent, const char *fmt, ...) |
| 164 |
{ |
| 165 |
va_list ap; |
| 166 |
char *buf; |
| 167 |
|
| 168 |
va_start(ap, fmt); |
| 169 |
if (vasprintf(&buf, fmt, ap) < 0) |
| 170 |
buf = NULL; |
| 171 |
va_end(ap); |
| 172 |
|
| 173 |
if (buf) { |
| 174 |
print_wrapped_text(indent, buf); |
| 175 |
free(buf); |
| 176 |
} else { |
| 177 |
print_wrapped_text(indent, "(formatting error)"); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
static int cap_in_programmatic_set(int cap) |
| 182 |
{ |
| 183 |
if (!include_cap_in_recommendations(cap)) |
| 184 |
return 0; |
| 185 |
|
| 186 |
if (state.capset_observed) |
| 187 |
return state.app.checks[cap].op_granted > 0; |
| 188 |
|
| 189 |
return state.app.checks[cap].granted > 0; |
| 190 |
} |
| 191 |
|
| 192 |
static void print_updatev_wrapped(const char *prefix, const char *cap_prefix, |
| 193 |
const char *suffix) |
| 194 |
{ |
| 195 |
int width = get_output_width(); |
| 196 |
size_t prefix_len = strlen(prefix); |
| 197 |
size_t suffix_len = strlen(suffix); |
| 198 |
int cur_len = prefix_len; |
| 199 |
int cont_indent = 8; |
| 200 |
int i; |
| 201 |
|
| 202 |
if (width < 40) |
| 203 |
width = 40; |
| 204 |
|
| 205 |
for (i = 0; prefix[i] == ' '; i++) |
| 206 |
; |
| 207 |
if (i > 0) |
| 208 |
cont_indent = i + 4; |
| 209 |
|
| 210 |
printf("%s", prefix); |
| 211 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 212 |
char cap_name[64]; |
| 213 |
char item[96]; |
| 214 |
size_t item_len; |
| 215 |
|
| 216 |
if (!cap_in_programmatic_set(i)) |
| 217 |
continue; |
| 218 |
|
| 219 |
cap_name_upper_buf(i, cap_name, sizeof(cap_name)); |
| 220 |
snprintf(item, sizeof(item), "%s%s", cap_prefix, cap_name); |
| 221 |
item_len = strlen(item); |
| 222 |
|
| 223 |
if (cur_len > (int)prefix_len && |
| 224 |
cur_len + 2 + (int)item_len + (int)suffix_len > width) { |
| 225 |
printf(",\n%*s%s", cont_indent, "", item); |
| 226 |
cur_len = cont_indent + item_len; |
| 227 |
} else { |
| 228 |
printf(", %s", item); |
| 229 |
cur_len += 2 + item_len; |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
if (cur_len + 2 + (int)suffix_len > width && cur_len > cont_indent) { |
| 234 |
printf(",\n%*s%s", cont_indent, "", suffix); |
| 235 |
} else { |
| 236 |
printf(", %s", suffix); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
void analyze_capabilities(void) |
| 241 |
{ |
| 242 |
int has_required = 0; |
| 243 |
int has_conditional = 0; |
| 244 |
int has_denied = 0; |
| 245 |
int required_count = 0; |
| 246 |
int conditional_count = 0; |
| 247 |
int denied_count = 0; |
| 248 |
unsigned long total_checks = 0; |
| 249 |
int i; |
| 250 |
int first; |
| 251 |
|
| 252 |
printf("\n"); |
| 253 |
print_rule('='); |
| 254 |
print_wrappedf("", "CAPABILITY ANALYSIS FOR: %s (PID %d)", |
| 255 |
state.app.exe, state.app.pid); |
| 256 |
print_rule('='); |
| 257 |
printf("\n"); |
| 258 |
|
| 259 |
printf("SYSTEM CONTEXT:\n"); |
| 260 |
print_rule('-'); |
| 261 |
printf(" Kernel version: %s\n", state.app.kernel_version); |
| 262 |
printf(" kernel.yama.ptrace_scope: %d\n", state.app.yama_ptrace_scope); |
| 263 |
printf(" kernel.kptr_restrict: %d\n", state.app.kptr_restrict); |
| 264 |
printf(" kernel.dmesg_restrict: %d\n", state.app.dmesg_restrict); |
| 265 |
printf(" kernel.modules_disabled: %d\n", state.app.modules_disabled); |
| 266 |
printf(" kernel.perf_event_paranoid: %d\n", |
| 267 |
state.app.perf_event_paranoid); |
| 268 |
printf(" kernel.unprivileged_bpf_disabled: %d\n", |
| 269 |
state.app.unprivileged_bpf_disabled); |
| 270 |
printf(" net.core.bpf_jit_enable: %d\n", state.app.bpf_jit_enable); |
| 271 |
printf(" net.core.bpf_jit_harden: %d\n", state.app.bpf_jit_harden); |
| 272 |
printf(" net.core.bpf_jit_kallsyms: %d\n", |
| 273 |
state.app.bpf_jit_kallsyms); |
| 274 |
printf(" vm.mmap_min_addr: %d\n", state.app.mmap_min_addr); |
| 275 |
printf(" fs.protected_hardlinks: %d\n", state.app.protected_hardlinks); |
| 276 |
printf(" fs.protected_symlinks: %d\n", state.app.protected_symlinks); |
| 277 |
printf(" fs.suid_dumpable: %d\n", state.app.suid_dumpable); |
| 278 |
printf("\n"); |
| 279 |
|
| 280 |
printf("REQUIRED CAPABILITIES:\n"); |
| 281 |
print_rule('-'); |
| 282 |
if (!state.capset_observed) { |
| 283 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 284 |
struct cap_check *check; |
| 285 |
|
| 286 |
check = &state.app.checks[i]; |
| 287 |
if (check->granted > 0) { |
| 288 |
has_required = 1; |
| 289 |
printf(" %s (#%d)\n", cap_name_safe(i), i); |
| 290 |
printf(" Checks: %lu granted, %lu denied\n", |
| 291 |
check->granted, check->denied); |
| 292 |
if (check->reason) |
| 293 |
print_wrappedf(" Reason: ", |
| 294 |
"%s", check->reason); |
| 295 |
if (!include_cap_in_recommendations(i)) |
| 296 |
print_wrapped_text(" Note: ", |
| 297 |
"Internal to capability setup; excluded from recommendations."); |
| 298 |
printf("\n"); |
| 299 |
} |
| 300 |
} |
| 301 |
if (!has_required) |
| 302 |
print_wrapped_text(" ", |
| 303 |
"None - Application does not require elevated capabilities!\n"); |
| 304 |
} else { |
| 305 |
print_wrapped_text("", |
| 306 |
"INITIALIZATION CAPABILITIES (before capability drop):"); |
| 307 |
print_rule('-'); |
| 308 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 309 |
struct cap_check *check = &state.app.checks[i]; |
| 310 |
|
| 311 |
if (check->granted > 0) { |
| 312 |
has_required = 1; |
| 313 |
printf(" %s (#%d)\n", cap_name_safe(i), i); |
| 314 |
printf(" Checks: %lu granted, %lu denied\n", |
| 315 |
check->granted, check->denied); |
| 316 |
if (check->reason) |
| 317 |
print_wrappedf(" Reason: ", |
| 318 |
"%s", check->reason); |
| 319 |
if (!include_cap_in_recommendations(i)) |
| 320 |
print_wrapped_text(" Note: ", |
| 321 |
"Internal to capability setup; excluded from recommendations."); |
| 322 |
printf("\n"); |
| 323 |
} |
| 324 |
} |
| 325 |
if (!has_required) |
| 326 |
printf(" None\n\n"); |
| 327 |
|
| 328 |
print_wrapped_text("", |
| 329 |
"OPERATIONAL CAPABILITIES (after capability drop):"); |
| 330 |
print_rule('-'); |
| 331 |
has_required = 0; |
| 332 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 333 |
struct cap_check *check = &state.app.checks[i]; |
| 334 |
|
| 335 |
if (check->op_granted > 0) { |
| 336 |
has_required = 1; |
| 337 |
printf(" %s (#%d)\n", cap_name_safe(i), i); |
| 338 |
printf(" Checks: %lu granted, %lu denied\n", |
| 339 |
check->op_granted, check->op_denied); |
| 340 |
if (check->op_reason) |
| 341 |
print_wrappedf(" Reason: ", |
| 342 |
"%s", check->op_reason); |
| 343 |
if (!include_cap_in_recommendations(i)) |
| 344 |
print_wrapped_text(" Note: ", |
| 345 |
"Internal to capability setup; excluded from recommendations."); |
| 346 |
printf("\n"); |
| 347 |
} |
| 348 |
} |
| 349 |
if (!has_required) |
| 350 |
printf(" None\n\n"); |
| 351 |
} |
| 352 |
|
| 353 |
printf("CONDITIONAL CAPABILITIES:\n"); |
| 354 |
print_rule('-'); |
| 355 |
|
| 356 |
if (state.app.yama_ptrace_scope > 0) { |
| 357 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 358 |
if (cap_total_checks(&state.app.checks[i]) > 0 && |
| 359 |
state.app.checks[i].granted == 0 && |
| 360 |
i == CAP_SYS_PTRACE) { |
| 361 |
has_conditional = 1; |
| 362 |
conditional_count++; |
| 363 |
printf(" CAP_SYS_PTRACE\n"); |
| 364 |
print_wrapped_text(" ", |
| 365 |
"Needed when kernel.yama.ptrace_scope > 0"); |
| 366 |
print_wrappedf(" ", |
| 367 |
"Current value: %d (capability needed)", |
| 368 |
state.app.yama_ptrace_scope); |
| 369 |
printf("\n"); |
| 370 |
} |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
if (state.app.perf_event_paranoid >= 2) { |
| 375 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 376 |
if (cap_total_checks(&state.app.checks[i]) > 0 && |
| 377 |
i == CAP_PERFMON) { |
| 378 |
has_conditional = 1; |
| 379 |
conditional_count++; |
| 380 |
printf(" CAP_PERFMON\n"); |
| 381 |
print_wrapped_text(" ", |
| 382 |
"Needed when kernel.perf_event_paranoid >= 2"); |
| 383 |
print_wrappedf(" ", |
| 384 |
"Current value: %d (capability needed)", |
| 385 |
state.app.perf_event_paranoid); |
| 386 |
print_wrapped_text(" Note: ", |
| 387 |
"CAP_SYS_ADMIN can substitute on kernels < 5.8"); |
| 388 |
printf("\n"); |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
if (state.app.unprivileged_bpf_disabled == 1) { |
| 394 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 395 |
if (cap_total_checks(&state.app.checks[i]) > 0 && |
| 396 |
i == CAP_BPF) { |
| 397 |
has_conditional = 1; |
| 398 |
conditional_count++; |
| 399 |
printf(" CAP_BPF\n"); |
| 400 |
print_wrapped_text(" ", |
| 401 |
"Needed when kernel.unprivileged_bpf_disabled = 1"); |
| 402 |
print_wrappedf(" ", |
| 403 |
"Current value: %d (capability needed)", |
| 404 |
state.app.unprivileged_bpf_disabled); |
| 405 |
print_wrapped_text(" Note: ", |
| 406 |
"CAP_SYS_ADMIN can substitute on kernels < 5.8"); |
| 407 |
printf("\n"); |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
if (state.app.kptr_restrict >= 1) { |
| 413 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 414 |
if (cap_total_checks(&state.app.checks[i]) > 0 && |
| 415 |
i == CAP_SYSLOG) { |
| 416 |
has_conditional = 1; |
| 417 |
conditional_count++; |
| 418 |
printf(" CAP_SYSLOG\n"); |
| 419 |
print_wrapped_text(" ", |
| 420 |
"Needed when kernel.kptr_restrict >= 1"); |
| 421 |
print_wrappedf(" ", |
| 422 |
"Current value: %d (capability needed)", |
| 423 |
state.app.kptr_restrict); |
| 424 |
printf("\n"); |
| 425 |
} |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
if (state.app.dmesg_restrict >= 1) { |
| 430 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 431 |
if (cap_total_checks(&state.app.checks[i]) > 0 && |
| 432 |
i == CAP_SYSLOG) { |
| 433 |
has_conditional = 1; |
| 434 |
conditional_count++; |
| 435 |
printf(" CAP_SYSLOG\n"); |
| 436 |
print_wrapped_text(" ", |
| 437 |
"Needed when kernel.dmesg_restrict >= 1"); |
| 438 |
print_wrappedf(" ", |
| 439 |
"Current value: %d (capability needed)", |
| 440 |
state.app.dmesg_restrict); |
| 441 |
printf("\n"); |
| 442 |
} |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
if (state.app.modules_disabled == 1) { |
| 447 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 448 |
if (cap_total_checks(&state.app.checks[i]) > 0 && |
| 449 |
i == CAP_SYS_MODULE) { |
| 450 |
has_conditional = 1; |
| 451 |
conditional_count++; |
| 452 |
printf(" NOTE: kernel.modules_disabled = 1\n"); |
| 453 |
print_wrapped_text(" ", |
| 454 |
"CAP_SYS_MODULE is ineffective!"); |
| 455 |
print_wrapped_text(" ", |
| 456 |
"Module loading is permanently disabled."); |
| 457 |
printf("\n"); |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
if (state.app.mmap_min_addr > 0) { |
| 463 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 464 |
if (cap_total_checks(&state.app.checks[i]) > 0 && |
| 465 |
i == CAP_SYS_RAWIO) { |
| 466 |
has_conditional = 1; |
| 467 |
conditional_count++; |
| 468 |
printf(" CAP_SYS_RAWIO\n"); |
| 469 |
print_wrapped_text(" ", |
| 470 |
"Needed when vm.mmap_min_addr > 0 to map low addresses"); |
| 471 |
print_wrappedf(" ", |
| 472 |
"Current value: %d (capability needed)", |
| 473 |
state.app.mmap_min_addr); |
| 474 |
printf("\n"); |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
if (state.app.protected_hardlinks == 1) { |
| 480 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 481 |
if (cap_total_checks(&state.app.checks[i]) > 0 && |
| 482 |
i == CAP_FOWNER) { |
| 483 |
has_conditional = 1; |
| 484 |
conditional_count++; |
| 485 |
printf(" CAP_FOWNER\n"); |
| 486 |
print_wrapped_text(" ", |
| 487 |
"Needed when fs.protected_hardlinks = 1 to link files not owned by the caller"); |
| 488 |
print_wrappedf(" ", |
| 489 |
"Current value: %d (capability needed)", |
| 490 |
state.app.protected_hardlinks); |
| 491 |
printf("\n"); |
| 492 |
} |
| 493 |
} |
| 494 |
} |
| 495 |
|
| 496 |
if (state.app.protected_symlinks == 1) { |
| 497 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 498 |
if (cap_total_checks(&state.app.checks[i]) > 0 && |
| 499 |
i == CAP_DAC_OVERRIDE) { |
| 500 |
has_conditional = 1; |
| 501 |
conditional_count++; |
| 502 |
printf(" CAP_DAC_OVERRIDE\n"); |
| 503 |
print_wrapped_text(" ", |
| 504 |
"Needed when fs.protected_symlinks = 1 for symlinks in world-writable directories"); |
| 505 |
print_wrappedf(" ", |
| 506 |
"Current value: %d (capability needed)", |
| 507 |
state.app.protected_symlinks); |
| 508 |
printf("\n"); |
| 509 |
} |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
if (state.app.suid_dumpable == 2) { |
| 514 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 515 |
if (cap_total_checks(&state.app.checks[i]) > 0 && |
| 516 |
i == CAP_SYS_PTRACE) { |
| 517 |
has_conditional = 1; |
| 518 |
conditional_count++; |
| 519 |
printf(" CAP_SYS_PTRACE\n"); |
| 520 |
print_wrapped_text(" ", |
| 521 |
"Needed when fs.suid_dumpable = 2 for core dumps and ptrace of setuid programs"); |
| 522 |
print_wrappedf(" ", |
| 523 |
"Current value: %d (capability needed)", |
| 524 |
state.app.suid_dumpable); |
| 525 |
printf("\n"); |
| 526 |
} |
| 527 |
} |
| 528 |
} |
| 529 |
|
| 530 |
if (!has_conditional) |
| 531 |
printf(" None\n\n"); |
| 532 |
|
| 533 |
printf("ATTEMPTED BUT DENIED:\n"); |
| 534 |
print_rule('-'); |
| 535 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 536 |
struct cap_check *check; |
| 537 |
|
| 538 |
check = &state.app.checks[i]; |
| 539 |
if (cap_total_denied(check) > 0 && cap_total_granted(check) == 0) { |
| 540 |
size_t j; |
| 541 |
|
| 542 |
has_denied = 1; |
| 543 |
printf(" %s (#%d)\n", cap_name_safe(i), i); |
| 544 |
printf(" Attempts: %lu (all denied)\n", |
| 545 |
cap_total_denied(check)); |
| 546 |
printf(" Syscalls: "); |
| 547 |
if (check->denied_syscall_count == 0) |
| 548 |
printf("unknown\n"); |
| 549 |
for (j = 0; j < check->denied_syscall_count; j++) { |
| 550 |
const char *syscall_name; |
| 551 |
int syscall_nr; |
| 552 |
|
| 553 |
syscall_nr = check->denied_syscalls[j]; |
| 554 |
syscall_name = syscall_name_from_nr(syscall_nr); |
| 555 |
if (j > 0) |
| 556 |
printf(", "); |
| 557 |
if (syscall_name) |
| 558 |
printf("%s", syscall_name); |
| 559 |
else |
| 560 |
printf("unknown(#%d)", syscall_nr); |
| 561 |
} |
| 562 |
if (check->denied_syscall_count > 0) |
| 563 |
printf("\n"); |
| 564 |
print_wrapped_text(" Impact: ", |
| 565 |
"Application may have reduced functionality"); |
| 566 |
printf("\n"); |
| 567 |
} |
| 568 |
} |
| 569 |
if (!has_denied) |
| 570 |
printf(" None\n\n"); |
| 571 |
|
| 572 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 573 |
total_checks += cap_total_checks(&state.app.checks[i]); |
| 574 |
if (cap_required_union(&state.app.checks[i])) |
| 575 |
required_count++; |
| 576 |
if (cap_total_denied(&state.app.checks[i]) > 0 && |
| 577 |
cap_total_granted(&state.app.checks[i]) == 0) |
| 578 |
denied_count++; |
| 579 |
} |
| 580 |
|
| 581 |
printf("SUMMARY:\n"); |
| 582 |
print_rule('-'); |
| 583 |
printf(" Total capability checks: %lu\n", total_checks); |
| 584 |
printf(" Required capabilities: %d\n", required_count); |
| 585 |
printf(" Conditional capabilities: %d\n", conditional_count); |
| 586 |
printf(" Denied operations: %d\n", denied_count); |
| 587 |
printf("\n"); |
| 588 |
|
| 589 |
if (required_count > 0) { |
| 590 |
printf("RECOMMENDATIONS:\n"); |
| 591 |
print_rule('-'); |
| 592 |
if (state.app.prog_type != UNSUPPORTED) { |
| 593 |
printf(" Programmatic solution (%s):\n", |
| 594 |
state.app.prog_type == ELF ? |
| 595 |
"C with libcap-ng" : |
| 596 |
"Python with python3-libcap-ng"); |
| 597 |
if (state.capset_observed) |
| 598 |
print_wrapped_text(" Note: ", |
| 599 |
"The application drops capabilities after initialization. The programmatic snippet reflects the operational set only."); |
| 600 |
|
| 601 |
if (state.app.prog_type == ELF) { |
| 602 |
printf(" #include <cap-ng.h>\n"); |
| 603 |
printf(" ...\n"); |
| 604 |
printf(" capng_clear(CAPNG_SELECT_BOTH);\n"); |
| 605 |
print_updatev_wrapped(" capng_updatev(CAPNG_ADD, " |
| 606 |
"CAPNG_EFFECTIVE|CAPNG_PERMITTED", |
| 607 |
"", "-1);\n"); |
| 608 |
printf(" if (capng_change_id(uid, gid, " |
| 609 |
"CAPNG_DROP_SUPP_GRP | " |
| 610 |
"CAPNG_CLEAR_BOUNDING))\n"); |
| 611 |
printf("\tperror(\"capng_change_id\");\n\n"); |
| 612 |
} else if (state.app.prog_type == PYTHON) { |
| 613 |
printf(" import sys\n"); |
| 614 |
printf(" import _capng as capng\n"); |
| 615 |
printf(" ...\n"); |
| 616 |
printf(" capng.capng_clear(capng.CAPNG_SELECT_BOTH)\n"); |
| 617 |
print_updatev_wrapped(" capng.capng_updatev(" |
| 618 |
"capng.CAPNG_ADD, " |
| 619 |
"capng.CAPNG_EFFECTIVE|" |
| 620 |
"capng.CAPNG_PERMITTED", |
| 621 |
"capng.", "-1)\n"); |
| 622 |
printf(" e = capng.capng_change_id(uid, gid, " |
| 623 |
"capng.CAPNG_DROP_SUPP_GRP | " |
| 624 |
"capng.CAPNG_CLEAR_BOUNDING)\n"); |
| 625 |
printf(" if e < 0:\n"); |
| 626 |
printf("\tprint(f\"Error: {e}\")\n"); |
| 627 |
printf("\tsys.exit(1)\n\n"); |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
printf(" For systemd service:\n"); |
| 632 |
if (state.capset_observed) |
| 633 |
print_wrapped_text(" Note: ", |
| 634 |
"Ambient capabilities must include initialization requirements. The application drops to the operational set internally via capset."); |
| 635 |
printf(" [Service]\n"); |
| 636 |
printf(" User=<non-root-user>\n"); |
| 637 |
printf(" Group=<non-root-group>\n"); |
| 638 |
printf(" AmbientCapabilities="); |
| 639 |
first = 1; |
| 640 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 641 |
if (cap_required_union(&state.app.checks[i]) && |
| 642 |
include_cap_in_recommendations(i)) { |
| 643 |
if (!first) |
| 644 |
printf(" "); |
| 645 |
printf("%s", cap_name_safe(i)); |
| 646 |
first = 0; |
| 647 |
} |
| 648 |
} |
| 649 |
printf("\n"); |
| 650 |
printf(" CapabilityBoundingSet="); |
| 651 |
first = 1; |
| 652 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 653 |
if (cap_required_union(&state.app.checks[i]) && |
| 654 |
include_cap_in_recommendations(i)) { |
| 655 |
if (!first) |
| 656 |
printf(" "); |
| 657 |
printf("%s", cap_name_safe(i)); |
| 658 |
first = 0; |
| 659 |
} |
| 660 |
} |
| 661 |
printf("\n\n"); |
| 662 |
|
| 663 |
printf(" For file capabilities (via filecap):\n"); |
| 664 |
if (state.capset_observed) |
| 665 |
print_wrapped_text(" Note: ", |
| 666 |
"File capabilities must include initialization requirements. The application drops to the operational set internally via capset."); |
| 667 |
printf(" filecap /path/to/binary"); |
| 668 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 669 |
if (cap_required_union(&state.app.checks[i]) && |
| 670 |
include_cap_in_recommendations(i)) |
| 671 |
printf(" %s", cap_name_safe(i)); |
| 672 |
} |
| 673 |
printf("\n\n"); |
| 674 |
|
| 675 |
printf(" For Docker/Podman:\n"); |
| 676 |
if (state.capset_observed) |
| 677 |
print_wrapped_text(" Note: ", |
| 678 |
"Container capabilities must include initialization requirements. The application drops to the operational set internally via capset."); |
| 679 |
printf(" docker run --user $(id -u):$(id -g) \\\n"); |
| 680 |
printf(" --cap-drop=ALL \\\n"); |
| 681 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 682 |
if (cap_required_union(&state.app.checks[i]) && |
| 683 |
include_cap_in_recommendations(i)) |
| 684 |
printf(" --cap-add=%s \\\n", |
| 685 |
cap_name_safe(i)); |
| 686 |
} |
| 687 |
printf(" your-image:tag\n\n"); |
| 688 |
|
| 689 |
printf(" For Kubernetes:\n"); |
| 690 |
if (state.capset_observed) |
| 691 |
print_wrapped_text(" Note: ", |
| 692 |
"Container capabilities must include initialization requirements. The application drops to the operational set internally via capset."); |
| 693 |
printf(" securityContext:\n"); |
| 694 |
printf(" runAsUser: 1000\n"); |
| 695 |
printf(" runAsGroup: 1000\n"); |
| 696 |
printf(" capabilities:\n"); |
| 697 |
printf(" drop:\n"); |
| 698 |
printf(" - ALL\n"); |
| 699 |
printf(" add:\n"); |
| 700 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 701 |
if (cap_required_union(&state.app.checks[i]) && |
| 702 |
include_cap_in_recommendations(i)) |
| 703 |
printf(" - %s\n", cap_name_safe(i)); |
| 704 |
} |
| 705 |
printf("\n"); |
| 706 |
} else { |
| 707 |
printf("RECOMMENDATIONS:\n"); |
| 708 |
print_rule('-'); |
| 709 |
print_wrapped_text(" ", |
| 710 |
"This application does not require any elevated capabilities!"); |
| 711 |
print_wrapped_text(" ", |
| 712 |
"Run as an unprivileged user with no special capabilities."); |
| 713 |
printf("\n"); |
| 714 |
} |
| 715 |
|
| 716 |
print_wrapped_text("EXPERIMENTAL NOTICE: ", |
| 717 |
"cap-audit output is experimental, but very close."); |
| 718 |
} |