1 /* auditsc.c -- System-call auditing support
2 * Handles all system-call specific auditing features.
4 * Copyright 2003-2004 Red Hat Inc., Durham, North Carolina.
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.
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.
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
21 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
23 * Many of the ideas implemented here are from Stephen C. Tweedie,
24 * especially the idea of avoiding a copy by using getname.
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.
32 #include <linux/init.h>
33 #include <asm/atomic.h>
34 #include <asm/types.h>
36 #include <linux/module.h>
37 #include <linux/mount.h>
38 #include <linux/socket.h>
39 #include <linux/audit.h>
40 #include <linux/personality.h>
41 #include <linux/time.h>
42 #include <asm/unistd.h>
45 1 = put_count checking
46 2 = verbose put_count checking
50 /* No syscall auditing will take place unless audit_enabled != 0. */
51 extern int audit_enabled
;
53 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
54 * for saving names from getname(). */
55 #define AUDIT_NAMES 20
57 /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
58 * audit_context from being used for nameless inodes from
60 #define AUDIT_NAMES_RESERVED 7
62 /* At task start time, the audit_state is set in the audit_context using
63 a per-task filter. At syscall entry, the audit_state is augmented by
64 the syscall filter. */
66 AUDIT_DISABLED
, /* Do not create per-task audit_context.
67 * No syscall-specific audit records can
69 AUDIT_SETUP_CONTEXT
, /* Create the per-task audit_context,
70 * but don't necessarily fill it in at
71 * syscall entry time (i.e., filter
73 AUDIT_BUILD_CONTEXT
, /* Create the per-task audit_context,
74 * and always fill it in at syscall
75 * entry time. This makes a full
76 * syscall record available if some
77 * other part of the kernel decides it
78 * should be recorded. */
79 AUDIT_RECORD_CONTEXT
/* Create the per-task audit_context,
80 * always fill it in at syscall entry
81 * time, and always write out the audit
82 * record at syscall exit time. */
85 /* When fs/namei.c:getname() is called, we store the pointer in name and
86 * we don't let putname() free it (instead we free all of the saved
87 * pointers at syscall exit time).
89 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
100 struct audit_aux_data
{
101 struct audit_aux_data
*next
;
105 #define AUDIT_AUX_IPCPERM 0
107 struct audit_aux_data_ipcctl
{
108 struct audit_aux_data d
;
110 unsigned long qbytes
;
116 struct audit_aux_data_socketcall
{
117 struct audit_aux_data d
;
119 unsigned long args
[0];
122 struct audit_aux_data_sockaddr
{
123 struct audit_aux_data d
;
128 struct audit_aux_data_path
{
129 struct audit_aux_data d
;
130 struct dentry
*dentry
;
131 struct vfsmount
*mnt
;
134 /* The per-task audit context. */
135 struct audit_context
{
136 int in_syscall
; /* 1 if task is in a syscall */
137 enum audit_state state
;
138 unsigned int serial
; /* serial number for record */
139 struct timespec ctime
; /* time of syscall entry */
140 uid_t loginuid
; /* login uid (identity) */
141 int major
; /* syscall number */
142 unsigned long argv
[4]; /* syscall arguments */
143 int return_valid
; /* return code is valid */
144 long return_code
;/* syscall return code */
145 int auditable
; /* 1 if record should be written */
147 struct audit_names names
[AUDIT_NAMES
];
148 struct audit_context
*previous
; /* For nested syscalls */
149 struct audit_aux_data
*aux
;
151 /* Save things to print about task_struct */
153 uid_t uid
, euid
, suid
, fsuid
;
154 gid_t gid
, egid
, sgid
, fsgid
;
155 unsigned long personality
;
165 /* There are three lists of rules -- one to search at task creation
166 * time, one to search at syscall entry time, and another to search at
167 * syscall exit time. */
168 static LIST_HEAD(audit_tsklist
);
169 static LIST_HEAD(audit_entlist
);
170 static LIST_HEAD(audit_extlist
);
173 struct list_head list
;
175 struct audit_rule rule
;
178 extern int audit_pid
;
180 /* Check to see if two rules are identical. It is called from
181 * audit_del_rule during AUDIT_DEL. */
182 static int audit_compare_rule(struct audit_rule
*a
, struct audit_rule
*b
)
186 if (a
->flags
!= b
->flags
)
189 if (a
->action
!= b
->action
)
192 if (a
->field_count
!= b
->field_count
)
195 for (i
= 0; i
< a
->field_count
; i
++) {
196 if (a
->fields
[i
] != b
->fields
[i
]
197 || a
->values
[i
] != b
->values
[i
])
201 for (i
= 0; i
< AUDIT_BITMASK_SIZE
; i
++)
202 if (a
->mask
[i
] != b
->mask
[i
])
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. */
211 static inline int audit_add_rule(struct audit_entry
*entry
,
212 struct list_head
*list
)
214 if (entry
->rule
.flags
& AUDIT_PREPEND
) {
215 entry
->rule
.flags
&= ~AUDIT_PREPEND
;
216 list_add_rcu(&entry
->list
, list
);
218 list_add_tail_rcu(&entry
->list
, list
);
223 static void audit_free_rule(struct rcu_head
*head
)
225 struct audit_entry
*e
= container_of(head
, struct audit_entry
, rcu
);
229 /* Note that audit_add_rule and audit_del_rule are called via
230 * audit_receive() in audit.c, and are protected by
231 * audit_netlink_sem. */
232 static inline int audit_del_rule(struct audit_rule
*rule
,
233 struct list_head
*list
)
235 struct audit_entry
*e
;
237 /* Do not use the _rcu iterator here, since this is the only
238 * deletion routine. */
239 list_for_each_entry(e
, list
, list
) {
240 if (!audit_compare_rule(rule
, &e
->rule
)) {
241 list_del_rcu(&e
->list
);
242 call_rcu(&e
->rcu
, audit_free_rule
);
246 return -EFAULT
; /* No matching rule */
249 /* Copy rule from user-space to kernel-space. Called during
251 static int audit_copy_rule(struct audit_rule
*d
, struct audit_rule
*s
)
255 if (s
->action
!= AUDIT_NEVER
256 && s
->action
!= AUDIT_POSSIBLE
257 && s
->action
!= AUDIT_ALWAYS
)
259 if (s
->field_count
< 0 || s
->field_count
> AUDIT_MAX_FIELDS
)
263 d
->action
= s
->action
;
264 d
->field_count
= s
->field_count
;
265 for (i
= 0; i
< d
->field_count
; i
++) {
266 d
->fields
[i
] = s
->fields
[i
];
267 d
->values
[i
] = s
->values
[i
];
269 for (i
= 0; i
< AUDIT_BITMASK_SIZE
; i
++) d
->mask
[i
] = s
->mask
[i
];
273 int audit_receive_filter(int type
, int pid
, int uid
, int seq
, void *data
,
277 struct audit_entry
*entry
;
282 /* The *_rcu iterators not needed here because we are
283 always called with audit_netlink_sem held. */
284 list_for_each_entry(entry
, &audit_tsklist
, list
)
285 audit_send_reply(pid
, seq
, AUDIT_LIST
, 0, 1,
286 &entry
->rule
, sizeof(entry
->rule
));
287 list_for_each_entry(entry
, &audit_entlist
, list
)
288 audit_send_reply(pid
, seq
, AUDIT_LIST
, 0, 1,
289 &entry
->rule
, sizeof(entry
->rule
));
290 list_for_each_entry(entry
, &audit_extlist
, list
)
291 audit_send_reply(pid
, seq
, AUDIT_LIST
, 0, 1,
292 &entry
->rule
, sizeof(entry
->rule
));
293 audit_send_reply(pid
, seq
, AUDIT_LIST
, 1, 1, NULL
, 0);
296 if (!(entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
)))
298 if (audit_copy_rule(&entry
->rule
, data
)) {
302 flags
= entry
->rule
.flags
;
303 if (!err
&& (flags
& AUDIT_PER_TASK
))
304 err
= audit_add_rule(entry
, &audit_tsklist
);
305 if (!err
&& (flags
& AUDIT_AT_ENTRY
))
306 err
= audit_add_rule(entry
, &audit_entlist
);
307 if (!err
&& (flags
& AUDIT_AT_EXIT
))
308 err
= audit_add_rule(entry
, &audit_extlist
);
309 audit_log(NULL
, AUDIT_CONFIG_CHANGE
,
310 "auid=%u added an audit rule\n", loginuid
);
313 flags
=((struct audit_rule
*)data
)->flags
;
314 if (!err
&& (flags
& AUDIT_PER_TASK
))
315 err
= audit_del_rule(data
, &audit_tsklist
);
316 if (!err
&& (flags
& AUDIT_AT_ENTRY
))
317 err
= audit_del_rule(data
, &audit_entlist
);
318 if (!err
&& (flags
& AUDIT_AT_EXIT
))
319 err
= audit_del_rule(data
, &audit_extlist
);
320 audit_log(NULL
, AUDIT_CONFIG_CHANGE
,
321 "auid=%u removed an audit rule\n", loginuid
);
330 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
332 static int audit_filter_rules(struct task_struct
*tsk
,
333 struct audit_rule
*rule
,
334 struct audit_context
*ctx
,
335 enum audit_state
*state
)
339 for (i
= 0; i
< rule
->field_count
; i
++) {
340 u32 field
= rule
->fields
[i
] & ~AUDIT_NEGATE
;
341 u32 value
= rule
->values
[i
];
346 result
= (tsk
->pid
== value
);
349 result
= (tsk
->uid
== value
);
352 result
= (tsk
->euid
== value
);
355 result
= (tsk
->suid
== value
);
358 result
= (tsk
->fsuid
== value
);
361 result
= (tsk
->gid
== value
);
364 result
= (tsk
->egid
== value
);
367 result
= (tsk
->sgid
== value
);
370 result
= (tsk
->fsgid
== value
);
373 result
= (tsk
->personality
== value
);
377 result
= (ctx
->arch
== value
);
381 if (ctx
&& ctx
->return_valid
)
382 result
= (ctx
->return_code
== value
);
385 if (ctx
&& ctx
->return_valid
)
386 result
= (ctx
->return_valid
== AUDITSC_SUCCESS
);
390 for (j
= 0; j
< ctx
->name_count
; j
++) {
391 if (MAJOR(ctx
->names
[j
].dev
)==value
) {
400 for (j
= 0; j
< ctx
->name_count
; j
++) {
401 if (MINOR(ctx
->names
[j
].dev
)==value
) {
410 for (j
= 0; j
< ctx
->name_count
; j
++) {
411 if (ctx
->names
[j
].ino
== value
) {
421 result
= (ctx
->loginuid
== value
);
428 result
= (ctx
->argv
[field
-AUDIT_ARG0
]==value
);
432 if (rule
->fields
[i
] & AUDIT_NEGATE
)
437 switch (rule
->action
) {
438 case AUDIT_NEVER
: *state
= AUDIT_DISABLED
; break;
439 case AUDIT_POSSIBLE
: *state
= AUDIT_BUILD_CONTEXT
; break;
440 case AUDIT_ALWAYS
: *state
= AUDIT_RECORD_CONTEXT
; break;
445 /* At process creation time, we can determine if system-call auditing is
446 * completely disabled for this task. Since we only have the task
447 * structure at this point, we can only check uid and gid.
449 static enum audit_state
audit_filter_task(struct task_struct
*tsk
)
451 struct audit_entry
*e
;
452 enum audit_state state
;
455 list_for_each_entry_rcu(e
, &audit_tsklist
, list
) {
456 if (audit_filter_rules(tsk
, &e
->rule
, NULL
, &state
)) {
462 return AUDIT_BUILD_CONTEXT
;
465 /* At syscall entry and exit time, this filter is called if the
466 * audit_state is not low enough that auditing cannot take place, but is
467 * also not high enough that we already know we have to write an audit
468 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
470 static enum audit_state
audit_filter_syscall(struct task_struct
*tsk
,
471 struct audit_context
*ctx
,
472 struct list_head
*list
)
474 struct audit_entry
*e
;
475 enum audit_state state
;
476 int word
= AUDIT_WORD(ctx
->major
);
477 int bit
= AUDIT_BIT(ctx
->major
);
480 list_for_each_entry_rcu(e
, list
, list
) {
481 if ((e
->rule
.mask
[word
] & bit
) == bit
482 && audit_filter_rules(tsk
, &e
->rule
, ctx
, &state
)) {
488 return AUDIT_BUILD_CONTEXT
;
491 /* This should be called with task_lock() held. */
492 static inline struct audit_context
*audit_get_context(struct task_struct
*tsk
,
496 struct audit_context
*context
= tsk
->audit_context
;
498 if (likely(!context
))
500 context
->return_valid
= return_valid
;
501 context
->return_code
= return_code
;
503 if (context
->in_syscall
&& !context
->auditable
) {
504 enum audit_state state
;
505 state
= audit_filter_syscall(tsk
, context
, &audit_extlist
);
506 if (state
== AUDIT_RECORD_CONTEXT
)
507 context
->auditable
= 1;
510 context
->pid
= tsk
->pid
;
511 context
->uid
= tsk
->uid
;
512 context
->gid
= tsk
->gid
;
513 context
->euid
= tsk
->euid
;
514 context
->suid
= tsk
->suid
;
515 context
->fsuid
= tsk
->fsuid
;
516 context
->egid
= tsk
->egid
;
517 context
->sgid
= tsk
->sgid
;
518 context
->fsgid
= tsk
->fsgid
;
519 context
->personality
= tsk
->personality
;
520 tsk
->audit_context
= NULL
;
524 static inline void audit_free_names(struct audit_context
*context
)
529 if (context
->auditable
530 ||context
->put_count
+ context
->ino_count
!= context
->name_count
) {
531 printk(KERN_ERR
"audit.c:%d(:%d): major=%d in_syscall=%d"
532 " name_count=%d put_count=%d"
533 " ino_count=%d [NOT freeing]\n",
535 context
->serial
, context
->major
, context
->in_syscall
,
536 context
->name_count
, context
->put_count
,
538 for (i
= 0; i
< context
->name_count
; i
++)
539 printk(KERN_ERR
"names[%d] = %p = %s\n", i
,
540 context
->names
[i
].name
,
541 context
->names
[i
].name
);
547 context
->put_count
= 0;
548 context
->ino_count
= 0;
551 for (i
= 0; i
< context
->name_count
; i
++)
552 if (context
->names
[i
].name
)
553 __putname(context
->names
[i
].name
);
554 context
->name_count
= 0;
557 static inline void audit_free_aux(struct audit_context
*context
)
559 struct audit_aux_data
*aux
;
561 while ((aux
= context
->aux
)) {
562 if (aux
->type
== AUDIT_AVC_PATH
) {
563 struct audit_aux_data_path
*axi
= (void *)aux
;
567 context
->aux
= aux
->next
;
572 static inline void audit_zero_context(struct audit_context
*context
,
573 enum audit_state state
)
575 uid_t loginuid
= context
->loginuid
;
577 memset(context
, 0, sizeof(*context
));
578 context
->state
= state
;
579 context
->loginuid
= loginuid
;
582 static inline struct audit_context
*audit_alloc_context(enum audit_state state
)
584 struct audit_context
*context
;
586 if (!(context
= kmalloc(sizeof(*context
), GFP_KERNEL
)))
588 audit_zero_context(context
, state
);
592 /* Filter on the task information and allocate a per-task audit context
593 * if necessary. Doing so turns on system call auditing for the
594 * specified task. This is called from copy_process, so no lock is
596 int audit_alloc(struct task_struct
*tsk
)
598 struct audit_context
*context
;
599 enum audit_state state
;
601 if (likely(!audit_enabled
))
602 return 0; /* Return if not auditing. */
604 state
= audit_filter_task(tsk
);
605 if (likely(state
== AUDIT_DISABLED
))
608 if (!(context
= audit_alloc_context(state
))) {
609 audit_log_lost("out of memory in audit_alloc");
613 /* Preserve login uid */
614 context
->loginuid
= -1;
615 if (current
->audit_context
)
616 context
->loginuid
= current
->audit_context
->loginuid
;
618 tsk
->audit_context
= context
;
619 set_tsk_thread_flag(tsk
, TIF_SYSCALL_AUDIT
);
623 static inline void audit_free_context(struct audit_context
*context
)
625 struct audit_context
*previous
;
629 previous
= context
->previous
;
630 if (previous
|| (count
&& count
< 10)) {
632 printk(KERN_ERR
"audit(:%d): major=%d name_count=%d:"
633 " freeing multiple contexts (%d)\n",
634 context
->serial
, context
->major
,
635 context
->name_count
, count
);
637 audit_free_names(context
);
638 audit_free_aux(context
);
643 printk(KERN_ERR
"audit: freed %d contexts\n", count
);
646 static void audit_log_task_info(struct audit_buffer
*ab
)
648 char name
[sizeof(current
->comm
)];
649 struct mm_struct
*mm
= current
->mm
;
650 struct vm_area_struct
*vma
;
652 get_task_comm(name
, current
);
653 audit_log_format(ab
, " comm=");
654 audit_log_untrustedstring(ab
, name
);
659 down_read(&mm
->mmap_sem
);
662 if ((vma
->vm_flags
& VM_EXECUTABLE
) &&
664 audit_log_d_path(ab
, "exe=",
665 vma
->vm_file
->f_dentry
,
666 vma
->vm_file
->f_vfsmnt
);
671 up_read(&mm
->mmap_sem
);
674 static void audit_log_exit(struct audit_context
*context
)
677 struct audit_buffer
*ab
;
679 ab
= audit_log_start(context
, AUDIT_SYSCALL
);
681 return; /* audit_panic has been called */
682 audit_log_format(ab
, "arch=%x syscall=%d",
683 context
->arch
, context
->major
);
684 if (context
->personality
!= PER_LINUX
)
685 audit_log_format(ab
, " per=%lx", context
->personality
);
686 if (context
->return_valid
)
687 audit_log_format(ab
, " success=%s exit=%ld",
688 (context
->return_valid
==AUDITSC_SUCCESS
)?"yes":"no",
689 context
->return_code
);
691 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
692 " pid=%d auid=%u uid=%u gid=%u"
693 " euid=%u suid=%u fsuid=%u"
694 " egid=%u sgid=%u fsgid=%u",
704 context
->euid
, context
->suid
, context
->fsuid
,
705 context
->egid
, context
->sgid
, context
->fsgid
);
706 audit_log_task_info(ab
);
708 while (context
->aux
) {
709 struct audit_aux_data
*aux
;
713 ab
= audit_log_start(context
, aux
->type
);
715 continue; /* audit_panic has been called */
719 struct audit_aux_data_ipcctl
*axi
= (void *)aux
;
721 " qbytes=%lx iuid=%u igid=%u mode=%x",
722 axi
->qbytes
, axi
->uid
, axi
->gid
, axi
->mode
);
725 case AUDIT_SOCKETCALL
: {
727 struct audit_aux_data_socketcall
*axs
= (void *)aux
;
728 audit_log_format(ab
, "nargs=%d", axs
->nargs
);
729 for (i
=0; i
<axs
->nargs
; i
++)
730 audit_log_format(ab
, " a%d=%lx", i
, axs
->args
[i
]);
733 case AUDIT_SOCKADDR
: {
734 struct audit_aux_data_sockaddr
*axs
= (void *)aux
;
736 audit_log_format(ab
, "saddr=");
737 audit_log_hex(ab
, axs
->a
, axs
->len
);
740 case AUDIT_AVC_PATH
: {
741 struct audit_aux_data_path
*axi
= (void *)aux
;
742 audit_log_d_path(ab
, "path=", axi
->dentry
, axi
->mnt
);
750 context
->aux
= aux
->next
;
754 for (i
= 0; i
< context
->name_count
; i
++) {
755 ab
= audit_log_start(context
, AUDIT_PATH
);
757 continue; /* audit_panic has been called */
758 audit_log_format(ab
, "item=%d", i
);
759 if (context
->names
[i
].name
) {
760 audit_log_format(ab
, " name=");
761 audit_log_untrustedstring(ab
, context
->names
[i
].name
);
763 if (context
->names
[i
].ino
!= (unsigned long)-1)
764 audit_log_format(ab
, " inode=%lu dev=%02x:%02x mode=%#o"
765 " ouid=%u ogid=%u rdev=%02x:%02x",
766 context
->names
[i
].ino
,
767 MAJOR(context
->names
[i
].dev
),
768 MINOR(context
->names
[i
].dev
),
769 context
->names
[i
].mode
,
770 context
->names
[i
].uid
,
771 context
->names
[i
].gid
,
772 MAJOR(context
->names
[i
].rdev
),
773 MINOR(context
->names
[i
].rdev
));
778 /* Free a per-task audit context. Called from copy_process and
779 * __put_task_struct. */
780 void audit_free(struct task_struct
*tsk
)
782 struct audit_context
*context
;
785 context
= audit_get_context(tsk
, 0, 0);
788 if (likely(!context
))
791 /* Check for system calls that do not go through the exit
792 * function (e.g., exit_group), then free context block. */
793 if (context
->in_syscall
&& context
->auditable
&& context
->pid
!= audit_pid
)
794 audit_log_exit(context
);
796 audit_free_context(context
);
799 /* Fill in audit context at syscall entry. This only happens if the
800 * audit context was created when the task was created and the state or
801 * filters demand the audit context be built. If the state from the
802 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
803 * then the record will be written at syscall exit time (otherwise, it
804 * will only be written if another part of the kernel requests that it
806 void audit_syscall_entry(struct task_struct
*tsk
, int arch
, int major
,
807 unsigned long a1
, unsigned long a2
,
808 unsigned long a3
, unsigned long a4
)
810 struct audit_context
*context
= tsk
->audit_context
;
811 enum audit_state state
;
815 /* This happens only on certain architectures that make system
816 * calls in kernel_thread via the entry.S interface, instead of
817 * with direct calls. (If you are porting to a new
818 * architecture, hitting this condition can indicate that you
819 * got the _exit/_leave calls backward in entry.S.)
823 * ppc64 yes (see arch/ppc64/kernel/misc.S)
825 * This also happens with vm86 emulation in a non-nested manner
826 * (entries without exits), so this case must be caught.
828 if (context
->in_syscall
) {
829 struct audit_context
*newctx
;
831 #if defined(__NR_vm86) && defined(__NR_vm86old)
832 /* vm86 mode should only be entered once */
833 if (major
== __NR_vm86
|| major
== __NR_vm86old
)
838 "audit(:%d) pid=%d in syscall=%d;"
839 " entering syscall=%d\n",
840 context
->serial
, tsk
->pid
, context
->major
, major
);
842 newctx
= audit_alloc_context(context
->state
);
844 newctx
->previous
= context
;
846 tsk
->audit_context
= newctx
;
848 /* If we can't alloc a new context, the best we
849 * can do is to leak memory (any pending putname
850 * will be lost). The only other alternative is
851 * to abandon auditing. */
852 audit_zero_context(context
, context
->state
);
855 BUG_ON(context
->in_syscall
|| context
->name_count
);
860 context
->arch
= arch
;
861 context
->major
= major
;
862 context
->argv
[0] = a1
;
863 context
->argv
[1] = a2
;
864 context
->argv
[2] = a3
;
865 context
->argv
[3] = a4
;
867 state
= context
->state
;
868 if (state
== AUDIT_SETUP_CONTEXT
|| state
== AUDIT_BUILD_CONTEXT
)
869 state
= audit_filter_syscall(tsk
, context
, &audit_entlist
);
870 if (likely(state
== AUDIT_DISABLED
))
873 context
->serial
= audit_serial();
874 context
->ctime
= CURRENT_TIME
;
875 context
->in_syscall
= 1;
876 context
->auditable
= !!(state
== AUDIT_RECORD_CONTEXT
);
879 /* Tear down after system call. If the audit context has been marked as
880 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
881 * filtering, or because some other part of the kernel write an audit
882 * message), then write out the syscall information. In call cases,
883 * free the names stored from getname(). */
884 void audit_syscall_exit(struct task_struct
*tsk
, int valid
, long return_code
)
886 struct audit_context
*context
;
888 get_task_struct(tsk
);
890 context
= audit_get_context(tsk
, valid
, return_code
);
893 /* Not having a context here is ok, since the parent may have
894 * called __put_task_struct. */
895 if (likely(!context
))
898 if (context
->in_syscall
&& context
->auditable
&& context
->pid
!= audit_pid
)
899 audit_log_exit(context
);
901 context
->in_syscall
= 0;
902 context
->auditable
= 0;
904 if (context
->previous
) {
905 struct audit_context
*new_context
= context
->previous
;
906 context
->previous
= NULL
;
907 audit_free_context(context
);
908 tsk
->audit_context
= new_context
;
910 audit_free_names(context
);
911 audit_free_aux(context
);
912 audit_zero_context(context
, context
->state
);
913 tsk
->audit_context
= context
;
915 put_task_struct(tsk
);
918 /* Add a name to the list. Called from fs/namei.c:getname(). */
919 void audit_getname(const char *name
)
921 struct audit_context
*context
= current
->audit_context
;
923 if (!context
|| IS_ERR(name
) || !name
)
926 if (!context
->in_syscall
) {
928 printk(KERN_ERR
"%s:%d(:%d): ignoring getname(%p)\n",
929 __FILE__
, __LINE__
, context
->serial
, name
);
934 BUG_ON(context
->name_count
>= AUDIT_NAMES
);
935 context
->names
[context
->name_count
].name
= name
;
936 context
->names
[context
->name_count
].ino
= (unsigned long)-1;
937 ++context
->name_count
;
940 /* Intercept a putname request. Called from
941 * include/linux/fs.h:putname(). If we have stored the name from
942 * getname in the audit context, then we delay the putname until syscall
944 void audit_putname(const char *name
)
946 struct audit_context
*context
= current
->audit_context
;
949 if (!context
->in_syscall
) {
951 printk(KERN_ERR
"%s:%d(:%d): __putname(%p)\n",
952 __FILE__
, __LINE__
, context
->serial
, name
);
953 if (context
->name_count
) {
955 for (i
= 0; i
< context
->name_count
; i
++)
956 printk(KERN_ERR
"name[%d] = %p = %s\n", i
,
957 context
->names
[i
].name
,
958 context
->names
[i
].name
);
965 ++context
->put_count
;
966 if (context
->put_count
> context
->name_count
) {
967 printk(KERN_ERR
"%s:%d(:%d): major=%d"
968 " in_syscall=%d putname(%p) name_count=%d"
971 context
->serial
, context
->major
,
972 context
->in_syscall
, name
, context
->name_count
,
980 /* Store the inode and device from a lookup. Called from
981 * fs/namei.c:path_lookup(). */
982 void audit_inode(const char *name
, const struct inode
*inode
)
985 struct audit_context
*context
= current
->audit_context
;
987 if (!context
->in_syscall
)
989 if (context
->name_count
990 && context
->names
[context
->name_count
-1].name
991 && context
->names
[context
->name_count
-1].name
== name
)
992 idx
= context
->name_count
- 1;
993 else if (context
->name_count
> 1
994 && context
->names
[context
->name_count
-2].name
995 && context
->names
[context
->name_count
-2].name
== name
)
996 idx
= context
->name_count
- 2;
998 /* FIXME: how much do we care about inodes that have no
999 * associated name? */
1000 if (context
->name_count
>= AUDIT_NAMES
- AUDIT_NAMES_RESERVED
)
1002 idx
= context
->name_count
++;
1003 context
->names
[idx
].name
= NULL
;
1005 ++context
->ino_count
;
1008 context
->names
[idx
].ino
= inode
->i_ino
;
1009 context
->names
[idx
].dev
= inode
->i_sb
->s_dev
;
1010 context
->names
[idx
].mode
= inode
->i_mode
;
1011 context
->names
[idx
].uid
= inode
->i_uid
;
1012 context
->names
[idx
].gid
= inode
->i_gid
;
1013 context
->names
[idx
].rdev
= inode
->i_rdev
;
1016 void auditsc_get_stamp(struct audit_context
*ctx
,
1017 struct timespec
*t
, unsigned int *serial
)
1019 t
->tv_sec
= ctx
->ctime
.tv_sec
;
1020 t
->tv_nsec
= ctx
->ctime
.tv_nsec
;
1021 *serial
= ctx
->serial
;
1025 int audit_set_loginuid(struct task_struct
*task
, uid_t loginuid
)
1027 if (task
->audit_context
) {
1028 struct audit_buffer
*ab
;
1030 ab
= audit_log_start(NULL
, AUDIT_LOGIN
);
1032 audit_log_format(ab
, "login pid=%d uid=%u "
1033 "old auid=%u new auid=%u",
1034 task
->pid
, task
->uid
,
1035 task
->audit_context
->loginuid
, loginuid
);
1038 task
->audit_context
->loginuid
= loginuid
;
1043 uid_t
audit_get_loginuid(struct audit_context
*ctx
)
1045 return ctx
? ctx
->loginuid
: -1;
1048 int audit_ipc_perms(unsigned long qbytes
, uid_t uid
, gid_t gid
, mode_t mode
)
1050 struct audit_aux_data_ipcctl
*ax
;
1051 struct audit_context
*context
= current
->audit_context
;
1053 if (likely(!context
))
1056 ax
= kmalloc(sizeof(*ax
), GFP_KERNEL
);
1060 ax
->qbytes
= qbytes
;
1065 ax
->d
.type
= AUDIT_IPC
;
1066 ax
->d
.next
= context
->aux
;
1067 context
->aux
= (void *)ax
;
1071 int audit_socketcall(int nargs
, unsigned long *args
)
1073 struct audit_aux_data_socketcall
*ax
;
1074 struct audit_context
*context
= current
->audit_context
;
1076 if (likely(!context
))
1079 ax
= kmalloc(sizeof(*ax
) + nargs
* sizeof(unsigned long), GFP_KERNEL
);
1084 memcpy(ax
->args
, args
, nargs
* sizeof(unsigned long));
1086 ax
->d
.type
= AUDIT_SOCKETCALL
;
1087 ax
->d
.next
= context
->aux
;
1088 context
->aux
= (void *)ax
;
1092 int audit_sockaddr(int len
, void *a
)
1094 struct audit_aux_data_sockaddr
*ax
;
1095 struct audit_context
*context
= current
->audit_context
;
1097 if (likely(!context
))
1100 ax
= kmalloc(sizeof(*ax
) + len
, GFP_KERNEL
);
1105 memcpy(ax
->a
, a
, len
);
1107 ax
->d
.type
= AUDIT_SOCKADDR
;
1108 ax
->d
.next
= context
->aux
;
1109 context
->aux
= (void *)ax
;
1113 int audit_avc_path(struct dentry
*dentry
, struct vfsmount
*mnt
)
1115 struct audit_aux_data_path
*ax
;
1116 struct audit_context
*context
= current
->audit_context
;
1118 if (likely(!context
))
1121 ax
= kmalloc(sizeof(*ax
), GFP_ATOMIC
);
1125 ax
->dentry
= dget(dentry
);
1126 ax
->mnt
= mntget(mnt
);
1128 ax
->d
.type
= AUDIT_AVC_PATH
;
1129 ax
->d
.next
= context
->aux
;
1130 context
->aux
= (void *)ax
;
1134 void audit_signal_info(int sig
, struct task_struct
*t
)
1136 extern pid_t audit_sig_pid
;
1137 extern uid_t audit_sig_uid
;
1139 if (unlikely(audit_pid
&& t
->pid
== audit_pid
)) {
1140 if (sig
== SIGTERM
|| sig
== SIGHUP
) {
1141 struct audit_context
*ctx
= current
->audit_context
;
1142 audit_sig_pid
= current
->pid
;
1144 audit_sig_uid
= ctx
->loginuid
;
1146 audit_sig_uid
= current
->uid
;