blob: 4a697c73faec0b952d4b240e7c3fadef49313148 [file] [log] [blame]
85c87212005-04-29 16:23:29 +01001/* audit.c -- Auditing support
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * 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 *
85c87212005-04-29 16:23:29 +010041 * Example user-space utilities: http://people.redhat.com/sgrubb/audit/
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 */
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.) */
58static int audit_initialized;
59
60/* No syscall auditing will take place unless audit_enabled != 0. */
61int audit_enabled;
62
63/* Default state when kernel boots without any parameters. */
64static int audit_default;
65
66/* If auditing cannot proceed, audit_failure selects what happens. */
67static 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. */
71static 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. */
76static int audit_rate_limit;
77
78/* Number of outstanding audit_buffers allowed. */
79static int audit_backlog_limit = 64;
80static atomic_t audit_backlog = ATOMIC_INIT(0);
81
82/* Records can be lost in several ways:
83 0) [suppressed in audit_alloc]
84 1) out of memory in audit_log_start [kmalloc of struct audit_buffer]
85 2) out of memory in audit_log_move [alloc_skb]
86 3) suppressed due to audit_rate_limit
87 4) suppressed due to audit_backlog_limit
88*/
89static atomic_t audit_lost = ATOMIC_INIT(0);
90
91/* The netlink socket. */
92static struct sock *audit_sock;
93
94/* There are two lists of audit buffers. The txlist contains audit
95 * buffers that cannot be sent immediately to the netlink device because
96 * we are in an irq context (these are sent later in a tasklet).
97 *
98 * The second list is a list of pre-allocated audit buffers (if more
99 * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of
100 * being placed on the freelist). */
101static DEFINE_SPINLOCK(audit_txlist_lock);
102static DEFINE_SPINLOCK(audit_freelist_lock);
103static int audit_freelist_count = 0;
104static LIST_HEAD(audit_txlist);
105static LIST_HEAD(audit_freelist);
106
107/* There are three lists of rules -- one to search at task creation
108 * time, one to search at syscall entry time, and another to search at
109 * syscall exit time. */
110static LIST_HEAD(audit_tsklist);
111static LIST_HEAD(audit_entlist);
112static LIST_HEAD(audit_extlist);
113
114/* The netlink socket is only to be read by 1 CPU, which lets us assume
115 * that list additions and deletions never happen simultaneiously in
116 * auditsc.c */
117static DECLARE_MUTEX(audit_netlink_sem);
118
119/* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
120 * audit records. Since printk uses a 1024 byte buffer, this buffer
121 * should be at least that large. */
122#define AUDIT_BUFSIZ 1024
123
124/* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the
125 * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */
126#define AUDIT_MAXFREE (2*NR_CPUS)
127
128/* The audit_buffer is used when formatting an audit record. The caller
129 * locks briefly to get the record off the freelist or to allocate the
130 * buffer, and locks briefly to send the buffer to the netlink layer or
131 * to place it on a transmit queue. Multiple audit_buffers can be in
132 * use simultaneously. */
133struct audit_buffer {
134 struct list_head list;
135 struct sk_buff_head sklist; /* formatted skbs ready to send */
136 struct audit_context *ctx; /* NULL or associated context */
137 int len; /* used area of tmp */
138 char tmp[AUDIT_BUFSIZ];
139
140 /* Pointer to header and contents */
141 struct nlmsghdr *nlh;
142 int total;
143 int type;
144 int pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145};
146
147void audit_set_type(struct audit_buffer *ab, int type)
148{
149 ab->type = type;
150}
151
152struct audit_entry {
153 struct list_head list;
154 struct audit_rule rule;
155};
156
157static void audit_log_end_irq(struct audit_buffer *ab);
158static void audit_log_end_fast(struct audit_buffer *ab);
159
160static void audit_panic(const char *message)
161{
162 switch (audit_failure)
163 {
164 case AUDIT_FAIL_SILENT:
165 break;
166 case AUDIT_FAIL_PRINTK:
167 printk(KERN_ERR "audit: %s\n", message);
168 break;
169 case AUDIT_FAIL_PANIC:
170 panic("audit: %s\n", message);
171 break;
172 }
173}
174
175static inline int audit_rate_check(void)
176{
177 static unsigned long last_check = 0;
178 static int messages = 0;
179 static DEFINE_SPINLOCK(lock);
180 unsigned long flags;
181 unsigned long now;
182 unsigned long elapsed;
183 int retval = 0;
184
185 if (!audit_rate_limit) return 1;
186
187 spin_lock_irqsave(&lock, flags);
188 if (++messages < audit_rate_limit) {
189 retval = 1;
190 } else {
191 now = jiffies;
192 elapsed = now - last_check;
193 if (elapsed > HZ) {
194 last_check = now;
195 messages = 0;
196 retval = 1;
197 }
198 }
199 spin_unlock_irqrestore(&lock, flags);
200
201 return retval;
202}
203
204/* Emit at least 1 message per second, even if audit_rate_check is
205 * throttling. */
206void audit_log_lost(const char *message)
207{
208 static unsigned long last_msg = 0;
209 static DEFINE_SPINLOCK(lock);
210 unsigned long flags;
211 unsigned long now;
212 int print;
213
214 atomic_inc(&audit_lost);
215
216 print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);
217
218 if (!print) {
219 spin_lock_irqsave(&lock, flags);
220 now = jiffies;
221 if (now - last_msg > HZ) {
222 print = 1;
223 last_msg = now;
224 }
225 spin_unlock_irqrestore(&lock, flags);
226 }
227
228 if (print) {
229 printk(KERN_WARNING
230 "audit: audit_lost=%d audit_backlog=%d"
231 " audit_rate_limit=%d audit_backlog_limit=%d\n",
232 atomic_read(&audit_lost),
233 atomic_read(&audit_backlog),
234 audit_rate_limit,
235 audit_backlog_limit);
236 audit_panic(message);
237 }
238
239}
240
Serge Hallync94c2572005-04-29 16:27:17 +0100241static int audit_set_rate_limit(int limit, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 int old = audit_rate_limit;
244 audit_rate_limit = limit;
Serge Hallync94c2572005-04-29 16:27:17 +0100245 audit_log(NULL, "audit_rate_limit=%d old=%d by auid %u",
246 audit_rate_limit, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return old;
248}
249
Serge Hallync94c2572005-04-29 16:27:17 +0100250static int audit_set_backlog_limit(int limit, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 int old = audit_backlog_limit;
253 audit_backlog_limit = limit;
Serge Hallync94c2572005-04-29 16:27:17 +0100254 audit_log(NULL, "audit_backlog_limit=%d old=%d by auid %u",
255 audit_backlog_limit, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 return old;
257}
258
Serge Hallync94c2572005-04-29 16:27:17 +0100259static int audit_set_enabled(int state, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 int old = audit_enabled;
262 if (state != 0 && state != 1)
263 return -EINVAL;
264 audit_enabled = state;
Serge Hallync94c2572005-04-29 16:27:17 +0100265 audit_log(NULL, "audit_enabled=%d old=%d by auid %u",
266 audit_enabled, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 return old;
268}
269
Serge Hallync94c2572005-04-29 16:27:17 +0100270static int audit_set_failure(int state, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
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;
Serge Hallync94c2572005-04-29 16:27:17 +0100278 audit_log(NULL, "audit_failure=%d old=%d by auid %u",
279 audit_failure, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return old;
281}
282
283#ifdef CONFIG_NET
284void 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
305nlmsg_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 */
314static 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 if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL))
325 err = -EPERM;
326 break;
327 case AUDIT_USER:
328 if (!cap_raised(eff_cap, CAP_AUDIT_WRITE))
329 err = -EPERM;
330 break;
331 default: /* bad msg */
332 err = -EINVAL;
333 }
334
335 return err;
336}
337
338static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
339{
340 u32 uid, pid, seq;
341 void *data;
342 struct audit_status *status_get, status_set;
343 int err;
344 struct audit_buffer *ab;
345 u16 msg_type = nlh->nlmsg_type;
Serge Hallync94c2572005-04-29 16:27:17 +0100346 uid_t loginuid; /* loginuid of sender */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type);
349 if (err)
350 return err;
351
352 pid = NETLINK_CREDS(skb)->pid;
353 uid = NETLINK_CREDS(skb)->uid;
Serge Hallync94c2572005-04-29 16:27:17 +0100354 loginuid = NETLINK_CB(skb).loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 seq = nlh->nlmsg_seq;
356 data = NLMSG_DATA(nlh);
357
358 switch (msg_type) {
359 case AUDIT_GET:
360 status_set.enabled = audit_enabled;
361 status_set.failure = audit_failure;
362 status_set.pid = audit_pid;
363 status_set.rate_limit = audit_rate_limit;
364 status_set.backlog_limit = audit_backlog_limit;
365 status_set.lost = atomic_read(&audit_lost);
366 status_set.backlog = atomic_read(&audit_backlog);
367 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
368 &status_set, sizeof(status_set));
369 break;
370 case AUDIT_SET:
371 if (nlh->nlmsg_len < sizeof(struct audit_status))
372 return -EINVAL;
373 status_get = (struct audit_status *)data;
374 if (status_get->mask & AUDIT_STATUS_ENABLED) {
Serge Hallync94c2572005-04-29 16:27:17 +0100375 err = audit_set_enabled(status_get->enabled, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 if (err < 0) return err;
377 }
378 if (status_get->mask & AUDIT_STATUS_FAILURE) {
Serge Hallync94c2572005-04-29 16:27:17 +0100379 err = audit_set_failure(status_get->failure, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (err < 0) return err;
381 }
382 if (status_get->mask & AUDIT_STATUS_PID) {
383 int old = audit_pid;
384 audit_pid = status_get->pid;
Serge Hallync94c2572005-04-29 16:27:17 +0100385 audit_log(NULL, "audit_pid=%d old=%d by auid %u",
386 audit_pid, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388 if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
Serge Hallync94c2572005-04-29 16:27:17 +0100389 audit_set_rate_limit(status_get->rate_limit, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
Serge Hallync94c2572005-04-29 16:27:17 +0100391 audit_set_backlog_limit(status_get->backlog_limit,
392 loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 break;
394 case AUDIT_USER:
395 ab = audit_log_start(NULL);
396 if (!ab)
397 break; /* audit_panic has been called */
398 audit_log_format(ab,
Serge Hallync94c2572005-04-29 16:27:17 +0100399 "user pid=%d uid=%d length=%d loginuid=%u"
400 " msg='%.1024s'",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 pid, uid,
402 (int)(nlh->nlmsg_len
403 - ((char *)data - (char *)nlh)),
Serge Hallync94c2572005-04-29 16:27:17 +0100404 loginuid, (char *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 ab->type = AUDIT_USER;
406 ab->pid = pid;
407 audit_log_end(ab);
408 break;
409 case AUDIT_ADD:
410 case AUDIT_DEL:
411 if (nlh->nlmsg_len < sizeof(struct audit_rule))
412 return -EINVAL;
413 /* fallthrough */
414 case AUDIT_LIST:
415#ifdef CONFIG_AUDITSYSCALL
416 err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
Serge Hallync94c2572005-04-29 16:27:17 +0100417 uid, seq, data, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418#else
419 err = -EOPNOTSUPP;
420#endif
421 break;
422 default:
423 err = -EINVAL;
424 break;
425 }
426
427 return err < 0 ? err : 0;
428}
429
430/* Get message from skb (based on rtnetlink_rcv_skb). Each message is
431 * processed by audit_receive_msg. Malformed skbs with wrong length are
432 * discarded silently. */
433static int audit_receive_skb(struct sk_buff *skb)
434{
435 int err;
436 struct nlmsghdr *nlh;
437 u32 rlen;
438
439 while (skb->len >= NLMSG_SPACE(0)) {
440 nlh = (struct nlmsghdr *)skb->data;
441 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
442 return 0;
443 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
444 if (rlen > skb->len)
445 rlen = skb->len;
446 if ((err = audit_receive_msg(skb, nlh))) {
447 netlink_ack(skb, nlh, err);
448 } else if (nlh->nlmsg_flags & NLM_F_ACK)
449 netlink_ack(skb, nlh, 0);
450 skb_pull(skb, rlen);
451 }
452 return 0;
453}
454
455/* Receive messages from netlink socket. */
456static void audit_receive(struct sock *sk, int length)
457{
458 struct sk_buff *skb;
459
460 if (down_trylock(&audit_netlink_sem))
461 return;
462
463 /* FIXME: this must not cause starvation */
464 while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
465 if (audit_receive_skb(skb) && skb->len)
466 skb_queue_head(&sk->sk_receive_queue, skb);
467 else
468 kfree_skb(skb);
469 }
470 up(&audit_netlink_sem);
471}
472
473/* Move data from tmp buffer into an skb. This is an extra copy, and
474 * that is unfortunate. However, the copy will only occur when a record
475 * is being written to user space, which is already a high-overhead
476 * operation. (Elimination of the copy is possible, for example, by
477 * writing directly into a pre-allocated skb, at the cost of wasting
478 * memory. */
479static void audit_log_move(struct audit_buffer *ab)
480{
481 struct sk_buff *skb;
482 char *start;
483 int extra = ab->nlh ? 0 : NLMSG_SPACE(0);
484
485 /* possible resubmission */
486 if (ab->len == 0)
487 return;
488
489 skb = skb_peek(&ab->sklist);
490 if (!skb || skb_tailroom(skb) <= ab->len + extra) {
491 skb = alloc_skb(2 * ab->len + extra, GFP_ATOMIC);
492 if (!skb) {
493 ab->len = 0; /* Lose information in ab->tmp */
494 audit_log_lost("out of memory in audit_log_move");
495 return;
496 }
497 __skb_queue_tail(&ab->sklist, skb);
498 if (!ab->nlh)
499 ab->nlh = (struct nlmsghdr *)skb_put(skb,
500 NLMSG_SPACE(0));
501 }
502 start = skb_put(skb, ab->len);
503 memcpy(start, ab->tmp, ab->len);
504 ab->len = 0;
505}
506
507/* Iterate over the skbuff in the audit_buffer, sending their contents
508 * to user space. */
509static inline int audit_log_drain(struct audit_buffer *ab)
510{
511 struct sk_buff *skb;
512
513 while ((skb = skb_dequeue(&ab->sklist))) {
514 int retval = 0;
515
516 if (audit_pid) {
517 if (ab->nlh) {
518 ab->nlh->nlmsg_len = ab->total;
519 ab->nlh->nlmsg_type = ab->type;
520 ab->nlh->nlmsg_flags = 0;
521 ab->nlh->nlmsg_seq = 0;
522 ab->nlh->nlmsg_pid = ab->pid;
523 }
524 skb_get(skb); /* because netlink_* frees */
525 retval = netlink_unicast(audit_sock, skb, audit_pid,
526 MSG_DONTWAIT);
527 }
Chris Wright37509e72005-04-29 17:19:14 +0100528 if (retval == -EAGAIN &&
529 (atomic_read(&audit_backlog)) < audit_backlog_limit) {
530 skb_queue_head(&ab->sklist, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 audit_log_end_irq(ab);
532 return 1;
533 }
534 if (retval < 0) {
535 if (retval == -ECONNREFUSED) {
536 printk(KERN_ERR
537 "audit: *NO* daemon at audit_pid=%d\n",
538 audit_pid);
539 audit_pid = 0;
540 } else
541 audit_log_lost("netlink socket too busy");
542 }
543 if (!audit_pid) { /* No daemon */
544 int offset = ab->nlh ? NLMSG_SPACE(0) : 0;
545 int len = skb->len - offset;
Peter Martuccellic7fcb0e2005-04-29 16:10:24 +0100546 skb->data[offset + len] = '\0';
547 printk(KERN_ERR "%s\n", skb->data + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549 kfree_skb(skb);
550 ab->nlh = NULL;
551 }
552 return 0;
553}
554
555/* Initialize audit support at boot time. */
556static int __init audit_init(void)
557{
558 printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
559 audit_default ? "enabled" : "disabled");
560 audit_sock = netlink_kernel_create(NETLINK_AUDIT, audit_receive);
561 if (!audit_sock)
562 audit_panic("cannot initialize netlink socket");
563
564 audit_initialized = 1;
565 audit_enabled = audit_default;
566 audit_log(NULL, "initialized");
567 return 0;
568}
569
570#else
571/* Without CONFIG_NET, we have no skbuffs. For now, print what we have
572 * in the buffer. */
573static void audit_log_move(struct audit_buffer *ab)
574{
575 printk(KERN_ERR "%*.*s\n", ab->len, ab->len, ab->tmp);
576 ab->len = 0;
577}
578
579static inline int audit_log_drain(struct audit_buffer *ab)
580{
581 return 0;
582}
583
584/* Initialize audit support at boot time. */
585int __init audit_init(void)
586{
587 printk(KERN_INFO "audit: initializing WITHOUT netlink support\n");
588 audit_sock = NULL;
589 audit_pid = 0;
590
591 audit_initialized = 1;
592 audit_enabled = audit_default;
593 audit_log(NULL, "initialized");
594 return 0;
595}
596#endif
597
598__initcall(audit_init);
599
600/* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
601static int __init audit_enable(char *str)
602{
603 audit_default = !!simple_strtol(str, NULL, 0);
604 printk(KERN_INFO "audit: %s%s\n",
605 audit_default ? "enabled" : "disabled",
606 audit_initialized ? "" : " (after initialization)");
607 if (audit_initialized)
608 audit_enabled = audit_default;
609 return 0;
610}
611
612__setup("audit=", audit_enable);
613
614
615/* Obtain an audit buffer. This routine does locking to obtain the
616 * audit buffer, but then no locking is required for calls to
617 * audit_log_*format. If the tsk is a task that is currently in a
618 * syscall, then the syscall is marked as auditable and an audit record
619 * will be written at syscall exit. If there is no associated task, tsk
620 * should be NULL. */
621struct audit_buffer *audit_log_start(struct audit_context *ctx)
622{
623 struct audit_buffer *ab = NULL;
624 unsigned long flags;
625 struct timespec t;
Steve Grubbd812ddb2005-04-29 16:09:52 +0100626 unsigned int serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 if (!audit_initialized)
629 return NULL;
630
631 if (audit_backlog_limit
632 && atomic_read(&audit_backlog) > audit_backlog_limit) {
633 if (audit_rate_check())
634 printk(KERN_WARNING
635 "audit: audit_backlog=%d > "
636 "audit_backlog_limit=%d\n",
637 atomic_read(&audit_backlog),
638 audit_backlog_limit);
639 audit_log_lost("backlog limit exceeded");
640 return NULL;
641 }
642
643 spin_lock_irqsave(&audit_freelist_lock, flags);
644 if (!list_empty(&audit_freelist)) {
645 ab = list_entry(audit_freelist.next,
646 struct audit_buffer, list);
647 list_del(&ab->list);
648 --audit_freelist_count;
649 }
650 spin_unlock_irqrestore(&audit_freelist_lock, flags);
651
652 if (!ab)
653 ab = kmalloc(sizeof(*ab), GFP_ATOMIC);
654 if (!ab) {
655 audit_log_lost("out of memory in audit_log_start");
656 return NULL;
657 }
658
659 atomic_inc(&audit_backlog);
660 skb_queue_head_init(&ab->sklist);
661
662 ab->ctx = ctx;
663 ab->len = 0;
664 ab->nlh = NULL;
665 ab->total = 0;
666 ab->type = AUDIT_KERNEL;
667 ab->pid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669#ifdef CONFIG_AUDITSYSCALL
670 if (ab->ctx)
671 audit_get_stamp(ab->ctx, &t, &serial);
672 else
673#endif
Steve Grubbd812ddb2005-04-29 16:09:52 +0100674 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 t = CURRENT_TIME;
Steve Grubbd812ddb2005-04-29 16:09:52 +0100676 serial = 0;
677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 audit_log_format(ab, "audit(%lu.%03lu:%u): ",
679 t.tv_sec, t.tv_nsec/1000000, serial);
680 return ab;
681}
682
683
684/* Format an audit message into the audit buffer. If there isn't enough
685 * room in the audit buffer, more room will be allocated and vsnprint
686 * will be called a second time. Currently, we assume that a printk
687 * can't format message larger than 1024 bytes, so we don't either. */
688static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
689 va_list args)
690{
691 int len, avail;
692
693 if (!ab)
694 return;
695
696 avail = sizeof(ab->tmp) - ab->len;
697 if (avail <= 0) {
698 audit_log_move(ab);
699 avail = sizeof(ab->tmp) - ab->len;
700 }
701 len = vsnprintf(ab->tmp + ab->len, avail, fmt, args);
702 if (len >= avail) {
703 /* The printk buffer is 1024 bytes long, so if we get
704 * here and AUDIT_BUFSIZ is at least 1024, then we can
705 * log everything that printk could have logged. */
706 audit_log_move(ab);
707 avail = sizeof(ab->tmp) - ab->len;
708 len = vsnprintf(ab->tmp + ab->len, avail, fmt, args);
709 }
710 ab->len += (len < avail) ? len : avail;
711 ab->total += (len < avail) ? len : avail;
712}
713
714/* Format a message into the audit buffer. All the work is done in
715 * audit_log_vformat. */
716void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
717{
718 va_list args;
719
720 if (!ab)
721 return;
722 va_start(args, fmt);
723 audit_log_vformat(ab, fmt, args);
724 va_end(args);
725}
726
83c7d092005-04-29 15:54:44 +0100727void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf, size_t len)
728{
729 int i;
730
731 for (i=0; i<len; i++)
732 audit_log_format(ab, "%02x", buf[i]);
733}
734
735void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
736{
Andrew Morton81b78542005-04-29 15:59:11 +0100737 const unsigned char *p = string;
83c7d092005-04-29 15:54:44 +0100738
739 while (*p) {
740 if (*p == '"' || *p == ' ' || *p < 0x20 || *p > 0x7f) {
741 audit_log_hex(ab, string, strlen(string));
742 return;
743 }
744 p++;
745 }
746 audit_log_format(ab, "\"%s\"", string);
747}
748
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750/* This is a helper-function to print the d_path without using a static
751 * buffer or allocating another buffer in addition to the one in
752 * audit_buffer. */
753void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
754 struct dentry *dentry, struct vfsmount *vfsmnt)
755{
756 char *p;
757 int len, avail;
758
759 if (prefix) audit_log_format(ab, " %s", prefix);
760
761 if (ab->len > 128)
762 audit_log_move(ab);
763 avail = sizeof(ab->tmp) - ab->len;
764 p = d_path(dentry, vfsmnt, ab->tmp + ab->len, avail);
765 if (IS_ERR(p)) {
766 /* FIXME: can we save some information here? */
767 audit_log_format(ab, "<toolong>");
768 } else {
769 /* path isn't at start of buffer */
770 len = (ab->tmp + sizeof(ab->tmp) - 1) - p;
771 memmove(ab->tmp + ab->len, p, len);
772 ab->len += len;
773 ab->total += len;
774 }
775}
776
777/* Remove queued messages from the audit_txlist and send them to userspace. */
778static void audit_tasklet_handler(unsigned long arg)
779{
780 LIST_HEAD(list);
781 struct audit_buffer *ab;
782 unsigned long flags;
783
784 spin_lock_irqsave(&audit_txlist_lock, flags);
785 list_splice_init(&audit_txlist, &list);
786 spin_unlock_irqrestore(&audit_txlist_lock, flags);
787
788 while (!list_empty(&list)) {
789 ab = list_entry(list.next, struct audit_buffer, list);
790 list_del(&ab->list);
791 audit_log_end_fast(ab);
792 }
793}
794
795static DECLARE_TASKLET(audit_tasklet, audit_tasklet_handler, 0);
796
797/* The netlink_* functions cannot be called inside an irq context, so
798 * the audit buffer is places on a queue and a tasklet is scheduled to
799 * remove them from the queue outside the irq context. May be called in
800 * any context. */
801static void audit_log_end_irq(struct audit_buffer *ab)
802{
803 unsigned long flags;
804
805 if (!ab)
806 return;
807 spin_lock_irqsave(&audit_txlist_lock, flags);
808 list_add_tail(&ab->list, &audit_txlist);
809 spin_unlock_irqrestore(&audit_txlist_lock, flags);
810
811 tasklet_schedule(&audit_tasklet);
812}
813
814/* Send the message in the audit buffer directly to user space. May not
815 * be called in an irq context. */
816static void audit_log_end_fast(struct audit_buffer *ab)
817{
818 unsigned long flags;
819
820 BUG_ON(in_irq());
821 if (!ab)
822 return;
823 if (!audit_rate_check()) {
824 audit_log_lost("rate limit exceeded");
825 } else {
826 audit_log_move(ab);
827 if (audit_log_drain(ab))
828 return;
829 }
830
831 atomic_dec(&audit_backlog);
832 spin_lock_irqsave(&audit_freelist_lock, flags);
833 if (++audit_freelist_count > AUDIT_MAXFREE)
834 kfree(ab);
835 else
836 list_add(&ab->list, &audit_freelist);
837 spin_unlock_irqrestore(&audit_freelist_lock, flags);
838}
839
840/* Send or queue the message in the audit buffer, depending on the
841 * current context. (A convenience function that may be called in any
842 * context.) */
843void audit_log_end(struct audit_buffer *ab)
844{
845 if (in_irq())
846 audit_log_end_irq(ab);
847 else
848 audit_log_end_fast(ab);
849}
850
851/* Log an audit record. This is a convenience function that calls
852 * audit_log_start, audit_log_vformat, and audit_log_end. It may be
853 * called in any context. */
854void audit_log(struct audit_context *ctx, const char *fmt, ...)
855{
856 struct audit_buffer *ab;
857 va_list args;
858
859 ab = audit_log_start(ctx);
860 if (ab) {
861 va_start(args, fmt);
862 audit_log_vformat(ab, fmt, args);
863 va_end(args);
864 audit_log_end(ab);
865 }
866}