blob: 0fbf1c116363a8ae139bef90c36b2eca21a742c6 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <asm/types.h>
Alan Cox715b49e2006-01-18 17:44:07 -080046#include <asm/atomic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/mm.h>
48#include <linux/module.h>
David Woodhouseb7d11252005-05-19 10:56:58 +010049#include <linux/err.h>
50#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52#include <linux/audit.h>
53
54#include <net/sock.h>
Amy Griffis93315ed2006-02-07 12:05:27 -050055#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <linux/skbuff.h>
57#include <linux/netlink.h>
Darrel Goeddel3dc7e312006-03-10 18:14:06 -060058#include <linux/selinux.h>
Amy Griffisf368c07d2006-04-07 16:55:56 -040059#include <linux/inotify.h>
Darrel Goeddel3dc7e312006-03-10 18:14:06 -060060
61#include "audit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63/* No auditing will take place until audit_initialized != 0.
64 * (Initialization happens after skb_init is called.) */
65static int audit_initialized;
66
67/* No syscall auditing will take place unless audit_enabled != 0. */
68int audit_enabled;
69
70/* Default state when kernel boots without any parameters. */
71static int audit_default;
72
73/* If auditing cannot proceed, audit_failure selects what happens. */
74static int audit_failure = AUDIT_FAIL_PRINTK;
75
76/* If audit records are to be written to the netlink socket, audit_pid
77 * contains the (non-zero) pid. */
Steve Grubbc2f0c7c2005-05-06 12:38:39 +010078int audit_pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Randy Dunlapb0dd25a2005-09-13 12:47:11 -070080/* If audit_rate_limit is non-zero, limit the rate of sending audit records
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 * to that number per second. This prevents DoS attacks, but results in
82 * audit records being dropped. */
83static int audit_rate_limit;
84
85/* Number of outstanding audit_buffers allowed. */
86static int audit_backlog_limit = 64;
David Woodhouseac4cec42005-07-02 14:08:48 +010087static int audit_backlog_wait_time = 60 * HZ;
88static int audit_backlog_wait_overflow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Steve Grubbc2f0c7c2005-05-06 12:38:39 +010090/* The identity of the user shutting down the audit system. */
91uid_t audit_sig_uid = -1;
92pid_t audit_sig_pid = -1;
Al Viroe1396062006-05-25 10:19:47 -040093u32 audit_sig_sid = 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +010094
Linus Torvalds1da177e2005-04-16 15:20:36 -070095/* Records can be lost in several ways:
96 0) [suppressed in audit_alloc]
97 1) out of memory in audit_log_start [kmalloc of struct audit_buffer]
98 2) out of memory in audit_log_move [alloc_skb]
99 3) suppressed due to audit_rate_limit
100 4) suppressed due to audit_backlog_limit
101*/
102static atomic_t audit_lost = ATOMIC_INIT(0);
103
104/* The netlink socket. */
105static struct sock *audit_sock;
106
Amy Griffisf368c07d2006-04-07 16:55:56 -0400107/* Inotify handle. */
108struct inotify_handle *audit_ih;
109
110/* Hash for inode-based rules */
111struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS];
112
David Woodhouseb7d11252005-05-19 10:56:58 +0100113/* The audit_freelist is a list of pre-allocated audit buffers (if more
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of
115 * being placed on the freelist). */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static DEFINE_SPINLOCK(audit_freelist_lock);
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700117static int audit_freelist_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static LIST_HEAD(audit_freelist);
119
David Woodhouseb7d11252005-05-19 10:56:58 +0100120static struct sk_buff_head audit_skb_queue;
121static struct task_struct *kauditd_task;
122static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait);
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100123static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Amy Griffisf368c07d2006-04-07 16:55:56 -0400125/* Serialize requests from userspace. */
126static DEFINE_MUTEX(audit_cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128/* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
129 * audit records. Since printk uses a 1024 byte buffer, this buffer
130 * should be at least that large. */
131#define AUDIT_BUFSIZ 1024
132
133/* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the
134 * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */
135#define AUDIT_MAXFREE (2*NR_CPUS)
136
137/* The audit_buffer is used when formatting an audit record. The caller
138 * locks briefly to get the record off the freelist or to allocate the
139 * buffer, and locks briefly to send the buffer to the netlink layer or
140 * to place it on a transmit queue. Multiple audit_buffers can be in
141 * use simultaneously. */
142struct audit_buffer {
143 struct list_head list;
Chris Wright8fc61152005-05-06 15:54:17 +0100144 struct sk_buff *skb; /* formatted skb ready to send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 struct audit_context *ctx; /* NULL or associated context */
Al Viro9796fdd2005-10-21 03:22:03 -0400146 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147};
148
Steve Grubbc0404992005-05-13 18:17:42 +0100149static void audit_set_pid(struct audit_buffer *ab, pid_t pid)
150{
151 struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
152 nlh->nlmsg_pid = pid;
153}
154
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000155void audit_panic(const char *message)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 switch (audit_failure)
158 {
159 case AUDIT_FAIL_SILENT:
160 break;
161 case AUDIT_FAIL_PRINTK:
162 printk(KERN_ERR "audit: %s\n", message);
163 break;
164 case AUDIT_FAIL_PANIC:
165 panic("audit: %s\n", message);
166 break;
167 }
168}
169
170static inline int audit_rate_check(void)
171{
172 static unsigned long last_check = 0;
173 static int messages = 0;
174 static DEFINE_SPINLOCK(lock);
175 unsigned long flags;
176 unsigned long now;
177 unsigned long elapsed;
178 int retval = 0;
179
180 if (!audit_rate_limit) return 1;
181
182 spin_lock_irqsave(&lock, flags);
183 if (++messages < audit_rate_limit) {
184 retval = 1;
185 } else {
186 now = jiffies;
187 elapsed = now - last_check;
188 if (elapsed > HZ) {
189 last_check = now;
190 messages = 0;
191 retval = 1;
192 }
193 }
194 spin_unlock_irqrestore(&lock, flags);
195
196 return retval;
197}
198
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700199/**
200 * audit_log_lost - conditionally log lost audit message event
201 * @message: the message stating reason for lost audit message
202 *
203 * Emit at least 1 message per second, even if audit_rate_check is
204 * throttling.
205 * Always increment the lost messages counter.
206*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207void audit_log_lost(const char *message)
208{
209 static unsigned long last_msg = 0;
210 static DEFINE_SPINLOCK(lock);
211 unsigned long flags;
212 unsigned long now;
213 int print;
214
215 atomic_inc(&audit_lost);
216
217 print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);
218
219 if (!print) {
220 spin_lock_irqsave(&lock, flags);
221 now = jiffies;
222 if (now - last_msg > HZ) {
223 print = 1;
224 last_msg = now;
225 }
226 spin_unlock_irqrestore(&lock, flags);
227 }
228
229 if (print) {
230 printk(KERN_WARNING
David Woodhouseb7d11252005-05-19 10:56:58 +0100231 "audit: audit_lost=%d audit_rate_limit=%d audit_backlog_limit=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 atomic_read(&audit_lost),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 audit_rate_limit,
234 audit_backlog_limit);
235 audit_panic(message);
236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Steve Grubbce29b682006-04-01 18:29:34 -0500239static int audit_set_rate_limit(int limit, uid_t loginuid, u32 sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
Steve Grubbce29b682006-04-01 18:29:34 -0500241 int old = audit_rate_limit;
242
243 if (sid) {
244 char *ctx = NULL;
245 u32 len;
246 int rc;
247 if ((rc = selinux_ctxid_to_string(sid, &ctx, &len)))
248 return rc;
249 else
250 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
251 "audit_rate_limit=%d old=%d by auid=%u subj=%s",
252 limit, old, loginuid, ctx);
253 kfree(ctx);
254 } else
255 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100256 "audit_rate_limit=%d old=%d by auid=%u",
Steve Grubbce29b682006-04-01 18:29:34 -0500257 limit, old, loginuid);
258 audit_rate_limit = limit;
Serge E. Hallyn5d136a02006-04-27 16:45:14 -0500259 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Steve Grubbce29b682006-04-01 18:29:34 -0500262static int audit_set_backlog_limit(int limit, uid_t loginuid, u32 sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
Steve Grubbce29b682006-04-01 18:29:34 -0500264 int old = audit_backlog_limit;
265
266 if (sid) {
267 char *ctx = NULL;
268 u32 len;
269 int rc;
270 if ((rc = selinux_ctxid_to_string(sid, &ctx, &len)))
271 return rc;
272 else
273 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
274 "audit_backlog_limit=%d old=%d by auid=%u subj=%s",
275 limit, old, loginuid, ctx);
276 kfree(ctx);
277 } else
278 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100279 "audit_backlog_limit=%d old=%d by auid=%u",
Steve Grubbce29b682006-04-01 18:29:34 -0500280 limit, old, loginuid);
281 audit_backlog_limit = limit;
Serge E. Hallyn5d136a02006-04-27 16:45:14 -0500282 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
Steve Grubbce29b682006-04-01 18:29:34 -0500285static int audit_set_enabled(int state, uid_t loginuid, u32 sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Steve Grubbce29b682006-04-01 18:29:34 -0500287 int old = audit_enabled;
288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 if (state != 0 && state != 1)
290 return -EINVAL;
Steve Grubbce29b682006-04-01 18:29:34 -0500291
292 if (sid) {
293 char *ctx = NULL;
294 u32 len;
295 int rc;
296 if ((rc = selinux_ctxid_to_string(sid, &ctx, &len)))
297 return rc;
298 else
299 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
300 "audit_enabled=%d old=%d by auid=%u subj=%s",
301 state, old, loginuid, ctx);
302 kfree(ctx);
303 } else
304 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100305 "audit_enabled=%d old=%d by auid=%u",
Steve Grubbce29b682006-04-01 18:29:34 -0500306 state, old, loginuid);
307 audit_enabled = state;
Serge E. Hallyn5d136a02006-04-27 16:45:14 -0500308 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
Steve Grubbce29b682006-04-01 18:29:34 -0500311static int audit_set_failure(int state, uid_t loginuid, u32 sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Steve Grubbce29b682006-04-01 18:29:34 -0500313 int old = audit_failure;
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (state != AUDIT_FAIL_SILENT
316 && state != AUDIT_FAIL_PRINTK
317 && state != AUDIT_FAIL_PANIC)
318 return -EINVAL;
Steve Grubbce29b682006-04-01 18:29:34 -0500319
320 if (sid) {
321 char *ctx = NULL;
322 u32 len;
323 int rc;
324 if ((rc = selinux_ctxid_to_string(sid, &ctx, &len)))
325 return rc;
326 else
327 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
328 "audit_failure=%d old=%d by auid=%u subj=%s",
329 state, old, loginuid, ctx);
330 kfree(ctx);
331 } else
332 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100333 "audit_failure=%d old=%d by auid=%u",
Steve Grubbce29b682006-04-01 18:29:34 -0500334 state, old, loginuid);
335 audit_failure = state;
Serge E. Hallyn5d136a02006-04-27 16:45:14 -0500336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Adrian Bunk97a41e22006-01-08 01:02:17 -0800339static int kauditd_thread(void *dummy)
David Woodhouseb7d11252005-05-19 10:56:58 +0100340{
341 struct sk_buff *skb;
342
343 while (1) {
344 skb = skb_dequeue(&audit_skb_queue);
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100345 wake_up(&audit_backlog_wait);
David Woodhouseb7d11252005-05-19 10:56:58 +0100346 if (skb) {
347 if (audit_pid) {
348 int err = netlink_unicast(audit_sock, skb, audit_pid, 0);
349 if (err < 0) {
350 BUG_ON(err != -ECONNREFUSED); /* Shoudn't happen */
351 printk(KERN_ERR "audit: *NO* daemon at audit_pid=%d\n", audit_pid);
352 audit_pid = 0;
353 }
354 } else {
David Woodhousee1b09eb2005-06-24 17:24:11 +0100355 printk(KERN_NOTICE "%s\n", skb->data + NLMSG_SPACE(0));
David Woodhouseb7d11252005-05-19 10:56:58 +0100356 kfree_skb(skb);
357 }
358 } else {
359 DECLARE_WAITQUEUE(wait, current);
360 set_current_state(TASK_INTERRUPTIBLE);
361 add_wait_queue(&kauditd_wait, &wait);
362
Pierre Ossman7a4ae742005-12-12 00:37:22 -0800363 if (!skb_queue_len(&audit_skb_queue)) {
364 try_to_freeze();
David Woodhouseb7d11252005-05-19 10:56:58 +0100365 schedule();
Pierre Ossman7a4ae742005-12-12 00:37:22 -0800366 }
David Woodhouseb7d11252005-05-19 10:56:58 +0100367
368 __set_current_state(TASK_RUNNING);
369 remove_wait_queue(&kauditd_wait, &wait);
370 }
371 }
372}
373
Al Viro9044e6b2006-05-22 01:09:24 -0400374int audit_send_list(void *_dest)
375{
376 struct audit_netlink_list *dest = _dest;
377 int pid = dest->pid;
378 struct sk_buff *skb;
379
380 /* wait for parent to finish and send an ACK */
Amy Griffisf368c07d2006-04-07 16:55:56 -0400381 mutex_lock(&audit_cmd_mutex);
382 mutex_unlock(&audit_cmd_mutex);
Al Viro9044e6b2006-05-22 01:09:24 -0400383
384 while ((skb = __skb_dequeue(&dest->q)) != NULL)
385 netlink_unicast(audit_sock, skb, pid, 0);
386
387 kfree(dest);
388
389 return 0;
390}
391
392struct sk_buff *audit_make_reply(int pid, int seq, int type, int done,
393 int multi, void *payload, int size)
394{
395 struct sk_buff *skb;
396 struct nlmsghdr *nlh;
397 int len = NLMSG_SPACE(size);
398 void *data;
399 int flags = multi ? NLM_F_MULTI : 0;
400 int t = done ? NLMSG_DONE : type;
401
402 skb = alloc_skb(len, GFP_KERNEL);
403 if (!skb)
404 return NULL;
405
406 nlh = NLMSG_PUT(skb, pid, seq, t, size);
407 nlh->nlmsg_flags = flags;
408 data = NLMSG_DATA(nlh);
409 memcpy(data, payload, size);
410 return skb;
411
412nlmsg_failure: /* Used by NLMSG_PUT */
413 if (skb)
414 kfree_skb(skb);
415 return NULL;
416}
417
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700418/**
419 * audit_send_reply - send an audit reply message via netlink
420 * @pid: process id to send reply to
421 * @seq: sequence number
422 * @type: audit message type
423 * @done: done (last) flag
424 * @multi: multi-part message flag
425 * @payload: payload data
426 * @size: payload size
427 *
428 * Allocates an skb, builds the netlink message, and sends it to the pid.
429 * No failure notifications.
430 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431void audit_send_reply(int pid, int seq, int type, int done, int multi,
432 void *payload, int size)
433{
434 struct sk_buff *skb;
Al Viro9044e6b2006-05-22 01:09:24 -0400435 skb = audit_make_reply(pid, seq, type, done, multi, payload, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 if (!skb)
David Woodhouseb7d11252005-05-19 10:56:58 +0100437 return;
David Woodhouseb7d11252005-05-19 10:56:58 +0100438 /* Ignore failure. It'll only happen if the sender goes away,
439 because our timeout is set to infinite. */
440 netlink_unicast(audit_sock, skb, pid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
444/*
445 * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
446 * control messages.
447 */
448static int audit_netlink_ok(kernel_cap_t eff_cap, u16 msg_type)
449{
450 int err = 0;
451
452 switch (msg_type) {
453 case AUDIT_GET:
454 case AUDIT_LIST:
Amy Griffis93315ed2006-02-07 12:05:27 -0500455 case AUDIT_LIST_RULES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 case AUDIT_SET:
457 case AUDIT_ADD:
Amy Griffis93315ed2006-02-07 12:05:27 -0500458 case AUDIT_ADD_RULE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 case AUDIT_DEL:
Amy Griffis93315ed2006-02-07 12:05:27 -0500460 case AUDIT_DEL_RULE:
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100461 case AUDIT_SIGNAL_INFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL))
463 err = -EPERM;
464 break;
Steve Grubb05474102005-05-21 00:18:37 +0100465 case AUDIT_USER:
David Woodhouse209aba02005-05-18 10:21:07 +0100466 case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
Steve Grubb90d526c2005-11-03 15:48:08 +0000467 case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (!cap_raised(eff_cap, CAP_AUDIT_WRITE))
469 err = -EPERM;
470 break;
471 default: /* bad msg */
472 err = -EINVAL;
473 }
474
475 return err;
476}
477
478static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
479{
Steve Grubbe7c34972006-04-03 09:08:13 -0400480 u32 uid, pid, seq, sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 void *data;
482 struct audit_status *status_get, status_set;
483 int err;
Steve Grubbc0404992005-05-13 18:17:42 +0100484 struct audit_buffer *ab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 u16 msg_type = nlh->nlmsg_type;
Serge Hallync94c2572005-04-29 16:27:17 +0100486 uid_t loginuid; /* loginuid of sender */
Al Viroe1396062006-05-25 10:19:47 -0400487 struct audit_sig_info *sig_data;
488 char *ctx;
489 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type);
492 if (err)
493 return err;
494
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700495 /* As soon as there's any sign of userspace auditd,
496 * start kauditd to talk to it */
David Woodhouseb7d11252005-05-19 10:56:58 +0100497 if (!kauditd_task)
498 kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd");
499 if (IS_ERR(kauditd_task)) {
500 err = PTR_ERR(kauditd_task);
501 kauditd_task = NULL;
502 return err;
503 }
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 pid = NETLINK_CREDS(skb)->pid;
506 uid = NETLINK_CREDS(skb)->uid;
Serge Hallync94c2572005-04-29 16:27:17 +0100507 loginuid = NETLINK_CB(skb).loginuid;
Steve Grubbe7c34972006-04-03 09:08:13 -0400508 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 seq = nlh->nlmsg_seq;
510 data = NLMSG_DATA(nlh);
511
512 switch (msg_type) {
513 case AUDIT_GET:
514 status_set.enabled = audit_enabled;
515 status_set.failure = audit_failure;
516 status_set.pid = audit_pid;
517 status_set.rate_limit = audit_rate_limit;
518 status_set.backlog_limit = audit_backlog_limit;
519 status_set.lost = atomic_read(&audit_lost);
David Woodhouseb7d11252005-05-19 10:56:58 +0100520 status_set.backlog = skb_queue_len(&audit_skb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
522 &status_set, sizeof(status_set));
523 break;
524 case AUDIT_SET:
525 if (nlh->nlmsg_len < sizeof(struct audit_status))
526 return -EINVAL;
527 status_get = (struct audit_status *)data;
528 if (status_get->mask & AUDIT_STATUS_ENABLED) {
Steve Grubbce29b682006-04-01 18:29:34 -0500529 err = audit_set_enabled(status_get->enabled,
530 loginuid, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (err < 0) return err;
532 }
533 if (status_get->mask & AUDIT_STATUS_FAILURE) {
Steve Grubbce29b682006-04-01 18:29:34 -0500534 err = audit_set_failure(status_get->failure,
535 loginuid, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (err < 0) return err;
537 }
538 if (status_get->mask & AUDIT_STATUS_PID) {
539 int old = audit_pid;
Steve Grubbce29b682006-04-01 18:29:34 -0500540 if (sid) {
Al Viroe1396062006-05-25 10:19:47 -0400541 if ((err = selinux_ctxid_to_string(
Steve Grubbce29b682006-04-01 18:29:34 -0500542 sid, &ctx, &len)))
Al Viroe1396062006-05-25 10:19:47 -0400543 return err;
Steve Grubbce29b682006-04-01 18:29:34 -0500544 else
545 audit_log(NULL, GFP_KERNEL,
546 AUDIT_CONFIG_CHANGE,
547 "audit_pid=%d old=%d by auid=%u subj=%s",
548 status_get->pid, old,
549 loginuid, ctx);
550 kfree(ctx);
551 } else
552 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
553 "audit_pid=%d old=%d by auid=%u",
554 status_get->pid, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 audit_pid = status_get->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 }
557 if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
Serge E. Hallyn5d136a02006-04-27 16:45:14 -0500558 err = audit_set_rate_limit(status_get->rate_limit,
Steve Grubbce29b682006-04-01 18:29:34 -0500559 loginuid, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
Serge E. Hallyn5d136a02006-04-27 16:45:14 -0500561 err = audit_set_backlog_limit(status_get->backlog_limit,
Steve Grubbce29b682006-04-01 18:29:34 -0500562 loginuid, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 break;
Steve Grubb05474102005-05-21 00:18:37 +0100564 case AUDIT_USER:
David Woodhouse209aba02005-05-18 10:21:07 +0100565 case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
Steve Grubb90d526c2005-11-03 15:48:08 +0000566 case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2:
David Woodhouse4a4cd632005-06-22 14:56:47 +0100567 if (!audit_enabled && msg_type != AUDIT_USER_AVC)
568 return 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100569
David Woodhouse5bb289b2005-06-24 14:14:05 +0100570 err = audit_filter_user(&NETLINK_CB(skb), msg_type);
David Woodhouse4a4cd632005-06-22 14:56:47 +0100571 if (err == 1) {
572 err = 0;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100573 ab = audit_log_start(NULL, GFP_KERNEL, msg_type);
David Woodhouse4a4cd632005-06-22 14:56:47 +0100574 if (ab) {
575 audit_log_format(ab,
Steve Grubbe7c34972006-04-03 09:08:13 -0400576 "user pid=%d uid=%u auid=%u",
577 pid, uid, loginuid);
578 if (sid) {
Steve Grubbe7c34972006-04-03 09:08:13 -0400579 if (selinux_ctxid_to_string(
580 sid, &ctx, &len)) {
581 audit_log_format(ab,
Steve Grubbce29b682006-04-01 18:29:34 -0500582 " ssid=%u", sid);
Steve Grubbe7c34972006-04-03 09:08:13 -0400583 /* Maybe call audit_panic? */
584 } else
585 audit_log_format(ab,
586 " subj=%s", ctx);
587 kfree(ctx);
588 }
589 audit_log_format(ab, " msg='%.1024s'",
590 (char *)data);
David Woodhouse4a4cd632005-06-22 14:56:47 +0100591 audit_set_pid(ab, pid);
592 audit_log_end(ab);
593 }
David Woodhouse0f45aa12005-06-19 19:35:50 +0100594 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 break;
596 case AUDIT_ADD:
597 case AUDIT_DEL:
Amy Griffis93315ed2006-02-07 12:05:27 -0500598 if (nlmsg_len(nlh) < sizeof(struct audit_rule))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return -EINVAL;
600 /* fallthrough */
601 case AUDIT_LIST:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
Amy Griffis93315ed2006-02-07 12:05:27 -0500603 uid, seq, data, nlmsg_len(nlh),
Steve Grubbce29b682006-04-01 18:29:34 -0500604 loginuid, sid);
Amy Griffis93315ed2006-02-07 12:05:27 -0500605 break;
606 case AUDIT_ADD_RULE:
607 case AUDIT_DEL_RULE:
608 if (nlmsg_len(nlh) < sizeof(struct audit_rule_data))
609 return -EINVAL;
610 /* fallthrough */
611 case AUDIT_LIST_RULES:
612 err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
613 uid, seq, data, nlmsg_len(nlh),
Steve Grubbce29b682006-04-01 18:29:34 -0500614 loginuid, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 break;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100616 case AUDIT_SIGNAL_INFO:
Al Viroe1396062006-05-25 10:19:47 -0400617 err = selinux_ctxid_to_string(audit_sig_sid, &ctx, &len);
618 if (err)
619 return err;
620 sig_data = kmalloc(sizeof(*sig_data) + len, GFP_KERNEL);
621 if (!sig_data) {
622 kfree(ctx);
623 return -ENOMEM;
624 }
625 sig_data->uid = audit_sig_uid;
626 sig_data->pid = audit_sig_pid;
627 memcpy(sig_data->ctx, ctx, len);
628 kfree(ctx);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100629 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO,
Al Viroe1396062006-05-25 10:19:47 -0400630 0, 0, sig_data, sizeof(*sig_data) + len);
631 kfree(sig_data);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100632 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 default:
634 err = -EINVAL;
635 break;
636 }
637
638 return err < 0 ? err : 0;
639}
640
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700641/*
642 * Get message from skb (based on rtnetlink_rcv_skb). Each message is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 * processed by audit_receive_msg. Malformed skbs with wrong length are
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700644 * discarded silently.
645 */
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700646static void audit_receive_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
648 int err;
649 struct nlmsghdr *nlh;
650 u32 rlen;
651
652 while (skb->len >= NLMSG_SPACE(0)) {
653 nlh = (struct nlmsghdr *)skb->data;
654 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700655 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
657 if (rlen > skb->len)
658 rlen = skb->len;
659 if ((err = audit_receive_msg(skb, nlh))) {
660 netlink_ack(skb, nlh, err);
661 } else if (nlh->nlmsg_flags & NLM_F_ACK)
662 netlink_ack(skb, nlh, 0);
663 skb_pull(skb, rlen);
664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
667/* Receive messages from netlink socket. */
668static void audit_receive(struct sock *sk, int length)
669{
670 struct sk_buff *skb;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700671 unsigned int qlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Amy Griffisf368c07d2006-04-07 16:55:56 -0400673 mutex_lock(&audit_cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700675 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
676 skb = skb_dequeue(&sk->sk_receive_queue);
677 audit_receive_skb(skb);
678 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
Amy Griffisf368c07d2006-04-07 16:55:56 -0400680 mutex_unlock(&audit_cmd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
Amy Griffisf368c07d2006-04-07 16:55:56 -0400683#ifdef CONFIG_AUDITSYSCALL
684static const struct inotify_operations audit_inotify_ops = {
685 .handle_event = audit_handle_ievent,
686 .destroy_watch = audit_free_parent,
687};
688#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
690/* Initialize audit support at boot time. */
691static int __init audit_init(void)
692{
Amy Griffisf368c07d2006-04-07 16:55:56 -0400693#ifdef CONFIG_AUDITSYSCALL
694 int i;
695#endif
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
698 audit_default ? "enabled" : "disabled");
Patrick McHardy06628602005-08-15 12:33:26 -0700699 audit_sock = netlink_kernel_create(NETLINK_AUDIT, 0, audit_receive,
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700700 THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 if (!audit_sock)
702 audit_panic("cannot initialize netlink socket");
Amy Griffis71e1c782006-03-06 22:40:05 -0500703 else
704 audit_sock->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
David Woodhouseb7d11252005-05-19 10:56:58 +0100706 skb_queue_head_init(&audit_skb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 audit_initialized = 1;
708 audit_enabled = audit_default;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600709
710 /* Register the callback with selinux. This callback will be invoked
711 * when a new policy is loaded. */
712 selinux_audit_set_callback(&selinux_audit_rule_update);
713
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100714 audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, "initialized");
Amy Griffisf368c07d2006-04-07 16:55:56 -0400715
716#ifdef CONFIG_AUDITSYSCALL
717 audit_ih = inotify_init(&audit_inotify_ops);
718 if (IS_ERR(audit_ih))
719 audit_panic("cannot initialize inotify handle");
720
721 for (i = 0; i < AUDIT_INODE_BUCKETS; i++)
722 INIT_LIST_HEAD(&audit_inode_hash[i]);
723#endif
724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return 0;
726}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727__initcall(audit_init);
728
729/* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
730static int __init audit_enable(char *str)
731{
732 audit_default = !!simple_strtol(str, NULL, 0);
733 printk(KERN_INFO "audit: %s%s\n",
734 audit_default ? "enabled" : "disabled",
735 audit_initialized ? "" : " (after initialization)");
736 if (audit_initialized)
737 audit_enabled = audit_default;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800738 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739}
740
741__setup("audit=", audit_enable);
742
Chris Wright16e19042005-05-06 15:53:34 +0100743static void audit_buffer_free(struct audit_buffer *ab)
744{
745 unsigned long flags;
746
Chris Wright8fc61152005-05-06 15:54:17 +0100747 if (!ab)
748 return;
749
Chris Wright5ac52f32005-05-06 15:54:53 +0100750 if (ab->skb)
751 kfree_skb(ab->skb);
David Woodhouseb7d11252005-05-19 10:56:58 +0100752
Chris Wright16e19042005-05-06 15:53:34 +0100753 spin_lock_irqsave(&audit_freelist_lock, flags);
Serge E. Hallyn5d136a02006-04-27 16:45:14 -0500754 if (audit_freelist_count > AUDIT_MAXFREE)
Chris Wright16e19042005-05-06 15:53:34 +0100755 kfree(ab);
Serge E. Hallyn5d136a02006-04-27 16:45:14 -0500756 else {
757 audit_freelist_count++;
Chris Wright16e19042005-05-06 15:53:34 +0100758 list_add(&ab->list, &audit_freelist);
Serge E. Hallyn5d136a02006-04-27 16:45:14 -0500759 }
Chris Wright16e19042005-05-06 15:53:34 +0100760 spin_unlock_irqrestore(&audit_freelist_lock, flags);
761}
762
Steve Grubbc0404992005-05-13 18:17:42 +0100763static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
Al Virodd0fc662005-10-07 07:46:04 +0100764 gfp_t gfp_mask, int type)
Chris Wright16e19042005-05-06 15:53:34 +0100765{
766 unsigned long flags;
767 struct audit_buffer *ab = NULL;
Steve Grubbc0404992005-05-13 18:17:42 +0100768 struct nlmsghdr *nlh;
Chris Wright16e19042005-05-06 15:53:34 +0100769
770 spin_lock_irqsave(&audit_freelist_lock, flags);
771 if (!list_empty(&audit_freelist)) {
772 ab = list_entry(audit_freelist.next,
773 struct audit_buffer, list);
774 list_del(&ab->list);
775 --audit_freelist_count;
776 }
777 spin_unlock_irqrestore(&audit_freelist_lock, flags);
778
779 if (!ab) {
David Woodhouse4332bdd2005-05-06 15:59:57 +0100780 ab = kmalloc(sizeof(*ab), gfp_mask);
Chris Wright16e19042005-05-06 15:53:34 +0100781 if (!ab)
Chris Wright8fc61152005-05-06 15:54:17 +0100782 goto err;
Chris Wright16e19042005-05-06 15:53:34 +0100783 }
Chris Wright8fc61152005-05-06 15:54:17 +0100784
David Woodhouse4332bdd2005-05-06 15:59:57 +0100785 ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask);
Chris Wright5ac52f32005-05-06 15:54:53 +0100786 if (!ab->skb)
Chris Wright8fc61152005-05-06 15:54:17 +0100787 goto err;
788
David Woodhouseb7d11252005-05-19 10:56:58 +0100789 ab->ctx = ctx;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100790 ab->gfp_mask = gfp_mask;
Steve Grubbc0404992005-05-13 18:17:42 +0100791 nlh = (struct nlmsghdr *)skb_put(ab->skb, NLMSG_SPACE(0));
792 nlh->nlmsg_type = type;
793 nlh->nlmsg_flags = 0;
794 nlh->nlmsg_pid = 0;
795 nlh->nlmsg_seq = 0;
Chris Wright16e19042005-05-06 15:53:34 +0100796 return ab;
Chris Wright8fc61152005-05-06 15:54:17 +0100797err:
798 audit_buffer_free(ab);
799 return NULL;
Chris Wright16e19042005-05-06 15:53:34 +0100800}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700802/**
803 * audit_serial - compute a serial number for the audit record
804 *
805 * Compute a serial number for the audit record. Audit records are
David Woodhousebfb44962005-05-21 21:08:09 +0100806 * written to user-space as soon as they are generated, so a complete
807 * audit record may be written in several pieces. The timestamp of the
808 * record and this serial number are used by the user-space tools to
809 * determine which pieces belong to the same audit record. The
810 * (timestamp,serial) tuple is unique for each syscall and is live from
811 * syscall entry to syscall exit.
812 *
David Woodhousebfb44962005-05-21 21:08:09 +0100813 * NOTE: Another possibility is to store the formatted records off the
814 * audit context (for those records that have a context), and emit them
815 * all at syscall exit. However, this could delay the reporting of
816 * significant errors until syscall exit (or never, if the system
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700817 * halts).
818 */
David Woodhousebfb44962005-05-21 21:08:09 +0100819unsigned int audit_serial(void)
820{
David Woodhoused5b454f2005-07-15 12:56:03 +0100821 static spinlock_t serial_lock = SPIN_LOCK_UNLOCKED;
822 static unsigned int serial = 0;
David Woodhousebfb44962005-05-21 21:08:09 +0100823
David Woodhoused5b454f2005-07-15 12:56:03 +0100824 unsigned long flags;
825 unsigned int ret;
David Woodhousebfb44962005-05-21 21:08:09 +0100826
David Woodhoused5b454f2005-07-15 12:56:03 +0100827 spin_lock_irqsave(&serial_lock, flags);
David Woodhousebfb44962005-05-21 21:08:09 +0100828 do {
David Woodhousece625a82005-07-18 14:24:46 -0400829 ret = ++serial;
830 } while (unlikely(!ret));
David Woodhoused5b454f2005-07-15 12:56:03 +0100831 spin_unlock_irqrestore(&serial_lock, flags);
David Woodhousebfb44962005-05-21 21:08:09 +0100832
David Woodhoused5b454f2005-07-15 12:56:03 +0100833 return ret;
David Woodhousebfb44962005-05-21 21:08:09 +0100834}
835
836static inline void audit_get_stamp(struct audit_context *ctx,
837 struct timespec *t, unsigned int *serial)
838{
839 if (ctx)
840 auditsc_get_stamp(ctx, t, serial);
841 else {
842 *t = CURRENT_TIME;
843 *serial = audit_serial();
844 }
845}
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847/* Obtain an audit buffer. This routine does locking to obtain the
848 * audit buffer, but then no locking is required for calls to
849 * audit_log_*format. If the tsk is a task that is currently in a
850 * syscall, then the syscall is marked as auditable and an audit record
851 * will be written at syscall exit. If there is no associated task, tsk
852 * should be NULL. */
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100853
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700854/**
855 * audit_log_start - obtain an audit buffer
856 * @ctx: audit_context (may be NULL)
857 * @gfp_mask: type of allocation
858 * @type: audit message type
859 *
860 * Returns audit_buffer pointer on success or NULL on error.
861 *
862 * Obtain an audit buffer. This routine does locking to obtain the
863 * audit buffer, but then no locking is required for calls to
864 * audit_log_*format. If the task (ctx) is a task that is currently in a
865 * syscall, then the syscall is marked as auditable and an audit record
866 * will be written at syscall exit. If there is no associated task, then
867 * task context (ctx) should be NULL.
868 */
Al Viro9796fdd2005-10-21 03:22:03 -0400869struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100870 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 struct audit_buffer *ab = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 struct timespec t;
Steve Grubbd812ddb2005-04-29 16:09:52 +0100874 unsigned int serial;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100875 int reserve;
David Woodhouseac4cec42005-07-02 14:08:48 +0100876 unsigned long timeout_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
878 if (!audit_initialized)
879 return NULL;
880
Dustin Kirklandc8edc802005-11-03 16:12:36 +0000881 if (unlikely(audit_filter_type(type)))
882 return NULL;
883
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100884 if (gfp_mask & __GFP_WAIT)
885 reserve = 0;
886 else
887 reserve = 5; /* Allow atomic callers to go up to five
888 entries over the normal backlog limit */
889
890 while (audit_backlog_limit
891 && skb_queue_len(&audit_skb_queue) > audit_backlog_limit + reserve) {
David Woodhouseac4cec42005-07-02 14:08:48 +0100892 if (gfp_mask & __GFP_WAIT && audit_backlog_wait_time
893 && time_before(jiffies, timeout_start + audit_backlog_wait_time)) {
894
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100895 /* Wait for auditd to drain the queue a little */
896 DECLARE_WAITQUEUE(wait, current);
897 set_current_state(TASK_INTERRUPTIBLE);
898 add_wait_queue(&audit_backlog_wait, &wait);
899
900 if (audit_backlog_limit &&
901 skb_queue_len(&audit_skb_queue) > audit_backlog_limit)
David Woodhouseac4cec42005-07-02 14:08:48 +0100902 schedule_timeout(timeout_start + audit_backlog_wait_time - jiffies);
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100903
904 __set_current_state(TASK_RUNNING);
905 remove_wait_queue(&audit_backlog_wait, &wait);
David Woodhouseac4cec42005-07-02 14:08:48 +0100906 continue;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100907 }
David Woodhousefb19b4c2005-05-19 14:55:56 +0100908 if (audit_rate_check())
909 printk(KERN_WARNING
910 "audit: audit_backlog=%d > "
911 "audit_backlog_limit=%d\n",
912 skb_queue_len(&audit_skb_queue),
913 audit_backlog_limit);
914 audit_log_lost("backlog limit exceeded");
David Woodhouseac4cec42005-07-02 14:08:48 +0100915 audit_backlog_wait_time = audit_backlog_wait_overflow;
916 wake_up(&audit_backlog_wait);
David Woodhousefb19b4c2005-05-19 14:55:56 +0100917 return NULL;
918 }
919
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100920 ab = audit_buffer_alloc(ctx, gfp_mask, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (!ab) {
922 audit_log_lost("out of memory in audit_log_start");
923 return NULL;
924 }
925
David Woodhousebfb44962005-05-21 21:08:09 +0100926 audit_get_stamp(ab->ctx, &t, &serial);
Chris Wright197c69c2005-05-11 10:54:05 +0100927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 audit_log_format(ab, "audit(%lu.%03lu:%u): ",
929 t.tv_sec, t.tv_nsec/1000000, serial);
930 return ab;
931}
932
Chris Wright8fc61152005-05-06 15:54:17 +0100933/**
Chris Wright5ac52f32005-05-06 15:54:53 +0100934 * audit_expand - expand skb in the audit buffer
Chris Wright8fc61152005-05-06 15:54:17 +0100935 * @ab: audit_buffer
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700936 * @extra: space to add at tail of the skb
Chris Wright8fc61152005-05-06 15:54:17 +0100937 *
938 * Returns 0 (no space) on failed expansion, or available space if
939 * successful.
940 */
David Woodhousee3b926b2005-05-10 18:56:08 +0100941static inline int audit_expand(struct audit_buffer *ab, int extra)
Chris Wright8fc61152005-05-06 15:54:17 +0100942{
Chris Wright5ac52f32005-05-06 15:54:53 +0100943 struct sk_buff *skb = ab->skb;
David Woodhousee3b926b2005-05-10 18:56:08 +0100944 int ret = pskb_expand_head(skb, skb_headroom(skb), extra,
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100945 ab->gfp_mask);
Chris Wright5ac52f32005-05-06 15:54:53 +0100946 if (ret < 0) {
947 audit_log_lost("out of memory in audit_expand");
Chris Wright8fc61152005-05-06 15:54:17 +0100948 return 0;
Chris Wright5ac52f32005-05-06 15:54:53 +0100949 }
950 return skb_tailroom(skb);
Chris Wright8fc61152005-05-06 15:54:17 +0100951}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700953/*
954 * Format an audit message into the audit buffer. If there isn't enough
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 * room in the audit buffer, more room will be allocated and vsnprint
956 * will be called a second time. Currently, we assume that a printk
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700957 * can't format message larger than 1024 bytes, so we don't either.
958 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
960 va_list args)
961{
962 int len, avail;
Chris Wright5ac52f32005-05-06 15:54:53 +0100963 struct sk_buff *skb;
David Woodhouseeecb0a72005-05-10 18:58:51 +0100964 va_list args2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965
966 if (!ab)
967 return;
968
Chris Wright5ac52f32005-05-06 15:54:53 +0100969 BUG_ON(!ab->skb);
970 skb = ab->skb;
971 avail = skb_tailroom(skb);
972 if (avail == 0) {
David Woodhousee3b926b2005-05-10 18:56:08 +0100973 avail = audit_expand(ab, AUDIT_BUFSIZ);
Chris Wright8fc61152005-05-06 15:54:17 +0100974 if (!avail)
975 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
David Woodhouseeecb0a72005-05-10 18:58:51 +0100977 va_copy(args2, args);
Chris Wright5ac52f32005-05-06 15:54:53 +0100978 len = vsnprintf(skb->tail, avail, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (len >= avail) {
980 /* The printk buffer is 1024 bytes long, so if we get
981 * here and AUDIT_BUFSIZ is at least 1024, then we can
982 * log everything that printk could have logged. */
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700983 avail = audit_expand(ab,
984 max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
Chris Wright8fc61152005-05-06 15:54:17 +0100985 if (!avail)
986 goto out;
David Woodhouseeecb0a72005-05-10 18:58:51 +0100987 len = vsnprintf(skb->tail, avail, fmt, args2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
Steve Grubb168b7172005-05-19 10:24:22 +0100989 if (len > 0)
990 skb_put(skb, len);
Chris Wright8fc61152005-05-06 15:54:17 +0100991out:
992 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700995/**
996 * audit_log_format - format a message into the audit buffer.
997 * @ab: audit_buffer
998 * @fmt: format string
999 * @...: optional parameters matching @fmt string
1000 *
1001 * All the work is done in audit_log_vformat.
1002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
1004{
1005 va_list args;
1006
1007 if (!ab)
1008 return;
1009 va_start(args, fmt);
1010 audit_log_vformat(ab, fmt, args);
1011 va_end(args);
1012}
1013
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001014/**
1015 * audit_log_hex - convert a buffer to hex and append it to the audit skb
1016 * @ab: the audit_buffer
1017 * @buf: buffer to convert to hex
1018 * @len: length of @buf to be converted
1019 *
1020 * No return value; failure to expand is silently ignored.
1021 *
1022 * This function will take the passed buf and convert it into a string of
1023 * ascii hex digits. The new string is placed onto the skb.
1024 */
1025void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf,
Steve Grubb168b7172005-05-19 10:24:22 +01001026 size_t len)
83c7d092005-04-29 15:54:44 +01001027{
Steve Grubb168b7172005-05-19 10:24:22 +01001028 int i, avail, new_len;
1029 unsigned char *ptr;
1030 struct sk_buff *skb;
1031 static const unsigned char *hex = "0123456789ABCDEF";
83c7d092005-04-29 15:54:44 +01001032
Steve Grubb168b7172005-05-19 10:24:22 +01001033 BUG_ON(!ab->skb);
1034 skb = ab->skb;
1035 avail = skb_tailroom(skb);
1036 new_len = len<<1;
1037 if (new_len >= avail) {
1038 /* Round the buffer request up to the next multiple */
1039 new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
1040 avail = audit_expand(ab, new_len);
1041 if (!avail)
1042 return;
1043 }
1044
1045 ptr = skb->tail;
1046 for (i=0; i<len; i++) {
1047 *ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */
1048 *ptr++ = hex[buf[i] & 0x0F]; /* Lower nibble */
1049 }
1050 *ptr = 0;
1051 skb_put(skb, len << 1); /* new string is twice the old string */
83c7d092005-04-29 15:54:44 +01001052}
1053
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001054/**
1055 * audit_log_unstrustedstring - log a string that may contain random characters
1056 * @ab: audit_buffer
1057 * @string: string to be logged
1058 *
1059 * This code will escape a string that is passed to it if the string
1060 * contains a control character, unprintable character, double quote mark,
Steve Grubb168b7172005-05-19 10:24:22 +01001061 * or a space. Unescaped strings will start and end with a double quote mark.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001062 * Strings that are escaped are printed in hex (2 digits per char).
1063 */
Al Viro473ae302006-04-26 14:04:08 -04001064const char *audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
83c7d092005-04-29 15:54:44 +01001065{
Andrew Morton81b78542005-04-29 15:59:11 +01001066 const unsigned char *p = string;
Al Viro473ae302006-04-26 14:04:08 -04001067 size_t len = strlen(string);
83c7d092005-04-29 15:54:44 +01001068
1069 while (*p) {
Steve Grubb168b7172005-05-19 10:24:22 +01001070 if (*p == '"' || *p < 0x21 || *p > 0x7f) {
Al Viro473ae302006-04-26 14:04:08 -04001071 audit_log_hex(ab, string, len);
1072 return string + len + 1;
83c7d092005-04-29 15:54:44 +01001073 }
1074 p++;
1075 }
1076 audit_log_format(ab, "\"%s\"", string);
Al Viro473ae302006-04-26 14:04:08 -04001077 return p + 1;
83c7d092005-04-29 15:54:44 +01001078}
1079
Steve Grubb168b7172005-05-19 10:24:22 +01001080/* This is a helper-function to print the escaped d_path */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
1082 struct dentry *dentry, struct vfsmount *vfsmnt)
1083{
Steve Grubb168b7172005-05-19 10:24:22 +01001084 char *p, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Chris Wright8fc61152005-05-06 15:54:17 +01001086 if (prefix)
1087 audit_log_format(ab, " %s", prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Steve Grubb168b7172005-05-19 10:24:22 +01001089 /* We will allow 11 spaces for ' (deleted)' to be appended */
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001090 path = kmalloc(PATH_MAX+11, ab->gfp_mask);
Steve Grubb168b7172005-05-19 10:24:22 +01001091 if (!path) {
1092 audit_log_format(ab, "<no memory>");
1093 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
Steve Grubb168b7172005-05-19 10:24:22 +01001095 p = d_path(dentry, vfsmnt, path, PATH_MAX+11);
1096 if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
1097 /* FIXME: can we save some information here? */
1098 audit_log_format(ab, "<too long>");
1099 } else
1100 audit_log_untrustedstring(ab, p);
1101 kfree(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102}
1103
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001104/**
1105 * audit_log_end - end one audit record
1106 * @ab: the audit_buffer
1107 *
1108 * The netlink_* functions cannot be called inside an irq context, so
1109 * the audit buffer is placed on a queue and a tasklet is scheduled to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 * remove them from the queue outside the irq context. May be called in
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001111 * any context.
1112 */
David Woodhouseb7d11252005-05-19 10:56:58 +01001113void audit_log_end(struct audit_buffer *ab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (!ab)
1116 return;
1117 if (!audit_rate_check()) {
1118 audit_log_lost("rate limit exceeded");
1119 } else {
David Woodhouseb7d11252005-05-19 10:56:58 +01001120 if (audit_pid) {
1121 struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
1122 nlh->nlmsg_len = ab->skb->len - NLMSG_SPACE(0);
1123 skb_queue_tail(&audit_skb_queue, ab->skb);
1124 ab->skb = NULL;
1125 wake_up_interruptible(&kauditd_wait);
1126 } else {
David Woodhousee1b09eb2005-06-24 17:24:11 +01001127 printk(KERN_NOTICE "%s\n", ab->skb->data + NLMSG_SPACE(0));
David Woodhouseb7d11252005-05-19 10:56:58 +01001128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 }
Chris Wright16e19042005-05-06 15:53:34 +01001130 audit_buffer_free(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131}
1132
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001133/**
1134 * audit_log - Log an audit record
1135 * @ctx: audit context
1136 * @gfp_mask: type of allocation
1137 * @type: audit message type
1138 * @fmt: format string to use
1139 * @...: variable parameters matching the format string
1140 *
1141 * This is a convenience function that calls audit_log_start,
1142 * audit_log_vformat, and audit_log_end. It may be called
1143 * in any context.
1144 */
Al Viro9796fdd2005-10-21 03:22:03 -04001145void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001146 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147{
1148 struct audit_buffer *ab;
1149 va_list args;
1150
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001151 ab = audit_log_start(ctx, gfp_mask, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 if (ab) {
1153 va_start(args, fmt);
1154 audit_log_vformat(ab, fmt, args);
1155 va_end(args);
1156 audit_log_end(ab);
1157 }
1158}
lorenzo@gnu.orgbf45da92006-03-09 00:33:47 +01001159
1160EXPORT_SYMBOL(audit_log_start);
1161EXPORT_SYMBOL(audit_log_end);
1162EXPORT_SYMBOL(audit_log_format);
1163EXPORT_SYMBOL(audit_log);