blob: 031f979019d1534cfba45b35153fa067d5fca511 [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.
5 * All Rights Reserved.
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 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
22 *
23 * Many of the ideas implemented here are from Stephen C. Tweedie,
24 * especially the idea of avoiding a copy by using getname.
25 *
26 * The method for actual interception of syscall entry and exit (not in
27 * this file -- see entry.S) is based on a GPL'd patch written by
28 * okir@suse.de and Copyright 2003 SuSE Linux AG.
29 *
30 */
31
32#include <linux/init.h>
33#include <asm/atomic.h>
34#include <asm/types.h>
35#include <linux/mm.h>
36#include <linux/module.h>
Stephen Smalley01116102005-05-21 00:15:52 +010037#include <linux/mount.h>
David Woodhouse3ec3b2f2005-05-17 12:08:48 +010038#include <linux/socket.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/audit.h>
40#include <linux/personality.h>
41#include <linux/time.h>
42#include <asm/unistd.h>
43
44/* 0 = no checking
45 1 = put_count checking
46 2 = verbose put_count checking
47*/
48#define AUDIT_DEBUG 0
49
50/* No syscall auditing will take place unless audit_enabled != 0. */
51extern int audit_enabled;
52
53/* AUDIT_NAMES is the number of slots we reserve in the audit_context
54 * for saving names from getname(). */
55#define AUDIT_NAMES 20
56
57/* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
58 * audit_context from being used for nameless inodes from
59 * path_lookup. */
60#define AUDIT_NAMES_RESERVED 7
61
62/* At task start time, the audit_state is set in the audit_context using
63 a per-task filter. At syscall entry, the audit_state is augmented by
64 the syscall filter. */
65enum audit_state {
66 AUDIT_DISABLED, /* Do not create per-task audit_context.
67 * No syscall-specific audit records can
68 * be generated. */
69 AUDIT_SETUP_CONTEXT, /* Create the per-task audit_context,
70 * but don't necessarily fill it in at
71 * syscall entry time (i.e., filter
72 * instead). */
73 AUDIT_BUILD_CONTEXT, /* Create the per-task audit_context,
74 * and always fill it in at syscall
75 * entry time. This makes a full
76 * syscall record available if some
77 * other part of the kernel decides it
78 * should be recorded. */
79 AUDIT_RECORD_CONTEXT /* Create the per-task audit_context,
80 * always fill it in at syscall entry
81 * time, and always write out the audit
82 * record at syscall exit time. */
83};
84
85/* When fs/namei.c:getname() is called, we store the pointer in name and
86 * we don't let putname() free it (instead we free all of the saved
87 * pointers at syscall exit time).
88 *
89 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
90struct audit_names {
91 const char *name;
92 unsigned long ino;
93 dev_t dev;
94 umode_t mode;
95 uid_t uid;
96 gid_t gid;
97 dev_t rdev;
David Woodhouseae7b9612005-06-20 16:11:05 +010098 unsigned flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099};
100
101struct audit_aux_data {
102 struct audit_aux_data *next;
103 int type;
104};
105
106#define AUDIT_AUX_IPCPERM 0
107
108struct audit_aux_data_ipcctl {
109 struct audit_aux_data d;
110 struct ipc_perm p;
111 unsigned long qbytes;
112 uid_t uid;
113 gid_t gid;
114 mode_t mode;
115};
116
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100117struct audit_aux_data_socketcall {
118 struct audit_aux_data d;
119 int nargs;
120 unsigned long args[0];
121};
122
123struct audit_aux_data_sockaddr {
124 struct audit_aux_data d;
125 int len;
126 char a[0];
127};
128
Stephen Smalley01116102005-05-21 00:15:52 +0100129struct audit_aux_data_path {
130 struct audit_aux_data d;
131 struct dentry *dentry;
132 struct vfsmount *mnt;
133};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135/* The per-task audit context. */
136struct audit_context {
137 int in_syscall; /* 1 if task is in a syscall */
138 enum audit_state state;
139 unsigned int serial; /* serial number for record */
140 struct timespec ctime; /* time of syscall entry */
141 uid_t loginuid; /* login uid (identity) */
142 int major; /* syscall number */
143 unsigned long argv[4]; /* syscall arguments */
144 int return_valid; /* return code is valid */
2fd6f582005-04-29 16:08:28 +0100145 long return_code;/* syscall return code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 int auditable; /* 1 if record should be written */
147 int name_count;
148 struct audit_names names[AUDIT_NAMES];
David Woodhouse8f37d472005-05-27 12:17:28 +0100149 struct dentry * pwd;
150 struct vfsmount * pwdmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 struct audit_context *previous; /* For nested syscalls */
152 struct audit_aux_data *aux;
153
154 /* Save things to print about task_struct */
155 pid_t pid;
156 uid_t uid, euid, suid, fsuid;
157 gid_t gid, egid, sgid, fsgid;
158 unsigned long personality;
2fd6f582005-04-29 16:08:28 +0100159 int arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161#if AUDIT_DEBUG
162 int put_count;
163 int ino_count;
164#endif
165};
166
167 /* Public API */
168/* There are three lists of rules -- one to search at task creation
169 * time, one to search at syscall entry time, and another to search at
170 * syscall exit time. */
David Woodhouse0f45aa12005-06-19 19:35:50 +0100171static struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
172 LIST_HEAD_INIT(audit_filter_list[0]),
173 LIST_HEAD_INIT(audit_filter_list[1]),
174 LIST_HEAD_INIT(audit_filter_list[2]),
175 LIST_HEAD_INIT(audit_filter_list[3]),
176 LIST_HEAD_INIT(audit_filter_list[4]),
177#if AUDIT_NR_FILTERS != 5
178#error Fix audit_filter_list initialiser
179#endif
180};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182struct audit_entry {
183 struct list_head list;
184 struct rcu_head rcu;
185 struct audit_rule rule;
186};
187
David Woodhouse7ca00262005-05-19 11:23:13 +0100188extern int audit_pid;
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/* Check to see if two rules are identical. It is called from
191 * audit_del_rule during AUDIT_DEL. */
192static int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
193{
194 int i;
195
196 if (a->flags != b->flags)
197 return 1;
198
199 if (a->action != b->action)
200 return 1;
201
202 if (a->field_count != b->field_count)
203 return 1;
204
205 for (i = 0; i < a->field_count; i++) {
206 if (a->fields[i] != b->fields[i]
207 || a->values[i] != b->values[i])
208 return 1;
209 }
210
211 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
212 if (a->mask[i] != b->mask[i])
213 return 1;
214
215 return 0;
216}
217
218/* Note that audit_add_rule and audit_del_rule are called via
219 * audit_receive() in audit.c, and are protected by
220 * audit_netlink_sem. */
David Woodhouse0f45aa12005-06-19 19:35:50 +0100221static inline void audit_add_rule(struct audit_entry *entry,
222 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
David Woodhouse0f45aa12005-06-19 19:35:50 +0100224 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
225 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 list_add_rcu(&entry->list, list);
227 } else {
228 list_add_tail_rcu(&entry->list, list);
229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
232static void audit_free_rule(struct rcu_head *head)
233{
234 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
235 kfree(e);
236}
237
238/* Note that audit_add_rule and audit_del_rule are called via
239 * audit_receive() in audit.c, and are protected by
240 * audit_netlink_sem. */
241static inline int audit_del_rule(struct audit_rule *rule,
242 struct list_head *list)
243{
244 struct audit_entry *e;
245
246 /* Do not use the _rcu iterator here, since this is the only
247 * deletion routine. */
248 list_for_each_entry(e, list, list) {
249 if (!audit_compare_rule(rule, &e->rule)) {
250 list_del_rcu(&e->list);
251 call_rcu(&e->rcu, audit_free_rule);
252 return 0;
253 }
254 }
David Woodhouse0f45aa12005-06-19 19:35:50 +0100255 return -ENOENT; /* No matching rule */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258/* Copy rule from user-space to kernel-space. Called during
259 * AUDIT_ADD. */
260static int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
261{
262 int i;
263
264 if (s->action != AUDIT_NEVER
265 && s->action != AUDIT_POSSIBLE
266 && s->action != AUDIT_ALWAYS)
267 return -1;
268 if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
269 return -1;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100270 if ((s->flags & ~AUDIT_FILTER_PREPEND) >= AUDIT_NR_FILTERS)
271 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 d->flags = s->flags;
274 d->action = s->action;
275 d->field_count = s->field_count;
276 for (i = 0; i < d->field_count; i++) {
277 d->fields[i] = s->fields[i];
278 d->values[i] = s->values[i];
279 }
280 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
281 return 0;
282}
283
Serge Hallync94c2572005-04-29 16:27:17 +0100284int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
285 uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 struct audit_entry *entry;
288 int err = 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100289 int i;
290 unsigned listnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292 switch (type) {
293 case AUDIT_LIST:
294 /* The *_rcu iterators not needed here because we are
295 always called with audit_netlink_sem held. */
David Woodhouse0f45aa12005-06-19 19:35:50 +0100296 for (i=0; i<AUDIT_NR_FILTERS; i++) {
297 list_for_each_entry(entry, &audit_filter_list[i], list)
298 audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
299 &entry->rule, sizeof(entry->rule));
300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
302 break;
303 case AUDIT_ADD:
304 if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
305 return -ENOMEM;
306 if (audit_copy_rule(&entry->rule, data)) {
307 kfree(entry);
308 return -EINVAL;
309 }
David Woodhouse0f45aa12005-06-19 19:35:50 +0100310 listnr = entry->rule.flags & ~AUDIT_FILTER_PREPEND;
311 audit_add_rule(entry, &audit_filter_list[listnr]);
Steve Grubbc0404992005-05-13 18:17:42 +0100312 audit_log(NULL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100313 "auid=%u added an audit rule\n", loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 break;
315 case AUDIT_DEL:
David Woodhouse0f45aa12005-06-19 19:35:50 +0100316 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
317 if (listnr >= AUDIT_NR_FILTERS)
318 return -EINVAL;
319
320 err = audit_del_rule(data, &audit_filter_list[listnr]);
321 if (!err)
322 audit_log(NULL, AUDIT_CONFIG_CHANGE,
323 "auid=%u removed an audit rule\n", loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 break;
325 default:
326 return -EINVAL;
327 }
328
329 return err;
330}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332/* Compare a task_struct with an audit_rule. Return 1 on match, 0
333 * otherwise. */
334static int audit_filter_rules(struct task_struct *tsk,
335 struct audit_rule *rule,
336 struct audit_context *ctx,
337 enum audit_state *state)
338{
339 int i, j;
340
341 for (i = 0; i < rule->field_count; i++) {
342 u32 field = rule->fields[i] & ~AUDIT_NEGATE;
343 u32 value = rule->values[i];
344 int result = 0;
345
346 switch (field) {
347 case AUDIT_PID:
348 result = (tsk->pid == value);
349 break;
350 case AUDIT_UID:
351 result = (tsk->uid == value);
352 break;
353 case AUDIT_EUID:
354 result = (tsk->euid == value);
355 break;
356 case AUDIT_SUID:
357 result = (tsk->suid == value);
358 break;
359 case AUDIT_FSUID:
360 result = (tsk->fsuid == value);
361 break;
362 case AUDIT_GID:
363 result = (tsk->gid == value);
364 break;
365 case AUDIT_EGID:
366 result = (tsk->egid == value);
367 break;
368 case AUDIT_SGID:
369 result = (tsk->sgid == value);
370 break;
371 case AUDIT_FSGID:
372 result = (tsk->fsgid == value);
373 break;
374 case AUDIT_PERS:
375 result = (tsk->personality == value);
376 break;
2fd6f582005-04-29 16:08:28 +0100377 case AUDIT_ARCH:
378 if (ctx)
379 result = (ctx->arch == value);
380 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 case AUDIT_EXIT:
383 if (ctx && ctx->return_valid)
384 result = (ctx->return_code == value);
385 break;
386 case AUDIT_SUCCESS:
387 if (ctx && ctx->return_valid)
2fd6f582005-04-29 16:08:28 +0100388 result = (ctx->return_valid == AUDITSC_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 break;
390 case AUDIT_DEVMAJOR:
391 if (ctx) {
392 for (j = 0; j < ctx->name_count; j++) {
393 if (MAJOR(ctx->names[j].dev)==value) {
394 ++result;
395 break;
396 }
397 }
398 }
399 break;
400 case AUDIT_DEVMINOR:
401 if (ctx) {
402 for (j = 0; j < ctx->name_count; j++) {
403 if (MINOR(ctx->names[j].dev)==value) {
404 ++result;
405 break;
406 }
407 }
408 }
409 break;
410 case AUDIT_INODE:
411 if (ctx) {
412 for (j = 0; j < ctx->name_count; j++) {
413 if (ctx->names[j].ino == value) {
414 ++result;
415 break;
416 }
417 }
418 }
419 break;
420 case AUDIT_LOGINUID:
421 result = 0;
422 if (ctx)
423 result = (ctx->loginuid == value);
424 break;
425 case AUDIT_ARG0:
426 case AUDIT_ARG1:
427 case AUDIT_ARG2:
428 case AUDIT_ARG3:
429 if (ctx)
430 result = (ctx->argv[field-AUDIT_ARG0]==value);
431 break;
432 }
433
434 if (rule->fields[i] & AUDIT_NEGATE)
435 result = !result;
436 if (!result)
437 return 0;
438 }
439 switch (rule->action) {
440 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
441 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
442 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
443 }
444 return 1;
445}
446
447/* At process creation time, we can determine if system-call auditing is
448 * completely disabled for this task. Since we only have the task
449 * structure at this point, we can only check uid and gid.
450 */
451static enum audit_state audit_filter_task(struct task_struct *tsk)
452{
453 struct audit_entry *e;
454 enum audit_state state;
455
456 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100457 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
459 rcu_read_unlock();
460 return state;
461 }
462 }
463 rcu_read_unlock();
464 return AUDIT_BUILD_CONTEXT;
465}
466
467/* At syscall entry and exit time, this filter is called if the
468 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100469 * also not high enough that we already know we have to write an audit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
471 */
472static enum audit_state audit_filter_syscall(struct task_struct *tsk,
473 struct audit_context *ctx,
474 struct list_head *list)
475{
476 struct audit_entry *e;
477 enum audit_state state;
478 int word = AUDIT_WORD(ctx->major);
479 int bit = AUDIT_BIT(ctx->major);
480
David Woodhousef7056d62005-06-20 16:07:33 +0100481 if (audit_pid && ctx->pid == audit_pid)
482 return AUDIT_DISABLED;
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 rcu_read_lock();
485 list_for_each_entry_rcu(e, list, list) {
486 if ((e->rule.mask[word] & bit) == bit
487 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
488 rcu_read_unlock();
489 return state;
490 }
491 }
492 rcu_read_unlock();
493 return AUDIT_BUILD_CONTEXT;
494}
495
David Woodhouse0f45aa12005-06-19 19:35:50 +0100496int audit_filter_user(struct task_struct *tsk, int type)
497{
498 struct audit_entry *e;
499 enum audit_state state;
500
David Woodhousef7056d62005-06-20 16:07:33 +0100501 if (audit_pid && tsk->pid == audit_pid)
502 return AUDIT_DISABLED;
503
David Woodhouse0f45aa12005-06-19 19:35:50 +0100504 rcu_read_lock();
505 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
506 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
507 rcu_read_unlock();
508 return state != AUDIT_DISABLED;
509 }
510 }
511 rcu_read_unlock();
512 return 1; /* Audit by default */
513
514}
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516/* This should be called with task_lock() held. */
517static inline struct audit_context *audit_get_context(struct task_struct *tsk,
518 int return_valid,
519 int return_code)
520{
521 struct audit_context *context = tsk->audit_context;
522
523 if (likely(!context))
524 return NULL;
525 context->return_valid = return_valid;
526 context->return_code = return_code;
527
528 if (context->in_syscall && !context->auditable) {
529 enum audit_state state;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100530 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 if (state == AUDIT_RECORD_CONTEXT)
532 context->auditable = 1;
533 }
534
535 context->pid = tsk->pid;
536 context->uid = tsk->uid;
537 context->gid = tsk->gid;
538 context->euid = tsk->euid;
539 context->suid = tsk->suid;
540 context->fsuid = tsk->fsuid;
541 context->egid = tsk->egid;
542 context->sgid = tsk->sgid;
543 context->fsgid = tsk->fsgid;
544 context->personality = tsk->personality;
545 tsk->audit_context = NULL;
546 return context;
547}
548
549static inline void audit_free_names(struct audit_context *context)
550{
551 int i;
552
553#if AUDIT_DEBUG == 2
554 if (context->auditable
555 ||context->put_count + context->ino_count != context->name_count) {
556 printk(KERN_ERR "audit.c:%d(:%d): major=%d in_syscall=%d"
557 " name_count=%d put_count=%d"
558 " ino_count=%d [NOT freeing]\n",
559 __LINE__,
560 context->serial, context->major, context->in_syscall,
561 context->name_count, context->put_count,
562 context->ino_count);
563 for (i = 0; i < context->name_count; i++)
564 printk(KERN_ERR "names[%d] = %p = %s\n", i,
565 context->names[i].name,
566 context->names[i].name);
567 dump_stack();
568 return;
569 }
570#endif
571#if AUDIT_DEBUG
572 context->put_count = 0;
573 context->ino_count = 0;
574#endif
575
576 for (i = 0; i < context->name_count; i++)
577 if (context->names[i].name)
578 __putname(context->names[i].name);
579 context->name_count = 0;
David Woodhouse8f37d472005-05-27 12:17:28 +0100580 if (context->pwd)
581 dput(context->pwd);
582 if (context->pwdmnt)
583 mntput(context->pwdmnt);
584 context->pwd = NULL;
585 context->pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586}
587
588static inline void audit_free_aux(struct audit_context *context)
589{
590 struct audit_aux_data *aux;
591
592 while ((aux = context->aux)) {
Stephen Smalley01116102005-05-21 00:15:52 +0100593 if (aux->type == AUDIT_AVC_PATH) {
594 struct audit_aux_data_path *axi = (void *)aux;
595 dput(axi->dentry);
596 mntput(axi->mnt);
597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 context->aux = aux->next;
599 kfree(aux);
600 }
601}
602
603static inline void audit_zero_context(struct audit_context *context,
604 enum audit_state state)
605{
606 uid_t loginuid = context->loginuid;
607
608 memset(context, 0, sizeof(*context));
609 context->state = state;
610 context->loginuid = loginuid;
611}
612
613static inline struct audit_context *audit_alloc_context(enum audit_state state)
614{
615 struct audit_context *context;
616
617 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
618 return NULL;
619 audit_zero_context(context, state);
620 return context;
621}
622
623/* Filter on the task information and allocate a per-task audit context
624 * if necessary. Doing so turns on system call auditing for the
625 * specified task. This is called from copy_process, so no lock is
626 * needed. */
627int audit_alloc(struct task_struct *tsk)
628{
629 struct audit_context *context;
630 enum audit_state state;
631
632 if (likely(!audit_enabled))
633 return 0; /* Return if not auditing. */
634
635 state = audit_filter_task(tsk);
636 if (likely(state == AUDIT_DISABLED))
637 return 0;
638
639 if (!(context = audit_alloc_context(state))) {
640 audit_log_lost("out of memory in audit_alloc");
641 return -ENOMEM;
642 }
643
644 /* Preserve login uid */
645 context->loginuid = -1;
646 if (current->audit_context)
647 context->loginuid = current->audit_context->loginuid;
648
649 tsk->audit_context = context;
650 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
651 return 0;
652}
653
654static inline void audit_free_context(struct audit_context *context)
655{
656 struct audit_context *previous;
657 int count = 0;
658
659 do {
660 previous = context->previous;
661 if (previous || (count && count < 10)) {
662 ++count;
663 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
664 " freeing multiple contexts (%d)\n",
665 context->serial, context->major,
666 context->name_count, count);
667 }
668 audit_free_names(context);
669 audit_free_aux(context);
670 kfree(context);
671 context = previous;
672 } while (context);
673 if (count >= 10)
674 printk(KERN_ERR "audit: freed %d contexts\n", count);
675}
676
Stephen Smalley219f0812005-04-18 10:47:35 -0700677static void audit_log_task_info(struct audit_buffer *ab)
678{
679 char name[sizeof(current->comm)];
680 struct mm_struct *mm = current->mm;
681 struct vm_area_struct *vma;
682
683 get_task_comm(name, current);
David Woodhouse99e45ee2005-05-23 21:57:41 +0100684 audit_log_format(ab, " comm=");
685 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700686
687 if (!mm)
688 return;
689
690 down_read(&mm->mmap_sem);
691 vma = mm->mmap;
692 while (vma) {
693 if ((vma->vm_flags & VM_EXECUTABLE) &&
694 vma->vm_file) {
695 audit_log_d_path(ab, "exe=",
696 vma->vm_file->f_dentry,
697 vma->vm_file->f_vfsmnt);
698 break;
699 }
700 vma = vma->vm_next;
701 }
702 up_read(&mm->mmap_sem);
703}
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705static void audit_log_exit(struct audit_context *context)
706{
707 int i;
708 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +0100709 struct audit_aux_data *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Steve Grubbc0404992005-05-13 18:17:42 +0100711 ab = audit_log_start(context, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (!ab)
713 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +0100714 audit_log_format(ab, "arch=%x syscall=%d",
715 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (context->personality != PER_LINUX)
717 audit_log_format(ab, " per=%lx", context->personality);
718 if (context->return_valid)
2fd6f582005-04-29 16:08:28 +0100719 audit_log_format(ab, " success=%s exit=%ld",
720 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
721 context->return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 audit_log_format(ab,
723 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Steve Grubb326e9c82005-05-21 00:22:31 +0100724 " pid=%d auid=%u uid=%u gid=%u"
725 " euid=%u suid=%u fsuid=%u"
726 " egid=%u sgid=%u fsgid=%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 context->argv[0],
728 context->argv[1],
729 context->argv[2],
730 context->argv[3],
731 context->name_count,
732 context->pid,
733 context->loginuid,
734 context->uid,
735 context->gid,
736 context->euid, context->suid, context->fsuid,
737 context->egid, context->sgid, context->fsgid);
Stephen Smalley219f0812005-04-18 10:47:35 -0700738 audit_log_task_info(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
David Woodhouse7551ced2005-05-26 12:04:57 +0100741 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +0100742
743 ab = audit_log_start(context, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (!ab)
745 continue; /* audit_panic has been called */
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 switch (aux->type) {
Steve Grubbc0404992005-05-13 18:17:42 +0100748 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 struct audit_aux_data_ipcctl *axi = (void *)aux;
750 audit_log_format(ab,
Steve Grubb326e9c82005-05-21 00:22:31 +0100751 " qbytes=%lx iuid=%u igid=%u mode=%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 axi->qbytes, axi->uid, axi->gid, axi->mode);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100753 break; }
754
755 case AUDIT_SOCKETCALL: {
756 int i;
757 struct audit_aux_data_socketcall *axs = (void *)aux;
758 audit_log_format(ab, "nargs=%d", axs->nargs);
759 for (i=0; i<axs->nargs; i++)
760 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
761 break; }
762
763 case AUDIT_SOCKADDR: {
764 struct audit_aux_data_sockaddr *axs = (void *)aux;
765
766 audit_log_format(ab, "saddr=");
767 audit_log_hex(ab, axs->a, axs->len);
768 break; }
Stephen Smalley01116102005-05-21 00:15:52 +0100769
770 case AUDIT_AVC_PATH: {
771 struct audit_aux_data_path *axi = (void *)aux;
772 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
Stephen Smalley01116102005-05-21 00:15:52 +0100773 break; }
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 }
778
David Woodhouse8f37d472005-05-27 12:17:28 +0100779 if (context->pwd && context->pwdmnt) {
780 ab = audit_log_start(context, AUDIT_CWD);
781 if (ab) {
782 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
783 audit_log_end(ab);
784 }
785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 for (i = 0; i < context->name_count; i++) {
Steve Grubbc0404992005-05-13 18:17:42 +0100787 ab = audit_log_start(context, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (!ab)
789 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +0100790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 audit_log_format(ab, "item=%d", i);
83c7d092005-04-29 15:54:44 +0100792 if (context->names[i].name) {
793 audit_log_format(ab, " name=");
794 audit_log_untrustedstring(ab, context->names[i].name);
795 }
David Woodhouseae7b9612005-06-20 16:11:05 +0100796 audit_log_format(ab, " flags=%x\n", context->names[i].flags);
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (context->names[i].ino != (unsigned long)-1)
799 audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#o"
Steve Grubb326e9c82005-05-21 00:22:31 +0100800 " ouid=%u ogid=%u rdev=%02x:%02x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 context->names[i].ino,
802 MAJOR(context->names[i].dev),
803 MINOR(context->names[i].dev),
804 context->names[i].mode,
805 context->names[i].uid,
806 context->names[i].gid,
807 MAJOR(context->names[i].rdev),
808 MINOR(context->names[i].rdev));
809 audit_log_end(ab);
810 }
811}
812
813/* Free a per-task audit context. Called from copy_process and
814 * __put_task_struct. */
815void audit_free(struct task_struct *tsk)
816{
817 struct audit_context *context;
818
819 task_lock(tsk);
820 context = audit_get_context(tsk, 0, 0);
821 task_unlock(tsk);
822
823 if (likely(!context))
824 return;
825
826 /* Check for system calls that do not go through the exit
827 * function (e.g., exit_group), then free context block. */
David Woodhousef7056d62005-06-20 16:07:33 +0100828 if (context->in_syscall && context->auditable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 audit_log_exit(context);
830
831 audit_free_context(context);
832}
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834/* Fill in audit context at syscall entry. This only happens if the
835 * audit context was created when the task was created and the state or
836 * filters demand the audit context be built. If the state from the
837 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
838 * then the record will be written at syscall exit time (otherwise, it
839 * will only be written if another part of the kernel requests that it
840 * be written). */
2fd6f582005-04-29 16:08:28 +0100841void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 unsigned long a1, unsigned long a2,
843 unsigned long a3, unsigned long a4)
844{
845 struct audit_context *context = tsk->audit_context;
846 enum audit_state state;
847
848 BUG_ON(!context);
849
850 /* This happens only on certain architectures that make system
851 * calls in kernel_thread via the entry.S interface, instead of
852 * with direct calls. (If you are porting to a new
853 * architecture, hitting this condition can indicate that you
854 * got the _exit/_leave calls backward in entry.S.)
855 *
856 * i386 no
857 * x86_64 no
858 * ppc64 yes (see arch/ppc64/kernel/misc.S)
859 *
860 * This also happens with vm86 emulation in a non-nested manner
861 * (entries without exits), so this case must be caught.
862 */
863 if (context->in_syscall) {
864 struct audit_context *newctx;
865
866#if defined(__NR_vm86) && defined(__NR_vm86old)
867 /* vm86 mode should only be entered once */
868 if (major == __NR_vm86 || major == __NR_vm86old)
869 return;
870#endif
871#if AUDIT_DEBUG
872 printk(KERN_ERR
873 "audit(:%d) pid=%d in syscall=%d;"
874 " entering syscall=%d\n",
875 context->serial, tsk->pid, context->major, major);
876#endif
877 newctx = audit_alloc_context(context->state);
878 if (newctx) {
879 newctx->previous = context;
880 context = newctx;
881 tsk->audit_context = newctx;
882 } else {
883 /* If we can't alloc a new context, the best we
884 * can do is to leak memory (any pending putname
885 * will be lost). The only other alternative is
886 * to abandon auditing. */
887 audit_zero_context(context, context->state);
888 }
889 }
890 BUG_ON(context->in_syscall || context->name_count);
891
892 if (!audit_enabled)
893 return;
894
2fd6f582005-04-29 16:08:28 +0100895 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 context->major = major;
897 context->argv[0] = a1;
898 context->argv[1] = a2;
899 context->argv[2] = a3;
900 context->argv[3] = a4;
901
902 state = context->state;
903 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
David Woodhouse0f45aa12005-06-19 19:35:50 +0100904 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (likely(state == AUDIT_DISABLED))
906 return;
907
908 context->serial = audit_serial();
909 context->ctime = CURRENT_TIME;
910 context->in_syscall = 1;
911 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
912}
913
914/* Tear down after system call. If the audit context has been marked as
915 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
916 * filtering, or because some other part of the kernel write an audit
917 * message), then write out the syscall information. In call cases,
918 * free the names stored from getname(). */
2fd6f582005-04-29 16:08:28 +0100919void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 struct audit_context *context;
922
923 get_task_struct(tsk);
924 task_lock(tsk);
2fd6f582005-04-29 16:08:28 +0100925 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 task_unlock(tsk);
927
928 /* Not having a context here is ok, since the parent may have
929 * called __put_task_struct. */
930 if (likely(!context))
931 return;
932
David Woodhousef7056d62005-06-20 16:07:33 +0100933 if (context->in_syscall && context->auditable)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 audit_log_exit(context);
935
936 context->in_syscall = 0;
937 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +0100938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 if (context->previous) {
940 struct audit_context *new_context = context->previous;
941 context->previous = NULL;
942 audit_free_context(context);
943 tsk->audit_context = new_context;
944 } else {
945 audit_free_names(context);
946 audit_free_aux(context);
947 audit_zero_context(context, context->state);
948 tsk->audit_context = context;
949 }
950 put_task_struct(tsk);
951}
952
953/* Add a name to the list. Called from fs/namei.c:getname(). */
954void audit_getname(const char *name)
955{
956 struct audit_context *context = current->audit_context;
957
958 if (!context || IS_ERR(name) || !name)
959 return;
960
961 if (!context->in_syscall) {
962#if AUDIT_DEBUG == 2
963 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
964 __FILE__, __LINE__, context->serial, name);
965 dump_stack();
966#endif
967 return;
968 }
969 BUG_ON(context->name_count >= AUDIT_NAMES);
970 context->names[context->name_count].name = name;
971 context->names[context->name_count].ino = (unsigned long)-1;
972 ++context->name_count;
David Woodhouse8f37d472005-05-27 12:17:28 +0100973 if (!context->pwd) {
974 read_lock(&current->fs->lock);
975 context->pwd = dget(current->fs->pwd);
976 context->pwdmnt = mntget(current->fs->pwdmnt);
977 read_unlock(&current->fs->lock);
978 }
979
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
981
982/* Intercept a putname request. Called from
983 * include/linux/fs.h:putname(). If we have stored the name from
984 * getname in the audit context, then we delay the putname until syscall
985 * exit. */
986void audit_putname(const char *name)
987{
988 struct audit_context *context = current->audit_context;
989
990 BUG_ON(!context);
991 if (!context->in_syscall) {
992#if AUDIT_DEBUG == 2
993 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
994 __FILE__, __LINE__, context->serial, name);
995 if (context->name_count) {
996 int i;
997 for (i = 0; i < context->name_count; i++)
998 printk(KERN_ERR "name[%d] = %p = %s\n", i,
999 context->names[i].name,
1000 context->names[i].name);
1001 }
1002#endif
1003 __putname(name);
1004 }
1005#if AUDIT_DEBUG
1006 else {
1007 ++context->put_count;
1008 if (context->put_count > context->name_count) {
1009 printk(KERN_ERR "%s:%d(:%d): major=%d"
1010 " in_syscall=%d putname(%p) name_count=%d"
1011 " put_count=%d\n",
1012 __FILE__, __LINE__,
1013 context->serial, context->major,
1014 context->in_syscall, name, context->name_count,
1015 context->put_count);
1016 dump_stack();
1017 }
1018 }
1019#endif
1020}
1021
1022/* Store the inode and device from a lookup. Called from
1023 * fs/namei.c:path_lookup(). */
David Woodhouseae7b9612005-06-20 16:11:05 +01001024void audit_inode(const char *name, const struct inode *inode, unsigned flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
1026 int idx;
1027 struct audit_context *context = current->audit_context;
1028
1029 if (!context->in_syscall)
1030 return;
1031 if (context->name_count
1032 && context->names[context->name_count-1].name
1033 && context->names[context->name_count-1].name == name)
1034 idx = context->name_count - 1;
1035 else if (context->name_count > 1
1036 && context->names[context->name_count-2].name
1037 && context->names[context->name_count-2].name == name)
1038 idx = context->name_count - 2;
1039 else {
1040 /* FIXME: how much do we care about inodes that have no
1041 * associated name? */
1042 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1043 return;
1044 idx = context->name_count++;
1045 context->names[idx].name = NULL;
1046#if AUDIT_DEBUG
1047 ++context->ino_count;
1048#endif
1049 }
David Woodhouseae7b9612005-06-20 16:11:05 +01001050 context->names[idx].flags = flags;
1051 context->names[idx].ino = inode->i_ino;
1052 context->names[idx].dev = inode->i_sb->s_dev;
1053 context->names[idx].mode = inode->i_mode;
1054 context->names[idx].uid = inode->i_uid;
1055 context->names[idx].gid = inode->i_gid;
1056 context->names[idx].rdev = inode->i_rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
David Woodhousebfb44962005-05-21 21:08:09 +01001059void auditsc_get_stamp(struct audit_context *ctx,
1060 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
David Woodhousebfb44962005-05-21 21:08:09 +01001062 t->tv_sec = ctx->ctime.tv_sec;
1063 t->tv_nsec = ctx->ctime.tv_nsec;
1064 *serial = ctx->serial;
1065 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
Steve Grubb456be6c2005-04-29 17:30:07 +01001068int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Steve Grubb456be6c2005-04-29 17:30:07 +01001070 if (task->audit_context) {
Steve Grubbc0404992005-05-13 18:17:42 +01001071 struct audit_buffer *ab;
1072
1073 ab = audit_log_start(NULL, AUDIT_LOGIN);
1074 if (ab) {
1075 audit_log_format(ab, "login pid=%d uid=%u "
Steve Grubb326e9c82005-05-21 00:22:31 +01001076 "old auid=%u new auid=%u",
Steve Grubbc0404992005-05-13 18:17:42 +01001077 task->pid, task->uid,
1078 task->audit_context->loginuid, loginuid);
1079 audit_log_end(ab);
1080 }
Steve Grubb456be6c2005-04-29 17:30:07 +01001081 task->audit_context->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083 return 0;
1084}
1085
1086uid_t audit_get_loginuid(struct audit_context *ctx)
1087{
1088 return ctx ? ctx->loginuid : -1;
1089}
1090
1091int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
1092{
1093 struct audit_aux_data_ipcctl *ax;
1094 struct audit_context *context = current->audit_context;
1095
1096 if (likely(!context))
1097 return 0;
1098
1099 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
1100 if (!ax)
1101 return -ENOMEM;
1102
1103 ax->qbytes = qbytes;
1104 ax->uid = uid;
1105 ax->gid = gid;
1106 ax->mode = mode;
1107
Steve Grubbc0404992005-05-13 18:17:42 +01001108 ax->d.type = AUDIT_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 ax->d.next = context->aux;
1110 context->aux = (void *)ax;
1111 return 0;
1112}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001113
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001114int audit_socketcall(int nargs, unsigned long *args)
1115{
1116 struct audit_aux_data_socketcall *ax;
1117 struct audit_context *context = current->audit_context;
1118
1119 if (likely(!context))
1120 return 0;
1121
1122 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1123 if (!ax)
1124 return -ENOMEM;
1125
1126 ax->nargs = nargs;
1127 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1128
1129 ax->d.type = AUDIT_SOCKETCALL;
1130 ax->d.next = context->aux;
1131 context->aux = (void *)ax;
1132 return 0;
1133}
1134
1135int audit_sockaddr(int len, void *a)
1136{
1137 struct audit_aux_data_sockaddr *ax;
1138 struct audit_context *context = current->audit_context;
1139
1140 if (likely(!context))
1141 return 0;
1142
1143 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1144 if (!ax)
1145 return -ENOMEM;
1146
1147 ax->len = len;
1148 memcpy(ax->a, a, len);
1149
1150 ax->d.type = AUDIT_SOCKADDR;
1151 ax->d.next = context->aux;
1152 context->aux = (void *)ax;
1153 return 0;
1154}
1155
Stephen Smalley01116102005-05-21 00:15:52 +01001156int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1157{
1158 struct audit_aux_data_path *ax;
1159 struct audit_context *context = current->audit_context;
1160
1161 if (likely(!context))
1162 return 0;
1163
1164 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1165 if (!ax)
1166 return -ENOMEM;
1167
1168 ax->dentry = dget(dentry);
1169 ax->mnt = mntget(mnt);
1170
1171 ax->d.type = AUDIT_AVC_PATH;
1172 ax->d.next = context->aux;
1173 context->aux = (void *)ax;
1174 return 0;
1175}
1176
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001177void audit_signal_info(int sig, struct task_struct *t)
1178{
1179 extern pid_t audit_sig_pid;
1180 extern uid_t audit_sig_uid;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001181
1182 if (unlikely(audit_pid && t->pid == audit_pid)) {
1183 if (sig == SIGTERM || sig == SIGHUP) {
1184 struct audit_context *ctx = current->audit_context;
1185 audit_sig_pid = current->pid;
1186 if (ctx)
1187 audit_sig_uid = ctx->loginuid;
1188 else
1189 audit_sig_uid = current->uid;
1190 }
1191 }
1192}
1193