blob: bf74bf02aa4be262d40f9737aff28b996a66ca47 [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
Steve Grubbce29b682006-04-01 18:29:34 -0500233static int audit_set_rate_limit(int limit, uid_t loginuid, u32 sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Steve Grubbce29b682006-04-01 18:29:34 -0500235 int old = audit_rate_limit;
236
237 if (sid) {
238 char *ctx = NULL;
239 u32 len;
240 int rc;
241 if ((rc = selinux_ctxid_to_string(sid, &ctx, &len)))
242 return rc;
243 else
244 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
245 "audit_rate_limit=%d old=%d by auid=%u subj=%s",
246 limit, old, loginuid, ctx);
247 kfree(ctx);
248 } else
249 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100250 "audit_rate_limit=%d old=%d by auid=%u",
Steve Grubbce29b682006-04-01 18:29:34 -0500251 limit, old, loginuid);
252 audit_rate_limit = limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 return old;
254}
255
Steve Grubbce29b682006-04-01 18:29:34 -0500256static int audit_set_backlog_limit(int limit, uid_t loginuid, u32 sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257{
Steve Grubbce29b682006-04-01 18:29:34 -0500258 int old = audit_backlog_limit;
259
260 if (sid) {
261 char *ctx = NULL;
262 u32 len;
263 int rc;
264 if ((rc = selinux_ctxid_to_string(sid, &ctx, &len)))
265 return rc;
266 else
267 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
268 "audit_backlog_limit=%d old=%d by auid=%u subj=%s",
269 limit, old, loginuid, ctx);
270 kfree(ctx);
271 } else
272 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100273 "audit_backlog_limit=%d old=%d by auid=%u",
Steve Grubbce29b682006-04-01 18:29:34 -0500274 limit, old, loginuid);
275 audit_backlog_limit = limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return old;
277}
278
Steve Grubbce29b682006-04-01 18:29:34 -0500279static int audit_set_enabled(int state, uid_t loginuid, u32 sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
Steve Grubbce29b682006-04-01 18:29:34 -0500281 int old = audit_enabled;
282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (state != 0 && state != 1)
284 return -EINVAL;
Steve Grubbce29b682006-04-01 18:29:34 -0500285
286 if (sid) {
287 char *ctx = NULL;
288 u32 len;
289 int rc;
290 if ((rc = selinux_ctxid_to_string(sid, &ctx, &len)))
291 return rc;
292 else
293 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
294 "audit_enabled=%d old=%d by auid=%u subj=%s",
295 state, old, loginuid, ctx);
296 kfree(ctx);
297 } else
298 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100299 "audit_enabled=%d old=%d by auid=%u",
Steve Grubbce29b682006-04-01 18:29:34 -0500300 state, old, loginuid);
301 audit_enabled = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 return old;
303}
304
Steve Grubbce29b682006-04-01 18:29:34 -0500305static int audit_set_failure(int state, uid_t loginuid, u32 sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
Steve Grubbce29b682006-04-01 18:29:34 -0500307 int old = audit_failure;
308
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 if (state != AUDIT_FAIL_SILENT
310 && state != AUDIT_FAIL_PRINTK
311 && state != AUDIT_FAIL_PANIC)
312 return -EINVAL;
Steve Grubbce29b682006-04-01 18:29:34 -0500313
314 if (sid) {
315 char *ctx = NULL;
316 u32 len;
317 int rc;
318 if ((rc = selinux_ctxid_to_string(sid, &ctx, &len)))
319 return rc;
320 else
321 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
322 "audit_failure=%d old=%d by auid=%u subj=%s",
323 state, old, loginuid, ctx);
324 kfree(ctx);
325 } else
326 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100327 "audit_failure=%d old=%d by auid=%u",
Steve Grubbce29b682006-04-01 18:29:34 -0500328 state, old, loginuid);
329 audit_failure = state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return old;
331}
332
Adrian Bunk97a41e22006-01-08 01:02:17 -0800333static int kauditd_thread(void *dummy)
David Woodhouseb7d11252005-05-19 10:56:58 +0100334{
335 struct sk_buff *skb;
336
337 while (1) {
338 skb = skb_dequeue(&audit_skb_queue);
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100339 wake_up(&audit_backlog_wait);
David Woodhouseb7d11252005-05-19 10:56:58 +0100340 if (skb) {
341 if (audit_pid) {
342 int err = netlink_unicast(audit_sock, skb, audit_pid, 0);
343 if (err < 0) {
344 BUG_ON(err != -ECONNREFUSED); /* Shoudn't happen */
345 printk(KERN_ERR "audit: *NO* daemon at audit_pid=%d\n", audit_pid);
346 audit_pid = 0;
347 }
348 } else {
David Woodhousee1b09eb2005-06-24 17:24:11 +0100349 printk(KERN_NOTICE "%s\n", skb->data + NLMSG_SPACE(0));
David Woodhouseb7d11252005-05-19 10:56:58 +0100350 kfree_skb(skb);
351 }
352 } else {
353 DECLARE_WAITQUEUE(wait, current);
354 set_current_state(TASK_INTERRUPTIBLE);
355 add_wait_queue(&kauditd_wait, &wait);
356
Pierre Ossman7a4ae742005-12-12 00:37:22 -0800357 if (!skb_queue_len(&audit_skb_queue)) {
358 try_to_freeze();
David Woodhouseb7d11252005-05-19 10:56:58 +0100359 schedule();
Pierre Ossman7a4ae742005-12-12 00:37:22 -0800360 }
David Woodhouseb7d11252005-05-19 10:56:58 +0100361
362 __set_current_state(TASK_RUNNING);
363 remove_wait_queue(&kauditd_wait, &wait);
364 }
365 }
David Woodhousefe7752b2005-12-15 18:33:52 +0000366 return 0;
David Woodhouseb7d11252005-05-19 10:56:58 +0100367}
368
Al Viro9044e6b2006-05-22 01:09:24 -0400369int audit_send_list(void *_dest)
370{
371 struct audit_netlink_list *dest = _dest;
372 int pid = dest->pid;
373 struct sk_buff *skb;
374
375 /* wait for parent to finish and send an ACK */
376 mutex_lock(&audit_netlink_mutex);
377 mutex_unlock(&audit_netlink_mutex);
378
379 while ((skb = __skb_dequeue(&dest->q)) != NULL)
380 netlink_unicast(audit_sock, skb, pid, 0);
381
382 kfree(dest);
383
384 return 0;
385}
386
387struct sk_buff *audit_make_reply(int pid, int seq, int type, int done,
388 int multi, void *payload, int size)
389{
390 struct sk_buff *skb;
391 struct nlmsghdr *nlh;
392 int len = NLMSG_SPACE(size);
393 void *data;
394 int flags = multi ? NLM_F_MULTI : 0;
395 int t = done ? NLMSG_DONE : type;
396
397 skb = alloc_skb(len, GFP_KERNEL);
398 if (!skb)
399 return NULL;
400
401 nlh = NLMSG_PUT(skb, pid, seq, t, size);
402 nlh->nlmsg_flags = flags;
403 data = NLMSG_DATA(nlh);
404 memcpy(data, payload, size);
405 return skb;
406
407nlmsg_failure: /* Used by NLMSG_PUT */
408 if (skb)
409 kfree_skb(skb);
410 return NULL;
411}
412
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700413/**
414 * audit_send_reply - send an audit reply message via netlink
415 * @pid: process id to send reply to
416 * @seq: sequence number
417 * @type: audit message type
418 * @done: done (last) flag
419 * @multi: multi-part message flag
420 * @payload: payload data
421 * @size: payload size
422 *
423 * Allocates an skb, builds the netlink message, and sends it to the pid.
424 * No failure notifications.
425 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426void audit_send_reply(int pid, int seq, int type, int done, int multi,
427 void *payload, int size)
428{
429 struct sk_buff *skb;
Al Viro9044e6b2006-05-22 01:09:24 -0400430 skb = audit_make_reply(pid, seq, type, done, multi, payload, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (!skb)
David Woodhouseb7d11252005-05-19 10:56:58 +0100432 return;
David Woodhouseb7d11252005-05-19 10:56:58 +0100433 /* Ignore failure. It'll only happen if the sender goes away,
434 because our timeout is set to infinite. */
435 netlink_unicast(audit_sock, skb, pid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437}
438
439/*
440 * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
441 * control messages.
442 */
443static int audit_netlink_ok(kernel_cap_t eff_cap, u16 msg_type)
444{
445 int err = 0;
446
447 switch (msg_type) {
448 case AUDIT_GET:
449 case AUDIT_LIST:
Amy Griffis93315ed2006-02-07 12:05:27 -0500450 case AUDIT_LIST_RULES:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 case AUDIT_SET:
452 case AUDIT_ADD:
Amy Griffis93315ed2006-02-07 12:05:27 -0500453 case AUDIT_ADD_RULE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 case AUDIT_DEL:
Amy Griffis93315ed2006-02-07 12:05:27 -0500455 case AUDIT_DEL_RULE:
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100456 case AUDIT_SIGNAL_INFO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL))
458 err = -EPERM;
459 break;
Steve Grubb05474102005-05-21 00:18:37 +0100460 case AUDIT_USER:
David Woodhouse209aba02005-05-18 10:21:07 +0100461 case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
Steve Grubb90d526c2005-11-03 15:48:08 +0000462 case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (!cap_raised(eff_cap, CAP_AUDIT_WRITE))
464 err = -EPERM;
465 break;
466 default: /* bad msg */
467 err = -EINVAL;
468 }
469
470 return err;
471}
472
473static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
474{
Steve Grubbe7c34972006-04-03 09:08:13 -0400475 u32 uid, pid, seq, sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 void *data;
477 struct audit_status *status_get, status_set;
478 int err;
Steve Grubbc0404992005-05-13 18:17:42 +0100479 struct audit_buffer *ab;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 u16 msg_type = nlh->nlmsg_type;
Serge Hallync94c2572005-04-29 16:27:17 +0100481 uid_t loginuid; /* loginuid of sender */
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100482 struct audit_sig_info sig_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type);
485 if (err)
486 return err;
487
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700488 /* As soon as there's any sign of userspace auditd,
489 * start kauditd to talk to it */
David Woodhouseb7d11252005-05-19 10:56:58 +0100490 if (!kauditd_task)
491 kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd");
492 if (IS_ERR(kauditd_task)) {
493 err = PTR_ERR(kauditd_task);
494 kauditd_task = NULL;
495 return err;
496 }
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 pid = NETLINK_CREDS(skb)->pid;
499 uid = NETLINK_CREDS(skb)->uid;
Serge Hallync94c2572005-04-29 16:27:17 +0100500 loginuid = NETLINK_CB(skb).loginuid;
Steve Grubbe7c34972006-04-03 09:08:13 -0400501 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 seq = nlh->nlmsg_seq;
503 data = NLMSG_DATA(nlh);
504
505 switch (msg_type) {
506 case AUDIT_GET:
507 status_set.enabled = audit_enabled;
508 status_set.failure = audit_failure;
509 status_set.pid = audit_pid;
510 status_set.rate_limit = audit_rate_limit;
511 status_set.backlog_limit = audit_backlog_limit;
512 status_set.lost = atomic_read(&audit_lost);
David Woodhouseb7d11252005-05-19 10:56:58 +0100513 status_set.backlog = skb_queue_len(&audit_skb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
515 &status_set, sizeof(status_set));
516 break;
517 case AUDIT_SET:
518 if (nlh->nlmsg_len < sizeof(struct audit_status))
519 return -EINVAL;
520 status_get = (struct audit_status *)data;
521 if (status_get->mask & AUDIT_STATUS_ENABLED) {
Steve Grubbce29b682006-04-01 18:29:34 -0500522 err = audit_set_enabled(status_get->enabled,
523 loginuid, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (err < 0) return err;
525 }
526 if (status_get->mask & AUDIT_STATUS_FAILURE) {
Steve Grubbce29b682006-04-01 18:29:34 -0500527 err = audit_set_failure(status_get->failure,
528 loginuid, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 if (err < 0) return err;
530 }
531 if (status_get->mask & AUDIT_STATUS_PID) {
532 int old = audit_pid;
Steve Grubbce29b682006-04-01 18:29:34 -0500533 if (sid) {
534 char *ctx = NULL;
535 u32 len;
536 int rc;
537 if ((rc = selinux_ctxid_to_string(
538 sid, &ctx, &len)))
539 return rc;
540 else
541 audit_log(NULL, GFP_KERNEL,
542 AUDIT_CONFIG_CHANGE,
543 "audit_pid=%d old=%d by auid=%u subj=%s",
544 status_get->pid, old,
545 loginuid, ctx);
546 kfree(ctx);
547 } else
548 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
549 "audit_pid=%d old=%d by auid=%u",
550 status_get->pid, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 audit_pid = status_get->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553 if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
Steve Grubbce29b682006-04-01 18:29:34 -0500554 audit_set_rate_limit(status_get->rate_limit,
555 loginuid, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
Serge Hallync94c2572005-04-29 16:27:17 +0100557 audit_set_backlog_limit(status_get->backlog_limit,
Steve Grubbce29b682006-04-01 18:29:34 -0500558 loginuid, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 break;
Steve Grubb05474102005-05-21 00:18:37 +0100560 case AUDIT_USER:
David Woodhouse209aba02005-05-18 10:21:07 +0100561 case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
Steve Grubb90d526c2005-11-03 15:48:08 +0000562 case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2:
David Woodhouse4a4cd632005-06-22 14:56:47 +0100563 if (!audit_enabled && msg_type != AUDIT_USER_AVC)
564 return 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100565
David Woodhouse5bb289b2005-06-24 14:14:05 +0100566 err = audit_filter_user(&NETLINK_CB(skb), msg_type);
David Woodhouse4a4cd632005-06-22 14:56:47 +0100567 if (err == 1) {
568 err = 0;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100569 ab = audit_log_start(NULL, GFP_KERNEL, msg_type);
David Woodhouse4a4cd632005-06-22 14:56:47 +0100570 if (ab) {
571 audit_log_format(ab,
Steve Grubbe7c34972006-04-03 09:08:13 -0400572 "user pid=%d uid=%u auid=%u",
573 pid, uid, loginuid);
574 if (sid) {
575 char *ctx = NULL;
576 u32 len;
577 if (selinux_ctxid_to_string(
578 sid, &ctx, &len)) {
579 audit_log_format(ab,
Steve Grubbce29b682006-04-01 18:29:34 -0500580 " ssid=%u", sid);
Steve Grubbe7c34972006-04-03 09:08:13 -0400581 /* Maybe call audit_panic? */
582 } else
583 audit_log_format(ab,
584 " subj=%s", ctx);
585 kfree(ctx);
586 }
587 audit_log_format(ab, " msg='%.1024s'",
588 (char *)data);
David Woodhouse4a4cd632005-06-22 14:56:47 +0100589 audit_set_pid(ab, pid);
590 audit_log_end(ab);
591 }
David Woodhouse0f45aa12005-06-19 19:35:50 +0100592 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 break;
594 case AUDIT_ADD:
595 case AUDIT_DEL:
Amy Griffis93315ed2006-02-07 12:05:27 -0500596 if (nlmsg_len(nlh) < sizeof(struct audit_rule))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return -EINVAL;
598 /* fallthrough */
599 case AUDIT_LIST:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
Amy Griffis93315ed2006-02-07 12:05:27 -0500601 uid, seq, data, nlmsg_len(nlh),
Steve Grubbce29b682006-04-01 18:29:34 -0500602 loginuid, sid);
Amy Griffis93315ed2006-02-07 12:05:27 -0500603 break;
604 case AUDIT_ADD_RULE:
605 case AUDIT_DEL_RULE:
606 if (nlmsg_len(nlh) < sizeof(struct audit_rule_data))
607 return -EINVAL;
608 /* fallthrough */
609 case AUDIT_LIST_RULES:
610 err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
611 uid, seq, data, nlmsg_len(nlh),
Steve Grubbce29b682006-04-01 18:29:34 -0500612 loginuid, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 break;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100614 case AUDIT_SIGNAL_INFO:
615 sig_data.uid = audit_sig_uid;
616 sig_data.pid = audit_sig_pid;
617 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO,
618 0, 0, &sig_data, sizeof(sig_data));
619 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 default:
621 err = -EINVAL;
622 break;
623 }
624
625 return err < 0 ? err : 0;
626}
627
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700628/*
629 * Get message from skb (based on rtnetlink_rcv_skb). Each message is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 * processed by audit_receive_msg. Malformed skbs with wrong length are
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700631 * discarded silently.
632 */
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700633static void audit_receive_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
635 int err;
636 struct nlmsghdr *nlh;
637 u32 rlen;
638
639 while (skb->len >= NLMSG_SPACE(0)) {
640 nlh = (struct nlmsghdr *)skb->data;
641 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700642 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
644 if (rlen > skb->len)
645 rlen = skb->len;
646 if ((err = audit_receive_msg(skb, nlh))) {
647 netlink_ack(skb, nlh, err);
648 } else if (nlh->nlmsg_flags & NLM_F_ACK)
649 netlink_ack(skb, nlh, 0);
650 skb_pull(skb, rlen);
651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
654/* Receive messages from netlink socket. */
655static void audit_receive(struct sock *sk, int length)
656{
657 struct sk_buff *skb;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700658 unsigned int qlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Ingo Molnar5a0bbce2006-03-07 23:51:38 -0800660 mutex_lock(&audit_netlink_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700662 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
663 skb = skb_dequeue(&sk->sk_receive_queue);
664 audit_receive_skb(skb);
665 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
Ingo Molnar5a0bbce2006-03-07 23:51:38 -0800667 mutex_unlock(&audit_netlink_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671/* Initialize audit support at boot time. */
672static int __init audit_init(void)
673{
674 printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
675 audit_default ? "enabled" : "disabled");
Patrick McHardy06628602005-08-15 12:33:26 -0700676 audit_sock = netlink_kernel_create(NETLINK_AUDIT, 0, audit_receive,
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700677 THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (!audit_sock)
679 audit_panic("cannot initialize netlink socket");
Amy Griffis71e1c782006-03-06 22:40:05 -0500680 else
681 audit_sock->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
David Woodhouseb7d11252005-05-19 10:56:58 +0100683 skb_queue_head_init(&audit_skb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 audit_initialized = 1;
685 audit_enabled = audit_default;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600686
687 /* Register the callback with selinux. This callback will be invoked
688 * when a new policy is loaded. */
689 selinux_audit_set_callback(&selinux_audit_rule_update);
690
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100691 audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, "initialized");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 return 0;
693}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694__initcall(audit_init);
695
696/* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
697static int __init audit_enable(char *str)
698{
699 audit_default = !!simple_strtol(str, NULL, 0);
700 printk(KERN_INFO "audit: %s%s\n",
701 audit_default ? "enabled" : "disabled",
702 audit_initialized ? "" : " (after initialization)");
703 if (audit_initialized)
704 audit_enabled = audit_default;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800705 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
708__setup("audit=", audit_enable);
709
Chris Wright16e19042005-05-06 15:53:34 +0100710static void audit_buffer_free(struct audit_buffer *ab)
711{
712 unsigned long flags;
713
Chris Wright8fc61152005-05-06 15:54:17 +0100714 if (!ab)
715 return;
716
Chris Wright5ac52f32005-05-06 15:54:53 +0100717 if (ab->skb)
718 kfree_skb(ab->skb);
David Woodhouseb7d11252005-05-19 10:56:58 +0100719
Chris Wright16e19042005-05-06 15:53:34 +0100720 spin_lock_irqsave(&audit_freelist_lock, flags);
721 if (++audit_freelist_count > AUDIT_MAXFREE)
722 kfree(ab);
723 else
724 list_add(&ab->list, &audit_freelist);
725 spin_unlock_irqrestore(&audit_freelist_lock, flags);
726}
727
Steve Grubbc0404992005-05-13 18:17:42 +0100728static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
Al Virodd0fc662005-10-07 07:46:04 +0100729 gfp_t gfp_mask, int type)
Chris Wright16e19042005-05-06 15:53:34 +0100730{
731 unsigned long flags;
732 struct audit_buffer *ab = NULL;
Steve Grubbc0404992005-05-13 18:17:42 +0100733 struct nlmsghdr *nlh;
Chris Wright16e19042005-05-06 15:53:34 +0100734
735 spin_lock_irqsave(&audit_freelist_lock, flags);
736 if (!list_empty(&audit_freelist)) {
737 ab = list_entry(audit_freelist.next,
738 struct audit_buffer, list);
739 list_del(&ab->list);
740 --audit_freelist_count;
741 }
742 spin_unlock_irqrestore(&audit_freelist_lock, flags);
743
744 if (!ab) {
David Woodhouse4332bdd2005-05-06 15:59:57 +0100745 ab = kmalloc(sizeof(*ab), gfp_mask);
Chris Wright16e19042005-05-06 15:53:34 +0100746 if (!ab)
Chris Wright8fc61152005-05-06 15:54:17 +0100747 goto err;
Chris Wright16e19042005-05-06 15:53:34 +0100748 }
Chris Wright8fc61152005-05-06 15:54:17 +0100749
David Woodhouse4332bdd2005-05-06 15:59:57 +0100750 ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask);
Chris Wright5ac52f32005-05-06 15:54:53 +0100751 if (!ab->skb)
Chris Wright8fc61152005-05-06 15:54:17 +0100752 goto err;
753
David Woodhouseb7d11252005-05-19 10:56:58 +0100754 ab->ctx = ctx;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100755 ab->gfp_mask = gfp_mask;
Steve Grubbc0404992005-05-13 18:17:42 +0100756 nlh = (struct nlmsghdr *)skb_put(ab->skb, NLMSG_SPACE(0));
757 nlh->nlmsg_type = type;
758 nlh->nlmsg_flags = 0;
759 nlh->nlmsg_pid = 0;
760 nlh->nlmsg_seq = 0;
Chris Wright16e19042005-05-06 15:53:34 +0100761 return ab;
Chris Wright8fc61152005-05-06 15:54:17 +0100762err:
763 audit_buffer_free(ab);
764 return NULL;
Chris Wright16e19042005-05-06 15:53:34 +0100765}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700767/**
768 * audit_serial - compute a serial number for the audit record
769 *
770 * Compute a serial number for the audit record. Audit records are
David Woodhousebfb44962005-05-21 21:08:09 +0100771 * written to user-space as soon as they are generated, so a complete
772 * audit record may be written in several pieces. The timestamp of the
773 * record and this serial number are used by the user-space tools to
774 * determine which pieces belong to the same audit record. The
775 * (timestamp,serial) tuple is unique for each syscall and is live from
776 * syscall entry to syscall exit.
777 *
David Woodhousebfb44962005-05-21 21:08:09 +0100778 * NOTE: Another possibility is to store the formatted records off the
779 * audit context (for those records that have a context), and emit them
780 * all at syscall exit. However, this could delay the reporting of
781 * significant errors until syscall exit (or never, if the system
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700782 * halts).
783 */
David Woodhousebfb44962005-05-21 21:08:09 +0100784unsigned int audit_serial(void)
785{
David Woodhoused5b454f2005-07-15 12:56:03 +0100786 static spinlock_t serial_lock = SPIN_LOCK_UNLOCKED;
787 static unsigned int serial = 0;
David Woodhousebfb44962005-05-21 21:08:09 +0100788
David Woodhoused5b454f2005-07-15 12:56:03 +0100789 unsigned long flags;
790 unsigned int ret;
David Woodhousebfb44962005-05-21 21:08:09 +0100791
David Woodhoused5b454f2005-07-15 12:56:03 +0100792 spin_lock_irqsave(&serial_lock, flags);
David Woodhousebfb44962005-05-21 21:08:09 +0100793 do {
David Woodhousece625a82005-07-18 14:24:46 -0400794 ret = ++serial;
795 } while (unlikely(!ret));
David Woodhoused5b454f2005-07-15 12:56:03 +0100796 spin_unlock_irqrestore(&serial_lock, flags);
David Woodhousebfb44962005-05-21 21:08:09 +0100797
David Woodhoused5b454f2005-07-15 12:56:03 +0100798 return ret;
David Woodhousebfb44962005-05-21 21:08:09 +0100799}
800
801static inline void audit_get_stamp(struct audit_context *ctx,
802 struct timespec *t, unsigned int *serial)
803{
804 if (ctx)
805 auditsc_get_stamp(ctx, t, serial);
806 else {
807 *t = CURRENT_TIME;
808 *serial = audit_serial();
809 }
810}
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812/* Obtain an audit buffer. This routine does locking to obtain the
813 * audit buffer, but then no locking is required for calls to
814 * audit_log_*format. If the tsk is a task that is currently in a
815 * syscall, then the syscall is marked as auditable and an audit record
816 * will be written at syscall exit. If there is no associated task, tsk
817 * should be NULL. */
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100818
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700819/**
820 * audit_log_start - obtain an audit buffer
821 * @ctx: audit_context (may be NULL)
822 * @gfp_mask: type of allocation
823 * @type: audit message type
824 *
825 * Returns audit_buffer pointer on success or NULL on error.
826 *
827 * Obtain an audit buffer. This routine does locking to obtain the
828 * audit buffer, but then no locking is required for calls to
829 * audit_log_*format. If the task (ctx) is a task that is currently in a
830 * syscall, then the syscall is marked as auditable and an audit record
831 * will be written at syscall exit. If there is no associated task, then
832 * task context (ctx) should be NULL.
833 */
Al Viro9796fdd2005-10-21 03:22:03 -0400834struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100835 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836{
837 struct audit_buffer *ab = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 struct timespec t;
Steve Grubbd812ddb2005-04-29 16:09:52 +0100839 unsigned int serial;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100840 int reserve;
David Woodhouseac4cec42005-07-02 14:08:48 +0100841 unsigned long timeout_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 if (!audit_initialized)
844 return NULL;
845
Dustin Kirklandc8edc802005-11-03 16:12:36 +0000846 if (unlikely(audit_filter_type(type)))
847 return NULL;
848
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100849 if (gfp_mask & __GFP_WAIT)
850 reserve = 0;
851 else
852 reserve = 5; /* Allow atomic callers to go up to five
853 entries over the normal backlog limit */
854
855 while (audit_backlog_limit
856 && skb_queue_len(&audit_skb_queue) > audit_backlog_limit + reserve) {
David Woodhouseac4cec42005-07-02 14:08:48 +0100857 if (gfp_mask & __GFP_WAIT && audit_backlog_wait_time
858 && time_before(jiffies, timeout_start + audit_backlog_wait_time)) {
859
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100860 /* Wait for auditd to drain the queue a little */
861 DECLARE_WAITQUEUE(wait, current);
862 set_current_state(TASK_INTERRUPTIBLE);
863 add_wait_queue(&audit_backlog_wait, &wait);
864
865 if (audit_backlog_limit &&
866 skb_queue_len(&audit_skb_queue) > audit_backlog_limit)
David Woodhouseac4cec42005-07-02 14:08:48 +0100867 schedule_timeout(timeout_start + audit_backlog_wait_time - jiffies);
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100868
869 __set_current_state(TASK_RUNNING);
870 remove_wait_queue(&audit_backlog_wait, &wait);
David Woodhouseac4cec42005-07-02 14:08:48 +0100871 continue;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100872 }
David Woodhousefb19b4c2005-05-19 14:55:56 +0100873 if (audit_rate_check())
874 printk(KERN_WARNING
875 "audit: audit_backlog=%d > "
876 "audit_backlog_limit=%d\n",
877 skb_queue_len(&audit_skb_queue),
878 audit_backlog_limit);
879 audit_log_lost("backlog limit exceeded");
David Woodhouseac4cec42005-07-02 14:08:48 +0100880 audit_backlog_wait_time = audit_backlog_wait_overflow;
881 wake_up(&audit_backlog_wait);
David Woodhousefb19b4c2005-05-19 14:55:56 +0100882 return NULL;
883 }
884
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100885 ab = audit_buffer_alloc(ctx, gfp_mask, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (!ab) {
887 audit_log_lost("out of memory in audit_log_start");
888 return NULL;
889 }
890
David Woodhousebfb44962005-05-21 21:08:09 +0100891 audit_get_stamp(ab->ctx, &t, &serial);
Chris Wright197c69c2005-05-11 10:54:05 +0100892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 audit_log_format(ab, "audit(%lu.%03lu:%u): ",
894 t.tv_sec, t.tv_nsec/1000000, serial);
895 return ab;
896}
897
Chris Wright8fc61152005-05-06 15:54:17 +0100898/**
Chris Wright5ac52f32005-05-06 15:54:53 +0100899 * audit_expand - expand skb in the audit buffer
Chris Wright8fc61152005-05-06 15:54:17 +0100900 * @ab: audit_buffer
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700901 * @extra: space to add at tail of the skb
Chris Wright8fc61152005-05-06 15:54:17 +0100902 *
903 * Returns 0 (no space) on failed expansion, or available space if
904 * successful.
905 */
David Woodhousee3b926b2005-05-10 18:56:08 +0100906static inline int audit_expand(struct audit_buffer *ab, int extra)
Chris Wright8fc61152005-05-06 15:54:17 +0100907{
Chris Wright5ac52f32005-05-06 15:54:53 +0100908 struct sk_buff *skb = ab->skb;
David Woodhousee3b926b2005-05-10 18:56:08 +0100909 int ret = pskb_expand_head(skb, skb_headroom(skb), extra,
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100910 ab->gfp_mask);
Chris Wright5ac52f32005-05-06 15:54:53 +0100911 if (ret < 0) {
912 audit_log_lost("out of memory in audit_expand");
Chris Wright8fc61152005-05-06 15:54:17 +0100913 return 0;
Chris Wright5ac52f32005-05-06 15:54:53 +0100914 }
915 return skb_tailroom(skb);
Chris Wright8fc61152005-05-06 15:54:17 +0100916}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700918/*
919 * Format an audit message into the audit buffer. If there isn't enough
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 * room in the audit buffer, more room will be allocated and vsnprint
921 * will be called a second time. Currently, we assume that a printk
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700922 * can't format message larger than 1024 bytes, so we don't either.
923 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
925 va_list args)
926{
927 int len, avail;
Chris Wright5ac52f32005-05-06 15:54:53 +0100928 struct sk_buff *skb;
David Woodhouseeecb0a72005-05-10 18:58:51 +0100929 va_list args2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 if (!ab)
932 return;
933
Chris Wright5ac52f32005-05-06 15:54:53 +0100934 BUG_ON(!ab->skb);
935 skb = ab->skb;
936 avail = skb_tailroom(skb);
937 if (avail == 0) {
David Woodhousee3b926b2005-05-10 18:56:08 +0100938 avail = audit_expand(ab, AUDIT_BUFSIZ);
Chris Wright8fc61152005-05-06 15:54:17 +0100939 if (!avail)
940 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 }
David Woodhouseeecb0a72005-05-10 18:58:51 +0100942 va_copy(args2, args);
Chris Wright5ac52f32005-05-06 15:54:53 +0100943 len = vsnprintf(skb->tail, avail, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 if (len >= avail) {
945 /* The printk buffer is 1024 bytes long, so if we get
946 * here and AUDIT_BUFSIZ is at least 1024, then we can
947 * log everything that printk could have logged. */
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700948 avail = audit_expand(ab,
949 max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
Chris Wright8fc61152005-05-06 15:54:17 +0100950 if (!avail)
951 goto out;
David Woodhouseeecb0a72005-05-10 18:58:51 +0100952 len = vsnprintf(skb->tail, avail, fmt, args2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 }
Steve Grubb168b7172005-05-19 10:24:22 +0100954 if (len > 0)
955 skb_put(skb, len);
Chris Wright8fc61152005-05-06 15:54:17 +0100956out:
957 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958}
959
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700960/**
961 * audit_log_format - format a message into the audit buffer.
962 * @ab: audit_buffer
963 * @fmt: format string
964 * @...: optional parameters matching @fmt string
965 *
966 * All the work is done in audit_log_vformat.
967 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
969{
970 va_list args;
971
972 if (!ab)
973 return;
974 va_start(args, fmt);
975 audit_log_vformat(ab, fmt, args);
976 va_end(args);
977}
978
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700979/**
980 * audit_log_hex - convert a buffer to hex and append it to the audit skb
981 * @ab: the audit_buffer
982 * @buf: buffer to convert to hex
983 * @len: length of @buf to be converted
984 *
985 * No return value; failure to expand is silently ignored.
986 *
987 * This function will take the passed buf and convert it into a string of
988 * ascii hex digits. The new string is placed onto the skb.
989 */
990void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf,
Steve Grubb168b7172005-05-19 10:24:22 +0100991 size_t len)
83c7d092005-04-29 15:54:44 +0100992{
Steve Grubb168b7172005-05-19 10:24:22 +0100993 int i, avail, new_len;
994 unsigned char *ptr;
995 struct sk_buff *skb;
996 static const unsigned char *hex = "0123456789ABCDEF";
83c7d092005-04-29 15:54:44 +0100997
Steve Grubb168b7172005-05-19 10:24:22 +0100998 BUG_ON(!ab->skb);
999 skb = ab->skb;
1000 avail = skb_tailroom(skb);
1001 new_len = len<<1;
1002 if (new_len >= avail) {
1003 /* Round the buffer request up to the next multiple */
1004 new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
1005 avail = audit_expand(ab, new_len);
1006 if (!avail)
1007 return;
1008 }
1009
1010 ptr = skb->tail;
1011 for (i=0; i<len; i++) {
1012 *ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */
1013 *ptr++ = hex[buf[i] & 0x0F]; /* Lower nibble */
1014 }
1015 *ptr = 0;
1016 skb_put(skb, len << 1); /* new string is twice the old string */
83c7d092005-04-29 15:54:44 +01001017}
1018
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001019/**
1020 * audit_log_unstrustedstring - log a string that may contain random characters
1021 * @ab: audit_buffer
1022 * @string: string to be logged
1023 *
1024 * This code will escape a string that is passed to it if the string
1025 * contains a control character, unprintable character, double quote mark,
Steve Grubb168b7172005-05-19 10:24:22 +01001026 * or a space. Unescaped strings will start and end with a double quote mark.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001027 * Strings that are escaped are printed in hex (2 digits per char).
1028 */
83c7d092005-04-29 15:54:44 +01001029void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
1030{
Andrew Morton81b78542005-04-29 15:59:11 +01001031 const unsigned char *p = string;
83c7d092005-04-29 15:54:44 +01001032
1033 while (*p) {
Steve Grubb168b7172005-05-19 10:24:22 +01001034 if (*p == '"' || *p < 0x21 || *p > 0x7f) {
83c7d092005-04-29 15:54:44 +01001035 audit_log_hex(ab, string, strlen(string));
1036 return;
1037 }
1038 p++;
1039 }
1040 audit_log_format(ab, "\"%s\"", string);
1041}
1042
Steve Grubb168b7172005-05-19 10:24:22 +01001043/* This is a helper-function to print the escaped d_path */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
1045 struct dentry *dentry, struct vfsmount *vfsmnt)
1046{
Steve Grubb168b7172005-05-19 10:24:22 +01001047 char *p, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Chris Wright8fc61152005-05-06 15:54:17 +01001049 if (prefix)
1050 audit_log_format(ab, " %s", prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Steve Grubb168b7172005-05-19 10:24:22 +01001052 /* We will allow 11 spaces for ' (deleted)' to be appended */
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001053 path = kmalloc(PATH_MAX+11, ab->gfp_mask);
Steve Grubb168b7172005-05-19 10:24:22 +01001054 if (!path) {
1055 audit_log_format(ab, "<no memory>");
1056 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 }
Steve Grubb168b7172005-05-19 10:24:22 +01001058 p = d_path(dentry, vfsmnt, path, PATH_MAX+11);
1059 if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
1060 /* FIXME: can we save some information here? */
1061 audit_log_format(ab, "<too long>");
1062 } else
1063 audit_log_untrustedstring(ab, p);
1064 kfree(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065}
1066
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001067/**
1068 * audit_log_end - end one audit record
1069 * @ab: the audit_buffer
1070 *
1071 * The netlink_* functions cannot be called inside an irq context, so
1072 * the audit buffer is placed on a queue and a tasklet is scheduled to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 * remove them from the queue outside the irq context. May be called in
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001074 * any context.
1075 */
David Woodhouseb7d11252005-05-19 10:56:58 +01001076void audit_log_end(struct audit_buffer *ab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 if (!ab)
1079 return;
1080 if (!audit_rate_check()) {
1081 audit_log_lost("rate limit exceeded");
1082 } else {
David Woodhouseb7d11252005-05-19 10:56:58 +01001083 if (audit_pid) {
1084 struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
1085 nlh->nlmsg_len = ab->skb->len - NLMSG_SPACE(0);
1086 skb_queue_tail(&audit_skb_queue, ab->skb);
1087 ab->skb = NULL;
1088 wake_up_interruptible(&kauditd_wait);
1089 } else {
David Woodhousee1b09eb2005-06-24 17:24:11 +01001090 printk(KERN_NOTICE "%s\n", ab->skb->data + NLMSG_SPACE(0));
David Woodhouseb7d11252005-05-19 10:56:58 +01001091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
Chris Wright16e19042005-05-06 15:53:34 +01001093 audit_buffer_free(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094}
1095
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001096/**
1097 * audit_log - Log an audit record
1098 * @ctx: audit context
1099 * @gfp_mask: type of allocation
1100 * @type: audit message type
1101 * @fmt: format string to use
1102 * @...: variable parameters matching the format string
1103 *
1104 * This is a convenience function that calls audit_log_start,
1105 * audit_log_vformat, and audit_log_end. It may be called
1106 * in any context.
1107 */
Al Viro9796fdd2005-10-21 03:22:03 -04001108void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001109 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110{
1111 struct audit_buffer *ab;
1112 va_list args;
1113
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001114 ab = audit_log_start(ctx, gfp_mask, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (ab) {
1116 va_start(args, fmt);
1117 audit_log_vformat(ab, fmt, args);
1118 va_end(args);
1119 audit_log_end(ab);
1120 }
1121}
lorenzo@gnu.orgbf45da92006-03-09 00:33:47 +01001122
1123EXPORT_SYMBOL(audit_log_start);
1124EXPORT_SYMBOL(audit_log_end);
1125EXPORT_SYMBOL(audit_log_format);
1126EXPORT_SYMBOL(audit_log);