blob: 0738a4b290e68b292d279cc124138a0fe1cb0354 [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;
Al Viroe1396062006-05-25 10:19:47 -040092u32 audit_sig_sid = 0;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +010093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094/* Records can be lost in several ways:
95 0) [suppressed in audit_alloc]
96 1) out of memory in audit_log_start [kmalloc of struct audit_buffer]
97 2) out of memory in audit_log_move [alloc_skb]
98 3) suppressed due to audit_rate_limit
99 4) suppressed due to audit_backlog_limit
100*/
101static atomic_t audit_lost = ATOMIC_INIT(0);
102
103/* The netlink socket. */
104static struct sock *audit_sock;
105
David Woodhouseb7d11252005-05-19 10:56:58 +0100106/* The audit_freelist is a list of pre-allocated audit buffers (if more
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of
108 * being placed on the freelist). */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static DEFINE_SPINLOCK(audit_freelist_lock);
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700110static int audit_freelist_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111static LIST_HEAD(audit_freelist);
112
David Woodhouseb7d11252005-05-19 10:56:58 +0100113static struct sk_buff_head audit_skb_queue;
114static struct task_struct *kauditd_task;
115static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait);
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100116static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118/* The netlink socket is only to be read by 1 CPU, which lets us assume
Steve Grubb23f32d12005-05-13 18:35:15 +0100119 * that list additions and deletions never happen simultaneously in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 * auditsc.c */
Ingo Molnar5a0bbce2006-03-07 23:51:38 -0800121DEFINE_MUTEX(audit_netlink_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123/* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
124 * audit records. Since printk uses a 1024 byte buffer, this buffer
125 * should be at least that large. */
126#define AUDIT_BUFSIZ 1024
127
128/* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the
129 * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */
130#define AUDIT_MAXFREE (2*NR_CPUS)
131
132/* The audit_buffer is used when formatting an audit record. The caller
133 * locks briefly to get the record off the freelist or to allocate the
134 * buffer, and locks briefly to send the buffer to the netlink layer or
135 * to place it on a transmit queue. Multiple audit_buffers can be in
136 * use simultaneously. */
137struct audit_buffer {
138 struct list_head list;
Chris Wright8fc61152005-05-06 15:54:17 +0100139 struct sk_buff *skb; /* formatted skb ready to send */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 struct audit_context *ctx; /* NULL or associated context */
Al Viro9796fdd2005-10-21 03:22:03 -0400141 gfp_t gfp_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142};
143
Steve Grubbc0404992005-05-13 18:17:42 +0100144static void audit_set_pid(struct audit_buffer *ab, pid_t pid)
145{
146 struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
147 nlh->nlmsg_pid = pid;
148}
149
Dustin Kirkland8c8570f2005-11-03 17:15:16 +0000150void audit_panic(const char *message)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 switch (audit_failure)
153 {
154 case AUDIT_FAIL_SILENT:
155 break;
156 case AUDIT_FAIL_PRINTK:
157 printk(KERN_ERR "audit: %s\n", message);
158 break;
159 case AUDIT_FAIL_PANIC:
160 panic("audit: %s\n", message);
161 break;
162 }
163}
164
165static inline int audit_rate_check(void)
166{
167 static unsigned long last_check = 0;
168 static int messages = 0;
169 static DEFINE_SPINLOCK(lock);
170 unsigned long flags;
171 unsigned long now;
172 unsigned long elapsed;
173 int retval = 0;
174
175 if (!audit_rate_limit) return 1;
176
177 spin_lock_irqsave(&lock, flags);
178 if (++messages < audit_rate_limit) {
179 retval = 1;
180 } else {
181 now = jiffies;
182 elapsed = now - last_check;
183 if (elapsed > HZ) {
184 last_check = now;
185 messages = 0;
186 retval = 1;
187 }
188 }
189 spin_unlock_irqrestore(&lock, flags);
190
191 return retval;
192}
193
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700194/**
195 * audit_log_lost - conditionally log lost audit message event
196 * @message: the message stating reason for lost audit message
197 *
198 * Emit at least 1 message per second, even if audit_rate_check is
199 * throttling.
200 * Always increment the lost messages counter.
201*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202void audit_log_lost(const char *message)
203{
204 static unsigned long last_msg = 0;
205 static DEFINE_SPINLOCK(lock);
206 unsigned long flags;
207 unsigned long now;
208 int print;
209
210 atomic_inc(&audit_lost);
211
212 print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);
213
214 if (!print) {
215 spin_lock_irqsave(&lock, flags);
216 now = jiffies;
217 if (now - last_msg > HZ) {
218 print = 1;
219 last_msg = now;
220 }
221 spin_unlock_irqrestore(&lock, flags);
222 }
223
224 if (print) {
225 printk(KERN_WARNING
David Woodhouseb7d11252005-05-19 10:56:58 +0100226 "audit: audit_lost=%d audit_rate_limit=%d audit_backlog_limit=%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 atomic_read(&audit_lost),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 audit_rate_limit,
229 audit_backlog_limit);
230 audit_panic(message);
231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Steve Grubbce29b682006-04-01 18:29:34 -0500234static int audit_set_rate_limit(int limit, uid_t loginuid, u32 sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Steve Grubbce29b682006-04-01 18:29:34 -0500236 int old = audit_rate_limit;
237
238 if (sid) {
239 char *ctx = NULL;
240 u32 len;
241 int rc;
242 if ((rc = selinux_ctxid_to_string(sid, &ctx, &len)))
243 return rc;
244 else
245 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
246 "audit_rate_limit=%d old=%d by auid=%u subj=%s",
247 limit, old, loginuid, ctx);
248 kfree(ctx);
249 } else
250 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100251 "audit_rate_limit=%d old=%d by auid=%u",
Steve Grubbce29b682006-04-01 18:29:34 -0500252 limit, old, loginuid);
253 audit_rate_limit = limit;
Serge E. Hallyn5d136a02006-04-27 16:45:14 -0500254 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Steve Grubbce29b682006-04-01 18:29:34 -0500257static int audit_set_backlog_limit(int limit, uid_t loginuid, u32 sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Steve Grubbce29b682006-04-01 18:29:34 -0500259 int old = audit_backlog_limit;
260
261 if (sid) {
262 char *ctx = NULL;
263 u32 len;
264 int rc;
265 if ((rc = selinux_ctxid_to_string(sid, &ctx, &len)))
266 return rc;
267 else
268 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
269 "audit_backlog_limit=%d old=%d by auid=%u subj=%s",
270 limit, old, loginuid, ctx);
271 kfree(ctx);
272 } else
273 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100274 "audit_backlog_limit=%d old=%d by auid=%u",
Steve Grubbce29b682006-04-01 18:29:34 -0500275 limit, old, loginuid);
276 audit_backlog_limit = limit;
Serge E. Hallyn5d136a02006-04-27 16:45:14 -0500277 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278}
279
Steve Grubbce29b682006-04-01 18:29:34 -0500280static int audit_set_enabled(int state, uid_t loginuid, u32 sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Steve Grubbce29b682006-04-01 18:29:34 -0500282 int old = audit_enabled;
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 if (state != 0 && state != 1)
285 return -EINVAL;
Steve Grubbce29b682006-04-01 18:29:34 -0500286
287 if (sid) {
288 char *ctx = NULL;
289 u32 len;
290 int rc;
291 if ((rc = selinux_ctxid_to_string(sid, &ctx, &len)))
292 return rc;
293 else
294 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
295 "audit_enabled=%d old=%d by auid=%u subj=%s",
296 state, old, loginuid, ctx);
297 kfree(ctx);
298 } else
299 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100300 "audit_enabled=%d old=%d by auid=%u",
Steve Grubbce29b682006-04-01 18:29:34 -0500301 state, old, loginuid);
302 audit_enabled = state;
Serge E. Hallyn5d136a02006-04-27 16:45:14 -0500303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305
Steve Grubbce29b682006-04-01 18:29:34 -0500306static int audit_set_failure(int state, uid_t loginuid, u32 sid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Steve Grubbce29b682006-04-01 18:29:34 -0500308 int old = audit_failure;
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (state != AUDIT_FAIL_SILENT
311 && state != AUDIT_FAIL_PRINTK
312 && state != AUDIT_FAIL_PANIC)
313 return -EINVAL;
Steve Grubbce29b682006-04-01 18:29:34 -0500314
315 if (sid) {
316 char *ctx = NULL;
317 u32 len;
318 int rc;
319 if ((rc = selinux_ctxid_to_string(sid, &ctx, &len)))
320 return rc;
321 else
322 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
323 "audit_failure=%d old=%d by auid=%u subj=%s",
324 state, old, loginuid, ctx);
325 kfree(ctx);
326 } else
327 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100328 "audit_failure=%d old=%d by auid=%u",
Steve Grubbce29b682006-04-01 18:29:34 -0500329 state, old, loginuid);
330 audit_failure = state;
Serge E. Hallyn5d136a02006-04-27 16:45:14 -0500331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Adrian Bunk97a41e22006-01-08 01:02:17 -0800334static int kauditd_thread(void *dummy)
David Woodhouseb7d11252005-05-19 10:56:58 +0100335{
336 struct sk_buff *skb;
337
338 while (1) {
339 skb = skb_dequeue(&audit_skb_queue);
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100340 wake_up(&audit_backlog_wait);
David Woodhouseb7d11252005-05-19 10:56:58 +0100341 if (skb) {
342 if (audit_pid) {
343 int err = netlink_unicast(audit_sock, skb, audit_pid, 0);
344 if (err < 0) {
345 BUG_ON(err != -ECONNREFUSED); /* Shoudn't happen */
346 printk(KERN_ERR "audit: *NO* daemon at audit_pid=%d\n", audit_pid);
347 audit_pid = 0;
348 }
349 } else {
David Woodhousee1b09eb2005-06-24 17:24:11 +0100350 printk(KERN_NOTICE "%s\n", skb->data + NLMSG_SPACE(0));
David Woodhouseb7d11252005-05-19 10:56:58 +0100351 kfree_skb(skb);
352 }
353 } else {
354 DECLARE_WAITQUEUE(wait, current);
355 set_current_state(TASK_INTERRUPTIBLE);
356 add_wait_queue(&kauditd_wait, &wait);
357
Pierre Ossman7a4ae742005-12-12 00:37:22 -0800358 if (!skb_queue_len(&audit_skb_queue)) {
359 try_to_freeze();
David Woodhouseb7d11252005-05-19 10:56:58 +0100360 schedule();
Pierre Ossman7a4ae742005-12-12 00:37:22 -0800361 }
David Woodhouseb7d11252005-05-19 10:56:58 +0100362
363 __set_current_state(TASK_RUNNING);
364 remove_wait_queue(&kauditd_wait, &wait);
365 }
366 }
367}
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 */
Al Viroe1396062006-05-25 10:19:47 -0400482 struct audit_sig_info *sig_data;
483 char *ctx;
484 u32 len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type);
487 if (err)
488 return err;
489
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700490 /* As soon as there's any sign of userspace auditd,
491 * start kauditd to talk to it */
David Woodhouseb7d11252005-05-19 10:56:58 +0100492 if (!kauditd_task)
493 kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd");
494 if (IS_ERR(kauditd_task)) {
495 err = PTR_ERR(kauditd_task);
496 kauditd_task = NULL;
497 return err;
498 }
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 pid = NETLINK_CREDS(skb)->pid;
501 uid = NETLINK_CREDS(skb)->uid;
Serge Hallync94c2572005-04-29 16:27:17 +0100502 loginuid = NETLINK_CB(skb).loginuid;
Steve Grubbe7c34972006-04-03 09:08:13 -0400503 sid = NETLINK_CB(skb).sid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 seq = nlh->nlmsg_seq;
505 data = NLMSG_DATA(nlh);
506
507 switch (msg_type) {
508 case AUDIT_GET:
509 status_set.enabled = audit_enabled;
510 status_set.failure = audit_failure;
511 status_set.pid = audit_pid;
512 status_set.rate_limit = audit_rate_limit;
513 status_set.backlog_limit = audit_backlog_limit;
514 status_set.lost = atomic_read(&audit_lost);
David Woodhouseb7d11252005-05-19 10:56:58 +0100515 status_set.backlog = skb_queue_len(&audit_skb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
517 &status_set, sizeof(status_set));
518 break;
519 case AUDIT_SET:
520 if (nlh->nlmsg_len < sizeof(struct audit_status))
521 return -EINVAL;
522 status_get = (struct audit_status *)data;
523 if (status_get->mask & AUDIT_STATUS_ENABLED) {
Steve Grubbce29b682006-04-01 18:29:34 -0500524 err = audit_set_enabled(status_get->enabled,
525 loginuid, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (err < 0) return err;
527 }
528 if (status_get->mask & AUDIT_STATUS_FAILURE) {
Steve Grubbce29b682006-04-01 18:29:34 -0500529 err = audit_set_failure(status_get->failure,
530 loginuid, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (err < 0) return err;
532 }
533 if (status_get->mask & AUDIT_STATUS_PID) {
534 int old = audit_pid;
Steve Grubbce29b682006-04-01 18:29:34 -0500535 if (sid) {
Al Viroe1396062006-05-25 10:19:47 -0400536 if ((err = selinux_ctxid_to_string(
Steve Grubbce29b682006-04-01 18:29:34 -0500537 sid, &ctx, &len)))
Al Viroe1396062006-05-25 10:19:47 -0400538 return err;
Steve Grubbce29b682006-04-01 18:29:34 -0500539 else
540 audit_log(NULL, GFP_KERNEL,
541 AUDIT_CONFIG_CHANGE,
542 "audit_pid=%d old=%d by auid=%u subj=%s",
543 status_get->pid, old,
544 loginuid, ctx);
545 kfree(ctx);
546 } else
547 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
548 "audit_pid=%d old=%d by auid=%u",
549 status_get->pid, old, loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 audit_pid = status_get->pid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 }
552 if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
Serge E. Hallyn5d136a02006-04-27 16:45:14 -0500553 err = audit_set_rate_limit(status_get->rate_limit,
Steve Grubbce29b682006-04-01 18:29:34 -0500554 loginuid, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
Serge E. Hallyn5d136a02006-04-27 16:45:14 -0500556 err = audit_set_backlog_limit(status_get->backlog_limit,
Steve Grubbce29b682006-04-01 18:29:34 -0500557 loginuid, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 break;
Steve Grubb05474102005-05-21 00:18:37 +0100559 case AUDIT_USER:
David Woodhouse209aba02005-05-18 10:21:07 +0100560 case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
Steve Grubb90d526c2005-11-03 15:48:08 +0000561 case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2:
David Woodhouse4a4cd632005-06-22 14:56:47 +0100562 if (!audit_enabled && msg_type != AUDIT_USER_AVC)
563 return 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100564
David Woodhouse5bb289b2005-06-24 14:14:05 +0100565 err = audit_filter_user(&NETLINK_CB(skb), msg_type);
David Woodhouse4a4cd632005-06-22 14:56:47 +0100566 if (err == 1) {
567 err = 0;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100568 ab = audit_log_start(NULL, GFP_KERNEL, msg_type);
David Woodhouse4a4cd632005-06-22 14:56:47 +0100569 if (ab) {
570 audit_log_format(ab,
Steve Grubbe7c34972006-04-03 09:08:13 -0400571 "user pid=%d uid=%u auid=%u",
572 pid, uid, loginuid);
573 if (sid) {
Steve Grubbe7c34972006-04-03 09:08:13 -0400574 if (selinux_ctxid_to_string(
575 sid, &ctx, &len)) {
576 audit_log_format(ab,
Steve Grubbce29b682006-04-01 18:29:34 -0500577 " ssid=%u", sid);
Steve Grubbe7c34972006-04-03 09:08:13 -0400578 /* Maybe call audit_panic? */
579 } else
580 audit_log_format(ab,
581 " subj=%s", ctx);
582 kfree(ctx);
583 }
584 audit_log_format(ab, " msg='%.1024s'",
585 (char *)data);
David Woodhouse4a4cd632005-06-22 14:56:47 +0100586 audit_set_pid(ab, pid);
587 audit_log_end(ab);
588 }
David Woodhouse0f45aa12005-06-19 19:35:50 +0100589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 break;
591 case AUDIT_ADD:
592 case AUDIT_DEL:
Amy Griffis93315ed2006-02-07 12:05:27 -0500593 if (nlmsg_len(nlh) < sizeof(struct audit_rule))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return -EINVAL;
595 /* fallthrough */
596 case AUDIT_LIST:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
Amy Griffis93315ed2006-02-07 12:05:27 -0500598 uid, seq, data, nlmsg_len(nlh),
Steve Grubbce29b682006-04-01 18:29:34 -0500599 loginuid, sid);
Amy Griffis93315ed2006-02-07 12:05:27 -0500600 break;
601 case AUDIT_ADD_RULE:
602 case AUDIT_DEL_RULE:
603 if (nlmsg_len(nlh) < sizeof(struct audit_rule_data))
604 return -EINVAL;
605 /* fallthrough */
606 case AUDIT_LIST_RULES:
607 err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
608 uid, seq, data, nlmsg_len(nlh),
Steve Grubbce29b682006-04-01 18:29:34 -0500609 loginuid, sid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 break;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100611 case AUDIT_SIGNAL_INFO:
Al Viroe1396062006-05-25 10:19:47 -0400612 err = selinux_ctxid_to_string(audit_sig_sid, &ctx, &len);
613 if (err)
614 return err;
615 sig_data = kmalloc(sizeof(*sig_data) + len, GFP_KERNEL);
616 if (!sig_data) {
617 kfree(ctx);
618 return -ENOMEM;
619 }
620 sig_data->uid = audit_sig_uid;
621 sig_data->pid = audit_sig_pid;
622 memcpy(sig_data->ctx, ctx, len);
623 kfree(ctx);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100624 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO,
Al Viroe1396062006-05-25 10:19:47 -0400625 0, 0, sig_data, sizeof(*sig_data) + len);
626 kfree(sig_data);
Steve Grubbc2f0c7c2005-05-06 12:38:39 +0100627 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 default:
629 err = -EINVAL;
630 break;
631 }
632
633 return err < 0 ? err : 0;
634}
635
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700636/*
637 * Get message from skb (based on rtnetlink_rcv_skb). Each message is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 * processed by audit_receive_msg. Malformed skbs with wrong length are
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700639 * discarded silently.
640 */
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700641static void audit_receive_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
643 int err;
644 struct nlmsghdr *nlh;
645 u32 rlen;
646
647 while (skb->len >= NLMSG_SPACE(0)) {
648 nlh = (struct nlmsghdr *)skb->data;
649 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700650 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
652 if (rlen > skb->len)
653 rlen = skb->len;
654 if ((err = audit_receive_msg(skb, nlh))) {
655 netlink_ack(skb, nlh, err);
656 } else if (nlh->nlmsg_flags & NLM_F_ACK)
657 netlink_ack(skb, nlh, 0);
658 skb_pull(skb, rlen);
659 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660}
661
662/* Receive messages from netlink socket. */
663static void audit_receive(struct sock *sk, int length)
664{
665 struct sk_buff *skb;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700666 unsigned int qlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Ingo Molnar5a0bbce2006-03-07 23:51:38 -0800668 mutex_lock(&audit_netlink_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700670 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
671 skb = skb_dequeue(&sk->sk_receive_queue);
672 audit_receive_skb(skb);
673 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
Ingo Molnar5a0bbce2006-03-07 23:51:38 -0800675 mutex_unlock(&audit_netlink_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676}
677
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679/* Initialize audit support at boot time. */
680static int __init audit_init(void)
681{
682 printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
683 audit_default ? "enabled" : "disabled");
Patrick McHardy06628602005-08-15 12:33:26 -0700684 audit_sock = netlink_kernel_create(NETLINK_AUDIT, 0, audit_receive,
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700685 THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (!audit_sock)
687 audit_panic("cannot initialize netlink socket");
Amy Griffis71e1c782006-03-06 22:40:05 -0500688 else
689 audit_sock->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
David Woodhouseb7d11252005-05-19 10:56:58 +0100691 skb_queue_head_init(&audit_skb_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 audit_initialized = 1;
693 audit_enabled = audit_default;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600694
695 /* Register the callback with selinux. This callback will be invoked
696 * when a new policy is loaded. */
697 selinux_audit_set_callback(&selinux_audit_rule_update);
698
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100699 audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, "initialized");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return 0;
701}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702__initcall(audit_init);
703
704/* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
705static int __init audit_enable(char *str)
706{
707 audit_default = !!simple_strtol(str, NULL, 0);
708 printk(KERN_INFO "audit: %s%s\n",
709 audit_default ? "enabled" : "disabled",
710 audit_initialized ? "" : " (after initialization)");
711 if (audit_initialized)
712 audit_enabled = audit_default;
OGAWA Hirofumi9b410462006-03-31 02:30:33 -0800713 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714}
715
716__setup("audit=", audit_enable);
717
Chris Wright16e19042005-05-06 15:53:34 +0100718static void audit_buffer_free(struct audit_buffer *ab)
719{
720 unsigned long flags;
721
Chris Wright8fc61152005-05-06 15:54:17 +0100722 if (!ab)
723 return;
724
Chris Wright5ac52f32005-05-06 15:54:53 +0100725 if (ab->skb)
726 kfree_skb(ab->skb);
David Woodhouseb7d11252005-05-19 10:56:58 +0100727
Chris Wright16e19042005-05-06 15:53:34 +0100728 spin_lock_irqsave(&audit_freelist_lock, flags);
Serge E. Hallyn5d136a02006-04-27 16:45:14 -0500729 if (audit_freelist_count > AUDIT_MAXFREE)
Chris Wright16e19042005-05-06 15:53:34 +0100730 kfree(ab);
Serge E. Hallyn5d136a02006-04-27 16:45:14 -0500731 else {
732 audit_freelist_count++;
Chris Wright16e19042005-05-06 15:53:34 +0100733 list_add(&ab->list, &audit_freelist);
Serge E. Hallyn5d136a02006-04-27 16:45:14 -0500734 }
Chris Wright16e19042005-05-06 15:53:34 +0100735 spin_unlock_irqrestore(&audit_freelist_lock, flags);
736}
737
Steve Grubbc0404992005-05-13 18:17:42 +0100738static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
Al Virodd0fc662005-10-07 07:46:04 +0100739 gfp_t gfp_mask, int type)
Chris Wright16e19042005-05-06 15:53:34 +0100740{
741 unsigned long flags;
742 struct audit_buffer *ab = NULL;
Steve Grubbc0404992005-05-13 18:17:42 +0100743 struct nlmsghdr *nlh;
Chris Wright16e19042005-05-06 15:53:34 +0100744
745 spin_lock_irqsave(&audit_freelist_lock, flags);
746 if (!list_empty(&audit_freelist)) {
747 ab = list_entry(audit_freelist.next,
748 struct audit_buffer, list);
749 list_del(&ab->list);
750 --audit_freelist_count;
751 }
752 spin_unlock_irqrestore(&audit_freelist_lock, flags);
753
754 if (!ab) {
David Woodhouse4332bdd2005-05-06 15:59:57 +0100755 ab = kmalloc(sizeof(*ab), gfp_mask);
Chris Wright16e19042005-05-06 15:53:34 +0100756 if (!ab)
Chris Wright8fc61152005-05-06 15:54:17 +0100757 goto err;
Chris Wright16e19042005-05-06 15:53:34 +0100758 }
Chris Wright8fc61152005-05-06 15:54:17 +0100759
David Woodhouse4332bdd2005-05-06 15:59:57 +0100760 ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask);
Chris Wright5ac52f32005-05-06 15:54:53 +0100761 if (!ab->skb)
Chris Wright8fc61152005-05-06 15:54:17 +0100762 goto err;
763
David Woodhouseb7d11252005-05-19 10:56:58 +0100764 ab->ctx = ctx;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100765 ab->gfp_mask = gfp_mask;
Steve Grubbc0404992005-05-13 18:17:42 +0100766 nlh = (struct nlmsghdr *)skb_put(ab->skb, NLMSG_SPACE(0));
767 nlh->nlmsg_type = type;
768 nlh->nlmsg_flags = 0;
769 nlh->nlmsg_pid = 0;
770 nlh->nlmsg_seq = 0;
Chris Wright16e19042005-05-06 15:53:34 +0100771 return ab;
Chris Wright8fc61152005-05-06 15:54:17 +0100772err:
773 audit_buffer_free(ab);
774 return NULL;
Chris Wright16e19042005-05-06 15:53:34 +0100775}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700777/**
778 * audit_serial - compute a serial number for the audit record
779 *
780 * Compute a serial number for the audit record. Audit records are
David Woodhousebfb44962005-05-21 21:08:09 +0100781 * written to user-space as soon as they are generated, so a complete
782 * audit record may be written in several pieces. The timestamp of the
783 * record and this serial number are used by the user-space tools to
784 * determine which pieces belong to the same audit record. The
785 * (timestamp,serial) tuple is unique for each syscall and is live from
786 * syscall entry to syscall exit.
787 *
David Woodhousebfb44962005-05-21 21:08:09 +0100788 * NOTE: Another possibility is to store the formatted records off the
789 * audit context (for those records that have a context), and emit them
790 * all at syscall exit. However, this could delay the reporting of
791 * significant errors until syscall exit (or never, if the system
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700792 * halts).
793 */
David Woodhousebfb44962005-05-21 21:08:09 +0100794unsigned int audit_serial(void)
795{
David Woodhoused5b454f2005-07-15 12:56:03 +0100796 static spinlock_t serial_lock = SPIN_LOCK_UNLOCKED;
797 static unsigned int serial = 0;
David Woodhousebfb44962005-05-21 21:08:09 +0100798
David Woodhoused5b454f2005-07-15 12:56:03 +0100799 unsigned long flags;
800 unsigned int ret;
David Woodhousebfb44962005-05-21 21:08:09 +0100801
David Woodhoused5b454f2005-07-15 12:56:03 +0100802 spin_lock_irqsave(&serial_lock, flags);
David Woodhousebfb44962005-05-21 21:08:09 +0100803 do {
David Woodhousece625a82005-07-18 14:24:46 -0400804 ret = ++serial;
805 } while (unlikely(!ret));
David Woodhoused5b454f2005-07-15 12:56:03 +0100806 spin_unlock_irqrestore(&serial_lock, flags);
David Woodhousebfb44962005-05-21 21:08:09 +0100807
David Woodhoused5b454f2005-07-15 12:56:03 +0100808 return ret;
David Woodhousebfb44962005-05-21 21:08:09 +0100809}
810
811static inline void audit_get_stamp(struct audit_context *ctx,
812 struct timespec *t, unsigned int *serial)
813{
814 if (ctx)
815 auditsc_get_stamp(ctx, t, serial);
816 else {
817 *t = CURRENT_TIME;
818 *serial = audit_serial();
819 }
820}
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822/* Obtain an audit buffer. This routine does locking to obtain the
823 * audit buffer, but then no locking is required for calls to
824 * audit_log_*format. If the tsk is a task that is currently in a
825 * syscall, then the syscall is marked as auditable and an audit record
826 * will be written at syscall exit. If there is no associated task, tsk
827 * should be NULL. */
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100828
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700829/**
830 * audit_log_start - obtain an audit buffer
831 * @ctx: audit_context (may be NULL)
832 * @gfp_mask: type of allocation
833 * @type: audit message type
834 *
835 * Returns audit_buffer pointer on success or NULL on error.
836 *
837 * Obtain an audit buffer. This routine does locking to obtain the
838 * audit buffer, but then no locking is required for calls to
839 * audit_log_*format. If the task (ctx) is a task that is currently in a
840 * syscall, then the syscall is marked as auditable and an audit record
841 * will be written at syscall exit. If there is no associated task, then
842 * task context (ctx) should be NULL.
843 */
Al Viro9796fdd2005-10-21 03:22:03 -0400844struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100845 int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846{
847 struct audit_buffer *ab = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 struct timespec t;
Steve Grubbd812ddb2005-04-29 16:09:52 +0100849 unsigned int serial;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100850 int reserve;
David Woodhouseac4cec42005-07-02 14:08:48 +0100851 unsigned long timeout_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 if (!audit_initialized)
854 return NULL;
855
Dustin Kirklandc8edc802005-11-03 16:12:36 +0000856 if (unlikely(audit_filter_type(type)))
857 return NULL;
858
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100859 if (gfp_mask & __GFP_WAIT)
860 reserve = 0;
861 else
862 reserve = 5; /* Allow atomic callers to go up to five
863 entries over the normal backlog limit */
864
865 while (audit_backlog_limit
866 && skb_queue_len(&audit_skb_queue) > audit_backlog_limit + reserve) {
David Woodhouseac4cec42005-07-02 14:08:48 +0100867 if (gfp_mask & __GFP_WAIT && audit_backlog_wait_time
868 && time_before(jiffies, timeout_start + audit_backlog_wait_time)) {
869
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100870 /* Wait for auditd to drain the queue a little */
871 DECLARE_WAITQUEUE(wait, current);
872 set_current_state(TASK_INTERRUPTIBLE);
873 add_wait_queue(&audit_backlog_wait, &wait);
874
875 if (audit_backlog_limit &&
876 skb_queue_len(&audit_skb_queue) > audit_backlog_limit)
David Woodhouseac4cec42005-07-02 14:08:48 +0100877 schedule_timeout(timeout_start + audit_backlog_wait_time - jiffies);
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100878
879 __set_current_state(TASK_RUNNING);
880 remove_wait_queue(&audit_backlog_wait, &wait);
David Woodhouseac4cec42005-07-02 14:08:48 +0100881 continue;
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100882 }
David Woodhousefb19b4c2005-05-19 14:55:56 +0100883 if (audit_rate_check())
884 printk(KERN_WARNING
885 "audit: audit_backlog=%d > "
886 "audit_backlog_limit=%d\n",
887 skb_queue_len(&audit_skb_queue),
888 audit_backlog_limit);
889 audit_log_lost("backlog limit exceeded");
David Woodhouseac4cec42005-07-02 14:08:48 +0100890 audit_backlog_wait_time = audit_backlog_wait_overflow;
891 wake_up(&audit_backlog_wait);
David Woodhousefb19b4c2005-05-19 14:55:56 +0100892 return NULL;
893 }
894
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100895 ab = audit_buffer_alloc(ctx, gfp_mask, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (!ab) {
897 audit_log_lost("out of memory in audit_log_start");
898 return NULL;
899 }
900
David Woodhousebfb44962005-05-21 21:08:09 +0100901 audit_get_stamp(ab->ctx, &t, &serial);
Chris Wright197c69c2005-05-11 10:54:05 +0100902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 audit_log_format(ab, "audit(%lu.%03lu:%u): ",
904 t.tv_sec, t.tv_nsec/1000000, serial);
905 return ab;
906}
907
Chris Wright8fc61152005-05-06 15:54:17 +0100908/**
Chris Wright5ac52f32005-05-06 15:54:53 +0100909 * audit_expand - expand skb in the audit buffer
Chris Wright8fc61152005-05-06 15:54:17 +0100910 * @ab: audit_buffer
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700911 * @extra: space to add at tail of the skb
Chris Wright8fc61152005-05-06 15:54:17 +0100912 *
913 * Returns 0 (no space) on failed expansion, or available space if
914 * successful.
915 */
David Woodhousee3b926b2005-05-10 18:56:08 +0100916static inline int audit_expand(struct audit_buffer *ab, int extra)
Chris Wright8fc61152005-05-06 15:54:17 +0100917{
Chris Wright5ac52f32005-05-06 15:54:53 +0100918 struct sk_buff *skb = ab->skb;
David Woodhousee3b926b2005-05-10 18:56:08 +0100919 int ret = pskb_expand_head(skb, skb_headroom(skb), extra,
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100920 ab->gfp_mask);
Chris Wright5ac52f32005-05-06 15:54:53 +0100921 if (ret < 0) {
922 audit_log_lost("out of memory in audit_expand");
Chris Wright8fc61152005-05-06 15:54:17 +0100923 return 0;
Chris Wright5ac52f32005-05-06 15:54:53 +0100924 }
925 return skb_tailroom(skb);
Chris Wright8fc61152005-05-06 15:54:17 +0100926}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700928/*
929 * Format an audit message into the audit buffer. If there isn't enough
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 * room in the audit buffer, more room will be allocated and vsnprint
931 * will be called a second time. Currently, we assume that a printk
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700932 * can't format message larger than 1024 bytes, so we don't either.
933 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
935 va_list args)
936{
937 int len, avail;
Chris Wright5ac52f32005-05-06 15:54:53 +0100938 struct sk_buff *skb;
David Woodhouseeecb0a72005-05-10 18:58:51 +0100939 va_list args2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 if (!ab)
942 return;
943
Chris Wright5ac52f32005-05-06 15:54:53 +0100944 BUG_ON(!ab->skb);
945 skb = ab->skb;
946 avail = skb_tailroom(skb);
947 if (avail == 0) {
David Woodhousee3b926b2005-05-10 18:56:08 +0100948 avail = audit_expand(ab, AUDIT_BUFSIZ);
Chris Wright8fc61152005-05-06 15:54:17 +0100949 if (!avail)
950 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
David Woodhouseeecb0a72005-05-10 18:58:51 +0100952 va_copy(args2, args);
Chris Wright5ac52f32005-05-06 15:54:53 +0100953 len = vsnprintf(skb->tail, avail, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (len >= avail) {
955 /* The printk buffer is 1024 bytes long, so if we get
956 * here and AUDIT_BUFSIZ is at least 1024, then we can
957 * log everything that printk could have logged. */
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700958 avail = audit_expand(ab,
959 max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
Chris Wright8fc61152005-05-06 15:54:17 +0100960 if (!avail)
961 goto out;
David Woodhouseeecb0a72005-05-10 18:58:51 +0100962 len = vsnprintf(skb->tail, avail, fmt, args2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
Steve Grubb168b7172005-05-19 10:24:22 +0100964 if (len > 0)
965 skb_put(skb, len);
Chris Wright8fc61152005-05-06 15:54:17 +0100966out:
967 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968}
969
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700970/**
971 * audit_log_format - format a message into the audit buffer.
972 * @ab: audit_buffer
973 * @fmt: format string
974 * @...: optional parameters matching @fmt string
975 *
976 * All the work is done in audit_log_vformat.
977 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
979{
980 va_list args;
981
982 if (!ab)
983 return;
984 va_start(args, fmt);
985 audit_log_vformat(ab, fmt, args);
986 va_end(args);
987}
988
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700989/**
990 * audit_log_hex - convert a buffer to hex and append it to the audit skb
991 * @ab: the audit_buffer
992 * @buf: buffer to convert to hex
993 * @len: length of @buf to be converted
994 *
995 * No return value; failure to expand is silently ignored.
996 *
997 * This function will take the passed buf and convert it into a string of
998 * ascii hex digits. The new string is placed onto the skb.
999 */
1000void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf,
Steve Grubb168b7172005-05-19 10:24:22 +01001001 size_t len)
83c7d092005-04-29 15:54:44 +01001002{
Steve Grubb168b7172005-05-19 10:24:22 +01001003 int i, avail, new_len;
1004 unsigned char *ptr;
1005 struct sk_buff *skb;
1006 static const unsigned char *hex = "0123456789ABCDEF";
83c7d092005-04-29 15:54:44 +01001007
Steve Grubb168b7172005-05-19 10:24:22 +01001008 BUG_ON(!ab->skb);
1009 skb = ab->skb;
1010 avail = skb_tailroom(skb);
1011 new_len = len<<1;
1012 if (new_len >= avail) {
1013 /* Round the buffer request up to the next multiple */
1014 new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
1015 avail = audit_expand(ab, new_len);
1016 if (!avail)
1017 return;
1018 }
1019
1020 ptr = skb->tail;
1021 for (i=0; i<len; i++) {
1022 *ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */
1023 *ptr++ = hex[buf[i] & 0x0F]; /* Lower nibble */
1024 }
1025 *ptr = 0;
1026 skb_put(skb, len << 1); /* new string is twice the old string */
83c7d092005-04-29 15:54:44 +01001027}
1028
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001029/**
1030 * audit_log_unstrustedstring - log a string that may contain random characters
1031 * @ab: audit_buffer
1032 * @string: string to be logged
1033 *
1034 * This code will escape a string that is passed to it if the string
1035 * contains a control character, unprintable character, double quote mark,
Steve Grubb168b7172005-05-19 10:24:22 +01001036 * or a space. Unescaped strings will start and end with a double quote mark.
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001037 * Strings that are escaped are printed in hex (2 digits per char).
1038 */
Al Viro473ae302006-04-26 14:04:08 -04001039const char *audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
83c7d092005-04-29 15:54:44 +01001040{
Andrew Morton81b78542005-04-29 15:59:11 +01001041 const unsigned char *p = string;
Al Viro473ae302006-04-26 14:04:08 -04001042 size_t len = strlen(string);
83c7d092005-04-29 15:54:44 +01001043
1044 while (*p) {
Steve Grubb168b7172005-05-19 10:24:22 +01001045 if (*p == '"' || *p < 0x21 || *p > 0x7f) {
Al Viro473ae302006-04-26 14:04:08 -04001046 audit_log_hex(ab, string, len);
1047 return string + len + 1;
83c7d092005-04-29 15:54:44 +01001048 }
1049 p++;
1050 }
1051 audit_log_format(ab, "\"%s\"", string);
Al Viro473ae302006-04-26 14:04:08 -04001052 return p + 1;
83c7d092005-04-29 15:54:44 +01001053}
1054
Steve Grubb168b7172005-05-19 10:24:22 +01001055/* This is a helper-function to print the escaped d_path */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
1057 struct dentry *dentry, struct vfsmount *vfsmnt)
1058{
Steve Grubb168b7172005-05-19 10:24:22 +01001059 char *p, *path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Chris Wright8fc61152005-05-06 15:54:17 +01001061 if (prefix)
1062 audit_log_format(ab, " %s", prefix);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
Steve Grubb168b7172005-05-19 10:24:22 +01001064 /* We will allow 11 spaces for ' (deleted)' to be appended */
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001065 path = kmalloc(PATH_MAX+11, ab->gfp_mask);
Steve Grubb168b7172005-05-19 10:24:22 +01001066 if (!path) {
1067 audit_log_format(ab, "<no memory>");
1068 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
Steve Grubb168b7172005-05-19 10:24:22 +01001070 p = d_path(dentry, vfsmnt, path, PATH_MAX+11);
1071 if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
1072 /* FIXME: can we save some information here? */
1073 audit_log_format(ab, "<too long>");
1074 } else
1075 audit_log_untrustedstring(ab, p);
1076 kfree(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077}
1078
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001079/**
1080 * audit_log_end - end one audit record
1081 * @ab: the audit_buffer
1082 *
1083 * The netlink_* functions cannot be called inside an irq context, so
1084 * the audit buffer is placed on a queue and a tasklet is scheduled to
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 * remove them from the queue outside the irq context. May be called in
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001086 * any context.
1087 */
David Woodhouseb7d11252005-05-19 10:56:58 +01001088void audit_log_end(struct audit_buffer *ab)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 if (!ab)
1091 return;
1092 if (!audit_rate_check()) {
1093 audit_log_lost("rate limit exceeded");
1094 } else {
David Woodhouseb7d11252005-05-19 10:56:58 +01001095 if (audit_pid) {
1096 struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
1097 nlh->nlmsg_len = ab->skb->len - NLMSG_SPACE(0);
1098 skb_queue_tail(&audit_skb_queue, ab->skb);
1099 ab->skb = NULL;
1100 wake_up_interruptible(&kauditd_wait);
1101 } else {
David Woodhousee1b09eb2005-06-24 17:24:11 +01001102 printk(KERN_NOTICE "%s\n", ab->skb->data + NLMSG_SPACE(0));
David Woodhouseb7d11252005-05-19 10:56:58 +01001103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
Chris Wright16e19042005-05-06 15:53:34 +01001105 audit_buffer_free(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001108/**
1109 * audit_log - Log an audit record
1110 * @ctx: audit context
1111 * @gfp_mask: type of allocation
1112 * @type: audit message type
1113 * @fmt: format string to use
1114 * @...: variable parameters matching the format string
1115 *
1116 * This is a convenience function that calls audit_log_start,
1117 * audit_log_vformat, and audit_log_end. It may be called
1118 * in any context.
1119 */
Al Viro9796fdd2005-10-21 03:22:03 -04001120void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001121 const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
1123 struct audit_buffer *ab;
1124 va_list args;
1125
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001126 ab = audit_log_start(ctx, gfp_mask, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (ab) {
1128 va_start(args, fmt);
1129 audit_log_vformat(ab, fmt, args);
1130 va_end(args);
1131 audit_log_end(ab);
1132 }
1133}
lorenzo@gnu.orgbf45da92006-03-09 00:33:47 +01001134
1135EXPORT_SYMBOL(audit_log_start);
1136EXPORT_SYMBOL(audit_log_end);
1137EXPORT_SYMBOL(audit_log_format);
1138EXPORT_SYMBOL(audit_log);