blob: 187164572bd0be0b19c248e11c0f620b703b08ed [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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143struct audit_entry {
144 struct list_head list;
145 struct audit_rule rule;
146};
147
148static void audit_log_end_irq(struct audit_buffer *ab);
149static void audit_log_end_fast(struct audit_buffer *ab);
150
151static void audit_panic(const char *message)
152{
153 switch (audit_failure)
154 {
155 case AUDIT_FAIL_SILENT:
156 break;
157 case AUDIT_FAIL_PRINTK:
158 printk(KERN_ERR "audit: %s\n", message);
159 break;
160 case AUDIT_FAIL_PANIC:
161 panic("audit: %s\n", message);
162 break;
163 }
164}
165
166static inline int audit_rate_check(void)
167{
168 static unsigned long last_check = 0;
169 static int messages = 0;
170 static DEFINE_SPINLOCK(lock);
171 unsigned long flags;
172 unsigned long now;
173 unsigned long elapsed;
174 int retval = 0;
175
176 if (!audit_rate_limit) return 1;
177
178 spin_lock_irqsave(&lock, flags);
179 if (++messages < audit_rate_limit) {
180 retval = 1;
181 } else {
182 now = jiffies;
183 elapsed = now - last_check;
184 if (elapsed > HZ) {
185 last_check = now;
186 messages = 0;
187 retval = 1;
188 }
189 }
190 spin_unlock_irqrestore(&lock, flags);
191
192 return retval;
193}
194
195/* Emit at least 1 message per second, even if audit_rate_check is
196 * throttling. */
197void audit_log_lost(const char *message)
198{
199 static unsigned long last_msg = 0;
200 static DEFINE_SPINLOCK(lock);
201 unsigned long flags;
202 unsigned long now;
203 int print;
204
205 atomic_inc(&audit_lost);
206
207 print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);
208
209 if (!print) {
210 spin_lock_irqsave(&lock, flags);
211 now = jiffies;
212 if (now - last_msg > HZ) {
213 print = 1;
214 last_msg = now;
215 }
216 spin_unlock_irqrestore(&lock, flags);
217 }
218
219 if (print) {
220 printk(KERN_WARNING
221 "audit: audit_lost=%d audit_backlog=%d"
222 " audit_rate_limit=%d audit_backlog_limit=%d\n",
223 atomic_read(&audit_lost),
224 atomic_read(&audit_backlog),
225 audit_rate_limit,
226 audit_backlog_limit);
227 audit_panic(message);
228 }
229
230}
231
Serge Hallync94c2572005-04-29 16:27:17 +0100232static int audit_set_rate_limit(int limit, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
234 int old = audit_rate_limit;
235 audit_rate_limit = limit;
Serge Hallync94c2572005-04-29 16:27:17 +0100236 audit_log(NULL, "audit_rate_limit=%d old=%d by auid %u",
237 audit_rate_limit, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return old;
239}
240
Serge Hallync94c2572005-04-29 16:27:17 +0100241static int audit_set_backlog_limit(int limit, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
243 int old = audit_backlog_limit;
244 audit_backlog_limit = limit;
Serge Hallync94c2572005-04-29 16:27:17 +0100245 audit_log(NULL, "audit_backlog_limit=%d old=%d by auid %u",
246 audit_backlog_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_enabled(int state, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 int old = audit_enabled;
253 if (state != 0 && state != 1)
254 return -EINVAL;
255 audit_enabled = state;
Serge Hallync94c2572005-04-29 16:27:17 +0100256 audit_log(NULL, "audit_enabled=%d old=%d by auid %u",
257 audit_enabled, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return old;
259}
260
Serge Hallync94c2572005-04-29 16:27:17 +0100261static int audit_set_failure(int state, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 int old = audit_failure;
264 if (state != AUDIT_FAIL_SILENT
265 && state != AUDIT_FAIL_PRINTK
266 && state != AUDIT_FAIL_PANIC)
267 return -EINVAL;
268 audit_failure = state;
Serge Hallync94c2572005-04-29 16:27:17 +0100269 audit_log(NULL, "audit_failure=%d old=%d by auid %u",
270 audit_failure, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 return old;
272}
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274void audit_send_reply(int pid, int seq, int type, int done, int multi,
275 void *payload, int size)
276{
277 struct sk_buff *skb;
278 struct nlmsghdr *nlh;
279 int len = NLMSG_SPACE(size);
280 void *data;
281 int flags = multi ? NLM_F_MULTI : 0;
282 int t = done ? NLMSG_DONE : type;
283
284 skb = alloc_skb(len, GFP_KERNEL);
285 if (!skb)
286 goto nlmsg_failure;
287
288 nlh = NLMSG_PUT(skb, pid, seq, t, len - sizeof(*nlh));
289 nlh->nlmsg_flags = flags;
290 data = NLMSG_DATA(nlh);
291 memcpy(data, payload, size);
292 netlink_unicast(audit_sock, skb, pid, MSG_DONTWAIT);
293 return;
294
295nlmsg_failure: /* Used by NLMSG_PUT */
296 if (skb)
297 kfree_skb(skb);
298}
299
300/*
301 * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
302 * control messages.
303 */
304static int audit_netlink_ok(kernel_cap_t eff_cap, u16 msg_type)
305{
306 int err = 0;
307
308 switch (msg_type) {
309 case AUDIT_GET:
310 case AUDIT_LIST:
311 case AUDIT_SET:
312 case AUDIT_ADD:
313 case AUDIT_DEL:
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100314 case AUDIT_SIGNAL_INFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL))
316 err = -EPERM;
317 break;
318 case AUDIT_USER:
319 if (!cap_raised(eff_cap, CAP_AUDIT_WRITE))
320 err = -EPERM;
321 break;
322 default: /* bad msg */
323 err = -EINVAL;
324 }
325
326 return err;
327}
328
329static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
330{
331 u32 uid, pid, seq;
332 void *data;
333 struct audit_status *status_get, status_set;
334 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 u16 msg_type = nlh->nlmsg_type;
Serge Hallync94c2572005-04-29 16:27:17 +0100336 uid_t loginuid; /* loginuid of sender */
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100337 struct audit_sig_info sig_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
339 err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type);
340 if (err)
341 return err;
342
343 pid = NETLINK_CREDS(skb)->pid;
344 uid = NETLINK_CREDS(skb)->uid;
Serge Hallync94c2572005-04-29 16:27:17 +0100345 loginuid = NETLINK_CB(skb).loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 seq = nlh->nlmsg_seq;
347 data = NLMSG_DATA(nlh);
348
349 switch (msg_type) {
350 case AUDIT_GET:
351 status_set.enabled = audit_enabled;
352 status_set.failure = audit_failure;
353 status_set.pid = audit_pid;
354 status_set.rate_limit = audit_rate_limit;
355 status_set.backlog_limit = audit_backlog_limit;
356 status_set.lost = atomic_read(&audit_lost);
357 status_set.backlog = atomic_read(&audit_backlog);
358 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
359 &status_set, sizeof(status_set));
360 break;
361 case AUDIT_SET:
362 if (nlh->nlmsg_len < sizeof(struct audit_status))
363 return -EINVAL;
364 status_get = (struct audit_status *)data;
365 if (status_get->mask & AUDIT_STATUS_ENABLED) {
Serge Hallync94c2572005-04-29 16:27:17 +0100366 err = audit_set_enabled(status_get->enabled, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 if (err < 0) return err;
368 }
369 if (status_get->mask & AUDIT_STATUS_FAILURE) {
Serge Hallync94c2572005-04-29 16:27:17 +0100370 err = audit_set_failure(status_get->failure, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (err < 0) return err;
372 }
373 if (status_get->mask & AUDIT_STATUS_PID) {
374 int old = audit_pid;
375 audit_pid = status_get->pid;
Serge Hallync94c2572005-04-29 16:27:17 +0100376 audit_log(NULL, "audit_pid=%d old=%d by auid %u",
377 audit_pid, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379 if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
Serge Hallync94c2572005-04-29 16:27:17 +0100380 audit_set_rate_limit(status_get->rate_limit, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
Serge Hallync94c2572005-04-29 16:27:17 +0100382 audit_set_backlog_limit(status_get->backlog_limit,
383 loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 break;
385 case AUDIT_USER:
Chris Wrightc1b773d2005-05-11 10:55:10 +0100386 audit_log_type(NULL, AUDIT_USER, pid,
Serge Hallync94c2572005-04-29 16:27:17 +0100387 "user pid=%d uid=%d length=%d loginuid=%u"
388 " msg='%.1024s'",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 pid, uid,
390 (int)(nlh->nlmsg_len
391 - ((char *)data - (char *)nlh)),
Serge Hallync94c2572005-04-29 16:27:17 +0100392 loginuid, (char *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 break;
394 case AUDIT_ADD:
395 case AUDIT_DEL:
396 if (nlh->nlmsg_len < sizeof(struct audit_rule))
397 return -EINVAL;
398 /* fallthrough */
399 case AUDIT_LIST:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
Serge Hallync94c2572005-04-29 16:27:17 +0100401 uid, seq, data, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 break;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100403 case AUDIT_SIGNAL_INFO:
404 sig_data.uid = audit_sig_uid;
405 sig_data.pid = audit_sig_pid;
406 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO,
407 0, 0, &sig_data, sizeof(sig_data));
408 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 default:
410 err = -EINVAL;
411 break;
412 }
413
414 return err < 0 ? err : 0;
415}
416
417/* Get message from skb (based on rtnetlink_rcv_skb). Each message is
418 * processed by audit_receive_msg. Malformed skbs with wrong length are
419 * discarded silently. */
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700420static void audit_receive_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 int err;
423 struct nlmsghdr *nlh;
424 u32 rlen;
425
426 while (skb->len >= NLMSG_SPACE(0)) {
427 nlh = (struct nlmsghdr *)skb->data;
428 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700429 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
431 if (rlen > skb->len)
432 rlen = skb->len;
433 if ((err = audit_receive_msg(skb, nlh))) {
434 netlink_ack(skb, nlh, err);
435 } else if (nlh->nlmsg_flags & NLM_F_ACK)
436 netlink_ack(skb, nlh, 0);
437 skb_pull(skb, rlen);
438 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
441/* Receive messages from netlink socket. */
442static void audit_receive(struct sock *sk, int length)
443{
444 struct sk_buff *skb;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700445 unsigned int qlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700447 down(&audit_netlink_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700449 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
450 skb = skb_dequeue(&sk->sk_receive_queue);
451 audit_receive_skb(skb);
452 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
454 up(&audit_netlink_sem);
455}
456
Chris Wright5ac52f32005-05-06 15:54:53 +0100457/* Grab skbuff from the audit_buffer and send to user space. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458static inline int audit_log_drain(struct audit_buffer *ab)
459{
Chris Wright8fc61152005-05-06 15:54:17 +0100460 struct sk_buff *skb = ab->skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
Chris Wright8fc61152005-05-06 15:54:17 +0100462 if (skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 int retval = 0;
464
465 if (audit_pid) {
Chris Wright5ac52f32005-05-06 15:54:53 +0100466 struct nlmsghdr *nlh = (struct nlmsghdr *)skb->data;
Chris Wright5a241d72005-05-11 10:43:07 +0100467 nlh->nlmsg_len = skb->len - NLMSG_SPACE(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 skb_get(skb); /* because netlink_* frees */
469 retval = netlink_unicast(audit_sock, skb, audit_pid,
470 MSG_DONTWAIT);
471 }
Chris Wright37509e72005-04-29 17:19:14 +0100472 if (retval == -EAGAIN &&
473 (atomic_read(&audit_backlog)) < audit_backlog_limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 audit_log_end_irq(ab);
475 return 1;
476 }
477 if (retval < 0) {
478 if (retval == -ECONNREFUSED) {
479 printk(KERN_ERR
480 "audit: *NO* daemon at audit_pid=%d\n",
481 audit_pid);
482 audit_pid = 0;
483 } else
484 audit_log_lost("netlink socket too busy");
485 }
486 if (!audit_pid) { /* No daemon */
Chris Wright8fc61152005-05-06 15:54:17 +0100487 int offset = NLMSG_SPACE(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 int len = skb->len - offset;
Peter Martuccellic7fcb0e2005-04-29 16:10:24 +0100489 skb->data[offset + len] = '\0';
490 printk(KERN_ERR "%s\n", skb->data + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493 return 0;
494}
495
496/* Initialize audit support at boot time. */
497static int __init audit_init(void)
498{
499 printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
500 audit_default ? "enabled" : "disabled");
501 audit_sock = netlink_kernel_create(NETLINK_AUDIT, audit_receive);
502 if (!audit_sock)
503 audit_panic("cannot initialize netlink socket");
504
505 audit_initialized = 1;
506 audit_enabled = audit_default;
507 audit_log(NULL, "initialized");
508 return 0;
509}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510__initcall(audit_init);
511
512/* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
513static int __init audit_enable(char *str)
514{
515 audit_default = !!simple_strtol(str, NULL, 0);
516 printk(KERN_INFO "audit: %s%s\n",
517 audit_default ? "enabled" : "disabled",
518 audit_initialized ? "" : " (after initialization)");
519 if (audit_initialized)
520 audit_enabled = audit_default;
521 return 0;
522}
523
524__setup("audit=", audit_enable);
525
Chris Wright16e19042005-05-06 15:53:34 +0100526static void audit_buffer_free(struct audit_buffer *ab)
527{
528 unsigned long flags;
529
Chris Wright8fc61152005-05-06 15:54:17 +0100530 if (!ab)
531 return;
532
Chris Wright5ac52f32005-05-06 15:54:53 +0100533 if (ab->skb)
534 kfree_skb(ab->skb);
Chris Wright16e19042005-05-06 15:53:34 +0100535 atomic_dec(&audit_backlog);
536 spin_lock_irqsave(&audit_freelist_lock, flags);
537 if (++audit_freelist_count > AUDIT_MAXFREE)
538 kfree(ab);
539 else
540 list_add(&ab->list, &audit_freelist);
541 spin_unlock_irqrestore(&audit_freelist_lock, flags);
542}
543
Chris Wrightc1b773d2005-05-11 10:55:10 +0100544static struct audit_buffer * audit_buffer_alloc(int gfp_mask)
Chris Wright16e19042005-05-06 15:53:34 +0100545{
546 unsigned long flags;
547 struct audit_buffer *ab = NULL;
548
549 spin_lock_irqsave(&audit_freelist_lock, flags);
550 if (!list_empty(&audit_freelist)) {
551 ab = list_entry(audit_freelist.next,
552 struct audit_buffer, list);
553 list_del(&ab->list);
554 --audit_freelist_count;
555 }
556 spin_unlock_irqrestore(&audit_freelist_lock, flags);
557
558 if (!ab) {
David Woodhouse4332bdd2005-05-06 15:59:57 +0100559 ab = kmalloc(sizeof(*ab), gfp_mask);
Chris Wright16e19042005-05-06 15:53:34 +0100560 if (!ab)
Chris Wright8fc61152005-05-06 15:54:17 +0100561 goto err;
Chris Wright16e19042005-05-06 15:53:34 +0100562 }
563 atomic_inc(&audit_backlog);
Chris Wright8fc61152005-05-06 15:54:17 +0100564
David Woodhouse4332bdd2005-05-06 15:59:57 +0100565 ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask);
Chris Wright5ac52f32005-05-06 15:54:53 +0100566 if (!ab->skb)
Chris Wright8fc61152005-05-06 15:54:17 +0100567 goto err;
568
Chris Wright16e19042005-05-06 15:53:34 +0100569 return ab;
Chris Wright8fc61152005-05-06 15:54:17 +0100570err:
571 audit_buffer_free(ab);
572 return NULL;
Chris Wright16e19042005-05-06 15:53:34 +0100573}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575/* Obtain an audit buffer. This routine does locking to obtain the
576 * audit buffer, but then no locking is required for calls to
577 * audit_log_*format. If the tsk is a task that is currently in a
578 * syscall, then the syscall is marked as auditable and an audit record
579 * will be written at syscall exit. If there is no associated task, tsk
580 * should be NULL. */
Chris Wrightc1b773d2005-05-11 10:55:10 +0100581struct audit_buffer *audit_log_start(struct audit_context *ctx, int type, int pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582{
583 struct audit_buffer *ab = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 struct timespec t;
Steve Grubbd812ddb2005-04-29 16:09:52 +0100585 unsigned int serial;
Chris Wrightc1b773d2005-05-11 10:55:10 +0100586 struct nlmsghdr *nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 if (!audit_initialized)
589 return NULL;
590
591 if (audit_backlog_limit
592 && atomic_read(&audit_backlog) > audit_backlog_limit) {
593 if (audit_rate_check())
594 printk(KERN_WARNING
595 "audit: audit_backlog=%d > "
596 "audit_backlog_limit=%d\n",
597 atomic_read(&audit_backlog),
598 audit_backlog_limit);
599 audit_log_lost("backlog limit exceeded");
600 return NULL;
601 }
602
Chris Wrightc1b773d2005-05-11 10:55:10 +0100603 ab = audit_buffer_alloc(GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if (!ab) {
605 audit_log_lost("out of memory in audit_log_start");
606 return NULL;
607 }
608
Chris Wrightc1b773d2005-05-11 10:55:10 +0100609 ab->ctx = ctx;
610 nlh = (struct nlmsghdr *)skb_put(ab->skb, NLMSG_SPACE(0));
611 nlh->nlmsg_type = type;
612 nlh->nlmsg_flags = 0;
613 nlh->nlmsg_pid = pid;
614 nlh->nlmsg_seq = 0;
615
Chris Wright197c69c2005-05-11 10:54:05 +0100616 if (!audit_get_stamp(ab->ctx, &t, &serial)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 t = CURRENT_TIME;
Steve Grubbd812ddb2005-04-29 16:09:52 +0100618 serial = 0;
619 }
Chris Wright197c69c2005-05-11 10:54:05 +0100620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 audit_log_format(ab, "audit(%lu.%03lu:%u): ",
622 t.tv_sec, t.tv_nsec/1000000, serial);
623 return ab;
624}
625
Chris Wright8fc61152005-05-06 15:54:17 +0100626/**
Chris Wright5ac52f32005-05-06 15:54:53 +0100627 * audit_expand - expand skb in the audit buffer
Chris Wright8fc61152005-05-06 15:54:17 +0100628 * @ab: audit_buffer
629 *
630 * Returns 0 (no space) on failed expansion, or available space if
631 * successful.
632 */
David Woodhousee3b926b2005-05-10 18:56:08 +0100633static inline int audit_expand(struct audit_buffer *ab, int extra)
Chris Wright8fc61152005-05-06 15:54:17 +0100634{
Chris Wright5ac52f32005-05-06 15:54:53 +0100635 struct sk_buff *skb = ab->skb;
David Woodhousee3b926b2005-05-10 18:56:08 +0100636 int ret = pskb_expand_head(skb, skb_headroom(skb), extra,
Chris Wright5ac52f32005-05-06 15:54:53 +0100637 GFP_ATOMIC);
638 if (ret < 0) {
639 audit_log_lost("out of memory in audit_expand");
Chris Wright8fc61152005-05-06 15:54:17 +0100640 return 0;
Chris Wright5ac52f32005-05-06 15:54:53 +0100641 }
642 return skb_tailroom(skb);
Chris Wright8fc61152005-05-06 15:54:17 +0100643}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
645/* Format an audit message into the audit buffer. If there isn't enough
646 * room in the audit buffer, more room will be allocated and vsnprint
647 * will be called a second time. Currently, we assume that a printk
648 * can't format message larger than 1024 bytes, so we don't either. */
649static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
650 va_list args)
651{
652 int len, avail;
Chris Wright5ac52f32005-05-06 15:54:53 +0100653 struct sk_buff *skb;
David Woodhouseeecb0a72005-05-10 18:58:51 +0100654 va_list args2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 if (!ab)
657 return;
658
Chris Wright5ac52f32005-05-06 15:54:53 +0100659 BUG_ON(!ab->skb);
660 skb = ab->skb;
661 avail = skb_tailroom(skb);
662 if (avail == 0) {
David Woodhousee3b926b2005-05-10 18:56:08 +0100663 avail = audit_expand(ab, AUDIT_BUFSIZ);
Chris Wright8fc61152005-05-06 15:54:17 +0100664 if (!avail)
665 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
David Woodhouseeecb0a72005-05-10 18:58:51 +0100667 va_copy(args2, args);
Chris Wright5ac52f32005-05-06 15:54:53 +0100668 len = vsnprintf(skb->tail, avail, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 if (len >= avail) {
670 /* The printk buffer is 1024 bytes long, so if we get
671 * here and AUDIT_BUFSIZ is at least 1024, then we can
672 * log everything that printk could have logged. */
David Woodhouse9ea74f02005-05-13 16:35:19 +0100673 avail = audit_expand(ab, max_t(AUDIT_BUFSIZ, 1+len-avail));
Chris Wright8fc61152005-05-06 15:54:17 +0100674 if (!avail)
675 goto out;
David Woodhouseeecb0a72005-05-10 18:58:51 +0100676 len = vsnprintf(skb->tail, avail, fmt, args2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
Chris Wright5ac52f32005-05-06 15:54:53 +0100678 skb_put(skb, (len < avail) ? len : avail);
Chris Wright8fc61152005-05-06 15:54:17 +0100679out:
680 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
683/* Format a message into the audit buffer. All the work is done in
684 * audit_log_vformat. */
685void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
686{
687 va_list args;
688
689 if (!ab)
690 return;
691 va_start(args, fmt);
692 audit_log_vformat(ab, fmt, args);
693 va_end(args);
694}
695
83c7d092005-04-29 15:54:44 +0100696void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf, size_t len)
697{
698 int i;
699
700 for (i=0; i<len; i++)
701 audit_log_format(ab, "%02x", buf[i]);
702}
703
704void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
705{
Andrew Morton81b78542005-04-29 15:59:11 +0100706 const unsigned char *p = string;
83c7d092005-04-29 15:54:44 +0100707
708 while (*p) {
709 if (*p == '"' || *p == ' ' || *p < 0x20 || *p > 0x7f) {
710 audit_log_hex(ab, string, strlen(string));
711 return;
712 }
713 p++;
714 }
715 audit_log_format(ab, "\"%s\"", string);
716}
717
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719/* This is a helper-function to print the d_path without using a static
720 * buffer or allocating another buffer in addition to the one in
721 * audit_buffer. */
722void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
723 struct dentry *dentry, struct vfsmount *vfsmnt)
724{
725 char *p;
Chris Wright5ac52f32005-05-06 15:54:53 +0100726 struct sk_buff *skb = ab->skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 int len, avail;
728
Chris Wright8fc61152005-05-06 15:54:17 +0100729 if (prefix)
730 audit_log_format(ab, " %s", prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Chris Wright5ac52f32005-05-06 15:54:53 +0100732 avail = skb_tailroom(skb);
733 p = d_path(dentry, vfsmnt, skb->tail, avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (IS_ERR(p)) {
735 /* FIXME: can we save some information here? */
736 audit_log_format(ab, "<toolong>");
737 } else {
Chris Wright5ac52f32005-05-06 15:54:53 +0100738 /* path isn't at start of buffer */
739 len = ((char *)skb->tail + avail - 1) - p;
740 memmove(skb->tail, p, len);
741 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 }
743}
744
745/* Remove queued messages from the audit_txlist and send them to userspace. */
746static void audit_tasklet_handler(unsigned long arg)
747{
748 LIST_HEAD(list);
749 struct audit_buffer *ab;
750 unsigned long flags;
751
752 spin_lock_irqsave(&audit_txlist_lock, flags);
753 list_splice_init(&audit_txlist, &list);
754 spin_unlock_irqrestore(&audit_txlist_lock, flags);
755
756 while (!list_empty(&list)) {
757 ab = list_entry(list.next, struct audit_buffer, list);
758 list_del(&ab->list);
759 audit_log_end_fast(ab);
760 }
761}
762
763static DECLARE_TASKLET(audit_tasklet, audit_tasklet_handler, 0);
764
765/* The netlink_* functions cannot be called inside an irq context, so
766 * the audit buffer is places on a queue and a tasklet is scheduled to
767 * remove them from the queue outside the irq context. May be called in
768 * any context. */
769static void audit_log_end_irq(struct audit_buffer *ab)
770{
771 unsigned long flags;
772
773 if (!ab)
774 return;
775 spin_lock_irqsave(&audit_txlist_lock, flags);
776 list_add_tail(&ab->list, &audit_txlist);
777 spin_unlock_irqrestore(&audit_txlist_lock, flags);
778
779 tasklet_schedule(&audit_tasklet);
780}
781
782/* Send the message in the audit buffer directly to user space. May not
783 * be called in an irq context. */
784static void audit_log_end_fast(struct audit_buffer *ab)
785{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 BUG_ON(in_irq());
787 if (!ab)
788 return;
789 if (!audit_rate_check()) {
790 audit_log_lost("rate limit exceeded");
791 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (audit_log_drain(ab))
793 return;
794 }
Chris Wright16e19042005-05-06 15:53:34 +0100795 audit_buffer_free(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
798/* Send or queue the message in the audit buffer, depending on the
799 * current context. (A convenience function that may be called in any
800 * context.) */
801void audit_log_end(struct audit_buffer *ab)
802{
803 if (in_irq())
804 audit_log_end_irq(ab);
805 else
806 audit_log_end_fast(ab);
807}
808
809/* Log an audit record. This is a convenience function that calls
810 * audit_log_start, audit_log_vformat, and audit_log_end. It may be
811 * called in any context. */
Chris Wrightc1b773d2005-05-11 10:55:10 +0100812void audit_log_type(struct audit_context *ctx, int type, int pid,
813 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
815 struct audit_buffer *ab;
816 va_list args;
817
Chris Wrightc1b773d2005-05-11 10:55:10 +0100818 ab = audit_log_start(ctx, type, pid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (ab) {
820 va_start(args, fmt);
821 audit_log_vformat(ab, fmt, args);
822 va_end(args);
823 audit_log_end(ab);
824 }
825}