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 | * | |
6a01b07f | 5 | * Copyright 2003-2007 Red Hat Inc., Durham, North Carolina. |
1da177e4 LT |
6 | * All Rights Reserved. |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | * | |
22 | * Written by Rickard E. (Rik) Faith <faith@redhat.com> | |
23 | * | |
24 | * Goals: 1) Integrate fully with SELinux. | |
25 | * 2) Minimal run-time overhead: | |
26 | * a) Minimal when syscall auditing is disabled (audit_enable=0). | |
27 | * b) Small when syscall auditing is enabled and no audit record | |
28 | * is generated (defer as much work as possible to record | |
29 | * generation time): | |
30 | * i) context is allocated, | |
31 | * ii) names from getname are stored without a copy, and | |
32 | * iii) inode information stored from path_lookup. | |
33 | * 3) Ability to disable syscall auditing at boot time (audit=0). | |
34 | * 4) Usable by other parts of the kernel (if audit_log* is called, | |
35 | * then a syscall record will be generated automatically for the | |
36 | * current syscall). | |
37 | * 5) Netlink interface to user-space. | |
38 | * 6) Support low-overhead kernel-based filtering to minimize the | |
39 | * information that must be passed to user-space. | |
40 | * | |
85c8721f | 41 | * Example user-space utilities: http://people.redhat.com/sgrubb/audit/ |
1da177e4 LT |
42 | */ |
43 | ||
44 | #include <linux/init.h> | |
1da177e4 | 45 | #include <asm/types.h> |
715b49ef | 46 | #include <asm/atomic.h> |
1da177e4 LT |
47 | #include <linux/mm.h> |
48 | #include <linux/module.h> | |
b7d11258 DW |
49 | #include <linux/err.h> |
50 | #include <linux/kthread.h> | |
1da177e4 LT |
51 | |
52 | #include <linux/audit.h> | |
53 | ||
54 | #include <net/sock.h> | |
93315ed6 | 55 | #include <net/netlink.h> |
1da177e4 LT |
56 | #include <linux/skbuff.h> |
57 | #include <linux/netlink.h> | |
3dc7e315 | 58 | #include <linux/selinux.h> |
f368c07d | 59 | #include <linux/inotify.h> |
7dfb7103 | 60 | #include <linux/freezer.h> |
522ed776 | 61 | #include <linux/tty.h> |
3dc7e315 DG |
62 | |
63 | #include "audit.h" | |
1da177e4 LT |
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 | ||
1a6b9f23 EP |
69 | #define AUDIT_OFF 0 |
70 | #define AUDIT_ON 1 | |
71 | #define AUDIT_LOCKED 2 | |
1da177e4 LT |
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. */ | |
c2f0c7c3 | 82 | int audit_pid; |
1da177e4 | 83 | |
b0dd25a8 | 84 | /* If audit_rate_limit is non-zero, limit the rate of sending audit records |
1da177e4 LT |
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; | |
ac4cec44 DW |
91 | static int audit_backlog_wait_time = 60 * HZ; |
92 | static int audit_backlog_wait_overflow = 0; | |
1da177e4 | 93 | |
c2f0c7c3 SG |
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; | |
e1396065 | 97 | u32 audit_sig_sid = 0; |
c2f0c7c3 | 98 | |
1da177e4 LT |
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 | ||
f368c07d AG |
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 | ||
b7d11258 | 117 | /* The audit_freelist is a list of pre-allocated audit buffers (if more |
1da177e4 LT |
118 | * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of |
119 | * being placed on the freelist). */ | |
1da177e4 | 120 | static DEFINE_SPINLOCK(audit_freelist_lock); |
b0dd25a8 | 121 | static int audit_freelist_count; |
1da177e4 LT |
122 | static LIST_HEAD(audit_freelist); |
123 | ||
b7d11258 DW |
124 | static struct sk_buff_head audit_skb_queue; |
125 | static struct task_struct *kauditd_task; | |
126 | static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait); | |
9ad9ad38 | 127 | static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait); |
1da177e4 | 128 | |
f368c07d AG |
129 | /* Serialize requests from userspace. */ |
130 | static DEFINE_MUTEX(audit_cmd_mutex); | |
1da177e4 LT |
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; | |
8fc6115c | 148 | struct sk_buff *skb; /* formatted skb ready to send */ |
1da177e4 | 149 | struct audit_context *ctx; /* NULL or associated context */ |
9796fdd8 | 150 | gfp_t gfp_mask; |
1da177e4 LT |
151 | }; |
152 | ||
c0404993 SG |
153 | static void audit_set_pid(struct audit_buffer *ab, pid_t pid) |
154 | { | |
50397bd1 EP |
155 | if (ab) { |
156 | struct nlmsghdr *nlh = nlmsg_hdr(ab->skb); | |
157 | nlh->nlmsg_pid = pid; | |
158 | } | |
c0404993 SG |
159 | } |
160 | ||
8c8570fb | 161 | void audit_panic(const char *message) |
1da177e4 LT |
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 | ||
b0dd25a8 RD |
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 | */ | |
1da177e4 LT |
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 | |
b7d11258 | 237 | "audit: audit_lost=%d audit_rate_limit=%d audit_backlog_limit=%d\n", |
1da177e4 | 238 | atomic_read(&audit_lost), |
1da177e4 LT |
239 | audit_rate_limit, |
240 | audit_backlog_limit); | |
241 | audit_panic(message); | |
242 | } | |
1da177e4 LT |
243 | } |
244 | ||
1a6b9f23 EP |
245 | static int audit_log_config_change(char *function_name, int new, int old, |
246 | uid_t loginuid, u32 sid, int allow_changes) | |
1da177e4 | 247 | { |
1a6b9f23 EP |
248 | struct audit_buffer *ab; |
249 | int rc = 0; | |
ce29b682 | 250 | |
1a6b9f23 EP |
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); | |
ce29b682 SG |
254 | if (sid) { |
255 | char *ctx = NULL; | |
256 | u32 len; | |
1a6b9f23 EP |
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); | |
6a01b07f | 264 | kfree(ctx); |
1a6b9f23 | 265 | } |
6a01b07f | 266 | } |
1a6b9f23 EP |
267 | audit_log_format(ab, " res=%d", allow_changes); |
268 | audit_log_end(ab); | |
6a01b07f | 269 | return rc; |
1da177e4 LT |
270 | } |
271 | ||
1a6b9f23 EP |
272 | static int audit_do_config_change(char *function_name, int *to_change, |
273 | int new, uid_t loginuid, u32 sid) | |
1da177e4 | 274 | { |
1a6b9f23 | 275 | int allow_changes, rc = 0, old = *to_change; |
6a01b07f SG |
276 | |
277 | /* check if we are locked */ | |
1a6b9f23 EP |
278 | if (audit_enabled == AUDIT_LOCKED) |
279 | allow_changes = 0; | |
6a01b07f | 280 | else |
1a6b9f23 | 281 | allow_changes = 1; |
ce29b682 | 282 | |
1a6b9f23 EP |
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; | |
6a01b07f | 288 | } |
6a01b07f SG |
289 | |
290 | /* If we are allowed, make the change */ | |
1a6b9f23 EP |
291 | if (allow_changes == 1) |
292 | *to_change = new; | |
6a01b07f SG |
293 | /* Not allowed, update reason */ |
294 | else if (rc == 0) | |
295 | rc = -EPERM; | |
296 | return rc; | |
1da177e4 LT |
297 | } |
298 | ||
1a6b9f23 | 299 | static int audit_set_rate_limit(int limit, uid_t loginuid, u32 sid) |
1da177e4 | 300 | { |
1a6b9f23 EP |
301 | return audit_do_config_change("audit_rate_limit", &audit_rate_limit, |
302 | limit, loginuid, sid); | |
303 | } | |
ce29b682 | 304 | |
1a6b9f23 EP |
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 | } | |
6a01b07f | 310 | |
1a6b9f23 EP |
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; | |
6a01b07f | 315 | |
1a6b9f23 EP |
316 | return audit_do_config_change("audit_enabled", &audit_enabled, state, |
317 | loginuid, sid); | |
1da177e4 LT |
318 | } |
319 | ||
ce29b682 | 320 | static int audit_set_failure(int state, uid_t loginuid, u32 sid) |
1da177e4 | 321 | { |
1da177e4 LT |
322 | if (state != AUDIT_FAIL_SILENT |
323 | && state != AUDIT_FAIL_PRINTK | |
324 | && state != AUDIT_FAIL_PANIC) | |
325 | return -EINVAL; | |
ce29b682 | 326 | |
1a6b9f23 EP |
327 | return audit_do_config_change("audit_failure", &audit_failure, state, |
328 | loginuid, sid); | |
1da177e4 LT |
329 | } |
330 | ||
97a41e26 | 331 | static int kauditd_thread(void *dummy) |
b7d11258 DW |
332 | { |
333 | struct sk_buff *skb; | |
334 | ||
83144186 | 335 | set_freezable(); |
4899b8b1 | 336 | while (!kthread_should_stop()) { |
b7d11258 | 337 | skb = skb_dequeue(&audit_skb_queue); |
9ad9ad38 | 338 | wake_up(&audit_backlog_wait); |
b7d11258 DW |
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 { | |
e1b09eba | 348 | printk(KERN_NOTICE "%s\n", skb->data + NLMSG_SPACE(0)); |
b7d11258 DW |
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 | ||
7a4ae749 PO |
356 | if (!skb_queue_len(&audit_skb_queue)) { |
357 | try_to_freeze(); | |
b7d11258 | 358 | schedule(); |
7a4ae749 | 359 | } |
b7d11258 DW |
360 | |
361 | __set_current_state(TASK_RUNNING); | |
362 | remove_wait_queue(&kauditd_wait, &wait); | |
363 | } | |
364 | } | |
4899b8b1 | 365 | return 0; |
b7d11258 DW |
366 | } |
367 | ||
522ed776 MT |
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 | ||
9044e6bc AV |
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 */ | |
f368c07d AG |
400 | mutex_lock(&audit_cmd_mutex); |
401 | mutex_unlock(&audit_cmd_mutex); | |
9044e6bc AV |
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 | ||
74c3cbe3 AV |
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 | ||
9044e6bc AV |
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 | ||
b0dd25a8 RD |
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 | */ | |
1da177e4 LT |
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; | |
9044e6bc | 469 | skb = audit_make_reply(pid, seq, type, done, multi, payload, size); |
1da177e4 | 470 | if (!skb) |
b7d11258 | 471 | return; |
b7d11258 DW |
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); | |
1da177e4 | 475 | return; |
1da177e4 LT |
476 | } |
477 | ||
478 | /* | |
479 | * Check for appropriate CAP_AUDIT_ capabilities on incoming audit | |
480 | * control messages. | |
481 | */ | |
c7bdb545 | 482 | static int audit_netlink_ok(struct sk_buff *skb, u16 msg_type) |
1da177e4 LT |
483 | { |
484 | int err = 0; | |
485 | ||
486 | switch (msg_type) { | |
487 | case AUDIT_GET: | |
488 | case AUDIT_LIST: | |
93315ed6 | 489 | case AUDIT_LIST_RULES: |
1da177e4 LT |
490 | case AUDIT_SET: |
491 | case AUDIT_ADD: | |
93315ed6 | 492 | case AUDIT_ADD_RULE: |
1da177e4 | 493 | case AUDIT_DEL: |
93315ed6 | 494 | case AUDIT_DEL_RULE: |
c2f0c7c3 | 495 | case AUDIT_SIGNAL_INFO: |
522ed776 MT |
496 | case AUDIT_TTY_GET: |
497 | case AUDIT_TTY_SET: | |
74c3cbe3 AV |
498 | case AUDIT_TRIM: |
499 | case AUDIT_MAKE_EQUIV: | |
c7bdb545 | 500 | if (security_netlink_recv(skb, CAP_AUDIT_CONTROL)) |
1da177e4 LT |
501 | err = -EPERM; |
502 | break; | |
05474106 | 503 | case AUDIT_USER: |
039b6b3e RD |
504 | case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG: |
505 | case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2: | |
c7bdb545 | 506 | if (security_netlink_recv(skb, CAP_AUDIT_WRITE)) |
1da177e4 LT |
507 | err = -EPERM; |
508 | break; | |
509 | default: /* bad msg */ | |
510 | err = -EINVAL; | |
511 | } | |
512 | ||
513 | return err; | |
514 | } | |
515 | ||
50397bd1 EP |
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 | ||
1da177e4 LT |
543 | static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
544 | { | |
e7c34970 | 545 | u32 uid, pid, seq, sid; |
1da177e4 LT |
546 | void *data; |
547 | struct audit_status *status_get, status_set; | |
548 | int err; | |
c0404993 | 549 | struct audit_buffer *ab; |
1da177e4 | 550 | u16 msg_type = nlh->nlmsg_type; |
c94c257c | 551 | uid_t loginuid; /* loginuid of sender */ |
e1396065 | 552 | struct audit_sig_info *sig_data; |
50397bd1 | 553 | char *ctx = NULL; |
e1396065 | 554 | u32 len; |
1da177e4 | 555 | |
c7bdb545 | 556 | err = audit_netlink_ok(skb, msg_type); |
1da177e4 LT |
557 | if (err) |
558 | return err; | |
559 | ||
b0dd25a8 RD |
560 | /* As soon as there's any sign of userspace auditd, |
561 | * start kauditd to talk to it */ | |
b7d11258 DW |
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 | ||
1da177e4 LT |
570 | pid = NETLINK_CREDS(skb)->pid; |
571 | uid = NETLINK_CREDS(skb)->uid; | |
c94c257c | 572 | loginuid = NETLINK_CB(skb).loginuid; |
e7c34970 | 573 | sid = NETLINK_CB(skb).sid; |
1da177e4 LT |
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); | |
b7d11258 | 585 | status_set.backlog = skb_queue_len(&audit_skb_queue); |
1da177e4 LT |
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) { | |
ce29b682 SG |
594 | err = audit_set_enabled(status_get->enabled, |
595 | loginuid, sid); | |
1da177e4 LT |
596 | if (err < 0) return err; |
597 | } | |
598 | if (status_get->mask & AUDIT_STATUS_FAILURE) { | |
ce29b682 SG |
599 | err = audit_set_failure(status_get->failure, |
600 | loginuid, sid); | |
1da177e4 LT |
601 | if (err < 0) return err; |
602 | } | |
603 | if (status_get->mask & AUDIT_STATUS_PID) { | |
1a6b9f23 EP |
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; | |
1da177e4 LT |
612 | } |
613 | if (status_get->mask & AUDIT_STATUS_RATE_LIMIT) | |
5d136a01 | 614 | err = audit_set_rate_limit(status_get->rate_limit, |
ce29b682 | 615 | loginuid, sid); |
1da177e4 | 616 | if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT) |
5d136a01 | 617 | err = audit_set_backlog_limit(status_get->backlog_limit, |
ce29b682 | 618 | loginuid, sid); |
1da177e4 | 619 | break; |
05474106 | 620 | case AUDIT_USER: |
039b6b3e RD |
621 | case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG: |
622 | case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2: | |
4a4cd633 DW |
623 | if (!audit_enabled && msg_type != AUDIT_USER_AVC) |
624 | return 0; | |
625 | ||
5bb289b5 | 626 | err = audit_filter_user(&NETLINK_CB(skb), msg_type); |
4a4cd633 DW |
627 | if (err == 1) { |
628 | err = 0; | |
522ed776 MT |
629 | if (msg_type == AUDIT_USER_TTY) { |
630 | err = audit_prepare_user_tty(pid, loginuid); | |
631 | if (err) | |
632 | break; | |
633 | } | |
50397bd1 EP |
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); | |
4a4cd633 | 647 | } |
50397bd1 EP |
648 | audit_set_pid(ab, pid); |
649 | audit_log_end(ab); | |
0f45aa18 | 650 | } |
1da177e4 LT |
651 | break; |
652 | case AUDIT_ADD: | |
653 | case AUDIT_DEL: | |
93315ed6 | 654 | if (nlmsg_len(nlh) < sizeof(struct audit_rule)) |
1da177e4 | 655 | return -EINVAL; |
1a6b9f23 | 656 | if (audit_enabled == AUDIT_LOCKED) { |
50397bd1 EP |
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); | |
6a01b07f SG |
663 | return -EPERM; |
664 | } | |
1da177e4 LT |
665 | /* fallthrough */ |
666 | case AUDIT_LIST: | |
1da177e4 | 667 | err = audit_receive_filter(nlh->nlmsg_type, NETLINK_CB(skb).pid, |
93315ed6 | 668 | uid, seq, data, nlmsg_len(nlh), |
ce29b682 | 669 | loginuid, sid); |
93315ed6 AG |
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; | |
1a6b9f23 | 675 | if (audit_enabled == AUDIT_LOCKED) { |
50397bd1 EP |
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); | |
6a01b07f SG |
682 | return -EPERM; |
683 | } | |
93315ed6 AG |
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), | |
ce29b682 | 688 | loginuid, sid); |
1da177e4 | 689 | break; |
74c3cbe3 AV |
690 | case AUDIT_TRIM: |
691 | audit_trim_trees(); | |
50397bd1 EP |
692 | |
693 | audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE, pid, | |
694 | uid, loginuid, sid); | |
695 | ||
74c3cbe3 AV |
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 | ||
50397bd1 EP |
725 | audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE, pid, |
726 | uid, loginuid, sid); | |
727 | ||
74c3cbe3 AV |
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 | } | |
c2f0c7c3 | 738 | case AUDIT_SIGNAL_INFO: |
1a70cd40 | 739 | err = selinux_sid_to_string(audit_sig_sid, &ctx, &len); |
e1396065 AV |
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); | |
5600b892 | 751 | audit_send_reply(NETLINK_CB(skb).pid, seq, AUDIT_SIGNAL_INFO, |
e1396065 AV |
752 | 0, 0, sig_data, sizeof(*sig_data) + len); |
753 | kfree(sig_data); | |
c2f0c7c3 | 754 | break; |
522ed776 MT |
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 | } | |
1da177e4 LT |
794 | default: |
795 | err = -EINVAL; | |
796 | break; | |
797 | } | |
798 | ||
799 | return err < 0 ? err : 0; | |
800 | } | |
801 | ||
b0dd25a8 RD |
802 | /* |
803 | * Get message from skb (based on rtnetlink_rcv_skb). Each message is | |
1da177e4 | 804 | * processed by audit_receive_msg. Malformed skbs with wrong length are |
b0dd25a8 RD |
805 | * discarded silently. |
806 | */ | |
2a0a6ebe | 807 | static void audit_receive_skb(struct sk_buff *skb) |
1da177e4 LT |
808 | { |
809 | int err; | |
810 | struct nlmsghdr *nlh; | |
811 | u32 rlen; | |
812 | ||
813 | while (skb->len >= NLMSG_SPACE(0)) { | |
b529ccf2 | 814 | nlh = nlmsg_hdr(skb); |
1da177e4 | 815 | if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len) |
2a0a6ebe | 816 | return; |
1da177e4 LT |
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 | } | |
1da177e4 LT |
826 | } |
827 | ||
828 | /* Receive messages from netlink socket. */ | |
cd40b7d3 | 829 | static void audit_receive(struct sk_buff *skb) |
1da177e4 | 830 | { |
f368c07d | 831 | mutex_lock(&audit_cmd_mutex); |
cd40b7d3 | 832 | audit_receive_skb(skb); |
f368c07d | 833 | mutex_unlock(&audit_cmd_mutex); |
1da177e4 LT |
834 | } |
835 | ||
f368c07d AG |
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 | |
1da177e4 LT |
842 | |
843 | /* Initialize audit support at boot time. */ | |
844 | static int __init audit_init(void) | |
845 | { | |
f368c07d | 846 | int i; |
f368c07d | 847 | |
1da177e4 LT |
848 | printk(KERN_INFO "audit: initializing netlink socket (%s)\n", |
849 | audit_default ? "enabled" : "disabled"); | |
b4b51029 EB |
850 | audit_sock = netlink_kernel_create(&init_net, NETLINK_AUDIT, 0, |
851 | audit_receive, NULL, THIS_MODULE); | |
1da177e4 LT |
852 | if (!audit_sock) |
853 | audit_panic("cannot initialize netlink socket"); | |
71e1c784 AG |
854 | else |
855 | audit_sock->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT; | |
1da177e4 | 856 | |
b7d11258 | 857 | skb_queue_head_init(&audit_skb_queue); |
1da177e4 LT |
858 | audit_initialized = 1; |
859 | audit_enabled = audit_default; | |
3dc7e315 DG |
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 | ||
9ad9ad38 | 865 | audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, "initialized"); |
f368c07d AG |
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"); | |
6988434e | 871 | #endif |
f368c07d AG |
872 | |
873 | for (i = 0; i < AUDIT_INODE_BUCKETS; i++) | |
874 | INIT_LIST_HEAD(&audit_inode_hash[i]); | |
f368c07d | 875 | |
1da177e4 LT |
876 | return 0; |
877 | } | |
1da177e4 LT |
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; | |
9b41046c | 889 | return 1; |
1da177e4 LT |
890 | } |
891 | ||
892 | __setup("audit=", audit_enable); | |
893 | ||
16e1904e CW |
894 | static void audit_buffer_free(struct audit_buffer *ab) |
895 | { | |
896 | unsigned long flags; | |
897 | ||
8fc6115c CW |
898 | if (!ab) |
899 | return; | |
900 | ||
5ac52f33 CW |
901 | if (ab->skb) |
902 | kfree_skb(ab->skb); | |
b7d11258 | 903 | |
16e1904e | 904 | spin_lock_irqsave(&audit_freelist_lock, flags); |
5d136a01 | 905 | if (audit_freelist_count > AUDIT_MAXFREE) |
16e1904e | 906 | kfree(ab); |
5d136a01 SH |
907 | else { |
908 | audit_freelist_count++; | |
16e1904e | 909 | list_add(&ab->list, &audit_freelist); |
5d136a01 | 910 | } |
16e1904e CW |
911 | spin_unlock_irqrestore(&audit_freelist_lock, flags); |
912 | } | |
913 | ||
c0404993 | 914 | static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx, |
dd0fc66f | 915 | gfp_t gfp_mask, int type) |
16e1904e CW |
916 | { |
917 | unsigned long flags; | |
918 | struct audit_buffer *ab = NULL; | |
c0404993 | 919 | struct nlmsghdr *nlh; |
16e1904e CW |
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) { | |
4332bdd3 | 931 | ab = kmalloc(sizeof(*ab), gfp_mask); |
16e1904e | 932 | if (!ab) |
8fc6115c | 933 | goto err; |
16e1904e | 934 | } |
8fc6115c | 935 | |
4332bdd3 | 936 | ab->skb = alloc_skb(AUDIT_BUFSIZ, gfp_mask); |
5ac52f33 | 937 | if (!ab->skb) |
8fc6115c CW |
938 | goto err; |
939 | ||
b7d11258 | 940 | ab->ctx = ctx; |
9ad9ad38 | 941 | ab->gfp_mask = gfp_mask; |
c0404993 SG |
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; | |
16e1904e | 947 | return ab; |
8fc6115c CW |
948 | err: |
949 | audit_buffer_free(ab); | |
950 | return NULL; | |
16e1904e | 951 | } |
1da177e4 | 952 | |
b0dd25a8 RD |
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 | |
bfb4496e DW |
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 | * | |
bfb4496e DW |
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 | |
b0dd25a8 RD |
968 | * halts). |
969 | */ | |
bfb4496e DW |
970 | unsigned int audit_serial(void) |
971 | { | |
34af946a | 972 | static DEFINE_SPINLOCK(serial_lock); |
d5b454f2 DW |
973 | static unsigned int serial = 0; |
974 | ||
975 | unsigned long flags; | |
976 | unsigned int ret; | |
bfb4496e | 977 | |
d5b454f2 | 978 | spin_lock_irqsave(&serial_lock, flags); |
bfb4496e | 979 | do { |
ce625a80 DW |
980 | ret = ++serial; |
981 | } while (unlikely(!ret)); | |
d5b454f2 | 982 | spin_unlock_irqrestore(&serial_lock, flags); |
bfb4496e | 983 | |
d5b454f2 | 984 | return ret; |
bfb4496e DW |
985 | } |
986 | ||
5600b892 | 987 | static inline void audit_get_stamp(struct audit_context *ctx, |
bfb4496e DW |
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 | ||
1da177e4 LT |
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. */ | |
9ad9ad38 | 1004 | |
b0dd25a8 RD |
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 | */ | |
9796fdd8 | 1020 | struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, |
9ad9ad38 | 1021 | int type) |
1da177e4 LT |
1022 | { |
1023 | struct audit_buffer *ab = NULL; | |
1da177e4 | 1024 | struct timespec t; |
d812ddbb | 1025 | unsigned int serial; |
9ad9ad38 | 1026 | int reserve; |
ac4cec44 | 1027 | unsigned long timeout_start = jiffies; |
1da177e4 LT |
1028 | |
1029 | if (!audit_initialized) | |
1030 | return NULL; | |
1031 | ||
c8edc80c DK |
1032 | if (unlikely(audit_filter_type(type))) |
1033 | return NULL; | |
1034 | ||
9ad9ad38 DW |
1035 | if (gfp_mask & __GFP_WAIT) |
1036 | reserve = 0; | |
1037 | else | |
5600b892 | 1038 | reserve = 5; /* Allow atomic callers to go up to five |
9ad9ad38 DW |
1039 | entries over the normal backlog limit */ |
1040 | ||
1041 | while (audit_backlog_limit | |
1042 | && skb_queue_len(&audit_skb_queue) > audit_backlog_limit + reserve) { | |
ac4cec44 DW |
1043 | if (gfp_mask & __GFP_WAIT && audit_backlog_wait_time |
1044 | && time_before(jiffies, timeout_start + audit_backlog_wait_time)) { | |
1045 | ||
9ad9ad38 DW |
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) | |
ac4cec44 | 1053 | schedule_timeout(timeout_start + audit_backlog_wait_time - jiffies); |
9ad9ad38 DW |
1054 | |
1055 | __set_current_state(TASK_RUNNING); | |
1056 | remove_wait_queue(&audit_backlog_wait, &wait); | |
ac4cec44 | 1057 | continue; |
9ad9ad38 | 1058 | } |
fb19b4c6 DW |
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"); | |
ac4cec44 DW |
1066 | audit_backlog_wait_time = audit_backlog_wait_overflow; |
1067 | wake_up(&audit_backlog_wait); | |
fb19b4c6 DW |
1068 | return NULL; |
1069 | } | |
1070 | ||
9ad9ad38 | 1071 | ab = audit_buffer_alloc(ctx, gfp_mask, type); |
1da177e4 LT |
1072 | if (!ab) { |
1073 | audit_log_lost("out of memory in audit_log_start"); | |
1074 | return NULL; | |
1075 | } | |
1076 | ||
bfb4496e | 1077 | audit_get_stamp(ab->ctx, &t, &serial); |
197c69c6 | 1078 | |
1da177e4 LT |
1079 | audit_log_format(ab, "audit(%lu.%03lu:%u): ", |
1080 | t.tv_sec, t.tv_nsec/1000000, serial); | |
1081 | return ab; | |
1082 | } | |
1083 | ||
8fc6115c | 1084 | /** |
5ac52f33 | 1085 | * audit_expand - expand skb in the audit buffer |
8fc6115c | 1086 | * @ab: audit_buffer |
b0dd25a8 | 1087 | * @extra: space to add at tail of the skb |
8fc6115c CW |
1088 | * |
1089 | * Returns 0 (no space) on failed expansion, or available space if | |
1090 | * successful. | |
1091 | */ | |
e3b926b4 | 1092 | static inline int audit_expand(struct audit_buffer *ab, int extra) |
8fc6115c | 1093 | { |
5ac52f33 | 1094 | struct sk_buff *skb = ab->skb; |
406a1d86 HX |
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 | ||
5ac52f33 CW |
1099 | if (ret < 0) { |
1100 | audit_log_lost("out of memory in audit_expand"); | |
8fc6115c | 1101 | return 0; |
5ac52f33 | 1102 | } |
406a1d86 HX |
1103 | |
1104 | skb->truesize += newtail - oldtail; | |
1105 | return newtail; | |
8fc6115c | 1106 | } |
1da177e4 | 1107 | |
b0dd25a8 RD |
1108 | /* |
1109 | * Format an audit message into the audit buffer. If there isn't enough | |
1da177e4 LT |
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 | |
b0dd25a8 RD |
1112 | * can't format message larger than 1024 bytes, so we don't either. |
1113 | */ | |
1da177e4 LT |
1114 | static void audit_log_vformat(struct audit_buffer *ab, const char *fmt, |
1115 | va_list args) | |
1116 | { | |
1117 | int len, avail; | |
5ac52f33 | 1118 | struct sk_buff *skb; |
eecb0a73 | 1119 | va_list args2; |
1da177e4 LT |
1120 | |
1121 | if (!ab) | |
1122 | return; | |
1123 | ||
5ac52f33 CW |
1124 | BUG_ON(!ab->skb); |
1125 | skb = ab->skb; | |
1126 | avail = skb_tailroom(skb); | |
1127 | if (avail == 0) { | |
e3b926b4 | 1128 | avail = audit_expand(ab, AUDIT_BUFSIZ); |
8fc6115c CW |
1129 | if (!avail) |
1130 | goto out; | |
1da177e4 | 1131 | } |
eecb0a73 | 1132 | va_copy(args2, args); |
27a884dc | 1133 | len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args); |
1da177e4 LT |
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. */ | |
b0dd25a8 RD |
1138 | avail = audit_expand(ab, |
1139 | max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail)); | |
8fc6115c CW |
1140 | if (!avail) |
1141 | goto out; | |
27a884dc | 1142 | len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args2); |
1da177e4 | 1143 | } |
168b7173 SG |
1144 | if (len > 0) |
1145 | skb_put(skb, len); | |
8fc6115c CW |
1146 | out: |
1147 | return; | |
1da177e4 LT |
1148 | } |
1149 | ||
b0dd25a8 RD |
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 | */ | |
1da177e4 LT |
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 | ||
b0dd25a8 RD |
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, | |
168b7173 | 1181 | size_t len) |
83c7d091 | 1182 | { |
168b7173 SG |
1183 | int i, avail, new_len; |
1184 | unsigned char *ptr; | |
1185 | struct sk_buff *skb; | |
1186 | static const unsigned char *hex = "0123456789ABCDEF"; | |
1187 | ||
8ef2d304 AG |
1188 | if (!ab) |
1189 | return; | |
1190 | ||
168b7173 SG |
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 | } | |
83c7d091 | 1202 | |
27a884dc | 1203 | ptr = skb_tail_pointer(skb); |
168b7173 SG |
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 */ | |
83c7d091 | 1210 | } |
1211 | ||
9c937dcc AG |
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 | ||
8ef2d304 AG |
1223 | if (!ab) |
1224 | return; | |
1225 | ||
9c937dcc AG |
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 | } | |
27a884dc | 1235 | ptr = skb_tail_pointer(skb); |
9c937dcc AG |
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 | ||
de6bbd1d EP |
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 | ||
b0dd25a8 | 1259 | /** |
522ed776 | 1260 | * audit_log_n_untrustedstring - log a string that may contain random characters |
b0dd25a8 | 1261 | * @ab: audit_buffer |
9c937dcc | 1262 | * @len: lenth of string (not including trailing null) |
b0dd25a8 RD |
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, | |
168b7173 | 1267 | * or a space. Unescaped strings will start and end with a double quote mark. |
b0dd25a8 | 1268 | * Strings that are escaped are printed in hex (2 digits per char). |
9c937dcc AG |
1269 | * |
1270 | * The caller specifies the number of characters in the string to log, which may | |
1271 | * or may not be the entire string. | |
b0dd25a8 | 1272 | */ |
de6bbd1d EP |
1273 | void audit_log_n_untrustedstring(struct audit_buffer *ab, size_t len, |
1274 | const char *string) | |
83c7d091 | 1275 | { |
de6bbd1d EP |
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); | |
83c7d091 | 1280 | } |
1281 | ||
9c937dcc | 1282 | /** |
522ed776 | 1283 | * audit_log_untrustedstring - log a string that may contain random characters |
9c937dcc AG |
1284 | * @ab: audit_buffer |
1285 | * @string: string to be logged | |
1286 | * | |
522ed776 | 1287 | * Same as audit_log_n_untrustedstring(), except that strlen is used to |
9c937dcc AG |
1288 | * determine string length. |
1289 | */ | |
de6bbd1d | 1290 | void audit_log_untrustedstring(struct audit_buffer *ab, const char *string) |
9c937dcc | 1291 | { |
de6bbd1d | 1292 | audit_log_n_untrustedstring(ab, strlen(string), string); |
9c937dcc AG |
1293 | } |
1294 | ||
168b7173 | 1295 | /* This is a helper-function to print the escaped d_path */ |
1da177e4 LT |
1296 | void audit_log_d_path(struct audit_buffer *ab, const char *prefix, |
1297 | struct dentry *dentry, struct vfsmount *vfsmnt) | |
1298 | { | |
168b7173 | 1299 | char *p, *path; |
1da177e4 | 1300 | |
8fc6115c CW |
1301 | if (prefix) |
1302 | audit_log_format(ab, " %s", prefix); | |
1da177e4 | 1303 | |
168b7173 | 1304 | /* We will allow 11 spaces for ' (deleted)' to be appended */ |
9ad9ad38 | 1305 | path = kmalloc(PATH_MAX+11, ab->gfp_mask); |
168b7173 SG |
1306 | if (!path) { |
1307 | audit_log_format(ab, "<no memory>"); | |
1308 | return; | |
1da177e4 | 1309 | } |
168b7173 SG |
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>"); | |
5600b892 | 1314 | } else |
168b7173 SG |
1315 | audit_log_untrustedstring(ab, p); |
1316 | kfree(path); | |
1da177e4 LT |
1317 | } |
1318 | ||
b0dd25a8 RD |
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 | |
1da177e4 | 1325 | * remove them from the queue outside the irq context. May be called in |
b0dd25a8 RD |
1326 | * any context. |
1327 | */ | |
b7d11258 | 1328 | void audit_log_end(struct audit_buffer *ab) |
1da177e4 | 1329 | { |
1da177e4 LT |
1330 | if (!ab) |
1331 | return; | |
1332 | if (!audit_rate_check()) { | |
1333 | audit_log_lost("rate limit exceeded"); | |
1334 | } else { | |
b7d11258 | 1335 | if (audit_pid) { |
b529ccf2 | 1336 | struct nlmsghdr *nlh = nlmsg_hdr(ab->skb); |
b7d11258 DW |
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 { | |
e445deb5 EP |
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)); | |
b7d11258 | 1344 | } |
1da177e4 | 1345 | } |
16e1904e | 1346 | audit_buffer_free(ab); |
1da177e4 LT |
1347 | } |
1348 | ||
b0dd25a8 RD |
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 | */ | |
5600b892 | 1361 | void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type, |
9ad9ad38 | 1362 | const char *fmt, ...) |
1da177e4 LT |
1363 | { |
1364 | struct audit_buffer *ab; | |
1365 | va_list args; | |
1366 | ||
9ad9ad38 | 1367 | ab = audit_log_start(ctx, gfp_mask, type); |
1da177e4 LT |
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 | } | |
bf45da97 | 1375 | |
1376 | EXPORT_SYMBOL(audit_log_start); | |
1377 | EXPORT_SYMBOL(audit_log_end); | |
1378 | EXPORT_SYMBOL(audit_log_format); | |
1379 | EXPORT_SYMBOL(audit_log); |