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