Commit | Line | Data |
---|---|---|
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> | |
45 | #include <asm/atomic.h> | |
46 | #include <asm/types.h> | |
47 | #include <linux/mm.h> | |
48 | #include <linux/module.h> | |
49 | ||
50 | #include <linux/audit.h> | |
51 | ||
52 | #include <net/sock.h> | |
53 | #include <linux/skbuff.h> | |
54 | #include <linux/netlink.h> | |
55 | ||
56 | /* No auditing will take place until audit_initialized != 0. | |
57 | * (Initialization happens after skb_init is called.) */ | |
58 | static int audit_initialized; | |
59 | ||
60 | /* No syscall auditing will take place unless audit_enabled != 0. */ | |
61 | int audit_enabled; | |
62 | ||
63 | /* Default state when kernel boots without any parameters. */ | |
64 | static int audit_default; | |
65 | ||
66 | /* If auditing cannot proceed, audit_failure selects what happens. */ | |
67 | static int audit_failure = AUDIT_FAIL_PRINTK; | |
68 | ||
69 | /* If audit records are to be written to the netlink socket, audit_pid | |
70 | * contains the (non-zero) pid. */ | |
c2f0c7c3 | 71 | int audit_pid; |
1da177e4 LT |
72 | |
73 | /* If audit_limit is non-zero, limit the rate of sending audit records | |
74 | * to that number per second. This prevents DoS attacks, but results in | |
75 | * audit records being dropped. */ | |
76 | static int audit_rate_limit; | |
77 | ||
78 | /* Number of outstanding audit_buffers allowed. */ | |
79 | static int audit_backlog_limit = 64; | |
80 | static atomic_t audit_backlog = ATOMIC_INIT(0); | |
81 | ||
c2f0c7c3 SG |
82 | /* The identity of the user shutting down the audit system. */ |
83 | uid_t audit_sig_uid = -1; | |
84 | pid_t audit_sig_pid = -1; | |
85 | ||
1da177e4 LT |
86 | /* Records can be lost in several ways: |
87 | 0) [suppressed in audit_alloc] | |
88 | 1) out of memory in audit_log_start [kmalloc of struct audit_buffer] | |
89 | 2) out of memory in audit_log_move [alloc_skb] | |
90 | 3) suppressed due to audit_rate_limit | |
91 | 4) suppressed due to audit_backlog_limit | |
92 | */ | |
93 | static atomic_t audit_lost = ATOMIC_INIT(0); | |
94 | ||
95 | /* The netlink socket. */ | |
96 | static struct sock *audit_sock; | |
97 | ||
98 | /* There are two lists of audit buffers. The txlist contains audit | |
99 | * buffers that cannot be sent immediately to the netlink device because | |
100 | * we are in an irq context (these are sent later in a tasklet). | |
101 | * | |
102 | * The second list is a list of pre-allocated audit buffers (if more | |
103 | * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of | |
104 | * being placed on the freelist). */ | |
105 | static DEFINE_SPINLOCK(audit_txlist_lock); | |
106 | static DEFINE_SPINLOCK(audit_freelist_lock); | |
107 | static int audit_freelist_count = 0; | |
108 | static LIST_HEAD(audit_txlist); | |
109 | static LIST_HEAD(audit_freelist); | |
110 | ||
111 | /* There are three lists of rules -- one to search at task creation | |
112 | * time, one to search at syscall entry time, and another to search at | |
113 | * syscall exit time. */ | |
114 | static LIST_HEAD(audit_tsklist); | |
115 | static LIST_HEAD(audit_entlist); | |
116 | static LIST_HEAD(audit_extlist); | |
117 | ||
118 | /* The netlink socket is only to be read by 1 CPU, which lets us assume | |
23f32d18 | 119 | * that list additions and deletions never happen simultaneously in |
1da177e4 LT |
120 | * auditsc.c */ |
121 | static DECLARE_MUTEX(audit_netlink_sem); | |
122 | ||
123 | /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting | |
124 | * audit records. Since printk uses a 1024 byte buffer, this buffer | |
125 | * should be at least that large. */ | |
126 | #define AUDIT_BUFSIZ 1024 | |
127 | ||
128 | /* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the | |
129 | * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */ | |
130 | #define AUDIT_MAXFREE (2*NR_CPUS) | |
131 | ||
132 | /* The audit_buffer is used when formatting an audit record. The caller | |
133 | * locks briefly to get the record off the freelist or to allocate the | |
134 | * buffer, and locks briefly to send the buffer to the netlink layer or | |
135 | * to place it on a transmit queue. Multiple audit_buffers can be in | |
136 | * use simultaneously. */ | |
137 | struct audit_buffer { | |
138 | struct list_head list; | |
8fc6115c | 139 | struct sk_buff *skb; /* formatted skb ready to send */ |
1da177e4 | 140 | struct audit_context *ctx; /* NULL or associated context */ |
1da177e4 LT |
141 | }; |
142 | ||
c0404993 SG |
143 | static 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 | ||
1da177e4 LT |
149 | struct audit_entry { |
150 | struct list_head list; | |
151 | struct audit_rule rule; | |
152 | }; | |
153 | ||
154 | static void audit_log_end_irq(struct audit_buffer *ab); | |
155 | static void audit_log_end_fast(struct audit_buffer *ab); | |
156 | ||
157 | static void audit_panic(const char *message) | |
158 | { | |
159 | switch (audit_failure) | |
160 | { | |
161 | case AUDIT_FAIL_SILENT: | |
162 | break; | |
163 | case AUDIT_FAIL_PRINTK: | |
164 | printk(KERN_ERR "audit: %s\n", message); | |
165 | break; | |
166 | case AUDIT_FAIL_PANIC: | |
167 | panic("audit: %s\n", message); | |
168 | break; | |
169 | } | |
170 | } | |
171 | ||
172 | static inline int audit_rate_check(void) | |
173 | { | |
174 | static unsigned long last_check = 0; | |
175 | static int messages = 0; | |
176 | static DEFINE_SPINLOCK(lock); | |
177 | unsigned long flags; | |
178 | unsigned long now; | |
179 | unsigned long elapsed; | |
180 | int retval = 0; | |
181 | ||
182 | if (!audit_rate_limit) return 1; | |
183 | ||
184 | spin_lock_irqsave(&lock, flags); | |
185 | if (++messages < audit_rate_limit) { | |
186 | retval = 1; | |
187 | } else { | |
188 | now = jiffies; | |
189 | elapsed = now - last_check; | |
190 | if (elapsed > HZ) { | |
191 | last_check = now; | |
192 | messages = 0; | |
193 | retval = 1; | |
194 | } | |
195 | } | |
196 | spin_unlock_irqrestore(&lock, flags); | |
197 | ||
198 | return retval; | |
199 | } | |
200 | ||
201 | /* Emit at least 1 message per second, even if audit_rate_check is | |
202 | * throttling. */ | |
203 | void audit_log_lost(const char *message) | |
204 | { | |
205 | static unsigned long last_msg = 0; | |
206 | static DEFINE_SPINLOCK(lock); | |
207 | unsigned long flags; | |
208 | unsigned long now; | |
209 | int print; | |
210 | ||
211 | atomic_inc(&audit_lost); | |
212 | ||
213 | print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit); | |
214 | ||
215 | if (!print) { | |
216 | spin_lock_irqsave(&lock, flags); | |
217 | now = jiffies; | |
218 | if (now - last_msg > HZ) { | |
219 | print = 1; | |
220 | last_msg = now; | |
221 | } | |
222 | spin_unlock_irqrestore(&lock, flags); | |
223 | } | |
224 | ||
225 | if (print) { | |
226 | printk(KERN_WARNING | |
227 | "audit: audit_lost=%d audit_backlog=%d" | |
228 | " audit_rate_limit=%d audit_backlog_limit=%d\n", | |
229 | atomic_read(&audit_lost), | |
230 | atomic_read(&audit_backlog), | |
231 | audit_rate_limit, | |
232 | audit_backlog_limit); | |
233 | audit_panic(message); | |
234 | } | |
235 | ||
236 | } | |
237 | ||
c94c257c | 238 | static int audit_set_rate_limit(int limit, uid_t loginuid) |
1da177e4 LT |
239 | { |
240 | int old = audit_rate_limit; | |
241 | audit_rate_limit = limit; | |
c0404993 SG |
242 | audit_log(NULL, AUDIT_CONFIG_CHANGE, |
243 | "audit_rate_limit=%d old=%d by auid %u", | |
c94c257c | 244 | audit_rate_limit, old, loginuid); |
1da177e4 LT |
245 | return old; |
246 | } | |
247 | ||
c94c257c | 248 | static int audit_set_backlog_limit(int limit, uid_t loginuid) |
1da177e4 LT |
249 | { |
250 | int old = audit_backlog_limit; | |
251 | audit_backlog_limit = limit; | |
c0404993 SG |
252 | audit_log(NULL, AUDIT_CONFIG_CHANGE, |
253 | "audit_backlog_limit=%d old=%d by auid %u", | |
c94c257c | 254 | audit_backlog_limit, old, loginuid); |
1da177e4 LT |
255 | return old; |
256 | } | |
257 | ||
c94c257c | 258 | static int audit_set_enabled(int state, uid_t loginuid) |
1da177e4 LT |
259 | { |
260 | int old = audit_enabled; | |
261 | if (state != 0 && state != 1) | |
262 | return -EINVAL; | |
263 | audit_enabled = state; | |
c0404993 SG |
264 | audit_log(NULL, AUDIT_CONFIG_CHANGE, |
265 | "audit_enabled=%d old=%d by auid %u", | |
266 | audit_enabled, old, loginuid); | |
1da177e4 LT |
267 | return old; |
268 | } | |
269 | ||
c94c257c | 270 | static int audit_set_failure(int state, uid_t loginuid) |
1da177e4 LT |
271 | { |
272 | int old = audit_failure; | |
273 | if (state != AUDIT_FAIL_SILENT | |
274 | && state != AUDIT_FAIL_PRINTK | |
275 | && state != AUDIT_FAIL_PANIC) | |
276 | return -EINVAL; | |
277 | audit_failure = state; | |
c0404993 SG |
278 | audit_log(NULL, AUDIT_CONFIG_CHANGE, |
279 | "audit_failure=%d old=%d by auid %u", | |
280 | audit_failure, old, loginuid); | |
1da177e4 LT |
281 | return old; |
282 | } | |
283 | ||
1da177e4 LT |
284 | void audit_send_reply(int pid, int seq, int type, int done, int multi, |
285 | void *payload, int size) | |
286 | { | |
287 | struct sk_buff *skb; | |
288 | struct nlmsghdr *nlh; | |
289 | int len = NLMSG_SPACE(size); | |
290 | void *data; | |
291 | int flags = multi ? NLM_F_MULTI : 0; | |
292 | int t = done ? NLMSG_DONE : type; | |
293 | ||
294 | skb = alloc_skb(len, GFP_KERNEL); | |
295 | if (!skb) | |
296 | goto nlmsg_failure; | |
297 | ||
298 | nlh = NLMSG_PUT(skb, pid, seq, t, len - sizeof(*nlh)); | |
299 | nlh->nlmsg_flags = flags; | |
300 | data = NLMSG_DATA(nlh); | |
301 | memcpy(data, payload, size); | |
302 | netlink_unicast(audit_sock, skb, pid, MSG_DONTWAIT); | |
303 | return; | |
304 | ||
305 | nlmsg_failure: /* Used by NLMSG_PUT */ | |
306 | if (skb) | |
307 | kfree_skb(skb); | |
308 | } | |
309 | ||
310 | /* | |
311 | * Check for appropriate CAP_AUDIT_ capabilities on incoming audit | |
312 | * control messages. | |
313 | */ | |
314 | static int audit_netlink_ok(kernel_cap_t eff_cap, u16 msg_type) | |
315 | { | |
316 | int err = 0; | |
317 | ||
318 | switch (msg_type) { | |
319 | case AUDIT_GET: | |
320 | case AUDIT_LIST: | |
321 | case AUDIT_SET: | |
322 | case AUDIT_ADD: | |
323 | case AUDIT_DEL: | |
c2f0c7c3 | 324 | case AUDIT_SIGNAL_INFO: |
1da177e4 LT |
325 | if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL)) |
326 | err = -EPERM; | |
327 | break; | |
209aba03 | 328 | case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG: |
1da177e4 LT |
329 | if (!cap_raised(eff_cap, CAP_AUDIT_WRITE)) |
330 | err = -EPERM; | |
331 | break; | |
332 | default: /* bad msg */ | |
333 | err = -EINVAL; | |
334 | } | |
335 | ||
336 | return err; | |
337 | } | |
338 | ||
339 | static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh) | |
340 | { | |
341 | u32 uid, pid, seq; | |
342 | void *data; | |
343 | struct audit_status *status_get, status_set; | |
344 | int err; | |
c0404993 | 345 | struct audit_buffer *ab; |
1da177e4 | 346 | u16 msg_type = nlh->nlmsg_type; |
c94c257c | 347 | uid_t loginuid; /* loginuid of sender */ |
c2f0c7c3 | 348 | struct audit_sig_info sig_data; |
1da177e4 LT |
349 | |
350 | err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type); | |
351 | if (err) | |
352 | return err; | |
353 | ||
354 | pid = NETLINK_CREDS(skb)->pid; | |
355 | uid = NETLINK_CREDS(skb)->uid; | |
c94c257c | 356 | loginuid = NETLINK_CB(skb).loginuid; |
1da177e4 LT |
357 | seq = nlh->nlmsg_seq; |
358 | data = NLMSG_DATA(nlh); | |
359 | ||
360 | switch (msg_type) { | |
361 | case AUDIT_GET: | |
362 | status_set.enabled = audit_enabled; | |
363 | status_set.failure = audit_failure; | |
364 | status_set.pid = audit_pid; | |
365 | status_set.rate_limit = audit_rate_limit; | |
366 | status_set.backlog_limit = audit_backlog_limit; | |
367 | status_set.lost = atomic_read(&audit_lost); | |
368 | status_set.backlog = atomic_read(&audit_backlog); | |
369 | audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0, | |
370 | &status_set, sizeof(status_set)); | |
371 | break; | |
372 | case AUDIT_SET: | |
373 | if (nlh->nlmsg_len < sizeof(struct audit_status)) | |
374 | return -EINVAL; | |
375 | status_get = (struct audit_status *)data; | |
376 | if (status_get->mask & AUDIT_STATUS_ENABLED) { | |
c94c257c | 377 | err = audit_set_enabled(status_get->enabled, loginuid); |
1da177e4 LT |
378 | if (err < 0) return err; |
379 | } | |
380 | if (status_get->mask & AUDIT_STATUS_FAILURE) { | |
c94c257c | 381 | err = audit_set_failure(status_get->failure, loginuid); |
1da177e4 LT |
382 | if (err < 0) return err; |
383 | } | |
384 | if (status_get->mask & AUDIT_STATUS_PID) { | |
385 | int old = audit_pid; | |
386 | audit_pid = status_get->pid; | |
c0404993 SG |
387 | audit_log(NULL, AUDIT_CONFIG_CHANGE, |
388 | "audit_pid=%d old=%d by auid %u", | |
c94c257c | 389 | audit_pid, old, loginuid); |
1da177e4 LT |
390 | } |
391 | if (status_get->mask & AUDIT_STATUS_RATE_LIMIT) | |
c94c257c | 392 | audit_set_rate_limit(status_get->rate_limit, loginuid); |
1da177e4 | 393 | if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT) |
c94c257c SH |
394 | audit_set_backlog_limit(status_get->backlog_limit, |
395 | loginuid); | |
1da177e4 | 396 | break; |
209aba03 | 397 | case AUDIT_FIRST_USER_MSG...AUDIT_LAST_USER_MSG: |
c0404993 SG |
398 | ab = audit_log_start(NULL, msg_type); |
399 | if (!ab) | |
400 | break; /* audit_panic has been called */ | |
401 | audit_log_format(ab, | |
c94c257c SH |
402 | "user pid=%d uid=%d length=%d loginuid=%u" |
403 | " msg='%.1024s'", | |
1da177e4 LT |
404 | pid, uid, |
405 | (int)(nlh->nlmsg_len | |
406 | - ((char *)data - (char *)nlh)), | |
c94c257c | 407 | loginuid, (char *)data); |
c0404993 SG |
408 | audit_set_pid(ab, pid); |
409 | audit_log_end(ab); | |
1da177e4 LT |
410 | break; |
411 | case AUDIT_ADD: | |
412 | case AUDIT_DEL: | |
413 | if (nlh->nlmsg_len < sizeof(struct audit_rule)) | |
414 | return -EINVAL; | |
415 | /* fallthrough */ | |
416 | case AUDIT_LIST: | |
1da177e4 | 417 | err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid, |
c94c257c | 418 | uid, seq, data, loginuid); |
1da177e4 | 419 | break; |
c2f0c7c3 SG |
420 | case AUDIT_SIGNAL_INFO: |
421 | sig_data.uid = audit_sig_uid; | |
422 | sig_data.pid = audit_sig_pid; | |
423 | audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO, | |
424 | 0, 0, &sig_data, sizeof(sig_data)); | |
425 | break; | |
1da177e4 LT |
426 | default: |
427 | err = -EINVAL; | |
428 | break; | |
429 | } | |
430 | ||
431 | return err < 0 ? err : 0; | |
432 | } | |
433 | ||
434 | /* Get message from skb (based on rtnetlink_rcv_skb). Each message is | |
435 | * processed by audit_receive_msg. Malformed skbs with wrong length are | |
436 | * discarded silently. */ | |
2a0a6ebe | 437 | static void audit_receive_skb(struct sk_buff *skb) |
1da177e4 LT |
438 | { |
439 | int err; | |
440 | struct nlmsghdr *nlh; | |
441 | u32 rlen; | |
442 | ||
443 | while (skb->len >= NLMSG_SPACE(0)) { | |
444 | nlh = (struct nlmsghdr *)skb->data; | |
445 | if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len) | |
2a0a6ebe | 446 | return; |
1da177e4 LT |
447 | rlen = NLMSG_ALIGN(nlh->nlmsg_len); |
448 | if (rlen > skb->len) | |
449 | rlen = skb->len; | |
450 | if ((err = audit_receive_msg(skb, nlh))) { | |
451 | netlink_ack(skb, nlh, err); | |
452 | } else if (nlh->nlmsg_flags & NLM_F_ACK) | |
453 | netlink_ack(skb, nlh, 0); | |
454 | skb_pull(skb, rlen); | |
455 | } | |
1da177e4 LT |
456 | } |
457 | ||
458 | /* Receive messages from netlink socket. */ | |
459 | static void audit_receive(struct sock *sk, int length) | |
460 | { | |
461 | struct sk_buff *skb; | |
2a0a6ebe | 462 | unsigned int qlen; |
1da177e4 | 463 | |
2a0a6ebe | 464 | down(&audit_netlink_sem); |
1da177e4 | 465 | |
2a0a6ebe HX |
466 | for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) { |
467 | skb = skb_dequeue(&sk->sk_receive_queue); | |
468 | audit_receive_skb(skb); | |
469 | kfree_skb(skb); | |
1da177e4 LT |
470 | } |
471 | up(&audit_netlink_sem); | |
472 | } | |
473 | ||
5ac52f33 | 474 | /* Grab skbuff from the audit_buffer and send to user space. */ |
1da177e4 LT |
475 | static inline int audit_log_drain(struct audit_buffer *ab) |
476 | { | |
8fc6115c | 477 | struct sk_buff *skb = ab->skb; |
1da177e4 | 478 | |
8fc6115c | 479 | if (skb) { |
1da177e4 LT |
480 | int retval = 0; |
481 | ||
482 | if (audit_pid) { | |
5ac52f33 | 483 | struct nlmsghdr *nlh = (struct nlmsghdr *)skb->data; |
5a241d77 | 484 | nlh->nlmsg_len = skb->len - NLMSG_SPACE(0); |
1da177e4 LT |
485 | skb_get(skb); /* because netlink_* frees */ |
486 | retval = netlink_unicast(audit_sock, skb, audit_pid, | |
487 | MSG_DONTWAIT); | |
488 | } | |
37509e74 CW |
489 | if (retval == -EAGAIN && |
490 | (atomic_read(&audit_backlog)) < audit_backlog_limit) { | |
1da177e4 LT |
491 | audit_log_end_irq(ab); |
492 | return 1; | |
493 | } | |
494 | if (retval < 0) { | |
495 | if (retval == -ECONNREFUSED) { | |
496 | printk(KERN_ERR | |
497 | "audit: *NO* daemon at audit_pid=%d\n", | |
498 | audit_pid); | |
499 | audit_pid = 0; | |
500 | } else | |
501 | audit_log_lost("netlink socket too busy"); | |
502 | } | |
503 | if (!audit_pid) { /* No daemon */ | |
8fc6115c | 504 | int offset = NLMSG_SPACE(0); |
1da177e4 | 505 | int len = skb->len - offset; |
c7fcb0ee PM |
506 | skb->data[offset + len] = '\0'; |
507 | printk(KERN_ERR "%s\n", skb->data + offset); | |
1da177e4 | 508 | } |
1da177e4 LT |
509 | } |
510 | return 0; | |
511 | } | |
512 | ||
513 | /* Initialize audit support at boot time. */ | |
514 | static int __init audit_init(void) | |
515 | { | |
516 | printk(KERN_INFO "audit: initializing netlink socket (%s)\n", | |
517 | audit_default ? "enabled" : "disabled"); | |
518 | audit_sock = netlink_kernel_create(NETLINK_AUDIT, audit_receive); | |
519 | if (!audit_sock) | |
520 | audit_panic("cannot initialize netlink socket"); | |
521 | ||
522 | audit_initialized = 1; | |
523 | audit_enabled = audit_default; | |
c0404993 | 524 | audit_log(NULL, AUDIT_KERNEL, "initialized"); |
1da177e4 LT |
525 | return 0; |
526 | } | |
1da177e4 LT |
527 | __initcall(audit_init); |
528 | ||
529 | /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */ | |
530 | static int __init audit_enable(char *str) | |
531 | { | |
532 | audit_default = !!simple_strtol(str, NULL, 0); | |
533 | printk(KERN_INFO "audit: %s%s\n", | |
534 | audit_default ? "enabled" : "disabled", | |
535 | audit_initialized ? "" : " (after initialization)"); | |
536 | if (audit_initialized) | |
537 | audit_enabled = audit_default; | |
538 | return 0; | |
539 | } | |
540 | ||
541 | __setup("audit=", audit_enable); | |
542 | ||
16e1904e CW |
543 | static void audit_buffer_free(struct audit_buffer *ab) |
544 | { | |
545 | unsigned long flags; | |
546 | ||
8fc6115c CW |
547 | if (!ab) |
548 | return; | |
549 | ||
5ac52f33 CW |
550 | if (ab->skb) |
551 | kfree_skb(ab->skb); | |
16e1904e CW |
552 | atomic_dec(&audit_backlog); |
553 | spin_lock_irqsave(&audit_freelist_lock, flags); | |
554 | if (++audit_freelist_count > AUDIT_MAXFREE) | |
555 | kfree(ab); | |
556 | else | |
557 | list_add(&ab->list, &audit_freelist); | |
558 | spin_unlock_irqrestore(&audit_freelist_lock, flags); | |
559 | } | |
560 | ||
c0404993 SG |
561 | static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx, |
562 | int gfp_mask, int type) | |
16e1904e CW |
563 | { |
564 | unsigned long flags; | |
565 | struct audit_buffer *ab = NULL; | |
c0404993 | 566 | struct nlmsghdr *nlh; |
16e1904e CW |
567 | |
568 | spin_lock_irqsave(&audit_freelist_lock, flags); | |
569 | if (!list_empty(&audit_freelist)) { | |
570 | ab = list_entry(audit_freelist.next, | |
571 | struct audit_buffer, list); | |
572 | list_del(&ab->list); | |
573 | --audit_freelist_count; | |
574 | } | |
575 | spin_unlock_irqrestore(&audit_freelist_lock, flags); | |
576 | ||
577 | if (!ab) { | |
4332bdd3 | 578 | ab = kmalloc(sizeof(*ab), gfp_mask); |
16e1904e | 579 | if (!ab) |
8fc6115c | 580 | goto err; |
16e1904e CW |
581 | } |
582 | atomic_inc(&audit_backlog); | |
8fc6115c | 583 | |
4332bdd3 | 584 | ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask); |
5ac52f33 | 585 | if (!ab->skb) |
8fc6115c CW |
586 | goto err; |
587 | ||
c0404993 SG |
588 | ab->ctx = ctx; |
589 | nlh = (struct nlmsghdr *)skb_put(ab->skb, NLMSG_SPACE(0)); | |
590 | nlh->nlmsg_type = type; | |
591 | nlh->nlmsg_flags = 0; | |
592 | nlh->nlmsg_pid = 0; | |
593 | nlh->nlmsg_seq = 0; | |
16e1904e | 594 | return ab; |
8fc6115c CW |
595 | err: |
596 | audit_buffer_free(ab); | |
597 | return NULL; | |
16e1904e | 598 | } |
1da177e4 LT |
599 | |
600 | /* Obtain an audit buffer. This routine does locking to obtain the | |
601 | * audit buffer, but then no locking is required for calls to | |
602 | * audit_log_*format. If the tsk is a task that is currently in a | |
603 | * syscall, then the syscall is marked as auditable and an audit record | |
604 | * will be written at syscall exit. If there is no associated task, tsk | |
605 | * should be NULL. */ | |
c0404993 | 606 | struct audit_buffer *audit_log_start(struct audit_context *ctx, int type) |
1da177e4 LT |
607 | { |
608 | struct audit_buffer *ab = NULL; | |
1da177e4 | 609 | struct timespec t; |
d812ddbb | 610 | unsigned int serial; |
1da177e4 LT |
611 | |
612 | if (!audit_initialized) | |
613 | return NULL; | |
614 | ||
615 | if (audit_backlog_limit | |
616 | && atomic_read(&audit_backlog) > audit_backlog_limit) { | |
617 | if (audit_rate_check()) | |
618 | printk(KERN_WARNING | |
619 | "audit: audit_backlog=%d > " | |
620 | "audit_backlog_limit=%d\n", | |
621 | atomic_read(&audit_backlog), | |
622 | audit_backlog_limit); | |
623 | audit_log_lost("backlog limit exceeded"); | |
624 | return NULL; | |
625 | } | |
626 | ||
c0404993 | 627 | ab = audit_buffer_alloc(ctx, GFP_ATOMIC, type); |
1da177e4 LT |
628 | if (!ab) { |
629 | audit_log_lost("out of memory in audit_log_start"); | |
630 | return NULL; | |
631 | } | |
632 | ||
197c69c6 | 633 | if (!audit_get_stamp(ab->ctx, &t, &serial)) { |
1da177e4 | 634 | t = CURRENT_TIME; |
d812ddbb SG |
635 | serial = 0; |
636 | } | |
197c69c6 | 637 | |
1da177e4 LT |
638 | audit_log_format(ab, "audit(%lu.%03lu:%u): ", |
639 | t.tv_sec, t.tv_nsec/1000000, serial); | |
640 | return ab; | |
641 | } | |
642 | ||
8fc6115c | 643 | /** |
5ac52f33 | 644 | * audit_expand - expand skb in the audit buffer |
8fc6115c CW |
645 | * @ab: audit_buffer |
646 | * | |
647 | * Returns 0 (no space) on failed expansion, or available space if | |
648 | * successful. | |
649 | */ | |
e3b926b4 | 650 | static inline int audit_expand(struct audit_buffer *ab, int extra) |
8fc6115c | 651 | { |
5ac52f33 | 652 | struct sk_buff *skb = ab->skb; |
e3b926b4 | 653 | int ret = pskb_expand_head(skb, skb_headroom(skb), extra, |
5ac52f33 CW |
654 | GFP_ATOMIC); |
655 | if (ret < 0) { | |
656 | audit_log_lost("out of memory in audit_expand"); | |
8fc6115c | 657 | return 0; |
5ac52f33 CW |
658 | } |
659 | return skb_tailroom(skb); | |
8fc6115c | 660 | } |
1da177e4 LT |
661 | |
662 | /* Format an audit message into the audit buffer. If there isn't enough | |
663 | * room in the audit buffer, more room will be allocated and vsnprint | |
664 | * will be called a second time. Currently, we assume that a printk | |
665 | * can't format message larger than 1024 bytes, so we don't either. */ | |
666 | static void audit_log_vformat(struct audit_buffer *ab, const char *fmt, | |
667 | va_list args) | |
668 | { | |
669 | int len, avail; | |
5ac52f33 | 670 | struct sk_buff *skb; |
eecb0a73 | 671 | va_list args2; |
1da177e4 LT |
672 | |
673 | if (!ab) | |
674 | return; | |
675 | ||
5ac52f33 CW |
676 | BUG_ON(!ab->skb); |
677 | skb = ab->skb; | |
678 | avail = skb_tailroom(skb); | |
679 | if (avail == 0) { | |
e3b926b4 | 680 | avail = audit_expand(ab, AUDIT_BUFSIZ); |
8fc6115c CW |
681 | if (!avail) |
682 | goto out; | |
1da177e4 | 683 | } |
eecb0a73 | 684 | va_copy(args2, args); |
5ac52f33 | 685 | len = vsnprintf(skb->tail, avail, fmt, args); |
1da177e4 LT |
686 | if (len >= avail) { |
687 | /* The printk buffer is 1024 bytes long, so if we get | |
688 | * here and AUDIT_BUFSIZ is at least 1024, then we can | |
689 | * log everything that printk could have logged. */ | |
5e014b10 | 690 | avail = audit_expand(ab, max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail)); |
8fc6115c CW |
691 | if (!avail) |
692 | goto out; | |
eecb0a73 | 693 | len = vsnprintf(skb->tail, avail, fmt, args2); |
1da177e4 | 694 | } |
168b7173 SG |
695 | if (len > 0) |
696 | skb_put(skb, len); | |
8fc6115c CW |
697 | out: |
698 | return; | |
1da177e4 LT |
699 | } |
700 | ||
701 | /* Format a message into the audit buffer. All the work is done in | |
702 | * audit_log_vformat. */ | |
703 | void audit_log_format(struct audit_buffer *ab, const char *fmt, ...) | |
704 | { | |
705 | va_list args; | |
706 | ||
707 | if (!ab) | |
708 | return; | |
709 | va_start(args, fmt); | |
710 | audit_log_vformat(ab, fmt, args); | |
711 | va_end(args); | |
712 | } | |
713 | ||
168b7173 SG |
714 | /* This function will take the passed buf and convert it into a string of |
715 | * ascii hex digits. The new string is placed onto the skb. */ | |
716 | void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf, | |
717 | size_t len) | |
83c7d091 | 718 | { |
168b7173 SG |
719 | int i, avail, new_len; |
720 | unsigned char *ptr; | |
721 | struct sk_buff *skb; | |
722 | static const unsigned char *hex = "0123456789ABCDEF"; | |
723 | ||
724 | BUG_ON(!ab->skb); | |
725 | skb = ab->skb; | |
726 | avail = skb_tailroom(skb); | |
727 | new_len = len<<1; | |
728 | if (new_len >= avail) { | |
729 | /* Round the buffer request up to the next multiple */ | |
730 | new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1); | |
731 | avail = audit_expand(ab, new_len); | |
732 | if (!avail) | |
733 | return; | |
734 | } | |
83c7d091 | 735 | |
168b7173 SG |
736 | ptr = skb->tail; |
737 | for (i=0; i<len; i++) { | |
738 | *ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */ | |
739 | *ptr++ = hex[buf[i] & 0x0F]; /* Lower nibble */ | |
740 | } | |
741 | *ptr = 0; | |
742 | skb_put(skb, len << 1); /* new string is twice the old string */ | |
83c7d091 | 743 | } |
744 | ||
168b7173 SG |
745 | /* This code will escape a string that is passed to it if the string |
746 | * contains a control character, unprintable character, double quote mark, | |
747 | * or a space. Unescaped strings will start and end with a double quote mark. | |
748 | * Strings that are escaped are printed in hex (2 digits per char). */ | |
83c7d091 | 749 | void audit_log_untrustedstring(struct audit_buffer *ab, const char *string) |
750 | { | |
81b7854d | 751 | const unsigned char *p = string; |
83c7d091 | 752 | |
753 | while (*p) { | |
168b7173 | 754 | if (*p == '"' || *p < 0x21 || *p > 0x7f) { |
83c7d091 | 755 | audit_log_hex(ab, string, strlen(string)); |
756 | return; | |
757 | } | |
758 | p++; | |
759 | } | |
760 | audit_log_format(ab, "\"%s\"", string); | |
761 | } | |
762 | ||
168b7173 | 763 | /* This is a helper-function to print the escaped d_path */ |
1da177e4 LT |
764 | void audit_log_d_path(struct audit_buffer *ab, const char *prefix, |
765 | struct dentry *dentry, struct vfsmount *vfsmnt) | |
766 | { | |
168b7173 | 767 | char *p, *path; |
1da177e4 | 768 | |
8fc6115c CW |
769 | if (prefix) |
770 | audit_log_format(ab, " %s", prefix); | |
1da177e4 | 771 | |
168b7173 SG |
772 | /* We will allow 11 spaces for ' (deleted)' to be appended */ |
773 | path = kmalloc(PATH_MAX+11, GFP_KERNEL); | |
774 | if (!path) { | |
775 | audit_log_format(ab, "<no memory>"); | |
776 | return; | |
1da177e4 | 777 | } |
168b7173 SG |
778 | p = d_path(dentry, vfsmnt, path, PATH_MAX+11); |
779 | if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */ | |
780 | /* FIXME: can we save some information here? */ | |
781 | audit_log_format(ab, "<too long>"); | |
782 | } else | |
783 | audit_log_untrustedstring(ab, p); | |
784 | kfree(path); | |
1da177e4 LT |
785 | } |
786 | ||
23f32d18 | 787 | /* Remove queued messages from the audit_txlist and send them to user space. */ |
1da177e4 LT |
788 | static void audit_tasklet_handler(unsigned long arg) |
789 | { | |
790 | LIST_HEAD(list); | |
791 | struct audit_buffer *ab; | |
792 | unsigned long flags; | |
793 | ||
794 | spin_lock_irqsave(&audit_txlist_lock, flags); | |
795 | list_splice_init(&audit_txlist, &list); | |
796 | spin_unlock_irqrestore(&audit_txlist_lock, flags); | |
797 | ||
798 | while (!list_empty(&list)) { | |
799 | ab = list_entry(list.next, struct audit_buffer, list); | |
800 | list_del(&ab->list); | |
801 | audit_log_end_fast(ab); | |
802 | } | |
803 | } | |
804 | ||
805 | static DECLARE_TASKLET(audit_tasklet, audit_tasklet_handler, 0); | |
806 | ||
807 | /* The netlink_* functions cannot be called inside an irq context, so | |
808 | * the audit buffer is places on a queue and a tasklet is scheduled to | |
809 | * remove them from the queue outside the irq context. May be called in | |
810 | * any context. */ | |
811 | static void audit_log_end_irq(struct audit_buffer *ab) | |
812 | { | |
813 | unsigned long flags; | |
814 | ||
815 | if (!ab) | |
816 | return; | |
817 | spin_lock_irqsave(&audit_txlist_lock, flags); | |
818 | list_add_tail(&ab->list, &audit_txlist); | |
819 | spin_unlock_irqrestore(&audit_txlist_lock, flags); | |
820 | ||
821 | tasklet_schedule(&audit_tasklet); | |
822 | } | |
823 | ||
824 | /* Send the message in the audit buffer directly to user space. May not | |
825 | * be called in an irq context. */ | |
826 | static void audit_log_end_fast(struct audit_buffer *ab) | |
827 | { | |
1da177e4 LT |
828 | BUG_ON(in_irq()); |
829 | if (!ab) | |
830 | return; | |
831 | if (!audit_rate_check()) { | |
832 | audit_log_lost("rate limit exceeded"); | |
833 | } else { | |
1da177e4 LT |
834 | if (audit_log_drain(ab)) |
835 | return; | |
836 | } | |
16e1904e | 837 | audit_buffer_free(ab); |
1da177e4 LT |
838 | } |
839 | ||
840 | /* Send or queue the message in the audit buffer, depending on the | |
841 | * current context. (A convenience function that may be called in any | |
842 | * context.) */ | |
843 | void audit_log_end(struct audit_buffer *ab) | |
844 | { | |
845 | if (in_irq()) | |
846 | audit_log_end_irq(ab); | |
847 | else | |
848 | audit_log_end_fast(ab); | |
849 | } | |
850 | ||
851 | /* Log an audit record. This is a convenience function that calls | |
852 | * audit_log_start, audit_log_vformat, and audit_log_end. It may be | |
853 | * called in any context. */ | |
c0404993 | 854 | void audit_log(struct audit_context *ctx, int type, const char *fmt, ...) |
1da177e4 LT |
855 | { |
856 | struct audit_buffer *ab; | |
857 | va_list args; | |
858 | ||
c0404993 | 859 | ab = audit_log_start(ctx, type); |
1da177e4 LT |
860 | if (ab) { |
861 | va_start(args, fmt); | |
862 | audit_log_vformat(ab, fmt, args); | |
863 | va_end(args); | |
864 | audit_log_end(ab); | |
865 | } | |
866 | } |