blob: 92062fd6cc8cec4deff933c04848782bfdcded11 [file] [log] [blame]
David Woodhousefe7752b2005-12-15 18:33:52 +00001/* 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>
Amy Griffisf368c07d2006-04-07 16:55:56 -040025#include <linux/mutex.h>
26#include <linux/fs.h>
27#include <linux/namei.h>
David Woodhousefe7752b2005-12-15 18:33:52 +000028#include <linux/netlink.h>
Amy Griffisf368c07d2006-04-07 16:55:56 -040029#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +020031#include <linux/security.h>
Eric W. Biederman48095d92014-02-03 17:25:33 -080032#include <net/net_namespace.h>
Eric W. Biederman6f285b12014-02-28 19:44:55 -080033#include <net/sock.h>
David Woodhousefe7752b2005-12-15 18:33:52 +000034#include "audit.h"
35
Amy Griffisf368c07d2006-04-07 16:55:56 -040036/*
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
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +020043 * LSM rules during filtering. If modified, these structures
Amy Griffisf368c07d2006-04-07 16:55:56 -040044 * 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
Amy Griffisf368c07d2006-04-07 16:55:56 -040049/* Audit filter lists, defined in <linux/audit.h> */
David Woodhousefe7752b2005-12-15 18:33:52 +000050struct 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};
Al Viroe45aa212008-12-15 01:17:50 -050061static 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};
David Woodhousefe7752b2005-12-15 18:33:52 +000069
Al Viro74c3cbe2007-07-22 08:04:18 -040070DEFINE_MUTEX(audit_filter_mutex);
Amy Griffisf368c07d2006-04-07 16:55:56 -040071
Amy Griffis93315ed2006-02-07 12:05:27 -050072static inline void audit_free_rule(struct audit_entry *e)
David Woodhousefe7752b2005-12-15 18:33:52 +000073{
Darrel Goeddel3dc7e312006-03-10 18:14:06 -060074 int i;
Zhenwen Xuc28bb7d2009-03-12 22:16:12 +080075 struct audit_krule *erule = &e->rule;
Eric Parisae7b8f42009-12-17 20:12:04 -050076
Amy Griffisf368c07d2006-04-07 16:55:56 -040077 /* some rules don't have associated watches */
Zhenwen Xuc28bb7d2009-03-12 22:16:12 +080078 if (erule->watch)
79 audit_put_watch(erule->watch);
80 if (erule->fields)
81 for (i = 0; i < erule->field_count; i++) {
82 struct audit_field *f = &erule->fields[i];
Ahmed S. Darwish04305e42008-04-19 09:59:43 +100083 kfree(f->lsm_str);
84 security_audit_rule_free(f->lsm_rule);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -060085 }
Zhenwen Xuc28bb7d2009-03-12 22:16:12 +080086 kfree(erule->fields);
87 kfree(erule->filterkey);
Amy Griffis93315ed2006-02-07 12:05:27 -050088 kfree(e);
David Woodhousefe7752b2005-12-15 18:33:52 +000089}
90
Al Viro74c3cbe2007-07-22 08:04:18 -040091void audit_free_rule_rcu(struct rcu_head *head)
Amy Griffis93315ed2006-02-07 12:05:27 -050092{
93 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
94 audit_free_rule(e);
95}
96
Darrel Goeddel3dc7e312006-03-10 18:14:06 -060097/* Initialize an audit filterlist entry. */
98static 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 if (unlikely(!entry))
105 return NULL;
106
107 fields = kzalloc(sizeof(*fields) * field_count, GFP_KERNEL);
108 if (unlikely(!fields)) {
109 kfree(entry);
110 return NULL;
111 }
112 entry->rule.fields = fields;
113
114 return entry;
115}
116
Amy Griffis93315ed2006-02-07 12:05:27 -0500117/* Unpack a filter field's string representation from user-space
118 * buffer. */
Al Viro74c3cbe2007-07-22 08:04:18 -0400119char *audit_unpack_string(void **bufp, size_t *remain, size_t len)
Amy Griffis93315ed2006-02-07 12:05:27 -0500120{
121 char *str;
122
123 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 if (len > PATH_MAX)
130 return ERR_PTR(-ENAMETOOLONG);
131
132 str = kmalloc(len + 1, GFP_KERNEL);
133 if (unlikely(!str))
134 return ERR_PTR(-ENOMEM);
135
136 memcpy(str, *bufp, len);
137 str[len] = 0;
138 *bufp += len;
139 *remain -= len;
140
141 return str;
142}
143
Amy Griffisf368c07d2006-04-07 16:55:56 -0400144/* Translate an inode field to kernel respresentation. */
145static inline int audit_to_inode(struct audit_krule *krule,
146 struct audit_field *f)
147{
148 if (krule->listnr != AUDIT_FILTER_EXIT ||
Al Viro5af75d82008-12-16 05:59:26 -0500149 krule->watch || krule->inode_f || krule->tree ||
150 (f->op != Audit_equal && f->op != Audit_not_equal))
Amy Griffisf368c07d2006-04-07 16:55:56 -0400151 return -EINVAL;
152
153 krule->inode_f = f;
154 return 0;
155}
156
Al Virob9155432006-07-01 03:56:16 -0400157static __u32 *classes[AUDIT_SYSCALL_CLASSES];
158
159int __init audit_register_class(int class, unsigned *list)
160{
161 __u32 *p = kzalloc(AUDIT_BITMASK_SIZE * sizeof(__u32), GFP_KERNEL);
162 if (!p)
163 return -ENOMEM;
164 while (*list != ~0U) {
165 unsigned n = *list++;
166 if (n >= AUDIT_BITMASK_SIZE * 32 - AUDIT_SYSCALL_CLASSES) {
167 kfree(p);
168 return -EINVAL;
169 }
170 p[AUDIT_WORD(n)] |= AUDIT_BIT(n);
171 }
172 if (class >= AUDIT_SYSCALL_CLASSES || classes[class]) {
173 kfree(p);
174 return -EINVAL;
175 }
176 classes[class] = p;
177 return 0;
178}
179
Al Viro55669bf2006-08-31 19:26:40 -0400180int audit_match_class(int class, unsigned syscall)
181{
Klaus Weidnerc926e4f2007-05-16 17:45:42 -0500182 if (unlikely(syscall >= AUDIT_BITMASK_SIZE * 32))
Al Viro55669bf2006-08-31 19:26:40 -0400183 return 0;
184 if (unlikely(class >= AUDIT_SYSCALL_CLASSES || !classes[class]))
185 return 0;
186 return classes[class][AUDIT_WORD(syscall)] & AUDIT_BIT(syscall);
187}
188
Al Viro327b9ee2007-05-15 20:37:10 +0100189#ifdef CONFIG_AUDITSYSCALL
Amy Griffise54dc242007-03-29 18:01:04 -0400190static inline int audit_match_class_bits(int class, u32 *mask)
191{
192 int i;
193
194 if (classes[class]) {
195 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
196 if (mask[i] & classes[class][i])
197 return 0;
198 }
199 return 1;
200}
201
202static int audit_match_signal(struct audit_entry *entry)
203{
204 struct audit_field *arch = entry->rule.arch_f;
205
206 if (!arch) {
207 /* When arch is unspecified, we must check both masks on biarch
208 * as syscall number alone is ambiguous. */
209 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL,
210 entry->rule.mask) &&
211 audit_match_class_bits(AUDIT_CLASS_SIGNAL_32,
212 entry->rule.mask));
213 }
214
215 switch(audit_classify_arch(arch->val)) {
216 case 0: /* native */
217 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL,
218 entry->rule.mask));
219 case 1: /* 32bit on biarch */
220 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL_32,
221 entry->rule.mask));
222 default:
223 return 1;
224 }
225}
Al Viro327b9ee2007-05-15 20:37:10 +0100226#endif
Amy Griffise54dc242007-03-29 18:01:04 -0400227
Amy Griffis93315ed2006-02-07 12:05:27 -0500228/* Common user-space to kernel rule translation. */
229static inline struct audit_entry *audit_to_entry_common(struct audit_rule *rule)
230{
231 unsigned listnr;
232 struct audit_entry *entry;
Amy Griffis93315ed2006-02-07 12:05:27 -0500233 int i, err;
234
235 err = -EINVAL;
236 listnr = rule->flags & ~AUDIT_FILTER_PREPEND;
237 switch(listnr) {
238 default:
239 goto exit_err;
Amy Griffis93315ed2006-02-07 12:05:27 -0500240#ifdef CONFIG_AUDITSYSCALL
241 case AUDIT_FILTER_ENTRY:
Eric Paris7ff68e52012-01-03 14:23:07 -0500242 if (rule->action == AUDIT_ALWAYS)
243 goto exit_err;
Amy Griffis93315ed2006-02-07 12:05:27 -0500244 case AUDIT_FILTER_EXIT:
245 case AUDIT_FILTER_TASK:
246#endif
Eric Paris7ff68e52012-01-03 14:23:07 -0500247 case AUDIT_FILTER_USER:
248 case AUDIT_FILTER_TYPE:
Amy Griffis93315ed2006-02-07 12:05:27 -0500249 ;
250 }
Al Viro014149c2006-05-23 01:36:13 -0400251 if (unlikely(rule->action == AUDIT_POSSIBLE)) {
252 printk(KERN_ERR "AUDIT_POSSIBLE is deprecated\n");
253 goto exit_err;
254 }
255 if (rule->action != AUDIT_NEVER && rule->action != AUDIT_ALWAYS)
Amy Griffis93315ed2006-02-07 12:05:27 -0500256 goto exit_err;
257 if (rule->field_count > AUDIT_MAX_FIELDS)
258 goto exit_err;
259
260 err = -ENOMEM;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600261 entry = audit_init_entry(rule->field_count);
262 if (!entry)
Amy Griffis93315ed2006-02-07 12:05:27 -0500263 goto exit_err;
Amy Griffis93315ed2006-02-07 12:05:27 -0500264
265 entry->rule.flags = rule->flags & AUDIT_FILTER_PREPEND;
266 entry->rule.listnr = listnr;
267 entry->rule.action = rule->action;
268 entry->rule.field_count = rule->field_count;
Amy Griffis93315ed2006-02-07 12:05:27 -0500269
270 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
271 entry->rule.mask[i] = rule->mask[i];
272
Al Virob9155432006-07-01 03:56:16 -0400273 for (i = 0; i < AUDIT_SYSCALL_CLASSES; i++) {
274 int bit = AUDIT_BITMASK_SIZE * 32 - i - 1;
275 __u32 *p = &entry->rule.mask[AUDIT_WORD(bit)];
276 __u32 *class;
277
278 if (!(*p & AUDIT_BIT(bit)))
279 continue;
280 *p &= ~AUDIT_BIT(bit);
281 class = classes[i];
282 if (class) {
283 int j;
284 for (j = 0; j < AUDIT_BITMASK_SIZE; j++)
285 entry->rule.mask[j] |= class[j];
286 }
287 }
288
Amy Griffis93315ed2006-02-07 12:05:27 -0500289 return entry;
290
291exit_err:
292 return ERR_PTR(err);
293}
294
Al Viro5af75d82008-12-16 05:59:26 -0500295static 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
307static u32 audit_to_op(u32 op)
308{
309 u32 n;
310 for (n = Audit_equal; n < Audit_bad && audit_ops[n] != op; n++)
311 ;
312 return n;
313}
314
Eric Parisab61d382013-04-16 17:26:51 -0400315/* check if an audit field is valid */
Eric Paris62062cf2013-04-16 13:08:43 -0400316static int audit_field_valid(struct audit_entry *entry, struct audit_field *f)
Amy Griffis93315ed2006-02-07 12:05:27 -0500317{
Eric Paris62062cf2013-04-16 13:08:43 -0400318 switch(f->type) {
319 case AUDIT_MSGTYPE:
320 if (entry->rule.listnr != AUDIT_FILTER_TYPE &&
321 entry->rule.listnr != AUDIT_FILTER_USER)
322 return -EINVAL;
323 break;
324 };
Amy Griffis93315ed2006-02-07 12:05:27 -0500325
Eric Parisab61d382013-04-16 17:26:51 -0400326 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:
Eric Paris78122032013-09-04 15:01:43 -0400348 case AUDIT_INODE:
Eric Parisab61d382013-04-16 17:26:51 -0400349 /* bit ops are only useful on syscall args */
350 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;
Eric W. Biederman780a7652013-04-09 02:22:10 -0700371 case AUDIT_LOGINUID_SET:
372 if ((f->val != 0) && (f->val != 1))
373 return -EINVAL;
374 /* FALL THROUGH */
Eric Parisab61d382013-04-16 17:26:51 -0400375 case AUDIT_ARCH:
376 if (f->op != Audit_not_equal && f->op != Audit_equal)
377 return -EINVAL;
378 break;
379 case AUDIT_PERM:
380 if (f->val & ~15)
381 return -EINVAL;
382 break;
383 case AUDIT_FILETYPE:
384 if (f->val & ~S_IFMT)
385 return -EINVAL;
386 break;
387 case AUDIT_FIELD_COMPARE:
388 if (f->val > AUDIT_MAX_FIELD_COMPARE)
389 return -EINVAL;
390 break;
391 };
Eric Paris62062cf2013-04-16 13:08:43 -0400392 return 0;
Amy Griffis93315ed2006-02-07 12:05:27 -0500393}
394
395/* Translate struct audit_rule_data to kernel's rule respresentation. */
396static 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;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600402 size_t remain = datasz - sizeof(struct audit_rule_data);
Amy Griffis93315ed2006-02-07 12:05:27 -0500403 int i;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600404 char *str;
Amy Griffis93315ed2006-02-07 12:05:27 -0500405
406 entry = audit_to_entry_common((struct audit_rule *)data);
407 if (IS_ERR(entry))
408 goto exit_nofree;
409
410 bufp = data->buf;
411 entry->rule.vers_ops = 2;
412 for (i = 0; i < data->field_count; i++) {
413 struct audit_field *f = &entry->rule.fields[i];
414
415 err = -EINVAL;
Al Viro5af75d82008-12-16 05:59:26 -0500416
417 f->op = audit_to_op(data->fieldflags[i]);
418 if (f->op == Audit_bad)
Amy Griffis93315ed2006-02-07 12:05:27 -0500419 goto exit_free;
420
Amy Griffis93315ed2006-02-07 12:05:27 -0500421 f->type = data->fields[i];
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600422 f->val = data->values[i];
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700423 f->uid = INVALID_UID;
424 f->gid = INVALID_GID;
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000425 f->lsm_str = NULL;
426 f->lsm_rule = NULL;
Eric Paris62062cf2013-04-16 13:08:43 -0400427
Eric W. Biederman780a7652013-04-09 02:22:10 -0700428 /* Support legacy tests for a valid loginuid */
Richard Guy Briggs42f74462013-05-20 15:08:18 -0400429 if ((f->type == AUDIT_LOGINUID) && (f->val == AUDIT_UID_UNSET)) {
Eric W. Biederman780a7652013-04-09 02:22:10 -0700430 f->type = AUDIT_LOGINUID_SET;
431 f->val = 0;
432 }
433
Eric Paris62062cf2013-04-16 13:08:43 -0400434 err = audit_field_valid(entry, f);
435 if (err)
436 goto exit_free;
437
438 err = -EINVAL;
Eric Parisab61d382013-04-16 17:26:51 -0400439 switch (f->type) {
Eric W. Biederman780a7652013-04-09 02:22:10 -0700440 case AUDIT_LOGINUID:
Al Viro0a73dcc2006-06-05 08:15:59 -0400441 case AUDIT_UID:
442 case AUDIT_EUID:
443 case AUDIT_SUID:
444 case AUDIT_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700445 case AUDIT_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700446 f->uid = make_kuid(current_user_ns(), f->val);
447 if (!uid_valid(f->uid))
448 goto exit_free;
449 break;
Al Viro0a73dcc2006-06-05 08:15:59 -0400450 case AUDIT_GID:
451 case AUDIT_EGID:
452 case AUDIT_SGID:
453 case AUDIT_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700454 case AUDIT_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700455 f->gid = make_kgid(current_user_ns(), f->val);
456 if (!gid_valid(f->gid))
457 goto exit_free;
458 break;
Amy Griffise54dc242007-03-29 18:01:04 -0400459 case AUDIT_ARCH:
460 entry->rule.arch_f = f;
461 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500462 case AUDIT_SUBJ_USER:
463 case AUDIT_SUBJ_ROLE:
464 case AUDIT_SUBJ_TYPE:
465 case AUDIT_SUBJ_SEN:
466 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500467 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:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600472 str = audit_unpack_string(&bufp, &remain, f->val);
473 if (IS_ERR(str))
474 goto exit_free;
475 entry->rule.buflen += f->val;
476
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200477 err = security_audit_rule_init(f->type, f->op, str,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000478 (void **)&f->lsm_rule);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600479 /* Keep currently invalid fields around in case they
480 * become valid after a policy reload. */
481 if (err == -EINVAL) {
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200482 printk(KERN_WARNING "audit rule for LSM "
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600483 "\'%s\' is invalid\n", str);
484 err = 0;
485 }
486 if (err) {
487 kfree(str);
488 goto exit_free;
489 } else
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000490 f->lsm_str = str;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600491 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400492 case AUDIT_WATCH:
493 str = audit_unpack_string(&bufp, &remain, f->val);
494 if (IS_ERR(str))
495 goto exit_free;
496 entry->rule.buflen += f->val;
497
498 err = audit_to_watch(&entry->rule, str, f->val, f->op);
499 if (err) {
500 kfree(str);
501 goto exit_free;
502 }
503 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400504 case AUDIT_DIR:
505 str = audit_unpack_string(&bufp, &remain, f->val);
506 if (IS_ERR(str))
507 goto exit_free;
508 entry->rule.buflen += f->val;
509
510 err = audit_make_tree(&entry->rule, str, f->op);
511 kfree(str);
512 if (err)
513 goto exit_free;
514 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400515 case AUDIT_INODE:
516 err = audit_to_inode(&entry->rule, f);
517 if (err)
518 goto exit_free;
519 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400520 case AUDIT_FILTERKEY:
Amy Griffis5adc8a62006-06-14 18:45:21 -0400521 if (entry->rule.filterkey || f->val > AUDIT_MAX_KEY_LEN)
522 goto exit_free;
523 str = audit_unpack_string(&bufp, &remain, f->val);
524 if (IS_ERR(str))
525 goto exit_free;
526 entry->rule.buflen += f->val;
527 entry->rule.filterkey = str;
528 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400529 }
530 }
531
Al Viro5af75d82008-12-16 05:59:26 -0500532 if (entry->rule.inode_f && entry->rule.inode_f->op == Audit_not_equal)
533 entry->rule.inode_f = NULL;
Amy Griffis93315ed2006-02-07 12:05:27 -0500534
535exit_nofree:
536 return entry;
537
538exit_free:
Chen Gang373e0f32013-04-29 15:05:18 -0700539 if (entry->rule.watch)
540 audit_put_watch(entry->rule.watch); /* matches initial get */
541 if (entry->rule.tree)
542 audit_put_tree(entry->rule.tree); /* that's the temporary one */
Amy Griffis93315ed2006-02-07 12:05:27 -0500543 audit_free_rule(entry);
544 return ERR_PTR(err);
545}
546
547/* Pack a filter field's string representation into data block. */
Al Viro74c3cbe2007-07-22 08:04:18 -0400548static inline size_t audit_pack_string(void **bufp, const char *str)
Amy Griffis93315ed2006-02-07 12:05:27 -0500549{
550 size_t len = strlen(str);
551
552 memcpy(*bufp, str, len);
553 *bufp += len;
554
555 return len;
556}
557
Amy Griffis93315ed2006-02-07 12:05:27 -0500558/* Translate kernel rule respresentation to struct audit_rule_data. */
559static 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 data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL);
566 if (unlikely(!data))
Amy Griffis0a3b4832006-05-02 15:06:01 -0400567 return NULL;
Amy Griffis93315ed2006-02-07 12:05:27 -0500568 memset(data, 0, sizeof(*data));
569
570 data->flags = krule->flags | krule->listnr;
571 data->action = krule->action;
572 data->field_count = krule->field_count;
573 bufp = data->buf;
574 for (i = 0; i < data->field_count; i++) {
575 struct audit_field *f = &krule->fields[i];
576
577 data->fields[i] = f->type;
Al Viro5af75d82008-12-16 05:59:26 -0500578 data->fieldflags[i] = audit_ops[f->op];
Amy Griffis93315ed2006-02-07 12:05:27 -0500579 switch(f->type) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500580 case AUDIT_SUBJ_USER:
581 case AUDIT_SUBJ_ROLE:
582 case AUDIT_SUBJ_TYPE:
583 case AUDIT_SUBJ_SEN:
584 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500585 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:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600590 data->buflen += data->values[i] =
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000591 audit_pack_string(&bufp, f->lsm_str);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600592 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400593 case AUDIT_WATCH:
594 data->buflen += data->values[i] =
Eric Pariscfcad622009-06-11 14:31:36 -0400595 audit_pack_string(&bufp,
596 audit_watch_path(krule->watch));
Amy Griffisf368c07d2006-04-07 16:55:56 -0400597 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400598 case AUDIT_DIR:
599 data->buflen += data->values[i] =
600 audit_pack_string(&bufp,
601 audit_tree_path(krule->tree));
602 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400603 case AUDIT_FILTERKEY:
604 data->buflen += data->values[i] =
605 audit_pack_string(&bufp, krule->filterkey);
606 break;
Amy Griffis93315ed2006-02-07 12:05:27 -0500607 default:
608 data->values[i] = f->val;
609 }
610 }
611 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. */
618static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b)
David Woodhousefe7752b2005-12-15 18:33:52 +0000619{
620 int i;
621
Amy Griffis93315ed2006-02-07 12:05:27 -0500622 if (a->flags != b->flags ||
623 a->listnr != b->listnr ||
624 a->action != b->action ||
625 a->field_count != b->field_count)
David Woodhousefe7752b2005-12-15 18:33:52 +0000626 return 1;
627
628 for (i = 0; i < a->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500629 if (a->fields[i].type != b->fields[i].type ||
630 a->fields[i].op != b->fields[i].op)
David Woodhousefe7752b2005-12-15 18:33:52 +0000631 return 1;
Amy Griffis93315ed2006-02-07 12:05:27 -0500632
633 switch(a->fields[i].type) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500634 case AUDIT_SUBJ_USER:
635 case AUDIT_SUBJ_ROLE:
636 case AUDIT_SUBJ_TYPE:
637 case AUDIT_SUBJ_SEN:
638 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500639 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:
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000644 if (strcmp(a->fields[i].lsm_str, b->fields[i].lsm_str))
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600645 return 1;
646 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400647 case AUDIT_WATCH:
Eric Pariscfcad622009-06-11 14:31:36 -0400648 if (strcmp(audit_watch_path(a->watch),
649 audit_watch_path(b->watch)))
Amy Griffisf368c07d2006-04-07 16:55:56 -0400650 return 1;
651 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400652 case AUDIT_DIR:
653 if (strcmp(audit_tree_path(a->tree),
654 audit_tree_path(b->tree)))
655 return 1;
656 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400657 case AUDIT_FILTERKEY:
658 /* both filterkeys exist based on above type compare */
659 if (strcmp(a->filterkey, b->filterkey))
660 return 1;
661 break;
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700662 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 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 if (!gid_eq(a->fields[i].gid, b->fields[i].gid))
677 return 1;
678 break;
Amy Griffis93315ed2006-02-07 12:05:27 -0500679 default:
680 if (a->fields[i].val != b->fields[i].val)
681 return 1;
682 }
David Woodhousefe7752b2005-12-15 18:33:52 +0000683 }
684
685 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
686 if (a->mask[i] != b->mask[i])
687 return 1;
688
689 return 0;
690}
691
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000692/* Duplicate LSM field information. The lsm_rule is opaque, so must be
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600693 * re-initialized. */
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200694static inline int audit_dupe_lsm_field(struct audit_field *df,
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600695 struct audit_field *sf)
696{
697 int ret = 0;
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000698 char *lsm_str;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600699
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000700 /* our own copy of lsm_str */
701 lsm_str = kstrdup(sf->lsm_str, GFP_KERNEL);
702 if (unlikely(!lsm_str))
Akinobu Mita3e1fbd12006-12-22 01:10:02 -0800703 return -ENOMEM;
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000704 df->lsm_str = lsm_str;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600705
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000706 /* our own (refreshed) copy of lsm_rule */
707 ret = security_audit_rule_init(df->type, df->op, df->lsm_str,
708 (void **)&df->lsm_rule);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600709 /* Keep currently invalid fields around in case they
710 * become valid after a policy reload. */
711 if (ret == -EINVAL) {
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200712 printk(KERN_WARNING "audit rule for LSM \'%s\' is "
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000713 "invalid\n", df->lsm_str);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600714 ret = 0;
715 }
716
717 return ret;
718}
719
720/* Duplicate an audit rule. This will be a deep copy with the exception
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200721 * of the watch - that pointer is carried over. The LSM specific fields
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600722 * will be updated in the copy. The point is to be able to replace the old
Amy Griffisf368c07d2006-04-07 16:55:56 -0400723 * 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. */
Eric Parisae7b8f42009-12-17 20:12:04 -0500726struct audit_entry *audit_dupe_rule(struct audit_krule *old)
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600727{
728 u32 fcount = old->field_count;
729 struct audit_entry *entry;
730 struct audit_krule *new;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400731 char *fk;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600732 int i, err = 0;
733
734 entry = audit_init_entry(fcount);
735 if (unlikely(!entry))
736 return ERR_PTR(-ENOMEM);
737
738 new = &entry->rule;
739 new->vers_ops = old->vers_ops;
740 new->flags = old->flags;
741 new->listnr = old->listnr;
742 new->action = old->action;
743 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
744 new->mask[i] = old->mask[i];
Al Viro0590b932008-12-14 23:45:27 -0500745 new->prio = old->prio;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600746 new->buflen = old->buflen;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400747 new->inode_f = old->inode_f;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600748 new->field_count = old->field_count;
Eric Parisae7b8f42009-12-17 20:12:04 -0500749
Al Viro74c3cbe2007-07-22 08:04:18 -0400750 /*
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 new->tree = old->tree;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600758 memcpy(new->fields, old->fields, sizeof(struct audit_field) * fcount);
759
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000760 /* deep copy this information, updating the lsm_rule fields, because
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600761 * the originals will all be freed when the old rule is freed. */
762 for (i = 0; i < fcount; i++) {
763 switch (new->fields[i].type) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500764 case AUDIT_SUBJ_USER:
765 case AUDIT_SUBJ_ROLE:
766 case AUDIT_SUBJ_TYPE:
767 case AUDIT_SUBJ_SEN:
768 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500769 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:
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200774 err = audit_dupe_lsm_field(&new->fields[i],
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600775 &old->fields[i]);
Amy Griffis5adc8a62006-06-14 18:45:21 -0400776 break;
777 case AUDIT_FILTERKEY:
778 fk = kstrdup(old->filterkey, GFP_KERNEL);
779 if (unlikely(!fk))
780 err = -ENOMEM;
781 else
782 new->filterkey = fk;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600783 }
784 if (err) {
785 audit_free_rule(entry);
786 return ERR_PTR(err);
787 }
788 }
789
Eric Parisae7b8f42009-12-17 20:12:04 -0500790 if (old->watch) {
791 audit_get_watch(old->watch);
792 new->watch = old->watch;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400793 }
794
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600795 return entry;
796}
797
Amy Griffisf368c07d2006-04-07 16:55:56 -0400798/* Find an existing audit rule.
799 * Caller must hold audit_filter_mutex to prevent stale rule data. */
800static struct audit_entry *audit_find_rule(struct audit_entry *entry,
Al Viro36c4f1b12008-12-15 01:50:28 -0500801 struct list_head **p)
Amy Griffisf368c07d2006-04-07 16:55:56 -0400802{
803 struct audit_entry *e, *found = NULL;
Al Viro36c4f1b12008-12-15 01:50:28 -0500804 struct list_head *list;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400805 int h;
806
Al Viro36c4f1b12008-12-15 01:50:28 -0500807 if (entry->rule.inode_f) {
808 h = audit_hash_ino(entry->rule.inode_f->val);
809 *p = list = &audit_inode_hash[h];
810 } else if (entry->rule.watch) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400811 /* we don't know the inode number, so must walk entire hash */
812 for (h = 0; h < AUDIT_INODE_BUCKETS; h++) {
813 list = &audit_inode_hash[h];
814 list_for_each_entry(e, list, list)
815 if (!audit_compare_rule(&entry->rule, &e->rule)) {
816 found = e;
817 goto out;
818 }
819 }
820 goto out;
Al Viro36c4f1b12008-12-15 01:50:28 -0500821 } else {
822 *p = list = &audit_filter_list[entry->rule.listnr];
Amy Griffisf368c07d2006-04-07 16:55:56 -0400823 }
824
825 list_for_each_entry(e, list, list)
826 if (!audit_compare_rule(&entry->rule, &e->rule)) {
827 found = e;
828 goto out;
829 }
830
831out:
832 return found;
833}
834
Al Viro0590b932008-12-14 23:45:27 -0500835static u64 prio_low = ~0ULL/2;
836static u64 prio_high = ~0ULL/2 - 1;
837
Amy Griffisf368c07d2006-04-07 16:55:56 -0400838/* Add rule to given filterlist if not a duplicate. */
Al Viro36c4f1b12008-12-15 01:50:28 -0500839static inline int audit_add_rule(struct audit_entry *entry)
Amy Griffisf368c07d2006-04-07 16:55:56 -0400840{
841 struct audit_entry *e;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400842 struct audit_watch *watch = entry->rule.watch;
Al Viro74c3cbe2007-07-22 08:04:18 -0400843 struct audit_tree *tree = entry->rule.tree;
Al Viro36c4f1b12008-12-15 01:50:28 -0500844 struct list_head *list;
Eric Parisae7b8f42009-12-17 20:12:04 -0500845 int err;
Al Viro471a5c72006-07-10 08:29:24 -0400846#ifdef CONFIG_AUDITSYSCALL
847 int dont_count = 0;
848
849 /* If either of these, don't count towards total */
850 if (entry->rule.listnr == AUDIT_FILTER_USER ||
851 entry->rule.listnr == AUDIT_FILTER_TYPE)
852 dont_count = 1;
853#endif
Amy Griffisf368c07d2006-04-07 16:55:56 -0400854
Amy Griffisf368c07d2006-04-07 16:55:56 -0400855 mutex_lock(&audit_filter_mutex);
Al Viro36c4f1b12008-12-15 01:50:28 -0500856 e = audit_find_rule(entry, &list);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400857 if (e) {
Eric Paris35fe4d02009-06-11 14:31:36 -0400858 mutex_unlock(&audit_filter_mutex);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400859 err = -EEXIST;
Al Viro74c3cbe2007-07-22 08:04:18 -0400860 /* normally audit_add_tree_rule() will free it on failure */
861 if (tree)
862 audit_put_tree(tree);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400863 goto error;
864 }
865
Amy Griffisf368c07d2006-04-07 16:55:56 -0400866 if (watch) {
867 /* audit_filter_mutex is dropped and re-taken during this call */
Eric Parisae7b8f42009-12-17 20:12:04 -0500868 err = audit_add_watch(&entry->rule, &list);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400869 if (err) {
870 mutex_unlock(&audit_filter_mutex);
Chen Gang2f992ee2013-07-08 15:59:38 -0700871 /*
872 * normally audit_add_tree_rule() will free it
873 * on failure
874 */
875 if (tree)
876 audit_put_tree(tree);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400877 goto error;
878 }
David Woodhousefe7752b2005-12-15 18:33:52 +0000879 }
Al Viro74c3cbe2007-07-22 08:04:18 -0400880 if (tree) {
881 err = audit_add_tree_rule(&entry->rule);
882 if (err) {
883 mutex_unlock(&audit_filter_mutex);
884 goto error;
885 }
886 }
David Woodhousefe7752b2005-12-15 18:33:52 +0000887
Al Viro0590b932008-12-14 23:45:27 -0500888 entry->rule.prio = ~0ULL;
889 if (entry->rule.listnr == AUDIT_FILTER_EXIT) {
890 if (entry->rule.flags & AUDIT_FILTER_PREPEND)
891 entry->rule.prio = ++prio_high;
892 else
893 entry->rule.prio = --prio_low;
894 }
895
David Woodhousefe7752b2005-12-15 18:33:52 +0000896 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
Al Viroe45aa212008-12-15 01:17:50 -0500897 list_add(&entry->rule.list,
898 &audit_rules_list[entry->rule.listnr]);
David Woodhousefe7752b2005-12-15 18:33:52 +0000899 list_add_rcu(&entry->list, list);
Amy Griffis6a2bcee2006-06-02 13:16:01 -0400900 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
David Woodhousefe7752b2005-12-15 18:33:52 +0000901 } else {
Al Viroe45aa212008-12-15 01:17:50 -0500902 list_add_tail(&entry->rule.list,
903 &audit_rules_list[entry->rule.listnr]);
David Woodhousefe7752b2005-12-15 18:33:52 +0000904 list_add_tail_rcu(&entry->list, list);
905 }
Al Viro471a5c72006-07-10 08:29:24 -0400906#ifdef CONFIG_AUDITSYSCALL
907 if (!dont_count)
908 audit_n_rules++;
Amy Griffise54dc242007-03-29 18:01:04 -0400909
910 if (!audit_match_signal(entry))
911 audit_signals++;
Al Viro471a5c72006-07-10 08:29:24 -0400912#endif
Amy Griffisf368c07d2006-04-07 16:55:56 -0400913 mutex_unlock(&audit_filter_mutex);
David Woodhousefe7752b2005-12-15 18:33:52 +0000914
Amy Griffisf368c07d2006-04-07 16:55:56 -0400915 return 0;
916
917error:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400918 if (watch)
919 audit_put_watch(watch); /* tmp watch, matches initial get */
920 return err;
David Woodhousefe7752b2005-12-15 18:33:52 +0000921}
922
Amy Griffisf368c07d2006-04-07 16:55:56 -0400923/* Remove an existing rule from filterlist. */
Al Viro36c4f1b12008-12-15 01:50:28 -0500924static inline int audit_del_rule(struct audit_entry *entry)
David Woodhousefe7752b2005-12-15 18:33:52 +0000925{
926 struct audit_entry *e;
Eric Pariscfcad622009-06-11 14:31:36 -0400927 struct audit_watch *watch = entry->rule.watch;
Al Viro74c3cbe2007-07-22 08:04:18 -0400928 struct audit_tree *tree = entry->rule.tree;
Al Viro36c4f1b12008-12-15 01:50:28 -0500929 struct list_head *list;
Al Viro36c4f1b12008-12-15 01:50:28 -0500930 int ret = 0;
Al Viro471a5c72006-07-10 08:29:24 -0400931#ifdef CONFIG_AUDITSYSCALL
932 int dont_count = 0;
933
934 /* If either of these, don't count towards total */
935 if (entry->rule.listnr == AUDIT_FILTER_USER ||
936 entry->rule.listnr == AUDIT_FILTER_TYPE)
937 dont_count = 1;
938#endif
David Woodhousefe7752b2005-12-15 18:33:52 +0000939
Amy Griffisf368c07d2006-04-07 16:55:56 -0400940 mutex_lock(&audit_filter_mutex);
Al Viro36c4f1b12008-12-15 01:50:28 -0500941 e = audit_find_rule(entry, &list);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400942 if (!e) {
943 mutex_unlock(&audit_filter_mutex);
944 ret = -ENOENT;
945 goto out;
946 }
947
Eric Pariscfcad622009-06-11 14:31:36 -0400948 if (e->rule.watch)
Eric Parisa05fb6c2009-12-17 20:12:05 -0500949 audit_remove_watch_rule(&e->rule);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400950
Al Viro74c3cbe2007-07-22 08:04:18 -0400951 if (e->rule.tree)
952 audit_remove_tree_rule(&e->rule);
953
Amy Griffisf368c07d2006-04-07 16:55:56 -0400954 list_del_rcu(&e->list);
Al Viroe45aa212008-12-15 01:17:50 -0500955 list_del(&e->rule.list);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400956 call_rcu(&e->rcu, audit_free_rule_rcu);
957
Al Viro471a5c72006-07-10 08:29:24 -0400958#ifdef CONFIG_AUDITSYSCALL
959 if (!dont_count)
960 audit_n_rules--;
Amy Griffise54dc242007-03-29 18:01:04 -0400961
962 if (!audit_match_signal(entry))
963 audit_signals--;
Al Viro471a5c72006-07-10 08:29:24 -0400964#endif
Amy Griffisf368c07d2006-04-07 16:55:56 -0400965 mutex_unlock(&audit_filter_mutex);
966
Amy Griffisf368c07d2006-04-07 16:55:56 -0400967out:
Eric Pariscfcad622009-06-11 14:31:36 -0400968 if (watch)
969 audit_put_watch(watch); /* match initial get */
Al Viro74c3cbe2007-07-22 08:04:18 -0400970 if (tree)
971 audit_put_tree(tree); /* that's the temporary one */
Amy Griffisf368c07d2006-04-07 16:55:56 -0400972
973 return ret;
David Woodhousefe7752b2005-12-15 18:33:52 +0000974}
975
Amy Griffis93315ed2006-02-07 12:05:27 -0500976/* List rules using struct audit_rule_data. */
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400977static void audit_list_rules(__u32 portid, int seq, struct sk_buff_head *q)
Amy Griffis93315ed2006-02-07 12:05:27 -0500978{
Al Viro9044e6b2006-05-22 01:09:24 -0400979 struct sk_buff *skb;
Al Viroe45aa212008-12-15 01:17:50 -0500980 struct audit_krule *r;
Amy Griffis93315ed2006-02-07 12:05:27 -0500981 int i;
982
Amy Griffisf368c07d2006-04-07 16:55:56 -0400983 /* This is a blocking read, so use audit_filter_mutex instead of rcu
984 * iterator to sync with list writers. */
Amy Griffis93315ed2006-02-07 12:05:27 -0500985 for (i=0; i<AUDIT_NR_FILTERS; i++) {
Al Viroe45aa212008-12-15 01:17:50 -0500986 list_for_each_entry(r, &audit_rules_list[i], list) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500987 struct audit_rule_data *data;
988
Al Viroe45aa212008-12-15 01:17:50 -0500989 data = audit_krule_to_data(r);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400990 if (unlikely(!data))
991 break;
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400992 skb = audit_make_reply(portid, seq, AUDIT_LIST_RULES,
993 0, 1, data,
994 sizeof(*data) + data->buflen);
Al Viro9044e6b2006-05-22 01:09:24 -0400995 if (skb)
996 skb_queue_tail(q, skb);
Amy Griffis93315ed2006-02-07 12:05:27 -0500997 kfree(data);
998 }
999 }
Richard Guy Briggsf9441632013-08-14 11:32:45 -04001000 skb = audit_make_reply(portid, seq, AUDIT_LIST_RULES, 1, 1, NULL, 0);
Al Viro9044e6b2006-05-22 01:09:24 -04001001 if (skb)
1002 skb_queue_tail(q, skb);
Amy Griffis93315ed2006-02-07 12:05:27 -05001003}
1004
Amy Griffis5adc8a62006-06-14 18:45:21 -04001005/* Log rule additions and removals */
Eric Parisdc9eb692013-04-19 13:23:09 -04001006static void audit_log_rule_change(char *action, struct audit_krule *rule, int res)
Amy Griffis5adc8a62006-06-14 18:45:21 -04001007{
1008 struct audit_buffer *ab;
Eric Parisdc9eb692013-04-19 13:23:09 -04001009 uid_t loginuid = from_kuid(&init_user_ns, audit_get_loginuid(current));
Eric Paris4440e852013-11-27 17:35:17 -05001010 unsigned int sessionid = audit_get_sessionid(current);
Amy Griffis5adc8a62006-06-14 18:45:21 -04001011
Eric Paris1a6b9f22008-01-07 17:09:31 -05001012 if (!audit_enabled)
1013 return;
1014
Amy Griffis5adc8a62006-06-14 18:45:21 -04001015 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
1016 if (!ab)
1017 return;
Eric Parisdc9eb692013-04-19 13:23:09 -04001018 audit_log_format(ab, "auid=%u ses=%u" ,loginuid, sessionid);
Eric Parisb122c372013-04-19 15:00:33 -04001019 audit_log_task_context(ab);
Eric Paris9d960982009-06-11 14:31:37 -04001020 audit_log_format(ab, " op=");
1021 audit_log_string(ab, action);
1022 audit_log_key(ab, rule->filterkey);
Amy Griffis5adc8a62006-06-14 18:45:21 -04001023 audit_log_format(ab, " list=%d res=%d", rule->listnr, res);
1024 audit_log_end(ab);
1025}
1026
David Woodhousefe7752b2005-12-15 18:33:52 +00001027/**
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001028 * audit_rule_change - apply all rules to the specified message type
David Woodhousefe7752b2005-12-15 18:33:52 +00001029 * @type: audit message type
Richard Guy Briggsf9441632013-08-14 11:32:45 -04001030 * @portid: target port id for netlink audit messages
David Woodhousefe7752b2005-12-15 18:33:52 +00001031 * @seq: netlink audit message sequence (serial) number
1032 * @data: payload data
Amy Griffis93315ed2006-02-07 12:05:27 -05001033 * @datasz: size of payload data
David Woodhousefe7752b2005-12-15 18:33:52 +00001034 */
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001035int audit_rule_change(int type, __u32 portid, int seq, void *data,
1036 size_t datasz)
David Woodhousefe7752b2005-12-15 18:33:52 +00001037{
Amy Griffis93315ed2006-02-07 12:05:27 -05001038 int err = 0;
1039 struct audit_entry *entry;
David Woodhousefe7752b2005-12-15 18:33:52 +00001040
1041 switch (type) {
Amy Griffis93315ed2006-02-07 12:05:27 -05001042 case AUDIT_ADD_RULE:
Eric Paris18900902013-04-18 19:16:36 -04001043 entry = audit_data_to_entry(data, datasz);
Amy Griffis93315ed2006-02-07 12:05:27 -05001044 if (IS_ERR(entry))
1045 return PTR_ERR(entry);
David Woodhousefe7752b2005-12-15 18:33:52 +00001046
Al Viro36c4f1b12008-12-15 01:50:28 -05001047 err = audit_add_rule(entry);
Eric Parisdc9eb692013-04-19 13:23:09 -04001048 audit_log_rule_change("add rule", &entry->rule, !err);
Steve Grubb5d330102006-01-09 09:48:17 -05001049 if (err)
Amy Griffis93315ed2006-02-07 12:05:27 -05001050 audit_free_rule(entry);
David Woodhousefe7752b2005-12-15 18:33:52 +00001051 break;
Amy Griffis93315ed2006-02-07 12:05:27 -05001052 case AUDIT_DEL_RULE:
Eric Paris18900902013-04-18 19:16:36 -04001053 entry = audit_data_to_entry(data, datasz);
Amy Griffis93315ed2006-02-07 12:05:27 -05001054 if (IS_ERR(entry))
1055 return PTR_ERR(entry);
David Woodhousefe7752b2005-12-15 18:33:52 +00001056
Al Viro36c4f1b12008-12-15 01:50:28 -05001057 err = audit_del_rule(entry);
Eric Parisdc9eb692013-04-19 13:23:09 -04001058 audit_log_rule_change("remove rule", &entry->rule, !err);
Amy Griffis93315ed2006-02-07 12:05:27 -05001059 audit_free_rule(entry);
David Woodhousefe7752b2005-12-15 18:33:52 +00001060 break;
1061 default:
1062 return -EINVAL;
1063 }
1064
1065 return err;
1066}
1067
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001068/**
1069 * audit_list_rules_send - list the audit rules
Eric W. Biedermand211f172014-03-08 15:31:54 -08001070 * @request_skb: skb of request we are replying to (used to target the reply)
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001071 * @seq: netlink audit message sequence (serial) number
1072 */
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001073int audit_list_rules_send(struct sk_buff *request_skb, int seq)
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001074{
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001075 u32 portid = NETLINK_CB(request_skb).portid;
1076 struct net *net = sock_net(NETLINK_CB(request_skb).sk);
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001077 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 if (!dest)
1089 return -ENOMEM;
Eric W. Biederman6f285b12014-02-28 19:44:55 -08001090 dest->net = get_net(net);
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001091 dest->portid = portid;
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001092 skb_queue_head_init(&dest->q);
1093
1094 mutex_lock(&audit_filter_mutex);
1095 audit_list_rules(portid, seq, &dest->q);
1096 mutex_unlock(&audit_filter_mutex);
1097
1098 tsk = kthread_run(audit_send_list, dest, "audit_send_list");
1099 if (IS_ERR(tsk)) {
1100 skb_queue_purge(&dest->q);
1101 kfree(dest);
1102 err = PTR_ERR(tsk);
1103 }
1104
1105 return err;
1106}
1107
Al Viro5af75d82008-12-16 05:59:26 -05001108int audit_comparator(u32 left, u32 op, u32 right)
David Woodhousefe7752b2005-12-15 18:33:52 +00001109{
1110 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05001111 case Audit_equal:
David Woodhousefe7752b2005-12-15 18:33:52 +00001112 return (left == right);
Al Viro5af75d82008-12-16 05:59:26 -05001113 case Audit_not_equal:
David Woodhousefe7752b2005-12-15 18:33:52 +00001114 return (left != right);
Al Viro5af75d82008-12-16 05:59:26 -05001115 case Audit_lt:
David Woodhousefe7752b2005-12-15 18:33:52 +00001116 return (left < right);
Al Viro5af75d82008-12-16 05:59:26 -05001117 case Audit_le:
David Woodhousefe7752b2005-12-15 18:33:52 +00001118 return (left <= right);
Al Viro5af75d82008-12-16 05:59:26 -05001119 case Audit_gt:
David Woodhousefe7752b2005-12-15 18:33:52 +00001120 return (left > right);
Al Viro5af75d82008-12-16 05:59:26 -05001121 case Audit_ge:
David Woodhousefe7752b2005-12-15 18:33:52 +00001122 return (left >= right);
Al Viro5af75d82008-12-16 05:59:26 -05001123 case Audit_bitmask:
Eric Paris74f23452007-06-04 17:00:14 -04001124 return (left & right);
Al Viro5af75d82008-12-16 05:59:26 -05001125 case Audit_bittest:
Eric Paris74f23452007-06-04 17:00:14 -04001126 return ((left & right) == right);
Al Viro5af75d82008-12-16 05:59:26 -05001127 default:
1128 BUG();
1129 return 0;
David Woodhousefe7752b2005-12-15 18:33:52 +00001130 }
1131}
1132
Eric W. Biedermanca57ec02012-09-11 02:18:08 -07001133int audit_uid_comparator(kuid_t left, u32 op, kuid_t right)
1134{
1135 switch (op) {
1136 case Audit_equal:
1137 return uid_eq(left, right);
1138 case Audit_not_equal:
1139 return !uid_eq(left, right);
1140 case Audit_lt:
1141 return uid_lt(left, right);
1142 case Audit_le:
1143 return uid_lte(left, right);
1144 case Audit_gt:
1145 return uid_gt(left, right);
1146 case Audit_ge:
1147 return uid_gte(left, right);
1148 case Audit_bitmask:
1149 case Audit_bittest:
1150 default:
1151 BUG();
1152 return 0;
1153 }
1154}
1155
1156int audit_gid_comparator(kgid_t left, u32 op, kgid_t right)
1157{
1158 switch (op) {
1159 case Audit_equal:
1160 return gid_eq(left, right);
1161 case Audit_not_equal:
1162 return !gid_eq(left, right);
1163 case Audit_lt:
1164 return gid_lt(left, right);
1165 case Audit_le:
1166 return gid_lte(left, right);
1167 case Audit_gt:
1168 return gid_gt(left, right);
1169 case Audit_ge:
1170 return gid_gte(left, right);
1171 case Audit_bitmask:
1172 case Audit_bittest:
1173 default:
1174 BUG();
1175 return 0;
1176 }
1177}
1178
Jeff Laytonbfcec702012-10-10 15:25:23 -04001179/**
1180 * parent_len - find the length of the parent portion of a pathname
1181 * @path: pathname of which to determine length
1182 */
1183int parent_len(const char *path)
1184{
1185 int plen;
1186 const char *p;
1187
1188 plen = strlen(path);
1189
1190 if (plen == 0)
1191 return plen;
1192
1193 /* disregard trailing slashes */
1194 p = path + plen - 1;
1195 while ((*p == '/') && (p > path))
1196 p--;
1197
1198 /* walk backward until we find the next slash or hit beginning */
1199 while ((*p != '/') && (p > path))
1200 p--;
1201
1202 /* did we find a slash? Then increment to include it in path */
1203 if (*p == '/')
1204 p++;
1205
1206 return p - path;
1207}
1208
Jeff Laytone3d6b072012-10-10 15:25:25 -04001209/**
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 */
1217int audit_compare_dname_path(const char *dname, const char *path, int parentlen)
Amy Griffisf368c07d2006-04-07 16:55:56 -04001218{
Jeff Laytone3d6b072012-10-10 15:25:25 -04001219 int dlen, pathlen;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001220 const char *p;
David Woodhousefe7752b2005-12-15 18:33:52 +00001221
Amy Griffisf368c07d2006-04-07 16:55:56 -04001222 dlen = strlen(dname);
Eric Paris29e9a342012-10-10 15:25:24 -04001223 pathlen = strlen(path);
1224 if (pathlen < dlen)
Amy Griffisf368c07d2006-04-07 16:55:56 -04001225 return 1;
1226
Jeff Laytone3d6b072012-10-10 15:25:25 -04001227 parentlen = parentlen == AUDIT_NAME_FULL ? parent_len(path) : parentlen;
Eric Paris29e9a342012-10-10 15:25:24 -04001228 if (pathlen - parentlen != dlen)
Amy Griffisf368c07d2006-04-07 16:55:56 -04001229 return 1;
Eric Paris29e9a342012-10-10 15:25:24 -04001230
1231 p = path + parentlen;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001232
1233 return strncmp(p, dname, dlen);
1234}
David Woodhousefe7752b2005-12-15 18:33:52 +00001235
Eric Paris62062cf2013-04-16 13:08:43 -04001236static int audit_filter_user_rules(struct audit_krule *rule, int type,
David Woodhousefe7752b2005-12-15 18:33:52 +00001237 enum audit_state *state)
1238{
1239 int i;
1240
1241 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -05001242 struct audit_field *f = &rule->fields[i];
David Woodhousefe7752b2005-12-15 18:33:52 +00001243 int result = 0;
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001244 u32 sid;
David Woodhousefe7752b2005-12-15 18:33:52 +00001245
Amy Griffis93315ed2006-02-07 12:05:27 -05001246 switch (f->type) {
David Woodhousefe7752b2005-12-15 18:33:52 +00001247 case AUDIT_PID:
Eric W. Biederman02276bd2012-09-10 23:10:16 -07001248 result = audit_comparator(task_pid_vnr(current), f->op, f->val);
David Woodhousefe7752b2005-12-15 18:33:52 +00001249 break;
1250 case AUDIT_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -07001251 result = audit_uid_comparator(current_uid(), f->op, f->uid);
David Woodhousefe7752b2005-12-15 18:33:52 +00001252 break;
1253 case AUDIT_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -07001254 result = audit_gid_comparator(current_gid(), f->op, f->gid);
David Woodhousefe7752b2005-12-15 18:33:52 +00001255 break;
1256 case AUDIT_LOGINUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -07001257 result = audit_uid_comparator(audit_get_loginuid(current),
1258 f->op, f->uid);
David Woodhousefe7752b2005-12-15 18:33:52 +00001259 break;
Eric W. Biederman780a7652013-04-09 02:22:10 -07001260 case AUDIT_LOGINUID_SET:
1261 result = audit_comparator(audit_loginuid_set(current),
1262 f->op, f->val);
1263 break;
Eric Paris62062cf2013-04-16 13:08:43 -04001264 case AUDIT_MSGTYPE:
1265 result = audit_comparator(type, f->op, f->val);
1266 break;
Miloslav Trmacd29be152010-09-16 18:14:11 -04001267 case AUDIT_SUBJ_USER:
1268 case AUDIT_SUBJ_ROLE:
1269 case AUDIT_SUBJ_TYPE:
1270 case AUDIT_SUBJ_SEN:
1271 case AUDIT_SUBJ_CLR:
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001272 if (f->lsm_rule) {
1273 security_task_getsecid(current, &sid);
1274 result = security_audit_rule_match(sid,
Miloslav Trmacd29be152010-09-16 18:14:11 -04001275 f->type,
1276 f->op,
1277 f->lsm_rule,
1278 NULL);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001279 }
Miloslav Trmacd29be152010-09-16 18:14:11 -04001280 break;
David Woodhousefe7752b2005-12-15 18:33:52 +00001281 }
1282
1283 if (!result)
1284 return 0;
1285 }
1286 switch (rule->action) {
1287 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
David Woodhousefe7752b2005-12-15 18:33:52 +00001288 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
1289 }
1290 return 1;
1291}
1292
Eric Paris62062cf2013-04-16 13:08:43 -04001293int audit_filter_user(int type)
David Woodhousefe7752b2005-12-15 18:33:52 +00001294{
Ingo Molnar11f57ce2007-02-10 01:46:09 -08001295 enum audit_state state = AUDIT_DISABLED;
David Woodhousefe7752b2005-12-15 18:33:52 +00001296 struct audit_entry *e;
Richard Guy Briggs724e4fc2013-11-25 21:57:51 -05001297 int rc, ret;
1298
1299 ret = 1; /* Audit by default */
David Woodhousefe7752b2005-12-15 18:33:52 +00001300
1301 rcu_read_lock();
1302 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
Richard Guy Briggs724e4fc2013-11-25 21:57:51 -05001303 rc = audit_filter_user_rules(&e->rule, type, &state);
1304 if (rc) {
1305 if (rc > 0 && state == AUDIT_DISABLED)
David Woodhousefe7752b2005-12-15 18:33:52 +00001306 ret = 0;
1307 break;
1308 }
1309 }
1310 rcu_read_unlock();
1311
Richard Guy Briggs724e4fc2013-11-25 21:57:51 -05001312 return ret;
David Woodhousefe7752b2005-12-15 18:33:52 +00001313}
1314
1315int audit_filter_type(int type)
1316{
1317 struct audit_entry *e;
1318 int result = 0;
Daniel Walker9ce34212007-10-18 03:06:06 -07001319
David Woodhousefe7752b2005-12-15 18:33:52 +00001320 rcu_read_lock();
1321 if (list_empty(&audit_filter_list[AUDIT_FILTER_TYPE]))
1322 goto unlock_and_return;
1323
1324 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TYPE],
1325 list) {
David Woodhousefe7752b2005-12-15 18:33:52 +00001326 int i;
Amy Griffis93315ed2006-02-07 12:05:27 -05001327 for (i = 0; i < e->rule.field_count; i++) {
1328 struct audit_field *f = &e->rule.fields[i];
1329 if (f->type == AUDIT_MSGTYPE) {
1330 result = audit_comparator(type, f->op, f->val);
David Woodhousefe7752b2005-12-15 18:33:52 +00001331 if (!result)
1332 break;
1333 }
1334 }
1335 if (result)
1336 goto unlock_and_return;
1337 }
1338unlock_and_return:
1339 rcu_read_unlock();
1340 return result;
1341}
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001342
Al Viroe45aa212008-12-15 01:17:50 -05001343static int update_lsm_rule(struct audit_krule *r)
Al Viro1a9d0792008-12-14 12:04:02 -05001344{
Al Viroe45aa212008-12-15 01:17:50 -05001345 struct audit_entry *entry = container_of(r, struct audit_entry, rule);
Al Viro1a9d0792008-12-14 12:04:02 -05001346 struct audit_entry *nentry;
Al Viro1a9d0792008-12-14 12:04:02 -05001347 int err = 0;
1348
Al Viroe45aa212008-12-15 01:17:50 -05001349 if (!security_audit_rule_known(r))
Al Viro1a9d0792008-12-14 12:04:02 -05001350 return 0;
1351
Eric Parisae7b8f42009-12-17 20:12:04 -05001352 nentry = audit_dupe_rule(r);
Al Viro1a9d0792008-12-14 12:04:02 -05001353 if (IS_ERR(nentry)) {
1354 /* save the first error encountered for the
1355 * return value */
1356 err = PTR_ERR(nentry);
1357 audit_panic("error updating LSM filters");
Eric Parisae7b8f42009-12-17 20:12:04 -05001358 if (r->watch)
Al Viroe45aa212008-12-15 01:17:50 -05001359 list_del(&r->rlist);
Al Viro1a9d0792008-12-14 12:04:02 -05001360 list_del_rcu(&entry->list);
Al Viroe45aa212008-12-15 01:17:50 -05001361 list_del(&r->list);
Al Viro1a9d0792008-12-14 12:04:02 -05001362 } else {
Eric Parisae7b8f42009-12-17 20:12:04 -05001363 if (r->watch || r->tree)
Al Viroe45aa212008-12-15 01:17:50 -05001364 list_replace_init(&r->rlist, &nentry->rule.rlist);
Al Viro1a9d0792008-12-14 12:04:02 -05001365 list_replace_rcu(&entry->list, &nentry->list);
Al Viroe45aa212008-12-15 01:17:50 -05001366 list_replace(&r->list, &nentry->rule.list);
Al Viro1a9d0792008-12-14 12:04:02 -05001367 }
1368 call_rcu(&entry->rcu, audit_free_rule_rcu);
1369
1370 return err;
1371}
1372
Ahmed S. Darwish04305e42008-04-19 09:59:43 +10001373/* This function will re-initialize the lsm_rule field of all applicable rules.
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +02001374 * It will traverse the filter lists serarching for rules that contain LSM
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001375 * specific filter fields. When such a rule is found, it is copied, the
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +02001376 * LSM field is re-initialized, and the old rule is replaced with the
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001377 * updated rule. */
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +02001378int audit_update_lsm_rules(void)
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001379{
Al Viroe45aa212008-12-15 01:17:50 -05001380 struct audit_krule *r, *n;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001381 int i, err = 0;
1382
Amy Griffisf368c07d2006-04-07 16:55:56 -04001383 /* audit_filter_mutex synchronizes the writers */
1384 mutex_lock(&audit_filter_mutex);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001385
1386 for (i = 0; i < AUDIT_NR_FILTERS; i++) {
Al Viroe45aa212008-12-15 01:17:50 -05001387 list_for_each_entry_safe(r, n, &audit_rules_list[i], list) {
1388 int res = update_lsm_rule(r);
Al Viro1a9d0792008-12-14 12:04:02 -05001389 if (!err)
1390 err = res;
1391 }
1392 }
Amy Griffisf368c07d2006-04-07 16:55:56 -04001393 mutex_unlock(&audit_filter_mutex);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001394
1395 return err;
1396}