[PATCH] Reworked patch for labels on user space messages
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / kernel / audit.c
CommitLineData
85c8721f 1/* audit.c -- Auditing support
1da177e4
LT
2 * 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 *
85c8721f 41 * Example user-space utilities: http://people.redhat.com/sgrubb/audit/
1da177e4
LT
42 */
43
44#include <linux/init.h>
1da177e4 45#include <asm/types.h>
715b49ef 46#include <asm/atomic.h>
1da177e4
LT
47#include <linux/mm.h>
48#include <linux/module.h>
b7d11258
DW
49#include <linux/err.h>
50#include <linux/kthread.h>
1da177e4
LT
51
52#include <linux/audit.h>
53
54#include <net/sock.h>
93315ed6 55#include <net/netlink.h>
1da177e4
LT
56#include <linux/skbuff.h>
57#include <linux/netlink.h>
3dc7e315
DG
58#include <linux/selinux.h>
59
60#include "audit.h"
1da177e4
LT
61
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. */
c2f0c7c3 77int audit_pid;
1da177e4 78
b0dd25a8 79/* If audit_rate_limit is non-zero, limit the rate of sending audit records
1da177e4
LT
80 * 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;
ac4cec44
DW
86static int audit_backlog_wait_time = 60 * HZ;
87static int audit_backlog_wait_overflow = 0;
1da177e4 88
c2f0c7c3
SG
89/* The identity of the user shutting down the audit system. */
90uid_t audit_sig_uid = -1;
91pid_t audit_sig_pid = -1;
92
1da177e4
LT
93/* 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
b7d11258 105/* The audit_freelist is a list of pre-allocated audit buffers (if more
1da177e4
LT
106 * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of
107 * being placed on the freelist). */
1da177e4 108static DEFINE_SPINLOCK(audit_freelist_lock);
b0dd25a8 109static int audit_freelist_count;
1da177e4
LT
110static LIST_HEAD(audit_freelist);
111
b7d11258
DW
112static struct sk_buff_head audit_skb_queue;
113static struct task_struct *kauditd_task;
114static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait);
9ad9ad38 115static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait);
1da177e4
LT
116
117/* The netlink socket is only to be read by 1 CPU, which lets us assume
23f32d18 118 * that list additions and deletions never happen simultaneously in
1da177e4 119 * auditsc.c */
5a0bbce5 120DEFINE_MUTEX(audit_netlink_mutex);
1da177e4
LT
121
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;
8fc6115c 138 struct sk_buff *skb; /* formatted skb ready to send */
1da177e4 139 struct audit_context *ctx; /* NULL or associated context */
9796fdd8 140 gfp_t gfp_mask;
1da177e4
LT
141};
142
c0404993
SG
143static 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
8c8570fb 149void audit_panic(const char *message)
1da177e4
LT
150{
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
b0dd25a8
RD
193/**
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*/
1da177e4
LT
201void 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
b7d11258 225 "audit: audit_lost=%d audit_rate_limit=%d audit_backlog_limit=%d\n",
1da177e4 226 atomic_read(&audit_lost),
1da177e4
LT
227 audit_rate_limit,
228 audit_backlog_limit);
229 audit_panic(message);
230 }
1da177e4
LT
231}
232
c94c257c 233static int audit_set_rate_limit(int limit, uid_t loginuid)
1da177e4
LT
234{
235 int old = audit_rate_limit;
236 audit_rate_limit = limit;
9ad9ad38 237 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
bccf6ae0 238 "audit_rate_limit=%d old=%d by auid=%u",
c94c257c 239 audit_rate_limit, old, loginuid);
1da177e4
LT
240 return old;
241}
242
c94c257c 243static int audit_set_backlog_limit(int limit, uid_t loginuid)
1da177e4
LT
244{
245 int old = audit_backlog_limit;
246 audit_backlog_limit = limit;
9ad9ad38 247 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
bccf6ae0 248 "audit_backlog_limit=%d old=%d by auid=%u",
c94c257c 249 audit_backlog_limit, old, loginuid);
1da177e4
LT
250 return old;
251}
252
c94c257c 253static int audit_set_enabled(int state, uid_t loginuid)
1da177e4
LT
254{
255 int old = audit_enabled;
256 if (state != 0 && state != 1)
257 return -EINVAL;
258 audit_enabled = state;
9ad9ad38 259 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
bccf6ae0 260 "audit_enabled=%d old=%d by auid=%u",
c0404993 261 audit_enabled, old, loginuid);
1da177e4
LT
262 return old;
263}
264
c94c257c 265static int audit_set_failure(int state, uid_t loginuid)
1da177e4
LT
266{
267 int old = audit_failure;
268 if (state != AUDIT_FAIL_SILENT
269 && state != AUDIT_FAIL_PRINTK
270 && state != AUDIT_FAIL_PANIC)
271 return -EINVAL;
272 audit_failure = state;
9ad9ad38 273 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
bccf6ae0 274 "audit_failure=%d old=%d by auid=%u",
c0404993 275 audit_failure, old, loginuid);
1da177e4
LT
276 return old;
277}
278
97a41e26 279static int kauditd_thread(void *dummy)
b7d11258
DW
280{
281 struct sk_buff *skb;
282
283 while (1) {
284 skb = skb_dequeue(&audit_skb_queue);
9ad9ad38 285 wake_up(&audit_backlog_wait);
b7d11258
DW
286 if (skb) {
287 if (audit_pid) {
288 int err = netlink_unicast(audit_sock, skb, audit_pid, 0);
289 if (err < 0) {
290 BUG_ON(err != -ECONNREFUSED); /* Shoudn't happen */
291 printk(KERN_ERR "audit: *NO* daemon at audit_pid=%d\n", audit_pid);
292 audit_pid = 0;
293 }
294 } else {
e1b09eba 295 printk(KERN_NOTICE "%s\n", skb->data + NLMSG_SPACE(0));
b7d11258
DW
296 kfree_skb(skb);
297 }
298 } else {
299 DECLARE_WAITQUEUE(wait, current);
300 set_current_state(TASK_INTERRUPTIBLE);
301 add_wait_queue(&kauditd_wait, &wait);
302
7a4ae749
PO
303 if (!skb_queue_len(&audit_skb_queue)) {
304 try_to_freeze();
b7d11258 305 schedule();
7a4ae749 306 }
b7d11258
DW
307
308 __set_current_state(TASK_RUNNING);
309 remove_wait_queue(&kauditd_wait, &wait);
310 }
311 }
fe7752ba 312 return 0;
b7d11258
DW
313}
314
b0dd25a8
RD
315/**
316 * audit_send_reply - send an audit reply message via netlink
317 * @pid: process id to send reply to
318 * @seq: sequence number
319 * @type: audit message type
320 * @done: done (last) flag
321 * @multi: multi-part message flag
322 * @payload: payload data
323 * @size: payload size
324 *
325 * Allocates an skb, builds the netlink message, and sends it to the pid.
326 * No failure notifications.
327 */
1da177e4
LT
328void audit_send_reply(int pid, int seq, int type, int done, int multi,
329 void *payload, int size)
330{
331 struct sk_buff *skb;
332 struct nlmsghdr *nlh;
333 int len = NLMSG_SPACE(size);
334 void *data;
335 int flags = multi ? NLM_F_MULTI : 0;
336 int t = done ? NLMSG_DONE : type;
337
338 skb = alloc_skb(len, GFP_KERNEL);
339 if (!skb)
b7d11258 340 return;
1da177e4 341
b7d11258 342 nlh = NLMSG_PUT(skb, pid, seq, t, size);
1da177e4
LT
343 nlh->nlmsg_flags = flags;
344 data = NLMSG_DATA(nlh);
345 memcpy(data, payload, size);
b7d11258
DW
346
347 /* Ignore failure. It'll only happen if the sender goes away,
348 because our timeout is set to infinite. */
349 netlink_unicast(audit_sock, skb, pid, 0);
1da177e4
LT
350 return;
351
352nlmsg_failure: /* Used by NLMSG_PUT */
353 if (skb)
354 kfree_skb(skb);
355}
356
357/*
358 * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
359 * control messages.
360 */
361static int audit_netlink_ok(kernel_cap_t eff_cap, u16 msg_type)
362{
363 int err = 0;
364
365 switch (msg_type) {
366 case AUDIT_GET:
367 case AUDIT_LIST:
93315ed6 368 case AUDIT_LIST_RULES:
1da177e4
LT
369 case AUDIT_SET:
370 case AUDIT_ADD:
93315ed6 371 case AUDIT_ADD_RULE:
1da177e4 372 case AUDIT_DEL:
93315ed6 373 case AUDIT_DEL_RULE:
c2f0c7c3 374 case AUDIT_SIGNAL_INFO:
1da177e4
LT
375 if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL))
376 err = -EPERM;
377 break;
05474106 378 case AUDIT_USER:
209aba03 379 case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
90d526c0 380 case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2:
1da177e4
LT
381 if (!cap_raised(eff_cap, CAP_AUDIT_WRITE))
382 err = -EPERM;
383 break;
384 default: /* bad msg */
385 err = -EINVAL;
386 }
387
388 return err;
389}
390
391static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
392{
e7c34970 393 u32 uid, pid, seq, sid;
1da177e4
LT
394 void *data;
395 struct audit_status *status_get, status_set;
396 int err;
c0404993 397 struct audit_buffer *ab;
1da177e4 398 u16 msg_type = nlh->nlmsg_type;
c94c257c 399 uid_t loginuid; /* loginuid of sender */
c2f0c7c3 400 struct audit_sig_info sig_data;
1da177e4
LT
401
402 err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type);
403 if (err)
404 return err;
405
b0dd25a8
RD
406 /* As soon as there's any sign of userspace auditd,
407 * start kauditd to talk to it */
b7d11258
DW
408 if (!kauditd_task)
409 kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd");
410 if (IS_ERR(kauditd_task)) {
411 err = PTR_ERR(kauditd_task);
412 kauditd_task = NULL;
413 return err;
414 }
415
1da177e4
LT
416 pid = NETLINK_CREDS(skb)->pid;
417 uid = NETLINK_CREDS(skb)->uid;
c94c257c 418 loginuid = NETLINK_CB(skb).loginuid;
e7c34970 419 sid = NETLINK_CB(skb).sid;
1da177e4
LT
420 seq = nlh->nlmsg_seq;
421 data = NLMSG_DATA(nlh);
422
423 switch (msg_type) {
424 case AUDIT_GET:
425 status_set.enabled = audit_enabled;
426 status_set.failure = audit_failure;
427 status_set.pid = audit_pid;
428 status_set.rate_limit = audit_rate_limit;
429 status_set.backlog_limit = audit_backlog_limit;
430 status_set.lost = atomic_read(&audit_lost);
b7d11258 431 status_set.backlog = skb_queue_len(&audit_skb_queue);
1da177e4
LT
432 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
433 &status_set, sizeof(status_set));
434 break;
435 case AUDIT_SET:
436 if (nlh->nlmsg_len < sizeof(struct audit_status))
437 return -EINVAL;
438 status_get = (struct audit_status *)data;
439 if (status_get->mask & AUDIT_STATUS_ENABLED) {
c94c257c 440 err = audit_set_enabled(status_get->enabled, loginuid);
1da177e4
LT
441 if (err < 0) return err;
442 }
443 if (status_get->mask & AUDIT_STATUS_FAILURE) {
c94c257c 444 err = audit_set_failure(status_get->failure, loginuid);
1da177e4
LT
445 if (err < 0) return err;
446 }
447 if (status_get->mask & AUDIT_STATUS_PID) {
448 int old = audit_pid;
449 audit_pid = status_get->pid;
9ad9ad38 450 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
bccf6ae0 451 "audit_pid=%d old=%d by auid=%u",
c94c257c 452 audit_pid, old, loginuid);
1da177e4
LT
453 }
454 if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
c94c257c 455 audit_set_rate_limit(status_get->rate_limit, loginuid);
1da177e4 456 if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
c94c257c
SH
457 audit_set_backlog_limit(status_get->backlog_limit,
458 loginuid);
1da177e4 459 break;
05474106 460 case AUDIT_USER:
209aba03 461 case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG:
90d526c0 462 case AUDIT_FIRST_USER_MSG2...AUDIT_LAST_USER_MSG2:
4a4cd633
DW
463 if (!audit_enabled && msg_type != AUDIT_USER_AVC)
464 return 0;
465
5bb289b5 466 err = audit_filter_user(&NETLINK_CB(skb), msg_type);
4a4cd633
DW
467 if (err == 1) {
468 err = 0;
9ad9ad38 469 ab = audit_log_start(NULL, GFP_KERNEL, msg_type);
4a4cd633
DW
470 if (ab) {
471 audit_log_format(ab,
e7c34970
SG
472 "user pid=%d uid=%u auid=%u",
473 pid, uid, loginuid);
474 if (sid) {
475 char *ctx = NULL;
476 u32 len;
477 if (selinux_ctxid_to_string(
478 sid, &ctx, &len)) {
479 audit_log_format(ab,
480 " subj=%u", sid);
481 /* Maybe call audit_panic? */
482 } else
483 audit_log_format(ab,
484 " subj=%s", ctx);
485 kfree(ctx);
486 }
487 audit_log_format(ab, " msg='%.1024s'",
488 (char *)data);
4a4cd633
DW
489 audit_set_pid(ab, pid);
490 audit_log_end(ab);
491 }
0f45aa18 492 }
1da177e4
LT
493 break;
494 case AUDIT_ADD:
495 case AUDIT_DEL:
93315ed6 496 if (nlmsg_len(nlh) < sizeof(struct audit_rule))
1da177e4
LT
497 return -EINVAL;
498 /* fallthrough */
499 case AUDIT_LIST:
1da177e4 500 err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
93315ed6
AG
501 uid, seq, data, nlmsg_len(nlh),
502 loginuid);
503 break;
504 case AUDIT_ADD_RULE:
505 case AUDIT_DEL_RULE:
506 if (nlmsg_len(nlh) < sizeof(struct audit_rule_data))
507 return -EINVAL;
508 /* fallthrough */
509 case AUDIT_LIST_RULES:
510 err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
511 uid, seq, data, nlmsg_len(nlh),
512 loginuid);
1da177e4 513 break;
c2f0c7c3
SG
514 case AUDIT_SIGNAL_INFO:
515 sig_data.uid = audit_sig_uid;
516 sig_data.pid = audit_sig_pid;
517 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO,
518 0, 0, &sig_data, sizeof(sig_data));
519 break;
1da177e4
LT
520 default:
521 err = -EINVAL;
522 break;
523 }
524
525 return err < 0 ? err : 0;
526}
527
b0dd25a8
RD
528/*
529 * Get message from skb (based on rtnetlink_rcv_skb). Each message is
1da177e4 530 * processed by audit_receive_msg. Malformed skbs with wrong length are
b0dd25a8
RD
531 * discarded silently.
532 */
2a0a6ebe 533static void audit_receive_skb(struct sk_buff *skb)
1da177e4
LT
534{
535 int err;
536 struct nlmsghdr *nlh;
537 u32 rlen;
538
539 while (skb->len >= NLMSG_SPACE(0)) {
540 nlh = (struct nlmsghdr *)skb->data;
541 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
2a0a6ebe 542 return;
1da177e4
LT
543 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
544 if (rlen > skb->len)
545 rlen = skb->len;
546 if ((err = audit_receive_msg(skb, nlh))) {
547 netlink_ack(skb, nlh, err);
548 } else if (nlh->nlmsg_flags & NLM_F_ACK)
549 netlink_ack(skb, nlh, 0);
550 skb_pull(skb, rlen);
551 }
1da177e4
LT
552}
553
554/* Receive messages from netlink socket. */
555static void audit_receive(struct sock *sk, int length)
556{
557 struct sk_buff *skb;
2a0a6ebe 558 unsigned int qlen;
1da177e4 559
5a0bbce5 560 mutex_lock(&audit_netlink_mutex);
1da177e4 561
2a0a6ebe
HX
562 for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) {
563 skb = skb_dequeue(&sk->sk_receive_queue);
564 audit_receive_skb(skb);
565 kfree_skb(skb);
1da177e4 566 }
5a0bbce5 567 mutex_unlock(&audit_netlink_mutex);
1da177e4
LT
568}
569
1da177e4
LT
570
571/* Initialize audit support at boot time. */
572static int __init audit_init(void)
573{
574 printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
575 audit_default ? "enabled" : "disabled");
06628607 576 audit_sock = netlink_kernel_create(NETLINK_AUDIT, 0, audit_receive,
4fdb3bb7 577 THIS_MODULE);
1da177e4
LT
578 if (!audit_sock)
579 audit_panic("cannot initialize netlink socket");
71e1c784
AG
580 else
581 audit_sock->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
1da177e4 582
b7d11258 583 skb_queue_head_init(&audit_skb_queue);
1da177e4
LT
584 audit_initialized = 1;
585 audit_enabled = audit_default;
3dc7e315
DG
586
587 /* Register the callback with selinux. This callback will be invoked
588 * when a new policy is loaded. */
589 selinux_audit_set_callback(&selinux_audit_rule_update);
590
9ad9ad38 591 audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, "initialized");
1da177e4
LT
592 return 0;
593}
1da177e4
LT
594__initcall(audit_init);
595
596/* Process kernel command-line parameter at boot time. audit=0 or audit=1. */
597static int __init audit_enable(char *str)
598{
599 audit_default = !!simple_strtol(str, NULL, 0);
600 printk(KERN_INFO "audit: %s%s\n",
601 audit_default ? "enabled" : "disabled",
602 audit_initialized ? "" : " (after initialization)");
603 if (audit_initialized)
604 audit_enabled = audit_default;
9b41046c 605 return 1;
1da177e4
LT
606}
607
608__setup("audit=", audit_enable);
609
16e1904e
CW
610static void audit_buffer_free(struct audit_buffer *ab)
611{
612 unsigned long flags;
613
8fc6115c
CW
614 if (!ab)
615 return;
616
5ac52f33
CW
617 if (ab->skb)
618 kfree_skb(ab->skb);
b7d11258 619
16e1904e
CW
620 spin_lock_irqsave(&audit_freelist_lock, flags);
621 if (++audit_freelist_count > AUDIT_MAXFREE)
622 kfree(ab);
623 else
624 list_add(&ab->list, &audit_freelist);
625 spin_unlock_irqrestore(&audit_freelist_lock, flags);
626}
627
c0404993 628static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
dd0fc66f 629 gfp_t gfp_mask, int type)
16e1904e
CW
630{
631 unsigned long flags;
632 struct audit_buffer *ab = NULL;
c0404993 633 struct nlmsghdr *nlh;
16e1904e
CW
634
635 spin_lock_irqsave(&audit_freelist_lock, flags);
636 if (!list_empty(&audit_freelist)) {
637 ab = list_entry(audit_freelist.next,
638 struct audit_buffer, list);
639 list_del(&ab->list);
640 --audit_freelist_count;
641 }
642 spin_unlock_irqrestore(&audit_freelist_lock, flags);
643
644 if (!ab) {
4332bdd3 645 ab = kmalloc(sizeof(*ab), gfp_mask);
16e1904e 646 if (!ab)
8fc6115c 647 goto err;
16e1904e 648 }
8fc6115c 649
4332bdd3 650 ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask);
5ac52f33 651 if (!ab->skb)
8fc6115c
CW
652 goto err;
653
b7d11258 654 ab->ctx = ctx;
9ad9ad38 655 ab->gfp_mask = gfp_mask;
c0404993
SG
656 nlh = (struct nlmsghdr *)skb_put(ab->skb, NLMSG_SPACE(0));
657 nlh->nlmsg_type = type;
658 nlh->nlmsg_flags = 0;
659 nlh->nlmsg_pid = 0;
660 nlh->nlmsg_seq = 0;
16e1904e 661 return ab;
8fc6115c
CW
662err:
663 audit_buffer_free(ab);
664 return NULL;
16e1904e 665}
1da177e4 666
b0dd25a8
RD
667/**
668 * audit_serial - compute a serial number for the audit record
669 *
670 * Compute a serial number for the audit record. Audit records are
bfb4496e
DW
671 * written to user-space as soon as they are generated, so a complete
672 * audit record may be written in several pieces. The timestamp of the
673 * record and this serial number are used by the user-space tools to
674 * determine which pieces belong to the same audit record. The
675 * (timestamp,serial) tuple is unique for each syscall and is live from
676 * syscall entry to syscall exit.
677 *
bfb4496e
DW
678 * NOTE: Another possibility is to store the formatted records off the
679 * audit context (for those records that have a context), and emit them
680 * all at syscall exit. However, this could delay the reporting of
681 * significant errors until syscall exit (or never, if the system
b0dd25a8
RD
682 * halts).
683 */
bfb4496e
DW
684unsigned int audit_serial(void)
685{
d5b454f2
DW
686 static spinlock_t serial_lock = SPIN_LOCK_UNLOCKED;
687 static unsigned int serial = 0;
688
689 unsigned long flags;
690 unsigned int ret;
bfb4496e 691
d5b454f2 692 spin_lock_irqsave(&serial_lock, flags);
bfb4496e 693 do {
ce625a80
DW
694 ret = ++serial;
695 } while (unlikely(!ret));
d5b454f2 696 spin_unlock_irqrestore(&serial_lock, flags);
bfb4496e 697
d5b454f2 698 return ret;
bfb4496e
DW
699}
700
701static inline void audit_get_stamp(struct audit_context *ctx,
702 struct timespec *t, unsigned int *serial)
703{
704 if (ctx)
705 auditsc_get_stamp(ctx, t, serial);
706 else {
707 *t = CURRENT_TIME;
708 *serial = audit_serial();
709 }
710}
711
1da177e4
LT
712/* Obtain an audit buffer. This routine does locking to obtain the
713 * audit buffer, but then no locking is required for calls to
714 * audit_log_*format. If the tsk is a task that is currently in a
715 * syscall, then the syscall is marked as auditable and an audit record
716 * will be written at syscall exit. If there is no associated task, tsk
717 * should be NULL. */
9ad9ad38 718
b0dd25a8
RD
719/**
720 * audit_log_start - obtain an audit buffer
721 * @ctx: audit_context (may be NULL)
722 * @gfp_mask: type of allocation
723 * @type: audit message type
724 *
725 * Returns audit_buffer pointer on success or NULL on error.
726 *
727 * Obtain an audit buffer. This routine does locking to obtain the
728 * audit buffer, but then no locking is required for calls to
729 * audit_log_*format. If the task (ctx) is a task that is currently in a
730 * syscall, then the syscall is marked as auditable and an audit record
731 * will be written at syscall exit. If there is no associated task, then
732 * task context (ctx) should be NULL.
733 */
9796fdd8 734struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
9ad9ad38 735 int type)
1da177e4
LT
736{
737 struct audit_buffer *ab = NULL;
1da177e4 738 struct timespec t;
d812ddbb 739 unsigned int serial;
9ad9ad38 740 int reserve;
ac4cec44 741 unsigned long timeout_start = jiffies;
1da177e4
LT
742
743 if (!audit_initialized)
744 return NULL;
745
c8edc80c
DK
746 if (unlikely(audit_filter_type(type)))
747 return NULL;
748
9ad9ad38
DW
749 if (gfp_mask & __GFP_WAIT)
750 reserve = 0;
751 else
752 reserve = 5; /* Allow atomic callers to go up to five
753 entries over the normal backlog limit */
754
755 while (audit_backlog_limit
756 && skb_queue_len(&audit_skb_queue) > audit_backlog_limit + reserve) {
ac4cec44
DW
757 if (gfp_mask & __GFP_WAIT && audit_backlog_wait_time
758 && time_before(jiffies, timeout_start + audit_backlog_wait_time)) {
759
9ad9ad38
DW
760 /* Wait for auditd to drain the queue a little */
761 DECLARE_WAITQUEUE(wait, current);
762 set_current_state(TASK_INTERRUPTIBLE);
763 add_wait_queue(&audit_backlog_wait, &wait);
764
765 if (audit_backlog_limit &&
766 skb_queue_len(&audit_skb_queue) > audit_backlog_limit)
ac4cec44 767 schedule_timeout(timeout_start + audit_backlog_wait_time - jiffies);
9ad9ad38
DW
768
769 __set_current_state(TASK_RUNNING);
770 remove_wait_queue(&audit_backlog_wait, &wait);
ac4cec44 771 continue;
9ad9ad38 772 }
fb19b4c6
DW
773 if (audit_rate_check())
774 printk(KERN_WARNING
775 "audit: audit_backlog=%d > "
776 "audit_backlog_limit=%d\n",
777 skb_queue_len(&audit_skb_queue),
778 audit_backlog_limit);
779 audit_log_lost("backlog limit exceeded");
ac4cec44
DW
780 audit_backlog_wait_time = audit_backlog_wait_overflow;
781 wake_up(&audit_backlog_wait);
fb19b4c6
DW
782 return NULL;
783 }
784
9ad9ad38 785 ab = audit_buffer_alloc(ctx, gfp_mask, type);
1da177e4
LT
786 if (!ab) {
787 audit_log_lost("out of memory in audit_log_start");
788 return NULL;
789 }
790
bfb4496e 791 audit_get_stamp(ab->ctx, &t, &serial);
197c69c6 792
1da177e4
LT
793 audit_log_format(ab, "audit(%lu.%03lu:%u): ",
794 t.tv_sec, t.tv_nsec/1000000, serial);
795 return ab;
796}
797
8fc6115c 798/**
5ac52f33 799 * audit_expand - expand skb in the audit buffer
8fc6115c 800 * @ab: audit_buffer
b0dd25a8 801 * @extra: space to add at tail of the skb
8fc6115c
CW
802 *
803 * Returns 0 (no space) on failed expansion, or available space if
804 * successful.
805 */
e3b926b4 806static inline int audit_expand(struct audit_buffer *ab, int extra)
8fc6115c 807{
5ac52f33 808 struct sk_buff *skb = ab->skb;
e3b926b4 809 int ret = pskb_expand_head(skb, skb_headroom(skb), extra,
9ad9ad38 810 ab->gfp_mask);
5ac52f33
CW
811 if (ret < 0) {
812 audit_log_lost("out of memory in audit_expand");
8fc6115c 813 return 0;
5ac52f33
CW
814 }
815 return skb_tailroom(skb);
8fc6115c 816}
1da177e4 817
b0dd25a8
RD
818/*
819 * Format an audit message into the audit buffer. If there isn't enough
1da177e4
LT
820 * room in the audit buffer, more room will be allocated and vsnprint
821 * will be called a second time. Currently, we assume that a printk
b0dd25a8
RD
822 * can't format message larger than 1024 bytes, so we don't either.
823 */
1da177e4
LT
824static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
825 va_list args)
826{
827 int len, avail;
5ac52f33 828 struct sk_buff *skb;
eecb0a73 829 va_list args2;
1da177e4
LT
830
831 if (!ab)
832 return;
833
5ac52f33
CW
834 BUG_ON(!ab->skb);
835 skb = ab->skb;
836 avail = skb_tailroom(skb);
837 if (avail == 0) {
e3b926b4 838 avail = audit_expand(ab, AUDIT_BUFSIZ);
8fc6115c
CW
839 if (!avail)
840 goto out;
1da177e4 841 }
eecb0a73 842 va_copy(args2, args);
5ac52f33 843 len = vsnprintf(skb->tail, avail, fmt, args);
1da177e4
LT
844 if (len >= avail) {
845 /* The printk buffer is 1024 bytes long, so if we get
846 * here and AUDIT_BUFSIZ is at least 1024, then we can
847 * log everything that printk could have logged. */
b0dd25a8
RD
848 avail = audit_expand(ab,
849 max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
8fc6115c
CW
850 if (!avail)
851 goto out;
eecb0a73 852 len = vsnprintf(skb->tail, avail, fmt, args2);
1da177e4 853 }
168b7173
SG
854 if (len > 0)
855 skb_put(skb, len);
8fc6115c
CW
856out:
857 return;
1da177e4
LT
858}
859
b0dd25a8
RD
860/**
861 * audit_log_format - format a message into the audit buffer.
862 * @ab: audit_buffer
863 * @fmt: format string
864 * @...: optional parameters matching @fmt string
865 *
866 * All the work is done in audit_log_vformat.
867 */
1da177e4
LT
868void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
869{
870 va_list args;
871
872 if (!ab)
873 return;
874 va_start(args, fmt);
875 audit_log_vformat(ab, fmt, args);
876 va_end(args);
877}
878
b0dd25a8
RD
879/**
880 * audit_log_hex - convert a buffer to hex and append it to the audit skb
881 * @ab: the audit_buffer
882 * @buf: buffer to convert to hex
883 * @len: length of @buf to be converted
884 *
885 * No return value; failure to expand is silently ignored.
886 *
887 * This function will take the passed buf and convert it into a string of
888 * ascii hex digits. The new string is placed onto the skb.
889 */
890void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf,
168b7173 891 size_t len)
83c7d091 892{
168b7173
SG
893 int i, avail, new_len;
894 unsigned char *ptr;
895 struct sk_buff *skb;
896 static const unsigned char *hex = "0123456789ABCDEF";
897
898 BUG_ON(!ab->skb);
899 skb = ab->skb;
900 avail = skb_tailroom(skb);
901 new_len = len<<1;
902 if (new_len >= avail) {
903 /* Round the buffer request up to the next multiple */
904 new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
905 avail = audit_expand(ab, new_len);
906 if (!avail)
907 return;
908 }
83c7d091 909
168b7173
SG
910 ptr = skb->tail;
911 for (i=0; i<len; i++) {
912 *ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */
913 *ptr++ = hex[buf[i] & 0x0F]; /* Lower nibble */
914 }
915 *ptr = 0;
916 skb_put(skb, len << 1); /* new string is twice the old string */
83c7d091
DW
917}
918
b0dd25a8
RD
919/**
920 * audit_log_unstrustedstring - log a string that may contain random characters
921 * @ab: audit_buffer
922 * @string: string to be logged
923 *
924 * This code will escape a string that is passed to it if the string
925 * contains a control character, unprintable character, double quote mark,
168b7173 926 * or a space. Unescaped strings will start and end with a double quote mark.
b0dd25a8
RD
927 * Strings that are escaped are printed in hex (2 digits per char).
928 */
83c7d091
DW
929void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
930{
81b7854d 931 const unsigned char *p = string;
83c7d091
DW
932
933 while (*p) {
168b7173 934 if (*p == '"' || *p < 0x21 || *p > 0x7f) {
83c7d091
DW
935 audit_log_hex(ab, string, strlen(string));
936 return;
937 }
938 p++;
939 }
940 audit_log_format(ab, "\"%s\"", string);
941}
942
168b7173 943/* This is a helper-function to print the escaped d_path */
1da177e4
LT
944void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
945 struct dentry *dentry, struct vfsmount *vfsmnt)
946{
168b7173 947 char *p, *path;
1da177e4 948
8fc6115c
CW
949 if (prefix)
950 audit_log_format(ab, " %s", prefix);
1da177e4 951
168b7173 952 /* We will allow 11 spaces for ' (deleted)' to be appended */
9ad9ad38 953 path = kmalloc(PATH_MAX+11, ab->gfp_mask);
168b7173
SG
954 if (!path) {
955 audit_log_format(ab, "<no memory>");
956 return;
1da177e4 957 }
168b7173
SG
958 p = d_path(dentry, vfsmnt, path, PATH_MAX+11);
959 if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
960 /* FIXME: can we save some information here? */
961 audit_log_format(ab, "<too long>");
962 } else
963 audit_log_untrustedstring(ab, p);
964 kfree(path);
1da177e4
LT
965}
966
b0dd25a8
RD
967/**
968 * audit_log_end - end one audit record
969 * @ab: the audit_buffer
970 *
971 * The netlink_* functions cannot be called inside an irq context, so
972 * the audit buffer is placed on a queue and a tasklet is scheduled to
1da177e4 973 * remove them from the queue outside the irq context. May be called in
b0dd25a8
RD
974 * any context.
975 */
b7d11258 976void audit_log_end(struct audit_buffer *ab)
1da177e4 977{
1da177e4
LT
978 if (!ab)
979 return;
980 if (!audit_rate_check()) {
981 audit_log_lost("rate limit exceeded");
982 } else {
b7d11258
DW
983 if (audit_pid) {
984 struct nlmsghdr *nlh = (struct nlmsghdr *)ab->skb->data;
985 nlh->nlmsg_len = ab->skb->len - NLMSG_SPACE(0);
986 skb_queue_tail(&audit_skb_queue, ab->skb);
987 ab->skb = NULL;
988 wake_up_interruptible(&kauditd_wait);
989 } else {
e1b09eba 990 printk(KERN_NOTICE "%s\n", ab->skb->data + NLMSG_SPACE(0));
b7d11258 991 }
1da177e4 992 }
16e1904e 993 audit_buffer_free(ab);
1da177e4
LT
994}
995
b0dd25a8
RD
996/**
997 * audit_log - Log an audit record
998 * @ctx: audit context
999 * @gfp_mask: type of allocation
1000 * @type: audit message type
1001 * @fmt: format string to use
1002 * @...: variable parameters matching the format string
1003 *
1004 * This is a convenience function that calls audit_log_start,
1005 * audit_log_vformat, and audit_log_end. It may be called
1006 * in any context.
1007 */
9796fdd8 1008void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
9ad9ad38 1009 const char *fmt, ...)
1da177e4
LT
1010{
1011 struct audit_buffer *ab;
1012 va_list args;
1013
9ad9ad38 1014 ab = audit_log_start(ctx, gfp_mask, type);
1da177e4
LT
1015 if (ab) {
1016 va_start(args, fmt);
1017 audit_log_vformat(ab, fmt, args);
1018 va_end(args);
1019 audit_log_end(ab);
1020 }
1021}
bf45da97 1022
1023EXPORT_SYMBOL(audit_log_start);
1024EXPORT_SYMBOL(audit_log_end);
1025EXPORT_SYMBOL(audit_log_format);
1026EXPORT_SYMBOL(audit_log);