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