Commit | Line | Data |
---|---|---|
fe7752ba DW |
1 | /* auditfilter.c -- filtering of audit events |
2 | * | |
3 | * Copyright 2003-2004 Red Hat, Inc. | |
4 | * Copyright 2005 Hewlett-Packard Development Company, L.P. | |
5 | * Copyright 2005 IBM Corporation | |
6 | * | |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
20 | */ | |
21 | ||
f952d10f RGB |
22 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
23 | ||
fe7752ba DW |
24 | #include <linux/kernel.h> |
25 | #include <linux/audit.h> | |
26 | #include <linux/kthread.h> | |
f368c07d AG |
27 | #include <linux/mutex.h> |
28 | #include <linux/fs.h> | |
29 | #include <linux/namei.h> | |
fe7752ba | 30 | #include <linux/netlink.h> |
f368c07d | 31 | #include <linux/sched.h> |
5a0e3ad6 | 32 | #include <linux/slab.h> |
2a862b32 | 33 | #include <linux/security.h> |
638a0fd2 | 34 | #include <net/net_namespace.h> |
fe7752ba DW |
35 | #include "audit.h" |
36 | ||
f368c07d AG |
37 | /* |
38 | * Locking model: | |
39 | * | |
40 | * audit_filter_mutex: | |
41 | * Synchronizes writes and blocking reads of audit's filterlist | |
42 | * data. Rcu is used to traverse the filterlist and access | |
43 | * contents of structs audit_entry, audit_watch and opaque | |
d7a96f3a | 44 | * LSM rules during filtering. If modified, these structures |
f368c07d AG |
45 | * must be copied and replace their counterparts in the filterlist. |
46 | * An audit_parent struct is not accessed during filtering, so may | |
47 | * be written directly provided audit_filter_mutex is held. | |
48 | */ | |
49 | ||
f368c07d | 50 | /* Audit filter lists, defined in <linux/audit.h> */ |
fe7752ba DW |
51 | struct list_head audit_filter_list[AUDIT_NR_FILTERS] = { |
52 | LIST_HEAD_INIT(audit_filter_list[0]), | |
53 | LIST_HEAD_INIT(audit_filter_list[1]), | |
54 | LIST_HEAD_INIT(audit_filter_list[2]), | |
55 | LIST_HEAD_INIT(audit_filter_list[3]), | |
56 | LIST_HEAD_INIT(audit_filter_list[4]), | |
57 | LIST_HEAD_INIT(audit_filter_list[5]), | |
58 | #if AUDIT_NR_FILTERS != 6 | |
59 | #error Fix audit_filter_list initialiser | |
60 | #endif | |
61 | }; | |
e45aa212 AV |
62 | static struct list_head audit_rules_list[AUDIT_NR_FILTERS] = { |
63 | LIST_HEAD_INIT(audit_rules_list[0]), | |
64 | LIST_HEAD_INIT(audit_rules_list[1]), | |
65 | LIST_HEAD_INIT(audit_rules_list[2]), | |
66 | LIST_HEAD_INIT(audit_rules_list[3]), | |
67 | LIST_HEAD_INIT(audit_rules_list[4]), | |
68 | LIST_HEAD_INIT(audit_rules_list[5]), | |
69 | }; | |
fe7752ba | 70 | |
74c3cbe3 | 71 | DEFINE_MUTEX(audit_filter_mutex); |
f368c07d | 72 | |
93315ed6 | 73 | static inline void audit_free_rule(struct audit_entry *e) |
fe7752ba | 74 | { |
3dc7e315 | 75 | int i; |
c28bb7da | 76 | struct audit_krule *erule = &e->rule; |
ae7b8f41 | 77 | |
f368c07d | 78 | /* some rules don't have associated watches */ |
c28bb7da ZX |
79 | if (erule->watch) |
80 | audit_put_watch(erule->watch); | |
81 | if (erule->fields) | |
82 | for (i = 0; i < erule->field_count; i++) { | |
83 | struct audit_field *f = &erule->fields[i]; | |
04305e4a AD |
84 | kfree(f->lsm_str); |
85 | security_audit_rule_free(f->lsm_rule); | |
3dc7e315 | 86 | } |
c28bb7da ZX |
87 | kfree(erule->fields); |
88 | kfree(erule->filterkey); | |
93315ed6 AG |
89 | kfree(e); |
90 | } | |
91 | ||
74c3cbe3 | 92 | void audit_free_rule_rcu(struct rcu_head *head) |
93315ed6 AG |
93 | { |
94 | struct audit_entry *e = container_of(head, struct audit_entry, rcu); | |
95 | audit_free_rule(e); | |
96 | } | |
97 | ||
3dc7e315 DG |
98 | /* Initialize an audit filterlist entry. */ |
99 | static inline struct audit_entry *audit_init_entry(u32 field_count) | |
100 | { | |
101 | struct audit_entry *entry; | |
102 | struct audit_field *fields; | |
103 | ||
104 | entry = kzalloc(sizeof(*entry), GFP_KERNEL); | |
105 | if (unlikely(!entry)) | |
106 | return NULL; | |
107 | ||
108 | fields = kzalloc(sizeof(*fields) * field_count, GFP_KERNEL); | |
109 | if (unlikely(!fields)) { | |
110 | kfree(entry); | |
111 | return NULL; | |
112 | } | |
113 | entry->rule.fields = fields; | |
114 | ||
115 | return entry; | |
116 | } | |
117 | ||
93315ed6 AG |
118 | /* Unpack a filter field's string representation from user-space |
119 | * buffer. */ | |
74c3cbe3 | 120 | char *audit_unpack_string(void **bufp, size_t *remain, size_t len) |
93315ed6 AG |
121 | { |
122 | char *str; | |
123 | ||
124 | if (!*bufp || (len == 0) || (len > *remain)) | |
125 | return ERR_PTR(-EINVAL); | |
126 | ||
127 | /* Of the currently implemented string fields, PATH_MAX | |
128 | * defines the longest valid length. | |
129 | */ | |
130 | if (len > PATH_MAX) | |
131 | return ERR_PTR(-ENAMETOOLONG); | |
132 | ||
133 | str = kmalloc(len + 1, GFP_KERNEL); | |
134 | if (unlikely(!str)) | |
135 | return ERR_PTR(-ENOMEM); | |
136 | ||
137 | memcpy(str, *bufp, len); | |
138 | str[len] = 0; | |
139 | *bufp += len; | |
140 | *remain -= len; | |
141 | ||
142 | return str; | |
143 | } | |
144 | ||
f368c07d AG |
145 | /* Translate an inode field to kernel respresentation. */ |
146 | static inline int audit_to_inode(struct audit_krule *krule, | |
147 | struct audit_field *f) | |
148 | { | |
149 | if (krule->listnr != AUDIT_FILTER_EXIT || | |
5af75d8d AV |
150 | krule->watch || krule->inode_f || krule->tree || |
151 | (f->op != Audit_equal && f->op != Audit_not_equal)) | |
f368c07d AG |
152 | return -EINVAL; |
153 | ||
154 | krule->inode_f = f; | |
155 | return 0; | |
156 | } | |
157 | ||
b915543b AV |
158 | static __u32 *classes[AUDIT_SYSCALL_CLASSES]; |
159 | ||
160 | int __init audit_register_class(int class, unsigned *list) | |
161 | { | |
162 | __u32 *p = kzalloc(AUDIT_BITMASK_SIZE * sizeof(__u32), GFP_KERNEL); | |
163 | if (!p) | |
164 | return -ENOMEM; | |
165 | while (*list != ~0U) { | |
166 | unsigned n = *list++; | |
167 | if (n >= AUDIT_BITMASK_SIZE * 32 - AUDIT_SYSCALL_CLASSES) { | |
168 | kfree(p); | |
169 | return -EINVAL; | |
170 | } | |
171 | p[AUDIT_WORD(n)] |= AUDIT_BIT(n); | |
172 | } | |
173 | if (class >= AUDIT_SYSCALL_CLASSES || classes[class]) { | |
174 | kfree(p); | |
175 | return -EINVAL; | |
176 | } | |
177 | classes[class] = p; | |
178 | return 0; | |
179 | } | |
180 | ||
55669bfa AV |
181 | int audit_match_class(int class, unsigned syscall) |
182 | { | |
c926e4f4 | 183 | if (unlikely(syscall >= AUDIT_BITMASK_SIZE * 32)) |
55669bfa AV |
184 | return 0; |
185 | if (unlikely(class >= AUDIT_SYSCALL_CLASSES || !classes[class])) | |
186 | return 0; | |
187 | return classes[class][AUDIT_WORD(syscall)] & AUDIT_BIT(syscall); | |
188 | } | |
189 | ||
327b9eeb | 190 | #ifdef CONFIG_AUDITSYSCALL |
e54dc243 AG |
191 | static inline int audit_match_class_bits(int class, u32 *mask) |
192 | { | |
193 | int i; | |
194 | ||
195 | if (classes[class]) { | |
196 | for (i = 0; i < AUDIT_BITMASK_SIZE; i++) | |
197 | if (mask[i] & classes[class][i]) | |
198 | return 0; | |
199 | } | |
200 | return 1; | |
201 | } | |
202 | ||
203 | static int audit_match_signal(struct audit_entry *entry) | |
204 | { | |
205 | struct audit_field *arch = entry->rule.arch_f; | |
206 | ||
207 | if (!arch) { | |
208 | /* When arch is unspecified, we must check both masks on biarch | |
209 | * as syscall number alone is ambiguous. */ | |
210 | return (audit_match_class_bits(AUDIT_CLASS_SIGNAL, | |
211 | entry->rule.mask) && | |
212 | audit_match_class_bits(AUDIT_CLASS_SIGNAL_32, | |
213 | entry->rule.mask)); | |
214 | } | |
215 | ||
216 | switch(audit_classify_arch(arch->val)) { | |
217 | case 0: /* native */ | |
218 | return (audit_match_class_bits(AUDIT_CLASS_SIGNAL, | |
219 | entry->rule.mask)); | |
220 | case 1: /* 32bit on biarch */ | |
221 | return (audit_match_class_bits(AUDIT_CLASS_SIGNAL_32, | |
222 | entry->rule.mask)); | |
223 | default: | |
224 | return 1; | |
225 | } | |
226 | } | |
327b9eeb | 227 | #endif |
e54dc243 | 228 | |
93315ed6 AG |
229 | /* Common user-space to kernel rule translation. */ |
230 | static inline struct audit_entry *audit_to_entry_common(struct audit_rule *rule) | |
231 | { | |
232 | unsigned listnr; | |
233 | struct audit_entry *entry; | |
93315ed6 AG |
234 | int i, err; |
235 | ||
236 | err = -EINVAL; | |
237 | listnr = rule->flags & ~AUDIT_FILTER_PREPEND; | |
238 | switch(listnr) { | |
239 | default: | |
240 | goto exit_err; | |
93315ed6 AG |
241 | #ifdef CONFIG_AUDITSYSCALL |
242 | case AUDIT_FILTER_ENTRY: | |
7ff68e53 EP |
243 | if (rule->action == AUDIT_ALWAYS) |
244 | goto exit_err; | |
93315ed6 AG |
245 | case AUDIT_FILTER_EXIT: |
246 | case AUDIT_FILTER_TASK: | |
247 | #endif | |
7ff68e53 EP |
248 | case AUDIT_FILTER_USER: |
249 | case AUDIT_FILTER_TYPE: | |
93315ed6 AG |
250 | ; |
251 | } | |
014149cc | 252 | if (unlikely(rule->action == AUDIT_POSSIBLE)) { |
f952d10f | 253 | pr_err("AUDIT_POSSIBLE is deprecated\n"); |
014149cc AV |
254 | goto exit_err; |
255 | } | |
256 | if (rule->action != AUDIT_NEVER && rule->action != AUDIT_ALWAYS) | |
93315ed6 AG |
257 | goto exit_err; |
258 | if (rule->field_count > AUDIT_MAX_FIELDS) | |
259 | goto exit_err; | |
260 | ||
261 | err = -ENOMEM; | |
3dc7e315 DG |
262 | entry = audit_init_entry(rule->field_count); |
263 | if (!entry) | |
93315ed6 | 264 | goto exit_err; |
93315ed6 AG |
265 | |
266 | entry->rule.flags = rule->flags & AUDIT_FILTER_PREPEND; | |
267 | entry->rule.listnr = listnr; | |
268 | entry->rule.action = rule->action; | |
269 | entry->rule.field_count = rule->field_count; | |
93315ed6 AG |
270 | |
271 | for (i = 0; i < AUDIT_BITMASK_SIZE; i++) | |
272 | entry->rule.mask[i] = rule->mask[i]; | |
273 | ||
b915543b AV |
274 | for (i = 0; i < AUDIT_SYSCALL_CLASSES; i++) { |
275 | int bit = AUDIT_BITMASK_SIZE * 32 - i - 1; | |
276 | __u32 *p = &entry->rule.mask[AUDIT_WORD(bit)]; | |
277 | __u32 *class; | |
278 | ||
279 | if (!(*p & AUDIT_BIT(bit))) | |
280 | continue; | |
281 | *p &= ~AUDIT_BIT(bit); | |
282 | class = classes[i]; | |
283 | if (class) { | |
284 | int j; | |
285 | for (j = 0; j < AUDIT_BITMASK_SIZE; j++) | |
286 | entry->rule.mask[j] |= class[j]; | |
287 | } | |
288 | } | |
289 | ||
93315ed6 AG |
290 | return entry; |
291 | ||
292 | exit_err: | |
293 | return ERR_PTR(err); | |
294 | } | |
295 | ||
5af75d8d AV |
296 | static u32 audit_ops[] = |
297 | { | |
298 | [Audit_equal] = AUDIT_EQUAL, | |
299 | [Audit_not_equal] = AUDIT_NOT_EQUAL, | |
300 | [Audit_bitmask] = AUDIT_BIT_MASK, | |
301 | [Audit_bittest] = AUDIT_BIT_TEST, | |
302 | [Audit_lt] = AUDIT_LESS_THAN, | |
303 | [Audit_gt] = AUDIT_GREATER_THAN, | |
304 | [Audit_le] = AUDIT_LESS_THAN_OR_EQUAL, | |
305 | [Audit_ge] = AUDIT_GREATER_THAN_OR_EQUAL, | |
306 | }; | |
307 | ||
308 | static u32 audit_to_op(u32 op) | |
309 | { | |
310 | u32 n; | |
311 | for (n = Audit_equal; n < Audit_bad && audit_ops[n] != op; n++) | |
312 | ; | |
313 | return n; | |
314 | } | |
315 | ||
ab61d38e | 316 | /* check if an audit field is valid */ |
62062cf8 | 317 | static int audit_field_valid(struct audit_entry *entry, struct audit_field *f) |
93315ed6 | 318 | { |
62062cf8 EP |
319 | switch(f->type) { |
320 | case AUDIT_MSGTYPE: | |
321 | if (entry->rule.listnr != AUDIT_FILTER_TYPE && | |
322 | entry->rule.listnr != AUDIT_FILTER_USER) | |
323 | return -EINVAL; | |
324 | break; | |
325 | }; | |
93315ed6 | 326 | |
ab61d38e EP |
327 | switch(f->type) { |
328 | default: | |
329 | return -EINVAL; | |
330 | case AUDIT_UID: | |
331 | case AUDIT_EUID: | |
332 | case AUDIT_SUID: | |
333 | case AUDIT_FSUID: | |
334 | case AUDIT_LOGINUID: | |
335 | case AUDIT_OBJ_UID: | |
336 | case AUDIT_GID: | |
337 | case AUDIT_EGID: | |
338 | case AUDIT_SGID: | |
339 | case AUDIT_FSGID: | |
340 | case AUDIT_OBJ_GID: | |
341 | case AUDIT_PID: | |
342 | case AUDIT_PERS: | |
343 | case AUDIT_MSGTYPE: | |
344 | case AUDIT_PPID: | |
345 | case AUDIT_DEVMAJOR: | |
346 | case AUDIT_DEVMINOR: | |
347 | case AUDIT_EXIT: | |
348 | case AUDIT_SUCCESS: | |
78122037 | 349 | case AUDIT_INODE: |
ab61d38e EP |
350 | /* bit ops are only useful on syscall args */ |
351 | if (f->op == Audit_bitmask || f->op == Audit_bittest) | |
352 | return -EINVAL; | |
353 | break; | |
354 | case AUDIT_ARG0: | |
355 | case AUDIT_ARG1: | |
356 | case AUDIT_ARG2: | |
357 | case AUDIT_ARG3: | |
358 | case AUDIT_SUBJ_USER: | |
359 | case AUDIT_SUBJ_ROLE: | |
360 | case AUDIT_SUBJ_TYPE: | |
361 | case AUDIT_SUBJ_SEN: | |
362 | case AUDIT_SUBJ_CLR: | |
363 | case AUDIT_OBJ_USER: | |
364 | case AUDIT_OBJ_ROLE: | |
365 | case AUDIT_OBJ_TYPE: | |
366 | case AUDIT_OBJ_LEV_LOW: | |
367 | case AUDIT_OBJ_LEV_HIGH: | |
368 | case AUDIT_WATCH: | |
369 | case AUDIT_DIR: | |
370 | case AUDIT_FILTERKEY: | |
371 | break; | |
780a7654 EB |
372 | case AUDIT_LOGINUID_SET: |
373 | if ((f->val != 0) && (f->val != 1)) | |
374 | return -EINVAL; | |
375 | /* FALL THROUGH */ | |
ab61d38e EP |
376 | case AUDIT_ARCH: |
377 | if (f->op != Audit_not_equal && f->op != Audit_equal) | |
378 | return -EINVAL; | |
379 | break; | |
380 | case AUDIT_PERM: | |
381 | if (f->val & ~15) | |
382 | return -EINVAL; | |
383 | break; | |
384 | case AUDIT_FILETYPE: | |
385 | if (f->val & ~S_IFMT) | |
386 | return -EINVAL; | |
387 | break; | |
388 | case AUDIT_FIELD_COMPARE: | |
389 | if (f->val > AUDIT_MAX_FIELD_COMPARE) | |
390 | return -EINVAL; | |
391 | break; | |
392 | }; | |
62062cf8 | 393 | return 0; |
fe7752ba DW |
394 | } |
395 | ||
93315ed6 AG |
396 | /* Translate struct audit_rule_data to kernel's rule respresentation. */ |
397 | static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data, | |
398 | size_t datasz) | |
fe7752ba | 399 | { |
93315ed6 AG |
400 | int err = 0; |
401 | struct audit_entry *entry; | |
402 | void *bufp; | |
3dc7e315 | 403 | size_t remain = datasz - sizeof(struct audit_rule_data); |
fe7752ba | 404 | int i; |
3dc7e315 | 405 | char *str; |
fe7752ba | 406 | |
93315ed6 AG |
407 | entry = audit_to_entry_common((struct audit_rule *)data); |
408 | if (IS_ERR(entry)) | |
409 | goto exit_nofree; | |
fe7752ba | 410 | |
93315ed6 AG |
411 | bufp = data->buf; |
412 | entry->rule.vers_ops = 2; | |
413 | for (i = 0; i < data->field_count; i++) { | |
414 | struct audit_field *f = &entry->rule.fields[i]; | |
415 | ||
416 | err = -EINVAL; | |
5af75d8d AV |
417 | |
418 | f->op = audit_to_op(data->fieldflags[i]); | |
419 | if (f->op == Audit_bad) | |
93315ed6 AG |
420 | goto exit_free; |
421 | ||
93315ed6 | 422 | f->type = data->fields[i]; |
3dc7e315 | 423 | f->val = data->values[i]; |
ca57ec0f EB |
424 | f->uid = INVALID_UID; |
425 | f->gid = INVALID_GID; | |
04305e4a AD |
426 | f->lsm_str = NULL; |
427 | f->lsm_rule = NULL; | |
62062cf8 | 428 | |
780a7654 | 429 | /* Support legacy tests for a valid loginuid */ |
42f74461 | 430 | if ((f->type == AUDIT_LOGINUID) && (f->val == AUDIT_UID_UNSET)) { |
780a7654 EB |
431 | f->type = AUDIT_LOGINUID_SET; |
432 | f->val = 0; | |
433 | } | |
434 | ||
62062cf8 EP |
435 | err = audit_field_valid(entry, f); |
436 | if (err) | |
437 | goto exit_free; | |
438 | ||
439 | err = -EINVAL; | |
ab61d38e | 440 | switch (f->type) { |
780a7654 | 441 | case AUDIT_LOGINUID: |
0a73dccc AV |
442 | case AUDIT_UID: |
443 | case AUDIT_EUID: | |
444 | case AUDIT_SUID: | |
445 | case AUDIT_FSUID: | |
ca57ec0f | 446 | case AUDIT_OBJ_UID: |
ca57ec0f EB |
447 | f->uid = make_kuid(current_user_ns(), f->val); |
448 | if (!uid_valid(f->uid)) | |
449 | goto exit_free; | |
450 | break; | |
0a73dccc AV |
451 | case AUDIT_GID: |
452 | case AUDIT_EGID: | |
453 | case AUDIT_SGID: | |
454 | case AUDIT_FSGID: | |
ca57ec0f | 455 | case AUDIT_OBJ_GID: |
ca57ec0f EB |
456 | f->gid = make_kgid(current_user_ns(), f->val); |
457 | if (!gid_valid(f->gid)) | |
458 | goto exit_free; | |
459 | break; | |
e54dc243 AG |
460 | case AUDIT_ARCH: |
461 | entry->rule.arch_f = f; | |
462 | break; | |
3a6b9f85 DG |
463 | case AUDIT_SUBJ_USER: |
464 | case AUDIT_SUBJ_ROLE: | |
465 | case AUDIT_SUBJ_TYPE: | |
466 | case AUDIT_SUBJ_SEN: | |
467 | case AUDIT_SUBJ_CLR: | |
6e5a2d1d DG |
468 | case AUDIT_OBJ_USER: |
469 | case AUDIT_OBJ_ROLE: | |
470 | case AUDIT_OBJ_TYPE: | |
471 | case AUDIT_OBJ_LEV_LOW: | |
472 | case AUDIT_OBJ_LEV_HIGH: | |
3dc7e315 DG |
473 | str = audit_unpack_string(&bufp, &remain, f->val); |
474 | if (IS_ERR(str)) | |
475 | goto exit_free; | |
476 | entry->rule.buflen += f->val; | |
477 | ||
d7a96f3a | 478 | err = security_audit_rule_init(f->type, f->op, str, |
04305e4a | 479 | (void **)&f->lsm_rule); |
3dc7e315 DG |
480 | /* Keep currently invalid fields around in case they |
481 | * become valid after a policy reload. */ | |
482 | if (err == -EINVAL) { | |
f952d10f RGB |
483 | pr_warn("audit rule for LSM \'%s\' is invalid\n", |
484 | str); | |
3dc7e315 DG |
485 | err = 0; |
486 | } | |
487 | if (err) { | |
488 | kfree(str); | |
489 | goto exit_free; | |
490 | } else | |
04305e4a | 491 | f->lsm_str = str; |
3dc7e315 | 492 | break; |
f368c07d AG |
493 | case AUDIT_WATCH: |
494 | str = audit_unpack_string(&bufp, &remain, f->val); | |
495 | if (IS_ERR(str)) | |
496 | goto exit_free; | |
497 | entry->rule.buflen += f->val; | |
498 | ||
499 | err = audit_to_watch(&entry->rule, str, f->val, f->op); | |
500 | if (err) { | |
501 | kfree(str); | |
502 | goto exit_free; | |
503 | } | |
504 | break; | |
74c3cbe3 AV |
505 | case AUDIT_DIR: |
506 | str = audit_unpack_string(&bufp, &remain, f->val); | |
507 | if (IS_ERR(str)) | |
508 | goto exit_free; | |
509 | entry->rule.buflen += f->val; | |
510 | ||
511 | err = audit_make_tree(&entry->rule, str, f->op); | |
512 | kfree(str); | |
513 | if (err) | |
514 | goto exit_free; | |
515 | break; | |
f368c07d AG |
516 | case AUDIT_INODE: |
517 | err = audit_to_inode(&entry->rule, f); | |
518 | if (err) | |
519 | goto exit_free; | |
520 | break; | |
5adc8a6a | 521 | case AUDIT_FILTERKEY: |
5adc8a6a AG |
522 | if (entry->rule.filterkey || f->val > AUDIT_MAX_KEY_LEN) |
523 | goto exit_free; | |
524 | str = audit_unpack_string(&bufp, &remain, f->val); | |
525 | if (IS_ERR(str)) | |
526 | goto exit_free; | |
527 | entry->rule.buflen += f->val; | |
528 | entry->rule.filterkey = str; | |
529 | break; | |
f368c07d AG |
530 | } |
531 | } | |
532 | ||
5af75d8d AV |
533 | if (entry->rule.inode_f && entry->rule.inode_f->op == Audit_not_equal) |
534 | entry->rule.inode_f = NULL; | |
93315ed6 AG |
535 | |
536 | exit_nofree: | |
537 | return entry; | |
538 | ||
539 | exit_free: | |
373e0f34 CG |
540 | if (entry->rule.watch) |
541 | audit_put_watch(entry->rule.watch); /* matches initial get */ | |
542 | if (entry->rule.tree) | |
543 | audit_put_tree(entry->rule.tree); /* that's the temporary one */ | |
93315ed6 AG |
544 | audit_free_rule(entry); |
545 | return ERR_PTR(err); | |
546 | } | |
547 | ||
548 | /* Pack a filter field's string representation into data block. */ | |
74c3cbe3 | 549 | static inline size_t audit_pack_string(void **bufp, const char *str) |
93315ed6 AG |
550 | { |
551 | size_t len = strlen(str); | |
552 | ||
553 | memcpy(*bufp, str, len); | |
554 | *bufp += len; | |
555 | ||
556 | return len; | |
557 | } | |
558 | ||
93315ed6 AG |
559 | /* Translate kernel rule respresentation to struct audit_rule_data. */ |
560 | static struct audit_rule_data *audit_krule_to_data(struct audit_krule *krule) | |
561 | { | |
562 | struct audit_rule_data *data; | |
563 | void *bufp; | |
564 | int i; | |
565 | ||
566 | data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL); | |
567 | if (unlikely(!data)) | |
0a3b483e | 568 | return NULL; |
93315ed6 AG |
569 | memset(data, 0, sizeof(*data)); |
570 | ||
571 | data->flags = krule->flags | krule->listnr; | |
572 | data->action = krule->action; | |
573 | data->field_count = krule->field_count; | |
574 | bufp = data->buf; | |
575 | for (i = 0; i < data->field_count; i++) { | |
576 | struct audit_field *f = &krule->fields[i]; | |
577 | ||
578 | data->fields[i] = f->type; | |
5af75d8d | 579 | data->fieldflags[i] = audit_ops[f->op]; |
93315ed6 | 580 | switch(f->type) { |
3a6b9f85 DG |
581 | case AUDIT_SUBJ_USER: |
582 | case AUDIT_SUBJ_ROLE: | |
583 | case AUDIT_SUBJ_TYPE: | |
584 | case AUDIT_SUBJ_SEN: | |
585 | case AUDIT_SUBJ_CLR: | |
6e5a2d1d DG |
586 | case AUDIT_OBJ_USER: |
587 | case AUDIT_OBJ_ROLE: | |
588 | case AUDIT_OBJ_TYPE: | |
589 | case AUDIT_OBJ_LEV_LOW: | |
590 | case AUDIT_OBJ_LEV_HIGH: | |
3dc7e315 | 591 | data->buflen += data->values[i] = |
04305e4a | 592 | audit_pack_string(&bufp, f->lsm_str); |
3dc7e315 | 593 | break; |
f368c07d AG |
594 | case AUDIT_WATCH: |
595 | data->buflen += data->values[i] = | |
cfcad62c EP |
596 | audit_pack_string(&bufp, |
597 | audit_watch_path(krule->watch)); | |
f368c07d | 598 | break; |
74c3cbe3 AV |
599 | case AUDIT_DIR: |
600 | data->buflen += data->values[i] = | |
601 | audit_pack_string(&bufp, | |
602 | audit_tree_path(krule->tree)); | |
603 | break; | |
5adc8a6a AG |
604 | case AUDIT_FILTERKEY: |
605 | data->buflen += data->values[i] = | |
606 | audit_pack_string(&bufp, krule->filterkey); | |
607 | break; | |
93315ed6 AG |
608 | default: |
609 | data->values[i] = f->val; | |
610 | } | |
611 | } | |
612 | for (i = 0; i < AUDIT_BITMASK_SIZE; i++) data->mask[i] = krule->mask[i]; | |
613 | ||
614 | return data; | |
615 | } | |
616 | ||
617 | /* Compare two rules in kernel format. Considered success if rules | |
618 | * don't match. */ | |
619 | static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b) | |
620 | { | |
621 | int i; | |
622 | ||
623 | if (a->flags != b->flags || | |
624 | a->listnr != b->listnr || | |
625 | a->action != b->action || | |
626 | a->field_count != b->field_count) | |
fe7752ba DW |
627 | return 1; |
628 | ||
629 | for (i = 0; i < a->field_count; i++) { | |
93315ed6 AG |
630 | if (a->fields[i].type != b->fields[i].type || |
631 | a->fields[i].op != b->fields[i].op) | |
fe7752ba | 632 | return 1; |
93315ed6 AG |
633 | |
634 | switch(a->fields[i].type) { | |
3a6b9f85 DG |
635 | case AUDIT_SUBJ_USER: |
636 | case AUDIT_SUBJ_ROLE: | |
637 | case AUDIT_SUBJ_TYPE: | |
638 | case AUDIT_SUBJ_SEN: | |
639 | case AUDIT_SUBJ_CLR: | |
6e5a2d1d DG |
640 | case AUDIT_OBJ_USER: |
641 | case AUDIT_OBJ_ROLE: | |
642 | case AUDIT_OBJ_TYPE: | |
643 | case AUDIT_OBJ_LEV_LOW: | |
644 | case AUDIT_OBJ_LEV_HIGH: | |
04305e4a | 645 | if (strcmp(a->fields[i].lsm_str, b->fields[i].lsm_str)) |
3dc7e315 DG |
646 | return 1; |
647 | break; | |
f368c07d | 648 | case AUDIT_WATCH: |
cfcad62c EP |
649 | if (strcmp(audit_watch_path(a->watch), |
650 | audit_watch_path(b->watch))) | |
f368c07d AG |
651 | return 1; |
652 | break; | |
74c3cbe3 AV |
653 | case AUDIT_DIR: |
654 | if (strcmp(audit_tree_path(a->tree), | |
655 | audit_tree_path(b->tree))) | |
656 | return 1; | |
657 | break; | |
5adc8a6a AG |
658 | case AUDIT_FILTERKEY: |
659 | /* both filterkeys exist based on above type compare */ | |
660 | if (strcmp(a->filterkey, b->filterkey)) | |
661 | return 1; | |
662 | break; | |
ca57ec0f EB |
663 | case AUDIT_UID: |
664 | case AUDIT_EUID: | |
665 | case AUDIT_SUID: | |
666 | case AUDIT_FSUID: | |
667 | case AUDIT_LOGINUID: | |
668 | case AUDIT_OBJ_UID: | |
669 | if (!uid_eq(a->fields[i].uid, b->fields[i].uid)) | |
670 | return 1; | |
671 | break; | |
672 | case AUDIT_GID: | |
673 | case AUDIT_EGID: | |
674 | case AUDIT_SGID: | |
675 | case AUDIT_FSGID: | |
676 | case AUDIT_OBJ_GID: | |
677 | if (!gid_eq(a->fields[i].gid, b->fields[i].gid)) | |
678 | return 1; | |
679 | break; | |
93315ed6 AG |
680 | default: |
681 | if (a->fields[i].val != b->fields[i].val) | |
682 | return 1; | |
683 | } | |
fe7752ba DW |
684 | } |
685 | ||
686 | for (i = 0; i < AUDIT_BITMASK_SIZE; i++) | |
687 | if (a->mask[i] != b->mask[i]) | |
688 | return 1; | |
689 | ||
690 | return 0; | |
691 | } | |
692 | ||
04305e4a | 693 | /* Duplicate LSM field information. The lsm_rule is opaque, so must be |
3dc7e315 | 694 | * re-initialized. */ |
d7a96f3a | 695 | static inline int audit_dupe_lsm_field(struct audit_field *df, |
3dc7e315 DG |
696 | struct audit_field *sf) |
697 | { | |
698 | int ret = 0; | |
04305e4a | 699 | char *lsm_str; |
3dc7e315 | 700 | |
04305e4a AD |
701 | /* our own copy of lsm_str */ |
702 | lsm_str = kstrdup(sf->lsm_str, GFP_KERNEL); | |
703 | if (unlikely(!lsm_str)) | |
3e1fbd12 | 704 | return -ENOMEM; |
04305e4a | 705 | df->lsm_str = lsm_str; |
3dc7e315 | 706 | |
04305e4a AD |
707 | /* our own (refreshed) copy of lsm_rule */ |
708 | ret = security_audit_rule_init(df->type, df->op, df->lsm_str, | |
709 | (void **)&df->lsm_rule); | |
3dc7e315 DG |
710 | /* Keep currently invalid fields around in case they |
711 | * become valid after a policy reload. */ | |
712 | if (ret == -EINVAL) { | |
f952d10f RGB |
713 | pr_warn("audit rule for LSM \'%s\' is invalid\n", |
714 | df->lsm_str); | |
3dc7e315 DG |
715 | ret = 0; |
716 | } | |
717 | ||
718 | return ret; | |
719 | } | |
720 | ||
721 | /* Duplicate an audit rule. This will be a deep copy with the exception | |
d7a96f3a | 722 | * of the watch - that pointer is carried over. The LSM specific fields |
3dc7e315 | 723 | * will be updated in the copy. The point is to be able to replace the old |
f368c07d AG |
724 | * rule with the new rule in the filterlist, then free the old rule. |
725 | * The rlist element is undefined; list manipulations are handled apart from | |
726 | * the initial copy. */ | |
ae7b8f41 | 727 | struct audit_entry *audit_dupe_rule(struct audit_krule *old) |
3dc7e315 DG |
728 | { |
729 | u32 fcount = old->field_count; | |
730 | struct audit_entry *entry; | |
731 | struct audit_krule *new; | |
5adc8a6a | 732 | char *fk; |
3dc7e315 DG |
733 | int i, err = 0; |
734 | ||
735 | entry = audit_init_entry(fcount); | |
736 | if (unlikely(!entry)) | |
737 | return ERR_PTR(-ENOMEM); | |
738 | ||
739 | new = &entry->rule; | |
740 | new->vers_ops = old->vers_ops; | |
741 | new->flags = old->flags; | |
742 | new->listnr = old->listnr; | |
743 | new->action = old->action; | |
744 | for (i = 0; i < AUDIT_BITMASK_SIZE; i++) | |
745 | new->mask[i] = old->mask[i]; | |
0590b933 | 746 | new->prio = old->prio; |
3dc7e315 | 747 | new->buflen = old->buflen; |
f368c07d | 748 | new->inode_f = old->inode_f; |
3dc7e315 | 749 | new->field_count = old->field_count; |
ae7b8f41 | 750 | |
74c3cbe3 AV |
751 | /* |
752 | * note that we are OK with not refcounting here; audit_match_tree() | |
753 | * never dereferences tree and we can't get false positives there | |
754 | * since we'd have to have rule gone from the list *and* removed | |
755 | * before the chunks found by lookup had been allocated, i.e. before | |
756 | * the beginning of list scan. | |
757 | */ | |
758 | new->tree = old->tree; | |
3dc7e315 DG |
759 | memcpy(new->fields, old->fields, sizeof(struct audit_field) * fcount); |
760 | ||
04305e4a | 761 | /* deep copy this information, updating the lsm_rule fields, because |
3dc7e315 DG |
762 | * the originals will all be freed when the old rule is freed. */ |
763 | for (i = 0; i < fcount; i++) { | |
764 | switch (new->fields[i].type) { | |
3a6b9f85 DG |
765 | case AUDIT_SUBJ_USER: |
766 | case AUDIT_SUBJ_ROLE: | |
767 | case AUDIT_SUBJ_TYPE: | |
768 | case AUDIT_SUBJ_SEN: | |
769 | case AUDIT_SUBJ_CLR: | |
6e5a2d1d DG |
770 | case AUDIT_OBJ_USER: |
771 | case AUDIT_OBJ_ROLE: | |
772 | case AUDIT_OBJ_TYPE: | |
773 | case AUDIT_OBJ_LEV_LOW: | |
774 | case AUDIT_OBJ_LEV_HIGH: | |
d7a96f3a | 775 | err = audit_dupe_lsm_field(&new->fields[i], |
3dc7e315 | 776 | &old->fields[i]); |
5adc8a6a AG |
777 | break; |
778 | case AUDIT_FILTERKEY: | |
779 | fk = kstrdup(old->filterkey, GFP_KERNEL); | |
780 | if (unlikely(!fk)) | |
781 | err = -ENOMEM; | |
782 | else | |
783 | new->filterkey = fk; | |
3dc7e315 DG |
784 | } |
785 | if (err) { | |
786 | audit_free_rule(entry); | |
787 | return ERR_PTR(err); | |
788 | } | |
789 | } | |
790 | ||
ae7b8f41 EP |
791 | if (old->watch) { |
792 | audit_get_watch(old->watch); | |
793 | new->watch = old->watch; | |
f368c07d AG |
794 | } |
795 | ||
3dc7e315 DG |
796 | return entry; |
797 | } | |
798 | ||
f368c07d AG |
799 | /* Find an existing audit rule. |
800 | * Caller must hold audit_filter_mutex to prevent stale rule data. */ | |
801 | static struct audit_entry *audit_find_rule(struct audit_entry *entry, | |
36c4f1b1 | 802 | struct list_head **p) |
f368c07d AG |
803 | { |
804 | struct audit_entry *e, *found = NULL; | |
36c4f1b1 | 805 | struct list_head *list; |
f368c07d AG |
806 | int h; |
807 | ||
36c4f1b1 AV |
808 | if (entry->rule.inode_f) { |
809 | h = audit_hash_ino(entry->rule.inode_f->val); | |
810 | *p = list = &audit_inode_hash[h]; | |
811 | } else if (entry->rule.watch) { | |
f368c07d AG |
812 | /* we don't know the inode number, so must walk entire hash */ |
813 | for (h = 0; h < AUDIT_INODE_BUCKETS; h++) { | |
814 | list = &audit_inode_hash[h]; | |
815 | list_for_each_entry(e, list, list) | |
816 | if (!audit_compare_rule(&entry->rule, &e->rule)) { | |
817 | found = e; | |
818 | goto out; | |
819 | } | |
820 | } | |
821 | goto out; | |
36c4f1b1 AV |
822 | } else { |
823 | *p = list = &audit_filter_list[entry->rule.listnr]; | |
f368c07d AG |
824 | } |
825 | ||
826 | list_for_each_entry(e, list, list) | |
827 | if (!audit_compare_rule(&entry->rule, &e->rule)) { | |
828 | found = e; | |
829 | goto out; | |
830 | } | |
831 | ||
832 | out: | |
833 | return found; | |
834 | } | |
835 | ||
0590b933 AV |
836 | static u64 prio_low = ~0ULL/2; |
837 | static u64 prio_high = ~0ULL/2 - 1; | |
838 | ||
f368c07d | 839 | /* Add rule to given filterlist if not a duplicate. */ |
36c4f1b1 | 840 | static inline int audit_add_rule(struct audit_entry *entry) |
fe7752ba | 841 | { |
93315ed6 | 842 | struct audit_entry *e; |
f368c07d | 843 | struct audit_watch *watch = entry->rule.watch; |
74c3cbe3 | 844 | struct audit_tree *tree = entry->rule.tree; |
36c4f1b1 | 845 | struct list_head *list; |
ae7b8f41 | 846 | int err; |
471a5c7c AV |
847 | #ifdef CONFIG_AUDITSYSCALL |
848 | int dont_count = 0; | |
849 | ||
850 | /* If either of these, don't count towards total */ | |
851 | if (entry->rule.listnr == AUDIT_FILTER_USER || | |
852 | entry->rule.listnr == AUDIT_FILTER_TYPE) | |
853 | dont_count = 1; | |
854 | #endif | |
f368c07d | 855 | |
f368c07d | 856 | mutex_lock(&audit_filter_mutex); |
36c4f1b1 | 857 | e = audit_find_rule(entry, &list); |
f368c07d | 858 | if (e) { |
35fe4d0b | 859 | mutex_unlock(&audit_filter_mutex); |
f368c07d | 860 | err = -EEXIST; |
74c3cbe3 AV |
861 | /* normally audit_add_tree_rule() will free it on failure */ |
862 | if (tree) | |
863 | audit_put_tree(tree); | |
f368c07d AG |
864 | goto error; |
865 | } | |
fe7752ba | 866 | |
f368c07d AG |
867 | if (watch) { |
868 | /* audit_filter_mutex is dropped and re-taken during this call */ | |
ae7b8f41 | 869 | err = audit_add_watch(&entry->rule, &list); |
f368c07d AG |
870 | if (err) { |
871 | mutex_unlock(&audit_filter_mutex); | |
2f992ee8 CG |
872 | /* |
873 | * normally audit_add_tree_rule() will free it | |
874 | * on failure | |
875 | */ | |
876 | if (tree) | |
877 | audit_put_tree(tree); | |
f368c07d AG |
878 | goto error; |
879 | } | |
fe7752ba | 880 | } |
74c3cbe3 AV |
881 | if (tree) { |
882 | err = audit_add_tree_rule(&entry->rule); | |
883 | if (err) { | |
884 | mutex_unlock(&audit_filter_mutex); | |
885 | goto error; | |
886 | } | |
887 | } | |
fe7752ba | 888 | |
0590b933 AV |
889 | entry->rule.prio = ~0ULL; |
890 | if (entry->rule.listnr == AUDIT_FILTER_EXIT) { | |
891 | if (entry->rule.flags & AUDIT_FILTER_PREPEND) | |
892 | entry->rule.prio = ++prio_high; | |
893 | else | |
894 | entry->rule.prio = --prio_low; | |
895 | } | |
896 | ||
fe7752ba | 897 | if (entry->rule.flags & AUDIT_FILTER_PREPEND) { |
e45aa212 AV |
898 | list_add(&entry->rule.list, |
899 | &audit_rules_list[entry->rule.listnr]); | |
fe7752ba | 900 | list_add_rcu(&entry->list, list); |
6a2bceec | 901 | entry->rule.flags &= ~AUDIT_FILTER_PREPEND; |
fe7752ba | 902 | } else { |
e45aa212 AV |
903 | list_add_tail(&entry->rule.list, |
904 | &audit_rules_list[entry->rule.listnr]); | |
fe7752ba DW |
905 | list_add_tail_rcu(&entry->list, list); |
906 | } | |
471a5c7c AV |
907 | #ifdef CONFIG_AUDITSYSCALL |
908 | if (!dont_count) | |
909 | audit_n_rules++; | |
e54dc243 AG |
910 | |
911 | if (!audit_match_signal(entry)) | |
912 | audit_signals++; | |
471a5c7c | 913 | #endif |
f368c07d | 914 | mutex_unlock(&audit_filter_mutex); |
fe7752ba | 915 | |
f368c07d AG |
916 | return 0; |
917 | ||
918 | error: | |
f368c07d AG |
919 | if (watch) |
920 | audit_put_watch(watch); /* tmp watch, matches initial get */ | |
921 | return err; | |
fe7752ba DW |
922 | } |
923 | ||
f368c07d | 924 | /* Remove an existing rule from filterlist. */ |
36c4f1b1 | 925 | static inline int audit_del_rule(struct audit_entry *entry) |
fe7752ba DW |
926 | { |
927 | struct audit_entry *e; | |
cfcad62c | 928 | struct audit_watch *watch = entry->rule.watch; |
74c3cbe3 | 929 | struct audit_tree *tree = entry->rule.tree; |
36c4f1b1 | 930 | struct list_head *list; |
36c4f1b1 | 931 | int ret = 0; |
471a5c7c AV |
932 | #ifdef CONFIG_AUDITSYSCALL |
933 | int dont_count = 0; | |
934 | ||
935 | /* If either of these, don't count towards total */ | |
936 | if (entry->rule.listnr == AUDIT_FILTER_USER || | |
937 | entry->rule.listnr == AUDIT_FILTER_TYPE) | |
938 | dont_count = 1; | |
939 | #endif | |
f368c07d | 940 | |
f368c07d | 941 | mutex_lock(&audit_filter_mutex); |
36c4f1b1 | 942 | e = audit_find_rule(entry, &list); |
f368c07d AG |
943 | if (!e) { |
944 | mutex_unlock(&audit_filter_mutex); | |
945 | ret = -ENOENT; | |
946 | goto out; | |
947 | } | |
948 | ||
cfcad62c | 949 | if (e->rule.watch) |
a05fb6cc | 950 | audit_remove_watch_rule(&e->rule); |
f368c07d | 951 | |
74c3cbe3 AV |
952 | if (e->rule.tree) |
953 | audit_remove_tree_rule(&e->rule); | |
954 | ||
f368c07d | 955 | list_del_rcu(&e->list); |
e45aa212 | 956 | list_del(&e->rule.list); |
f368c07d AG |
957 | call_rcu(&e->rcu, audit_free_rule_rcu); |
958 | ||
471a5c7c AV |
959 | #ifdef CONFIG_AUDITSYSCALL |
960 | if (!dont_count) | |
961 | audit_n_rules--; | |
e54dc243 AG |
962 | |
963 | if (!audit_match_signal(entry)) | |
964 | audit_signals--; | |
471a5c7c | 965 | #endif |
f368c07d AG |
966 | mutex_unlock(&audit_filter_mutex); |
967 | ||
f368c07d | 968 | out: |
cfcad62c EP |
969 | if (watch) |
970 | audit_put_watch(watch); /* match initial get */ | |
74c3cbe3 AV |
971 | if (tree) |
972 | audit_put_tree(tree); /* that's the temporary one */ | |
f368c07d AG |
973 | |
974 | return ret; | |
fe7752ba DW |
975 | } |
976 | ||
93315ed6 | 977 | /* List rules using struct audit_rule_data. */ |
f9441639 | 978 | static void audit_list_rules(__u32 portid, int seq, struct sk_buff_head *q) |
93315ed6 | 979 | { |
9044e6bc | 980 | struct sk_buff *skb; |
e45aa212 | 981 | struct audit_krule *r; |
93315ed6 AG |
982 | int i; |
983 | ||
f368c07d AG |
984 | /* This is a blocking read, so use audit_filter_mutex instead of rcu |
985 | * iterator to sync with list writers. */ | |
93315ed6 | 986 | for (i=0; i<AUDIT_NR_FILTERS; i++) { |
e45aa212 | 987 | list_for_each_entry(r, &audit_rules_list[i], list) { |
f368c07d AG |
988 | struct audit_rule_data *data; |
989 | ||
e45aa212 | 990 | data = audit_krule_to_data(r); |
f368c07d AG |
991 | if (unlikely(!data)) |
992 | break; | |
f9441639 RGB |
993 | skb = audit_make_reply(portid, seq, AUDIT_LIST_RULES, |
994 | 0, 1, data, | |
995 | sizeof(*data) + data->buflen); | |
9044e6bc AV |
996 | if (skb) |
997 | skb_queue_tail(q, skb); | |
93315ed6 AG |
998 | kfree(data); |
999 | } | |
1000 | } | |
f9441639 | 1001 | skb = audit_make_reply(portid, seq, AUDIT_LIST_RULES, 1, 1, NULL, 0); |
9044e6bc AV |
1002 | if (skb) |
1003 | skb_queue_tail(q, skb); | |
93315ed6 AG |
1004 | } |
1005 | ||
5adc8a6a | 1006 | /* Log rule additions and removals */ |
dc9eb698 | 1007 | static void audit_log_rule_change(char *action, struct audit_krule *rule, int res) |
5adc8a6a AG |
1008 | { |
1009 | struct audit_buffer *ab; | |
dc9eb698 | 1010 | uid_t loginuid = from_kuid(&init_user_ns, audit_get_loginuid(current)); |
4440e854 | 1011 | unsigned int sessionid = audit_get_sessionid(current); |
5adc8a6a | 1012 | |
1a6b9f23 EP |
1013 | if (!audit_enabled) |
1014 | return; | |
1015 | ||
5adc8a6a AG |
1016 | ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE); |
1017 | if (!ab) | |
1018 | return; | |
dc9eb698 | 1019 | audit_log_format(ab, "auid=%u ses=%u" ,loginuid, sessionid); |
b122c376 | 1020 | audit_log_task_context(ab); |
9d960985 EP |
1021 | audit_log_format(ab, " op="); |
1022 | audit_log_string(ab, action); | |
1023 | audit_log_key(ab, rule->filterkey); | |
5adc8a6a AG |
1024 | audit_log_format(ab, " list=%d res=%d", rule->listnr, res); |
1025 | audit_log_end(ab); | |
1026 | } | |
1027 | ||
fe7752ba | 1028 | /** |
ce0d9f04 | 1029 | * audit_rule_change - apply all rules to the specified message type |
fe7752ba | 1030 | * @type: audit message type |
f9441639 | 1031 | * @portid: target port id for netlink audit messages |
fe7752ba DW |
1032 | * @seq: netlink audit message sequence (serial) number |
1033 | * @data: payload data | |
93315ed6 | 1034 | * @datasz: size of payload data |
fe7752ba | 1035 | */ |
ce0d9f04 RGB |
1036 | int audit_rule_change(int type, __u32 portid, int seq, void *data, |
1037 | size_t datasz) | |
fe7752ba | 1038 | { |
93315ed6 AG |
1039 | int err = 0; |
1040 | struct audit_entry *entry; | |
fe7752ba DW |
1041 | |
1042 | switch (type) { | |
93315ed6 | 1043 | case AUDIT_ADD_RULE: |
18900909 | 1044 | entry = audit_data_to_entry(data, datasz); |
93315ed6 AG |
1045 | if (IS_ERR(entry)) |
1046 | return PTR_ERR(entry); | |
1047 | ||
36c4f1b1 | 1048 | err = audit_add_rule(entry); |
dc9eb698 | 1049 | audit_log_rule_change("add rule", &entry->rule, !err); |
5d330108 | 1050 | if (err) |
93315ed6 | 1051 | audit_free_rule(entry); |
fe7752ba | 1052 | break; |
93315ed6 | 1053 | case AUDIT_DEL_RULE: |
18900909 | 1054 | entry = audit_data_to_entry(data, datasz); |
93315ed6 AG |
1055 | if (IS_ERR(entry)) |
1056 | return PTR_ERR(entry); | |
1057 | ||
36c4f1b1 | 1058 | err = audit_del_rule(entry); |
dc9eb698 | 1059 | audit_log_rule_change("remove rule", &entry->rule, !err); |
93315ed6 | 1060 | audit_free_rule(entry); |
fe7752ba DW |
1061 | break; |
1062 | default: | |
1063 | return -EINVAL; | |
1064 | } | |
1065 | ||
1066 | return err; | |
1067 | } | |
1068 | ||
ce0d9f04 RGB |
1069 | /** |
1070 | * audit_list_rules_send - list the audit rules | |
1071 | * @portid: target portid for netlink audit messages | |
1072 | * @seq: netlink audit message sequence (serial) number | |
1073 | */ | |
1074 | int audit_list_rules_send(__u32 portid, int seq) | |
1075 | { | |
1076 | struct task_struct *tsk; | |
1077 | struct audit_netlink_list *dest; | |
1078 | int err = 0; | |
1079 | ||
1080 | /* We can't just spew out the rules here because we might fill | |
1081 | * the available socket buffer space and deadlock waiting for | |
1082 | * auditctl to read from it... which isn't ever going to | |
1083 | * happen if we're actually running in the context of auditctl | |
1084 | * trying to _send_ the stuff */ | |
1085 | ||
1086 | dest = kmalloc(sizeof(struct audit_netlink_list), GFP_KERNEL); | |
1087 | if (!dest) | |
1088 | return -ENOMEM; | |
638a0fd2 | 1089 | dest->net = get_net(current->nsproxy->net_ns); |
ce0d9f04 | 1090 | dest->portid = portid; |
ce0d9f04 RGB |
1091 | skb_queue_head_init(&dest->q); |
1092 | ||
1093 | mutex_lock(&audit_filter_mutex); | |
1094 | audit_list_rules(portid, seq, &dest->q); | |
1095 | mutex_unlock(&audit_filter_mutex); | |
1096 | ||
1097 | tsk = kthread_run(audit_send_list, dest, "audit_send_list"); | |
1098 | if (IS_ERR(tsk)) { | |
1099 | skb_queue_purge(&dest->q); | |
1100 | kfree(dest); | |
1101 | err = PTR_ERR(tsk); | |
1102 | } | |
1103 | ||
1104 | return err; | |
1105 | } | |
1106 | ||
5af75d8d | 1107 | int audit_comparator(u32 left, u32 op, u32 right) |
fe7752ba DW |
1108 | { |
1109 | switch (op) { | |
5af75d8d | 1110 | case Audit_equal: |
fe7752ba | 1111 | return (left == right); |
5af75d8d | 1112 | case Audit_not_equal: |
fe7752ba | 1113 | return (left != right); |
5af75d8d | 1114 | case Audit_lt: |
fe7752ba | 1115 | return (left < right); |
5af75d8d | 1116 | case Audit_le: |
fe7752ba | 1117 | return (left <= right); |
5af75d8d | 1118 | case Audit_gt: |
fe7752ba | 1119 | return (left > right); |
5af75d8d | 1120 | case Audit_ge: |
fe7752ba | 1121 | return (left >= right); |
5af75d8d | 1122 | case Audit_bitmask: |
74f2345b | 1123 | return (left & right); |
5af75d8d | 1124 | case Audit_bittest: |
74f2345b | 1125 | return ((left & right) == right); |
5af75d8d AV |
1126 | default: |
1127 | BUG(); | |
1128 | return 0; | |
fe7752ba DW |
1129 | } |
1130 | } | |
1131 | ||
ca57ec0f EB |
1132 | int audit_uid_comparator(kuid_t left, u32 op, kuid_t right) |
1133 | { | |
1134 | switch (op) { | |
1135 | case Audit_equal: | |
1136 | return uid_eq(left, right); | |
1137 | case Audit_not_equal: | |
1138 | return !uid_eq(left, right); | |
1139 | case Audit_lt: | |
1140 | return uid_lt(left, right); | |
1141 | case Audit_le: | |
1142 | return uid_lte(left, right); | |
1143 | case Audit_gt: | |
1144 | return uid_gt(left, right); | |
1145 | case Audit_ge: | |
1146 | return uid_gte(left, right); | |
1147 | case Audit_bitmask: | |
1148 | case Audit_bittest: | |
1149 | default: | |
1150 | BUG(); | |
1151 | return 0; | |
1152 | } | |
1153 | } | |
1154 | ||
1155 | int audit_gid_comparator(kgid_t left, u32 op, kgid_t right) | |
1156 | { | |
1157 | switch (op) { | |
1158 | case Audit_equal: | |
1159 | return gid_eq(left, right); | |
1160 | case Audit_not_equal: | |
1161 | return !gid_eq(left, right); | |
1162 | case Audit_lt: | |
1163 | return gid_lt(left, right); | |
1164 | case Audit_le: | |
1165 | return gid_lte(left, right); | |
1166 | case Audit_gt: | |
1167 | return gid_gt(left, right); | |
1168 | case Audit_ge: | |
1169 | return gid_gte(left, right); | |
1170 | case Audit_bitmask: | |
1171 | case Audit_bittest: | |
1172 | default: | |
1173 | BUG(); | |
1174 | return 0; | |
1175 | } | |
1176 | } | |
1177 | ||
bfcec708 JL |
1178 | /** |
1179 | * parent_len - find the length of the parent portion of a pathname | |
1180 | * @path: pathname of which to determine length | |
1181 | */ | |
1182 | int parent_len(const char *path) | |
1183 | { | |
1184 | int plen; | |
1185 | const char *p; | |
1186 | ||
1187 | plen = strlen(path); | |
1188 | ||
1189 | if (plen == 0) | |
1190 | return plen; | |
1191 | ||
1192 | /* disregard trailing slashes */ | |
1193 | p = path + plen - 1; | |
1194 | while ((*p == '/') && (p > path)) | |
1195 | p--; | |
1196 | ||
1197 | /* walk backward until we find the next slash or hit beginning */ | |
1198 | while ((*p != '/') && (p > path)) | |
1199 | p--; | |
1200 | ||
1201 | /* did we find a slash? Then increment to include it in path */ | |
1202 | if (*p == '/') | |
1203 | p++; | |
1204 | ||
1205 | return p - path; | |
1206 | } | |
1207 | ||
e3d6b07b JL |
1208 | /** |
1209 | * audit_compare_dname_path - compare given dentry name with last component in | |
1210 | * given path. Return of 0 indicates a match. | |
1211 | * @dname: dentry name that we're comparing | |
1212 | * @path: full pathname that we're comparing | |
1213 | * @parentlen: length of the parent if known. Passing in AUDIT_NAME_FULL | |
1214 | * here indicates that we must compute this value. | |
1215 | */ | |
1216 | int audit_compare_dname_path(const char *dname, const char *path, int parentlen) | |
f368c07d | 1217 | { |
e3d6b07b | 1218 | int dlen, pathlen; |
f368c07d AG |
1219 | const char *p; |
1220 | ||
f368c07d | 1221 | dlen = strlen(dname); |
29e9a346 EP |
1222 | pathlen = strlen(path); |
1223 | if (pathlen < dlen) | |
f368c07d AG |
1224 | return 1; |
1225 | ||
e3d6b07b | 1226 | parentlen = parentlen == AUDIT_NAME_FULL ? parent_len(path) : parentlen; |
29e9a346 | 1227 | if (pathlen - parentlen != dlen) |
f368c07d | 1228 | return 1; |
29e9a346 EP |
1229 | |
1230 | p = path + parentlen; | |
fe7752ba | 1231 | |
f368c07d AG |
1232 | return strncmp(p, dname, dlen); |
1233 | } | |
fe7752ba | 1234 | |
62062cf8 | 1235 | static int audit_filter_user_rules(struct audit_krule *rule, int type, |
fe7752ba DW |
1236 | enum audit_state *state) |
1237 | { | |
1238 | int i; | |
1239 | ||
1240 | for (i = 0; i < rule->field_count; i++) { | |
93315ed6 | 1241 | struct audit_field *f = &rule->fields[i]; |
fe7752ba | 1242 | int result = 0; |
c53fa1ed | 1243 | u32 sid; |
fe7752ba | 1244 | |
93315ed6 | 1245 | switch (f->type) { |
fe7752ba | 1246 | case AUDIT_PID: |
02276bda | 1247 | result = audit_comparator(task_pid_vnr(current), f->op, f->val); |
fe7752ba DW |
1248 | break; |
1249 | case AUDIT_UID: | |
ca57ec0f | 1250 | result = audit_uid_comparator(current_uid(), f->op, f->uid); |
fe7752ba DW |
1251 | break; |
1252 | case AUDIT_GID: | |
ca57ec0f | 1253 | result = audit_gid_comparator(current_gid(), f->op, f->gid); |
fe7752ba DW |
1254 | break; |
1255 | case AUDIT_LOGINUID: | |
ca57ec0f EB |
1256 | result = audit_uid_comparator(audit_get_loginuid(current), |
1257 | f->op, f->uid); | |
fe7752ba | 1258 | break; |
780a7654 EB |
1259 | case AUDIT_LOGINUID_SET: |
1260 | result = audit_comparator(audit_loginuid_set(current), | |
1261 | f->op, f->val); | |
1262 | break; | |
62062cf8 EP |
1263 | case AUDIT_MSGTYPE: |
1264 | result = audit_comparator(type, f->op, f->val); | |
1265 | break; | |
d29be158 MT |
1266 | case AUDIT_SUBJ_USER: |
1267 | case AUDIT_SUBJ_ROLE: | |
1268 | case AUDIT_SUBJ_TYPE: | |
1269 | case AUDIT_SUBJ_SEN: | |
1270 | case AUDIT_SUBJ_CLR: | |
c53fa1ed PM |
1271 | if (f->lsm_rule) { |
1272 | security_task_getsecid(current, &sid); | |
1273 | result = security_audit_rule_match(sid, | |
d29be158 MT |
1274 | f->type, |
1275 | f->op, | |
1276 | f->lsm_rule, | |
1277 | NULL); | |
c53fa1ed | 1278 | } |
d29be158 | 1279 | break; |
fe7752ba DW |
1280 | } |
1281 | ||
1282 | if (!result) | |
1283 | return 0; | |
1284 | } | |
1285 | switch (rule->action) { | |
1286 | case AUDIT_NEVER: *state = AUDIT_DISABLED; break; | |
fe7752ba DW |
1287 | case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break; |
1288 | } | |
1289 | return 1; | |
1290 | } | |
1291 | ||
62062cf8 | 1292 | int audit_filter_user(int type) |
fe7752ba | 1293 | { |
11f57ced | 1294 | enum audit_state state = AUDIT_DISABLED; |
fe7752ba | 1295 | struct audit_entry *e; |
724e4fcc RGB |
1296 | int rc, ret; |
1297 | ||
1298 | ret = 1; /* Audit by default */ | |
fe7752ba DW |
1299 | |
1300 | rcu_read_lock(); | |
1301 | list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) { | |
724e4fcc RGB |
1302 | rc = audit_filter_user_rules(&e->rule, type, &state); |
1303 | if (rc) { | |
1304 | if (rc > 0 && state == AUDIT_DISABLED) | |
fe7752ba DW |
1305 | ret = 0; |
1306 | break; | |
1307 | } | |
1308 | } | |
1309 | rcu_read_unlock(); | |
1310 | ||
724e4fcc | 1311 | return ret; |
fe7752ba DW |
1312 | } |
1313 | ||
1314 | int audit_filter_type(int type) | |
1315 | { | |
1316 | struct audit_entry *e; | |
1317 | int result = 0; | |
9ce34218 | 1318 | |
fe7752ba DW |
1319 | rcu_read_lock(); |
1320 | if (list_empty(&audit_filter_list[AUDIT_FILTER_TYPE])) | |
1321 | goto unlock_and_return; | |
1322 | ||
1323 | list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TYPE], | |
1324 | list) { | |
fe7752ba | 1325 | int i; |
93315ed6 AG |
1326 | for (i = 0; i < e->rule.field_count; i++) { |
1327 | struct audit_field *f = &e->rule.fields[i]; | |
1328 | if (f->type == AUDIT_MSGTYPE) { | |
1329 | result = audit_comparator(type, f->op, f->val); | |
fe7752ba DW |
1330 | if (!result) |
1331 | break; | |
1332 | } | |
1333 | } | |
1334 | if (result) | |
1335 | goto unlock_and_return; | |
1336 | } | |
1337 | unlock_and_return: | |
1338 | rcu_read_unlock(); | |
1339 | return result; | |
1340 | } | |
3dc7e315 | 1341 | |
e45aa212 | 1342 | static int update_lsm_rule(struct audit_krule *r) |
1a9d0797 | 1343 | { |
e45aa212 | 1344 | struct audit_entry *entry = container_of(r, struct audit_entry, rule); |
1a9d0797 | 1345 | struct audit_entry *nentry; |
1a9d0797 AV |
1346 | int err = 0; |
1347 | ||
e45aa212 | 1348 | if (!security_audit_rule_known(r)) |
1a9d0797 AV |
1349 | return 0; |
1350 | ||
ae7b8f41 | 1351 | nentry = audit_dupe_rule(r); |
1a9d0797 AV |
1352 | if (IS_ERR(nentry)) { |
1353 | /* save the first error encountered for the | |
1354 | * return value */ | |
1355 | err = PTR_ERR(nentry); | |
1356 | audit_panic("error updating LSM filters"); | |
ae7b8f41 | 1357 | if (r->watch) |
e45aa212 | 1358 | list_del(&r->rlist); |
1a9d0797 | 1359 | list_del_rcu(&entry->list); |
e45aa212 | 1360 | list_del(&r->list); |
1a9d0797 | 1361 | } else { |
ae7b8f41 | 1362 | if (r->watch || r->tree) |
e45aa212 | 1363 | list_replace_init(&r->rlist, &nentry->rule.rlist); |
1a9d0797 | 1364 | list_replace_rcu(&entry->list, &nentry->list); |
e45aa212 | 1365 | list_replace(&r->list, &nentry->rule.list); |
1a9d0797 AV |
1366 | } |
1367 | call_rcu(&entry->rcu, audit_free_rule_rcu); | |
1368 | ||
1369 | return err; | |
1370 | } | |
1371 | ||
04305e4a | 1372 | /* This function will re-initialize the lsm_rule field of all applicable rules. |
d7a96f3a | 1373 | * It will traverse the filter lists serarching for rules that contain LSM |
3dc7e315 | 1374 | * specific filter fields. When such a rule is found, it is copied, the |
d7a96f3a | 1375 | * LSM field is re-initialized, and the old rule is replaced with the |
3dc7e315 | 1376 | * updated rule. */ |
d7a96f3a | 1377 | int audit_update_lsm_rules(void) |
3dc7e315 | 1378 | { |
e45aa212 | 1379 | struct audit_krule *r, *n; |
3dc7e315 DG |
1380 | int i, err = 0; |
1381 | ||
f368c07d AG |
1382 | /* audit_filter_mutex synchronizes the writers */ |
1383 | mutex_lock(&audit_filter_mutex); | |
3dc7e315 DG |
1384 | |
1385 | for (i = 0; i < AUDIT_NR_FILTERS; i++) { | |
e45aa212 AV |
1386 | list_for_each_entry_safe(r, n, &audit_rules_list[i], list) { |
1387 | int res = update_lsm_rule(r); | |
1a9d0797 AV |
1388 | if (!err) |
1389 | err = res; | |
3dc7e315 DG |
1390 | } |
1391 | } | |
f368c07d | 1392 | mutex_unlock(&audit_filter_mutex); |
3dc7e315 DG |
1393 | |
1394 | return err; | |
1395 | } |