blob: 5d4edc6f7a32ef7ce4b4b6e4d44df6f2c74ce11a [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>
30#include <linux/inotify.h>
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +020031#include <linux/security.h>
David Woodhousefe7752b2005-12-15 18:33:52 +000032#include "audit.h"
33
Amy Griffisf368c07d2006-04-07 16:55:56 -040034/*
35 * Locking model:
36 *
37 * audit_filter_mutex:
38 * Synchronizes writes and blocking reads of audit's filterlist
39 * data. Rcu is used to traverse the filterlist and access
40 * contents of structs audit_entry, audit_watch and opaque
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +020041 * LSM rules during filtering. If modified, these structures
Amy Griffisf368c07d2006-04-07 16:55:56 -040042 * must be copied and replace their counterparts in the filterlist.
43 * An audit_parent struct is not accessed during filtering, so may
44 * be written directly provided audit_filter_mutex is held.
45 */
46
47/*
48 * Reference counting:
49 *
50 * audit_parent: lifetime is from audit_init_parent() to receipt of an IN_IGNORED
51 * event. Each audit_watch holds a reference to its associated parent.
52 *
53 * audit_watch: if added to lists, lifetime is from audit_init_watch() to
54 * audit_remove_watch(). Additionally, an audit_watch may exist
55 * temporarily to assist in searching existing filter data. Each
56 * audit_krule holds a reference to its associated watch.
57 */
58
59struct audit_parent {
60 struct list_head ilist; /* entry in inotify registration list */
61 struct list_head watches; /* associated watches */
62 struct inotify_watch wdata; /* inotify watch data */
63 unsigned flags; /* status flags */
64};
65
66/*
67 * audit_parent status flags:
68 *
69 * AUDIT_PARENT_INVALID - set anytime rules/watches are auto-removed due to
70 * a filesystem event to ensure we're adding audit watches to a valid parent.
71 * Technically not needed for IN_DELETE_SELF or IN_UNMOUNT events, as we cannot
72 * receive them while we have nameidata, but must be used for IN_MOVE_SELF which
73 * we can receive while holding nameidata.
74 */
75#define AUDIT_PARENT_INVALID 0x001
76
77/* Audit filter lists, defined in <linux/audit.h> */
David Woodhousefe7752b2005-12-15 18:33:52 +000078struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
79 LIST_HEAD_INIT(audit_filter_list[0]),
80 LIST_HEAD_INIT(audit_filter_list[1]),
81 LIST_HEAD_INIT(audit_filter_list[2]),
82 LIST_HEAD_INIT(audit_filter_list[3]),
83 LIST_HEAD_INIT(audit_filter_list[4]),
84 LIST_HEAD_INIT(audit_filter_list[5]),
85#if AUDIT_NR_FILTERS != 6
86#error Fix audit_filter_list initialiser
87#endif
88};
Al Viroe45aa212008-12-15 01:17:50 -050089static struct list_head audit_rules_list[AUDIT_NR_FILTERS] = {
90 LIST_HEAD_INIT(audit_rules_list[0]),
91 LIST_HEAD_INIT(audit_rules_list[1]),
92 LIST_HEAD_INIT(audit_rules_list[2]),
93 LIST_HEAD_INIT(audit_rules_list[3]),
94 LIST_HEAD_INIT(audit_rules_list[4]),
95 LIST_HEAD_INIT(audit_rules_list[5]),
96};
David Woodhousefe7752b2005-12-15 18:33:52 +000097
Al Viro74c3cbe2007-07-22 08:04:18 -040098DEFINE_MUTEX(audit_filter_mutex);
Amy Griffisf368c07d2006-04-07 16:55:56 -040099
Amy Griffisf368c07d2006-04-07 16:55:56 -0400100/* Inotify events we care about. */
101#define AUDIT_IN_WATCH IN_MOVE|IN_CREATE|IN_DELETE|IN_DELETE_SELF|IN_MOVE_SELF
102
103void audit_free_parent(struct inotify_watch *i_watch)
104{
105 struct audit_parent *parent;
106
107 parent = container_of(i_watch, struct audit_parent, wdata);
108 WARN_ON(!list_empty(&parent->watches));
109 kfree(parent);
110}
111
112static inline void audit_get_watch(struct audit_watch *watch)
113{
114 atomic_inc(&watch->count);
115}
116
117static void audit_put_watch(struct audit_watch *watch)
118{
119 if (atomic_dec_and_test(&watch->count)) {
120 WARN_ON(watch->parent);
121 WARN_ON(!list_empty(&watch->rules));
122 kfree(watch->path);
123 kfree(watch);
124 }
125}
126
127static void audit_remove_watch(struct audit_watch *watch)
128{
129 list_del(&watch->wlist);
130 put_inotify_watch(&watch->parent->wdata);
131 watch->parent = NULL;
132 audit_put_watch(watch); /* match initial get */
133}
134
Amy Griffis93315ed2006-02-07 12:05:27 -0500135static inline void audit_free_rule(struct audit_entry *e)
David Woodhousefe7752b2005-12-15 18:33:52 +0000136{
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600137 int i;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400138
139 /* some rules don't have associated watches */
140 if (e->rule.watch)
141 audit_put_watch(e->rule.watch);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600142 if (e->rule.fields)
143 for (i = 0; i < e->rule.field_count; i++) {
144 struct audit_field *f = &e->rule.fields[i];
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000145 kfree(f->lsm_str);
146 security_audit_rule_free(f->lsm_rule);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600147 }
Amy Griffis93315ed2006-02-07 12:05:27 -0500148 kfree(e->rule.fields);
Amy Griffis5adc8a62006-06-14 18:45:21 -0400149 kfree(e->rule.filterkey);
Amy Griffis93315ed2006-02-07 12:05:27 -0500150 kfree(e);
David Woodhousefe7752b2005-12-15 18:33:52 +0000151}
152
Al Viro74c3cbe2007-07-22 08:04:18 -0400153void audit_free_rule_rcu(struct rcu_head *head)
Amy Griffis93315ed2006-02-07 12:05:27 -0500154{
155 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
156 audit_free_rule(e);
157}
158
Amy Griffisf368c07d2006-04-07 16:55:56 -0400159/* Initialize a parent watch entry. */
160static struct audit_parent *audit_init_parent(struct nameidata *ndp)
161{
162 struct audit_parent *parent;
163 s32 wd;
164
165 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
166 if (unlikely(!parent))
167 return ERR_PTR(-ENOMEM);
168
169 INIT_LIST_HEAD(&parent->watches);
170 parent->flags = 0;
171
172 inotify_init_watch(&parent->wdata);
173 /* grab a ref so inotify watch hangs around until we take audit_filter_mutex */
174 get_inotify_watch(&parent->wdata);
Jan Blunck4ac91372008-02-14 19:34:32 -0800175 wd = inotify_add_watch(audit_ih, &parent->wdata,
176 ndp->path.dentry->d_inode, AUDIT_IN_WATCH);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400177 if (wd < 0) {
178 audit_free_parent(&parent->wdata);
179 return ERR_PTR(wd);
180 }
181
182 return parent;
183}
184
185/* Initialize a watch entry. */
186static struct audit_watch *audit_init_watch(char *path)
187{
188 struct audit_watch *watch;
189
190 watch = kzalloc(sizeof(*watch), GFP_KERNEL);
191 if (unlikely(!watch))
192 return ERR_PTR(-ENOMEM);
193
194 INIT_LIST_HEAD(&watch->rules);
195 atomic_set(&watch->count, 1);
196 watch->path = path;
197 watch->dev = (dev_t)-1;
198 watch->ino = (unsigned long)-1;
199
200 return watch;
201}
202
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600203/* Initialize an audit filterlist entry. */
204static inline struct audit_entry *audit_init_entry(u32 field_count)
205{
206 struct audit_entry *entry;
207 struct audit_field *fields;
208
209 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
210 if (unlikely(!entry))
211 return NULL;
212
213 fields = kzalloc(sizeof(*fields) * field_count, GFP_KERNEL);
214 if (unlikely(!fields)) {
215 kfree(entry);
216 return NULL;
217 }
218 entry->rule.fields = fields;
219
220 return entry;
221}
222
Amy Griffis93315ed2006-02-07 12:05:27 -0500223/* Unpack a filter field's string representation from user-space
224 * buffer. */
Al Viro74c3cbe2007-07-22 08:04:18 -0400225char *audit_unpack_string(void **bufp, size_t *remain, size_t len)
Amy Griffis93315ed2006-02-07 12:05:27 -0500226{
227 char *str;
228
229 if (!*bufp || (len == 0) || (len > *remain))
230 return ERR_PTR(-EINVAL);
231
232 /* Of the currently implemented string fields, PATH_MAX
233 * defines the longest valid length.
234 */
235 if (len > PATH_MAX)
236 return ERR_PTR(-ENAMETOOLONG);
237
238 str = kmalloc(len + 1, GFP_KERNEL);
239 if (unlikely(!str))
240 return ERR_PTR(-ENOMEM);
241
242 memcpy(str, *bufp, len);
243 str[len] = 0;
244 *bufp += len;
245 *remain -= len;
246
247 return str;
248}
249
Amy Griffisf368c07d2006-04-07 16:55:56 -0400250/* Translate an inode field to kernel respresentation. */
251static inline int audit_to_inode(struct audit_krule *krule,
252 struct audit_field *f)
253{
254 if (krule->listnr != AUDIT_FILTER_EXIT ||
Al Viro74c3cbe2007-07-22 08:04:18 -0400255 krule->watch || krule->inode_f || krule->tree)
Amy Griffisf368c07d2006-04-07 16:55:56 -0400256 return -EINVAL;
257
258 krule->inode_f = f;
259 return 0;
260}
261
262/* Translate a watch string to kernel respresentation. */
263static int audit_to_watch(struct audit_krule *krule, char *path, int len,
264 u32 op)
265{
266 struct audit_watch *watch;
267
268 if (!audit_ih)
269 return -EOPNOTSUPP;
270
271 if (path[0] != '/' || path[len-1] == '/' ||
272 krule->listnr != AUDIT_FILTER_EXIT ||
273 op & ~AUDIT_EQUAL ||
Al Viro74c3cbe2007-07-22 08:04:18 -0400274 krule->inode_f || krule->watch || krule->tree)
Amy Griffisf368c07d2006-04-07 16:55:56 -0400275 return -EINVAL;
276
277 watch = audit_init_watch(path);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -0700278 if (IS_ERR(watch))
Amy Griffisf368c07d2006-04-07 16:55:56 -0400279 return PTR_ERR(watch);
280
281 audit_get_watch(watch);
282 krule->watch = watch;
283
284 return 0;
285}
286
Al Virob9155432006-07-01 03:56:16 -0400287static __u32 *classes[AUDIT_SYSCALL_CLASSES];
288
289int __init audit_register_class(int class, unsigned *list)
290{
291 __u32 *p = kzalloc(AUDIT_BITMASK_SIZE * sizeof(__u32), GFP_KERNEL);
292 if (!p)
293 return -ENOMEM;
294 while (*list != ~0U) {
295 unsigned n = *list++;
296 if (n >= AUDIT_BITMASK_SIZE * 32 - AUDIT_SYSCALL_CLASSES) {
297 kfree(p);
298 return -EINVAL;
299 }
300 p[AUDIT_WORD(n)] |= AUDIT_BIT(n);
301 }
302 if (class >= AUDIT_SYSCALL_CLASSES || classes[class]) {
303 kfree(p);
304 return -EINVAL;
305 }
306 classes[class] = p;
307 return 0;
308}
309
Al Viro55669bf2006-08-31 19:26:40 -0400310int audit_match_class(int class, unsigned syscall)
311{
Klaus Weidnerc926e4f2007-05-16 17:45:42 -0500312 if (unlikely(syscall >= AUDIT_BITMASK_SIZE * 32))
Al Viro55669bf2006-08-31 19:26:40 -0400313 return 0;
314 if (unlikely(class >= AUDIT_SYSCALL_CLASSES || !classes[class]))
315 return 0;
316 return classes[class][AUDIT_WORD(syscall)] & AUDIT_BIT(syscall);
317}
318
Al Viro327b9ee2007-05-15 20:37:10 +0100319#ifdef CONFIG_AUDITSYSCALL
Amy Griffise54dc242007-03-29 18:01:04 -0400320static inline int audit_match_class_bits(int class, u32 *mask)
321{
322 int i;
323
324 if (classes[class]) {
325 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
326 if (mask[i] & classes[class][i])
327 return 0;
328 }
329 return 1;
330}
331
332static int audit_match_signal(struct audit_entry *entry)
333{
334 struct audit_field *arch = entry->rule.arch_f;
335
336 if (!arch) {
337 /* When arch is unspecified, we must check both masks on biarch
338 * as syscall number alone is ambiguous. */
339 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL,
340 entry->rule.mask) &&
341 audit_match_class_bits(AUDIT_CLASS_SIGNAL_32,
342 entry->rule.mask));
343 }
344
345 switch(audit_classify_arch(arch->val)) {
346 case 0: /* native */
347 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL,
348 entry->rule.mask));
349 case 1: /* 32bit on biarch */
350 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL_32,
351 entry->rule.mask));
352 default:
353 return 1;
354 }
355}
Al Viro327b9ee2007-05-15 20:37:10 +0100356#endif
Amy Griffise54dc242007-03-29 18:01:04 -0400357
Amy Griffis93315ed2006-02-07 12:05:27 -0500358/* Common user-space to kernel rule translation. */
359static inline struct audit_entry *audit_to_entry_common(struct audit_rule *rule)
360{
361 unsigned listnr;
362 struct audit_entry *entry;
Amy Griffis93315ed2006-02-07 12:05:27 -0500363 int i, err;
364
365 err = -EINVAL;
366 listnr = rule->flags & ~AUDIT_FILTER_PREPEND;
367 switch(listnr) {
368 default:
369 goto exit_err;
370 case AUDIT_FILTER_USER:
371 case AUDIT_FILTER_TYPE:
372#ifdef CONFIG_AUDITSYSCALL
373 case AUDIT_FILTER_ENTRY:
374 case AUDIT_FILTER_EXIT:
375 case AUDIT_FILTER_TASK:
376#endif
377 ;
378 }
Al Viro014149c2006-05-23 01:36:13 -0400379 if (unlikely(rule->action == AUDIT_POSSIBLE)) {
380 printk(KERN_ERR "AUDIT_POSSIBLE is deprecated\n");
381 goto exit_err;
382 }
383 if (rule->action != AUDIT_NEVER && rule->action != AUDIT_ALWAYS)
Amy Griffis93315ed2006-02-07 12:05:27 -0500384 goto exit_err;
385 if (rule->field_count > AUDIT_MAX_FIELDS)
386 goto exit_err;
387
388 err = -ENOMEM;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600389 entry = audit_init_entry(rule->field_count);
390 if (!entry)
Amy Griffis93315ed2006-02-07 12:05:27 -0500391 goto exit_err;
Amy Griffis93315ed2006-02-07 12:05:27 -0500392
393 entry->rule.flags = rule->flags & AUDIT_FILTER_PREPEND;
394 entry->rule.listnr = listnr;
395 entry->rule.action = rule->action;
396 entry->rule.field_count = rule->field_count;
Amy Griffis93315ed2006-02-07 12:05:27 -0500397
398 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
399 entry->rule.mask[i] = rule->mask[i];
400
Al Virob9155432006-07-01 03:56:16 -0400401 for (i = 0; i < AUDIT_SYSCALL_CLASSES; i++) {
402 int bit = AUDIT_BITMASK_SIZE * 32 - i - 1;
403 __u32 *p = &entry->rule.mask[AUDIT_WORD(bit)];
404 __u32 *class;
405
406 if (!(*p & AUDIT_BIT(bit)))
407 continue;
408 *p &= ~AUDIT_BIT(bit);
409 class = classes[i];
410 if (class) {
411 int j;
412 for (j = 0; j < AUDIT_BITMASK_SIZE; j++)
413 entry->rule.mask[j] |= class[j];
414 }
415 }
416
Amy Griffis93315ed2006-02-07 12:05:27 -0500417 return entry;
418
419exit_err:
420 return ERR_PTR(err);
421}
422
423/* Translate struct audit_rule to kernel's rule respresentation.
424 * Exists for backward compatibility with userspace. */
425static struct audit_entry *audit_rule_to_entry(struct audit_rule *rule)
426{
427 struct audit_entry *entry;
Harvey Harrison7719e432008-04-27 02:39:56 -0700428 struct audit_field *ino_f;
Amy Griffis93315ed2006-02-07 12:05:27 -0500429 int err = 0;
430 int i;
431
432 entry = audit_to_entry_common(rule);
433 if (IS_ERR(entry))
434 goto exit_nofree;
435
436 for (i = 0; i < rule->field_count; i++) {
437 struct audit_field *f = &entry->rule.fields[i];
438
Amy Griffis93315ed2006-02-07 12:05:27 -0500439 f->op = rule->fields[i] & (AUDIT_NEGATE|AUDIT_OPERATORS);
440 f->type = rule->fields[i] & ~(AUDIT_NEGATE|AUDIT_OPERATORS);
441 f->val = rule->values[i];
442
Amy Griffisf368c07d2006-04-07 16:55:56 -0400443 err = -EINVAL;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400444 switch(f->type) {
Al Viro0a73dcc2006-06-05 08:15:59 -0400445 default:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400446 goto exit_free;
Al Viro0a73dcc2006-06-05 08:15:59 -0400447 case AUDIT_PID:
448 case AUDIT_UID:
449 case AUDIT_EUID:
450 case AUDIT_SUID:
451 case AUDIT_FSUID:
452 case AUDIT_GID:
453 case AUDIT_EGID:
454 case AUDIT_SGID:
455 case AUDIT_FSGID:
456 case AUDIT_LOGINUID:
457 case AUDIT_PERS:
Al Viro0a73dcc2006-06-05 08:15:59 -0400458 case AUDIT_MSGTYPE:
Steve Grubb3b33ac32006-08-26 14:06:20 -0400459 case AUDIT_PPID:
Al Viro0a73dcc2006-06-05 08:15:59 -0400460 case AUDIT_DEVMAJOR:
461 case AUDIT_DEVMINOR:
462 case AUDIT_EXIT:
463 case AUDIT_SUCCESS:
Eric Paris74f23452007-06-04 17:00:14 -0400464 /* bit ops are only useful on syscall args */
465 if (f->op == AUDIT_BIT_MASK ||
466 f->op == AUDIT_BIT_TEST) {
467 err = -EINVAL;
468 goto exit_free;
469 }
470 break;
Al Viro0a73dcc2006-06-05 08:15:59 -0400471 case AUDIT_ARG0:
472 case AUDIT_ARG1:
473 case AUDIT_ARG2:
474 case AUDIT_ARG3:
475 break;
Eric Paris4b8a3112006-09-28 17:46:21 -0400476 /* arch is only allowed to be = or != */
477 case AUDIT_ARCH:
478 if ((f->op != AUDIT_NOT_EQUAL) && (f->op != AUDIT_EQUAL)
479 && (f->op != AUDIT_NEGATE) && (f->op)) {
480 err = -EINVAL;
481 goto exit_free;
482 }
Amy Griffise54dc242007-03-29 18:01:04 -0400483 entry->rule.arch_f = f;
Eric Paris4b8a3112006-09-28 17:46:21 -0400484 break;
Al Viro55669bf2006-08-31 19:26:40 -0400485 case AUDIT_PERM:
486 if (f->val & ~15)
487 goto exit_free;
488 break;
Al Viro8b67dca2008-04-28 04:15:49 -0400489 case AUDIT_FILETYPE:
490 if ((f->val & ~S_IFMT) > S_IFMT)
491 goto exit_free;
492 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400493 case AUDIT_INODE:
494 err = audit_to_inode(&entry->rule, f);
495 if (err)
496 goto exit_free;
497 break;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600498 }
499
Amy Griffis93315ed2006-02-07 12:05:27 -0500500 entry->rule.vers_ops = (f->op & AUDIT_OPERATORS) ? 2 : 1;
Dustin Kirklandd9d9ec62006-02-16 13:40:01 -0600501
502 /* Support for legacy operators where
503 * AUDIT_NEGATE bit signifies != and otherwise assumes == */
Amy Griffis93315ed2006-02-07 12:05:27 -0500504 if (f->op & AUDIT_NEGATE)
Dustin Kirklandd9d9ec62006-02-16 13:40:01 -0600505 f->op = AUDIT_NOT_EQUAL;
506 else if (!f->op)
507 f->op = AUDIT_EQUAL;
508 else if (f->op == AUDIT_OPERATORS) {
509 err = -EINVAL;
510 goto exit_free;
511 }
Amy Griffis93315ed2006-02-07 12:05:27 -0500512 }
513
Harvey Harrison7719e432008-04-27 02:39:56 -0700514 ino_f = entry->rule.inode_f;
515 if (ino_f) {
516 switch(ino_f->op) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400517 case AUDIT_NOT_EQUAL:
518 entry->rule.inode_f = NULL;
519 case AUDIT_EQUAL:
520 break;
521 default:
Amy Griffis5422e012006-08-01 17:52:26 -0400522 err = -EINVAL;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400523 goto exit_free;
524 }
525 }
526
Amy Griffis93315ed2006-02-07 12:05:27 -0500527exit_nofree:
528 return entry;
529
530exit_free:
531 audit_free_rule(entry);
532 return ERR_PTR(err);
533}
534
535/* Translate struct audit_rule_data to kernel's rule respresentation. */
536static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
537 size_t datasz)
538{
539 int err = 0;
540 struct audit_entry *entry;
Harvey Harrison7719e432008-04-27 02:39:56 -0700541 struct audit_field *ino_f;
Amy Griffis93315ed2006-02-07 12:05:27 -0500542 void *bufp;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600543 size_t remain = datasz - sizeof(struct audit_rule_data);
Amy Griffis93315ed2006-02-07 12:05:27 -0500544 int i;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600545 char *str;
Amy Griffis93315ed2006-02-07 12:05:27 -0500546
547 entry = audit_to_entry_common((struct audit_rule *)data);
548 if (IS_ERR(entry))
549 goto exit_nofree;
550
551 bufp = data->buf;
552 entry->rule.vers_ops = 2;
553 for (i = 0; i < data->field_count; i++) {
554 struct audit_field *f = &entry->rule.fields[i];
555
556 err = -EINVAL;
557 if (!(data->fieldflags[i] & AUDIT_OPERATORS) ||
558 data->fieldflags[i] & ~AUDIT_OPERATORS)
559 goto exit_free;
560
561 f->op = data->fieldflags[i] & AUDIT_OPERATORS;
562 f->type = data->fields[i];
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600563 f->val = data->values[i];
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000564 f->lsm_str = NULL;
565 f->lsm_rule = NULL;
Amy Griffis93315ed2006-02-07 12:05:27 -0500566 switch(f->type) {
Al Viro0a73dcc2006-06-05 08:15:59 -0400567 case AUDIT_PID:
568 case AUDIT_UID:
569 case AUDIT_EUID:
570 case AUDIT_SUID:
571 case AUDIT_FSUID:
572 case AUDIT_GID:
573 case AUDIT_EGID:
574 case AUDIT_SGID:
575 case AUDIT_FSGID:
576 case AUDIT_LOGINUID:
577 case AUDIT_PERS:
Al Viro0a73dcc2006-06-05 08:15:59 -0400578 case AUDIT_MSGTYPE:
579 case AUDIT_PPID:
580 case AUDIT_DEVMAJOR:
581 case AUDIT_DEVMINOR:
582 case AUDIT_EXIT:
583 case AUDIT_SUCCESS:
584 case AUDIT_ARG0:
585 case AUDIT_ARG1:
586 case AUDIT_ARG2:
587 case AUDIT_ARG3:
588 break;
Amy Griffise54dc242007-03-29 18:01:04 -0400589 case AUDIT_ARCH:
590 entry->rule.arch_f = f;
591 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500592 case AUDIT_SUBJ_USER:
593 case AUDIT_SUBJ_ROLE:
594 case AUDIT_SUBJ_TYPE:
595 case AUDIT_SUBJ_SEN:
596 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500597 case AUDIT_OBJ_USER:
598 case AUDIT_OBJ_ROLE:
599 case AUDIT_OBJ_TYPE:
600 case AUDIT_OBJ_LEV_LOW:
601 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600602 str = audit_unpack_string(&bufp, &remain, f->val);
603 if (IS_ERR(str))
604 goto exit_free;
605 entry->rule.buflen += f->val;
606
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200607 err = security_audit_rule_init(f->type, f->op, str,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000608 (void **)&f->lsm_rule);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600609 /* Keep currently invalid fields around in case they
610 * become valid after a policy reload. */
611 if (err == -EINVAL) {
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200612 printk(KERN_WARNING "audit rule for LSM "
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600613 "\'%s\' is invalid\n", str);
614 err = 0;
615 }
616 if (err) {
617 kfree(str);
618 goto exit_free;
619 } else
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000620 f->lsm_str = str;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600621 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400622 case AUDIT_WATCH:
623 str = audit_unpack_string(&bufp, &remain, f->val);
624 if (IS_ERR(str))
625 goto exit_free;
626 entry->rule.buflen += f->val;
627
628 err = audit_to_watch(&entry->rule, str, f->val, f->op);
629 if (err) {
630 kfree(str);
631 goto exit_free;
632 }
633 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400634 case AUDIT_DIR:
635 str = audit_unpack_string(&bufp, &remain, f->val);
636 if (IS_ERR(str))
637 goto exit_free;
638 entry->rule.buflen += f->val;
639
640 err = audit_make_tree(&entry->rule, str, f->op);
641 kfree(str);
642 if (err)
643 goto exit_free;
644 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400645 case AUDIT_INODE:
646 err = audit_to_inode(&entry->rule, f);
647 if (err)
648 goto exit_free;
649 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400650 case AUDIT_FILTERKEY:
651 err = -EINVAL;
652 if (entry->rule.filterkey || f->val > AUDIT_MAX_KEY_LEN)
653 goto exit_free;
654 str = audit_unpack_string(&bufp, &remain, f->val);
655 if (IS_ERR(str))
656 goto exit_free;
657 entry->rule.buflen += f->val;
658 entry->rule.filterkey = str;
659 break;
Al Viro55669bf2006-08-31 19:26:40 -0400660 case AUDIT_PERM:
661 if (f->val & ~15)
662 goto exit_free;
663 break;
Al Viro8b67dca2008-04-28 04:15:49 -0400664 case AUDIT_FILETYPE:
665 if ((f->val & ~S_IFMT) > S_IFMT)
666 goto exit_free;
667 break;
Al Viro0a73dcc2006-06-05 08:15:59 -0400668 default:
669 goto exit_free;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400670 }
671 }
672
Harvey Harrison7719e432008-04-27 02:39:56 -0700673 ino_f = entry->rule.inode_f;
674 if (ino_f) {
675 switch(ino_f->op) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400676 case AUDIT_NOT_EQUAL:
677 entry->rule.inode_f = NULL;
678 case AUDIT_EQUAL:
679 break;
680 default:
Amy Griffis5422e012006-08-01 17:52:26 -0400681 err = -EINVAL;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400682 goto exit_free;
Amy Griffis93315ed2006-02-07 12:05:27 -0500683 }
684 }
685
686exit_nofree:
687 return entry;
688
689exit_free:
690 audit_free_rule(entry);
691 return ERR_PTR(err);
692}
693
694/* Pack a filter field's string representation into data block. */
Al Viro74c3cbe2007-07-22 08:04:18 -0400695static inline size_t audit_pack_string(void **bufp, const char *str)
Amy Griffis93315ed2006-02-07 12:05:27 -0500696{
697 size_t len = strlen(str);
698
699 memcpy(*bufp, str, len);
700 *bufp += len;
701
702 return len;
703}
704
705/* Translate kernel rule respresentation to struct audit_rule.
706 * Exists for backward compatibility with userspace. */
707static struct audit_rule *audit_krule_to_rule(struct audit_krule *krule)
708{
709 struct audit_rule *rule;
710 int i;
711
Burman Yan4668edc2006-12-06 20:38:51 -0800712 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
Amy Griffis93315ed2006-02-07 12:05:27 -0500713 if (unlikely(!rule))
Amy Griffis0a3b4832006-05-02 15:06:01 -0400714 return NULL;
Amy Griffis93315ed2006-02-07 12:05:27 -0500715
716 rule->flags = krule->flags | krule->listnr;
717 rule->action = krule->action;
718 rule->field_count = krule->field_count;
719 for (i = 0; i < rule->field_count; i++) {
720 rule->values[i] = krule->fields[i].val;
721 rule->fields[i] = krule->fields[i].type;
722
723 if (krule->vers_ops == 1) {
724 if (krule->fields[i].op & AUDIT_NOT_EQUAL)
725 rule->fields[i] |= AUDIT_NEGATE;
726 } else {
727 rule->fields[i] |= krule->fields[i].op;
728 }
729 }
730 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) rule->mask[i] = krule->mask[i];
731
732 return rule;
733}
734
735/* Translate kernel rule respresentation to struct audit_rule_data. */
736static struct audit_rule_data *audit_krule_to_data(struct audit_krule *krule)
737{
738 struct audit_rule_data *data;
739 void *bufp;
740 int i;
741
742 data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL);
743 if (unlikely(!data))
Amy Griffis0a3b4832006-05-02 15:06:01 -0400744 return NULL;
Amy Griffis93315ed2006-02-07 12:05:27 -0500745 memset(data, 0, sizeof(*data));
746
747 data->flags = krule->flags | krule->listnr;
748 data->action = krule->action;
749 data->field_count = krule->field_count;
750 bufp = data->buf;
751 for (i = 0; i < data->field_count; i++) {
752 struct audit_field *f = &krule->fields[i];
753
754 data->fields[i] = f->type;
755 data->fieldflags[i] = f->op;
756 switch(f->type) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500757 case AUDIT_SUBJ_USER:
758 case AUDIT_SUBJ_ROLE:
759 case AUDIT_SUBJ_TYPE:
760 case AUDIT_SUBJ_SEN:
761 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500762 case AUDIT_OBJ_USER:
763 case AUDIT_OBJ_ROLE:
764 case AUDIT_OBJ_TYPE:
765 case AUDIT_OBJ_LEV_LOW:
766 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600767 data->buflen += data->values[i] =
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000768 audit_pack_string(&bufp, f->lsm_str);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600769 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400770 case AUDIT_WATCH:
771 data->buflen += data->values[i] =
772 audit_pack_string(&bufp, krule->watch->path);
773 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400774 case AUDIT_DIR:
775 data->buflen += data->values[i] =
776 audit_pack_string(&bufp,
777 audit_tree_path(krule->tree));
778 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400779 case AUDIT_FILTERKEY:
780 data->buflen += data->values[i] =
781 audit_pack_string(&bufp, krule->filterkey);
782 break;
Amy Griffis93315ed2006-02-07 12:05:27 -0500783 default:
784 data->values[i] = f->val;
785 }
786 }
787 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) data->mask[i] = krule->mask[i];
788
789 return data;
790}
791
792/* Compare two rules in kernel format. Considered success if rules
793 * don't match. */
794static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b)
David Woodhousefe7752b2005-12-15 18:33:52 +0000795{
796 int i;
797
Amy Griffis93315ed2006-02-07 12:05:27 -0500798 if (a->flags != b->flags ||
799 a->listnr != b->listnr ||
800 a->action != b->action ||
801 a->field_count != b->field_count)
David Woodhousefe7752b2005-12-15 18:33:52 +0000802 return 1;
803
804 for (i = 0; i < a->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500805 if (a->fields[i].type != b->fields[i].type ||
806 a->fields[i].op != b->fields[i].op)
David Woodhousefe7752b2005-12-15 18:33:52 +0000807 return 1;
Amy Griffis93315ed2006-02-07 12:05:27 -0500808
809 switch(a->fields[i].type) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500810 case AUDIT_SUBJ_USER:
811 case AUDIT_SUBJ_ROLE:
812 case AUDIT_SUBJ_TYPE:
813 case AUDIT_SUBJ_SEN:
814 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500815 case AUDIT_OBJ_USER:
816 case AUDIT_OBJ_ROLE:
817 case AUDIT_OBJ_TYPE:
818 case AUDIT_OBJ_LEV_LOW:
819 case AUDIT_OBJ_LEV_HIGH:
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000820 if (strcmp(a->fields[i].lsm_str, b->fields[i].lsm_str))
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600821 return 1;
822 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400823 case AUDIT_WATCH:
824 if (strcmp(a->watch->path, b->watch->path))
825 return 1;
826 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400827 case AUDIT_DIR:
828 if (strcmp(audit_tree_path(a->tree),
829 audit_tree_path(b->tree)))
830 return 1;
831 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400832 case AUDIT_FILTERKEY:
833 /* both filterkeys exist based on above type compare */
834 if (strcmp(a->filterkey, b->filterkey))
835 return 1;
836 break;
Amy Griffis93315ed2006-02-07 12:05:27 -0500837 default:
838 if (a->fields[i].val != b->fields[i].val)
839 return 1;
840 }
David Woodhousefe7752b2005-12-15 18:33:52 +0000841 }
842
843 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
844 if (a->mask[i] != b->mask[i])
845 return 1;
846
847 return 0;
848}
849
Amy Griffisf368c07d2006-04-07 16:55:56 -0400850/* Duplicate the given audit watch. The new watch's rules list is initialized
851 * to an empty list and wlist is undefined. */
852static struct audit_watch *audit_dupe_watch(struct audit_watch *old)
853{
854 char *path;
855 struct audit_watch *new;
856
857 path = kstrdup(old->path, GFP_KERNEL);
858 if (unlikely(!path))
859 return ERR_PTR(-ENOMEM);
860
861 new = audit_init_watch(path);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -0700862 if (IS_ERR(new)) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400863 kfree(path);
864 goto out;
865 }
866
867 new->dev = old->dev;
868 new->ino = old->ino;
869 get_inotify_watch(&old->parent->wdata);
870 new->parent = old->parent;
871
872out:
873 return new;
874}
875
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000876/* Duplicate LSM field information. The lsm_rule is opaque, so must be
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600877 * re-initialized. */
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200878static inline int audit_dupe_lsm_field(struct audit_field *df,
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600879 struct audit_field *sf)
880{
881 int ret = 0;
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000882 char *lsm_str;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600883
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000884 /* our own copy of lsm_str */
885 lsm_str = kstrdup(sf->lsm_str, GFP_KERNEL);
886 if (unlikely(!lsm_str))
Akinobu Mita3e1fbd12006-12-22 01:10:02 -0800887 return -ENOMEM;
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000888 df->lsm_str = lsm_str;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600889
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000890 /* our own (refreshed) copy of lsm_rule */
891 ret = security_audit_rule_init(df->type, df->op, df->lsm_str,
892 (void **)&df->lsm_rule);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600893 /* Keep currently invalid fields around in case they
894 * become valid after a policy reload. */
895 if (ret == -EINVAL) {
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200896 printk(KERN_WARNING "audit rule for LSM \'%s\' is "
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000897 "invalid\n", df->lsm_str);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600898 ret = 0;
899 }
900
901 return ret;
902}
903
904/* Duplicate an audit rule. This will be a deep copy with the exception
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200905 * of the watch - that pointer is carried over. The LSM specific fields
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600906 * will be updated in the copy. The point is to be able to replace the old
Amy Griffisf368c07d2006-04-07 16:55:56 -0400907 * rule with the new rule in the filterlist, then free the old rule.
908 * The rlist element is undefined; list manipulations are handled apart from
909 * the initial copy. */
910static struct audit_entry *audit_dupe_rule(struct audit_krule *old,
911 struct audit_watch *watch)
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600912{
913 u32 fcount = old->field_count;
914 struct audit_entry *entry;
915 struct audit_krule *new;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400916 char *fk;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600917 int i, err = 0;
918
919 entry = audit_init_entry(fcount);
920 if (unlikely(!entry))
921 return ERR_PTR(-ENOMEM);
922
923 new = &entry->rule;
924 new->vers_ops = old->vers_ops;
925 new->flags = old->flags;
926 new->listnr = old->listnr;
927 new->action = old->action;
928 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
929 new->mask[i] = old->mask[i];
Al Viro0590b932008-12-14 23:45:27 -0500930 new->prio = old->prio;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600931 new->buflen = old->buflen;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400932 new->inode_f = old->inode_f;
933 new->watch = NULL;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600934 new->field_count = old->field_count;
Al Viro74c3cbe2007-07-22 08:04:18 -0400935 /*
936 * note that we are OK with not refcounting here; audit_match_tree()
937 * never dereferences tree and we can't get false positives there
938 * since we'd have to have rule gone from the list *and* removed
939 * before the chunks found by lookup had been allocated, i.e. before
940 * the beginning of list scan.
941 */
942 new->tree = old->tree;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600943 memcpy(new->fields, old->fields, sizeof(struct audit_field) * fcount);
944
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000945 /* deep copy this information, updating the lsm_rule fields, because
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600946 * the originals will all be freed when the old rule is freed. */
947 for (i = 0; i < fcount; i++) {
948 switch (new->fields[i].type) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500949 case AUDIT_SUBJ_USER:
950 case AUDIT_SUBJ_ROLE:
951 case AUDIT_SUBJ_TYPE:
952 case AUDIT_SUBJ_SEN:
953 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500954 case AUDIT_OBJ_USER:
955 case AUDIT_OBJ_ROLE:
956 case AUDIT_OBJ_TYPE:
957 case AUDIT_OBJ_LEV_LOW:
958 case AUDIT_OBJ_LEV_HIGH:
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200959 err = audit_dupe_lsm_field(&new->fields[i],
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600960 &old->fields[i]);
Amy Griffis5adc8a62006-06-14 18:45:21 -0400961 break;
962 case AUDIT_FILTERKEY:
963 fk = kstrdup(old->filterkey, GFP_KERNEL);
964 if (unlikely(!fk))
965 err = -ENOMEM;
966 else
967 new->filterkey = fk;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600968 }
969 if (err) {
970 audit_free_rule(entry);
971 return ERR_PTR(err);
972 }
973 }
974
Amy Griffisf368c07d2006-04-07 16:55:56 -0400975 if (watch) {
976 audit_get_watch(watch);
977 new->watch = watch;
978 }
979
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600980 return entry;
981}
982
Amy Griffisf368c07d2006-04-07 16:55:56 -0400983/* Update inode info in audit rules based on filesystem event. */
984static void audit_update_watch(struct audit_parent *parent,
985 const char *dname, dev_t dev,
986 unsigned long ino, unsigned invalidating)
David Woodhousefe7752b2005-12-15 18:33:52 +0000987{
Amy Griffisf368c07d2006-04-07 16:55:56 -0400988 struct audit_watch *owatch, *nwatch, *nextw;
989 struct audit_krule *r, *nextr;
990 struct audit_entry *oentry, *nentry;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400991
992 mutex_lock(&audit_filter_mutex);
993 list_for_each_entry_safe(owatch, nextw, &parent->watches, wlist) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400994 if (audit_compare_dname_path(dname, owatch->path, NULL))
Amy Griffisf368c07d2006-04-07 16:55:56 -0400995 continue;
996
997 /* If the update involves invalidating rules, do the inode-based
998 * filtering now, so we don't omit records. */
Al Viro0590b932008-12-14 23:45:27 -0500999 if (invalidating && current->audit_context)
1000 audit_filter_inodes(current, current->audit_context);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001001
1002 nwatch = audit_dupe_watch(owatch);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07001003 if (IS_ERR(nwatch)) {
Amy Griffisf368c07d2006-04-07 16:55:56 -04001004 mutex_unlock(&audit_filter_mutex);
1005 audit_panic("error updating watch, skipping");
1006 return;
1007 }
1008 nwatch->dev = dev;
1009 nwatch->ino = ino;
1010
1011 list_for_each_entry_safe(r, nextr, &owatch->rules, rlist) {
1012
1013 oentry = container_of(r, struct audit_entry, rule);
1014 list_del(&oentry->rule.rlist);
1015 list_del_rcu(&oentry->list);
1016
1017 nentry = audit_dupe_rule(&oentry->rule, nwatch);
Al Viroe45aa212008-12-15 01:17:50 -05001018 if (IS_ERR(nentry)) {
1019 list_del(&oentry->rule.list);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001020 audit_panic("error updating watch, removing");
Al Viroe45aa212008-12-15 01:17:50 -05001021 } else {
Amy Griffisf368c07d2006-04-07 16:55:56 -04001022 int h = audit_hash_ino((u32)ino);
1023 list_add(&nentry->rule.rlist, &nwatch->rules);
1024 list_add_rcu(&nentry->list, &audit_inode_hash[h]);
Al Viroe45aa212008-12-15 01:17:50 -05001025 list_replace(&oentry->rule.list,
1026 &nentry->rule.list);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001027 }
1028
1029 call_rcu(&oentry->rcu, audit_free_rule_rcu);
1030 }
1031
Eric Paris1a6b9f22008-01-07 17:09:31 -05001032 if (audit_enabled) {
1033 struct audit_buffer *ab;
1034 ab = audit_log_start(NULL, GFP_KERNEL,
1035 AUDIT_CONFIG_CHANGE);
zhangxiliang036bbf72008-08-01 09:47:01 +08001036 audit_log_format(ab, "auid=%u ses=%u",
1037 audit_get_loginuid(current),
1038 audit_get_sessionid(current));
Eric Paris1a6b9f22008-01-07 17:09:31 -05001039 audit_log_format(ab,
zhangxiliang036bbf72008-08-01 09:47:01 +08001040 " op=updated rules specifying path=");
Eric Paris1a6b9f22008-01-07 17:09:31 -05001041 audit_log_untrustedstring(ab, owatch->path);
1042 audit_log_format(ab, " with dev=%u ino=%lu\n",
1043 dev, ino);
1044 audit_log_format(ab, " list=%d res=1", r->listnr);
1045 audit_log_end(ab);
1046 }
Amy Griffisf368c07d2006-04-07 16:55:56 -04001047 audit_remove_watch(owatch);
1048 goto add_watch_to_parent; /* event applies to a single watch */
1049 }
1050 mutex_unlock(&audit_filter_mutex);
1051 return;
1052
1053add_watch_to_parent:
1054 list_add(&nwatch->wlist, &parent->watches);
1055 mutex_unlock(&audit_filter_mutex);
1056 return;
1057}
1058
1059/* Remove all watches & rules associated with a parent that is going away. */
1060static void audit_remove_parent_watches(struct audit_parent *parent)
1061{
1062 struct audit_watch *w, *nextw;
1063 struct audit_krule *r, *nextr;
Amy Griffis93315ed2006-02-07 12:05:27 -05001064 struct audit_entry *e;
David Woodhousefe7752b2005-12-15 18:33:52 +00001065
Amy Griffisf368c07d2006-04-07 16:55:56 -04001066 mutex_lock(&audit_filter_mutex);
1067 parent->flags |= AUDIT_PARENT_INVALID;
1068 list_for_each_entry_safe(w, nextw, &parent->watches, wlist) {
1069 list_for_each_entry_safe(r, nextr, &w->rules, rlist) {
1070 e = container_of(r, struct audit_entry, rule);
Eric Paris1a6b9f22008-01-07 17:09:31 -05001071 if (audit_enabled) {
1072 struct audit_buffer *ab;
1073 ab = audit_log_start(NULL, GFP_KERNEL,
1074 AUDIT_CONFIG_CHANGE);
zhangxiliang036bbf72008-08-01 09:47:01 +08001075 audit_log_format(ab, "auid=%u ses=%u",
1076 audit_get_loginuid(current),
1077 audit_get_sessionid(current));
1078 audit_log_format(ab, " op=remove rule path=");
Eric Paris1a6b9f22008-01-07 17:09:31 -05001079 audit_log_untrustedstring(ab, w->path);
1080 if (r->filterkey) {
1081 audit_log_format(ab, " key=");
1082 audit_log_untrustedstring(ab,
1083 r->filterkey);
1084 } else
1085 audit_log_format(ab, " key=(null)");
1086 audit_log_format(ab, " list=%d res=1",
1087 r->listnr);
1088 audit_log_end(ab);
1089 }
Amy Griffisf368c07d2006-04-07 16:55:56 -04001090 list_del(&r->rlist);
Al Viroe45aa212008-12-15 01:17:50 -05001091 list_del(&r->list);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001092 list_del_rcu(&e->list);
1093 call_rcu(&e->rcu, audit_free_rule_rcu);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001094 }
1095 audit_remove_watch(w);
1096 }
1097 mutex_unlock(&audit_filter_mutex);
1098}
1099
1100/* Unregister inotify watches for parents on in_list.
1101 * Generates an IN_IGNORED event. */
1102static void audit_inotify_unregister(struct list_head *in_list)
1103{
1104 struct audit_parent *p, *n;
1105
1106 list_for_each_entry_safe(p, n, in_list, ilist) {
1107 list_del(&p->ilist);
1108 inotify_rm_watch(audit_ih, &p->wdata);
Al Viro8f7b0ba2008-11-15 01:15:43 +00001109 /* the unpin matching the pin in audit_do_del_rule() */
1110 unpin_inotify_watch(&p->wdata);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001111 }
1112}
1113
1114/* Find an existing audit rule.
1115 * Caller must hold audit_filter_mutex to prevent stale rule data. */
1116static struct audit_entry *audit_find_rule(struct audit_entry *entry,
1117 struct list_head *list)
1118{
1119 struct audit_entry *e, *found = NULL;
1120 int h;
1121
1122 if (entry->rule.watch) {
1123 /* we don't know the inode number, so must walk entire hash */
1124 for (h = 0; h < AUDIT_INODE_BUCKETS; h++) {
1125 list = &audit_inode_hash[h];
1126 list_for_each_entry(e, list, list)
1127 if (!audit_compare_rule(&entry->rule, &e->rule)) {
1128 found = e;
1129 goto out;
1130 }
1131 }
1132 goto out;
1133 }
1134
1135 list_for_each_entry(e, list, list)
1136 if (!audit_compare_rule(&entry->rule, &e->rule)) {
1137 found = e;
1138 goto out;
1139 }
1140
1141out:
1142 return found;
1143}
1144
1145/* Get path information necessary for adding watches. */
1146static int audit_get_nd(char *path, struct nameidata **ndp,
1147 struct nameidata **ndw)
1148{
1149 struct nameidata *ndparent, *ndwatch;
1150 int err;
1151
1152 ndparent = kmalloc(sizeof(*ndparent), GFP_KERNEL);
1153 if (unlikely(!ndparent))
1154 return -ENOMEM;
1155
1156 ndwatch = kmalloc(sizeof(*ndwatch), GFP_KERNEL);
1157 if (unlikely(!ndwatch)) {
1158 kfree(ndparent);
1159 return -ENOMEM;
1160 }
1161
1162 err = path_lookup(path, LOOKUP_PARENT, ndparent);
1163 if (err) {
1164 kfree(ndparent);
1165 kfree(ndwatch);
1166 return err;
1167 }
1168
1169 err = path_lookup(path, 0, ndwatch);
1170 if (err) {
1171 kfree(ndwatch);
1172 ndwatch = NULL;
1173 }
1174
1175 *ndp = ndparent;
1176 *ndw = ndwatch;
1177
1178 return 0;
1179}
1180
1181/* Release resources used for watch path information. */
1182static void audit_put_nd(struct nameidata *ndp, struct nameidata *ndw)
1183{
1184 if (ndp) {
Jan Blunck1d957f92008-02-14 19:34:35 -08001185 path_put(&ndp->path);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001186 kfree(ndp);
1187 }
1188 if (ndw) {
Jan Blunck1d957f92008-02-14 19:34:35 -08001189 path_put(&ndw->path);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001190 kfree(ndw);
1191 }
1192}
1193
1194/* Associate the given rule with an existing parent inotify_watch.
1195 * Caller must hold audit_filter_mutex. */
1196static void audit_add_to_parent(struct audit_krule *krule,
1197 struct audit_parent *parent)
1198{
1199 struct audit_watch *w, *watch = krule->watch;
1200 int watch_found = 0;
1201
1202 list_for_each_entry(w, &parent->watches, wlist) {
1203 if (strcmp(watch->path, w->path))
1204 continue;
1205
1206 watch_found = 1;
1207
1208 /* put krule's and initial refs to temporary watch */
1209 audit_put_watch(watch);
1210 audit_put_watch(watch);
1211
1212 audit_get_watch(w);
1213 krule->watch = watch = w;
1214 break;
1215 }
1216
1217 if (!watch_found) {
1218 get_inotify_watch(&parent->wdata);
1219 watch->parent = parent;
1220
1221 list_add(&watch->wlist, &parent->watches);
1222 }
1223 list_add(&krule->rlist, &watch->rules);
1224}
1225
1226/* Find a matching watch entry, or add this one.
1227 * Caller must hold audit_filter_mutex. */
1228static int audit_add_watch(struct audit_krule *krule, struct nameidata *ndp,
1229 struct nameidata *ndw)
1230{
1231 struct audit_watch *watch = krule->watch;
1232 struct inotify_watch *i_watch;
1233 struct audit_parent *parent;
1234 int ret = 0;
1235
1236 /* update watch filter fields */
1237 if (ndw) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001238 watch->dev = ndw->path.dentry->d_inode->i_sb->s_dev;
1239 watch->ino = ndw->path.dentry->d_inode->i_ino;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001240 }
1241
1242 /* The audit_filter_mutex must not be held during inotify calls because
1243 * we hold it during inotify event callback processing. If an existing
1244 * inotify watch is found, inotify_find_watch() grabs a reference before
1245 * returning.
1246 */
1247 mutex_unlock(&audit_filter_mutex);
1248
Jan Blunck4ac91372008-02-14 19:34:32 -08001249 if (inotify_find_watch(audit_ih, ndp->path.dentry->d_inode,
1250 &i_watch) < 0) {
Amy Griffisf368c07d2006-04-07 16:55:56 -04001251 parent = audit_init_parent(ndp);
1252 if (IS_ERR(parent)) {
1253 /* caller expects mutex locked */
1254 mutex_lock(&audit_filter_mutex);
1255 return PTR_ERR(parent);
1256 }
1257 } else
1258 parent = container_of(i_watch, struct audit_parent, wdata);
1259
1260 mutex_lock(&audit_filter_mutex);
1261
1262 /* parent was moved before we took audit_filter_mutex */
1263 if (parent->flags & AUDIT_PARENT_INVALID)
1264 ret = -ENOENT;
1265 else
1266 audit_add_to_parent(krule, parent);
1267
1268 /* match get in audit_init_parent or inotify_find_watch */
1269 put_inotify_watch(&parent->wdata);
1270 return ret;
1271}
1272
Al Viro0590b932008-12-14 23:45:27 -05001273static u64 prio_low = ~0ULL/2;
1274static u64 prio_high = ~0ULL/2 - 1;
1275
Amy Griffisf368c07d2006-04-07 16:55:56 -04001276/* Add rule to given filterlist if not a duplicate. */
1277static inline int audit_add_rule(struct audit_entry *entry,
1278 struct list_head *list)
1279{
1280 struct audit_entry *e;
1281 struct audit_field *inode_f = entry->rule.inode_f;
1282 struct audit_watch *watch = entry->rule.watch;
Al Viro74c3cbe2007-07-22 08:04:18 -04001283 struct audit_tree *tree = entry->rule.tree;
Jeff Garzik6f686d32007-07-16 21:25:01 -04001284 struct nameidata *ndp = NULL, *ndw = NULL;
1285 int h, err;
Al Viro471a5c72006-07-10 08:29:24 -04001286#ifdef CONFIG_AUDITSYSCALL
1287 int dont_count = 0;
1288
1289 /* If either of these, don't count towards total */
1290 if (entry->rule.listnr == AUDIT_FILTER_USER ||
1291 entry->rule.listnr == AUDIT_FILTER_TYPE)
1292 dont_count = 1;
1293#endif
Amy Griffisf368c07d2006-04-07 16:55:56 -04001294
1295 if (inode_f) {
1296 h = audit_hash_ino(inode_f->val);
1297 list = &audit_inode_hash[h];
1298 }
1299
1300 mutex_lock(&audit_filter_mutex);
1301 e = audit_find_rule(entry, list);
1302 mutex_unlock(&audit_filter_mutex);
1303 if (e) {
1304 err = -EEXIST;
Al Viro74c3cbe2007-07-22 08:04:18 -04001305 /* normally audit_add_tree_rule() will free it on failure */
1306 if (tree)
1307 audit_put_tree(tree);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001308 goto error;
1309 }
1310
1311 /* Avoid calling path_lookup under audit_filter_mutex. */
1312 if (watch) {
1313 err = audit_get_nd(watch->path, &ndp, &ndw);
1314 if (err)
1315 goto error;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001316 }
1317
1318 mutex_lock(&audit_filter_mutex);
1319 if (watch) {
1320 /* audit_filter_mutex is dropped and re-taken during this call */
1321 err = audit_add_watch(&entry->rule, ndp, ndw);
1322 if (err) {
1323 mutex_unlock(&audit_filter_mutex);
1324 goto error;
1325 }
1326 h = audit_hash_ino((u32)watch->ino);
1327 list = &audit_inode_hash[h];
David Woodhousefe7752b2005-12-15 18:33:52 +00001328 }
Al Viro74c3cbe2007-07-22 08:04:18 -04001329 if (tree) {
1330 err = audit_add_tree_rule(&entry->rule);
1331 if (err) {
1332 mutex_unlock(&audit_filter_mutex);
1333 goto error;
1334 }
1335 }
David Woodhousefe7752b2005-12-15 18:33:52 +00001336
Al Viro0590b932008-12-14 23:45:27 -05001337 entry->rule.prio = ~0ULL;
1338 if (entry->rule.listnr == AUDIT_FILTER_EXIT) {
1339 if (entry->rule.flags & AUDIT_FILTER_PREPEND)
1340 entry->rule.prio = ++prio_high;
1341 else
1342 entry->rule.prio = --prio_low;
1343 }
1344
David Woodhousefe7752b2005-12-15 18:33:52 +00001345 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
Al Viroe45aa212008-12-15 01:17:50 -05001346 list_add(&entry->rule.list,
1347 &audit_rules_list[entry->rule.listnr]);
David Woodhousefe7752b2005-12-15 18:33:52 +00001348 list_add_rcu(&entry->list, list);
Amy Griffis6a2bcee2006-06-02 13:16:01 -04001349 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
David Woodhousefe7752b2005-12-15 18:33:52 +00001350 } else {
Al Viroe45aa212008-12-15 01:17:50 -05001351 list_add_tail(&entry->rule.list,
1352 &audit_rules_list[entry->rule.listnr]);
David Woodhousefe7752b2005-12-15 18:33:52 +00001353 list_add_tail_rcu(&entry->list, list);
1354 }
Al Viro471a5c72006-07-10 08:29:24 -04001355#ifdef CONFIG_AUDITSYSCALL
1356 if (!dont_count)
1357 audit_n_rules++;
Amy Griffise54dc242007-03-29 18:01:04 -04001358
1359 if (!audit_match_signal(entry))
1360 audit_signals++;
Al Viro471a5c72006-07-10 08:29:24 -04001361#endif
Amy Griffisf368c07d2006-04-07 16:55:56 -04001362 mutex_unlock(&audit_filter_mutex);
David Woodhousefe7752b2005-12-15 18:33:52 +00001363
Jeff Garzik6f686d32007-07-16 21:25:01 -04001364 audit_put_nd(ndp, ndw); /* NULL args OK */
Amy Griffisf368c07d2006-04-07 16:55:56 -04001365 return 0;
1366
1367error:
Jeff Garzik6f686d32007-07-16 21:25:01 -04001368 audit_put_nd(ndp, ndw); /* NULL args OK */
Amy Griffisf368c07d2006-04-07 16:55:56 -04001369 if (watch)
1370 audit_put_watch(watch); /* tmp watch, matches initial get */
1371 return err;
David Woodhousefe7752b2005-12-15 18:33:52 +00001372}
1373
Amy Griffisf368c07d2006-04-07 16:55:56 -04001374/* Remove an existing rule from filterlist. */
Amy Griffis93315ed2006-02-07 12:05:27 -05001375static inline int audit_del_rule(struct audit_entry *entry,
David Woodhousefe7752b2005-12-15 18:33:52 +00001376 struct list_head *list)
1377{
1378 struct audit_entry *e;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001379 struct audit_field *inode_f = entry->rule.inode_f;
1380 struct audit_watch *watch, *tmp_watch = entry->rule.watch;
Al Viro74c3cbe2007-07-22 08:04:18 -04001381 struct audit_tree *tree = entry->rule.tree;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001382 LIST_HEAD(inotify_list);
1383 int h, ret = 0;
Al Viro471a5c72006-07-10 08:29:24 -04001384#ifdef CONFIG_AUDITSYSCALL
1385 int dont_count = 0;
1386
1387 /* If either of these, don't count towards total */
1388 if (entry->rule.listnr == AUDIT_FILTER_USER ||
1389 entry->rule.listnr == AUDIT_FILTER_TYPE)
1390 dont_count = 1;
1391#endif
David Woodhousefe7752b2005-12-15 18:33:52 +00001392
Amy Griffisf368c07d2006-04-07 16:55:56 -04001393 if (inode_f) {
1394 h = audit_hash_ino(inode_f->val);
1395 list = &audit_inode_hash[h];
1396 }
1397
1398 mutex_lock(&audit_filter_mutex);
1399 e = audit_find_rule(entry, list);
1400 if (!e) {
1401 mutex_unlock(&audit_filter_mutex);
1402 ret = -ENOENT;
1403 goto out;
1404 }
1405
1406 watch = e->rule.watch;
1407 if (watch) {
1408 struct audit_parent *parent = watch->parent;
1409
1410 list_del(&e->rule.rlist);
1411
1412 if (list_empty(&watch->rules)) {
1413 audit_remove_watch(watch);
1414
1415 if (list_empty(&parent->watches)) {
1416 /* Put parent on the inotify un-registration
1417 * list. Grab a reference before releasing
1418 * audit_filter_mutex, to be released in
Al Viro8f7b0ba2008-11-15 01:15:43 +00001419 * audit_inotify_unregister().
1420 * If filesystem is going away, just leave
1421 * the sucker alone, eviction will take
1422 * care of it.
1423 */
1424 if (pin_inotify_watch(&parent->wdata))
1425 list_add(&parent->ilist, &inotify_list);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001426 }
David Woodhousefe7752b2005-12-15 18:33:52 +00001427 }
1428 }
Amy Griffisf368c07d2006-04-07 16:55:56 -04001429
Al Viro74c3cbe2007-07-22 08:04:18 -04001430 if (e->rule.tree)
1431 audit_remove_tree_rule(&e->rule);
1432
Amy Griffisf368c07d2006-04-07 16:55:56 -04001433 list_del_rcu(&e->list);
Al Viroe45aa212008-12-15 01:17:50 -05001434 list_del(&e->rule.list);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001435 call_rcu(&e->rcu, audit_free_rule_rcu);
1436
Al Viro471a5c72006-07-10 08:29:24 -04001437#ifdef CONFIG_AUDITSYSCALL
1438 if (!dont_count)
1439 audit_n_rules--;
Amy Griffise54dc242007-03-29 18:01:04 -04001440
1441 if (!audit_match_signal(entry))
1442 audit_signals--;
Al Viro471a5c72006-07-10 08:29:24 -04001443#endif
Amy Griffisf368c07d2006-04-07 16:55:56 -04001444 mutex_unlock(&audit_filter_mutex);
1445
1446 if (!list_empty(&inotify_list))
1447 audit_inotify_unregister(&inotify_list);
1448
1449out:
1450 if (tmp_watch)
1451 audit_put_watch(tmp_watch); /* match initial get */
Al Viro74c3cbe2007-07-22 08:04:18 -04001452 if (tree)
1453 audit_put_tree(tree); /* that's the temporary one */
Amy Griffisf368c07d2006-04-07 16:55:56 -04001454
1455 return ret;
David Woodhousefe7752b2005-12-15 18:33:52 +00001456}
1457
Amy Griffis93315ed2006-02-07 12:05:27 -05001458/* List rules using struct audit_rule. Exists for backward
1459 * compatibility with userspace. */
Al Viro9044e6b2006-05-22 01:09:24 -04001460static void audit_list(int pid, int seq, struct sk_buff_head *q)
David Woodhousefe7752b2005-12-15 18:33:52 +00001461{
Al Viro9044e6b2006-05-22 01:09:24 -04001462 struct sk_buff *skb;
Al Viroe45aa212008-12-15 01:17:50 -05001463 struct audit_krule *r;
David Woodhousefe7752b2005-12-15 18:33:52 +00001464 int i;
1465
Amy Griffisf368c07d2006-04-07 16:55:56 -04001466 /* This is a blocking read, so use audit_filter_mutex instead of rcu
1467 * iterator to sync with list writers. */
David Woodhousefe7752b2005-12-15 18:33:52 +00001468 for (i=0; i<AUDIT_NR_FILTERS; i++) {
Al Viroe45aa212008-12-15 01:17:50 -05001469 list_for_each_entry(r, &audit_rules_list[i], list) {
Amy Griffis93315ed2006-02-07 12:05:27 -05001470 struct audit_rule *rule;
1471
Al Viroe45aa212008-12-15 01:17:50 -05001472 rule = audit_krule_to_rule(r);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001473 if (unlikely(!rule))
1474 break;
1475 skb = audit_make_reply(pid, seq, AUDIT_LIST, 0, 1,
1476 rule, sizeof(*rule));
1477 if (skb)
1478 skb_queue_tail(q, skb);
1479 kfree(rule);
1480 }
1481 }
Al Viro9044e6b2006-05-22 01:09:24 -04001482 skb = audit_make_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
1483 if (skb)
1484 skb_queue_tail(q, skb);
David Woodhousefe7752b2005-12-15 18:33:52 +00001485}
1486
Amy Griffis93315ed2006-02-07 12:05:27 -05001487/* List rules using struct audit_rule_data. */
Al Viro9044e6b2006-05-22 01:09:24 -04001488static void audit_list_rules(int pid, int seq, struct sk_buff_head *q)
Amy Griffis93315ed2006-02-07 12:05:27 -05001489{
Al Viro9044e6b2006-05-22 01:09:24 -04001490 struct sk_buff *skb;
Al Viroe45aa212008-12-15 01:17:50 -05001491 struct audit_krule *r;
Amy Griffis93315ed2006-02-07 12:05:27 -05001492 int i;
1493
Amy Griffisf368c07d2006-04-07 16:55:56 -04001494 /* This is a blocking read, so use audit_filter_mutex instead of rcu
1495 * iterator to sync with list writers. */
Amy Griffis93315ed2006-02-07 12:05:27 -05001496 for (i=0; i<AUDIT_NR_FILTERS; i++) {
Al Viroe45aa212008-12-15 01:17:50 -05001497 list_for_each_entry(r, &audit_rules_list[i], list) {
Amy Griffis93315ed2006-02-07 12:05:27 -05001498 struct audit_rule_data *data;
1499
Al Viroe45aa212008-12-15 01:17:50 -05001500 data = audit_krule_to_data(r);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001501 if (unlikely(!data))
1502 break;
1503 skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 0, 1,
1504 data, sizeof(*data) + data->buflen);
Al Viro9044e6b2006-05-22 01:09:24 -04001505 if (skb)
1506 skb_queue_tail(q, skb);
Amy Griffis93315ed2006-02-07 12:05:27 -05001507 kfree(data);
1508 }
1509 }
Al Viro9044e6b2006-05-22 01:09:24 -04001510 skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 1, 1, NULL, 0);
1511 if (skb)
1512 skb_queue_tail(q, skb);
Amy Griffis93315ed2006-02-07 12:05:27 -05001513}
1514
Amy Griffis5adc8a62006-06-14 18:45:21 -04001515/* Log rule additions and removals */
Eric Paris25323862008-04-18 10:09:25 -04001516static void audit_log_rule_change(uid_t loginuid, u32 sessionid, u32 sid,
1517 char *action, struct audit_krule *rule,
1518 int res)
Amy Griffis5adc8a62006-06-14 18:45:21 -04001519{
1520 struct audit_buffer *ab;
1521
Eric Paris1a6b9f22008-01-07 17:09:31 -05001522 if (!audit_enabled)
1523 return;
1524
Amy Griffis5adc8a62006-06-14 18:45:21 -04001525 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
1526 if (!ab)
1527 return;
Eric Paris25323862008-04-18 10:09:25 -04001528 audit_log_format(ab, "auid=%u ses=%u", loginuid, sessionid);
Amy Griffis5adc8a62006-06-14 18:45:21 -04001529 if (sid) {
1530 char *ctx = NULL;
1531 u32 len;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001532 if (security_secid_to_secctx(sid, &ctx, &len))
Amy Griffis5adc8a62006-06-14 18:45:21 -04001533 audit_log_format(ab, " ssid=%u", sid);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001534 else {
Amy Griffis5adc8a62006-06-14 18:45:21 -04001535 audit_log_format(ab, " subj=%s", ctx);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001536 security_release_secctx(ctx, len);
1537 }
Amy Griffis5adc8a62006-06-14 18:45:21 -04001538 }
Steve Grubba17b4ad2006-12-14 11:48:47 -05001539 audit_log_format(ab, " op=%s rule key=", action);
Amy Griffis5adc8a62006-06-14 18:45:21 -04001540 if (rule->filterkey)
1541 audit_log_untrustedstring(ab, rule->filterkey);
1542 else
1543 audit_log_format(ab, "(null)");
1544 audit_log_format(ab, " list=%d res=%d", rule->listnr, res);
1545 audit_log_end(ab);
1546}
1547
David Woodhousefe7752b2005-12-15 18:33:52 +00001548/**
1549 * audit_receive_filter - apply all rules to the specified message type
1550 * @type: audit message type
1551 * @pid: target pid for netlink audit messages
1552 * @uid: target uid for netlink audit messages
1553 * @seq: netlink audit message sequence (serial) number
1554 * @data: payload data
Amy Griffis93315ed2006-02-07 12:05:27 -05001555 * @datasz: size of payload data
David Woodhousefe7752b2005-12-15 18:33:52 +00001556 * @loginuid: loginuid of sender
Randy Dunlap9f0aecd2008-05-19 15:09:21 -07001557 * @sessionid: sessionid for netlink audit message
Steve Grubbce29b682006-04-01 18:29:34 -05001558 * @sid: SE Linux Security ID of sender
David Woodhousefe7752b2005-12-15 18:33:52 +00001559 */
1560int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
Eric Paris25323862008-04-18 10:09:25 -04001561 size_t datasz, uid_t loginuid, u32 sessionid, u32 sid)
David Woodhousefe7752b2005-12-15 18:33:52 +00001562{
1563 struct task_struct *tsk;
Al Viro9044e6b2006-05-22 01:09:24 -04001564 struct audit_netlink_list *dest;
Amy Griffis93315ed2006-02-07 12:05:27 -05001565 int err = 0;
1566 struct audit_entry *entry;
David Woodhousefe7752b2005-12-15 18:33:52 +00001567
1568 switch (type) {
1569 case AUDIT_LIST:
Amy Griffis93315ed2006-02-07 12:05:27 -05001570 case AUDIT_LIST_RULES:
David Woodhousefe7752b2005-12-15 18:33:52 +00001571 /* We can't just spew out the rules here because we might fill
1572 * the available socket buffer space and deadlock waiting for
1573 * auditctl to read from it... which isn't ever going to
1574 * happen if we're actually running in the context of auditctl
1575 * trying to _send_ the stuff */
Daniel Walker9ce34212007-10-18 03:06:06 -07001576
Al Viro9044e6b2006-05-22 01:09:24 -04001577 dest = kmalloc(sizeof(struct audit_netlink_list), GFP_KERNEL);
David Woodhousefe7752b2005-12-15 18:33:52 +00001578 if (!dest)
1579 return -ENOMEM;
Al Viro9044e6b2006-05-22 01:09:24 -04001580 dest->pid = pid;
1581 skb_queue_head_init(&dest->q);
David Woodhousefe7752b2005-12-15 18:33:52 +00001582
Amy Griffisf368c07d2006-04-07 16:55:56 -04001583 mutex_lock(&audit_filter_mutex);
Amy Griffis93315ed2006-02-07 12:05:27 -05001584 if (type == AUDIT_LIST)
Al Viro9044e6b2006-05-22 01:09:24 -04001585 audit_list(pid, seq, &dest->q);
Amy Griffis93315ed2006-02-07 12:05:27 -05001586 else
Al Viro9044e6b2006-05-22 01:09:24 -04001587 audit_list_rules(pid, seq, &dest->q);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001588 mutex_unlock(&audit_filter_mutex);
Al Viro9044e6b2006-05-22 01:09:24 -04001589
1590 tsk = kthread_run(audit_send_list, dest, "audit_send_list");
David Woodhousefe7752b2005-12-15 18:33:52 +00001591 if (IS_ERR(tsk)) {
Al Viro9044e6b2006-05-22 01:09:24 -04001592 skb_queue_purge(&dest->q);
David Woodhousefe7752b2005-12-15 18:33:52 +00001593 kfree(dest);
1594 err = PTR_ERR(tsk);
1595 }
1596 break;
1597 case AUDIT_ADD:
Amy Griffis93315ed2006-02-07 12:05:27 -05001598 case AUDIT_ADD_RULE:
1599 if (type == AUDIT_ADD)
1600 entry = audit_rule_to_entry(data);
1601 else
1602 entry = audit_data_to_entry(data, datasz);
1603 if (IS_ERR(entry))
1604 return PTR_ERR(entry);
David Woodhousefe7752b2005-12-15 18:33:52 +00001605
Amy Griffis93315ed2006-02-07 12:05:27 -05001606 err = audit_add_rule(entry,
1607 &audit_filter_list[entry->rule.listnr]);
Eric Paris25323862008-04-18 10:09:25 -04001608 audit_log_rule_change(loginuid, sessionid, sid, "add",
1609 &entry->rule, !err);
Steve Grubb5d330102006-01-09 09:48:17 -05001610
1611 if (err)
Amy Griffis93315ed2006-02-07 12:05:27 -05001612 audit_free_rule(entry);
David Woodhousefe7752b2005-12-15 18:33:52 +00001613 break;
1614 case AUDIT_DEL:
Amy Griffis93315ed2006-02-07 12:05:27 -05001615 case AUDIT_DEL_RULE:
1616 if (type == AUDIT_DEL)
1617 entry = audit_rule_to_entry(data);
1618 else
1619 entry = audit_data_to_entry(data, datasz);
1620 if (IS_ERR(entry))
1621 return PTR_ERR(entry);
David Woodhousefe7752b2005-12-15 18:33:52 +00001622
Amy Griffis93315ed2006-02-07 12:05:27 -05001623 err = audit_del_rule(entry,
1624 &audit_filter_list[entry->rule.listnr]);
Eric Paris25323862008-04-18 10:09:25 -04001625 audit_log_rule_change(loginuid, sessionid, sid, "remove",
1626 &entry->rule, !err);
Steve Grubb5d330102006-01-09 09:48:17 -05001627
Amy Griffis93315ed2006-02-07 12:05:27 -05001628 audit_free_rule(entry);
David Woodhousefe7752b2005-12-15 18:33:52 +00001629 break;
1630 default:
1631 return -EINVAL;
1632 }
1633
1634 return err;
1635}
1636
1637int audit_comparator(const u32 left, const u32 op, const u32 right)
1638{
1639 switch (op) {
1640 case AUDIT_EQUAL:
1641 return (left == right);
1642 case AUDIT_NOT_EQUAL:
1643 return (left != right);
1644 case AUDIT_LESS_THAN:
1645 return (left < right);
1646 case AUDIT_LESS_THAN_OR_EQUAL:
1647 return (left <= right);
1648 case AUDIT_GREATER_THAN:
1649 return (left > right);
1650 case AUDIT_GREATER_THAN_OR_EQUAL:
1651 return (left >= right);
Eric Paris74f23452007-06-04 17:00:14 -04001652 case AUDIT_BIT_MASK:
1653 return (left & right);
1654 case AUDIT_BIT_TEST:
1655 return ((left & right) == right);
David Woodhousefe7752b2005-12-15 18:33:52 +00001656 }
Dustin Kirklandd9d9ec62006-02-16 13:40:01 -06001657 BUG();
1658 return 0;
David Woodhousefe7752b2005-12-15 18:33:52 +00001659}
1660
Amy Griffisf368c07d2006-04-07 16:55:56 -04001661/* Compare given dentry name with last component in given path,
1662 * return of 0 indicates a match. */
Amy Griffis9c937dc2006-06-08 23:19:31 -04001663int audit_compare_dname_path(const char *dname, const char *path,
1664 int *dirlen)
Amy Griffisf368c07d2006-04-07 16:55:56 -04001665{
1666 int dlen, plen;
1667 const char *p;
David Woodhousefe7752b2005-12-15 18:33:52 +00001668
Amy Griffisf368c07d2006-04-07 16:55:56 -04001669 if (!dname || !path)
1670 return 1;
1671
1672 dlen = strlen(dname);
1673 plen = strlen(path);
1674 if (plen < dlen)
1675 return 1;
1676
1677 /* disregard trailing slashes */
1678 p = path + plen - 1;
1679 while ((*p == '/') && (p > path))
1680 p--;
1681
1682 /* find last path component */
1683 p = p - dlen + 1;
1684 if (p < path)
1685 return 1;
1686 else if (p > path) {
1687 if (*--p != '/')
1688 return 1;
1689 else
1690 p++;
1691 }
1692
Amy Griffis9c937dc2006-06-08 23:19:31 -04001693 /* return length of path's directory component */
1694 if (dirlen)
1695 *dirlen = p - path;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001696 return strncmp(p, dname, dlen);
1697}
David Woodhousefe7752b2005-12-15 18:33:52 +00001698
1699static int audit_filter_user_rules(struct netlink_skb_parms *cb,
Amy Griffis93315ed2006-02-07 12:05:27 -05001700 struct audit_krule *rule,
David Woodhousefe7752b2005-12-15 18:33:52 +00001701 enum audit_state *state)
1702{
1703 int i;
1704
1705 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -05001706 struct audit_field *f = &rule->fields[i];
David Woodhousefe7752b2005-12-15 18:33:52 +00001707 int result = 0;
1708
Amy Griffis93315ed2006-02-07 12:05:27 -05001709 switch (f->type) {
David Woodhousefe7752b2005-12-15 18:33:52 +00001710 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -05001711 result = audit_comparator(cb->creds.pid, f->op, f->val);
David Woodhousefe7752b2005-12-15 18:33:52 +00001712 break;
1713 case AUDIT_UID:
Amy Griffis93315ed2006-02-07 12:05:27 -05001714 result = audit_comparator(cb->creds.uid, f->op, f->val);
David Woodhousefe7752b2005-12-15 18:33:52 +00001715 break;
1716 case AUDIT_GID:
Amy Griffis93315ed2006-02-07 12:05:27 -05001717 result = audit_comparator(cb->creds.gid, f->op, f->val);
David Woodhousefe7752b2005-12-15 18:33:52 +00001718 break;
1719 case AUDIT_LOGINUID:
Amy Griffis93315ed2006-02-07 12:05:27 -05001720 result = audit_comparator(cb->loginuid, f->op, f->val);
David Woodhousefe7752b2005-12-15 18:33:52 +00001721 break;
1722 }
1723
1724 if (!result)
1725 return 0;
1726 }
1727 switch (rule->action) {
1728 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
David Woodhousefe7752b2005-12-15 18:33:52 +00001729 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
1730 }
1731 return 1;
1732}
1733
Peng Haitaod8de7242008-05-20 09:13:02 +08001734int audit_filter_user(struct netlink_skb_parms *cb)
David Woodhousefe7752b2005-12-15 18:33:52 +00001735{
Ingo Molnar11f57ce2007-02-10 01:46:09 -08001736 enum audit_state state = AUDIT_DISABLED;
David Woodhousefe7752b2005-12-15 18:33:52 +00001737 struct audit_entry *e;
David Woodhousefe7752b2005-12-15 18:33:52 +00001738 int ret = 1;
1739
1740 rcu_read_lock();
1741 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
1742 if (audit_filter_user_rules(cb, &e->rule, &state)) {
1743 if (state == AUDIT_DISABLED)
1744 ret = 0;
1745 break;
1746 }
1747 }
1748 rcu_read_unlock();
1749
1750 return ret; /* Audit by default */
1751}
1752
1753int audit_filter_type(int type)
1754{
1755 struct audit_entry *e;
1756 int result = 0;
Daniel Walker9ce34212007-10-18 03:06:06 -07001757
David Woodhousefe7752b2005-12-15 18:33:52 +00001758 rcu_read_lock();
1759 if (list_empty(&audit_filter_list[AUDIT_FILTER_TYPE]))
1760 goto unlock_and_return;
1761
1762 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TYPE],
1763 list) {
David Woodhousefe7752b2005-12-15 18:33:52 +00001764 int i;
Amy Griffis93315ed2006-02-07 12:05:27 -05001765 for (i = 0; i < e->rule.field_count; i++) {
1766 struct audit_field *f = &e->rule.fields[i];
1767 if (f->type == AUDIT_MSGTYPE) {
1768 result = audit_comparator(type, f->op, f->val);
David Woodhousefe7752b2005-12-15 18:33:52 +00001769 if (!result)
1770 break;
1771 }
1772 }
1773 if (result)
1774 goto unlock_and_return;
1775 }
1776unlock_and_return:
1777 rcu_read_unlock();
1778 return result;
1779}
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001780
Al Viroe45aa212008-12-15 01:17:50 -05001781static int update_lsm_rule(struct audit_krule *r)
Al Viro1a9d0792008-12-14 12:04:02 -05001782{
Al Viroe45aa212008-12-15 01:17:50 -05001783 struct audit_entry *entry = container_of(r, struct audit_entry, rule);
Al Viro1a9d0792008-12-14 12:04:02 -05001784 struct audit_entry *nentry;
1785 struct audit_watch *watch;
1786 struct audit_tree *tree;
1787 int err = 0;
1788
Al Viroe45aa212008-12-15 01:17:50 -05001789 if (!security_audit_rule_known(r))
Al Viro1a9d0792008-12-14 12:04:02 -05001790 return 0;
1791
Al Viroe45aa212008-12-15 01:17:50 -05001792 watch = r->watch;
1793 tree = r->tree;
1794 nentry = audit_dupe_rule(r, watch);
Al Viro1a9d0792008-12-14 12:04:02 -05001795 if (IS_ERR(nentry)) {
1796 /* save the first error encountered for the
1797 * return value */
1798 err = PTR_ERR(nentry);
1799 audit_panic("error updating LSM filters");
1800 if (watch)
Al Viroe45aa212008-12-15 01:17:50 -05001801 list_del(&r->rlist);
Al Viro1a9d0792008-12-14 12:04:02 -05001802 list_del_rcu(&entry->list);
Al Viroe45aa212008-12-15 01:17:50 -05001803 list_del(&r->list);
Al Viro1a9d0792008-12-14 12:04:02 -05001804 } else {
1805 if (watch) {
1806 list_add(&nentry->rule.rlist, &watch->rules);
Al Viroe45aa212008-12-15 01:17:50 -05001807 list_del(&r->rlist);
Al Viro1a9d0792008-12-14 12:04:02 -05001808 } else if (tree)
Al Viroe45aa212008-12-15 01:17:50 -05001809 list_replace_init(&r->rlist, &nentry->rule.rlist);
Al Viro1a9d0792008-12-14 12:04:02 -05001810 list_replace_rcu(&entry->list, &nentry->list);
Al Viroe45aa212008-12-15 01:17:50 -05001811 list_replace(&r->list, &nentry->rule.list);
Al Viro1a9d0792008-12-14 12:04:02 -05001812 }
1813 call_rcu(&entry->rcu, audit_free_rule_rcu);
1814
1815 return err;
1816}
1817
Ahmed S. Darwish04305e42008-04-19 09:59:43 +10001818/* This function will re-initialize the lsm_rule field of all applicable rules.
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +02001819 * It will traverse the filter lists serarching for rules that contain LSM
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001820 * specific filter fields. When such a rule is found, it is copied, the
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +02001821 * LSM field is re-initialized, and the old rule is replaced with the
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001822 * updated rule. */
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +02001823int audit_update_lsm_rules(void)
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001824{
Al Viroe45aa212008-12-15 01:17:50 -05001825 struct audit_krule *r, *n;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001826 int i, err = 0;
1827
Amy Griffisf368c07d2006-04-07 16:55:56 -04001828 /* audit_filter_mutex synchronizes the writers */
1829 mutex_lock(&audit_filter_mutex);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001830
1831 for (i = 0; i < AUDIT_NR_FILTERS; i++) {
Al Viroe45aa212008-12-15 01:17:50 -05001832 list_for_each_entry_safe(r, n, &audit_rules_list[i], list) {
1833 int res = update_lsm_rule(r);
Al Viro1a9d0792008-12-14 12:04:02 -05001834 if (!err)
1835 err = res;
1836 }
1837 }
Amy Griffisf368c07d2006-04-07 16:55:56 -04001838 mutex_unlock(&audit_filter_mutex);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001839
1840 return err;
1841}
Amy Griffisf368c07d2006-04-07 16:55:56 -04001842
1843/* Update watch data in audit rules based on inotify events. */
1844void audit_handle_ievent(struct inotify_watch *i_watch, u32 wd, u32 mask,
1845 u32 cookie, const char *dname, struct inode *inode)
1846{
1847 struct audit_parent *parent;
1848
1849 parent = container_of(i_watch, struct audit_parent, wdata);
1850
1851 if (mask & (IN_CREATE|IN_MOVED_TO) && inode)
1852 audit_update_watch(parent, dname, inode->i_sb->s_dev,
1853 inode->i_ino, 0);
1854 else if (mask & (IN_DELETE|IN_MOVED_FROM))
1855 audit_update_watch(parent, dname, (dev_t)-1, (unsigned long)-1, 1);
1856 /* inotify automatically removes the watch and sends IN_IGNORED */
1857 else if (mask & (IN_DELETE_SELF|IN_UNMOUNT))
1858 audit_remove_parent_watches(parent);
1859 /* inotify does not remove the watch, so remove it manually */
1860 else if(mask & IN_MOVE_SELF) {
1861 audit_remove_parent_watches(parent);
1862 inotify_remove_watch_locked(audit_ih, i_watch);
1863 } else if (mask & IN_IGNORED)
1864 put_inotify_watch(i_watch);
1865}