Debian Codegraph @

src/util/audit.c

raw
1 /*
2 * Audit Helpers
3 */
4
5 #include <c-stdaux.h>
6 #include <cap-ng.h>
7 #include <grp.h>
8 #include <libaudit.h>
9 #include <stdlib.h>
10 #include <sys/prctl.h>
11 #include <unistd.h>
12 #include "util/audit.h"
13 #include "util/error.h"
14
15 static int audit_fd = -1;
16
17 /**
18 * util_audit_drop_permissions() - drop process permissions
19 * @uid: uid to set
20 * @gid: gid to set
21 *
22 * This performs a setuid(2) and setgid(2) syscall, clearing all process
23 * permissions we have, but retaining required capabilities.
24 *
25 * The reason this lives in the audit-subsystem, is that the audit-subsystem is
26 * the only code that needs to retain capabilities. Hence, the cap-ng
27 * dependency is only required for the audit code. If the capability
28 * dependencies get more comples, we have to rework this, but for now it should
29 * be fine.
30 *
31 * Return: 0 on success, or a negative error code on failure.
32 */
33 int util_audit_drop_permissions(uint32_t uid, uint32_t gid) {
34 int r;
35
36 /*
37 * This is modeled exactly after the behavior of dbus-daemon(1). We
38 * have to be compatibile and fail in the exact same situations. This
39 * means, only try to retain CAP_AUDIT_WRITE if we are running as root
40 * and own it. In all other cases, simply drop privileges to the
41 * requested IDs.
42 */
43
44 if (geteuid() != 0) {
45 /*
46 * For compatibility to dbus-daemon, this must be
47 * non-fatal.
48 */
49 setgroups(0, NULL);
50
51 r = setgid(gid);
52 if (r < 0)
53 return error_origin(-errno);
54
55 r = setuid(uid);
56 if (r < 0)
57 return error_origin(-errno);
58 } else {
59 bool have_audit_write;
60
61 r = capng_have_capability(CAPNG_PERMITTED, CAP_AUDIT_WRITE);
62 if (r == CAPNG_FAIL)
63 return error_origin(-EIO);
64 else if (r == 1)
65 have_audit_write = true;
66 else
67 have_audit_write = false;
68
69 capng_clear(CAPNG_SELECT_BOTH);
70
71 if (have_audit_write) {
72 r = capng_update(CAPNG_ADD,
73 CAPNG_EFFECTIVE | CAPNG_PERMITTED | CAPNG_INHERITABLE,
74 CAP_AUDIT_WRITE);
75 if (r < 0)
76 return error_origin(-EINVAL);
77 }
78
79 r = capng_change_id(uid, gid, CAPNG_DROP_SUPP_GRP);
80 if (r)
81 return error_origin(-EPERM);
82
83 if (have_audit_write) {
84 r = prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_AUDIT_WRITE, 0, 0);
85 if (r < 0)
86 return error_origin(-errno);
87 }
88 }
89
90 return 0;
91 }
92
93 /**
94 * util_audit_log() - log a message to the audit subsystem
95 * @type: the audit message type, see enum in audit.h
96 * @message: the message to be logged
97 * @uid: the UID of the user causing the message to be logged
98 *
99 * Log the message to the audit subsystem. If audit is disabled, return
100 * UTIL_AUDIT_E_UNAVAILABLE instead.
101 *
102 * Return: 0 on success, UTIL_AUDIT_E_UNAVAILABLE if audit is not
103 * available, or a negative error code on failure.
104 */
105 int util_audit_log(int type, const char *message, uid_t uid) {
106 int r;
107
108 if (audit_fd < 0)
109 return UTIL_AUDIT_E_UNAVAILABLE;
110
111 switch (type) {
112 case UTIL_AUDIT_TYPE_AVC:
113 r = audit_log_user_avc_message(
114 audit_fd,
115 AUDIT_USER_AVC,
116 message,
117 NULL,
118 NULL,
119 NULL,
120 uid
121 );
122 break;
123 case UTIL_AUDIT_TYPE_POLICYLOAD:
124 r = audit_log_user_message(
125 audit_fd,
126 AUDIT_USER_MAC_POLICY_LOAD,
127 message,
128 NULL,
129 NULL,
130 NULL,
131 1
132 );
133 break;
134 case UTIL_AUDIT_TYPE_MAC_STATUS:
135 r = audit_log_user_message(
136 audit_fd,
137 AUDIT_USER_MAC_STATUS,
138 message,
139 NULL,
140 NULL,
141 NULL,
142 1
143 );
144 break;
145 default:
146 return error_origin(-ENOTRECOVERABLE);
147 }
148
149 if (r <= 0)
150 return error_origin(-errno);
151
152 return 0;
153 }
154
155 /**
156 * util_audit_init_global() - initialize the global audit context
157 *
158 * Initialize the global audit context. This must be called before any
159 * other audit function. If audit is not supported, the context is
160 * initialized to indicate that, but the function still succeeds.
161 *
162 * Return: the 0 on success, negative error code on failure.
163 */
164 int util_audit_init_global(void) {
165 int r;
166
167 c_assert(audit_fd < 0);
168
169 r = capng_have_capability(CAPNG_EFFECTIVE, CAP_AUDIT_WRITE);
170 if (r == 0)
171 /*
172 * Without the right capability, any writes will fail, so treat this
173 * as if audit is unsupported.
174 */
175 return 0;
176 else if (r == CAPNG_FAIL)
177 return error_origin(-EIO);
178
179 r = audit_open();
180 if (r < 0 && errno != EINVAL && errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT)
181 return error_origin(-errno);
182
183 audit_fd = r;
184
185 return 0;
186 }
187
188 /**
189 * util_audit_deinit_global() - deinitialize the global audit context
190 *
191 * Deinitialize the resources initialized by util_audit_init_global(). This
192 * must be called exactly once, after which no more audit functions may be
193 * called.
194 */
195 void util_audit_deinit_global(void) {
196 if (audit_fd < 0)
197 return;
198
199 audit_close(audit_fd);
200 audit_fd = -1;
201 }