45 #include <asm/types.h>
48 #include <linux/export.h>
49 #include <linux/slab.h>
53 #include <linux/audit.h>
58 #ifdef CONFIG_SECURITY
61 #include <linux/netlink.h>
63 #include <linux/tty.h>
70 #define AUDIT_DISABLED -1
71 #define AUDIT_UNINITIALIZED 0
72 #define AUDIT_INITIALIZED 1
73 static int audit_initialized;
77 #define AUDIT_LOCKED 2
84 static int audit_default;
95 static int audit_nlk_portid;
100 static int audit_rate_limit;
103 static int audit_backlog_limit = 64;
104 static int audit_backlog_wait_time = 60 *
HZ;
105 static int audit_backlog_wait_overflow = 0;
122 static struct sock *audit_sock;
131 static int audit_freelist_count;
147 #define AUDIT_BUFSIZ 1024
151 #define AUDIT_MAXFREE (2*NR_CPUS)
180 switch (audit_failure)
185 if (printk_ratelimit())
191 panic(
"audit: %s\n", message);
196 static inline int audit_rate_check(
void)
198 static unsigned long last_check = 0;
199 static int messages = 0;
203 unsigned long elapsed;
206 if (!audit_rate_limit)
return 1;
209 if (++messages < audit_rate_limit) {
213 elapsed = now - last_check;
220 spin_unlock_irqrestore(&lock, flags);
235 static unsigned long last_msg = 0;
248 if (now - last_msg >
HZ) {
252 spin_unlock_irqrestore(&
lock, flags);
256 if (printk_ratelimit())
258 "audit: audit_lost=%d audit_rate_limit=%d "
259 "audit_backlog_limit=%d\n",
262 audit_backlog_limit);
267 static int audit_log_config_change(
char *function_name,
int new,
int old,
295 static int audit_do_config_change(
char *function_name,
int *to_change,
299 int allow_changes, rc = 0, old = *to_change;
308 rc = audit_log_config_change(function_name,
new, old, loginuid,
309 sessionid, sid, allow_changes);
315 if (allow_changes == 1)
323 static int audit_set_rate_limit(
int limit,
kuid_t loginuid,
u32 sessionid,
326 return audit_do_config_change(
"audit_rate_limit", &audit_rate_limit,
327 limit, loginuid, sessionid, sid);
330 static int audit_set_backlog_limit(
int limit,
kuid_t loginuid,
u32 sessionid,
333 return audit_do_config_change(
"audit_backlog_limit", &audit_backlog_limit,
334 limit, loginuid, sessionid, sid);
343 rc = audit_do_config_change(
"audit_enabled", &
audit_enabled, state,
344 loginuid, sessionid, sid);
352 static int audit_set_failure(
int state,
kuid_t loginuid,
u32 sessionid,
u32 sid)
359 return audit_do_config_change(
"audit_failure", &audit_failure, state,
360 loginuid, sessionid, sid);
372 static void audit_hold_skb(
struct sk_buff *
skb)
375 skb_queue_len(&audit_skb_hold_queue) < audit_backlog_limit)
385 static void audit_printk_skb(
struct sk_buff *skb)
387 struct nlmsghdr *nlh = nlmsg_hdr(skb);
388 char *
data = nlmsg_data(nlh);
391 if (printk_ratelimit())
400 static void kauditd_send_skb(
struct sk_buff *skb)
418 static int kauditd_thread(
void *
dummy)
441 kauditd_send_skb(skb);
451 kauditd_send_skb(skb);
453 audit_printk_skb(skb);
459 if (!skb_queue_len(&audit_skb_queue)) {
481 while ((skb = __skb_dequeue(&dest->
q)) !=
NULL)
502 nlh = nlmsg_put(skb, pid, seq, t, size, flags);
505 data = nlmsg_data(nlh);
506 memcpy(data, payload, size);
514 static int audit_send_reply_thread(
void *
arg)
540 static void audit_send_reply(
int pid,
int seq,
int type,
int done,
int multi,
558 tsk =
kthread_run(audit_send_reply_thread, reply,
"audit_send_reply");
609 static int audit_log_common_recv_msg(
struct audit_buffer **ab,
u16 msg_type,
639 static int audit_receive_msg(
struct sk_buff *skb,
struct nlmsghdr *nlh)
653 err = audit_netlink_ok(skb, msg_type);
661 if (IS_ERR(kauditd_task)) {
662 err = PTR_ERR(kauditd_task);
667 loginuid = audit_get_loginuid(
current);
668 sessionid = audit_get_sessionid(
current);
671 data = nlmsg_data(nlh);
676 status_set.
failure = audit_failure;
681 status_set.
backlog = skb_queue_len(&audit_skb_queue);
683 &status_set,
sizeof(status_set));
690 err = audit_set_enabled(status_get->
enabled,
691 loginuid, sessionid, sid);
696 err = audit_set_failure(status_get->
failure,
697 loginuid, sessionid, sid);
702 int new_pid = status_get->
pid;
705 audit_log_config_change(
"audit_pid", new_pid,
713 err = audit_set_rate_limit(status_get->
rate_limit,
714 loginuid, sessionid, sid);
720 loginuid, sessionid, sid);
737 audit_log_common_recv_msg(&ab, msg_type,
738 loginuid, sessionid, sid);
747 size = nlmsg_len(nlh);
749 ((
unsigned char *)data)[size - 1] ==
'\0')
759 if (nlmsg_len(nlh) <
sizeof(
struct audit_rule))
763 loginuid, sessionid, sid);
773 seq, data, nlmsg_len(nlh),
774 loginuid, sessionid, sid);
782 loginuid, sessionid, sid);
792 seq, data, nlmsg_len(nlh),
793 loginuid, sessionid, sid);
799 loginuid, sessionid, sid);
807 size_t msglen = nlmsg_len(nlh);
811 if (msglen < 2 *
sizeof(
u32))
814 bufp += 2 *
sizeof(
u32);
815 msglen -= 2 *
sizeof(
u32);
831 loginuid, sessionid, sid);
863 0, 0, sig_data,
sizeof(*sig_data) + len);
870 spin_lock_irq(&tsk->
sighand->siglock);
871 s.enabled = tsk->
signal->audit_tty != 0;
872 spin_unlock_irq(&tsk->
sighand->siglock);
888 spin_lock_irq(&tsk->
sighand->siglock);
890 spin_unlock_irq(&tsk->
sighand->siglock);
898 return err < 0 ? err : 0;
905 static void audit_receive_skb(
struct sk_buff *skb)
915 nlh = nlmsg_hdr(skb);
919 err = audit_receive_msg(skb, nlh);
929 static void audit_receive(
struct sk_buff *skb)
932 audit_receive_skb(skb);
937 static int __init audit_init(
void)
941 .input = audit_receive,
948 audit_default ?
"enabled" :
"disabled");
955 skb_queue_head_init(&audit_skb_queue);
956 skb_queue_head_init(&audit_skb_hold_queue);
971 static int __init audit_enable(
char *
str)
983 printk(
" (after initialization)");
985 printk(
" (until reboot)");
992 __setup(
"audit=", audit_enable);
1008 audit_freelist_count++;
1009 list_add(&ab->
list, &audit_freelist);
1011 spin_unlock_irqrestore(&audit_freelist_lock, flags);
1017 unsigned long flags;
1022 if (!list_empty(&audit_freelist)) {
1026 --audit_freelist_count;
1028 spin_unlock_irqrestore(&audit_freelist_lock, flags);
1031 ab =
kmalloc(
sizeof(*ab), gfp_mask);
1043 nlh = nlmsg_put(ab->
skb, 0, 0, type, 0, 0);
1053 audit_buffer_free(ab);
1077 static unsigned int serial = 0;
1079 unsigned long flags;
1086 spin_unlock_irqrestore(&serial_lock, flags);
1091 static inline void audit_get_stamp(
struct audit_context *ctx,
1129 unsigned long timeout_start =
jiffies;
1143 while (audit_backlog_limit
1144 && skb_queue_len(&audit_skb_queue) > audit_backlog_limit + reserve) {
1145 if (gfp_mask & __GFP_WAIT && audit_backlog_wait_time
1153 if (audit_backlog_limit &&
1154 skb_queue_len(&audit_skb_queue) > audit_backlog_limit)
1161 if (audit_rate_check() && printk_ratelimit())
1163 "audit: audit_backlog=%d > "
1164 "audit_backlog_limit=%d\n",
1165 skb_queue_len(&audit_skb_queue),
1166 audit_backlog_limit);
1168 audit_backlog_wait_time = audit_backlog_wait_overflow;
1173 ab = audit_buffer_alloc(ctx, gfp_mask, type);
1179 audit_get_stamp(ab->
ctx, &t, &serial);
1197 int oldtail = skb_tailroom(skb);
1199 int newtail = skb_tailroom(skb);
1206 skb->
truesize += newtail - oldtail;
1216 static void audit_log_vformat(
struct audit_buffer *ab,
const char *
fmt,
1228 avail = skb_tailroom(skb);
1234 va_copy(args2, args);
1235 len =
vsnprintf(skb_tail_pointer(skb), avail, fmt, args);
1240 avail = audit_expand(ab,
1244 len =
vsnprintf(skb_tail_pointer(skb), avail, fmt, args2);
1269 audit_log_vformat(ab, fmt, args);
1290 static const unsigned char *hex =
"0123456789ABCDEF";
1297 avail = skb_tailroom(skb);
1299 if (new_len >= avail) {
1302 avail = audit_expand(ab, new_len);
1307 ptr = skb_tail_pointer(skb);
1308 for (i=0; i<
len; i++) {
1309 *ptr++ = hex[(buf[
i] & 0xF0)>>4];
1310 *ptr++ = hex[buf[
i] & 0x0F];
1332 avail = skb_tailroom(skb);
1334 if (new_len > avail) {
1335 avail = audit_expand(ab, new_len);
1339 ptr = skb_tail_pointer(skb);
1341 memcpy(ptr,
string, slen);
1355 const unsigned char *
p;
1356 for (p =
string; p < (
const unsigned char *)
string + len; p++) {
1357 if (*p ==
'"' || *p < 0x21 || *p > 0x7e)
1411 audit_log_string(ab,
"<no_memory>");
1417 audit_log_string(ab,
"<too_long>");
1468 if (!audit_rate_check()) {
1478 audit_printk_skb(ab->
skb);
1482 audit_buffer_free(ab);
1498 const char *fmt, ...)
1506 audit_log_vformat(ab, fmt, args);
1512 #ifdef CONFIG_SECURITY