blob: dae3570b3a3b0660881b00701cb25f3af9a93afa [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
Steve Grubb23f32d12005-05-13 18:35:15 +0100119 * that list additions and deletions never happen simultaneously in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 * 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
Steve Grubbc0404992005-05-13 18:17:42 +0100143static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149struct audit_entry {
150 struct list_head list;
151 struct audit_rule rule;
152};
153
154static void audit_log_end_irq(struct audit_buffer *ab);
155static void audit_log_end_fast(struct audit_buffer *ab);
156
157static 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
172static 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. */
203void 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
Serge Hallync94c2572005-04-29 16:27:17 +0100238static int audit_set_rate_limit(int limit, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 int old = audit_rate_limit;
241 audit_rate_limit = limit;
Steve Grubbc0404992005-05-13 18:17:42 +0100242 audit_log(NULL, AUDIT_CONFIG_CHANGE,
243 "audit_rate_limit=%d old=%d by auid %u",
Serge Hallync94c2572005-04-29 16:27:17 +0100244 audit_rate_limit, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return old;
246}
247
Serge Hallync94c2572005-04-29 16:27:17 +0100248static int audit_set_backlog_limit(int limit, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 int old = audit_backlog_limit;
251 audit_backlog_limit = limit;
Steve Grubbc0404992005-05-13 18:17:42 +0100252 audit_log(NULL, AUDIT_CONFIG_CHANGE,
253 "audit_backlog_limit=%d old=%d by auid %u",
Serge Hallync94c2572005-04-29 16:27:17 +0100254 audit_backlog_limit, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return old;
256}
257
Serge Hallync94c2572005-04-29 16:27:17 +0100258static int audit_set_enabled(int state, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 int old = audit_enabled;
261 if (state != 0 && state != 1)
262 return -EINVAL;
263 audit_enabled = state;
Steve Grubbc0404992005-05-13 18:17:42 +0100264 audit_log(NULL, AUDIT_CONFIG_CHANGE,
265 "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;
Steve Grubbc0404992005-05-13 18:17:42 +0100278 audit_log(NULL, AUDIT_CONFIG_CHANGE,
279 "audit_failure=%d old=%d by auid %u",
280 audit_failure, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 return old;
282}
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284void 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:
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100324 case AUDIT_SIGNAL_INFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL))
326 err = -EPERM;
327 break;
David Woodhouse209aba02005-05-18 10:21:07 +0100328 case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 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
339static 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;
Steve Grubbc0404992005-05-13 18:17:42 +0100345 struct audit_buffer *ab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 u16 msg_type = nlh->nlmsg_type;
Serge Hallync94c2572005-04-29 16:27:17 +0100347 uid_t loginuid; /* loginuid of sender */
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100348 struct audit_sig_info sig_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
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;
Serge Hallync94c2572005-04-29 16:27:17 +0100356 loginuid = NETLINK_CB(skb).loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 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) {
Serge Hallync94c2572005-04-29 16:27:17 +0100377 err = audit_set_enabled(status_get->enabled, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (err < 0) return err;
379 }
380 if (status_get->mask & AUDIT_STATUS_FAILURE) {
Serge Hallync94c2572005-04-29 16:27:17 +0100381 err = audit_set_failure(status_get->failure, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 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;
Steve Grubbc0404992005-05-13 18:17:42 +0100387 audit_log(NULL, AUDIT_CONFIG_CHANGE,
388 "audit_pid=%d old=%d by auid %u",
Serge Hallync94c2572005-04-29 16:27:17 +0100389 audit_pid, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391 if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
Serge Hallync94c2572005-04-29 16:27:17 +0100392 audit_set_rate_limit(status_get->rate_limit, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
Serge Hallync94c2572005-04-29 16:27:17 +0100394 audit_set_backlog_limit(status_get->backlog_limit,
395 loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 break;
David Woodhouse209aba02005-05-18 10:21:07 +0100397 case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
Steve Grubbc0404992005-05-13 18:17:42 +0100398 ab = audit_log_start(NULL, msg_type);
399 if (!ab)
400 break; /* audit_panic has been called */
401 audit_log_format(ab,
Serge Hallync94c2572005-04-29 16:27:17 +0100402 "user pid=%d uid=%d length=%d loginuid=%u"
403 " msg='%.1024s'",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 pid, uid,
405 (int)(nlh->nlmsg_len
406 - ((char *)data - (char *)nlh)),
Serge Hallync94c2572005-04-29 16:27:17 +0100407 loginuid, (char *)data);
Steve Grubbc0404992005-05-13 18:17:42 +0100408 audit_set_pid(ab, pid);
409 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 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:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
Serge Hallync94c2572005-04-29 16:27:17 +0100418 uid, seq, data, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 break;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100420 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 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. */
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700437static void audit_receive_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
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)
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700446 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
458/* Receive messages from netlink socket. */
459static void audit_receive(struct sock *sk, int length)
460{
461 struct sk_buff *skb;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700462 unsigned int qlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700464 down(&audit_netlink_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700466 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471 up(&audit_netlink_sem);
472}
473
Chris Wright5ac52f32005-05-06 15:54:53 +0100474/* Grab skbuff from the audit_buffer and send to user space. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475static inline int audit_log_drain(struct audit_buffer *ab)
476{
Chris Wright8fc61152005-05-06 15:54:17 +0100477 struct sk_buff *skb = ab->skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Chris Wright8fc61152005-05-06 15:54:17 +0100479 if (skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 int retval = 0;
481
482 if (audit_pid) {
Chris Wright5ac52f32005-05-06 15:54:53 +0100483 struct nlmsghdr *nlh = (struct nlmsghdr *)skb->data;
Chris Wright5a241d72005-05-11 10:43:07 +0100484 nlh->nlmsg_len = skb->len - NLMSG_SPACE(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 skb_get(skb); /* because netlink_* frees */
486 retval = netlink_unicast(audit_sock, skb, audit_pid,
487 MSG_DONTWAIT);
488 }
Chris Wright37509e72005-04-29 17:19:14 +0100489 if (retval == -EAGAIN &&
490 (atomic_read(&audit_backlog)) < audit_backlog_limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 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 */
Chris Wright8fc61152005-05-06 15:54:17 +0100504 int offset = NLMSG_SPACE(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 int len = skb->len - offset;
Peter Martuccellic7fcb0e2005-04-29 16:10:24 +0100506 skb->data[offset + len] = '\0';
507 printk(KERN_ERR "%s\n", skb->data + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510 return 0;
511}
512
513/* Initialize audit support at boot time. */
514static 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;
Steve Grubbc0404992005-05-13 18:17:42 +0100524 audit_log(NULL, AUDIT_KERNEL, "initialized");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return 0;
526}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527__initcall(audit_init);
528
529/* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
530static 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
Chris Wright16e19042005-05-06 15:53:34 +0100543static void audit_buffer_free(struct audit_buffer *ab)
544{
545 unsigned long flags;
546
Chris Wright8fc61152005-05-06 15:54:17 +0100547 if (!ab)
548 return;
549
Chris Wright5ac52f32005-05-06 15:54:53 +0100550 if (ab->skb)
551 kfree_skb(ab->skb);
Chris Wright16e19042005-05-06 15:53:34 +0100552 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
Steve Grubbc0404992005-05-13 18:17:42 +0100561static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
562 int gfp_mask, int type)
Chris Wright16e19042005-05-06 15:53:34 +0100563{
564 unsigned long flags;
565 struct audit_buffer *ab = NULL;
Steve Grubbc0404992005-05-13 18:17:42 +0100566 struct nlmsghdr *nlh;
Chris Wright16e19042005-05-06 15:53:34 +0100567
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) {
David Woodhouse4332bdd2005-05-06 15:59:57 +0100578 ab = kmalloc(sizeof(*ab), gfp_mask);
Chris Wright16e19042005-05-06 15:53:34 +0100579 if (!ab)
Chris Wright8fc61152005-05-06 15:54:17 +0100580 goto err;
Chris Wright16e19042005-05-06 15:53:34 +0100581 }
582 atomic_inc(&audit_backlog);
Chris Wright8fc61152005-05-06 15:54:17 +0100583
David Woodhouse4332bdd2005-05-06 15:59:57 +0100584 ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask);
Chris Wright5ac52f32005-05-06 15:54:53 +0100585 if (!ab->skb)
Chris Wright8fc61152005-05-06 15:54:17 +0100586 goto err;
587
Steve Grubbc0404992005-05-13 18:17:42 +0100588 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;
Chris Wright16e19042005-05-06 15:53:34 +0100594 return ab;
Chris Wright8fc61152005-05-06 15:54:17 +0100595err:
596 audit_buffer_free(ab);
597 return NULL;
Chris Wright16e19042005-05-06 15:53:34 +0100598}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
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. */
Steve Grubbc0404992005-05-13 18:17:42 +0100606struct audit_buffer *audit_log_start(struct audit_context *ctx, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
608 struct audit_buffer *ab = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 struct timespec t;
Steve Grubbd812ddb2005-04-29 16:09:52 +0100610 unsigned int serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
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
Steve Grubbc0404992005-05-13 18:17:42 +0100627 ab = audit_buffer_alloc(ctx, GFP_ATOMIC, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (!ab) {
629 audit_log_lost("out of memory in audit_log_start");
630 return NULL;
631 }
632
Chris Wright197c69c2005-05-11 10:54:05 +0100633 if (!audit_get_stamp(ab->ctx, &t, &serial)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 t = CURRENT_TIME;
Steve Grubbd812ddb2005-04-29 16:09:52 +0100635 serial = 0;
636 }
Chris Wright197c69c2005-05-11 10:54:05 +0100637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 audit_log_format(ab, "audit(%lu.%03lu:%u): ",
639 t.tv_sec, t.tv_nsec/1000000, serial);
640 return ab;
641}
642
Chris Wright8fc61152005-05-06 15:54:17 +0100643/**
Chris Wright5ac52f32005-05-06 15:54:53 +0100644 * audit_expand - expand skb in the audit buffer
Chris Wright8fc61152005-05-06 15:54:17 +0100645 * @ab: audit_buffer
646 *
647 * Returns 0 (no space) on failed expansion, or available space if
648 * successful.
649 */
David Woodhousee3b926b2005-05-10 18:56:08 +0100650static inline int audit_expand(struct audit_buffer *ab, int extra)
Chris Wright8fc61152005-05-06 15:54:17 +0100651{
Chris Wright5ac52f32005-05-06 15:54:53 +0100652 struct sk_buff *skb = ab->skb;
David Woodhousee3b926b2005-05-10 18:56:08 +0100653 int ret = pskb_expand_head(skb, skb_headroom(skb), extra,
Chris Wright5ac52f32005-05-06 15:54:53 +0100654 GFP_ATOMIC);
655 if (ret < 0) {
656 audit_log_lost("out of memory in audit_expand");
Chris Wright8fc61152005-05-06 15:54:17 +0100657 return 0;
Chris Wright5ac52f32005-05-06 15:54:53 +0100658 }
659 return skb_tailroom(skb);
Chris Wright8fc61152005-05-06 15:54:17 +0100660}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
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. */
666static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
667 va_list args)
668{
669 int len, avail;
Chris Wright5ac52f32005-05-06 15:54:53 +0100670 struct sk_buff *skb;
David Woodhouseeecb0a72005-05-10 18:58:51 +0100671 va_list args2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
673 if (!ab)
674 return;
675
Chris Wright5ac52f32005-05-06 15:54:53 +0100676 BUG_ON(!ab->skb);
677 skb = ab->skb;
678 avail = skb_tailroom(skb);
679 if (avail == 0) {
David Woodhousee3b926b2005-05-10 18:56:08 +0100680 avail = audit_expand(ab, AUDIT_BUFSIZ);
Chris Wright8fc61152005-05-06 15:54:17 +0100681 if (!avail)
682 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
David Woodhouseeecb0a72005-05-10 18:58:51 +0100684 va_copy(args2, args);
Chris Wright5ac52f32005-05-06 15:54:53 +0100685 len = vsnprintf(skb->tail, avail, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 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. */
David Woodhouse5e014b12005-05-13 18:50:33 +0100690 avail = audit_expand(ab, max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
Chris Wright8fc61152005-05-06 15:54:17 +0100691 if (!avail)
692 goto out;
David Woodhouseeecb0a72005-05-10 18:58:51 +0100693 len = vsnprintf(skb->tail, avail, fmt, args2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Steve Grubb168b7172005-05-19 10:24:22 +0100695 if (len > 0)
696 skb_put(skb, len);
Chris Wright8fc61152005-05-06 15:54:17 +0100697out:
698 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
701/* Format a message into the audit buffer. All the work is done in
702 * audit_log_vformat. */
703void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
704{
705 va_list args;
706
707 if (!ab)
708 return;
709 va_start(args, fmt);
710 audit_log_vformat(ab, fmt, args);
711 va_end(args);
712}
713
Steve Grubb168b7172005-05-19 10:24:22 +0100714/* This function will take the passed buf and convert it into a string of
715 * ascii hex digits. The new string is placed onto the skb. */
716void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf,
717 size_t len)
83c7d092005-04-29 15:54:44 +0100718{
Steve Grubb168b7172005-05-19 10:24:22 +0100719 int i, avail, new_len;
720 unsigned char *ptr;
721 struct sk_buff *skb;
722 static const unsigned char *hex = "0123456789ABCDEF";
83c7d092005-04-29 15:54:44 +0100723
Steve Grubb168b7172005-05-19 10:24:22 +0100724 BUG_ON(!ab->skb);
725 skb = ab->skb;
726 avail = skb_tailroom(skb);
727 new_len = len<<1;
728 if (new_len >= avail) {
729 /* Round the buffer request up to the next multiple */
730 new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
731 avail = audit_expand(ab, new_len);
732 if (!avail)
733 return;
734 }
735
736 ptr = skb->tail;
737 for (i=0; i<len; i++) {
738 *ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */
739 *ptr++ = hex[buf[i] & 0x0F]; /* Lower nibble */
740 }
741 *ptr = 0;
742 skb_put(skb, len << 1); /* new string is twice the old string */
83c7d092005-04-29 15:54:44 +0100743}
744
Steve Grubb168b7172005-05-19 10:24:22 +0100745/* This code will escape a string that is passed to it if the string
746 * contains a control character, unprintable character, double quote mark,
747 * or a space. Unescaped strings will start and end with a double quote mark.
748 * Strings that are escaped are printed in hex (2 digits per char). */
83c7d092005-04-29 15:54:44 +0100749void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
750{
Andrew Morton81b78542005-04-29 15:59:11 +0100751 const unsigned char *p = string;
83c7d092005-04-29 15:54:44 +0100752
753 while (*p) {
Steve Grubb168b7172005-05-19 10:24:22 +0100754 if (*p == '"' || *p < 0x21 || *p > 0x7f) {
83c7d092005-04-29 15:54:44 +0100755 audit_log_hex(ab, string, strlen(string));
756 return;
757 }
758 p++;
759 }
760 audit_log_format(ab, "\"%s\"", string);
761}
762
Steve Grubb168b7172005-05-19 10:24:22 +0100763/* This is a helper-function to print the escaped d_path */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
765 struct dentry *dentry, struct vfsmount *vfsmnt)
766{
Steve Grubb168b7172005-05-19 10:24:22 +0100767 char *p, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Chris Wright8fc61152005-05-06 15:54:17 +0100769 if (prefix)
770 audit_log_format(ab, " %s", prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
Steve Grubb168b7172005-05-19 10:24:22 +0100772 /* We will allow 11 spaces for ' (deleted)' to be appended */
773 path = kmalloc(PATH_MAX+11, GFP_KERNEL);
774 if (!path) {
775 audit_log_format(ab, "<no memory>");
776 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
Steve Grubb168b7172005-05-19 10:24:22 +0100778 p = d_path(dentry, vfsmnt, path, PATH_MAX+11);
779 if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
780 /* FIXME: can we save some information here? */
781 audit_log_format(ab, "<too long>");
782 } else
783 audit_log_untrustedstring(ab, p);
784 kfree(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
Steve Grubb23f32d12005-05-13 18:35:15 +0100787/* Remove queued messages from the audit_txlist and send them to user space. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788static void audit_tasklet_handler(unsigned long arg)
789{
790 LIST_HEAD(list);
791 struct audit_buffer *ab;
792 unsigned long flags;
793
794 spin_lock_irqsave(&audit_txlist_lock, flags);
795 list_splice_init(&audit_txlist, &list);
796 spin_unlock_irqrestore(&audit_txlist_lock, flags);
797
798 while (!list_empty(&list)) {
799 ab = list_entry(list.next, struct audit_buffer, list);
800 list_del(&ab->list);
801 audit_log_end_fast(ab);
802 }
803}
804
805static DECLARE_TASKLET(audit_tasklet, audit_tasklet_handler, 0);
806
807/* The netlink_* functions cannot be called inside an irq context, so
808 * the audit buffer is places on a queue and a tasklet is scheduled to
809 * remove them from the queue outside the irq context. May be called in
810 * any context. */
811static void audit_log_end_irq(struct audit_buffer *ab)
812{
813 unsigned long flags;
814
815 if (!ab)
816 return;
817 spin_lock_irqsave(&audit_txlist_lock, flags);
818 list_add_tail(&ab->list, &audit_txlist);
819 spin_unlock_irqrestore(&audit_txlist_lock, flags);
820
821 tasklet_schedule(&audit_tasklet);
822}
823
824/* Send the message in the audit buffer directly to user space. May not
825 * be called in an irq context. */
826static void audit_log_end_fast(struct audit_buffer *ab)
827{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 BUG_ON(in_irq());
829 if (!ab)
830 return;
831 if (!audit_rate_check()) {
832 audit_log_lost("rate limit exceeded");
833 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (audit_log_drain(ab))
835 return;
836 }
Chris Wright16e19042005-05-06 15:53:34 +0100837 audit_buffer_free(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838}
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. */
Steve Grubbc0404992005-05-13 18:17:42 +0100854void audit_log(struct audit_context *ctx, int type, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 struct audit_buffer *ab;
857 va_list args;
858
Steve Grubbc0404992005-05-13 18:17:42 +0100859 ab = audit_log_start(ctx, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 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}