]>
Commit | Line | Data |
---|---|---|
1 | /* audit.c -- Auditing support | |
2 | * Gateway between the kernel (e.g., selinux) and the user-space audit daemon. | |
3 | * System-call specific features have moved to auditsc.c | |
4 | * | |
5 | * Copyright 2003-2007 Red Hat Inc., Durham, North Carolina. | |
6 | * All Rights Reserved. | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
21 | * | |
22 | * Written by Rickard E. (Rik) Faith <faith@redhat.com> | |
23 | * | |
24 | * Goals: 1) Integrate fully with Security Modules. | |
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 | * | |
41 | * Example user-space utilities: http://people.redhat.com/sgrubb/audit/ | |
42 | */ | |
43 | ||
44 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | |
45 | ||
46 | #include <linux/file.h> | |
47 | #include <linux/init.h> | |
48 | #include <linux/types.h> | |
49 | #include <linux/atomic.h> | |
50 | #include <linux/mm.h> | |
51 | #include <linux/export.h> | |
52 | #include <linux/slab.h> | |
53 | #include <linux/err.h> | |
54 | #include <linux/kthread.h> | |
55 | #include <linux/kernel.h> | |
56 | #include <linux/syscalls.h> | |
57 | ||
58 | #include <linux/audit.h> | |
59 | ||
60 | #include <net/sock.h> | |
61 | #include <net/netlink.h> | |
62 | #include <linux/skbuff.h> | |
63 | #ifdef CONFIG_SECURITY | |
64 | #include <linux/security.h> | |
65 | #endif | |
66 | #include <linux/freezer.h> | |
67 | #include <linux/pid_namespace.h> | |
68 | #include <net/netns/generic.h> | |
69 | ||
70 | #include "audit.h" | |
71 | ||
72 | /* No auditing will take place until audit_initialized == AUDIT_INITIALIZED. | |
73 | * (Initialization happens after skb_init is called.) */ | |
74 | #define AUDIT_DISABLED -1 | |
75 | #define AUDIT_UNINITIALIZED 0 | |
76 | #define AUDIT_INITIALIZED 1 | |
77 | static int audit_initialized; | |
78 | ||
79 | #define AUDIT_OFF 0 | |
80 | #define AUDIT_ON 1 | |
81 | #define AUDIT_LOCKED 2 | |
82 | u32 audit_enabled; | |
83 | u32 audit_ever_enabled; | |
84 | ||
85 | EXPORT_SYMBOL_GPL(audit_enabled); | |
86 | ||
87 | /* Default state when kernel boots without any parameters. */ | |
88 | static u32 audit_default; | |
89 | ||
90 | /* If auditing cannot proceed, audit_failure selects what happens. */ | |
91 | static u32 audit_failure = AUDIT_FAIL_PRINTK; | |
92 | ||
93 | /* | |
94 | * If audit records are to be written to the netlink socket, audit_pid | |
95 | * contains the pid of the auditd process and audit_nlk_portid contains | |
96 | * the portid to use to send netlink messages to that process. | |
97 | */ | |
98 | int audit_pid; | |
99 | static __u32 audit_nlk_portid; | |
100 | ||
101 | /* If audit_rate_limit is non-zero, limit the rate of sending audit records | |
102 | * to that number per second. This prevents DoS attacks, but results in | |
103 | * audit records being dropped. */ | |
104 | static u32 audit_rate_limit; | |
105 | ||
106 | /* Number of outstanding audit_buffers allowed. | |
107 | * When set to zero, this means unlimited. */ | |
108 | static u32 audit_backlog_limit = 64; | |
109 | #define AUDIT_BACKLOG_WAIT_TIME (60 * HZ) | |
110 | static u32 audit_backlog_wait_time = AUDIT_BACKLOG_WAIT_TIME; | |
111 | ||
112 | /* The identity of the user shutting down the audit system. */ | |
113 | kuid_t audit_sig_uid = INVALID_UID; | |
114 | pid_t audit_sig_pid = -1; | |
115 | u32 audit_sig_sid = 0; | |
116 | ||
117 | /* Records can be lost in several ways: | |
118 | 0) [suppressed in audit_alloc] | |
119 | 1) out of memory in audit_log_start [kmalloc of struct audit_buffer] | |
120 | 2) out of memory in audit_log_move [alloc_skb] | |
121 | 3) suppressed due to audit_rate_limit | |
122 | 4) suppressed due to audit_backlog_limit | |
123 | */ | |
124 | static atomic_t audit_lost = ATOMIC_INIT(0); | |
125 | ||
126 | /* The netlink socket. */ | |
127 | static struct sock *audit_sock; | |
128 | static unsigned int audit_net_id; | |
129 | ||
130 | /* Hash for inode-based rules */ | |
131 | struct list_head audit_inode_hash[AUDIT_INODE_BUCKETS]; | |
132 | ||
133 | /* The audit_freelist is a list of pre-allocated audit buffers (if more | |
134 | * than AUDIT_MAXFREE are in use, the audit buffer is freed instead of | |
135 | * being placed on the freelist). */ | |
136 | static DEFINE_SPINLOCK(audit_freelist_lock); | |
137 | static int audit_freelist_count; | |
138 | static LIST_HEAD(audit_freelist); | |
139 | ||
140 | /* queue msgs to send via kauditd_task */ | |
141 | static struct sk_buff_head audit_queue; | |
142 | /* queue msgs due to temporary unicast send problems */ | |
143 | static struct sk_buff_head audit_retry_queue; | |
144 | /* queue msgs waiting for new auditd connection */ | |
145 | static struct sk_buff_head audit_hold_queue; | |
146 | ||
147 | /* queue servicing thread */ | |
148 | static struct task_struct *kauditd_task; | |
149 | static DECLARE_WAIT_QUEUE_HEAD(kauditd_wait); | |
150 | ||
151 | /* waitqueue for callers who are blocked on the audit backlog */ | |
152 | static DECLARE_WAIT_QUEUE_HEAD(audit_backlog_wait); | |
153 | ||
154 | static struct audit_features af = {.vers = AUDIT_FEATURE_VERSION, | |
155 | .mask = -1, | |
156 | .features = 0, | |
157 | .lock = 0,}; | |
158 | ||
159 | static char *audit_feature_names[2] = { | |
160 | "only_unset_loginuid", | |
161 | "loginuid_immutable", | |
162 | }; | |
163 | ||
164 | ||
165 | /* Serialize requests from userspace. */ | |
166 | DEFINE_MUTEX(audit_cmd_mutex); | |
167 | ||
168 | /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting | |
169 | * audit records. Since printk uses a 1024 byte buffer, this buffer | |
170 | * should be at least that large. */ | |
171 | #define AUDIT_BUFSIZ 1024 | |
172 | ||
173 | /* AUDIT_MAXFREE is the number of empty audit_buffers we keep on the | |
174 | * audit_freelist. Doing so eliminates many kmalloc/kfree calls. */ | |
175 | #define AUDIT_MAXFREE (2*NR_CPUS) | |
176 | ||
177 | /* The audit_buffer is used when formatting an audit record. The caller | |
178 | * locks briefly to get the record off the freelist or to allocate the | |
179 | * buffer, and locks briefly to send the buffer to the netlink layer or | |
180 | * to place it on a transmit queue. Multiple audit_buffers can be in | |
181 | * use simultaneously. */ | |
182 | struct audit_buffer { | |
183 | struct list_head list; | |
184 | struct sk_buff *skb; /* formatted skb ready to send */ | |
185 | struct audit_context *ctx; /* NULL or associated context */ | |
186 | gfp_t gfp_mask; | |
187 | }; | |
188 | ||
189 | struct audit_reply { | |
190 | __u32 portid; | |
191 | struct net *net; | |
192 | struct sk_buff *skb; | |
193 | }; | |
194 | ||
195 | static void audit_set_portid(struct audit_buffer *ab, __u32 portid) | |
196 | { | |
197 | if (ab) { | |
198 | struct nlmsghdr *nlh = nlmsg_hdr(ab->skb); | |
199 | nlh->nlmsg_pid = portid; | |
200 | } | |
201 | } | |
202 | ||
203 | void audit_panic(const char *message) | |
204 | { | |
205 | switch (audit_failure) { | |
206 | case AUDIT_FAIL_SILENT: | |
207 | break; | |
208 | case AUDIT_FAIL_PRINTK: | |
209 | if (printk_ratelimit()) | |
210 | pr_err("%s\n", message); | |
211 | break; | |
212 | case AUDIT_FAIL_PANIC: | |
213 | /* test audit_pid since printk is always losey, why bother? */ | |
214 | if (audit_pid) | |
215 | panic("audit: %s\n", message); | |
216 | break; | |
217 | } | |
218 | } | |
219 | ||
220 | static inline int audit_rate_check(void) | |
221 | { | |
222 | static unsigned long last_check = 0; | |
223 | static int messages = 0; | |
224 | static DEFINE_SPINLOCK(lock); | |
225 | unsigned long flags; | |
226 | unsigned long now; | |
227 | unsigned long elapsed; | |
228 | int retval = 0; | |
229 | ||
230 | if (!audit_rate_limit) return 1; | |
231 | ||
232 | spin_lock_irqsave(&lock, flags); | |
233 | if (++messages < audit_rate_limit) { | |
234 | retval = 1; | |
235 | } else { | |
236 | now = jiffies; | |
237 | elapsed = now - last_check; | |
238 | if (elapsed > HZ) { | |
239 | last_check = now; | |
240 | messages = 0; | |
241 | retval = 1; | |
242 | } | |
243 | } | |
244 | spin_unlock_irqrestore(&lock, flags); | |
245 | ||
246 | return retval; | |
247 | } | |
248 | ||
249 | /** | |
250 | * audit_log_lost - conditionally log lost audit message event | |
251 | * @message: the message stating reason for lost audit message | |
252 | * | |
253 | * Emit at least 1 message per second, even if audit_rate_check is | |
254 | * throttling. | |
255 | * Always increment the lost messages counter. | |
256 | */ | |
257 | void audit_log_lost(const char *message) | |
258 | { | |
259 | static unsigned long last_msg = 0; | |
260 | static DEFINE_SPINLOCK(lock); | |
261 | unsigned long flags; | |
262 | unsigned long now; | |
263 | int print; | |
264 | ||
265 | atomic_inc(&audit_lost); | |
266 | ||
267 | print = (audit_failure == AUDIT_FAIL_PANIC || !audit_rate_limit); | |
268 | ||
269 | if (!print) { | |
270 | spin_lock_irqsave(&lock, flags); | |
271 | now = jiffies; | |
272 | if (now - last_msg > HZ) { | |
273 | print = 1; | |
274 | last_msg = now; | |
275 | } | |
276 | spin_unlock_irqrestore(&lock, flags); | |
277 | } | |
278 | ||
279 | if (print) { | |
280 | if (printk_ratelimit()) | |
281 | pr_warn("audit_lost=%u audit_rate_limit=%u audit_backlog_limit=%u\n", | |
282 | atomic_read(&audit_lost), | |
283 | audit_rate_limit, | |
284 | audit_backlog_limit); | |
285 | audit_panic(message); | |
286 | } | |
287 | } | |
288 | ||
289 | static int audit_log_config_change(char *function_name, u32 new, u32 old, | |
290 | int allow_changes) | |
291 | { | |
292 | struct audit_buffer *ab; | |
293 | int rc = 0; | |
294 | ||
295 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE); | |
296 | if (unlikely(!ab)) | |
297 | return rc; | |
298 | audit_log_format(ab, "%s=%u old=%u", function_name, new, old); | |
299 | audit_log_session_info(ab); | |
300 | rc = audit_log_task_context(ab); | |
301 | if (rc) | |
302 | allow_changes = 0; /* Something weird, deny request */ | |
303 | audit_log_format(ab, " res=%d", allow_changes); | |
304 | audit_log_end(ab); | |
305 | return rc; | |
306 | } | |
307 | ||
308 | static int audit_do_config_change(char *function_name, u32 *to_change, u32 new) | |
309 | { | |
310 | int allow_changes, rc = 0; | |
311 | u32 old = *to_change; | |
312 | ||
313 | /* check if we are locked */ | |
314 | if (audit_enabled == AUDIT_LOCKED) | |
315 | allow_changes = 0; | |
316 | else | |
317 | allow_changes = 1; | |
318 | ||
319 | if (audit_enabled != AUDIT_OFF) { | |
320 | rc = audit_log_config_change(function_name, new, old, allow_changes); | |
321 | if (rc) | |
322 | allow_changes = 0; | |
323 | } | |
324 | ||
325 | /* If we are allowed, make the change */ | |
326 | if (allow_changes == 1) | |
327 | *to_change = new; | |
328 | /* Not allowed, update reason */ | |
329 | else if (rc == 0) | |
330 | rc = -EPERM; | |
331 | return rc; | |
332 | } | |
333 | ||
334 | static int audit_set_rate_limit(u32 limit) | |
335 | { | |
336 | return audit_do_config_change("audit_rate_limit", &audit_rate_limit, limit); | |
337 | } | |
338 | ||
339 | static int audit_set_backlog_limit(u32 limit) | |
340 | { | |
341 | return audit_do_config_change("audit_backlog_limit", &audit_backlog_limit, limit); | |
342 | } | |
343 | ||
344 | static int audit_set_backlog_wait_time(u32 timeout) | |
345 | { | |
346 | return audit_do_config_change("audit_backlog_wait_time", | |
347 | &audit_backlog_wait_time, timeout); | |
348 | } | |
349 | ||
350 | static int audit_set_enabled(u32 state) | |
351 | { | |
352 | int rc; | |
353 | if (state > AUDIT_LOCKED) | |
354 | return -EINVAL; | |
355 | ||
356 | rc = audit_do_config_change("audit_enabled", &audit_enabled, state); | |
357 | if (!rc) | |
358 | audit_ever_enabled |= !!state; | |
359 | ||
360 | return rc; | |
361 | } | |
362 | ||
363 | static int audit_set_failure(u32 state) | |
364 | { | |
365 | if (state != AUDIT_FAIL_SILENT | |
366 | && state != AUDIT_FAIL_PRINTK | |
367 | && state != AUDIT_FAIL_PANIC) | |
368 | return -EINVAL; | |
369 | ||
370 | return audit_do_config_change("audit_failure", &audit_failure, state); | |
371 | } | |
372 | ||
373 | /* | |
374 | * For one reason or another this nlh isn't getting delivered to the userspace | |
375 | * audit daemon, just send it to printk. | |
376 | */ | |
377 | static void kauditd_printk_skb(struct sk_buff *skb) | |
378 | { | |
379 | struct nlmsghdr *nlh = nlmsg_hdr(skb); | |
380 | char *data = nlmsg_data(nlh); | |
381 | ||
382 | if (nlh->nlmsg_type != AUDIT_EOE) { | |
383 | if (printk_ratelimit()) | |
384 | pr_notice("type=%d %s\n", nlh->nlmsg_type, data); | |
385 | else | |
386 | audit_log_lost("printk limit exceeded"); | |
387 | } | |
388 | } | |
389 | ||
390 | /** | |
391 | * kauditd_hold_skb - Queue an audit record, waiting for auditd | |
392 | * @skb: audit record | |
393 | * | |
394 | * Description: | |
395 | * Queue the audit record, waiting for an instance of auditd. When this | |
396 | * function is called we haven't given up yet on sending the record, but things | |
397 | * are not looking good. The first thing we want to do is try to write the | |
398 | * record via printk and then see if we want to try and hold on to the record | |
399 | * and queue it, if we have room. If we want to hold on to the record, but we | |
400 | * don't have room, record a record lost message. | |
401 | */ | |
402 | static void kauditd_hold_skb(struct sk_buff *skb) | |
403 | { | |
404 | /* at this point it is uncertain if we will ever send this to auditd so | |
405 | * try to send the message via printk before we go any further */ | |
406 | kauditd_printk_skb(skb); | |
407 | ||
408 | /* can we just silently drop the message? */ | |
409 | if (!audit_default) { | |
410 | kfree_skb(skb); | |
411 | return; | |
412 | } | |
413 | ||
414 | /* if we have room, queue the message */ | |
415 | if (!audit_backlog_limit || | |
416 | skb_queue_len(&audit_hold_queue) < audit_backlog_limit) { | |
417 | skb_queue_tail(&audit_hold_queue, skb); | |
418 | return; | |
419 | } | |
420 | ||
421 | /* we have no other options - drop the message */ | |
422 | audit_log_lost("kauditd hold queue overflow"); | |
423 | kfree_skb(skb); | |
424 | } | |
425 | ||
426 | /** | |
427 | * kauditd_retry_skb - Queue an audit record, attempt to send again to auditd | |
428 | * @skb: audit record | |
429 | * | |
430 | * Description: | |
431 | * Not as serious as kauditd_hold_skb() as we still have a connected auditd, | |
432 | * but for some reason we are having problems sending it audit records so | |
433 | * queue the given record and attempt to resend. | |
434 | */ | |
435 | static void kauditd_retry_skb(struct sk_buff *skb) | |
436 | { | |
437 | /* NOTE: because records should only live in the retry queue for a | |
438 | * short period of time, before either being sent or moved to the hold | |
439 | * queue, we don't currently enforce a limit on this queue */ | |
440 | skb_queue_tail(&audit_retry_queue, skb); | |
441 | } | |
442 | ||
443 | /** | |
444 | * auditd_reset - Disconnect the auditd connection | |
445 | * | |
446 | * Description: | |
447 | * Break the auditd/kauditd connection and move all the records in the retry | |
448 | * queue into the hold queue in case auditd reconnects. The audit_cmd_mutex | |
449 | * must be held when calling this function. | |
450 | */ | |
451 | static void auditd_reset(void) | |
452 | { | |
453 | struct sk_buff *skb; | |
454 | ||
455 | /* break the connection */ | |
456 | if (audit_sock) { | |
457 | sock_put(audit_sock); | |
458 | audit_sock = NULL; | |
459 | } | |
460 | audit_pid = 0; | |
461 | audit_nlk_portid = 0; | |
462 | ||
463 | /* flush all of the retry queue to the hold queue */ | |
464 | while ((skb = skb_dequeue(&audit_retry_queue))) | |
465 | kauditd_hold_skb(skb); | |
466 | } | |
467 | ||
468 | /** | |
469 | * kauditd_send_unicast_skb - Send a record via unicast to auditd | |
470 | * @skb: audit record | |
471 | */ | |
472 | static int kauditd_send_unicast_skb(struct sk_buff *skb) | |
473 | { | |
474 | int rc; | |
475 | ||
476 | /* if we know nothing is connected, don't even try the netlink call */ | |
477 | if (!audit_pid) | |
478 | return -ECONNREFUSED; | |
479 | ||
480 | /* get an extra skb reference in case we fail to send */ | |
481 | skb_get(skb); | |
482 | rc = netlink_unicast(audit_sock, skb, audit_nlk_portid, 0); | |
483 | if (rc >= 0) { | |
484 | consume_skb(skb); | |
485 | rc = 0; | |
486 | } | |
487 | ||
488 | return rc; | |
489 | } | |
490 | ||
491 | /* | |
492 | * kauditd_send_multicast_skb - Send a record to any multicast listeners | |
493 | * @skb: audit record | |
494 | * | |
495 | * Description: | |
496 | * This function doesn't consume an skb as might be expected since it has to | |
497 | * copy it anyways. | |
498 | */ | |
499 | static void kauditd_send_multicast_skb(struct sk_buff *skb) | |
500 | { | |
501 | struct sk_buff *copy; | |
502 | struct audit_net *aunet = net_generic(&init_net, audit_net_id); | |
503 | struct sock *sock = aunet->nlsk; | |
504 | struct nlmsghdr *nlh; | |
505 | ||
506 | if (!netlink_has_listeners(sock, AUDIT_NLGRP_READLOG)) | |
507 | return; | |
508 | ||
509 | /* | |
510 | * The seemingly wasteful skb_copy() rather than bumping the refcount | |
511 | * using skb_get() is necessary because non-standard mods are made to | |
512 | * the skb by the original kaudit unicast socket send routine. The | |
513 | * existing auditd daemon assumes this breakage. Fixing this would | |
514 | * require co-ordinating a change in the established protocol between | |
515 | * the kaudit kernel subsystem and the auditd userspace code. There is | |
516 | * no reason for new multicast clients to continue with this | |
517 | * non-compliance. | |
518 | */ | |
519 | copy = skb_copy(skb, GFP_KERNEL); | |
520 | if (!copy) | |
521 | return; | |
522 | nlh = nlmsg_hdr(copy); | |
523 | nlh->nlmsg_len = skb->len; | |
524 | ||
525 | nlmsg_multicast(sock, copy, 0, AUDIT_NLGRP_READLOG, GFP_KERNEL); | |
526 | } | |
527 | ||
528 | /** | |
529 | * kauditd_wake_condition - Return true when it is time to wake kauditd_thread | |
530 | * | |
531 | * Description: | |
532 | * This function is for use by the wait_event_freezable() call in | |
533 | * kauditd_thread(). | |
534 | */ | |
535 | static int kauditd_wake_condition(void) | |
536 | { | |
537 | static int pid_last = 0; | |
538 | int rc; | |
539 | int pid = audit_pid; | |
540 | ||
541 | /* wake on new messages or a change in the connected auditd */ | |
542 | rc = skb_queue_len(&audit_queue) || (pid && pid != pid_last); | |
543 | if (rc) | |
544 | pid_last = pid; | |
545 | ||
546 | return rc; | |
547 | } | |
548 | ||
549 | static int kauditd_thread(void *dummy) | |
550 | { | |
551 | int rc; | |
552 | int auditd = 0; | |
553 | int reschedule = 0; | |
554 | struct sk_buff *skb; | |
555 | struct nlmsghdr *nlh; | |
556 | ||
557 | #define UNICAST_RETRIES 5 | |
558 | #define AUDITD_BAD(x,y) \ | |
559 | ((x) == -ECONNREFUSED || (x) == -EPERM || ++(y) >= UNICAST_RETRIES) | |
560 | ||
561 | /* NOTE: we do invalidate the auditd connection flag on any sending | |
562 | * errors, but we only "restore" the connection flag at specific places | |
563 | * in the loop in order to help ensure proper ordering of audit | |
564 | * records */ | |
565 | ||
566 | set_freezable(); | |
567 | while (!kthread_should_stop()) { | |
568 | /* NOTE: possible area for future improvement is to look at | |
569 | * the hold and retry queues, since only this thread | |
570 | * has access to these queues we might be able to do | |
571 | * our own queuing and skip some/all of the locking */ | |
572 | ||
573 | /* NOTE: it might be a fun experiment to split the hold and | |
574 | * retry queue handling to another thread, but the | |
575 | * synchronization issues and other overhead might kill | |
576 | * any performance gains */ | |
577 | ||
578 | /* attempt to flush the hold queue */ | |
579 | while (auditd && (skb = skb_dequeue(&audit_hold_queue))) { | |
580 | rc = kauditd_send_unicast_skb(skb); | |
581 | if (rc) { | |
582 | /* requeue to the same spot */ | |
583 | skb_queue_head(&audit_hold_queue, skb); | |
584 | ||
585 | auditd = 0; | |
586 | if (AUDITD_BAD(rc, reschedule)) { | |
587 | mutex_lock(&audit_cmd_mutex); | |
588 | auditd_reset(); | |
589 | mutex_unlock(&audit_cmd_mutex); | |
590 | reschedule = 0; | |
591 | } | |
592 | } else | |
593 | /* we were able to send successfully */ | |
594 | reschedule = 0; | |
595 | } | |
596 | ||
597 | /* attempt to flush the retry queue */ | |
598 | while (auditd && (skb = skb_dequeue(&audit_retry_queue))) { | |
599 | rc = kauditd_send_unicast_skb(skb); | |
600 | if (rc) { | |
601 | auditd = 0; | |
602 | if (AUDITD_BAD(rc, reschedule)) { | |
603 | kauditd_hold_skb(skb); | |
604 | mutex_lock(&audit_cmd_mutex); | |
605 | auditd_reset(); | |
606 | mutex_unlock(&audit_cmd_mutex); | |
607 | reschedule = 0; | |
608 | } else | |
609 | /* temporary problem (we hope), queue | |
610 | * to the same spot and retry */ | |
611 | skb_queue_head(&audit_retry_queue, skb); | |
612 | } else | |
613 | /* we were able to send successfully */ | |
614 | reschedule = 0; | |
615 | } | |
616 | ||
617 | /* standard queue processing, try to be as quick as possible */ | |
618 | quick_loop: | |
619 | skb = skb_dequeue(&audit_queue); | |
620 | if (skb) { | |
621 | /* setup the netlink header, see the comments in | |
622 | * kauditd_send_multicast_skb() for length quirks */ | |
623 | nlh = nlmsg_hdr(skb); | |
624 | nlh->nlmsg_len = skb->len - NLMSG_HDRLEN; | |
625 | ||
626 | /* attempt to send to any multicast listeners */ | |
627 | kauditd_send_multicast_skb(skb); | |
628 | ||
629 | /* attempt to send to auditd, queue on failure */ | |
630 | if (auditd) { | |
631 | rc = kauditd_send_unicast_skb(skb); | |
632 | if (rc) { | |
633 | auditd = 0; | |
634 | if (AUDITD_BAD(rc, reschedule)) { | |
635 | mutex_lock(&audit_cmd_mutex); | |
636 | auditd_reset(); | |
637 | mutex_unlock(&audit_cmd_mutex); | |
638 | reschedule = 0; | |
639 | } | |
640 | ||
641 | /* move to the retry queue */ | |
642 | kauditd_retry_skb(skb); | |
643 | } else | |
644 | /* everything is working so go fast! */ | |
645 | goto quick_loop; | |
646 | } else if (reschedule) | |
647 | /* we are currently having problems, move to | |
648 | * the retry queue */ | |
649 | kauditd_retry_skb(skb); | |
650 | else | |
651 | /* dump the message via printk and hold it */ | |
652 | kauditd_hold_skb(skb); | |
653 | } else { | |
654 | /* we have flushed the backlog so wake everyone */ | |
655 | wake_up(&audit_backlog_wait); | |
656 | ||
657 | /* if everything is okay with auditd (if present), go | |
658 | * to sleep until there is something new in the queue | |
659 | * or we have a change in the connected auditd; | |
660 | * otherwise simply reschedule to give things a chance | |
661 | * to recover */ | |
662 | if (reschedule) { | |
663 | set_current_state(TASK_INTERRUPTIBLE); | |
664 | schedule(); | |
665 | } else | |
666 | wait_event_freezable(kauditd_wait, | |
667 | kauditd_wake_condition()); | |
668 | ||
669 | /* update the auditd connection status */ | |
670 | auditd = (audit_pid ? 1 : 0); | |
671 | } | |
672 | } | |
673 | ||
674 | return 0; | |
675 | } | |
676 | ||
677 | int audit_send_list(void *_dest) | |
678 | { | |
679 | struct audit_netlink_list *dest = _dest; | |
680 | struct sk_buff *skb; | |
681 | struct net *net = dest->net; | |
682 | struct audit_net *aunet = net_generic(net, audit_net_id); | |
683 | ||
684 | /* wait for parent to finish and send an ACK */ | |
685 | mutex_lock(&audit_cmd_mutex); | |
686 | mutex_unlock(&audit_cmd_mutex); | |
687 | ||
688 | while ((skb = __skb_dequeue(&dest->q)) != NULL) | |
689 | netlink_unicast(aunet->nlsk, skb, dest->portid, 0); | |
690 | ||
691 | put_net(net); | |
692 | kfree(dest); | |
693 | ||
694 | return 0; | |
695 | } | |
696 | ||
697 | struct sk_buff *audit_make_reply(__u32 portid, int seq, int type, int done, | |
698 | int multi, const void *payload, int size) | |
699 | { | |
700 | struct sk_buff *skb; | |
701 | struct nlmsghdr *nlh; | |
702 | void *data; | |
703 | int flags = multi ? NLM_F_MULTI : 0; | |
704 | int t = done ? NLMSG_DONE : type; | |
705 | ||
706 | skb = nlmsg_new(size, GFP_KERNEL); | |
707 | if (!skb) | |
708 | return NULL; | |
709 | ||
710 | nlh = nlmsg_put(skb, portid, seq, t, size, flags); | |
711 | if (!nlh) | |
712 | goto out_kfree_skb; | |
713 | data = nlmsg_data(nlh); | |
714 | memcpy(data, payload, size); | |
715 | return skb; | |
716 | ||
717 | out_kfree_skb: | |
718 | kfree_skb(skb); | |
719 | return NULL; | |
720 | } | |
721 | ||
722 | static int audit_send_reply_thread(void *arg) | |
723 | { | |
724 | struct audit_reply *reply = (struct audit_reply *)arg; | |
725 | struct net *net = reply->net; | |
726 | struct audit_net *aunet = net_generic(net, audit_net_id); | |
727 | ||
728 | mutex_lock(&audit_cmd_mutex); | |
729 | mutex_unlock(&audit_cmd_mutex); | |
730 | ||
731 | /* Ignore failure. It'll only happen if the sender goes away, | |
732 | because our timeout is set to infinite. */ | |
733 | netlink_unicast(aunet->nlsk , reply->skb, reply->portid, 0); | |
734 | put_net(net); | |
735 | kfree(reply); | |
736 | return 0; | |
737 | } | |
738 | ||
739 | /** | |
740 | * audit_send_reply - send an audit reply message via netlink | |
741 | * @request_skb: skb of request we are replying to (used to target the reply) | |
742 | * @seq: sequence number | |
743 | * @type: audit message type | |
744 | * @done: done (last) flag | |
745 | * @multi: multi-part message flag | |
746 | * @payload: payload data | |
747 | * @size: payload size | |
748 | * | |
749 | * Allocates an skb, builds the netlink message, and sends it to the port id. | |
750 | * No failure notifications. | |
751 | */ | |
752 | static void audit_send_reply(struct sk_buff *request_skb, int seq, int type, int done, | |
753 | int multi, const void *payload, int size) | |
754 | { | |
755 | u32 portid = NETLINK_CB(request_skb).portid; | |
756 | struct net *net = sock_net(NETLINK_CB(request_skb).sk); | |
757 | struct sk_buff *skb; | |
758 | struct task_struct *tsk; | |
759 | struct audit_reply *reply = kmalloc(sizeof(struct audit_reply), | |
760 | GFP_KERNEL); | |
761 | ||
762 | if (!reply) | |
763 | return; | |
764 | ||
765 | skb = audit_make_reply(portid, seq, type, done, multi, payload, size); | |
766 | if (!skb) | |
767 | goto out; | |
768 | ||
769 | reply->net = get_net(net); | |
770 | reply->portid = portid; | |
771 | reply->skb = skb; | |
772 | ||
773 | tsk = kthread_run(audit_send_reply_thread, reply, "audit_send_reply"); | |
774 | if (!IS_ERR(tsk)) | |
775 | return; | |
776 | kfree_skb(skb); | |
777 | out: | |
778 | kfree(reply); | |
779 | } | |
780 | ||
781 | /* | |
782 | * Check for appropriate CAP_AUDIT_ capabilities on incoming audit | |
783 | * control messages. | |
784 | */ | |
785 | static int audit_netlink_ok(struct sk_buff *skb, u16 msg_type) | |
786 | { | |
787 | int err = 0; | |
788 | ||
789 | /* Only support initial user namespace for now. */ | |
790 | /* | |
791 | * We return ECONNREFUSED because it tricks userspace into thinking | |
792 | * that audit was not configured into the kernel. Lots of users | |
793 | * configure their PAM stack (because that's what the distro does) | |
794 | * to reject login if unable to send messages to audit. If we return | |
795 | * ECONNREFUSED the PAM stack thinks the kernel does not have audit | |
796 | * configured in and will let login proceed. If we return EPERM | |
797 | * userspace will reject all logins. This should be removed when we | |
798 | * support non init namespaces!! | |
799 | */ | |
800 | if (current_user_ns() != &init_user_ns) | |
801 | return -ECONNREFUSED; | |
802 | ||
803 | switch (msg_type) { | |
804 | case AUDIT_LIST: | |
805 | case AUDIT_ADD: | |
806 | case AUDIT_DEL: | |
807 | return -EOPNOTSUPP; | |
808 | case AUDIT_GET: | |
809 | case AUDIT_SET: | |
810 | case AUDIT_GET_FEATURE: | |
811 | case AUDIT_SET_FEATURE: | |
812 | case AUDIT_LIST_RULES: | |
813 | case AUDIT_ADD_RULE: | |
814 | case AUDIT_DEL_RULE: | |
815 | case AUDIT_SIGNAL_INFO: | |
816 | case AUDIT_TTY_GET: | |
817 | case AUDIT_TTY_SET: | |
818 | case AUDIT_TRIM: | |
819 | case AUDIT_MAKE_EQUIV: | |
820 | /* Only support auditd and auditctl in initial pid namespace | |
821 | * for now. */ | |
822 | if (task_active_pid_ns(current) != &init_pid_ns) | |
823 | return -EPERM; | |
824 | ||
825 | if (!netlink_capable(skb, CAP_AUDIT_CONTROL)) | |
826 | err = -EPERM; | |
827 | break; | |
828 | case AUDIT_USER: | |
829 | case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG: | |
830 | case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2: | |
831 | if (!netlink_capable(skb, CAP_AUDIT_WRITE)) | |
832 | err = -EPERM; | |
833 | break; | |
834 | default: /* bad msg */ | |
835 | err = -EINVAL; | |
836 | } | |
837 | ||
838 | return err; | |
839 | } | |
840 | ||
841 | static void audit_log_common_recv_msg(struct audit_buffer **ab, u16 msg_type) | |
842 | { | |
843 | uid_t uid = from_kuid(&init_user_ns, current_uid()); | |
844 | pid_t pid = task_tgid_nr(current); | |
845 | ||
846 | if (!audit_enabled && msg_type != AUDIT_USER_AVC) { | |
847 | *ab = NULL; | |
848 | return; | |
849 | } | |
850 | ||
851 | *ab = audit_log_start(NULL, GFP_KERNEL, msg_type); | |
852 | if (unlikely(!*ab)) | |
853 | return; | |
854 | audit_log_format(*ab, "pid=%d uid=%u", pid, uid); | |
855 | audit_log_session_info(*ab); | |
856 | audit_log_task_context(*ab); | |
857 | } | |
858 | ||
859 | int is_audit_feature_set(int i) | |
860 | { | |
861 | return af.features & AUDIT_FEATURE_TO_MASK(i); | |
862 | } | |
863 | ||
864 | ||
865 | static int audit_get_feature(struct sk_buff *skb) | |
866 | { | |
867 | u32 seq; | |
868 | ||
869 | seq = nlmsg_hdr(skb)->nlmsg_seq; | |
870 | ||
871 | audit_send_reply(skb, seq, AUDIT_GET_FEATURE, 0, 0, &af, sizeof(af)); | |
872 | ||
873 | return 0; | |
874 | } | |
875 | ||
876 | static void audit_log_feature_change(int which, u32 old_feature, u32 new_feature, | |
877 | u32 old_lock, u32 new_lock, int res) | |
878 | { | |
879 | struct audit_buffer *ab; | |
880 | ||
881 | if (audit_enabled == AUDIT_OFF) | |
882 | return; | |
883 | ||
884 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_FEATURE_CHANGE); | |
885 | audit_log_task_info(ab, current); | |
886 | audit_log_format(ab, " feature=%s old=%u new=%u old_lock=%u new_lock=%u res=%d", | |
887 | audit_feature_names[which], !!old_feature, !!new_feature, | |
888 | !!old_lock, !!new_lock, res); | |
889 | audit_log_end(ab); | |
890 | } | |
891 | ||
892 | static int audit_set_feature(struct sk_buff *skb) | |
893 | { | |
894 | struct audit_features *uaf; | |
895 | int i; | |
896 | ||
897 | BUILD_BUG_ON(AUDIT_LAST_FEATURE + 1 > ARRAY_SIZE(audit_feature_names)); | |
898 | uaf = nlmsg_data(nlmsg_hdr(skb)); | |
899 | ||
900 | /* if there is ever a version 2 we should handle that here */ | |
901 | ||
902 | for (i = 0; i <= AUDIT_LAST_FEATURE; i++) { | |
903 | u32 feature = AUDIT_FEATURE_TO_MASK(i); | |
904 | u32 old_feature, new_feature, old_lock, new_lock; | |
905 | ||
906 | /* if we are not changing this feature, move along */ | |
907 | if (!(feature & uaf->mask)) | |
908 | continue; | |
909 | ||
910 | old_feature = af.features & feature; | |
911 | new_feature = uaf->features & feature; | |
912 | new_lock = (uaf->lock | af.lock) & feature; | |
913 | old_lock = af.lock & feature; | |
914 | ||
915 | /* are we changing a locked feature? */ | |
916 | if (old_lock && (new_feature != old_feature)) { | |
917 | audit_log_feature_change(i, old_feature, new_feature, | |
918 | old_lock, new_lock, 0); | |
919 | return -EPERM; | |
920 | } | |
921 | } | |
922 | /* nothing invalid, do the changes */ | |
923 | for (i = 0; i <= AUDIT_LAST_FEATURE; i++) { | |
924 | u32 feature = AUDIT_FEATURE_TO_MASK(i); | |
925 | u32 old_feature, new_feature, old_lock, new_lock; | |
926 | ||
927 | /* if we are not changing this feature, move along */ | |
928 | if (!(feature & uaf->mask)) | |
929 | continue; | |
930 | ||
931 | old_feature = af.features & feature; | |
932 | new_feature = uaf->features & feature; | |
933 | old_lock = af.lock & feature; | |
934 | new_lock = (uaf->lock | af.lock) & feature; | |
935 | ||
936 | if (new_feature != old_feature) | |
937 | audit_log_feature_change(i, old_feature, new_feature, | |
938 | old_lock, new_lock, 1); | |
939 | ||
940 | if (new_feature) | |
941 | af.features |= feature; | |
942 | else | |
943 | af.features &= ~feature; | |
944 | af.lock |= new_lock; | |
945 | } | |
946 | ||
947 | return 0; | |
948 | } | |
949 | ||
950 | static int audit_replace(pid_t pid) | |
951 | { | |
952 | struct sk_buff *skb = audit_make_reply(0, 0, AUDIT_REPLACE, 0, 0, | |
953 | &pid, sizeof(pid)); | |
954 | ||
955 | if (!skb) | |
956 | return -ENOMEM; | |
957 | return netlink_unicast(audit_sock, skb, audit_nlk_portid, 0); | |
958 | } | |
959 | ||
960 | static int audit_receive_msg(struct sk_buff *skb, struct nlmsghdr *nlh) | |
961 | { | |
962 | u32 seq; | |
963 | void *data; | |
964 | int err; | |
965 | struct audit_buffer *ab; | |
966 | u16 msg_type = nlh->nlmsg_type; | |
967 | struct audit_sig_info *sig_data; | |
968 | char *ctx = NULL; | |
969 | u32 len; | |
970 | ||
971 | err = audit_netlink_ok(skb, msg_type); | |
972 | if (err) | |
973 | return err; | |
974 | ||
975 | seq = nlh->nlmsg_seq; | |
976 | data = nlmsg_data(nlh); | |
977 | ||
978 | switch (msg_type) { | |
979 | case AUDIT_GET: { | |
980 | struct audit_status s; | |
981 | memset(&s, 0, sizeof(s)); | |
982 | s.enabled = audit_enabled; | |
983 | s.failure = audit_failure; | |
984 | s.pid = audit_pid; | |
985 | s.rate_limit = audit_rate_limit; | |
986 | s.backlog_limit = audit_backlog_limit; | |
987 | s.lost = atomic_read(&audit_lost); | |
988 | s.backlog = skb_queue_len(&audit_queue); | |
989 | s.feature_bitmap = AUDIT_FEATURE_BITMAP_ALL; | |
990 | s.backlog_wait_time = audit_backlog_wait_time; | |
991 | audit_send_reply(skb, seq, AUDIT_GET, 0, 0, &s, sizeof(s)); | |
992 | break; | |
993 | } | |
994 | case AUDIT_SET: { | |
995 | struct audit_status s; | |
996 | memset(&s, 0, sizeof(s)); | |
997 | /* guard against past and future API changes */ | |
998 | memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh))); | |
999 | if (s.mask & AUDIT_STATUS_ENABLED) { | |
1000 | err = audit_set_enabled(s.enabled); | |
1001 | if (err < 0) | |
1002 | return err; | |
1003 | } | |
1004 | if (s.mask & AUDIT_STATUS_FAILURE) { | |
1005 | err = audit_set_failure(s.failure); | |
1006 | if (err < 0) | |
1007 | return err; | |
1008 | } | |
1009 | if (s.mask & AUDIT_STATUS_PID) { | |
1010 | /* NOTE: we are using task_tgid_vnr() below because | |
1011 | * the s.pid value is relative to the namespace | |
1012 | * of the caller; at present this doesn't matter | |
1013 | * much since you can really only run auditd | |
1014 | * from the initial pid namespace, but something | |
1015 | * to keep in mind if this changes */ | |
1016 | int new_pid = s.pid; | |
1017 | pid_t requesting_pid = task_tgid_vnr(current); | |
1018 | ||
1019 | if ((!new_pid) && (requesting_pid != audit_pid)) { | |
1020 | audit_log_config_change("audit_pid", new_pid, audit_pid, 0); | |
1021 | return -EACCES; | |
1022 | } | |
1023 | if (audit_pid && new_pid && | |
1024 | audit_replace(requesting_pid) != -ECONNREFUSED) { | |
1025 | audit_log_config_change("audit_pid", new_pid, audit_pid, 0); | |
1026 | return -EEXIST; | |
1027 | } | |
1028 | if (audit_enabled != AUDIT_OFF) | |
1029 | audit_log_config_change("audit_pid", new_pid, audit_pid, 1); | |
1030 | if (new_pid) { | |
1031 | if (audit_sock) | |
1032 | sock_put(audit_sock); | |
1033 | audit_pid = new_pid; | |
1034 | audit_nlk_portid = NETLINK_CB(skb).portid; | |
1035 | sock_hold(skb->sk); | |
1036 | audit_sock = skb->sk; | |
1037 | } else { | |
1038 | auditd_reset(); | |
1039 | } | |
1040 | wake_up_interruptible(&kauditd_wait); | |
1041 | } | |
1042 | if (s.mask & AUDIT_STATUS_RATE_LIMIT) { | |
1043 | err = audit_set_rate_limit(s.rate_limit); | |
1044 | if (err < 0) | |
1045 | return err; | |
1046 | } | |
1047 | if (s.mask & AUDIT_STATUS_BACKLOG_LIMIT) { | |
1048 | err = audit_set_backlog_limit(s.backlog_limit); | |
1049 | if (err < 0) | |
1050 | return err; | |
1051 | } | |
1052 | if (s.mask & AUDIT_STATUS_BACKLOG_WAIT_TIME) { | |
1053 | if (sizeof(s) > (size_t)nlh->nlmsg_len) | |
1054 | return -EINVAL; | |
1055 | if (s.backlog_wait_time > 10*AUDIT_BACKLOG_WAIT_TIME) | |
1056 | return -EINVAL; | |
1057 | err = audit_set_backlog_wait_time(s.backlog_wait_time); | |
1058 | if (err < 0) | |
1059 | return err; | |
1060 | } | |
1061 | if (s.mask == AUDIT_STATUS_LOST) { | |
1062 | u32 lost = atomic_xchg(&audit_lost, 0); | |
1063 | ||
1064 | audit_log_config_change("lost", 0, lost, 1); | |
1065 | return lost; | |
1066 | } | |
1067 | break; | |
1068 | } | |
1069 | case AUDIT_GET_FEATURE: | |
1070 | err = audit_get_feature(skb); | |
1071 | if (err) | |
1072 | return err; | |
1073 | break; | |
1074 | case AUDIT_SET_FEATURE: | |
1075 | err = audit_set_feature(skb); | |
1076 | if (err) | |
1077 | return err; | |
1078 | break; | |
1079 | case AUDIT_USER: | |
1080 | case AUDIT_FIRST_USER_MSG ... AUDIT_LAST_USER_MSG: | |
1081 | case AUDIT_FIRST_USER_MSG2 ... AUDIT_LAST_USER_MSG2: | |
1082 | if (!audit_enabled && msg_type != AUDIT_USER_AVC) | |
1083 | return 0; | |
1084 | ||
1085 | err = audit_filter(msg_type, AUDIT_FILTER_USER); | |
1086 | if (err == 1) { /* match or error */ | |
1087 | err = 0; | |
1088 | if (msg_type == AUDIT_USER_TTY) { | |
1089 | err = tty_audit_push(); | |
1090 | if (err) | |
1091 | break; | |
1092 | } | |
1093 | mutex_unlock(&audit_cmd_mutex); | |
1094 | audit_log_common_recv_msg(&ab, msg_type); | |
1095 | if (msg_type != AUDIT_USER_TTY) | |
1096 | audit_log_format(ab, " msg='%.*s'", | |
1097 | AUDIT_MESSAGE_TEXT_MAX, | |
1098 | (char *)data); | |
1099 | else { | |
1100 | int size; | |
1101 | ||
1102 | audit_log_format(ab, " data="); | |
1103 | size = nlmsg_len(nlh); | |
1104 | if (size > 0 && | |
1105 | ((unsigned char *)data)[size - 1] == '\0') | |
1106 | size--; | |
1107 | audit_log_n_untrustedstring(ab, data, size); | |
1108 | } | |
1109 | audit_set_portid(ab, NETLINK_CB(skb).portid); | |
1110 | audit_log_end(ab); | |
1111 | mutex_lock(&audit_cmd_mutex); | |
1112 | } | |
1113 | break; | |
1114 | case AUDIT_ADD_RULE: | |
1115 | case AUDIT_DEL_RULE: | |
1116 | if (nlmsg_len(nlh) < sizeof(struct audit_rule_data)) | |
1117 | return -EINVAL; | |
1118 | if (audit_enabled == AUDIT_LOCKED) { | |
1119 | audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE); | |
1120 | audit_log_format(ab, " audit_enabled=%d res=0", audit_enabled); | |
1121 | audit_log_end(ab); | |
1122 | return -EPERM; | |
1123 | } | |
1124 | err = audit_rule_change(msg_type, NETLINK_CB(skb).portid, | |
1125 | seq, data, nlmsg_len(nlh)); | |
1126 | break; | |
1127 | case AUDIT_LIST_RULES: | |
1128 | err = audit_list_rules_send(skb, seq); | |
1129 | break; | |
1130 | case AUDIT_TRIM: | |
1131 | audit_trim_trees(); | |
1132 | audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE); | |
1133 | audit_log_format(ab, " op=trim res=1"); | |
1134 | audit_log_end(ab); | |
1135 | break; | |
1136 | case AUDIT_MAKE_EQUIV: { | |
1137 | void *bufp = data; | |
1138 | u32 sizes[2]; | |
1139 | size_t msglen = nlmsg_len(nlh); | |
1140 | char *old, *new; | |
1141 | ||
1142 | err = -EINVAL; | |
1143 | if (msglen < 2 * sizeof(u32)) | |
1144 | break; | |
1145 | memcpy(sizes, bufp, 2 * sizeof(u32)); | |
1146 | bufp += 2 * sizeof(u32); | |
1147 | msglen -= 2 * sizeof(u32); | |
1148 | old = audit_unpack_string(&bufp, &msglen, sizes[0]); | |
1149 | if (IS_ERR(old)) { | |
1150 | err = PTR_ERR(old); | |
1151 | break; | |
1152 | } | |
1153 | new = audit_unpack_string(&bufp, &msglen, sizes[1]); | |
1154 | if (IS_ERR(new)) { | |
1155 | err = PTR_ERR(new); | |
1156 | kfree(old); | |
1157 | break; | |
1158 | } | |
1159 | /* OK, here comes... */ | |
1160 | err = audit_tag_tree(old, new); | |
1161 | ||
1162 | audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE); | |
1163 | ||
1164 | audit_log_format(ab, " op=make_equiv old="); | |
1165 | audit_log_untrustedstring(ab, old); | |
1166 | audit_log_format(ab, " new="); | |
1167 | audit_log_untrustedstring(ab, new); | |
1168 | audit_log_format(ab, " res=%d", !err); | |
1169 | audit_log_end(ab); | |
1170 | kfree(old); | |
1171 | kfree(new); | |
1172 | break; | |
1173 | } | |
1174 | case AUDIT_SIGNAL_INFO: | |
1175 | len = 0; | |
1176 | if (audit_sig_sid) { | |
1177 | err = security_secid_to_secctx(audit_sig_sid, &ctx, &len); | |
1178 | if (err) | |
1179 | return err; | |
1180 | } | |
1181 | sig_data = kmalloc(sizeof(*sig_data) + len, GFP_KERNEL); | |
1182 | if (!sig_data) { | |
1183 | if (audit_sig_sid) | |
1184 | security_release_secctx(ctx, len); | |
1185 | return -ENOMEM; | |
1186 | } | |
1187 | sig_data->uid = from_kuid(&init_user_ns, audit_sig_uid); | |
1188 | sig_data->pid = audit_sig_pid; | |
1189 | if (audit_sig_sid) { | |
1190 | memcpy(sig_data->ctx, ctx, len); | |
1191 | security_release_secctx(ctx, len); | |
1192 | } | |
1193 | audit_send_reply(skb, seq, AUDIT_SIGNAL_INFO, 0, 0, | |
1194 | sig_data, sizeof(*sig_data) + len); | |
1195 | kfree(sig_data); | |
1196 | break; | |
1197 | case AUDIT_TTY_GET: { | |
1198 | struct audit_tty_status s; | |
1199 | unsigned int t; | |
1200 | ||
1201 | t = READ_ONCE(current->signal->audit_tty); | |
1202 | s.enabled = t & AUDIT_TTY_ENABLE; | |
1203 | s.log_passwd = !!(t & AUDIT_TTY_LOG_PASSWD); | |
1204 | ||
1205 | audit_send_reply(skb, seq, AUDIT_TTY_GET, 0, 0, &s, sizeof(s)); | |
1206 | break; | |
1207 | } | |
1208 | case AUDIT_TTY_SET: { | |
1209 | struct audit_tty_status s, old; | |
1210 | struct audit_buffer *ab; | |
1211 | unsigned int t; | |
1212 | ||
1213 | memset(&s, 0, sizeof(s)); | |
1214 | /* guard against past and future API changes */ | |
1215 | memcpy(&s, data, min_t(size_t, sizeof(s), nlmsg_len(nlh))); | |
1216 | /* check if new data is valid */ | |
1217 | if ((s.enabled != 0 && s.enabled != 1) || | |
1218 | (s.log_passwd != 0 && s.log_passwd != 1)) | |
1219 | err = -EINVAL; | |
1220 | ||
1221 | if (err) | |
1222 | t = READ_ONCE(current->signal->audit_tty); | |
1223 | else { | |
1224 | t = s.enabled | (-s.log_passwd & AUDIT_TTY_LOG_PASSWD); | |
1225 | t = xchg(¤t->signal->audit_tty, t); | |
1226 | } | |
1227 | old.enabled = t & AUDIT_TTY_ENABLE; | |
1228 | old.log_passwd = !!(t & AUDIT_TTY_LOG_PASSWD); | |
1229 | ||
1230 | audit_log_common_recv_msg(&ab, AUDIT_CONFIG_CHANGE); | |
1231 | audit_log_format(ab, " op=tty_set old-enabled=%d new-enabled=%d" | |
1232 | " old-log_passwd=%d new-log_passwd=%d res=%d", | |
1233 | old.enabled, s.enabled, old.log_passwd, | |
1234 | s.log_passwd, !err); | |
1235 | audit_log_end(ab); | |
1236 | break; | |
1237 | } | |
1238 | default: | |
1239 | err = -EINVAL; | |
1240 | break; | |
1241 | } | |
1242 | ||
1243 | return err < 0 ? err : 0; | |
1244 | } | |
1245 | ||
1246 | /* | |
1247 | * Get message from skb. Each message is processed by audit_receive_msg. | |
1248 | * Malformed skbs with wrong length are discarded silently. | |
1249 | */ | |
1250 | static void audit_receive_skb(struct sk_buff *skb) | |
1251 | { | |
1252 | struct nlmsghdr *nlh; | |
1253 | /* | |
1254 | * len MUST be signed for nlmsg_next to be able to dec it below 0 | |
1255 | * if the nlmsg_len was not aligned | |
1256 | */ | |
1257 | int len; | |
1258 | int err; | |
1259 | ||
1260 | nlh = nlmsg_hdr(skb); | |
1261 | len = skb->len; | |
1262 | ||
1263 | while (nlmsg_ok(nlh, len)) { | |
1264 | err = audit_receive_msg(skb, nlh); | |
1265 | /* if err or if this message says it wants a response */ | |
1266 | if (err || (nlh->nlmsg_flags & NLM_F_ACK)) | |
1267 | netlink_ack(skb, nlh, err); | |
1268 | ||
1269 | nlh = nlmsg_next(nlh, &len); | |
1270 | } | |
1271 | } | |
1272 | ||
1273 | /* Receive messages from netlink socket. */ | |
1274 | static void audit_receive(struct sk_buff *skb) | |
1275 | { | |
1276 | mutex_lock(&audit_cmd_mutex); | |
1277 | audit_receive_skb(skb); | |
1278 | mutex_unlock(&audit_cmd_mutex); | |
1279 | } | |
1280 | ||
1281 | /* Run custom bind function on netlink socket group connect or bind requests. */ | |
1282 | static int audit_bind(struct net *net, int group) | |
1283 | { | |
1284 | if (!capable(CAP_AUDIT_READ)) | |
1285 | return -EPERM; | |
1286 | ||
1287 | return 0; | |
1288 | } | |
1289 | ||
1290 | static int __net_init audit_net_init(struct net *net) | |
1291 | { | |
1292 | struct netlink_kernel_cfg cfg = { | |
1293 | .input = audit_receive, | |
1294 | .bind = audit_bind, | |
1295 | .flags = NL_CFG_F_NONROOT_RECV, | |
1296 | .groups = AUDIT_NLGRP_MAX, | |
1297 | }; | |
1298 | ||
1299 | struct audit_net *aunet = net_generic(net, audit_net_id); | |
1300 | ||
1301 | aunet->nlsk = netlink_kernel_create(net, NETLINK_AUDIT, &cfg); | |
1302 | if (aunet->nlsk == NULL) { | |
1303 | audit_panic("cannot initialize netlink socket in namespace"); | |
1304 | return -ENOMEM; | |
1305 | } | |
1306 | aunet->nlsk->sk_sndtimeo = MAX_SCHEDULE_TIMEOUT; | |
1307 | return 0; | |
1308 | } | |
1309 | ||
1310 | static void __net_exit audit_net_exit(struct net *net) | |
1311 | { | |
1312 | struct audit_net *aunet = net_generic(net, audit_net_id); | |
1313 | struct sock *sock = aunet->nlsk; | |
1314 | mutex_lock(&audit_cmd_mutex); | |
1315 | if (sock == audit_sock) | |
1316 | auditd_reset(); | |
1317 | mutex_unlock(&audit_cmd_mutex); | |
1318 | ||
1319 | netlink_kernel_release(sock); | |
1320 | aunet->nlsk = NULL; | |
1321 | } | |
1322 | ||
1323 | static struct pernet_operations audit_net_ops __net_initdata = { | |
1324 | .init = audit_net_init, | |
1325 | .exit = audit_net_exit, | |
1326 | .id = &audit_net_id, | |
1327 | .size = sizeof(struct audit_net), | |
1328 | }; | |
1329 | ||
1330 | /* Initialize audit support at boot time. */ | |
1331 | static int __init audit_init(void) | |
1332 | { | |
1333 | int i; | |
1334 | ||
1335 | if (audit_initialized == AUDIT_DISABLED) | |
1336 | return 0; | |
1337 | ||
1338 | pr_info("initializing netlink subsys (%s)\n", | |
1339 | audit_default ? "enabled" : "disabled"); | |
1340 | register_pernet_subsys(&audit_net_ops); | |
1341 | ||
1342 | skb_queue_head_init(&audit_queue); | |
1343 | skb_queue_head_init(&audit_retry_queue); | |
1344 | skb_queue_head_init(&audit_hold_queue); | |
1345 | audit_initialized = AUDIT_INITIALIZED; | |
1346 | audit_enabled = audit_default; | |
1347 | audit_ever_enabled |= !!audit_default; | |
1348 | ||
1349 | for (i = 0; i < AUDIT_INODE_BUCKETS; i++) | |
1350 | INIT_LIST_HEAD(&audit_inode_hash[i]); | |
1351 | ||
1352 | kauditd_task = kthread_run(kauditd_thread, NULL, "kauditd"); | |
1353 | if (IS_ERR(kauditd_task)) { | |
1354 | int err = PTR_ERR(kauditd_task); | |
1355 | panic("audit: failed to start the kauditd thread (%d)\n", err); | |
1356 | } | |
1357 | ||
1358 | audit_log(NULL, GFP_KERNEL, AUDIT_KERNEL, | |
1359 | "state=initialized audit_enabled=%u res=1", | |
1360 | audit_enabled); | |
1361 | ||
1362 | return 0; | |
1363 | } | |
1364 | __initcall(audit_init); | |
1365 | ||
1366 | /* Process kernel command-line parameter at boot time. audit=0 or audit=1. */ | |
1367 | static int __init audit_enable(char *str) | |
1368 | { | |
1369 | audit_default = !!simple_strtol(str, NULL, 0); | |
1370 | if (!audit_default) | |
1371 | audit_initialized = AUDIT_DISABLED; | |
1372 | ||
1373 | pr_info("%s\n", audit_default ? | |
1374 | "enabled (after initialization)" : "disabled (until reboot)"); | |
1375 | ||
1376 | return 1; | |
1377 | } | |
1378 | __setup("audit=", audit_enable); | |
1379 | ||
1380 | /* Process kernel command-line parameter at boot time. | |
1381 | * audit_backlog_limit=<n> */ | |
1382 | static int __init audit_backlog_limit_set(char *str) | |
1383 | { | |
1384 | u32 audit_backlog_limit_arg; | |
1385 | ||
1386 | pr_info("audit_backlog_limit: "); | |
1387 | if (kstrtouint(str, 0, &audit_backlog_limit_arg)) { | |
1388 | pr_cont("using default of %u, unable to parse %s\n", | |
1389 | audit_backlog_limit, str); | |
1390 | return 1; | |
1391 | } | |
1392 | ||
1393 | audit_backlog_limit = audit_backlog_limit_arg; | |
1394 | pr_cont("%d\n", audit_backlog_limit); | |
1395 | ||
1396 | return 1; | |
1397 | } | |
1398 | __setup("audit_backlog_limit=", audit_backlog_limit_set); | |
1399 | ||
1400 | static void audit_buffer_free(struct audit_buffer *ab) | |
1401 | { | |
1402 | unsigned long flags; | |
1403 | ||
1404 | if (!ab) | |
1405 | return; | |
1406 | ||
1407 | kfree_skb(ab->skb); | |
1408 | spin_lock_irqsave(&audit_freelist_lock, flags); | |
1409 | if (audit_freelist_count > AUDIT_MAXFREE) | |
1410 | kfree(ab); | |
1411 | else { | |
1412 | audit_freelist_count++; | |
1413 | list_add(&ab->list, &audit_freelist); | |
1414 | } | |
1415 | spin_unlock_irqrestore(&audit_freelist_lock, flags); | |
1416 | } | |
1417 | ||
1418 | static struct audit_buffer * audit_buffer_alloc(struct audit_context *ctx, | |
1419 | gfp_t gfp_mask, int type) | |
1420 | { | |
1421 | unsigned long flags; | |
1422 | struct audit_buffer *ab = NULL; | |
1423 | struct nlmsghdr *nlh; | |
1424 | ||
1425 | spin_lock_irqsave(&audit_freelist_lock, flags); | |
1426 | if (!list_empty(&audit_freelist)) { | |
1427 | ab = list_entry(audit_freelist.next, | |
1428 | struct audit_buffer, list); | |
1429 | list_del(&ab->list); | |
1430 | --audit_freelist_count; | |
1431 | } | |
1432 | spin_unlock_irqrestore(&audit_freelist_lock, flags); | |
1433 | ||
1434 | if (!ab) { | |
1435 | ab = kmalloc(sizeof(*ab), gfp_mask); | |
1436 | if (!ab) | |
1437 | goto err; | |
1438 | } | |
1439 | ||
1440 | ab->ctx = ctx; | |
1441 | ab->gfp_mask = gfp_mask; | |
1442 | ||
1443 | ab->skb = nlmsg_new(AUDIT_BUFSIZ, gfp_mask); | |
1444 | if (!ab->skb) | |
1445 | goto err; | |
1446 | ||
1447 | nlh = nlmsg_put(ab->skb, 0, 0, type, 0, 0); | |
1448 | if (!nlh) | |
1449 | goto out_kfree_skb; | |
1450 | ||
1451 | return ab; | |
1452 | ||
1453 | out_kfree_skb: | |
1454 | kfree_skb(ab->skb); | |
1455 | ab->skb = NULL; | |
1456 | err: | |
1457 | audit_buffer_free(ab); | |
1458 | return NULL; | |
1459 | } | |
1460 | ||
1461 | /** | |
1462 | * audit_serial - compute a serial number for the audit record | |
1463 | * | |
1464 | * Compute a serial number for the audit record. Audit records are | |
1465 | * written to user-space as soon as they are generated, so a complete | |
1466 | * audit record may be written in several pieces. The timestamp of the | |
1467 | * record and this serial number are used by the user-space tools to | |
1468 | * determine which pieces belong to the same audit record. The | |
1469 | * (timestamp,serial) tuple is unique for each syscall and is live from | |
1470 | * syscall entry to syscall exit. | |
1471 | * | |
1472 | * NOTE: Another possibility is to store the formatted records off the | |
1473 | * audit context (for those records that have a context), and emit them | |
1474 | * all at syscall exit. However, this could delay the reporting of | |
1475 | * significant errors until syscall exit (or never, if the system | |
1476 | * halts). | |
1477 | */ | |
1478 | unsigned int audit_serial(void) | |
1479 | { | |
1480 | static atomic_t serial = ATOMIC_INIT(0); | |
1481 | ||
1482 | return atomic_add_return(1, &serial); | |
1483 | } | |
1484 | ||
1485 | static inline void audit_get_stamp(struct audit_context *ctx, | |
1486 | struct timespec *t, unsigned int *serial) | |
1487 | { | |
1488 | if (!ctx || !auditsc_get_stamp(ctx, t, serial)) { | |
1489 | *t = CURRENT_TIME; | |
1490 | *serial = audit_serial(); | |
1491 | } | |
1492 | } | |
1493 | ||
1494 | /** | |
1495 | * audit_log_start - obtain an audit buffer | |
1496 | * @ctx: audit_context (may be NULL) | |
1497 | * @gfp_mask: type of allocation | |
1498 | * @type: audit message type | |
1499 | * | |
1500 | * Returns audit_buffer pointer on success or NULL on error. | |
1501 | * | |
1502 | * Obtain an audit buffer. This routine does locking to obtain the | |
1503 | * audit buffer, but then no locking is required for calls to | |
1504 | * audit_log_*format. If the task (ctx) is a task that is currently in a | |
1505 | * syscall, then the syscall is marked as auditable and an audit record | |
1506 | * will be written at syscall exit. If there is no associated task, then | |
1507 | * task context (ctx) should be NULL. | |
1508 | */ | |
1509 | struct audit_buffer *audit_log_start(struct audit_context *ctx, gfp_t gfp_mask, | |
1510 | int type) | |
1511 | { | |
1512 | struct audit_buffer *ab; | |
1513 | struct timespec t; | |
1514 | unsigned int uninitialized_var(serial); | |
1515 | ||
1516 | if (audit_initialized != AUDIT_INITIALIZED) | |
1517 | return NULL; | |
1518 | ||
1519 | if (unlikely(!audit_filter(type, AUDIT_FILTER_TYPE))) | |
1520 | return NULL; | |
1521 | ||
1522 | /* don't ever fail/sleep on these two conditions: | |
1523 | * 1. auditd generated record - since we need auditd to drain the | |
1524 | * queue; also, when we are checking for auditd, compare PIDs using | |
1525 | * task_tgid_vnr() since auditd_pid is set in audit_receive_msg() | |
1526 | * using a PID anchored in the caller's namespace | |
1527 | * 2. audit command message - record types 1000 through 1099 inclusive | |
1528 | * are command messages/records used to manage the kernel subsystem | |
1529 | * and the audit userspace, blocking on these messages could cause | |
1530 | * problems under load so don't do it (note: not all of these | |
1531 | * command types are valid as record types, but it is quicker to | |
1532 | * just check two ints than a series of ints in a if/switch stmt) */ | |
1533 | if (!((audit_pid && audit_pid == task_tgid_vnr(current)) || | |
1534 | (type >= 1000 && type <= 1099))) { | |
1535 | long sleep_time = audit_backlog_wait_time; | |
1536 | ||
1537 | while (audit_backlog_limit && | |
1538 | (skb_queue_len(&audit_queue) > audit_backlog_limit)) { | |
1539 | /* wake kauditd to try and flush the queue */ | |
1540 | wake_up_interruptible(&kauditd_wait); | |
1541 | ||
1542 | /* sleep if we are allowed and we haven't exhausted our | |
1543 | * backlog wait limit */ | |
1544 | if ((gfp_mask & __GFP_DIRECT_RECLAIM) && | |
1545 | (sleep_time > 0)) { | |
1546 | DECLARE_WAITQUEUE(wait, current); | |
1547 | ||
1548 | add_wait_queue_exclusive(&audit_backlog_wait, | |
1549 | &wait); | |
1550 | set_current_state(TASK_UNINTERRUPTIBLE); | |
1551 | sleep_time = schedule_timeout(sleep_time); | |
1552 | remove_wait_queue(&audit_backlog_wait, &wait); | |
1553 | } else { | |
1554 | if (audit_rate_check() && printk_ratelimit()) | |
1555 | pr_warn("audit_backlog=%d > audit_backlog_limit=%d\n", | |
1556 | skb_queue_len(&audit_queue), | |
1557 | audit_backlog_limit); | |
1558 | audit_log_lost("backlog limit exceeded"); | |
1559 | return NULL; | |
1560 | } | |
1561 | } | |
1562 | } | |
1563 | ||
1564 | ab = audit_buffer_alloc(ctx, gfp_mask, type); | |
1565 | if (!ab) { | |
1566 | audit_log_lost("out of memory in audit_log_start"); | |
1567 | return NULL; | |
1568 | } | |
1569 | ||
1570 | audit_get_stamp(ab->ctx, &t, &serial); | |
1571 | audit_log_format(ab, "audit(%lu.%03lu:%u): ", | |
1572 | t.tv_sec, t.tv_nsec/1000000, serial); | |
1573 | ||
1574 | return ab; | |
1575 | } | |
1576 | ||
1577 | /** | |
1578 | * audit_expand - expand skb in the audit buffer | |
1579 | * @ab: audit_buffer | |
1580 | * @extra: space to add at tail of the skb | |
1581 | * | |
1582 | * Returns 0 (no space) on failed expansion, or available space if | |
1583 | * successful. | |
1584 | */ | |
1585 | static inline int audit_expand(struct audit_buffer *ab, int extra) | |
1586 | { | |
1587 | struct sk_buff *skb = ab->skb; | |
1588 | int oldtail = skb_tailroom(skb); | |
1589 | int ret = pskb_expand_head(skb, 0, extra, ab->gfp_mask); | |
1590 | int newtail = skb_tailroom(skb); | |
1591 | ||
1592 | if (ret < 0) { | |
1593 | audit_log_lost("out of memory in audit_expand"); | |
1594 | return 0; | |
1595 | } | |
1596 | ||
1597 | skb->truesize += newtail - oldtail; | |
1598 | return newtail; | |
1599 | } | |
1600 | ||
1601 | /* | |
1602 | * Format an audit message into the audit buffer. If there isn't enough | |
1603 | * room in the audit buffer, more room will be allocated and vsnprint | |
1604 | * will be called a second time. Currently, we assume that a printk | |
1605 | * can't format message larger than 1024 bytes, so we don't either. | |
1606 | */ | |
1607 | static void audit_log_vformat(struct audit_buffer *ab, const char *fmt, | |
1608 | va_list args) | |
1609 | { | |
1610 | int len, avail; | |
1611 | struct sk_buff *skb; | |
1612 | va_list args2; | |
1613 | ||
1614 | if (!ab) | |
1615 | return; | |
1616 | ||
1617 | BUG_ON(!ab->skb); | |
1618 | skb = ab->skb; | |
1619 | avail = skb_tailroom(skb); | |
1620 | if (avail == 0) { | |
1621 | avail = audit_expand(ab, AUDIT_BUFSIZ); | |
1622 | if (!avail) | |
1623 | goto out; | |
1624 | } | |
1625 | va_copy(args2, args); | |
1626 | len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args); | |
1627 | if (len >= avail) { | |
1628 | /* The printk buffer is 1024 bytes long, so if we get | |
1629 | * here and AUDIT_BUFSIZ is at least 1024, then we can | |
1630 | * log everything that printk could have logged. */ | |
1631 | avail = audit_expand(ab, | |
1632 | max_t(unsigned, AUDIT_BUFSIZ, 1+len-avail)); | |
1633 | if (!avail) | |
1634 | goto out_va_end; | |
1635 | len = vsnprintf(skb_tail_pointer(skb), avail, fmt, args2); | |
1636 | } | |
1637 | if (len > 0) | |
1638 | skb_put(skb, len); | |
1639 | out_va_end: | |
1640 | va_end(args2); | |
1641 | out: | |
1642 | return; | |
1643 | } | |
1644 | ||
1645 | /** | |
1646 | * audit_log_format - format a message into the audit buffer. | |
1647 | * @ab: audit_buffer | |
1648 | * @fmt: format string | |
1649 | * @...: optional parameters matching @fmt string | |
1650 | * | |
1651 | * All the work is done in audit_log_vformat. | |
1652 | */ | |
1653 | void audit_log_format(struct audit_buffer *ab, const char *fmt, ...) | |
1654 | { | |
1655 | va_list args; | |
1656 | ||
1657 | if (!ab) | |
1658 | return; | |
1659 | va_start(args, fmt); | |
1660 | audit_log_vformat(ab, fmt, args); | |
1661 | va_end(args); | |
1662 | } | |
1663 | ||
1664 | /** | |
1665 | * audit_log_hex - convert a buffer to hex and append it to the audit skb | |
1666 | * @ab: the audit_buffer | |
1667 | * @buf: buffer to convert to hex | |
1668 | * @len: length of @buf to be converted | |
1669 | * | |
1670 | * No return value; failure to expand is silently ignored. | |
1671 | * | |
1672 | * This function will take the passed buf and convert it into a string of | |
1673 | * ascii hex digits. The new string is placed onto the skb. | |
1674 | */ | |
1675 | void audit_log_n_hex(struct audit_buffer *ab, const unsigned char *buf, | |
1676 | size_t len) | |
1677 | { | |
1678 | int i, avail, new_len; | |
1679 | unsigned char *ptr; | |
1680 | struct sk_buff *skb; | |
1681 | ||
1682 | if (!ab) | |
1683 | return; | |
1684 | ||
1685 | BUG_ON(!ab->skb); | |
1686 | skb = ab->skb; | |
1687 | avail = skb_tailroom(skb); | |
1688 | new_len = len<<1; | |
1689 | if (new_len >= avail) { | |
1690 | /* Round the buffer request up to the next multiple */ | |
1691 | new_len = AUDIT_BUFSIZ*(((new_len-avail)/AUDIT_BUFSIZ) + 1); | |
1692 | avail = audit_expand(ab, new_len); | |
1693 | if (!avail) | |
1694 | return; | |
1695 | } | |
1696 | ||
1697 | ptr = skb_tail_pointer(skb); | |
1698 | for (i = 0; i < len; i++) | |
1699 | ptr = hex_byte_pack_upper(ptr, buf[i]); | |
1700 | *ptr = 0; | |
1701 | skb_put(skb, len << 1); /* new string is twice the old string */ | |
1702 | } | |
1703 | ||
1704 | /* | |
1705 | * Format a string of no more than slen characters into the audit buffer, | |
1706 | * enclosed in quote marks. | |
1707 | */ | |
1708 | void audit_log_n_string(struct audit_buffer *ab, const char *string, | |
1709 | size_t slen) | |
1710 | { | |
1711 | int avail, new_len; | |
1712 | unsigned char *ptr; | |
1713 | struct sk_buff *skb; | |
1714 | ||
1715 | if (!ab) | |
1716 | return; | |
1717 | ||
1718 | BUG_ON(!ab->skb); | |
1719 | skb = ab->skb; | |
1720 | avail = skb_tailroom(skb); | |
1721 | new_len = slen + 3; /* enclosing quotes + null terminator */ | |
1722 | if (new_len > avail) { | |
1723 | avail = audit_expand(ab, new_len); | |
1724 | if (!avail) | |
1725 | return; | |
1726 | } | |
1727 | ptr = skb_tail_pointer(skb); | |
1728 | *ptr++ = '"'; | |
1729 | memcpy(ptr, string, slen); | |
1730 | ptr += slen; | |
1731 | *ptr++ = '"'; | |
1732 | *ptr = 0; | |
1733 | skb_put(skb, slen + 2); /* don't include null terminator */ | |
1734 | } | |
1735 | ||
1736 | /** | |
1737 | * audit_string_contains_control - does a string need to be logged in hex | |
1738 | * @string: string to be checked | |
1739 | * @len: max length of the string to check | |
1740 | */ | |
1741 | bool audit_string_contains_control(const char *string, size_t len) | |
1742 | { | |
1743 | const unsigned char *p; | |
1744 | for (p = string; p < (const unsigned char *)string + len; p++) { | |
1745 | if (*p == '"' || *p < 0x21 || *p > 0x7e) | |
1746 | return true; | |
1747 | } | |
1748 | return false; | |
1749 | } | |
1750 | ||
1751 | /** | |
1752 | * audit_log_n_untrustedstring - log a string that may contain random characters | |
1753 | * @ab: audit_buffer | |
1754 | * @len: length of string (not including trailing null) | |
1755 | * @string: string to be logged | |
1756 | * | |
1757 | * This code will escape a string that is passed to it if the string | |
1758 | * contains a control character, unprintable character, double quote mark, | |
1759 | * or a space. Unescaped strings will start and end with a double quote mark. | |
1760 | * Strings that are escaped are printed in hex (2 digits per char). | |
1761 | * | |
1762 | * The caller specifies the number of characters in the string to log, which may | |
1763 | * or may not be the entire string. | |
1764 | */ | |
1765 | void audit_log_n_untrustedstring(struct audit_buffer *ab, const char *string, | |
1766 | size_t len) | |
1767 | { | |
1768 | if (audit_string_contains_control(string, len)) | |
1769 | audit_log_n_hex(ab, string, len); | |
1770 | else | |
1771 | audit_log_n_string(ab, string, len); | |
1772 | } | |
1773 | ||
1774 | /** | |
1775 | * audit_log_untrustedstring - log a string that may contain random characters | |
1776 | * @ab: audit_buffer | |
1777 | * @string: string to be logged | |
1778 | * | |
1779 | * Same as audit_log_n_untrustedstring(), except that strlen is used to | |
1780 | * determine string length. | |
1781 | */ | |
1782 | void audit_log_untrustedstring(struct audit_buffer *ab, const char *string) | |
1783 | { | |
1784 | audit_log_n_untrustedstring(ab, string, strlen(string)); | |
1785 | } | |
1786 | ||
1787 | /* This is a helper-function to print the escaped d_path */ | |
1788 | void audit_log_d_path(struct audit_buffer *ab, const char *prefix, | |
1789 | const struct path *path) | |
1790 | { | |
1791 | char *p, *pathname; | |
1792 | ||
1793 | if (prefix) | |
1794 | audit_log_format(ab, "%s", prefix); | |
1795 | ||
1796 | /* We will allow 11 spaces for ' (deleted)' to be appended */ | |
1797 | pathname = kmalloc(PATH_MAX+11, ab->gfp_mask); | |
1798 | if (!pathname) { | |
1799 | audit_log_string(ab, "<no_memory>"); | |
1800 | return; | |
1801 | } | |
1802 | p = d_path(path, pathname, PATH_MAX+11); | |
1803 | if (IS_ERR(p)) { /* Should never happen since we send PATH_MAX */ | |
1804 | /* FIXME: can we save some information here? */ | |
1805 | audit_log_string(ab, "<too_long>"); | |
1806 | } else | |
1807 | audit_log_untrustedstring(ab, p); | |
1808 | kfree(pathname); | |
1809 | } | |
1810 | ||
1811 | void audit_log_session_info(struct audit_buffer *ab) | |
1812 | { | |
1813 | unsigned int sessionid = audit_get_sessionid(current); | |
1814 | uid_t auid = from_kuid(&init_user_ns, audit_get_loginuid(current)); | |
1815 | ||
1816 | audit_log_format(ab, " auid=%u ses=%u", auid, sessionid); | |
1817 | } | |
1818 | ||
1819 | void audit_log_key(struct audit_buffer *ab, char *key) | |
1820 | { | |
1821 | audit_log_format(ab, " key="); | |
1822 | if (key) | |
1823 | audit_log_untrustedstring(ab, key); | |
1824 | else | |
1825 | audit_log_format(ab, "(null)"); | |
1826 | } | |
1827 | ||
1828 | void audit_log_cap(struct audit_buffer *ab, char *prefix, kernel_cap_t *cap) | |
1829 | { | |
1830 | int i; | |
1831 | ||
1832 | audit_log_format(ab, " %s=", prefix); | |
1833 | CAP_FOR_EACH_U32(i) { | |
1834 | audit_log_format(ab, "%08x", | |
1835 | cap->cap[CAP_LAST_U32 - i]); | |
1836 | } | |
1837 | } | |
1838 | ||
1839 | static void audit_log_fcaps(struct audit_buffer *ab, struct audit_names *name) | |
1840 | { | |
1841 | kernel_cap_t *perm = &name->fcap.permitted; | |
1842 | kernel_cap_t *inh = &name->fcap.inheritable; | |
1843 | int log = 0; | |
1844 | ||
1845 | if (!cap_isclear(*perm)) { | |
1846 | audit_log_cap(ab, "cap_fp", perm); | |
1847 | log = 1; | |
1848 | } | |
1849 | if (!cap_isclear(*inh)) { | |
1850 | audit_log_cap(ab, "cap_fi", inh); | |
1851 | log = 1; | |
1852 | } | |
1853 | ||
1854 | if (log) | |
1855 | audit_log_format(ab, " cap_fe=%d cap_fver=%x", | |
1856 | name->fcap.fE, name->fcap_ver); | |
1857 | } | |
1858 | ||
1859 | static inline int audit_copy_fcaps(struct audit_names *name, | |
1860 | const struct dentry *dentry) | |
1861 | { | |
1862 | struct cpu_vfs_cap_data caps; | |
1863 | int rc; | |
1864 | ||
1865 | if (!dentry) | |
1866 | return 0; | |
1867 | ||
1868 | rc = get_vfs_caps_from_disk(dentry, &caps); | |
1869 | if (rc) | |
1870 | return rc; | |
1871 | ||
1872 | name->fcap.permitted = caps.permitted; | |
1873 | name->fcap.inheritable = caps.inheritable; | |
1874 | name->fcap.fE = !!(caps.magic_etc & VFS_CAP_FLAGS_EFFECTIVE); | |
1875 | name->fcap_ver = (caps.magic_etc & VFS_CAP_REVISION_MASK) >> | |
1876 | VFS_CAP_REVISION_SHIFT; | |
1877 | ||
1878 | return 0; | |
1879 | } | |
1880 | ||
1881 | /* Copy inode data into an audit_names. */ | |
1882 | void audit_copy_inode(struct audit_names *name, const struct dentry *dentry, | |
1883 | struct inode *inode) | |
1884 | { | |
1885 | name->ino = inode->i_ino; | |
1886 | name->dev = inode->i_sb->s_dev; | |
1887 | name->mode = inode->i_mode; | |
1888 | name->uid = inode->i_uid; | |
1889 | name->gid = inode->i_gid; | |
1890 | name->rdev = inode->i_rdev; | |
1891 | security_inode_getsecid(inode, &name->osid); | |
1892 | audit_copy_fcaps(name, dentry); | |
1893 | } | |
1894 | ||
1895 | /** | |
1896 | * audit_log_name - produce AUDIT_PATH record from struct audit_names | |
1897 | * @context: audit_context for the task | |
1898 | * @n: audit_names structure with reportable details | |
1899 | * @path: optional path to report instead of audit_names->name | |
1900 | * @record_num: record number to report when handling a list of names | |
1901 | * @call_panic: optional pointer to int that will be updated if secid fails | |
1902 | */ | |
1903 | void audit_log_name(struct audit_context *context, struct audit_names *n, | |
1904 | const struct path *path, int record_num, int *call_panic) | |
1905 | { | |
1906 | struct audit_buffer *ab; | |
1907 | ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH); | |
1908 | if (!ab) | |
1909 | return; | |
1910 | ||
1911 | audit_log_format(ab, "item=%d", record_num); | |
1912 | ||
1913 | if (path) | |
1914 | audit_log_d_path(ab, " name=", path); | |
1915 | else if (n->name) { | |
1916 | switch (n->name_len) { | |
1917 | case AUDIT_NAME_FULL: | |
1918 | /* log the full path */ | |
1919 | audit_log_format(ab, " name="); | |
1920 | audit_log_untrustedstring(ab, n->name->name); | |
1921 | break; | |
1922 | case 0: | |
1923 | /* name was specified as a relative path and the | |
1924 | * directory component is the cwd */ | |
1925 | audit_log_d_path(ab, " name=", &context->pwd); | |
1926 | break; | |
1927 | default: | |
1928 | /* log the name's directory component */ | |
1929 | audit_log_format(ab, " name="); | |
1930 | audit_log_n_untrustedstring(ab, n->name->name, | |
1931 | n->name_len); | |
1932 | } | |
1933 | } else | |
1934 | audit_log_format(ab, " name=(null)"); | |
1935 | ||
1936 | if (n->ino != AUDIT_INO_UNSET) | |
1937 | audit_log_format(ab, " inode=%lu" | |
1938 | " dev=%02x:%02x mode=%#ho" | |
1939 | " ouid=%u ogid=%u rdev=%02x:%02x", | |
1940 | n->ino, | |
1941 | MAJOR(n->dev), | |
1942 | MINOR(n->dev), | |
1943 | n->mode, | |
1944 | from_kuid(&init_user_ns, n->uid), | |
1945 | from_kgid(&init_user_ns, n->gid), | |
1946 | MAJOR(n->rdev), | |
1947 | MINOR(n->rdev)); | |
1948 | if (n->osid != 0) { | |
1949 | char *ctx = NULL; | |
1950 | u32 len; | |
1951 | if (security_secid_to_secctx( | |
1952 | n->osid, &ctx, &len)) { | |
1953 | audit_log_format(ab, " osid=%u", n->osid); | |
1954 | if (call_panic) | |
1955 | *call_panic = 2; | |
1956 | } else { | |
1957 | audit_log_format(ab, " obj=%s", ctx); | |
1958 | security_release_secctx(ctx, len); | |
1959 | } | |
1960 | } | |
1961 | ||
1962 | /* log the audit_names record type */ | |
1963 | audit_log_format(ab, " nametype="); | |
1964 | switch(n->type) { | |
1965 | case AUDIT_TYPE_NORMAL: | |
1966 | audit_log_format(ab, "NORMAL"); | |
1967 | break; | |
1968 | case AUDIT_TYPE_PARENT: | |
1969 | audit_log_format(ab, "PARENT"); | |
1970 | break; | |
1971 | case AUDIT_TYPE_CHILD_DELETE: | |
1972 | audit_log_format(ab, "DELETE"); | |
1973 | break; | |
1974 | case AUDIT_TYPE_CHILD_CREATE: | |
1975 | audit_log_format(ab, "CREATE"); | |
1976 | break; | |
1977 | default: | |
1978 | audit_log_format(ab, "UNKNOWN"); | |
1979 | break; | |
1980 | } | |
1981 | ||
1982 | audit_log_fcaps(ab, n); | |
1983 | audit_log_end(ab); | |
1984 | } | |
1985 | ||
1986 | int audit_log_task_context(struct audit_buffer *ab) | |
1987 | { | |
1988 | char *ctx = NULL; | |
1989 | unsigned len; | |
1990 | int error; | |
1991 | u32 sid; | |
1992 | ||
1993 | security_task_getsecid(current, &sid); | |
1994 | if (!sid) | |
1995 | return 0; | |
1996 | ||
1997 | error = security_secid_to_secctx(sid, &ctx, &len); | |
1998 | if (error) { | |
1999 | if (error != -EINVAL) | |
2000 | goto error_path; | |
2001 | return 0; | |
2002 | } | |
2003 | ||
2004 | audit_log_format(ab, " subj=%s", ctx); | |
2005 | security_release_secctx(ctx, len); | |
2006 | return 0; | |
2007 | ||
2008 | error_path: | |
2009 | audit_panic("error in audit_log_task_context"); | |
2010 | return error; | |
2011 | } | |
2012 | EXPORT_SYMBOL(audit_log_task_context); | |
2013 | ||
2014 | void audit_log_d_path_exe(struct audit_buffer *ab, | |
2015 | struct mm_struct *mm) | |
2016 | { | |
2017 | struct file *exe_file; | |
2018 | ||
2019 | if (!mm) | |
2020 | goto out_null; | |
2021 | ||
2022 | exe_file = get_mm_exe_file(mm); | |
2023 | if (!exe_file) | |
2024 | goto out_null; | |
2025 | ||
2026 | audit_log_d_path(ab, " exe=", &exe_file->f_path); | |
2027 | fput(exe_file); | |
2028 | return; | |
2029 | out_null: | |
2030 | audit_log_format(ab, " exe=(null)"); | |
2031 | } | |
2032 | ||
2033 | struct tty_struct *audit_get_tty(struct task_struct *tsk) | |
2034 | { | |
2035 | struct tty_struct *tty = NULL; | |
2036 | unsigned long flags; | |
2037 | ||
2038 | spin_lock_irqsave(&tsk->sighand->siglock, flags); | |
2039 | if (tsk->signal) | |
2040 | tty = tty_kref_get(tsk->signal->tty); | |
2041 | spin_unlock_irqrestore(&tsk->sighand->siglock, flags); | |
2042 | return tty; | |
2043 | } | |
2044 | ||
2045 | void audit_put_tty(struct tty_struct *tty) | |
2046 | { | |
2047 | tty_kref_put(tty); | |
2048 | } | |
2049 | ||
2050 | void audit_log_task_info(struct audit_buffer *ab, struct task_struct *tsk) | |
2051 | { | |
2052 | const struct cred *cred; | |
2053 | char comm[sizeof(tsk->comm)]; | |
2054 | struct tty_struct *tty; | |
2055 | ||
2056 | if (!ab) | |
2057 | return; | |
2058 | ||
2059 | /* tsk == current */ | |
2060 | cred = current_cred(); | |
2061 | tty = audit_get_tty(tsk); | |
2062 | audit_log_format(ab, | |
2063 | " ppid=%d pid=%d auid=%u uid=%u gid=%u" | |
2064 | " euid=%u suid=%u fsuid=%u" | |
2065 | " egid=%u sgid=%u fsgid=%u tty=%s ses=%u", | |
2066 | task_ppid_nr(tsk), | |
2067 | task_tgid_nr(tsk), | |
2068 | from_kuid(&init_user_ns, audit_get_loginuid(tsk)), | |
2069 | from_kuid(&init_user_ns, cred->uid), | |
2070 | from_kgid(&init_user_ns, cred->gid), | |
2071 | from_kuid(&init_user_ns, cred->euid), | |
2072 | from_kuid(&init_user_ns, cred->suid), | |
2073 | from_kuid(&init_user_ns, cred->fsuid), | |
2074 | from_kgid(&init_user_ns, cred->egid), | |
2075 | from_kgid(&init_user_ns, cred->sgid), | |
2076 | from_kgid(&init_user_ns, cred->fsgid), | |
2077 | tty ? tty_name(tty) : "(none)", | |
2078 | audit_get_sessionid(tsk)); | |
2079 | audit_put_tty(tty); | |
2080 | audit_log_format(ab, " comm="); | |
2081 | audit_log_untrustedstring(ab, get_task_comm(comm, tsk)); | |
2082 | audit_log_d_path_exe(ab, tsk->mm); | |
2083 | audit_log_task_context(ab); | |
2084 | } | |
2085 | EXPORT_SYMBOL(audit_log_task_info); | |
2086 | ||
2087 | /** | |
2088 | * audit_log_link_denied - report a link restriction denial | |
2089 | * @operation: specific link operation | |
2090 | * @link: the path that triggered the restriction | |
2091 | */ | |
2092 | void audit_log_link_denied(const char *operation, const struct path *link) | |
2093 | { | |
2094 | struct audit_buffer *ab; | |
2095 | struct audit_names *name; | |
2096 | ||
2097 | name = kzalloc(sizeof(*name), GFP_NOFS); | |
2098 | if (!name) | |
2099 | return; | |
2100 | ||
2101 | /* Generate AUDIT_ANOM_LINK with subject, operation, outcome. */ | |
2102 | ab = audit_log_start(current->audit_context, GFP_KERNEL, | |
2103 | AUDIT_ANOM_LINK); | |
2104 | if (!ab) | |
2105 | goto out; | |
2106 | audit_log_format(ab, "op=%s", operation); | |
2107 | audit_log_task_info(ab, current); | |
2108 | audit_log_format(ab, " res=0"); | |
2109 | audit_log_end(ab); | |
2110 | ||
2111 | /* Generate AUDIT_PATH record with object. */ | |
2112 | name->type = AUDIT_TYPE_NORMAL; | |
2113 | audit_copy_inode(name, link->dentry, d_backing_inode(link->dentry)); | |
2114 | audit_log_name(current->audit_context, name, link, 0, NULL); | |
2115 | out: | |
2116 | kfree(name); | |
2117 | } | |
2118 | ||
2119 | /** | |
2120 | * audit_log_end - end one audit record | |
2121 | * @ab: the audit_buffer | |
2122 | * | |
2123 | * We can not do a netlink send inside an irq context because it blocks (last | |
2124 | * arg, flags, is not set to MSG_DONTWAIT), so the audit buffer is placed on a | |
2125 | * queue and a tasklet is scheduled to remove them from the queue outside the | |
2126 | * irq context. May be called in any context. | |
2127 | */ | |
2128 | void audit_log_end(struct audit_buffer *ab) | |
2129 | { | |
2130 | if (!ab) | |
2131 | return; | |
2132 | if (!audit_rate_check()) { | |
2133 | audit_log_lost("rate limit exceeded"); | |
2134 | } else { | |
2135 | skb_queue_tail(&audit_queue, ab->skb); | |
2136 | wake_up_interruptible(&kauditd_wait); | |
2137 | ab->skb = NULL; | |
2138 | } | |
2139 | audit_buffer_free(ab); | |
2140 | } | |
2141 | ||
2142 | /** | |
2143 | * audit_log - Log an audit record | |
2144 | * @ctx: audit context | |
2145 | * @gfp_mask: type of allocation | |
2146 | * @type: audit message type | |
2147 | * @fmt: format string to use | |
2148 | * @...: variable parameters matching the format string | |
2149 | * | |
2150 | * This is a convenience function that calls audit_log_start, | |
2151 | * audit_log_vformat, and audit_log_end. It may be called | |
2152 | * in any context. | |
2153 | */ | |
2154 | void audit_log(struct audit_context *ctx, gfp_t gfp_mask, int type, | |
2155 | const char *fmt, ...) | |
2156 | { | |
2157 | struct audit_buffer *ab; | |
2158 | va_list args; | |
2159 | ||
2160 | ab = audit_log_start(ctx, gfp_mask, type); | |
2161 | if (ab) { | |
2162 | va_start(args, fmt); | |
2163 | audit_log_vformat(ab, fmt, args); | |
2164 | va_end(args); | |
2165 | audit_log_end(ab); | |
2166 | } | |
2167 | } | |
2168 | ||
2169 | #ifdef CONFIG_SECURITY | |
2170 | /** | |
2171 | * audit_log_secctx - Converts and logs SELinux context | |
2172 | * @ab: audit_buffer | |
2173 | * @secid: security number | |
2174 | * | |
2175 | * This is a helper function that calls security_secid_to_secctx to convert | |
2176 | * secid to secctx and then adds the (converted) SELinux context to the audit | |
2177 | * log by calling audit_log_format, thus also preventing leak of internal secid | |
2178 | * to userspace. If secid cannot be converted audit_panic is called. | |
2179 | */ | |
2180 | void audit_log_secctx(struct audit_buffer *ab, u32 secid) | |
2181 | { | |
2182 | u32 len; | |
2183 | char *secctx; | |
2184 | ||
2185 | if (security_secid_to_secctx(secid, &secctx, &len)) { | |
2186 | audit_panic("Cannot convert secid to context"); | |
2187 | } else { | |
2188 | audit_log_format(ab, " obj=%s", secctx); | |
2189 | security_release_secctx(secctx, len); | |
2190 | } | |
2191 | } | |
2192 | EXPORT_SYMBOL(audit_log_secctx); | |
2193 | #endif | |
2194 | ||
2195 | EXPORT_SYMBOL(audit_log_start); | |
2196 | EXPORT_SYMBOL(audit_log_end); | |
2197 | EXPORT_SYMBOL(audit_log_format); | |
2198 | EXPORT_SYMBOL(audit_log); |