Debian Codegraph @

utils/cap-audit/cap_audit.h

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 #ifndef CAP_AUDIT_H
23 #define CAP_AUDIT_H
24
25 #include "config.h"
26
27 #include <bpf/libbpf.h>
28 #include <linux/capability.h>
29 #include <signal.h>
30 #include <stddef.h>
31 #include <stdio.h>
32 #include <sys/types.h>
33
34 #include "cap-ng.h"
35 #include "cap_audit.skel.h"
36
37 #ifndef CAP_OPT_NOAUDIT
38 #define CAP_OPT_NOAUDIT 0x2
39 #endif
40
41 #define ELFMAGIC "\177ELF"
42
43 typedef enum { UNSUPPORTED, ELF, PYTHON } type_t;
44
45 struct cap_event {
46 __u64 timestamp_ns;
47 __u32 pid;
48 __u32 tid;
49 __u32 uid;
50 __u32 gid;
51 int capability;
52 int result;
53 int syscall_nr;
54 char comm[16];
55 __u64 stack_id;
56 __u32 targ_ns_inum;
57 __u32 cap_opts;
58 };
59
60 struct cap_check {
61 int capability;
62 unsigned long count;
63 unsigned long granted;
64 unsigned long denied;
65 int needed;
66 char *reason;
67 unsigned long op_count;
68 unsigned long op_granted;
69 unsigned long op_denied;
70 int op_needed;
71 char *op_reason;
72 int *denied_syscalls;
73 size_t denied_syscall_count;
74 size_t denied_syscall_capacity;
75 };
76
77 struct app_caps {
78 pid_t pid;
79 char *exe;
80 int execve_nr;
81 int mmap_nr;
82 int brk_nr;
83 int mprotect_nr;
84 int mremap_nr;
85 int capset_nr;
86 type_t prog_type;
87 struct cap_check checks[CAP_LAST_CAP + 1];
88 int yama_ptrace_scope;
89 int kptr_restrict;
90 int dmesg_restrict;
91 int modules_disabled;
92 int perf_event_paranoid;
93 int unprivileged_bpf_disabled;
94 int bpf_jit_enable;
95 int bpf_jit_harden;
96 int bpf_jit_kallsyms;
97 int mmap_min_addr;
98 int protected_hardlinks;
99 int protected_symlinks;
100 int suid_dumpable;
101 char kernel_version[64];
102 int file_caps;
103 int file_setpcap;
104 };
105
106 struct audit_state {
107 struct cap_audit_bpf *skel;
108 struct ring_buffer *rb;
109 struct app_caps app;
110 int verbose;
111 int json_output;
112 int yaml_output;
113 int sync_pipe[2];
114 char **target_argv;
115 int capset_observed;
116 volatile sig_atomic_t stop;
117 int shutting_down;
118 };
119
120 extern struct audit_state state;
121 extern int audit_machine;
122
123 int handle_cap_event(void *ctx, void *data, size_t data_sz);
124 void analyze_capabilities(void);
125 void output_json(void);
126 void output_yaml(void);
127 int include_cap_in_recommendations(int cap);
128 const char *cap_name_safe(int cap);
129 const char *syscall_name_from_nr(int nr);
130 void read_sysctl(const char *path, int *value);
131 void read_system_state(struct app_caps *app);
132 int resolve_target_exe(pid_t pid, char *exepath, size_t exepath_len);
133 int inspect_target_file_caps(pid_t pid);
134 char *json_escape(const char *input);
135 void update_reason_to(char **target, int syscall_nr);
136 void update_reason(struct cap_check *check, int syscall_nr);
137 void update_reason_op(struct cap_check *check, int syscall_nr);
138 int cap_required_union(const struct cap_check *check);
139 unsigned long cap_total_checks(const struct cap_check *check);
140 unsigned long cap_total_granted(const struct cap_check *check);
141 unsigned long cap_total_denied(const struct cap_check *check);
142 const char *cap_union_reason(const struct cap_check *check);
143 type_t classify_app(const char *exe);
144
145 #endif