| 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 |
/* JSON output serialization: machine-readable capability summaries for |
| 23 |
* automated tooling and regression comparison. |
| 24 |
*/ |
| 25 |
|
| 26 |
#include "cap_audit.h" |
| 27 |
|
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
|
| 31 |
void output_json(void) |
| 32 |
{ |
| 33 |
int i; |
| 34 |
int first_cap; |
| 35 |
int first_denied; |
| 36 |
char *exe_json; |
| 37 |
char *kernel_json; |
| 38 |
|
| 39 |
exe_json = json_escape(state.app.exe); |
| 40 |
kernel_json = json_escape(state.app.kernel_version); |
| 41 |
|
| 42 |
printf("{\n"); |
| 43 |
printf(" \"application\": {\n"); |
| 44 |
printf(" \"pid\": %d,\n", state.app.pid); |
| 45 |
printf(" \"comm\": \"%s\"\n", exe_json ? exe_json : ""); |
| 46 |
printf(" },\n"); |
| 47 |
|
| 48 |
printf(" \"system_context\": {\n"); |
| 49 |
printf(" \"kernel_version\": \"%s\",\n", |
| 50 |
kernel_json ? kernel_json : ""); |
| 51 |
printf(" \"yama_ptrace_scope\": %d,\n", state.app.yama_ptrace_scope); |
| 52 |
printf(" \"kptr_restrict\": %d,\n", state.app.kptr_restrict); |
| 53 |
printf(" \"dmesg_restrict\": %d,\n", state.app.dmesg_restrict); |
| 54 |
printf(" \"modules_disabled\": %d,\n", state.app.modules_disabled); |
| 55 |
printf(" \"perf_event_paranoid\": %d,\n", |
| 56 |
state.app.perf_event_paranoid); |
| 57 |
printf(" \"unprivileged_bpf_disabled\": %d,\n", |
| 58 |
state.app.unprivileged_bpf_disabled); |
| 59 |
printf(" \"bpf_jit_enable\": %d,\n", state.app.bpf_jit_enable); |
| 60 |
printf(" \"bpf_jit_harden\": %d,\n", state.app.bpf_jit_harden); |
| 61 |
printf(" \"bpf_jit_kallsyms\": %d,\n", |
| 62 |
state.app.bpf_jit_kallsyms); |
| 63 |
printf(" \"vm_mmap_min_addr\": %d,\n", state.app.mmap_min_addr); |
| 64 |
printf(" \"fs_protected_hardlinks\": %d,\n", |
| 65 |
state.app.protected_hardlinks); |
| 66 |
printf(" \"fs_protected_symlinks\": %d,\n", |
| 67 |
state.app.protected_symlinks); |
| 68 |
printf(" \"fs_suid_dumpable\": %d\n", state.app.suid_dumpable); |
| 69 |
printf(" },\n"); |
| 70 |
|
| 71 |
free(exe_json); |
| 72 |
free(kernel_json); |
| 73 |
|
| 74 |
printf(" \"capability_drop_observed\": %s,\n", |
| 75 |
state.capset_observed ? "true" : "false"); |
| 76 |
|
| 77 |
printf(" \"required_capabilities\": [\n"); |
| 78 |
first_cap = 1; |
| 79 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 80 |
struct cap_check *check = &state.app.checks[i]; |
| 81 |
char *name_json; |
| 82 |
char *reason_json; |
| 83 |
|
| 84 |
if (cap_required_union(check)) { |
| 85 |
name_json = json_escape(capng_capability_to_name(i)); |
| 86 |
reason_json = cap_union_reason(check) ? |
| 87 |
json_escape(cap_union_reason(check)) : NULL; |
| 88 |
if (!first_cap) |
| 89 |
printf(",\n"); |
| 90 |
printf(" {\n"); |
| 91 |
printf(" \"number\": %d,\n", i); |
| 92 |
printf(" \"name\": \"%s\",\n", |
| 93 |
name_json ? name_json : ""); |
| 94 |
printf(" \"checks\": {\n"); |
| 95 |
printf(" \"total\": %lu,\n", |
| 96 |
cap_total_checks(check)); |
| 97 |
printf(" \"granted\": %lu,\n", |
| 98 |
cap_total_granted(check)); |
| 99 |
printf(" \"denied\": %lu\n", |
| 100 |
cap_total_denied(check)); |
| 101 |
printf(" }"); |
| 102 |
if (cap_union_reason(check)) |
| 103 |
printf(",\n \"reason\": \"%s\"\n", |
| 104 |
reason_json ? reason_json : ""); |
| 105 |
else |
| 106 |
printf("\n"); |
| 107 |
printf(" }"); |
| 108 |
first_cap = 0; |
| 109 |
free(name_json); |
| 110 |
free(reason_json); |
| 111 |
} |
| 112 |
} |
| 113 |
printf("\n ]"); |
| 114 |
|
| 115 |
if (state.capset_observed) { |
| 116 |
printf(",\n \"initialization_capabilities\": [\n"); |
| 117 |
first_cap = 1; |
| 118 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 119 |
struct cap_check *check = &state.app.checks[i]; |
| 120 |
char *name_json; |
| 121 |
char *reason_json; |
| 122 |
|
| 123 |
if (check->granted == 0) |
| 124 |
continue; |
| 125 |
|
| 126 |
name_json = json_escape(capng_capability_to_name(i)); |
| 127 |
reason_json = check->reason ? |
| 128 |
json_escape(check->reason) : NULL; |
| 129 |
if (!first_cap) |
| 130 |
printf(",\n"); |
| 131 |
printf(" {\n"); |
| 132 |
printf(" \"number\": %d,\n", i); |
| 133 |
printf(" \"name\": \"%s\",\n", |
| 134 |
name_json ? name_json : ""); |
| 135 |
printf(" \"checks\": {\n"); |
| 136 |
printf(" \"total\": %lu,\n", check->count); |
| 137 |
printf(" \"granted\": %lu,\n", check->granted); |
| 138 |
printf(" \"denied\": %lu\n", check->denied); |
| 139 |
printf(" }"); |
| 140 |
if (check->reason) |
| 141 |
printf(",\n \"reason\": \"%s\"\n", |
| 142 |
reason_json ? reason_json : ""); |
| 143 |
else |
| 144 |
printf("\n"); |
| 145 |
printf(" }"); |
| 146 |
first_cap = 0; |
| 147 |
free(name_json); |
| 148 |
free(reason_json); |
| 149 |
} |
| 150 |
printf("\n ],\n"); |
| 151 |
|
| 152 |
printf(" \"operational_capabilities\": [\n"); |
| 153 |
first_cap = 1; |
| 154 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 155 |
struct cap_check *check = &state.app.checks[i]; |
| 156 |
char *name_json; |
| 157 |
char *reason_json; |
| 158 |
|
| 159 |
if (check->op_granted == 0) |
| 160 |
continue; |
| 161 |
|
| 162 |
name_json = json_escape(capng_capability_to_name(i)); |
| 163 |
reason_json = check->op_reason ? |
| 164 |
json_escape(check->op_reason) : NULL; |
| 165 |
if (!first_cap) |
| 166 |
printf(",\n"); |
| 167 |
printf(" {\n"); |
| 168 |
printf(" \"number\": %d,\n", i); |
| 169 |
printf(" \"name\": \"%s\",\n", |
| 170 |
name_json ? name_json : ""); |
| 171 |
printf(" \"checks\": {\n"); |
| 172 |
printf(" \"total\": %lu,\n", check->op_count); |
| 173 |
printf(" \"granted\": %lu,\n", |
| 174 |
check->op_granted); |
| 175 |
printf(" \"denied\": %lu\n", |
| 176 |
check->op_denied); |
| 177 |
printf(" }"); |
| 178 |
if (check->op_reason) |
| 179 |
printf(",\n \"reason\": \"%s\"\n", |
| 180 |
reason_json ? reason_json : ""); |
| 181 |
else |
| 182 |
printf("\n"); |
| 183 |
printf(" }"); |
| 184 |
first_cap = 0; |
| 185 |
free(name_json); |
| 186 |
free(reason_json); |
| 187 |
} |
| 188 |
printf("\n ]"); |
| 189 |
} |
| 190 |
|
| 191 |
printf(",\n"); |
| 192 |
|
| 193 |
printf(" \"denied_capabilities\": [\n"); |
| 194 |
first_denied = 1; |
| 195 |
for (i = 0; i <= CAP_LAST_CAP; i++) { |
| 196 |
struct cap_check *check = &state.app.checks[i]; |
| 197 |
char *name_json; |
| 198 |
|
| 199 |
if (cap_total_denied(check) > 0 && cap_total_granted(check) == 0) { |
| 200 |
name_json = json_escape(capng_capability_to_name(i)); |
| 201 |
if (!first_denied) |
| 202 |
printf(",\n"); |
| 203 |
printf(" {\n"); |
| 204 |
printf(" \"number\": %d,\n", i); |
| 205 |
printf(" \"name\": \"%s\",\n", |
| 206 |
name_json ? name_json : ""); |
| 207 |
printf(" \"attempts\": %lu\n", |
| 208 |
cap_total_denied(check)); |
| 209 |
printf(" }"); |
| 210 |
first_denied = 0; |
| 211 |
free(name_json); |
| 212 |
} |
| 213 |
} |
| 214 |
printf("\n ]\n"); |
| 215 |
printf("}\n"); |
| 216 |
} |