[PATCH] SE Linux audit events
[linux-block.git] / kernel / auditfilter.c
CommitLineData
fe7752ba
DW
1/* 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>
26#include "audit.h"
27
28/* There are three lists of rules -- one to search at task creation
29 * time, one to search at syscall entry time, and another to search at
30 * syscall exit time. */
31struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
32 LIST_HEAD_INIT(audit_filter_list[0]),
33 LIST_HEAD_INIT(audit_filter_list[1]),
34 LIST_HEAD_INIT(audit_filter_list[2]),
35 LIST_HEAD_INIT(audit_filter_list[3]),
36 LIST_HEAD_INIT(audit_filter_list[4]),
37 LIST_HEAD_INIT(audit_filter_list[5]),
38#if AUDIT_NR_FILTERS != 6
39#error Fix audit_filter_list initialiser
40#endif
41};
42
43/* Copy rule from user-space to kernel-space. Called from
44 * audit_add_rule during AUDIT_ADD. */
45static inline int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
46{
47 int i;
48
49 if (s->action != AUDIT_NEVER
50 && s->action != AUDIT_POSSIBLE
51 && s->action != AUDIT_ALWAYS)
52 return -1;
53 if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
54 return -1;
55 if ((s->flags & ~AUDIT_FILTER_PREPEND) >= AUDIT_NR_FILTERS)
56 return -1;
57
58 d->flags = s->flags;
59 d->action = s->action;
60 d->field_count = s->field_count;
61 for (i = 0; i < d->field_count; i++) {
62 d->fields[i] = s->fields[i];
63 d->values[i] = s->values[i];
64 }
65 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
66 return 0;
67}
68
69/* Check to see if two rules are identical. It is called from
70 * audit_add_rule during AUDIT_ADD and
71 * audit_del_rule during AUDIT_DEL. */
d884596f 72static int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
fe7752ba
DW
73{
74 int i;
75
76 if (a->flags != b->flags)
77 return 1;
78
79 if (a->action != b->action)
80 return 1;
81
82 if (a->field_count != b->field_count)
83 return 1;
84
85 for (i = 0; i < a->field_count; i++) {
86 if (a->fields[i] != b->fields[i]
87 || a->values[i] != b->values[i])
88 return 1;
89 }
90
91 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
92 if (a->mask[i] != b->mask[i])
93 return 1;
94
95 return 0;
96}
97
98/* Note that audit_add_rule and audit_del_rule are called via
99 * audit_receive() in audit.c, and are protected by
100 * audit_netlink_sem. */
101static inline int audit_add_rule(struct audit_rule *rule,
102 struct list_head *list)
103{
104 struct audit_entry *entry;
105 int i;
106
107 /* Do not use the _rcu iterator here, since this is the only
108 * addition routine. */
109 list_for_each_entry(entry, list, list) {
d884596f 110 if (!audit_compare_rule(rule, &entry->rule))
fe7752ba 111 return -EEXIST;
fe7752ba
DW
112 }
113
114 for (i = 0; i < rule->field_count; i++) {
115 if (rule->fields[i] & AUDIT_UNUSED_BITS)
116 return -EINVAL;
d884596f 117 if ( rule->fields[i] & AUDIT_NEGATE)
fe7752ba
DW
118 rule->fields[i] |= AUDIT_NOT_EQUAL;
119 else if ( (rule->fields[i] & AUDIT_OPERATORS) == 0 )
120 rule->fields[i] |= AUDIT_EQUAL;
d884596f 121 rule->fields[i] &= ~AUDIT_NEGATE;
fe7752ba
DW
122 }
123
124 if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
125 return -ENOMEM;
126 if (audit_copy_rule(&entry->rule, rule)) {
127 kfree(entry);
128 return -EINVAL;
129 }
130
131 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
132 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
133 list_add_rcu(&entry->list, list);
134 } else {
135 list_add_tail_rcu(&entry->list, list);
136 }
137
138 return 0;
139}
140
141static inline void audit_free_rule(struct rcu_head *head)
142{
143 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
144 kfree(e);
145}
146
147/* Note that audit_add_rule and audit_del_rule are called via
148 * audit_receive() in audit.c, and are protected by
149 * audit_netlink_sem. */
150static inline int audit_del_rule(struct audit_rule *rule,
151 struct list_head *list)
152{
153 struct audit_entry *e;
154
155 /* Do not use the _rcu iterator here, since this is the only
156 * deletion routine. */
157 list_for_each_entry(e, list, list) {
158 if (!audit_compare_rule(rule, &e->rule)) {
159 list_del_rcu(&e->list);
160 call_rcu(&e->rcu, audit_free_rule);
161 return 0;
162 }
163 }
164 return -ENOENT; /* No matching rule */
165}
166
167static int audit_list_rules(void *_dest)
168{
169 int pid, seq;
170 int *dest = _dest;
171 struct audit_entry *entry;
172 int i;
173
174 pid = dest[0];
175 seq = dest[1];
176 kfree(dest);
177
178 down(&audit_netlink_sem);
179
180 /* The *_rcu iterators not needed here because we are
181 always called with audit_netlink_sem held. */
182 for (i=0; i<AUDIT_NR_FILTERS; i++) {
183 list_for_each_entry(entry, &audit_filter_list[i], list)
184 audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
185 &entry->rule, sizeof(entry->rule));
186 }
187 audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
188
189 up(&audit_netlink_sem);
190 return 0;
191}
192
193/**
194 * audit_receive_filter - apply all rules to the specified message type
195 * @type: audit message type
196 * @pid: target pid for netlink audit messages
197 * @uid: target uid for netlink audit messages
198 * @seq: netlink audit message sequence (serial) number
199 * @data: payload data
200 * @loginuid: loginuid of sender
201 */
202int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
203 uid_t loginuid)
204{
205 struct task_struct *tsk;
206 int *dest;
207 int err = 0;
208 unsigned listnr;
209
210 switch (type) {
211 case AUDIT_LIST:
212 /* We can't just spew out the rules here because we might fill
213 * the available socket buffer space and deadlock waiting for
214 * auditctl to read from it... which isn't ever going to
215 * happen if we're actually running in the context of auditctl
216 * trying to _send_ the stuff */
217
218 dest = kmalloc(2 * sizeof(int), GFP_KERNEL);
219 if (!dest)
220 return -ENOMEM;
221 dest[0] = pid;
222 dest[1] = seq;
223
224 tsk = kthread_run(audit_list_rules, dest, "audit_list_rules");
225 if (IS_ERR(tsk)) {
226 kfree(dest);
227 err = PTR_ERR(tsk);
228 }
229 break;
230 case AUDIT_ADD:
231 listnr = ((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
232 switch(listnr) {
233 default:
234 return -EINVAL;
235
236 case AUDIT_FILTER_USER:
237 case AUDIT_FILTER_TYPE:
238#ifdef CONFIG_AUDITSYSCALL
239 case AUDIT_FILTER_ENTRY:
240 case AUDIT_FILTER_EXIT:
241 case AUDIT_FILTER_TASK:
242#endif
243 ;
244 }
245 err = audit_add_rule(data, &audit_filter_list[listnr]);
246 if (!err)
247 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
248 "auid=%u added an audit rule\n", loginuid);
249 break;
250 case AUDIT_DEL:
251 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
252 if (listnr >= AUDIT_NR_FILTERS)
253 return -EINVAL;
254
255 err = audit_del_rule(data, &audit_filter_list[listnr]);
256 if (!err)
257 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
258 "auid=%u removed an audit rule\n", loginuid);
259 break;
260 default:
261 return -EINVAL;
262 }
263
264 return err;
265}
266
267int audit_comparator(const u32 left, const u32 op, const u32 right)
268{
269 switch (op) {
270 case AUDIT_EQUAL:
271 return (left == right);
272 case AUDIT_NOT_EQUAL:
273 return (left != right);
274 case AUDIT_LESS_THAN:
275 return (left < right);
276 case AUDIT_LESS_THAN_OR_EQUAL:
277 return (left <= right);
278 case AUDIT_GREATER_THAN:
279 return (left > right);
280 case AUDIT_GREATER_THAN_OR_EQUAL:
281 return (left >= right);
282 default:
283 return -EINVAL;
284 }
285}
286
287
288
289static int audit_filter_user_rules(struct netlink_skb_parms *cb,
290 struct audit_rule *rule,
291 enum audit_state *state)
292{
293 int i;
294
295 for (i = 0; i < rule->field_count; i++) {
296 u32 field = rule->fields[i] & ~AUDIT_OPERATORS;
297 u32 op = rule->fields[i] & AUDIT_OPERATORS;
298 u32 value = rule->values[i];
299 int result = 0;
300
301 switch (field) {
302 case AUDIT_PID:
303 result = audit_comparator(cb->creds.pid, op, value);
304 break;
305 case AUDIT_UID:
306 result = audit_comparator(cb->creds.uid, op, value);
307 break;
308 case AUDIT_GID:
309 result = audit_comparator(cb->creds.gid, op, value);
310 break;
311 case AUDIT_LOGINUID:
312 result = audit_comparator(cb->loginuid, op, value);
313 break;
314 }
315
316 if (!result)
317 return 0;
318 }
319 switch (rule->action) {
320 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
321 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
322 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
323 }
324 return 1;
325}
326
327int audit_filter_user(struct netlink_skb_parms *cb, int type)
328{
329 struct audit_entry *e;
330 enum audit_state state;
331 int ret = 1;
332
333 rcu_read_lock();
334 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
335 if (audit_filter_user_rules(cb, &e->rule, &state)) {
336 if (state == AUDIT_DISABLED)
337 ret = 0;
338 break;
339 }
340 }
341 rcu_read_unlock();
342
343 return ret; /* Audit by default */
344}
345
346int audit_filter_type(int type)
347{
348 struct audit_entry *e;
349 int result = 0;
350
351 rcu_read_lock();
352 if (list_empty(&audit_filter_list[AUDIT_FILTER_TYPE]))
353 goto unlock_and_return;
354
355 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TYPE],
356 list) {
357 struct audit_rule *rule = &e->rule;
358 int i;
359 for (i = 0; i < rule->field_count; i++) {
360 u32 field = rule->fields[i] & ~AUDIT_OPERATORS;
361 u32 op = rule->fields[i] & AUDIT_OPERATORS;
362 u32 value = rule->values[i];
363 if ( field == AUDIT_MSGTYPE ) {
364 result = audit_comparator(type, op, value);
365 if (!result)
366 break;
367 }
368 }
369 if (result)
370 goto unlock_and_return;
371 }
372unlock_and_return:
373 rcu_read_unlock();
374 return result;
375}