]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - kernel/audit.c
AUDIT: Treat all user messages identically.
[mirror_ubuntu-bionic-kernel.git] / kernel / audit.c
1 /* audit.c -- Auditing support
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 *
5 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
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 *
24 * Goals: 1) Integrate fully with SELinux.
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 *
41 * Example user-space utilities: http://people.redhat.com/sgrubb/audit/
42 */
43
44 #include <linux/init.h>
45 #include <asm/atomic.h>
46 #include <asm/types.h>
47 #include <linux/mm.h>
48 #include <linux/module.h>
49
50 #include <linux/audit.h>
51
52 #include <net/sock.h>
53 #include <linux/skbuff.h>
54 #include <linux/netlink.h>
55
56 /* No auditing will take place until audit_initialized != 0.
57 * (Initialization happens after skb_init is called.) */
58 static int audit_initialized;
59
60 /* No syscall auditing will take place unless audit_enabled != 0. */
61 int audit_enabled;
62
63 /* Default state when kernel boots without any parameters. */
64 static int audit_default;
65
66 /* If auditing cannot proceed, audit_failure selects what happens. */
67 static int audit_failure = AUDIT_FAIL_PRINTK;
68
69 /* If audit records are to be written to the netlink socket, audit_pid
70 * contains the (non-zero) pid. */
71 int audit_pid;
72
73 /* If audit_limit is non-zero, limit the rate of sending audit records
74 * to that number per second. This prevents DoS attacks, but results in
75 * audit records being dropped. */
76 static int audit_rate_limit;
77
78 /* Number of outstanding audit_buffers allowed. */
79 static int audit_backlog_limit = 64;
80 static atomic_t audit_backlog = ATOMIC_INIT(0);
81
82 /* The identity of the user shutting down the audit system. */
83 uid_t audit_sig_uid = -1;
84 pid_t audit_sig_pid = -1;
85
86 /* Records can be lost in several ways:
87 0) [suppressed in audit_alloc]
88 1) out of memory in audit_log_start [kmalloc of struct audit_buffer]
89 2) out of memory in audit_log_move [alloc_skb]
90 3) suppressed due to audit_rate_limit
91 4) suppressed due to audit_backlog_limit
92 */
93 static atomic_t audit_lost = ATOMIC_INIT(0);
94
95 /* The netlink socket. */
96 static struct sock *audit_sock;
97
98 /* There are two lists of audit buffers. The txlist contains audit
99 * buffers that cannot be sent immediately to the netlink device because
100 * we are in an irq context (these are sent later in a tasklet).
101 *
102 * The second list is a list of pre-allocated audit buffers (if more
103 * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of
104 * being placed on the freelist). */
105 static DEFINE_SPINLOCK(audit_txlist_lock);
106 static DEFINE_SPINLOCK(audit_freelist_lock);
107 static int audit_freelist_count = 0;
108 static LIST_HEAD(audit_txlist);
109 static LIST_HEAD(audit_freelist);
110
111 /* There are three lists of rules -- one to search at task creation
112 * time, one to search at syscall entry time, and another to search at
113 * syscall exit time. */
114 static LIST_HEAD(audit_tsklist);
115 static LIST_HEAD(audit_entlist);
116 static LIST_HEAD(audit_extlist);
117
118 /* The netlink socket is only to be read by 1 CPU, which lets us assume
119 * that list additions and deletions never happen simultaneously in
120 * auditsc.c */
121 static DECLARE_MUTEX(audit_netlink_sem);
122
123 /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
124 * audit records. Since printk uses a 1024 byte buffer, this buffer
125 * should be at least that large. */
126 #define AUDIT_BUFSIZ 1024
127
128 /* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the
129 * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */
130 #define AUDIT_MAXFREE (2*NR_CPUS)
131
132 /* The audit_buffer is used when formatting an audit record. The caller
133 * locks briefly to get the record off the freelist or to allocate the
134 * buffer, and locks briefly to send the buffer to the netlink layer or
135 * to place it on a transmit queue. Multiple audit_buffers can be in
136 * use simultaneously. */
137 struct audit_buffer {
138 struct list_head list;
139 struct sk_buff *skb; /* formatted skb ready to send */
140 struct audit_context *ctx; /* NULL or associated context */
141 };
142
143 static void audit_set_pid(struct audit_buffer *ab, pid_t pid)
144 {
145 struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
146 nlh->nlmsg_pid = pid;
147 }
148
149 struct audit_entry {
150 struct list_head list;
151 struct audit_rule rule;
152 };
153
154 static void audit_log_end_irq(struct audit_buffer *ab);
155 static void audit_log_end_fast(struct audit_buffer *ab);
156
157 static void audit_panic(const char *message)
158 {
159 switch (audit_failure)
160 {
161 case AUDIT_FAIL_SILENT:
162 break;
163 case AUDIT_FAIL_PRINTK:
164 printk(KERN_ERR "audit: %s\n", message);
165 break;
166 case AUDIT_FAIL_PANIC:
167 panic("audit: %s\n", message);
168 break;
169 }
170 }
171
172 static inline int audit_rate_check(void)
173 {
174 static unsigned long last_check = 0;
175 static int messages = 0;
176 static DEFINE_SPINLOCK(lock);
177 unsigned long flags;
178 unsigned long now;
179 unsigned long elapsed;
180 int retval = 0;
181
182 if (!audit_rate_limit) return 1;
183
184 spin_lock_irqsave(&lock, flags);
185 if (++messages < audit_rate_limit) {
186 retval = 1;
187 } else {
188 now = jiffies;
189 elapsed = now - last_check;
190 if (elapsed > HZ) {
191 last_check = now;
192 messages = 0;
193 retval = 1;
194 }
195 }
196 spin_unlock_irqrestore(&lock, flags);
197
198 return retval;
199 }
200
201 /* Emit at least 1 message per second, even if audit_rate_check is
202 * throttling. */
203 void audit_log_lost(const char *message)
204 {
205 static unsigned long last_msg = 0;
206 static DEFINE_SPINLOCK(lock);
207 unsigned long flags;
208 unsigned long now;
209 int print;
210
211 atomic_inc(&audit_lost);
212
213 print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);
214
215 if (!print) {
216 spin_lock_irqsave(&lock, flags);
217 now = jiffies;
218 if (now - last_msg > HZ) {
219 print = 1;
220 last_msg = now;
221 }
222 spin_unlock_irqrestore(&lock, flags);
223 }
224
225 if (print) {
226 printk(KERN_WARNING
227 "audit: audit_lost=%d audit_backlog=%d"
228 " audit_rate_limit=%d audit_backlog_limit=%d\n",
229 atomic_read(&audit_lost),
230 atomic_read(&audit_backlog),
231 audit_rate_limit,
232 audit_backlog_limit);
233 audit_panic(message);
234 }
235
236 }
237
238 static int audit_set_rate_limit(int limit, uid_t loginuid)
239 {
240 int old = audit_rate_limit;
241 audit_rate_limit = limit;
242 audit_log(NULL, AUDIT_CONFIG_CHANGE,
243 "audit_rate_limit=%d old=%d by auid %u",
244 audit_rate_limit, old, loginuid);
245 return old;
246 }
247
248 static int audit_set_backlog_limit(int limit, uid_t loginuid)
249 {
250 int old = audit_backlog_limit;
251 audit_backlog_limit = limit;
252 audit_log(NULL, AUDIT_CONFIG_CHANGE,
253 "audit_backlog_limit=%d old=%d by auid %u",
254 audit_backlog_limit, old, loginuid);
255 return old;
256 }
257
258 static int audit_set_enabled(int state, uid_t loginuid)
259 {
260 int old = audit_enabled;
261 if (state != 0 && state != 1)
262 return -EINVAL;
263 audit_enabled = state;
264 audit_log(NULL, AUDIT_CONFIG_CHANGE,
265 "audit_enabled=%d old=%d by auid %u",
266 audit_enabled, old, loginuid);
267 return old;
268 }
269
270 static int audit_set_failure(int state, uid_t loginuid)
271 {
272 int old = audit_failure;
273 if (state != AUDIT_FAIL_SILENT
274 && state != AUDIT_FAIL_PRINTK
275 && state != AUDIT_FAIL_PANIC)
276 return -EINVAL;
277 audit_failure = state;
278 audit_log(NULL, AUDIT_CONFIG_CHANGE,
279 "audit_failure=%d old=%d by auid %u",
280 audit_failure, old, loginuid);
281 return old;
282 }
283
284 void audit_send_reply(int pid, int seq, int type, int done, int multi,
285 void *payload, int size)
286 {
287 struct sk_buff *skb;
288 struct nlmsghdr *nlh;
289 int len = NLMSG_SPACE(size);
290 void *data;
291 int flags = multi ? NLM_F_MULTI : 0;
292 int t = done ? NLMSG_DONE : type;
293
294 skb = alloc_skb(len, GFP_KERNEL);
295 if (!skb)
296 goto nlmsg_failure;
297
298 nlh = NLMSG_PUT(skb, pid, seq, t, len - sizeof(*nlh));
299 nlh->nlmsg_flags = flags;
300 data = NLMSG_DATA(nlh);
301 memcpy(data, payload, size);
302 netlink_unicast(audit_sock, skb, pid, MSG_DONTWAIT);
303 return;
304
305 nlmsg_failure: /* Used by NLMSG_PUT */
306 if (skb)
307 kfree_skb(skb);
308 }
309
310 /*
311 * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
312 * control messages.
313 */
314 static int audit_netlink_ok(kernel_cap_t eff_cap, u16 msg_type)
315 {
316 int err = 0;
317
318 switch (msg_type) {
319 case AUDIT_GET:
320 case AUDIT_LIST:
321 case AUDIT_SET:
322 case AUDIT_ADD:
323 case AUDIT_DEL:
324 case AUDIT_SIGNAL_INFO:
325 if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL))
326 err = -EPERM;
327 break;
328 case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
329 if (!cap_raised(eff_cap, CAP_AUDIT_WRITE))
330 err = -EPERM;
331 break;
332 default: /* bad msg */
333 err = -EINVAL;
334 }
335
336 return err;
337 }
338
339 static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
340 {
341 u32 uid, pid, seq;
342 void *data;
343 struct audit_status *status_get, status_set;
344 int err;
345 struct audit_buffer *ab;
346 u16 msg_type = nlh->nlmsg_type;
347 uid_t loginuid; /* loginuid of sender */
348 struct audit_sig_info sig_data;
349
350 err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type);
351 if (err)
352 return err;
353
354 pid = NETLINK_CREDS(skb)->pid;
355 uid = NETLINK_CREDS(skb)->uid;
356 loginuid = NETLINK_CB(skb).loginuid;
357 seq = nlh->nlmsg_seq;
358 data = NLMSG_DATA(nlh);
359
360 switch (msg_type) {
361 case AUDIT_GET:
362 status_set.enabled = audit_enabled;
363 status_set.failure = audit_failure;
364 status_set.pid = audit_pid;
365 status_set.rate_limit = audit_rate_limit;
366 status_set.backlog_limit = audit_backlog_limit;
367 status_set.lost = atomic_read(&audit_lost);
368 status_set.backlog = atomic_read(&audit_backlog);
369 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
370 &status_set, sizeof(status_set));
371 break;
372 case AUDIT_SET:
373 if (nlh->nlmsg_len < sizeof(struct audit_status))
374 return -EINVAL;
375 status_get = (struct audit_status *)data;
376 if (status_get->mask & AUDIT_STATUS_ENABLED) {
377 err = audit_set_enabled(status_get->enabled, loginuid);
378 if (err < 0) return err;
379 }
380 if (status_get->mask & AUDIT_STATUS_FAILURE) {
381 err = audit_set_failure(status_get->failure, loginuid);
382 if (err < 0) return err;
383 }
384 if (status_get->mask & AUDIT_STATUS_PID) {
385 int old = audit_pid;
386 audit_pid = status_get->pid;
387 audit_log(NULL, AUDIT_CONFIG_CHANGE,
388 "audit_pid=%d old=%d by auid %u",
389 audit_pid, old, loginuid);
390 }
391 if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
392 audit_set_rate_limit(status_get->rate_limit, loginuid);
393 if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
394 audit_set_backlog_limit(status_get->backlog_limit,
395 loginuid);
396 break;
397 case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
398 ab = audit_log_start(NULL, msg_type);
399 if (!ab)
400 break; /* audit_panic has been called */
401 audit_log_format(ab,
402 "user pid=%d uid=%d length=%d loginuid=%u"
403 " msg='%.1024s'",
404 pid, uid,
405 (int)(nlh->nlmsg_len
406 - ((char *)data - (char *)nlh)),
407 loginuid, (char *)data);
408 audit_set_pid(ab, pid);
409 audit_log_end(ab);
410 break;
411 case AUDIT_ADD:
412 case AUDIT_DEL:
413 if (nlh->nlmsg_len < sizeof(struct audit_rule))
414 return -EINVAL;
415 /* fallthrough */
416 case AUDIT_LIST:
417 err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
418 uid, seq, data, loginuid);
419 break;
420 case AUDIT_SIGNAL_INFO:
421 sig_data.uid = audit_sig_uid;
422 sig_data.pid = audit_sig_pid;
423 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO,
424 0, 0, &sig_data, sizeof(sig_data));
425 break;
426 default:
427 err = -EINVAL;
428 break;
429 }
430
431 return err < 0 ? err : 0;
432 }
433
434 /* Get message from skb (based on rtnetlink_rcv_skb). Each message is
435 * processed by audit_receive_msg. Malformed skbs with wrong length are
436 * discarded silently. */
437 static void audit_receive_skb(struct sk_buff *skb)
438 {
439 int err;
440 struct nlmsghdr *nlh;
441 u32 rlen;
442
443 while (skb->len >= NLMSG_SPACE(0)) {
444 nlh = (struct nlmsghdr *)skb->data;
445 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
446 return;
447 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
448 if (rlen > skb->len)
449 rlen = skb->len;
450 if ((err = audit_receive_msg(skb, nlh))) {
451 netlink_ack(skb, nlh, err);
452 } else if (nlh->nlmsg_flags & NLM_F_ACK)
453 netlink_ack(skb, nlh, 0);
454 skb_pull(skb, rlen);
455 }
456 }
457
458 /* Receive messages from netlink socket. */
459 static void audit_receive(struct sock *sk, int length)
460 {
461 struct sk_buff *skb;
462 unsigned int qlen;
463
464 down(&audit_netlink_sem);
465
466 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
467 skb = skb_dequeue(&sk->sk_receive_queue);
468 audit_receive_skb(skb);
469 kfree_skb(skb);
470 }
471 up(&audit_netlink_sem);
472 }
473
474 /* Grab skbuff from the audit_buffer and send to user space. */
475 static inline int audit_log_drain(struct audit_buffer *ab)
476 {
477 struct sk_buff *skb = ab->skb;
478
479 if (skb) {
480 int retval = 0;
481
482 if (audit_pid) {
483 struct nlmsghdr *nlh = (struct nlmsghdr *)skb->data;
484 nlh->nlmsg_len = skb->len - NLMSG_SPACE(0);
485 skb_get(skb); /* because netlink_* frees */
486 retval = netlink_unicast(audit_sock, skb, audit_pid,
487 MSG_DONTWAIT);
488 }
489 if (retval == -EAGAIN &&
490 (atomic_read(&audit_backlog)) < audit_backlog_limit) {
491 audit_log_end_irq(ab);
492 return 1;
493 }
494 if (retval < 0) {
495 if (retval == -ECONNREFUSED) {
496 printk(KERN_ERR
497 "audit: *NO* daemon at audit_pid=%d\n",
498 audit_pid);
499 audit_pid = 0;
500 } else
501 audit_log_lost("netlink socket too busy");
502 }
503 if (!audit_pid) { /* No daemon */
504 int offset = NLMSG_SPACE(0);
505 int len = skb->len - offset;
506 skb->data[offset + len] = '\0';
507 printk(KERN_ERR "%s\n", skb->data + offset);
508 }
509 }
510 return 0;
511 }
512
513 /* Initialize audit support at boot time. */
514 static int __init audit_init(void)
515 {
516 printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
517 audit_default ? "enabled" : "disabled");
518 audit_sock = netlink_kernel_create(NETLINK_AUDIT, audit_receive);
519 if (!audit_sock)
520 audit_panic("cannot initialize netlink socket");
521
522 audit_initialized = 1;
523 audit_enabled = audit_default;
524 audit_log(NULL, AUDIT_KERNEL, "initialized");
525 return 0;
526 }
527 __initcall(audit_init);
528
529 /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
530 static int __init audit_enable(char *str)
531 {
532 audit_default = !!simple_strtol(str, NULL, 0);
533 printk(KERN_INFO "audit: %s%s\n",
534 audit_default ? "enabled" : "disabled",
535 audit_initialized ? "" : " (after initialization)");
536 if (audit_initialized)
537 audit_enabled = audit_default;
538 return 0;
539 }
540
541 __setup("audit=", audit_enable);
542
543 static void audit_buffer_free(struct audit_buffer *ab)
544 {
545 unsigned long flags;
546
547 if (!ab)
548 return;
549
550 if (ab->skb)
551 kfree_skb(ab->skb);
552 atomic_dec(&audit_backlog);
553 spin_lock_irqsave(&audit_freelist_lock, flags);
554 if (++audit_freelist_count > AUDIT_MAXFREE)
555 kfree(ab);
556 else
557 list_add(&ab->list, &audit_freelist);
558 spin_unlock_irqrestore(&audit_freelist_lock, flags);
559 }
560
561 static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
562 int gfp_mask, int type)
563 {
564 unsigned long flags;
565 struct audit_buffer *ab = NULL;
566 struct nlmsghdr *nlh;
567
568 spin_lock_irqsave(&audit_freelist_lock, flags);
569 if (!list_empty(&audit_freelist)) {
570 ab = list_entry(audit_freelist.next,
571 struct audit_buffer, list);
572 list_del(&ab->list);
573 --audit_freelist_count;
574 }
575 spin_unlock_irqrestore(&audit_freelist_lock, flags);
576
577 if (!ab) {
578 ab = kmalloc(sizeof(*ab), gfp_mask);
579 if (!ab)
580 goto err;
581 }
582 atomic_inc(&audit_backlog);
583
584 ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask);
585 if (!ab->skb)
586 goto err;
587
588 ab->ctx = ctx;
589 nlh = (struct nlmsghdr *)skb_put(ab->skb, NLMSG_SPACE(0));
590 nlh->nlmsg_type = type;
591 nlh->nlmsg_flags = 0;
592 nlh->nlmsg_pid = 0;
593 nlh->nlmsg_seq = 0;
594 return ab;
595 err:
596 audit_buffer_free(ab);
597 return NULL;
598 }
599
600 /* Obtain an audit buffer. This routine does locking to obtain the
601 * audit buffer, but then no locking is required for calls to
602 * audit_log_*format. If the tsk is a task that is currently in a
603 * syscall, then the syscall is marked as auditable and an audit record
604 * will be written at syscall exit. If there is no associated task, tsk
605 * should be NULL. */
606 struct audit_buffer *audit_log_start(struct audit_context *ctx, int type)
607 {
608 struct audit_buffer *ab = NULL;
609 struct timespec t;
610 unsigned int serial;
611
612 if (!audit_initialized)
613 return NULL;
614
615 if (audit_backlog_limit
616 && atomic_read(&audit_backlog) > audit_backlog_limit) {
617 if (audit_rate_check())
618 printk(KERN_WARNING
619 "audit: audit_backlog=%d > "
620 "audit_backlog_limit=%d\n",
621 atomic_read(&audit_backlog),
622 audit_backlog_limit);
623 audit_log_lost("backlog limit exceeded");
624 return NULL;
625 }
626
627 ab = audit_buffer_alloc(ctx, GFP_ATOMIC, type);
628 if (!ab) {
629 audit_log_lost("out of memory in audit_log_start");
630 return NULL;
631 }
632
633 if (!audit_get_stamp(ab->ctx, &t, &serial)) {
634 t = CURRENT_TIME;
635 serial = 0;
636 }
637
638 audit_log_format(ab, "audit(%lu.%03lu:%u): ",
639 t.tv_sec, t.tv_nsec/1000000, serial);
640 return ab;
641 }
642
643 /**
644 * audit_expand - expand skb in the audit buffer
645 * @ab: audit_buffer
646 *
647 * Returns 0 (no space) on failed expansion, or available space if
648 * successful.
649 */
650 static inline int audit_expand(struct audit_buffer *ab, int extra)
651 {
652 struct sk_buff *skb = ab->skb;
653 int ret = pskb_expand_head(skb, skb_headroom(skb), extra,
654 GFP_ATOMIC);
655 if (ret < 0) {
656 audit_log_lost("out of memory in audit_expand");
657 return 0;
658 }
659 return skb_tailroom(skb);
660 }
661
662 /* Format an audit message into the audit buffer. If there isn't enough
663 * room in the audit buffer, more room will be allocated and vsnprint
664 * will be called a second time. Currently, we assume that a printk
665 * can't format message larger than 1024 bytes, so we don't either. */
666 static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
667 va_list args)
668 {
669 int len, avail;
670 struct sk_buff *skb;
671 va_list args2;
672
673 if (!ab)
674 return;
675
676 BUG_ON(!ab->skb);
677 skb = ab->skb;
678 avail = skb_tailroom(skb);
679 if (avail == 0) {
680 avail = audit_expand(ab, AUDIT_BUFSIZ);
681 if (!avail)
682 goto out;
683 }
684 va_copy(args2, args);
685 len = vsnprintf(skb->tail, avail, fmt, args);
686 if (len >= avail) {
687 /* The printk buffer is 1024 bytes long, so if we get
688 * here and AUDIT_BUFSIZ is at least 1024, then we can
689 * log everything that printk could have logged. */
690 avail = audit_expand(ab, max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
691 if (!avail)
692 goto out;
693 len = vsnprintf(skb->tail, avail, fmt, args2);
694 }
695 skb_put(skb, (len < avail) ? len : avail);
696 out:
697 return;
698 }
699
700 /* Format a message into the audit buffer. All the work is done in
701 * audit_log_vformat. */
702 void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
703 {
704 va_list args;
705
706 if (!ab)
707 return;
708 va_start(args, fmt);
709 audit_log_vformat(ab, fmt, args);
710 va_end(args);
711 }
712
713 void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf, size_t len)
714 {
715 int i;
716
717 for (i=0; i<len; i++)
718 audit_log_format(ab, "%02x", buf[i]);
719 }
720
721 void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
722 {
723 const unsigned char *p = string;
724
725 while (*p) {
726 if (*p == '"' || *p == ' ' || *p < 0x20 || *p > 0x7f) {
727 audit_log_hex(ab, string, strlen(string));
728 return;
729 }
730 p++;
731 }
732 audit_log_format(ab, "\"%s\"", string);
733 }
734
735
736 /* This is a helper-function to print the d_path without using a static
737 * buffer or allocating another buffer in addition to the one in
738 * audit_buffer. */
739 void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
740 struct dentry *dentry, struct vfsmount *vfsmnt)
741 {
742 char *p;
743 struct sk_buff *skb = ab->skb;
744 int len, avail;
745
746 if (prefix)
747 audit_log_format(ab, " %s", prefix);
748
749 avail = skb_tailroom(skb);
750 p = d_path(dentry, vfsmnt, skb->tail, avail);
751 if (IS_ERR(p)) {
752 /* FIXME: can we save some information here? */
753 audit_log_format(ab, "<toolong>");
754 } else {
755 /* path isn't at start of buffer */
756 len = ((char *)skb->tail + avail - 1) - p;
757 memmove(skb->tail, p, len);
758 skb_put(skb, len);
759 }
760 }
761
762 /* Remove queued messages from the audit_txlist and send them to user space. */
763 static void audit_tasklet_handler(unsigned long arg)
764 {
765 LIST_HEAD(list);
766 struct audit_buffer *ab;
767 unsigned long flags;
768
769 spin_lock_irqsave(&audit_txlist_lock, flags);
770 list_splice_init(&audit_txlist, &list);
771 spin_unlock_irqrestore(&audit_txlist_lock, flags);
772
773 while (!list_empty(&list)) {
774 ab = list_entry(list.next, struct audit_buffer, list);
775 list_del(&ab->list);
776 audit_log_end_fast(ab);
777 }
778 }
779
780 static DECLARE_TASKLET(audit_tasklet, audit_tasklet_handler, 0);
781
782 /* The netlink_* functions cannot be called inside an irq context, so
783 * the audit buffer is places on a queue and a tasklet is scheduled to
784 * remove them from the queue outside the irq context. May be called in
785 * any context. */
786 static void audit_log_end_irq(struct audit_buffer *ab)
787 {
788 unsigned long flags;
789
790 if (!ab)
791 return;
792 spin_lock_irqsave(&audit_txlist_lock, flags);
793 list_add_tail(&ab->list, &audit_txlist);
794 spin_unlock_irqrestore(&audit_txlist_lock, flags);
795
796 tasklet_schedule(&audit_tasklet);
797 }
798
799 /* Send the message in the audit buffer directly to user space. May not
800 * be called in an irq context. */
801 static void audit_log_end_fast(struct audit_buffer *ab)
802 {
803 BUG_ON(in_irq());
804 if (!ab)
805 return;
806 if (!audit_rate_check()) {
807 audit_log_lost("rate limit exceeded");
808 } else {
809 if (audit_log_drain(ab))
810 return;
811 }
812 audit_buffer_free(ab);
813 }
814
815 /* Send or queue the message in the audit buffer, depending on the
816 * current context. (A convenience function that may be called in any
817 * context.) */
818 void audit_log_end(struct audit_buffer *ab)
819 {
820 if (in_irq())
821 audit_log_end_irq(ab);
822 else
823 audit_log_end_fast(ab);
824 }
825
826 /* Log an audit record. This is a convenience function that calls
827 * audit_log_start, audit_log_vformat, and audit_log_end. It may be
828 * called in any context. */
829 void audit_log(struct audit_context *ctx, int type, const char *fmt, ...)
830 {
831 struct audit_buffer *ab;
832 va_list args;
833
834 ab = audit_log_start(ctx, type);
835 if (ab) {
836 va_start(args, fmt);
837 audit_log_vformat(ab, fmt, args);
838 va_end(args);
839 audit_log_end(ab);
840 }
841 }