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