blob: 995a2e86808db658d789f07297b66777538366b6 [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};
89
Al Viro74c3cbe2007-07-22 08:04:18 -040090DEFINE_MUTEX(audit_filter_mutex);
Amy Griffisf368c07d2006-04-07 16:55:56 -040091
Amy Griffisf368c07d2006-04-07 16:55:56 -040092/* Inotify events we care about. */
93#define AUDIT_IN_WATCH IN_MOVE|IN_CREATE|IN_DELETE|IN_DELETE_SELF|IN_MOVE_SELF
94
95void audit_free_parent(struct inotify_watch *i_watch)
96{
97 struct audit_parent *parent;
98
99 parent = container_of(i_watch, struct audit_parent, wdata);
100 WARN_ON(!list_empty(&parent->watches));
101 kfree(parent);
102}
103
104static inline void audit_get_watch(struct audit_watch *watch)
105{
106 atomic_inc(&watch->count);
107}
108
109static void audit_put_watch(struct audit_watch *watch)
110{
111 if (atomic_dec_and_test(&watch->count)) {
112 WARN_ON(watch->parent);
113 WARN_ON(!list_empty(&watch->rules));
114 kfree(watch->path);
115 kfree(watch);
116 }
117}
118
119static void audit_remove_watch(struct audit_watch *watch)
120{
121 list_del(&watch->wlist);
122 put_inotify_watch(&watch->parent->wdata);
123 watch->parent = NULL;
124 audit_put_watch(watch); /* match initial get */
125}
126
Amy Griffis93315ed2006-02-07 12:05:27 -0500127static inline void audit_free_rule(struct audit_entry *e)
David Woodhousefe7752b2005-12-15 18:33:52 +0000128{
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600129 int i;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400130
131 /* some rules don't have associated watches */
132 if (e->rule.watch)
133 audit_put_watch(e->rule.watch);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600134 if (e->rule.fields)
135 for (i = 0; i < e->rule.field_count; i++) {
136 struct audit_field *f = &e->rule.fields[i];
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000137 kfree(f->lsm_str);
138 security_audit_rule_free(f->lsm_rule);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600139 }
Amy Griffis93315ed2006-02-07 12:05:27 -0500140 kfree(e->rule.fields);
Amy Griffis5adc8a62006-06-14 18:45:21 -0400141 kfree(e->rule.filterkey);
Amy Griffis93315ed2006-02-07 12:05:27 -0500142 kfree(e);
David Woodhousefe7752b2005-12-15 18:33:52 +0000143}
144
Al Viro74c3cbe2007-07-22 08:04:18 -0400145void audit_free_rule_rcu(struct rcu_head *head)
Amy Griffis93315ed2006-02-07 12:05:27 -0500146{
147 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
148 audit_free_rule(e);
149}
150
Amy Griffisf368c07d2006-04-07 16:55:56 -0400151/* Initialize a parent watch entry. */
152static struct audit_parent *audit_init_parent(struct nameidata *ndp)
153{
154 struct audit_parent *parent;
155 s32 wd;
156
157 parent = kzalloc(sizeof(*parent), GFP_KERNEL);
158 if (unlikely(!parent))
159 return ERR_PTR(-ENOMEM);
160
161 INIT_LIST_HEAD(&parent->watches);
162 parent->flags = 0;
163
164 inotify_init_watch(&parent->wdata);
165 /* grab a ref so inotify watch hangs around until we take audit_filter_mutex */
166 get_inotify_watch(&parent->wdata);
Jan Blunck4ac91372008-02-14 19:34:32 -0800167 wd = inotify_add_watch(audit_ih, &parent->wdata,
168 ndp->path.dentry->d_inode, AUDIT_IN_WATCH);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400169 if (wd < 0) {
170 audit_free_parent(&parent->wdata);
171 return ERR_PTR(wd);
172 }
173
174 return parent;
175}
176
177/* Initialize a watch entry. */
178static struct audit_watch *audit_init_watch(char *path)
179{
180 struct audit_watch *watch;
181
182 watch = kzalloc(sizeof(*watch), GFP_KERNEL);
183 if (unlikely(!watch))
184 return ERR_PTR(-ENOMEM);
185
186 INIT_LIST_HEAD(&watch->rules);
187 atomic_set(&watch->count, 1);
188 watch->path = path;
189 watch->dev = (dev_t)-1;
190 watch->ino = (unsigned long)-1;
191
192 return watch;
193}
194
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600195/* Initialize an audit filterlist entry. */
196static inline struct audit_entry *audit_init_entry(u32 field_count)
197{
198 struct audit_entry *entry;
199 struct audit_field *fields;
200
201 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
202 if (unlikely(!entry))
203 return NULL;
204
205 fields = kzalloc(sizeof(*fields) * field_count, GFP_KERNEL);
206 if (unlikely(!fields)) {
207 kfree(entry);
208 return NULL;
209 }
210 entry->rule.fields = fields;
211
212 return entry;
213}
214
Amy Griffis93315ed2006-02-07 12:05:27 -0500215/* Unpack a filter field's string representation from user-space
216 * buffer. */
Al Viro74c3cbe2007-07-22 08:04:18 -0400217char *audit_unpack_string(void **bufp, size_t *remain, size_t len)
Amy Griffis93315ed2006-02-07 12:05:27 -0500218{
219 char *str;
220
221 if (!*bufp || (len == 0) || (len > *remain))
222 return ERR_PTR(-EINVAL);
223
224 /* Of the currently implemented string fields, PATH_MAX
225 * defines the longest valid length.
226 */
227 if (len > PATH_MAX)
228 return ERR_PTR(-ENAMETOOLONG);
229
230 str = kmalloc(len + 1, GFP_KERNEL);
231 if (unlikely(!str))
232 return ERR_PTR(-ENOMEM);
233
234 memcpy(str, *bufp, len);
235 str[len] = 0;
236 *bufp += len;
237 *remain -= len;
238
239 return str;
240}
241
Amy Griffisf368c07d2006-04-07 16:55:56 -0400242/* Translate an inode field to kernel respresentation. */
243static inline int audit_to_inode(struct audit_krule *krule,
244 struct audit_field *f)
245{
246 if (krule->listnr != AUDIT_FILTER_EXIT ||
Al Viro74c3cbe2007-07-22 08:04:18 -0400247 krule->watch || krule->inode_f || krule->tree)
Amy Griffisf368c07d2006-04-07 16:55:56 -0400248 return -EINVAL;
249
250 krule->inode_f = f;
251 return 0;
252}
253
254/* Translate a watch string to kernel respresentation. */
255static int audit_to_watch(struct audit_krule *krule, char *path, int len,
256 u32 op)
257{
258 struct audit_watch *watch;
259
260 if (!audit_ih)
261 return -EOPNOTSUPP;
262
263 if (path[0] != '/' || path[len-1] == '/' ||
264 krule->listnr != AUDIT_FILTER_EXIT ||
265 op & ~AUDIT_EQUAL ||
Al Viro74c3cbe2007-07-22 08:04:18 -0400266 krule->inode_f || krule->watch || krule->tree)
Amy Griffisf368c07d2006-04-07 16:55:56 -0400267 return -EINVAL;
268
269 watch = audit_init_watch(path);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -0700270 if (IS_ERR(watch))
Amy Griffisf368c07d2006-04-07 16:55:56 -0400271 return PTR_ERR(watch);
272
273 audit_get_watch(watch);
274 krule->watch = watch;
275
276 return 0;
277}
278
Al Virob9155432006-07-01 03:56:16 -0400279static __u32 *classes[AUDIT_SYSCALL_CLASSES];
280
281int __init audit_register_class(int class, unsigned *list)
282{
283 __u32 *p = kzalloc(AUDIT_BITMASK_SIZE * sizeof(__u32), GFP_KERNEL);
284 if (!p)
285 return -ENOMEM;
286 while (*list != ~0U) {
287 unsigned n = *list++;
288 if (n >= AUDIT_BITMASK_SIZE * 32 - AUDIT_SYSCALL_CLASSES) {
289 kfree(p);
290 return -EINVAL;
291 }
292 p[AUDIT_WORD(n)] |= AUDIT_BIT(n);
293 }
294 if (class >= AUDIT_SYSCALL_CLASSES || classes[class]) {
295 kfree(p);
296 return -EINVAL;
297 }
298 classes[class] = p;
299 return 0;
300}
301
Al Viro55669bf2006-08-31 19:26:40 -0400302int audit_match_class(int class, unsigned syscall)
303{
Klaus Weidnerc926e4f2007-05-16 17:45:42 -0500304 if (unlikely(syscall >= AUDIT_BITMASK_SIZE * 32))
Al Viro55669bf2006-08-31 19:26:40 -0400305 return 0;
306 if (unlikely(class >= AUDIT_SYSCALL_CLASSES || !classes[class]))
307 return 0;
308 return classes[class][AUDIT_WORD(syscall)] & AUDIT_BIT(syscall);
309}
310
Al Viro327b9ee2007-05-15 20:37:10 +0100311#ifdef CONFIG_AUDITSYSCALL
Amy Griffise54dc242007-03-29 18:01:04 -0400312static inline int audit_match_class_bits(int class, u32 *mask)
313{
314 int i;
315
316 if (classes[class]) {
317 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
318 if (mask[i] & classes[class][i])
319 return 0;
320 }
321 return 1;
322}
323
324static int audit_match_signal(struct audit_entry *entry)
325{
326 struct audit_field *arch = entry->rule.arch_f;
327
328 if (!arch) {
329 /* When arch is unspecified, we must check both masks on biarch
330 * as syscall number alone is ambiguous. */
331 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL,
332 entry->rule.mask) &&
333 audit_match_class_bits(AUDIT_CLASS_SIGNAL_32,
334 entry->rule.mask));
335 }
336
337 switch(audit_classify_arch(arch->val)) {
338 case 0: /* native */
339 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL,
340 entry->rule.mask));
341 case 1: /* 32bit on biarch */
342 return (audit_match_class_bits(AUDIT_CLASS_SIGNAL_32,
343 entry->rule.mask));
344 default:
345 return 1;
346 }
347}
Al Viro327b9ee2007-05-15 20:37:10 +0100348#endif
Amy Griffise54dc242007-03-29 18:01:04 -0400349
Amy Griffis93315ed2006-02-07 12:05:27 -0500350/* Common user-space to kernel rule translation. */
351static inline struct audit_entry *audit_to_entry_common(struct audit_rule *rule)
352{
353 unsigned listnr;
354 struct audit_entry *entry;
Amy Griffis93315ed2006-02-07 12:05:27 -0500355 int i, err;
356
357 err = -EINVAL;
358 listnr = rule->flags & ~AUDIT_FILTER_PREPEND;
359 switch(listnr) {
360 default:
361 goto exit_err;
362 case AUDIT_FILTER_USER:
363 case AUDIT_FILTER_TYPE:
364#ifdef CONFIG_AUDITSYSCALL
365 case AUDIT_FILTER_ENTRY:
366 case AUDIT_FILTER_EXIT:
367 case AUDIT_FILTER_TASK:
368#endif
369 ;
370 }
Al Viro014149c2006-05-23 01:36:13 -0400371 if (unlikely(rule->action == AUDIT_POSSIBLE)) {
372 printk(KERN_ERR "AUDIT_POSSIBLE is deprecated\n");
373 goto exit_err;
374 }
375 if (rule->action != AUDIT_NEVER && rule->action != AUDIT_ALWAYS)
Amy Griffis93315ed2006-02-07 12:05:27 -0500376 goto exit_err;
377 if (rule->field_count > AUDIT_MAX_FIELDS)
378 goto exit_err;
379
380 err = -ENOMEM;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600381 entry = audit_init_entry(rule->field_count);
382 if (!entry)
Amy Griffis93315ed2006-02-07 12:05:27 -0500383 goto exit_err;
Amy Griffis93315ed2006-02-07 12:05:27 -0500384
385 entry->rule.flags = rule->flags & AUDIT_FILTER_PREPEND;
386 entry->rule.listnr = listnr;
387 entry->rule.action = rule->action;
388 entry->rule.field_count = rule->field_count;
Amy Griffis93315ed2006-02-07 12:05:27 -0500389
390 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
391 entry->rule.mask[i] = rule->mask[i];
392
Al Virob9155432006-07-01 03:56:16 -0400393 for (i = 0; i < AUDIT_SYSCALL_CLASSES; i++) {
394 int bit = AUDIT_BITMASK_SIZE * 32 - i - 1;
395 __u32 *p = &entry->rule.mask[AUDIT_WORD(bit)];
396 __u32 *class;
397
398 if (!(*p & AUDIT_BIT(bit)))
399 continue;
400 *p &= ~AUDIT_BIT(bit);
401 class = classes[i];
402 if (class) {
403 int j;
404 for (j = 0; j < AUDIT_BITMASK_SIZE; j++)
405 entry->rule.mask[j] |= class[j];
406 }
407 }
408
Amy Griffis93315ed2006-02-07 12:05:27 -0500409 return entry;
410
411exit_err:
412 return ERR_PTR(err);
413}
414
415/* Translate struct audit_rule to kernel's rule respresentation.
416 * Exists for backward compatibility with userspace. */
417static struct audit_entry *audit_rule_to_entry(struct audit_rule *rule)
418{
419 struct audit_entry *entry;
Harvey Harrison7719e432008-04-27 02:39:56 -0700420 struct audit_field *ino_f;
Amy Griffis93315ed2006-02-07 12:05:27 -0500421 int err = 0;
422 int i;
423
424 entry = audit_to_entry_common(rule);
425 if (IS_ERR(entry))
426 goto exit_nofree;
427
428 for (i = 0; i < rule->field_count; i++) {
429 struct audit_field *f = &entry->rule.fields[i];
430
Amy Griffis93315ed2006-02-07 12:05:27 -0500431 f->op = rule->fields[i] & (AUDIT_NEGATE|AUDIT_OPERATORS);
432 f->type = rule->fields[i] & ~(AUDIT_NEGATE|AUDIT_OPERATORS);
433 f->val = rule->values[i];
434
Amy Griffisf368c07d2006-04-07 16:55:56 -0400435 err = -EINVAL;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400436 switch(f->type) {
Al Viro0a73dcc2006-06-05 08:15:59 -0400437 default:
Amy Griffisf368c07d2006-04-07 16:55:56 -0400438 goto exit_free;
Al Viro0a73dcc2006-06-05 08:15:59 -0400439 case AUDIT_PID:
440 case AUDIT_UID:
441 case AUDIT_EUID:
442 case AUDIT_SUID:
443 case AUDIT_FSUID:
444 case AUDIT_GID:
445 case AUDIT_EGID:
446 case AUDIT_SGID:
447 case AUDIT_FSGID:
448 case AUDIT_LOGINUID:
449 case AUDIT_PERS:
Al Viro0a73dcc2006-06-05 08:15:59 -0400450 case AUDIT_MSGTYPE:
Steve Grubb3b33ac32006-08-26 14:06:20 -0400451 case AUDIT_PPID:
Al Viro0a73dcc2006-06-05 08:15:59 -0400452 case AUDIT_DEVMAJOR:
453 case AUDIT_DEVMINOR:
454 case AUDIT_EXIT:
455 case AUDIT_SUCCESS:
Eric Paris74f23452007-06-04 17:00:14 -0400456 /* bit ops are only useful on syscall args */
457 if (f->op == AUDIT_BIT_MASK ||
458 f->op == AUDIT_BIT_TEST) {
459 err = -EINVAL;
460 goto exit_free;
461 }
462 break;
Al Viro0a73dcc2006-06-05 08:15:59 -0400463 case AUDIT_ARG0:
464 case AUDIT_ARG1:
465 case AUDIT_ARG2:
466 case AUDIT_ARG3:
467 break;
Eric Paris4b8a3112006-09-28 17:46:21 -0400468 /* arch is only allowed to be = or != */
469 case AUDIT_ARCH:
470 if ((f->op != AUDIT_NOT_EQUAL) && (f->op != AUDIT_EQUAL)
471 && (f->op != AUDIT_NEGATE) && (f->op)) {
472 err = -EINVAL;
473 goto exit_free;
474 }
Amy Griffise54dc242007-03-29 18:01:04 -0400475 entry->rule.arch_f = f;
Eric Paris4b8a3112006-09-28 17:46:21 -0400476 break;
Al Viro55669bf2006-08-31 19:26:40 -0400477 case AUDIT_PERM:
478 if (f->val & ~15)
479 goto exit_free;
480 break;
Al Viro8b67dca2008-04-28 04:15:49 -0400481 case AUDIT_FILETYPE:
482 if ((f->val & ~S_IFMT) > S_IFMT)
483 goto exit_free;
484 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400485 case AUDIT_INODE:
486 err = audit_to_inode(&entry->rule, f);
487 if (err)
488 goto exit_free;
489 break;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600490 }
491
Amy Griffis93315ed2006-02-07 12:05:27 -0500492 entry->rule.vers_ops = (f->op & AUDIT_OPERATORS) ? 2 : 1;
Dustin Kirklandd9d9ec62006-02-16 13:40:01 -0600493
494 /* Support for legacy operators where
495 * AUDIT_NEGATE bit signifies != and otherwise assumes == */
Amy Griffis93315ed2006-02-07 12:05:27 -0500496 if (f->op & AUDIT_NEGATE)
Dustin Kirklandd9d9ec62006-02-16 13:40:01 -0600497 f->op = AUDIT_NOT_EQUAL;
498 else if (!f->op)
499 f->op = AUDIT_EQUAL;
500 else if (f->op == AUDIT_OPERATORS) {
501 err = -EINVAL;
502 goto exit_free;
503 }
Amy Griffis93315ed2006-02-07 12:05:27 -0500504 }
505
Harvey Harrison7719e432008-04-27 02:39:56 -0700506 ino_f = entry->rule.inode_f;
507 if (ino_f) {
508 switch(ino_f->op) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400509 case AUDIT_NOT_EQUAL:
510 entry->rule.inode_f = NULL;
511 case AUDIT_EQUAL:
512 break;
513 default:
Amy Griffis5422e012006-08-01 17:52:26 -0400514 err = -EINVAL;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400515 goto exit_free;
516 }
517 }
518
Amy Griffis93315ed2006-02-07 12:05:27 -0500519exit_nofree:
520 return entry;
521
522exit_free:
523 audit_free_rule(entry);
524 return ERR_PTR(err);
525}
526
527/* Translate struct audit_rule_data to kernel's rule respresentation. */
528static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
529 size_t datasz)
530{
531 int err = 0;
532 struct audit_entry *entry;
Harvey Harrison7719e432008-04-27 02:39:56 -0700533 struct audit_field *ino_f;
Amy Griffis93315ed2006-02-07 12:05:27 -0500534 void *bufp;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600535 size_t remain = datasz - sizeof(struct audit_rule_data);
Amy Griffis93315ed2006-02-07 12:05:27 -0500536 int i;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600537 char *str;
Amy Griffis93315ed2006-02-07 12:05:27 -0500538
539 entry = audit_to_entry_common((struct audit_rule *)data);
540 if (IS_ERR(entry))
541 goto exit_nofree;
542
543 bufp = data->buf;
544 entry->rule.vers_ops = 2;
545 for (i = 0; i < data->field_count; i++) {
546 struct audit_field *f = &entry->rule.fields[i];
547
548 err = -EINVAL;
549 if (!(data->fieldflags[i] & AUDIT_OPERATORS) ||
550 data->fieldflags[i] & ~AUDIT_OPERATORS)
551 goto exit_free;
552
553 f->op = data->fieldflags[i] & AUDIT_OPERATORS;
554 f->type = data->fields[i];
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600555 f->val = data->values[i];
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000556 f->lsm_str = NULL;
557 f->lsm_rule = NULL;
Amy Griffis93315ed2006-02-07 12:05:27 -0500558 switch(f->type) {
Al Viro0a73dcc2006-06-05 08:15:59 -0400559 case AUDIT_PID:
560 case AUDIT_UID:
561 case AUDIT_EUID:
562 case AUDIT_SUID:
563 case AUDIT_FSUID:
564 case AUDIT_GID:
565 case AUDIT_EGID:
566 case AUDIT_SGID:
567 case AUDIT_FSGID:
568 case AUDIT_LOGINUID:
569 case AUDIT_PERS:
Al Viro0a73dcc2006-06-05 08:15:59 -0400570 case AUDIT_MSGTYPE:
571 case AUDIT_PPID:
572 case AUDIT_DEVMAJOR:
573 case AUDIT_DEVMINOR:
574 case AUDIT_EXIT:
575 case AUDIT_SUCCESS:
576 case AUDIT_ARG0:
577 case AUDIT_ARG1:
578 case AUDIT_ARG2:
579 case AUDIT_ARG3:
580 break;
Amy Griffise54dc242007-03-29 18:01:04 -0400581 case AUDIT_ARCH:
582 entry->rule.arch_f = f;
583 break;
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500584 case AUDIT_SUBJ_USER:
585 case AUDIT_SUBJ_ROLE:
586 case AUDIT_SUBJ_TYPE:
587 case AUDIT_SUBJ_SEN:
588 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500589 case AUDIT_OBJ_USER:
590 case AUDIT_OBJ_ROLE:
591 case AUDIT_OBJ_TYPE:
592 case AUDIT_OBJ_LEV_LOW:
593 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600594 str = audit_unpack_string(&bufp, &remain, f->val);
595 if (IS_ERR(str))
596 goto exit_free;
597 entry->rule.buflen += f->val;
598
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200599 err = security_audit_rule_init(f->type, f->op, str,
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000600 (void **)&f->lsm_rule);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600601 /* Keep currently invalid fields around in case they
602 * become valid after a policy reload. */
603 if (err == -EINVAL) {
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200604 printk(KERN_WARNING "audit rule for LSM "
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600605 "\'%s\' is invalid\n", str);
606 err = 0;
607 }
608 if (err) {
609 kfree(str);
610 goto exit_free;
611 } else
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000612 f->lsm_str = str;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600613 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400614 case AUDIT_WATCH:
615 str = audit_unpack_string(&bufp, &remain, f->val);
616 if (IS_ERR(str))
617 goto exit_free;
618 entry->rule.buflen += f->val;
619
620 err = audit_to_watch(&entry->rule, str, f->val, f->op);
621 if (err) {
622 kfree(str);
623 goto exit_free;
624 }
625 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400626 case AUDIT_DIR:
627 str = audit_unpack_string(&bufp, &remain, f->val);
628 if (IS_ERR(str))
629 goto exit_free;
630 entry->rule.buflen += f->val;
631
632 err = audit_make_tree(&entry->rule, str, f->op);
633 kfree(str);
634 if (err)
635 goto exit_free;
636 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400637 case AUDIT_INODE:
638 err = audit_to_inode(&entry->rule, f);
639 if (err)
640 goto exit_free;
641 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400642 case AUDIT_FILTERKEY:
643 err = -EINVAL;
644 if (entry->rule.filterkey || f->val > AUDIT_MAX_KEY_LEN)
645 goto exit_free;
646 str = audit_unpack_string(&bufp, &remain, f->val);
647 if (IS_ERR(str))
648 goto exit_free;
649 entry->rule.buflen += f->val;
650 entry->rule.filterkey = str;
651 break;
Al Viro55669bf2006-08-31 19:26:40 -0400652 case AUDIT_PERM:
653 if (f->val & ~15)
654 goto exit_free;
655 break;
Al Viro8b67dca2008-04-28 04:15:49 -0400656 case AUDIT_FILETYPE:
657 if ((f->val & ~S_IFMT) > S_IFMT)
658 goto exit_free;
659 break;
Al Viro0a73dcc2006-06-05 08:15:59 -0400660 default:
661 goto exit_free;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400662 }
663 }
664
Harvey Harrison7719e432008-04-27 02:39:56 -0700665 ino_f = entry->rule.inode_f;
666 if (ino_f) {
667 switch(ino_f->op) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400668 case AUDIT_NOT_EQUAL:
669 entry->rule.inode_f = NULL;
670 case AUDIT_EQUAL:
671 break;
672 default:
Amy Griffis5422e012006-08-01 17:52:26 -0400673 err = -EINVAL;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400674 goto exit_free;
Amy Griffis93315ed2006-02-07 12:05:27 -0500675 }
676 }
677
678exit_nofree:
679 return entry;
680
681exit_free:
682 audit_free_rule(entry);
683 return ERR_PTR(err);
684}
685
686/* Pack a filter field's string representation into data block. */
Al Viro74c3cbe2007-07-22 08:04:18 -0400687static inline size_t audit_pack_string(void **bufp, const char *str)
Amy Griffis93315ed2006-02-07 12:05:27 -0500688{
689 size_t len = strlen(str);
690
691 memcpy(*bufp, str, len);
692 *bufp += len;
693
694 return len;
695}
696
697/* Translate kernel rule respresentation to struct audit_rule.
698 * Exists for backward compatibility with userspace. */
699static struct audit_rule *audit_krule_to_rule(struct audit_krule *krule)
700{
701 struct audit_rule *rule;
702 int i;
703
Burman Yan4668edc2006-12-06 20:38:51 -0800704 rule = kzalloc(sizeof(*rule), GFP_KERNEL);
Amy Griffis93315ed2006-02-07 12:05:27 -0500705 if (unlikely(!rule))
Amy Griffis0a3b4832006-05-02 15:06:01 -0400706 return NULL;
Amy Griffis93315ed2006-02-07 12:05:27 -0500707
708 rule->flags = krule->flags | krule->listnr;
709 rule->action = krule->action;
710 rule->field_count = krule->field_count;
711 for (i = 0; i < rule->field_count; i++) {
712 rule->values[i] = krule->fields[i].val;
713 rule->fields[i] = krule->fields[i].type;
714
715 if (krule->vers_ops == 1) {
716 if (krule->fields[i].op & AUDIT_NOT_EQUAL)
717 rule->fields[i] |= AUDIT_NEGATE;
718 } else {
719 rule->fields[i] |= krule->fields[i].op;
720 }
721 }
722 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) rule->mask[i] = krule->mask[i];
723
724 return rule;
725}
726
727/* Translate kernel rule respresentation to struct audit_rule_data. */
728static struct audit_rule_data *audit_krule_to_data(struct audit_krule *krule)
729{
730 struct audit_rule_data *data;
731 void *bufp;
732 int i;
733
734 data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL);
735 if (unlikely(!data))
Amy Griffis0a3b4832006-05-02 15:06:01 -0400736 return NULL;
Amy Griffis93315ed2006-02-07 12:05:27 -0500737 memset(data, 0, sizeof(*data));
738
739 data->flags = krule->flags | krule->listnr;
740 data->action = krule->action;
741 data->field_count = krule->field_count;
742 bufp = data->buf;
743 for (i = 0; i < data->field_count; i++) {
744 struct audit_field *f = &krule->fields[i];
745
746 data->fields[i] = f->type;
747 data->fieldflags[i] = f->op;
748 switch(f->type) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500749 case AUDIT_SUBJ_USER:
750 case AUDIT_SUBJ_ROLE:
751 case AUDIT_SUBJ_TYPE:
752 case AUDIT_SUBJ_SEN:
753 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500754 case AUDIT_OBJ_USER:
755 case AUDIT_OBJ_ROLE:
756 case AUDIT_OBJ_TYPE:
757 case AUDIT_OBJ_LEV_LOW:
758 case AUDIT_OBJ_LEV_HIGH:
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600759 data->buflen += data->values[i] =
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000760 audit_pack_string(&bufp, f->lsm_str);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600761 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400762 case AUDIT_WATCH:
763 data->buflen += data->values[i] =
764 audit_pack_string(&bufp, krule->watch->path);
765 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400766 case AUDIT_DIR:
767 data->buflen += data->values[i] =
768 audit_pack_string(&bufp,
769 audit_tree_path(krule->tree));
770 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400771 case AUDIT_FILTERKEY:
772 data->buflen += data->values[i] =
773 audit_pack_string(&bufp, krule->filterkey);
774 break;
Amy Griffis93315ed2006-02-07 12:05:27 -0500775 default:
776 data->values[i] = f->val;
777 }
778 }
779 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) data->mask[i] = krule->mask[i];
780
781 return data;
782}
783
784/* Compare two rules in kernel format. Considered success if rules
785 * don't match. */
786static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b)
David Woodhousefe7752b2005-12-15 18:33:52 +0000787{
788 int i;
789
Amy Griffis93315ed2006-02-07 12:05:27 -0500790 if (a->flags != b->flags ||
791 a->listnr != b->listnr ||
792 a->action != b->action ||
793 a->field_count != b->field_count)
David Woodhousefe7752b2005-12-15 18:33:52 +0000794 return 1;
795
796 for (i = 0; i < a->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500797 if (a->fields[i].type != b->fields[i].type ||
798 a->fields[i].op != b->fields[i].op)
David Woodhousefe7752b2005-12-15 18:33:52 +0000799 return 1;
Amy Griffis93315ed2006-02-07 12:05:27 -0500800
801 switch(a->fields[i].type) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500802 case AUDIT_SUBJ_USER:
803 case AUDIT_SUBJ_ROLE:
804 case AUDIT_SUBJ_TYPE:
805 case AUDIT_SUBJ_SEN:
806 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500807 case AUDIT_OBJ_USER:
808 case AUDIT_OBJ_ROLE:
809 case AUDIT_OBJ_TYPE:
810 case AUDIT_OBJ_LEV_LOW:
811 case AUDIT_OBJ_LEV_HIGH:
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000812 if (strcmp(a->fields[i].lsm_str, b->fields[i].lsm_str))
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600813 return 1;
814 break;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400815 case AUDIT_WATCH:
816 if (strcmp(a->watch->path, b->watch->path))
817 return 1;
818 break;
Al Viro74c3cbe2007-07-22 08:04:18 -0400819 case AUDIT_DIR:
820 if (strcmp(audit_tree_path(a->tree),
821 audit_tree_path(b->tree)))
822 return 1;
823 break;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400824 case AUDIT_FILTERKEY:
825 /* both filterkeys exist based on above type compare */
826 if (strcmp(a->filterkey, b->filterkey))
827 return 1;
828 break;
Amy Griffis93315ed2006-02-07 12:05:27 -0500829 default:
830 if (a->fields[i].val != b->fields[i].val)
831 return 1;
832 }
David Woodhousefe7752b2005-12-15 18:33:52 +0000833 }
834
835 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
836 if (a->mask[i] != b->mask[i])
837 return 1;
838
839 return 0;
840}
841
Amy Griffisf368c07d2006-04-07 16:55:56 -0400842/* Duplicate the given audit watch. The new watch's rules list is initialized
843 * to an empty list and wlist is undefined. */
844static struct audit_watch *audit_dupe_watch(struct audit_watch *old)
845{
846 char *path;
847 struct audit_watch *new;
848
849 path = kstrdup(old->path, GFP_KERNEL);
850 if (unlikely(!path))
851 return ERR_PTR(-ENOMEM);
852
853 new = audit_init_watch(path);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -0700854 if (IS_ERR(new)) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400855 kfree(path);
856 goto out;
857 }
858
859 new->dev = old->dev;
860 new->ino = old->ino;
861 get_inotify_watch(&old->parent->wdata);
862 new->parent = old->parent;
863
864out:
865 return new;
866}
867
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000868/* Duplicate LSM field information. The lsm_rule is opaque, so must be
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600869 * re-initialized. */
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200870static inline int audit_dupe_lsm_field(struct audit_field *df,
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600871 struct audit_field *sf)
872{
873 int ret = 0;
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000874 char *lsm_str;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600875
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000876 /* our own copy of lsm_str */
877 lsm_str = kstrdup(sf->lsm_str, GFP_KERNEL);
878 if (unlikely(!lsm_str))
Akinobu Mita3e1fbd12006-12-22 01:10:02 -0800879 return -ENOMEM;
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000880 df->lsm_str = lsm_str;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600881
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000882 /* our own (refreshed) copy of lsm_rule */
883 ret = security_audit_rule_init(df->type, df->op, df->lsm_str,
884 (void **)&df->lsm_rule);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600885 /* Keep currently invalid fields around in case they
886 * become valid after a policy reload. */
887 if (ret == -EINVAL) {
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200888 printk(KERN_WARNING "audit rule for LSM \'%s\' is "
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000889 "invalid\n", df->lsm_str);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600890 ret = 0;
891 }
892
893 return ret;
894}
895
896/* Duplicate an audit rule. This will be a deep copy with the exception
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200897 * of the watch - that pointer is carried over. The LSM specific fields
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600898 * will be updated in the copy. The point is to be able to replace the old
Amy Griffisf368c07d2006-04-07 16:55:56 -0400899 * rule with the new rule in the filterlist, then free the old rule.
900 * The rlist element is undefined; list manipulations are handled apart from
901 * the initial copy. */
902static struct audit_entry *audit_dupe_rule(struct audit_krule *old,
903 struct audit_watch *watch)
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600904{
905 u32 fcount = old->field_count;
906 struct audit_entry *entry;
907 struct audit_krule *new;
Amy Griffis5adc8a62006-06-14 18:45:21 -0400908 char *fk;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600909 int i, err = 0;
910
911 entry = audit_init_entry(fcount);
912 if (unlikely(!entry))
913 return ERR_PTR(-ENOMEM);
914
915 new = &entry->rule;
916 new->vers_ops = old->vers_ops;
917 new->flags = old->flags;
918 new->listnr = old->listnr;
919 new->action = old->action;
920 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
921 new->mask[i] = old->mask[i];
Al Viro0590b932008-12-14 23:45:27 -0500922 new->prio = old->prio;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600923 new->buflen = old->buflen;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400924 new->inode_f = old->inode_f;
925 new->watch = NULL;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600926 new->field_count = old->field_count;
Al Viro74c3cbe2007-07-22 08:04:18 -0400927 /*
928 * note that we are OK with not refcounting here; audit_match_tree()
929 * never dereferences tree and we can't get false positives there
930 * since we'd have to have rule gone from the list *and* removed
931 * before the chunks found by lookup had been allocated, i.e. before
932 * the beginning of list scan.
933 */
934 new->tree = old->tree;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600935 memcpy(new->fields, old->fields, sizeof(struct audit_field) * fcount);
936
Ahmed S. Darwish04305e42008-04-19 09:59:43 +1000937 /* deep copy this information, updating the lsm_rule fields, because
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600938 * the originals will all be freed when the old rule is freed. */
939 for (i = 0; i < fcount; i++) {
940 switch (new->fields[i].type) {
Darrel Goeddel3a6b9f82006-06-29 16:56:39 -0500941 case AUDIT_SUBJ_USER:
942 case AUDIT_SUBJ_ROLE:
943 case AUDIT_SUBJ_TYPE:
944 case AUDIT_SUBJ_SEN:
945 case AUDIT_SUBJ_CLR:
Darrel Goeddel6e5a2d12006-06-29 16:57:08 -0500946 case AUDIT_OBJ_USER:
947 case AUDIT_OBJ_ROLE:
948 case AUDIT_OBJ_TYPE:
949 case AUDIT_OBJ_LEV_LOW:
950 case AUDIT_OBJ_LEV_HIGH:
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +0200951 err = audit_dupe_lsm_field(&new->fields[i],
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600952 &old->fields[i]);
Amy Griffis5adc8a62006-06-14 18:45:21 -0400953 break;
954 case AUDIT_FILTERKEY:
955 fk = kstrdup(old->filterkey, GFP_KERNEL);
956 if (unlikely(!fk))
957 err = -ENOMEM;
958 else
959 new->filterkey = fk;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600960 }
961 if (err) {
962 audit_free_rule(entry);
963 return ERR_PTR(err);
964 }
965 }
966
Amy Griffisf368c07d2006-04-07 16:55:56 -0400967 if (watch) {
968 audit_get_watch(watch);
969 new->watch = watch;
970 }
971
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600972 return entry;
973}
974
Amy Griffisf368c07d2006-04-07 16:55:56 -0400975/* Update inode info in audit rules based on filesystem event. */
976static void audit_update_watch(struct audit_parent *parent,
977 const char *dname, dev_t dev,
978 unsigned long ino, unsigned invalidating)
David Woodhousefe7752b2005-12-15 18:33:52 +0000979{
Amy Griffisf368c07d2006-04-07 16:55:56 -0400980 struct audit_watch *owatch, *nwatch, *nextw;
981 struct audit_krule *r, *nextr;
982 struct audit_entry *oentry, *nentry;
Amy Griffisf368c07d2006-04-07 16:55:56 -0400983
984 mutex_lock(&audit_filter_mutex);
985 list_for_each_entry_safe(owatch, nextw, &parent->watches, wlist) {
Amy Griffis9c937dc2006-06-08 23:19:31 -0400986 if (audit_compare_dname_path(dname, owatch->path, NULL))
Amy Griffisf368c07d2006-04-07 16:55:56 -0400987 continue;
988
989 /* If the update involves invalidating rules, do the inode-based
990 * filtering now, so we don't omit records. */
Al Viro0590b932008-12-14 23:45:27 -0500991 if (invalidating && current->audit_context)
992 audit_filter_inodes(current, current->audit_context);
Amy Griffisf368c07d2006-04-07 16:55:56 -0400993
994 nwatch = audit_dupe_watch(owatch);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -0700995 if (IS_ERR(nwatch)) {
Amy Griffisf368c07d2006-04-07 16:55:56 -0400996 mutex_unlock(&audit_filter_mutex);
997 audit_panic("error updating watch, skipping");
998 return;
999 }
1000 nwatch->dev = dev;
1001 nwatch->ino = ino;
1002
1003 list_for_each_entry_safe(r, nextr, &owatch->rules, rlist) {
1004
1005 oentry = container_of(r, struct audit_entry, rule);
1006 list_del(&oentry->rule.rlist);
1007 list_del_rcu(&oentry->list);
1008
1009 nentry = audit_dupe_rule(&oentry->rule, nwatch);
Hirofumi Nakagawa801678c2008-04-29 01:03:09 -07001010 if (IS_ERR(nentry))
Amy Griffisf368c07d2006-04-07 16:55:56 -04001011 audit_panic("error updating watch, removing");
1012 else {
1013 int h = audit_hash_ino((u32)ino);
1014 list_add(&nentry->rule.rlist, &nwatch->rules);
1015 list_add_rcu(&nentry->list, &audit_inode_hash[h]);
1016 }
1017
1018 call_rcu(&oentry->rcu, audit_free_rule_rcu);
1019 }
1020
Eric Paris1a6b9f22008-01-07 17:09:31 -05001021 if (audit_enabled) {
1022 struct audit_buffer *ab;
1023 ab = audit_log_start(NULL, GFP_KERNEL,
1024 AUDIT_CONFIG_CHANGE);
zhangxiliang036bbf72008-08-01 09:47:01 +08001025 audit_log_format(ab, "auid=%u ses=%u",
1026 audit_get_loginuid(current),
1027 audit_get_sessionid(current));
Eric Paris1a6b9f22008-01-07 17:09:31 -05001028 audit_log_format(ab,
zhangxiliang036bbf72008-08-01 09:47:01 +08001029 " op=updated rules specifying path=");
Eric Paris1a6b9f22008-01-07 17:09:31 -05001030 audit_log_untrustedstring(ab, owatch->path);
1031 audit_log_format(ab, " with dev=%u ino=%lu\n",
1032 dev, ino);
1033 audit_log_format(ab, " list=%d res=1", r->listnr);
1034 audit_log_end(ab);
1035 }
Amy Griffisf368c07d2006-04-07 16:55:56 -04001036 audit_remove_watch(owatch);
1037 goto add_watch_to_parent; /* event applies to a single watch */
1038 }
1039 mutex_unlock(&audit_filter_mutex);
1040 return;
1041
1042add_watch_to_parent:
1043 list_add(&nwatch->wlist, &parent->watches);
1044 mutex_unlock(&audit_filter_mutex);
1045 return;
1046}
1047
1048/* Remove all watches & rules associated with a parent that is going away. */
1049static void audit_remove_parent_watches(struct audit_parent *parent)
1050{
1051 struct audit_watch *w, *nextw;
1052 struct audit_krule *r, *nextr;
Amy Griffis93315ed2006-02-07 12:05:27 -05001053 struct audit_entry *e;
David Woodhousefe7752b2005-12-15 18:33:52 +00001054
Amy Griffisf368c07d2006-04-07 16:55:56 -04001055 mutex_lock(&audit_filter_mutex);
1056 parent->flags |= AUDIT_PARENT_INVALID;
1057 list_for_each_entry_safe(w, nextw, &parent->watches, wlist) {
1058 list_for_each_entry_safe(r, nextr, &w->rules, rlist) {
1059 e = container_of(r, struct audit_entry, rule);
Eric Paris1a6b9f22008-01-07 17:09:31 -05001060 if (audit_enabled) {
1061 struct audit_buffer *ab;
1062 ab = audit_log_start(NULL, GFP_KERNEL,
1063 AUDIT_CONFIG_CHANGE);
zhangxiliang036bbf72008-08-01 09:47:01 +08001064 audit_log_format(ab, "auid=%u ses=%u",
1065 audit_get_loginuid(current),
1066 audit_get_sessionid(current));
1067 audit_log_format(ab, " op=remove rule path=");
Eric Paris1a6b9f22008-01-07 17:09:31 -05001068 audit_log_untrustedstring(ab, w->path);
1069 if (r->filterkey) {
1070 audit_log_format(ab, " key=");
1071 audit_log_untrustedstring(ab,
1072 r->filterkey);
1073 } else
1074 audit_log_format(ab, " key=(null)");
1075 audit_log_format(ab, " list=%d res=1",
1076 r->listnr);
1077 audit_log_end(ab);
1078 }
Amy Griffisf368c07d2006-04-07 16:55:56 -04001079 list_del(&r->rlist);
1080 list_del_rcu(&e->list);
1081 call_rcu(&e->rcu, audit_free_rule_rcu);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001082 }
1083 audit_remove_watch(w);
1084 }
1085 mutex_unlock(&audit_filter_mutex);
1086}
1087
1088/* Unregister inotify watches for parents on in_list.
1089 * Generates an IN_IGNORED event. */
1090static void audit_inotify_unregister(struct list_head *in_list)
1091{
1092 struct audit_parent *p, *n;
1093
1094 list_for_each_entry_safe(p, n, in_list, ilist) {
1095 list_del(&p->ilist);
1096 inotify_rm_watch(audit_ih, &p->wdata);
Al Viro8f7b0ba2008-11-15 01:15:43 +00001097 /* the unpin matching the pin in audit_do_del_rule() */
1098 unpin_inotify_watch(&p->wdata);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001099 }
1100}
1101
1102/* Find an existing audit rule.
1103 * Caller must hold audit_filter_mutex to prevent stale rule data. */
1104static struct audit_entry *audit_find_rule(struct audit_entry *entry,
1105 struct list_head *list)
1106{
1107 struct audit_entry *e, *found = NULL;
1108 int h;
1109
1110 if (entry->rule.watch) {
1111 /* we don't know the inode number, so must walk entire hash */
1112 for (h = 0; h < AUDIT_INODE_BUCKETS; h++) {
1113 list = &audit_inode_hash[h];
1114 list_for_each_entry(e, list, list)
1115 if (!audit_compare_rule(&entry->rule, &e->rule)) {
1116 found = e;
1117 goto out;
1118 }
1119 }
1120 goto out;
1121 }
1122
1123 list_for_each_entry(e, list, list)
1124 if (!audit_compare_rule(&entry->rule, &e->rule)) {
1125 found = e;
1126 goto out;
1127 }
1128
1129out:
1130 return found;
1131}
1132
1133/* Get path information necessary for adding watches. */
1134static int audit_get_nd(char *path, struct nameidata **ndp,
1135 struct nameidata **ndw)
1136{
1137 struct nameidata *ndparent, *ndwatch;
1138 int err;
1139
1140 ndparent = kmalloc(sizeof(*ndparent), GFP_KERNEL);
1141 if (unlikely(!ndparent))
1142 return -ENOMEM;
1143
1144 ndwatch = kmalloc(sizeof(*ndwatch), GFP_KERNEL);
1145 if (unlikely(!ndwatch)) {
1146 kfree(ndparent);
1147 return -ENOMEM;
1148 }
1149
1150 err = path_lookup(path, LOOKUP_PARENT, ndparent);
1151 if (err) {
1152 kfree(ndparent);
1153 kfree(ndwatch);
1154 return err;
1155 }
1156
1157 err = path_lookup(path, 0, ndwatch);
1158 if (err) {
1159 kfree(ndwatch);
1160 ndwatch = NULL;
1161 }
1162
1163 *ndp = ndparent;
1164 *ndw = ndwatch;
1165
1166 return 0;
1167}
1168
1169/* Release resources used for watch path information. */
1170static void audit_put_nd(struct nameidata *ndp, struct nameidata *ndw)
1171{
1172 if (ndp) {
Jan Blunck1d957f92008-02-14 19:34:35 -08001173 path_put(&ndp->path);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001174 kfree(ndp);
1175 }
1176 if (ndw) {
Jan Blunck1d957f92008-02-14 19:34:35 -08001177 path_put(&ndw->path);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001178 kfree(ndw);
1179 }
1180}
1181
1182/* Associate the given rule with an existing parent inotify_watch.
1183 * Caller must hold audit_filter_mutex. */
1184static void audit_add_to_parent(struct audit_krule *krule,
1185 struct audit_parent *parent)
1186{
1187 struct audit_watch *w, *watch = krule->watch;
1188 int watch_found = 0;
1189
1190 list_for_each_entry(w, &parent->watches, wlist) {
1191 if (strcmp(watch->path, w->path))
1192 continue;
1193
1194 watch_found = 1;
1195
1196 /* put krule's and initial refs to temporary watch */
1197 audit_put_watch(watch);
1198 audit_put_watch(watch);
1199
1200 audit_get_watch(w);
1201 krule->watch = watch = w;
1202 break;
1203 }
1204
1205 if (!watch_found) {
1206 get_inotify_watch(&parent->wdata);
1207 watch->parent = parent;
1208
1209 list_add(&watch->wlist, &parent->watches);
1210 }
1211 list_add(&krule->rlist, &watch->rules);
1212}
1213
1214/* Find a matching watch entry, or add this one.
1215 * Caller must hold audit_filter_mutex. */
1216static int audit_add_watch(struct audit_krule *krule, struct nameidata *ndp,
1217 struct nameidata *ndw)
1218{
1219 struct audit_watch *watch = krule->watch;
1220 struct inotify_watch *i_watch;
1221 struct audit_parent *parent;
1222 int ret = 0;
1223
1224 /* update watch filter fields */
1225 if (ndw) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001226 watch->dev = ndw->path.dentry->d_inode->i_sb->s_dev;
1227 watch->ino = ndw->path.dentry->d_inode->i_ino;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001228 }
1229
1230 /* The audit_filter_mutex must not be held during inotify calls because
1231 * we hold it during inotify event callback processing. If an existing
1232 * inotify watch is found, inotify_find_watch() grabs a reference before
1233 * returning.
1234 */
1235 mutex_unlock(&audit_filter_mutex);
1236
Jan Blunck4ac91372008-02-14 19:34:32 -08001237 if (inotify_find_watch(audit_ih, ndp->path.dentry->d_inode,
1238 &i_watch) < 0) {
Amy Griffisf368c07d2006-04-07 16:55:56 -04001239 parent = audit_init_parent(ndp);
1240 if (IS_ERR(parent)) {
1241 /* caller expects mutex locked */
1242 mutex_lock(&audit_filter_mutex);
1243 return PTR_ERR(parent);
1244 }
1245 } else
1246 parent = container_of(i_watch, struct audit_parent, wdata);
1247
1248 mutex_lock(&audit_filter_mutex);
1249
1250 /* parent was moved before we took audit_filter_mutex */
1251 if (parent->flags & AUDIT_PARENT_INVALID)
1252 ret = -ENOENT;
1253 else
1254 audit_add_to_parent(krule, parent);
1255
1256 /* match get in audit_init_parent or inotify_find_watch */
1257 put_inotify_watch(&parent->wdata);
1258 return ret;
1259}
1260
Al Viro0590b932008-12-14 23:45:27 -05001261static u64 prio_low = ~0ULL/2;
1262static u64 prio_high = ~0ULL/2 - 1;
1263
Amy Griffisf368c07d2006-04-07 16:55:56 -04001264/* Add rule to given filterlist if not a duplicate. */
1265static inline int audit_add_rule(struct audit_entry *entry,
1266 struct list_head *list)
1267{
1268 struct audit_entry *e;
1269 struct audit_field *inode_f = entry->rule.inode_f;
1270 struct audit_watch *watch = entry->rule.watch;
Al Viro74c3cbe2007-07-22 08:04:18 -04001271 struct audit_tree *tree = entry->rule.tree;
Jeff Garzik6f686d32007-07-16 21:25:01 -04001272 struct nameidata *ndp = NULL, *ndw = NULL;
1273 int h, err;
Al Viro471a5c72006-07-10 08:29:24 -04001274#ifdef CONFIG_AUDITSYSCALL
1275 int dont_count = 0;
1276
1277 /* If either of these, don't count towards total */
1278 if (entry->rule.listnr == AUDIT_FILTER_USER ||
1279 entry->rule.listnr == AUDIT_FILTER_TYPE)
1280 dont_count = 1;
1281#endif
Amy Griffisf368c07d2006-04-07 16:55:56 -04001282
1283 if (inode_f) {
1284 h = audit_hash_ino(inode_f->val);
1285 list = &audit_inode_hash[h];
1286 }
1287
1288 mutex_lock(&audit_filter_mutex);
1289 e = audit_find_rule(entry, list);
1290 mutex_unlock(&audit_filter_mutex);
1291 if (e) {
1292 err = -EEXIST;
Al Viro74c3cbe2007-07-22 08:04:18 -04001293 /* normally audit_add_tree_rule() will free it on failure */
1294 if (tree)
1295 audit_put_tree(tree);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001296 goto error;
1297 }
1298
1299 /* Avoid calling path_lookup under audit_filter_mutex. */
1300 if (watch) {
1301 err = audit_get_nd(watch->path, &ndp, &ndw);
1302 if (err)
1303 goto error;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001304 }
1305
1306 mutex_lock(&audit_filter_mutex);
1307 if (watch) {
1308 /* audit_filter_mutex is dropped and re-taken during this call */
1309 err = audit_add_watch(&entry->rule, ndp, ndw);
1310 if (err) {
1311 mutex_unlock(&audit_filter_mutex);
1312 goto error;
1313 }
1314 h = audit_hash_ino((u32)watch->ino);
1315 list = &audit_inode_hash[h];
David Woodhousefe7752b2005-12-15 18:33:52 +00001316 }
Al Viro74c3cbe2007-07-22 08:04:18 -04001317 if (tree) {
1318 err = audit_add_tree_rule(&entry->rule);
1319 if (err) {
1320 mutex_unlock(&audit_filter_mutex);
1321 goto error;
1322 }
1323 }
David Woodhousefe7752b2005-12-15 18:33:52 +00001324
Al Viro0590b932008-12-14 23:45:27 -05001325 entry->rule.prio = ~0ULL;
1326 if (entry->rule.listnr == AUDIT_FILTER_EXIT) {
1327 if (entry->rule.flags & AUDIT_FILTER_PREPEND)
1328 entry->rule.prio = ++prio_high;
1329 else
1330 entry->rule.prio = --prio_low;
1331 }
1332
David Woodhousefe7752b2005-12-15 18:33:52 +00001333 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
David Woodhousefe7752b2005-12-15 18:33:52 +00001334 list_add_rcu(&entry->list, list);
Amy Griffis6a2bcee2006-06-02 13:16:01 -04001335 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
David Woodhousefe7752b2005-12-15 18:33:52 +00001336 } else {
1337 list_add_tail_rcu(&entry->list, list);
1338 }
Al Viro471a5c72006-07-10 08:29:24 -04001339#ifdef CONFIG_AUDITSYSCALL
1340 if (!dont_count)
1341 audit_n_rules++;
Amy Griffise54dc242007-03-29 18:01:04 -04001342
1343 if (!audit_match_signal(entry))
1344 audit_signals++;
Al Viro471a5c72006-07-10 08:29:24 -04001345#endif
Amy Griffisf368c07d2006-04-07 16:55:56 -04001346 mutex_unlock(&audit_filter_mutex);
David Woodhousefe7752b2005-12-15 18:33:52 +00001347
Jeff Garzik6f686d32007-07-16 21:25:01 -04001348 audit_put_nd(ndp, ndw); /* NULL args OK */
Amy Griffisf368c07d2006-04-07 16:55:56 -04001349 return 0;
1350
1351error:
Jeff Garzik6f686d32007-07-16 21:25:01 -04001352 audit_put_nd(ndp, ndw); /* NULL args OK */
Amy Griffisf368c07d2006-04-07 16:55:56 -04001353 if (watch)
1354 audit_put_watch(watch); /* tmp watch, matches initial get */
1355 return err;
David Woodhousefe7752b2005-12-15 18:33:52 +00001356}
1357
Amy Griffisf368c07d2006-04-07 16:55:56 -04001358/* Remove an existing rule from filterlist. */
Amy Griffis93315ed2006-02-07 12:05:27 -05001359static inline int audit_del_rule(struct audit_entry *entry,
David Woodhousefe7752b2005-12-15 18:33:52 +00001360 struct list_head *list)
1361{
1362 struct audit_entry *e;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001363 struct audit_field *inode_f = entry->rule.inode_f;
1364 struct audit_watch *watch, *tmp_watch = entry->rule.watch;
Al Viro74c3cbe2007-07-22 08:04:18 -04001365 struct audit_tree *tree = entry->rule.tree;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001366 LIST_HEAD(inotify_list);
1367 int h, ret = 0;
Al Viro471a5c72006-07-10 08:29:24 -04001368#ifdef CONFIG_AUDITSYSCALL
1369 int dont_count = 0;
1370
1371 /* If either of these, don't count towards total */
1372 if (entry->rule.listnr == AUDIT_FILTER_USER ||
1373 entry->rule.listnr == AUDIT_FILTER_TYPE)
1374 dont_count = 1;
1375#endif
David Woodhousefe7752b2005-12-15 18:33:52 +00001376
Amy Griffisf368c07d2006-04-07 16:55:56 -04001377 if (inode_f) {
1378 h = audit_hash_ino(inode_f->val);
1379 list = &audit_inode_hash[h];
1380 }
1381
1382 mutex_lock(&audit_filter_mutex);
1383 e = audit_find_rule(entry, list);
1384 if (!e) {
1385 mutex_unlock(&audit_filter_mutex);
1386 ret = -ENOENT;
1387 goto out;
1388 }
1389
1390 watch = e->rule.watch;
1391 if (watch) {
1392 struct audit_parent *parent = watch->parent;
1393
1394 list_del(&e->rule.rlist);
1395
1396 if (list_empty(&watch->rules)) {
1397 audit_remove_watch(watch);
1398
1399 if (list_empty(&parent->watches)) {
1400 /* Put parent on the inotify un-registration
1401 * list. Grab a reference before releasing
1402 * audit_filter_mutex, to be released in
Al Viro8f7b0ba2008-11-15 01:15:43 +00001403 * audit_inotify_unregister().
1404 * If filesystem is going away, just leave
1405 * the sucker alone, eviction will take
1406 * care of it.
1407 */
1408 if (pin_inotify_watch(&parent->wdata))
1409 list_add(&parent->ilist, &inotify_list);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001410 }
David Woodhousefe7752b2005-12-15 18:33:52 +00001411 }
1412 }
Amy Griffisf368c07d2006-04-07 16:55:56 -04001413
Al Viro74c3cbe2007-07-22 08:04:18 -04001414 if (e->rule.tree)
1415 audit_remove_tree_rule(&e->rule);
1416
Amy Griffisf368c07d2006-04-07 16:55:56 -04001417 list_del_rcu(&e->list);
1418 call_rcu(&e->rcu, audit_free_rule_rcu);
1419
Al Viro471a5c72006-07-10 08:29:24 -04001420#ifdef CONFIG_AUDITSYSCALL
1421 if (!dont_count)
1422 audit_n_rules--;
Amy Griffise54dc242007-03-29 18:01:04 -04001423
1424 if (!audit_match_signal(entry))
1425 audit_signals--;
Al Viro471a5c72006-07-10 08:29:24 -04001426#endif
Amy Griffisf368c07d2006-04-07 16:55:56 -04001427 mutex_unlock(&audit_filter_mutex);
1428
1429 if (!list_empty(&inotify_list))
1430 audit_inotify_unregister(&inotify_list);
1431
1432out:
1433 if (tmp_watch)
1434 audit_put_watch(tmp_watch); /* match initial get */
Al Viro74c3cbe2007-07-22 08:04:18 -04001435 if (tree)
1436 audit_put_tree(tree); /* that's the temporary one */
Amy Griffisf368c07d2006-04-07 16:55:56 -04001437
1438 return ret;
David Woodhousefe7752b2005-12-15 18:33:52 +00001439}
1440
Amy Griffis93315ed2006-02-07 12:05:27 -05001441/* List rules using struct audit_rule. Exists for backward
1442 * compatibility with userspace. */
Al Viro9044e6b2006-05-22 01:09:24 -04001443static void audit_list(int pid, int seq, struct sk_buff_head *q)
David Woodhousefe7752b2005-12-15 18:33:52 +00001444{
Al Viro9044e6b2006-05-22 01:09:24 -04001445 struct sk_buff *skb;
David Woodhousefe7752b2005-12-15 18:33:52 +00001446 struct audit_entry *entry;
1447 int i;
1448
Amy Griffisf368c07d2006-04-07 16:55:56 -04001449 /* This is a blocking read, so use audit_filter_mutex instead of rcu
1450 * iterator to sync with list writers. */
David Woodhousefe7752b2005-12-15 18:33:52 +00001451 for (i=0; i<AUDIT_NR_FILTERS; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -05001452 list_for_each_entry(entry, &audit_filter_list[i], list) {
1453 struct audit_rule *rule;
1454
1455 rule = audit_krule_to_rule(&entry->rule);
1456 if (unlikely(!rule))
1457 break;
Al Viro9044e6b2006-05-22 01:09:24 -04001458 skb = audit_make_reply(pid, seq, AUDIT_LIST, 0, 1,
Amy Griffis93315ed2006-02-07 12:05:27 -05001459 rule, sizeof(*rule));
Al Viro9044e6b2006-05-22 01:09:24 -04001460 if (skb)
1461 skb_queue_tail(q, skb);
Amy Griffis93315ed2006-02-07 12:05:27 -05001462 kfree(rule);
1463 }
David Woodhousefe7752b2005-12-15 18:33:52 +00001464 }
Amy Griffisf368c07d2006-04-07 16:55:56 -04001465 for (i = 0; i < AUDIT_INODE_BUCKETS; i++) {
1466 list_for_each_entry(entry, &audit_inode_hash[i], list) {
1467 struct audit_rule *rule;
1468
1469 rule = audit_krule_to_rule(&entry->rule);
1470 if (unlikely(!rule))
1471 break;
1472 skb = audit_make_reply(pid, seq, AUDIT_LIST, 0, 1,
1473 rule, sizeof(*rule));
1474 if (skb)
1475 skb_queue_tail(q, skb);
1476 kfree(rule);
1477 }
1478 }
Al Viro9044e6b2006-05-22 01:09:24 -04001479 skb = audit_make_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
1480 if (skb)
1481 skb_queue_tail(q, skb);
David Woodhousefe7752b2005-12-15 18:33:52 +00001482}
1483
Amy Griffis93315ed2006-02-07 12:05:27 -05001484/* List rules using struct audit_rule_data. */
Al Viro9044e6b2006-05-22 01:09:24 -04001485static void audit_list_rules(int pid, int seq, struct sk_buff_head *q)
Amy Griffis93315ed2006-02-07 12:05:27 -05001486{
Al Viro9044e6b2006-05-22 01:09:24 -04001487 struct sk_buff *skb;
Amy Griffis93315ed2006-02-07 12:05:27 -05001488 struct audit_entry *e;
1489 int i;
1490
Amy Griffisf368c07d2006-04-07 16:55:56 -04001491 /* This is a blocking read, so use audit_filter_mutex instead of rcu
1492 * iterator to sync with list writers. */
Amy Griffis93315ed2006-02-07 12:05:27 -05001493 for (i=0; i<AUDIT_NR_FILTERS; i++) {
1494 list_for_each_entry(e, &audit_filter_list[i], list) {
1495 struct audit_rule_data *data;
1496
1497 data = audit_krule_to_data(&e->rule);
1498 if (unlikely(!data))
1499 break;
Al Viro9044e6b2006-05-22 01:09:24 -04001500 skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 0, 1,
Amy Griffisf368c07d2006-04-07 16:55:56 -04001501 data, sizeof(*data) + data->buflen);
1502 if (skb)
1503 skb_queue_tail(q, skb);
1504 kfree(data);
1505 }
1506 }
1507 for (i=0; i< AUDIT_INODE_BUCKETS; i++) {
1508 list_for_each_entry(e, &audit_inode_hash[i], list) {
1509 struct audit_rule_data *data;
1510
1511 data = audit_krule_to_data(&e->rule);
1512 if (unlikely(!data))
1513 break;
1514 skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 0, 1,
1515 data, sizeof(*data) + data->buflen);
Al Viro9044e6b2006-05-22 01:09:24 -04001516 if (skb)
1517 skb_queue_tail(q, skb);
Amy Griffis93315ed2006-02-07 12:05:27 -05001518 kfree(data);
1519 }
1520 }
Al Viro9044e6b2006-05-22 01:09:24 -04001521 skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 1, 1, NULL, 0);
1522 if (skb)
1523 skb_queue_tail(q, skb);
Amy Griffis93315ed2006-02-07 12:05:27 -05001524}
1525
Amy Griffis5adc8a62006-06-14 18:45:21 -04001526/* Log rule additions and removals */
Eric Paris25323862008-04-18 10:09:25 -04001527static void audit_log_rule_change(uid_t loginuid, u32 sessionid, u32 sid,
1528 char *action, struct audit_krule *rule,
1529 int res)
Amy Griffis5adc8a62006-06-14 18:45:21 -04001530{
1531 struct audit_buffer *ab;
1532
Eric Paris1a6b9f22008-01-07 17:09:31 -05001533 if (!audit_enabled)
1534 return;
1535
Amy Griffis5adc8a62006-06-14 18:45:21 -04001536 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE);
1537 if (!ab)
1538 return;
Eric Paris25323862008-04-18 10:09:25 -04001539 audit_log_format(ab, "auid=%u ses=%u", loginuid, sessionid);
Amy Griffis5adc8a62006-06-14 18:45:21 -04001540 if (sid) {
1541 char *ctx = NULL;
1542 u32 len;
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001543 if (security_secid_to_secctx(sid, &ctx, &len))
Amy Griffis5adc8a62006-06-14 18:45:21 -04001544 audit_log_format(ab, " ssid=%u", sid);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001545 else {
Amy Griffis5adc8a62006-06-14 18:45:21 -04001546 audit_log_format(ab, " subj=%s", ctx);
Ahmed S. Darwish2a862b32008-03-01 21:54:38 +02001547 security_release_secctx(ctx, len);
1548 }
Amy Griffis5adc8a62006-06-14 18:45:21 -04001549 }
Steve Grubba17b4ad2006-12-14 11:48:47 -05001550 audit_log_format(ab, " op=%s rule key=", action);
Amy Griffis5adc8a62006-06-14 18:45:21 -04001551 if (rule->filterkey)
1552 audit_log_untrustedstring(ab, rule->filterkey);
1553 else
1554 audit_log_format(ab, "(null)");
1555 audit_log_format(ab, " list=%d res=%d", rule->listnr, res);
1556 audit_log_end(ab);
1557}
1558
David Woodhousefe7752b2005-12-15 18:33:52 +00001559/**
1560 * audit_receive_filter - apply all rules to the specified message type
1561 * @type: audit message type
1562 * @pid: target pid for netlink audit messages
1563 * @uid: target uid for netlink audit messages
1564 * @seq: netlink audit message sequence (serial) number
1565 * @data: payload data
Amy Griffis93315ed2006-02-07 12:05:27 -05001566 * @datasz: size of payload data
David Woodhousefe7752b2005-12-15 18:33:52 +00001567 * @loginuid: loginuid of sender
Randy Dunlap9f0aecd2008-05-19 15:09:21 -07001568 * @sessionid: sessionid for netlink audit message
Steve Grubbce29b682006-04-01 18:29:34 -05001569 * @sid: SE Linux Security ID of sender
David Woodhousefe7752b2005-12-15 18:33:52 +00001570 */
1571int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
Eric Paris25323862008-04-18 10:09:25 -04001572 size_t datasz, uid_t loginuid, u32 sessionid, u32 sid)
David Woodhousefe7752b2005-12-15 18:33:52 +00001573{
1574 struct task_struct *tsk;
Al Viro9044e6b2006-05-22 01:09:24 -04001575 struct audit_netlink_list *dest;
Amy Griffis93315ed2006-02-07 12:05:27 -05001576 int err = 0;
1577 struct audit_entry *entry;
David Woodhousefe7752b2005-12-15 18:33:52 +00001578
1579 switch (type) {
1580 case AUDIT_LIST:
Amy Griffis93315ed2006-02-07 12:05:27 -05001581 case AUDIT_LIST_RULES:
David Woodhousefe7752b2005-12-15 18:33:52 +00001582 /* We can't just spew out the rules here because we might fill
1583 * the available socket buffer space and deadlock waiting for
1584 * auditctl to read from it... which isn't ever going to
1585 * happen if we're actually running in the context of auditctl
1586 * trying to _send_ the stuff */
Daniel Walker9ce34212007-10-18 03:06:06 -07001587
Al Viro9044e6b2006-05-22 01:09:24 -04001588 dest = kmalloc(sizeof(struct audit_netlink_list), GFP_KERNEL);
David Woodhousefe7752b2005-12-15 18:33:52 +00001589 if (!dest)
1590 return -ENOMEM;
Al Viro9044e6b2006-05-22 01:09:24 -04001591 dest->pid = pid;
1592 skb_queue_head_init(&dest->q);
David Woodhousefe7752b2005-12-15 18:33:52 +00001593
Amy Griffisf368c07d2006-04-07 16:55:56 -04001594 mutex_lock(&audit_filter_mutex);
Amy Griffis93315ed2006-02-07 12:05:27 -05001595 if (type == AUDIT_LIST)
Al Viro9044e6b2006-05-22 01:09:24 -04001596 audit_list(pid, seq, &dest->q);
Amy Griffis93315ed2006-02-07 12:05:27 -05001597 else
Al Viro9044e6b2006-05-22 01:09:24 -04001598 audit_list_rules(pid, seq, &dest->q);
Amy Griffisf368c07d2006-04-07 16:55:56 -04001599 mutex_unlock(&audit_filter_mutex);
Al Viro9044e6b2006-05-22 01:09:24 -04001600
1601 tsk = kthread_run(audit_send_list, dest, "audit_send_list");
David Woodhousefe7752b2005-12-15 18:33:52 +00001602 if (IS_ERR(tsk)) {
Al Viro9044e6b2006-05-22 01:09:24 -04001603 skb_queue_purge(&dest->q);
David Woodhousefe7752b2005-12-15 18:33:52 +00001604 kfree(dest);
1605 err = PTR_ERR(tsk);
1606 }
1607 break;
1608 case AUDIT_ADD:
Amy Griffis93315ed2006-02-07 12:05:27 -05001609 case AUDIT_ADD_RULE:
1610 if (type == AUDIT_ADD)
1611 entry = audit_rule_to_entry(data);
1612 else
1613 entry = audit_data_to_entry(data, datasz);
1614 if (IS_ERR(entry))
1615 return PTR_ERR(entry);
David Woodhousefe7752b2005-12-15 18:33:52 +00001616
Amy Griffis93315ed2006-02-07 12:05:27 -05001617 err = audit_add_rule(entry,
1618 &audit_filter_list[entry->rule.listnr]);
Eric Paris25323862008-04-18 10:09:25 -04001619 audit_log_rule_change(loginuid, sessionid, sid, "add",
1620 &entry->rule, !err);
Steve Grubb5d330102006-01-09 09:48:17 -05001621
1622 if (err)
Amy Griffis93315ed2006-02-07 12:05:27 -05001623 audit_free_rule(entry);
David Woodhousefe7752b2005-12-15 18:33:52 +00001624 break;
1625 case AUDIT_DEL:
Amy Griffis93315ed2006-02-07 12:05:27 -05001626 case AUDIT_DEL_RULE:
1627 if (type == AUDIT_DEL)
1628 entry = audit_rule_to_entry(data);
1629 else
1630 entry = audit_data_to_entry(data, datasz);
1631 if (IS_ERR(entry))
1632 return PTR_ERR(entry);
David Woodhousefe7752b2005-12-15 18:33:52 +00001633
Amy Griffis93315ed2006-02-07 12:05:27 -05001634 err = audit_del_rule(entry,
1635 &audit_filter_list[entry->rule.listnr]);
Eric Paris25323862008-04-18 10:09:25 -04001636 audit_log_rule_change(loginuid, sessionid, sid, "remove",
1637 &entry->rule, !err);
Steve Grubb5d330102006-01-09 09:48:17 -05001638
Amy Griffis93315ed2006-02-07 12:05:27 -05001639 audit_free_rule(entry);
David Woodhousefe7752b2005-12-15 18:33:52 +00001640 break;
1641 default:
1642 return -EINVAL;
1643 }
1644
1645 return err;
1646}
1647
1648int audit_comparator(const u32 left, const u32 op, const u32 right)
1649{
1650 switch (op) {
1651 case AUDIT_EQUAL:
1652 return (left == right);
1653 case AUDIT_NOT_EQUAL:
1654 return (left != right);
1655 case AUDIT_LESS_THAN:
1656 return (left < right);
1657 case AUDIT_LESS_THAN_OR_EQUAL:
1658 return (left <= right);
1659 case AUDIT_GREATER_THAN:
1660 return (left > right);
1661 case AUDIT_GREATER_THAN_OR_EQUAL:
1662 return (left >= right);
Eric Paris74f23452007-06-04 17:00:14 -04001663 case AUDIT_BIT_MASK:
1664 return (left & right);
1665 case AUDIT_BIT_TEST:
1666 return ((left & right) == right);
David Woodhousefe7752b2005-12-15 18:33:52 +00001667 }
Dustin Kirklandd9d9ec62006-02-16 13:40:01 -06001668 BUG();
1669 return 0;
David Woodhousefe7752b2005-12-15 18:33:52 +00001670}
1671
Amy Griffisf368c07d2006-04-07 16:55:56 -04001672/* Compare given dentry name with last component in given path,
1673 * return of 0 indicates a match. */
Amy Griffis9c937dc2006-06-08 23:19:31 -04001674int audit_compare_dname_path(const char *dname, const char *path,
1675 int *dirlen)
Amy Griffisf368c07d2006-04-07 16:55:56 -04001676{
1677 int dlen, plen;
1678 const char *p;
David Woodhousefe7752b2005-12-15 18:33:52 +00001679
Amy Griffisf368c07d2006-04-07 16:55:56 -04001680 if (!dname || !path)
1681 return 1;
1682
1683 dlen = strlen(dname);
1684 plen = strlen(path);
1685 if (plen < dlen)
1686 return 1;
1687
1688 /* disregard trailing slashes */
1689 p = path + plen - 1;
1690 while ((*p == '/') && (p > path))
1691 p--;
1692
1693 /* find last path component */
1694 p = p - dlen + 1;
1695 if (p < path)
1696 return 1;
1697 else if (p > path) {
1698 if (*--p != '/')
1699 return 1;
1700 else
1701 p++;
1702 }
1703
Amy Griffis9c937dc2006-06-08 23:19:31 -04001704 /* return length of path's directory component */
1705 if (dirlen)
1706 *dirlen = p - path;
Amy Griffisf368c07d2006-04-07 16:55:56 -04001707 return strncmp(p, dname, dlen);
1708}
David Woodhousefe7752b2005-12-15 18:33:52 +00001709
1710static int audit_filter_user_rules(struct netlink_skb_parms *cb,
Amy Griffis93315ed2006-02-07 12:05:27 -05001711 struct audit_krule *rule,
David Woodhousefe7752b2005-12-15 18:33:52 +00001712 enum audit_state *state)
1713{
1714 int i;
1715
1716 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -05001717 struct audit_field *f = &rule->fields[i];
David Woodhousefe7752b2005-12-15 18:33:52 +00001718 int result = 0;
1719
Amy Griffis93315ed2006-02-07 12:05:27 -05001720 switch (f->type) {
David Woodhousefe7752b2005-12-15 18:33:52 +00001721 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -05001722 result = audit_comparator(cb->creds.pid, f->op, f->val);
David Woodhousefe7752b2005-12-15 18:33:52 +00001723 break;
1724 case AUDIT_UID:
Amy Griffis93315ed2006-02-07 12:05:27 -05001725 result = audit_comparator(cb->creds.uid, f->op, f->val);
David Woodhousefe7752b2005-12-15 18:33:52 +00001726 break;
1727 case AUDIT_GID:
Amy Griffis93315ed2006-02-07 12:05:27 -05001728 result = audit_comparator(cb->creds.gid, f->op, f->val);
David Woodhousefe7752b2005-12-15 18:33:52 +00001729 break;
1730 case AUDIT_LOGINUID:
Amy Griffis93315ed2006-02-07 12:05:27 -05001731 result = audit_comparator(cb->loginuid, f->op, f->val);
David Woodhousefe7752b2005-12-15 18:33:52 +00001732 break;
1733 }
1734
1735 if (!result)
1736 return 0;
1737 }
1738 switch (rule->action) {
1739 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
David Woodhousefe7752b2005-12-15 18:33:52 +00001740 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
1741 }
1742 return 1;
1743}
1744
Peng Haitaod8de7242008-05-20 09:13:02 +08001745int audit_filter_user(struct netlink_skb_parms *cb)
David Woodhousefe7752b2005-12-15 18:33:52 +00001746{
Ingo Molnar11f57ce2007-02-10 01:46:09 -08001747 enum audit_state state = AUDIT_DISABLED;
David Woodhousefe7752b2005-12-15 18:33:52 +00001748 struct audit_entry *e;
David Woodhousefe7752b2005-12-15 18:33:52 +00001749 int ret = 1;
1750
1751 rcu_read_lock();
1752 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
1753 if (audit_filter_user_rules(cb, &e->rule, &state)) {
1754 if (state == AUDIT_DISABLED)
1755 ret = 0;
1756 break;
1757 }
1758 }
1759 rcu_read_unlock();
1760
1761 return ret; /* Audit by default */
1762}
1763
1764int audit_filter_type(int type)
1765{
1766 struct audit_entry *e;
1767 int result = 0;
Daniel Walker9ce34212007-10-18 03:06:06 -07001768
David Woodhousefe7752b2005-12-15 18:33:52 +00001769 rcu_read_lock();
1770 if (list_empty(&audit_filter_list[AUDIT_FILTER_TYPE]))
1771 goto unlock_and_return;
1772
1773 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TYPE],
1774 list) {
David Woodhousefe7752b2005-12-15 18:33:52 +00001775 int i;
Amy Griffis93315ed2006-02-07 12:05:27 -05001776 for (i = 0; i < e->rule.field_count; i++) {
1777 struct audit_field *f = &e->rule.fields[i];
1778 if (f->type == AUDIT_MSGTYPE) {
1779 result = audit_comparator(type, f->op, f->val);
David Woodhousefe7752b2005-12-15 18:33:52 +00001780 if (!result)
1781 break;
1782 }
1783 }
1784 if (result)
1785 goto unlock_and_return;
1786 }
1787unlock_and_return:
1788 rcu_read_unlock();
1789 return result;
1790}
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001791
Al Viro1a9d0792008-12-14 12:04:02 -05001792static int update_lsm_rule(struct audit_entry *entry)
1793{
1794 struct audit_entry *nentry;
1795 struct audit_watch *watch;
1796 struct audit_tree *tree;
1797 int err = 0;
1798
1799 if (!security_audit_rule_known(&entry->rule))
1800 return 0;
1801
1802 watch = entry->rule.watch;
1803 tree = entry->rule.tree;
1804 nentry = audit_dupe_rule(&entry->rule, watch);
1805 if (IS_ERR(nentry)) {
1806 /* save the first error encountered for the
1807 * return value */
1808 err = PTR_ERR(nentry);
1809 audit_panic("error updating LSM filters");
1810 if (watch)
1811 list_del(&entry->rule.rlist);
1812 list_del_rcu(&entry->list);
1813 } else {
1814 if (watch) {
1815 list_add(&nentry->rule.rlist, &watch->rules);
1816 list_del(&entry->rule.rlist);
1817 } else if (tree)
1818 list_replace_init(&entry->rule.rlist,
1819 &nentry->rule.rlist);
1820 list_replace_rcu(&entry->list, &nentry->list);
1821 }
1822 call_rcu(&entry->rcu, audit_free_rule_rcu);
1823
1824 return err;
1825}
1826
Ahmed S. Darwish04305e42008-04-19 09:59:43 +10001827/* This function will re-initialize the lsm_rule field of all applicable rules.
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +02001828 * It will traverse the filter lists serarching for rules that contain LSM
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001829 * specific filter fields. When such a rule is found, it is copied, the
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +02001830 * LSM field is re-initialized, and the old rule is replaced with the
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001831 * updated rule. */
Ahmed S. Darwishd7a96f32008-03-01 22:01:11 +02001832int audit_update_lsm_rules(void)
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001833{
Al Viro1a9d0792008-12-14 12:04:02 -05001834 struct audit_entry *e, *n;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001835 int i, err = 0;
1836
Amy Griffisf368c07d2006-04-07 16:55:56 -04001837 /* audit_filter_mutex synchronizes the writers */
1838 mutex_lock(&audit_filter_mutex);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001839
1840 for (i = 0; i < AUDIT_NR_FILTERS; i++) {
Al Viro1a9d0792008-12-14 12:04:02 -05001841 list_for_each_entry_safe(e, n, &audit_filter_list[i], list) {
1842 int res = update_lsm_rule(e);
1843 if (!err)
1844 err = res;
1845 }
1846 }
1847 for (i=0; i< AUDIT_INODE_BUCKETS; i++) {
1848 list_for_each_entry_safe(e, n, &audit_inode_hash[i], list) {
1849 int res = update_lsm_rule(e);
1850 if (!err)
1851 err = res;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001852 }
1853 }
1854
Amy Griffisf368c07d2006-04-07 16:55:56 -04001855 mutex_unlock(&audit_filter_mutex);
Darrel Goeddel3dc7e312006-03-10 18:14:06 -06001856
1857 return err;
1858}
Amy Griffisf368c07d2006-04-07 16:55:56 -04001859
1860/* Update watch data in audit rules based on inotify events. */
1861void audit_handle_ievent(struct inotify_watch *i_watch, u32 wd, u32 mask,
1862 u32 cookie, const char *dname, struct inode *inode)
1863{
1864 struct audit_parent *parent;
1865
1866 parent = container_of(i_watch, struct audit_parent, wdata);
1867
1868 if (mask & (IN_CREATE|IN_MOVED_TO) && inode)
1869 audit_update_watch(parent, dname, inode->i_sb->s_dev,
1870 inode->i_ino, 0);
1871 else if (mask & (IN_DELETE|IN_MOVED_FROM))
1872 audit_update_watch(parent, dname, (dev_t)-1, (unsigned long)-1, 1);
1873 /* inotify automatically removes the watch and sends IN_IGNORED */
1874 else if (mask & (IN_DELETE_SELF|IN_UNMOUNT))
1875 audit_remove_parent_watches(parent);
1876 /* inotify does not remove the watch, so remove it manually */
1877 else if(mask & IN_MOVE_SELF) {
1878 audit_remove_parent_watches(parent);
1879 inotify_remove_watch_locked(audit_ih, i_watch);
1880 } else if (mask & IN_IGNORED)
1881 put_inotify_watch(i_watch);
1882}