| 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 |
/* YAML output serialization: alternate machine-readable output mirroring |
| 23 |
* the JSON structure for downstream consumers. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "cap_audit.h" |
| 27 |
|
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
|
| 31 |
void output_yaml(void) |
| 32 |
{ |
| 33 |
int i; |
| 34 |
char *exe_yaml = json_escape(state.app.exe); |
| 35 |
char *kernel_yaml = json_escape(state.app.kernel_version); |
| 36 |
|
| 37 |
printf("application:\n"); |
| 38 |
printf(" pid: %d\n", state.app.pid); |
| 39 |
printf(" comm: \"%s\"\n", exe_yaml ? exe_yaml : ""); |
| 40 |
|
| 41 |
printf("system_context:\n"); |
| 42 |
printf(" kernel_version: \"%s\"\n", kernel_yaml ? kernel_yaml : ""); |
| 43 |
printf(" yama_ptrace_scope: %d\n", state.app.yama_ptrace_scope); |
| 44 |
printf(" kptr_restrict: %d\n", state.app.kptr_restrict); |
| 45 |
printf(" dmesg_restrict: %d\n", state.app.dmesg_restrict); |
| 46 |
printf(" modules_disabled: %d\n", state.app.modules_disabled); |
| 47 |
printf(" perf_event_paranoid: %d\n", |
| 48 |
state.app.perf_event_paranoid); |
| 49 |
printf(" unprivileged_bpf_disabled: %d\n", |
| 50 |
state.app.unprivileged_bpf_disabled); |
| 51 |
printf(" bpf_jit_enable: %d\n", state.app.bpf_jit_enable); |
| 52 |
printf(" bpf_jit_harden: %d\n", state.app.bpf_jit_harden); |
| 53 |
printf(" bpf_jit_kallsyms: %d\n", state.app.bpf_jit_kallsyms); |
| 54 |
printf(" vm_mmap_min_addr: %d\n", state.app.mmap_min_addr); |
| 55 |
printf(" fs_protected_hardlinks: %d\n", |
| 56 |
state.app.protected_hardlinks); |
| 57 |
printf(" fs_protected_symlinks: %d\n", |
| 58 |
state.app.protected_symlinks); |
| 59 |
printf(" fs_suid_dumpable: %d\n", state.app.suid_dumpable); |
| 60 |
free(exe_yaml); |
| 61 |
free(kernel_yaml); |
| 62 |
|
| 63 |
printf("capability_drop_observed: %s\n", |
| 64 |
state.capset_observed ? "true" : "false"); |
| 65 |
|
| 66 |
printf("required_capabilities:\n"); |
| 67 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 68 |
struct cap_check *check = &state.app.checks[i]; |
| 69 |
|
| 70 |
if (cap_required_union(check)) { |
| 71 |
printf(" - number: %d\n", i); |
| 72 |
printf(" name: %s\n", cap_name_safe(i)); |
| 73 |
printf(" checks:\n"); |
| 74 |
printf(" total: %lu\n", cap_total_checks(check)); |
| 75 |
printf(" granted: %lu\n", cap_total_granted(check)); |
| 76 |
printf(" denied: %lu\n", cap_total_denied(check)); |
| 77 |
if (cap_union_reason(check)) { |
| 78 |
char *reason_yaml = |
| 79 |
json_escape(cap_union_reason(check)); |
| 80 |
printf(" reason: \"%s\"\n", |
| 81 |
reason_yaml ? reason_yaml : ""); |
| 82 |
free(reason_yaml); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
if (state.capset_observed) { |
| 88 |
printf("initialization_capabilities:\n"); |
| 89 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 90 |
struct cap_check *check = &state.app.checks[i]; |
| 91 |
|
| 92 |
if (check->granted == 0) |
| 93 |
continue; |
| 94 |
|
| 95 |
printf(" - number: %d\n", i); |
| 96 |
printf(" name: %s\n", cap_name_safe(i)); |
| 97 |
printf(" checks:\n"); |
| 98 |
printf(" total: %lu\n", check->count); |
| 99 |
printf(" granted: %lu\n", check->granted); |
| 100 |
printf(" denied: %lu\n", check->denied); |
| 101 |
if (check->reason) { |
| 102 |
char *reason_yaml = json_escape(check->reason); |
| 103 |
printf(" reason: \"%s\"\n", |
| 104 |
reason_yaml ? reason_yaml : ""); |
| 105 |
free(reason_yaml); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
printf("operational_capabilities:\n"); |
| 110 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 111 |
struct cap_check *check = &state.app.checks[i]; |
| 112 |
|
| 113 |
if (check->op_granted == 0) |
| 114 |
continue; |
| 115 |
|
| 116 |
printf(" - number: %d\n", i); |
| 117 |
printf(" name: %s\n", cap_name_safe(i)); |
| 118 |
printf(" checks:\n"); |
| 119 |
printf(" total: %lu\n", check->op_count); |
| 120 |
printf(" granted: %lu\n", check->op_granted); |
| 121 |
printf(" denied: %lu\n", check->op_denied); |
| 122 |
if (check->op_reason) { |
| 123 |
char *reason_yaml = json_escape(check->op_reason); |
| 124 |
printf(" reason: \"%s\"\n", |
| 125 |
reason_yaml ? reason_yaml : ""); |
| 126 |
free(reason_yaml); |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
printf("denied_capabilities:\n"); |
| 132 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 133 |
struct cap_check *check = &state.app.checks[i]; |
| 134 |
|
| 135 |
if (cap_total_denied(check) > 0 && cap_total_granted(check) == 0) { |
| 136 |
printf(" - number: %d\n", i); |
| 137 |
printf(" name: %s\n", cap_name_safe(i)); |
| 138 |
printf(" attempts: %lu\n", cap_total_denied(check)); |
| 139 |
} |
| 140 |
} |
| 141 |
} |