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
];
149 struct vfsmount
* pwdmnt
;
150 struct audit_context
*previous
; /* For nested syscalls */
151 struct audit_aux_data
*aux
;
153 /* Save things to print about task_struct */
155 uid_t uid
, euid
, suid
, fsuid
;
156 gid_t gid
, egid
, sgid
, fsgid
;
157 unsigned long personality
;
167 /* There are three lists of rules -- one to search at task creation
168 * time, one to search at syscall entry time, and another to search at
169 * syscall exit time. */
170 static struct list_head audit_filter_list
[AUDIT_NR_FILTERS
] = {
171 LIST_HEAD_INIT(audit_filter_list
[0]),
172 LIST_HEAD_INIT(audit_filter_list
[1]),
173 LIST_HEAD_INIT(audit_filter_list
[2]),
174 LIST_HEAD_INIT(audit_filter_list
[3]),
175 LIST_HEAD_INIT(audit_filter_list
[4]),
176 #if AUDIT_NR_FILTERS != 5
177 #error Fix audit_filter_list initialiser
182 struct list_head list
;
184 struct audit_rule rule
;
187 extern int audit_pid
;
189 /* Check to see if two rules are identical. It is called from
190 * audit_del_rule during AUDIT_DEL. */
191 static int audit_compare_rule(struct audit_rule
*a
, struct audit_rule
*b
)
195 if (a
->flags
!= b
->flags
)
198 if (a
->action
!= b
->action
)
201 if (a
->field_count
!= b
->field_count
)
204 for (i
= 0; i
< a
->field_count
; i
++) {
205 if (a
->fields
[i
] != b
->fields
[i
]
206 || a
->values
[i
] != b
->values
[i
])
210 for (i
= 0; i
< AUDIT_BITMASK_SIZE
; i
++)
211 if (a
->mask
[i
] != b
->mask
[i
])
217 /* Note that audit_add_rule and audit_del_rule are called via
218 * audit_receive() in audit.c, and are protected by
219 * audit_netlink_sem. */
220 static inline void audit_add_rule(struct audit_entry
*entry
,
221 struct list_head
*list
)
223 if (entry
->rule
.flags
& AUDIT_FILTER_PREPEND
) {
224 entry
->rule
.flags
&= ~AUDIT_FILTER_PREPEND
;
225 list_add_rcu(&entry
->list
, list
);
227 list_add_tail_rcu(&entry
->list
, list
);
231 static void audit_free_rule(struct rcu_head
*head
)
233 struct audit_entry
*e
= container_of(head
, struct audit_entry
, rcu
);
237 /* Note that audit_add_rule and audit_del_rule are called via
238 * audit_receive() in audit.c, and are protected by
239 * audit_netlink_sem. */
240 static inline int audit_del_rule(struct audit_rule
*rule
,
241 struct list_head
*list
)
243 struct audit_entry
*e
;
245 /* Do not use the _rcu iterator here, since this is the only
246 * deletion routine. */
247 list_for_each_entry(e
, list
, list
) {
248 if (!audit_compare_rule(rule
, &e
->rule
)) {
249 list_del_rcu(&e
->list
);
250 call_rcu(&e
->rcu
, audit_free_rule
);
254 return -ENOENT
; /* No matching rule */
257 /* Copy rule from user-space to kernel-space. Called during
259 static int audit_copy_rule(struct audit_rule
*d
, struct audit_rule
*s
)
263 if (s
->action
!= AUDIT_NEVER
264 && s
->action
!= AUDIT_POSSIBLE
265 && s
->action
!= AUDIT_ALWAYS
)
267 if (s
->field_count
< 0 || s
->field_count
> AUDIT_MAX_FIELDS
)
269 if ((s
->flags
& ~AUDIT_FILTER_PREPEND
) >= AUDIT_NR_FILTERS
)
273 d
->action
= s
->action
;
274 d
->field_count
= s
->field_count
;
275 for (i
= 0; i
< d
->field_count
; i
++) {
276 d
->fields
[i
] = s
->fields
[i
];
277 d
->values
[i
] = s
->values
[i
];
279 for (i
= 0; i
< AUDIT_BITMASK_SIZE
; i
++) d
->mask
[i
] = s
->mask
[i
];
283 int audit_receive_filter(int type
, int pid
, int uid
, int seq
, void *data
,
286 struct audit_entry
*entry
;
293 /* The *_rcu iterators not needed here because we are
294 always called with audit_netlink_sem held. */
295 for (i
=0; i
<AUDIT_NR_FILTERS
; i
++) {
296 list_for_each_entry(entry
, &audit_filter_list
[i
], list
)
297 audit_send_reply(pid
, seq
, AUDIT_LIST
, 0, 1,
298 &entry
->rule
, sizeof(entry
->rule
));
300 audit_send_reply(pid
, seq
, AUDIT_LIST
, 1, 1, NULL
, 0);
303 if (!(entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
)))
305 if (audit_copy_rule(&entry
->rule
, data
)) {
309 listnr
= entry
->rule
.flags
& ~AUDIT_FILTER_PREPEND
;
310 audit_add_rule(entry
, &audit_filter_list
[listnr
]);
311 audit_log(NULL
, AUDIT_CONFIG_CHANGE
,
312 "auid=%u added an audit rule\n", loginuid
);
315 listnr
=((struct audit_rule
*)data
)->flags
& ~AUDIT_FILTER_PREPEND
;
316 if (listnr
>= AUDIT_NR_FILTERS
)
319 err
= audit_del_rule(data
, &audit_filter_list
[listnr
]);
321 audit_log(NULL
, AUDIT_CONFIG_CHANGE
,
322 "auid=%u removed an audit rule\n", loginuid
);
331 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
333 static int audit_filter_rules(struct task_struct
*tsk
,
334 struct audit_rule
*rule
,
335 struct audit_context
*ctx
,
336 enum audit_state
*state
)
340 for (i
= 0; i
< rule
->field_count
; i
++) {
341 u32 field
= rule
->fields
[i
] & ~AUDIT_NEGATE
;
342 u32 value
= rule
->values
[i
];
347 result
= (tsk
->pid
== value
);
350 result
= (tsk
->uid
== value
);
353 result
= (tsk
->euid
== value
);
356 result
= (tsk
->suid
== value
);
359 result
= (tsk
->fsuid
== value
);
362 result
= (tsk
->gid
== value
);
365 result
= (tsk
->egid
== value
);
368 result
= (tsk
->sgid
== value
);
371 result
= (tsk
->fsgid
== value
);
374 result
= (tsk
->personality
== value
);
378 result
= (ctx
->arch
== value
);
382 if (ctx
&& ctx
->return_valid
)
383 result
= (ctx
->return_code
== value
);
386 if (ctx
&& ctx
->return_valid
)
387 result
= (ctx
->return_valid
== AUDITSC_SUCCESS
);
391 for (j
= 0; j
< ctx
->name_count
; j
++) {
392 if (MAJOR(ctx
->names
[j
].dev
)==value
) {
401 for (j
= 0; j
< ctx
->name_count
; j
++) {
402 if (MINOR(ctx
->names
[j
].dev
)==value
) {
411 for (j
= 0; j
< ctx
->name_count
; j
++) {
412 if (ctx
->names
[j
].ino
== value
) {
422 result
= (ctx
->loginuid
== value
);
429 result
= (ctx
->argv
[field
-AUDIT_ARG0
]==value
);
433 if (rule
->fields
[i
] & AUDIT_NEGATE
)
438 switch (rule
->action
) {
439 case AUDIT_NEVER
: *state
= AUDIT_DISABLED
; break;
440 case AUDIT_POSSIBLE
: *state
= AUDIT_BUILD_CONTEXT
; break;
441 case AUDIT_ALWAYS
: *state
= AUDIT_RECORD_CONTEXT
; break;
446 /* At process creation time, we can determine if system-call auditing is
447 * completely disabled for this task. Since we only have the task
448 * structure at this point, we can only check uid and gid.
450 static enum audit_state
audit_filter_task(struct task_struct
*tsk
)
452 struct audit_entry
*e
;
453 enum audit_state state
;
456 list_for_each_entry_rcu(e
, &audit_filter_list
[AUDIT_FILTER_TASK
], list
) {
457 if (audit_filter_rules(tsk
, &e
->rule
, NULL
, &state
)) {
463 return AUDIT_BUILD_CONTEXT
;
466 /* At syscall entry and exit time, this filter is called if the
467 * audit_state is not low enough that auditing cannot take place, but is
468 * also not high enough that we already know we have to write an audit
469 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
471 static enum audit_state
audit_filter_syscall(struct task_struct
*tsk
,
472 struct audit_context
*ctx
,
473 struct list_head
*list
)
475 struct audit_entry
*e
;
476 enum audit_state state
;
477 int word
= AUDIT_WORD(ctx
->major
);
478 int bit
= AUDIT_BIT(ctx
->major
);
480 if (audit_pid
&& ctx
->pid
== audit_pid
)
481 return AUDIT_DISABLED
;
484 list_for_each_entry_rcu(e
, list
, list
) {
485 if ((e
->rule
.mask
[word
] & bit
) == bit
486 && audit_filter_rules(tsk
, &e
->rule
, ctx
, &state
)) {
492 return AUDIT_BUILD_CONTEXT
;
495 int audit_filter_user(struct task_struct
*tsk
, int type
)
497 struct audit_entry
*e
;
498 enum audit_state state
;
500 if (audit_pid
&& tsk
->pid
== audit_pid
)
501 return AUDIT_DISABLED
;
504 list_for_each_entry_rcu(e
, &audit_filter_list
[AUDIT_FILTER_USER
], list
) {
505 if (audit_filter_rules(tsk
, &e
->rule
, NULL
, &state
)) {
507 return state
!= AUDIT_DISABLED
;
511 return 1; /* Audit by default */
515 /* This should be called with task_lock() held. */
516 static inline struct audit_context
*audit_get_context(struct task_struct
*tsk
,
520 struct audit_context
*context
= tsk
->audit_context
;
522 if (likely(!context
))
524 context
->return_valid
= return_valid
;
525 context
->return_code
= return_code
;
527 if (context
->in_syscall
&& !context
->auditable
) {
528 enum audit_state state
;
529 state
= audit_filter_syscall(tsk
, context
, &audit_filter_list
[AUDIT_FILTER_EXIT
]);
530 if (state
== AUDIT_RECORD_CONTEXT
)
531 context
->auditable
= 1;
534 context
->pid
= tsk
->pid
;
535 context
->uid
= tsk
->uid
;
536 context
->gid
= tsk
->gid
;
537 context
->euid
= tsk
->euid
;
538 context
->suid
= tsk
->suid
;
539 context
->fsuid
= tsk
->fsuid
;
540 context
->egid
= tsk
->egid
;
541 context
->sgid
= tsk
->sgid
;
542 context
->fsgid
= tsk
->fsgid
;
543 context
->personality
= tsk
->personality
;
544 tsk
->audit_context
= NULL
;
548 static inline void audit_free_names(struct audit_context
*context
)
553 if (context
->auditable
554 ||context
->put_count
+ context
->ino_count
!= context
->name_count
) {
555 printk(KERN_ERR
"audit.c:%d(:%d): major=%d in_syscall=%d"
556 " name_count=%d put_count=%d"
557 " ino_count=%d [NOT freeing]\n",
559 context
->serial
, context
->major
, context
->in_syscall
,
560 context
->name_count
, context
->put_count
,
562 for (i
= 0; i
< context
->name_count
; i
++)
563 printk(KERN_ERR
"names[%d] = %p = %s\n", i
,
564 context
->names
[i
].name
,
565 context
->names
[i
].name
);
571 context
->put_count
= 0;
572 context
->ino_count
= 0;
575 for (i
= 0; i
< context
->name_count
; i
++)
576 if (context
->names
[i
].name
)
577 __putname(context
->names
[i
].name
);
578 context
->name_count
= 0;
582 mntput(context
->pwdmnt
);
584 context
->pwdmnt
= NULL
;
587 static inline void audit_free_aux(struct audit_context
*context
)
589 struct audit_aux_data
*aux
;
591 while ((aux
= context
->aux
)) {
592 if (aux
->type
== AUDIT_AVC_PATH
) {
593 struct audit_aux_data_path
*axi
= (void *)aux
;
597 context
->aux
= aux
->next
;
602 static inline void audit_zero_context(struct audit_context
*context
,
603 enum audit_state state
)
605 uid_t loginuid
= context
->loginuid
;
607 memset(context
, 0, sizeof(*context
));
608 context
->state
= state
;
609 context
->loginuid
= loginuid
;
612 static inline struct audit_context
*audit_alloc_context(enum audit_state state
)
614 struct audit_context
*context
;
616 if (!(context
= kmalloc(sizeof(*context
), GFP_KERNEL
)))
618 audit_zero_context(context
, state
);
622 /* Filter on the task information and allocate a per-task audit context
623 * if necessary. Doing so turns on system call auditing for the
624 * specified task. This is called from copy_process, so no lock is
626 int audit_alloc(struct task_struct
*tsk
)
628 struct audit_context
*context
;
629 enum audit_state state
;
631 if (likely(!audit_enabled
))
632 return 0; /* Return if not auditing. */
634 state
= audit_filter_task(tsk
);
635 if (likely(state
== AUDIT_DISABLED
))
638 if (!(context
= audit_alloc_context(state
))) {
639 audit_log_lost("out of memory in audit_alloc");
643 /* Preserve login uid */
644 context
->loginuid
= -1;
645 if (current
->audit_context
)
646 context
->loginuid
= current
->audit_context
->loginuid
;
648 tsk
->audit_context
= context
;
649 set_tsk_thread_flag(tsk
, TIF_SYSCALL_AUDIT
);
653 static inline void audit_free_context(struct audit_context
*context
)
655 struct audit_context
*previous
;
659 previous
= context
->previous
;
660 if (previous
|| (count
&& count
< 10)) {
662 printk(KERN_ERR
"audit(:%d): major=%d name_count=%d:"
663 " freeing multiple contexts (%d)\n",
664 context
->serial
, context
->major
,
665 context
->name_count
, count
);
667 audit_free_names(context
);
668 audit_free_aux(context
);
673 printk(KERN_ERR
"audit: freed %d contexts\n", count
);
676 static void audit_log_task_info(struct audit_buffer
*ab
)
678 char name
[sizeof(current
->comm
)];
679 struct mm_struct
*mm
= current
->mm
;
680 struct vm_area_struct
*vma
;
682 get_task_comm(name
, current
);
683 audit_log_format(ab
, " comm=");
684 audit_log_untrustedstring(ab
, name
);
689 down_read(&mm
->mmap_sem
);
692 if ((vma
->vm_flags
& VM_EXECUTABLE
) &&
694 audit_log_d_path(ab
, "exe=",
695 vma
->vm_file
->f_dentry
,
696 vma
->vm_file
->f_vfsmnt
);
701 up_read(&mm
->mmap_sem
);
704 static void audit_log_exit(struct audit_context
*context
)
707 struct audit_buffer
*ab
;
708 struct audit_aux_data
*aux
;
710 ab
= audit_log_start(context
, AUDIT_SYSCALL
);
712 return; /* audit_panic has been called */
713 audit_log_format(ab
, "arch=%x syscall=%d",
714 context
->arch
, context
->major
);
715 if (context
->personality
!= PER_LINUX
)
716 audit_log_format(ab
, " per=%lx", context
->personality
);
717 if (context
->return_valid
)
718 audit_log_format(ab
, " success=%s exit=%ld",
719 (context
->return_valid
==AUDITSC_SUCCESS
)?"yes":"no",
720 context
->return_code
);
722 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
723 " pid=%d auid=%u uid=%u gid=%u"
724 " euid=%u suid=%u fsuid=%u"
725 " egid=%u sgid=%u fsgid=%u",
735 context
->euid
, context
->suid
, context
->fsuid
,
736 context
->egid
, context
->sgid
, context
->fsgid
);
737 audit_log_task_info(ab
);
740 for (aux
= context
->aux
; aux
; aux
= aux
->next
) {
742 ab
= audit_log_start(context
, aux
->type
);
744 continue; /* audit_panic has been called */
748 struct audit_aux_data_ipcctl
*axi
= (void *)aux
;
750 " qbytes=%lx iuid=%u igid=%u mode=%x",
751 axi
->qbytes
, axi
->uid
, axi
->gid
, axi
->mode
);
754 case AUDIT_SOCKETCALL
: {
756 struct audit_aux_data_socketcall
*axs
= (void *)aux
;
757 audit_log_format(ab
, "nargs=%d", axs
->nargs
);
758 for (i
=0; i
<axs
->nargs
; i
++)
759 audit_log_format(ab
, " a%d=%lx", i
, axs
->args
[i
]);
762 case AUDIT_SOCKADDR
: {
763 struct audit_aux_data_sockaddr
*axs
= (void *)aux
;
765 audit_log_format(ab
, "saddr=");
766 audit_log_hex(ab
, axs
->a
, axs
->len
);
769 case AUDIT_AVC_PATH
: {
770 struct audit_aux_data_path
*axi
= (void *)aux
;
771 audit_log_d_path(ab
, "path=", axi
->dentry
, axi
->mnt
);
778 if (context
->pwd
&& context
->pwdmnt
) {
779 ab
= audit_log_start(context
, AUDIT_CWD
);
781 audit_log_d_path(ab
, "cwd=", context
->pwd
, context
->pwdmnt
);
785 for (i
= 0; i
< context
->name_count
; i
++) {
786 ab
= audit_log_start(context
, AUDIT_PATH
);
788 continue; /* audit_panic has been called */
790 audit_log_format(ab
, "item=%d", i
);
791 if (context
->names
[i
].name
) {
792 audit_log_format(ab
, " name=");
793 audit_log_untrustedstring(ab
, context
->names
[i
].name
);
795 if (context
->names
[i
].ino
!= (unsigned long)-1)
796 audit_log_format(ab
, " inode=%lu dev=%02x:%02x mode=%#o"
797 " ouid=%u ogid=%u rdev=%02x:%02x",
798 context
->names
[i
].ino
,
799 MAJOR(context
->names
[i
].dev
),
800 MINOR(context
->names
[i
].dev
),
801 context
->names
[i
].mode
,
802 context
->names
[i
].uid
,
803 context
->names
[i
].gid
,
804 MAJOR(context
->names
[i
].rdev
),
805 MINOR(context
->names
[i
].rdev
));
810 /* Free a per-task audit context. Called from copy_process and
811 * __put_task_struct. */
812 void audit_free(struct task_struct
*tsk
)
814 struct audit_context
*context
;
817 context
= audit_get_context(tsk
, 0, 0);
820 if (likely(!context
))
823 /* Check for system calls that do not go through the exit
824 * function (e.g., exit_group), then free context block. */
825 if (context
->in_syscall
&& context
->auditable
)
826 audit_log_exit(context
);
828 audit_free_context(context
);
831 /* Fill in audit context at syscall entry. This only happens if the
832 * audit context was created when the task was created and the state or
833 * filters demand the audit context be built. If the state from the
834 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
835 * then the record will be written at syscall exit time (otherwise, it
836 * will only be written if another part of the kernel requests that it
838 void audit_syscall_entry(struct task_struct
*tsk
, int arch
, int major
,
839 unsigned long a1
, unsigned long a2
,
840 unsigned long a3
, unsigned long a4
)
842 struct audit_context
*context
= tsk
->audit_context
;
843 enum audit_state state
;
847 /* This happens only on certain architectures that make system
848 * calls in kernel_thread via the entry.S interface, instead of
849 * with direct calls. (If you are porting to a new
850 * architecture, hitting this condition can indicate that you
851 * got the _exit/_leave calls backward in entry.S.)
855 * ppc64 yes (see arch/ppc64/kernel/misc.S)
857 * This also happens with vm86 emulation in a non-nested manner
858 * (entries without exits), so this case must be caught.
860 if (context
->in_syscall
) {
861 struct audit_context
*newctx
;
863 #if defined(__NR_vm86) && defined(__NR_vm86old)
864 /* vm86 mode should only be entered once */
865 if (major
== __NR_vm86
|| major
== __NR_vm86old
)
870 "audit(:%d) pid=%d in syscall=%d;"
871 " entering syscall=%d\n",
872 context
->serial
, tsk
->pid
, context
->major
, major
);
874 newctx
= audit_alloc_context(context
->state
);
876 newctx
->previous
= context
;
878 tsk
->audit_context
= newctx
;
880 /* If we can't alloc a new context, the best we
881 * can do is to leak memory (any pending putname
882 * will be lost). The only other alternative is
883 * to abandon auditing. */
884 audit_zero_context(context
, context
->state
);
887 BUG_ON(context
->in_syscall
|| context
->name_count
);
892 context
->arch
= arch
;
893 context
->major
= major
;
894 context
->argv
[0] = a1
;
895 context
->argv
[1] = a2
;
896 context
->argv
[2] = a3
;
897 context
->argv
[3] = a4
;
899 state
= context
->state
;
900 if (state
== AUDIT_SETUP_CONTEXT
|| state
== AUDIT_BUILD_CONTEXT
)
901 state
= audit_filter_syscall(tsk
, context
, &audit_filter_list
[AUDIT_FILTER_ENTRY
]);
902 if (likely(state
== AUDIT_DISABLED
))
905 context
->serial
= audit_serial();
906 context
->ctime
= CURRENT_TIME
;
907 context
->in_syscall
= 1;
908 context
->auditable
= !!(state
== AUDIT_RECORD_CONTEXT
);
911 /* Tear down after system call. If the audit context has been marked as
912 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
913 * filtering, or because some other part of the kernel write an audit
914 * message), then write out the syscall information. In call cases,
915 * free the names stored from getname(). */
916 void audit_syscall_exit(struct task_struct
*tsk
, int valid
, long return_code
)
918 struct audit_context
*context
;
920 get_task_struct(tsk
);
922 context
= audit_get_context(tsk
, valid
, return_code
);
925 /* Not having a context here is ok, since the parent may have
926 * called __put_task_struct. */
927 if (likely(!context
))
930 if (context
->in_syscall
&& context
->auditable
)
931 audit_log_exit(context
);
933 context
->in_syscall
= 0;
934 context
->auditable
= 0;
936 if (context
->previous
) {
937 struct audit_context
*new_context
= context
->previous
;
938 context
->previous
= NULL
;
939 audit_free_context(context
);
940 tsk
->audit_context
= new_context
;
942 audit_free_names(context
);
943 audit_free_aux(context
);
944 audit_zero_context(context
, context
->state
);
945 tsk
->audit_context
= context
;
947 put_task_struct(tsk
);
950 /* Add a name to the list. Called from fs/namei.c:getname(). */
951 void audit_getname(const char *name
)
953 struct audit_context
*context
= current
->audit_context
;
955 if (!context
|| IS_ERR(name
) || !name
)
958 if (!context
->in_syscall
) {
960 printk(KERN_ERR
"%s:%d(:%d): ignoring getname(%p)\n",
961 __FILE__
, __LINE__
, context
->serial
, name
);
966 BUG_ON(context
->name_count
>= AUDIT_NAMES
);
967 context
->names
[context
->name_count
].name
= name
;
968 context
->names
[context
->name_count
].ino
= (unsigned long)-1;
969 ++context
->name_count
;
971 read_lock(¤t
->fs
->lock
);
972 context
->pwd
= dget(current
->fs
->pwd
);
973 context
->pwdmnt
= mntget(current
->fs
->pwdmnt
);
974 read_unlock(¤t
->fs
->lock
);
979 /* Intercept a putname request. Called from
980 * include/linux/fs.h:putname(). If we have stored the name from
981 * getname in the audit context, then we delay the putname until syscall
983 void audit_putname(const char *name
)
985 struct audit_context
*context
= current
->audit_context
;
988 if (!context
->in_syscall
) {
990 printk(KERN_ERR
"%s:%d(:%d): __putname(%p)\n",
991 __FILE__
, __LINE__
, context
->serial
, name
);
992 if (context
->name_count
) {
994 for (i
= 0; i
< context
->name_count
; i
++)
995 printk(KERN_ERR
"name[%d] = %p = %s\n", i
,
996 context
->names
[i
].name
,
997 context
->names
[i
].name
);
1004 ++context
->put_count
;
1005 if (context
->put_count
> context
->name_count
) {
1006 printk(KERN_ERR
"%s:%d(:%d): major=%d"
1007 " in_syscall=%d putname(%p) name_count=%d"
1010 context
->serial
, context
->major
,
1011 context
->in_syscall
, name
, context
->name_count
,
1012 context
->put_count
);
1019 /* Store the inode and device from a lookup. Called from
1020 * fs/namei.c:path_lookup(). */
1021 void audit_inode(const char *name
, const struct inode
*inode
)
1024 struct audit_context
*context
= current
->audit_context
;
1026 if (!context
->in_syscall
)
1028 if (context
->name_count
1029 && context
->names
[context
->name_count
-1].name
1030 && context
->names
[context
->name_count
-1].name
== name
)
1031 idx
= context
->name_count
- 1;
1032 else if (context
->name_count
> 1
1033 && context
->names
[context
->name_count
-2].name
1034 && context
->names
[context
->name_count
-2].name
== name
)
1035 idx
= context
->name_count
- 2;
1037 /* FIXME: how much do we care about inodes that have no
1038 * associated name? */
1039 if (context
->name_count
>= AUDIT_NAMES
- AUDIT_NAMES_RESERVED
)
1041 idx
= context
->name_count
++;
1042 context
->names
[idx
].name
= NULL
;
1044 ++context
->ino_count
;
1047 context
->names
[idx
].ino
= inode
->i_ino
;
1048 context
->names
[idx
].dev
= inode
->i_sb
->s_dev
;
1049 context
->names
[idx
].mode
= inode
->i_mode
;
1050 context
->names
[idx
].uid
= inode
->i_uid
;
1051 context
->names
[idx
].gid
= inode
->i_gid
;
1052 context
->names
[idx
].rdev
= inode
->i_rdev
;
1055 void auditsc_get_stamp(struct audit_context
*ctx
,
1056 struct timespec
*t
, unsigned int *serial
)
1058 t
->tv_sec
= ctx
->ctime
.tv_sec
;
1059 t
->tv_nsec
= ctx
->ctime
.tv_nsec
;
1060 *serial
= ctx
->serial
;
1064 int audit_set_loginuid(struct task_struct
*task
, uid_t loginuid
)
1066 if (task
->audit_context
) {
1067 struct audit_buffer
*ab
;
1069 ab
= audit_log_start(NULL
, AUDIT_LOGIN
);
1071 audit_log_format(ab
, "login pid=%d uid=%u "
1072 "old auid=%u new auid=%u",
1073 task
->pid
, task
->uid
,
1074 task
->audit_context
->loginuid
, loginuid
);
1077 task
->audit_context
->loginuid
= loginuid
;
1082 uid_t
audit_get_loginuid(struct audit_context
*ctx
)
1084 return ctx
? ctx
->loginuid
: -1;
1087 int audit_ipc_perms(unsigned long qbytes
, uid_t uid
, gid_t gid
, mode_t mode
)
1089 struct audit_aux_data_ipcctl
*ax
;
1090 struct audit_context
*context
= current
->audit_context
;
1092 if (likely(!context
))
1095 ax
= kmalloc(sizeof(*ax
), GFP_KERNEL
);
1099 ax
->qbytes
= qbytes
;
1104 ax
->d
.type
= AUDIT_IPC
;
1105 ax
->d
.next
= context
->aux
;
1106 context
->aux
= (void *)ax
;
1110 int audit_socketcall(int nargs
, unsigned long *args
)
1112 struct audit_aux_data_socketcall
*ax
;
1113 struct audit_context
*context
= current
->audit_context
;
1115 if (likely(!context
))
1118 ax
= kmalloc(sizeof(*ax
) + nargs
* sizeof(unsigned long), GFP_KERNEL
);
1123 memcpy(ax
->args
, args
, nargs
* sizeof(unsigned long));
1125 ax
->d
.type
= AUDIT_SOCKETCALL
;
1126 ax
->d
.next
= context
->aux
;
1127 context
->aux
= (void *)ax
;
1131 int audit_sockaddr(int len
, void *a
)
1133 struct audit_aux_data_sockaddr
*ax
;
1134 struct audit_context
*context
= current
->audit_context
;
1136 if (likely(!context
))
1139 ax
= kmalloc(sizeof(*ax
) + len
, GFP_KERNEL
);
1144 memcpy(ax
->a
, a
, len
);
1146 ax
->d
.type
= AUDIT_SOCKADDR
;
1147 ax
->d
.next
= context
->aux
;
1148 context
->aux
= (void *)ax
;
1152 int audit_avc_path(struct dentry
*dentry
, struct vfsmount
*mnt
)
1154 struct audit_aux_data_path
*ax
;
1155 struct audit_context
*context
= current
->audit_context
;
1157 if (likely(!context
))
1160 ax
= kmalloc(sizeof(*ax
), GFP_ATOMIC
);
1164 ax
->dentry
= dget(dentry
);
1165 ax
->mnt
= mntget(mnt
);
1167 ax
->d
.type
= AUDIT_AVC_PATH
;
1168 ax
->d
.next
= context
->aux
;
1169 context
->aux
= (void *)ax
;
1173 void audit_signal_info(int sig
, struct task_struct
*t
)
1175 extern pid_t audit_sig_pid
;
1176 extern uid_t audit_sig_uid
;
1178 if (unlikely(audit_pid
&& t
->pid
== audit_pid
)) {
1179 if (sig
== SIGTERM
|| sig
== SIGHUP
) {
1180 struct audit_context
*ctx
= current
->audit_context
;
1181 audit_sig_pid
= current
->pid
;
1183 audit_sig_uid
= ctx
->loginuid
;
1185 audit_sig_uid
= current
->uid
;