]>
Commit | Line | Data |
---|---|---|
85c8721f | 1 | /* audit.c -- Auditing support |
1da177e4 LT |
2 | * Gateway between the kernel (e.g., selinux) and the user-space audit daemon. |
3 | * System-call specific features have moved to auditsc.c | |
4 | * | |
6a01b07f | 5 | * Copyright 2003-2007 Red Hat Inc., Durham, North Carolina. |
1da177e4 LT |
6 | * All Rights Reserved. |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | * | |
22 | * Written by Rickard E. (Rik) Faith <faith@redhat.com> | |
23 | * | |
d7a96f3a | 24 | * Goals: 1) Integrate fully with Security Modules. |
1da177e4 LT |
25 | * 2) Minimal run-time overhead: |
26 | * a) Minimal when syscall auditing is disabled (audit_enable=0). | |
27 | * b) Small when syscall auditing is enabled and no audit record | |
28 | * is generated (defer as much work as possible to record | |
29 | * generation time): | |
30 | * i) context is allocated, | |
31 | * ii) names from getname are stored without a copy, and | |
32 | * iii) inode information stored from path_lookup. | |
33 | * 3) Ability to disable syscall auditing at boot time (audit=0). | |
34 | * 4) Usable by other parts of the kernel (if audit_log* is called, | |
35 | * then a syscall record will be generated automatically for the | |
36 | * current syscall). | |
37 | * 5) Netlink interface to user-space. | |
38 | * 6) Support low-overhead kernel-based filtering to minimize the | |
39 | * information that must be passed to user-space. | |
40 | * | |
85c8721f | 41 | * Example user-space utilities: http://people.redhat.com/sgrubb/audit/ |
1da177e4 LT |
42 | */ |
43 | ||
d957f7b7 JP |
44 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
45 | ||
5b282552 | 46 | #include <linux/file.h> |
1da177e4 | 47 | #include <linux/init.h> |
7153e402 | 48 | #include <linux/types.h> |
60063497 | 49 | #include <linux/atomic.h> |
1da177e4 | 50 | #include <linux/mm.h> |
9984de1a | 51 | #include <linux/export.h> |
5a0e3ad6 | 52 | #include <linux/slab.h> |
b7d11258 DW |
53 | #include <linux/err.h> |
54 | #include <linux/kthread.h> | |
46e959ea | 55 | #include <linux/kernel.h> |
b24a30a7 | 56 | #include <linux/syscalls.h> |
1da177e4 LT |
57 | |
58 | #include <linux/audit.h> | |
59 | ||
60 | #include <net/sock.h> | |
93315ed6 | 61 | #include <net/netlink.h> |
1da177e4 | 62 | #include <linux/skbuff.h> |
131ad62d MDF |
63 | #ifdef CONFIG_SECURITY |
64 | #include <linux/security.h> | |
65 | #endif | |
7dfb7103 | 66 | #include <linux/freezer.h> |
34e36d8e | 67 | #include <linux/pid_namespace.h> |
33faba7f | 68 | #include <net/netns/generic.h> |
3dc7e315 DG |
69 | |
70 | #include "audit.h" | |
1da177e4 | 71 | |
a3f07114 | 72 | /* No auditing will take place until audit_initialized == AUDIT_INITIALIZED. |
1da177e4 | 73 | * (Initialization happens after skb_init is called.) */ |
a3f07114 EP |
74 | #define AUDIT_DISABLED -1 |
75 | #define AUDIT_UNINITIALIZED 0 | |
76 | #define AUDIT_INITIALIZED 1 | |
1da177e4 LT |
77 | static int audit_initialized; |
78 | ||
1a6b9f23 EP |
79 | #define AUDIT_OFF 0 |
80 | #define AUDIT_ON 1 | |
81 | #define AUDIT_LOCKED 2 | |
3e1d0bb6 JP |
82 | u32 audit_enabled; |
83 | u32 audit_ever_enabled; | |
1da177e4 | 84 | |
ae9d67af JE |
85 | EXPORT_SYMBOL_GPL(audit_enabled); |
86 | ||
1da177e4 | 87 | /* Default state when kernel boots without any parameters. */ |
3e1d0bb6 | 88 | static u32 audit_default; |
1da177e4 LT |
89 | |
90 | /* If auditing cannot proceed, audit_failure selects what happens. */ | |
3e1d0bb6 | 91 | static u32 audit_failure = AUDIT_FAIL_PRINTK; |
1da177e4 | 92 | |
75c0371a PE |
93 | /* |
94 | * If audit records are to be written to the netlink socket, audit_pid | |
15e47304 EB |
95 | * contains the pid of the auditd process and audit_nlk_portid contains |
96 | * the portid to use to send netlink messages to that process. | |
75c0371a | 97 | */ |
c2f0c7c3 | 98 | int audit_pid; |
f9441639 | 99 | static __u32 audit_nlk_portid; |
1da177e4 | 100 | |
b0dd25a8 | 101 | /* If audit_rate_limit is non-zero, limit the rate of sending audit records |
1da177e4 LT |
102 | * to that number per second. This prevents DoS attacks, but results in |
103 | * audit records being dropped. */ | |
3e1d0bb6 | 104 | static u32 audit_rate_limit; |
1da177e4 | 105 | |
40c0775e RGB |
106 | /* Number of outstanding audit_buffers allowed. |
107 | * When set to zero, this means unlimited. */ | |
3e1d0bb6 | 108 | static u32 audit_backlog_limit = 64; |
e789e561 | 109 | #define AUDIT_BACKLOG_WAIT_TIME (60 * HZ) |
3e1d0bb6 | 110 | static u32 audit_backlog_wait_time = AUDIT_BACKLOG_WAIT_TIME; |
1da177e4 | 111 | |
c2f0c7c3 | 112 | /* The identity of the user shutting down the audit system. */ |
cca080d9 | 113 | kuid_t audit_sig_uid = INVALID_UID; |
c2f0c7c3 | 114 | pid_t audit_sig_pid = -1; |
e1396065 | 115 | u32 audit_sig_sid = 0; |
c2f0c7c3 | 116 | |
1da177e4 LT |
117 | /* Records can be lost in several ways: |
118 | 0) [suppressed in audit_alloc] | |
119 | 1) out of memory in audit_log_start [kmalloc of struct audit_buffer] | |
120 | 2) out of memory in audit_log_move [alloc_skb] | |
121 | 3) suppressed due to audit_rate_limit | |
122 | 4) suppressed due to audit_backlog_limit | |
123 | */ | |
124 | static atomic_t audit_lost = ATOMIC_INIT(0); | |
125 | ||
126 | /* The netlink socket. */ | |
127 | static struct sock *audit_sock; | |
c0a8d9b0 | 128 | static int audit_net_id; |
1da177e4 | 129 | |
f368c07d AG |
130 | /* Hash for inode-based rules */ |
131 | struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS]; | |
132 | ||
b7d11258 | 133 | /* The audit_freelist is a list of pre-allocated audit buffers (if more |
1da177e4 LT |
134 | * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of |
135 | * being placed on the freelist). */ | |
1da177e4 | 136 | static DEFINE_SPINLOCK(audit_freelist_lock); |
b0dd25a8 | 137 | static int audit_freelist_count; |
1da177e4 LT |
138 | static LIST_HEAD(audit_freelist); |
139 | ||
c6480207 | 140 | /* queue msgs to send via kauditd_task */ |
af8b824f | 141 | static struct sk_buff_head audit_queue; |
c6480207 PM |
142 | /* queue msgs due to temporary unicast send problems */ |
143 | static struct sk_buff_head audit_retry_queue; | |
144 | /* queue msgs waiting for new auditd connection */ | |
af8b824f | 145 | static struct sk_buff_head audit_hold_queue; |
c6480207 PM |
146 | |
147 | /* queue servicing thread */ | |
b7d11258 DW |
148 | static struct task_struct *kauditd_task; |
149 | static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait); | |
c6480207 PM |
150 | |
151 | /* waitqueue for callers who are blocked on the audit backlog */ | |
9ad9ad38 | 152 | static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait); |
1da177e4 | 153 | |
b0fed402 EP |
154 | static struct audit_features af = {.vers = AUDIT_FEATURE_VERSION, |
155 | .mask = -1, | |
156 | .features = 0, | |
157 | .lock = 0,}; | |
158 | ||
21b85c31 | 159 | static char *audit_feature_names[2] = { |
d040e5af | 160 | "only_unset_loginuid", |
21b85c31 | 161 | "loginuid_immutable", |
b0fed402 EP |
162 | }; |
163 | ||
164 | ||
f368c07d | 165 | /* Serialize requests from userspace. */ |
916d7576 | 166 | DEFINE_MUTEX(audit_cmd_mutex); |
1da177e4 LT |
167 | |
168 | /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting | |
169 | * audit records. Since printk uses a 1024 byte buffer, this buffer | |
170 | * should be at least that large. */ | |
171 | #define AUDIT_BUFSIZ 1024 | |
172 | ||
173 | /* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the | |
174 | * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */ | |
175 | #define AUDIT_MAXFREE (2*NR_CPUS) | |
176 | ||
177 | /* The audit_buffer is used when formatting an audit record. The caller | |
178 | * locks briefly to get the record off the freelist or to allocate the | |
179 | * buffer, and locks briefly to send the buffer to the netlink layer or | |
180 | * to place it on a transmit queue. Multiple audit_buffers can be in | |
181 | * use simultaneously. */ | |
182 | struct audit_buffer { | |
183 | struct list_head list; | |
8fc6115c | 184 | struct sk_buff *skb; /* formatted skb ready to send */ |
1da177e4 | 185 | struct audit_context *ctx; /* NULL or associated context */ |
9796fdd8 | 186 | gfp_t gfp_mask; |
1da177e4 LT |
187 | }; |
188 | ||
f09ac9db | 189 | struct audit_reply { |
f9441639 | 190 | __u32 portid; |
638a0fd2 | 191 | struct net *net; |
f09ac9db EP |
192 | struct sk_buff *skb; |
193 | }; | |
194 | ||
f9441639 | 195 | static void audit_set_portid(struct audit_buffer *ab, __u32 portid) |
c0404993 | 196 | { |
50397bd1 EP |
197 | if (ab) { |
198 | struct nlmsghdr *nlh = nlmsg_hdr(ab->skb); | |
f9441639 | 199 | nlh->nlmsg_pid = portid; |
50397bd1 | 200 | } |
c0404993 SG |
201 | } |
202 | ||
8c8570fb | 203 | void audit_panic(const char *message) |
1da177e4 | 204 | { |
d957f7b7 | 205 | switch (audit_failure) { |
1da177e4 LT |
206 | case AUDIT_FAIL_SILENT: |
207 | break; | |
208 | case AUDIT_FAIL_PRINTK: | |
320f1b1e | 209 | if (printk_ratelimit()) |
d957f7b7 | 210 | pr_err("%s\n", message); |
1da177e4 LT |
211 | break; |
212 | case AUDIT_FAIL_PANIC: | |
b29ee87e EP |
213 | /* test audit_pid since printk is always losey, why bother? */ |
214 | if (audit_pid) | |
215 | panic("audit: %s\n", message); | |
1da177e4 LT |
216 | break; |
217 | } | |
218 | } | |
219 | ||
220 | static inline int audit_rate_check(void) | |
221 | { | |
222 | static unsigned long last_check = 0; | |
223 | static int messages = 0; | |
224 | static DEFINE_SPINLOCK(lock); | |
225 | unsigned long flags; | |
226 | unsigned long now; | |
227 | unsigned long elapsed; | |
228 | int retval = 0; | |
229 | ||
230 | if (!audit_rate_limit) return 1; | |
231 | ||
232 | spin_lock_irqsave(&lock, flags); | |
233 | if (++messages < audit_rate_limit) { | |
234 | retval = 1; | |
235 | } else { | |
236 | now = jiffies; | |
237 | elapsed = now - last_check; | |
238 | if (elapsed > HZ) { | |
239 | last_check = now; | |
240 | messages = 0; | |
241 | retval = 1; | |
242 | } | |
243 | } | |
244 | spin_unlock_irqrestore(&lock, flags); | |
245 | ||
246 | return retval; | |
247 | } | |
248 | ||
b0dd25a8 RD |
249 | /** |
250 | * audit_log_lost - conditionally log lost audit message event | |
251 | * @message: the message stating reason for lost audit message | |
252 | * | |
253 | * Emit at least 1 message per second, even if audit_rate_check is | |
254 | * throttling. | |
255 | * Always increment the lost messages counter. | |
256 | */ | |
1da177e4 LT |
257 | void audit_log_lost(const char *message) |
258 | { | |
259 | static unsigned long last_msg = 0; | |
260 | static DEFINE_SPINLOCK(lock); | |
261 | unsigned long flags; | |
262 | unsigned long now; | |
263 | int print; | |
264 | ||
265 | atomic_inc(&audit_lost); | |
266 | ||
267 | print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit); | |
268 | ||
269 | if (!print) { | |
270 | spin_lock_irqsave(&lock, flags); | |
271 | now = jiffies; | |
272 | if (now - last_msg > HZ) { | |
273 | print = 1; | |
274 | last_msg = now; | |
275 | } | |
276 | spin_unlock_irqrestore(&lock, flags); | |
277 | } | |
278 | ||
279 | if (print) { | |
320f1b1e | 280 | if (printk_ratelimit()) |
3e1d0bb6 | 281 | pr_warn("audit_lost=%u audit_rate_limit=%u audit_backlog_limit=%u\n", |
320f1b1e EP |
282 | atomic_read(&audit_lost), |
283 | audit_rate_limit, | |
284 | audit_backlog_limit); | |
1da177e4 LT |
285 | audit_panic(message); |
286 | } | |
1da177e4 LT |
287 | } |
288 | ||
3e1d0bb6 | 289 | static int audit_log_config_change(char *function_name, u32 new, u32 old, |
2532386f | 290 | int allow_changes) |
1da177e4 | 291 | { |
1a6b9f23 EP |
292 | struct audit_buffer *ab; |
293 | int rc = 0; | |
ce29b682 | 294 | |
1a6b9f23 | 295 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE); |
0644ec0c KC |
296 | if (unlikely(!ab)) |
297 | return rc; | |
3e1d0bb6 | 298 | audit_log_format(ab, "%s=%u old=%u", function_name, new, old); |
4d3fb709 | 299 | audit_log_session_info(ab); |
b122c376 EP |
300 | rc = audit_log_task_context(ab); |
301 | if (rc) | |
302 | allow_changes = 0; /* Something weird, deny request */ | |
1a6b9f23 EP |
303 | audit_log_format(ab, " res=%d", allow_changes); |
304 | audit_log_end(ab); | |
6a01b07f | 305 | return rc; |
1da177e4 LT |
306 | } |
307 | ||
3e1d0bb6 | 308 | static int audit_do_config_change(char *function_name, u32 *to_change, u32 new) |
1da177e4 | 309 | { |
3e1d0bb6 JP |
310 | int allow_changes, rc = 0; |
311 | u32 old = *to_change; | |
6a01b07f SG |
312 | |
313 | /* check if we are locked */ | |
1a6b9f23 EP |
314 | if (audit_enabled == AUDIT_LOCKED) |
315 | allow_changes = 0; | |
6a01b07f | 316 | else |
1a6b9f23 | 317 | allow_changes = 1; |
ce29b682 | 318 | |
1a6b9f23 | 319 | if (audit_enabled != AUDIT_OFF) { |
dc9eb698 | 320 | rc = audit_log_config_change(function_name, new, old, allow_changes); |
1a6b9f23 EP |
321 | if (rc) |
322 | allow_changes = 0; | |
6a01b07f | 323 | } |
6a01b07f SG |
324 | |
325 | /* If we are allowed, make the change */ | |
1a6b9f23 EP |
326 | if (allow_changes == 1) |
327 | *to_change = new; | |
6a01b07f SG |
328 | /* Not allowed, update reason */ |
329 | else if (rc == 0) | |
330 | rc = -EPERM; | |
331 | return rc; | |
1da177e4 LT |
332 | } |
333 | ||
3e1d0bb6 | 334 | static int audit_set_rate_limit(u32 limit) |
1da177e4 | 335 | { |
dc9eb698 | 336 | return audit_do_config_change("audit_rate_limit", &audit_rate_limit, limit); |
1a6b9f23 | 337 | } |
ce29b682 | 338 | |
3e1d0bb6 | 339 | static int audit_set_backlog_limit(u32 limit) |
1a6b9f23 | 340 | { |
dc9eb698 | 341 | return audit_do_config_change("audit_backlog_limit", &audit_backlog_limit, limit); |
1a6b9f23 | 342 | } |
6a01b07f | 343 | |
3e1d0bb6 | 344 | static int audit_set_backlog_wait_time(u32 timeout) |
51cc83f0 RGB |
345 | { |
346 | return audit_do_config_change("audit_backlog_wait_time", | |
31975424 | 347 | &audit_backlog_wait_time, timeout); |
51cc83f0 RGB |
348 | } |
349 | ||
3e1d0bb6 | 350 | static int audit_set_enabled(u32 state) |
1a6b9f23 | 351 | { |
b593d384 | 352 | int rc; |
724e7bfc | 353 | if (state > AUDIT_LOCKED) |
1a6b9f23 | 354 | return -EINVAL; |
6a01b07f | 355 | |
dc9eb698 | 356 | rc = audit_do_config_change("audit_enabled", &audit_enabled, state); |
b593d384 EP |
357 | if (!rc) |
358 | audit_ever_enabled |= !!state; | |
359 | ||
360 | return rc; | |
1da177e4 LT |
361 | } |
362 | ||
3e1d0bb6 | 363 | static int audit_set_failure(u32 state) |
1da177e4 | 364 | { |
1da177e4 LT |
365 | if (state != AUDIT_FAIL_SILENT |
366 | && state != AUDIT_FAIL_PRINTK | |
367 | && state != AUDIT_FAIL_PANIC) | |
368 | return -EINVAL; | |
ce29b682 | 369 | |
dc9eb698 | 370 | return audit_do_config_change("audit_failure", &audit_failure, state); |
1da177e4 LT |
371 | } |
372 | ||
038cbcf6 EP |
373 | /* |
374 | * For one reason or another this nlh isn't getting delivered to the userspace | |
375 | * audit daemon, just send it to printk. | |
376 | */ | |
af8b824f | 377 | static void kauditd_printk_skb(struct sk_buff *skb) |
038cbcf6 EP |
378 | { |
379 | struct nlmsghdr *nlh = nlmsg_hdr(skb); | |
c64e66c6 | 380 | char *data = nlmsg_data(nlh); |
038cbcf6 EP |
381 | |
382 | if (nlh->nlmsg_type != AUDIT_EOE) { | |
383 | if (printk_ratelimit()) | |
d957f7b7 | 384 | pr_notice("type=%d %s\n", nlh->nlmsg_type, data); |
038cbcf6 | 385 | else |
f1283527 | 386 | audit_log_lost("printk limit exceeded"); |
038cbcf6 | 387 | } |
c6480207 PM |
388 | } |
389 | ||
390 | /** | |
391 | * kauditd_hold_skb - Queue an audit record, waiting for auditd | |
392 | * @skb: audit record | |
393 | * | |
394 | * Description: | |
395 | * Queue the audit record, waiting for an instance of auditd. When this | |
396 | * function is called we haven't given up yet on sending the record, but things | |
397 | * are not looking good. The first thing we want to do is try to write the | |
398 | * record via printk and then see if we want to try and hold on to the record | |
399 | * and queue it, if we have room. If we want to hold on to the record, but we | |
400 | * don't have room, record a record lost message. | |
401 | */ | |
402 | static void kauditd_hold_skb(struct sk_buff *skb) | |
403 | { | |
404 | /* at this point it is uncertain if we will ever send this to auditd so | |
405 | * try to send the message via printk before we go any further */ | |
406 | kauditd_printk_skb(skb); | |
407 | ||
408 | /* can we just silently drop the message? */ | |
409 | if (!audit_default) { | |
410 | kfree_skb(skb); | |
411 | return; | |
412 | } | |
413 | ||
414 | /* if we have room, queue the message */ | |
415 | if (!audit_backlog_limit || | |
416 | skb_queue_len(&audit_hold_queue) < audit_backlog_limit) { | |
417 | skb_queue_tail(&audit_hold_queue, skb); | |
418 | return; | |
419 | } | |
038cbcf6 | 420 | |
c6480207 PM |
421 | /* we have no other options - drop the message */ |
422 | audit_log_lost("kauditd hold queue overflow"); | |
423 | kfree_skb(skb); | |
038cbcf6 EP |
424 | } |
425 | ||
c6480207 PM |
426 | /** |
427 | * kauditd_retry_skb - Queue an audit record, attempt to send again to auditd | |
428 | * @skb: audit record | |
429 | * | |
430 | * Description: | |
431 | * Not as serious as kauditd_hold_skb() as we still have a connected auditd, | |
432 | * but for some reason we are having problems sending it audit records so | |
433 | * queue the given record and attempt to resend. | |
434 | */ | |
435 | static void kauditd_retry_skb(struct sk_buff *skb) | |
f3d357b0 | 436 | { |
c6480207 PM |
437 | /* NOTE: because records should only live in the retry queue for a |
438 | * short period of time, before either being sent or moved to the hold | |
439 | * queue, we don't currently enforce a limit on this queue */ | |
440 | skb_queue_tail(&audit_retry_queue, skb); | |
441 | } | |
32a1dbae | 442 | |
c6480207 PM |
443 | /** |
444 | * auditd_reset - Disconnect the auditd connection | |
445 | * | |
446 | * Description: | |
447 | * Break the auditd/kauditd connection and move all the records in the retry | |
448 | * queue into the hold queue in case auditd reconnects. | |
449 | */ | |
450 | static void auditd_reset(void) | |
451 | { | |
452 | struct sk_buff *skb; | |
453 | ||
454 | /* break the connection */ | |
455 | audit_pid = 0; | |
456 | audit_sock = NULL; | |
457 | ||
458 | /* flush all of the retry queue to the hold queue */ | |
459 | while ((skb = skb_dequeue(&audit_retry_queue))) | |
460 | kauditd_hold_skb(skb); | |
461 | } | |
462 | ||
463 | /** | |
464 | * kauditd_send_unicast_skb - Send a record via unicast to auditd | |
465 | * @skb: audit record | |
466 | */ | |
467 | static int kauditd_send_unicast_skb(struct sk_buff *skb) | |
468 | { | |
469 | int rc; | |
470 | ||
471 | /* get an extra skb reference in case we fail to send */ | |
f3d357b0 | 472 | skb_get(skb); |
c6480207 PM |
473 | rc = netlink_unicast(audit_sock, skb, audit_nlk_portid, 0); |
474 | if (rc >= 0) { | |
70d4bf6d | 475 | consume_skb(skb); |
c6480207 PM |
476 | rc = 0; |
477 | } | |
478 | ||
479 | return rc; | |
f3d357b0 EP |
480 | } |
481 | ||
451f9216 | 482 | /* |
c6480207 PM |
483 | * kauditd_send_multicast_skb - Send a record to any multicast listeners |
484 | * @skb: audit record | |
451f9216 | 485 | * |
c6480207 | 486 | * Description: |
451f9216 RGB |
487 | * This function doesn't consume an skb as might be expected since it has to |
488 | * copy it anyways. | |
489 | */ | |
c6480207 | 490 | static void kauditd_send_multicast_skb(struct sk_buff *skb) |
451f9216 | 491 | { |
c6480207 PM |
492 | struct sk_buff *copy; |
493 | struct audit_net *aunet = net_generic(&init_net, audit_net_id); | |
494 | struct sock *sock = aunet->nlsk; | |
495 | struct nlmsghdr *nlh; | |
451f9216 | 496 | |
7f74ecd7 RGB |
497 | if (!netlink_has_listeners(sock, AUDIT_NLGRP_READLOG)) |
498 | return; | |
499 | ||
451f9216 RGB |
500 | /* |
501 | * The seemingly wasteful skb_copy() rather than bumping the refcount | |
502 | * using skb_get() is necessary because non-standard mods are made to | |
503 | * the skb by the original kaudit unicast socket send routine. The | |
504 | * existing auditd daemon assumes this breakage. Fixing this would | |
505 | * require co-ordinating a change in the established protocol between | |
506 | * the kaudit kernel subsystem and the auditd userspace code. There is | |
507 | * no reason for new multicast clients to continue with this | |
508 | * non-compliance. | |
509 | */ | |
c6480207 | 510 | copy = skb_copy(skb, GFP_KERNEL); |
451f9216 RGB |
511 | if (!copy) |
512 | return; | |
c6480207 PM |
513 | nlh = nlmsg_hdr(copy); |
514 | nlh->nlmsg_len = skb->len; | |
451f9216 | 515 | |
c6480207 | 516 | nlmsg_multicast(sock, copy, 0, AUDIT_NLGRP_READLOG, GFP_KERNEL); |
451f9216 RGB |
517 | } |
518 | ||
c6480207 PM |
519 | /** |
520 | * kauditd_wake_condition - Return true when it is time to wake kauditd_thread | |
b551d1d9 | 521 | * |
c6480207 PM |
522 | * Description: |
523 | * This function is for use by the wait_event_freezable() call in | |
524 | * kauditd_thread(). | |
b551d1d9 | 525 | */ |
c6480207 | 526 | static int kauditd_wake_condition(void) |
b7d11258 | 527 | { |
c6480207 PM |
528 | static int pid_last = 0; |
529 | int rc; | |
530 | int pid = audit_pid; | |
b551d1d9 | 531 | |
c6480207 PM |
532 | /* wake on new messages or a change in the connected auditd */ |
533 | rc = skb_queue_len(&audit_queue) || (pid && pid != pid_last); | |
534 | if (rc) | |
535 | pid_last = pid; | |
b551d1d9 | 536 | |
c6480207 | 537 | return rc; |
b551d1d9 RGB |
538 | } |
539 | ||
97a41e26 | 540 | static int kauditd_thread(void *dummy) |
b7d11258 | 541 | { |
c6480207 PM |
542 | int rc; |
543 | int auditd = 0; | |
544 | int reschedule = 0; | |
4aa83872 PM |
545 | struct sk_buff *skb; |
546 | struct nlmsghdr *nlh; | |
547 | ||
c6480207 PM |
548 | #define UNICAST_RETRIES 5 |
549 | #define AUDITD_BAD(x,y) \ | |
550 | ((x) == -ECONNREFUSED || (x) == -EPERM || ++(y) >= UNICAST_RETRIES) | |
551 | ||
552 | /* NOTE: we do invalidate the auditd connection flag on any sending | |
553 | * errors, but we only "restore" the connection flag at specific places | |
554 | * in the loop in order to help ensure proper ordering of audit | |
555 | * records */ | |
556 | ||
83144186 | 557 | set_freezable(); |
4899b8b1 | 558 | while (!kthread_should_stop()) { |
c6480207 PM |
559 | /* NOTE: possible area for future improvement is to look at |
560 | * the hold and retry queues, since only this thread | |
561 | * has access to these queues we might be able to do | |
562 | * our own queuing and skip some/all of the locking */ | |
563 | ||
564 | /* NOTE: it might be a fun experiment to split the hold and | |
565 | * retry queue handling to another thread, but the | |
566 | * synchronization issues and other overhead might kill | |
567 | * any performance gains */ | |
568 | ||
569 | /* attempt to flush the hold queue */ | |
570 | while (auditd && (skb = skb_dequeue(&audit_hold_queue))) { | |
571 | rc = kauditd_send_unicast_skb(skb); | |
572 | if (rc) { | |
573 | /* requeue to the same spot */ | |
574 | skb_queue_head(&audit_hold_queue, skb); | |
575 | ||
576 | auditd = 0; | |
577 | if (AUDITD_BAD(rc, reschedule)) { | |
578 | auditd_reset(); | |
579 | reschedule = 0; | |
580 | } | |
581 | } else | |
582 | /* we were able to send successfully */ | |
583 | reschedule = 0; | |
584 | } | |
585 | ||
586 | /* attempt to flush the retry queue */ | |
587 | while (auditd && (skb = skb_dequeue(&audit_retry_queue))) { | |
588 | rc = kauditd_send_unicast_skb(skb); | |
589 | if (rc) { | |
590 | auditd = 0; | |
591 | if (AUDITD_BAD(rc, reschedule)) { | |
592 | kauditd_hold_skb(skb); | |
593 | auditd_reset(); | |
594 | reschedule = 0; | |
595 | } else | |
596 | /* temporary problem (we hope), queue | |
597 | * to the same spot and retry */ | |
598 | skb_queue_head(&audit_retry_queue, skb); | |
599 | } else | |
600 | /* we were able to send successfully */ | |
601 | reschedule = 0; | |
602 | } | |
f3d357b0 | 603 | |
c6480207 PM |
604 | /* standard queue processing, try to be as quick as possible */ |
605 | quick_loop: | |
af8b824f | 606 | skb = skb_dequeue(&audit_queue); |
b7d11258 | 607 | if (skb) { |
c6480207 PM |
608 | /* setup the netlink header, see the comments in |
609 | * kauditd_send_multicast_skb() for length quirks */ | |
4aa83872 | 610 | nlh = nlmsg_hdr(skb); |
c6480207 PM |
611 | nlh->nlmsg_len = skb->len - NLMSG_HDRLEN; |
612 | ||
613 | /* attempt to send to any multicast listeners */ | |
614 | kauditd_send_multicast_skb(skb); | |
615 | ||
616 | /* attempt to send to auditd, queue on failure */ | |
617 | if (auditd) { | |
618 | rc = kauditd_send_unicast_skb(skb); | |
619 | if (rc) { | |
620 | auditd = 0; | |
621 | if (AUDITD_BAD(rc, reschedule)) { | |
622 | auditd_reset(); | |
623 | reschedule = 0; | |
624 | } | |
625 | ||
626 | /* move to the retry queue */ | |
627 | kauditd_retry_skb(skb); | |
628 | } else | |
629 | /* everything is working so go fast! */ | |
630 | goto quick_loop; | |
631 | } else if (reschedule) | |
632 | /* we are currently having problems, move to | |
633 | * the retry queue */ | |
634 | kauditd_retry_skb(skb); | |
038cbcf6 | 635 | else |
c6480207 PM |
636 | /* dump the message via printk and hold it */ |
637 | kauditd_hold_skb(skb); | |
4aa83872 | 638 | } else { |
c6480207 | 639 | /* we have flushed the backlog so wake everyone */ |
4aa83872 | 640 | wake_up(&audit_backlog_wait); |
c6480207 PM |
641 | |
642 | /* if everything is okay with auditd (if present), go | |
643 | * to sleep until there is something new in the queue | |
644 | * or we have a change in the connected auditd; | |
645 | * otherwise simply reschedule to give things a chance | |
646 | * to recover */ | |
647 | if (reschedule) { | |
648 | set_current_state(TASK_INTERRUPTIBLE); | |
649 | schedule(); | |
650 | } else | |
651 | wait_event_freezable(kauditd_wait, | |
652 | kauditd_wake_condition()); | |
653 | ||
654 | /* update the auditd connection status */ | |
655 | auditd = (audit_pid ? 1 : 0); | |
3320c513 | 656 | } |
b7d11258 | 657 | } |
c6480207 | 658 | |
4899b8b1 | 659 | return 0; |
b7d11258 DW |
660 | } |
661 | ||
9044e6bc AV |
662 | int audit_send_list(void *_dest) |
663 | { | |
664 | struct audit_netlink_list *dest = _dest; | |
9044e6bc | 665 | struct sk_buff *skb; |
48095d99 | 666 | struct net *net = dest->net; |
33faba7f | 667 | struct audit_net *aunet = net_generic(net, audit_net_id); |
9044e6bc AV |
668 | |
669 | /* wait for parent to finish and send an ACK */ | |
f368c07d AG |
670 | mutex_lock(&audit_cmd_mutex); |
671 | mutex_unlock(&audit_cmd_mutex); | |
9044e6bc AV |
672 | |
673 | while ((skb = __skb_dequeue(&dest->q)) != NULL) | |
33faba7f | 674 | netlink_unicast(aunet->nlsk, skb, dest->portid, 0); |
9044e6bc | 675 | |
48095d99 | 676 | put_net(net); |
9044e6bc AV |
677 | kfree(dest); |
678 | ||
679 | return 0; | |
680 | } | |
681 | ||
f9441639 | 682 | struct sk_buff *audit_make_reply(__u32 portid, int seq, int type, int done, |
b8800aa5 | 683 | int multi, const void *payload, int size) |
9044e6bc AV |
684 | { |
685 | struct sk_buff *skb; | |
686 | struct nlmsghdr *nlh; | |
9044e6bc AV |
687 | void *data; |
688 | int flags = multi ? NLM_F_MULTI : 0; | |
689 | int t = done ? NLMSG_DONE : type; | |
690 | ||
ee080e6c | 691 | skb = nlmsg_new(size, GFP_KERNEL); |
9044e6bc AV |
692 | if (!skb) |
693 | return NULL; | |
694 | ||
f9441639 | 695 | nlh = nlmsg_put(skb, portid, seq, t, size, flags); |
c64e66c6 DM |
696 | if (!nlh) |
697 | goto out_kfree_skb; | |
698 | data = nlmsg_data(nlh); | |
9044e6bc AV |
699 | memcpy(data, payload, size); |
700 | return skb; | |
701 | ||
c64e66c6 DM |
702 | out_kfree_skb: |
703 | kfree_skb(skb); | |
9044e6bc AV |
704 | return NULL; |
705 | } | |
706 | ||
f09ac9db EP |
707 | static int audit_send_reply_thread(void *arg) |
708 | { | |
709 | struct audit_reply *reply = (struct audit_reply *)arg; | |
48095d99 | 710 | struct net *net = reply->net; |
33faba7f | 711 | struct audit_net *aunet = net_generic(net, audit_net_id); |
f09ac9db EP |
712 | |
713 | mutex_lock(&audit_cmd_mutex); | |
714 | mutex_unlock(&audit_cmd_mutex); | |
715 | ||
716 | /* Ignore failure. It'll only happen if the sender goes away, | |
717 | because our timeout is set to infinite. */ | |
33faba7f | 718 | netlink_unicast(aunet->nlsk , reply->skb, reply->portid, 0); |
48095d99 | 719 | put_net(net); |
f09ac9db EP |
720 | kfree(reply); |
721 | return 0; | |
722 | } | |
c6480207 | 723 | |
b0dd25a8 RD |
724 | /** |
725 | * audit_send_reply - send an audit reply message via netlink | |
d211f177 | 726 | * @request_skb: skb of request we are replying to (used to target the reply) |
b0dd25a8 RD |
727 | * @seq: sequence number |
728 | * @type: audit message type | |
729 | * @done: done (last) flag | |
730 | * @multi: multi-part message flag | |
731 | * @payload: payload data | |
732 | * @size: payload size | |
733 | * | |
f9441639 | 734 | * Allocates an skb, builds the netlink message, and sends it to the port id. |
b0dd25a8 RD |
735 | * No failure notifications. |
736 | */ | |
6f285b19 | 737 | static void audit_send_reply(struct sk_buff *request_skb, int seq, int type, int done, |
f9441639 | 738 | int multi, const void *payload, int size) |
1da177e4 | 739 | { |
6f285b19 EB |
740 | u32 portid = NETLINK_CB(request_skb).portid; |
741 | struct net *net = sock_net(NETLINK_CB(request_skb).sk); | |
f09ac9db EP |
742 | struct sk_buff *skb; |
743 | struct task_struct *tsk; | |
744 | struct audit_reply *reply = kmalloc(sizeof(struct audit_reply), | |
745 | GFP_KERNEL); | |
746 | ||
747 | if (!reply) | |
748 | return; | |
749 | ||
f9441639 | 750 | skb = audit_make_reply(portid, seq, type, done, multi, payload, size); |
1da177e4 | 751 | if (!skb) |
fcaf1eb8 | 752 | goto out; |
f09ac9db | 753 | |
6f285b19 | 754 | reply->net = get_net(net); |
f9441639 | 755 | reply->portid = portid; |
f09ac9db EP |
756 | reply->skb = skb; |
757 | ||
758 | tsk = kthread_run(audit_send_reply_thread, reply, "audit_send_reply"); | |
fcaf1eb8 AM |
759 | if (!IS_ERR(tsk)) |
760 | return; | |
761 | kfree_skb(skb); | |
762 | out: | |
763 | kfree(reply); | |
1da177e4 LT |
764 | } |
765 | ||
766 | /* | |
767 | * Check for appropriate CAP_AUDIT_ capabilities on incoming audit | |
768 | * control messages. | |
769 | */ | |
c7bdb545 | 770 | static int audit_netlink_ok(struct sk_buff *skb, u16 msg_type) |
1da177e4 LT |
771 | { |
772 | int err = 0; | |
773 | ||
5a3cb3b6 | 774 | /* Only support initial user namespace for now. */ |
aa4af831 EP |
775 | /* |
776 | * We return ECONNREFUSED because it tricks userspace into thinking | |
777 | * that audit was not configured into the kernel. Lots of users | |
778 | * configure their PAM stack (because that's what the distro does) | |
779 | * to reject login if unable to send messages to audit. If we return | |
780 | * ECONNREFUSED the PAM stack thinks the kernel does not have audit | |
781 | * configured in and will let login proceed. If we return EPERM | |
782 | * userspace will reject all logins. This should be removed when we | |
783 | * support non init namespaces!! | |
784 | */ | |
0b747172 | 785 | if (current_user_ns() != &init_user_ns) |
aa4af831 | 786 | return -ECONNREFUSED; |
34e36d8e | 787 | |
1da177e4 | 788 | switch (msg_type) { |
1da177e4 | 789 | case AUDIT_LIST: |
1da177e4 LT |
790 | case AUDIT_ADD: |
791 | case AUDIT_DEL: | |
18900909 EP |
792 | return -EOPNOTSUPP; |
793 | case AUDIT_GET: | |
794 | case AUDIT_SET: | |
b0fed402 EP |
795 | case AUDIT_GET_FEATURE: |
796 | case AUDIT_SET_FEATURE: | |
18900909 EP |
797 | case AUDIT_LIST_RULES: |
798 | case AUDIT_ADD_RULE: | |
93315ed6 | 799 | case AUDIT_DEL_RULE: |
c2f0c7c3 | 800 | case AUDIT_SIGNAL_INFO: |
522ed776 MT |
801 | case AUDIT_TTY_GET: |
802 | case AUDIT_TTY_SET: | |
74c3cbe3 AV |
803 | case AUDIT_TRIM: |
804 | case AUDIT_MAKE_EQUIV: | |
5a3cb3b6 RGB |
805 | /* Only support auditd and auditctl in initial pid namespace |
806 | * for now. */ | |
5985de67 | 807 | if (task_active_pid_ns(current) != &init_pid_ns) |
5a3cb3b6 RGB |
808 | return -EPERM; |
809 | ||
90f62cf3 | 810 | if (!netlink_capable(skb, CAP_AUDIT_CONTROL)) |
1da177e4 LT |
811 | err = -EPERM; |
812 | break; | |
05474106 | 813 | case AUDIT_USER: |
039b6b3e RD |
814 | case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG: |
815 | case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2: | |
90f62cf3 | 816 | if (!netlink_capable(skb, CAP_AUDIT_WRITE)) |
1da177e4 LT |
817 | err = -EPERM; |
818 | break; | |
819 | default: /* bad msg */ | |
820 | err = -EINVAL; | |
821 | } | |
822 | ||
823 | return err; | |
824 | } | |
825 | ||
233a6866 | 826 | static void audit_log_common_recv_msg(struct audit_buffer **ab, u16 msg_type) |
50397bd1 | 827 | { |
dc9eb698 | 828 | uid_t uid = from_kuid(&init_user_ns, current_uid()); |
f1dc4867 | 829 | pid_t pid = task_tgid_nr(current); |
50397bd1 | 830 | |
0868a5e1 | 831 | if (!audit_enabled && msg_type != AUDIT_USER_AVC) { |
50397bd1 | 832 | *ab = NULL; |
233a6866 | 833 | return; |
50397bd1 EP |
834 | } |
835 | ||
836 | *ab = audit_log_start(NULL, GFP_KERNEL, msg_type); | |
0644ec0c | 837 | if (unlikely(!*ab)) |
233a6866 | 838 | return; |
f1dc4867 | 839 | audit_log_format(*ab, "pid=%d uid=%u", pid, uid); |
4d3fb709 | 840 | audit_log_session_info(*ab); |
b122c376 | 841 | audit_log_task_context(*ab); |
50397bd1 EP |
842 | } |
843 | ||
b0fed402 EP |
844 | int is_audit_feature_set(int i) |
845 | { | |
846 | return af.features & AUDIT_FEATURE_TO_MASK(i); | |
847 | } | |
848 | ||
849 | ||
850 | static int audit_get_feature(struct sk_buff *skb) | |
851 | { | |
852 | u32 seq; | |
853 | ||
854 | seq = nlmsg_hdr(skb)->nlmsg_seq; | |
855 | ||
9ef91514 | 856 | audit_send_reply(skb, seq, AUDIT_GET_FEATURE, 0, 0, &af, sizeof(af)); |
b0fed402 EP |
857 | |
858 | return 0; | |
859 | } | |
860 | ||
861 | static void audit_log_feature_change(int which, u32 old_feature, u32 new_feature, | |
862 | u32 old_lock, u32 new_lock, int res) | |
863 | { | |
864 | struct audit_buffer *ab; | |
865 | ||
b6c50fe0 G |
866 | if (audit_enabled == AUDIT_OFF) |
867 | return; | |
868 | ||
b0fed402 | 869 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_FEATURE_CHANGE); |
ad2ac263 | 870 | audit_log_task_info(ab, current); |
897f1acb | 871 | audit_log_format(ab, " feature=%s old=%u new=%u old_lock=%u new_lock=%u res=%d", |
b0fed402 EP |
872 | audit_feature_names[which], !!old_feature, !!new_feature, |
873 | !!old_lock, !!new_lock, res); | |
874 | audit_log_end(ab); | |
875 | } | |
876 | ||
877 | static int audit_set_feature(struct sk_buff *skb) | |
878 | { | |
879 | struct audit_features *uaf; | |
880 | int i; | |
881 | ||
6eed9b26 | 882 | BUILD_BUG_ON(AUDIT_LAST_FEATURE + 1 > ARRAY_SIZE(audit_feature_names)); |
b0fed402 EP |
883 | uaf = nlmsg_data(nlmsg_hdr(skb)); |
884 | ||
885 | /* if there is ever a version 2 we should handle that here */ | |
886 | ||
887 | for (i = 0; i <= AUDIT_LAST_FEATURE; i++) { | |
888 | u32 feature = AUDIT_FEATURE_TO_MASK(i); | |
889 | u32 old_feature, new_feature, old_lock, new_lock; | |
890 | ||
891 | /* if we are not changing this feature, move along */ | |
892 | if (!(feature & uaf->mask)) | |
893 | continue; | |
894 | ||
895 | old_feature = af.features & feature; | |
896 | new_feature = uaf->features & feature; | |
897 | new_lock = (uaf->lock | af.lock) & feature; | |
898 | old_lock = af.lock & feature; | |
899 | ||
900 | /* are we changing a locked feature? */ | |
4547b3bc | 901 | if (old_lock && (new_feature != old_feature)) { |
b0fed402 EP |
902 | audit_log_feature_change(i, old_feature, new_feature, |
903 | old_lock, new_lock, 0); | |
904 | return -EPERM; | |
905 | } | |
906 | } | |
907 | /* nothing invalid, do the changes */ | |
908 | for (i = 0; i <= AUDIT_LAST_FEATURE; i++) { | |
909 | u32 feature = AUDIT_FEATURE_TO_MASK(i); | |
910 | u32 old_feature, new_feature, old_lock, new_lock; | |
911 | ||
912 | /* if we are not changing this feature, move along */ | |
913 | if (!(feature & uaf->mask)) | |
914 | continue; | |
915 | ||
916 | old_feature = af.features & feature; | |
917 | new_feature = uaf->features & feature; | |
918 | old_lock = af.lock & feature; | |
919 | new_lock = (uaf->lock | af.lock) & feature; | |
920 | ||
921 | if (new_feature != old_feature) | |
922 | audit_log_feature_change(i, old_feature, new_feature, | |
923 | old_lock, new_lock, 1); | |
924 | ||
925 | if (new_feature) | |
926 | af.features |= feature; | |
927 | else | |
928 | af.features &= ~feature; | |
929 | af.lock |= new_lock; | |
930 | } | |
931 | ||
932 | return 0; | |
933 | } | |
934 | ||
133e1e5a RGB |
935 | static int audit_replace(pid_t pid) |
936 | { | |
937 | struct sk_buff *skb = audit_make_reply(0, 0, AUDIT_REPLACE, 0, 0, | |
938 | &pid, sizeof(pid)); | |
939 | ||
940 | if (!skb) | |
941 | return -ENOMEM; | |
942 | return netlink_unicast(audit_sock, skb, audit_nlk_portid, 0); | |
943 | } | |
944 | ||
1da177e4 LT |
945 | static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
946 | { | |
dc9eb698 | 947 | u32 seq; |
1da177e4 | 948 | void *data; |
1da177e4 | 949 | int err; |
c0404993 | 950 | struct audit_buffer *ab; |
1da177e4 | 951 | u16 msg_type = nlh->nlmsg_type; |
e1396065 | 952 | struct audit_sig_info *sig_data; |
50397bd1 | 953 | char *ctx = NULL; |
e1396065 | 954 | u32 len; |
1da177e4 | 955 | |
c7bdb545 | 956 | err = audit_netlink_ok(skb, msg_type); |
1da177e4 LT |
957 | if (err) |
958 | return err; | |
959 | ||
1da177e4 | 960 | seq = nlh->nlmsg_seq; |
c64e66c6 | 961 | data = nlmsg_data(nlh); |
1da177e4 LT |
962 | |
963 | switch (msg_type) { | |
09f883a9 RGB |
964 | case AUDIT_GET: { |
965 | struct audit_status s; | |
966 | memset(&s, 0, sizeof(s)); | |
967 | s.enabled = audit_enabled; | |
968 | s.failure = audit_failure; | |
969 | s.pid = audit_pid; | |
970 | s.rate_limit = audit_rate_limit; | |
971 | s.backlog_limit = audit_backlog_limit; | |
972 | s.lost = atomic_read(&audit_lost); | |
af8b824f | 973 | s.backlog = skb_queue_len(&audit_queue); |
0288d718 | 974 | s.feature_bitmap = AUDIT_FEATURE_BITMAP_ALL; |
31975424 | 975 | s.backlog_wait_time = audit_backlog_wait_time; |
6f285b19 | 976 | audit_send_reply(skb, seq, AUDIT_GET, 0, 0, &s, sizeof(s)); |
1da177e4 | 977 | break; |
09f883a9 RGB |
978 | } |
979 | case AUDIT_SET: { | |
980 | struct audit_status s; | |
981 | memset(&s, 0, sizeof(s)); | |
982 | /* guard against past and future API changes */ | |
983 | memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh))); | |
984 | if (s.mask & AUDIT_STATUS_ENABLED) { | |
985 | err = audit_set_enabled(s.enabled); | |
20c6aaa3 | 986 | if (err < 0) |
987 | return err; | |
1da177e4 | 988 | } |
09f883a9 RGB |
989 | if (s.mask & AUDIT_STATUS_FAILURE) { |
990 | err = audit_set_failure(s.failure); | |
20c6aaa3 | 991 | if (err < 0) |
992 | return err; | |
1da177e4 | 993 | } |
09f883a9 RGB |
994 | if (s.mask & AUDIT_STATUS_PID) { |
995 | int new_pid = s.pid; | |
133e1e5a | 996 | pid_t requesting_pid = task_tgid_vnr(current); |
1a6b9f23 | 997 | |
935c9e7f RGB |
998 | if ((!new_pid) && (requesting_pid != audit_pid)) { |
999 | audit_log_config_change("audit_pid", new_pid, audit_pid, 0); | |
34eab0a7 | 1000 | return -EACCES; |
935c9e7f | 1001 | } |
133e1e5a | 1002 | if (audit_pid && new_pid && |
935c9e7f RGB |
1003 | audit_replace(requesting_pid) != -ECONNREFUSED) { |
1004 | audit_log_config_change("audit_pid", new_pid, audit_pid, 0); | |
133e1e5a | 1005 | return -EEXIST; |
935c9e7f | 1006 | } |
1a6b9f23 | 1007 | if (audit_enabled != AUDIT_OFF) |
dc9eb698 | 1008 | audit_log_config_change("audit_pid", new_pid, audit_pid, 1); |
1a6b9f23 | 1009 | audit_pid = new_pid; |
15e47304 | 1010 | audit_nlk_portid = NETLINK_CB(skb).portid; |
de92fc97 | 1011 | audit_sock = skb->sk; |
e1d16621 | 1012 | wake_up_interruptible(&kauditd_wait); |
1da177e4 | 1013 | } |
09f883a9 RGB |
1014 | if (s.mask & AUDIT_STATUS_RATE_LIMIT) { |
1015 | err = audit_set_rate_limit(s.rate_limit); | |
20c6aaa3 | 1016 | if (err < 0) |
1017 | return err; | |
1018 | } | |
51cc83f0 | 1019 | if (s.mask & AUDIT_STATUS_BACKLOG_LIMIT) { |
09f883a9 | 1020 | err = audit_set_backlog_limit(s.backlog_limit); |
51cc83f0 RGB |
1021 | if (err < 0) |
1022 | return err; | |
1023 | } | |
3f0c5fad EP |
1024 | if (s.mask & AUDIT_STATUS_BACKLOG_WAIT_TIME) { |
1025 | if (sizeof(s) > (size_t)nlh->nlmsg_len) | |
1026 | return -EINVAL; | |
724e7bfc | 1027 | if (s.backlog_wait_time > 10*AUDIT_BACKLOG_WAIT_TIME) |
3f0c5fad EP |
1028 | return -EINVAL; |
1029 | err = audit_set_backlog_wait_time(s.backlog_wait_time); | |
1030 | if (err < 0) | |
1031 | return err; | |
51cc83f0 | 1032 | } |
1da177e4 | 1033 | break; |
09f883a9 | 1034 | } |
b0fed402 EP |
1035 | case AUDIT_GET_FEATURE: |
1036 | err = audit_get_feature(skb); | |
1037 | if (err) | |
1038 | return err; | |
1039 | break; | |
1040 | case AUDIT_SET_FEATURE: | |
1041 | err = audit_set_feature(skb); | |
1042 | if (err) | |
1043 | return err; | |
1044 | break; | |
05474106 | 1045 | case AUDIT_USER: |
039b6b3e RD |
1046 | case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG: |
1047 | case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2: | |
4a4cd633 DW |
1048 | if (!audit_enabled && msg_type != AUDIT_USER_AVC) |
1049 | return 0; | |
1050 | ||
86b2efbe | 1051 | err = audit_filter(msg_type, AUDIT_FILTER_USER); |
724e4fcc | 1052 | if (err == 1) { /* match or error */ |
4a4cd633 | 1053 | err = 0; |
522ed776 | 1054 | if (msg_type == AUDIT_USER_TTY) { |
37282a77 | 1055 | err = tty_audit_push(); |
522ed776 MT |
1056 | if (err) |
1057 | break; | |
1058 | } | |
1b7b533f | 1059 | mutex_unlock(&audit_cmd_mutex); |
dc9eb698 | 1060 | audit_log_common_recv_msg(&ab, msg_type); |
50397bd1 | 1061 | if (msg_type != AUDIT_USER_TTY) |
b50eba7e RGB |
1062 | audit_log_format(ab, " msg='%.*s'", |
1063 | AUDIT_MESSAGE_TEXT_MAX, | |
50397bd1 EP |
1064 | (char *)data); |
1065 | else { | |
1066 | int size; | |
1067 | ||
f7616102 | 1068 | audit_log_format(ab, " data="); |
50397bd1 | 1069 | size = nlmsg_len(nlh); |
55ad2f8d MT |
1070 | if (size > 0 && |
1071 | ((unsigned char *)data)[size - 1] == '\0') | |
1072 | size--; | |
b556f8ad | 1073 | audit_log_n_untrustedstring(ab, data, size); |
4a4cd633 | 1074 | } |
f9441639 | 1075 | audit_set_portid(ab, NETLINK_CB(skb).portid); |
50397bd1 | 1076 | audit_log_end(ab); |
1b7b533f | 1077 | mutex_lock(&audit_cmd_mutex); |
0f45aa18 | 1078 | } |
1da177e4 | 1079 | break; |
93315ed6 AG |
1080 | case AUDIT_ADD_RULE: |
1081 | case AUDIT_DEL_RULE: | |
1082 | if (nlmsg_len(nlh) < sizeof(struct audit_rule_data)) | |
1083 | return -EINVAL; | |
1a6b9f23 | 1084 | if (audit_enabled == AUDIT_LOCKED) { |
dc9eb698 EP |
1085 | audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE); |
1086 | audit_log_format(ab, " audit_enabled=%d res=0", audit_enabled); | |
50397bd1 | 1087 | audit_log_end(ab); |
6a01b07f SG |
1088 | return -EPERM; |
1089 | } | |
ce0d9f04 | 1090 | err = audit_rule_change(msg_type, NETLINK_CB(skb).portid, |
dc9eb698 | 1091 | seq, data, nlmsg_len(nlh)); |
1da177e4 | 1092 | break; |
ce0d9f04 | 1093 | case AUDIT_LIST_RULES: |
6f285b19 | 1094 | err = audit_list_rules_send(skb, seq); |
ce0d9f04 | 1095 | break; |
74c3cbe3 AV |
1096 | case AUDIT_TRIM: |
1097 | audit_trim_trees(); | |
dc9eb698 | 1098 | audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE); |
74c3cbe3 AV |
1099 | audit_log_format(ab, " op=trim res=1"); |
1100 | audit_log_end(ab); | |
1101 | break; | |
1102 | case AUDIT_MAKE_EQUIV: { | |
1103 | void *bufp = data; | |
1104 | u32 sizes[2]; | |
7719e437 | 1105 | size_t msglen = nlmsg_len(nlh); |
74c3cbe3 AV |
1106 | char *old, *new; |
1107 | ||
1108 | err = -EINVAL; | |
7719e437 | 1109 | if (msglen < 2 * sizeof(u32)) |
74c3cbe3 AV |
1110 | break; |
1111 | memcpy(sizes, bufp, 2 * sizeof(u32)); | |
1112 | bufp += 2 * sizeof(u32); | |
7719e437 HH |
1113 | msglen -= 2 * sizeof(u32); |
1114 | old = audit_unpack_string(&bufp, &msglen, sizes[0]); | |
74c3cbe3 AV |
1115 | if (IS_ERR(old)) { |
1116 | err = PTR_ERR(old); | |
1117 | break; | |
1118 | } | |
7719e437 | 1119 | new = audit_unpack_string(&bufp, &msglen, sizes[1]); |
74c3cbe3 AV |
1120 | if (IS_ERR(new)) { |
1121 | err = PTR_ERR(new); | |
1122 | kfree(old); | |
1123 | break; | |
1124 | } | |
1125 | /* OK, here comes... */ | |
1126 | err = audit_tag_tree(old, new); | |
1127 | ||
dc9eb698 | 1128 | audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE); |
50397bd1 | 1129 | |
74c3cbe3 AV |
1130 | audit_log_format(ab, " op=make_equiv old="); |
1131 | audit_log_untrustedstring(ab, old); | |
1132 | audit_log_format(ab, " new="); | |
1133 | audit_log_untrustedstring(ab, new); | |
1134 | audit_log_format(ab, " res=%d", !err); | |
1135 | audit_log_end(ab); | |
1136 | kfree(old); | |
1137 | kfree(new); | |
1138 | break; | |
1139 | } | |
c2f0c7c3 | 1140 | case AUDIT_SIGNAL_INFO: |
939cbf26 EP |
1141 | len = 0; |
1142 | if (audit_sig_sid) { | |
1143 | err = security_secid_to_secctx(audit_sig_sid, &ctx, &len); | |
1144 | if (err) | |
1145 | return err; | |
1146 | } | |
e1396065 AV |
1147 | sig_data = kmalloc(sizeof(*sig_data) + len, GFP_KERNEL); |
1148 | if (!sig_data) { | |
939cbf26 EP |
1149 | if (audit_sig_sid) |
1150 | security_release_secctx(ctx, len); | |
e1396065 AV |
1151 | return -ENOMEM; |
1152 | } | |
cca080d9 | 1153 | sig_data->uid = from_kuid(&init_user_ns, audit_sig_uid); |
e1396065 | 1154 | sig_data->pid = audit_sig_pid; |
939cbf26 EP |
1155 | if (audit_sig_sid) { |
1156 | memcpy(sig_data->ctx, ctx, len); | |
1157 | security_release_secctx(ctx, len); | |
1158 | } | |
6f285b19 EB |
1159 | audit_send_reply(skb, seq, AUDIT_SIGNAL_INFO, 0, 0, |
1160 | sig_data, sizeof(*sig_data) + len); | |
e1396065 | 1161 | kfree(sig_data); |
c2f0c7c3 | 1162 | break; |
522ed776 MT |
1163 | case AUDIT_TTY_GET: { |
1164 | struct audit_tty_status s; | |
2e28d38a | 1165 | unsigned int t; |
8aa14b64 | 1166 | |
2e28d38a PH |
1167 | t = READ_ONCE(current->signal->audit_tty); |
1168 | s.enabled = t & AUDIT_TTY_ENABLE; | |
1169 | s.log_passwd = !!(t & AUDIT_TTY_LOG_PASSWD); | |
8aa14b64 | 1170 | |
6f285b19 | 1171 | audit_send_reply(skb, seq, AUDIT_TTY_GET, 0, 0, &s, sizeof(s)); |
522ed776 MT |
1172 | break; |
1173 | } | |
1174 | case AUDIT_TTY_SET: { | |
a06e56b2 | 1175 | struct audit_tty_status s, old; |
a06e56b2 | 1176 | struct audit_buffer *ab; |
2e28d38a | 1177 | unsigned int t; |
0e23bacc EP |
1178 | |
1179 | memset(&s, 0, sizeof(s)); | |
1180 | /* guard against past and future API changes */ | |
1181 | memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh))); | |
1182 | /* check if new data is valid */ | |
1183 | if ((s.enabled != 0 && s.enabled != 1) || | |
1184 | (s.log_passwd != 0 && s.log_passwd != 1)) | |
1185 | err = -EINVAL; | |
a06e56b2 | 1186 | |
2e28d38a PH |
1187 | if (err) |
1188 | t = READ_ONCE(current->signal->audit_tty); | |
1189 | else { | |
1190 | t = s.enabled | (-s.log_passwd & AUDIT_TTY_LOG_PASSWD); | |
1191 | t = xchg(¤t->signal->audit_tty, t); | |
0e23bacc | 1192 | } |
2e28d38a PH |
1193 | old.enabled = t & AUDIT_TTY_ENABLE; |
1194 | old.log_passwd = !!(t & AUDIT_TTY_LOG_PASSWD); | |
522ed776 | 1195 | |
a06e56b2 | 1196 | audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE); |
1ce319f1 EP |
1197 | audit_log_format(ab, " op=tty_set old-enabled=%d new-enabled=%d" |
1198 | " old-log_passwd=%d new-log_passwd=%d res=%d", | |
1199 | old.enabled, s.enabled, old.log_passwd, | |
1200 | s.log_passwd, !err); | |
a06e56b2 | 1201 | audit_log_end(ab); |
522ed776 MT |
1202 | break; |
1203 | } | |
1da177e4 LT |
1204 | default: |
1205 | err = -EINVAL; | |
1206 | break; | |
1207 | } | |
1208 | ||
1209 | return err < 0 ? err : 0; | |
1210 | } | |
1211 | ||
b0dd25a8 | 1212 | /* |
ea7ae60b EP |
1213 | * Get message from skb. Each message is processed by audit_receive_msg. |
1214 | * Malformed skbs with wrong length are discarded silently. | |
b0dd25a8 | 1215 | */ |
2a0a6ebe | 1216 | static void audit_receive_skb(struct sk_buff *skb) |
1da177e4 | 1217 | { |
ea7ae60b EP |
1218 | struct nlmsghdr *nlh; |
1219 | /* | |
94191213 | 1220 | * len MUST be signed for nlmsg_next to be able to dec it below 0 |
ea7ae60b EP |
1221 | * if the nlmsg_len was not aligned |
1222 | */ | |
1223 | int len; | |
1224 | int err; | |
1225 | ||
1226 | nlh = nlmsg_hdr(skb); | |
1227 | len = skb->len; | |
1228 | ||
94191213 | 1229 | while (nlmsg_ok(nlh, len)) { |
ea7ae60b EP |
1230 | err = audit_receive_msg(skb, nlh); |
1231 | /* if err or if this message says it wants a response */ | |
1232 | if (err || (nlh->nlmsg_flags & NLM_F_ACK)) | |
1da177e4 | 1233 | netlink_ack(skb, nlh, err); |
ea7ae60b | 1234 | |
2851da57 | 1235 | nlh = nlmsg_next(nlh, &len); |
1da177e4 | 1236 | } |
1da177e4 LT |
1237 | } |
1238 | ||
1239 | /* Receive messages from netlink socket. */ | |
cd40b7d3 | 1240 | static void audit_receive(struct sk_buff *skb) |
1da177e4 | 1241 | { |
f368c07d | 1242 | mutex_lock(&audit_cmd_mutex); |
cd40b7d3 | 1243 | audit_receive_skb(skb); |
f368c07d | 1244 | mutex_unlock(&audit_cmd_mutex); |
1da177e4 LT |
1245 | } |
1246 | ||
3a101b8d | 1247 | /* Run custom bind function on netlink socket group connect or bind requests. */ |
023e2cfa | 1248 | static int audit_bind(struct net *net, int group) |
3a101b8d RGB |
1249 | { |
1250 | if (!capable(CAP_AUDIT_READ)) | |
1251 | return -EPERM; | |
1252 | ||
1253 | return 0; | |
1254 | } | |
1255 | ||
33faba7f | 1256 | static int __net_init audit_net_init(struct net *net) |
1da177e4 | 1257 | { |
a31f2d17 PNA |
1258 | struct netlink_kernel_cfg cfg = { |
1259 | .input = audit_receive, | |
3a101b8d | 1260 | .bind = audit_bind, |
451f9216 RGB |
1261 | .flags = NL_CFG_F_NONROOT_RECV, |
1262 | .groups = AUDIT_NLGRP_MAX, | |
a31f2d17 | 1263 | }; |
f368c07d | 1264 | |
33faba7f RGB |
1265 | struct audit_net *aunet = net_generic(net, audit_net_id); |
1266 | ||
33faba7f | 1267 | aunet->nlsk = netlink_kernel_create(net, NETLINK_AUDIT, &cfg); |
11ee39eb | 1268 | if (aunet->nlsk == NULL) { |
33faba7f | 1269 | audit_panic("cannot initialize netlink socket in namespace"); |
11ee39eb G |
1270 | return -ENOMEM; |
1271 | } | |
1272 | aunet->nlsk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT; | |
33faba7f RGB |
1273 | return 0; |
1274 | } | |
1275 | ||
1276 | static void __net_exit audit_net_exit(struct net *net) | |
1277 | { | |
1278 | struct audit_net *aunet = net_generic(net, audit_net_id); | |
1279 | struct sock *sock = aunet->nlsk; | |
c6480207 PM |
1280 | if (sock == audit_sock) |
1281 | auditd_reset(); | |
33faba7f | 1282 | |
e231d54c | 1283 | RCU_INIT_POINTER(aunet->nlsk, NULL); |
33faba7f RGB |
1284 | synchronize_net(); |
1285 | netlink_kernel_release(sock); | |
1286 | } | |
1287 | ||
8626877b | 1288 | static struct pernet_operations audit_net_ops __net_initdata = { |
33faba7f RGB |
1289 | .init = audit_net_init, |
1290 | .exit = audit_net_exit, | |
1291 | .id = &audit_net_id, | |
1292 | .size = sizeof(struct audit_net), | |
1293 | }; | |
1294 | ||
1295 | /* Initialize audit support at boot time. */ | |
1296 | static int __init audit_init(void) | |
1297 | { | |
1298 | int i; | |
1299 | ||
a3f07114 EP |
1300 | if (audit_initialized == AUDIT_DISABLED) |
1301 | return 0; | |
1302 | ||
d957f7b7 JP |
1303 | pr_info("initializing netlink subsys (%s)\n", |
1304 | audit_default ? "enabled" : "disabled"); | |
33faba7f | 1305 | register_pernet_subsys(&audit_net_ops); |
1da177e4 | 1306 | |
af8b824f | 1307 | skb_queue_head_init(&audit_queue); |
c6480207 | 1308 | skb_queue_head_init(&audit_retry_queue); |
af8b824f | 1309 | skb_queue_head_init(&audit_hold_queue); |
a3f07114 | 1310 | audit_initialized = AUDIT_INITIALIZED; |
1da177e4 | 1311 | audit_enabled = audit_default; |
b593d384 | 1312 | audit_ever_enabled |= !!audit_default; |
3dc7e315 | 1313 | |
f368c07d AG |
1314 | for (i = 0; i < AUDIT_INODE_BUCKETS; i++) |
1315 | INIT_LIST_HEAD(&audit_inode_hash[i]); | |
f368c07d | 1316 | |
6c925564 PM |
1317 | kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd"); |
1318 | if (IS_ERR(kauditd_task)) { | |
1319 | int err = PTR_ERR(kauditd_task); | |
1320 | panic("audit: failed to start the kauditd thread (%d)\n", err); | |
1321 | } | |
1322 | ||
1323 | audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, "initialized"); | |
1324 | ||
1da177e4 LT |
1325 | return 0; |
1326 | } | |
1da177e4 LT |
1327 | __initcall(audit_init); |
1328 | ||
1329 | /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */ | |
1330 | static int __init audit_enable(char *str) | |
1331 | { | |
1332 | audit_default = !!simple_strtol(str, NULL, 0); | |
a3f07114 EP |
1333 | if (!audit_default) |
1334 | audit_initialized = AUDIT_DISABLED; | |
1335 | ||
d957f7b7 | 1336 | pr_info("%s\n", audit_default ? |
d3ca0344 | 1337 | "enabled (after initialization)" : "disabled (until reboot)"); |
a3f07114 | 1338 | |
9b41046c | 1339 | return 1; |
1da177e4 | 1340 | } |
1da177e4 LT |
1341 | __setup("audit=", audit_enable); |
1342 | ||
f910fde7 RGB |
1343 | /* Process kernel command-line parameter at boot time. |
1344 | * audit_backlog_limit=<n> */ | |
1345 | static int __init audit_backlog_limit_set(char *str) | |
1346 | { | |
3e1d0bb6 | 1347 | u32 audit_backlog_limit_arg; |
d957f7b7 | 1348 | |
f910fde7 | 1349 | pr_info("audit_backlog_limit: "); |
3e1d0bb6 JP |
1350 | if (kstrtouint(str, 0, &audit_backlog_limit_arg)) { |
1351 | pr_cont("using default of %u, unable to parse %s\n", | |
d957f7b7 | 1352 | audit_backlog_limit, str); |
f910fde7 RGB |
1353 | return 1; |
1354 | } | |
3e1d0bb6 JP |
1355 | |
1356 | audit_backlog_limit = audit_backlog_limit_arg; | |
d957f7b7 | 1357 | pr_cont("%d\n", audit_backlog_limit); |
f910fde7 RGB |
1358 | |
1359 | return 1; | |
1360 | } | |
1361 | __setup("audit_backlog_limit=", audit_backlog_limit_set); | |
1362 | ||
16e1904e CW |
1363 | static void audit_buffer_free(struct audit_buffer *ab) |
1364 | { | |
1365 | unsigned long flags; | |
1366 | ||
8fc6115c CW |
1367 | if (!ab) |
1368 | return; | |
1369 | ||
d865e573 | 1370 | kfree_skb(ab->skb); |
16e1904e | 1371 | spin_lock_irqsave(&audit_freelist_lock, flags); |
5d136a01 | 1372 | if (audit_freelist_count > AUDIT_MAXFREE) |
16e1904e | 1373 | kfree(ab); |
5d136a01 SH |
1374 | else { |
1375 | audit_freelist_count++; | |
16e1904e | 1376 | list_add(&ab->list, &audit_freelist); |
5d136a01 | 1377 | } |
16e1904e CW |
1378 | spin_unlock_irqrestore(&audit_freelist_lock, flags); |
1379 | } | |
1380 | ||
c0404993 | 1381 | static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx, |
dd0fc66f | 1382 | gfp_t gfp_mask, int type) |
16e1904e CW |
1383 | { |
1384 | unsigned long flags; | |
1385 | struct audit_buffer *ab = NULL; | |
c0404993 | 1386 | struct nlmsghdr *nlh; |
16e1904e CW |
1387 | |
1388 | spin_lock_irqsave(&audit_freelist_lock, flags); | |
1389 | if (!list_empty(&audit_freelist)) { | |
1390 | ab = list_entry(audit_freelist.next, | |
1391 | struct audit_buffer, list); | |
1392 | list_del(&ab->list); | |
1393 | --audit_freelist_count; | |
1394 | } | |
1395 | spin_unlock_irqrestore(&audit_freelist_lock, flags); | |
1396 | ||
1397 | if (!ab) { | |
4332bdd3 | 1398 | ab = kmalloc(sizeof(*ab), gfp_mask); |
16e1904e | 1399 | if (!ab) |
8fc6115c | 1400 | goto err; |
16e1904e | 1401 | } |
8fc6115c | 1402 | |
b7d11258 | 1403 | ab->ctx = ctx; |
9ad9ad38 | 1404 | ab->gfp_mask = gfp_mask; |
ee080e6c EP |
1405 | |
1406 | ab->skb = nlmsg_new(AUDIT_BUFSIZ, gfp_mask); | |
1407 | if (!ab->skb) | |
c64e66c6 | 1408 | goto err; |
ee080e6c | 1409 | |
c64e66c6 DM |
1410 | nlh = nlmsg_put(ab->skb, 0, 0, type, 0, 0); |
1411 | if (!nlh) | |
1412 | goto out_kfree_skb; | |
ee080e6c | 1413 | |
16e1904e | 1414 | return ab; |
ee080e6c | 1415 | |
c64e66c6 | 1416 | out_kfree_skb: |
ee080e6c EP |
1417 | kfree_skb(ab->skb); |
1418 | ab->skb = NULL; | |
8fc6115c CW |
1419 | err: |
1420 | audit_buffer_free(ab); | |
1421 | return NULL; | |
16e1904e | 1422 | } |
1da177e4 | 1423 | |
b0dd25a8 RD |
1424 | /** |
1425 | * audit_serial - compute a serial number for the audit record | |
1426 | * | |
1427 | * Compute a serial number for the audit record. Audit records are | |
bfb4496e DW |
1428 | * written to user-space as soon as they are generated, so a complete |
1429 | * audit record may be written in several pieces. The timestamp of the | |
1430 | * record and this serial number are used by the user-space tools to | |
1431 | * determine which pieces belong to the same audit record. The | |
1432 | * (timestamp,serial) tuple is unique for each syscall and is live from | |
1433 | * syscall entry to syscall exit. | |
1434 | * | |
bfb4496e DW |
1435 | * NOTE: Another possibility is to store the formatted records off the |
1436 | * audit context (for those records that have a context), and emit them | |
1437 | * all at syscall exit. However, this could delay the reporting of | |
1438 | * significant errors until syscall exit (or never, if the system | |
b0dd25a8 RD |
1439 | * halts). |
1440 | */ | |
bfb4496e DW |
1441 | unsigned int audit_serial(void) |
1442 | { | |
01478d7d | 1443 | static atomic_t serial = ATOMIC_INIT(0); |
d5b454f2 | 1444 | |
01478d7d | 1445 | return atomic_add_return(1, &serial); |
bfb4496e DW |
1446 | } |
1447 | ||
5600b892 | 1448 | static inline void audit_get_stamp(struct audit_context *ctx, |
bfb4496e DW |
1449 | struct timespec *t, unsigned int *serial) |
1450 | { | |
48887e63 | 1451 | if (!ctx || !auditsc_get_stamp(ctx, t, serial)) { |
bfb4496e DW |
1452 | *t = CURRENT_TIME; |
1453 | *serial = audit_serial(); | |
1454 | } | |
1455 | } | |
1456 | ||
b0dd25a8 RD |
1457 | /** |
1458 | * audit_log_start - obtain an audit buffer | |
1459 | * @ctx: audit_context (may be NULL) | |
1460 | * @gfp_mask: type of allocation | |
1461 | * @type: audit message type | |
1462 | * | |
1463 | * Returns audit_buffer pointer on success or NULL on error. | |
1464 | * | |
1465 | * Obtain an audit buffer. This routine does locking to obtain the | |
1466 | * audit buffer, but then no locking is required for calls to | |
1467 | * audit_log_*format. If the task (ctx) is a task that is currently in a | |
1468 | * syscall, then the syscall is marked as auditable and an audit record | |
1469 | * will be written at syscall exit. If there is no associated task, then | |
1470 | * task context (ctx) should be NULL. | |
1471 | */ | |
9796fdd8 | 1472 | struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, |
9ad9ad38 | 1473 | int type) |
1da177e4 | 1474 | { |
31975424 PM |
1475 | struct audit_buffer *ab; |
1476 | struct timespec t; | |
1477 | unsigned int uninitialized_var(serial); | |
1da177e4 | 1478 | |
a3f07114 | 1479 | if (audit_initialized != AUDIT_INITIALIZED) |
1da177e4 LT |
1480 | return NULL; |
1481 | ||
86b2efbe | 1482 | if (unlikely(!audit_filter(type, AUDIT_FILTER_TYPE))) |
c8edc80c DK |
1483 | return NULL; |
1484 | ||
31975424 PM |
1485 | /* don't ever fail/sleep on auditd since we need auditd to drain the |
1486 | * queue; also, when we are checking for auditd, compare PIDs using | |
1487 | * task_tgid_vnr() since auditd_pid is set in audit_receive_msg() using | |
1488 | * a PID anchored in the caller's namespace */ | |
1489 | if (!(audit_pid && audit_pid == task_tgid_vnr(current))) { | |
1490 | long sleep_time = audit_backlog_wait_time; | |
1491 | ||
1492 | while (audit_backlog_limit && | |
1493 | (skb_queue_len(&audit_queue) > audit_backlog_limit)) { | |
1494 | /* wake kauditd to try and flush the queue */ | |
1495 | wake_up_interruptible(&kauditd_wait); | |
1496 | ||
1497 | /* sleep if we are allowed and we haven't exhausted our | |
1498 | * backlog wait limit */ | |
1499 | if ((gfp_mask & __GFP_DIRECT_RECLAIM) && | |
1500 | (sleep_time > 0)) { | |
1501 | DECLARE_WAITQUEUE(wait, current); | |
1502 | ||
1503 | add_wait_queue_exclusive(&audit_backlog_wait, | |
1504 | &wait); | |
1505 | set_current_state(TASK_UNINTERRUPTIBLE); | |
1506 | sleep_time = schedule_timeout(sleep_time); | |
1507 | remove_wait_queue(&audit_backlog_wait, &wait); | |
1508 | } else { | |
1509 | if (audit_rate_check() && printk_ratelimit()) | |
1510 | pr_warn("audit_backlog=%d > audit_backlog_limit=%d\n", | |
1511 | skb_queue_len(&audit_queue), | |
1512 | audit_backlog_limit); | |
1513 | audit_log_lost("backlog limit exceeded"); | |
1514 | return NULL; | |
8ac1c8d5 | 1515 | } |
9ad9ad38 | 1516 | } |
fb19b4c6 DW |
1517 | } |
1518 | ||
9ad9ad38 | 1519 | ab = audit_buffer_alloc(ctx, gfp_mask, type); |
1da177e4 LT |
1520 | if (!ab) { |
1521 | audit_log_lost("out of memory in audit_log_start"); | |
1522 | return NULL; | |
1523 | } | |
1524 | ||
bfb4496e | 1525 | audit_get_stamp(ab->ctx, &t, &serial); |
1da177e4 LT |
1526 | audit_log_format(ab, "audit(%lu.%03lu:%u): ", |
1527 | t.tv_sec, t.tv_nsec/1000000, serial); | |
31975424 | 1528 | |
1da177e4 LT |
1529 | return ab; |
1530 | } | |
1531 | ||
8fc6115c | 1532 | /** |
5ac52f33 | 1533 | * audit_expand - expand skb in the audit buffer |
8fc6115c | 1534 | * @ab: audit_buffer |
b0dd25a8 | 1535 | * @extra: space to add at tail of the skb |
8fc6115c CW |
1536 | * |
1537 | * Returns 0 (no space) on failed expansion, or available space if | |
1538 | * successful. | |
1539 | */ | |
e3b926b4 | 1540 | static inline int audit_expand(struct audit_buffer *ab, int extra) |
8fc6115c | 1541 | { |
5ac52f33 | 1542 | struct sk_buff *skb = ab->skb; |
406a1d86 HX |
1543 | int oldtail = skb_tailroom(skb); |
1544 | int ret = pskb_expand_head(skb, 0, extra, ab->gfp_mask); | |
1545 | int newtail = skb_tailroom(skb); | |
1546 | ||
5ac52f33 CW |
1547 | if (ret < 0) { |
1548 | audit_log_lost("out of memory in audit_expand"); | |
8fc6115c | 1549 | return 0; |
5ac52f33 | 1550 | } |
406a1d86 HX |
1551 | |
1552 | skb->truesize += newtail - oldtail; | |
1553 | return newtail; | |
8fc6115c | 1554 | } |
1da177e4 | 1555 | |
b0dd25a8 RD |
1556 | /* |
1557 | * Format an audit message into the audit buffer. If there isn't enough | |
1da177e4 LT |
1558 | * room in the audit buffer, more room will be allocated and vsnprint |
1559 | * will be called a second time. Currently, we assume that a printk | |
b0dd25a8 RD |
1560 | * can't format message larger than 1024 bytes, so we don't either. |
1561 | */ | |
1da177e4 LT |
1562 | static void audit_log_vformat(struct audit_buffer *ab, const char *fmt, |
1563 | va_list args) | |
1564 | { | |
1565 | int len, avail; | |
5ac52f33 | 1566 | struct sk_buff *skb; |
eecb0a73 | 1567 | va_list args2; |
1da177e4 LT |
1568 | |
1569 | if (!ab) | |
1570 | return; | |
1571 | ||
5ac52f33 CW |
1572 | BUG_ON(!ab->skb); |
1573 | skb = ab->skb; | |
1574 | avail = skb_tailroom(skb); | |
1575 | if (avail == 0) { | |
e3b926b4 | 1576 | avail = audit_expand(ab, AUDIT_BUFSIZ); |
8fc6115c CW |
1577 | if (!avail) |
1578 | goto out; | |
1da177e4 | 1579 | } |
eecb0a73 | 1580 | va_copy(args2, args); |
27a884dc | 1581 | len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args); |
1da177e4 LT |
1582 | if (len >= avail) { |
1583 | /* The printk buffer is 1024 bytes long, so if we get | |
1584 | * here and AUDIT_BUFSIZ is at least 1024, then we can | |
1585 | * log everything that printk could have logged. */ | |
b0dd25a8 RD |
1586 | avail = audit_expand(ab, |
1587 | max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail)); | |
8fc6115c | 1588 | if (!avail) |
a0e86bd4 | 1589 | goto out_va_end; |
27a884dc | 1590 | len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args2); |
1da177e4 | 1591 | } |
168b7173 SG |
1592 | if (len > 0) |
1593 | skb_put(skb, len); | |
a0e86bd4 JJ |
1594 | out_va_end: |
1595 | va_end(args2); | |
8fc6115c CW |
1596 | out: |
1597 | return; | |
1da177e4 LT |
1598 | } |
1599 | ||
b0dd25a8 RD |
1600 | /** |
1601 | * audit_log_format - format a message into the audit buffer. | |
1602 | * @ab: audit_buffer | |
1603 | * @fmt: format string | |
1604 | * @...: optional parameters matching @fmt string | |
1605 | * | |
1606 | * All the work is done in audit_log_vformat. | |
1607 | */ | |
1da177e4 LT |
1608 | void audit_log_format(struct audit_buffer *ab, const char *fmt, ...) |
1609 | { | |
1610 | va_list args; | |
1611 | ||
1612 | if (!ab) | |
1613 | return; | |
1614 | va_start(args, fmt); | |
1615 | audit_log_vformat(ab, fmt, args); | |
1616 | va_end(args); | |
1617 | } | |
1618 | ||
b0dd25a8 RD |
1619 | /** |
1620 | * audit_log_hex - convert a buffer to hex and append it to the audit skb | |
1621 | * @ab: the audit_buffer | |
1622 | * @buf: buffer to convert to hex | |
1623 | * @len: length of @buf to be converted | |
1624 | * | |
1625 | * No return value; failure to expand is silently ignored. | |
1626 | * | |
1627 | * This function will take the passed buf and convert it into a string of | |
1628 | * ascii hex digits. The new string is placed onto the skb. | |
1629 | */ | |
b556f8ad | 1630 | void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf, |
168b7173 | 1631 | size_t len) |
83c7d091 | 1632 | { |
168b7173 SG |
1633 | int i, avail, new_len; |
1634 | unsigned char *ptr; | |
1635 | struct sk_buff *skb; | |
168b7173 | 1636 | |
8ef2d304 AG |
1637 | if (!ab) |
1638 | return; | |
1639 | ||
168b7173 SG |
1640 | BUG_ON(!ab->skb); |
1641 | skb = ab->skb; | |
1642 | avail = skb_tailroom(skb); | |
1643 | new_len = len<<1; | |
1644 | if (new_len >= avail) { | |
1645 | /* Round the buffer request up to the next multiple */ | |
1646 | new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1); | |
1647 | avail = audit_expand(ab, new_len); | |
1648 | if (!avail) | |
1649 | return; | |
1650 | } | |
83c7d091 | 1651 | |
27a884dc | 1652 | ptr = skb_tail_pointer(skb); |
b8dbc324 JP |
1653 | for (i = 0; i < len; i++) |
1654 | ptr = hex_byte_pack_upper(ptr, buf[i]); | |
168b7173 SG |
1655 | *ptr = 0; |
1656 | skb_put(skb, len << 1); /* new string is twice the old string */ | |
83c7d091 DW |
1657 | } |
1658 | ||
9c937dcc AG |
1659 | /* |
1660 | * Format a string of no more than slen characters into the audit buffer, | |
1661 | * enclosed in quote marks. | |
1662 | */ | |
b556f8ad EP |
1663 | void audit_log_n_string(struct audit_buffer *ab, const char *string, |
1664 | size_t slen) | |
9c937dcc AG |
1665 | { |
1666 | int avail, new_len; | |
1667 | unsigned char *ptr; | |
1668 | struct sk_buff *skb; | |
1669 | ||
8ef2d304 AG |
1670 | if (!ab) |
1671 | return; | |
1672 | ||
9c937dcc AG |
1673 | BUG_ON(!ab->skb); |
1674 | skb = ab->skb; | |
1675 | avail = skb_tailroom(skb); | |
1676 | new_len = slen + 3; /* enclosing quotes + null terminator */ | |
1677 | if (new_len > avail) { | |
1678 | avail = audit_expand(ab, new_len); | |
1679 | if (!avail) | |
1680 | return; | |
1681 | } | |
27a884dc | 1682 | ptr = skb_tail_pointer(skb); |
9c937dcc AG |
1683 | *ptr++ = '"'; |
1684 | memcpy(ptr, string, slen); | |
1685 | ptr += slen; | |
1686 | *ptr++ = '"'; | |
1687 | *ptr = 0; | |
1688 | skb_put(skb, slen + 2); /* don't include null terminator */ | |
1689 | } | |
1690 | ||
de6bbd1d EP |
1691 | /** |
1692 | * audit_string_contains_control - does a string need to be logged in hex | |
f706d5d2 DJ |
1693 | * @string: string to be checked |
1694 | * @len: max length of the string to check | |
de6bbd1d | 1695 | */ |
9fcf836b | 1696 | bool audit_string_contains_control(const char *string, size_t len) |
de6bbd1d EP |
1697 | { |
1698 | const unsigned char *p; | |
b3897f56 | 1699 | for (p = string; p < (const unsigned char *)string + len; p++) { |
1d6c9649 | 1700 | if (*p == '"' || *p < 0x21 || *p > 0x7e) |
9fcf836b | 1701 | return true; |
de6bbd1d | 1702 | } |
9fcf836b | 1703 | return false; |
de6bbd1d EP |
1704 | } |
1705 | ||
b0dd25a8 | 1706 | /** |
522ed776 | 1707 | * audit_log_n_untrustedstring - log a string that may contain random characters |
b0dd25a8 | 1708 | * @ab: audit_buffer |
f706d5d2 | 1709 | * @len: length of string (not including trailing null) |
b0dd25a8 RD |
1710 | * @string: string to be logged |
1711 | * | |
1712 | * This code will escape a string that is passed to it if the string | |
1713 | * contains a control character, unprintable character, double quote mark, | |
168b7173 | 1714 | * or a space. Unescaped strings will start and end with a double quote mark. |
b0dd25a8 | 1715 | * Strings that are escaped are printed in hex (2 digits per char). |
9c937dcc AG |
1716 | * |
1717 | * The caller specifies the number of characters in the string to log, which may | |
1718 | * or may not be the entire string. | |
b0dd25a8 | 1719 | */ |
b556f8ad EP |
1720 | void audit_log_n_untrustedstring(struct audit_buffer *ab, const char *string, |
1721 | size_t len) | |
83c7d091 | 1722 | { |
de6bbd1d | 1723 | if (audit_string_contains_control(string, len)) |
b556f8ad | 1724 | audit_log_n_hex(ab, string, len); |
de6bbd1d | 1725 | else |
b556f8ad | 1726 | audit_log_n_string(ab, string, len); |
83c7d091 DW |
1727 | } |
1728 | ||
9c937dcc | 1729 | /** |
522ed776 | 1730 | * audit_log_untrustedstring - log a string that may contain random characters |
9c937dcc AG |
1731 | * @ab: audit_buffer |
1732 | * @string: string to be logged | |
1733 | * | |
522ed776 | 1734 | * Same as audit_log_n_untrustedstring(), except that strlen is used to |
9c937dcc AG |
1735 | * determine string length. |
1736 | */ | |
de6bbd1d | 1737 | void audit_log_untrustedstring(struct audit_buffer *ab, const char *string) |
9c937dcc | 1738 | { |
b556f8ad | 1739 | audit_log_n_untrustedstring(ab, string, strlen(string)); |
9c937dcc AG |
1740 | } |
1741 | ||
168b7173 | 1742 | /* This is a helper-function to print the escaped d_path */ |
1da177e4 | 1743 | void audit_log_d_path(struct audit_buffer *ab, const char *prefix, |
66b3fad3 | 1744 | const struct path *path) |
1da177e4 | 1745 | { |
44707fdf | 1746 | char *p, *pathname; |
1da177e4 | 1747 | |
8fc6115c | 1748 | if (prefix) |
c158a35c | 1749 | audit_log_format(ab, "%s", prefix); |
1da177e4 | 1750 | |
168b7173 | 1751 | /* We will allow 11 spaces for ' (deleted)' to be appended */ |
44707fdf JB |
1752 | pathname = kmalloc(PATH_MAX+11, ab->gfp_mask); |
1753 | if (!pathname) { | |
def57543 | 1754 | audit_log_string(ab, "<no_memory>"); |
168b7173 | 1755 | return; |
1da177e4 | 1756 | } |
cf28b486 | 1757 | p = d_path(path, pathname, PATH_MAX+11); |
168b7173 SG |
1758 | if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */ |
1759 | /* FIXME: can we save some information here? */ | |
def57543 | 1760 | audit_log_string(ab, "<too_long>"); |
5600b892 | 1761 | } else |
168b7173 | 1762 | audit_log_untrustedstring(ab, p); |
44707fdf | 1763 | kfree(pathname); |
1da177e4 LT |
1764 | } |
1765 | ||
4d3fb709 EP |
1766 | void audit_log_session_info(struct audit_buffer *ab) |
1767 | { | |
4440e854 | 1768 | unsigned int sessionid = audit_get_sessionid(current); |
4d3fb709 EP |
1769 | uid_t auid = from_kuid(&init_user_ns, audit_get_loginuid(current)); |
1770 | ||
b8f89caa | 1771 | audit_log_format(ab, " auid=%u ses=%u", auid, sessionid); |
4d3fb709 EP |
1772 | } |
1773 | ||
9d960985 EP |
1774 | void audit_log_key(struct audit_buffer *ab, char *key) |
1775 | { | |
1776 | audit_log_format(ab, " key="); | |
1777 | if (key) | |
1778 | audit_log_untrustedstring(ab, key); | |
1779 | else | |
1780 | audit_log_format(ab, "(null)"); | |
1781 | } | |
1782 | ||
b24a30a7 EP |
1783 | void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap) |
1784 | { | |
1785 | int i; | |
1786 | ||
1787 | audit_log_format(ab, " %s=", prefix); | |
1788 | CAP_FOR_EACH_U32(i) { | |
1789 | audit_log_format(ab, "%08x", | |
7d8b6c63 | 1790 | cap->cap[CAP_LAST_U32 - i]); |
b24a30a7 EP |
1791 | } |
1792 | } | |
1793 | ||
691e6d59 | 1794 | static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name) |
b24a30a7 EP |
1795 | { |
1796 | kernel_cap_t *perm = &name->fcap.permitted; | |
1797 | kernel_cap_t *inh = &name->fcap.inheritable; | |
1798 | int log = 0; | |
1799 | ||
1800 | if (!cap_isclear(*perm)) { | |
1801 | audit_log_cap(ab, "cap_fp", perm); | |
1802 | log = 1; | |
1803 | } | |
1804 | if (!cap_isclear(*inh)) { | |
1805 | audit_log_cap(ab, "cap_fi", inh); | |
1806 | log = 1; | |
1807 | } | |
1808 | ||
1809 | if (log) | |
1810 | audit_log_format(ab, " cap_fe=%d cap_fver=%x", | |
1811 | name->fcap.fE, name->fcap_ver); | |
1812 | } | |
1813 | ||
1814 | static inline int audit_copy_fcaps(struct audit_names *name, | |
1815 | const struct dentry *dentry) | |
1816 | { | |
1817 | struct cpu_vfs_cap_data caps; | |
1818 | int rc; | |
1819 | ||
1820 | if (!dentry) | |
1821 | return 0; | |
1822 | ||
1823 | rc = get_vfs_caps_from_disk(dentry, &caps); | |
1824 | if (rc) | |
1825 | return rc; | |
1826 | ||
1827 | name->fcap.permitted = caps.permitted; | |
1828 | name->fcap.inheritable = caps.inheritable; | |
1829 | name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE); | |
1830 | name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >> | |
1831 | VFS_CAP_REVISION_SHIFT; | |
1832 | ||
1833 | return 0; | |
1834 | } | |
1835 | ||
1836 | /* Copy inode data into an audit_names. */ | |
1837 | void audit_copy_inode(struct audit_names *name, const struct dentry *dentry, | |
d6335d77 | 1838 | struct inode *inode) |
b24a30a7 EP |
1839 | { |
1840 | name->ino = inode->i_ino; | |
1841 | name->dev = inode->i_sb->s_dev; | |
1842 | name->mode = inode->i_mode; | |
1843 | name->uid = inode->i_uid; | |
1844 | name->gid = inode->i_gid; | |
1845 | name->rdev = inode->i_rdev; | |
1846 | security_inode_getsecid(inode, &name->osid); | |
1847 | audit_copy_fcaps(name, dentry); | |
1848 | } | |
1849 | ||
1850 | /** | |
1851 | * audit_log_name - produce AUDIT_PATH record from struct audit_names | |
1852 | * @context: audit_context for the task | |
1853 | * @n: audit_names structure with reportable details | |
1854 | * @path: optional path to report instead of audit_names->name | |
1855 | * @record_num: record number to report when handling a list of names | |
1856 | * @call_panic: optional pointer to int that will be updated if secid fails | |
1857 | */ | |
1858 | void audit_log_name(struct audit_context *context, struct audit_names *n, | |
1859 | struct path *path, int record_num, int *call_panic) | |
1860 | { | |
1861 | struct audit_buffer *ab; | |
1862 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH); | |
1863 | if (!ab) | |
1864 | return; | |
1865 | ||
1866 | audit_log_format(ab, "item=%d", record_num); | |
1867 | ||
1868 | if (path) | |
1869 | audit_log_d_path(ab, " name=", path); | |
1870 | else if (n->name) { | |
1871 | switch (n->name_len) { | |
1872 | case AUDIT_NAME_FULL: | |
1873 | /* log the full path */ | |
1874 | audit_log_format(ab, " name="); | |
1875 | audit_log_untrustedstring(ab, n->name->name); | |
1876 | break; | |
1877 | case 0: | |
1878 | /* name was specified as a relative path and the | |
1879 | * directory component is the cwd */ | |
1880 | audit_log_d_path(ab, " name=", &context->pwd); | |
1881 | break; | |
1882 | default: | |
1883 | /* log the name's directory component */ | |
1884 | audit_log_format(ab, " name="); | |
1885 | audit_log_n_untrustedstring(ab, n->name->name, | |
1886 | n->name_len); | |
1887 | } | |
1888 | } else | |
1889 | audit_log_format(ab, " name=(null)"); | |
1890 | ||
425afcff | 1891 | if (n->ino != AUDIT_INO_UNSET) |
b24a30a7 EP |
1892 | audit_log_format(ab, " inode=%lu" |
1893 | " dev=%02x:%02x mode=%#ho" | |
1894 | " ouid=%u ogid=%u rdev=%02x:%02x", | |
1895 | n->ino, | |
1896 | MAJOR(n->dev), | |
1897 | MINOR(n->dev), | |
1898 | n->mode, | |
1899 | from_kuid(&init_user_ns, n->uid), | |
1900 | from_kgid(&init_user_ns, n->gid), | |
1901 | MAJOR(n->rdev), | |
1902 | MINOR(n->rdev)); | |
b24a30a7 EP |
1903 | if (n->osid != 0) { |
1904 | char *ctx = NULL; | |
1905 | u32 len; | |
1906 | if (security_secid_to_secctx( | |
1907 | n->osid, &ctx, &len)) { | |
1908 | audit_log_format(ab, " osid=%u", n->osid); | |
1909 | if (call_panic) | |
1910 | *call_panic = 2; | |
1911 | } else { | |
1912 | audit_log_format(ab, " obj=%s", ctx); | |
1913 | security_release_secctx(ctx, len); | |
1914 | } | |
1915 | } | |
1916 | ||
d3aea84a JL |
1917 | /* log the audit_names record type */ |
1918 | audit_log_format(ab, " nametype="); | |
1919 | switch(n->type) { | |
1920 | case AUDIT_TYPE_NORMAL: | |
1921 | audit_log_format(ab, "NORMAL"); | |
1922 | break; | |
1923 | case AUDIT_TYPE_PARENT: | |
1924 | audit_log_format(ab, "PARENT"); | |
1925 | break; | |
1926 | case AUDIT_TYPE_CHILD_DELETE: | |
1927 | audit_log_format(ab, "DELETE"); | |
1928 | break; | |
1929 | case AUDIT_TYPE_CHILD_CREATE: | |
1930 | audit_log_format(ab, "CREATE"); | |
1931 | break; | |
1932 | default: | |
1933 | audit_log_format(ab, "UNKNOWN"); | |
1934 | break; | |
1935 | } | |
1936 | ||
b24a30a7 EP |
1937 | audit_log_fcaps(ab, n); |
1938 | audit_log_end(ab); | |
1939 | } | |
1940 | ||
1941 | int audit_log_task_context(struct audit_buffer *ab) | |
1942 | { | |
1943 | char *ctx = NULL; | |
1944 | unsigned len; | |
1945 | int error; | |
1946 | u32 sid; | |
1947 | ||
1948 | security_task_getsecid(current, &sid); | |
1949 | if (!sid) | |
1950 | return 0; | |
1951 | ||
1952 | error = security_secid_to_secctx(sid, &ctx, &len); | |
1953 | if (error) { | |
1954 | if (error != -EINVAL) | |
1955 | goto error_path; | |
1956 | return 0; | |
1957 | } | |
1958 | ||
1959 | audit_log_format(ab, " subj=%s", ctx); | |
1960 | security_release_secctx(ctx, len); | |
1961 | return 0; | |
1962 | ||
1963 | error_path: | |
1964 | audit_panic("error in audit_log_task_context"); | |
1965 | return error; | |
1966 | } | |
1967 | EXPORT_SYMBOL(audit_log_task_context); | |
1968 | ||
4766b199 DB |
1969 | void audit_log_d_path_exe(struct audit_buffer *ab, |
1970 | struct mm_struct *mm) | |
1971 | { | |
5b282552 DB |
1972 | struct file *exe_file; |
1973 | ||
1974 | if (!mm) | |
1975 | goto out_null; | |
4766b199 | 1976 | |
5b282552 DB |
1977 | exe_file = get_mm_exe_file(mm); |
1978 | if (!exe_file) | |
1979 | goto out_null; | |
1980 | ||
1981 | audit_log_d_path(ab, " exe=", &exe_file->f_path); | |
1982 | fput(exe_file); | |
1983 | return; | |
1984 | out_null: | |
1985 | audit_log_format(ab, " exe=(null)"); | |
4766b199 DB |
1986 | } |
1987 | ||
3f5be2da RGB |
1988 | struct tty_struct *audit_get_tty(struct task_struct *tsk) |
1989 | { | |
1990 | struct tty_struct *tty = NULL; | |
1991 | unsigned long flags; | |
1992 | ||
1993 | spin_lock_irqsave(&tsk->sighand->siglock, flags); | |
1994 | if (tsk->signal) | |
1995 | tty = tty_kref_get(tsk->signal->tty); | |
1996 | spin_unlock_irqrestore(&tsk->sighand->siglock, flags); | |
1997 | return tty; | |
1998 | } | |
1999 | ||
2000 | void audit_put_tty(struct tty_struct *tty) | |
2001 | { | |
2002 | tty_kref_put(tty); | |
2003 | } | |
2004 | ||
b24a30a7 EP |
2005 | void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk) |
2006 | { | |
2007 | const struct cred *cred; | |
9eab339b | 2008 | char comm[sizeof(tsk->comm)]; |
db0a6fb5 | 2009 | struct tty_struct *tty; |
b24a30a7 EP |
2010 | |
2011 | if (!ab) | |
2012 | return; | |
2013 | ||
2014 | /* tsk == current */ | |
2015 | cred = current_cred(); | |
db0a6fb5 | 2016 | tty = audit_get_tty(tsk); |
b24a30a7 | 2017 | audit_log_format(ab, |
c92cdeb4 | 2018 | " ppid=%d pid=%d auid=%u uid=%u gid=%u" |
b24a30a7 | 2019 | " euid=%u suid=%u fsuid=%u" |
2f2ad101 | 2020 | " egid=%u sgid=%u fsgid=%u tty=%s ses=%u", |
c92cdeb4 | 2021 | task_ppid_nr(tsk), |
f1dc4867 | 2022 | task_pid_nr(tsk), |
b24a30a7 EP |
2023 | from_kuid(&init_user_ns, audit_get_loginuid(tsk)), |
2024 | from_kuid(&init_user_ns, cred->uid), | |
2025 | from_kgid(&init_user_ns, cred->gid), | |
2026 | from_kuid(&init_user_ns, cred->euid), | |
2027 | from_kuid(&init_user_ns, cred->suid), | |
2028 | from_kuid(&init_user_ns, cred->fsuid), | |
2029 | from_kgid(&init_user_ns, cred->egid), | |
2030 | from_kgid(&init_user_ns, cred->sgid), | |
2031 | from_kgid(&init_user_ns, cred->fsgid), | |
db0a6fb5 RGB |
2032 | tty ? tty_name(tty) : "(none)", |
2033 | audit_get_sessionid(tsk)); | |
2034 | audit_put_tty(tty); | |
b24a30a7 | 2035 | audit_log_format(ab, " comm="); |
9eab339b | 2036 | audit_log_untrustedstring(ab, get_task_comm(comm, tsk)); |
4766b199 | 2037 | audit_log_d_path_exe(ab, tsk->mm); |
b24a30a7 EP |
2038 | audit_log_task_context(ab); |
2039 | } | |
2040 | EXPORT_SYMBOL(audit_log_task_info); | |
2041 | ||
a51d9eaa KC |
2042 | /** |
2043 | * audit_log_link_denied - report a link restriction denial | |
22011964 | 2044 | * @operation: specific link operation |
a51d9eaa KC |
2045 | * @link: the path that triggered the restriction |
2046 | */ | |
2047 | void audit_log_link_denied(const char *operation, struct path *link) | |
2048 | { | |
2049 | struct audit_buffer *ab; | |
b24a30a7 EP |
2050 | struct audit_names *name; |
2051 | ||
2052 | name = kzalloc(sizeof(*name), GFP_NOFS); | |
2053 | if (!name) | |
2054 | return; | |
a51d9eaa | 2055 | |
b24a30a7 | 2056 | /* Generate AUDIT_ANOM_LINK with subject, operation, outcome. */ |
a51d9eaa KC |
2057 | ab = audit_log_start(current->audit_context, GFP_KERNEL, |
2058 | AUDIT_ANOM_LINK); | |
d1c7d97a | 2059 | if (!ab) |
b24a30a7 EP |
2060 | goto out; |
2061 | audit_log_format(ab, "op=%s", operation); | |
2062 | audit_log_task_info(ab, current); | |
2063 | audit_log_format(ab, " res=0"); | |
a51d9eaa | 2064 | audit_log_end(ab); |
b24a30a7 EP |
2065 | |
2066 | /* Generate AUDIT_PATH record with object. */ | |
2067 | name->type = AUDIT_TYPE_NORMAL; | |
3b362157 | 2068 | audit_copy_inode(name, link->dentry, d_backing_inode(link->dentry)); |
b24a30a7 EP |
2069 | audit_log_name(current->audit_context, name, link, 0, NULL); |
2070 | out: | |
2071 | kfree(name); | |
a51d9eaa KC |
2072 | } |
2073 | ||
b0dd25a8 RD |
2074 | /** |
2075 | * audit_log_end - end one audit record | |
2076 | * @ab: the audit_buffer | |
2077 | * | |
4aa83872 PM |
2078 | * We can not do a netlink send inside an irq context because it blocks (last |
2079 | * arg, flags, is not set to MSG_DONTWAIT), so the audit buffer is placed on a | |
2080 | * queue and a tasklet is scheduled to remove them from the queue outside the | |
2081 | * irq context. May be called in any context. | |
b0dd25a8 | 2082 | */ |
b7d11258 | 2083 | void audit_log_end(struct audit_buffer *ab) |
1da177e4 | 2084 | { |
1da177e4 LT |
2085 | if (!ab) |
2086 | return; | |
2087 | if (!audit_rate_check()) { | |
2088 | audit_log_lost("rate limit exceeded"); | |
2089 | } else { | |
af8b824f | 2090 | skb_queue_tail(&audit_queue, ab->skb); |
4aa83872 | 2091 | wake_up_interruptible(&kauditd_wait); |
f3d357b0 | 2092 | ab->skb = NULL; |
1da177e4 | 2093 | } |
16e1904e | 2094 | audit_buffer_free(ab); |
1da177e4 LT |
2095 | } |
2096 | ||
b0dd25a8 RD |
2097 | /** |
2098 | * audit_log - Log an audit record | |
2099 | * @ctx: audit context | |
2100 | * @gfp_mask: type of allocation | |
2101 | * @type: audit message type | |
2102 | * @fmt: format string to use | |
2103 | * @...: variable parameters matching the format string | |
2104 | * | |
2105 | * This is a convenience function that calls audit_log_start, | |
2106 | * audit_log_vformat, and audit_log_end. It may be called | |
2107 | * in any context. | |
2108 | */ | |
5600b892 | 2109 | void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type, |
9ad9ad38 | 2110 | const char *fmt, ...) |
1da177e4 LT |
2111 | { |
2112 | struct audit_buffer *ab; | |
2113 | va_list args; | |
2114 | ||
9ad9ad38 | 2115 | ab = audit_log_start(ctx, gfp_mask, type); |
1da177e4 LT |
2116 | if (ab) { |
2117 | va_start(args, fmt); | |
2118 | audit_log_vformat(ab, fmt, args); | |
2119 | va_end(args); | |
2120 | audit_log_end(ab); | |
2121 | } | |
2122 | } | |
bf45da97 | 2123 | |
131ad62d MDF |
2124 | #ifdef CONFIG_SECURITY |
2125 | /** | |
2126 | * audit_log_secctx - Converts and logs SELinux context | |
2127 | * @ab: audit_buffer | |
2128 | * @secid: security number | |
2129 | * | |
2130 | * This is a helper function that calls security_secid_to_secctx to convert | |
2131 | * secid to secctx and then adds the (converted) SELinux context to the audit | |
2132 | * log by calling audit_log_format, thus also preventing leak of internal secid | |
2133 | * to userspace. If secid cannot be converted audit_panic is called. | |
2134 | */ | |
2135 | void audit_log_secctx(struct audit_buffer *ab, u32 secid) | |
2136 | { | |
2137 | u32 len; | |
2138 | char *secctx; | |
2139 | ||
2140 | if (security_secid_to_secctx(secid, &secctx, &len)) { | |
2141 | audit_panic("Cannot convert secid to context"); | |
2142 | } else { | |
2143 | audit_log_format(ab, " obj=%s", secctx); | |
2144 | security_release_secctx(secctx, len); | |
2145 | } | |
2146 | } | |
2147 | EXPORT_SYMBOL(audit_log_secctx); | |
2148 | #endif | |
2149 | ||
bf45da97 | 2150 | EXPORT_SYMBOL(audit_log_start); |
2151 | EXPORT_SYMBOL(audit_log_end); | |
2152 | EXPORT_SYMBOL(audit_log_format); | |
2153 | EXPORT_SYMBOL(audit_log); |