blob: b3fccd6808f90e00e99b64d25ee82c8ef6e12bd4 [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>
25#include <linux/netlink.h>
Darrel Goeddel3dc7e312006-03-10 18:14:06 -060026#include <linux/selinux.h>
David Woodhousefe7752b2005-12-15 18:33:52 +000027#include "audit.h"
28
29/* There are three lists of rules -- one to search at task creation
30 * time, one to search at syscall entry time, and another to search at
31 * syscall exit time. */
32struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
33 LIST_HEAD_INIT(audit_filter_list[0]),
34 LIST_HEAD_INIT(audit_filter_list[1]),
35 LIST_HEAD_INIT(audit_filter_list[2]),
36 LIST_HEAD_INIT(audit_filter_list[3]),
37 LIST_HEAD_INIT(audit_filter_list[4]),
38 LIST_HEAD_INIT(audit_filter_list[5]),
39#if AUDIT_NR_FILTERS != 6
40#error Fix audit_filter_list initialiser
41#endif
42};
43
Amy Griffis93315ed2006-02-07 12:05:27 -050044static inline void audit_free_rule(struct audit_entry *e)
David Woodhousefe7752b2005-12-15 18:33:52 +000045{
Darrel Goeddel3dc7e312006-03-10 18:14:06 -060046 int i;
47 if (e->rule.fields)
48 for (i = 0; i < e->rule.field_count; i++) {
49 struct audit_field *f = &e->rule.fields[i];
50 kfree(f->se_str);
51 selinux_audit_rule_free(f->se_rule);
52 }
Amy Griffis93315ed2006-02-07 12:05:27 -050053 kfree(e->rule.fields);
54 kfree(e);
David Woodhousefe7752b2005-12-15 18:33:52 +000055}
56
Amy Griffis93315ed2006-02-07 12:05:27 -050057static inline void audit_free_rule_rcu(struct rcu_head *head)
58{
59 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
60 audit_free_rule(e);
61}
62
Darrel Goeddel3dc7e312006-03-10 18:14:06 -060063/* Initialize an audit filterlist entry. */
64static inline struct audit_entry *audit_init_entry(u32 field_count)
65{
66 struct audit_entry *entry;
67 struct audit_field *fields;
68
69 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
70 if (unlikely(!entry))
71 return NULL;
72
73 fields = kzalloc(sizeof(*fields) * field_count, GFP_KERNEL);
74 if (unlikely(!fields)) {
75 kfree(entry);
76 return NULL;
77 }
78 entry->rule.fields = fields;
79
80 return entry;
81}
82
Amy Griffis93315ed2006-02-07 12:05:27 -050083/* Unpack a filter field's string representation from user-space
84 * buffer. */
Darrel Goeddel3dc7e312006-03-10 18:14:06 -060085static char *audit_unpack_string(void **bufp, size_t *remain, size_t len)
Amy Griffis93315ed2006-02-07 12:05:27 -050086{
87 char *str;
88
89 if (!*bufp || (len == 0) || (len > *remain))
90 return ERR_PTR(-EINVAL);
91
92 /* Of the currently implemented string fields, PATH_MAX
93 * defines the longest valid length.
94 */
95 if (len > PATH_MAX)
96 return ERR_PTR(-ENAMETOOLONG);
97
98 str = kmalloc(len + 1, GFP_KERNEL);
99 if (unlikely(!str))
100 return ERR_PTR(-ENOMEM);
101
102 memcpy(str, *bufp, len);
103 str[len] = 0;
104 *bufp += len;
105 *remain -= len;
106
107 return str;
108}
109
110/* Common user-space to kernel rule translation. */
111static inline struct audit_entry *audit_to_entry_common(struct audit_rule *rule)
112{
113 unsigned listnr;
114 struct audit_entry *entry;
Amy Griffis93315ed2006-02-07 12:05:27 -0500115 int i, err;
116
117 err = -EINVAL;
118 listnr = rule->flags & ~AUDIT_FILTER_PREPEND;
119 switch(listnr) {
120 default:
121 goto exit_err;
122 case AUDIT_FILTER_USER:
123 case AUDIT_FILTER_TYPE:
124#ifdef CONFIG_AUDITSYSCALL
125 case AUDIT_FILTER_ENTRY:
126 case AUDIT_FILTER_EXIT:
127 case AUDIT_FILTER_TASK:
128#endif
129 ;
130 }
131 if (rule->action != AUDIT_NEVER && rule->action != AUDIT_POSSIBLE &&
132 rule->action != AUDIT_ALWAYS)
133 goto exit_err;
134 if (rule->field_count > AUDIT_MAX_FIELDS)
135 goto exit_err;
136
137 err = -ENOMEM;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600138 entry = audit_init_entry(rule->field_count);
139 if (!entry)
Amy Griffis93315ed2006-02-07 12:05:27 -0500140 goto exit_err;
Amy Griffis93315ed2006-02-07 12:05:27 -0500141
142 entry->rule.flags = rule->flags & AUDIT_FILTER_PREPEND;
143 entry->rule.listnr = listnr;
144 entry->rule.action = rule->action;
145 entry->rule.field_count = rule->field_count;
Amy Griffis93315ed2006-02-07 12:05:27 -0500146
147 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
148 entry->rule.mask[i] = rule->mask[i];
149
150 return entry;
151
152exit_err:
153 return ERR_PTR(err);
154}
155
156/* Translate struct audit_rule to kernel's rule respresentation.
157 * Exists for backward compatibility with userspace. */
158static struct audit_entry *audit_rule_to_entry(struct audit_rule *rule)
159{
160 struct audit_entry *entry;
161 int err = 0;
162 int i;
163
164 entry = audit_to_entry_common(rule);
165 if (IS_ERR(entry))
166 goto exit_nofree;
167
168 for (i = 0; i < rule->field_count; i++) {
169 struct audit_field *f = &entry->rule.fields[i];
170
Amy Griffis93315ed2006-02-07 12:05:27 -0500171 f->op = rule->fields[i] & (AUDIT_NEGATE|AUDIT_OPERATORS);
172 f->type = rule->fields[i] & ~(AUDIT_NEGATE|AUDIT_OPERATORS);
173 f->val = rule->values[i];
174
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600175 if (f->type & AUDIT_UNUSED_BITS ||
176 f->type == AUDIT_SE_USER ||
177 f->type == AUDIT_SE_ROLE ||
178 f->type == AUDIT_SE_TYPE ||
179 f->type == AUDIT_SE_SEN ||
180 f->type == AUDIT_SE_CLR) {
181 err = -EINVAL;
182 goto exit_free;
183 }
184
Amy Griffis93315ed2006-02-07 12:05:27 -0500185 entry->rule.vers_ops = (f->op & AUDIT_OPERATORS) ? 2 : 1;
Dustin Kirklandd9d9ec62006-02-16 13:40:01 -0600186
187 /* Support for legacy operators where
188 * AUDIT_NEGATE bit signifies != and otherwise assumes == */
Amy Griffis93315ed2006-02-07 12:05:27 -0500189 if (f->op & AUDIT_NEGATE)
Dustin Kirklandd9d9ec62006-02-16 13:40:01 -0600190 f->op = AUDIT_NOT_EQUAL;
191 else if (!f->op)
192 f->op = AUDIT_EQUAL;
193 else if (f->op == AUDIT_OPERATORS) {
194 err = -EINVAL;
195 goto exit_free;
196 }
Amy Griffis93315ed2006-02-07 12:05:27 -0500197 }
198
199exit_nofree:
200 return entry;
201
202exit_free:
203 audit_free_rule(entry);
204 return ERR_PTR(err);
205}
206
207/* Translate struct audit_rule_data to kernel's rule respresentation. */
208static struct audit_entry *audit_data_to_entry(struct audit_rule_data *data,
209 size_t datasz)
210{
211 int err = 0;
212 struct audit_entry *entry;
213 void *bufp;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600214 size_t remain = datasz - sizeof(struct audit_rule_data);
Amy Griffis93315ed2006-02-07 12:05:27 -0500215 int i;
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600216 char *str;
Amy Griffis93315ed2006-02-07 12:05:27 -0500217
218 entry = audit_to_entry_common((struct audit_rule *)data);
219 if (IS_ERR(entry))
220 goto exit_nofree;
221
222 bufp = data->buf;
223 entry->rule.vers_ops = 2;
224 for (i = 0; i < data->field_count; i++) {
225 struct audit_field *f = &entry->rule.fields[i];
226
227 err = -EINVAL;
228 if (!(data->fieldflags[i] & AUDIT_OPERATORS) ||
229 data->fieldflags[i] & ~AUDIT_OPERATORS)
230 goto exit_free;
231
232 f->op = data->fieldflags[i] & AUDIT_OPERATORS;
233 f->type = data->fields[i];
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600234 f->val = data->values[i];
235 f->se_str = NULL;
236 f->se_rule = NULL;
Amy Griffis93315ed2006-02-07 12:05:27 -0500237 switch(f->type) {
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600238 case AUDIT_SE_USER:
239 case AUDIT_SE_ROLE:
240 case AUDIT_SE_TYPE:
241 case AUDIT_SE_SEN:
242 case AUDIT_SE_CLR:
243 str = audit_unpack_string(&bufp, &remain, f->val);
244 if (IS_ERR(str))
245 goto exit_free;
246 entry->rule.buflen += f->val;
247
248 err = selinux_audit_rule_init(f->type, f->op, str,
249 &f->se_rule);
250 /* Keep currently invalid fields around in case they
251 * become valid after a policy reload. */
252 if (err == -EINVAL) {
253 printk(KERN_WARNING "audit rule for selinux "
254 "\'%s\' is invalid\n", str);
255 err = 0;
256 }
257 if (err) {
258 kfree(str);
259 goto exit_free;
260 } else
261 f->se_str = str;
262 break;
Amy Griffis93315ed2006-02-07 12:05:27 -0500263 }
264 }
265
266exit_nofree:
267 return entry;
268
269exit_free:
270 audit_free_rule(entry);
271 return ERR_PTR(err);
272}
273
274/* Pack a filter field's string representation into data block. */
275static inline size_t audit_pack_string(void **bufp, char *str)
276{
277 size_t len = strlen(str);
278
279 memcpy(*bufp, str, len);
280 *bufp += len;
281
282 return len;
283}
284
285/* Translate kernel rule respresentation to struct audit_rule.
286 * Exists for backward compatibility with userspace. */
287static struct audit_rule *audit_krule_to_rule(struct audit_krule *krule)
288{
289 struct audit_rule *rule;
290 int i;
291
292 rule = kmalloc(sizeof(*rule), GFP_KERNEL);
293 if (unlikely(!rule))
Amy Griffis0a3b4832006-05-02 15:06:01 -0400294 return NULL;
Amy Griffis93315ed2006-02-07 12:05:27 -0500295 memset(rule, 0, sizeof(*rule));
296
297 rule->flags = krule->flags | krule->listnr;
298 rule->action = krule->action;
299 rule->field_count = krule->field_count;
300 for (i = 0; i < rule->field_count; i++) {
301 rule->values[i] = krule->fields[i].val;
302 rule->fields[i] = krule->fields[i].type;
303
304 if (krule->vers_ops == 1) {
305 if (krule->fields[i].op & AUDIT_NOT_EQUAL)
306 rule->fields[i] |= AUDIT_NEGATE;
307 } else {
308 rule->fields[i] |= krule->fields[i].op;
309 }
310 }
311 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) rule->mask[i] = krule->mask[i];
312
313 return rule;
314}
315
316/* Translate kernel rule respresentation to struct audit_rule_data. */
317static struct audit_rule_data *audit_krule_to_data(struct audit_krule *krule)
318{
319 struct audit_rule_data *data;
320 void *bufp;
321 int i;
322
323 data = kmalloc(sizeof(*data) + krule->buflen, GFP_KERNEL);
324 if (unlikely(!data))
Amy Griffis0a3b4832006-05-02 15:06:01 -0400325 return NULL;
Amy Griffis93315ed2006-02-07 12:05:27 -0500326 memset(data, 0, sizeof(*data));
327
328 data->flags = krule->flags | krule->listnr;
329 data->action = krule->action;
330 data->field_count = krule->field_count;
331 bufp = data->buf;
332 for (i = 0; i < data->field_count; i++) {
333 struct audit_field *f = &krule->fields[i];
334
335 data->fields[i] = f->type;
336 data->fieldflags[i] = f->op;
337 switch(f->type) {
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600338 case AUDIT_SE_USER:
339 case AUDIT_SE_ROLE:
340 case AUDIT_SE_TYPE:
341 case AUDIT_SE_SEN:
342 case AUDIT_SE_CLR:
343 data->buflen += data->values[i] =
344 audit_pack_string(&bufp, f->se_str);
345 break;
Amy Griffis93315ed2006-02-07 12:05:27 -0500346 default:
347 data->values[i] = f->val;
348 }
349 }
350 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) data->mask[i] = krule->mask[i];
351
352 return data;
353}
354
355/* Compare two rules in kernel format. Considered success if rules
356 * don't match. */
357static int audit_compare_rule(struct audit_krule *a, struct audit_krule *b)
David Woodhousefe7752b2005-12-15 18:33:52 +0000358{
359 int i;
360
Amy Griffis93315ed2006-02-07 12:05:27 -0500361 if (a->flags != b->flags ||
362 a->listnr != b->listnr ||
363 a->action != b->action ||
364 a->field_count != b->field_count)
David Woodhousefe7752b2005-12-15 18:33:52 +0000365 return 1;
366
367 for (i = 0; i < a->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500368 if (a->fields[i].type != b->fields[i].type ||
369 a->fields[i].op != b->fields[i].op)
David Woodhousefe7752b2005-12-15 18:33:52 +0000370 return 1;
Amy Griffis93315ed2006-02-07 12:05:27 -0500371
372 switch(a->fields[i].type) {
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600373 case AUDIT_SE_USER:
374 case AUDIT_SE_ROLE:
375 case AUDIT_SE_TYPE:
376 case AUDIT_SE_SEN:
377 case AUDIT_SE_CLR:
378 if (strcmp(a->fields[i].se_str, b->fields[i].se_str))
379 return 1;
380 break;
Amy Griffis93315ed2006-02-07 12:05:27 -0500381 default:
382 if (a->fields[i].val != b->fields[i].val)
383 return 1;
384 }
David Woodhousefe7752b2005-12-15 18:33:52 +0000385 }
386
387 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
388 if (a->mask[i] != b->mask[i])
389 return 1;
390
391 return 0;
392}
393
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600394/* Duplicate selinux field information. The se_rule is opaque, so must be
395 * re-initialized. */
396static inline int audit_dupe_selinux_field(struct audit_field *df,
397 struct audit_field *sf)
398{
399 int ret = 0;
400 char *se_str;
401
402 /* our own copy of se_str */
403 se_str = kstrdup(sf->se_str, GFP_KERNEL);
404 if (unlikely(IS_ERR(se_str)))
405 return -ENOMEM;
406 df->se_str = se_str;
407
408 /* our own (refreshed) copy of se_rule */
409 ret = selinux_audit_rule_init(df->type, df->op, df->se_str,
410 &df->se_rule);
411 /* Keep currently invalid fields around in case they
412 * become valid after a policy reload. */
413 if (ret == -EINVAL) {
414 printk(KERN_WARNING "audit rule for selinux \'%s\' is "
415 "invalid\n", df->se_str);
416 ret = 0;
417 }
418
419 return ret;
420}
421
422/* Duplicate an audit rule. This will be a deep copy with the exception
423 * of the watch - that pointer is carried over. The selinux specific fields
424 * will be updated in the copy. The point is to be able to replace the old
425 * rule with the new rule in the filterlist, then free the old rule. */
426static struct audit_entry *audit_dupe_rule(struct audit_krule *old)
427{
428 u32 fcount = old->field_count;
429 struct audit_entry *entry;
430 struct audit_krule *new;
431 int i, err = 0;
432
433 entry = audit_init_entry(fcount);
434 if (unlikely(!entry))
435 return ERR_PTR(-ENOMEM);
436
437 new = &entry->rule;
438 new->vers_ops = old->vers_ops;
439 new->flags = old->flags;
440 new->listnr = old->listnr;
441 new->action = old->action;
442 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
443 new->mask[i] = old->mask[i];
444 new->buflen = old->buflen;
445 new->field_count = old->field_count;
446 memcpy(new->fields, old->fields, sizeof(struct audit_field) * fcount);
447
448 /* deep copy this information, updating the se_rule fields, because
449 * the originals will all be freed when the old rule is freed. */
450 for (i = 0; i < fcount; i++) {
451 switch (new->fields[i].type) {
452 case AUDIT_SE_USER:
453 case AUDIT_SE_ROLE:
454 case AUDIT_SE_TYPE:
455 case AUDIT_SE_SEN:
456 case AUDIT_SE_CLR:
457 err = audit_dupe_selinux_field(&new->fields[i],
458 &old->fields[i]);
459 }
460 if (err) {
461 audit_free_rule(entry);
462 return ERR_PTR(err);
463 }
464 }
465
466 return entry;
467}
468
Amy Griffis93315ed2006-02-07 12:05:27 -0500469/* Add rule to given filterlist if not a duplicate. Protected by
Ingo Molnar5a0bbce2006-03-07 23:51:38 -0800470 * audit_netlink_mutex. */
Amy Griffis93315ed2006-02-07 12:05:27 -0500471static inline int audit_add_rule(struct audit_entry *entry,
David Woodhousefe7752b2005-12-15 18:33:52 +0000472 struct list_head *list)
473{
Amy Griffis93315ed2006-02-07 12:05:27 -0500474 struct audit_entry *e;
David Woodhousefe7752b2005-12-15 18:33:52 +0000475
476 /* Do not use the _rcu iterator here, since this is the only
477 * addition routine. */
Amy Griffis93315ed2006-02-07 12:05:27 -0500478 list_for_each_entry(e, list, list) {
479 if (!audit_compare_rule(&entry->rule, &e->rule))
David Woodhousefe7752b2005-12-15 18:33:52 +0000480 return -EEXIST;
David Woodhousefe7752b2005-12-15 18:33:52 +0000481 }
482
David Woodhousefe7752b2005-12-15 18:33:52 +0000483 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
David Woodhousefe7752b2005-12-15 18:33:52 +0000484 list_add_rcu(&entry->list, list);
485 } else {
486 list_add_tail_rcu(&entry->list, list);
487 }
488
489 return 0;
490}
491
Amy Griffis93315ed2006-02-07 12:05:27 -0500492/* Remove an existing rule from filterlist. Protected by
Ingo Molnar5a0bbce2006-03-07 23:51:38 -0800493 * audit_netlink_mutex. */
Amy Griffis93315ed2006-02-07 12:05:27 -0500494static inline int audit_del_rule(struct audit_entry *entry,
David Woodhousefe7752b2005-12-15 18:33:52 +0000495 struct list_head *list)
496{
497 struct audit_entry *e;
498
499 /* Do not use the _rcu iterator here, since this is the only
500 * deletion routine. */
501 list_for_each_entry(e, list, list) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500502 if (!audit_compare_rule(&entry->rule, &e->rule)) {
David Woodhousefe7752b2005-12-15 18:33:52 +0000503 list_del_rcu(&e->list);
Amy Griffis93315ed2006-02-07 12:05:27 -0500504 call_rcu(&e->rcu, audit_free_rule_rcu);
David Woodhousefe7752b2005-12-15 18:33:52 +0000505 return 0;
506 }
507 }
508 return -ENOENT; /* No matching rule */
509}
510
Amy Griffis93315ed2006-02-07 12:05:27 -0500511/* List rules using struct audit_rule. Exists for backward
512 * compatibility with userspace. */
Al Viro9044e6b2006-05-22 01:09:24 -0400513static void audit_list(int pid, int seq, struct sk_buff_head *q)
David Woodhousefe7752b2005-12-15 18:33:52 +0000514{
Al Viro9044e6b2006-05-22 01:09:24 -0400515 struct sk_buff *skb;
David Woodhousefe7752b2005-12-15 18:33:52 +0000516 struct audit_entry *entry;
517 int i;
518
David Woodhousefe7752b2005-12-15 18:33:52 +0000519 /* The *_rcu iterators not needed here because we are
Ingo Molnar5a0bbce2006-03-07 23:51:38 -0800520 always called with audit_netlink_mutex held. */
David Woodhousefe7752b2005-12-15 18:33:52 +0000521 for (i=0; i<AUDIT_NR_FILTERS; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500522 list_for_each_entry(entry, &audit_filter_list[i], list) {
523 struct audit_rule *rule;
524
525 rule = audit_krule_to_rule(&entry->rule);
526 if (unlikely(!rule))
527 break;
Al Viro9044e6b2006-05-22 01:09:24 -0400528 skb = audit_make_reply(pid, seq, AUDIT_LIST, 0, 1,
Amy Griffis93315ed2006-02-07 12:05:27 -0500529 rule, sizeof(*rule));
Al Viro9044e6b2006-05-22 01:09:24 -0400530 if (skb)
531 skb_queue_tail(q, skb);
Amy Griffis93315ed2006-02-07 12:05:27 -0500532 kfree(rule);
533 }
David Woodhousefe7752b2005-12-15 18:33:52 +0000534 }
Al Viro9044e6b2006-05-22 01:09:24 -0400535 skb = audit_make_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
536 if (skb)
537 skb_queue_tail(q, skb);
David Woodhousefe7752b2005-12-15 18:33:52 +0000538}
539
Amy Griffis93315ed2006-02-07 12:05:27 -0500540/* List rules using struct audit_rule_data. */
Al Viro9044e6b2006-05-22 01:09:24 -0400541static void audit_list_rules(int pid, int seq, struct sk_buff_head *q)
Amy Griffis93315ed2006-02-07 12:05:27 -0500542{
Al Viro9044e6b2006-05-22 01:09:24 -0400543 struct sk_buff *skb;
Amy Griffis93315ed2006-02-07 12:05:27 -0500544 struct audit_entry *e;
545 int i;
546
Amy Griffis93315ed2006-02-07 12:05:27 -0500547 /* The *_rcu iterators not needed here because we are
Ingo Molnar5a0bbce2006-03-07 23:51:38 -0800548 always called with audit_netlink_mutex held. */
Amy Griffis93315ed2006-02-07 12:05:27 -0500549 for (i=0; i<AUDIT_NR_FILTERS; i++) {
550 list_for_each_entry(e, &audit_filter_list[i], list) {
551 struct audit_rule_data *data;
552
553 data = audit_krule_to_data(&e->rule);
554 if (unlikely(!data))
555 break;
Al Viro9044e6b2006-05-22 01:09:24 -0400556 skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 0, 1,
Amy Griffis93315ed2006-02-07 12:05:27 -0500557 data, sizeof(*data));
Al Viro9044e6b2006-05-22 01:09:24 -0400558 if (skb)
559 skb_queue_tail(q, skb);
Amy Griffis93315ed2006-02-07 12:05:27 -0500560 kfree(data);
561 }
562 }
Al Viro9044e6b2006-05-22 01:09:24 -0400563 skb = audit_make_reply(pid, seq, AUDIT_LIST_RULES, 1, 1, NULL, 0);
564 if (skb)
565 skb_queue_tail(q, skb);
Amy Griffis93315ed2006-02-07 12:05:27 -0500566}
567
David Woodhousefe7752b2005-12-15 18:33:52 +0000568/**
569 * audit_receive_filter - apply all rules to the specified message type
570 * @type: audit message type
571 * @pid: target pid for netlink audit messages
572 * @uid: target uid for netlink audit messages
573 * @seq: netlink audit message sequence (serial) number
574 * @data: payload data
Amy Griffis93315ed2006-02-07 12:05:27 -0500575 * @datasz: size of payload data
David Woodhousefe7752b2005-12-15 18:33:52 +0000576 * @loginuid: loginuid of sender
Steve Grubbce29b682006-04-01 18:29:34 -0500577 * @sid: SE Linux Security ID of sender
David Woodhousefe7752b2005-12-15 18:33:52 +0000578 */
579int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
Steve Grubbce29b682006-04-01 18:29:34 -0500580 size_t datasz, uid_t loginuid, u32 sid)
David Woodhousefe7752b2005-12-15 18:33:52 +0000581{
582 struct task_struct *tsk;
Al Viro9044e6b2006-05-22 01:09:24 -0400583 struct audit_netlink_list *dest;
Amy Griffis93315ed2006-02-07 12:05:27 -0500584 int err = 0;
585 struct audit_entry *entry;
David Woodhousefe7752b2005-12-15 18:33:52 +0000586
587 switch (type) {
588 case AUDIT_LIST:
Amy Griffis93315ed2006-02-07 12:05:27 -0500589 case AUDIT_LIST_RULES:
David Woodhousefe7752b2005-12-15 18:33:52 +0000590 /* We can't just spew out the rules here because we might fill
591 * the available socket buffer space and deadlock waiting for
592 * auditctl to read from it... which isn't ever going to
593 * happen if we're actually running in the context of auditctl
594 * trying to _send_ the stuff */
595
Al Viro9044e6b2006-05-22 01:09:24 -0400596 dest = kmalloc(sizeof(struct audit_netlink_list), GFP_KERNEL);
David Woodhousefe7752b2005-12-15 18:33:52 +0000597 if (!dest)
598 return -ENOMEM;
Al Viro9044e6b2006-05-22 01:09:24 -0400599 dest->pid = pid;
600 skb_queue_head_init(&dest->q);
David Woodhousefe7752b2005-12-15 18:33:52 +0000601
Amy Griffis93315ed2006-02-07 12:05:27 -0500602 if (type == AUDIT_LIST)
Al Viro9044e6b2006-05-22 01:09:24 -0400603 audit_list(pid, seq, &dest->q);
Amy Griffis93315ed2006-02-07 12:05:27 -0500604 else
Al Viro9044e6b2006-05-22 01:09:24 -0400605 audit_list_rules(pid, seq, &dest->q);
606
607 tsk = kthread_run(audit_send_list, dest, "audit_send_list");
David Woodhousefe7752b2005-12-15 18:33:52 +0000608 if (IS_ERR(tsk)) {
Al Viro9044e6b2006-05-22 01:09:24 -0400609 skb_queue_purge(&dest->q);
David Woodhousefe7752b2005-12-15 18:33:52 +0000610 kfree(dest);
611 err = PTR_ERR(tsk);
612 }
613 break;
614 case AUDIT_ADD:
Amy Griffis93315ed2006-02-07 12:05:27 -0500615 case AUDIT_ADD_RULE:
616 if (type == AUDIT_ADD)
617 entry = audit_rule_to_entry(data);
618 else
619 entry = audit_data_to_entry(data, datasz);
620 if (IS_ERR(entry))
621 return PTR_ERR(entry);
David Woodhousefe7752b2005-12-15 18:33:52 +0000622
Amy Griffis93315ed2006-02-07 12:05:27 -0500623 err = audit_add_rule(entry,
624 &audit_filter_list[entry->rule.listnr]);
Steve Grubbce29b682006-04-01 18:29:34 -0500625 if (sid) {
626 char *ctx = NULL;
627 u32 len;
628 if (selinux_ctxid_to_string(sid, &ctx, &len)) {
629 /* Maybe call audit_panic? */
630 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
631 "auid=%u ssid=%u add rule to list=%d res=%d",
632 loginuid, sid, entry->rule.listnr, !err);
633 } else
634 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
635 "auid=%u subj=%s add rule to list=%d res=%d",
636 loginuid, ctx, entry->rule.listnr, !err);
637 kfree(ctx);
638 } else
639 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
640 "auid=%u add rule to list=%d res=%d",
641 loginuid, entry->rule.listnr, !err);
Steve Grubb5d330102006-01-09 09:48:17 -0500642
643 if (err)
Amy Griffis93315ed2006-02-07 12:05:27 -0500644 audit_free_rule(entry);
David Woodhousefe7752b2005-12-15 18:33:52 +0000645 break;
646 case AUDIT_DEL:
Amy Griffis93315ed2006-02-07 12:05:27 -0500647 case AUDIT_DEL_RULE:
648 if (type == AUDIT_DEL)
649 entry = audit_rule_to_entry(data);
650 else
651 entry = audit_data_to_entry(data, datasz);
652 if (IS_ERR(entry))
653 return PTR_ERR(entry);
David Woodhousefe7752b2005-12-15 18:33:52 +0000654
Amy Griffis93315ed2006-02-07 12:05:27 -0500655 err = audit_del_rule(entry,
656 &audit_filter_list[entry->rule.listnr]);
Steve Grubbce29b682006-04-01 18:29:34 -0500657
658 if (sid) {
659 char *ctx = NULL;
660 u32 len;
661 if (selinux_ctxid_to_string(sid, &ctx, &len)) {
662 /* Maybe call audit_panic? */
663 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
664 "auid=%u ssid=%u remove rule from list=%d res=%d",
665 loginuid, sid, entry->rule.listnr, !err);
666 } else
667 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
668 "auid=%u subj=%s remove rule from list=%d res=%d",
669 loginuid, ctx, entry->rule.listnr, !err);
670 kfree(ctx);
671 } else
672 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
673 "auid=%u remove rule from list=%d res=%d",
674 loginuid, entry->rule.listnr, !err);
Steve Grubb5d330102006-01-09 09:48:17 -0500675
Amy Griffis93315ed2006-02-07 12:05:27 -0500676 audit_free_rule(entry);
David Woodhousefe7752b2005-12-15 18:33:52 +0000677 break;
678 default:
679 return -EINVAL;
680 }
681
682 return err;
683}
684
685int audit_comparator(const u32 left, const u32 op, const u32 right)
686{
687 switch (op) {
688 case AUDIT_EQUAL:
689 return (left == right);
690 case AUDIT_NOT_EQUAL:
691 return (left != right);
692 case AUDIT_LESS_THAN:
693 return (left < right);
694 case AUDIT_LESS_THAN_OR_EQUAL:
695 return (left <= right);
696 case AUDIT_GREATER_THAN:
697 return (left > right);
698 case AUDIT_GREATER_THAN_OR_EQUAL:
699 return (left >= right);
David Woodhousefe7752b2005-12-15 18:33:52 +0000700 }
Dustin Kirklandd9d9ec62006-02-16 13:40:01 -0600701 BUG();
702 return 0;
David Woodhousefe7752b2005-12-15 18:33:52 +0000703}
704
705
706
707static int audit_filter_user_rules(struct netlink_skb_parms *cb,
Amy Griffis93315ed2006-02-07 12:05:27 -0500708 struct audit_krule *rule,
David Woodhousefe7752b2005-12-15 18:33:52 +0000709 enum audit_state *state)
710{
711 int i;
712
713 for (i = 0; i < rule->field_count; i++) {
Amy Griffis93315ed2006-02-07 12:05:27 -0500714 struct audit_field *f = &rule->fields[i];
David Woodhousefe7752b2005-12-15 18:33:52 +0000715 int result = 0;
716
Amy Griffis93315ed2006-02-07 12:05:27 -0500717 switch (f->type) {
David Woodhousefe7752b2005-12-15 18:33:52 +0000718 case AUDIT_PID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500719 result = audit_comparator(cb->creds.pid, f->op, f->val);
David Woodhousefe7752b2005-12-15 18:33:52 +0000720 break;
721 case AUDIT_UID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500722 result = audit_comparator(cb->creds.uid, f->op, f->val);
David Woodhousefe7752b2005-12-15 18:33:52 +0000723 break;
724 case AUDIT_GID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500725 result = audit_comparator(cb->creds.gid, f->op, f->val);
David Woodhousefe7752b2005-12-15 18:33:52 +0000726 break;
727 case AUDIT_LOGINUID:
Amy Griffis93315ed2006-02-07 12:05:27 -0500728 result = audit_comparator(cb->loginuid, f->op, f->val);
David Woodhousefe7752b2005-12-15 18:33:52 +0000729 break;
730 }
731
732 if (!result)
733 return 0;
734 }
735 switch (rule->action) {
736 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
737 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
738 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
739 }
740 return 1;
741}
742
743int audit_filter_user(struct netlink_skb_parms *cb, int type)
744{
745 struct audit_entry *e;
746 enum audit_state state;
747 int ret = 1;
748
749 rcu_read_lock();
750 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
751 if (audit_filter_user_rules(cb, &e->rule, &state)) {
752 if (state == AUDIT_DISABLED)
753 ret = 0;
754 break;
755 }
756 }
757 rcu_read_unlock();
758
759 return ret; /* Audit by default */
760}
761
762int audit_filter_type(int type)
763{
764 struct audit_entry *e;
765 int result = 0;
766
767 rcu_read_lock();
768 if (list_empty(&audit_filter_list[AUDIT_FILTER_TYPE]))
769 goto unlock_and_return;
770
771 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TYPE],
772 list) {
David Woodhousefe7752b2005-12-15 18:33:52 +0000773 int i;
Amy Griffis93315ed2006-02-07 12:05:27 -0500774 for (i = 0; i < e->rule.field_count; i++) {
775 struct audit_field *f = &e->rule.fields[i];
776 if (f->type == AUDIT_MSGTYPE) {
777 result = audit_comparator(type, f->op, f->val);
David Woodhousefe7752b2005-12-15 18:33:52 +0000778 if (!result)
779 break;
780 }
781 }
782 if (result)
783 goto unlock_and_return;
784 }
785unlock_and_return:
786 rcu_read_unlock();
787 return result;
788}
Darrel Goeddel3dc7e312006-03-10 18:14:06 -0600789
790/* Check to see if the rule contains any selinux fields. Returns 1 if there
791 are selinux fields specified in the rule, 0 otherwise. */
792static inline int audit_rule_has_selinux(struct audit_krule *rule)
793{
794 int i;
795
796 for (i = 0; i < rule->field_count; i++) {
797 struct audit_field *f = &rule->fields[i];
798 switch (f->type) {
799 case AUDIT_SE_USER:
800 case AUDIT_SE_ROLE:
801 case AUDIT_SE_TYPE:
802 case AUDIT_SE_SEN:
803 case AUDIT_SE_CLR:
804 return 1;
805 }
806 }
807
808 return 0;
809}
810
811/* This function will re-initialize the se_rule field of all applicable rules.
812 * It will traverse the filter lists serarching for rules that contain selinux
813 * specific filter fields. When such a rule is found, it is copied, the
814 * selinux field is re-initialized, and the old rule is replaced with the
815 * updated rule. */
816int selinux_audit_rule_update(void)
817{
818 struct audit_entry *entry, *n, *nentry;
819 int i, err = 0;
820
821 /* audit_netlink_mutex synchronizes the writers */
822 mutex_lock(&audit_netlink_mutex);
823
824 for (i = 0; i < AUDIT_NR_FILTERS; i++) {
825 list_for_each_entry_safe(entry, n, &audit_filter_list[i], list) {
826 if (!audit_rule_has_selinux(&entry->rule))
827 continue;
828
829 nentry = audit_dupe_rule(&entry->rule);
830 if (unlikely(IS_ERR(nentry))) {
831 /* save the first error encountered for the
832 * return value */
833 if (!err)
834 err = PTR_ERR(nentry);
835 audit_panic("error updating selinux filters");
836 list_del_rcu(&entry->list);
837 } else {
838 list_replace_rcu(&entry->list, &nentry->list);
839 }
840 call_rcu(&entry->rcu, audit_free_rule_rcu);
841 }
842 }
843
844 mutex_unlock(&audit_netlink_mutex);
845
846 return err;
847}