blob: a5e3d73d73e4831fc7da48da41862870de537bcf [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>
David Woodhousefe7752b2005-12-15 18:33:52 +000033#include "audit.h"
34
Amy Griffisf368c07d2006-04-07 16:55:56 -040035/*
36 * Locking model:
37 *
38 * audit_filter_mutex:
39 * Synchronizes writes and blocking reads of audit's filterlist
40 * data. Rcu is used to traverse the filterlist and access
41 * contents of structs audit_entry, audit_watch and opaque
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +020042 * LSM rules during filtering. If modified, these structures
Amy Griffisf368c07d2006-04-07 16:55:56 -040043 * must be copied and replace their counterparts in the filterlist.
44 * An audit_parent struct is not accessed during filtering, so may
45 * be written directly provided audit_filter_mutex is held.
46 */
47
Amy Griffisf368c07d2006-04-07 16:55:56 -040048/* Audit filter lists, defined in <linux/audit.h> */
David Woodhousefe7752b2005-12-15 18:33:52 +000049struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
50 LIST_HEAD_INIT(audit_filter_list[0]),
51 LIST_HEAD_INIT(audit_filter_list[1]),
52 LIST_HEAD_INIT(audit_filter_list[2]),
53 LIST_HEAD_INIT(audit_filter_list[3]),
54 LIST_HEAD_INIT(audit_filter_list[4]),
55 LIST_HEAD_INIT(audit_filter_list[5]),
56#if AUDIT_NR_FILTERS != 6
57#error Fix audit_filter_list initialiser
58#endif
59};
Al Viroe45aa212008-12-15 01:17:50 -050060static struct list_head audit_rules_list[AUDIT_NR_FILTERS] = {
61 LIST_HEAD_INIT(audit_rules_list[0]),
62 LIST_HEAD_INIT(audit_rules_list[1]),
63 LIST_HEAD_INIT(audit_rules_list[2]),
64 LIST_HEAD_INIT(audit_rules_list[3]),
65 LIST_HEAD_INIT(audit_rules_list[4]),
66 LIST_HEAD_INIT(audit_rules_list[5]),
67};
David Woodhousefe7752b2005-12-15 18:33:52 +000068
Al Viro74c3cbe2007-07-22 08:04:18 -040069DEFINE_MUTEX(audit_filter_mutex);
Amy Griffisf368c07d2006-04-07 16:55:56 -040070
Amy Griffis93315ed2006-02-07 12:05:27 -050071static inline void audit_free_rule(struct audit_entry *e)
David Woodhousefe7752b2005-12-15 18:33:52 +000072{
Darrel Goeddel3dc7e312006-03-10 18:14:06 -060073 int i;
Zhenwen Xuc28bb7d2009-03-12 22:16:12 +080074 struct audit_krule *erule = &e->rule;
Eric Parisae7b8f42009-12-17 20:12:04 -050075
Amy Griffisf368c07d2006-04-07 16:55:56 -040076 /* some rules don't have associated watches */
Zhenwen Xuc28bb7d2009-03-12 22:16:12 +080077 if (erule->watch)
78 audit_put_watch(erule->watch);
79 if (erule->fields)
80 for (i = 0; i < erule->field_count; i++) {
81 struct audit_field *f = &erule->fields[i];
Ahmed S. Darwish04305e42008-04-19 09:59:43 +100082 kfree(f->lsm_str);
83 security_audit_rule_free(f->lsm_rule);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -060084 }
Zhenwen Xuc28bb7d2009-03-12 22:16:12 +080085 kfree(erule->fields);
86 kfree(erule->filterkey);
Amy Griffis93315ed2006-02-07 12:05:27 -050087 kfree(e);
David Woodhousefe7752b2005-12-15 18:33:52 +000088}
89
Al Viro74c3cbe2007-07-22 08:04:18 -040090void audit_free_rule_rcu(struct rcu_head *head)
Amy Griffis93315ed2006-02-07 12:05:27 -050091{
92 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
93 audit_free_rule(e);
94}
95
Darrel Goeddel3dc7e312006-03-10 18:14:06 -060096/* Initialize an audit filterlist entry. */
97static inline struct audit_entry *audit_init_entry(u32 field_count)
98{
99 struct audit_entry *entry;
100 struct audit_field *fields;
101
102 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
103 if (unlikely(!entry))
104 return NULL;
105
106 fields = kzalloc(sizeof(*fields) * field_count, GFP_KERNEL);
107 if (unlikely(!fields)) {
108 kfree(entry);
109 return NULL;
110 }
111 entry->rule.fields = fields;
112
113 return entry;
114}
115
Amy Griffis93315ed2006-02-07 12:05:27 -0500116/* Unpack a filter field's string representation from user-space
117 * buffer. */
Al Viro74c3cbe2007-07-22 08:04:18 -0400118char *audit_unpack_string(void **bufp, size_t *remain, size_t len)
Amy Griffis93315ed2006-02-07 12:05:27 -0500119{
120 char *str;
121
122 if (!*bufp || (len == 0) || (len > *remain))
123 return ERR_PTR(-EINVAL);
124
125 /* Of the currently implemented string fields, PATH_MAX
126 * defines the longest valid length.
127 */
128 if (len > PATH_MAX)
129 return ERR_PTR(-ENAMETOOLONG);
130
131 str = kmalloc(len + 1, GFP_KERNEL);
132 if (unlikely(!str))
133 return ERR_PTR(-ENOMEM);
134
135 memcpy(str, *bufp, len);
136 str[len] = 0;
137 *bufp += len;
138 *remain -= len;
139
140 return str;
141}
142
Amy Griffisf368c07d2006-04-07 16:55:56 -0400143/* Translate an inode field to kernel respresentation. */
144static inline int audit_to_inode(struct audit_krule *krule,
145 struct audit_field *f)
146{
147 if (krule->listnr != AUDIT_FILTER_EXIT ||
Al Viro5af75d82008-12-16 05:59:26 -0500148 krule->watch || krule->inode_f || krule->tree ||
149 (f->op != Audit_equal && f->op != Audit_not_equal))
Amy Griffisf368c07d2006-04-07 16:55:56 -0400150 return -EINVAL;
151
152 krule->inode_f = f;
153 return 0;
154}
155
Al Virob9155432006-07-01 03:56:16 -0400156static __u32 *classes[AUDIT_SYSCALL_CLASSES];
157
158int __init audit_register_class(int class, unsigned *list)
159{
160 __u32 *p = kzalloc(AUDIT_BITMASK_SIZE * sizeof(__u32), GFP_KERNEL);
161 if (!p)
162 return -ENOMEM;
163 while (*list != ~0U) {
164 unsigned n = *list++;
165 if (n >= AUDIT_BITMASK_SIZE * 32 - AUDIT_SYSCALL_CLASSES) {
166 kfree(p);
167 return -EINVAL;
168 }
169 p[AUDIT_WORD(n)] |= AUDIT_BIT(n);
170 }
171 if (class >= AUDIT_SYSCALL_CLASSES || classes[class]) {
172 kfree(p);
173 return -EINVAL;
174 }
175 classes[class] = p;
176 return 0;
177}
178
Al Viro55669bf2006-08-31 19:26:40 -0400179int audit_match_class(int class, unsigned syscall)
180{
Klaus Weidnerc926e4f2007-05-16 17:45:42 -0500181 if (unlikely(syscall >= AUDIT_BITMASK_SIZE * 32))
Al Viro55669bf2006-08-31 19:26:40 -0400182 return 0;
183 if (unlikely(class >= AUDIT_SYSCALL_CLASSES || !classes[class]))
184 return 0;
185 return classes[class][AUDIT_WORD(syscall)] & AUDIT_BIT(syscall);
186}
187
Al Viro327b9ee2007-05-15 20:37:10 +0100188#ifdef CONFIG_AUDITSYSCALL
Amy Griffise54dc242007-03-29 18:01:04 -0400189static inline int audit_match_class_bits(int class, u32 *mask)
190{
191 int i;
192
193 if (classes[class]) {
194 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
195 if (mask[i] & classes[class][i])
196 return 0;
197 }
198 return 1;
199}
200
201static int audit_match_signal(struct audit_entry *entry)
202{
203 struct audit_field *arch = entry->rule.arch_f;
204
205 if (!arch) {
206 /* When arch is unspecified, we must check both masks on biarch
207 * as syscall number alone is ambiguous. */
208 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL,
209 entry->rule.mask) &&
210 audit_match_class_bits(AUDIT_CLASS_SIGNAL_32,
211 entry->rule.mask));
212 }
213
214 switch(audit_classify_arch(arch->val)) {
215 case 0: /* native */
216 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL,
217 entry->rule.mask));
218 case 1: /* 32bit on biarch */
219 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL_32,
220 entry->rule.mask));
221 default:
222 return 1;
223 }
224}
Al Viro327b9ee2007-05-15 20:37:10 +0100225#endif
Amy Griffise54dc242007-03-29 18:01:04 -0400226
Amy Griffis93315ed2006-02-07 12:05:27 -0500227/* Common user-space to kernel rule translation. */
228static inline struct audit_entry *audit_to_entry_common(struct audit_rule *rule)
229{
230 unsigned listnr;
231 struct audit_entry *entry;
Amy Griffis93315ed2006-02-07 12:05:27 -0500232 int i, err;
233
234 err = -EINVAL;
235 listnr = rule->flags & ~AUDIT_FILTER_PREPEND;
236 switch(listnr) {
237 default:
238 goto exit_err;
Amy Griffis93315ed2006-02-07 12:05:27 -0500239#ifdef CONFIG_AUDITSYSCALL
240 case AUDIT_FILTER_ENTRY:
Eric Paris7ff68e52012-01-03 14:23:07 -0500241 if (rule->action == AUDIT_ALWAYS)
242 goto exit_err;
Amy Griffis93315ed2006-02-07 12:05:27 -0500243 case AUDIT_FILTER_EXIT:
244 case AUDIT_FILTER_TASK:
245#endif
Eric Paris7ff68e52012-01-03 14:23:07 -0500246 case AUDIT_FILTER_USER:
247 case AUDIT_FILTER_TYPE:
Amy Griffis93315ed2006-02-07 12:05:27 -0500248 ;
249 }
Al Viro014149c2006-05-23 01:36:13 -0400250 if (unlikely(rule->action == AUDIT_POSSIBLE)) {
251 printk(KERN_ERR "AUDIT_POSSIBLE is deprecated\n");
252 goto exit_err;
253 }
254 if (rule->action != AUDIT_NEVER && rule->action != AUDIT_ALWAYS)
Amy Griffis93315ed2006-02-07 12:05:27 -0500255 goto exit_err;
256 if (rule->field_count > AUDIT_MAX_FIELDS)
257 goto exit_err;
258
259 err = -ENOMEM;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600260 entry = audit_init_entry(rule->field_count);
261 if (!entry)
Amy Griffis93315ed2006-02-07 12:05:27 -0500262 goto exit_err;
Amy Griffis93315ed2006-02-07 12:05:27 -0500263
264 entry->rule.flags = rule->flags & AUDIT_FILTER_PREPEND;
265 entry->rule.listnr = listnr;
266 entry->rule.action = rule->action;
267 entry->rule.field_count = rule->field_count;
Amy Griffis93315ed2006-02-07 12:05:27 -0500268
269 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
270 entry->rule.mask[i] = rule->mask[i];
271
Al Virob9155432006-07-01 03:56:16 -0400272 for (i = 0; i < AUDIT_SYSCALL_CLASSES; i++) {
273 int bit = AUDIT_BITMASK_SIZE * 32 - i - 1;
274 __u32 *p = &entry->rule.mask[AUDIT_WORD(bit)];
275 __u32 *class;
276
277 if (!(*p & AUDIT_BIT(bit)))
278 continue;
279 *p &= ~AUDIT_BIT(bit);
280 class = classes[i];
281 if (class) {
282 int j;
283 for (j = 0; j < AUDIT_BITMASK_SIZE; j++)
284 entry->rule.mask[j] |= class[j];
285 }
286 }
287
Amy Griffis93315ed2006-02-07 12:05:27 -0500288 return entry;
289
290exit_err:
291 return ERR_PTR(err);
292}
293
Al Viro5af75d82008-12-16 05:59:26 -0500294static u32 audit_ops[] =
295{
296 [Audit_equal] = AUDIT_EQUAL,
297 [Audit_not_equal] = AUDIT_NOT_EQUAL,
298 [Audit_bitmask] = AUDIT_BIT_MASK,
299 [Audit_bittest] = AUDIT_BIT_TEST,
300 [Audit_lt] = AUDIT_LESS_THAN,
301 [Audit_gt] = AUDIT_GREATER_THAN,
302 [Audit_le] = AUDIT_LESS_THAN_OR_EQUAL,
303 [Audit_ge] = AUDIT_GREATER_THAN_OR_EQUAL,
304};
305
306static u32 audit_to_op(u32 op)
307{
308 u32 n;
309 for (n = Audit_equal; n < Audit_bad && audit_ops[n] != op; n++)
310 ;
311 return n;
312}
313
Eric Parisab61d382013-04-16 17:26:51 -0400314/* check if an audit field is valid */
Eric Paris62062cf2013-04-16 13:08:43 -0400315static int audit_field_valid(struct audit_entry *entry, struct audit_field *f)
Amy Griffis93315ed2006-02-07 12:05:27 -0500316{
Eric Paris62062cf2013-04-16 13:08:43 -0400317 switch(f->type) {
318 case AUDIT_MSGTYPE:
319 if (entry->rule.listnr != AUDIT_FILTER_TYPE &&
320 entry->rule.listnr != AUDIT_FILTER_USER)
321 return -EINVAL;
322 break;
323 };
Amy Griffis93315ed2006-02-07 12:05:27 -0500324
Eric Parisab61d382013-04-16 17:26:51 -0400325 switch(f->type) {
326 default:
327 return -EINVAL;
328 case AUDIT_UID:
329 case AUDIT_EUID:
330 case AUDIT_SUID:
331 case AUDIT_FSUID:
332 case AUDIT_LOGINUID:
333 case AUDIT_OBJ_UID:
334 case AUDIT_GID:
335 case AUDIT_EGID:
336 case AUDIT_SGID:
337 case AUDIT_FSGID:
338 case AUDIT_OBJ_GID:
339 case AUDIT_PID:
340 case AUDIT_PERS:
341 case AUDIT_MSGTYPE:
342 case AUDIT_PPID:
343 case AUDIT_DEVMAJOR:
344 case AUDIT_DEVMINOR:
345 case AUDIT_EXIT:
346 case AUDIT_SUCCESS:
Eric Paris78122032013-09-04 15:01:43 -0400347 case AUDIT_INODE:
Eric Parisab61d382013-04-16 17:26:51 -0400348 /* bit ops are only useful on syscall args */
349 if (f->op == Audit_bitmask || f->op == Audit_bittest)
350 return -EINVAL;
351 break;
352 case AUDIT_ARG0:
353 case AUDIT_ARG1:
354 case AUDIT_ARG2:
355 case AUDIT_ARG3:
356 case AUDIT_SUBJ_USER:
357 case AUDIT_SUBJ_ROLE:
358 case AUDIT_SUBJ_TYPE:
359 case AUDIT_SUBJ_SEN:
360 case AUDIT_SUBJ_CLR:
361 case AUDIT_OBJ_USER:
362 case AUDIT_OBJ_ROLE:
363 case AUDIT_OBJ_TYPE:
364 case AUDIT_OBJ_LEV_LOW:
365 case AUDIT_OBJ_LEV_HIGH:
366 case AUDIT_WATCH:
367 case AUDIT_DIR:
368 case AUDIT_FILTERKEY:
369 break;
Eric W. Biederman780a7652013-04-09 02:22:10 -0700370 case AUDIT_LOGINUID_SET:
371 if ((f->val != 0) && (f->val != 1))
372 return -EINVAL;
373 /* FALL THROUGH */
Eric Parisab61d382013-04-16 17:26:51 -0400374 case AUDIT_ARCH:
375 if (f->op != Audit_not_equal && f->op != Audit_equal)
376 return -EINVAL;
377 break;
378 case AUDIT_PERM:
379 if (f->val & ~15)
380 return -EINVAL;
381 break;
382 case AUDIT_FILETYPE:
383 if (f->val & ~S_IFMT)
384 return -EINVAL;
385 break;
386 case AUDIT_FIELD_COMPARE:
387 if (f->val > AUDIT_MAX_FIELD_COMPARE)
388 return -EINVAL;
389 break;
390 };
Eric Paris62062cf2013-04-16 13:08:43 -0400391 return 0;
Amy Griffis93315ed2006-02-07 12:05:27 -0500392}
393
394/* Translate struct audit_rule_data to kernel's rule respresentation. */
395static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
396 size_t datasz)
397{
398 int err = 0;
399 struct audit_entry *entry;
400 void *bufp;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600401 size_t remain = datasz - sizeof(struct audit_rule_data);
Amy Griffis93315ed2006-02-07 12:05:27 -0500402 int i;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600403 char *str;
Amy Griffis93315ed2006-02-07 12:05:27 -0500404
405 entry = audit_to_entry_common((struct audit_rule *)data);
406 if (IS_ERR(entry))
407 goto exit_nofree;
408
409 bufp = data->buf;
410 entry->rule.vers_ops = 2;
411 for (i = 0; i < data->field_count; i++) {
412 struct audit_field *f = &entry->rule.fields[i];
413
414 err = -EINVAL;
Al Viro5af75d82008-12-16 05:59:26 -0500415
416 f->op = audit_to_op(data->fieldflags[i]);
417 if (f->op == Audit_bad)
Amy Griffis93315ed2006-02-07 12:05:27 -0500418 goto exit_free;
419
Amy Griffis93315ed2006-02-07 12:05:27 -0500420 f->type = data->fields[i];
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600421 f->val = data->values[i];
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700422 f->uid = INVALID_UID;
423 f->gid = INVALID_GID;
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000424 f->lsm_str = NULL;
425 f->lsm_rule = NULL;
Eric Paris62062cf2013-04-16 13:08:43 -0400426
Eric W. Biederman780a7652013-04-09 02:22:10 -0700427 /* Support legacy tests for a valid loginuid */
Richard Guy Briggs42f74462013-05-20 15:08:18 -0400428 if ((f->type == AUDIT_LOGINUID) && (f->val == AUDIT_UID_UNSET)) {
Eric W. Biederman780a7652013-04-09 02:22:10 -0700429 f->type = AUDIT_LOGINUID_SET;
430 f->val = 0;
431 }
432
Eric Paris62062cf2013-04-16 13:08:43 -0400433 err = audit_field_valid(entry, f);
434 if (err)
435 goto exit_free;
436
437 err = -EINVAL;
Eric Parisab61d382013-04-16 17:26:51 -0400438 switch (f->type) {
Eric W. Biederman780a7652013-04-09 02:22:10 -0700439 case AUDIT_LOGINUID:
Al Viro0a73dcc2006-06-05 08:15:59 -0400440 case AUDIT_UID:
441 case AUDIT_EUID:
442 case AUDIT_SUID:
443 case AUDIT_FSUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700444 case AUDIT_OBJ_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700445 f->uid = make_kuid(current_user_ns(), f->val);
446 if (!uid_valid(f->uid))
447 goto exit_free;
448 break;
Al Viro0a73dcc2006-06-05 08:15:59 -0400449 case AUDIT_GID:
450 case AUDIT_EGID:
451 case AUDIT_SGID:
452 case AUDIT_FSGID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700453 case AUDIT_OBJ_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700454 f->gid = make_kgid(current_user_ns(), f->val);
455 if (!gid_valid(f->gid))
456 goto exit_free;
457 break;
Amy Griffise54dc242007-03-29 18:01:04 -0400458 case AUDIT_ARCH:
459 entry->rule.arch_f = f;
460 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500461 case AUDIT_SUBJ_USER:
462 case AUDIT_SUBJ_ROLE:
463 case AUDIT_SUBJ_TYPE:
464 case AUDIT_SUBJ_SEN:
465 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500466 case AUDIT_OBJ_USER:
467 case AUDIT_OBJ_ROLE:
468 case AUDIT_OBJ_TYPE:
469 case AUDIT_OBJ_LEV_LOW:
470 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600471 str = audit_unpack_string(&bufp, &remain, f->val);
472 if (IS_ERR(str))
473 goto exit_free;
474 entry->rule.buflen += f->val;
475
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200476 err = security_audit_rule_init(f->type, f->op, str,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000477 (void **)&f->lsm_rule);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600478 /* Keep currently invalid fields around in case they
479 * become valid after a policy reload. */
480 if (err == -EINVAL) {
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200481 printk(KERN_WARNING "audit rule for LSM "
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600482 "\'%s\' is invalid\n", str);
483 err = 0;
484 }
485 if (err) {
486 kfree(str);
487 goto exit_free;
488 } else
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000489 f->lsm_str = str;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600490 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400491 case AUDIT_WATCH:
492 str = audit_unpack_string(&bufp, &remain, f->val);
493 if (IS_ERR(str))
494 goto exit_free;
495 entry->rule.buflen += f->val;
496
497 err = audit_to_watch(&entry->rule, str, f->val, f->op);
498 if (err) {
499 kfree(str);
500 goto exit_free;
501 }
502 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400503 case AUDIT_DIR:
504 str = audit_unpack_string(&bufp, &remain, f->val);
505 if (IS_ERR(str))
506 goto exit_free;
507 entry->rule.buflen += f->val;
508
509 err = audit_make_tree(&entry->rule, str, f->op);
510 kfree(str);
511 if (err)
512 goto exit_free;
513 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400514 case AUDIT_INODE:
515 err = audit_to_inode(&entry->rule, f);
516 if (err)
517 goto exit_free;
518 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400519 case AUDIT_FILTERKEY:
Amy Griffis5adc8a62006-06-14 18:45:21 -0400520 if (entry->rule.filterkey || f->val > AUDIT_MAX_KEY_LEN)
521 goto exit_free;
522 str = audit_unpack_string(&bufp, &remain, f->val);
523 if (IS_ERR(str))
524 goto exit_free;
525 entry->rule.buflen += f->val;
526 entry->rule.filterkey = str;
527 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400528 }
529 }
530
Al Viro5af75d82008-12-16 05:59:26 -0500531 if (entry->rule.inode_f && entry->rule.inode_f->op == Audit_not_equal)
532 entry->rule.inode_f = NULL;
Amy Griffis93315ed2006-02-07 12:05:27 -0500533
534exit_nofree:
535 return entry;
536
537exit_free:
Chen Gang373e0f32013-04-29 15:05:18 -0700538 if (entry->rule.watch)
539 audit_put_watch(entry->rule.watch); /* matches initial get */
540 if (entry->rule.tree)
541 audit_put_tree(entry->rule.tree); /* that's the temporary one */
Amy Griffis93315ed2006-02-07 12:05:27 -0500542 audit_free_rule(entry);
543 return ERR_PTR(err);
544}
545
546/* Pack a filter field's string representation into data block. */
Al Viro74c3cbe2007-07-22 08:04:18 -0400547static inline size_t audit_pack_string(void **bufp, const char *str)
Amy Griffis93315ed2006-02-07 12:05:27 -0500548{
549 size_t len = strlen(str);
550
551 memcpy(*bufp, str, len);
552 *bufp += len;
553
554 return len;
555}
556
Amy Griffis93315ed2006-02-07 12:05:27 -0500557/* Translate kernel rule respresentation to struct audit_rule_data. */
558static struct audit_rule_data *audit_krule_to_data(struct audit_krule *krule)
559{
560 struct audit_rule_data *data;
561 void *bufp;
562 int i;
563
564 data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL);
565 if (unlikely(!data))
Amy Griffis0a3b4832006-05-02 15:06:01 -0400566 return NULL;
Amy Griffis93315ed2006-02-07 12:05:27 -0500567 memset(data, 0, sizeof(*data));
568
569 data->flags = krule->flags | krule->listnr;
570 data->action = krule->action;
571 data->field_count = krule->field_count;
572 bufp = data->buf;
573 for (i = 0; i < data->field_count; i++) {
574 struct audit_field *f = &krule->fields[i];
575
576 data->fields[i] = f->type;
Al Viro5af75d82008-12-16 05:59:26 -0500577 data->fieldflags[i] = audit_ops[f->op];
Amy Griffis93315ed2006-02-07 12:05:27 -0500578 switch(f->type) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500579 case AUDIT_SUBJ_USER:
580 case AUDIT_SUBJ_ROLE:
581 case AUDIT_SUBJ_TYPE:
582 case AUDIT_SUBJ_SEN:
583 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500584 case AUDIT_OBJ_USER:
585 case AUDIT_OBJ_ROLE:
586 case AUDIT_OBJ_TYPE:
587 case AUDIT_OBJ_LEV_LOW:
588 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600589 data->buflen += data->values[i] =
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000590 audit_pack_string(&bufp, f->lsm_str);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600591 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400592 case AUDIT_WATCH:
593 data->buflen += data->values[i] =
Eric Pariscfcad622009-06-11 14:31:36 -0400594 audit_pack_string(&bufp,
595 audit_watch_path(krule->watch));
Amy Griffisf368c07d2006-04-07 16:55:56 -0400596 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400597 case AUDIT_DIR:
598 data->buflen += data->values[i] =
599 audit_pack_string(&bufp,
600 audit_tree_path(krule->tree));
601 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400602 case AUDIT_FILTERKEY:
603 data->buflen += data->values[i] =
604 audit_pack_string(&bufp, krule->filterkey);
605 break;
Amy Griffis93315ed2006-02-07 12:05:27 -0500606 default:
607 data->values[i] = f->val;
608 }
609 }
610 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) data->mask[i] = krule->mask[i];
611
612 return data;
613}
614
615/* Compare two rules in kernel format. Considered success if rules
616 * don't match. */
617static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b)
David Woodhousefe7752b2005-12-15 18:33:52 +0000618{
619 int i;
620
Amy Griffis93315ed2006-02-07 12:05:27 -0500621 if (a->flags != b->flags ||
622 a->listnr != b->listnr ||
623 a->action != b->action ||
624 a->field_count != b->field_count)
David Woodhousefe7752b2005-12-15 18:33:52 +0000625 return 1;
626
627 for (i = 0; i < a->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500628 if (a->fields[i].type != b->fields[i].type ||
629 a->fields[i].op != b->fields[i].op)
David Woodhousefe7752b2005-12-15 18:33:52 +0000630 return 1;
Amy Griffis93315ed2006-02-07 12:05:27 -0500631
632 switch(a->fields[i].type) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500633 case AUDIT_SUBJ_USER:
634 case AUDIT_SUBJ_ROLE:
635 case AUDIT_SUBJ_TYPE:
636 case AUDIT_SUBJ_SEN:
637 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500638 case AUDIT_OBJ_USER:
639 case AUDIT_OBJ_ROLE:
640 case AUDIT_OBJ_TYPE:
641 case AUDIT_OBJ_LEV_LOW:
642 case AUDIT_OBJ_LEV_HIGH:
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000643 if (strcmp(a->fields[i].lsm_str, b->fields[i].lsm_str))
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600644 return 1;
645 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400646 case AUDIT_WATCH:
Eric Pariscfcad622009-06-11 14:31:36 -0400647 if (strcmp(audit_watch_path(a->watch),
648 audit_watch_path(b->watch)))
Amy Griffisf368c07d2006-04-07 16:55:56 -0400649 return 1;
650 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400651 case AUDIT_DIR:
652 if (strcmp(audit_tree_path(a->tree),
653 audit_tree_path(b->tree)))
654 return 1;
655 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400656 case AUDIT_FILTERKEY:
657 /* both filterkeys exist based on above type compare */
658 if (strcmp(a->filterkey, b->filterkey))
659 return 1;
660 break;
Eric W. Biedermanca57ec02012-09-11 02:18:08 -0700661 case AUDIT_UID:
662 case AUDIT_EUID:
663 case AUDIT_SUID:
664 case AUDIT_FSUID:
665 case AUDIT_LOGINUID:
666 case AUDIT_OBJ_UID:
667 if (!uid_eq(a->fields[i].uid, b->fields[i].uid))
668 return 1;
669 break;
670 case AUDIT_GID:
671 case AUDIT_EGID:
672 case AUDIT_SGID:
673 case AUDIT_FSGID:
674 case AUDIT_OBJ_GID:
675 if (!gid_eq(a->fields[i].gid, b->fields[i].gid))
676 return 1;
677 break;
Amy Griffis93315ed2006-02-07 12:05:27 -0500678 default:
679 if (a->fields[i].val != b->fields[i].val)
680 return 1;
681 }
David Woodhousefe7752b2005-12-15 18:33:52 +0000682 }
683
684 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
685 if (a->mask[i] != b->mask[i])
686 return 1;
687
688 return 0;
689}
690
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000691/* Duplicate LSM field information. The lsm_rule is opaque, so must be
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600692 * re-initialized. */
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200693static inline int audit_dupe_lsm_field(struct audit_field *df,
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600694 struct audit_field *sf)
695{
696 int ret = 0;
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000697 char *lsm_str;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600698
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000699 /* our own copy of lsm_str */
700 lsm_str = kstrdup(sf->lsm_str, GFP_KERNEL);
701 if (unlikely(!lsm_str))
Akinobu Mita3e1fbd12006-12-22 01:10:02 -0800702 return -ENOMEM;
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000703 df->lsm_str = lsm_str;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600704
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000705 /* our own (refreshed) copy of lsm_rule */
706 ret = security_audit_rule_init(df->type, df->op, df->lsm_str,
707 (void **)&df->lsm_rule);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600708 /* Keep currently invalid fields around in case they
709 * become valid after a policy reload. */
710 if (ret == -EINVAL) {
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200711 printk(KERN_WARNING "audit rule for LSM \'%s\' is "
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000712 "invalid\n", df->lsm_str);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600713 ret = 0;
714 }
715
716 return ret;
717}
718
719/* Duplicate an audit rule. This will be a deep copy with the exception
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200720 * of the watch - that pointer is carried over. The LSM specific fields
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600721 * will be updated in the copy. The point is to be able to replace the old
Amy Griffisf368c07d2006-04-07 16:55:56 -0400722 * rule with the new rule in the filterlist, then free the old rule.
723 * The rlist element is undefined; list manipulations are handled apart from
724 * the initial copy. */
Eric Parisae7b8f42009-12-17 20:12:04 -0500725struct audit_entry *audit_dupe_rule(struct audit_krule *old)
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600726{
727 u32 fcount = old->field_count;
728 struct audit_entry *entry;
729 struct audit_krule *new;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400730 char *fk;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600731 int i, err = 0;
732
733 entry = audit_init_entry(fcount);
734 if (unlikely(!entry))
735 return ERR_PTR(-ENOMEM);
736
737 new = &entry->rule;
738 new->vers_ops = old->vers_ops;
739 new->flags = old->flags;
740 new->listnr = old->listnr;
741 new->action = old->action;
742 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
743 new->mask[i] = old->mask[i];
Al Viro0590b932008-12-14 23:45:27 -0500744 new->prio = old->prio;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600745 new->buflen = old->buflen;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400746 new->inode_f = old->inode_f;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600747 new->field_count = old->field_count;
Eric Parisae7b8f42009-12-17 20:12:04 -0500748
Al Viro74c3cbe2007-07-22 08:04:18 -0400749 /*
750 * note that we are OK with not refcounting here; audit_match_tree()
751 * never dereferences tree and we can't get false positives there
752 * since we'd have to have rule gone from the list *and* removed
753 * before the chunks found by lookup had been allocated, i.e. before
754 * the beginning of list scan.
755 */
756 new->tree = old->tree;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600757 memcpy(new->fields, old->fields, sizeof(struct audit_field) * fcount);
758
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000759 /* deep copy this information, updating the lsm_rule fields, because
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600760 * the originals will all be freed when the old rule is freed. */
761 for (i = 0; i < fcount; i++) {
762 switch (new->fields[i].type) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500763 case AUDIT_SUBJ_USER:
764 case AUDIT_SUBJ_ROLE:
765 case AUDIT_SUBJ_TYPE:
766 case AUDIT_SUBJ_SEN:
767 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500768 case AUDIT_OBJ_USER:
769 case AUDIT_OBJ_ROLE:
770 case AUDIT_OBJ_TYPE:
771 case AUDIT_OBJ_LEV_LOW:
772 case AUDIT_OBJ_LEV_HIGH:
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200773 err = audit_dupe_lsm_field(&new->fields[i],
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600774 &old->fields[i]);
Amy Griffis5adc8a62006-06-14 18:45:21 -0400775 break;
776 case AUDIT_FILTERKEY:
777 fk = kstrdup(old->filterkey, GFP_KERNEL);
778 if (unlikely(!fk))
779 err = -ENOMEM;
780 else
781 new->filterkey = fk;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600782 }
783 if (err) {
784 audit_free_rule(entry);
785 return ERR_PTR(err);
786 }
787 }
788
Eric Parisae7b8f42009-12-17 20:12:04 -0500789 if (old->watch) {
790 audit_get_watch(old->watch);
791 new->watch = old->watch;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400792 }
793
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600794 return entry;
795}
796
Amy Griffisf368c07d2006-04-07 16:55:56 -0400797/* Find an existing audit rule.
798 * Caller must hold audit_filter_mutex to prevent stale rule data. */
799static struct audit_entry *audit_find_rule(struct audit_entry *entry,
Al Viro36c4f1b12008-12-15 01:50:28 -0500800 struct list_head **p)
Amy Griffisf368c07d2006-04-07 16:55:56 -0400801{
802 struct audit_entry *e, *found = NULL;
Al Viro36c4f1b12008-12-15 01:50:28 -0500803 struct list_head *list;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400804 int h;
805
Al Viro36c4f1b12008-12-15 01:50:28 -0500806 if (entry->rule.inode_f) {
807 h = audit_hash_ino(entry->rule.inode_f->val);
808 *p = list = &audit_inode_hash[h];
809 } else if (entry->rule.watch) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400810 /* we don't know the inode number, so must walk entire hash */
811 for (h = 0; h < AUDIT_INODE_BUCKETS; h++) {
812 list = &audit_inode_hash[h];
813 list_for_each_entry(e, list, list)
814 if (!audit_compare_rule(&entry->rule, &e->rule)) {
815 found = e;
816 goto out;
817 }
818 }
819 goto out;
Al Viro36c4f1b12008-12-15 01:50:28 -0500820 } else {
821 *p = list = &audit_filter_list[entry->rule.listnr];
Amy Griffisf368c07d2006-04-07 16:55:56 -0400822 }
823
824 list_for_each_entry(e, list, list)
825 if (!audit_compare_rule(&entry->rule, &e->rule)) {
826 found = e;
827 goto out;
828 }
829
830out:
831 return found;
832}
833
Al Viro0590b932008-12-14 23:45:27 -0500834static u64 prio_low = ~0ULL/2;
835static u64 prio_high = ~0ULL/2 - 1;
836
Amy Griffisf368c07d2006-04-07 16:55:56 -0400837/* Add rule to given filterlist if not a duplicate. */
Al Viro36c4f1b12008-12-15 01:50:28 -0500838static inline int audit_add_rule(struct audit_entry *entry)
Amy Griffisf368c07d2006-04-07 16:55:56 -0400839{
840 struct audit_entry *e;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400841 struct audit_watch *watch = entry->rule.watch;
Al Viro74c3cbe2007-07-22 08:04:18 -0400842 struct audit_tree *tree = entry->rule.tree;
Al Viro36c4f1b12008-12-15 01:50:28 -0500843 struct list_head *list;
Eric Parisae7b8f42009-12-17 20:12:04 -0500844 int err;
Al Viro471a5c72006-07-10 08:29:24 -0400845#ifdef CONFIG_AUDITSYSCALL
846 int dont_count = 0;
847
848 /* If either of these, don't count towards total */
849 if (entry->rule.listnr == AUDIT_FILTER_USER ||
850 entry->rule.listnr == AUDIT_FILTER_TYPE)
851 dont_count = 1;
852#endif
Amy Griffisf368c07d2006-04-07 16:55:56 -0400853
Amy Griffisf368c07d2006-04-07 16:55:56 -0400854 mutex_lock(&audit_filter_mutex);
Al Viro36c4f1b12008-12-15 01:50:28 -0500855 e = audit_find_rule(entry, &list);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400856 if (e) {
Eric Paris35fe4d02009-06-11 14:31:36 -0400857 mutex_unlock(&audit_filter_mutex);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400858 err = -EEXIST;
Al Viro74c3cbe2007-07-22 08:04:18 -0400859 /* normally audit_add_tree_rule() will free it on failure */
860 if (tree)
861 audit_put_tree(tree);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400862 goto error;
863 }
864
Amy Griffisf368c07d2006-04-07 16:55:56 -0400865 if (watch) {
866 /* audit_filter_mutex is dropped and re-taken during this call */
Eric Parisae7b8f42009-12-17 20:12:04 -0500867 err = audit_add_watch(&entry->rule, &list);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400868 if (err) {
869 mutex_unlock(&audit_filter_mutex);
Chen Gang2f992ee2013-07-08 15:59:38 -0700870 /*
871 * normally audit_add_tree_rule() will free it
872 * on failure
873 */
874 if (tree)
875 audit_put_tree(tree);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400876 goto error;
877 }
David Woodhousefe7752b2005-12-15 18:33:52 +0000878 }
Al Viro74c3cbe2007-07-22 08:04:18 -0400879 if (tree) {
880 err = audit_add_tree_rule(&entry->rule);
881 if (err) {
882 mutex_unlock(&audit_filter_mutex);
883 goto error;
884 }
885 }
David Woodhousefe7752b2005-12-15 18:33:52 +0000886
Al Viro0590b932008-12-14 23:45:27 -0500887 entry->rule.prio = ~0ULL;
888 if (entry->rule.listnr == AUDIT_FILTER_EXIT) {
889 if (entry->rule.flags & AUDIT_FILTER_PREPEND)
890 entry->rule.prio = ++prio_high;
891 else
892 entry->rule.prio = --prio_low;
893 }
894
David Woodhousefe7752b2005-12-15 18:33:52 +0000895 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
Al Viroe45aa212008-12-15 01:17:50 -0500896 list_add(&entry->rule.list,
897 &audit_rules_list[entry->rule.listnr]);
David Woodhousefe7752b2005-12-15 18:33:52 +0000898 list_add_rcu(&entry->list, list);
Amy Griffis6a2bcee2006-06-02 13:16:01 -0400899 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
David Woodhousefe7752b2005-12-15 18:33:52 +0000900 } else {
Al Viroe45aa212008-12-15 01:17:50 -0500901 list_add_tail(&entry->rule.list,
902 &audit_rules_list[entry->rule.listnr]);
David Woodhousefe7752b2005-12-15 18:33:52 +0000903 list_add_tail_rcu(&entry->list, list);
904 }
Al Viro471a5c72006-07-10 08:29:24 -0400905#ifdef CONFIG_AUDITSYSCALL
906 if (!dont_count)
907 audit_n_rules++;
Amy Griffise54dc242007-03-29 18:01:04 -0400908
909 if (!audit_match_signal(entry))
910 audit_signals++;
Al Viro471a5c72006-07-10 08:29:24 -0400911#endif
Amy Griffisf368c07d2006-04-07 16:55:56 -0400912 mutex_unlock(&audit_filter_mutex);
David Woodhousefe7752b2005-12-15 18:33:52 +0000913
Amy Griffisf368c07d2006-04-07 16:55:56 -0400914 return 0;
915
916error:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400917 if (watch)
918 audit_put_watch(watch); /* tmp watch, matches initial get */
919 return err;
David Woodhousefe7752b2005-12-15 18:33:52 +0000920}
921
Amy Griffisf368c07d2006-04-07 16:55:56 -0400922/* Remove an existing rule from filterlist. */
Al Viro36c4f1b12008-12-15 01:50:28 -0500923static inline int audit_del_rule(struct audit_entry *entry)
David Woodhousefe7752b2005-12-15 18:33:52 +0000924{
925 struct audit_entry *e;
Eric Pariscfcad622009-06-11 14:31:36 -0400926 struct audit_watch *watch = entry->rule.watch;
Al Viro74c3cbe2007-07-22 08:04:18 -0400927 struct audit_tree *tree = entry->rule.tree;
Al Viro36c4f1b12008-12-15 01:50:28 -0500928 struct list_head *list;
Al Viro36c4f1b12008-12-15 01:50:28 -0500929 int ret = 0;
Al Viro471a5c72006-07-10 08:29:24 -0400930#ifdef CONFIG_AUDITSYSCALL
931 int dont_count = 0;
932
933 /* If either of these, don't count towards total */
934 if (entry->rule.listnr == AUDIT_FILTER_USER ||
935 entry->rule.listnr == AUDIT_FILTER_TYPE)
936 dont_count = 1;
937#endif
David Woodhousefe7752b2005-12-15 18:33:52 +0000938
Amy Griffisf368c07d2006-04-07 16:55:56 -0400939 mutex_lock(&audit_filter_mutex);
Al Viro36c4f1b12008-12-15 01:50:28 -0500940 e = audit_find_rule(entry, &list);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400941 if (!e) {
942 mutex_unlock(&audit_filter_mutex);
943 ret = -ENOENT;
944 goto out;
945 }
946
Eric Pariscfcad622009-06-11 14:31:36 -0400947 if (e->rule.watch)
Eric Parisa05fb6c2009-12-17 20:12:05 -0500948 audit_remove_watch_rule(&e->rule);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400949
Al Viro74c3cbe2007-07-22 08:04:18 -0400950 if (e->rule.tree)
951 audit_remove_tree_rule(&e->rule);
952
Amy Griffisf368c07d2006-04-07 16:55:56 -0400953 list_del_rcu(&e->list);
Al Viroe45aa212008-12-15 01:17:50 -0500954 list_del(&e->rule.list);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400955 call_rcu(&e->rcu, audit_free_rule_rcu);
956
Al Viro471a5c72006-07-10 08:29:24 -0400957#ifdef CONFIG_AUDITSYSCALL
958 if (!dont_count)
959 audit_n_rules--;
Amy Griffise54dc242007-03-29 18:01:04 -0400960
961 if (!audit_match_signal(entry))
962 audit_signals--;
Al Viro471a5c72006-07-10 08:29:24 -0400963#endif
Amy Griffisf368c07d2006-04-07 16:55:56 -0400964 mutex_unlock(&audit_filter_mutex);
965
Amy Griffisf368c07d2006-04-07 16:55:56 -0400966out:
Eric Pariscfcad622009-06-11 14:31:36 -0400967 if (watch)
968 audit_put_watch(watch); /* match initial get */
Al Viro74c3cbe2007-07-22 08:04:18 -0400969 if (tree)
970 audit_put_tree(tree); /* that's the temporary one */
Amy Griffisf368c07d2006-04-07 16:55:56 -0400971
972 return ret;
David Woodhousefe7752b2005-12-15 18:33:52 +0000973}
974
Amy Griffis93315ed2006-02-07 12:05:27 -0500975/* List rules using struct audit_rule_data. */
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400976static void audit_list_rules(__u32 portid, int seq, struct sk_buff_head *q)
Amy Griffis93315ed2006-02-07 12:05:27 -0500977{
Al Viro9044e6b2006-05-22 01:09:24 -0400978 struct sk_buff *skb;
Al Viroe45aa212008-12-15 01:17:50 -0500979 struct audit_krule *r;
Amy Griffis93315ed2006-02-07 12:05:27 -0500980 int i;
981
Amy Griffisf368c07d2006-04-07 16:55:56 -0400982 /* This is a blocking read, so use audit_filter_mutex instead of rcu
983 * iterator to sync with list writers. */
Amy Griffis93315ed2006-02-07 12:05:27 -0500984 for (i=0; i<AUDIT_NR_FILTERS; i++) {
Al Viroe45aa212008-12-15 01:17:50 -0500985 list_for_each_entry(r, &audit_rules_list[i], list) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500986 struct audit_rule_data *data;
987
Al Viroe45aa212008-12-15 01:17:50 -0500988 data = audit_krule_to_data(r);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400989 if (unlikely(!data))
990 break;
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400991 skb = audit_make_reply(portid, seq, AUDIT_LIST_RULES,
992 0, 1, data,
993 sizeof(*data) + data->buflen);
Al Viro9044e6b2006-05-22 01:09:24 -0400994 if (skb)
995 skb_queue_tail(q, skb);
Amy Griffis93315ed2006-02-07 12:05:27 -0500996 kfree(data);
997 }
998 }
Richard Guy Briggsf9441632013-08-14 11:32:45 -0400999 skb = audit_make_reply(portid, seq, AUDIT_LIST_RULES, 1, 1, NULL, 0);
Al Viro9044e6b2006-05-22 01:09:24 -04001000 if (skb)
1001 skb_queue_tail(q, skb);
Amy Griffis93315ed2006-02-07 12:05:27 -05001002}
1003
Amy Griffis5adc8a62006-06-14 18:45:21 -04001004/* Log rule additions and removals */
Eric Parisdc9eb692013-04-19 13:23:09 -04001005static void audit_log_rule_change(char *action, struct audit_krule *rule, int res)
Amy Griffis5adc8a62006-06-14 18:45:21 -04001006{
1007 struct audit_buffer *ab;
Eric Parisdc9eb692013-04-19 13:23:09 -04001008 uid_t loginuid = from_kuid(&init_user_ns, audit_get_loginuid(current));
Eric Paris4440e852013-11-27 17:35:17 -05001009 unsigned int sessionid = audit_get_sessionid(current);
Amy Griffis5adc8a62006-06-14 18:45:21 -04001010
Eric Paris1a6b9f22008-01-07 17:09:31 -05001011 if (!audit_enabled)
1012 return;
1013
Amy Griffis5adc8a62006-06-14 18:45:21 -04001014 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
1015 if (!ab)
1016 return;
Eric Parisdc9eb692013-04-19 13:23:09 -04001017 audit_log_format(ab, "auid=%u ses=%u" ,loginuid, sessionid);
Eric Parisb122c372013-04-19 15:00:33 -04001018 audit_log_task_context(ab);
Eric Paris9d960982009-06-11 14:31:37 -04001019 audit_log_format(ab, " op=");
1020 audit_log_string(ab, action);
1021 audit_log_key(ab, rule->filterkey);
Amy Griffis5adc8a62006-06-14 18:45:21 -04001022 audit_log_format(ab, " list=%d res=%d", rule->listnr, res);
1023 audit_log_end(ab);
1024}
1025
David Woodhousefe7752b2005-12-15 18:33:52 +00001026/**
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001027 * audit_rule_change - apply all rules to the specified message type
David Woodhousefe7752b2005-12-15 18:33:52 +00001028 * @type: audit message type
Richard Guy Briggsf9441632013-08-14 11:32:45 -04001029 * @portid: target port id for netlink audit messages
David Woodhousefe7752b2005-12-15 18:33:52 +00001030 * @seq: netlink audit message sequence (serial) number
1031 * @data: payload data
Amy Griffis93315ed2006-02-07 12:05:27 -05001032 * @datasz: size of payload data
David Woodhousefe7752b2005-12-15 18:33:52 +00001033 */
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001034int audit_rule_change(int type, __u32 portid, int seq, void *data,
1035 size_t datasz)
David Woodhousefe7752b2005-12-15 18:33:52 +00001036{
Amy Griffis93315ed2006-02-07 12:05:27 -05001037 int err = 0;
1038 struct audit_entry *entry;
David Woodhousefe7752b2005-12-15 18:33:52 +00001039
1040 switch (type) {
Amy Griffis93315ed2006-02-07 12:05:27 -05001041 case AUDIT_ADD_RULE:
Eric Paris18900902013-04-18 19:16:36 -04001042 entry = audit_data_to_entry(data, datasz);
Amy Griffis93315ed2006-02-07 12:05:27 -05001043 if (IS_ERR(entry))
1044 return PTR_ERR(entry);
David Woodhousefe7752b2005-12-15 18:33:52 +00001045
Al Viro36c4f1b12008-12-15 01:50:28 -05001046 err = audit_add_rule(entry);
Eric Parisdc9eb692013-04-19 13:23:09 -04001047 audit_log_rule_change("add rule", &entry->rule, !err);
Steve Grubb5d330102006-01-09 09:48:17 -05001048 if (err)
Amy Griffis93315ed2006-02-07 12:05:27 -05001049 audit_free_rule(entry);
David Woodhousefe7752b2005-12-15 18:33:52 +00001050 break;
Amy Griffis93315ed2006-02-07 12:05:27 -05001051 case AUDIT_DEL_RULE:
Eric Paris18900902013-04-18 19:16:36 -04001052 entry = audit_data_to_entry(data, datasz);
Amy Griffis93315ed2006-02-07 12:05:27 -05001053 if (IS_ERR(entry))
1054 return PTR_ERR(entry);
David Woodhousefe7752b2005-12-15 18:33:52 +00001055
Al Viro36c4f1b12008-12-15 01:50:28 -05001056 err = audit_del_rule(entry);
Eric Parisdc9eb692013-04-19 13:23:09 -04001057 audit_log_rule_change("remove rule", &entry->rule, !err);
Amy Griffis93315ed2006-02-07 12:05:27 -05001058 audit_free_rule(entry);
David Woodhousefe7752b2005-12-15 18:33:52 +00001059 break;
1060 default:
1061 return -EINVAL;
1062 }
1063
1064 return err;
1065}
1066
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001067/**
1068 * audit_list_rules_send - list the audit rules
1069 * @portid: target portid for netlink audit messages
1070 * @seq: netlink audit message sequence (serial) number
1071 */
1072int audit_list_rules_send(__u32 portid, int seq)
1073{
1074 struct task_struct *tsk;
1075 struct audit_netlink_list *dest;
1076 int err = 0;
1077
1078 /* We can't just spew out the rules here because we might fill
1079 * the available socket buffer space and deadlock waiting for
1080 * auditctl to read from it... which isn't ever going to
1081 * happen if we're actually running in the context of auditctl
1082 * trying to _send_ the stuff */
1083
1084 dest = kmalloc(sizeof(struct audit_netlink_list), GFP_KERNEL);
1085 if (!dest)
1086 return -ENOMEM;
Eric W. Biederman48095d92014-02-03 17:25:33 -08001087 dest->net = get_net(current->nsproxy->net_ns);
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001088 dest->portid = portid;
Richard Guy Briggsce0d9f02013-11-20 14:01:53 -05001089 skb_queue_head_init(&dest->q);
1090
1091 mutex_lock(&audit_filter_mutex);
1092 audit_list_rules(portid, seq, &dest->q);
1093 mutex_unlock(&audit_filter_mutex);
1094
1095 tsk = kthread_run(audit_send_list, dest, "audit_send_list");
1096 if (IS_ERR(tsk)) {
1097 skb_queue_purge(&dest->q);
1098 kfree(dest);
1099 err = PTR_ERR(tsk);
1100 }
1101
1102 return err;
1103}
1104
Al Viro5af75d82008-12-16 05:59:26 -05001105int audit_comparator(u32 left, u32 op, u32 right)
David Woodhousefe7752b2005-12-15 18:33:52 +00001106{
1107 switch (op) {
Al Viro5af75d82008-12-16 05:59:26 -05001108 case Audit_equal:
David Woodhousefe7752b2005-12-15 18:33:52 +00001109 return (left == right);
Al Viro5af75d82008-12-16 05:59:26 -05001110 case Audit_not_equal:
David Woodhousefe7752b2005-12-15 18:33:52 +00001111 return (left != right);
Al Viro5af75d82008-12-16 05:59:26 -05001112 case Audit_lt:
David Woodhousefe7752b2005-12-15 18:33:52 +00001113 return (left < right);
Al Viro5af75d82008-12-16 05:59:26 -05001114 case Audit_le:
David Woodhousefe7752b2005-12-15 18:33:52 +00001115 return (left <= right);
Al Viro5af75d82008-12-16 05:59:26 -05001116 case Audit_gt:
David Woodhousefe7752b2005-12-15 18:33:52 +00001117 return (left > right);
Al Viro5af75d82008-12-16 05:59:26 -05001118 case Audit_ge:
David Woodhousefe7752b2005-12-15 18:33:52 +00001119 return (left >= right);
Al Viro5af75d82008-12-16 05:59:26 -05001120 case Audit_bitmask:
Eric Paris74f23452007-06-04 17:00:14 -04001121 return (left & right);
Al Viro5af75d82008-12-16 05:59:26 -05001122 case Audit_bittest:
Eric Paris74f23452007-06-04 17:00:14 -04001123 return ((left & right) == right);
Al Viro5af75d82008-12-16 05:59:26 -05001124 default:
1125 BUG();
1126 return 0;
David Woodhousefe7752b2005-12-15 18:33:52 +00001127 }
1128}
1129
Eric W. Biedermanca57ec02012-09-11 02:18:08 -07001130int audit_uid_comparator(kuid_t left, u32 op, kuid_t right)
1131{
1132 switch (op) {
1133 case Audit_equal:
1134 return uid_eq(left, right);
1135 case Audit_not_equal:
1136 return !uid_eq(left, right);
1137 case Audit_lt:
1138 return uid_lt(left, right);
1139 case Audit_le:
1140 return uid_lte(left, right);
1141 case Audit_gt:
1142 return uid_gt(left, right);
1143 case Audit_ge:
1144 return uid_gte(left, right);
1145 case Audit_bitmask:
1146 case Audit_bittest:
1147 default:
1148 BUG();
1149 return 0;
1150 }
1151}
1152
1153int audit_gid_comparator(kgid_t left, u32 op, kgid_t right)
1154{
1155 switch (op) {
1156 case Audit_equal:
1157 return gid_eq(left, right);
1158 case Audit_not_equal:
1159 return !gid_eq(left, right);
1160 case Audit_lt:
1161 return gid_lt(left, right);
1162 case Audit_le:
1163 return gid_lte(left, right);
1164 case Audit_gt:
1165 return gid_gt(left, right);
1166 case Audit_ge:
1167 return gid_gte(left, right);
1168 case Audit_bitmask:
1169 case Audit_bittest:
1170 default:
1171 BUG();
1172 return 0;
1173 }
1174}
1175
Jeff Laytonbfcec702012-10-10 15:25:23 -04001176/**
1177 * parent_len - find the length of the parent portion of a pathname
1178 * @path: pathname of which to determine length
1179 */
1180int parent_len(const char *path)
1181{
1182 int plen;
1183 const char *p;
1184
1185 plen = strlen(path);
1186
1187 if (plen == 0)
1188 return plen;
1189
1190 /* disregard trailing slashes */
1191 p = path + plen - 1;
1192 while ((*p == '/') && (p > path))
1193 p--;
1194
1195 /* walk backward until we find the next slash or hit beginning */
1196 while ((*p != '/') && (p > path))
1197 p--;
1198
1199 /* did we find a slash? Then increment to include it in path */
1200 if (*p == '/')
1201 p++;
1202
1203 return p - path;
1204}
1205
Jeff Laytone3d6b072012-10-10 15:25:25 -04001206/**
1207 * audit_compare_dname_path - compare given dentry name with last component in
1208 * given path. Return of 0 indicates a match.
1209 * @dname: dentry name that we're comparing
1210 * @path: full pathname that we're comparing
1211 * @parentlen: length of the parent if known. Passing in AUDIT_NAME_FULL
1212 * here indicates that we must compute this value.
1213 */
1214int audit_compare_dname_path(const char *dname, const char *path, int parentlen)
Amy Griffisf368c07d2006-04-07 16:55:56 -04001215{
Jeff Laytone3d6b072012-10-10 15:25:25 -04001216 int dlen, pathlen;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001217 const char *p;
David Woodhousefe7752b2005-12-15 18:33:52 +00001218
Amy Griffisf368c07d2006-04-07 16:55:56 -04001219 dlen = strlen(dname);
Eric Paris29e9a342012-10-10 15:25:24 -04001220 pathlen = strlen(path);
1221 if (pathlen < dlen)
Amy Griffisf368c07d2006-04-07 16:55:56 -04001222 return 1;
1223
Jeff Laytone3d6b072012-10-10 15:25:25 -04001224 parentlen = parentlen == AUDIT_NAME_FULL ? parent_len(path) : parentlen;
Eric Paris29e9a342012-10-10 15:25:24 -04001225 if (pathlen - parentlen != dlen)
Amy Griffisf368c07d2006-04-07 16:55:56 -04001226 return 1;
Eric Paris29e9a342012-10-10 15:25:24 -04001227
1228 p = path + parentlen;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001229
1230 return strncmp(p, dname, dlen);
1231}
David Woodhousefe7752b2005-12-15 18:33:52 +00001232
Eric Paris62062cf2013-04-16 13:08:43 -04001233static int audit_filter_user_rules(struct audit_krule *rule, int type,
David Woodhousefe7752b2005-12-15 18:33:52 +00001234 enum audit_state *state)
1235{
1236 int i;
1237
1238 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -05001239 struct audit_field *f = &rule->fields[i];
David Woodhousefe7752b2005-12-15 18:33:52 +00001240 int result = 0;
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001241 u32 sid;
David Woodhousefe7752b2005-12-15 18:33:52 +00001242
Amy Griffis93315ed2006-02-07 12:05:27 -05001243 switch (f->type) {
David Woodhousefe7752b2005-12-15 18:33:52 +00001244 case AUDIT_PID:
Eric W. Biederman02276bd2012-09-10 23:10:16 -07001245 result = audit_comparator(task_pid_vnr(current), f->op, f->val);
David Woodhousefe7752b2005-12-15 18:33:52 +00001246 break;
1247 case AUDIT_UID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -07001248 result = audit_uid_comparator(current_uid(), f->op, f->uid);
David Woodhousefe7752b2005-12-15 18:33:52 +00001249 break;
1250 case AUDIT_GID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -07001251 result = audit_gid_comparator(current_gid(), f->op, f->gid);
David Woodhousefe7752b2005-12-15 18:33:52 +00001252 break;
1253 case AUDIT_LOGINUID:
Eric W. Biedermanca57ec02012-09-11 02:18:08 -07001254 result = audit_uid_comparator(audit_get_loginuid(current),
1255 f->op, f->uid);
David Woodhousefe7752b2005-12-15 18:33:52 +00001256 break;
Eric W. Biederman780a7652013-04-09 02:22:10 -07001257 case AUDIT_LOGINUID_SET:
1258 result = audit_comparator(audit_loginuid_set(current),
1259 f->op, f->val);
1260 break;
Eric Paris62062cf2013-04-16 13:08:43 -04001261 case AUDIT_MSGTYPE:
1262 result = audit_comparator(type, f->op, f->val);
1263 break;
Miloslav Trmacd29be152010-09-16 18:14:11 -04001264 case AUDIT_SUBJ_USER:
1265 case AUDIT_SUBJ_ROLE:
1266 case AUDIT_SUBJ_TYPE:
1267 case AUDIT_SUBJ_SEN:
1268 case AUDIT_SUBJ_CLR:
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001269 if (f->lsm_rule) {
1270 security_task_getsecid(current, &sid);
1271 result = security_audit_rule_match(sid,
Miloslav Trmacd29be152010-09-16 18:14:11 -04001272 f->type,
1273 f->op,
1274 f->lsm_rule,
1275 NULL);
Patrick McHardyc53fa1e2011-03-03 10:55:40 -08001276 }
Miloslav Trmacd29be152010-09-16 18:14:11 -04001277 break;
David Woodhousefe7752b2005-12-15 18:33:52 +00001278 }
1279
1280 if (!result)
1281 return 0;
1282 }
1283 switch (rule->action) {
1284 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
David Woodhousefe7752b2005-12-15 18:33:52 +00001285 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
1286 }
1287 return 1;
1288}
1289
Eric Paris62062cf2013-04-16 13:08:43 -04001290int audit_filter_user(int type)
David Woodhousefe7752b2005-12-15 18:33:52 +00001291{
Ingo Molnar11f57ce2007-02-10 01:46:09 -08001292 enum audit_state state = AUDIT_DISABLED;
David Woodhousefe7752b2005-12-15 18:33:52 +00001293 struct audit_entry *e;
Richard Guy Briggs724e4fc2013-11-25 21:57:51 -05001294 int rc, ret;
1295
1296 ret = 1; /* Audit by default */
David Woodhousefe7752b2005-12-15 18:33:52 +00001297
1298 rcu_read_lock();
1299 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
Richard Guy Briggs724e4fc2013-11-25 21:57:51 -05001300 rc = audit_filter_user_rules(&e->rule, type, &state);
1301 if (rc) {
1302 if (rc > 0 && state == AUDIT_DISABLED)
David Woodhousefe7752b2005-12-15 18:33:52 +00001303 ret = 0;
1304 break;
1305 }
1306 }
1307 rcu_read_unlock();
1308
Richard Guy Briggs724e4fc2013-11-25 21:57:51 -05001309 return ret;
David Woodhousefe7752b2005-12-15 18:33:52 +00001310}
1311
1312int audit_filter_type(int type)
1313{
1314 struct audit_entry *e;
1315 int result = 0;
Daniel Walker9ce34212007-10-18 03:06:06 -07001316
David Woodhousefe7752b2005-12-15 18:33:52 +00001317 rcu_read_lock();
1318 if (list_empty(&audit_filter_list[AUDIT_FILTER_TYPE]))
1319 goto unlock_and_return;
1320
1321 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TYPE],
1322 list) {
David Woodhousefe7752b2005-12-15 18:33:52 +00001323 int i;
Amy Griffis93315ed2006-02-07 12:05:27 -05001324 for (i = 0; i < e->rule.field_count; i++) {
1325 struct audit_field *f = &e->rule.fields[i];
1326 if (f->type == AUDIT_MSGTYPE) {
1327 result = audit_comparator(type, f->op, f->val);
David Woodhousefe7752b2005-12-15 18:33:52 +00001328 if (!result)
1329 break;
1330 }
1331 }
1332 if (result)
1333 goto unlock_and_return;
1334 }
1335unlock_and_return:
1336 rcu_read_unlock();
1337 return result;
1338}
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001339
Al Viroe45aa212008-12-15 01:17:50 -05001340static int update_lsm_rule(struct audit_krule *r)
Al Viro1a9d0792008-12-14 12:04:02 -05001341{
Al Viroe45aa212008-12-15 01:17:50 -05001342 struct audit_entry *entry = container_of(r, struct audit_entry, rule);
Al Viro1a9d0792008-12-14 12:04:02 -05001343 struct audit_entry *nentry;
Al Viro1a9d0792008-12-14 12:04:02 -05001344 int err = 0;
1345
Al Viroe45aa212008-12-15 01:17:50 -05001346 if (!security_audit_rule_known(r))
Al Viro1a9d0792008-12-14 12:04:02 -05001347 return 0;
1348
Eric Parisae7b8f42009-12-17 20:12:04 -05001349 nentry = audit_dupe_rule(r);
Al Viro1a9d0792008-12-14 12:04:02 -05001350 if (IS_ERR(nentry)) {
1351 /* save the first error encountered for the
1352 * return value */
1353 err = PTR_ERR(nentry);
1354 audit_panic("error updating LSM filters");
Eric Parisae7b8f42009-12-17 20:12:04 -05001355 if (r->watch)
Al Viroe45aa212008-12-15 01:17:50 -05001356 list_del(&r->rlist);
Al Viro1a9d0792008-12-14 12:04:02 -05001357 list_del_rcu(&entry->list);
Al Viroe45aa212008-12-15 01:17:50 -05001358 list_del(&r->list);
Al Viro1a9d0792008-12-14 12:04:02 -05001359 } else {
Eric Parisae7b8f42009-12-17 20:12:04 -05001360 if (r->watch || r->tree)
Al Viroe45aa212008-12-15 01:17:50 -05001361 list_replace_init(&r->rlist, &nentry->rule.rlist);
Al Viro1a9d0792008-12-14 12:04:02 -05001362 list_replace_rcu(&entry->list, &nentry->list);
Al Viroe45aa212008-12-15 01:17:50 -05001363 list_replace(&r->list, &nentry->rule.list);
Al Viro1a9d0792008-12-14 12:04:02 -05001364 }
1365 call_rcu(&entry->rcu, audit_free_rule_rcu);
1366
1367 return err;
1368}
1369
Ahmed S. Darwish04305e42008-04-19 09:59:43 +10001370/* This function will re-initialize the lsm_rule field of all applicable rules.
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +02001371 * It will traverse the filter lists serarching for rules that contain LSM
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001372 * specific filter fields. When such a rule is found, it is copied, the
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +02001373 * LSM field is re-initialized, and the old rule is replaced with the
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001374 * updated rule. */
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +02001375int audit_update_lsm_rules(void)
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001376{
Al Viroe45aa212008-12-15 01:17:50 -05001377 struct audit_krule *r, *n;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001378 int i, err = 0;
1379
Amy Griffisf368c07d2006-04-07 16:55:56 -04001380 /* audit_filter_mutex synchronizes the writers */
1381 mutex_lock(&audit_filter_mutex);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001382
1383 for (i = 0; i < AUDIT_NR_FILTERS; i++) {
Al Viroe45aa212008-12-15 01:17:50 -05001384 list_for_each_entry_safe(r, n, &audit_rules_list[i], list) {
1385 int res = update_lsm_rule(r);
Al Viro1a9d0792008-12-14 12:04:02 -05001386 if (!err)
1387 err = res;
1388 }
1389 }
Amy Griffisf368c07d2006-04-07 16:55:56 -04001390 mutex_unlock(&audit_filter_mutex);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001391
1392 return err;
1393}