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