]>
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 | |
119 | * that list additions and deletions never happen simultaneiously in | |
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; | |
139 | struct sk_buff_head sklist; /* formatted skbs ready to send */ | |
140 | struct audit_context *ctx; /* NULL or associated context */ | |
141 | int len; /* used area of tmp */ | |
142 | char tmp[AUDIT_BUFSIZ]; | |
143 | ||
144 | /* Pointer to header and contents */ | |
145 | struct nlmsghdr *nlh; | |
146 | int total; | |
147 | int type; | |
148 | int pid; | |
1da177e4 LT |
149 | }; |
150 | ||
151 | void audit_set_type(struct audit_buffer *ab, int type) | |
152 | { | |
153 | ab->type = type; | |
154 | } | |
155 | ||
156 | struct audit_entry { | |
157 | struct list_head list; | |
158 | struct audit_rule rule; | |
159 | }; | |
160 | ||
161 | static void audit_log_end_irq(struct audit_buffer *ab); | |
162 | static void audit_log_end_fast(struct audit_buffer *ab); | |
163 | ||
164 | static void audit_panic(const char *message) | |
165 | { | |
166 | switch (audit_failure) | |
167 | { | |
168 | case AUDIT_FAIL_SILENT: | |
169 | break; | |
170 | case AUDIT_FAIL_PRINTK: | |
171 | printk(KERN_ERR "audit: %s\n", message); | |
172 | break; | |
173 | case AUDIT_FAIL_PANIC: | |
174 | panic("audit: %s\n", message); | |
175 | break; | |
176 | } | |
177 | } | |
178 | ||
179 | static inline int audit_rate_check(void) | |
180 | { | |
181 | static unsigned long last_check = 0; | |
182 | static int messages = 0; | |
183 | static DEFINE_SPINLOCK(lock); | |
184 | unsigned long flags; | |
185 | unsigned long now; | |
186 | unsigned long elapsed; | |
187 | int retval = 0; | |
188 | ||
189 | if (!audit_rate_limit) return 1; | |
190 | ||
191 | spin_lock_irqsave(&lock, flags); | |
192 | if (++messages < audit_rate_limit) { | |
193 | retval = 1; | |
194 | } else { | |
195 | now = jiffies; | |
196 | elapsed = now - last_check; | |
197 | if (elapsed > HZ) { | |
198 | last_check = now; | |
199 | messages = 0; | |
200 | retval = 1; | |
201 | } | |
202 | } | |
203 | spin_unlock_irqrestore(&lock, flags); | |
204 | ||
205 | return retval; | |
206 | } | |
207 | ||
208 | /* Emit at least 1 message per second, even if audit_rate_check is | |
209 | * throttling. */ | |
210 | void audit_log_lost(const char *message) | |
211 | { | |
212 | static unsigned long last_msg = 0; | |
213 | static DEFINE_SPINLOCK(lock); | |
214 | unsigned long flags; | |
215 | unsigned long now; | |
216 | int print; | |
217 | ||
218 | atomic_inc(&audit_lost); | |
219 | ||
220 | print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit); | |
221 | ||
222 | if (!print) { | |
223 | spin_lock_irqsave(&lock, flags); | |
224 | now = jiffies; | |
225 | if (now - last_msg > HZ) { | |
226 | print = 1; | |
227 | last_msg = now; | |
228 | } | |
229 | spin_unlock_irqrestore(&lock, flags); | |
230 | } | |
231 | ||
232 | if (print) { | |
233 | printk(KERN_WARNING | |
234 | "audit: audit_lost=%d audit_backlog=%d" | |
235 | " audit_rate_limit=%d audit_backlog_limit=%d\n", | |
236 | atomic_read(&audit_lost), | |
237 | atomic_read(&audit_backlog), | |
238 | audit_rate_limit, | |
239 | audit_backlog_limit); | |
240 | audit_panic(message); | |
241 | } | |
242 | ||
243 | } | |
244 | ||
c94c257c | 245 | static int audit_set_rate_limit(int limit, uid_t loginuid) |
1da177e4 LT |
246 | { |
247 | int old = audit_rate_limit; | |
248 | audit_rate_limit = limit; | |
c94c257c SH |
249 | audit_log(NULL, "audit_rate_limit=%d old=%d by auid %u", |
250 | audit_rate_limit, old, loginuid); | |
1da177e4 LT |
251 | return old; |
252 | } | |
253 | ||
c94c257c | 254 | static int audit_set_backlog_limit(int limit, uid_t loginuid) |
1da177e4 LT |
255 | { |
256 | int old = audit_backlog_limit; | |
257 | audit_backlog_limit = limit; | |
c94c257c SH |
258 | audit_log(NULL, "audit_backlog_limit=%d old=%d by auid %u", |
259 | audit_backlog_limit, old, loginuid); | |
1da177e4 LT |
260 | return old; |
261 | } | |
262 | ||
c94c257c | 263 | static int audit_set_enabled(int state, uid_t loginuid) |
1da177e4 LT |
264 | { |
265 | int old = audit_enabled; | |
266 | if (state != 0 && state != 1) | |
267 | return -EINVAL; | |
268 | audit_enabled = state; | |
c94c257c SH |
269 | audit_log(NULL, "audit_enabled=%d old=%d by auid %u", |
270 | audit_enabled, old, loginuid); | |
1da177e4 LT |
271 | return old; |
272 | } | |
273 | ||
c94c257c | 274 | static int audit_set_failure(int state, uid_t loginuid) |
1da177e4 LT |
275 | { |
276 | int old = audit_failure; | |
277 | if (state != AUDIT_FAIL_SILENT | |
278 | && state != AUDIT_FAIL_PRINTK | |
279 | && state != AUDIT_FAIL_PANIC) | |
280 | return -EINVAL; | |
281 | audit_failure = state; | |
c94c257c SH |
282 | audit_log(NULL, "audit_failure=%d old=%d by auid %u", |
283 | audit_failure, old, loginuid); | |
1da177e4 LT |
284 | return old; |
285 | } | |
286 | ||
287 | #ifdef CONFIG_NET | |
288 | void audit_send_reply(int pid, int seq, int type, int done, int multi, | |
289 | void *payload, int size) | |
290 | { | |
291 | struct sk_buff *skb; | |
292 | struct nlmsghdr *nlh; | |
293 | int len = NLMSG_SPACE(size); | |
294 | void *data; | |
295 | int flags = multi ? NLM_F_MULTI : 0; | |
296 | int t = done ? NLMSG_DONE : type; | |
297 | ||
298 | skb = alloc_skb(len, GFP_KERNEL); | |
299 | if (!skb) | |
300 | goto nlmsg_failure; | |
301 | ||
302 | nlh = NLMSG_PUT(skb, pid, seq, t, len - sizeof(*nlh)); | |
303 | nlh->nlmsg_flags = flags; | |
304 | data = NLMSG_DATA(nlh); | |
305 | memcpy(data, payload, size); | |
306 | netlink_unicast(audit_sock, skb, pid, MSG_DONTWAIT); | |
307 | return; | |
308 | ||
309 | nlmsg_failure: /* Used by NLMSG_PUT */ | |
310 | if (skb) | |
311 | kfree_skb(skb); | |
312 | } | |
313 | ||
314 | /* | |
315 | * Check for appropriate CAP_AUDIT_ capabilities on incoming audit | |
316 | * control messages. | |
317 | */ | |
318 | static int audit_netlink_ok(kernel_cap_t eff_cap, u16 msg_type) | |
319 | { | |
320 | int err = 0; | |
321 | ||
322 | switch (msg_type) { | |
323 | case AUDIT_GET: | |
324 | case AUDIT_LIST: | |
325 | case AUDIT_SET: | |
326 | case AUDIT_ADD: | |
327 | case AUDIT_DEL: | |
c2f0c7c3 | 328 | case AUDIT_SIGNAL_INFO: |
1da177e4 LT |
329 | if (!cap_raised(eff_cap, CAP_AUDIT_CONTROL)) |
330 | err = -EPERM; | |
331 | break; | |
332 | case AUDIT_USER: | |
333 | if (!cap_raised(eff_cap, CAP_AUDIT_WRITE)) | |
334 | err = -EPERM; | |
335 | break; | |
336 | default: /* bad msg */ | |
337 | err = -EINVAL; | |
338 | } | |
339 | ||
340 | return err; | |
341 | } | |
342 | ||
343 | static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh) | |
344 | { | |
345 | u32 uid, pid, seq; | |
346 | void *data; | |
347 | struct audit_status *status_get, status_set; | |
348 | int err; | |
349 | struct audit_buffer *ab; | |
350 | u16 msg_type = nlh->nlmsg_type; | |
c94c257c | 351 | uid_t loginuid; /* loginuid of sender */ |
c2f0c7c3 | 352 | struct audit_sig_info sig_data; |
1da177e4 LT |
353 | |
354 | err = audit_netlink_ok(NETLINK_CB(skb).eff_cap, msg_type); | |
355 | if (err) | |
356 | return err; | |
357 | ||
358 | pid = NETLINK_CREDS(skb)->pid; | |
359 | uid = NETLINK_CREDS(skb)->uid; | |
c94c257c | 360 | loginuid = NETLINK_CB(skb).loginuid; |
1da177e4 LT |
361 | seq = nlh->nlmsg_seq; |
362 | data = NLMSG_DATA(nlh); | |
363 | ||
364 | switch (msg_type) { | |
365 | case AUDIT_GET: | |
366 | status_set.enabled = audit_enabled; | |
367 | status_set.failure = audit_failure; | |
368 | status_set.pid = audit_pid; | |
369 | status_set.rate_limit = audit_rate_limit; | |
370 | status_set.backlog_limit = audit_backlog_limit; | |
371 | status_set.lost = atomic_read(&audit_lost); | |
372 | status_set.backlog = atomic_read(&audit_backlog); | |
373 | audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_GET, 0, 0, | |
374 | &status_set, sizeof(status_set)); | |
375 | break; | |
376 | case AUDIT_SET: | |
377 | if (nlh->nlmsg_len < sizeof(struct audit_status)) | |
378 | return -EINVAL; | |
379 | status_get = (struct audit_status *)data; | |
380 | if (status_get->mask & AUDIT_STATUS_ENABLED) { | |
c94c257c | 381 | err = audit_set_enabled(status_get->enabled, loginuid); |
1da177e4 LT |
382 | if (err < 0) return err; |
383 | } | |
384 | if (status_get->mask & AUDIT_STATUS_FAILURE) { | |
c94c257c | 385 | err = audit_set_failure(status_get->failure, loginuid); |
1da177e4 LT |
386 | if (err < 0) return err; |
387 | } | |
388 | if (status_get->mask & AUDIT_STATUS_PID) { | |
389 | int old = audit_pid; | |
390 | audit_pid = status_get->pid; | |
c94c257c SH |
391 | audit_log(NULL, "audit_pid=%d old=%d by auid %u", |
392 | audit_pid, old, loginuid); | |
1da177e4 LT |
393 | } |
394 | if (status_get->mask & AUDIT_STATUS_RATE_LIMIT) | |
c94c257c | 395 | audit_set_rate_limit(status_get->rate_limit, loginuid); |
1da177e4 | 396 | if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT) |
c94c257c SH |
397 | audit_set_backlog_limit(status_get->backlog_limit, |
398 | loginuid); | |
1da177e4 LT |
399 | break; |
400 | case AUDIT_USER: | |
401 | ab = audit_log_start(NULL); | |
402 | if (!ab) | |
403 | break; /* audit_panic has been called */ | |
404 | audit_log_format(ab, | |
c94c257c SH |
405 | "user pid=%d uid=%d length=%d loginuid=%u" |
406 | " msg='%.1024s'", | |
1da177e4 LT |
407 | pid, uid, |
408 | (int)(nlh->nlmsg_len | |
409 | - ((char *)data - (char *)nlh)), | |
c94c257c | 410 | loginuid, (char *)data); |
1da177e4 LT |
411 | ab->type = AUDIT_USER; |
412 | ab->pid = pid; | |
413 | audit_log_end(ab); | |
414 | break; | |
415 | case AUDIT_ADD: | |
416 | case AUDIT_DEL: | |
417 | if (nlh->nlmsg_len < sizeof(struct audit_rule)) | |
418 | return -EINVAL; | |
419 | /* fallthrough */ | |
420 | case AUDIT_LIST: | |
421 | #ifdef CONFIG_AUDITSYSCALL | |
422 | err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid, | |
c94c257c | 423 | uid, seq, data, loginuid); |
1da177e4 LT |
424 | #else |
425 | err = -EOPNOTSUPP; | |
426 | #endif | |
427 | break; | |
c2f0c7c3 SG |
428 | case AUDIT_SIGNAL_INFO: |
429 | sig_data.uid = audit_sig_uid; | |
430 | sig_data.pid = audit_sig_pid; | |
431 | audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO, | |
432 | 0, 0, &sig_data, sizeof(sig_data)); | |
433 | break; | |
1da177e4 LT |
434 | default: |
435 | err = -EINVAL; | |
436 | break; | |
437 | } | |
438 | ||
439 | return err < 0 ? err : 0; | |
440 | } | |
441 | ||
442 | /* Get message from skb (based on rtnetlink_rcv_skb). Each message is | |
443 | * processed by audit_receive_msg. Malformed skbs with wrong length are | |
444 | * discarded silently. */ | |
2a0a6ebe | 445 | static void audit_receive_skb(struct sk_buff *skb) |
1da177e4 LT |
446 | { |
447 | int err; | |
448 | struct nlmsghdr *nlh; | |
449 | u32 rlen; | |
450 | ||
451 | while (skb->len >= NLMSG_SPACE(0)) { | |
452 | nlh = (struct nlmsghdr *)skb->data; | |
453 | if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len) | |
2a0a6ebe | 454 | return; |
1da177e4 LT |
455 | rlen = NLMSG_ALIGN(nlh->nlmsg_len); |
456 | if (rlen > skb->len) | |
457 | rlen = skb->len; | |
458 | if ((err = audit_receive_msg(skb, nlh))) { | |
459 | netlink_ack(skb, nlh, err); | |
460 | } else if (nlh->nlmsg_flags & NLM_F_ACK) | |
461 | netlink_ack(skb, nlh, 0); | |
462 | skb_pull(skb, rlen); | |
463 | } | |
1da177e4 LT |
464 | } |
465 | ||
466 | /* Receive messages from netlink socket. */ | |
467 | static void audit_receive(struct sock *sk, int length) | |
468 | { | |
469 | struct sk_buff *skb; | |
2a0a6ebe | 470 | unsigned int qlen; |
1da177e4 | 471 | |
2a0a6ebe | 472 | down(&audit_netlink_sem); |
1da177e4 | 473 | |
2a0a6ebe HX |
474 | for (qlen = skb_queue_len(&sk->sk_receive_queue); qlen; qlen--) { |
475 | skb = skb_dequeue(&sk->sk_receive_queue); | |
476 | audit_receive_skb(skb); | |
477 | kfree_skb(skb); | |
1da177e4 LT |
478 | } |
479 | up(&audit_netlink_sem); | |
480 | } | |
481 | ||
482 | /* Move data from tmp buffer into an skb. This is an extra copy, and | |
483 | * that is unfortunate. However, the copy will only occur when a record | |
484 | * is being written to user space, which is already a high-overhead | |
485 | * operation. (Elimination of the copy is possible, for example, by | |
486 | * writing directly into a pre-allocated skb, at the cost of wasting | |
487 | * memory. */ | |
488 | static void audit_log_move(struct audit_buffer *ab) | |
489 | { | |
490 | struct sk_buff *skb; | |
491 | char *start; | |
492 | int extra = ab->nlh ? 0 : NLMSG_SPACE(0); | |
493 | ||
494 | /* possible resubmission */ | |
495 | if (ab->len == 0) | |
496 | return; | |
497 | ||
0dd8e06b | 498 | skb = skb_peek_tail(&ab->sklist); |
1da177e4 LT |
499 | if (!skb || skb_tailroom(skb) <= ab->len + extra) { |
500 | skb = alloc_skb(2 * ab->len + extra, GFP_ATOMIC); | |
501 | if (!skb) { | |
502 | ab->len = 0; /* Lose information in ab->tmp */ | |
503 | audit_log_lost("out of memory in audit_log_move"); | |
504 | return; | |
505 | } | |
506 | __skb_queue_tail(&ab->sklist, skb); | |
507 | if (!ab->nlh) | |
508 | ab->nlh = (struct nlmsghdr *)skb_put(skb, | |
509 | NLMSG_SPACE(0)); | |
510 | } | |
511 | start = skb_put(skb, ab->len); | |
512 | memcpy(start, ab->tmp, ab->len); | |
513 | ab->len = 0; | |
514 | } | |
515 | ||
516 | /* Iterate over the skbuff in the audit_buffer, sending their contents | |
517 | * to user space. */ | |
518 | static inline int audit_log_drain(struct audit_buffer *ab) | |
519 | { | |
520 | struct sk_buff *skb; | |
521 | ||
522 | while ((skb = skb_dequeue(&ab->sklist))) { | |
523 | int retval = 0; | |
524 | ||
525 | if (audit_pid) { | |
526 | if (ab->nlh) { | |
527 | ab->nlh->nlmsg_len = ab->total; | |
528 | ab->nlh->nlmsg_type = ab->type; | |
529 | ab->nlh->nlmsg_flags = 0; | |
530 | ab->nlh->nlmsg_seq = 0; | |
531 | ab->nlh->nlmsg_pid = ab->pid; | |
532 | } | |
533 | skb_get(skb); /* because netlink_* frees */ | |
534 | retval = netlink_unicast(audit_sock, skb, audit_pid, | |
535 | MSG_DONTWAIT); | |
536 | } | |
37509e74 CW |
537 | if (retval == -EAGAIN && |
538 | (atomic_read(&audit_backlog)) < audit_backlog_limit) { | |
539 | skb_queue_head(&ab->sklist, skb); | |
1da177e4 LT |
540 | audit_log_end_irq(ab); |
541 | return 1; | |
542 | } | |
543 | if (retval < 0) { | |
544 | if (retval == -ECONNREFUSED) { | |
545 | printk(KERN_ERR | |
546 | "audit: *NO* daemon at audit_pid=%d\n", | |
547 | audit_pid); | |
548 | audit_pid = 0; | |
549 | } else | |
550 | audit_log_lost("netlink socket too busy"); | |
551 | } | |
552 | if (!audit_pid) { /* No daemon */ | |
553 | int offset = ab->nlh ? NLMSG_SPACE(0) : 0; | |
554 | int len = skb->len - offset; | |
c7fcb0ee PM |
555 | skb->data[offset + len] = '\0'; |
556 | printk(KERN_ERR "%s\n", skb->data + offset); | |
1da177e4 LT |
557 | } |
558 | kfree_skb(skb); | |
559 | ab->nlh = NULL; | |
560 | } | |
561 | return 0; | |
562 | } | |
563 | ||
564 | /* Initialize audit support at boot time. */ | |
565 | static int __init audit_init(void) | |
566 | { | |
567 | printk(KERN_INFO "audit: initializing netlink socket (%s)\n", | |
568 | audit_default ? "enabled" : "disabled"); | |
569 | audit_sock = netlink_kernel_create(NETLINK_AUDIT, audit_receive); | |
570 | if (!audit_sock) | |
571 | audit_panic("cannot initialize netlink socket"); | |
572 | ||
573 | audit_initialized = 1; | |
574 | audit_enabled = audit_default; | |
575 | audit_log(NULL, "initialized"); | |
576 | return 0; | |
577 | } | |
578 | ||
579 | #else | |
580 | /* Without CONFIG_NET, we have no skbuffs. For now, print what we have | |
581 | * in the buffer. */ | |
582 | static void audit_log_move(struct audit_buffer *ab) | |
583 | { | |
584 | printk(KERN_ERR "%*.*s\n", ab->len, ab->len, ab->tmp); | |
585 | ab->len = 0; | |
586 | } | |
587 | ||
588 | static inline int audit_log_drain(struct audit_buffer *ab) | |
589 | { | |
590 | return 0; | |
591 | } | |
592 | ||
593 | /* Initialize audit support at boot time. */ | |
594 | int __init audit_init(void) | |
595 | { | |
596 | printk(KERN_INFO "audit: initializing WITHOUT netlink support\n"); | |
597 | audit_sock = NULL; | |
598 | audit_pid = 0; | |
599 | ||
600 | audit_initialized = 1; | |
601 | audit_enabled = audit_default; | |
602 | audit_log(NULL, "initialized"); | |
603 | return 0; | |
604 | } | |
605 | #endif | |
606 | ||
607 | __initcall(audit_init); | |
608 | ||
609 | /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */ | |
610 | static int __init audit_enable(char *str) | |
611 | { | |
612 | audit_default = !!simple_strtol(str, NULL, 0); | |
613 | printk(KERN_INFO "audit: %s%s\n", | |
614 | audit_default ? "enabled" : "disabled", | |
615 | audit_initialized ? "" : " (after initialization)"); | |
616 | if (audit_initialized) | |
617 | audit_enabled = audit_default; | |
618 | return 0; | |
619 | } | |
620 | ||
621 | __setup("audit=", audit_enable); | |
622 | ||
16e1904e CW |
623 | static void audit_buffer_free(struct audit_buffer *ab) |
624 | { | |
625 | unsigned long flags; | |
626 | ||
627 | atomic_dec(&audit_backlog); | |
628 | spin_lock_irqsave(&audit_freelist_lock, flags); | |
629 | if (++audit_freelist_count > AUDIT_MAXFREE) | |
630 | kfree(ab); | |
631 | else | |
632 | list_add(&ab->list, &audit_freelist); | |
633 | spin_unlock_irqrestore(&audit_freelist_lock, flags); | |
634 | } | |
635 | ||
636 | static struct audit_buffer * audit_buffer_alloc(int gfp_mask) | |
637 | { | |
638 | unsigned long flags; | |
639 | struct audit_buffer *ab = NULL; | |
640 | ||
641 | spin_lock_irqsave(&audit_freelist_lock, flags); | |
642 | if (!list_empty(&audit_freelist)) { | |
643 | ab = list_entry(audit_freelist.next, | |
644 | struct audit_buffer, list); | |
645 | list_del(&ab->list); | |
646 | --audit_freelist_count; | |
647 | } | |
648 | spin_unlock_irqrestore(&audit_freelist_lock, flags); | |
649 | ||
650 | if (!ab) { | |
651 | ab = kmalloc(sizeof(*ab), GFP_ATOMIC); | |
652 | if (!ab) | |
653 | goto out; | |
654 | } | |
655 | atomic_inc(&audit_backlog); | |
656 | out: | |
657 | return ab; | |
658 | } | |
1da177e4 LT |
659 | |
660 | /* Obtain an audit buffer. This routine does locking to obtain the | |
661 | * audit buffer, but then no locking is required for calls to | |
662 | * audit_log_*format. If the tsk is a task that is currently in a | |
663 | * syscall, then the syscall is marked as auditable and an audit record | |
664 | * will be written at syscall exit. If there is no associated task, tsk | |
665 | * should be NULL. */ | |
666 | struct audit_buffer *audit_log_start(struct audit_context *ctx) | |
667 | { | |
668 | struct audit_buffer *ab = NULL; | |
1da177e4 | 669 | struct timespec t; |
d812ddbb | 670 | unsigned int serial; |
1da177e4 LT |
671 | |
672 | if (!audit_initialized) | |
673 | return NULL; | |
674 | ||
675 | if (audit_backlog_limit | |
676 | && atomic_read(&audit_backlog) > audit_backlog_limit) { | |
677 | if (audit_rate_check()) | |
678 | printk(KERN_WARNING | |
679 | "audit: audit_backlog=%d > " | |
680 | "audit_backlog_limit=%d\n", | |
681 | atomic_read(&audit_backlog), | |
682 | audit_backlog_limit); | |
683 | audit_log_lost("backlog limit exceeded"); | |
684 | return NULL; | |
685 | } | |
686 | ||
16e1904e | 687 | ab = audit_buffer_alloc(GFP_ATOMIC); |
1da177e4 LT |
688 | if (!ab) { |
689 | audit_log_lost("out of memory in audit_log_start"); | |
690 | return NULL; | |
691 | } | |
692 | ||
1da177e4 LT |
693 | skb_queue_head_init(&ab->sklist); |
694 | ||
695 | ab->ctx = ctx; | |
696 | ab->len = 0; | |
697 | ab->nlh = NULL; | |
698 | ab->total = 0; | |
699 | ab->type = AUDIT_KERNEL; | |
700 | ab->pid = 0; | |
1da177e4 LT |
701 | |
702 | #ifdef CONFIG_AUDITSYSCALL | |
703 | if (ab->ctx) | |
704 | audit_get_stamp(ab->ctx, &t, &serial); | |
705 | else | |
706 | #endif | |
d812ddbb | 707 | { |
1da177e4 | 708 | t = CURRENT_TIME; |
d812ddbb SG |
709 | serial = 0; |
710 | } | |
1da177e4 LT |
711 | audit_log_format(ab, "audit(%lu.%03lu:%u): ", |
712 | t.tv_sec, t.tv_nsec/1000000, serial); | |
713 | return ab; | |
714 | } | |
715 | ||
716 | ||
717 | /* Format an audit message into the audit buffer. If there isn't enough | |
718 | * room in the audit buffer, more room will be allocated and vsnprint | |
719 | * will be called a second time. Currently, we assume that a printk | |
720 | * can't format message larger than 1024 bytes, so we don't either. */ | |
721 | static void audit_log_vformat(struct audit_buffer *ab, const char *fmt, | |
722 | va_list args) | |
723 | { | |
724 | int len, avail; | |
725 | ||
726 | if (!ab) | |
727 | return; | |
728 | ||
729 | avail = sizeof(ab->tmp) - ab->len; | |
730 | if (avail <= 0) { | |
731 | audit_log_move(ab); | |
732 | avail = sizeof(ab->tmp) - ab->len; | |
733 | } | |
734 | len = vsnprintf(ab->tmp + ab->len, avail, fmt, args); | |
735 | if (len >= avail) { | |
736 | /* The printk buffer is 1024 bytes long, so if we get | |
737 | * here and AUDIT_BUFSIZ is at least 1024, then we can | |
738 | * log everything that printk could have logged. */ | |
739 | audit_log_move(ab); | |
740 | avail = sizeof(ab->tmp) - ab->len; | |
741 | len = vsnprintf(ab->tmp + ab->len, avail, fmt, args); | |
742 | } | |
743 | ab->len += (len < avail) ? len : avail; | |
744 | ab->total += (len < avail) ? len : avail; | |
745 | } | |
746 | ||
747 | /* Format a message into the audit buffer. All the work is done in | |
748 | * audit_log_vformat. */ | |
749 | void audit_log_format(struct audit_buffer *ab, const char *fmt, ...) | |
750 | { | |
751 | va_list args; | |
752 | ||
753 | if (!ab) | |
754 | return; | |
755 | va_start(args, fmt); | |
756 | audit_log_vformat(ab, fmt, args); | |
757 | va_end(args); | |
758 | } | |
759 | ||
83c7d091 DW |
760 | void audit_log_hex(struct audit_buffer *ab, const unsigned char *buf, size_t len) |
761 | { | |
762 | int i; | |
763 | ||
764 | for (i=0; i<len; i++) | |
765 | audit_log_format(ab, "%02x", buf[i]); | |
766 | } | |
767 | ||
768 | void audit_log_untrustedstring(struct audit_buffer *ab, const char *string) | |
769 | { | |
81b7854d | 770 | const unsigned char *p = string; |
83c7d091 DW |
771 | |
772 | while (*p) { | |
773 | if (*p == '"' || *p == ' ' || *p < 0x20 || *p > 0x7f) { | |
774 | audit_log_hex(ab, string, strlen(string)); | |
775 | return; | |
776 | } | |
777 | p++; | |
778 | } | |
779 | audit_log_format(ab, "\"%s\"", string); | |
780 | } | |
781 | ||
782 | ||
1da177e4 LT |
783 | /* This is a helper-function to print the d_path without using a static |
784 | * buffer or allocating another buffer in addition to the one in | |
785 | * audit_buffer. */ | |
786 | void audit_log_d_path(struct audit_buffer *ab, const char *prefix, | |
787 | struct dentry *dentry, struct vfsmount *vfsmnt) | |
788 | { | |
789 | char *p; | |
790 | int len, avail; | |
791 | ||
792 | if (prefix) audit_log_format(ab, " %s", prefix); | |
793 | ||
794 | if (ab->len > 128) | |
795 | audit_log_move(ab); | |
796 | avail = sizeof(ab->tmp) - ab->len; | |
797 | p = d_path(dentry, vfsmnt, ab->tmp + ab->len, avail); | |
798 | if (IS_ERR(p)) { | |
799 | /* FIXME: can we save some information here? */ | |
800 | audit_log_format(ab, "<toolong>"); | |
801 | } else { | |
802 | /* path isn't at start of buffer */ | |
803 | len = (ab->tmp + sizeof(ab->tmp) - 1) - p; | |
804 | memmove(ab->tmp + ab->len, p, len); | |
805 | ab->len += len; | |
806 | ab->total += len; | |
807 | } | |
808 | } | |
809 | ||
810 | /* Remove queued messages from the audit_txlist and send them to userspace. */ | |
811 | static void audit_tasklet_handler(unsigned long arg) | |
812 | { | |
813 | LIST_HEAD(list); | |
814 | struct audit_buffer *ab; | |
815 | unsigned long flags; | |
816 | ||
817 | spin_lock_irqsave(&audit_txlist_lock, flags); | |
818 | list_splice_init(&audit_txlist, &list); | |
819 | spin_unlock_irqrestore(&audit_txlist_lock, flags); | |
820 | ||
821 | while (!list_empty(&list)) { | |
822 | ab = list_entry(list.next, struct audit_buffer, list); | |
823 | list_del(&ab->list); | |
824 | audit_log_end_fast(ab); | |
825 | } | |
826 | } | |
827 | ||
828 | static DECLARE_TASKLET(audit_tasklet, audit_tasklet_handler, 0); | |
829 | ||
830 | /* The netlink_* functions cannot be called inside an irq context, so | |
831 | * the audit buffer is places on a queue and a tasklet is scheduled to | |
832 | * remove them from the queue outside the irq context. May be called in | |
833 | * any context. */ | |
834 | static void audit_log_end_irq(struct audit_buffer *ab) | |
835 | { | |
836 | unsigned long flags; | |
837 | ||
838 | if (!ab) | |
839 | return; | |
840 | spin_lock_irqsave(&audit_txlist_lock, flags); | |
841 | list_add_tail(&ab->list, &audit_txlist); | |
842 | spin_unlock_irqrestore(&audit_txlist_lock, flags); | |
843 | ||
844 | tasklet_schedule(&audit_tasklet); | |
845 | } | |
846 | ||
847 | /* Send the message in the audit buffer directly to user space. May not | |
848 | * be called in an irq context. */ | |
849 | static void audit_log_end_fast(struct audit_buffer *ab) | |
850 | { | |
1da177e4 LT |
851 | BUG_ON(in_irq()); |
852 | if (!ab) | |
853 | return; | |
854 | if (!audit_rate_check()) { | |
855 | audit_log_lost("rate limit exceeded"); | |
856 | } else { | |
857 | audit_log_move(ab); | |
858 | if (audit_log_drain(ab)) | |
859 | return; | |
860 | } | |
16e1904e | 861 | audit_buffer_free(ab); |
1da177e4 LT |
862 | } |
863 | ||
864 | /* Send or queue the message in the audit buffer, depending on the | |
865 | * current context. (A convenience function that may be called in any | |
866 | * context.) */ | |
867 | void audit_log_end(struct audit_buffer *ab) | |
868 | { | |
869 | if (in_irq()) | |
870 | audit_log_end_irq(ab); | |
871 | else | |
872 | audit_log_end_fast(ab); | |
873 | } | |
874 | ||
875 | /* Log an audit record. This is a convenience function that calls | |
876 | * audit_log_start, audit_log_vformat, and audit_log_end. It may be | |
877 | * called in any context. */ | |
878 | void audit_log(struct audit_context *ctx, const char *fmt, ...) | |
879 | { | |
880 | struct audit_buffer *ab; | |
881 | va_list args; | |
882 | ||
883 | ab = audit_log_start(ctx); | |
884 | if (ab) { | |
885 | va_start(args, fmt); | |
886 | audit_log_vformat(ab, fmt, args); | |
887 | va_end(args); | |
888 | audit_log_end(ab); | |
889 | } | |
890 | } |