]>
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 | * | |
d7a96f3a | 24 | * Goals: 1) Integrate fully with Security Modules. |
1da177e4 LT |
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> |
60063497 | 46 | #include <linux/atomic.h> |
1da177e4 | 47 | #include <linux/mm.h> |
9984de1a | 48 | #include <linux/export.h> |
5a0e3ad6 | 49 | #include <linux/slab.h> |
b7d11258 DW |
50 | #include <linux/err.h> |
51 | #include <linux/kthread.h> | |
1da177e4 LT |
52 | |
53 | #include <linux/audit.h> | |
54 | ||
55 | #include <net/sock.h> | |
93315ed6 | 56 | #include <net/netlink.h> |
1da177e4 | 57 | #include <linux/skbuff.h> |
131ad62d MDF |
58 | #ifdef CONFIG_SECURITY |
59 | #include <linux/security.h> | |
60 | #endif | |
1da177e4 | 61 | #include <linux/netlink.h> |
7dfb7103 | 62 | #include <linux/freezer.h> |
522ed776 | 63 | #include <linux/tty.h> |
34e36d8e | 64 | #include <linux/pid_namespace.h> |
3dc7e315 DG |
65 | |
66 | #include "audit.h" | |
1da177e4 | 67 | |
a3f07114 | 68 | /* No auditing will take place until audit_initialized == AUDIT_INITIALIZED. |
1da177e4 | 69 | * (Initialization happens after skb_init is called.) */ |
a3f07114 EP |
70 | #define AUDIT_DISABLED -1 |
71 | #define AUDIT_UNINITIALIZED 0 | |
72 | #define AUDIT_INITIALIZED 1 | |
1da177e4 LT |
73 | static int audit_initialized; |
74 | ||
1a6b9f23 EP |
75 | #define AUDIT_OFF 0 |
76 | #define AUDIT_ON 1 | |
77 | #define AUDIT_LOCKED 2 | |
1da177e4 | 78 | int audit_enabled; |
b593d384 | 79 | int audit_ever_enabled; |
1da177e4 | 80 | |
ae9d67af JE |
81 | EXPORT_SYMBOL_GPL(audit_enabled); |
82 | ||
1da177e4 LT |
83 | /* Default state when kernel boots without any parameters. */ |
84 | static int audit_default; | |
85 | ||
86 | /* If auditing cannot proceed, audit_failure selects what happens. */ | |
87 | static int audit_failure = AUDIT_FAIL_PRINTK; | |
88 | ||
75c0371a PE |
89 | /* |
90 | * If audit records are to be written to the netlink socket, audit_pid | |
15e47304 EB |
91 | * contains the pid of the auditd process and audit_nlk_portid contains |
92 | * the portid to use to send netlink messages to that process. | |
75c0371a | 93 | */ |
c2f0c7c3 | 94 | int audit_pid; |
15e47304 | 95 | static int audit_nlk_portid; |
1da177e4 | 96 | |
b0dd25a8 | 97 | /* If audit_rate_limit is non-zero, limit the rate of sending audit records |
1da177e4 LT |
98 | * to that number per second. This prevents DoS attacks, but results in |
99 | * audit records being dropped. */ | |
100 | static int audit_rate_limit; | |
101 | ||
102 | /* Number of outstanding audit_buffers allowed. */ | |
103 | static int audit_backlog_limit = 64; | |
ac4cec44 DW |
104 | static int audit_backlog_wait_time = 60 * HZ; |
105 | static int audit_backlog_wait_overflow = 0; | |
1da177e4 | 106 | |
c2f0c7c3 | 107 | /* The identity of the user shutting down the audit system. */ |
cca080d9 | 108 | kuid_t audit_sig_uid = INVALID_UID; |
c2f0c7c3 | 109 | pid_t audit_sig_pid = -1; |
e1396065 | 110 | u32 audit_sig_sid = 0; |
c2f0c7c3 | 111 | |
1da177e4 LT |
112 | /* Records can be lost in several ways: |
113 | 0) [suppressed in audit_alloc] | |
114 | 1) out of memory in audit_log_start [kmalloc of struct audit_buffer] | |
115 | 2) out of memory in audit_log_move [alloc_skb] | |
116 | 3) suppressed due to audit_rate_limit | |
117 | 4) suppressed due to audit_backlog_limit | |
118 | */ | |
119 | static atomic_t audit_lost = ATOMIC_INIT(0); | |
120 | ||
121 | /* The netlink socket. */ | |
122 | static struct sock *audit_sock; | |
123 | ||
f368c07d AG |
124 | /* Hash for inode-based rules */ |
125 | struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS]; | |
126 | ||
b7d11258 | 127 | /* The audit_freelist is a list of pre-allocated audit buffers (if more |
1da177e4 LT |
128 | * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of |
129 | * being placed on the freelist). */ | |
1da177e4 | 130 | static DEFINE_SPINLOCK(audit_freelist_lock); |
b0dd25a8 | 131 | static int audit_freelist_count; |
1da177e4 LT |
132 | static LIST_HEAD(audit_freelist); |
133 | ||
b7d11258 | 134 | static struct sk_buff_head audit_skb_queue; |
f3d357b0 EP |
135 | /* queue of skbs to send to auditd when/if it comes back */ |
136 | static struct sk_buff_head audit_skb_hold_queue; | |
b7d11258 DW |
137 | static struct task_struct *kauditd_task; |
138 | static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait); | |
9ad9ad38 | 139 | static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait); |
1da177e4 | 140 | |
f368c07d | 141 | /* Serialize requests from userspace. */ |
916d7576 | 142 | DEFINE_MUTEX(audit_cmd_mutex); |
1da177e4 LT |
143 | |
144 | /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting | |
145 | * audit records. Since printk uses a 1024 byte buffer, this buffer | |
146 | * should be at least that large. */ | |
147 | #define AUDIT_BUFSIZ 1024 | |
148 | ||
149 | /* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the | |
150 | * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */ | |
151 | #define AUDIT_MAXFREE (2*NR_CPUS) | |
152 | ||
153 | /* The audit_buffer is used when formatting an audit record. The caller | |
154 | * locks briefly to get the record off the freelist or to allocate the | |
155 | * buffer, and locks briefly to send the buffer to the netlink layer or | |
156 | * to place it on a transmit queue. Multiple audit_buffers can be in | |
157 | * use simultaneously. */ | |
158 | struct audit_buffer { | |
159 | struct list_head list; | |
8fc6115c | 160 | struct sk_buff *skb; /* formatted skb ready to send */ |
1da177e4 | 161 | struct audit_context *ctx; /* NULL or associated context */ |
9796fdd8 | 162 | gfp_t gfp_mask; |
1da177e4 LT |
163 | }; |
164 | ||
f09ac9db EP |
165 | struct audit_reply { |
166 | int pid; | |
167 | struct sk_buff *skb; | |
168 | }; | |
169 | ||
c0404993 SG |
170 | static void audit_set_pid(struct audit_buffer *ab, pid_t pid) |
171 | { | |
50397bd1 EP |
172 | if (ab) { |
173 | struct nlmsghdr *nlh = nlmsg_hdr(ab->skb); | |
174 | nlh->nlmsg_pid = pid; | |
175 | } | |
c0404993 SG |
176 | } |
177 | ||
8c8570fb | 178 | void audit_panic(const char *message) |
1da177e4 LT |
179 | { |
180 | switch (audit_failure) | |
181 | { | |
182 | case AUDIT_FAIL_SILENT: | |
183 | break; | |
184 | case AUDIT_FAIL_PRINTK: | |
320f1b1e EP |
185 | if (printk_ratelimit()) |
186 | printk(KERN_ERR "audit: %s\n", message); | |
1da177e4 LT |
187 | break; |
188 | case AUDIT_FAIL_PANIC: | |
b29ee87e EP |
189 | /* test audit_pid since printk is always losey, why bother? */ |
190 | if (audit_pid) | |
191 | panic("audit: %s\n", message); | |
1da177e4 LT |
192 | break; |
193 | } | |
194 | } | |
195 | ||
196 | static inline int audit_rate_check(void) | |
197 | { | |
198 | static unsigned long last_check = 0; | |
199 | static int messages = 0; | |
200 | static DEFINE_SPINLOCK(lock); | |
201 | unsigned long flags; | |
202 | unsigned long now; | |
203 | unsigned long elapsed; | |
204 | int retval = 0; | |
205 | ||
206 | if (!audit_rate_limit) return 1; | |
207 | ||
208 | spin_lock_irqsave(&lock, flags); | |
209 | if (++messages < audit_rate_limit) { | |
210 | retval = 1; | |
211 | } else { | |
212 | now = jiffies; | |
213 | elapsed = now - last_check; | |
214 | if (elapsed > HZ) { | |
215 | last_check = now; | |
216 | messages = 0; | |
217 | retval = 1; | |
218 | } | |
219 | } | |
220 | spin_unlock_irqrestore(&lock, flags); | |
221 | ||
222 | return retval; | |
223 | } | |
224 | ||
b0dd25a8 RD |
225 | /** |
226 | * audit_log_lost - conditionally log lost audit message event | |
227 | * @message: the message stating reason for lost audit message | |
228 | * | |
229 | * Emit at least 1 message per second, even if audit_rate_check is | |
230 | * throttling. | |
231 | * Always increment the lost messages counter. | |
232 | */ | |
1da177e4 LT |
233 | void audit_log_lost(const char *message) |
234 | { | |
235 | static unsigned long last_msg = 0; | |
236 | static DEFINE_SPINLOCK(lock); | |
237 | unsigned long flags; | |
238 | unsigned long now; | |
239 | int print; | |
240 | ||
241 | atomic_inc(&audit_lost); | |
242 | ||
243 | print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit); | |
244 | ||
245 | if (!print) { | |
246 | spin_lock_irqsave(&lock, flags); | |
247 | now = jiffies; | |
248 | if (now - last_msg > HZ) { | |
249 | print = 1; | |
250 | last_msg = now; | |
251 | } | |
252 | spin_unlock_irqrestore(&lock, flags); | |
253 | } | |
254 | ||
255 | if (print) { | |
320f1b1e EP |
256 | if (printk_ratelimit()) |
257 | printk(KERN_WARNING | |
258 | "audit: audit_lost=%d audit_rate_limit=%d " | |
259 | "audit_backlog_limit=%d\n", | |
260 | atomic_read(&audit_lost), | |
261 | audit_rate_limit, | |
262 | audit_backlog_limit); | |
1da177e4 LT |
263 | audit_panic(message); |
264 | } | |
1da177e4 LT |
265 | } |
266 | ||
1a6b9f23 | 267 | static int audit_log_config_change(char *function_name, int new, int old, |
e1760bd5 | 268 | kuid_t loginuid, u32 sessionid, u32 sid, |
2532386f | 269 | int allow_changes) |
1da177e4 | 270 | { |
1a6b9f23 EP |
271 | struct audit_buffer *ab; |
272 | int rc = 0; | |
ce29b682 | 273 | |
1a6b9f23 | 274 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE); |
0644ec0c KC |
275 | if (unlikely(!ab)) |
276 | return rc; | |
2532386f | 277 | audit_log_format(ab, "%s=%d old=%d auid=%u ses=%u", function_name, new, |
e1760bd5 | 278 | old, from_kuid(&init_user_ns, loginuid), sessionid); |
ce29b682 SG |
279 | if (sid) { |
280 | char *ctx = NULL; | |
281 | u32 len; | |
1a6b9f23 | 282 | |
2a862b32 | 283 | rc = security_secid_to_secctx(sid, &ctx, &len); |
1a6b9f23 EP |
284 | if (rc) { |
285 | audit_log_format(ab, " sid=%u", sid); | |
286 | allow_changes = 0; /* Something weird, deny request */ | |
287 | } else { | |
288 | audit_log_format(ab, " subj=%s", ctx); | |
2a862b32 | 289 | security_release_secctx(ctx, len); |
1a6b9f23 | 290 | } |
6a01b07f | 291 | } |
1a6b9f23 EP |
292 | audit_log_format(ab, " res=%d", allow_changes); |
293 | audit_log_end(ab); | |
6a01b07f | 294 | return rc; |
1da177e4 LT |
295 | } |
296 | ||
1a6b9f23 | 297 | static int audit_do_config_change(char *function_name, int *to_change, |
e1760bd5 | 298 | int new, kuid_t loginuid, u32 sessionid, |
2532386f | 299 | u32 sid) |
1da177e4 | 300 | { |
1a6b9f23 | 301 | int allow_changes, rc = 0, old = *to_change; |
6a01b07f SG |
302 | |
303 | /* check if we are locked */ | |
1a6b9f23 EP |
304 | if (audit_enabled == AUDIT_LOCKED) |
305 | allow_changes = 0; | |
6a01b07f | 306 | else |
1a6b9f23 | 307 | allow_changes = 1; |
ce29b682 | 308 | |
1a6b9f23 | 309 | if (audit_enabled != AUDIT_OFF) { |
2532386f EP |
310 | rc = audit_log_config_change(function_name, new, old, loginuid, |
311 | sessionid, sid, allow_changes); | |
1a6b9f23 EP |
312 | if (rc) |
313 | allow_changes = 0; | |
6a01b07f | 314 | } |
6a01b07f SG |
315 | |
316 | /* If we are allowed, make the change */ | |
1a6b9f23 EP |
317 | if (allow_changes == 1) |
318 | *to_change = new; | |
6a01b07f SG |
319 | /* Not allowed, update reason */ |
320 | else if (rc == 0) | |
321 | rc = -EPERM; | |
322 | return rc; | |
1da177e4 LT |
323 | } |
324 | ||
e1760bd5 | 325 | static int audit_set_rate_limit(int limit, kuid_t loginuid, u32 sessionid, |
2532386f | 326 | u32 sid) |
1da177e4 | 327 | { |
1a6b9f23 | 328 | return audit_do_config_change("audit_rate_limit", &audit_rate_limit, |
2532386f | 329 | limit, loginuid, sessionid, sid); |
1a6b9f23 | 330 | } |
ce29b682 | 331 | |
e1760bd5 | 332 | static int audit_set_backlog_limit(int limit, kuid_t loginuid, u32 sessionid, |
2532386f | 333 | u32 sid) |
1a6b9f23 EP |
334 | { |
335 | return audit_do_config_change("audit_backlog_limit", &audit_backlog_limit, | |
2532386f | 336 | limit, loginuid, sessionid, sid); |
1a6b9f23 | 337 | } |
6a01b07f | 338 | |
e1760bd5 | 339 | static int audit_set_enabled(int state, kuid_t loginuid, u32 sessionid, u32 sid) |
1a6b9f23 | 340 | { |
b593d384 | 341 | int rc; |
1a6b9f23 EP |
342 | if (state < AUDIT_OFF || state > AUDIT_LOCKED) |
343 | return -EINVAL; | |
6a01b07f | 344 | |
b593d384 | 345 | rc = audit_do_config_change("audit_enabled", &audit_enabled, state, |
2532386f | 346 | loginuid, sessionid, sid); |
b593d384 EP |
347 | |
348 | if (!rc) | |
349 | audit_ever_enabled |= !!state; | |
350 | ||
351 | return rc; | |
1da177e4 LT |
352 | } |
353 | ||
e1760bd5 | 354 | static int audit_set_failure(int state, kuid_t loginuid, u32 sessionid, u32 sid) |
1da177e4 | 355 | { |
1da177e4 LT |
356 | if (state != AUDIT_FAIL_SILENT |
357 | && state != AUDIT_FAIL_PRINTK | |
358 | && state != AUDIT_FAIL_PANIC) | |
359 | return -EINVAL; | |
ce29b682 | 360 | |
1a6b9f23 | 361 | return audit_do_config_change("audit_failure", &audit_failure, state, |
2532386f | 362 | loginuid, sessionid, sid); |
1da177e4 LT |
363 | } |
364 | ||
f3d357b0 EP |
365 | /* |
366 | * Queue skbs to be sent to auditd when/if it comes back. These skbs should | |
367 | * already have been sent via prink/syslog and so if these messages are dropped | |
368 | * it is not a huge concern since we already passed the audit_log_lost() | |
369 | * notification and stuff. This is just nice to get audit messages during | |
370 | * boot before auditd is running or messages generated while auditd is stopped. | |
371 | * This only holds messages is audit_default is set, aka booting with audit=1 | |
372 | * or building your kernel that way. | |
373 | */ | |
374 | static void audit_hold_skb(struct sk_buff *skb) | |
375 | { | |
376 | if (audit_default && | |
377 | skb_queue_len(&audit_skb_hold_queue) < audit_backlog_limit) | |
378 | skb_queue_tail(&audit_skb_hold_queue, skb); | |
379 | else | |
380 | kfree_skb(skb); | |
381 | } | |
382 | ||
038cbcf6 EP |
383 | /* |
384 | * For one reason or another this nlh isn't getting delivered to the userspace | |
385 | * audit daemon, just send it to printk. | |
386 | */ | |
387 | static void audit_printk_skb(struct sk_buff *skb) | |
388 | { | |
389 | struct nlmsghdr *nlh = nlmsg_hdr(skb); | |
c64e66c6 | 390 | char *data = nlmsg_data(nlh); |
038cbcf6 EP |
391 | |
392 | if (nlh->nlmsg_type != AUDIT_EOE) { | |
393 | if (printk_ratelimit()) | |
394 | printk(KERN_NOTICE "type=%d %s\n", nlh->nlmsg_type, data); | |
395 | else | |
396 | audit_log_lost("printk limit exceeded\n"); | |
397 | } | |
398 | ||
399 | audit_hold_skb(skb); | |
400 | } | |
401 | ||
f3d357b0 EP |
402 | static void kauditd_send_skb(struct sk_buff *skb) |
403 | { | |
404 | int err; | |
405 | /* take a reference in case we can't send it and we want to hold it */ | |
406 | skb_get(skb); | |
15e47304 | 407 | err = netlink_unicast(audit_sock, skb, audit_nlk_portid, 0); |
f3d357b0 | 408 | if (err < 0) { |
c9404c9c | 409 | BUG_ON(err != -ECONNREFUSED); /* Shouldn't happen */ |
f3d357b0 | 410 | printk(KERN_ERR "audit: *NO* daemon at audit_pid=%d\n", audit_pid); |
9db3b9bc | 411 | audit_log_lost("auditd disappeared\n"); |
f3d357b0 EP |
412 | audit_pid = 0; |
413 | /* we might get lucky and get this in the next auditd */ | |
414 | audit_hold_skb(skb); | |
415 | } else | |
416 | /* drop the extra reference if sent ok */ | |
70d4bf6d | 417 | consume_skb(skb); |
f3d357b0 EP |
418 | } |
419 | ||
b551d1d9 RGB |
420 | /* |
421 | * flush_hold_queue - empty the hold queue if auditd appears | |
422 | * | |
423 | * If auditd just started, drain the queue of messages already | |
424 | * sent to syslog/printk. Remember loss here is ok. We already | |
425 | * called audit_log_lost() if it didn't go out normally. so the | |
426 | * race between the skb_dequeue and the next check for audit_pid | |
427 | * doesn't matter. | |
428 | * | |
429 | * If you ever find kauditd to be too slow we can get a perf win | |
430 | * by doing our own locking and keeping better track if there | |
431 | * are messages in this queue. I don't see the need now, but | |
432 | * in 5 years when I want to play with this again I'll see this | |
433 | * note and still have no friggin idea what i'm thinking today. | |
434 | */ | |
435 | static void flush_hold_queue(void) | |
436 | { | |
437 | struct sk_buff *skb; | |
438 | ||
439 | if (!audit_default || !audit_pid) | |
440 | return; | |
441 | ||
442 | skb = skb_dequeue(&audit_skb_hold_queue); | |
443 | if (likely(!skb)) | |
444 | return; | |
445 | ||
446 | while (skb && audit_pid) { | |
447 | kauditd_send_skb(skb); | |
448 | skb = skb_dequeue(&audit_skb_hold_queue); | |
449 | } | |
450 | ||
451 | /* | |
452 | * if auditd just disappeared but we | |
453 | * dequeued an skb we need to drop ref | |
454 | */ | |
455 | if (skb) | |
456 | consume_skb(skb); | |
457 | } | |
458 | ||
97a41e26 | 459 | static int kauditd_thread(void *dummy) |
b7d11258 | 460 | { |
83144186 | 461 | set_freezable(); |
4899b8b1 | 462 | while (!kthread_should_stop()) { |
3320c513 RGB |
463 | struct sk_buff *skb; |
464 | DECLARE_WAITQUEUE(wait, current); | |
465 | ||
b551d1d9 | 466 | flush_hold_queue(); |
f3d357b0 | 467 | |
b7d11258 | 468 | skb = skb_dequeue(&audit_skb_queue); |
9ad9ad38 | 469 | wake_up(&audit_backlog_wait); |
b7d11258 | 470 | if (skb) { |
f3d357b0 EP |
471 | if (audit_pid) |
472 | kauditd_send_skb(skb); | |
038cbcf6 EP |
473 | else |
474 | audit_printk_skb(skb); | |
3320c513 RGB |
475 | continue; |
476 | } | |
477 | set_current_state(TASK_INTERRUPTIBLE); | |
478 | add_wait_queue(&kauditd_wait, &wait); | |
b7d11258 | 479 | |
3320c513 RGB |
480 | if (!skb_queue_len(&audit_skb_queue)) { |
481 | try_to_freeze(); | |
482 | schedule(); | |
b7d11258 | 483 | } |
3320c513 RGB |
484 | |
485 | __set_current_state(TASK_RUNNING); | |
486 | remove_wait_queue(&kauditd_wait, &wait); | |
b7d11258 | 487 | } |
4899b8b1 | 488 | return 0; |
b7d11258 DW |
489 | } |
490 | ||
9044e6bc AV |
491 | int audit_send_list(void *_dest) |
492 | { | |
493 | struct audit_netlink_list *dest = _dest; | |
494 | int pid = dest->pid; | |
495 | struct sk_buff *skb; | |
496 | ||
497 | /* wait for parent to finish and send an ACK */ | |
f368c07d AG |
498 | mutex_lock(&audit_cmd_mutex); |
499 | mutex_unlock(&audit_cmd_mutex); | |
9044e6bc AV |
500 | |
501 | while ((skb = __skb_dequeue(&dest->q)) != NULL) | |
502 | netlink_unicast(audit_sock, skb, pid, 0); | |
503 | ||
504 | kfree(dest); | |
505 | ||
506 | return 0; | |
507 | } | |
508 | ||
509 | struct sk_buff *audit_make_reply(int pid, int seq, int type, int done, | |
b8800aa5 | 510 | int multi, const void *payload, int size) |
9044e6bc AV |
511 | { |
512 | struct sk_buff *skb; | |
513 | struct nlmsghdr *nlh; | |
9044e6bc AV |
514 | void *data; |
515 | int flags = multi ? NLM_F_MULTI : 0; | |
516 | int t = done ? NLMSG_DONE : type; | |
517 | ||
ee080e6c | 518 | skb = nlmsg_new(size, GFP_KERNEL); |
9044e6bc AV |
519 | if (!skb) |
520 | return NULL; | |
521 | ||
c64e66c6 DM |
522 | nlh = nlmsg_put(skb, pid, seq, t, size, flags); |
523 | if (!nlh) | |
524 | goto out_kfree_skb; | |
525 | data = nlmsg_data(nlh); | |
9044e6bc AV |
526 | memcpy(data, payload, size); |
527 | return skb; | |
528 | ||
c64e66c6 DM |
529 | out_kfree_skb: |
530 | kfree_skb(skb); | |
9044e6bc AV |
531 | return NULL; |
532 | } | |
533 | ||
f09ac9db EP |
534 | static int audit_send_reply_thread(void *arg) |
535 | { | |
536 | struct audit_reply *reply = (struct audit_reply *)arg; | |
537 | ||
538 | mutex_lock(&audit_cmd_mutex); | |
539 | mutex_unlock(&audit_cmd_mutex); | |
540 | ||
541 | /* Ignore failure. It'll only happen if the sender goes away, | |
542 | because our timeout is set to infinite. */ | |
543 | netlink_unicast(audit_sock, reply->skb, reply->pid, 0); | |
544 | kfree(reply); | |
545 | return 0; | |
546 | } | |
b0dd25a8 RD |
547 | /** |
548 | * audit_send_reply - send an audit reply message via netlink | |
549 | * @pid: process id to send reply to | |
550 | * @seq: sequence number | |
551 | * @type: audit message type | |
552 | * @done: done (last) flag | |
553 | * @multi: multi-part message flag | |
554 | * @payload: payload data | |
555 | * @size: payload size | |
556 | * | |
557 | * Allocates an skb, builds the netlink message, and sends it to the pid. | |
558 | * No failure notifications. | |
559 | */ | |
b8800aa5 SH |
560 | static void audit_send_reply(int pid, int seq, int type, int done, int multi, |
561 | const void *payload, int size) | |
1da177e4 | 562 | { |
f09ac9db EP |
563 | struct sk_buff *skb; |
564 | struct task_struct *tsk; | |
565 | struct audit_reply *reply = kmalloc(sizeof(struct audit_reply), | |
566 | GFP_KERNEL); | |
567 | ||
568 | if (!reply) | |
569 | return; | |
570 | ||
9044e6bc | 571 | skb = audit_make_reply(pid, seq, type, done, multi, payload, size); |
1da177e4 | 572 | if (!skb) |
fcaf1eb8 | 573 | goto out; |
f09ac9db EP |
574 | |
575 | reply->pid = pid; | |
576 | reply->skb = skb; | |
577 | ||
578 | tsk = kthread_run(audit_send_reply_thread, reply, "audit_send_reply"); | |
fcaf1eb8 AM |
579 | if (!IS_ERR(tsk)) |
580 | return; | |
581 | kfree_skb(skb); | |
582 | out: | |
583 | kfree(reply); | |
1da177e4 LT |
584 | } |
585 | ||
586 | /* | |
587 | * Check for appropriate CAP_AUDIT_ capabilities on incoming audit | |
588 | * control messages. | |
589 | */ | |
c7bdb545 | 590 | static int audit_netlink_ok(struct sk_buff *skb, u16 msg_type) |
1da177e4 LT |
591 | { |
592 | int err = 0; | |
593 | ||
34e36d8e EB |
594 | /* Only support the initial namespaces for now. */ |
595 | if ((current_user_ns() != &init_user_ns) || | |
596 | (task_active_pid_ns(current) != &init_pid_ns)) | |
597 | return -EPERM; | |
598 | ||
1da177e4 | 599 | switch (msg_type) { |
1da177e4 | 600 | case AUDIT_LIST: |
1da177e4 LT |
601 | case AUDIT_ADD: |
602 | case AUDIT_DEL: | |
18900909 EP |
603 | return -EOPNOTSUPP; |
604 | case AUDIT_GET: | |
605 | case AUDIT_SET: | |
606 | case AUDIT_LIST_RULES: | |
607 | case AUDIT_ADD_RULE: | |
93315ed6 | 608 | case AUDIT_DEL_RULE: |
c2f0c7c3 | 609 | case AUDIT_SIGNAL_INFO: |
522ed776 MT |
610 | case AUDIT_TTY_GET: |
611 | case AUDIT_TTY_SET: | |
74c3cbe3 AV |
612 | case AUDIT_TRIM: |
613 | case AUDIT_MAKE_EQUIV: | |
fd778461 | 614 | if (!capable(CAP_AUDIT_CONTROL)) |
1da177e4 LT |
615 | err = -EPERM; |
616 | break; | |
05474106 | 617 | case AUDIT_USER: |
039b6b3e RD |
618 | case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG: |
619 | case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2: | |
fd778461 | 620 | if (!capable(CAP_AUDIT_WRITE)) |
1da177e4 LT |
621 | err = -EPERM; |
622 | break; | |
623 | default: /* bad msg */ | |
624 | err = -EINVAL; | |
625 | } | |
626 | ||
627 | return err; | |
628 | } | |
629 | ||
50397bd1 | 630 | static int audit_log_common_recv_msg(struct audit_buffer **ab, u16 msg_type, |
e1760bd5 | 631 | kuid_t auid, u32 ses, u32 sid) |
50397bd1 EP |
632 | { |
633 | int rc = 0; | |
634 | char *ctx = NULL; | |
635 | u32 len; | |
636 | ||
637 | if (!audit_enabled) { | |
638 | *ab = NULL; | |
639 | return rc; | |
640 | } | |
641 | ||
642 | *ab = audit_log_start(NULL, GFP_KERNEL, msg_type); | |
0644ec0c KC |
643 | if (unlikely(!*ab)) |
644 | return rc; | |
3035c51e | 645 | audit_log_format(*ab, "pid=%d uid=%u auid=%u ses=%u", |
860c0aaf EB |
646 | task_tgid_vnr(current), |
647 | from_kuid(&init_user_ns, current_uid()), | |
e1760bd5 | 648 | from_kuid(&init_user_ns, auid), ses); |
50397bd1 | 649 | if (sid) { |
2a862b32 | 650 | rc = security_secid_to_secctx(sid, &ctx, &len); |
50397bd1 EP |
651 | if (rc) |
652 | audit_log_format(*ab, " ssid=%u", sid); | |
2a862b32 | 653 | else { |
50397bd1 | 654 | audit_log_format(*ab, " subj=%s", ctx); |
2a862b32 AD |
655 | security_release_secctx(ctx, len); |
656 | } | |
50397bd1 EP |
657 | } |
658 | ||
659 | return rc; | |
660 | } | |
661 | ||
1da177e4 LT |
662 | static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
663 | { | |
860c0aaf | 664 | u32 seq, sid; |
1da177e4 LT |
665 | void *data; |
666 | struct audit_status *status_get, status_set; | |
667 | int err; | |
c0404993 | 668 | struct audit_buffer *ab; |
1da177e4 | 669 | u16 msg_type = nlh->nlmsg_type; |
e1760bd5 | 670 | kuid_t loginuid; /* loginuid of sender */ |
2532386f | 671 | u32 sessionid; |
e1396065 | 672 | struct audit_sig_info *sig_data; |
50397bd1 | 673 | char *ctx = NULL; |
e1396065 | 674 | u32 len; |
1da177e4 | 675 | |
c7bdb545 | 676 | err = audit_netlink_ok(skb, msg_type); |
1da177e4 LT |
677 | if (err) |
678 | return err; | |
679 | ||
c53fa1ed PM |
680 | loginuid = audit_get_loginuid(current); |
681 | sessionid = audit_get_sessionid(current); | |
682 | security_task_getsecid(current, &sid); | |
1da177e4 | 683 | seq = nlh->nlmsg_seq; |
c64e66c6 | 684 | data = nlmsg_data(nlh); |
1da177e4 LT |
685 | |
686 | switch (msg_type) { | |
687 | case AUDIT_GET: | |
688 | status_set.enabled = audit_enabled; | |
689 | status_set.failure = audit_failure; | |
690 | status_set.pid = audit_pid; | |
691 | status_set.rate_limit = audit_rate_limit; | |
692 | status_set.backlog_limit = audit_backlog_limit; | |
693 | status_set.lost = atomic_read(&audit_lost); | |
b7d11258 | 694 | status_set.backlog = skb_queue_len(&audit_skb_queue); |
15e47304 | 695 | audit_send_reply(NETLINK_CB(skb).portid, seq, AUDIT_GET, 0, 0, |
1da177e4 LT |
696 | &status_set, sizeof(status_set)); |
697 | break; | |
698 | case AUDIT_SET: | |
699 | if (nlh->nlmsg_len < sizeof(struct audit_status)) | |
700 | return -EINVAL; | |
701 | status_get = (struct audit_status *)data; | |
702 | if (status_get->mask & AUDIT_STATUS_ENABLED) { | |
ce29b682 | 703 | err = audit_set_enabled(status_get->enabled, |
2532386f | 704 | loginuid, sessionid, sid); |
20c6aaa3 | 705 | if (err < 0) |
706 | return err; | |
1da177e4 LT |
707 | } |
708 | if (status_get->mask & AUDIT_STATUS_FAILURE) { | |
ce29b682 | 709 | err = audit_set_failure(status_get->failure, |
2532386f | 710 | loginuid, sessionid, sid); |
20c6aaa3 | 711 | if (err < 0) |
712 | return err; | |
1da177e4 LT |
713 | } |
714 | if (status_get->mask & AUDIT_STATUS_PID) { | |
1a6b9f23 EP |
715 | int new_pid = status_get->pid; |
716 | ||
717 | if (audit_enabled != AUDIT_OFF) | |
718 | audit_log_config_change("audit_pid", new_pid, | |
719 | audit_pid, loginuid, | |
2532386f | 720 | sessionid, sid, 1); |
1a6b9f23 EP |
721 | |
722 | audit_pid = new_pid; | |
15e47304 | 723 | audit_nlk_portid = NETLINK_CB(skb).portid; |
1da177e4 | 724 | } |
20c6aaa3 | 725 | if (status_get->mask & AUDIT_STATUS_RATE_LIMIT) { |
5d136a01 | 726 | err = audit_set_rate_limit(status_get->rate_limit, |
2532386f | 727 | loginuid, sessionid, sid); |
20c6aaa3 | 728 | if (err < 0) |
729 | return err; | |
730 | } | |
1da177e4 | 731 | if (status_get->mask & AUDIT_STATUS_BACKLOG_LIMIT) |
5d136a01 | 732 | err = audit_set_backlog_limit(status_get->backlog_limit, |
2532386f | 733 | loginuid, sessionid, sid); |
1da177e4 | 734 | break; |
05474106 | 735 | case AUDIT_USER: |
039b6b3e RD |
736 | case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG: |
737 | case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2: | |
4a4cd633 DW |
738 | if (!audit_enabled && msg_type != AUDIT_USER_AVC) |
739 | return 0; | |
740 | ||
62062cf8 | 741 | err = audit_filter_user(msg_type); |
4a4cd633 DW |
742 | if (err == 1) { |
743 | err = 0; | |
522ed776 | 744 | if (msg_type == AUDIT_USER_TTY) { |
f95732e2 | 745 | err = tty_audit_push_task(current, loginuid, |
2532386f | 746 | sessionid); |
522ed776 MT |
747 | if (err) |
748 | break; | |
749 | } | |
860c0aaf | 750 | audit_log_common_recv_msg(&ab, msg_type, |
2532386f | 751 | loginuid, sessionid, sid); |
50397bd1 EP |
752 | |
753 | if (msg_type != AUDIT_USER_TTY) | |
754 | audit_log_format(ab, " msg='%.1024s'", | |
755 | (char *)data); | |
756 | else { | |
757 | int size; | |
758 | ||
f7616102 | 759 | audit_log_format(ab, " data="); |
50397bd1 | 760 | size = nlmsg_len(nlh); |
55ad2f8d MT |
761 | if (size > 0 && |
762 | ((unsigned char *)data)[size - 1] == '\0') | |
763 | size--; | |
b556f8ad | 764 | audit_log_n_untrustedstring(ab, data, size); |
4a4cd633 | 765 | } |
aecdc33e | 766 | audit_set_pid(ab, NETLINK_CB(skb).portid); |
50397bd1 | 767 | audit_log_end(ab); |
0f45aa18 | 768 | } |
1da177e4 | 769 | break; |
93315ed6 AG |
770 | case AUDIT_ADD_RULE: |
771 | case AUDIT_DEL_RULE: | |
772 | if (nlmsg_len(nlh) < sizeof(struct audit_rule_data)) | |
773 | return -EINVAL; | |
1a6b9f23 | 774 | if (audit_enabled == AUDIT_LOCKED) { |
860c0aaf EB |
775 | audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE, |
776 | loginuid, sessionid, sid); | |
50397bd1 EP |
777 | |
778 | audit_log_format(ab, " audit_enabled=%d res=0", | |
779 | audit_enabled); | |
780 | audit_log_end(ab); | |
6a01b07f SG |
781 | return -EPERM; |
782 | } | |
93315ed6 AG |
783 | /* fallthrough */ |
784 | case AUDIT_LIST_RULES: | |
15e47304 | 785 | err = audit_receive_filter(msg_type, NETLINK_CB(skb).portid, |
017143fe | 786 | seq, data, nlmsg_len(nlh), |
2532386f | 787 | loginuid, sessionid, sid); |
1da177e4 | 788 | break; |
74c3cbe3 AV |
789 | case AUDIT_TRIM: |
790 | audit_trim_trees(); | |
50397bd1 | 791 | |
860c0aaf EB |
792 | audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE, |
793 | loginuid, sessionid, sid); | |
50397bd1 | 794 | |
74c3cbe3 AV |
795 | audit_log_format(ab, " op=trim res=1"); |
796 | audit_log_end(ab); | |
797 | break; | |
798 | case AUDIT_MAKE_EQUIV: { | |
799 | void *bufp = data; | |
800 | u32 sizes[2]; | |
7719e437 | 801 | size_t msglen = nlmsg_len(nlh); |
74c3cbe3 AV |
802 | char *old, *new; |
803 | ||
804 | err = -EINVAL; | |
7719e437 | 805 | if (msglen < 2 * sizeof(u32)) |
74c3cbe3 AV |
806 | break; |
807 | memcpy(sizes, bufp, 2 * sizeof(u32)); | |
808 | bufp += 2 * sizeof(u32); | |
7719e437 HH |
809 | msglen -= 2 * sizeof(u32); |
810 | old = audit_unpack_string(&bufp, &msglen, sizes[0]); | |
74c3cbe3 AV |
811 | if (IS_ERR(old)) { |
812 | err = PTR_ERR(old); | |
813 | break; | |
814 | } | |
7719e437 | 815 | new = audit_unpack_string(&bufp, &msglen, sizes[1]); |
74c3cbe3 AV |
816 | if (IS_ERR(new)) { |
817 | err = PTR_ERR(new); | |
818 | kfree(old); | |
819 | break; | |
820 | } | |
821 | /* OK, here comes... */ | |
822 | err = audit_tag_tree(old, new); | |
823 | ||
860c0aaf EB |
824 | audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE, |
825 | loginuid, sessionid, sid); | |
50397bd1 | 826 | |
74c3cbe3 AV |
827 | audit_log_format(ab, " op=make_equiv old="); |
828 | audit_log_untrustedstring(ab, old); | |
829 | audit_log_format(ab, " new="); | |
830 | audit_log_untrustedstring(ab, new); | |
831 | audit_log_format(ab, " res=%d", !err); | |
832 | audit_log_end(ab); | |
833 | kfree(old); | |
834 | kfree(new); | |
835 | break; | |
836 | } | |
c2f0c7c3 | 837 | case AUDIT_SIGNAL_INFO: |
939cbf26 EP |
838 | len = 0; |
839 | if (audit_sig_sid) { | |
840 | err = security_secid_to_secctx(audit_sig_sid, &ctx, &len); | |
841 | if (err) | |
842 | return err; | |
843 | } | |
e1396065 AV |
844 | sig_data = kmalloc(sizeof(*sig_data) + len, GFP_KERNEL); |
845 | if (!sig_data) { | |
939cbf26 EP |
846 | if (audit_sig_sid) |
847 | security_release_secctx(ctx, len); | |
e1396065 AV |
848 | return -ENOMEM; |
849 | } | |
cca080d9 | 850 | sig_data->uid = from_kuid(&init_user_ns, audit_sig_uid); |
e1396065 | 851 | sig_data->pid = audit_sig_pid; |
939cbf26 EP |
852 | if (audit_sig_sid) { |
853 | memcpy(sig_data->ctx, ctx, len); | |
854 | security_release_secctx(ctx, len); | |
855 | } | |
15e47304 | 856 | audit_send_reply(NETLINK_CB(skb).portid, seq, AUDIT_SIGNAL_INFO, |
e1396065 AV |
857 | 0, 0, sig_data, sizeof(*sig_data) + len); |
858 | kfree(sig_data); | |
c2f0c7c3 | 859 | break; |
522ed776 MT |
860 | case AUDIT_TTY_GET: { |
861 | struct audit_tty_status s; | |
8aa14b64 EB |
862 | struct task_struct *tsk = current; |
863 | ||
864 | spin_lock_irq(&tsk->sighand->siglock); | |
865 | s.enabled = tsk->signal->audit_tty != 0; | |
866 | spin_unlock_irq(&tsk->sighand->siglock); | |
867 | ||
aecdc33e | 868 | audit_send_reply(NETLINK_CB(skb).portid, seq, |
8aa14b64 | 869 | AUDIT_TTY_GET, 0, 0, &s, sizeof(s)); |
522ed776 MT |
870 | break; |
871 | } | |
872 | case AUDIT_TTY_SET: { | |
873 | struct audit_tty_status *s; | |
8aa14b64 | 874 | struct task_struct *tsk = current; |
522ed776 MT |
875 | |
876 | if (nlh->nlmsg_len < sizeof(struct audit_tty_status)) | |
877 | return -EINVAL; | |
878 | s = data; | |
879 | if (s->enabled != 0 && s->enabled != 1) | |
880 | return -EINVAL; | |
8aa14b64 EB |
881 | |
882 | spin_lock_irq(&tsk->sighand->siglock); | |
883 | tsk->signal->audit_tty = s->enabled != 0; | |
884 | spin_unlock_irq(&tsk->sighand->siglock); | |
522ed776 MT |
885 | break; |
886 | } | |
1da177e4 LT |
887 | default: |
888 | err = -EINVAL; | |
889 | break; | |
890 | } | |
891 | ||
892 | return err < 0 ? err : 0; | |
893 | } | |
894 | ||
b0dd25a8 | 895 | /* |
ea7ae60b EP |
896 | * Get message from skb. Each message is processed by audit_receive_msg. |
897 | * Malformed skbs with wrong length are discarded silently. | |
b0dd25a8 | 898 | */ |
2a0a6ebe | 899 | static void audit_receive_skb(struct sk_buff *skb) |
1da177e4 | 900 | { |
ea7ae60b EP |
901 | struct nlmsghdr *nlh; |
902 | /* | |
903 | * len MUST be signed for NLMSG_NEXT to be able to dec it below 0 | |
904 | * if the nlmsg_len was not aligned | |
905 | */ | |
906 | int len; | |
907 | int err; | |
908 | ||
909 | nlh = nlmsg_hdr(skb); | |
910 | len = skb->len; | |
911 | ||
912 | while (NLMSG_OK(nlh, len)) { | |
913 | err = audit_receive_msg(skb, nlh); | |
914 | /* if err or if this message says it wants a response */ | |
915 | if (err || (nlh->nlmsg_flags & NLM_F_ACK)) | |
1da177e4 | 916 | netlink_ack(skb, nlh, err); |
ea7ae60b EP |
917 | |
918 | nlh = NLMSG_NEXT(nlh, len); | |
1da177e4 | 919 | } |
1da177e4 LT |
920 | } |
921 | ||
922 | /* Receive messages from netlink socket. */ | |
cd40b7d3 | 923 | static void audit_receive(struct sk_buff *skb) |
1da177e4 | 924 | { |
f368c07d | 925 | mutex_lock(&audit_cmd_mutex); |
cd40b7d3 | 926 | audit_receive_skb(skb); |
f368c07d | 927 | mutex_unlock(&audit_cmd_mutex); |
1da177e4 LT |
928 | } |
929 | ||
1da177e4 LT |
930 | /* Initialize audit support at boot time. */ |
931 | static int __init audit_init(void) | |
932 | { | |
f368c07d | 933 | int i; |
a31f2d17 PNA |
934 | struct netlink_kernel_cfg cfg = { |
935 | .input = audit_receive, | |
936 | }; | |
f368c07d | 937 | |
a3f07114 EP |
938 | if (audit_initialized == AUDIT_DISABLED) |
939 | return 0; | |
940 | ||
1da177e4 LT |
941 | printk(KERN_INFO "audit: initializing netlink socket (%s)\n", |
942 | audit_default ? "enabled" : "disabled"); | |
9f00d977 | 943 | audit_sock = netlink_kernel_create(&init_net, NETLINK_AUDIT, &cfg); |
1da177e4 LT |
944 | if (!audit_sock) |
945 | audit_panic("cannot initialize netlink socket"); | |
71e1c784 AG |
946 | else |
947 | audit_sock->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT; | |
1da177e4 | 948 | |
6ff5e459 RGB |
949 | kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd"); |
950 | if (IS_ERR(kauditd_task)) | |
951 | return PTR_ERR(kauditd_task); | |
952 | ||
b7d11258 | 953 | skb_queue_head_init(&audit_skb_queue); |
f3d357b0 | 954 | skb_queue_head_init(&audit_skb_hold_queue); |
a3f07114 | 955 | audit_initialized = AUDIT_INITIALIZED; |
1da177e4 | 956 | audit_enabled = audit_default; |
b593d384 | 957 | audit_ever_enabled |= !!audit_default; |
3dc7e315 | 958 | |
9ad9ad38 | 959 | audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, "initialized"); |
f368c07d | 960 | |
f368c07d AG |
961 | for (i = 0; i < AUDIT_INODE_BUCKETS; i++) |
962 | INIT_LIST_HEAD(&audit_inode_hash[i]); | |
f368c07d | 963 | |
1da177e4 LT |
964 | return 0; |
965 | } | |
1da177e4 LT |
966 | __initcall(audit_init); |
967 | ||
968 | /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */ | |
969 | static int __init audit_enable(char *str) | |
970 | { | |
971 | audit_default = !!simple_strtol(str, NULL, 0); | |
a3f07114 EP |
972 | if (!audit_default) |
973 | audit_initialized = AUDIT_DISABLED; | |
974 | ||
975 | printk(KERN_INFO "audit: %s", audit_default ? "enabled" : "disabled"); | |
976 | ||
977 | if (audit_initialized == AUDIT_INITIALIZED) { | |
1da177e4 | 978 | audit_enabled = audit_default; |
b593d384 | 979 | audit_ever_enabled |= !!audit_default; |
a3f07114 EP |
980 | } else if (audit_initialized == AUDIT_UNINITIALIZED) { |
981 | printk(" (after initialization)"); | |
982 | } else { | |
983 | printk(" (until reboot)"); | |
b593d384 | 984 | } |
a3f07114 EP |
985 | printk("\n"); |
986 | ||
9b41046c | 987 | return 1; |
1da177e4 LT |
988 | } |
989 | ||
990 | __setup("audit=", audit_enable); | |
991 | ||
16e1904e CW |
992 | static void audit_buffer_free(struct audit_buffer *ab) |
993 | { | |
994 | unsigned long flags; | |
995 | ||
8fc6115c CW |
996 | if (!ab) |
997 | return; | |
998 | ||
5ac52f33 CW |
999 | if (ab->skb) |
1000 | kfree_skb(ab->skb); | |
b7d11258 | 1001 | |
16e1904e | 1002 | spin_lock_irqsave(&audit_freelist_lock, flags); |
5d136a01 | 1003 | if (audit_freelist_count > AUDIT_MAXFREE) |
16e1904e | 1004 | kfree(ab); |
5d136a01 SH |
1005 | else { |
1006 | audit_freelist_count++; | |
16e1904e | 1007 | list_add(&ab->list, &audit_freelist); |
5d136a01 | 1008 | } |
16e1904e CW |
1009 | spin_unlock_irqrestore(&audit_freelist_lock, flags); |
1010 | } | |
1011 | ||
c0404993 | 1012 | static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx, |
dd0fc66f | 1013 | gfp_t gfp_mask, int type) |
16e1904e CW |
1014 | { |
1015 | unsigned long flags; | |
1016 | struct audit_buffer *ab = NULL; | |
c0404993 | 1017 | struct nlmsghdr *nlh; |
16e1904e CW |
1018 | |
1019 | spin_lock_irqsave(&audit_freelist_lock, flags); | |
1020 | if (!list_empty(&audit_freelist)) { | |
1021 | ab = list_entry(audit_freelist.next, | |
1022 | struct audit_buffer, list); | |
1023 | list_del(&ab->list); | |
1024 | --audit_freelist_count; | |
1025 | } | |
1026 | spin_unlock_irqrestore(&audit_freelist_lock, flags); | |
1027 | ||
1028 | if (!ab) { | |
4332bdd3 | 1029 | ab = kmalloc(sizeof(*ab), gfp_mask); |
16e1904e | 1030 | if (!ab) |
8fc6115c | 1031 | goto err; |
16e1904e | 1032 | } |
8fc6115c | 1033 | |
b7d11258 | 1034 | ab->ctx = ctx; |
9ad9ad38 | 1035 | ab->gfp_mask = gfp_mask; |
ee080e6c EP |
1036 | |
1037 | ab->skb = nlmsg_new(AUDIT_BUFSIZ, gfp_mask); | |
1038 | if (!ab->skb) | |
c64e66c6 | 1039 | goto err; |
ee080e6c | 1040 | |
c64e66c6 DM |
1041 | nlh = nlmsg_put(ab->skb, 0, 0, type, 0, 0); |
1042 | if (!nlh) | |
1043 | goto out_kfree_skb; | |
ee080e6c | 1044 | |
16e1904e | 1045 | return ab; |
ee080e6c | 1046 | |
c64e66c6 | 1047 | out_kfree_skb: |
ee080e6c EP |
1048 | kfree_skb(ab->skb); |
1049 | ab->skb = NULL; | |
8fc6115c CW |
1050 | err: |
1051 | audit_buffer_free(ab); | |
1052 | return NULL; | |
16e1904e | 1053 | } |
1da177e4 | 1054 | |
b0dd25a8 RD |
1055 | /** |
1056 | * audit_serial - compute a serial number for the audit record | |
1057 | * | |
1058 | * Compute a serial number for the audit record. Audit records are | |
bfb4496e DW |
1059 | * written to user-space as soon as they are generated, so a complete |
1060 | * audit record may be written in several pieces. The timestamp of the | |
1061 | * record and this serial number are used by the user-space tools to | |
1062 | * determine which pieces belong to the same audit record. The | |
1063 | * (timestamp,serial) tuple is unique for each syscall and is live from | |
1064 | * syscall entry to syscall exit. | |
1065 | * | |
bfb4496e DW |
1066 | * NOTE: Another possibility is to store the formatted records off the |
1067 | * audit context (for those records that have a context), and emit them | |
1068 | * all at syscall exit. However, this could delay the reporting of | |
1069 | * significant errors until syscall exit (or never, if the system | |
b0dd25a8 RD |
1070 | * halts). |
1071 | */ | |
bfb4496e DW |
1072 | unsigned int audit_serial(void) |
1073 | { | |
34af946a | 1074 | static DEFINE_SPINLOCK(serial_lock); |
d5b454f2 DW |
1075 | static unsigned int serial = 0; |
1076 | ||
1077 | unsigned long flags; | |
1078 | unsigned int ret; | |
bfb4496e | 1079 | |
d5b454f2 | 1080 | spin_lock_irqsave(&serial_lock, flags); |
bfb4496e | 1081 | do { |
ce625a80 DW |
1082 | ret = ++serial; |
1083 | } while (unlikely(!ret)); | |
d5b454f2 | 1084 | spin_unlock_irqrestore(&serial_lock, flags); |
bfb4496e | 1085 | |
d5b454f2 | 1086 | return ret; |
bfb4496e DW |
1087 | } |
1088 | ||
5600b892 | 1089 | static inline void audit_get_stamp(struct audit_context *ctx, |
bfb4496e DW |
1090 | struct timespec *t, unsigned int *serial) |
1091 | { | |
48887e63 | 1092 | if (!ctx || !auditsc_get_stamp(ctx, t, serial)) { |
bfb4496e DW |
1093 | *t = CURRENT_TIME; |
1094 | *serial = audit_serial(); | |
1095 | } | |
1096 | } | |
1097 | ||
82919919 AM |
1098 | /* |
1099 | * Wait for auditd to drain the queue a little | |
1100 | */ | |
1101 | static void wait_for_auditd(unsigned long sleep_time) | |
1102 | { | |
1103 | DECLARE_WAITQUEUE(wait, current); | |
1104 | set_current_state(TASK_INTERRUPTIBLE); | |
1105 | add_wait_queue(&audit_backlog_wait, &wait); | |
1106 | ||
1107 | if (audit_backlog_limit && | |
1108 | skb_queue_len(&audit_skb_queue) > audit_backlog_limit) | |
1109 | schedule_timeout(sleep_time); | |
1110 | ||
1111 | __set_current_state(TASK_RUNNING); | |
1112 | remove_wait_queue(&audit_backlog_wait, &wait); | |
1113 | } | |
1114 | ||
1da177e4 LT |
1115 | /* Obtain an audit buffer. This routine does locking to obtain the |
1116 | * audit buffer, but then no locking is required for calls to | |
1117 | * audit_log_*format. If the tsk is a task that is currently in a | |
1118 | * syscall, then the syscall is marked as auditable and an audit record | |
1119 | * will be written at syscall exit. If there is no associated task, tsk | |
1120 | * should be NULL. */ | |
9ad9ad38 | 1121 | |
b0dd25a8 RD |
1122 | /** |
1123 | * audit_log_start - obtain an audit buffer | |
1124 | * @ctx: audit_context (may be NULL) | |
1125 | * @gfp_mask: type of allocation | |
1126 | * @type: audit message type | |
1127 | * | |
1128 | * Returns audit_buffer pointer on success or NULL on error. | |
1129 | * | |
1130 | * Obtain an audit buffer. This routine does locking to obtain the | |
1131 | * audit buffer, but then no locking is required for calls to | |
1132 | * audit_log_*format. If the task (ctx) is a task that is currently in a | |
1133 | * syscall, then the syscall is marked as auditable and an audit record | |
1134 | * will be written at syscall exit. If there is no associated task, then | |
1135 | * task context (ctx) should be NULL. | |
1136 | */ | |
9796fdd8 | 1137 | struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, |
9ad9ad38 | 1138 | int type) |
1da177e4 LT |
1139 | { |
1140 | struct audit_buffer *ab = NULL; | |
1da177e4 | 1141 | struct timespec t; |
ef00be05 | 1142 | unsigned int uninitialized_var(serial); |
9ad9ad38 | 1143 | int reserve; |
ac4cec44 | 1144 | unsigned long timeout_start = jiffies; |
1da177e4 | 1145 | |
a3f07114 | 1146 | if (audit_initialized != AUDIT_INITIALIZED) |
1da177e4 LT |
1147 | return NULL; |
1148 | ||
c8edc80c DK |
1149 | if (unlikely(audit_filter_type(type))) |
1150 | return NULL; | |
1151 | ||
9ad9ad38 DW |
1152 | if (gfp_mask & __GFP_WAIT) |
1153 | reserve = 0; | |
1154 | else | |
5600b892 | 1155 | reserve = 5; /* Allow atomic callers to go up to five |
9ad9ad38 DW |
1156 | entries over the normal backlog limit */ |
1157 | ||
1158 | while (audit_backlog_limit | |
1159 | && skb_queue_len(&audit_skb_queue) > audit_backlog_limit + reserve) { | |
82919919 AM |
1160 | if (gfp_mask & __GFP_WAIT && audit_backlog_wait_time) { |
1161 | unsigned long sleep_time; | |
9ad9ad38 | 1162 | |
82919919 AM |
1163 | sleep_time = timeout_start + audit_backlog_wait_time - |
1164 | jiffies; | |
1165 | if ((long)sleep_time > 0) | |
1166 | wait_for_auditd(sleep_time); | |
ac4cec44 | 1167 | continue; |
9ad9ad38 | 1168 | } |
320f1b1e | 1169 | if (audit_rate_check() && printk_ratelimit()) |
fb19b4c6 DW |
1170 | printk(KERN_WARNING |
1171 | "audit: audit_backlog=%d > " | |
1172 | "audit_backlog_limit=%d\n", | |
1173 | skb_queue_len(&audit_skb_queue), | |
1174 | audit_backlog_limit); | |
1175 | audit_log_lost("backlog limit exceeded"); | |
ac4cec44 DW |
1176 | audit_backlog_wait_time = audit_backlog_wait_overflow; |
1177 | wake_up(&audit_backlog_wait); | |
fb19b4c6 DW |
1178 | return NULL; |
1179 | } | |
1180 | ||
9ad9ad38 | 1181 | ab = audit_buffer_alloc(ctx, gfp_mask, type); |
1da177e4 LT |
1182 | if (!ab) { |
1183 | audit_log_lost("out of memory in audit_log_start"); | |
1184 | return NULL; | |
1185 | } | |
1186 | ||
bfb4496e | 1187 | audit_get_stamp(ab->ctx, &t, &serial); |
197c69c6 | 1188 | |
1da177e4 LT |
1189 | audit_log_format(ab, "audit(%lu.%03lu:%u): ", |
1190 | t.tv_sec, t.tv_nsec/1000000, serial); | |
1191 | return ab; | |
1192 | } | |
1193 | ||
8fc6115c | 1194 | /** |
5ac52f33 | 1195 | * audit_expand - expand skb in the audit buffer |
8fc6115c | 1196 | * @ab: audit_buffer |
b0dd25a8 | 1197 | * @extra: space to add at tail of the skb |
8fc6115c CW |
1198 | * |
1199 | * Returns 0 (no space) on failed expansion, or available space if | |
1200 | * successful. | |
1201 | */ | |
e3b926b4 | 1202 | static inline int audit_expand(struct audit_buffer *ab, int extra) |
8fc6115c | 1203 | { |
5ac52f33 | 1204 | struct sk_buff *skb = ab->skb; |
406a1d86 HX |
1205 | int oldtail = skb_tailroom(skb); |
1206 | int ret = pskb_expand_head(skb, 0, extra, ab->gfp_mask); | |
1207 | int newtail = skb_tailroom(skb); | |
1208 | ||
5ac52f33 CW |
1209 | if (ret < 0) { |
1210 | audit_log_lost("out of memory in audit_expand"); | |
8fc6115c | 1211 | return 0; |
5ac52f33 | 1212 | } |
406a1d86 HX |
1213 | |
1214 | skb->truesize += newtail - oldtail; | |
1215 | return newtail; | |
8fc6115c | 1216 | } |
1da177e4 | 1217 | |
b0dd25a8 RD |
1218 | /* |
1219 | * Format an audit message into the audit buffer. If there isn't enough | |
1da177e4 LT |
1220 | * room in the audit buffer, more room will be allocated and vsnprint |
1221 | * will be called a second time. Currently, we assume that a printk | |
b0dd25a8 RD |
1222 | * can't format message larger than 1024 bytes, so we don't either. |
1223 | */ | |
1da177e4 LT |
1224 | static void audit_log_vformat(struct audit_buffer *ab, const char *fmt, |
1225 | va_list args) | |
1226 | { | |
1227 | int len, avail; | |
5ac52f33 | 1228 | struct sk_buff *skb; |
eecb0a73 | 1229 | va_list args2; |
1da177e4 LT |
1230 | |
1231 | if (!ab) | |
1232 | return; | |
1233 | ||
5ac52f33 CW |
1234 | BUG_ON(!ab->skb); |
1235 | skb = ab->skb; | |
1236 | avail = skb_tailroom(skb); | |
1237 | if (avail == 0) { | |
e3b926b4 | 1238 | avail = audit_expand(ab, AUDIT_BUFSIZ); |
8fc6115c CW |
1239 | if (!avail) |
1240 | goto out; | |
1da177e4 | 1241 | } |
eecb0a73 | 1242 | va_copy(args2, args); |
27a884dc | 1243 | len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args); |
1da177e4 LT |
1244 | if (len >= avail) { |
1245 | /* The printk buffer is 1024 bytes long, so if we get | |
1246 | * here and AUDIT_BUFSIZ is at least 1024, then we can | |
1247 | * log everything that printk could have logged. */ | |
b0dd25a8 RD |
1248 | avail = audit_expand(ab, |
1249 | max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail)); | |
8fc6115c | 1250 | if (!avail) |
a0e86bd4 | 1251 | goto out_va_end; |
27a884dc | 1252 | len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args2); |
1da177e4 | 1253 | } |
168b7173 SG |
1254 | if (len > 0) |
1255 | skb_put(skb, len); | |
a0e86bd4 JJ |
1256 | out_va_end: |
1257 | va_end(args2); | |
8fc6115c CW |
1258 | out: |
1259 | return; | |
1da177e4 LT |
1260 | } |
1261 | ||
b0dd25a8 RD |
1262 | /** |
1263 | * audit_log_format - format a message into the audit buffer. | |
1264 | * @ab: audit_buffer | |
1265 | * @fmt: format string | |
1266 | * @...: optional parameters matching @fmt string | |
1267 | * | |
1268 | * All the work is done in audit_log_vformat. | |
1269 | */ | |
1da177e4 LT |
1270 | void audit_log_format(struct audit_buffer *ab, const char *fmt, ...) |
1271 | { | |
1272 | va_list args; | |
1273 | ||
1274 | if (!ab) | |
1275 | return; | |
1276 | va_start(args, fmt); | |
1277 | audit_log_vformat(ab, fmt, args); | |
1278 | va_end(args); | |
1279 | } | |
1280 | ||
b0dd25a8 RD |
1281 | /** |
1282 | * audit_log_hex - convert a buffer to hex and append it to the audit skb | |
1283 | * @ab: the audit_buffer | |
1284 | * @buf: buffer to convert to hex | |
1285 | * @len: length of @buf to be converted | |
1286 | * | |
1287 | * No return value; failure to expand is silently ignored. | |
1288 | * | |
1289 | * This function will take the passed buf and convert it into a string of | |
1290 | * ascii hex digits. The new string is placed onto the skb. | |
1291 | */ | |
b556f8ad | 1292 | void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf, |
168b7173 | 1293 | size_t len) |
83c7d091 | 1294 | { |
168b7173 SG |
1295 | int i, avail, new_len; |
1296 | unsigned char *ptr; | |
1297 | struct sk_buff *skb; | |
1298 | static const unsigned char *hex = "0123456789ABCDEF"; | |
1299 | ||
8ef2d304 AG |
1300 | if (!ab) |
1301 | return; | |
1302 | ||
168b7173 SG |
1303 | BUG_ON(!ab->skb); |
1304 | skb = ab->skb; | |
1305 | avail = skb_tailroom(skb); | |
1306 | new_len = len<<1; | |
1307 | if (new_len >= avail) { | |
1308 | /* Round the buffer request up to the next multiple */ | |
1309 | new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1); | |
1310 | avail = audit_expand(ab, new_len); | |
1311 | if (!avail) | |
1312 | return; | |
1313 | } | |
83c7d091 | 1314 | |
27a884dc | 1315 | ptr = skb_tail_pointer(skb); |
168b7173 SG |
1316 | for (i=0; i<len; i++) { |
1317 | *ptr++ = hex[(buf[i] & 0xF0)>>4]; /* Upper nibble */ | |
1318 | *ptr++ = hex[buf[i] & 0x0F]; /* Lower nibble */ | |
1319 | } | |
1320 | *ptr = 0; | |
1321 | skb_put(skb, len << 1); /* new string is twice the old string */ | |
83c7d091 DW |
1322 | } |
1323 | ||
9c937dcc AG |
1324 | /* |
1325 | * Format a string of no more than slen characters into the audit buffer, | |
1326 | * enclosed in quote marks. | |
1327 | */ | |
b556f8ad EP |
1328 | void audit_log_n_string(struct audit_buffer *ab, const char *string, |
1329 | size_t slen) | |
9c937dcc AG |
1330 | { |
1331 | int avail, new_len; | |
1332 | unsigned char *ptr; | |
1333 | struct sk_buff *skb; | |
1334 | ||
8ef2d304 AG |
1335 | if (!ab) |
1336 | return; | |
1337 | ||
9c937dcc AG |
1338 | BUG_ON(!ab->skb); |
1339 | skb = ab->skb; | |
1340 | avail = skb_tailroom(skb); | |
1341 | new_len = slen + 3; /* enclosing quotes + null terminator */ | |
1342 | if (new_len > avail) { | |
1343 | avail = audit_expand(ab, new_len); | |
1344 | if (!avail) | |
1345 | return; | |
1346 | } | |
27a884dc | 1347 | ptr = skb_tail_pointer(skb); |
9c937dcc AG |
1348 | *ptr++ = '"'; |
1349 | memcpy(ptr, string, slen); | |
1350 | ptr += slen; | |
1351 | *ptr++ = '"'; | |
1352 | *ptr = 0; | |
1353 | skb_put(skb, slen + 2); /* don't include null terminator */ | |
1354 | } | |
1355 | ||
de6bbd1d EP |
1356 | /** |
1357 | * audit_string_contains_control - does a string need to be logged in hex | |
f706d5d2 DJ |
1358 | * @string: string to be checked |
1359 | * @len: max length of the string to check | |
de6bbd1d EP |
1360 | */ |
1361 | int audit_string_contains_control(const char *string, size_t len) | |
1362 | { | |
1363 | const unsigned char *p; | |
b3897f56 | 1364 | for (p = string; p < (const unsigned char *)string + len; p++) { |
1d6c9649 | 1365 | if (*p == '"' || *p < 0x21 || *p > 0x7e) |
de6bbd1d EP |
1366 | return 1; |
1367 | } | |
1368 | return 0; | |
1369 | } | |
1370 | ||
b0dd25a8 | 1371 | /** |
522ed776 | 1372 | * audit_log_n_untrustedstring - log a string that may contain random characters |
b0dd25a8 | 1373 | * @ab: audit_buffer |
f706d5d2 | 1374 | * @len: length of string (not including trailing null) |
b0dd25a8 RD |
1375 | * @string: string to be logged |
1376 | * | |
1377 | * This code will escape a string that is passed to it if the string | |
1378 | * contains a control character, unprintable character, double quote mark, | |
168b7173 | 1379 | * or a space. Unescaped strings will start and end with a double quote mark. |
b0dd25a8 | 1380 | * Strings that are escaped are printed in hex (2 digits per char). |
9c937dcc AG |
1381 | * |
1382 | * The caller specifies the number of characters in the string to log, which may | |
1383 | * or may not be the entire string. | |
b0dd25a8 | 1384 | */ |
b556f8ad EP |
1385 | void audit_log_n_untrustedstring(struct audit_buffer *ab, const char *string, |
1386 | size_t len) | |
83c7d091 | 1387 | { |
de6bbd1d | 1388 | if (audit_string_contains_control(string, len)) |
b556f8ad | 1389 | audit_log_n_hex(ab, string, len); |
de6bbd1d | 1390 | else |
b556f8ad | 1391 | audit_log_n_string(ab, string, len); |
83c7d091 DW |
1392 | } |
1393 | ||
9c937dcc | 1394 | /** |
522ed776 | 1395 | * audit_log_untrustedstring - log a string that may contain random characters |
9c937dcc AG |
1396 | * @ab: audit_buffer |
1397 | * @string: string to be logged | |
1398 | * | |
522ed776 | 1399 | * Same as audit_log_n_untrustedstring(), except that strlen is used to |
9c937dcc AG |
1400 | * determine string length. |
1401 | */ | |
de6bbd1d | 1402 | void audit_log_untrustedstring(struct audit_buffer *ab, const char *string) |
9c937dcc | 1403 | { |
b556f8ad | 1404 | audit_log_n_untrustedstring(ab, string, strlen(string)); |
9c937dcc AG |
1405 | } |
1406 | ||
168b7173 | 1407 | /* This is a helper-function to print the escaped d_path */ |
1da177e4 | 1408 | void audit_log_d_path(struct audit_buffer *ab, const char *prefix, |
66b3fad3 | 1409 | const struct path *path) |
1da177e4 | 1410 | { |
44707fdf | 1411 | char *p, *pathname; |
1da177e4 | 1412 | |
8fc6115c | 1413 | if (prefix) |
c158a35c | 1414 | audit_log_format(ab, "%s", prefix); |
1da177e4 | 1415 | |
168b7173 | 1416 | /* We will allow 11 spaces for ' (deleted)' to be appended */ |
44707fdf JB |
1417 | pathname = kmalloc(PATH_MAX+11, ab->gfp_mask); |
1418 | if (!pathname) { | |
def57543 | 1419 | audit_log_string(ab, "<no_memory>"); |
168b7173 | 1420 | return; |
1da177e4 | 1421 | } |
cf28b486 | 1422 | p = d_path(path, pathname, PATH_MAX+11); |
168b7173 SG |
1423 | if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */ |
1424 | /* FIXME: can we save some information here? */ | |
def57543 | 1425 | audit_log_string(ab, "<too_long>"); |
5600b892 | 1426 | } else |
168b7173 | 1427 | audit_log_untrustedstring(ab, p); |
44707fdf | 1428 | kfree(pathname); |
1da177e4 LT |
1429 | } |
1430 | ||
9d960985 EP |
1431 | void audit_log_key(struct audit_buffer *ab, char *key) |
1432 | { | |
1433 | audit_log_format(ab, " key="); | |
1434 | if (key) | |
1435 | audit_log_untrustedstring(ab, key); | |
1436 | else | |
1437 | audit_log_format(ab, "(null)"); | |
1438 | } | |
1439 | ||
a51d9eaa KC |
1440 | /** |
1441 | * audit_log_link_denied - report a link restriction denial | |
1442 | * @operation: specific link opreation | |
1443 | * @link: the path that triggered the restriction | |
1444 | */ | |
1445 | void audit_log_link_denied(const char *operation, struct path *link) | |
1446 | { | |
1447 | struct audit_buffer *ab; | |
1448 | ||
1449 | ab = audit_log_start(current->audit_context, GFP_KERNEL, | |
1450 | AUDIT_ANOM_LINK); | |
d1c7d97a SL |
1451 | if (!ab) |
1452 | return; | |
a51d9eaa KC |
1453 | audit_log_format(ab, "op=%s action=denied", operation); |
1454 | audit_log_format(ab, " pid=%d comm=", current->pid); | |
1455 | audit_log_untrustedstring(ab, current->comm); | |
1456 | audit_log_d_path(ab, " path=", link); | |
1457 | audit_log_format(ab, " dev="); | |
1458 | audit_log_untrustedstring(ab, link->dentry->d_inode->i_sb->s_id); | |
1459 | audit_log_format(ab, " ino=%lu", link->dentry->d_inode->i_ino); | |
1460 | audit_log_end(ab); | |
1461 | } | |
1462 | ||
b0dd25a8 RD |
1463 | /** |
1464 | * audit_log_end - end one audit record | |
1465 | * @ab: the audit_buffer | |
1466 | * | |
1467 | * The netlink_* functions cannot be called inside an irq context, so | |
1468 | * the audit buffer is placed on a queue and a tasklet is scheduled to | |
1da177e4 | 1469 | * remove them from the queue outside the irq context. May be called in |
b0dd25a8 RD |
1470 | * any context. |
1471 | */ | |
b7d11258 | 1472 | void audit_log_end(struct audit_buffer *ab) |
1da177e4 | 1473 | { |
1da177e4 LT |
1474 | if (!ab) |
1475 | return; | |
1476 | if (!audit_rate_check()) { | |
1477 | audit_log_lost("rate limit exceeded"); | |
1478 | } else { | |
8d07a67c | 1479 | struct nlmsghdr *nlh = nlmsg_hdr(ab->skb); |
f3d357b0 EP |
1480 | nlh->nlmsg_len = ab->skb->len - NLMSG_SPACE(0); |
1481 | ||
b7d11258 | 1482 | if (audit_pid) { |
b7d11258 | 1483 | skb_queue_tail(&audit_skb_queue, ab->skb); |
b7d11258 | 1484 | wake_up_interruptible(&kauditd_wait); |
f3d357b0 | 1485 | } else { |
038cbcf6 | 1486 | audit_printk_skb(ab->skb); |
b7d11258 | 1487 | } |
f3d357b0 | 1488 | ab->skb = NULL; |
1da177e4 | 1489 | } |
16e1904e | 1490 | audit_buffer_free(ab); |
1da177e4 LT |
1491 | } |
1492 | ||
b0dd25a8 RD |
1493 | /** |
1494 | * audit_log - Log an audit record | |
1495 | * @ctx: audit context | |
1496 | * @gfp_mask: type of allocation | |
1497 | * @type: audit message type | |
1498 | * @fmt: format string to use | |
1499 | * @...: variable parameters matching the format string | |
1500 | * | |
1501 | * This is a convenience function that calls audit_log_start, | |
1502 | * audit_log_vformat, and audit_log_end. It may be called | |
1503 | * in any context. | |
1504 | */ | |
5600b892 | 1505 | void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type, |
9ad9ad38 | 1506 | const char *fmt, ...) |
1da177e4 LT |
1507 | { |
1508 | struct audit_buffer *ab; | |
1509 | va_list args; | |
1510 | ||
9ad9ad38 | 1511 | ab = audit_log_start(ctx, gfp_mask, type); |
1da177e4 LT |
1512 | if (ab) { |
1513 | va_start(args, fmt); | |
1514 | audit_log_vformat(ab, fmt, args); | |
1515 | va_end(args); | |
1516 | audit_log_end(ab); | |
1517 | } | |
1518 | } | |
bf45da97 | 1519 | |
131ad62d MDF |
1520 | #ifdef CONFIG_SECURITY |
1521 | /** | |
1522 | * audit_log_secctx - Converts and logs SELinux context | |
1523 | * @ab: audit_buffer | |
1524 | * @secid: security number | |
1525 | * | |
1526 | * This is a helper function that calls security_secid_to_secctx to convert | |
1527 | * secid to secctx and then adds the (converted) SELinux context to the audit | |
1528 | * log by calling audit_log_format, thus also preventing leak of internal secid | |
1529 | * to userspace. If secid cannot be converted audit_panic is called. | |
1530 | */ | |
1531 | void audit_log_secctx(struct audit_buffer *ab, u32 secid) | |
1532 | { | |
1533 | u32 len; | |
1534 | char *secctx; | |
1535 | ||
1536 | if (security_secid_to_secctx(secid, &secctx, &len)) { | |
1537 | audit_panic("Cannot convert secid to context"); | |
1538 | } else { | |
1539 | audit_log_format(ab, " obj=%s", secctx); | |
1540 | security_release_secctx(secctx, len); | |
1541 | } | |
1542 | } | |
1543 | EXPORT_SYMBOL(audit_log_secctx); | |
1544 | #endif | |
1545 | ||
bf45da97 | 1546 | EXPORT_SYMBOL(audit_log_start); |
1547 | EXPORT_SYMBOL(audit_log_end); | |
1548 | EXPORT_SYMBOL(audit_log_format); | |
1549 | EXPORT_SYMBOL(audit_log); |