blob: 6b4fbb1c012957e8cd52d99f942cc3a8a438e02c [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;
98};
99
100struct audit_aux_data {
101 struct audit_aux_data *next;
102 int type;
103};
104
105#define AUDIT_AUX_IPCPERM 0
106
107struct audit_aux_data_ipcctl {
108 struct audit_aux_data d;
109 struct ipc_perm p;
110 unsigned long qbytes;
111 uid_t uid;
112 gid_t gid;
113 mode_t mode;
114};
115
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100116struct audit_aux_data_socketcall {
117 struct audit_aux_data d;
118 int nargs;
119 unsigned long args[0];
120};
121
122struct audit_aux_data_sockaddr {
123 struct audit_aux_data d;
124 int len;
125 char a[0];
126};
127
Stephen Smalley01116102005-05-21 00:15:52 +0100128struct audit_aux_data_path {
129 struct audit_aux_data d;
130 struct dentry *dentry;
131 struct vfsmount *mnt;
132};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134/* The per-task audit context. */
135struct audit_context {
136 int in_syscall; /* 1 if task is in a syscall */
137 enum audit_state state;
138 unsigned int serial; /* serial number for record */
139 struct timespec ctime; /* time of syscall entry */
140 uid_t loginuid; /* login uid (identity) */
141 int major; /* syscall number */
142 unsigned long argv[4]; /* syscall arguments */
143 int return_valid; /* return code is valid */
2fd6f582005-04-29 16:08:28 +0100144 long return_code;/* syscall return code */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 int auditable; /* 1 if record should be written */
146 int name_count;
147 struct audit_names names[AUDIT_NAMES];
David Woodhouse8f37d472005-05-27 12:17:28 +0100148 struct dentry * pwd;
149 struct vfsmount * pwdmnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 struct audit_context *previous; /* For nested syscalls */
151 struct audit_aux_data *aux;
152
153 /* Save things to print about task_struct */
154 pid_t pid;
155 uid_t uid, euid, suid, fsuid;
156 gid_t gid, egid, sgid, fsgid;
157 unsigned long personality;
2fd6f582005-04-29 16:08:28 +0100158 int arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160#if AUDIT_DEBUG
161 int put_count;
162 int ino_count;
163#endif
164};
165
166 /* Public API */
167/* There are three lists of rules -- one to search at task creation
168 * time, one to search at syscall entry time, and another to search at
169 * syscall exit time. */
David Woodhouse0f45aa12005-06-19 19:35:50 +0100170static struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
171 LIST_HEAD_INIT(audit_filter_list[0]),
172 LIST_HEAD_INIT(audit_filter_list[1]),
173 LIST_HEAD_INIT(audit_filter_list[2]),
174 LIST_HEAD_INIT(audit_filter_list[3]),
175 LIST_HEAD_INIT(audit_filter_list[4]),
176#if AUDIT_NR_FILTERS != 5
177#error Fix audit_filter_list initialiser
178#endif
179};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181struct audit_entry {
182 struct list_head list;
183 struct rcu_head rcu;
184 struct audit_rule rule;
185};
186
David Woodhouse7ca00262005-05-19 11:23:13 +0100187extern int audit_pid;
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/* Check to see if two rules are identical. It is called from
190 * audit_del_rule during AUDIT_DEL. */
191static int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
192{
193 int i;
194
195 if (a->flags != b->flags)
196 return 1;
197
198 if (a->action != b->action)
199 return 1;
200
201 if (a->field_count != b->field_count)
202 return 1;
203
204 for (i = 0; i < a->field_count; i++) {
205 if (a->fields[i] != b->fields[i]
206 || a->values[i] != b->values[i])
207 return 1;
208 }
209
210 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
211 if (a->mask[i] != b->mask[i])
212 return 1;
213
214 return 0;
215}
216
217/* Note that audit_add_rule and audit_del_rule are called via
218 * audit_receive() in audit.c, and are protected by
219 * audit_netlink_sem. */
David Woodhouse0f45aa12005-06-19 19:35:50 +0100220static inline void audit_add_rule(struct audit_entry *entry,
221 struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
David Woodhouse0f45aa12005-06-19 19:35:50 +0100223 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
224 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 list_add_rcu(&entry->list, list);
226 } else {
227 list_add_tail_rcu(&entry->list, list);
228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
231static void audit_free_rule(struct rcu_head *head)
232{
233 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
234 kfree(e);
235}
236
237/* Note that audit_add_rule and audit_del_rule are called via
238 * audit_receive() in audit.c, and are protected by
239 * audit_netlink_sem. */
240static inline int audit_del_rule(struct audit_rule *rule,
241 struct list_head *list)
242{
243 struct audit_entry *e;
244
245 /* Do not use the _rcu iterator here, since this is the only
246 * deletion routine. */
247 list_for_each_entry(e, list, list) {
248 if (!audit_compare_rule(rule, &e->rule)) {
249 list_del_rcu(&e->list);
250 call_rcu(&e->rcu, audit_free_rule);
251 return 0;
252 }
253 }
David Woodhouse0f45aa12005-06-19 19:35:50 +0100254 return -ENOENT; /* No matching rule */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257/* Copy rule from user-space to kernel-space. Called during
258 * AUDIT_ADD. */
259static int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
260{
261 int i;
262
263 if (s->action != AUDIT_NEVER
264 && s->action != AUDIT_POSSIBLE
265 && s->action != AUDIT_ALWAYS)
266 return -1;
267 if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
268 return -1;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100269 if ((s->flags & ~AUDIT_FILTER_PREPEND) >= AUDIT_NR_FILTERS)
270 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
272 d->flags = s->flags;
273 d->action = s->action;
274 d->field_count = s->field_count;
275 for (i = 0; i < d->field_count; i++) {
276 d->fields[i] = s->fields[i];
277 d->values[i] = s->values[i];
278 }
279 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
280 return 0;
281}
282
Serge Hallync94c2572005-04-29 16:27:17 +0100283int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
284 uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 struct audit_entry *entry;
287 int err = 0;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100288 int i;
289 unsigned listnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290
291 switch (type) {
292 case AUDIT_LIST:
293 /* The *_rcu iterators not needed here because we are
294 always called with audit_netlink_sem held. */
David Woodhouse0f45aa12005-06-19 19:35:50 +0100295 for (i=0; i<AUDIT_NR_FILTERS; i++) {
296 list_for_each_entry(entry, &audit_filter_list[i], list)
297 audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
298 &entry->rule, sizeof(entry->rule));
299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
301 break;
302 case AUDIT_ADD:
303 if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
304 return -ENOMEM;
305 if (audit_copy_rule(&entry->rule, data)) {
306 kfree(entry);
307 return -EINVAL;
308 }
David Woodhouse0f45aa12005-06-19 19:35:50 +0100309 listnr = entry->rule.flags & ~AUDIT_FILTER_PREPEND;
310 audit_add_rule(entry, &audit_filter_list[listnr]);
Steve Grubbc0404992005-05-13 18:17:42 +0100311 audit_log(NULL, AUDIT_CONFIG_CHANGE,
David Woodhousebccf6ae2005-05-23 21:35:28 +0100312 "auid=%u added an audit rule\n", loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 break;
314 case AUDIT_DEL:
David Woodhouse0f45aa12005-06-19 19:35:50 +0100315 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
316 if (listnr >= AUDIT_NR_FILTERS)
317 return -EINVAL;
318
319 err = audit_del_rule(data, &audit_filter_list[listnr]);
320 if (!err)
321 audit_log(NULL, AUDIT_CONFIG_CHANGE,
322 "auid=%u removed an audit rule\n", loginuid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 break;
324 default:
325 return -EINVAL;
326 }
327
328 return err;
329}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331/* Compare a task_struct with an audit_rule. Return 1 on match, 0
332 * otherwise. */
333static int audit_filter_rules(struct task_struct *tsk,
334 struct audit_rule *rule,
335 struct audit_context *ctx,
336 enum audit_state *state)
337{
338 int i, j;
339
340 for (i = 0; i < rule->field_count; i++) {
341 u32 field = rule->fields[i] & ~AUDIT_NEGATE;
342 u32 value = rule->values[i];
343 int result = 0;
344
345 switch (field) {
346 case AUDIT_PID:
347 result = (tsk->pid == value);
348 break;
349 case AUDIT_UID:
350 result = (tsk->uid == value);
351 break;
352 case AUDIT_EUID:
353 result = (tsk->euid == value);
354 break;
355 case AUDIT_SUID:
356 result = (tsk->suid == value);
357 break;
358 case AUDIT_FSUID:
359 result = (tsk->fsuid == value);
360 break;
361 case AUDIT_GID:
362 result = (tsk->gid == value);
363 break;
364 case AUDIT_EGID:
365 result = (tsk->egid == value);
366 break;
367 case AUDIT_SGID:
368 result = (tsk->sgid == value);
369 break;
370 case AUDIT_FSGID:
371 result = (tsk->fsgid == value);
372 break;
373 case AUDIT_PERS:
374 result = (tsk->personality == value);
375 break;
2fd6f582005-04-29 16:08:28 +0100376 case AUDIT_ARCH:
377 if (ctx)
378 result = (ctx->arch == value);
379 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 case AUDIT_EXIT:
382 if (ctx && ctx->return_valid)
383 result = (ctx->return_code == value);
384 break;
385 case AUDIT_SUCCESS:
386 if (ctx && ctx->return_valid)
2fd6f582005-04-29 16:08:28 +0100387 result = (ctx->return_valid == AUDITSC_SUCCESS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 break;
389 case AUDIT_DEVMAJOR:
390 if (ctx) {
391 for (j = 0; j < ctx->name_count; j++) {
392 if (MAJOR(ctx->names[j].dev)==value) {
393 ++result;
394 break;
395 }
396 }
397 }
398 break;
399 case AUDIT_DEVMINOR:
400 if (ctx) {
401 for (j = 0; j < ctx->name_count; j++) {
402 if (MINOR(ctx->names[j].dev)==value) {
403 ++result;
404 break;
405 }
406 }
407 }
408 break;
409 case AUDIT_INODE:
410 if (ctx) {
411 for (j = 0; j < ctx->name_count; j++) {
412 if (ctx->names[j].ino == value) {
413 ++result;
414 break;
415 }
416 }
417 }
418 break;
419 case AUDIT_LOGINUID:
420 result = 0;
421 if (ctx)
422 result = (ctx->loginuid == value);
423 break;
424 case AUDIT_ARG0:
425 case AUDIT_ARG1:
426 case AUDIT_ARG2:
427 case AUDIT_ARG3:
428 if (ctx)
429 result = (ctx->argv[field-AUDIT_ARG0]==value);
430 break;
431 }
432
433 if (rule->fields[i] & AUDIT_NEGATE)
434 result = !result;
435 if (!result)
436 return 0;
437 }
438 switch (rule->action) {
439 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
440 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
441 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
442 }
443 return 1;
444}
445
446/* At process creation time, we can determine if system-call auditing is
447 * completely disabled for this task. Since we only have the task
448 * structure at this point, we can only check uid and gid.
449 */
450static enum audit_state audit_filter_task(struct task_struct *tsk)
451{
452 struct audit_entry *e;
453 enum audit_state state;
454
455 rcu_read_lock();
David Woodhouse0f45aa12005-06-19 19:35:50 +0100456 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
458 rcu_read_unlock();
459 return state;
460 }
461 }
462 rcu_read_unlock();
463 return AUDIT_BUILD_CONTEXT;
464}
465
466/* At syscall entry and exit time, this filter is called if the
467 * audit_state is not low enough that auditing cannot take place, but is
Steve Grubb23f32d12005-05-13 18:35:15 +0100468 * also not high enough that we already know we have to write an audit
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
470 */
471static enum audit_state audit_filter_syscall(struct task_struct *tsk,
472 struct audit_context *ctx,
473 struct list_head *list)
474{
475 struct audit_entry *e;
476 enum audit_state state;
477 int word = AUDIT_WORD(ctx->major);
478 int bit = AUDIT_BIT(ctx->major);
479
480 rcu_read_lock();
481 list_for_each_entry_rcu(e, list, list) {
482 if ((e->rule.mask[word] & bit) == bit
483 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
484 rcu_read_unlock();
485 return state;
486 }
487 }
488 rcu_read_unlock();
489 return AUDIT_BUILD_CONTEXT;
490}
491
David Woodhouse0f45aa12005-06-19 19:35:50 +0100492int audit_filter_user(struct task_struct *tsk, int type)
493{
494 struct audit_entry *e;
495 enum audit_state state;
496
497 rcu_read_lock();
498 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
499 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
500 rcu_read_unlock();
501 return state != AUDIT_DISABLED;
502 }
503 }
504 rcu_read_unlock();
505 return 1; /* Audit by default */
506
507}
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509/* This should be called with task_lock() held. */
510static inline struct audit_context *audit_get_context(struct task_struct *tsk,
511 int return_valid,
512 int return_code)
513{
514 struct audit_context *context = tsk->audit_context;
515
516 if (likely(!context))
517 return NULL;
518 context->return_valid = return_valid;
519 context->return_code = return_code;
520
521 if (context->in_syscall && !context->auditable) {
522 enum audit_state state;
David Woodhouse0f45aa12005-06-19 19:35:50 +0100523 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 if (state == AUDIT_RECORD_CONTEXT)
525 context->auditable = 1;
526 }
527
528 context->pid = tsk->pid;
529 context->uid = tsk->uid;
530 context->gid = tsk->gid;
531 context->euid = tsk->euid;
532 context->suid = tsk->suid;
533 context->fsuid = tsk->fsuid;
534 context->egid = tsk->egid;
535 context->sgid = tsk->sgid;
536 context->fsgid = tsk->fsgid;
537 context->personality = tsk->personality;
538 tsk->audit_context = NULL;
539 return context;
540}
541
542static inline void audit_free_names(struct audit_context *context)
543{
544 int i;
545
546#if AUDIT_DEBUG == 2
547 if (context->auditable
548 ||context->put_count + context->ino_count != context->name_count) {
549 printk(KERN_ERR "audit.c:%d(:%d): major=%d in_syscall=%d"
550 " name_count=%d put_count=%d"
551 " ino_count=%d [NOT freeing]\n",
552 __LINE__,
553 context->serial, context->major, context->in_syscall,
554 context->name_count, context->put_count,
555 context->ino_count);
556 for (i = 0; i < context->name_count; i++)
557 printk(KERN_ERR "names[%d] = %p = %s\n", i,
558 context->names[i].name,
559 context->names[i].name);
560 dump_stack();
561 return;
562 }
563#endif
564#if AUDIT_DEBUG
565 context->put_count = 0;
566 context->ino_count = 0;
567#endif
568
569 for (i = 0; i < context->name_count; i++)
570 if (context->names[i].name)
571 __putname(context->names[i].name);
572 context->name_count = 0;
David Woodhouse8f37d472005-05-27 12:17:28 +0100573 if (context->pwd)
574 dput(context->pwd);
575 if (context->pwdmnt)
576 mntput(context->pwdmnt);
577 context->pwd = NULL;
578 context->pwdmnt = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579}
580
581static inline void audit_free_aux(struct audit_context *context)
582{
583 struct audit_aux_data *aux;
584
585 while ((aux = context->aux)) {
Stephen Smalley01116102005-05-21 00:15:52 +0100586 if (aux->type == AUDIT_AVC_PATH) {
587 struct audit_aux_data_path *axi = (void *)aux;
588 dput(axi->dentry);
589 mntput(axi->mnt);
590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 context->aux = aux->next;
592 kfree(aux);
593 }
594}
595
596static inline void audit_zero_context(struct audit_context *context,
597 enum audit_state state)
598{
599 uid_t loginuid = context->loginuid;
600
601 memset(context, 0, sizeof(*context));
602 context->state = state;
603 context->loginuid = loginuid;
604}
605
606static inline struct audit_context *audit_alloc_context(enum audit_state state)
607{
608 struct audit_context *context;
609
610 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
611 return NULL;
612 audit_zero_context(context, state);
613 return context;
614}
615
616/* Filter on the task information and allocate a per-task audit context
617 * if necessary. Doing so turns on system call auditing for the
618 * specified task. This is called from copy_process, so no lock is
619 * needed. */
620int audit_alloc(struct task_struct *tsk)
621{
622 struct audit_context *context;
623 enum audit_state state;
624
625 if (likely(!audit_enabled))
626 return 0; /* Return if not auditing. */
627
628 state = audit_filter_task(tsk);
629 if (likely(state == AUDIT_DISABLED))
630 return 0;
631
632 if (!(context = audit_alloc_context(state))) {
633 audit_log_lost("out of memory in audit_alloc");
634 return -ENOMEM;
635 }
636
637 /* Preserve login uid */
638 context->loginuid = -1;
639 if (current->audit_context)
640 context->loginuid = current->audit_context->loginuid;
641
642 tsk->audit_context = context;
643 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
644 return 0;
645}
646
647static inline void audit_free_context(struct audit_context *context)
648{
649 struct audit_context *previous;
650 int count = 0;
651
652 do {
653 previous = context->previous;
654 if (previous || (count && count < 10)) {
655 ++count;
656 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
657 " freeing multiple contexts (%d)\n",
658 context->serial, context->major,
659 context->name_count, count);
660 }
661 audit_free_names(context);
662 audit_free_aux(context);
663 kfree(context);
664 context = previous;
665 } while (context);
666 if (count >= 10)
667 printk(KERN_ERR "audit: freed %d contexts\n", count);
668}
669
Stephen Smalley219f0812005-04-18 10:47:35 -0700670static void audit_log_task_info(struct audit_buffer *ab)
671{
672 char name[sizeof(current->comm)];
673 struct mm_struct *mm = current->mm;
674 struct vm_area_struct *vma;
675
676 get_task_comm(name, current);
David Woodhouse99e45ee2005-05-23 21:57:41 +0100677 audit_log_format(ab, " comm=");
678 audit_log_untrustedstring(ab, name);
Stephen Smalley219f0812005-04-18 10:47:35 -0700679
680 if (!mm)
681 return;
682
683 down_read(&mm->mmap_sem);
684 vma = mm->mmap;
685 while (vma) {
686 if ((vma->vm_flags & VM_EXECUTABLE) &&
687 vma->vm_file) {
688 audit_log_d_path(ab, "exe=",
689 vma->vm_file->f_dentry,
690 vma->vm_file->f_vfsmnt);
691 break;
692 }
693 vma = vma->vm_next;
694 }
695 up_read(&mm->mmap_sem);
696}
697
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698static void audit_log_exit(struct audit_context *context)
699{
700 int i;
701 struct audit_buffer *ab;
David Woodhouse7551ced2005-05-26 12:04:57 +0100702 struct audit_aux_data *aux;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Steve Grubbc0404992005-05-13 18:17:42 +0100704 ab = audit_log_start(context, AUDIT_SYSCALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (!ab)
706 return; /* audit_panic has been called */
David Woodhousebccf6ae2005-05-23 21:35:28 +0100707 audit_log_format(ab, "arch=%x syscall=%d",
708 context->arch, context->major);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 if (context->personality != PER_LINUX)
710 audit_log_format(ab, " per=%lx", context->personality);
711 if (context->return_valid)
2fd6f582005-04-29 16:08:28 +0100712 audit_log_format(ab, " success=%s exit=%ld",
713 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
714 context->return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 audit_log_format(ab,
716 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
Steve Grubb326e9c82005-05-21 00:22:31 +0100717 " pid=%d auid=%u uid=%u gid=%u"
718 " euid=%u suid=%u fsuid=%u"
719 " egid=%u sgid=%u fsgid=%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 context->argv[0],
721 context->argv[1],
722 context->argv[2],
723 context->argv[3],
724 context->name_count,
725 context->pid,
726 context->loginuid,
727 context->uid,
728 context->gid,
729 context->euid, context->suid, context->fsuid,
730 context->egid, context->sgid, context->fsgid);
Stephen Smalley219f0812005-04-18 10:47:35 -0700731 audit_log_task_info(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
David Woodhouse7551ced2005-05-26 12:04:57 +0100734 for (aux = context->aux; aux; aux = aux->next) {
Steve Grubbc0404992005-05-13 18:17:42 +0100735
736 ab = audit_log_start(context, aux->type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 if (!ab)
738 continue; /* audit_panic has been called */
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 switch (aux->type) {
Steve Grubbc0404992005-05-13 18:17:42 +0100741 case AUDIT_IPC: {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 struct audit_aux_data_ipcctl *axi = (void *)aux;
743 audit_log_format(ab,
Steve Grubb326e9c82005-05-21 00:22:31 +0100744 " qbytes=%lx iuid=%u igid=%u mode=%x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 axi->qbytes, axi->uid, axi->gid, axi->mode);
David Woodhouse3ec3b2f2005-05-17 12:08:48 +0100746 break; }
747
748 case AUDIT_SOCKETCALL: {
749 int i;
750 struct audit_aux_data_socketcall *axs = (void *)aux;
751 audit_log_format(ab, "nargs=%d", axs->nargs);
752 for (i=0; i<axs->nargs; i++)
753 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
754 break; }
755
756 case AUDIT_SOCKADDR: {
757 struct audit_aux_data_sockaddr *axs = (void *)aux;
758
759 audit_log_format(ab, "saddr=");
760 audit_log_hex(ab, axs->a, axs->len);
761 break; }
Stephen Smalley01116102005-05-21 00:15:52 +0100762
763 case AUDIT_AVC_PATH: {
764 struct audit_aux_data_path *axi = (void *)aux;
765 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
Stephen Smalley01116102005-05-21 00:15:52 +0100766 break; }
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769 audit_log_end(ab);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771
David Woodhouse8f37d472005-05-27 12:17:28 +0100772 if (context->pwd && context->pwdmnt) {
773 ab = audit_log_start(context, AUDIT_CWD);
774 if (ab) {
775 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
776 audit_log_end(ab);
777 }
778 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 for (i = 0; i < context->name_count; i++) {
Steve Grubbc0404992005-05-13 18:17:42 +0100780 ab = audit_log_start(context, AUDIT_PATH);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (!ab)
782 continue; /* audit_panic has been called */
David Woodhouse8f37d472005-05-27 12:17:28 +0100783
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 audit_log_format(ab, "item=%d", i);
83c7d092005-04-29 15:54:44 +0100785 if (context->names[i].name) {
786 audit_log_format(ab, " name=");
787 audit_log_untrustedstring(ab, context->names[i].name);
788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (context->names[i].ino != (unsigned long)-1)
790 audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#o"
Steve Grubb326e9c82005-05-21 00:22:31 +0100791 " ouid=%u ogid=%u rdev=%02x:%02x",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 context->names[i].ino,
793 MAJOR(context->names[i].dev),
794 MINOR(context->names[i].dev),
795 context->names[i].mode,
796 context->names[i].uid,
797 context->names[i].gid,
798 MAJOR(context->names[i].rdev),
799 MINOR(context->names[i].rdev));
800 audit_log_end(ab);
801 }
802}
803
804/* Free a per-task audit context. Called from copy_process and
805 * __put_task_struct. */
806void audit_free(struct task_struct *tsk)
807{
808 struct audit_context *context;
809
810 task_lock(tsk);
811 context = audit_get_context(tsk, 0, 0);
812 task_unlock(tsk);
813
814 if (likely(!context))
815 return;
816
817 /* Check for system calls that do not go through the exit
818 * function (e.g., exit_group), then free context block. */
David Woodhouse7ca00262005-05-19 11:23:13 +0100819 if (context->in_syscall && context->auditable && context->pid != audit_pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 audit_log_exit(context);
821
822 audit_free_context(context);
823}
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825/* Fill in audit context at syscall entry. This only happens if the
826 * audit context was created when the task was created and the state or
827 * filters demand the audit context be built. If the state from the
828 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
829 * then the record will be written at syscall exit time (otherwise, it
830 * will only be written if another part of the kernel requests that it
831 * be written). */
2fd6f582005-04-29 16:08:28 +0100832void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 unsigned long a1, unsigned long a2,
834 unsigned long a3, unsigned long a4)
835{
836 struct audit_context *context = tsk->audit_context;
837 enum audit_state state;
838
839 BUG_ON(!context);
840
841 /* This happens only on certain architectures that make system
842 * calls in kernel_thread via the entry.S interface, instead of
843 * with direct calls. (If you are porting to a new
844 * architecture, hitting this condition can indicate that you
845 * got the _exit/_leave calls backward in entry.S.)
846 *
847 * i386 no
848 * x86_64 no
849 * ppc64 yes (see arch/ppc64/kernel/misc.S)
850 *
851 * This also happens with vm86 emulation in a non-nested manner
852 * (entries without exits), so this case must be caught.
853 */
854 if (context->in_syscall) {
855 struct audit_context *newctx;
856
857#if defined(__NR_vm86) && defined(__NR_vm86old)
858 /* vm86 mode should only be entered once */
859 if (major == __NR_vm86 || major == __NR_vm86old)
860 return;
861#endif
862#if AUDIT_DEBUG
863 printk(KERN_ERR
864 "audit(:%d) pid=%d in syscall=%d;"
865 " entering syscall=%d\n",
866 context->serial, tsk->pid, context->major, major);
867#endif
868 newctx = audit_alloc_context(context->state);
869 if (newctx) {
870 newctx->previous = context;
871 context = newctx;
872 tsk->audit_context = newctx;
873 } else {
874 /* If we can't alloc a new context, the best we
875 * can do is to leak memory (any pending putname
876 * will be lost). The only other alternative is
877 * to abandon auditing. */
878 audit_zero_context(context, context->state);
879 }
880 }
881 BUG_ON(context->in_syscall || context->name_count);
882
883 if (!audit_enabled)
884 return;
885
2fd6f582005-04-29 16:08:28 +0100886 context->arch = arch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 context->major = major;
888 context->argv[0] = a1;
889 context->argv[1] = a2;
890 context->argv[2] = a3;
891 context->argv[3] = a4;
892
893 state = context->state;
894 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
David Woodhouse0f45aa12005-06-19 19:35:50 +0100895 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (likely(state == AUDIT_DISABLED))
897 return;
898
899 context->serial = audit_serial();
900 context->ctime = CURRENT_TIME;
901 context->in_syscall = 1;
902 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
903}
904
905/* Tear down after system call. If the audit context has been marked as
906 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
907 * filtering, or because some other part of the kernel write an audit
908 * message), then write out the syscall information. In call cases,
909 * free the names stored from getname(). */
2fd6f582005-04-29 16:08:28 +0100910void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911{
912 struct audit_context *context;
913
914 get_task_struct(tsk);
915 task_lock(tsk);
2fd6f582005-04-29 16:08:28 +0100916 context = audit_get_context(tsk, valid, return_code);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 task_unlock(tsk);
918
919 /* Not having a context here is ok, since the parent may have
920 * called __put_task_struct. */
921 if (likely(!context))
922 return;
923
David Woodhouse7ca00262005-05-19 11:23:13 +0100924 if (context->in_syscall && context->auditable && context->pid != audit_pid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 audit_log_exit(context);
926
927 context->in_syscall = 0;
928 context->auditable = 0;
2fd6f582005-04-29 16:08:28 +0100929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (context->previous) {
931 struct audit_context *new_context = context->previous;
932 context->previous = NULL;
933 audit_free_context(context);
934 tsk->audit_context = new_context;
935 } else {
936 audit_free_names(context);
937 audit_free_aux(context);
938 audit_zero_context(context, context->state);
939 tsk->audit_context = context;
940 }
941 put_task_struct(tsk);
942}
943
944/* Add a name to the list. Called from fs/namei.c:getname(). */
945void audit_getname(const char *name)
946{
947 struct audit_context *context = current->audit_context;
948
949 if (!context || IS_ERR(name) || !name)
950 return;
951
952 if (!context->in_syscall) {
953#if AUDIT_DEBUG == 2
954 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
955 __FILE__, __LINE__, context->serial, name);
956 dump_stack();
957#endif
958 return;
959 }
960 BUG_ON(context->name_count >= AUDIT_NAMES);
961 context->names[context->name_count].name = name;
962 context->names[context->name_count].ino = (unsigned long)-1;
963 ++context->name_count;
David Woodhouse8f37d472005-05-27 12:17:28 +0100964 if (!context->pwd) {
965 read_lock(&current->fs->lock);
966 context->pwd = dget(current->fs->pwd);
967 context->pwdmnt = mntget(current->fs->pwdmnt);
968 read_unlock(&current->fs->lock);
969 }
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
973/* Intercept a putname request. Called from
974 * include/linux/fs.h:putname(). If we have stored the name from
975 * getname in the audit context, then we delay the putname until syscall
976 * exit. */
977void audit_putname(const char *name)
978{
979 struct audit_context *context = current->audit_context;
980
981 BUG_ON(!context);
982 if (!context->in_syscall) {
983#if AUDIT_DEBUG == 2
984 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
985 __FILE__, __LINE__, context->serial, name);
986 if (context->name_count) {
987 int i;
988 for (i = 0; i < context->name_count; i++)
989 printk(KERN_ERR "name[%d] = %p = %s\n", i,
990 context->names[i].name,
991 context->names[i].name);
992 }
993#endif
994 __putname(name);
995 }
996#if AUDIT_DEBUG
997 else {
998 ++context->put_count;
999 if (context->put_count > context->name_count) {
1000 printk(KERN_ERR "%s:%d(:%d): major=%d"
1001 " in_syscall=%d putname(%p) name_count=%d"
1002 " put_count=%d\n",
1003 __FILE__, __LINE__,
1004 context->serial, context->major,
1005 context->in_syscall, name, context->name_count,
1006 context->put_count);
1007 dump_stack();
1008 }
1009 }
1010#endif
1011}
1012
1013/* Store the inode and device from a lookup. Called from
1014 * fs/namei.c:path_lookup(). */
1015void audit_inode(const char *name, const struct inode *inode)
1016{
1017 int idx;
1018 struct audit_context *context = current->audit_context;
1019
1020 if (!context->in_syscall)
1021 return;
1022 if (context->name_count
1023 && context->names[context->name_count-1].name
1024 && context->names[context->name_count-1].name == name)
1025 idx = context->name_count - 1;
1026 else if (context->name_count > 1
1027 && context->names[context->name_count-2].name
1028 && context->names[context->name_count-2].name == name)
1029 idx = context->name_count - 2;
1030 else {
1031 /* FIXME: how much do we care about inodes that have no
1032 * associated name? */
1033 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1034 return;
1035 idx = context->name_count++;
1036 context->names[idx].name = NULL;
1037#if AUDIT_DEBUG
1038 ++context->ino_count;
1039#endif
1040 }
1041 context->names[idx].ino = inode->i_ino;
1042 context->names[idx].dev = inode->i_sb->s_dev;
1043 context->names[idx].mode = inode->i_mode;
1044 context->names[idx].uid = inode->i_uid;
1045 context->names[idx].gid = inode->i_gid;
1046 context->names[idx].rdev = inode->i_rdev;
1047}
1048
David Woodhousebfb44962005-05-21 21:08:09 +01001049void auditsc_get_stamp(struct audit_context *ctx,
1050 struct timespec *t, unsigned int *serial)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051{
David Woodhousebfb44962005-05-21 21:08:09 +01001052 t->tv_sec = ctx->ctime.tv_sec;
1053 t->tv_nsec = ctx->ctime.tv_nsec;
1054 *serial = ctx->serial;
1055 ctx->auditable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056}
1057
Steve Grubb456be6c2005-04-29 17:30:07 +01001058int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Steve Grubb456be6c2005-04-29 17:30:07 +01001060 if (task->audit_context) {
Steve Grubbc0404992005-05-13 18:17:42 +01001061 struct audit_buffer *ab;
1062
1063 ab = audit_log_start(NULL, AUDIT_LOGIN);
1064 if (ab) {
1065 audit_log_format(ab, "login pid=%d uid=%u "
Steve Grubb326e9c82005-05-21 00:22:31 +01001066 "old auid=%u new auid=%u",
Steve Grubbc0404992005-05-13 18:17:42 +01001067 task->pid, task->uid,
1068 task->audit_context->loginuid, loginuid);
1069 audit_log_end(ab);
1070 }
Steve Grubb456be6c2005-04-29 17:30:07 +01001071 task->audit_context->loginuid = loginuid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 }
1073 return 0;
1074}
1075
1076uid_t audit_get_loginuid(struct audit_context *ctx)
1077{
1078 return ctx ? ctx->loginuid : -1;
1079}
1080
1081int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
1082{
1083 struct audit_aux_data_ipcctl *ax;
1084 struct audit_context *context = current->audit_context;
1085
1086 if (likely(!context))
1087 return 0;
1088
1089 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
1090 if (!ax)
1091 return -ENOMEM;
1092
1093 ax->qbytes = qbytes;
1094 ax->uid = uid;
1095 ax->gid = gid;
1096 ax->mode = mode;
1097
Steve Grubbc0404992005-05-13 18:17:42 +01001098 ax->d.type = AUDIT_IPC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 ax->d.next = context->aux;
1100 context->aux = (void *)ax;
1101 return 0;
1102}
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001103
David Woodhouse3ec3b2f2005-05-17 12:08:48 +01001104int audit_socketcall(int nargs, unsigned long *args)
1105{
1106 struct audit_aux_data_socketcall *ax;
1107 struct audit_context *context = current->audit_context;
1108
1109 if (likely(!context))
1110 return 0;
1111
1112 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1113 if (!ax)
1114 return -ENOMEM;
1115
1116 ax->nargs = nargs;
1117 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1118
1119 ax->d.type = AUDIT_SOCKETCALL;
1120 ax->d.next = context->aux;
1121 context->aux = (void *)ax;
1122 return 0;
1123}
1124
1125int audit_sockaddr(int len, void *a)
1126{
1127 struct audit_aux_data_sockaddr *ax;
1128 struct audit_context *context = current->audit_context;
1129
1130 if (likely(!context))
1131 return 0;
1132
1133 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1134 if (!ax)
1135 return -ENOMEM;
1136
1137 ax->len = len;
1138 memcpy(ax->a, a, len);
1139
1140 ax->d.type = AUDIT_SOCKADDR;
1141 ax->d.next = context->aux;
1142 context->aux = (void *)ax;
1143 return 0;
1144}
1145
Stephen Smalley01116102005-05-21 00:15:52 +01001146int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1147{
1148 struct audit_aux_data_path *ax;
1149 struct audit_context *context = current->audit_context;
1150
1151 if (likely(!context))
1152 return 0;
1153
1154 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1155 if (!ax)
1156 return -ENOMEM;
1157
1158 ax->dentry = dget(dentry);
1159 ax->mnt = mntget(mnt);
1160
1161 ax->d.type = AUDIT_AVC_PATH;
1162 ax->d.next = context->aux;
1163 context->aux = (void *)ax;
1164 return 0;
1165}
1166
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001167void audit_signal_info(int sig, struct task_struct *t)
1168{
1169 extern pid_t audit_sig_pid;
1170 extern uid_t audit_sig_uid;
Steve Grubbc2f0c7c2005-05-06 12:38:39 +01001171
1172 if (unlikely(audit_pid && t->pid == audit_pid)) {
1173 if (sig == SIGTERM || sig == SIGHUP) {
1174 struct audit_context *ctx = current->audit_context;
1175 audit_sig_pid = current->pid;
1176 if (ctx)
1177 audit_sig_uid = ctx->loginuid;
1178 else
1179 audit_sig_uid = current->uid;
1180 }
1181 }
1182}
1183