blob: 4eb97b62d7fa75b9cd3b1819e74b162329577a37 [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>
58
59/* No auditing will take place until audit_initialized != 0.
60 * (Initialization happens after skb_init is called.) */
61static int audit_initialized;
62
63/* No syscall auditing will take place unless audit_enabled != 0. */
64int audit_enabled;
65
66/* Default state when kernel boots without any parameters. */
67static int audit_default;
68
69/* If auditing cannot proceed, audit_failure selects what happens. */
70static int audit_failure = AUDIT_FAIL_PRINTK;
71
72/* If audit records are to be written to the netlink socket, audit_pid
73 * contains the (non-zero) pid. */
Steve Grubbc2f0c7c2005-05-06 12:38:39 +010074int audit_pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Randy Dunlapb0dd25a2005-09-13 12:47:11 -070076/* If audit_rate_limit is non-zero, limit the rate of sending audit records
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * to that number per second. This prevents DoS attacks, but results in
78 * audit records being dropped. */
79static int audit_rate_limit;
80
81/* Number of outstanding audit_buffers allowed. */
82static int audit_backlog_limit = 64;
David Woodhouseac4cec42005-07-02 14:08:48 +010083static int audit_backlog_wait_time = 60 * HZ;
84static int audit_backlog_wait_overflow = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Steve Grubbc2f0c7c2005-05-06 12:38:39 +010086/* The identity of the user shutting down the audit system. */
87uid_t audit_sig_uid = -1;
88pid_t audit_sig_pid = -1;
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* Records can be lost in several ways:
91 0) [suppressed in audit_alloc]
92 1) out of memory in audit_log_start [kmalloc of struct audit_buffer]
93 2) out of memory in audit_log_move [alloc_skb]
94 3) suppressed due to audit_rate_limit
95 4) suppressed due to audit_backlog_limit
96*/
97static atomic_t audit_lost = ATOMIC_INIT(0);
98
99/* The netlink socket. */
100static struct sock *audit_sock;
101
David Woodhouseb7d11252005-05-19 10:56:58 +0100102/* The audit_freelist is a list of pre-allocated audit buffers (if more
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of
104 * being placed on the freelist). */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105static DEFINE_SPINLOCK(audit_freelist_lock);
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700106static int audit_freelist_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107static LIST_HEAD(audit_freelist);
108
David Woodhouseb7d11252005-05-19 10:56:58 +0100109static struct sk_buff_head audit_skb_queue;
110static struct task_struct *kauditd_task;
111static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait);
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100112static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114/* The netlink socket is only to be read by 1 CPU, which lets us assume
Steve Grubb23f32d12005-05-13 18:35:15 +0100115 * that list additions and deletions never happen simultaneously in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 * auditsc.c */
David Woodhousef6a789d2005-06-21 16:22:01 +0100117DECLARE_MUTEX(audit_netlink_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119/* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
120 * audit records. Since printk uses a 1024 byte buffer, this buffer
121 * should be at least that large. */
122#define AUDIT_BUFSIZ 1024
123
124/* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the
125 * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */
126#define AUDIT_MAXFREE (2*NR_CPUS)
127
128/* The audit_buffer is used when formatting an audit record. The caller
129 * locks briefly to get the record off the freelist or to allocate the
130 * buffer, and locks briefly to send the buffer to the netlink layer or
131 * to place it on a transmit queue. Multiple audit_buffers can be in
132 * use simultaneously. */
133struct audit_buffer {
134 struct list_head list;
Chris Wright8fc61152005-05-06 15:54:17 +0100135 struct sk_buff *skb; /* formatted skb ready to send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 struct audit_context *ctx; /* NULL or associated context */
Al Viro9796fdd2005-10-21 03:22:03 -0400137 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138};
139
Steve Grubbc0404992005-05-13 18:17:42 +0100140static void audit_set_pid(struct audit_buffer *ab, pid_t pid)
141{
142 struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
143 nlh->nlmsg_pid = pid;
144}
145
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000146void audit_panic(const char *message)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 switch (audit_failure)
149 {
150 case AUDIT_FAIL_SILENT:
151 break;
152 case AUDIT_FAIL_PRINTK:
153 printk(KERN_ERR "audit: %s\n", message);
154 break;
155 case AUDIT_FAIL_PANIC:
156 panic("audit: %s\n", message);
157 break;
158 }
159}
160
161static inline int audit_rate_check(void)
162{
163 static unsigned long last_check = 0;
164 static int messages = 0;
165 static DEFINE_SPINLOCK(lock);
166 unsigned long flags;
167 unsigned long now;
168 unsigned long elapsed;
169 int retval = 0;
170
171 if (!audit_rate_limit) return 1;
172
173 spin_lock_irqsave(&lock, flags);
174 if (++messages < audit_rate_limit) {
175 retval = 1;
176 } else {
177 now = jiffies;
178 elapsed = now - last_check;
179 if (elapsed > HZ) {
180 last_check = now;
181 messages = 0;
182 retval = 1;
183 }
184 }
185 spin_unlock_irqrestore(&lock, flags);
186
187 return retval;
188}
189
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700190/**
191 * audit_log_lost - conditionally log lost audit message event
192 * @message: the message stating reason for lost audit message
193 *
194 * Emit at least 1 message per second, even if audit_rate_check is
195 * throttling.
196 * Always increment the lost messages counter.
197*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198void audit_log_lost(const char *message)
199{
200 static unsigned long last_msg = 0;
201 static DEFINE_SPINLOCK(lock);
202 unsigned long flags;
203 unsigned long now;
204 int print;
205
206 atomic_inc(&audit_lost);
207
208 print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);
209
210 if (!print) {
211 spin_lock_irqsave(&lock, flags);
212 now = jiffies;
213 if (now - last_msg > HZ) {
214 print = 1;
215 last_msg = now;
216 }
217 spin_unlock_irqrestore(&lock, flags);
218 }
219
220 if (print) {
221 printk(KERN_WARNING
David Woodhouseb7d11252005-05-19 10:56:58 +0100222 "audit: audit_lost=%d audit_rate_limit=%d audit_backlog_limit=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 atomic_read(&audit_lost),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 audit_rate_limit,
225 audit_backlog_limit);
226 audit_panic(message);
227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Serge Hallync94c2572005-04-29 16:27:17 +0100230static int audit_set_rate_limit(int limit, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 int old = audit_rate_limit;
233 audit_rate_limit = limit;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100234 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100235 "audit_rate_limit=%d old=%d by auid=%u",
Serge Hallync94c2572005-04-29 16:27:17 +0100236 audit_rate_limit, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return old;
238}
239
Serge Hallync94c2572005-04-29 16:27:17 +0100240static int audit_set_backlog_limit(int limit, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 int old = audit_backlog_limit;
243 audit_backlog_limit = limit;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100244 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100245 "audit_backlog_limit=%d old=%d by auid=%u",
Serge Hallync94c2572005-04-29 16:27:17 +0100246 audit_backlog_limit, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return old;
248}
249
Serge Hallync94c2572005-04-29 16:27:17 +0100250static int audit_set_enabled(int state, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251{
252 int old = audit_enabled;
253 if (state != 0 && state != 1)
254 return -EINVAL;
255 audit_enabled = state;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100256 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100257 "audit_enabled=%d old=%d by auid=%u",
Steve Grubbc0404992005-05-13 18:17:42 +0100258 audit_enabled, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 return old;
260}
261
Serge Hallync94c2572005-04-29 16:27:17 +0100262static int audit_set_failure(int state, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263{
264 int old = audit_failure;
265 if (state != AUDIT_FAIL_SILENT
266 && state != AUDIT_FAIL_PRINTK
267 && state != AUDIT_FAIL_PANIC)
268 return -EINVAL;
269 audit_failure = state;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100270 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100271 "audit_failure=%d old=%d by auid=%u",
Steve Grubbc0404992005-05-13 18:17:42 +0100272 audit_failure, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return old;
274}
275
Adrian Bunk97a41e22006-01-08 01:02:17 -0800276static int kauditd_thread(void *dummy)
David Woodhouseb7d11252005-05-19 10:56:58 +0100277{
278 struct sk_buff *skb;
279
280 while (1) {
281 skb = skb_dequeue(&audit_skb_queue);
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100282 wake_up(&audit_backlog_wait);
David Woodhouseb7d11252005-05-19 10:56:58 +0100283 if (skb) {
284 if (audit_pid) {
285 int err = netlink_unicast(audit_sock, skb, audit_pid, 0);
286 if (err < 0) {
287 BUG_ON(err != -ECONNREFUSED); /* Shoudn't happen */
288 printk(KERN_ERR "audit: *NO* daemon at audit_pid=%d\n", audit_pid);
289 audit_pid = 0;
290 }
291 } else {
David Woodhousee1b09eb2005-06-24 17:24:11 +0100292 printk(KERN_NOTICE "%s\n", skb->data + NLMSG_SPACE(0));
David Woodhouseb7d11252005-05-19 10:56:58 +0100293 kfree_skb(skb);
294 }
295 } else {
296 DECLARE_WAITQUEUE(wait, current);
297 set_current_state(TASK_INTERRUPTIBLE);
298 add_wait_queue(&kauditd_wait, &wait);
299
Pierre Ossman7a4ae742005-12-12 00:37:22 -0800300 if (!skb_queue_len(&audit_skb_queue)) {
301 try_to_freeze();
David Woodhouseb7d11252005-05-19 10:56:58 +0100302 schedule();
Pierre Ossman7a4ae742005-12-12 00:37:22 -0800303 }
David Woodhouseb7d11252005-05-19 10:56:58 +0100304
305 __set_current_state(TASK_RUNNING);
306 remove_wait_queue(&kauditd_wait, &wait);
307 }
308 }
David Woodhousefe7752b2005-12-15 18:33:52 +0000309 return 0;
David Woodhouseb7d11252005-05-19 10:56:58 +0100310}
311
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700312/**
313 * audit_send_reply - send an audit reply message via netlink
314 * @pid: process id to send reply to
315 * @seq: sequence number
316 * @type: audit message type
317 * @done: done (last) flag
318 * @multi: multi-part message flag
319 * @payload: payload data
320 * @size: payload size
321 *
322 * Allocates an skb, builds the netlink message, and sends it to the pid.
323 * No failure notifications.
324 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325void audit_send_reply(int pid, int seq, int type, int done, int multi,
326 void *payload, int size)
327{
328 struct sk_buff *skb;
329 struct nlmsghdr *nlh;
330 int len = NLMSG_SPACE(size);
331 void *data;
332 int flags = multi ? NLM_F_MULTI : 0;
333 int t = done ? NLMSG_DONE : type;
334
335 skb = alloc_skb(len, GFP_KERNEL);
336 if (!skb)
David Woodhouseb7d11252005-05-19 10:56:58 +0100337 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
David Woodhouseb7d11252005-05-19 10:56:58 +0100339 nlh = NLMSG_PUT(skb, pid, seq, t, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 nlh->nlmsg_flags = flags;
341 data = NLMSG_DATA(nlh);
342 memcpy(data, payload, size);
David Woodhouseb7d11252005-05-19 10:56:58 +0100343
344 /* Ignore failure. It'll only happen if the sender goes away,
345 because our timeout is set to infinite. */
346 netlink_unicast(audit_sock, skb, pid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return;
348
349nlmsg_failure: /* Used by NLMSG_PUT */
350 if (skb)
351 kfree_skb(skb);
352}
353
354/*
355 * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
356 * control messages.
357 */
358static int audit_netlink_ok(kernel_cap_t eff_cap, u16 msg_type)
359{
360 int err = 0;
361
362 switch (msg_type) {
363 case AUDIT_GET:
364 case AUDIT_LIST:
Amy Griffis93315ed2006-02-07 12:05:27 -0500365 case AUDIT_LIST_RULES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 case AUDIT_SET:
367 case AUDIT_ADD:
Amy Griffis93315ed2006-02-07 12:05:27 -0500368 case AUDIT_ADD_RULE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 case AUDIT_DEL:
Amy Griffis93315ed2006-02-07 12:05:27 -0500370 case AUDIT_DEL_RULE:
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100371 case AUDIT_SIGNAL_INFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL))
373 err = -EPERM;
374 break;
Steve Grubb05474102005-05-21 00:18:37 +0100375 case AUDIT_USER:
David Woodhouse209aba02005-05-18 10:21:07 +0100376 case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
Steve Grubb90d526c2005-11-03 15:48:08 +0000377 case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 if (!cap_raised(eff_cap, CAP_AUDIT_WRITE))
379 err = -EPERM;
380 break;
381 default: /* bad msg */
382 err = -EINVAL;
383 }
384
385 return err;
386}
387
388static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
389{
390 u32 uid, pid, seq;
391 void *data;
392 struct audit_status *status_get, status_set;
393 int err;
Steve Grubbc0404992005-05-13 18:17:42 +0100394 struct audit_buffer *ab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 u16 msg_type = nlh->nlmsg_type;
Serge Hallync94c2572005-04-29 16:27:17 +0100396 uid_t loginuid; /* loginuid of sender */
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100397 struct audit_sig_info sig_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type);
400 if (err)
401 return err;
402
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700403 /* As soon as there's any sign of userspace auditd,
404 * start kauditd to talk to it */
David Woodhouseb7d11252005-05-19 10:56:58 +0100405 if (!kauditd_task)
406 kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd");
407 if (IS_ERR(kauditd_task)) {
408 err = PTR_ERR(kauditd_task);
409 kauditd_task = NULL;
410 return err;
411 }
412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 pid = NETLINK_CREDS(skb)->pid;
414 uid = NETLINK_CREDS(skb)->uid;
Serge Hallync94c2572005-04-29 16:27:17 +0100415 loginuid = NETLINK_CB(skb).loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 seq = nlh->nlmsg_seq;
417 data = NLMSG_DATA(nlh);
418
419 switch (msg_type) {
420 case AUDIT_GET:
421 status_set.enabled = audit_enabled;
422 status_set.failure = audit_failure;
423 status_set.pid = audit_pid;
424 status_set.rate_limit = audit_rate_limit;
425 status_set.backlog_limit = audit_backlog_limit;
426 status_set.lost = atomic_read(&audit_lost);
David Woodhouseb7d11252005-05-19 10:56:58 +0100427 status_set.backlog = skb_queue_len(&audit_skb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
429 &status_set, sizeof(status_set));
430 break;
431 case AUDIT_SET:
432 if (nlh->nlmsg_len < sizeof(struct audit_status))
433 return -EINVAL;
434 status_get = (struct audit_status *)data;
435 if (status_get->mask & AUDIT_STATUS_ENABLED) {
Serge Hallync94c2572005-04-29 16:27:17 +0100436 err = audit_set_enabled(status_get->enabled, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 if (err < 0) return err;
438 }
439 if (status_get->mask & AUDIT_STATUS_FAILURE) {
Serge Hallync94c2572005-04-29 16:27:17 +0100440 err = audit_set_failure(status_get->failure, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 if (err < 0) return err;
442 }
443 if (status_get->mask & AUDIT_STATUS_PID) {
444 int old = audit_pid;
445 audit_pid = status_get->pid;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100446 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100447 "audit_pid=%d old=%d by auid=%u",
Serge Hallync94c2572005-04-29 16:27:17 +0100448 audit_pid, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
450 if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
Serge Hallync94c2572005-04-29 16:27:17 +0100451 audit_set_rate_limit(status_get->rate_limit, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
Serge Hallync94c2572005-04-29 16:27:17 +0100453 audit_set_backlog_limit(status_get->backlog_limit,
454 loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 break;
Steve Grubb05474102005-05-21 00:18:37 +0100456 case AUDIT_USER:
David Woodhouse209aba02005-05-18 10:21:07 +0100457 case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
Steve Grubb90d526c2005-11-03 15:48:08 +0000458 case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2:
David Woodhouse4a4cd632005-06-22 14:56:47 +0100459 if (!audit_enabled && msg_type != AUDIT_USER_AVC)
460 return 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100461
David Woodhouse5bb289b2005-06-24 14:14:05 +0100462 err = audit_filter_user(&NETLINK_CB(skb), msg_type);
David Woodhouse4a4cd632005-06-22 14:56:47 +0100463 if (err == 1) {
464 err = 0;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100465 ab = audit_log_start(NULL, GFP_KERNEL, msg_type);
David Woodhouse4a4cd632005-06-22 14:56:47 +0100466 if (ab) {
467 audit_log_format(ab,
468 "user pid=%d uid=%u auid=%u msg='%.1024s'",
469 pid, uid, loginuid, (char *)data);
470 audit_set_pid(ab, pid);
471 audit_log_end(ab);
472 }
David Woodhouse0f45aa12005-06-19 19:35:50 +0100473 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 break;
475 case AUDIT_ADD:
476 case AUDIT_DEL:
Amy Griffis93315ed2006-02-07 12:05:27 -0500477 if (nlmsg_len(nlh) < sizeof(struct audit_rule))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return -EINVAL;
479 /* fallthrough */
480 case AUDIT_LIST:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
Amy Griffis93315ed2006-02-07 12:05:27 -0500482 uid, seq, data, nlmsg_len(nlh),
483 loginuid);
484 break;
485 case AUDIT_ADD_RULE:
486 case AUDIT_DEL_RULE:
487 if (nlmsg_len(nlh) < sizeof(struct audit_rule_data))
488 return -EINVAL;
489 /* fallthrough */
490 case AUDIT_LIST_RULES:
491 err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
492 uid, seq, data, nlmsg_len(nlh),
493 loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 break;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100495 case AUDIT_SIGNAL_INFO:
496 sig_data.uid = audit_sig_uid;
497 sig_data.pid = audit_sig_pid;
498 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO,
499 0, 0, &sig_data, sizeof(sig_data));
500 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 default:
502 err = -EINVAL;
503 break;
504 }
505
506 return err < 0 ? err : 0;
507}
508
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700509/*
510 * Get message from skb (based on rtnetlink_rcv_skb). Each message is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 * processed by audit_receive_msg. Malformed skbs with wrong length are
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700512 * discarded silently.
513 */
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700514static void audit_receive_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515{
516 int err;
517 struct nlmsghdr *nlh;
518 u32 rlen;
519
520 while (skb->len >= NLMSG_SPACE(0)) {
521 nlh = (struct nlmsghdr *)skb->data;
522 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700523 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
525 if (rlen > skb->len)
526 rlen = skb->len;
527 if ((err = audit_receive_msg(skb, nlh))) {
528 netlink_ack(skb, nlh, err);
529 } else if (nlh->nlmsg_flags & NLM_F_ACK)
530 netlink_ack(skb, nlh, 0);
531 skb_pull(skb, rlen);
532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
535/* Receive messages from netlink socket. */
536static void audit_receive(struct sock *sk, int length)
537{
538 struct sk_buff *skb;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700539 unsigned int qlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700541 down(&audit_netlink_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700543 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
544 skb = skb_dequeue(&sk->sk_receive_queue);
545 audit_receive_skb(skb);
546 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548 up(&audit_netlink_sem);
549}
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552/* Initialize audit support at boot time. */
553static int __init audit_init(void)
554{
555 printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
556 audit_default ? "enabled" : "disabled");
Patrick McHardy06628602005-08-15 12:33:26 -0700557 audit_sock = netlink_kernel_create(NETLINK_AUDIT, 0, audit_receive,
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700558 THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (!audit_sock)
560 audit_panic("cannot initialize netlink socket");
561
David Woodhouseb7d11252005-05-19 10:56:58 +0100562 audit_sock->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
563 skb_queue_head_init(&audit_skb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 audit_initialized = 1;
565 audit_enabled = audit_default;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100566 audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, "initialized");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return 0;
568}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569__initcall(audit_init);
570
571/* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
572static int __init audit_enable(char *str)
573{
574 audit_default = !!simple_strtol(str, NULL, 0);
575 printk(KERN_INFO "audit: %s%s\n",
576 audit_default ? "enabled" : "disabled",
577 audit_initialized ? "" : " (after initialization)");
578 if (audit_initialized)
579 audit_enabled = audit_default;
580 return 0;
581}
582
583__setup("audit=", audit_enable);
584
Chris Wright16e19042005-05-06 15:53:34 +0100585static void audit_buffer_free(struct audit_buffer *ab)
586{
587 unsigned long flags;
588
Chris Wright8fc61152005-05-06 15:54:17 +0100589 if (!ab)
590 return;
591
Chris Wright5ac52f32005-05-06 15:54:53 +0100592 if (ab->skb)
593 kfree_skb(ab->skb);
David Woodhouseb7d11252005-05-19 10:56:58 +0100594
Chris Wright16e19042005-05-06 15:53:34 +0100595 spin_lock_irqsave(&audit_freelist_lock, flags);
596 if (++audit_freelist_count > AUDIT_MAXFREE)
597 kfree(ab);
598 else
599 list_add(&ab->list, &audit_freelist);
600 spin_unlock_irqrestore(&audit_freelist_lock, flags);
601}
602
Steve Grubbc0404992005-05-13 18:17:42 +0100603static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
Al Virodd0fc662005-10-07 07:46:04 +0100604 gfp_t gfp_mask, int type)
Chris Wright16e19042005-05-06 15:53:34 +0100605{
606 unsigned long flags;
607 struct audit_buffer *ab = NULL;
Steve Grubbc0404992005-05-13 18:17:42 +0100608 struct nlmsghdr *nlh;
Chris Wright16e19042005-05-06 15:53:34 +0100609
610 spin_lock_irqsave(&audit_freelist_lock, flags);
611 if (!list_empty(&audit_freelist)) {
612 ab = list_entry(audit_freelist.next,
613 struct audit_buffer, list);
614 list_del(&ab->list);
615 --audit_freelist_count;
616 }
617 spin_unlock_irqrestore(&audit_freelist_lock, flags);
618
619 if (!ab) {
David Woodhouse4332bdd2005-05-06 15:59:57 +0100620 ab = kmalloc(sizeof(*ab), gfp_mask);
Chris Wright16e19042005-05-06 15:53:34 +0100621 if (!ab)
Chris Wright8fc61152005-05-06 15:54:17 +0100622 goto err;
Chris Wright16e19042005-05-06 15:53:34 +0100623 }
Chris Wright8fc61152005-05-06 15:54:17 +0100624
David Woodhouse4332bdd2005-05-06 15:59:57 +0100625 ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask);
Chris Wright5ac52f32005-05-06 15:54:53 +0100626 if (!ab->skb)
Chris Wright8fc61152005-05-06 15:54:17 +0100627 goto err;
628
David Woodhouseb7d11252005-05-19 10:56:58 +0100629 ab->ctx = ctx;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100630 ab->gfp_mask = gfp_mask;
Steve Grubbc0404992005-05-13 18:17:42 +0100631 nlh = (struct nlmsghdr *)skb_put(ab->skb, NLMSG_SPACE(0));
632 nlh->nlmsg_type = type;
633 nlh->nlmsg_flags = 0;
634 nlh->nlmsg_pid = 0;
635 nlh->nlmsg_seq = 0;
Chris Wright16e19042005-05-06 15:53:34 +0100636 return ab;
Chris Wright8fc61152005-05-06 15:54:17 +0100637err:
638 audit_buffer_free(ab);
639 return NULL;
Chris Wright16e19042005-05-06 15:53:34 +0100640}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700642/**
643 * audit_serial - compute a serial number for the audit record
644 *
645 * Compute a serial number for the audit record. Audit records are
David Woodhousebfb44962005-05-21 21:08:09 +0100646 * written to user-space as soon as they are generated, so a complete
647 * audit record may be written in several pieces. The timestamp of the
648 * record and this serial number are used by the user-space tools to
649 * determine which pieces belong to the same audit record. The
650 * (timestamp,serial) tuple is unique for each syscall and is live from
651 * syscall entry to syscall exit.
652 *
David Woodhousebfb44962005-05-21 21:08:09 +0100653 * NOTE: Another possibility is to store the formatted records off the
654 * audit context (for those records that have a context), and emit them
655 * all at syscall exit. However, this could delay the reporting of
656 * significant errors until syscall exit (or never, if the system
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700657 * halts).
658 */
David Woodhousebfb44962005-05-21 21:08:09 +0100659unsigned int audit_serial(void)
660{
David Woodhoused5b454f2005-07-15 12:56:03 +0100661 static spinlock_t serial_lock = SPIN_LOCK_UNLOCKED;
662 static unsigned int serial = 0;
David Woodhousebfb44962005-05-21 21:08:09 +0100663
David Woodhoused5b454f2005-07-15 12:56:03 +0100664 unsigned long flags;
665 unsigned int ret;
David Woodhousebfb44962005-05-21 21:08:09 +0100666
David Woodhoused5b454f2005-07-15 12:56:03 +0100667 spin_lock_irqsave(&serial_lock, flags);
David Woodhousebfb44962005-05-21 21:08:09 +0100668 do {
David Woodhousece625a82005-07-18 14:24:46 -0400669 ret = ++serial;
670 } while (unlikely(!ret));
David Woodhoused5b454f2005-07-15 12:56:03 +0100671 spin_unlock_irqrestore(&serial_lock, flags);
David Woodhousebfb44962005-05-21 21:08:09 +0100672
David Woodhoused5b454f2005-07-15 12:56:03 +0100673 return ret;
David Woodhousebfb44962005-05-21 21:08:09 +0100674}
675
676static inline void audit_get_stamp(struct audit_context *ctx,
677 struct timespec *t, unsigned int *serial)
678{
679 if (ctx)
680 auditsc_get_stamp(ctx, t, serial);
681 else {
682 *t = CURRENT_TIME;
683 *serial = audit_serial();
684 }
685}
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687/* Obtain an audit buffer. This routine does locking to obtain the
688 * audit buffer, but then no locking is required for calls to
689 * audit_log_*format. If the tsk is a task that is currently in a
690 * syscall, then the syscall is marked as auditable and an audit record
691 * will be written at syscall exit. If there is no associated task, tsk
692 * should be NULL. */
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100693
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700694/**
695 * audit_log_start - obtain an audit buffer
696 * @ctx: audit_context (may be NULL)
697 * @gfp_mask: type of allocation
698 * @type: audit message type
699 *
700 * Returns audit_buffer pointer on success or NULL on error.
701 *
702 * Obtain an audit buffer. This routine does locking to obtain the
703 * audit buffer, but then no locking is required for calls to
704 * audit_log_*format. If the task (ctx) is a task that is currently in a
705 * syscall, then the syscall is marked as auditable and an audit record
706 * will be written at syscall exit. If there is no associated task, then
707 * task context (ctx) should be NULL.
708 */
Al Viro9796fdd2005-10-21 03:22:03 -0400709struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100710 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 struct audit_buffer *ab = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 struct timespec t;
Steve Grubbd812ddb2005-04-29 16:09:52 +0100714 unsigned int serial;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100715 int reserve;
David Woodhouseac4cec42005-07-02 14:08:48 +0100716 unsigned long timeout_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 if (!audit_initialized)
719 return NULL;
720
Dustin Kirklandc8edc802005-11-03 16:12:36 +0000721 if (unlikely(audit_filter_type(type)))
722 return NULL;
723
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100724 if (gfp_mask & __GFP_WAIT)
725 reserve = 0;
726 else
727 reserve = 5; /* Allow atomic callers to go up to five
728 entries over the normal backlog limit */
729
730 while (audit_backlog_limit
731 && skb_queue_len(&audit_skb_queue) > audit_backlog_limit + reserve) {
David Woodhouseac4cec42005-07-02 14:08:48 +0100732 if (gfp_mask & __GFP_WAIT && audit_backlog_wait_time
733 && time_before(jiffies, timeout_start + audit_backlog_wait_time)) {
734
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100735 /* Wait for auditd to drain the queue a little */
736 DECLARE_WAITQUEUE(wait, current);
737 set_current_state(TASK_INTERRUPTIBLE);
738 add_wait_queue(&audit_backlog_wait, &wait);
739
740 if (audit_backlog_limit &&
741 skb_queue_len(&audit_skb_queue) > audit_backlog_limit)
David Woodhouseac4cec42005-07-02 14:08:48 +0100742 schedule_timeout(timeout_start + audit_backlog_wait_time - jiffies);
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100743
744 __set_current_state(TASK_RUNNING);
745 remove_wait_queue(&audit_backlog_wait, &wait);
David Woodhouseac4cec42005-07-02 14:08:48 +0100746 continue;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100747 }
David Woodhousefb19b4c2005-05-19 14:55:56 +0100748 if (audit_rate_check())
749 printk(KERN_WARNING
750 "audit: audit_backlog=%d > "
751 "audit_backlog_limit=%d\n",
752 skb_queue_len(&audit_skb_queue),
753 audit_backlog_limit);
754 audit_log_lost("backlog limit exceeded");
David Woodhouseac4cec42005-07-02 14:08:48 +0100755 audit_backlog_wait_time = audit_backlog_wait_overflow;
756 wake_up(&audit_backlog_wait);
David Woodhousefb19b4c2005-05-19 14:55:56 +0100757 return NULL;
758 }
759
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100760 ab = audit_buffer_alloc(ctx, gfp_mask, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (!ab) {
762 audit_log_lost("out of memory in audit_log_start");
763 return NULL;
764 }
765
David Woodhousebfb44962005-05-21 21:08:09 +0100766 audit_get_stamp(ab->ctx, &t, &serial);
Chris Wright197c69c2005-05-11 10:54:05 +0100767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 audit_log_format(ab, "audit(%lu.%03lu:%u): ",
769 t.tv_sec, t.tv_nsec/1000000, serial);
770 return ab;
771}
772
Chris Wright8fc61152005-05-06 15:54:17 +0100773/**
Chris Wright5ac52f32005-05-06 15:54:53 +0100774 * audit_expand - expand skb in the audit buffer
Chris Wright8fc61152005-05-06 15:54:17 +0100775 * @ab: audit_buffer
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700776 * @extra: space to add at tail of the skb
Chris Wright8fc61152005-05-06 15:54:17 +0100777 *
778 * Returns 0 (no space) on failed expansion, or available space if
779 * successful.
780 */
David Woodhousee3b926b2005-05-10 18:56:08 +0100781static inline int audit_expand(struct audit_buffer *ab, int extra)
Chris Wright8fc61152005-05-06 15:54:17 +0100782{
Chris Wright5ac52f32005-05-06 15:54:53 +0100783 struct sk_buff *skb = ab->skb;
David Woodhousee3b926b2005-05-10 18:56:08 +0100784 int ret = pskb_expand_head(skb, skb_headroom(skb), extra,
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100785 ab->gfp_mask);
Chris Wright5ac52f32005-05-06 15:54:53 +0100786 if (ret < 0) {
787 audit_log_lost("out of memory in audit_expand");
Chris Wright8fc61152005-05-06 15:54:17 +0100788 return 0;
Chris Wright5ac52f32005-05-06 15:54:53 +0100789 }
790 return skb_tailroom(skb);
Chris Wright8fc61152005-05-06 15:54:17 +0100791}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700793/*
794 * Format an audit message into the audit buffer. If there isn't enough
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 * room in the audit buffer, more room will be allocated and vsnprint
796 * will be called a second time. Currently, we assume that a printk
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700797 * can't format message larger than 1024 bytes, so we don't either.
798 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
800 va_list args)
801{
802 int len, avail;
Chris Wright5ac52f32005-05-06 15:54:53 +0100803 struct sk_buff *skb;
David Woodhouseeecb0a72005-05-10 18:58:51 +0100804 va_list args2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 if (!ab)
807 return;
808
Chris Wright5ac52f32005-05-06 15:54:53 +0100809 BUG_ON(!ab->skb);
810 skb = ab->skb;
811 avail = skb_tailroom(skb);
812 if (avail == 0) {
David Woodhousee3b926b2005-05-10 18:56:08 +0100813 avail = audit_expand(ab, AUDIT_BUFSIZ);
Chris Wright8fc61152005-05-06 15:54:17 +0100814 if (!avail)
815 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
David Woodhouseeecb0a72005-05-10 18:58:51 +0100817 va_copy(args2, args);
Chris Wright5ac52f32005-05-06 15:54:53 +0100818 len = vsnprintf(skb->tail, avail, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (len >= avail) {
820 /* The printk buffer is 1024 bytes long, so if we get
821 * here and AUDIT_BUFSIZ is at least 1024, then we can
822 * log everything that printk could have logged. */
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700823 avail = audit_expand(ab,
824 max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
Chris Wright8fc61152005-05-06 15:54:17 +0100825 if (!avail)
826 goto out;
David Woodhouseeecb0a72005-05-10 18:58:51 +0100827 len = vsnprintf(skb->tail, avail, fmt, args2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
Steve Grubb168b7172005-05-19 10:24:22 +0100829 if (len > 0)
830 skb_put(skb, len);
Chris Wright8fc61152005-05-06 15:54:17 +0100831out:
832 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700835/**
836 * audit_log_format - format a message into the audit buffer.
837 * @ab: audit_buffer
838 * @fmt: format string
839 * @...: optional parameters matching @fmt string
840 *
841 * All the work is done in audit_log_vformat.
842 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
844{
845 va_list args;
846
847 if (!ab)
848 return;
849 va_start(args, fmt);
850 audit_log_vformat(ab, fmt, args);
851 va_end(args);
852}
853
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700854/**
855 * audit_log_hex - convert a buffer to hex and append it to the audit skb
856 * @ab: the audit_buffer
857 * @buf: buffer to convert to hex
858 * @len: length of @buf to be converted
859 *
860 * No return value; failure to expand is silently ignored.
861 *
862 * This function will take the passed buf and convert it into a string of
863 * ascii hex digits. The new string is placed onto the skb.
864 */
865void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf,
Steve Grubb168b7172005-05-19 10:24:22 +0100866 size_t len)
83c7d092005-04-29 15:54:44 +0100867{
Steve Grubb168b7172005-05-19 10:24:22 +0100868 int i, avail, new_len;
869 unsigned char *ptr;
870 struct sk_buff *skb;
871 static const unsigned char *hex = "0123456789ABCDEF";
83c7d092005-04-29 15:54:44 +0100872
Steve Grubb168b7172005-05-19 10:24:22 +0100873 BUG_ON(!ab->skb);
874 skb = ab->skb;
875 avail = skb_tailroom(skb);
876 new_len = len<<1;
877 if (new_len >= avail) {
878 /* Round the buffer request up to the next multiple */
879 new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
880 avail = audit_expand(ab, new_len);
881 if (!avail)
882 return;
883 }
884
885 ptr = skb->tail;
886 for (i=0; i<len; i++) {
887 *ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */
888 *ptr++ = hex[buf[i] & 0x0F]; /* Lower nibble */
889 }
890 *ptr = 0;
891 skb_put(skb, len << 1); /* new string is twice the old string */
83c7d092005-04-29 15:54:44 +0100892}
893
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700894/**
895 * audit_log_unstrustedstring - log a string that may contain random characters
896 * @ab: audit_buffer
897 * @string: string to be logged
898 *
899 * This code will escape a string that is passed to it if the string
900 * contains a control character, unprintable character, double quote mark,
Steve Grubb168b7172005-05-19 10:24:22 +0100901 * or a space. Unescaped strings will start and end with a double quote mark.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700902 * Strings that are escaped are printed in hex (2 digits per char).
903 */
83c7d092005-04-29 15:54:44 +0100904void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
905{
Andrew Morton81b78542005-04-29 15:59:11 +0100906 const unsigned char *p = string;
83c7d092005-04-29 15:54:44 +0100907
908 while (*p) {
Steve Grubb168b7172005-05-19 10:24:22 +0100909 if (*p == '"' || *p < 0x21 || *p > 0x7f) {
83c7d092005-04-29 15:54:44 +0100910 audit_log_hex(ab, string, strlen(string));
911 return;
912 }
913 p++;
914 }
915 audit_log_format(ab, "\"%s\"", string);
916}
917
Steve Grubb168b7172005-05-19 10:24:22 +0100918/* This is a helper-function to print the escaped d_path */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
920 struct dentry *dentry, struct vfsmount *vfsmnt)
921{
Steve Grubb168b7172005-05-19 10:24:22 +0100922 char *p, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
Chris Wright8fc61152005-05-06 15:54:17 +0100924 if (prefix)
925 audit_log_format(ab, " %s", prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
Steve Grubb168b7172005-05-19 10:24:22 +0100927 /* We will allow 11 spaces for ' (deleted)' to be appended */
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100928 path = kmalloc(PATH_MAX+11, ab->gfp_mask);
Steve Grubb168b7172005-05-19 10:24:22 +0100929 if (!path) {
930 audit_log_format(ab, "<no memory>");
931 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
Steve Grubb168b7172005-05-19 10:24:22 +0100933 p = d_path(dentry, vfsmnt, path, PATH_MAX+11);
934 if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
935 /* FIXME: can we save some information here? */
936 audit_log_format(ab, "<too long>");
937 } else
938 audit_log_untrustedstring(ab, p);
939 kfree(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940}
941
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700942/**
943 * audit_log_end - end one audit record
944 * @ab: the audit_buffer
945 *
946 * The netlink_* functions cannot be called inside an irq context, so
947 * the audit buffer is placed on a queue and a tasklet is scheduled to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 * remove them from the queue outside the irq context. May be called in
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700949 * any context.
950 */
David Woodhouseb7d11252005-05-19 10:56:58 +0100951void audit_log_end(struct audit_buffer *ab)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 if (!ab)
954 return;
955 if (!audit_rate_check()) {
956 audit_log_lost("rate limit exceeded");
957 } else {
David Woodhouseb7d11252005-05-19 10:56:58 +0100958 if (audit_pid) {
959 struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
960 nlh->nlmsg_len = ab->skb->len - NLMSG_SPACE(0);
961 skb_queue_tail(&audit_skb_queue, ab->skb);
962 ab->skb = NULL;
963 wake_up_interruptible(&kauditd_wait);
964 } else {
David Woodhousee1b09eb2005-06-24 17:24:11 +0100965 printk(KERN_NOTICE "%s\n", ab->skb->data + NLMSG_SPACE(0));
David Woodhouseb7d11252005-05-19 10:56:58 +0100966 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
Chris Wright16e19042005-05-06 15:53:34 +0100968 audit_buffer_free(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969}
970
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700971/**
972 * audit_log - Log an audit record
973 * @ctx: audit context
974 * @gfp_mask: type of allocation
975 * @type: audit message type
976 * @fmt: format string to use
977 * @...: variable parameters matching the format string
978 *
979 * This is a convenience function that calls audit_log_start,
980 * audit_log_vformat, and audit_log_end. It may be called
981 * in any context.
982 */
Al Viro9796fdd2005-10-21 03:22:03 -0400983void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100984 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
986 struct audit_buffer *ab;
987 va_list args;
988
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100989 ab = audit_log_start(ctx, gfp_mask, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (ab) {
991 va_start(args, fmt);
992 audit_log_vformat(ab, fmt, args);
993 va_end(args);
994 audit_log_end(ab);
995 }
996}