blob: c18b769e23a27a58407e6767c092ce2526870457 [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. */
Steve Grubbc2f0c7c2005-05-06 12:38:39 +010071int audit_pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
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
Steve Grubbc2f0c7c2005-05-06 12:38:39 +010082/* The identity of the user shutting down the audit system. */
83uid_t audit_sig_uid = -1;
84pid_t audit_sig_pid = -1;
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086/* 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*/
93static atomic_t audit_lost = ATOMIC_INIT(0);
94
95/* The netlink socket. */
96static 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). */
105static DEFINE_SPINLOCK(audit_txlist_lock);
106static DEFINE_SPINLOCK(audit_freelist_lock);
107static int audit_freelist_count = 0;
108static LIST_HEAD(audit_txlist);
109static 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. */
114static LIST_HEAD(audit_tsklist);
115static LIST_HEAD(audit_entlist);
116static 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 simultaneiously in
120 * auditsc.c */
121static 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. */
137struct audit_buffer {
138 struct list_head list;
Chris Wright8fc61152005-05-06 15:54:17 +0100139 struct sk_buff *skb; /* formatted skb ready to send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 struct audit_context *ctx; /* NULL or associated context */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141};
142
143void audit_set_type(struct audit_buffer *ab, int type)
144{
Chris Wright5ac52f32005-05-06 15:54:53 +0100145 struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
146 nlh->nlmsg_type = type;
147}
148
149static void audit_set_pid(struct audit_buffer *ab, pid_t pid)
150{
151 struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
152 nlh->nlmsg_pid = pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155struct audit_entry {
156 struct list_head list;
157 struct audit_rule rule;
158};
159
160static void audit_log_end_irq(struct audit_buffer *ab);
161static void audit_log_end_fast(struct audit_buffer *ab);
162
163static void audit_panic(const char *message)
164{
165 switch (audit_failure)
166 {
167 case AUDIT_FAIL_SILENT:
168 break;
169 case AUDIT_FAIL_PRINTK:
170 printk(KERN_ERR "audit: %s\n", message);
171 break;
172 case AUDIT_FAIL_PANIC:
173 panic("audit: %s\n", message);
174 break;
175 }
176}
177
178static inline int audit_rate_check(void)
179{
180 static unsigned long last_check = 0;
181 static int messages = 0;
182 static DEFINE_SPINLOCK(lock);
183 unsigned long flags;
184 unsigned long now;
185 unsigned long elapsed;
186 int retval = 0;
187
188 if (!audit_rate_limit) return 1;
189
190 spin_lock_irqsave(&lock, flags);
191 if (++messages < audit_rate_limit) {
192 retval = 1;
193 } else {
194 now = jiffies;
195 elapsed = now - last_check;
196 if (elapsed > HZ) {
197 last_check = now;
198 messages = 0;
199 retval = 1;
200 }
201 }
202 spin_unlock_irqrestore(&lock, flags);
203
204 return retval;
205}
206
207/* Emit at least 1 message per second, even if audit_rate_check is
208 * throttling. */
209void audit_log_lost(const char *message)
210{
211 static unsigned long last_msg = 0;
212 static DEFINE_SPINLOCK(lock);
213 unsigned long flags;
214 unsigned long now;
215 int print;
216
217 atomic_inc(&audit_lost);
218
219 print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);
220
221 if (!print) {
222 spin_lock_irqsave(&lock, flags);
223 now = jiffies;
224 if (now - last_msg > HZ) {
225 print = 1;
226 last_msg = now;
227 }
228 spin_unlock_irqrestore(&lock, flags);
229 }
230
231 if (print) {
232 printk(KERN_WARNING
233 "audit: audit_lost=%d audit_backlog=%d"
234 " audit_rate_limit=%d audit_backlog_limit=%d\n",
235 atomic_read(&audit_lost),
236 atomic_read(&audit_backlog),
237 audit_rate_limit,
238 audit_backlog_limit);
239 audit_panic(message);
240 }
241
242}
243
Serge Hallync94c2572005-04-29 16:27:17 +0100244static int audit_set_rate_limit(int limit, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 int old = audit_rate_limit;
247 audit_rate_limit = limit;
Serge Hallync94c2572005-04-29 16:27:17 +0100248 audit_log(NULL, "audit_rate_limit=%d old=%d by auid %u",
249 audit_rate_limit, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return old;
251}
252
Serge Hallync94c2572005-04-29 16:27:17 +0100253static int audit_set_backlog_limit(int limit, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 int old = audit_backlog_limit;
256 audit_backlog_limit = limit;
Serge Hallync94c2572005-04-29 16:27:17 +0100257 audit_log(NULL, "audit_backlog_limit=%d old=%d by auid %u",
258 audit_backlog_limit, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return old;
260}
261
Serge Hallync94c2572005-04-29 16:27:17 +0100262static int audit_set_enabled(int state, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 int old = audit_enabled;
265 if (state != 0 && state != 1)
266 return -EINVAL;
267 audit_enabled = state;
Serge Hallync94c2572005-04-29 16:27:17 +0100268 audit_log(NULL, "audit_enabled=%d old=%d by auid %u",
269 audit_enabled, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return old;
271}
272
Serge Hallync94c2572005-04-29 16:27:17 +0100273static int audit_set_failure(int state, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274{
275 int old = audit_failure;
276 if (state != AUDIT_FAIL_SILENT
277 && state != AUDIT_FAIL_PRINTK
278 && state != AUDIT_FAIL_PANIC)
279 return -EINVAL;
280 audit_failure = state;
Serge Hallync94c2572005-04-29 16:27:17 +0100281 audit_log(NULL, "audit_failure=%d old=%d by auid %u",
282 audit_failure, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return old;
284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286void audit_send_reply(int pid, int seq, int type, int done, int multi,
287 void *payload, int size)
288{
289 struct sk_buff *skb;
290 struct nlmsghdr *nlh;
291 int len = NLMSG_SPACE(size);
292 void *data;
293 int flags = multi ? NLM_F_MULTI : 0;
294 int t = done ? NLMSG_DONE : type;
295
296 skb = alloc_skb(len, GFP_KERNEL);
297 if (!skb)
298 goto nlmsg_failure;
299
300 nlh = NLMSG_PUT(skb, pid, seq, t, len - sizeof(*nlh));
301 nlh->nlmsg_flags = flags;
302 data = NLMSG_DATA(nlh);
303 memcpy(data, payload, size);
304 netlink_unicast(audit_sock, skb, pid, MSG_DONTWAIT);
305 return;
306
307nlmsg_failure: /* Used by NLMSG_PUT */
308 if (skb)
309 kfree_skb(skb);
310}
311
312/*
313 * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
314 * control messages.
315 */
316static int audit_netlink_ok(kernel_cap_t eff_cap, u16 msg_type)
317{
318 int err = 0;
319
320 switch (msg_type) {
321 case AUDIT_GET:
322 case AUDIT_LIST:
323 case AUDIT_SET:
324 case AUDIT_ADD:
325 case AUDIT_DEL:
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100326 case AUDIT_SIGNAL_INFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL))
328 err = -EPERM;
329 break;
330 case AUDIT_USER:
331 if (!cap_raised(eff_cap, CAP_AUDIT_WRITE))
332 err = -EPERM;
333 break;
334 default: /* bad msg */
335 err = -EINVAL;
336 }
337
338 return err;
339}
340
341static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
342{
343 u32 uid, pid, seq;
344 void *data;
345 struct audit_status *status_get, status_set;
346 int err;
347 struct audit_buffer *ab;
348 u16 msg_type = nlh->nlmsg_type;
Serge Hallync94c2572005-04-29 16:27:17 +0100349 uid_t loginuid; /* loginuid of sender */
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100350 struct audit_sig_info sig_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type);
353 if (err)
354 return err;
355
356 pid = NETLINK_CREDS(skb)->pid;
357 uid = NETLINK_CREDS(skb)->uid;
Serge Hallync94c2572005-04-29 16:27:17 +0100358 loginuid = NETLINK_CB(skb).loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 seq = nlh->nlmsg_seq;
360 data = NLMSG_DATA(nlh);
361
362 switch (msg_type) {
363 case AUDIT_GET:
364 status_set.enabled = audit_enabled;
365 status_set.failure = audit_failure;
366 status_set.pid = audit_pid;
367 status_set.rate_limit = audit_rate_limit;
368 status_set.backlog_limit = audit_backlog_limit;
369 status_set.lost = atomic_read(&audit_lost);
370 status_set.backlog = atomic_read(&audit_backlog);
371 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
372 &status_set, sizeof(status_set));
373 break;
374 case AUDIT_SET:
375 if (nlh->nlmsg_len < sizeof(struct audit_status))
376 return -EINVAL;
377 status_get = (struct audit_status *)data;
378 if (status_get->mask & AUDIT_STATUS_ENABLED) {
Serge Hallync94c2572005-04-29 16:27:17 +0100379 err = audit_set_enabled(status_get->enabled, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (err < 0) return err;
381 }
382 if (status_get->mask & AUDIT_STATUS_FAILURE) {
Serge Hallync94c2572005-04-29 16:27:17 +0100383 err = audit_set_failure(status_get->failure, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (err < 0) return err;
385 }
386 if (status_get->mask & AUDIT_STATUS_PID) {
387 int old = audit_pid;
388 audit_pid = status_get->pid;
Serge Hallync94c2572005-04-29 16:27:17 +0100389 audit_log(NULL, "audit_pid=%d old=%d by auid %u",
390 audit_pid, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392 if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
Serge Hallync94c2572005-04-29 16:27:17 +0100393 audit_set_rate_limit(status_get->rate_limit, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
Serge Hallync94c2572005-04-29 16:27:17 +0100395 audit_set_backlog_limit(status_get->backlog_limit,
396 loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 break;
398 case AUDIT_USER:
399 ab = audit_log_start(NULL);
400 if (!ab)
401 break; /* audit_panic has been called */
402 audit_log_format(ab,
Serge Hallync94c2572005-04-29 16:27:17 +0100403 "user pid=%d uid=%d length=%d loginuid=%u"
404 " msg='%.1024s'",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 pid, uid,
406 (int)(nlh->nlmsg_len
407 - ((char *)data - (char *)nlh)),
Serge Hallync94c2572005-04-29 16:27:17 +0100408 loginuid, (char *)data);
Chris Wright5ac52f32005-05-06 15:54:53 +0100409 audit_set_type(ab, AUDIT_USER);
410 audit_set_pid(ab, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 audit_log_end(ab);
412 break;
413 case AUDIT_ADD:
414 case AUDIT_DEL:
415 if (nlh->nlmsg_len < sizeof(struct audit_rule))
416 return -EINVAL;
417 /* fallthrough */
418 case AUDIT_LIST:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
Serge Hallync94c2572005-04-29 16:27:17 +0100420 uid, seq, data, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 break;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100422 case AUDIT_SIGNAL_INFO:
423 sig_data.uid = audit_sig_uid;
424 sig_data.pid = audit_sig_pid;
425 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO,
426 0, 0, &sig_data, sizeof(sig_data));
427 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 default:
429 err = -EINVAL;
430 break;
431 }
432
433 return err < 0 ? err : 0;
434}
435
436/* Get message from skb (based on rtnetlink_rcv_skb). Each message is
437 * processed by audit_receive_msg. Malformed skbs with wrong length are
438 * discarded silently. */
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700439static void audit_receive_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 int err;
442 struct nlmsghdr *nlh;
443 u32 rlen;
444
445 while (skb->len >= NLMSG_SPACE(0)) {
446 nlh = (struct nlmsghdr *)skb->data;
447 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700448 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
450 if (rlen > skb->len)
451 rlen = skb->len;
452 if ((err = audit_receive_msg(skb, nlh))) {
453 netlink_ack(skb, nlh, err);
454 } else if (nlh->nlmsg_flags & NLM_F_ACK)
455 netlink_ack(skb, nlh, 0);
456 skb_pull(skb, rlen);
457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
460/* Receive messages from netlink socket. */
461static void audit_receive(struct sock *sk, int length)
462{
463 struct sk_buff *skb;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700464 unsigned int qlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700466 down(&audit_netlink_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700468 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
469 skb = skb_dequeue(&sk->sk_receive_queue);
470 audit_receive_skb(skb);
471 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473 up(&audit_netlink_sem);
474}
475
Chris Wright5ac52f32005-05-06 15:54:53 +0100476/* Grab skbuff from the audit_buffer and send to user space. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477static inline int audit_log_drain(struct audit_buffer *ab)
478{
Chris Wright8fc61152005-05-06 15:54:17 +0100479 struct sk_buff *skb = ab->skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Chris Wright8fc61152005-05-06 15:54:17 +0100481 if (skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 int retval = 0;
483
484 if (audit_pid) {
Chris Wright5ac52f32005-05-06 15:54:53 +0100485 struct nlmsghdr *nlh = (struct nlmsghdr *)skb->data;
Chris Wright5a241d72005-05-11 10:43:07 +0100486 nlh->nlmsg_len = skb->len - NLMSG_SPACE(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 skb_get(skb); /* because netlink_* frees */
488 retval = netlink_unicast(audit_sock, skb, audit_pid,
489 MSG_DONTWAIT);
490 }
Chris Wright37509e72005-04-29 17:19:14 +0100491 if (retval == -EAGAIN &&
492 (atomic_read(&audit_backlog)) < audit_backlog_limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 audit_log_end_irq(ab);
494 return 1;
495 }
496 if (retval < 0) {
497 if (retval == -ECONNREFUSED) {
498 printk(KERN_ERR
499 "audit: *NO* daemon at audit_pid=%d\n",
500 audit_pid);
501 audit_pid = 0;
502 } else
503 audit_log_lost("netlink socket too busy");
504 }
505 if (!audit_pid) { /* No daemon */
Chris Wright8fc61152005-05-06 15:54:17 +0100506 int offset = NLMSG_SPACE(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 int len = skb->len - offset;
Peter Martuccellic7fcb0e2005-04-29 16:10:24 +0100508 skb->data[offset + len] = '\0';
509 printk(KERN_ERR "%s\n", skb->data + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 }
512 return 0;
513}
514
515/* Initialize audit support at boot time. */
516static int __init audit_init(void)
517{
518 printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
519 audit_default ? "enabled" : "disabled");
520 audit_sock = netlink_kernel_create(NETLINK_AUDIT, audit_receive);
521 if (!audit_sock)
522 audit_panic("cannot initialize netlink socket");
523
524 audit_initialized = 1;
525 audit_enabled = audit_default;
526 audit_log(NULL, "initialized");
527 return 0;
528}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529__initcall(audit_init);
530
531/* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
532static int __init audit_enable(char *str)
533{
534 audit_default = !!simple_strtol(str, NULL, 0);
535 printk(KERN_INFO "audit: %s%s\n",
536 audit_default ? "enabled" : "disabled",
537 audit_initialized ? "" : " (after initialization)");
538 if (audit_initialized)
539 audit_enabled = audit_default;
540 return 0;
541}
542
543__setup("audit=", audit_enable);
544
Chris Wright16e19042005-05-06 15:53:34 +0100545static void audit_buffer_free(struct audit_buffer *ab)
546{
547 unsigned long flags;
548
Chris Wright8fc61152005-05-06 15:54:17 +0100549 if (!ab)
550 return;
551
Chris Wright5ac52f32005-05-06 15:54:53 +0100552 if (ab->skb)
553 kfree_skb(ab->skb);
Chris Wright16e19042005-05-06 15:53:34 +0100554 atomic_dec(&audit_backlog);
555 spin_lock_irqsave(&audit_freelist_lock, flags);
556 if (++audit_freelist_count > AUDIT_MAXFREE)
557 kfree(ab);
558 else
559 list_add(&ab->list, &audit_freelist);
560 spin_unlock_irqrestore(&audit_freelist_lock, flags);
561}
562
Chris Wright8fc61152005-05-06 15:54:17 +0100563static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
564 int gfp_mask)
Chris Wright16e19042005-05-06 15:53:34 +0100565{
566 unsigned long flags;
567 struct audit_buffer *ab = NULL;
Chris Wright5ac52f32005-05-06 15:54:53 +0100568 struct nlmsghdr *nlh;
Chris Wright16e19042005-05-06 15:53:34 +0100569
570 spin_lock_irqsave(&audit_freelist_lock, flags);
571 if (!list_empty(&audit_freelist)) {
572 ab = list_entry(audit_freelist.next,
573 struct audit_buffer, list);
574 list_del(&ab->list);
575 --audit_freelist_count;
576 }
577 spin_unlock_irqrestore(&audit_freelist_lock, flags);
578
579 if (!ab) {
David Woodhouse4332bdd2005-05-06 15:59:57 +0100580 ab = kmalloc(sizeof(*ab), gfp_mask);
Chris Wright16e19042005-05-06 15:53:34 +0100581 if (!ab)
Chris Wright8fc61152005-05-06 15:54:17 +0100582 goto err;
Chris Wright16e19042005-05-06 15:53:34 +0100583 }
584 atomic_inc(&audit_backlog);
Chris Wright8fc61152005-05-06 15:54:17 +0100585
David Woodhouse4332bdd2005-05-06 15:59:57 +0100586 ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask);
Chris Wright5ac52f32005-05-06 15:54:53 +0100587 if (!ab->skb)
Chris Wright8fc61152005-05-06 15:54:17 +0100588 goto err;
589
Chris Wright8fc61152005-05-06 15:54:17 +0100590 ab->ctx = ctx;
Chris Wright5ac52f32005-05-06 15:54:53 +0100591 nlh = (struct nlmsghdr *)skb_put(ab->skb, NLMSG_SPACE(0));
592 nlh->nlmsg_type = AUDIT_KERNEL;
593 nlh->nlmsg_flags = 0;
594 nlh->nlmsg_pid = 0;
595 nlh->nlmsg_seq = 0;
Chris Wright16e19042005-05-06 15:53:34 +0100596 return ab;
Chris Wright8fc61152005-05-06 15:54:17 +0100597err:
598 audit_buffer_free(ab);
599 return NULL;
Chris Wright16e19042005-05-06 15:53:34 +0100600}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602/* Obtain an audit buffer. This routine does locking to obtain the
603 * audit buffer, but then no locking is required for calls to
604 * audit_log_*format. If the tsk is a task that is currently in a
605 * syscall, then the syscall is marked as auditable and an audit record
606 * will be written at syscall exit. If there is no associated task, tsk
607 * should be NULL. */
608struct audit_buffer *audit_log_start(struct audit_context *ctx)
609{
610 struct audit_buffer *ab = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 struct timespec t;
Steve Grubbd812ddb2005-04-29 16:09:52 +0100612 unsigned int serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 if (!audit_initialized)
615 return NULL;
616
617 if (audit_backlog_limit
618 && atomic_read(&audit_backlog) > audit_backlog_limit) {
619 if (audit_rate_check())
620 printk(KERN_WARNING
621 "audit: audit_backlog=%d > "
622 "audit_backlog_limit=%d\n",
623 atomic_read(&audit_backlog),
624 audit_backlog_limit);
625 audit_log_lost("backlog limit exceeded");
626 return NULL;
627 }
628
Chris Wright8fc61152005-05-06 15:54:17 +0100629 ab = audit_buffer_alloc(ctx, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 if (!ab) {
631 audit_log_lost("out of memory in audit_log_start");
632 return NULL;
633 }
634
Chris Wright197c69c2005-05-11 10:54:05 +0100635 if (!audit_get_stamp(ab->ctx, &t, &serial)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 t = CURRENT_TIME;
Steve Grubbd812ddb2005-04-29 16:09:52 +0100637 serial = 0;
638 }
Chris Wright197c69c2005-05-11 10:54:05 +0100639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 audit_log_format(ab, "audit(%lu.%03lu:%u): ",
641 t.tv_sec, t.tv_nsec/1000000, serial);
642 return ab;
643}
644
Chris Wright8fc61152005-05-06 15:54:17 +0100645/**
Chris Wright5ac52f32005-05-06 15:54:53 +0100646 * audit_expand - expand skb in the audit buffer
Chris Wright8fc61152005-05-06 15:54:17 +0100647 * @ab: audit_buffer
648 *
649 * Returns 0 (no space) on failed expansion, or available space if
650 * successful.
651 */
David Woodhousee3b926b2005-05-10 18:56:08 +0100652static inline int audit_expand(struct audit_buffer *ab, int extra)
Chris Wright8fc61152005-05-06 15:54:17 +0100653{
Chris Wright5ac52f32005-05-06 15:54:53 +0100654 struct sk_buff *skb = ab->skb;
David Woodhousee3b926b2005-05-10 18:56:08 +0100655 int ret = pskb_expand_head(skb, skb_headroom(skb), extra,
Chris Wright5ac52f32005-05-06 15:54:53 +0100656 GFP_ATOMIC);
657 if (ret < 0) {
658 audit_log_lost("out of memory in audit_expand");
Chris Wright8fc61152005-05-06 15:54:17 +0100659 return 0;
Chris Wright5ac52f32005-05-06 15:54:53 +0100660 }
661 return skb_tailroom(skb);
Chris Wright8fc61152005-05-06 15:54:17 +0100662}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
664/* Format an audit message into the audit buffer. If there isn't enough
665 * room in the audit buffer, more room will be allocated and vsnprint
666 * will be called a second time. Currently, we assume that a printk
667 * can't format message larger than 1024 bytes, so we don't either. */
668static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
669 va_list args)
670{
671 int len, avail;
Chris Wright5ac52f32005-05-06 15:54:53 +0100672 struct sk_buff *skb;
David Woodhouseeecb0a72005-05-10 18:58:51 +0100673 va_list args2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 if (!ab)
676 return;
677
Chris Wright5ac52f32005-05-06 15:54:53 +0100678 BUG_ON(!ab->skb);
679 skb = ab->skb;
680 avail = skb_tailroom(skb);
681 if (avail == 0) {
David Woodhousee3b926b2005-05-10 18:56:08 +0100682 avail = audit_expand(ab, AUDIT_BUFSIZ);
Chris Wright8fc61152005-05-06 15:54:17 +0100683 if (!avail)
684 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
David Woodhouseeecb0a72005-05-10 18:58:51 +0100686 va_copy(args2, args);
Chris Wright5ac52f32005-05-06 15:54:53 +0100687 len = vsnprintf(skb->tail, avail, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (len >= avail) {
689 /* The printk buffer is 1024 bytes long, so if we get
690 * here and AUDIT_BUFSIZ is at least 1024, then we can
691 * log everything that printk could have logged. */
David Woodhousee3b926b2005-05-10 18:56:08 +0100692 avail = audit_expand(ab, 1+len-avail);
Chris Wright8fc61152005-05-06 15:54:17 +0100693 if (!avail)
694 goto out;
David Woodhouseeecb0a72005-05-10 18:58:51 +0100695 len = vsnprintf(skb->tail, avail, fmt, args2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 }
Chris Wright5ac52f32005-05-06 15:54:53 +0100697 skb_put(skb, (len < avail) ? len : avail);
Chris Wright8fc61152005-05-06 15:54:17 +0100698out:
699 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700}
701
702/* Format a message into the audit buffer. All the work is done in
703 * audit_log_vformat. */
704void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
705{
706 va_list args;
707
708 if (!ab)
709 return;
710 va_start(args, fmt);
711 audit_log_vformat(ab, fmt, args);
712 va_end(args);
713}
714
83c7d092005-04-29 15:54:44 +0100715void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf, size_t len)
716{
717 int i;
718
719 for (i=0; i<len; i++)
720 audit_log_format(ab, "%02x", buf[i]);
721}
722
723void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
724{
Andrew Morton81b78542005-04-29 15:59:11 +0100725 const unsigned char *p = string;
83c7d092005-04-29 15:54:44 +0100726
727 while (*p) {
728 if (*p == '"' || *p == ' ' || *p < 0x20 || *p > 0x7f) {
729 audit_log_hex(ab, string, strlen(string));
730 return;
731 }
732 p++;
733 }
734 audit_log_format(ab, "\"%s\"", string);
735}
736
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738/* This is a helper-function to print the d_path without using a static
739 * buffer or allocating another buffer in addition to the one in
740 * audit_buffer. */
741void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
742 struct dentry *dentry, struct vfsmount *vfsmnt)
743{
744 char *p;
Chris Wright5ac52f32005-05-06 15:54:53 +0100745 struct sk_buff *skb = ab->skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 int len, avail;
747
Chris Wright8fc61152005-05-06 15:54:17 +0100748 if (prefix)
749 audit_log_format(ab, " %s", prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Chris Wright5ac52f32005-05-06 15:54:53 +0100751 avail = skb_tailroom(skb);
752 p = d_path(dentry, vfsmnt, skb->tail, avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (IS_ERR(p)) {
754 /* FIXME: can we save some information here? */
755 audit_log_format(ab, "<toolong>");
756 } else {
Chris Wright5ac52f32005-05-06 15:54:53 +0100757 /* path isn't at start of buffer */
758 len = ((char *)skb->tail + avail - 1) - p;
759 memmove(skb->tail, p, len);
760 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 }
762}
763
764/* Remove queued messages from the audit_txlist and send them to userspace. */
765static void audit_tasklet_handler(unsigned long arg)
766{
767 LIST_HEAD(list);
768 struct audit_buffer *ab;
769 unsigned long flags;
770
771 spin_lock_irqsave(&audit_txlist_lock, flags);
772 list_splice_init(&audit_txlist, &list);
773 spin_unlock_irqrestore(&audit_txlist_lock, flags);
774
775 while (!list_empty(&list)) {
776 ab = list_entry(list.next, struct audit_buffer, list);
777 list_del(&ab->list);
778 audit_log_end_fast(ab);
779 }
780}
781
782static DECLARE_TASKLET(audit_tasklet, audit_tasklet_handler, 0);
783
784/* The netlink_* functions cannot be called inside an irq context, so
785 * the audit buffer is places on a queue and a tasklet is scheduled to
786 * remove them from the queue outside the irq context. May be called in
787 * any context. */
788static void audit_log_end_irq(struct audit_buffer *ab)
789{
790 unsigned long flags;
791
792 if (!ab)
793 return;
794 spin_lock_irqsave(&audit_txlist_lock, flags);
795 list_add_tail(&ab->list, &audit_txlist);
796 spin_unlock_irqrestore(&audit_txlist_lock, flags);
797
798 tasklet_schedule(&audit_tasklet);
799}
800
801/* Send the message in the audit buffer directly to user space. May not
802 * be called in an irq context. */
803static void audit_log_end_fast(struct audit_buffer *ab)
804{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 BUG_ON(in_irq());
806 if (!ab)
807 return;
808 if (!audit_rate_check()) {
809 audit_log_lost("rate limit exceeded");
810 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (audit_log_drain(ab))
812 return;
813 }
Chris Wright16e19042005-05-06 15:53:34 +0100814 audit_buffer_free(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
817/* Send or queue the message in the audit buffer, depending on the
818 * current context. (A convenience function that may be called in any
819 * context.) */
820void audit_log_end(struct audit_buffer *ab)
821{
822 if (in_irq())
823 audit_log_end_irq(ab);
824 else
825 audit_log_end_fast(ab);
826}
827
828/* Log an audit record. This is a convenience function that calls
829 * audit_log_start, audit_log_vformat, and audit_log_end. It may be
830 * called in any context. */
831void audit_log(struct audit_context *ctx, const char *fmt, ...)
832{
833 struct audit_buffer *ab;
834 va_list args;
835
836 ab = audit_log_start(ctx);
837 if (ab) {
838 va_start(args, fmt);
839 audit_log_vformat(ab, fmt, args);
840 va_end(args);
841 audit_log_end(ab);
842 }
843}