blob: 4e940c05ede8a0a839ad0e03137e8222893ab431 [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
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;
328 case AUDIT_USER:
Steve Grubbc0404992005-05-13 18:17:42 +0100329 case AUDIT_USER_AUTH:
330 case AUDIT_USER_ACCT:
331 case AUDIT_USER_MGMT:
332 case AUDIT_CRED_ACQ:
333 case AUDIT_CRED_DISP:
334 case AUDIT_USER_START:
335 case AUDIT_USER_END:
336 case AUDIT_USER_AVC:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (!cap_raised(eff_cap, CAP_AUDIT_WRITE))
338 err = -EPERM;
339 break;
340 default: /* bad msg */
341 err = -EINVAL;
342 }
343
344 return err;
345}
346
347static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
348{
349 u32 uid, pid, seq;
350 void *data;
351 struct audit_status *status_get, status_set;
352 int err;
Steve Grubbc0404992005-05-13 18:17:42 +0100353 struct audit_buffer *ab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 u16 msg_type = nlh->nlmsg_type;
Serge Hallync94c2572005-04-29 16:27:17 +0100355 uid_t loginuid; /* loginuid of sender */
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100356 struct audit_sig_info sig_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type);
359 if (err)
360 return err;
361
362 pid = NETLINK_CREDS(skb)->pid;
363 uid = NETLINK_CREDS(skb)->uid;
Serge Hallync94c2572005-04-29 16:27:17 +0100364 loginuid = NETLINK_CB(skb).loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 seq = nlh->nlmsg_seq;
366 data = NLMSG_DATA(nlh);
367
368 switch (msg_type) {
369 case AUDIT_GET:
370 status_set.enabled = audit_enabled;
371 status_set.failure = audit_failure;
372 status_set.pid = audit_pid;
373 status_set.rate_limit = audit_rate_limit;
374 status_set.backlog_limit = audit_backlog_limit;
375 status_set.lost = atomic_read(&audit_lost);
376 status_set.backlog = atomic_read(&audit_backlog);
377 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
378 &status_set, sizeof(status_set));
379 break;
380 case AUDIT_SET:
381 if (nlh->nlmsg_len < sizeof(struct audit_status))
382 return -EINVAL;
383 status_get = (struct audit_status *)data;
384 if (status_get->mask & AUDIT_STATUS_ENABLED) {
Serge Hallync94c2572005-04-29 16:27:17 +0100385 err = audit_set_enabled(status_get->enabled, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 if (err < 0) return err;
387 }
388 if (status_get->mask & AUDIT_STATUS_FAILURE) {
Serge Hallync94c2572005-04-29 16:27:17 +0100389 err = audit_set_failure(status_get->failure, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (err < 0) return err;
391 }
392 if (status_get->mask & AUDIT_STATUS_PID) {
393 int old = audit_pid;
394 audit_pid = status_get->pid;
Steve Grubbc0404992005-05-13 18:17:42 +0100395 audit_log(NULL, AUDIT_CONFIG_CHANGE,
396 "audit_pid=%d old=%d by auid %u",
Serge Hallync94c2572005-04-29 16:27:17 +0100397 audit_pid, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 }
399 if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
Serge Hallync94c2572005-04-29 16:27:17 +0100400 audit_set_rate_limit(status_get->rate_limit, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
Serge Hallync94c2572005-04-29 16:27:17 +0100402 audit_set_backlog_limit(status_get->backlog_limit,
403 loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 break;
405 case AUDIT_USER:
Steve Grubbc0404992005-05-13 18:17:42 +0100406 case AUDIT_USER_AUTH:
407 case AUDIT_USER_ACCT:
408 case AUDIT_USER_MGMT:
409 case AUDIT_CRED_ACQ:
410 case AUDIT_CRED_DISP:
411 case AUDIT_USER_START:
412 case AUDIT_USER_END:
413 case AUDIT_USER_AVC:
414 ab = audit_log_start(NULL, msg_type);
415 if (!ab)
416 break; /* audit_panic has been called */
417 audit_log_format(ab,
Serge Hallync94c2572005-04-29 16:27:17 +0100418 "user pid=%d uid=%d length=%d loginuid=%u"
419 " msg='%.1024s'",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 pid, uid,
421 (int)(nlh->nlmsg_len
422 - ((char *)data - (char *)nlh)),
Serge Hallync94c2572005-04-29 16:27:17 +0100423 loginuid, (char *)data);
Steve Grubbc0404992005-05-13 18:17:42 +0100424 audit_set_pid(ab, pid);
425 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 break;
427 case AUDIT_ADD:
428 case AUDIT_DEL:
429 if (nlh->nlmsg_len < sizeof(struct audit_rule))
430 return -EINVAL;
431 /* fallthrough */
432 case AUDIT_LIST:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
Serge Hallync94c2572005-04-29 16:27:17 +0100434 uid, seq, data, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 break;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100436 case AUDIT_SIGNAL_INFO:
437 sig_data.uid = audit_sig_uid;
438 sig_data.pid = audit_sig_pid;
439 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO,
440 0, 0, &sig_data, sizeof(sig_data));
441 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 default:
443 err = -EINVAL;
444 break;
445 }
446
447 return err < 0 ? err : 0;
448}
449
450/* Get message from skb (based on rtnetlink_rcv_skb). Each message is
451 * processed by audit_receive_msg. Malformed skbs with wrong length are
452 * discarded silently. */
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700453static void audit_receive_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
455 int err;
456 struct nlmsghdr *nlh;
457 u32 rlen;
458
459 while (skb->len >= NLMSG_SPACE(0)) {
460 nlh = (struct nlmsghdr *)skb->data;
461 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700462 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
464 if (rlen > skb->len)
465 rlen = skb->len;
466 if ((err = audit_receive_msg(skb, nlh))) {
467 netlink_ack(skb, nlh, err);
468 } else if (nlh->nlmsg_flags & NLM_F_ACK)
469 netlink_ack(skb, nlh, 0);
470 skb_pull(skb, rlen);
471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
474/* Receive messages from netlink socket. */
475static void audit_receive(struct sock *sk, int length)
476{
477 struct sk_buff *skb;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700478 unsigned int qlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700480 down(&audit_netlink_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700482 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
483 skb = skb_dequeue(&sk->sk_receive_queue);
484 audit_receive_skb(skb);
485 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487 up(&audit_netlink_sem);
488}
489
Chris Wright5ac52f32005-05-06 15:54:53 +0100490/* Grab skbuff from the audit_buffer and send to user space. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491static inline int audit_log_drain(struct audit_buffer *ab)
492{
Chris Wright8fc61152005-05-06 15:54:17 +0100493 struct sk_buff *skb = ab->skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Chris Wright8fc61152005-05-06 15:54:17 +0100495 if (skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 int retval = 0;
497
498 if (audit_pid) {
Chris Wright5ac52f32005-05-06 15:54:53 +0100499 struct nlmsghdr *nlh = (struct nlmsghdr *)skb->data;
Chris Wright5a241d72005-05-11 10:43:07 +0100500 nlh->nlmsg_len = skb->len - NLMSG_SPACE(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 skb_get(skb); /* because netlink_* frees */
502 retval = netlink_unicast(audit_sock, skb, audit_pid,
503 MSG_DONTWAIT);
504 }
Chris Wright37509e72005-04-29 17:19:14 +0100505 if (retval == -EAGAIN &&
506 (atomic_read(&audit_backlog)) < audit_backlog_limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 audit_log_end_irq(ab);
508 return 1;
509 }
510 if (retval < 0) {
511 if (retval == -ECONNREFUSED) {
512 printk(KERN_ERR
513 "audit: *NO* daemon at audit_pid=%d\n",
514 audit_pid);
515 audit_pid = 0;
516 } else
517 audit_log_lost("netlink socket too busy");
518 }
519 if (!audit_pid) { /* No daemon */
Chris Wright8fc61152005-05-06 15:54:17 +0100520 int offset = NLMSG_SPACE(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 int len = skb->len - offset;
Peter Martuccellic7fcb0e2005-04-29 16:10:24 +0100522 skb->data[offset + len] = '\0';
523 printk(KERN_ERR "%s\n", skb->data + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526 return 0;
527}
528
529/* Initialize audit support at boot time. */
530static int __init audit_init(void)
531{
532 printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
533 audit_default ? "enabled" : "disabled");
534 audit_sock = netlink_kernel_create(NETLINK_AUDIT, audit_receive);
535 if (!audit_sock)
536 audit_panic("cannot initialize netlink socket");
537
538 audit_initialized = 1;
539 audit_enabled = audit_default;
Steve Grubbc0404992005-05-13 18:17:42 +0100540 audit_log(NULL, AUDIT_KERNEL, "initialized");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return 0;
542}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543__initcall(audit_init);
544
545/* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
546static int __init audit_enable(char *str)
547{
548 audit_default = !!simple_strtol(str, NULL, 0);
549 printk(KERN_INFO "audit: %s%s\n",
550 audit_default ? "enabled" : "disabled",
551 audit_initialized ? "" : " (after initialization)");
552 if (audit_initialized)
553 audit_enabled = audit_default;
554 return 0;
555}
556
557__setup("audit=", audit_enable);
558
Chris Wright16e19042005-05-06 15:53:34 +0100559static void audit_buffer_free(struct audit_buffer *ab)
560{
561 unsigned long flags;
562
Chris Wright8fc61152005-05-06 15:54:17 +0100563 if (!ab)
564 return;
565
Chris Wright5ac52f32005-05-06 15:54:53 +0100566 if (ab->skb)
567 kfree_skb(ab->skb);
Chris Wright16e19042005-05-06 15:53:34 +0100568 atomic_dec(&audit_backlog);
569 spin_lock_irqsave(&audit_freelist_lock, flags);
570 if (++audit_freelist_count > AUDIT_MAXFREE)
571 kfree(ab);
572 else
573 list_add(&ab->list, &audit_freelist);
574 spin_unlock_irqrestore(&audit_freelist_lock, flags);
575}
576
Steve Grubbc0404992005-05-13 18:17:42 +0100577static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
578 int gfp_mask, int type)
Chris Wright16e19042005-05-06 15:53:34 +0100579{
580 unsigned long flags;
581 struct audit_buffer *ab = NULL;
Steve Grubbc0404992005-05-13 18:17:42 +0100582 struct nlmsghdr *nlh;
Chris Wright16e19042005-05-06 15:53:34 +0100583
584 spin_lock_irqsave(&audit_freelist_lock, flags);
585 if (!list_empty(&audit_freelist)) {
586 ab = list_entry(audit_freelist.next,
587 struct audit_buffer, list);
588 list_del(&ab->list);
589 --audit_freelist_count;
590 }
591 spin_unlock_irqrestore(&audit_freelist_lock, flags);
592
593 if (!ab) {
David Woodhouse4332bdd2005-05-06 15:59:57 +0100594 ab = kmalloc(sizeof(*ab), gfp_mask);
Chris Wright16e19042005-05-06 15:53:34 +0100595 if (!ab)
Chris Wright8fc61152005-05-06 15:54:17 +0100596 goto err;
Chris Wright16e19042005-05-06 15:53:34 +0100597 }
598 atomic_inc(&audit_backlog);
Chris Wright8fc61152005-05-06 15:54:17 +0100599
David Woodhouse4332bdd2005-05-06 15:59:57 +0100600 ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask);
Chris Wright5ac52f32005-05-06 15:54:53 +0100601 if (!ab->skb)
Chris Wright8fc61152005-05-06 15:54:17 +0100602 goto err;
603
Steve Grubbc0404992005-05-13 18:17:42 +0100604 ab->ctx = ctx;
605 nlh = (struct nlmsghdr *)skb_put(ab->skb, NLMSG_SPACE(0));
606 nlh->nlmsg_type = type;
607 nlh->nlmsg_flags = 0;
608 nlh->nlmsg_pid = 0;
609 nlh->nlmsg_seq = 0;
Chris Wright16e19042005-05-06 15:53:34 +0100610 return ab;
Chris Wright8fc61152005-05-06 15:54:17 +0100611err:
612 audit_buffer_free(ab);
613 return NULL;
Chris Wright16e19042005-05-06 15:53:34 +0100614}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616/* Obtain an audit buffer. This routine does locking to obtain the
617 * audit buffer, but then no locking is required for calls to
618 * audit_log_*format. If the tsk is a task that is currently in a
619 * syscall, then the syscall is marked as auditable and an audit record
620 * will be written at syscall exit. If there is no associated task, tsk
621 * should be NULL. */
Steve Grubbc0404992005-05-13 18:17:42 +0100622struct audit_buffer *audit_log_start(struct audit_context *ctx, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
624 struct audit_buffer *ab = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 struct timespec t;
Steve Grubbd812ddb2005-04-29 16:09:52 +0100626 unsigned int serial;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 if (!audit_initialized)
629 return NULL;
630
631 if (audit_backlog_limit
632 && atomic_read(&audit_backlog) > audit_backlog_limit) {
633 if (audit_rate_check())
634 printk(KERN_WARNING
635 "audit: audit_backlog=%d > "
636 "audit_backlog_limit=%d\n",
637 atomic_read(&audit_backlog),
638 audit_backlog_limit);
639 audit_log_lost("backlog limit exceeded");
640 return NULL;
641 }
642
Steve Grubbc0404992005-05-13 18:17:42 +0100643 ab = audit_buffer_alloc(ctx, GFP_ATOMIC, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 if (!ab) {
645 audit_log_lost("out of memory in audit_log_start");
646 return NULL;
647 }
648
Chris Wright197c69c2005-05-11 10:54:05 +0100649 if (!audit_get_stamp(ab->ctx, &t, &serial)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 t = CURRENT_TIME;
Steve Grubbd812ddb2005-04-29 16:09:52 +0100651 serial = 0;
652 }
Chris Wright197c69c2005-05-11 10:54:05 +0100653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 audit_log_format(ab, "audit(%lu.%03lu:%u): ",
655 t.tv_sec, t.tv_nsec/1000000, serial);
656 return ab;
657}
658
Chris Wright8fc61152005-05-06 15:54:17 +0100659/**
Chris Wright5ac52f32005-05-06 15:54:53 +0100660 * audit_expand - expand skb in the audit buffer
Chris Wright8fc61152005-05-06 15:54:17 +0100661 * @ab: audit_buffer
662 *
663 * Returns 0 (no space) on failed expansion, or available space if
664 * successful.
665 */
David Woodhousee3b926b2005-05-10 18:56:08 +0100666static inline int audit_expand(struct audit_buffer *ab, int extra)
Chris Wright8fc61152005-05-06 15:54:17 +0100667{
Chris Wright5ac52f32005-05-06 15:54:53 +0100668 struct sk_buff *skb = ab->skb;
David Woodhousee3b926b2005-05-10 18:56:08 +0100669 int ret = pskb_expand_head(skb, skb_headroom(skb), extra,
Chris Wright5ac52f32005-05-06 15:54:53 +0100670 GFP_ATOMIC);
671 if (ret < 0) {
672 audit_log_lost("out of memory in audit_expand");
Chris Wright8fc61152005-05-06 15:54:17 +0100673 return 0;
Chris Wright5ac52f32005-05-06 15:54:53 +0100674 }
675 return skb_tailroom(skb);
Chris Wright8fc61152005-05-06 15:54:17 +0100676}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678/* Format an audit message into the audit buffer. If there isn't enough
679 * room in the audit buffer, more room will be allocated and vsnprint
680 * will be called a second time. Currently, we assume that a printk
681 * can't format message larger than 1024 bytes, so we don't either. */
682static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
683 va_list args)
684{
685 int len, avail;
Chris Wright5ac52f32005-05-06 15:54:53 +0100686 struct sk_buff *skb;
David Woodhouseeecb0a72005-05-10 18:58:51 +0100687 va_list args2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 if (!ab)
690 return;
691
Chris Wright5ac52f32005-05-06 15:54:53 +0100692 BUG_ON(!ab->skb);
693 skb = ab->skb;
694 avail = skb_tailroom(skb);
695 if (avail == 0) {
David Woodhousee3b926b2005-05-10 18:56:08 +0100696 avail = audit_expand(ab, AUDIT_BUFSIZ);
Chris Wright8fc61152005-05-06 15:54:17 +0100697 if (!avail)
698 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 }
David Woodhouseeecb0a72005-05-10 18:58:51 +0100700 va_copy(args2, args);
Chris Wright5ac52f32005-05-06 15:54:53 +0100701 len = vsnprintf(skb->tail, avail, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 if (len >= avail) {
703 /* The printk buffer is 1024 bytes long, so if we get
704 * here and AUDIT_BUFSIZ is at least 1024, then we can
705 * log everything that printk could have logged. */
David Woodhouse9ea74f02005-05-13 16:35:19 +0100706 avail = audit_expand(ab, max_t(AUDIT_BUFSIZ, 1+len-avail));
Chris Wright8fc61152005-05-06 15:54:17 +0100707 if (!avail)
708 goto out;
David Woodhouseeecb0a72005-05-10 18:58:51 +0100709 len = vsnprintf(skb->tail, avail, fmt, args2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
Chris Wright5ac52f32005-05-06 15:54:53 +0100711 skb_put(skb, (len < avail) ? len : avail);
Chris Wright8fc61152005-05-06 15:54:17 +0100712out:
713 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
716/* Format a message into the audit buffer. All the work is done in
717 * audit_log_vformat. */
718void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
719{
720 va_list args;
721
722 if (!ab)
723 return;
724 va_start(args, fmt);
725 audit_log_vformat(ab, fmt, args);
726 va_end(args);
727}
728
83c7d092005-04-29 15:54:44 +0100729void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf, size_t len)
730{
731 int i;
732
733 for (i=0; i<len; i++)
734 audit_log_format(ab, "%02x", buf[i]);
735}
736
737void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
738{
Andrew Morton81b78542005-04-29 15:59:11 +0100739 const unsigned char *p = string;
83c7d092005-04-29 15:54:44 +0100740
741 while (*p) {
742 if (*p == '"' || *p == ' ' || *p < 0x20 || *p > 0x7f) {
743 audit_log_hex(ab, string, strlen(string));
744 return;
745 }
746 p++;
747 }
748 audit_log_format(ab, "\"%s\"", string);
749}
750
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752/* This is a helper-function to print the d_path without using a static
753 * buffer or allocating another buffer in addition to the one in
754 * audit_buffer. */
755void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
756 struct dentry *dentry, struct vfsmount *vfsmnt)
757{
758 char *p;
Chris Wright5ac52f32005-05-06 15:54:53 +0100759 struct sk_buff *skb = ab->skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 int len, avail;
761
Chris Wright8fc61152005-05-06 15:54:17 +0100762 if (prefix)
763 audit_log_format(ab, " %s", prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Chris Wright5ac52f32005-05-06 15:54:53 +0100765 avail = skb_tailroom(skb);
766 p = d_path(dentry, vfsmnt, skb->tail, avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (IS_ERR(p)) {
768 /* FIXME: can we save some information here? */
769 audit_log_format(ab, "<toolong>");
770 } else {
Chris Wright5ac52f32005-05-06 15:54:53 +0100771 /* path isn't at start of buffer */
772 len = ((char *)skb->tail + avail - 1) - p;
773 memmove(skb->tail, p, len);
774 skb_put(skb, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776}
777
778/* Remove queued messages from the audit_txlist and send them to userspace. */
779static void audit_tasklet_handler(unsigned long arg)
780{
781 LIST_HEAD(list);
782 struct audit_buffer *ab;
783 unsigned long flags;
784
785 spin_lock_irqsave(&audit_txlist_lock, flags);
786 list_splice_init(&audit_txlist, &list);
787 spin_unlock_irqrestore(&audit_txlist_lock, flags);
788
789 while (!list_empty(&list)) {
790 ab = list_entry(list.next, struct audit_buffer, list);
791 list_del(&ab->list);
792 audit_log_end_fast(ab);
793 }
794}
795
796static DECLARE_TASKLET(audit_tasklet, audit_tasklet_handler, 0);
797
798/* The netlink_* functions cannot be called inside an irq context, so
799 * the audit buffer is places on a queue and a tasklet is scheduled to
800 * remove them from the queue outside the irq context. May be called in
801 * any context. */
802static void audit_log_end_irq(struct audit_buffer *ab)
803{
804 unsigned long flags;
805
806 if (!ab)
807 return;
808 spin_lock_irqsave(&audit_txlist_lock, flags);
809 list_add_tail(&ab->list, &audit_txlist);
810 spin_unlock_irqrestore(&audit_txlist_lock, flags);
811
812 tasklet_schedule(&audit_tasklet);
813}
814
815/* Send the message in the audit buffer directly to user space. May not
816 * be called in an irq context. */
817static void audit_log_end_fast(struct audit_buffer *ab)
818{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 BUG_ON(in_irq());
820 if (!ab)
821 return;
822 if (!audit_rate_check()) {
823 audit_log_lost("rate limit exceeded");
824 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (audit_log_drain(ab))
826 return;
827 }
Chris Wright16e19042005-05-06 15:53:34 +0100828 audit_buffer_free(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
831/* Send or queue the message in the audit buffer, depending on the
832 * current context. (A convenience function that may be called in any
833 * context.) */
834void audit_log_end(struct audit_buffer *ab)
835{
836 if (in_irq())
837 audit_log_end_irq(ab);
838 else
839 audit_log_end_fast(ab);
840}
841
842/* Log an audit record. This is a convenience function that calls
843 * audit_log_start, audit_log_vformat, and audit_log_end. It may be
844 * called in any context. */
Steve Grubbc0404992005-05-13 18:17:42 +0100845void audit_log(struct audit_context *ctx, int type, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
847 struct audit_buffer *ab;
848 va_list args;
849
Steve Grubbc0404992005-05-13 18:17:42 +0100850 ab = audit_log_start(ctx, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (ab) {
852 va_start(args, fmt);
853 audit_log_vformat(ab, fmt, args);
854 va_end(args);
855 audit_log_end(ab);
856 }
857}