[AUDIT] clean up audit_receive_msg()
[safe/jmp/linux-2.6] / kernel / audit.c
1 /* audit.c -- Auditing support
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-2007 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  *
41  * Example user-space utilities: http://people.redhat.com/sgrubb/audit/
42  */
43
44 #include <linux/init.h>
45 #include <asm/types.h>
46 #include <asm/atomic.h>
47 #include <linux/mm.h>
48 #include <linux/module.h>
49 #include <linux/err.h>
50 #include <linux/kthread.h>
51
52 #include <linux/audit.h>
53
54 #include <net/sock.h>
55 #include <net/netlink.h>
56 #include <linux/skbuff.h>
57 #include <linux/netlink.h>
58 #include <linux/selinux.h>
59 #include <linux/inotify.h>
60 #include <linux/freezer.h>
61 #include <linux/tty.h>
62
63 #include "audit.h"
64
65 /* No auditing will take place until audit_initialized != 0.
66  * (Initialization happens after skb_init is called.) */
67 static int      audit_initialized;
68
69 #define AUDIT_OFF       0
70 #define AUDIT_ON        1
71 #define AUDIT_LOCKED    2
72 int             audit_enabled;
73
74 /* Default state when kernel boots without any parameters. */
75 static int      audit_default;
76
77 /* If auditing cannot proceed, audit_failure selects what happens. */
78 static int      audit_failure = AUDIT_FAIL_PRINTK;
79
80 /* If audit records are to be written to the netlink socket, audit_pid
81  * contains the (non-zero) pid. */
82 int             audit_pid;
83
84 /* If audit_rate_limit is non-zero, limit the rate of sending audit records
85  * to that number per second.  This prevents DoS attacks, but results in
86  * audit records being dropped. */
87 static int      audit_rate_limit;
88
89 /* Number of outstanding audit_buffers allowed. */
90 static int      audit_backlog_limit = 64;
91 static int      audit_backlog_wait_time = 60 * HZ;
92 static int      audit_backlog_wait_overflow = 0;
93
94 /* The identity of the user shutting down the audit system. */
95 uid_t           audit_sig_uid = -1;
96 pid_t           audit_sig_pid = -1;
97 u32             audit_sig_sid = 0;
98
99 /* Records can be lost in several ways:
100    0) [suppressed in audit_alloc]
101    1) out of memory in audit_log_start [kmalloc of struct audit_buffer]
102    2) out of memory in audit_log_move [alloc_skb]
103    3) suppressed due to audit_rate_limit
104    4) suppressed due to audit_backlog_limit
105 */
106 static atomic_t    audit_lost = ATOMIC_INIT(0);
107
108 /* The netlink socket. */
109 static struct sock *audit_sock;
110
111 /* Inotify handle. */
112 struct inotify_handle *audit_ih;
113
114 /* Hash for inode-based rules */
115 struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS];
116
117 /* The audit_freelist is a list of pre-allocated audit buffers (if more
118  * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of
119  * being placed on the freelist). */
120 static DEFINE_SPINLOCK(audit_freelist_lock);
121 static int         audit_freelist_count;
122 static LIST_HEAD(audit_freelist);
123
124 static struct sk_buff_head audit_skb_queue;
125 static struct task_struct *kauditd_task;
126 static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait);
127 static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait);
128
129 /* Serialize requests from userspace. */
130 static DEFINE_MUTEX(audit_cmd_mutex);
131
132 /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
133  * audit records.  Since printk uses a 1024 byte buffer, this buffer
134  * should be at least that large. */
135 #define AUDIT_BUFSIZ 1024
136
137 /* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the
138  * audit_freelist.  Doing so eliminates many kmalloc/kfree calls. */
139 #define AUDIT_MAXFREE  (2*NR_CPUS)
140
141 /* The audit_buffer is used when formatting an audit record.  The caller
142  * locks briefly to get the record off the freelist or to allocate the
143  * buffer, and locks briefly to send the buffer to the netlink layer or
144  * to place it on a transmit queue.  Multiple audit_buffers can be in
145  * use simultaneously. */
146 struct audit_buffer {
147         struct list_head     list;
148         struct sk_buff       *skb;      /* formatted skb ready to send */
149         struct audit_context *ctx;      /* NULL or associated context */
150         gfp_t                gfp_mask;
151 };
152
153 static void audit_set_pid(struct audit_buffer *ab, pid_t pid)
154 {
155         if (ab) {
156                 struct nlmsghdr *nlh = nlmsg_hdr(ab->skb);
157                 nlh->nlmsg_pid = pid;
158         }
159 }
160
161 void audit_panic(const char *message)
162 {
163         switch (audit_failure)
164         {
165         case AUDIT_FAIL_SILENT:
166                 break;
167         case AUDIT_FAIL_PRINTK:
168                 printk(KERN_ERR "audit: %s\n", message);
169                 break;
170         case AUDIT_FAIL_PANIC:
171                 panic("audit: %s\n", message);
172                 break;
173         }
174 }
175
176 static inline int audit_rate_check(void)
177 {
178         static unsigned long    last_check = 0;
179         static int              messages   = 0;
180         static DEFINE_SPINLOCK(lock);
181         unsigned long           flags;
182         unsigned long           now;
183         unsigned long           elapsed;
184         int                     retval     = 0;
185
186         if (!audit_rate_limit) return 1;
187
188         spin_lock_irqsave(&lock, flags);
189         if (++messages < audit_rate_limit) {
190                 retval = 1;
191         } else {
192                 now     = jiffies;
193                 elapsed = now - last_check;
194                 if (elapsed > HZ) {
195                         last_check = now;
196                         messages   = 0;
197                         retval     = 1;
198                 }
199         }
200         spin_unlock_irqrestore(&lock, flags);
201
202         return retval;
203 }
204
205 /**
206  * audit_log_lost - conditionally log lost audit message event
207  * @message: the message stating reason for lost audit message
208  *
209  * Emit at least 1 message per second, even if audit_rate_check is
210  * throttling.
211  * Always increment the lost messages counter.
212 */
213 void audit_log_lost(const char *message)
214 {
215         static unsigned long    last_msg = 0;
216         static DEFINE_SPINLOCK(lock);
217         unsigned long           flags;
218         unsigned long           now;
219         int                     print;
220
221         atomic_inc(&audit_lost);
222
223         print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit);
224
225         if (!print) {
226                 spin_lock_irqsave(&lock, flags);
227                 now = jiffies;
228                 if (now - last_msg > HZ) {
229                         print = 1;
230                         last_msg = now;
231                 }
232                 spin_unlock_irqrestore(&lock, flags);
233         }
234
235         if (print) {
236                 printk(KERN_WARNING
237                        "audit: audit_lost=%d audit_rate_limit=%d audit_backlog_limit=%d\n",
238                        atomic_read(&audit_lost),
239                        audit_rate_limit,
240                        audit_backlog_limit);
241                 audit_panic(message);
242         }
243 }
244
245 static int audit_log_config_change(char *function_name, int new, int old,
246                                    uid_t loginuid, u32 sid, int allow_changes)
247 {
248         struct audit_buffer *ab;
249         int rc = 0;
250
251         ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
252         audit_log_format(ab, "%s=%d old=%d by auid=%u", function_name, new,
253                          old, loginuid);
254         if (sid) {
255                 char *ctx = NULL;
256                 u32 len;
257
258                 rc = selinux_sid_to_string(sid, &ctx, &len);
259                 if (rc) {
260                         audit_log_format(ab, " sid=%u", sid);
261                         allow_changes = 0; /* Something weird, deny request */
262                 } else {
263                         audit_log_format(ab, " subj=%s", ctx);
264                         kfree(ctx);
265                 }
266         }
267         audit_log_format(ab, " res=%d", allow_changes);
268         audit_log_end(ab);
269         return rc;
270 }
271
272 static int audit_do_config_change(char *function_name, int *to_change,
273                                   int new, uid_t loginuid, u32 sid)
274 {
275         int allow_changes, rc = 0, old = *to_change;
276
277         /* check if we are locked */
278         if (audit_enabled == AUDIT_LOCKED)
279                 allow_changes = 0;
280         else
281                 allow_changes = 1;
282
283         if (audit_enabled != AUDIT_OFF) {
284                 rc = audit_log_config_change(function_name, new, old,
285                                              loginuid, sid, allow_changes);
286                 if (rc)
287                         allow_changes = 0;
288         }
289
290         /* If we are allowed, make the change */
291         if (allow_changes == 1)
292                 *to_change = new;
293         /* Not allowed, update reason */
294         else if (rc == 0)
295                 rc = -EPERM;
296         return rc;
297 }
298
299 static int audit_set_rate_limit(int limit, uid_t loginuid, u32 sid)
300 {
301         return audit_do_config_change("audit_rate_limit", &audit_rate_limit,
302                                       limit, loginuid, sid);
303 }
304
305 static int audit_set_backlog_limit(int limit, uid_t loginuid, u32 sid)
306 {
307         return audit_do_config_change("audit_backlog_limit", &audit_backlog_limit,
308                                       limit, loginuid, sid);
309 }
310
311 static int audit_set_enabled(int state, uid_t loginuid, u32 sid)
312 {
313         if (state < AUDIT_OFF || state > AUDIT_LOCKED)
314                 return -EINVAL;
315
316         return audit_do_config_change("audit_enabled", &audit_enabled, state,
317                                       loginuid, sid);
318 }
319
320 static int audit_set_failure(int state, uid_t loginuid, u32 sid)
321 {
322         if (state != AUDIT_FAIL_SILENT
323             && state != AUDIT_FAIL_PRINTK
324             && state != AUDIT_FAIL_PANIC)
325                 return -EINVAL;
326
327         return audit_do_config_change("audit_failure", &audit_failure, state,
328                                       loginuid, sid);
329 }
330
331 static int kauditd_thread(void *dummy)
332 {
333         struct sk_buff *skb;
334
335         set_freezable();
336         while (!kthread_should_stop()) {
337                 skb = skb_dequeue(&audit_skb_queue);
338                 wake_up(&audit_backlog_wait);
339                 if (skb) {
340                         if (audit_pid) {
341                                 int err = netlink_unicast(audit_sock, skb, audit_pid, 0);
342                                 if (err < 0) {
343                                         BUG_ON(err != -ECONNREFUSED); /* Shoudn't happen */
344                                         printk(KERN_ERR "audit: *NO* daemon at audit_pid=%d\n", audit_pid);
345                                         audit_pid = 0;
346                                 }
347                         } else {
348                                 printk(KERN_NOTICE "%s\n", skb->data + NLMSG_SPACE(0));
349                                 kfree_skb(skb);
350                         }
351                 } else {
352                         DECLARE_WAITQUEUE(wait, current);
353                         set_current_state(TASK_INTERRUPTIBLE);
354                         add_wait_queue(&kauditd_wait, &wait);
355
356                         if (!skb_queue_len(&audit_skb_queue)) {
357                                 try_to_freeze();
358                                 schedule();
359                         }
360
361                         __set_current_state(TASK_RUNNING);
362                         remove_wait_queue(&kauditd_wait, &wait);
363                 }
364         }
365         return 0;
366 }
367
368 static int audit_prepare_user_tty(pid_t pid, uid_t loginuid)
369 {
370         struct task_struct *tsk;
371         int err;
372
373         read_lock(&tasklist_lock);
374         tsk = find_task_by_pid(pid);
375         err = -ESRCH;
376         if (!tsk)
377                 goto out;
378         err = 0;
379
380         spin_lock_irq(&tsk->sighand->siglock);
381         if (!tsk->signal->audit_tty)
382                 err = -EPERM;
383         spin_unlock_irq(&tsk->sighand->siglock);
384         if (err)
385                 goto out;
386
387         tty_audit_push_task(tsk, loginuid);
388 out:
389         read_unlock(&tasklist_lock);
390         return err;
391 }
392
393 int audit_send_list(void *_dest)
394 {
395         struct audit_netlink_list *dest = _dest;
396         int pid = dest->pid;
397         struct sk_buff *skb;
398
399         /* wait for parent to finish and send an ACK */
400         mutex_lock(&audit_cmd_mutex);
401         mutex_unlock(&audit_cmd_mutex);
402
403         while ((skb = __skb_dequeue(&dest->q)) != NULL)
404                 netlink_unicast(audit_sock, skb, pid, 0);
405
406         kfree(dest);
407
408         return 0;
409 }
410
411 #ifdef CONFIG_AUDIT_TREE
412 static int prune_tree_thread(void *unused)
413 {
414         mutex_lock(&audit_cmd_mutex);
415         audit_prune_trees();
416         mutex_unlock(&audit_cmd_mutex);
417         return 0;
418 }
419
420 void audit_schedule_prune(void)
421 {
422         kthread_run(prune_tree_thread, NULL, "audit_prune_tree");
423 }
424 #endif
425
426 struct sk_buff *audit_make_reply(int pid, int seq, int type, int done,
427                                  int multi, void *payload, int size)
428 {
429         struct sk_buff  *skb;
430         struct nlmsghdr *nlh;
431         int             len = NLMSG_SPACE(size);
432         void            *data;
433         int             flags = multi ? NLM_F_MULTI : 0;
434         int             t     = done  ? NLMSG_DONE  : type;
435
436         skb = alloc_skb(len, GFP_KERNEL);
437         if (!skb)
438                 return NULL;
439
440         nlh              = NLMSG_PUT(skb, pid, seq, t, size);
441         nlh->nlmsg_flags = flags;
442         data             = NLMSG_DATA(nlh);
443         memcpy(data, payload, size);
444         return skb;
445
446 nlmsg_failure:                  /* Used by NLMSG_PUT */
447         if (skb)
448                 kfree_skb(skb);
449         return NULL;
450 }
451
452 /**
453  * audit_send_reply - send an audit reply message via netlink
454  * @pid: process id to send reply to
455  * @seq: sequence number
456  * @type: audit message type
457  * @done: done (last) flag
458  * @multi: multi-part message flag
459  * @payload: payload data
460  * @size: payload size
461  *
462  * Allocates an skb, builds the netlink message, and sends it to the pid.
463  * No failure notifications.
464  */
465 void audit_send_reply(int pid, int seq, int type, int done, int multi,
466                       void *payload, int size)
467 {
468         struct sk_buff  *skb;
469         skb = audit_make_reply(pid, seq, type, done, multi, payload, size);
470         if (!skb)
471                 return;
472         /* Ignore failure. It'll only happen if the sender goes away,
473            because our timeout is set to infinite. */
474         netlink_unicast(audit_sock, skb, pid, 0);
475         return;
476 }
477
478 /*
479  * Check for appropriate CAP_AUDIT_ capabilities on incoming audit
480  * control messages.
481  */
482 static int audit_netlink_ok(struct sk_buff *skb, u16 msg_type)
483 {
484         int err = 0;
485
486         switch (msg_type) {
487         case AUDIT_GET:
488         case AUDIT_LIST:
489         case AUDIT_LIST_RULES:
490         case AUDIT_SET:
491         case AUDIT_ADD:
492         case AUDIT_ADD_RULE:
493         case AUDIT_DEL:
494         case AUDIT_DEL_RULE:
495         case AUDIT_SIGNAL_INFO:
496         case AUDIT_TTY_GET:
497         case AUDIT_TTY_SET:
498         case AUDIT_TRIM:
499         case AUDIT_MAKE_EQUIV:
500                 if (security_netlink_recv(skb, CAP_AUDIT_CONTROL))
501                         err = -EPERM;
502                 break;
503         case AUDIT_USER:
504         case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
505         case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
506                 if (security_netlink_recv(skb, CAP_AUDIT_WRITE))
507                         err = -EPERM;
508                 break;
509         default:  /* bad msg */
510                 err = -EINVAL;
511         }
512
513         return err;
514 }
515
516 static int audit_log_common_recv_msg(struct audit_buffer **ab, u16 msg_type,
517                                      u32 pid, u32 uid, uid_t auid, u32 sid)
518 {
519         int rc = 0;
520         char *ctx = NULL;
521         u32 len;
522
523         if (!audit_enabled) {
524                 *ab = NULL;
525                 return rc;
526         }
527
528         *ab = audit_log_start(NULL, GFP_KERNEL, msg_type);
529         audit_log_format(*ab, "user pid=%d uid=%u auid=%u",
530                          pid, uid, auid);
531         if (sid) {
532                 rc = selinux_sid_to_string(sid, &ctx, &len);
533                 if (rc)
534                         audit_log_format(*ab, " ssid=%u", sid);
535                 else
536                         audit_log_format(*ab, " subj=%s", ctx);
537                 kfree(ctx);
538         }
539
540         return rc;
541 }
542
543 static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
544 {
545         u32                     uid, pid, seq, sid;
546         void                    *data;
547         struct audit_status     *status_get, status_set;
548         int                     err;
549         struct audit_buffer     *ab;
550         u16                     msg_type = nlh->nlmsg_type;
551         uid_t                   loginuid; /* loginuid of sender */
552         struct audit_sig_info   *sig_data;
553         char                    *ctx = NULL;
554         u32                     len;
555
556         err = audit_netlink_ok(skb, msg_type);
557         if (err)
558                 return err;
559
560         /* As soon as there's any sign of userspace auditd,
561          * start kauditd to talk to it */
562         if (!kauditd_task)
563                 kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd");
564         if (IS_ERR(kauditd_task)) {
565                 err = PTR_ERR(kauditd_task);
566                 kauditd_task = NULL;
567                 return err;
568         }
569
570         pid  = NETLINK_CREDS(skb)->pid;
571         uid  = NETLINK_CREDS(skb)->uid;
572         loginuid = NETLINK_CB(skb).loginuid;
573         sid  = NETLINK_CB(skb).sid;
574         seq  = nlh->nlmsg_seq;
575         data = NLMSG_DATA(nlh);
576
577         switch (msg_type) {
578         case AUDIT_GET:
579                 status_set.enabled       = audit_enabled;
580                 status_set.failure       = audit_failure;
581                 status_set.pid           = audit_pid;
582                 status_set.rate_limit    = audit_rate_limit;
583                 status_set.backlog_limit = audit_backlog_limit;
584                 status_set.lost          = atomic_read(&audit_lost);
585                 status_set.backlog       = skb_queue_len(&audit_skb_queue);
586                 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0,
587                                  &status_set, sizeof(status_set));
588                 break;
589         case AUDIT_SET:
590                 if (nlh->nlmsg_len < sizeof(struct audit_status))
591                         return -EINVAL;
592                 status_get   = (struct audit_status *)data;
593                 if (status_get->mask & AUDIT_STATUS_ENABLED) {
594                         err = audit_set_enabled(status_get->enabled,
595                                                         loginuid, sid);
596                         if (err < 0) return err;
597                 }
598                 if (status_get->mask & AUDIT_STATUS_FAILURE) {
599                         err = audit_set_failure(status_get->failure,
600                                                          loginuid, sid);
601                         if (err < 0) return err;
602                 }
603                 if (status_get->mask & AUDIT_STATUS_PID) {
604                         int new_pid = status_get->pid;
605
606                         if (audit_enabled != AUDIT_OFF)
607                                 audit_log_config_change("audit_pid", new_pid,
608                                                         audit_pid, loginuid,
609                                                         sid, 1);
610
611                         audit_pid = new_pid;
612                 }
613                 if (status_get->mask & AUDIT_STATUS_RATE_LIMIT)
614                         err = audit_set_rate_limit(status_get->rate_limit,
615                                                          loginuid, sid);
616                 if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT)
617                         err = audit_set_backlog_limit(status_get->backlog_limit,
618                                                         loginuid, sid);
619                 break;
620         case AUDIT_USER:
621         case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG:
622         case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2:
623                 if (!audit_enabled && msg_type != AUDIT_USER_AVC)
624                         return 0;
625
626                 err = audit_filter_user(&NETLINK_CB(skb), msg_type);
627                 if (err == 1) {
628                         err = 0;
629                         if (msg_type == AUDIT_USER_TTY) {
630                                 err = audit_prepare_user_tty(pid, loginuid);
631                                 if (err)
632                                         break;
633                         }
634                         audit_log_common_recv_msg(&ab, msg_type, pid, uid,
635                                                   loginuid, sid);
636
637                         if (msg_type != AUDIT_USER_TTY)
638                                 audit_log_format(ab, " msg='%.1024s'",
639                                                  (char *)data);
640                         else {
641                                 int size;
642
643                                 audit_log_format(ab, " msg=");
644                                 size = nlmsg_len(nlh);
645                                 audit_log_n_untrustedstring(ab, size,
646                                                             data);
647                         }
648                         audit_set_pid(ab, pid);
649                         audit_log_end(ab);
650                 }
651                 break;
652         case AUDIT_ADD:
653         case AUDIT_DEL:
654                 if (nlmsg_len(nlh) < sizeof(struct audit_rule))
655                         return -EINVAL;
656                 if (audit_enabled == AUDIT_LOCKED) {
657                         audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE, pid,
658                                                   uid, loginuid, sid);
659
660                         audit_log_format(ab, " audit_enabled=%d res=0",
661                                          audit_enabled);
662                         audit_log_end(ab);
663                         return -EPERM;
664                 }
665                 /* fallthrough */
666         case AUDIT_LIST:
667                 err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
668                                            uid, seq, data, nlmsg_len(nlh),
669                                            loginuid, sid);
670                 break;
671         case AUDIT_ADD_RULE:
672         case AUDIT_DEL_RULE:
673                 if (nlmsg_len(nlh) < sizeof(struct audit_rule_data))
674                         return -EINVAL;
675                 if (audit_enabled == AUDIT_LOCKED) {
676                         audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE, pid,
677                                                   uid, loginuid, sid);
678
679                         audit_log_format(ab, " audit_enabled=%d res=0",
680                                          audit_enabled);
681                         audit_log_end(ab);
682                         return -EPERM;
683                 }
684                 /* fallthrough */
685         case AUDIT_LIST_RULES:
686                 err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid,
687                                            uid, seq, data, nlmsg_len(nlh),
688                                            loginuid, sid);
689                 break;
690         case AUDIT_TRIM:
691                 audit_trim_trees();
692
693                 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE, pid,
694                                           uid, loginuid, sid);
695
696                 audit_log_format(ab, " op=trim res=1");
697                 audit_log_end(ab);
698                 break;
699         case AUDIT_MAKE_EQUIV: {
700                 void *bufp = data;
701                 u32 sizes[2];
702                 size_t len = nlmsg_len(nlh);
703                 char *old, *new;
704
705                 err = -EINVAL;
706                 if (len < 2 * sizeof(u32))
707                         break;
708                 memcpy(sizes, bufp, 2 * sizeof(u32));
709                 bufp += 2 * sizeof(u32);
710                 len -= 2 * sizeof(u32);
711                 old = audit_unpack_string(&bufp, &len, sizes[0]);
712                 if (IS_ERR(old)) {
713                         err = PTR_ERR(old);
714                         break;
715                 }
716                 new = audit_unpack_string(&bufp, &len, sizes[1]);
717                 if (IS_ERR(new)) {
718                         err = PTR_ERR(new);
719                         kfree(old);
720                         break;
721                 }
722                 /* OK, here comes... */
723                 err = audit_tag_tree(old, new);
724
725                 audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE, pid,
726                                           uid, loginuid, sid);
727
728                 audit_log_format(ab, " op=make_equiv old=");
729                 audit_log_untrustedstring(ab, old);
730                 audit_log_format(ab, " new=");
731                 audit_log_untrustedstring(ab, new);
732                 audit_log_format(ab, " res=%d", !err);
733                 audit_log_end(ab);
734                 kfree(old);
735                 kfree(new);
736                 break;
737         }
738         case AUDIT_SIGNAL_INFO:
739                 err = selinux_sid_to_string(audit_sig_sid, &ctx, &len);
740                 if (err)
741                         return err;
742                 sig_data = kmalloc(sizeof(*sig_data) + len, GFP_KERNEL);
743                 if (!sig_data) {
744                         kfree(ctx);
745                         return -ENOMEM;
746                 }
747                 sig_data->uid = audit_sig_uid;
748                 sig_data->pid = audit_sig_pid;
749                 memcpy(sig_data->ctx, ctx, len);
750                 kfree(ctx);
751                 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO,
752                                 0, 0, sig_data, sizeof(*sig_data) + len);
753                 kfree(sig_data);
754                 break;
755         case AUDIT_TTY_GET: {
756                 struct audit_tty_status s;
757                 struct task_struct *tsk;
758
759                 read_lock(&tasklist_lock);
760                 tsk = find_task_by_pid(pid);
761                 if (!tsk)
762                         err = -ESRCH;
763                 else {
764                         spin_lock_irq(&tsk->sighand->siglock);
765                         s.enabled = tsk->signal->audit_tty != 0;
766                         spin_unlock_irq(&tsk->sighand->siglock);
767                 }
768                 read_unlock(&tasklist_lock);
769                 audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_TTY_GET, 0, 0,
770                                  &s, sizeof(s));
771                 break;
772         }
773         case AUDIT_TTY_SET: {
774                 struct audit_tty_status *s;
775                 struct task_struct *tsk;
776
777                 if (nlh->nlmsg_len < sizeof(struct audit_tty_status))
778                         return -EINVAL;
779                 s = data;
780                 if (s->enabled != 0 && s->enabled != 1)
781                         return -EINVAL;
782                 read_lock(&tasklist_lock);
783                 tsk = find_task_by_pid(pid);
784                 if (!tsk)
785                         err = -ESRCH;
786                 else {
787                         spin_lock_irq(&tsk->sighand->siglock);
788                         tsk->signal->audit_tty = s->enabled != 0;
789                         spin_unlock_irq(&tsk->sighand->siglock);
790                 }
791                 read_unlock(&tasklist_lock);
792                 break;
793         }
794         default:
795                 err = -EINVAL;
796                 break;
797         }
798
799         return err < 0 ? err : 0;
800 }
801
802 /*
803  * Get message from skb (based on rtnetlink_rcv_skb).  Each message is
804  * processed by audit_receive_msg.  Malformed skbs with wrong length are
805  * discarded silently.
806  */
807 static void audit_receive_skb(struct sk_buff *skb)
808 {
809         int             err;
810         struct nlmsghdr *nlh;
811         u32             rlen;
812
813         while (skb->len >= NLMSG_SPACE(0)) {
814                 nlh = nlmsg_hdr(skb);
815                 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
816                         return;
817                 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
818                 if (rlen > skb->len)
819                         rlen = skb->len;
820                 if ((err = audit_receive_msg(skb, nlh))) {
821                         netlink_ack(skb, nlh, err);
822                 } else if (nlh->nlmsg_flags & NLM_F_ACK)
823                         netlink_ack(skb, nlh, 0);
824                 skb_pull(skb, rlen);
825         }
826 }
827
828 /* Receive messages from netlink socket. */
829 static void audit_receive(struct sk_buff  *skb)
830 {
831         mutex_lock(&audit_cmd_mutex);
832         audit_receive_skb(skb);
833         mutex_unlock(&audit_cmd_mutex);
834 }
835
836 #ifdef CONFIG_AUDITSYSCALL
837 static const struct inotify_operations audit_inotify_ops = {
838         .handle_event   = audit_handle_ievent,
839         .destroy_watch  = audit_free_parent,
840 };
841 #endif
842
843 /* Initialize audit support at boot time. */
844 static int __init audit_init(void)
845 {
846         int i;
847
848         printk(KERN_INFO "audit: initializing netlink socket (%s)\n",
849                audit_default ? "enabled" : "disabled");
850         audit_sock = netlink_kernel_create(&init_net, NETLINK_AUDIT, 0,
851                                            audit_receive, NULL, THIS_MODULE);
852         if (!audit_sock)
853                 audit_panic("cannot initialize netlink socket");
854         else
855                 audit_sock->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT;
856
857         skb_queue_head_init(&audit_skb_queue);
858         audit_initialized = 1;
859         audit_enabled = audit_default;
860
861         /* Register the callback with selinux.  This callback will be invoked
862          * when a new policy is loaded. */
863         selinux_audit_set_callback(&selinux_audit_rule_update);
864
865         audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, "initialized");
866
867 #ifdef CONFIG_AUDITSYSCALL
868         audit_ih = inotify_init(&audit_inotify_ops);
869         if (IS_ERR(audit_ih))
870                 audit_panic("cannot initialize inotify handle");
871 #endif
872
873         for (i = 0; i < AUDIT_INODE_BUCKETS; i++)
874                 INIT_LIST_HEAD(&audit_inode_hash[i]);
875
876         return 0;
877 }
878 __initcall(audit_init);
879
880 /* Process kernel command-line parameter at boot time.  audit=0 or audit=1. */
881 static int __init audit_enable(char *str)
882 {
883         audit_default = !!simple_strtol(str, NULL, 0);
884         printk(KERN_INFO "audit: %s%s\n",
885                audit_default ? "enabled" : "disabled",
886                audit_initialized ? "" : " (after initialization)");
887         if (audit_initialized)
888                 audit_enabled = audit_default;
889         return 1;
890 }
891
892 __setup("audit=", audit_enable);
893
894 static void audit_buffer_free(struct audit_buffer *ab)
895 {
896         unsigned long flags;
897
898         if (!ab)
899                 return;
900
901         if (ab->skb)
902                 kfree_skb(ab->skb);
903
904         spin_lock_irqsave(&audit_freelist_lock, flags);
905         if (audit_freelist_count > AUDIT_MAXFREE)
906                 kfree(ab);
907         else {
908                 audit_freelist_count++;
909                 list_add(&ab->list, &audit_freelist);
910         }
911         spin_unlock_irqrestore(&audit_freelist_lock, flags);
912 }
913
914 static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx,
915                                                 gfp_t gfp_mask, int type)
916 {
917         unsigned long flags;
918         struct audit_buffer *ab = NULL;
919         struct nlmsghdr *nlh;
920
921         spin_lock_irqsave(&audit_freelist_lock, flags);
922         if (!list_empty(&audit_freelist)) {
923                 ab = list_entry(audit_freelist.next,
924                                 struct audit_buffer, list);
925                 list_del(&ab->list);
926                 --audit_freelist_count;
927         }
928         spin_unlock_irqrestore(&audit_freelist_lock, flags);
929
930         if (!ab) {
931                 ab = kmalloc(sizeof(*ab), gfp_mask);
932                 if (!ab)
933                         goto err;
934         }
935
936         ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask);
937         if (!ab->skb)
938                 goto err;
939
940         ab->ctx = ctx;
941         ab->gfp_mask = gfp_mask;
942         nlh = (struct nlmsghdr *)skb_put(ab->skb, NLMSG_SPACE(0));
943         nlh->nlmsg_type = type;
944         nlh->nlmsg_flags = 0;
945         nlh->nlmsg_pid = 0;
946         nlh->nlmsg_seq = 0;
947         return ab;
948 err:
949         audit_buffer_free(ab);
950         return NULL;
951 }
952
953 /**
954  * audit_serial - compute a serial number for the audit record
955  *
956  * Compute a serial number for the audit record.  Audit records are
957  * written to user-space as soon as they are generated, so a complete
958  * audit record may be written in several pieces.  The timestamp of the
959  * record and this serial number are used by the user-space tools to
960  * determine which pieces belong to the same audit record.  The
961  * (timestamp,serial) tuple is unique for each syscall and is live from
962  * syscall entry to syscall exit.
963  *
964  * NOTE: Another possibility is to store the formatted records off the
965  * audit context (for those records that have a context), and emit them
966  * all at syscall exit.  However, this could delay the reporting of
967  * significant errors until syscall exit (or never, if the system
968  * halts).
969  */
970 unsigned int audit_serial(void)
971 {
972         static DEFINE_SPINLOCK(serial_lock);
973         static unsigned int serial = 0;
974
975         unsigned long flags;
976         unsigned int ret;
977
978         spin_lock_irqsave(&serial_lock, flags);
979         do {
980                 ret = ++serial;
981         } while (unlikely(!ret));
982         spin_unlock_irqrestore(&serial_lock, flags);
983
984         return ret;
985 }
986
987 static inline void audit_get_stamp(struct audit_context *ctx,
988                                    struct timespec *t, unsigned int *serial)
989 {
990         if (ctx)
991                 auditsc_get_stamp(ctx, t, serial);
992         else {
993                 *t = CURRENT_TIME;
994                 *serial = audit_serial();
995         }
996 }
997
998 /* Obtain an audit buffer.  This routine does locking to obtain the
999  * audit buffer, but then no locking is required for calls to
1000  * audit_log_*format.  If the tsk is a task that is currently in a
1001  * syscall, then the syscall is marked as auditable and an audit record
1002  * will be written at syscall exit.  If there is no associated task, tsk
1003  * should be NULL. */
1004
1005 /**
1006  * audit_log_start - obtain an audit buffer
1007  * @ctx: audit_context (may be NULL)
1008  * @gfp_mask: type of allocation
1009  * @type: audit message type
1010  *
1011  * Returns audit_buffer pointer on success or NULL on error.
1012  *
1013  * Obtain an audit buffer.  This routine does locking to obtain the
1014  * audit buffer, but then no locking is required for calls to
1015  * audit_log_*format.  If the task (ctx) is a task that is currently in a
1016  * syscall, then the syscall is marked as auditable and an audit record
1017  * will be written at syscall exit.  If there is no associated task, then
1018  * task context (ctx) should be NULL.
1019  */
1020 struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask,
1021                                      int type)
1022 {
1023         struct audit_buffer     *ab     = NULL;
1024         struct timespec         t;
1025         unsigned int            serial;
1026         int reserve;
1027         unsigned long timeout_start = jiffies;
1028
1029         if (!audit_initialized)
1030                 return NULL;
1031
1032         if (unlikely(audit_filter_type(type)))
1033                 return NULL;
1034
1035         if (gfp_mask & __GFP_WAIT)
1036                 reserve = 0;
1037         else
1038                 reserve = 5; /* Allow atomic callers to go up to five
1039                                 entries over the normal backlog limit */
1040
1041         while (audit_backlog_limit
1042                && skb_queue_len(&audit_skb_queue) > audit_backlog_limit + reserve) {
1043                 if (gfp_mask & __GFP_WAIT && audit_backlog_wait_time
1044                     && time_before(jiffies, timeout_start + audit_backlog_wait_time)) {
1045
1046                         /* Wait for auditd to drain the queue a little */
1047                         DECLARE_WAITQUEUE(wait, current);
1048                         set_current_state(TASK_INTERRUPTIBLE);
1049                         add_wait_queue(&audit_backlog_wait, &wait);
1050
1051                         if (audit_backlog_limit &&
1052                             skb_queue_len(&audit_skb_queue) > audit_backlog_limit)
1053                                 schedule_timeout(timeout_start + audit_backlog_wait_time - jiffies);
1054
1055                         __set_current_state(TASK_RUNNING);
1056                         remove_wait_queue(&audit_backlog_wait, &wait);
1057                         continue;
1058                 }
1059                 if (audit_rate_check())
1060                         printk(KERN_WARNING
1061                                "audit: audit_backlog=%d > "
1062                                "audit_backlog_limit=%d\n",
1063                                skb_queue_len(&audit_skb_queue),
1064                                audit_backlog_limit);
1065                 audit_log_lost("backlog limit exceeded");
1066                 audit_backlog_wait_time = audit_backlog_wait_overflow;
1067                 wake_up(&audit_backlog_wait);
1068                 return NULL;
1069         }
1070
1071         ab = audit_buffer_alloc(ctx, gfp_mask, type);
1072         if (!ab) {
1073                 audit_log_lost("out of memory in audit_log_start");
1074                 return NULL;
1075         }
1076
1077         audit_get_stamp(ab->ctx, &t, &serial);
1078
1079         audit_log_format(ab, "audit(%lu.%03lu:%u): ",
1080                          t.tv_sec, t.tv_nsec/1000000, serial);
1081         return ab;
1082 }
1083
1084 /**
1085  * audit_expand - expand skb in the audit buffer
1086  * @ab: audit_buffer
1087  * @extra: space to add at tail of the skb
1088  *
1089  * Returns 0 (no space) on failed expansion, or available space if
1090  * successful.
1091  */
1092 static inline int audit_expand(struct audit_buffer *ab, int extra)
1093 {
1094         struct sk_buff *skb = ab->skb;
1095         int oldtail = skb_tailroom(skb);
1096         int ret = pskb_expand_head(skb, 0, extra, ab->gfp_mask);
1097         int newtail = skb_tailroom(skb);
1098
1099         if (ret < 0) {
1100                 audit_log_lost("out of memory in audit_expand");
1101                 return 0;
1102         }
1103
1104         skb->truesize += newtail - oldtail;
1105         return newtail;
1106 }
1107
1108 /*
1109  * Format an audit message into the audit buffer.  If there isn't enough
1110  * room in the audit buffer, more room will be allocated and vsnprint
1111  * will be called a second time.  Currently, we assume that a printk
1112  * can't format message larger than 1024 bytes, so we don't either.
1113  */
1114 static void audit_log_vformat(struct audit_buffer *ab, const char *fmt,
1115                               va_list args)
1116 {
1117         int len, avail;
1118         struct sk_buff *skb;
1119         va_list args2;
1120
1121         if (!ab)
1122                 return;
1123
1124         BUG_ON(!ab->skb);
1125         skb = ab->skb;
1126         avail = skb_tailroom(skb);
1127         if (avail == 0) {
1128                 avail = audit_expand(ab, AUDIT_BUFSIZ);
1129                 if (!avail)
1130                         goto out;
1131         }
1132         va_copy(args2, args);
1133         len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args);
1134         if (len >= avail) {
1135                 /* The printk buffer is 1024 bytes long, so if we get
1136                  * here and AUDIT_BUFSIZ is at least 1024, then we can
1137                  * log everything that printk could have logged. */
1138                 avail = audit_expand(ab,
1139                         max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail));
1140                 if (!avail)
1141                         goto out;
1142                 len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args2);
1143         }
1144         if (len > 0)
1145                 skb_put(skb, len);
1146 out:
1147         return;
1148 }
1149
1150 /**
1151  * audit_log_format - format a message into the audit buffer.
1152  * @ab: audit_buffer
1153  * @fmt: format string
1154  * @...: optional parameters matching @fmt string
1155  *
1156  * All the work is done in audit_log_vformat.
1157  */
1158 void audit_log_format(struct audit_buffer *ab, const char *fmt, ...)
1159 {
1160         va_list args;
1161
1162         if (!ab)
1163                 return;
1164         va_start(args, fmt);
1165         audit_log_vformat(ab, fmt, args);
1166         va_end(args);
1167 }
1168
1169 /**
1170  * audit_log_hex - convert a buffer to hex and append it to the audit skb
1171  * @ab: the audit_buffer
1172  * @buf: buffer to convert to hex
1173  * @len: length of @buf to be converted
1174  *
1175  * No return value; failure to expand is silently ignored.
1176  *
1177  * This function will take the passed buf and convert it into a string of
1178  * ascii hex digits. The new string is placed onto the skb.
1179  */
1180 void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf,
1181                 size_t len)
1182 {
1183         int i, avail, new_len;
1184         unsigned char *ptr;
1185         struct sk_buff *skb;
1186         static const unsigned char *hex = "0123456789ABCDEF";
1187
1188         if (!ab)
1189                 return;
1190
1191         BUG_ON(!ab->skb);
1192         skb = ab->skb;
1193         avail = skb_tailroom(skb);
1194         new_len = len<<1;
1195         if (new_len >= avail) {
1196                 /* Round the buffer request up to the next multiple */
1197                 new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1);
1198                 avail = audit_expand(ab, new_len);
1199                 if (!avail)
1200                         return;
1201         }
1202
1203         ptr = skb_tail_pointer(skb);
1204         for (i=0; i<len; i++) {
1205                 *ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */
1206                 *ptr++ = hex[buf[i] & 0x0F];      /* Lower nibble */
1207         }
1208         *ptr = 0;
1209         skb_put(skb, len << 1); /* new string is twice the old string */
1210 }
1211
1212 /*
1213  * Format a string of no more than slen characters into the audit buffer,
1214  * enclosed in quote marks.
1215  */
1216 static void audit_log_n_string(struct audit_buffer *ab, size_t slen,
1217                                const char *string)
1218 {
1219         int avail, new_len;
1220         unsigned char *ptr;
1221         struct sk_buff *skb;
1222
1223         if (!ab)
1224                 return;
1225
1226         BUG_ON(!ab->skb);
1227         skb = ab->skb;
1228         avail = skb_tailroom(skb);
1229         new_len = slen + 3;     /* enclosing quotes + null terminator */
1230         if (new_len > avail) {
1231                 avail = audit_expand(ab, new_len);
1232                 if (!avail)
1233                         return;
1234         }
1235         ptr = skb_tail_pointer(skb);
1236         *ptr++ = '"';
1237         memcpy(ptr, string, slen);
1238         ptr += slen;
1239         *ptr++ = '"';
1240         *ptr = 0;
1241         skb_put(skb, slen + 2); /* don't include null terminator */
1242 }
1243
1244 /**
1245  * audit_string_contains_control - does a string need to be logged in hex
1246  * @string - string to be checked
1247  * @len - max length of the string to check
1248  */
1249 int audit_string_contains_control(const char *string, size_t len)
1250 {
1251         const unsigned char *p;
1252         for (p = string; p < (const unsigned char *)string + len && *p; p++) {
1253                 if (*p == '"' || *p < 0x21 || *p > 0x7f)
1254                         return 1;
1255         }
1256         return 0;
1257 }
1258
1259 /**
1260  * audit_log_n_untrustedstring - log a string that may contain random characters
1261  * @ab: audit_buffer
1262  * @len: lenth of string (not including trailing null)
1263  * @string: string to be logged
1264  *
1265  * This code will escape a string that is passed to it if the string
1266  * contains a control character, unprintable character, double quote mark,
1267  * or a space. Unescaped strings will start and end with a double quote mark.
1268  * Strings that are escaped are printed in hex (2 digits per char).
1269  *
1270  * The caller specifies the number of characters in the string to log, which may
1271  * or may not be the entire string.
1272  */
1273 void audit_log_n_untrustedstring(struct audit_buffer *ab, size_t len,
1274                                  const char *string)
1275 {
1276         if (audit_string_contains_control(string, len))
1277                 audit_log_hex(ab, string, len);
1278         else
1279                 audit_log_n_string(ab, len, string);
1280 }
1281
1282 /**
1283  * audit_log_untrustedstring - log a string that may contain random characters
1284  * @ab: audit_buffer
1285  * @string: string to be logged
1286  *
1287  * Same as audit_log_n_untrustedstring(), except that strlen is used to
1288  * determine string length.
1289  */
1290 void audit_log_untrustedstring(struct audit_buffer *ab, const char *string)
1291 {
1292         audit_log_n_untrustedstring(ab, strlen(string), string);
1293 }
1294
1295 /* This is a helper-function to print the escaped d_path */
1296 void audit_log_d_path(struct audit_buffer *ab, const char *prefix,
1297                       struct dentry *dentry, struct vfsmount *vfsmnt)
1298 {
1299         char *p, *path;
1300
1301         if (prefix)
1302                 audit_log_format(ab, " %s", prefix);
1303
1304         /* We will allow 11 spaces for ' (deleted)' to be appended */
1305         path = kmalloc(PATH_MAX+11, ab->gfp_mask);
1306         if (!path) {
1307                 audit_log_format(ab, "<no memory>");
1308                 return;
1309         }
1310         p = d_path(dentry, vfsmnt, path, PATH_MAX+11);
1311         if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */
1312                 /* FIXME: can we save some information here? */
1313                 audit_log_format(ab, "<too long>");
1314         } else
1315                 audit_log_untrustedstring(ab, p);
1316         kfree(path);
1317 }
1318
1319 /**
1320  * audit_log_end - end one audit record
1321  * @ab: the audit_buffer
1322  *
1323  * The netlink_* functions cannot be called inside an irq context, so
1324  * the audit buffer is placed on a queue and a tasklet is scheduled to
1325  * remove them from the queue outside the irq context.  May be called in
1326  * any context.
1327  */
1328 void audit_log_end(struct audit_buffer *ab)
1329 {
1330         if (!ab)
1331                 return;
1332         if (!audit_rate_check()) {
1333                 audit_log_lost("rate limit exceeded");
1334         } else {
1335                 if (audit_pid) {
1336                         struct nlmsghdr *nlh = nlmsg_hdr(ab->skb);
1337                         nlh->nlmsg_len = ab->skb->len - NLMSG_SPACE(0);
1338                         skb_queue_tail(&audit_skb_queue, ab->skb);
1339                         ab->skb = NULL;
1340                         wake_up_interruptible(&kauditd_wait);
1341                 } else {
1342                         struct nlmsghdr *nlh = nlmsg_hdr(ab->skb);
1343                         printk(KERN_NOTICE "type=%d %s\n", nlh->nlmsg_type, ab->skb->data + NLMSG_SPACE(0));
1344                 }
1345         }
1346         audit_buffer_free(ab);
1347 }
1348
1349 /**
1350  * audit_log - Log an audit record
1351  * @ctx: audit context
1352  * @gfp_mask: type of allocation
1353  * @type: audit message type
1354  * @fmt: format string to use
1355  * @...: variable parameters matching the format string
1356  *
1357  * This is a convenience function that calls audit_log_start,
1358  * audit_log_vformat, and audit_log_end.  It may be called
1359  * in any context.
1360  */
1361 void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type,
1362                const char *fmt, ...)
1363 {
1364         struct audit_buffer *ab;
1365         va_list args;
1366
1367         ab = audit_log_start(ctx, gfp_mask, type);
1368         if (ab) {
1369                 va_start(args, fmt);
1370                 audit_log_vformat(ab, fmt, args);
1371                 va_end(args);
1372                 audit_log_end(ab);
1373         }
1374 }
1375
1376 EXPORT_SYMBOL(audit_log_start);
1377 EXPORT_SYMBOL(audit_log_end);
1378 EXPORT_SYMBOL(audit_log_format);
1379 EXPORT_SYMBOL(audit_log);