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