blob: 31917ac730aff2603ecaf1b4376d241d726fdccd [file] [log] [blame]
85c87212005-04-29 16:23:29 +01001/* auditsc.c -- System-call auditing support
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Handles all system-call specific auditing features.
3 *
4 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
Amy Griffis73241cc2005-11-03 16:00:25 +00005 * Copyright 2005 Hewlett-Packard Development Company, L.P.
Dustin Kirklandb63862f2005-11-03 15:41:46 +00006 * Copyright (C) 2005 IBM Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * All Rights Reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
24 *
25 * Many of the ideas implemented here are from Stephen C. Tweedie,
26 * especially the idea of avoiding a copy by using getname.
27 *
28 * The method for actual interception of syscall entry and exit (not in
29 * this file -- see entry.S) is based on a GPL'd patch written by
30 * okir@suse.de and Copyright 2003 SuSE Linux AG.
31 *
Dustin Kirklandb63862f2005-11-03 15:41:46 +000032 * The support of additional filter rules compares (>, <, >=, <=) was
33 * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
34 *
Amy Griffis73241cc2005-11-03 16:00:25 +000035 * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
36 * filesystem information.
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 */
38
39#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/types.h>
Alan Cox715b49e2006-01-18 17:44:07 -080041#include <asm/atomic.h>
Amy Griffis73241cc2005-11-03 16:00:25 +000042#include <asm/types.h>
43#include <linux/fs.h>
44#include <linux/namei.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/mm.h>
46#include <linux/module.h>
Stephen Smalley01116102005-05-21 00:15:52 +010047#include <linux/mount.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010048#include <linux/socket.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/audit.h>
50#include <linux/personality.h>
51#include <linux/time.h>
David Woodhousef6a789d2005-06-21 16:22:01 +010052#include <linux/kthread.h>
David Woodhouse5bb289b2005-06-24 14:14:05 +010053#include <linux/netlink.h>
David Woodhousef5561962005-07-13 22:47:07 +010054#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/unistd.h>
56
57/* 0 = no checking
58 1 = put_count checking
59 2 = verbose put_count checking
60*/
61#define AUDIT_DEBUG 0
62
63/* No syscall auditing will take place unless audit_enabled != 0. */
64extern int audit_enabled;
65
66/* AUDIT_NAMES is the number of slots we reserve in the audit_context
67 * for saving names from getname(). */
68#define AUDIT_NAMES 20
69
70/* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
71 * audit_context from being used for nameless inodes from
72 * path_lookup. */
73#define AUDIT_NAMES_RESERVED 7
74
75/* At task start time, the audit_state is set in the audit_context using
76 a per-task filter. At syscall entry, the audit_state is augmented by
77 the syscall filter. */
78enum audit_state {
79 AUDIT_DISABLED, /* Do not create per-task audit_context.
80 * No syscall-specific audit records can
81 * be generated. */
82 AUDIT_SETUP_CONTEXT, /* Create the per-task audit_context,
83 * but don't necessarily fill it in at
84 * syscall entry time (i.e., filter
85 * instead). */
86 AUDIT_BUILD_CONTEXT, /* Create the per-task audit_context,
87 * and always fill it in at syscall
88 * entry time. This makes a full
89 * syscall record available if some
90 * other part of the kernel decides it
91 * should be recorded. */
92 AUDIT_RECORD_CONTEXT /* Create the per-task audit_context,
93 * always fill it in at syscall entry
94 * time, and always write out the audit
95 * record at syscall exit time. */
96};
97
98/* When fs/namei.c:getname() is called, we store the pointer in name and
99 * we don't let putname() free it (instead we free all of the saved
100 * pointers at syscall exit time).
101 *
102 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
103struct audit_names {
104 const char *name;
105 unsigned long ino;
Amy Griffis73241cc2005-11-03 16:00:25 +0000106 unsigned long pino;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 dev_t dev;
108 umode_t mode;
109 uid_t uid;
110 gid_t gid;
111 dev_t rdev;
112};
113
114struct audit_aux_data {
115 struct audit_aux_data *next;
116 int type;
117};
118
119#define AUDIT_AUX_IPCPERM 0
120
121struct audit_aux_data_ipcctl {
122 struct audit_aux_data d;
123 struct ipc_perm p;
124 unsigned long qbytes;
125 uid_t uid;
126 gid_t gid;
127 mode_t mode;
128};
129
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100130struct audit_aux_data_socketcall {
131 struct audit_aux_data d;
132 int nargs;
133 unsigned long args[0];
134};
135
136struct audit_aux_data_sockaddr {
137 struct audit_aux_data d;
138 int len;
139 char a[0];
140};
141
Stephen Smalley01116102005-05-21 00:15:52 +0100142struct audit_aux_data_path {
143 struct audit_aux_data d;
144 struct dentry *dentry;
145 struct vfsmount *mnt;
146};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148/* The per-task audit context. */
149struct audit_context {
150 int in_syscall; /* 1 if task is in a syscall */
151 enum audit_state state;
152 unsigned int serial; /* serial number for record */
153 struct timespec ctime; /* time of syscall entry */
154 uid_t loginuid; /* login uid (identity) */
155 int major; /* syscall number */
156 unsigned long argv[4]; /* syscall arguments */
157 int return_valid; /* return code is valid */
2fd6f582005-04-29 16:08:28 +0100158 long return_code;/* syscall return code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 int auditable; /* 1 if record should be written */
160 int name_count;
161 struct audit_names names[AUDIT_NAMES];
David Woodhouse8f37d472005-05-27 12:17:28 +0100162 struct dentry * pwd;
163 struct vfsmount * pwdmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 struct audit_context *previous; /* For nested syscalls */
165 struct audit_aux_data *aux;
166
167 /* Save things to print about task_struct */
168 pid_t pid;
169 uid_t uid, euid, suid, fsuid;
170 gid_t gid, egid, sgid, fsgid;
171 unsigned long personality;
2fd6f582005-04-29 16:08:28 +0100172 int arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174#if AUDIT_DEBUG
175 int put_count;
176 int ino_count;
177#endif
178};
179
180 /* Public API */
181/* There are three lists of rules -- one to search at task creation
182 * time, one to search at syscall entry time, and another to search at
183 * syscall exit time. */
David Woodhouse0f45aa12005-06-19 19:35:50 +0100184static struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
185 LIST_HEAD_INIT(audit_filter_list[0]),
186 LIST_HEAD_INIT(audit_filter_list[1]),
187 LIST_HEAD_INIT(audit_filter_list[2]),
188 LIST_HEAD_INIT(audit_filter_list[3]),
189 LIST_HEAD_INIT(audit_filter_list[4]),
Dustin Kirklandc8edc802005-11-03 16:12:36 +0000190 LIST_HEAD_INIT(audit_filter_list[5]),
191#if AUDIT_NR_FILTERS != 6
David Woodhouse0f45aa12005-06-19 19:35:50 +0100192#error Fix audit_filter_list initialiser
193#endif
194};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196struct audit_entry {
197 struct list_head list;
198 struct rcu_head rcu;
199 struct audit_rule rule;
200};
201
David Woodhouse7ca00262005-05-19 11:23:13 +0100202extern int audit_pid;
203
Amy Griffis3c789a12005-08-17 16:05:35 +0100204/* Copy rule from user-space to kernel-space. Called from
205 * audit_add_rule during AUDIT_ADD. */
206static inline int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
207{
208 int i;
209
210 if (s->action != AUDIT_NEVER
211 && s->action != AUDIT_POSSIBLE
212 && s->action != AUDIT_ALWAYS)
213 return -1;
214 if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
215 return -1;
216 if ((s->flags & ~AUDIT_FILTER_PREPEND) >= AUDIT_NR_FILTERS)
217 return -1;
218
219 d->flags = s->flags;
220 d->action = s->action;
221 d->field_count = s->field_count;
222 for (i = 0; i < d->field_count; i++) {
223 d->fields[i] = s->fields[i];
224 d->values[i] = s->values[i];
225 }
226 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
227 return 0;
228}
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230/* Check to see if two rules are identical. It is called from
Amy Griffis3c789a12005-08-17 16:05:35 +0100231 * audit_add_rule during AUDIT_ADD and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 * audit_del_rule during AUDIT_DEL. */
Amy Griffis3c789a12005-08-17 16:05:35 +0100233static inline int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
235 int i;
236
237 if (a->flags != b->flags)
238 return 1;
239
240 if (a->action != b->action)
241 return 1;
242
243 if (a->field_count != b->field_count)
244 return 1;
245
246 for (i = 0; i < a->field_count; i++) {
247 if (a->fields[i] != b->fields[i]
248 || a->values[i] != b->values[i])
249 return 1;
250 }
251
252 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
253 if (a->mask[i] != b->mask[i])
254 return 1;
255
256 return 0;
257}
258
259/* Note that audit_add_rule and audit_del_rule are called via
260 * audit_receive() in audit.c, and are protected by
261 * audit_netlink_sem. */
Amy Griffis3c789a12005-08-17 16:05:35 +0100262static inline int audit_add_rule(struct audit_rule *rule,
David Woodhouse0f45aa12005-06-19 19:35:50 +0100263 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Amy Griffis3c789a12005-08-17 16:05:35 +0100265 struct audit_entry *entry;
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000266 int i;
Amy Griffis3c789a12005-08-17 16:05:35 +0100267
268 /* Do not use the _rcu iterator here, since this is the only
269 * addition routine. */
270 list_for_each_entry(entry, list, list) {
271 if (!audit_compare_rule(rule, &entry->rule)) {
272 return -EEXIST;
273 }
274 }
275
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000276 for (i = 0; i < rule->field_count; i++) {
277 if (rule->fields[i] & AUDIT_UNUSED_BITS)
278 return -EINVAL;
279 if ( rule->fields[i] & AUDIT_NEGATE )
280 rule->fields[i] |= AUDIT_NOT_EQUAL;
281 else if ( (rule->fields[i] & AUDIT_OPERATORS) == 0 )
282 rule->fields[i] |= AUDIT_EQUAL;
283 rule->fields[i] &= (~AUDIT_NEGATE);
284 }
285
Amy Griffis3c789a12005-08-17 16:05:35 +0100286 if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
287 return -ENOMEM;
288 if (audit_copy_rule(&entry->rule, rule)) {
289 kfree(entry);
290 return -EINVAL;
291 }
292
David Woodhouse0f45aa12005-06-19 19:35:50 +0100293 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
294 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 list_add_rcu(&entry->list, list);
296 } else {
297 list_add_tail_rcu(&entry->list, list);
298 }
Amy Griffis3c789a12005-08-17 16:05:35 +0100299
300 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Amy Griffis3c789a12005-08-17 16:05:35 +0100303static inline void audit_free_rule(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304{
305 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
306 kfree(e);
307}
308
309/* Note that audit_add_rule and audit_del_rule are called via
310 * audit_receive() in audit.c, and are protected by
311 * audit_netlink_sem. */
312static inline int audit_del_rule(struct audit_rule *rule,
313 struct list_head *list)
314{
315 struct audit_entry *e;
316
317 /* Do not use the _rcu iterator here, since this is the only
318 * deletion routine. */
319 list_for_each_entry(e, list, list) {
320 if (!audit_compare_rule(rule, &e->rule)) {
321 list_del_rcu(&e->list);
322 call_rcu(&e->rcu, audit_free_rule);
323 return 0;
324 }
325 }
David Woodhouse0f45aa12005-06-19 19:35:50 +0100326 return -ENOENT; /* No matching rule */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
David Woodhousef6a789d2005-06-21 16:22:01 +0100329static int audit_list_rules(void *_dest)
330{
331 int pid, seq;
332 int *dest = _dest;
333 struct audit_entry *entry;
334 int i;
335
336 pid = dest[0];
337 seq = dest[1];
338 kfree(dest);
339
340 down(&audit_netlink_sem);
341
342 /* The *_rcu iterators not needed here because we are
343 always called with audit_netlink_sem held. */
344 for (i=0; i<AUDIT_NR_FILTERS; i++) {
345 list_for_each_entry(entry, &audit_filter_list[i], list)
346 audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
347 &entry->rule, sizeof(entry->rule));
348 }
349 audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
350
351 up(&audit_netlink_sem);
352 return 0;
353}
354
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700355/**
356 * audit_receive_filter - apply all rules to the specified message type
357 * @type: audit message type
358 * @pid: target pid for netlink audit messages
359 * @uid: target uid for netlink audit messages
360 * @seq: netlink audit message sequence (serial) number
361 * @data: payload data
362 * @loginuid: loginuid of sender
363 */
Serge Hallync94c2572005-04-29 16:27:17 +0100364int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
365 uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
David Woodhousef6a789d2005-06-21 16:22:01 +0100367 struct task_struct *tsk;
368 int *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 int err = 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100370 unsigned listnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 switch (type) {
373 case AUDIT_LIST:
David Woodhousef6a789d2005-06-21 16:22:01 +0100374 /* We can't just spew out the rules here because we might fill
375 * the available socket buffer space and deadlock waiting for
376 * auditctl to read from it... which isn't ever going to
377 * happen if we're actually running in the context of auditctl
378 * trying to _send_ the stuff */
379
380 dest = kmalloc(2 * sizeof(int), GFP_KERNEL);
381 if (!dest)
382 return -ENOMEM;
383 dest[0] = pid;
384 dest[1] = seq;
385
386 tsk = kthread_run(audit_list_rules, dest, "audit_list_rules");
387 if (IS_ERR(tsk)) {
388 kfree(dest);
389 err = PTR_ERR(tsk);
David Woodhouse0f45aa12005-06-19 19:35:50 +0100390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 break;
392 case AUDIT_ADD:
Amy Griffis3c789a12005-08-17 16:05:35 +0100393 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
394 if (listnr >= AUDIT_NR_FILTERS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 return -EINVAL;
Amy Griffis3c789a12005-08-17 16:05:35 +0100396
397 err = audit_add_rule(data, &audit_filter_list[listnr]);
398 if (!err)
399 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
400 "auid=%u added an audit rule\n", loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 break;
402 case AUDIT_DEL:
David Woodhouse0f45aa12005-06-19 19:35:50 +0100403 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
404 if (listnr >= AUDIT_NR_FILTERS)
405 return -EINVAL;
406
407 err = audit_del_rule(data, &audit_filter_list[listnr]);
408 if (!err)
David Woodhouse9ad9ad32005-06-22 15:04:33 +0100409 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
David Woodhouse0f45aa12005-06-19 19:35:50 +0100410 "auid=%u removed an audit rule\n", loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 break;
412 default:
413 return -EINVAL;
414 }
415
416 return err;
417}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000419static int audit_comparator(const u32 left, const u32 op, const u32 right)
420{
421 switch (op) {
422 case AUDIT_EQUAL:
423 return (left == right);
424 case AUDIT_NOT_EQUAL:
425 return (left != right);
426 case AUDIT_LESS_THAN:
427 return (left < right);
428 case AUDIT_LESS_THAN_OR_EQUAL:
429 return (left <= right);
430 case AUDIT_GREATER_THAN:
431 return (left > right);
432 case AUDIT_GREATER_THAN_OR_EQUAL:
433 return (left >= right);
434 default:
435 return -EINVAL;
436 }
437}
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439/* Compare a task_struct with an audit_rule. Return 1 on match, 0
440 * otherwise. */
441static int audit_filter_rules(struct task_struct *tsk,
442 struct audit_rule *rule,
443 struct audit_context *ctx,
444 enum audit_state *state)
445{
446 int i, j;
447
448 for (i = 0; i < rule->field_count; i++) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000449 u32 field = rule->fields[i] & ~AUDIT_OPERATORS;
450 u32 op = rule->fields[i] & AUDIT_OPERATORS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 u32 value = rule->values[i];
452 int result = 0;
453
454 switch (field) {
455 case AUDIT_PID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000456 result = audit_comparator(tsk->pid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 break;
458 case AUDIT_UID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000459 result = audit_comparator(tsk->uid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 break;
461 case AUDIT_EUID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000462 result = audit_comparator(tsk->euid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 break;
464 case AUDIT_SUID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000465 result = audit_comparator(tsk->suid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 break;
467 case AUDIT_FSUID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000468 result = audit_comparator(tsk->fsuid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 break;
470 case AUDIT_GID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000471 result = audit_comparator(tsk->gid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 break;
473 case AUDIT_EGID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000474 result = audit_comparator(tsk->egid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 break;
476 case AUDIT_SGID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000477 result = audit_comparator(tsk->sgid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 break;
479 case AUDIT_FSGID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000480 result = audit_comparator(tsk->fsgid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 break;
482 case AUDIT_PERS:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000483 result = audit_comparator(tsk->personality, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 break;
2fd6f582005-04-29 16:08:28 +0100485 case AUDIT_ARCH:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000486 if (ctx)
487 result = audit_comparator(ctx->arch, op, value);
2fd6f582005-04-29 16:08:28 +0100488 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490 case AUDIT_EXIT:
491 if (ctx && ctx->return_valid)
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000492 result = audit_comparator(ctx->return_code, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 break;
494 case AUDIT_SUCCESS:
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100495 if (ctx && ctx->return_valid) {
496 if (value)
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000497 result = audit_comparator(ctx->return_valid, op, AUDITSC_SUCCESS);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100498 else
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000499 result = audit_comparator(ctx->return_valid, op, AUDITSC_FAILURE);
David Woodhouseb01f2cc2005-08-27 10:25:43 +0100500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 break;
502 case AUDIT_DEVMAJOR:
503 if (ctx) {
504 for (j = 0; j < ctx->name_count; j++) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000505 if (audit_comparator(MAJOR(ctx->names[j].dev), op, value)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 ++result;
507 break;
508 }
509 }
510 }
511 break;
512 case AUDIT_DEVMINOR:
513 if (ctx) {
514 for (j = 0; j < ctx->name_count; j++) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000515 if (audit_comparator(MINOR(ctx->names[j].dev), op, value)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 ++result;
517 break;
518 }
519 }
520 }
521 break;
522 case AUDIT_INODE:
523 if (ctx) {
524 for (j = 0; j < ctx->name_count; j++) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000525 if (audit_comparator(ctx->names[j].ino, op, value) ||
526 audit_comparator(ctx->names[j].pino, op, value)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 ++result;
528 break;
529 }
530 }
531 }
532 break;
533 case AUDIT_LOGINUID:
534 result = 0;
535 if (ctx)
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000536 result = audit_comparator(ctx->loginuid, op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 break;
538 case AUDIT_ARG0:
539 case AUDIT_ARG1:
540 case AUDIT_ARG2:
541 case AUDIT_ARG3:
542 if (ctx)
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000543 result = audit_comparator(ctx->argv[field-AUDIT_ARG0], op, value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 break;
545 }
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (!result)
548 return 0;
549 }
550 switch (rule->action) {
551 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
552 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
553 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
554 }
555 return 1;
556}
557
558/* At process creation time, we can determine if system-call auditing is
559 * completely disabled for this task. Since we only have the task
560 * structure at this point, we can only check uid and gid.
561 */
562static enum audit_state audit_filter_task(struct task_struct *tsk)
563{
564 struct audit_entry *e;
565 enum audit_state state;
566
567 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100568 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
570 rcu_read_unlock();
571 return state;
572 }
573 }
574 rcu_read_unlock();
575 return AUDIT_BUILD_CONTEXT;
576}
577
578/* At syscall entry and exit time, this filter is called if the
579 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100580 * also not high enough that we already know we have to write an audit
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700581 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 */
583static enum audit_state audit_filter_syscall(struct task_struct *tsk,
584 struct audit_context *ctx,
585 struct list_head *list)
586{
587 struct audit_entry *e;
David Woodhousec3896492005-08-17 14:49:57 +0100588 enum audit_state state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
David Woodhouse351bb722005-07-14 14:40:06 +0100590 if (audit_pid && tsk->tgid == audit_pid)
David Woodhousef7056d62005-06-20 16:07:33 +0100591 return AUDIT_DISABLED;
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 rcu_read_lock();
David Woodhousec3896492005-08-17 14:49:57 +0100594 if (!list_empty(list)) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000595 int word = AUDIT_WORD(ctx->major);
596 int bit = AUDIT_BIT(ctx->major);
David Woodhousec3896492005-08-17 14:49:57 +0100597
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000598 list_for_each_entry_rcu(e, list, list) {
599 if ((e->rule.mask[word] & bit) == bit
600 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
601 rcu_read_unlock();
602 return state;
603 }
604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 }
606 rcu_read_unlock();
607 return AUDIT_BUILD_CONTEXT;
608}
609
David Woodhouse5bb289b2005-06-24 14:14:05 +0100610static int audit_filter_user_rules(struct netlink_skb_parms *cb,
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000611 struct audit_rule *rule,
612 enum audit_state *state)
David Woodhouse0f45aa12005-06-19 19:35:50 +0100613{
David Woodhouse5bb289b2005-06-24 14:14:05 +0100614 int i;
615
616 for (i = 0; i < rule->field_count; i++) {
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000617 u32 field = rule->fields[i] & ~AUDIT_OPERATORS;
618 u32 op = rule->fields[i] & AUDIT_OPERATORS;
David Woodhouse5bb289b2005-06-24 14:14:05 +0100619 u32 value = rule->values[i];
620 int result = 0;
621
622 switch (field) {
623 case AUDIT_PID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000624 result = audit_comparator(cb->creds.pid, op, value);
David Woodhouse5bb289b2005-06-24 14:14:05 +0100625 break;
626 case AUDIT_UID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000627 result = audit_comparator(cb->creds.uid, op, value);
David Woodhouse5bb289b2005-06-24 14:14:05 +0100628 break;
629 case AUDIT_GID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000630 result = audit_comparator(cb->creds.gid, op, value);
David Woodhouse5bb289b2005-06-24 14:14:05 +0100631 break;
632 case AUDIT_LOGINUID:
Dustin Kirklandb63862f2005-11-03 15:41:46 +0000633 result = audit_comparator(cb->loginuid, op, value);
David Woodhouse5bb289b2005-06-24 14:14:05 +0100634 break;
635 }
636
David Woodhouse5bb289b2005-06-24 14:14:05 +0100637 if (!result)
638 return 0;
639 }
640 switch (rule->action) {
641 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
642 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
643 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
644 }
645 return 1;
646}
647
648int audit_filter_user(struct netlink_skb_parms *cb, int type)
649{
David Woodhouse0f45aa12005-06-19 19:35:50 +0100650 struct audit_entry *e;
651 enum audit_state state;
David Woodhouse4a4cd632005-06-22 14:56:47 +0100652 int ret = 1;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100653
654 rcu_read_lock();
655 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
David Woodhouse5bb289b2005-06-24 14:14:05 +0100656 if (audit_filter_user_rules(cb, &e->rule, &state)) {
David Woodhouse4a4cd632005-06-22 14:56:47 +0100657 if (state == AUDIT_DISABLED)
658 ret = 0;
659 break;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100660 }
661 }
662 rcu_read_unlock();
David Woodhouse4a4cd632005-06-22 14:56:47 +0100663
David Woodhouse993e2d412005-06-24 08:21:49 +0100664 return ret; /* Audit by default */
David Woodhouse0f45aa12005-06-19 19:35:50 +0100665}
666
Dustin Kirklandc8edc802005-11-03 16:12:36 +0000667int audit_filter_type(int type)
668{
669 struct audit_entry *e;
670 int result = 0;
671
672 rcu_read_lock();
673 if (list_empty(&audit_filter_list[AUDIT_FILTER_TYPE]))
674 goto unlock_and_return;
675
676 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TYPE],
677 list) {
678 struct audit_rule *rule = &e->rule;
679 int i;
680 for (i = 0; i < rule->field_count; i++) {
681 u32 field = rule->fields[i] & ~AUDIT_OPERATORS;
682 u32 op = rule->fields[i] & AUDIT_OPERATORS;
683 u32 value = rule->values[i];
684 if ( field == AUDIT_MSGTYPE ) {
685 result = audit_comparator(type, op, value);
686 if (!result)
687 break;
688 }
689 }
690 if (result)
691 goto unlock_and_return;
692 }
693unlock_and_return:
694 rcu_read_unlock();
695 return result;
696}
697
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699/* This should be called with task_lock() held. */
700static inline struct audit_context *audit_get_context(struct task_struct *tsk,
701 int return_valid,
702 int return_code)
703{
704 struct audit_context *context = tsk->audit_context;
705
706 if (likely(!context))
707 return NULL;
708 context->return_valid = return_valid;
709 context->return_code = return_code;
710
David Woodhouse21af6c42005-07-02 14:10:46 +0100711 if (context->in_syscall && !context->auditable) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 enum audit_state state;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100713 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 if (state == AUDIT_RECORD_CONTEXT)
715 context->auditable = 1;
716 }
717
718 context->pid = tsk->pid;
719 context->uid = tsk->uid;
720 context->gid = tsk->gid;
721 context->euid = tsk->euid;
722 context->suid = tsk->suid;
723 context->fsuid = tsk->fsuid;
724 context->egid = tsk->egid;
725 context->sgid = tsk->sgid;
726 context->fsgid = tsk->fsgid;
727 context->personality = tsk->personality;
728 tsk->audit_context = NULL;
729 return context;
730}
731
732static inline void audit_free_names(struct audit_context *context)
733{
734 int i;
735
736#if AUDIT_DEBUG == 2
737 if (context->auditable
738 ||context->put_count + context->ino_count != context->name_count) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000739 printk(KERN_ERR "%s:%d(:%d): major=%d in_syscall=%d"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 " name_count=%d put_count=%d"
741 " ino_count=%d [NOT freeing]\n",
Amy Griffis73241cc2005-11-03 16:00:25 +0000742 __FILE__, __LINE__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 context->serial, context->major, context->in_syscall,
744 context->name_count, context->put_count,
745 context->ino_count);
746 for (i = 0; i < context->name_count; i++)
747 printk(KERN_ERR "names[%d] = %p = %s\n", i,
748 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +0000749 context->names[i].name ?: "(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 dump_stack();
751 return;
752 }
753#endif
754#if AUDIT_DEBUG
755 context->put_count = 0;
756 context->ino_count = 0;
757#endif
758
759 for (i = 0; i < context->name_count; i++)
760 if (context->names[i].name)
761 __putname(context->names[i].name);
762 context->name_count = 0;
David Woodhouse8f37d472005-05-27 12:17:28 +0100763 if (context->pwd)
764 dput(context->pwd);
765 if (context->pwdmnt)
766 mntput(context->pwdmnt);
767 context->pwd = NULL;
768 context->pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
771static inline void audit_free_aux(struct audit_context *context)
772{
773 struct audit_aux_data *aux;
774
775 while ((aux = context->aux)) {
Stephen Smalley01116102005-05-21 00:15:52 +0100776 if (aux->type == AUDIT_AVC_PATH) {
777 struct audit_aux_data_path *axi = (void *)aux;
778 dput(axi->dentry);
779 mntput(axi->mnt);
780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 context->aux = aux->next;
782 kfree(aux);
783 }
784}
785
786static inline void audit_zero_context(struct audit_context *context,
787 enum audit_state state)
788{
789 uid_t loginuid = context->loginuid;
790
791 memset(context, 0, sizeof(*context));
792 context->state = state;
793 context->loginuid = loginuid;
794}
795
796static inline struct audit_context *audit_alloc_context(enum audit_state state)
797{
798 struct audit_context *context;
799
800 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
801 return NULL;
802 audit_zero_context(context, state);
803 return context;
804}
805
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700806/**
807 * audit_alloc - allocate an audit context block for a task
808 * @tsk: task
809 *
810 * Filter on the task information and allocate a per-task audit context
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 * if necessary. Doing so turns on system call auditing for the
812 * specified task. This is called from copy_process, so no lock is
Randy Dunlapb0dd25a2005-09-13 12:47:11 -0700813 * needed.
814 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815int audit_alloc(struct task_struct *tsk)
816{
817 struct audit_context *context;
818 enum audit_state state;
819
820 if (likely(!audit_enabled))
821 return 0; /* Return if not auditing. */
822
823 state = audit_filter_task(tsk);
824 if (likely(state == AUDIT_DISABLED))
825 return 0;
826
827 if (!(context = audit_alloc_context(state))) {
828 audit_log_lost("out of memory in audit_alloc");
829 return -ENOMEM;
830 }
831
832 /* Preserve login uid */
833 context->loginuid = -1;
834 if (current->audit_context)
835 context->loginuid = current->audit_context->loginuid;
836
837 tsk->audit_context = context;
838 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
839 return 0;
840}
841
842static inline void audit_free_context(struct audit_context *context)
843{
844 struct audit_context *previous;
845 int count = 0;
846
847 do {
848 previous = context->previous;
849 if (previous || (count && count < 10)) {
850 ++count;
851 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
852 " freeing multiple contexts (%d)\n",
853 context->serial, context->major,
854 context->name_count, count);
855 }
856 audit_free_names(context);
857 audit_free_aux(context);
858 kfree(context);
859 context = previous;
860 } while (context);
861 if (count >= 10)
862 printk(KERN_ERR "audit: freed %d contexts\n", count);
863}
864
Stephen Smalley219f0812005-04-18 10:47:35 -0700865static void audit_log_task_info(struct audit_buffer *ab)
866{
867 char name[sizeof(current->comm)];
868 struct mm_struct *mm = current->mm;
869 struct vm_area_struct *vma;
870
871 get_task_comm(name, current);
David Woodhouse99e45ee2005-05-23 21:57:41 +0100872 audit_log_format(ab, " comm=");
873 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700874
875 if (!mm)
876 return;
877
878 down_read(&mm->mmap_sem);
879 vma = mm->mmap;
880 while (vma) {
881 if ((vma->vm_flags & VM_EXECUTABLE) &&
882 vma->vm_file) {
883 audit_log_d_path(ab, "exe=",
884 vma->vm_file->f_dentry,
885 vma->vm_file->f_vfsmnt);
886 break;
887 }
888 vma = vma->vm_next;
889 }
890 up_read(&mm->mmap_sem);
891}
892
Al Viro9796fdd2005-10-21 03:22:03 -0400893static void audit_log_exit(struct audit_context *context, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
895 int i;
896 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +0100897 struct audit_aux_data *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
David Woodhousef5561962005-07-13 22:47:07 +0100899 ab = audit_log_start(context, gfp_mask, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 if (!ab)
901 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +0100902 audit_log_format(ab, "arch=%x syscall=%d",
903 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (context->personality != PER_LINUX)
905 audit_log_format(ab, " per=%lx", context->personality);
906 if (context->return_valid)
2fd6f582005-04-29 16:08:28 +0100907 audit_log_format(ab, " success=%s exit=%ld",
908 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
909 context->return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 audit_log_format(ab,
911 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Steve Grubb326e9c82005-05-21 00:22:31 +0100912 " pid=%d auid=%u uid=%u gid=%u"
913 " euid=%u suid=%u fsuid=%u"
914 " egid=%u sgid=%u fsgid=%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 context->argv[0],
916 context->argv[1],
917 context->argv[2],
918 context->argv[3],
919 context->name_count,
920 context->pid,
921 context->loginuid,
922 context->uid,
923 context->gid,
924 context->euid, context->suid, context->fsuid,
925 context->egid, context->sgid, context->fsgid);
Stephen Smalley219f0812005-04-18 10:47:35 -0700926 audit_log_task_info(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
David Woodhouse7551ced2005-05-26 12:04:57 +0100929 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +0100930
Al Viroef20c8c2006-02-18 15:41:50 -0500931 ab = audit_log_start(context, gfp_mask, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (!ab)
933 continue; /* audit_panic has been called */
934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 switch (aux->type) {
Steve Grubbc0404992005-05-13 18:17:42 +0100936 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 struct audit_aux_data_ipcctl *axi = (void *)aux;
938 audit_log_format(ab,
Steve Grubb326e9c82005-05-21 00:22:31 +0100939 " qbytes=%lx iuid=%u igid=%u mode=%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 axi->qbytes, axi->uid, axi->gid, axi->mode);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100941 break; }
942
943 case AUDIT_SOCKETCALL: {
944 int i;
945 struct audit_aux_data_socketcall *axs = (void *)aux;
946 audit_log_format(ab, "nargs=%d", axs->nargs);
947 for (i=0; i<axs->nargs; i++)
948 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
949 break; }
950
951 case AUDIT_SOCKADDR: {
952 struct audit_aux_data_sockaddr *axs = (void *)aux;
953
954 audit_log_format(ab, "saddr=");
955 audit_log_hex(ab, axs->a, axs->len);
956 break; }
Stephen Smalley01116102005-05-21 00:15:52 +0100957
958 case AUDIT_AVC_PATH: {
959 struct audit_aux_data_path *axi = (void *)aux;
960 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
Stephen Smalley01116102005-05-21 00:15:52 +0100961 break; }
962
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966
David Woodhouse8f37d472005-05-27 12:17:28 +0100967 if (context->pwd && context->pwdmnt) {
Al Viroef20c8c2006-02-18 15:41:50 -0500968 ab = audit_log_start(context, gfp_mask, AUDIT_CWD);
David Woodhouse8f37d472005-05-27 12:17:28 +0100969 if (ab) {
970 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
971 audit_log_end(ab);
972 }
973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 for (i = 0; i < context->name_count; i++) {
Amy Griffis73241cc2005-11-03 16:00:25 +0000975 unsigned long ino = context->names[i].ino;
976 unsigned long pino = context->names[i].pino;
977
Al Viroef20c8c2006-02-18 15:41:50 -0500978 ab = audit_log_start(context, gfp_mask, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (!ab)
980 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +0100981
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 audit_log_format(ab, "item=%d", i);
Amy Griffis73241cc2005-11-03 16:00:25 +0000983
984 audit_log_format(ab, " name=");
985 if (context->names[i].name)
83c7d092005-04-29 15:54:44 +0100986 audit_log_untrustedstring(ab, context->names[i].name);
Amy Griffis73241cc2005-11-03 16:00:25 +0000987 else
988 audit_log_format(ab, "(null)");
989
990 if (pino != (unsigned long)-1)
991 audit_log_format(ab, " parent=%lu", pino);
992 if (ino != (unsigned long)-1)
993 audit_log_format(ab, " inode=%lu", ino);
994 if ((pino != (unsigned long)-1) || (ino != (unsigned long)-1))
995 audit_log_format(ab, " dev=%02x:%02x mode=%#o"
996 " ouid=%u ogid=%u rdev=%02x:%02x",
997 MAJOR(context->names[i].dev),
998 MINOR(context->names[i].dev),
999 context->names[i].mode,
1000 context->names[i].uid,
1001 context->names[i].gid,
1002 MAJOR(context->names[i].rdev),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 MINOR(context->names[i].rdev));
1004 audit_log_end(ab);
1005 }
1006}
1007
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001008/**
1009 * audit_free - free a per-task audit context
1010 * @tsk: task whose audit context block to free
1011 *
1012 * Called from copy_process and __put_task_struct.
1013 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014void audit_free(struct task_struct *tsk)
1015{
1016 struct audit_context *context;
1017
1018 task_lock(tsk);
1019 context = audit_get_context(tsk, 0, 0);
1020 task_unlock(tsk);
1021
1022 if (likely(!context))
1023 return;
1024
1025 /* Check for system calls that do not go through the exit
David Woodhousef5561962005-07-13 22:47:07 +01001026 * function (e.g., exit_group), then free context block.
1027 * We use GFP_ATOMIC here because we might be doing this
1028 * in the context of the idle thread */
David Woodhousef7056d62005-06-20 16:07:33 +01001029 if (context->in_syscall && context->auditable)
David Woodhousef5561962005-07-13 22:47:07 +01001030 audit_log_exit(context, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 audit_free_context(context);
1033}
1034
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001035/**
1036 * audit_syscall_entry - fill in an audit record at syscall entry
1037 * @tsk: task being audited
1038 * @arch: architecture type
1039 * @major: major syscall type (function)
1040 * @a1: additional syscall register 1
1041 * @a2: additional syscall register 2
1042 * @a3: additional syscall register 3
1043 * @a4: additional syscall register 4
1044 *
1045 * Fill in audit context at syscall entry. This only happens if the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 * audit context was created when the task was created and the state or
1047 * filters demand the audit context be built. If the state from the
1048 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1049 * then the record will be written at syscall exit time (otherwise, it
1050 * will only be written if another part of the kernel requests that it
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001051 * be written).
1052 */
2fd6f582005-04-29 16:08:28 +01001053void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 unsigned long a1, unsigned long a2,
1055 unsigned long a3, unsigned long a4)
1056{
1057 struct audit_context *context = tsk->audit_context;
1058 enum audit_state state;
1059
1060 BUG_ON(!context);
1061
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001062 /*
1063 * This happens only on certain architectures that make system
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 * calls in kernel_thread via the entry.S interface, instead of
1065 * with direct calls. (If you are porting to a new
1066 * architecture, hitting this condition can indicate that you
1067 * got the _exit/_leave calls backward in entry.S.)
1068 *
1069 * i386 no
1070 * x86_64 no
1071 * ppc64 yes (see arch/ppc64/kernel/misc.S)
1072 *
1073 * This also happens with vm86 emulation in a non-nested manner
1074 * (entries without exits), so this case must be caught.
1075 */
1076 if (context->in_syscall) {
1077 struct audit_context *newctx;
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079#if AUDIT_DEBUG
1080 printk(KERN_ERR
1081 "audit(:%d) pid=%d in syscall=%d;"
1082 " entering syscall=%d\n",
1083 context->serial, tsk->pid, context->major, major);
1084#endif
1085 newctx = audit_alloc_context(context->state);
1086 if (newctx) {
1087 newctx->previous = context;
1088 context = newctx;
1089 tsk->audit_context = newctx;
1090 } else {
1091 /* If we can't alloc a new context, the best we
1092 * can do is to leak memory (any pending putname
1093 * will be lost). The only other alternative is
1094 * to abandon auditing. */
1095 audit_zero_context(context, context->state);
1096 }
1097 }
1098 BUG_ON(context->in_syscall || context->name_count);
1099
1100 if (!audit_enabled)
1101 return;
1102
2fd6f582005-04-29 16:08:28 +01001103 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 context->major = major;
1105 context->argv[0] = a1;
1106 context->argv[1] = a2;
1107 context->argv[2] = a3;
1108 context->argv[3] = a4;
1109
1110 state = context->state;
1111 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
David Woodhouse0f45aa12005-06-19 19:35:50 +01001112 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (likely(state == AUDIT_DISABLED))
1114 return;
1115
David Woodhousece625a82005-07-18 14:24:46 -04001116 context->serial = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 context->ctime = CURRENT_TIME;
1118 context->in_syscall = 1;
1119 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
1120}
1121
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001122/**
1123 * audit_syscall_exit - deallocate audit context after a system call
1124 * @tsk: task being audited
1125 * @valid: success/failure flag
1126 * @return_code: syscall return value
1127 *
1128 * Tear down after system call. If the audit context has been marked as
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1130 * filtering, or because some other part of the kernel write an audit
1131 * message), then write out the syscall information. In call cases,
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001132 * free the names stored from getname().
1133 */
2fd6f582005-04-29 16:08:28 +01001134void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
1136 struct audit_context *context;
1137
1138 get_task_struct(tsk);
1139 task_lock(tsk);
2fd6f582005-04-29 16:08:28 +01001140 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 task_unlock(tsk);
1142
1143 /* Not having a context here is ok, since the parent may have
1144 * called __put_task_struct. */
1145 if (likely(!context))
David Woodhouse413a1c72005-08-17 14:45:55 +01001146 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
David Woodhousef7056d62005-06-20 16:07:33 +01001148 if (context->in_syscall && context->auditable)
David Woodhousef5561962005-07-13 22:47:07 +01001149 audit_log_exit(context, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 context->in_syscall = 0;
1152 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +01001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 if (context->previous) {
1155 struct audit_context *new_context = context->previous;
1156 context->previous = NULL;
1157 audit_free_context(context);
1158 tsk->audit_context = new_context;
1159 } else {
1160 audit_free_names(context);
1161 audit_free_aux(context);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 tsk->audit_context = context;
1163 }
David Woodhouse413a1c72005-08-17 14:45:55 +01001164 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 put_task_struct(tsk);
1166}
1167
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001168/**
1169 * audit_getname - add a name to the list
1170 * @name: name to add
1171 *
1172 * Add a name to the list of audit names for this context.
1173 * Called from fs/namei.c:getname().
1174 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175void audit_getname(const char *name)
1176{
1177 struct audit_context *context = current->audit_context;
1178
1179 if (!context || IS_ERR(name) || !name)
1180 return;
1181
1182 if (!context->in_syscall) {
1183#if AUDIT_DEBUG == 2
1184 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1185 __FILE__, __LINE__, context->serial, name);
1186 dump_stack();
1187#endif
1188 return;
1189 }
1190 BUG_ON(context->name_count >= AUDIT_NAMES);
1191 context->names[context->name_count].name = name;
1192 context->names[context->name_count].ino = (unsigned long)-1;
1193 ++context->name_count;
David Woodhouse8f37d472005-05-27 12:17:28 +01001194 if (!context->pwd) {
1195 read_lock(&current->fs->lock);
1196 context->pwd = dget(current->fs->pwd);
1197 context->pwdmnt = mntget(current->fs->pwdmnt);
1198 read_unlock(&current->fs->lock);
1199 }
1200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001203/* audit_putname - intercept a putname request
1204 * @name: name to intercept and delay for putname
1205 *
1206 * If we have stored the name from getname in the audit context,
1207 * then we delay the putname until syscall exit.
1208 * Called from include/linux/fs.h:putname().
1209 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210void audit_putname(const char *name)
1211{
1212 struct audit_context *context = current->audit_context;
1213
1214 BUG_ON(!context);
1215 if (!context->in_syscall) {
1216#if AUDIT_DEBUG == 2
1217 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1218 __FILE__, __LINE__, context->serial, name);
1219 if (context->name_count) {
1220 int i;
1221 for (i = 0; i < context->name_count; i++)
1222 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1223 context->names[i].name,
Amy Griffis73241cc2005-11-03 16:00:25 +00001224 context->names[i].name ?: "(null)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 }
1226#endif
1227 __putname(name);
1228 }
1229#if AUDIT_DEBUG
1230 else {
1231 ++context->put_count;
1232 if (context->put_count > context->name_count) {
1233 printk(KERN_ERR "%s:%d(:%d): major=%d"
1234 " in_syscall=%d putname(%p) name_count=%d"
1235 " put_count=%d\n",
1236 __FILE__, __LINE__,
1237 context->serial, context->major,
1238 context->in_syscall, name, context->name_count,
1239 context->put_count);
1240 dump_stack();
1241 }
1242 }
1243#endif
1244}
1245
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001246/**
1247 * audit_inode - store the inode and device from a lookup
1248 * @name: name being audited
1249 * @inode: inode being audited
1250 * @flags: lookup flags (as used in path_lookup())
1251 *
1252 * Called from fs/namei.c:path_lookup().
1253 */
Amy Griffis73241cc2005-11-03 16:00:25 +00001254void __audit_inode(const char *name, const struct inode *inode, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
1256 int idx;
1257 struct audit_context *context = current->audit_context;
1258
1259 if (!context->in_syscall)
1260 return;
1261 if (context->name_count
1262 && context->names[context->name_count-1].name
1263 && context->names[context->name_count-1].name == name)
1264 idx = context->name_count - 1;
1265 else if (context->name_count > 1
1266 && context->names[context->name_count-2].name
1267 && context->names[context->name_count-2].name == name)
1268 idx = context->name_count - 2;
1269 else {
1270 /* FIXME: how much do we care about inodes that have no
1271 * associated name? */
1272 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1273 return;
1274 idx = context->name_count++;
1275 context->names[idx].name = NULL;
1276#if AUDIT_DEBUG
1277 ++context->ino_count;
1278#endif
1279 }
David Woodhouseae7b9612005-06-20 16:11:05 +01001280 context->names[idx].dev = inode->i_sb->s_dev;
1281 context->names[idx].mode = inode->i_mode;
1282 context->names[idx].uid = inode->i_uid;
1283 context->names[idx].gid = inode->i_gid;
1284 context->names[idx].rdev = inode->i_rdev;
Amy Griffis73241cc2005-11-03 16:00:25 +00001285 if ((flags & LOOKUP_PARENT) && (strcmp(name, "/") != 0) &&
1286 (strcmp(name, ".") != 0)) {
1287 context->names[idx].ino = (unsigned long)-1;
1288 context->names[idx].pino = inode->i_ino;
1289 } else {
1290 context->names[idx].ino = inode->i_ino;
1291 context->names[idx].pino = (unsigned long)-1;
1292 }
1293}
1294
1295/**
1296 * audit_inode_child - collect inode info for created/removed objects
1297 * @dname: inode's dentry name
1298 * @inode: inode being audited
1299 * @pino: inode number of dentry parent
1300 *
1301 * For syscalls that create or remove filesystem objects, audit_inode
1302 * can only collect information for the filesystem object's parent.
1303 * This call updates the audit context with the child's information.
1304 * Syscalls that create a new filesystem object must be hooked after
1305 * the object is created. Syscalls that remove a filesystem object
1306 * must be hooked prior, in order to capture the target inode during
1307 * unsuccessful attempts.
1308 */
1309void __audit_inode_child(const char *dname, const struct inode *inode,
1310 unsigned long pino)
1311{
1312 int idx;
1313 struct audit_context *context = current->audit_context;
1314
1315 if (!context->in_syscall)
1316 return;
1317
1318 /* determine matching parent */
1319 if (dname)
1320 for (idx = 0; idx < context->name_count; idx++)
1321 if (context->names[idx].pino == pino) {
1322 const char *n;
1323 const char *name = context->names[idx].name;
1324 int dlen = strlen(dname);
1325 int nlen = name ? strlen(name) : 0;
1326
1327 if (nlen < dlen)
1328 continue;
1329
1330 /* disregard trailing slashes */
1331 n = name + nlen - 1;
1332 while ((*n == '/') && (n > name))
1333 n--;
1334
1335 /* find last path component */
1336 n = n - dlen + 1;
1337 if (n < name)
1338 continue;
1339 else if (n > name) {
1340 if (*--n != '/')
1341 continue;
1342 else
1343 n++;
1344 }
1345
1346 if (strncmp(n, dname, dlen) == 0)
1347 goto update_context;
1348 }
1349
1350 /* catch-all in case match not found */
1351 idx = context->name_count++;
1352 context->names[idx].name = NULL;
1353 context->names[idx].pino = pino;
1354#if AUDIT_DEBUG
1355 context->ino_count++;
1356#endif
1357
1358update_context:
1359 if (inode) {
1360 context->names[idx].ino = inode->i_ino;
1361 context->names[idx].dev = inode->i_sb->s_dev;
1362 context->names[idx].mode = inode->i_mode;
1363 context->names[idx].uid = inode->i_uid;
1364 context->names[idx].gid = inode->i_gid;
1365 context->names[idx].rdev = inode->i_rdev;
1366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367}
1368
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001369/**
1370 * auditsc_get_stamp - get local copies of audit_context values
1371 * @ctx: audit_context for the task
1372 * @t: timespec to store time recorded in the audit_context
1373 * @serial: serial value that is recorded in the audit_context
1374 *
1375 * Also sets the context as auditable.
1376 */
David Woodhousebfb44962005-05-21 21:08:09 +01001377void auditsc_get_stamp(struct audit_context *ctx,
1378 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
David Woodhousece625a82005-07-18 14:24:46 -04001380 if (!ctx->serial)
1381 ctx->serial = audit_serial();
David Woodhousebfb44962005-05-21 21:08:09 +01001382 t->tv_sec = ctx->ctime.tv_sec;
1383 t->tv_nsec = ctx->ctime.tv_nsec;
1384 *serial = ctx->serial;
1385 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001388/**
1389 * audit_set_loginuid - set a task's audit_context loginuid
1390 * @task: task whose audit context is being modified
1391 * @loginuid: loginuid value
1392 *
1393 * Returns 0.
1394 *
1395 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1396 */
Steve Grubb456be6c2005-04-29 17:30:07 +01001397int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398{
Steve Grubb456be6c2005-04-29 17:30:07 +01001399 if (task->audit_context) {
Steve Grubbc0404992005-05-13 18:17:42 +01001400 struct audit_buffer *ab;
1401
David Woodhouse9ad9ad32005-06-22 15:04:33 +01001402 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
Steve Grubbc0404992005-05-13 18:17:42 +01001403 if (ab) {
1404 audit_log_format(ab, "login pid=%d uid=%u "
Steve Grubb326e9c82005-05-21 00:22:31 +01001405 "old auid=%u new auid=%u",
Steve Grubbc0404992005-05-13 18:17:42 +01001406 task->pid, task->uid,
1407 task->audit_context->loginuid, loginuid);
1408 audit_log_end(ab);
1409 }
Steve Grubb456be6c2005-04-29 17:30:07 +01001410 task->audit_context->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
1412 return 0;
1413}
1414
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001415/**
1416 * audit_get_loginuid - get the loginuid for an audit_context
1417 * @ctx: the audit_context
1418 *
1419 * Returns the context's loginuid or -1 if @ctx is NULL.
1420 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421uid_t audit_get_loginuid(struct audit_context *ctx)
1422{
1423 return ctx ? ctx->loginuid : -1;
1424}
1425
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001426/**
1427 * audit_ipc_perms - record audit data for ipc
1428 * @qbytes: msgq bytes
1429 * @uid: msgq user id
1430 * @gid: msgq group id
1431 * @mode: msgq mode (permissions)
1432 *
1433 * Returns 0 for success or NULL context or < 0 on error.
1434 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
1436{
1437 struct audit_aux_data_ipcctl *ax;
1438 struct audit_context *context = current->audit_context;
1439
1440 if (likely(!context))
1441 return 0;
1442
1443 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
1444 if (!ax)
1445 return -ENOMEM;
1446
1447 ax->qbytes = qbytes;
1448 ax->uid = uid;
1449 ax->gid = gid;
1450 ax->mode = mode;
1451
Steve Grubbc0404992005-05-13 18:17:42 +01001452 ax->d.type = AUDIT_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 ax->d.next = context->aux;
1454 context->aux = (void *)ax;
1455 return 0;
1456}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001457
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001458/**
1459 * audit_socketcall - record audit data for sys_socketcall
1460 * @nargs: number of args
1461 * @args: args array
1462 *
1463 * Returns 0 for success or NULL context or < 0 on error.
1464 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001465int audit_socketcall(int nargs, unsigned long *args)
1466{
1467 struct audit_aux_data_socketcall *ax;
1468 struct audit_context *context = current->audit_context;
1469
1470 if (likely(!context))
1471 return 0;
1472
1473 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1474 if (!ax)
1475 return -ENOMEM;
1476
1477 ax->nargs = nargs;
1478 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1479
1480 ax->d.type = AUDIT_SOCKETCALL;
1481 ax->d.next = context->aux;
1482 context->aux = (void *)ax;
1483 return 0;
1484}
1485
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001486/**
1487 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1488 * @len: data length in user space
1489 * @a: data address in kernel space
1490 *
1491 * Returns 0 for success or NULL context or < 0 on error.
1492 */
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001493int audit_sockaddr(int len, void *a)
1494{
1495 struct audit_aux_data_sockaddr *ax;
1496 struct audit_context *context = current->audit_context;
1497
1498 if (likely(!context))
1499 return 0;
1500
1501 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1502 if (!ax)
1503 return -ENOMEM;
1504
1505 ax->len = len;
1506 memcpy(ax->a, a, len);
1507
1508 ax->d.type = AUDIT_SOCKADDR;
1509 ax->d.next = context->aux;
1510 context->aux = (void *)ax;
1511 return 0;
1512}
1513
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001514/**
1515 * audit_avc_path - record the granting or denial of permissions
1516 * @dentry: dentry to record
1517 * @mnt: mnt to record
1518 *
1519 * Returns 0 for success or NULL context or < 0 on error.
1520 *
1521 * Called from security/selinux/avc.c::avc_audit()
1522 */
Stephen Smalley01116102005-05-21 00:15:52 +01001523int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1524{
1525 struct audit_aux_data_path *ax;
1526 struct audit_context *context = current->audit_context;
1527
1528 if (likely(!context))
1529 return 0;
1530
1531 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1532 if (!ax)
1533 return -ENOMEM;
1534
1535 ax->dentry = dget(dentry);
1536 ax->mnt = mntget(mnt);
1537
1538 ax->d.type = AUDIT_AVC_PATH;
1539 ax->d.next = context->aux;
1540 context->aux = (void *)ax;
1541 return 0;
1542}
1543
Randy Dunlapb0dd25a2005-09-13 12:47:11 -07001544/**
1545 * audit_signal_info - record signal info for shutting down audit subsystem
1546 * @sig: signal value
1547 * @t: task being signaled
1548 *
1549 * If the audit subsystem is being terminated, record the task (pid)
1550 * and uid that is doing that.
1551 */
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001552void audit_signal_info(int sig, struct task_struct *t)
1553{
1554 extern pid_t audit_sig_pid;
1555 extern uid_t audit_sig_uid;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001556
David Woodhouse582edda2005-07-13 22:39:34 +01001557 if (unlikely(audit_pid && t->tgid == audit_pid)) {
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001558 if (sig == SIGTERM || sig == SIGHUP) {
1559 struct audit_context *ctx = current->audit_context;
1560 audit_sig_pid = current->pid;
1561 if (ctx)
1562 audit_sig_uid = ctx->loginuid;
1563 else
1564 audit_sig_uid = current->uid;
1565 }
1566 }
1567}