Debian Codegraph @

utils/cap-audit/cap_audit_event.c

raw
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 /* Ring buffer event processing: noise filtering, shutdown backstop,
23 * and per-capability accounting for observed checks.
24 */
25
26 #include "cap_audit.h"
27
28 #include <stdio.h>
29 #include <stdlib.h>
30
31 static int is_always_noise(const struct cap_event *e)
32 {
33 if (e->syscall_nr == state.app.execve_nr &&
34 (e->capability == CAP_SYS_ADMIN ||
35 e->capability == CAP_SETPCAP))
36 return 1;
37
38 if ((e->cap_opts & CAP_OPT_NOAUDIT) &&
39 e->capability == CAP_SYS_ADMIN &&
40 (e->syscall_nr == state.app.brk_nr ||
41 e->syscall_nr == state.app.mmap_nr ||
42 e->syscall_nr == state.app.mprotect_nr ||
43 e->syscall_nr == state.app.mremap_nr))
44 return 1;
45
46 return 0;
47 }
48
49 static int is_shutdown_noise(const struct cap_event *e)
50 {
51 if (!state.shutting_down)
52 return 0;
53
54 if (e->pid != (__u32)state.app.pid)
55 return 0;
56
57 if (e->capability == CAP_SYS_ADMIN ||
58 e->capability == CAP_SETPCAP)
59 return 1;
60
61 return 0;
62 }
63
64 static void add_denied_syscall(struct cap_check *check, int syscall_nr)
65 {
66 size_t i;
67 int *tmp;
68 size_t new_cap;
69
70 for (i = 0; i < check->denied_syscall_count; i++) {
71 if (check->denied_syscalls[i] == syscall_nr)
72 return;
73 }
74
75 if (check->denied_syscall_count == check->denied_syscall_capacity) {
76 new_cap = check->denied_syscall_capacity ?
77 check->denied_syscall_capacity * 2 : 4;
78 tmp = realloc(check->denied_syscalls, new_cap * sizeof(int));
79 if (!tmp)
80 return;
81 check->denied_syscalls = tmp;
82 check->denied_syscall_capacity = new_cap;
83 }
84
85 check->denied_syscalls[check->denied_syscall_count++] = syscall_nr;
86 }
87
88 int handle_cap_event(void *ctx __attribute__((unused)), void *data,
89 size_t data_sz __attribute__((unused)))
90 {
91 const struct cap_event *e = data;
92 int op_phase = state.capset_observed;
93
94 if (is_always_noise(e)) {
95 if (state.verbose) {
96 if (e->syscall_nr == state.app.execve_nr)
97 printf("[CAP] Filtered exec noise: "
98 "cap=%s syscall=%s\n",
99 cap_name_safe(e->capability),
100 syscall_name_from_nr(e->syscall_nr) ?:
101 "unknown");
102 else
103 printf("[CAP] Filtered advisory check: "
104 "cap=%s syscall=%s (CAP_OPT_NOAUDIT)\n",
105 cap_name_safe(e->capability),
106 syscall_name_from_nr(e->syscall_nr) ?:
107 "unknown");
108 }
109 return 0;
110 }
111
112 if (is_shutdown_noise(e)) {
113 if (state.verbose)
114 printf("[CAP] Filtered shutdown noise: "
115 "cap=%s syscall=%s\n",
116 cap_name_safe(e->capability),
117 syscall_name_from_nr(e->syscall_nr) ?:
118 "unknown");
119 return 0;
120 }
121
122 if (state.verbose) {
123 printf("[CAP] pid=%d cap=%s result=%s syscall=%s "
124 "comm=%s\n",
125 e->pid, cap_name_safe(e->capability),
126 e->result ? "GRANTED" : "DENIED",
127 syscall_name_from_nr(e->syscall_nr) ?: "unknown",
128 e->comm);
129 }
130
131 if (!state.capset_observed &&
132 e->syscall_nr == state.app.capset_nr &&
133 e->pid == (__u32)state.app.pid) {
134 state.capset_observed = 1;
135 if (state.verbose)
136 printf("[CAP] Capability drop detected (capset from "
137 "initial PID); switching to operational "
138 "phase\n");
139 }
140
141 if (e->capability >= 0 && e->capability <= CAP_LAST_CAP) {
142 struct cap_check *check;
143
144 check = &state.app.checks[e->capability];
145 check->capability = e->capability;
146
147 if (op_phase) {
148 check->op_count++;
149 if (e->result > 0)
150 check->op_granted++;
151 else if (e->result == 0) {
152 check->op_denied++;
153 add_denied_syscall(check, e->syscall_nr);
154 }
155 if (e->result > 0 && check->op_needed != 1) {
156 check->op_needed = 1;
157 update_reason_op(check, e->syscall_nr);
158 }
159 } else {
160 check->count++;
161 if (e->result > 0)
162 check->granted++;
163 else if (e->result == 0) {
164 check->denied++;
165 add_denied_syscall(check, e->syscall_nr);
166 }
167
168 if (e->result > 0 && check->needed != 1) {
169 check->needed = 1;
170 update_reason(check, e->syscall_nr);
171 }
172 }
173 }
174
175 return 0;
176 }