blob: 9060be750c48151f6f61a67c9f0eb344fb11a583 [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>
59
60#include "audit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/* No auditing will take place until audit_initialized != 0.
63 * (Initialization happens after skb_init is called.) */
64static int audit_initialized;
65
66/* No syscall auditing will take place unless audit_enabled != 0. */
67int audit_enabled;
68
69/* Default state when kernel boots without any parameters. */
70static int audit_default;
71
72/* If auditing cannot proceed, audit_failure selects what happens. */
73static int audit_failure = AUDIT_FAIL_PRINTK;
74
75/* If audit records are to be written to the netlink socket, audit_pid
76 * contains the (non-zero) pid. */
Steve Grubbc2f0c7c2005-05-06 12:38:39 +010077int audit_pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Randy Dunlapb0dd25a2005-09-13 12:47:11 -070079/* If audit_rate_limit is non-zero, limit the rate of sending audit records
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * to that number per second. This prevents DoS attacks, but results in
81 * audit records being dropped. */
82static int audit_rate_limit;
83
84/* Number of outstanding audit_buffers allowed. */
85static int audit_backlog_limit = 64;
David Woodhouseac4cec42005-07-02 14:08:48 +010086static int audit_backlog_wait_time = 60 * HZ;
87static int audit_backlog_wait_overflow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Steve Grubbc2f0c7c2005-05-06 12:38:39 +010089/* The identity of the user shutting down the audit system. */
90uid_t audit_sig_uid = -1;
91pid_t audit_sig_pid = -1;
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093/* Records can be lost in several ways:
94 0) [suppressed in audit_alloc]
95 1) out of memory in audit_log_start [kmalloc of struct audit_buffer]
96 2) out of memory in audit_log_move [alloc_skb]
97 3) suppressed due to audit_rate_limit
98 4) suppressed due to audit_backlog_limit
99*/
100static atomic_t audit_lost = ATOMIC_INIT(0);
101
102/* The netlink socket. */
103static struct sock *audit_sock;
104
David Woodhouseb7d11252005-05-19 10:56:58 +0100105/* The audit_freelist is a list of pre-allocated audit buffers (if more
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of
107 * being placed on the freelist). */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108static DEFINE_SPINLOCK(audit_freelist_lock);
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700109static int audit_freelist_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static LIST_HEAD(audit_freelist);
111
David Woodhouseb7d11252005-05-19 10:56:58 +0100112static struct sk_buff_head audit_skb_queue;
113static struct task_struct *kauditd_task;
114static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait);
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100115static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117/* The netlink socket is only to be read by 1 CPU, which lets us assume
Steve Grubb23f32d12005-05-13 18:35:15 +0100118 * that list additions and deletions never happen simultaneously in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 * auditsc.c */
Ingo Molnar5a0bbce2006-03-07 23:51:38 -0800120DEFINE_MUTEX(audit_netlink_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122/* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
123 * audit records. Since printk uses a 1024 byte buffer, this buffer
124 * should be at least that large. */
125#define AUDIT_BUFSIZ 1024
126
127/* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the
128 * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */
129#define AUDIT_MAXFREE (2*NR_CPUS)
130
131/* The audit_buffer is used when formatting an audit record. The caller
132 * locks briefly to get the record off the freelist or to allocate the
133 * buffer, and locks briefly to send the buffer to the netlink layer or
134 * to place it on a transmit queue. Multiple audit_buffers can be in
135 * use simultaneously. */
136struct audit_buffer {
137 struct list_head list;
Chris Wright8fc61152005-05-06 15:54:17 +0100138 struct sk_buff *skb; /* formatted skb ready to send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 struct audit_context *ctx; /* NULL or associated context */
Al Viro9796fdd2005-10-21 03:22:03 -0400140 gfp_t gfp_mask;
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
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000149void audit_panic(const char *message)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
151 switch (audit_failure)
152 {
153 case AUDIT_FAIL_SILENT:
154 break;
155 case AUDIT_FAIL_PRINTK:
156 printk(KERN_ERR "audit: %s\n", message);
157 break;
158 case AUDIT_FAIL_PANIC:
159 panic("audit: %s\n", message);
160 break;
161 }
162}
163
164static inline int audit_rate_check(void)
165{
166 static unsigned long last_check = 0;
167 static int messages = 0;
168 static DEFINE_SPINLOCK(lock);
169 unsigned long flags;
170 unsigned long now;
171 unsigned long elapsed;
172 int retval = 0;
173
174 if (!audit_rate_limit) return 1;
175
176 spin_lock_irqsave(&lock, flags);
177 if (++messages < audit_rate_limit) {
178 retval = 1;
179 } else {
180 now = jiffies;
181 elapsed = now - last_check;
182 if (elapsed > HZ) {
183 last_check = now;
184 messages = 0;
185 retval = 1;
186 }
187 }
188 spin_unlock_irqrestore(&lock, flags);
189
190 return retval;
191}
192
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700193/**
194 * audit_log_lost - conditionally log lost audit message event
195 * @message: the message stating reason for lost audit message
196 *
197 * Emit at least 1 message per second, even if audit_rate_check is
198 * throttling.
199 * Always increment the lost messages counter.
200*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201void audit_log_lost(const char *message)
202{
203 static unsigned long last_msg = 0;
204 static DEFINE_SPINLOCK(lock);
205 unsigned long flags;
206 unsigned long now;
207 int print;
208
209 atomic_inc(&audit_lost);
210
211 print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);
212
213 if (!print) {
214 spin_lock_irqsave(&lock, flags);
215 now = jiffies;
216 if (now - last_msg > HZ) {
217 print = 1;
218 last_msg = now;
219 }
220 spin_unlock_irqrestore(&lock, flags);
221 }
222
223 if (print) {
224 printk(KERN_WARNING
David Woodhouseb7d11252005-05-19 10:56:58 +0100225 "audit: audit_lost=%d audit_rate_limit=%d audit_backlog_limit=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 atomic_read(&audit_lost),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 audit_rate_limit,
228 audit_backlog_limit);
229 audit_panic(message);
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
Serge Hallync94c2572005-04-29 16:27:17 +0100233static int audit_set_rate_limit(int limit, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 int old = audit_rate_limit;
236 audit_rate_limit = limit;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100237 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100238 "audit_rate_limit=%d old=%d by auid=%u",
Serge Hallync94c2572005-04-29 16:27:17 +0100239 audit_rate_limit, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return old;
241}
242
Serge Hallync94c2572005-04-29 16:27:17 +0100243static int audit_set_backlog_limit(int limit, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 int old = audit_backlog_limit;
246 audit_backlog_limit = limit;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100247 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100248 "audit_backlog_limit=%d old=%d by auid=%u",
Serge Hallync94c2572005-04-29 16:27:17 +0100249 audit_backlog_limit, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return old;
251}
252
Serge Hallync94c2572005-04-29 16:27:17 +0100253static int audit_set_enabled(int state, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 int old = audit_enabled;
256 if (state != 0 && state != 1)
257 return -EINVAL;
258 audit_enabled = state;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100259 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100260 "audit_enabled=%d old=%d by auid=%u",
Steve Grubbc0404992005-05-13 18:17:42 +0100261 audit_enabled, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return old;
263}
264
Serge Hallync94c2572005-04-29 16:27:17 +0100265static int audit_set_failure(int state, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
267 int old = audit_failure;
268 if (state != AUDIT_FAIL_SILENT
269 && state != AUDIT_FAIL_PRINTK
270 && state != AUDIT_FAIL_PANIC)
271 return -EINVAL;
272 audit_failure = state;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100273 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100274 "audit_failure=%d old=%d by auid=%u",
Steve Grubbc0404992005-05-13 18:17:42 +0100275 audit_failure, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return old;
277}
278
Adrian Bunk97a41e22006-01-08 01:02:17 -0800279static int kauditd_thread(void *dummy)
David Woodhouseb7d11252005-05-19 10:56:58 +0100280{
281 struct sk_buff *skb;
282
283 while (1) {
284 skb = skb_dequeue(&audit_skb_queue);
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100285 wake_up(&audit_backlog_wait);
David Woodhouseb7d11252005-05-19 10:56:58 +0100286 if (skb) {
287 if (audit_pid) {
288 int err = netlink_unicast(audit_sock, skb, audit_pid, 0);
289 if (err < 0) {
290 BUG_ON(err != -ECONNREFUSED); /* Shoudn't happen */
291 printk(KERN_ERR "audit: *NO* daemon at audit_pid=%d\n", audit_pid);
292 audit_pid = 0;
293 }
294 } else {
David Woodhousee1b09eb2005-06-24 17:24:11 +0100295 printk(KERN_NOTICE "%s\n", skb->data + NLMSG_SPACE(0));
David Woodhouseb7d11252005-05-19 10:56:58 +0100296 kfree_skb(skb);
297 }
298 } else {
299 DECLARE_WAITQUEUE(wait, current);
300 set_current_state(TASK_INTERRUPTIBLE);
301 add_wait_queue(&kauditd_wait, &wait);
302
Pierre Ossman7a4ae742005-12-12 00:37:22 -0800303 if (!skb_queue_len(&audit_skb_queue)) {
304 try_to_freeze();
David Woodhouseb7d11252005-05-19 10:56:58 +0100305 schedule();
Pierre Ossman7a4ae742005-12-12 00:37:22 -0800306 }
David Woodhouseb7d11252005-05-19 10:56:58 +0100307
308 __set_current_state(TASK_RUNNING);
309 remove_wait_queue(&kauditd_wait, &wait);
310 }
311 }
David Woodhousefe7752b2005-12-15 18:33:52 +0000312 return 0;
David Woodhouseb7d11252005-05-19 10:56:58 +0100313}
314
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700315/**
316 * audit_send_reply - send an audit reply message via netlink
317 * @pid: process id to send reply to
318 * @seq: sequence number
319 * @type: audit message type
320 * @done: done (last) flag
321 * @multi: multi-part message flag
322 * @payload: payload data
323 * @size: payload size
324 *
325 * Allocates an skb, builds the netlink message, and sends it to the pid.
326 * No failure notifications.
327 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328void audit_send_reply(int pid, int seq, int type, int done, int multi,
329 void *payload, int size)
330{
331 struct sk_buff *skb;
332 struct nlmsghdr *nlh;
333 int len = NLMSG_SPACE(size);
334 void *data;
335 int flags = multi ? NLM_F_MULTI : 0;
336 int t = done ? NLMSG_DONE : type;
337
338 skb = alloc_skb(len, GFP_KERNEL);
339 if (!skb)
David Woodhouseb7d11252005-05-19 10:56:58 +0100340 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
David Woodhouseb7d11252005-05-19 10:56:58 +0100342 nlh = NLMSG_PUT(skb, pid, seq, t, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 nlh->nlmsg_flags = flags;
344 data = NLMSG_DATA(nlh);
345 memcpy(data, payload, size);
David Woodhouseb7d11252005-05-19 10:56:58 +0100346
347 /* Ignore failure. It'll only happen if the sender goes away,
348 because our timeout is set to infinite. */
349 netlink_unicast(audit_sock, skb, pid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 return;
351
352nlmsg_failure: /* Used by NLMSG_PUT */
353 if (skb)
354 kfree_skb(skb);
355}
356
357/*
358 * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
359 * control messages.
360 */
361static int audit_netlink_ok(kernel_cap_t eff_cap, u16 msg_type)
362{
363 int err = 0;
364
365 switch (msg_type) {
366 case AUDIT_GET:
367 case AUDIT_LIST:
Amy Griffis93315ed2006-02-07 12:05:27 -0500368 case AUDIT_LIST_RULES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 case AUDIT_SET:
370 case AUDIT_ADD:
Amy Griffis93315ed2006-02-07 12:05:27 -0500371 case AUDIT_ADD_RULE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 case AUDIT_DEL:
Amy Griffis93315ed2006-02-07 12:05:27 -0500373 case AUDIT_DEL_RULE:
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100374 case AUDIT_SIGNAL_INFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL))
376 err = -EPERM;
377 break;
Steve Grubb05474102005-05-21 00:18:37 +0100378 case AUDIT_USER:
David Woodhouse209aba02005-05-18 10:21:07 +0100379 case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
Steve Grubb90d526c2005-11-03 15:48:08 +0000380 case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 if (!cap_raised(eff_cap, CAP_AUDIT_WRITE))
382 err = -EPERM;
383 break;
384 default: /* bad msg */
385 err = -EINVAL;
386 }
387
388 return err;
389}
390
391static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
392{
393 u32 uid, pid, seq;
394 void *data;
395 struct audit_status *status_get, status_set;
396 int err;
Steve Grubbc0404992005-05-13 18:17:42 +0100397 struct audit_buffer *ab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 u16 msg_type = nlh->nlmsg_type;
Serge Hallync94c2572005-04-29 16:27:17 +0100399 uid_t loginuid; /* loginuid of sender */
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100400 struct audit_sig_info sig_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type);
403 if (err)
404 return err;
405
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700406 /* As soon as there's any sign of userspace auditd,
407 * start kauditd to talk to it */
David Woodhouseb7d11252005-05-19 10:56:58 +0100408 if (!kauditd_task)
409 kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd");
410 if (IS_ERR(kauditd_task)) {
411 err = PTR_ERR(kauditd_task);
412 kauditd_task = NULL;
413 return err;
414 }
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 pid = NETLINK_CREDS(skb)->pid;
417 uid = NETLINK_CREDS(skb)->uid;
Serge Hallync94c2572005-04-29 16:27:17 +0100418 loginuid = NETLINK_CB(skb).loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 seq = nlh->nlmsg_seq;
420 data = NLMSG_DATA(nlh);
421
422 switch (msg_type) {
423 case AUDIT_GET:
424 status_set.enabled = audit_enabled;
425 status_set.failure = audit_failure;
426 status_set.pid = audit_pid;
427 status_set.rate_limit = audit_rate_limit;
428 status_set.backlog_limit = audit_backlog_limit;
429 status_set.lost = atomic_read(&audit_lost);
David Woodhouseb7d11252005-05-19 10:56:58 +0100430 status_set.backlog = skb_queue_len(&audit_skb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
432 &status_set, sizeof(status_set));
433 break;
434 case AUDIT_SET:
435 if (nlh->nlmsg_len < sizeof(struct audit_status))
436 return -EINVAL;
437 status_get = (struct audit_status *)data;
438 if (status_get->mask & AUDIT_STATUS_ENABLED) {
Serge Hallync94c2572005-04-29 16:27:17 +0100439 err = audit_set_enabled(status_get->enabled, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 if (err < 0) return err;
441 }
442 if (status_get->mask & AUDIT_STATUS_FAILURE) {
Serge Hallync94c2572005-04-29 16:27:17 +0100443 err = audit_set_failure(status_get->failure, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 if (err < 0) return err;
445 }
446 if (status_get->mask & AUDIT_STATUS_PID) {
447 int old = audit_pid;
448 audit_pid = status_get->pid;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100449 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100450 "audit_pid=%d old=%d by auid=%u",
Serge Hallync94c2572005-04-29 16:27:17 +0100451 audit_pid, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453 if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
Serge Hallync94c2572005-04-29 16:27:17 +0100454 audit_set_rate_limit(status_get->rate_limit, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
Serge Hallync94c2572005-04-29 16:27:17 +0100456 audit_set_backlog_limit(status_get->backlog_limit,
457 loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 break;
Steve Grubb05474102005-05-21 00:18:37 +0100459 case AUDIT_USER:
David Woodhouse209aba02005-05-18 10:21:07 +0100460 case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
Steve Grubb90d526c2005-11-03 15:48:08 +0000461 case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2:
David Woodhouse4a4cd632005-06-22 14:56:47 +0100462 if (!audit_enabled && msg_type != AUDIT_USER_AVC)
463 return 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100464
David Woodhouse5bb289b2005-06-24 14:14:05 +0100465 err = audit_filter_user(&NETLINK_CB(skb), msg_type);
David Woodhouse4a4cd632005-06-22 14:56:47 +0100466 if (err == 1) {
467 err = 0;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100468 ab = audit_log_start(NULL, GFP_KERNEL, msg_type);
David Woodhouse4a4cd632005-06-22 14:56:47 +0100469 if (ab) {
470 audit_log_format(ab,
471 "user pid=%d uid=%u auid=%u msg='%.1024s'",
472 pid, uid, loginuid, (char *)data);
473 audit_set_pid(ab, pid);
474 audit_log_end(ab);
475 }
David Woodhouse0f45aa12005-06-19 19:35:50 +0100476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 break;
478 case AUDIT_ADD:
479 case AUDIT_DEL:
Amy Griffis93315ed2006-02-07 12:05:27 -0500480 if (nlmsg_len(nlh) < sizeof(struct audit_rule))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return -EINVAL;
482 /* fallthrough */
483 case AUDIT_LIST:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
Amy Griffis93315ed2006-02-07 12:05:27 -0500485 uid, seq, data, nlmsg_len(nlh),
486 loginuid);
487 break;
488 case AUDIT_ADD_RULE:
489 case AUDIT_DEL_RULE:
490 if (nlmsg_len(nlh) < sizeof(struct audit_rule_data))
491 return -EINVAL;
492 /* fallthrough */
493 case AUDIT_LIST_RULES:
494 err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
495 uid, seq, data, nlmsg_len(nlh),
496 loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 break;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100498 case AUDIT_SIGNAL_INFO:
499 sig_data.uid = audit_sig_uid;
500 sig_data.pid = audit_sig_pid;
501 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO,
502 0, 0, &sig_data, sizeof(sig_data));
503 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 default:
505 err = -EINVAL;
506 break;
507 }
508
509 return err < 0 ? err : 0;
510}
511
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700512/*
513 * Get message from skb (based on rtnetlink_rcv_skb). Each message is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 * processed by audit_receive_msg. Malformed skbs with wrong length are
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700515 * discarded silently.
516 */
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700517static void audit_receive_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
519 int err;
520 struct nlmsghdr *nlh;
521 u32 rlen;
522
523 while (skb->len >= NLMSG_SPACE(0)) {
524 nlh = (struct nlmsghdr *)skb->data;
525 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700526 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
528 if (rlen > skb->len)
529 rlen = skb->len;
530 if ((err = audit_receive_msg(skb, nlh))) {
531 netlink_ack(skb, nlh, err);
532 } else if (nlh->nlmsg_flags & NLM_F_ACK)
533 netlink_ack(skb, nlh, 0);
534 skb_pull(skb, rlen);
535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
538/* Receive messages from netlink socket. */
539static void audit_receive(struct sock *sk, int length)
540{
541 struct sk_buff *skb;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700542 unsigned int qlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Ingo Molnar5a0bbce2006-03-07 23:51:38 -0800544 mutex_lock(&audit_netlink_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700546 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
547 skb = skb_dequeue(&sk->sk_receive_queue);
548 audit_receive_skb(skb);
549 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
Ingo Molnar5a0bbce2006-03-07 23:51:38 -0800551 mutex_unlock(&audit_netlink_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555/* Initialize audit support at boot time. */
556static int __init audit_init(void)
557{
558 printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
559 audit_default ? "enabled" : "disabled");
Patrick McHardy06628602005-08-15 12:33:26 -0700560 audit_sock = netlink_kernel_create(NETLINK_AUDIT, 0, audit_receive,
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700561 THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 if (!audit_sock)
563 audit_panic("cannot initialize netlink socket");
Amy Griffis71e1c782006-03-06 22:40:05 -0500564 else
565 audit_sock->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
David Woodhouseb7d11252005-05-19 10:56:58 +0100567 skb_queue_head_init(&audit_skb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 audit_initialized = 1;
569 audit_enabled = audit_default;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600570
571 /* Register the callback with selinux. This callback will be invoked
572 * when a new policy is loaded. */
573 selinux_audit_set_callback(&selinux_audit_rule_update);
574
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100575 audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, "initialized");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return 0;
577}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578__initcall(audit_init);
579
580/* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
581static int __init audit_enable(char *str)
582{
583 audit_default = !!simple_strtol(str, NULL, 0);
584 printk(KERN_INFO "audit: %s%s\n",
585 audit_default ? "enabled" : "disabled",
586 audit_initialized ? "" : " (after initialization)");
587 if (audit_initialized)
588 audit_enabled = audit_default;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800589 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590}
591
592__setup("audit=", audit_enable);
593
Chris Wright16e19042005-05-06 15:53:34 +0100594static void audit_buffer_free(struct audit_buffer *ab)
595{
596 unsigned long flags;
597
Chris Wright8fc61152005-05-06 15:54:17 +0100598 if (!ab)
599 return;
600
Chris Wright5ac52f32005-05-06 15:54:53 +0100601 if (ab->skb)
602 kfree_skb(ab->skb);
David Woodhouseb7d11252005-05-19 10:56:58 +0100603
Chris Wright16e19042005-05-06 15:53:34 +0100604 spin_lock_irqsave(&audit_freelist_lock, flags);
605 if (++audit_freelist_count > AUDIT_MAXFREE)
606 kfree(ab);
607 else
608 list_add(&ab->list, &audit_freelist);
609 spin_unlock_irqrestore(&audit_freelist_lock, flags);
610}
611
Steve Grubbc0404992005-05-13 18:17:42 +0100612static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
Al Virodd0fc662005-10-07 07:46:04 +0100613 gfp_t gfp_mask, int type)
Chris Wright16e19042005-05-06 15:53:34 +0100614{
615 unsigned long flags;
616 struct audit_buffer *ab = NULL;
Steve Grubbc0404992005-05-13 18:17:42 +0100617 struct nlmsghdr *nlh;
Chris Wright16e19042005-05-06 15:53:34 +0100618
619 spin_lock_irqsave(&audit_freelist_lock, flags);
620 if (!list_empty(&audit_freelist)) {
621 ab = list_entry(audit_freelist.next,
622 struct audit_buffer, list);
623 list_del(&ab->list);
624 --audit_freelist_count;
625 }
626 spin_unlock_irqrestore(&audit_freelist_lock, flags);
627
628 if (!ab) {
David Woodhouse4332bdd2005-05-06 15:59:57 +0100629 ab = kmalloc(sizeof(*ab), gfp_mask);
Chris Wright16e19042005-05-06 15:53:34 +0100630 if (!ab)
Chris Wright8fc61152005-05-06 15:54:17 +0100631 goto err;
Chris Wright16e19042005-05-06 15:53:34 +0100632 }
Chris Wright8fc61152005-05-06 15:54:17 +0100633
David Woodhouse4332bdd2005-05-06 15:59:57 +0100634 ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask);
Chris Wright5ac52f32005-05-06 15:54:53 +0100635 if (!ab->skb)
Chris Wright8fc61152005-05-06 15:54:17 +0100636 goto err;
637
David Woodhouseb7d11252005-05-19 10:56:58 +0100638 ab->ctx = ctx;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100639 ab->gfp_mask = gfp_mask;
Steve Grubbc0404992005-05-13 18:17:42 +0100640 nlh = (struct nlmsghdr *)skb_put(ab->skb, NLMSG_SPACE(0));
641 nlh->nlmsg_type = type;
642 nlh->nlmsg_flags = 0;
643 nlh->nlmsg_pid = 0;
644 nlh->nlmsg_seq = 0;
Chris Wright16e19042005-05-06 15:53:34 +0100645 return ab;
Chris Wright8fc61152005-05-06 15:54:17 +0100646err:
647 audit_buffer_free(ab);
648 return NULL;
Chris Wright16e19042005-05-06 15:53:34 +0100649}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700651/**
652 * audit_serial - compute a serial number for the audit record
653 *
654 * Compute a serial number for the audit record. Audit records are
David Woodhousebfb44962005-05-21 21:08:09 +0100655 * written to user-space as soon as they are generated, so a complete
656 * audit record may be written in several pieces. The timestamp of the
657 * record and this serial number are used by the user-space tools to
658 * determine which pieces belong to the same audit record. The
659 * (timestamp,serial) tuple is unique for each syscall and is live from
660 * syscall entry to syscall exit.
661 *
David Woodhousebfb44962005-05-21 21:08:09 +0100662 * NOTE: Another possibility is to store the formatted records off the
663 * audit context (for those records that have a context), and emit them
664 * all at syscall exit. However, this could delay the reporting of
665 * significant errors until syscall exit (or never, if the system
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700666 * halts).
667 */
David Woodhousebfb44962005-05-21 21:08:09 +0100668unsigned int audit_serial(void)
669{
David Woodhoused5b454f2005-07-15 12:56:03 +0100670 static spinlock_t serial_lock = SPIN_LOCK_UNLOCKED;
671 static unsigned int serial = 0;
David Woodhousebfb44962005-05-21 21:08:09 +0100672
David Woodhoused5b454f2005-07-15 12:56:03 +0100673 unsigned long flags;
674 unsigned int ret;
David Woodhousebfb44962005-05-21 21:08:09 +0100675
David Woodhoused5b454f2005-07-15 12:56:03 +0100676 spin_lock_irqsave(&serial_lock, flags);
David Woodhousebfb44962005-05-21 21:08:09 +0100677 do {
David Woodhousece625a82005-07-18 14:24:46 -0400678 ret = ++serial;
679 } while (unlikely(!ret));
David Woodhoused5b454f2005-07-15 12:56:03 +0100680 spin_unlock_irqrestore(&serial_lock, flags);
David Woodhousebfb44962005-05-21 21:08:09 +0100681
David Woodhoused5b454f2005-07-15 12:56:03 +0100682 return ret;
David Woodhousebfb44962005-05-21 21:08:09 +0100683}
684
685static inline void audit_get_stamp(struct audit_context *ctx,
686 struct timespec *t, unsigned int *serial)
687{
688 if (ctx)
689 auditsc_get_stamp(ctx, t, serial);
690 else {
691 *t = CURRENT_TIME;
692 *serial = audit_serial();
693 }
694}
695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696/* Obtain an audit buffer. This routine does locking to obtain the
697 * audit buffer, but then no locking is required for calls to
698 * audit_log_*format. If the tsk is a task that is currently in a
699 * syscall, then the syscall is marked as auditable and an audit record
700 * will be written at syscall exit. If there is no associated task, tsk
701 * should be NULL. */
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100702
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700703/**
704 * audit_log_start - obtain an audit buffer
705 * @ctx: audit_context (may be NULL)
706 * @gfp_mask: type of allocation
707 * @type: audit message type
708 *
709 * Returns audit_buffer pointer on success or NULL on error.
710 *
711 * Obtain an audit buffer. This routine does locking to obtain the
712 * audit buffer, but then no locking is required for calls to
713 * audit_log_*format. If the task (ctx) is a task that is currently in a
714 * syscall, then the syscall is marked as auditable and an audit record
715 * will be written at syscall exit. If there is no associated task, then
716 * task context (ctx) should be NULL.
717 */
Al Viro9796fdd2005-10-21 03:22:03 -0400718struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100719 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
721 struct audit_buffer *ab = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 struct timespec t;
Steve Grubbd812ddb2005-04-29 16:09:52 +0100723 unsigned int serial;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100724 int reserve;
David Woodhouseac4cec42005-07-02 14:08:48 +0100725 unsigned long timeout_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726
727 if (!audit_initialized)
728 return NULL;
729
Dustin Kirklandc8edc802005-11-03 16:12:36 +0000730 if (unlikely(audit_filter_type(type)))
731 return NULL;
732
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100733 if (gfp_mask & __GFP_WAIT)
734 reserve = 0;
735 else
736 reserve = 5; /* Allow atomic callers to go up to five
737 entries over the normal backlog limit */
738
739 while (audit_backlog_limit
740 && skb_queue_len(&audit_skb_queue) > audit_backlog_limit + reserve) {
David Woodhouseac4cec42005-07-02 14:08:48 +0100741 if (gfp_mask & __GFP_WAIT && audit_backlog_wait_time
742 && time_before(jiffies, timeout_start + audit_backlog_wait_time)) {
743
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100744 /* Wait for auditd to drain the queue a little */
745 DECLARE_WAITQUEUE(wait, current);
746 set_current_state(TASK_INTERRUPTIBLE);
747 add_wait_queue(&audit_backlog_wait, &wait);
748
749 if (audit_backlog_limit &&
750 skb_queue_len(&audit_skb_queue) > audit_backlog_limit)
David Woodhouseac4cec42005-07-02 14:08:48 +0100751 schedule_timeout(timeout_start + audit_backlog_wait_time - jiffies);
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100752
753 __set_current_state(TASK_RUNNING);
754 remove_wait_queue(&audit_backlog_wait, &wait);
David Woodhouseac4cec42005-07-02 14:08:48 +0100755 continue;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100756 }
David Woodhousefb19b4c2005-05-19 14:55:56 +0100757 if (audit_rate_check())
758 printk(KERN_WARNING
759 "audit: audit_backlog=%d > "
760 "audit_backlog_limit=%d\n",
761 skb_queue_len(&audit_skb_queue),
762 audit_backlog_limit);
763 audit_log_lost("backlog limit exceeded");
David Woodhouseac4cec42005-07-02 14:08:48 +0100764 audit_backlog_wait_time = audit_backlog_wait_overflow;
765 wake_up(&audit_backlog_wait);
David Woodhousefb19b4c2005-05-19 14:55:56 +0100766 return NULL;
767 }
768
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100769 ab = audit_buffer_alloc(ctx, gfp_mask, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (!ab) {
771 audit_log_lost("out of memory in audit_log_start");
772 return NULL;
773 }
774
David Woodhousebfb44962005-05-21 21:08:09 +0100775 audit_get_stamp(ab->ctx, &t, &serial);
Chris Wright197c69c2005-05-11 10:54:05 +0100776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 audit_log_format(ab, "audit(%lu.%03lu:%u): ",
778 t.tv_sec, t.tv_nsec/1000000, serial);
779 return ab;
780}
781
Chris Wright8fc61152005-05-06 15:54:17 +0100782/**
Chris Wright5ac52f32005-05-06 15:54:53 +0100783 * audit_expand - expand skb in the audit buffer
Chris Wright8fc61152005-05-06 15:54:17 +0100784 * @ab: audit_buffer
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700785 * @extra: space to add at tail of the skb
Chris Wright8fc61152005-05-06 15:54:17 +0100786 *
787 * Returns 0 (no space) on failed expansion, or available space if
788 * successful.
789 */
David Woodhousee3b926b2005-05-10 18:56:08 +0100790static inline int audit_expand(struct audit_buffer *ab, int extra)
Chris Wright8fc61152005-05-06 15:54:17 +0100791{
Chris Wright5ac52f32005-05-06 15:54:53 +0100792 struct sk_buff *skb = ab->skb;
David Woodhousee3b926b2005-05-10 18:56:08 +0100793 int ret = pskb_expand_head(skb, skb_headroom(skb), extra,
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100794 ab->gfp_mask);
Chris Wright5ac52f32005-05-06 15:54:53 +0100795 if (ret < 0) {
796 audit_log_lost("out of memory in audit_expand");
Chris Wright8fc61152005-05-06 15:54:17 +0100797 return 0;
Chris Wright5ac52f32005-05-06 15:54:53 +0100798 }
799 return skb_tailroom(skb);
Chris Wright8fc61152005-05-06 15:54:17 +0100800}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700802/*
803 * Format an audit message into the audit buffer. If there isn't enough
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 * room in the audit buffer, more room will be allocated and vsnprint
805 * will be called a second time. Currently, we assume that a printk
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700806 * can't format message larger than 1024 bytes, so we don't either.
807 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
809 va_list args)
810{
811 int len, avail;
Chris Wright5ac52f32005-05-06 15:54:53 +0100812 struct sk_buff *skb;
David Woodhouseeecb0a72005-05-10 18:58:51 +0100813 va_list args2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 if (!ab)
816 return;
817
Chris Wright5ac52f32005-05-06 15:54:53 +0100818 BUG_ON(!ab->skb);
819 skb = ab->skb;
820 avail = skb_tailroom(skb);
821 if (avail == 0) {
David Woodhousee3b926b2005-05-10 18:56:08 +0100822 avail = audit_expand(ab, AUDIT_BUFSIZ);
Chris Wright8fc61152005-05-06 15:54:17 +0100823 if (!avail)
824 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
David Woodhouseeecb0a72005-05-10 18:58:51 +0100826 va_copy(args2, args);
Chris Wright5ac52f32005-05-06 15:54:53 +0100827 len = vsnprintf(skb->tail, avail, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 if (len >= avail) {
829 /* The printk buffer is 1024 bytes long, so if we get
830 * here and AUDIT_BUFSIZ is at least 1024, then we can
831 * log everything that printk could have logged. */
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700832 avail = audit_expand(ab,
833 max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
Chris Wright8fc61152005-05-06 15:54:17 +0100834 if (!avail)
835 goto out;
David Woodhouseeecb0a72005-05-10 18:58:51 +0100836 len = vsnprintf(skb->tail, avail, fmt, args2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Steve Grubb168b7172005-05-19 10:24:22 +0100838 if (len > 0)
839 skb_put(skb, len);
Chris Wright8fc61152005-05-06 15:54:17 +0100840out:
841 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700844/**
845 * audit_log_format - format a message into the audit buffer.
846 * @ab: audit_buffer
847 * @fmt: format string
848 * @...: optional parameters matching @fmt string
849 *
850 * All the work is done in audit_log_vformat.
851 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
853{
854 va_list args;
855
856 if (!ab)
857 return;
858 va_start(args, fmt);
859 audit_log_vformat(ab, fmt, args);
860 va_end(args);
861}
862
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700863/**
864 * audit_log_hex - convert a buffer to hex and append it to the audit skb
865 * @ab: the audit_buffer
866 * @buf: buffer to convert to hex
867 * @len: length of @buf to be converted
868 *
869 * No return value; failure to expand is silently ignored.
870 *
871 * This function will take the passed buf and convert it into a string of
872 * ascii hex digits. The new string is placed onto the skb.
873 */
874void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf,
Steve Grubb168b7172005-05-19 10:24:22 +0100875 size_t len)
83c7d092005-04-29 15:54:44 +0100876{
Steve Grubb168b7172005-05-19 10:24:22 +0100877 int i, avail, new_len;
878 unsigned char *ptr;
879 struct sk_buff *skb;
880 static const unsigned char *hex = "0123456789ABCDEF";
83c7d092005-04-29 15:54:44 +0100881
Steve Grubb168b7172005-05-19 10:24:22 +0100882 BUG_ON(!ab->skb);
883 skb = ab->skb;
884 avail = skb_tailroom(skb);
885 new_len = len<<1;
886 if (new_len >= avail) {
887 /* Round the buffer request up to the next multiple */
888 new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
889 avail = audit_expand(ab, new_len);
890 if (!avail)
891 return;
892 }
893
894 ptr = skb->tail;
895 for (i=0; i<len; i++) {
896 *ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */
897 *ptr++ = hex[buf[i] & 0x0F]; /* Lower nibble */
898 }
899 *ptr = 0;
900 skb_put(skb, len << 1); /* new string is twice the old string */
83c7d092005-04-29 15:54:44 +0100901}
902
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700903/**
904 * audit_log_unstrustedstring - log a string that may contain random characters
905 * @ab: audit_buffer
906 * @string: string to be logged
907 *
908 * This code will escape a string that is passed to it if the string
909 * contains a control character, unprintable character, double quote mark,
Steve Grubb168b7172005-05-19 10:24:22 +0100910 * or a space. Unescaped strings will start and end with a double quote mark.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700911 * Strings that are escaped are printed in hex (2 digits per char).
912 */
83c7d092005-04-29 15:54:44 +0100913void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
914{
Andrew Morton81b78542005-04-29 15:59:11 +0100915 const unsigned char *p = string;
83c7d092005-04-29 15:54:44 +0100916
917 while (*p) {
Steve Grubb168b7172005-05-19 10:24:22 +0100918 if (*p == '"' || *p < 0x21 || *p > 0x7f) {
83c7d092005-04-29 15:54:44 +0100919 audit_log_hex(ab, string, strlen(string));
920 return;
921 }
922 p++;
923 }
924 audit_log_format(ab, "\"%s\"", string);
925}
926
Steve Grubb168b7172005-05-19 10:24:22 +0100927/* This is a helper-function to print the escaped d_path */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
929 struct dentry *dentry, struct vfsmount *vfsmnt)
930{
Steve Grubb168b7172005-05-19 10:24:22 +0100931 char *p, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Chris Wright8fc61152005-05-06 15:54:17 +0100933 if (prefix)
934 audit_log_format(ab, " %s", prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
Steve Grubb168b7172005-05-19 10:24:22 +0100936 /* We will allow 11 spaces for ' (deleted)' to be appended */
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100937 path = kmalloc(PATH_MAX+11, ab->gfp_mask);
Steve Grubb168b7172005-05-19 10:24:22 +0100938 if (!path) {
939 audit_log_format(ab, "<no memory>");
940 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
Steve Grubb168b7172005-05-19 10:24:22 +0100942 p = d_path(dentry, vfsmnt, path, PATH_MAX+11);
943 if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
944 /* FIXME: can we save some information here? */
945 audit_log_format(ab, "<too long>");
946 } else
947 audit_log_untrustedstring(ab, p);
948 kfree(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
950
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700951/**
952 * audit_log_end - end one audit record
953 * @ab: the audit_buffer
954 *
955 * The netlink_* functions cannot be called inside an irq context, so
956 * the audit buffer is placed on a queue and a tasklet is scheduled to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 * remove them from the queue outside the irq context. May be called in
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700958 * any context.
959 */
David Woodhouseb7d11252005-05-19 10:56:58 +0100960void audit_log_end(struct audit_buffer *ab)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 if (!ab)
963 return;
964 if (!audit_rate_check()) {
965 audit_log_lost("rate limit exceeded");
966 } else {
David Woodhouseb7d11252005-05-19 10:56:58 +0100967 if (audit_pid) {
968 struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
969 nlh->nlmsg_len = ab->skb->len - NLMSG_SPACE(0);
970 skb_queue_tail(&audit_skb_queue, ab->skb);
971 ab->skb = NULL;
972 wake_up_interruptible(&kauditd_wait);
973 } else {
David Woodhousee1b09eb2005-06-24 17:24:11 +0100974 printk(KERN_NOTICE "%s\n", ab->skb->data + NLMSG_SPACE(0));
David Woodhouseb7d11252005-05-19 10:56:58 +0100975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 }
Chris Wright16e19042005-05-06 15:53:34 +0100977 audit_buffer_free(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978}
979
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700980/**
981 * audit_log - Log an audit record
982 * @ctx: audit context
983 * @gfp_mask: type of allocation
984 * @type: audit message type
985 * @fmt: format string to use
986 * @...: variable parameters matching the format string
987 *
988 * This is a convenience function that calls audit_log_start,
989 * audit_log_vformat, and audit_log_end. It may be called
990 * in any context.
991 */
Al Viro9796fdd2005-10-21 03:22:03 -0400992void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100993 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
995 struct audit_buffer *ab;
996 va_list args;
997
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100998 ab = audit_log_start(ctx, gfp_mask, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 if (ab) {
1000 va_start(args, fmt);
1001 audit_log_vformat(ab, fmt, args);
1002 va_end(args);
1003 audit_log_end(ab);
1004 }
1005}
lorenzo@gnu.orgbf45da92006-03-09 00:33:47 +01001006
1007EXPORT_SYMBOL(audit_log_start);
1008EXPORT_SYMBOL(audit_log_end);
1009EXPORT_SYMBOL(audit_log_format);
1010EXPORT_SYMBOL(audit_log);