AUDIT: Speed up audit_filter_syscall() for the non-auditable case.
[deliverable/linux.git] / kernel / auditsc.c
CommitLineData
85c8721f 1/* auditsc.c -- System-call auditing support
1da177e4
LT
2 * 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>
01116105 37#include <linux/mount.h>
3ec3b2fb 38#include <linux/socket.h>
1da177e4
LT
39#include <linux/audit.h>
40#include <linux/personality.h>
41#include <linux/time.h>
f6a789d1 42#include <linux/kthread.h>
5bb289b5 43#include <linux/netlink.h>
f5561964 44#include <linux/compiler.h>
1da177e4
LT
45#include <asm/unistd.h>
46
47/* 0 = no checking
48 1 = put_count checking
49 2 = verbose put_count checking
50*/
51#define AUDIT_DEBUG 0
52
53/* No syscall auditing will take place unless audit_enabled != 0. */
54extern int audit_enabled;
55
56/* AUDIT_NAMES is the number of slots we reserve in the audit_context
57 * for saving names from getname(). */
58#define AUDIT_NAMES 20
59
60/* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
61 * audit_context from being used for nameless inodes from
62 * path_lookup. */
63#define AUDIT_NAMES_RESERVED 7
64
65/* At task start time, the audit_state is set in the audit_context using
66 a per-task filter. At syscall entry, the audit_state is augmented by
67 the syscall filter. */
68enum audit_state {
69 AUDIT_DISABLED, /* Do not create per-task audit_context.
70 * No syscall-specific audit records can
71 * be generated. */
72 AUDIT_SETUP_CONTEXT, /* Create the per-task audit_context,
73 * but don't necessarily fill it in at
74 * syscall entry time (i.e., filter
75 * instead). */
76 AUDIT_BUILD_CONTEXT, /* Create the per-task audit_context,
77 * and always fill it in at syscall
78 * entry time. This makes a full
79 * syscall record available if some
80 * other part of the kernel decides it
81 * should be recorded. */
82 AUDIT_RECORD_CONTEXT /* Create the per-task audit_context,
83 * always fill it in at syscall entry
84 * time, and always write out the audit
85 * record at syscall exit time. */
86};
87
88/* When fs/namei.c:getname() is called, we store the pointer in name and
89 * we don't let putname() free it (instead we free all of the saved
90 * pointers at syscall exit time).
91 *
92 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
93struct audit_names {
94 const char *name;
95 unsigned long ino;
96 dev_t dev;
97 umode_t mode;
98 uid_t uid;
99 gid_t gid;
100 dev_t rdev;
ae7b961b 101 unsigned flags;
1da177e4
LT
102};
103
104struct audit_aux_data {
105 struct audit_aux_data *next;
106 int type;
107};
108
109#define AUDIT_AUX_IPCPERM 0
110
111struct audit_aux_data_ipcctl {
112 struct audit_aux_data d;
113 struct ipc_perm p;
114 unsigned long qbytes;
115 uid_t uid;
116 gid_t gid;
117 mode_t mode;
118};
119
3ec3b2fb
DW
120struct audit_aux_data_socketcall {
121 struct audit_aux_data d;
122 int nargs;
123 unsigned long args[0];
124};
125
126struct audit_aux_data_sockaddr {
127 struct audit_aux_data d;
128 int len;
129 char a[0];
130};
131
01116105
SS
132struct audit_aux_data_path {
133 struct audit_aux_data d;
134 struct dentry *dentry;
135 struct vfsmount *mnt;
136};
1da177e4
LT
137
138/* The per-task audit context. */
139struct audit_context {
140 int in_syscall; /* 1 if task is in a syscall */
141 enum audit_state state;
142 unsigned int serial; /* serial number for record */
143 struct timespec ctime; /* time of syscall entry */
144 uid_t loginuid; /* login uid (identity) */
145 int major; /* syscall number */
146 unsigned long argv[4]; /* syscall arguments */
147 int return_valid; /* return code is valid */
2fd6f58b 148 long return_code;/* syscall return code */
1da177e4
LT
149 int auditable; /* 1 if record should be written */
150 int name_count;
151 struct audit_names names[AUDIT_NAMES];
8f37d47c
DW
152 struct dentry * pwd;
153 struct vfsmount * pwdmnt;
1da177e4
LT
154 struct audit_context *previous; /* For nested syscalls */
155 struct audit_aux_data *aux;
156
157 /* Save things to print about task_struct */
158 pid_t pid;
159 uid_t uid, euid, suid, fsuid;
160 gid_t gid, egid, sgid, fsgid;
161 unsigned long personality;
2fd6f58b 162 int arch;
1da177e4
LT
163
164#if AUDIT_DEBUG
165 int put_count;
166 int ino_count;
167#endif
168};
169
170 /* Public API */
171/* There are three lists of rules -- one to search at task creation
172 * time, one to search at syscall entry time, and another to search at
173 * syscall exit time. */
0f45aa18
DW
174static struct list_head audit_filter_list[AUDIT_NR_FILTERS] = {
175 LIST_HEAD_INIT(audit_filter_list[0]),
176 LIST_HEAD_INIT(audit_filter_list[1]),
177 LIST_HEAD_INIT(audit_filter_list[2]),
178 LIST_HEAD_INIT(audit_filter_list[3]),
179 LIST_HEAD_INIT(audit_filter_list[4]),
180#if AUDIT_NR_FILTERS != 5
181#error Fix audit_filter_list initialiser
182#endif
183};
1da177e4
LT
184
185struct audit_entry {
186 struct list_head list;
187 struct rcu_head rcu;
188 struct audit_rule rule;
189};
190
7ca00264
DW
191extern int audit_pid;
192
1da177e4
LT
193/* Check to see if two rules are identical. It is called from
194 * audit_del_rule during AUDIT_DEL. */
195static int audit_compare_rule(struct audit_rule *a, struct audit_rule *b)
196{
197 int i;
198
199 if (a->flags != b->flags)
200 return 1;
201
202 if (a->action != b->action)
203 return 1;
204
205 if (a->field_count != b->field_count)
206 return 1;
207
208 for (i = 0; i < a->field_count; i++) {
209 if (a->fields[i] != b->fields[i]
210 || a->values[i] != b->values[i])
211 return 1;
212 }
213
214 for (i = 0; i < AUDIT_BITMASK_SIZE; i++)
215 if (a->mask[i] != b->mask[i])
216 return 1;
217
218 return 0;
219}
220
221/* Note that audit_add_rule and audit_del_rule are called via
222 * audit_receive() in audit.c, and are protected by
223 * audit_netlink_sem. */
0f45aa18
DW
224static inline void audit_add_rule(struct audit_entry *entry,
225 struct list_head *list)
1da177e4 226{
0f45aa18
DW
227 if (entry->rule.flags & AUDIT_FILTER_PREPEND) {
228 entry->rule.flags &= ~AUDIT_FILTER_PREPEND;
1da177e4
LT
229 list_add_rcu(&entry->list, list);
230 } else {
231 list_add_tail_rcu(&entry->list, list);
232 }
1da177e4
LT
233}
234
235static void audit_free_rule(struct rcu_head *head)
236{
237 struct audit_entry *e = container_of(head, struct audit_entry, rcu);
238 kfree(e);
239}
240
241/* Note that audit_add_rule and audit_del_rule are called via
242 * audit_receive() in audit.c, and are protected by
243 * audit_netlink_sem. */
244static inline int audit_del_rule(struct audit_rule *rule,
245 struct list_head *list)
246{
247 struct audit_entry *e;
248
249 /* Do not use the _rcu iterator here, since this is the only
250 * deletion routine. */
251 list_for_each_entry(e, list, list) {
252 if (!audit_compare_rule(rule, &e->rule)) {
253 list_del_rcu(&e->list);
254 call_rcu(&e->rcu, audit_free_rule);
255 return 0;
256 }
257 }
0f45aa18 258 return -ENOENT; /* No matching rule */
1da177e4
LT
259}
260
1da177e4
LT
261/* Copy rule from user-space to kernel-space. Called during
262 * AUDIT_ADD. */
263static int audit_copy_rule(struct audit_rule *d, struct audit_rule *s)
264{
265 int i;
266
267 if (s->action != AUDIT_NEVER
268 && s->action != AUDIT_POSSIBLE
269 && s->action != AUDIT_ALWAYS)
270 return -1;
271 if (s->field_count < 0 || s->field_count > AUDIT_MAX_FIELDS)
272 return -1;
0f45aa18
DW
273 if ((s->flags & ~AUDIT_FILTER_PREPEND) >= AUDIT_NR_FILTERS)
274 return -1;
1da177e4
LT
275
276 d->flags = s->flags;
277 d->action = s->action;
278 d->field_count = s->field_count;
279 for (i = 0; i < d->field_count; i++) {
280 d->fields[i] = s->fields[i];
281 d->values[i] = s->values[i];
282 }
283 for (i = 0; i < AUDIT_BITMASK_SIZE; i++) d->mask[i] = s->mask[i];
284 return 0;
285}
286
f6a789d1
DW
287static int audit_list_rules(void *_dest)
288{
289 int pid, seq;
290 int *dest = _dest;
291 struct audit_entry *entry;
292 int i;
293
294 pid = dest[0];
295 seq = dest[1];
296 kfree(dest);
297
298 down(&audit_netlink_sem);
299
300 /* The *_rcu iterators not needed here because we are
301 always called with audit_netlink_sem held. */
302 for (i=0; i<AUDIT_NR_FILTERS; i++) {
303 list_for_each_entry(entry, &audit_filter_list[i], list)
304 audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
305 &entry->rule, sizeof(entry->rule));
306 }
307 audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
308
309 up(&audit_netlink_sem);
310 return 0;
311}
312
c94c257c
SH
313int audit_receive_filter(int type, int pid, int uid, int seq, void *data,
314 uid_t loginuid)
1da177e4 315{
1da177e4 316 struct audit_entry *entry;
f6a789d1
DW
317 struct task_struct *tsk;
318 int *dest;
1da177e4 319 int err = 0;
0f45aa18 320 unsigned listnr;
1da177e4
LT
321
322 switch (type) {
323 case AUDIT_LIST:
f6a789d1
DW
324 /* We can't just spew out the rules here because we might fill
325 * the available socket buffer space and deadlock waiting for
326 * auditctl to read from it... which isn't ever going to
327 * happen if we're actually running in the context of auditctl
328 * trying to _send_ the stuff */
329
330 dest = kmalloc(2 * sizeof(int), GFP_KERNEL);
331 if (!dest)
332 return -ENOMEM;
333 dest[0] = pid;
334 dest[1] = seq;
335
336 tsk = kthread_run(audit_list_rules, dest, "audit_list_rules");
337 if (IS_ERR(tsk)) {
338 kfree(dest);
339 err = PTR_ERR(tsk);
0f45aa18 340 }
1da177e4
LT
341 break;
342 case AUDIT_ADD:
343 if (!(entry = kmalloc(sizeof(*entry), GFP_KERNEL)))
344 return -ENOMEM;
345 if (audit_copy_rule(&entry->rule, data)) {
346 kfree(entry);
347 return -EINVAL;
348 }
0f45aa18
DW
349 listnr = entry->rule.flags & ~AUDIT_FILTER_PREPEND;
350 audit_add_rule(entry, &audit_filter_list[listnr]);
9ad9ad38 351 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
bccf6ae0 352 "auid=%u added an audit rule\n", loginuid);
1da177e4
LT
353 break;
354 case AUDIT_DEL:
0f45aa18
DW
355 listnr =((struct audit_rule *)data)->flags & ~AUDIT_FILTER_PREPEND;
356 if (listnr >= AUDIT_NR_FILTERS)
357 return -EINVAL;
358
359 err = audit_del_rule(data, &audit_filter_list[listnr]);
360 if (!err)
9ad9ad38 361 audit_log(NULL, GFP_KERNEL, AUDIT_CONFIG_CHANGE,
0f45aa18 362 "auid=%u removed an audit rule\n", loginuid);
1da177e4
LT
363 break;
364 default:
365 return -EINVAL;
366 }
367
368 return err;
369}
1da177e4
LT
370
371/* Compare a task_struct with an audit_rule. Return 1 on match, 0
372 * otherwise. */
373static int audit_filter_rules(struct task_struct *tsk,
374 struct audit_rule *rule,
375 struct audit_context *ctx,
376 enum audit_state *state)
377{
378 int i, j;
379
380 for (i = 0; i < rule->field_count; i++) {
381 u32 field = rule->fields[i] & ~AUDIT_NEGATE;
382 u32 value = rule->values[i];
383 int result = 0;
384
385 switch (field) {
386 case AUDIT_PID:
387 result = (tsk->pid == value);
388 break;
389 case AUDIT_UID:
390 result = (tsk->uid == value);
391 break;
392 case AUDIT_EUID:
393 result = (tsk->euid == value);
394 break;
395 case AUDIT_SUID:
396 result = (tsk->suid == value);
397 break;
398 case AUDIT_FSUID:
399 result = (tsk->fsuid == value);
400 break;
401 case AUDIT_GID:
402 result = (tsk->gid == value);
403 break;
404 case AUDIT_EGID:
405 result = (tsk->egid == value);
406 break;
407 case AUDIT_SGID:
408 result = (tsk->sgid == value);
409 break;
410 case AUDIT_FSGID:
411 result = (tsk->fsgid == value);
412 break;
413 case AUDIT_PERS:
414 result = (tsk->personality == value);
415 break;
2fd6f58b 416 case AUDIT_ARCH:
417 if (ctx)
418 result = (ctx->arch == value);
419 break;
1da177e4
LT
420
421 case AUDIT_EXIT:
422 if (ctx && ctx->return_valid)
423 result = (ctx->return_code == value);
424 break;
425 case AUDIT_SUCCESS:
426 if (ctx && ctx->return_valid)
2fd6f58b 427 result = (ctx->return_valid == AUDITSC_SUCCESS);
1da177e4
LT
428 break;
429 case AUDIT_DEVMAJOR:
430 if (ctx) {
431 for (j = 0; j < ctx->name_count; j++) {
432 if (MAJOR(ctx->names[j].dev)==value) {
433 ++result;
434 break;
435 }
436 }
437 }
438 break;
439 case AUDIT_DEVMINOR:
440 if (ctx) {
441 for (j = 0; j < ctx->name_count; j++) {
442 if (MINOR(ctx->names[j].dev)==value) {
443 ++result;
444 break;
445 }
446 }
447 }
448 break;
449 case AUDIT_INODE:
450 if (ctx) {
451 for (j = 0; j < ctx->name_count; j++) {
452 if (ctx->names[j].ino == value) {
453 ++result;
454 break;
455 }
456 }
457 }
458 break;
459 case AUDIT_LOGINUID:
460 result = 0;
461 if (ctx)
462 result = (ctx->loginuid == value);
463 break;
464 case AUDIT_ARG0:
465 case AUDIT_ARG1:
466 case AUDIT_ARG2:
467 case AUDIT_ARG3:
468 if (ctx)
469 result = (ctx->argv[field-AUDIT_ARG0]==value);
470 break;
471 }
472
473 if (rule->fields[i] & AUDIT_NEGATE)
474 result = !result;
475 if (!result)
476 return 0;
477 }
478 switch (rule->action) {
479 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
480 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
481 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
482 }
483 return 1;
484}
485
486/* At process creation time, we can determine if system-call auditing is
487 * completely disabled for this task. Since we only have the task
488 * structure at this point, we can only check uid and gid.
489 */
490static enum audit_state audit_filter_task(struct task_struct *tsk)
491{
492 struct audit_entry *e;
493 enum audit_state state;
494
495 rcu_read_lock();
0f45aa18 496 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_TASK], list) {
1da177e4
LT
497 if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
498 rcu_read_unlock();
499 return state;
500 }
501 }
502 rcu_read_unlock();
503 return AUDIT_BUILD_CONTEXT;
504}
505
506/* At syscall entry and exit time, this filter is called if the
507 * audit_state is not low enough that auditing cannot take place, but is
23f32d18 508 * also not high enough that we already know we have to write an audit
1da177e4
LT
509 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
510 */
511static enum audit_state audit_filter_syscall(struct task_struct *tsk,
512 struct audit_context *ctx,
513 struct list_head *list)
514{
515 struct audit_entry *e;
c3896495 516 enum audit_state state;
1da177e4 517
351bb722 518 if (audit_pid && tsk->tgid == audit_pid)
f7056d64
DW
519 return AUDIT_DISABLED;
520
1da177e4 521 rcu_read_lock();
c3896495
DW
522 if (!list_empty(list)) {
523 int word = AUDIT_WORD(ctx->major);
524 int bit = AUDIT_BIT(ctx->major);
525
526 list_for_each_entry_rcu(e, list, list) {
527 if ((e->rule.mask[word] & bit) == bit
528 && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
529 rcu_read_unlock();
530 return state;
531 }
532 }
1da177e4
LT
533 }
534 rcu_read_unlock();
535 return AUDIT_BUILD_CONTEXT;
536}
537
5bb289b5
DW
538static int audit_filter_user_rules(struct netlink_skb_parms *cb,
539 struct audit_rule *rule,
540 enum audit_state *state)
541{
542 int i;
543
544 for (i = 0; i < rule->field_count; i++) {
545 u32 field = rule->fields[i] & ~AUDIT_NEGATE;
546 u32 value = rule->values[i];
547 int result = 0;
548
549 switch (field) {
550 case AUDIT_PID:
551 result = (cb->creds.pid == value);
552 break;
553 case AUDIT_UID:
554 result = (cb->creds.uid == value);
555 break;
556 case AUDIT_GID:
557 result = (cb->creds.gid == value);
558 break;
559 case AUDIT_LOGINUID:
560 result = (cb->loginuid == value);
561 break;
562 }
563
564 if (rule->fields[i] & AUDIT_NEGATE)
565 result = !result;
566 if (!result)
567 return 0;
568 }
569 switch (rule->action) {
570 case AUDIT_NEVER: *state = AUDIT_DISABLED; break;
571 case AUDIT_POSSIBLE: *state = AUDIT_BUILD_CONTEXT; break;
572 case AUDIT_ALWAYS: *state = AUDIT_RECORD_CONTEXT; break;
573 }
574 return 1;
575}
576
577int audit_filter_user(struct netlink_skb_parms *cb, int type)
0f45aa18
DW
578{
579 struct audit_entry *e;
580 enum audit_state state;
4a4cd633 581 int ret = 1;
0f45aa18
DW
582
583 rcu_read_lock();
584 list_for_each_entry_rcu(e, &audit_filter_list[AUDIT_FILTER_USER], list) {
5bb289b5 585 if (audit_filter_user_rules(cb, &e->rule, &state)) {
4a4cd633
DW
586 if (state == AUDIT_DISABLED)
587 ret = 0;
588 break;
0f45aa18
DW
589 }
590 }
591 rcu_read_unlock();
4a4cd633 592
993e2d41 593 return ret; /* Audit by default */
0f45aa18
DW
594}
595
1da177e4
LT
596/* This should be called with task_lock() held. */
597static inline struct audit_context *audit_get_context(struct task_struct *tsk,
598 int return_valid,
599 int return_code)
600{
601 struct audit_context *context = tsk->audit_context;
602
603 if (likely(!context))
604 return NULL;
605 context->return_valid = return_valid;
606 context->return_code = return_code;
607
21af6c4f 608 if (context->in_syscall && !context->auditable) {
1da177e4 609 enum audit_state state;
0f45aa18 610 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_EXIT]);
1da177e4
LT
611 if (state == AUDIT_RECORD_CONTEXT)
612 context->auditable = 1;
613 }
614
615 context->pid = tsk->pid;
616 context->uid = tsk->uid;
617 context->gid = tsk->gid;
618 context->euid = tsk->euid;
619 context->suid = tsk->suid;
620 context->fsuid = tsk->fsuid;
621 context->egid = tsk->egid;
622 context->sgid = tsk->sgid;
623 context->fsgid = tsk->fsgid;
624 context->personality = tsk->personality;
625 tsk->audit_context = NULL;
626 return context;
627}
628
629static inline void audit_free_names(struct audit_context *context)
630{
631 int i;
632
633#if AUDIT_DEBUG == 2
634 if (context->auditable
635 ||context->put_count + context->ino_count != context->name_count) {
636 printk(KERN_ERR "audit.c:%d(:%d): major=%d in_syscall=%d"
637 " name_count=%d put_count=%d"
638 " ino_count=%d [NOT freeing]\n",
639 __LINE__,
640 context->serial, context->major, context->in_syscall,
641 context->name_count, context->put_count,
642 context->ino_count);
643 for (i = 0; i < context->name_count; i++)
644 printk(KERN_ERR "names[%d] = %p = %s\n", i,
645 context->names[i].name,
646 context->names[i].name);
647 dump_stack();
648 return;
649 }
650#endif
651#if AUDIT_DEBUG
652 context->put_count = 0;
653 context->ino_count = 0;
654#endif
655
656 for (i = 0; i < context->name_count; i++)
657 if (context->names[i].name)
658 __putname(context->names[i].name);
659 context->name_count = 0;
8f37d47c
DW
660 if (context->pwd)
661 dput(context->pwd);
662 if (context->pwdmnt)
663 mntput(context->pwdmnt);
664 context->pwd = NULL;
665 context->pwdmnt = NULL;
1da177e4
LT
666}
667
668static inline void audit_free_aux(struct audit_context *context)
669{
670 struct audit_aux_data *aux;
671
672 while ((aux = context->aux)) {
01116105
SS
673 if (aux->type == AUDIT_AVC_PATH) {
674 struct audit_aux_data_path *axi = (void *)aux;
675 dput(axi->dentry);
676 mntput(axi->mnt);
677 }
1da177e4
LT
678 context->aux = aux->next;
679 kfree(aux);
680 }
681}
682
683static inline void audit_zero_context(struct audit_context *context,
684 enum audit_state state)
685{
686 uid_t loginuid = context->loginuid;
687
688 memset(context, 0, sizeof(*context));
689 context->state = state;
690 context->loginuid = loginuid;
691}
692
693static inline struct audit_context *audit_alloc_context(enum audit_state state)
694{
695 struct audit_context *context;
696
697 if (!(context = kmalloc(sizeof(*context), GFP_KERNEL)))
698 return NULL;
699 audit_zero_context(context, state);
700 return context;
701}
702
703/* Filter on the task information and allocate a per-task audit context
704 * if necessary. Doing so turns on system call auditing for the
705 * specified task. This is called from copy_process, so no lock is
706 * needed. */
707int audit_alloc(struct task_struct *tsk)
708{
709 struct audit_context *context;
710 enum audit_state state;
711
712 if (likely(!audit_enabled))
713 return 0; /* Return if not auditing. */
714
715 state = audit_filter_task(tsk);
716 if (likely(state == AUDIT_DISABLED))
717 return 0;
718
719 if (!(context = audit_alloc_context(state))) {
720 audit_log_lost("out of memory in audit_alloc");
721 return -ENOMEM;
722 }
723
724 /* Preserve login uid */
725 context->loginuid = -1;
726 if (current->audit_context)
727 context->loginuid = current->audit_context->loginuid;
728
729 tsk->audit_context = context;
730 set_tsk_thread_flag(tsk, TIF_SYSCALL_AUDIT);
731 return 0;
732}
733
734static inline void audit_free_context(struct audit_context *context)
735{
736 struct audit_context *previous;
737 int count = 0;
738
739 do {
740 previous = context->previous;
741 if (previous || (count && count < 10)) {
742 ++count;
743 printk(KERN_ERR "audit(:%d): major=%d name_count=%d:"
744 " freeing multiple contexts (%d)\n",
745 context->serial, context->major,
746 context->name_count, count);
747 }
748 audit_free_names(context);
749 audit_free_aux(context);
750 kfree(context);
751 context = previous;
752 } while (context);
753 if (count >= 10)
754 printk(KERN_ERR "audit: freed %d contexts\n", count);
755}
756
219f0817
SS
757static void audit_log_task_info(struct audit_buffer *ab)
758{
759 char name[sizeof(current->comm)];
760 struct mm_struct *mm = current->mm;
761 struct vm_area_struct *vma;
762
763 get_task_comm(name, current);
99e45eea
DW
764 audit_log_format(ab, " comm=");
765 audit_log_untrustedstring(ab, name);
219f0817
SS
766
767 if (!mm)
768 return;
769
770 down_read(&mm->mmap_sem);
771 vma = mm->mmap;
772 while (vma) {
773 if ((vma->vm_flags & VM_EXECUTABLE) &&
774 vma->vm_file) {
775 audit_log_d_path(ab, "exe=",
776 vma->vm_file->f_dentry,
777 vma->vm_file->f_vfsmnt);
778 break;
779 }
780 vma = vma->vm_next;
781 }
782 up_read(&mm->mmap_sem);
783}
784
f5561964 785static void audit_log_exit(struct audit_context *context, unsigned int gfp_mask)
1da177e4
LT
786{
787 int i;
788 struct audit_buffer *ab;
7551ced3 789 struct audit_aux_data *aux;
1da177e4 790
f5561964 791 ab = audit_log_start(context, gfp_mask, AUDIT_SYSCALL);
1da177e4
LT
792 if (!ab)
793 return; /* audit_panic has been called */
bccf6ae0
DW
794 audit_log_format(ab, "arch=%x syscall=%d",
795 context->arch, context->major);
1da177e4
LT
796 if (context->personality != PER_LINUX)
797 audit_log_format(ab, " per=%lx", context->personality);
798 if (context->return_valid)
2fd6f58b 799 audit_log_format(ab, " success=%s exit=%ld",
800 (context->return_valid==AUDITSC_SUCCESS)?"yes":"no",
801 context->return_code);
1da177e4
LT
802 audit_log_format(ab,
803 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
326e9c8b
SG
804 " pid=%d auid=%u uid=%u gid=%u"
805 " euid=%u suid=%u fsuid=%u"
806 " egid=%u sgid=%u fsgid=%u",
1da177e4
LT
807 context->argv[0],
808 context->argv[1],
809 context->argv[2],
810 context->argv[3],
811 context->name_count,
812 context->pid,
813 context->loginuid,
814 context->uid,
815 context->gid,
816 context->euid, context->suid, context->fsuid,
817 context->egid, context->sgid, context->fsgid);
219f0817 818 audit_log_task_info(ab);
1da177e4 819 audit_log_end(ab);
1da177e4 820
7551ced3 821 for (aux = context->aux; aux; aux = aux->next) {
c0404993 822
9ad9ad38 823 ab = audit_log_start(context, GFP_KERNEL, aux->type);
1da177e4
LT
824 if (!ab)
825 continue; /* audit_panic has been called */
826
1da177e4 827 switch (aux->type) {
c0404993 828 case AUDIT_IPC: {
1da177e4
LT
829 struct audit_aux_data_ipcctl *axi = (void *)aux;
830 audit_log_format(ab,
326e9c8b 831 " qbytes=%lx iuid=%u igid=%u mode=%x",
1da177e4 832 axi->qbytes, axi->uid, axi->gid, axi->mode);
3ec3b2fb
DW
833 break; }
834
835 case AUDIT_SOCKETCALL: {
836 int i;
837 struct audit_aux_data_socketcall *axs = (void *)aux;
838 audit_log_format(ab, "nargs=%d", axs->nargs);
839 for (i=0; i<axs->nargs; i++)
840 audit_log_format(ab, " a%d=%lx", i, axs->args[i]);
841 break; }
842
843 case AUDIT_SOCKADDR: {
844 struct audit_aux_data_sockaddr *axs = (void *)aux;
845
846 audit_log_format(ab, "saddr=");
847 audit_log_hex(ab, axs->a, axs->len);
848 break; }
01116105
SS
849
850 case AUDIT_AVC_PATH: {
851 struct audit_aux_data_path *axi = (void *)aux;
852 audit_log_d_path(ab, "path=", axi->dentry, axi->mnt);
01116105
SS
853 break; }
854
1da177e4
LT
855 }
856 audit_log_end(ab);
1da177e4
LT
857 }
858
8f37d47c 859 if (context->pwd && context->pwdmnt) {
9ad9ad38 860 ab = audit_log_start(context, GFP_KERNEL, AUDIT_CWD);
8f37d47c
DW
861 if (ab) {
862 audit_log_d_path(ab, "cwd=", context->pwd, context->pwdmnt);
863 audit_log_end(ab);
864 }
865 }
1da177e4 866 for (i = 0; i < context->name_count; i++) {
9ad9ad38 867 ab = audit_log_start(context, GFP_KERNEL, AUDIT_PATH);
1da177e4
LT
868 if (!ab)
869 continue; /* audit_panic has been called */
8f37d47c 870
1da177e4 871 audit_log_format(ab, "item=%d", i);
83c7d091 872 if (context->names[i].name) {
873 audit_log_format(ab, " name=");
874 audit_log_untrustedstring(ab, context->names[i].name);
875 }
ae7b961b
DW
876 audit_log_format(ab, " flags=%x\n", context->names[i].flags);
877
1da177e4
LT
878 if (context->names[i].ino != (unsigned long)-1)
879 audit_log_format(ab, " inode=%lu dev=%02x:%02x mode=%#o"
326e9c8b 880 " ouid=%u ogid=%u rdev=%02x:%02x",
1da177e4
LT
881 context->names[i].ino,
882 MAJOR(context->names[i].dev),
883 MINOR(context->names[i].dev),
884 context->names[i].mode,
885 context->names[i].uid,
886 context->names[i].gid,
887 MAJOR(context->names[i].rdev),
888 MINOR(context->names[i].rdev));
889 audit_log_end(ab);
890 }
891}
892
893/* Free a per-task audit context. Called from copy_process and
894 * __put_task_struct. */
895void audit_free(struct task_struct *tsk)
896{
897 struct audit_context *context;
898
899 task_lock(tsk);
900 context = audit_get_context(tsk, 0, 0);
901 task_unlock(tsk);
902
903 if (likely(!context))
904 return;
905
906 /* Check for system calls that do not go through the exit
f5561964
DW
907 * function (e.g., exit_group), then free context block.
908 * We use GFP_ATOMIC here because we might be doing this
909 * in the context of the idle thread */
f7056d64 910 if (context->in_syscall && context->auditable)
f5561964 911 audit_log_exit(context, GFP_ATOMIC);
1da177e4
LT
912
913 audit_free_context(context);
914}
915
1da177e4
LT
916/* Fill in audit context at syscall entry. This only happens if the
917 * audit context was created when the task was created and the state or
918 * filters demand the audit context be built. If the state from the
919 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
920 * then the record will be written at syscall exit time (otherwise, it
921 * will only be written if another part of the kernel requests that it
922 * be written). */
2fd6f58b 923void audit_syscall_entry(struct task_struct *tsk, int arch, int major,
1da177e4
LT
924 unsigned long a1, unsigned long a2,
925 unsigned long a3, unsigned long a4)
926{
927 struct audit_context *context = tsk->audit_context;
928 enum audit_state state;
929
930 BUG_ON(!context);
931
932 /* This happens only on certain architectures that make system
933 * calls in kernel_thread via the entry.S interface, instead of
934 * with direct calls. (If you are porting to a new
935 * architecture, hitting this condition can indicate that you
936 * got the _exit/_leave calls backward in entry.S.)
937 *
938 * i386 no
939 * x86_64 no
940 * ppc64 yes (see arch/ppc64/kernel/misc.S)
941 *
942 * This also happens with vm86 emulation in a non-nested manner
943 * (entries without exits), so this case must be caught.
944 */
945 if (context->in_syscall) {
946 struct audit_context *newctx;
947
948#if defined(__NR_vm86) && defined(__NR_vm86old)
949 /* vm86 mode should only be entered once */
950 if (major == __NR_vm86 || major == __NR_vm86old)
951 return;
952#endif
953#if AUDIT_DEBUG
954 printk(KERN_ERR
955 "audit(:%d) pid=%d in syscall=%d;"
956 " entering syscall=%d\n",
957 context->serial, tsk->pid, context->major, major);
958#endif
959 newctx = audit_alloc_context(context->state);
960 if (newctx) {
961 newctx->previous = context;
962 context = newctx;
963 tsk->audit_context = newctx;
964 } else {
965 /* If we can't alloc a new context, the best we
966 * can do is to leak memory (any pending putname
967 * will be lost). The only other alternative is
968 * to abandon auditing. */
969 audit_zero_context(context, context->state);
970 }
971 }
972 BUG_ON(context->in_syscall || context->name_count);
973
974 if (!audit_enabled)
975 return;
976
2fd6f58b 977 context->arch = arch;
1da177e4
LT
978 context->major = major;
979 context->argv[0] = a1;
980 context->argv[1] = a2;
981 context->argv[2] = a3;
982 context->argv[3] = a4;
983
984 state = context->state;
985 if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
0f45aa18 986 state = audit_filter_syscall(tsk, context, &audit_filter_list[AUDIT_FILTER_ENTRY]);
1da177e4
LT
987 if (likely(state == AUDIT_DISABLED))
988 return;
989
ce625a80 990 context->serial = 0;
1da177e4
LT
991 context->ctime = CURRENT_TIME;
992 context->in_syscall = 1;
993 context->auditable = !!(state == AUDIT_RECORD_CONTEXT);
994}
995
996/* Tear down after system call. If the audit context has been marked as
997 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
998 * filtering, or because some other part of the kernel write an audit
999 * message), then write out the syscall information. In call cases,
1000 * free the names stored from getname(). */
2fd6f58b 1001void audit_syscall_exit(struct task_struct *tsk, int valid, long return_code)
1da177e4
LT
1002{
1003 struct audit_context *context;
1004
1005 get_task_struct(tsk);
1006 task_lock(tsk);
2fd6f58b 1007 context = audit_get_context(tsk, valid, return_code);
1da177e4
LT
1008 task_unlock(tsk);
1009
1010 /* Not having a context here is ok, since the parent may have
1011 * called __put_task_struct. */
1012 if (likely(!context))
413a1c75 1013 goto out;
1da177e4 1014
f7056d64 1015 if (context->in_syscall && context->auditable)
f5561964 1016 audit_log_exit(context, GFP_KERNEL);
1da177e4
LT
1017
1018 context->in_syscall = 0;
1019 context->auditable = 0;
2fd6f58b 1020
1da177e4
LT
1021 if (context->previous) {
1022 struct audit_context *new_context = context->previous;
1023 context->previous = NULL;
1024 audit_free_context(context);
1025 tsk->audit_context = new_context;
1026 } else {
1027 audit_free_names(context);
1028 audit_free_aux(context);
1da177e4
LT
1029 tsk->audit_context = context;
1030 }
413a1c75 1031 out:
1da177e4
LT
1032 put_task_struct(tsk);
1033}
1034
1035/* Add a name to the list. Called from fs/namei.c:getname(). */
1036void audit_getname(const char *name)
1037{
1038 struct audit_context *context = current->audit_context;
1039
1040 if (!context || IS_ERR(name) || !name)
1041 return;
1042
1043 if (!context->in_syscall) {
1044#if AUDIT_DEBUG == 2
1045 printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
1046 __FILE__, __LINE__, context->serial, name);
1047 dump_stack();
1048#endif
1049 return;
1050 }
1051 BUG_ON(context->name_count >= AUDIT_NAMES);
1052 context->names[context->name_count].name = name;
1053 context->names[context->name_count].ino = (unsigned long)-1;
1054 ++context->name_count;
8f37d47c
DW
1055 if (!context->pwd) {
1056 read_lock(&current->fs->lock);
1057 context->pwd = dget(current->fs->pwd);
1058 context->pwdmnt = mntget(current->fs->pwdmnt);
1059 read_unlock(&current->fs->lock);
1060 }
1061
1da177e4
LT
1062}
1063
1064/* Intercept a putname request. Called from
1065 * include/linux/fs.h:putname(). If we have stored the name from
1066 * getname in the audit context, then we delay the putname until syscall
1067 * exit. */
1068void audit_putname(const char *name)
1069{
1070 struct audit_context *context = current->audit_context;
1071
1072 BUG_ON(!context);
1073 if (!context->in_syscall) {
1074#if AUDIT_DEBUG == 2
1075 printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
1076 __FILE__, __LINE__, context->serial, name);
1077 if (context->name_count) {
1078 int i;
1079 for (i = 0; i < context->name_count; i++)
1080 printk(KERN_ERR "name[%d] = %p = %s\n", i,
1081 context->names[i].name,
1082 context->names[i].name);
1083 }
1084#endif
1085 __putname(name);
1086 }
1087#if AUDIT_DEBUG
1088 else {
1089 ++context->put_count;
1090 if (context->put_count > context->name_count) {
1091 printk(KERN_ERR "%s:%d(:%d): major=%d"
1092 " in_syscall=%d putname(%p) name_count=%d"
1093 " put_count=%d\n",
1094 __FILE__, __LINE__,
1095 context->serial, context->major,
1096 context->in_syscall, name, context->name_count,
1097 context->put_count);
1098 dump_stack();
1099 }
1100 }
1101#endif
1102}
1103
1104/* Store the inode and device from a lookup. Called from
1105 * fs/namei.c:path_lookup(). */
ae7b961b 1106void audit_inode(const char *name, const struct inode *inode, unsigned flags)
1da177e4
LT
1107{
1108 int idx;
1109 struct audit_context *context = current->audit_context;
1110
1111 if (!context->in_syscall)
1112 return;
1113 if (context->name_count
1114 && context->names[context->name_count-1].name
1115 && context->names[context->name_count-1].name == name)
1116 idx = context->name_count - 1;
1117 else if (context->name_count > 1
1118 && context->names[context->name_count-2].name
1119 && context->names[context->name_count-2].name == name)
1120 idx = context->name_count - 2;
1121 else {
1122 /* FIXME: how much do we care about inodes that have no
1123 * associated name? */
1124 if (context->name_count >= AUDIT_NAMES - AUDIT_NAMES_RESERVED)
1125 return;
1126 idx = context->name_count++;
1127 context->names[idx].name = NULL;
1128#if AUDIT_DEBUG
1129 ++context->ino_count;
1130#endif
1131 }
ae7b961b
DW
1132 context->names[idx].flags = flags;
1133 context->names[idx].ino = inode->i_ino;
1134 context->names[idx].dev = inode->i_sb->s_dev;
1135 context->names[idx].mode = inode->i_mode;
1136 context->names[idx].uid = inode->i_uid;
1137 context->names[idx].gid = inode->i_gid;
1138 context->names[idx].rdev = inode->i_rdev;
1da177e4
LT
1139}
1140
bfb4496e
DW
1141void auditsc_get_stamp(struct audit_context *ctx,
1142 struct timespec *t, unsigned int *serial)
1da177e4 1143{
ce625a80
DW
1144 if (!ctx->serial)
1145 ctx->serial = audit_serial();
bfb4496e
DW
1146 t->tv_sec = ctx->ctime.tv_sec;
1147 t->tv_nsec = ctx->ctime.tv_nsec;
1148 *serial = ctx->serial;
1149 ctx->auditable = 1;
1da177e4
LT
1150}
1151
456be6cd 1152int audit_set_loginuid(struct task_struct *task, uid_t loginuid)
1da177e4 1153{
456be6cd 1154 if (task->audit_context) {
c0404993
SG
1155 struct audit_buffer *ab;
1156
9ad9ad38 1157 ab = audit_log_start(NULL, GFP_KERNEL, AUDIT_LOGIN);
c0404993
SG
1158 if (ab) {
1159 audit_log_format(ab, "login pid=%d uid=%u "
326e9c8b 1160 "old auid=%u new auid=%u",
c0404993
SG
1161 task->pid, task->uid,
1162 task->audit_context->loginuid, loginuid);
1163 audit_log_end(ab);
1164 }
456be6cd 1165 task->audit_context->loginuid = loginuid;
1da177e4
LT
1166 }
1167 return 0;
1168}
1169
1170uid_t audit_get_loginuid(struct audit_context *ctx)
1171{
1172 return ctx ? ctx->loginuid : -1;
1173}
1174
1175int audit_ipc_perms(unsigned long qbytes, uid_t uid, gid_t gid, mode_t mode)
1176{
1177 struct audit_aux_data_ipcctl *ax;
1178 struct audit_context *context = current->audit_context;
1179
1180 if (likely(!context))
1181 return 0;
1182
1183 ax = kmalloc(sizeof(*ax), GFP_KERNEL);
1184 if (!ax)
1185 return -ENOMEM;
1186
1187 ax->qbytes = qbytes;
1188 ax->uid = uid;
1189 ax->gid = gid;
1190 ax->mode = mode;
1191
c0404993 1192 ax->d.type = AUDIT_IPC;
1da177e4
LT
1193 ax->d.next = context->aux;
1194 context->aux = (void *)ax;
1195 return 0;
1196}
c2f0c7c3 1197
3ec3b2fb
DW
1198int audit_socketcall(int nargs, unsigned long *args)
1199{
1200 struct audit_aux_data_socketcall *ax;
1201 struct audit_context *context = current->audit_context;
1202
1203 if (likely(!context))
1204 return 0;
1205
1206 ax = kmalloc(sizeof(*ax) + nargs * sizeof(unsigned long), GFP_KERNEL);
1207 if (!ax)
1208 return -ENOMEM;
1209
1210 ax->nargs = nargs;
1211 memcpy(ax->args, args, nargs * sizeof(unsigned long));
1212
1213 ax->d.type = AUDIT_SOCKETCALL;
1214 ax->d.next = context->aux;
1215 context->aux = (void *)ax;
1216 return 0;
1217}
1218
1219int audit_sockaddr(int len, void *a)
1220{
1221 struct audit_aux_data_sockaddr *ax;
1222 struct audit_context *context = current->audit_context;
1223
1224 if (likely(!context))
1225 return 0;
1226
1227 ax = kmalloc(sizeof(*ax) + len, GFP_KERNEL);
1228 if (!ax)
1229 return -ENOMEM;
1230
1231 ax->len = len;
1232 memcpy(ax->a, a, len);
1233
1234 ax->d.type = AUDIT_SOCKADDR;
1235 ax->d.next = context->aux;
1236 context->aux = (void *)ax;
1237 return 0;
1238}
1239
01116105
SS
1240int audit_avc_path(struct dentry *dentry, struct vfsmount *mnt)
1241{
1242 struct audit_aux_data_path *ax;
1243 struct audit_context *context = current->audit_context;
1244
1245 if (likely(!context))
1246 return 0;
1247
1248 ax = kmalloc(sizeof(*ax), GFP_ATOMIC);
1249 if (!ax)
1250 return -ENOMEM;
1251
1252 ax->dentry = dget(dentry);
1253 ax->mnt = mntget(mnt);
1254
1255 ax->d.type = AUDIT_AVC_PATH;
1256 ax->d.next = context->aux;
1257 context->aux = (void *)ax;
1258 return 0;
1259}
1260
c2f0c7c3
SG
1261void audit_signal_info(int sig, struct task_struct *t)
1262{
1263 extern pid_t audit_sig_pid;
1264 extern uid_t audit_sig_uid;
c2f0c7c3 1265
582edda5 1266 if (unlikely(audit_pid && t->tgid == audit_pid)) {
c2f0c7c3
SG
1267 if (sig == SIGTERM || sig == SIGHUP) {
1268 struct audit_context *ctx = current->audit_context;
1269 audit_sig_pid = current->pid;
1270 if (ctx)
1271 audit_sig_uid = ctx->loginuid;
1272 else
1273 audit_sig_uid = current->uid;
1274 }
1275 }
1276}
1277
This page took 0.13053 seconds and 5 git commands to generate.