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.
5 * Copyright 2005 Hewlett-Packard Development Company, L.P.
6 * Copyright (C) 2005 IBM Corporation
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * Written by Rickard E. (Rik) Faith <faith@redhat.com>
25 * Many of the ideas implemented here are from Stephen C. Tweedie,
26 * especially the idea of avoiding a copy by using getname.
28 * The method for actual interception of syscall entry and exit (not in
29 * this file -- see entry.S) is based on a GPL'd patch written by
30 * okir@suse.de and Copyright 2003 SuSE Linux AG.
32 * The support of additional filter rules compares (>, <, >=, <=) was
33 * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
35 * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
36 * filesystem information.
38 * Subject and object context labeling support added by <danjones@us.ibm.com>
39 * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
42 #include <linux/init.h>
43 #include <asm/types.h>
44 #include <asm/atomic.h>
45 #include <asm/types.h>
47 #include <linux/namei.h>
49 #include <linux/module.h>
50 #include <linux/mount.h>
51 #include <linux/socket.h>
52 #include <linux/audit.h>
53 #include <linux/personality.h>
54 #include <linux/time.h>
55 #include <linux/netlink.h>
56 #include <linux/compiler.h>
57 #include <asm/unistd.h>
58 #include <linux/security.h>
59 #include <linux/list.h>
60 #include <linux/tty.h>
64 extern struct list_head audit_filter_list
[];
66 /* No syscall auditing will take place unless audit_enabled != 0. */
67 extern int audit_enabled
;
69 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
70 * for saving names from getname(). */
71 #define AUDIT_NAMES 20
73 /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
74 * audit_context from being used for nameless inodes from
76 #define AUDIT_NAMES_RESERVED 7
78 /* When fs/namei.c:getname() is called, we store the pointer in name and
79 * we don't let putname() free it (instead we free all of the saved
80 * pointers at syscall exit time).
82 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
95 struct audit_aux_data
{
96 struct audit_aux_data
*next
;
100 #define AUDIT_AUX_IPCPERM 0
102 struct audit_aux_data_ipcctl
{
103 struct audit_aux_data d
;
105 unsigned long qbytes
;
112 struct audit_aux_data_socketcall
{
113 struct audit_aux_data d
;
115 unsigned long args
[0];
118 struct audit_aux_data_sockaddr
{
119 struct audit_aux_data d
;
124 struct audit_aux_data_path
{
125 struct audit_aux_data d
;
126 struct dentry
*dentry
;
127 struct vfsmount
*mnt
;
130 /* The per-task audit context. */
131 struct audit_context
{
132 int in_syscall
; /* 1 if task is in a syscall */
133 enum audit_state state
;
134 unsigned int serial
; /* serial number for record */
135 struct timespec ctime
; /* time of syscall entry */
136 uid_t loginuid
; /* login uid (identity) */
137 int major
; /* syscall number */
138 unsigned long argv
[4]; /* syscall arguments */
139 int return_valid
; /* return code is valid */
140 long return_code
;/* syscall return code */
141 int auditable
; /* 1 if record should be written */
143 struct audit_names names
[AUDIT_NAMES
];
145 struct vfsmount
* pwdmnt
;
146 struct audit_context
*previous
; /* For nested syscalls */
147 struct audit_aux_data
*aux
;
149 /* Save things to print about task_struct */
151 uid_t uid
, euid
, suid
, fsuid
;
152 gid_t gid
, egid
, sgid
, fsgid
;
153 unsigned long personality
;
163 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
165 static int audit_filter_rules(struct task_struct
*tsk
,
166 struct audit_krule
*rule
,
167 struct audit_context
*ctx
,
168 enum audit_state
*state
)
172 for (i
= 0; i
< rule
->field_count
; i
++) {
173 struct audit_field
*f
= &rule
->fields
[i
];
178 result
= audit_comparator(tsk
->pid
, f
->op
, f
->val
);
181 result
= audit_comparator(tsk
->uid
, f
->op
, f
->val
);
184 result
= audit_comparator(tsk
->euid
, f
->op
, f
->val
);
187 result
= audit_comparator(tsk
->suid
, f
->op
, f
->val
);
190 result
= audit_comparator(tsk
->fsuid
, f
->op
, f
->val
);
193 result
= audit_comparator(tsk
->gid
, f
->op
, f
->val
);
196 result
= audit_comparator(tsk
->egid
, f
->op
, f
->val
);
199 result
= audit_comparator(tsk
->sgid
, f
->op
, f
->val
);
202 result
= audit_comparator(tsk
->fsgid
, f
->op
, f
->val
);
205 result
= audit_comparator(tsk
->personality
, f
->op
, f
->val
);
209 result
= audit_comparator(ctx
->arch
, f
->op
, f
->val
);
213 if (ctx
&& ctx
->return_valid
)
214 result
= audit_comparator(ctx
->return_code
, f
->op
, f
->val
);
217 if (ctx
&& ctx
->return_valid
) {
219 result
= audit_comparator(ctx
->return_valid
, f
->op
, AUDITSC_SUCCESS
);
221 result
= audit_comparator(ctx
->return_valid
, f
->op
, AUDITSC_FAILURE
);
226 for (j
= 0; j
< ctx
->name_count
; j
++) {
227 if (audit_comparator(MAJOR(ctx
->names
[j
].dev
), f
->op
, f
->val
)) {
236 for (j
= 0; j
< ctx
->name_count
; j
++) {
237 if (audit_comparator(MINOR(ctx
->names
[j
].dev
), f
->op
, f
->val
)) {
246 for (j
= 0; j
< ctx
->name_count
; j
++) {
247 if (audit_comparator(ctx
->names
[j
].ino
, f
->op
, f
->val
) ||
248 audit_comparator(ctx
->names
[j
].pino
, f
->op
, f
->val
)) {
258 result
= audit_comparator(ctx
->loginuid
, f
->op
, f
->val
);
265 result
= audit_comparator(ctx
->argv
[f
->type
-AUDIT_ARG0
], f
->op
, f
->val
);
272 switch (rule
->action
) {
273 case AUDIT_NEVER
: *state
= AUDIT_DISABLED
; break;
274 case AUDIT_POSSIBLE
: *state
= AUDIT_BUILD_CONTEXT
; break;
275 case AUDIT_ALWAYS
: *state
= AUDIT_RECORD_CONTEXT
; break;
280 /* At process creation time, we can determine if system-call auditing is
281 * completely disabled for this task. Since we only have the task
282 * structure at this point, we can only check uid and gid.
284 static enum audit_state
audit_filter_task(struct task_struct
*tsk
)
286 struct audit_entry
*e
;
287 enum audit_state state
;
290 list_for_each_entry_rcu(e
, &audit_filter_list
[AUDIT_FILTER_TASK
], list
) {
291 if (audit_filter_rules(tsk
, &e
->rule
, NULL
, &state
)) {
297 return AUDIT_BUILD_CONTEXT
;
300 /* At syscall entry and exit time, this filter is called if the
301 * audit_state is not low enough that auditing cannot take place, but is
302 * also not high enough that we already know we have to write an audit
303 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
305 static enum audit_state
audit_filter_syscall(struct task_struct
*tsk
,
306 struct audit_context
*ctx
,
307 struct list_head
*list
)
309 struct audit_entry
*e
;
310 enum audit_state state
;
312 if (audit_pid
&& tsk
->tgid
== audit_pid
)
313 return AUDIT_DISABLED
;
316 if (!list_empty(list
)) {
317 int word
= AUDIT_WORD(ctx
->major
);
318 int bit
= AUDIT_BIT(ctx
->major
);
320 list_for_each_entry_rcu(e
, list
, list
) {
321 if ((e
->rule
.mask
[word
] & bit
) == bit
322 && audit_filter_rules(tsk
, &e
->rule
, ctx
, &state
)) {
329 return AUDIT_BUILD_CONTEXT
;
332 static inline struct audit_context
*audit_get_context(struct task_struct
*tsk
,
336 struct audit_context
*context
= tsk
->audit_context
;
338 if (likely(!context
))
340 context
->return_valid
= return_valid
;
341 context
->return_code
= return_code
;
343 if (context
->in_syscall
&& !context
->auditable
) {
344 enum audit_state state
;
345 state
= audit_filter_syscall(tsk
, context
, &audit_filter_list
[AUDIT_FILTER_EXIT
]);
346 if (state
== AUDIT_RECORD_CONTEXT
)
347 context
->auditable
= 1;
350 context
->pid
= tsk
->pid
;
351 context
->uid
= tsk
->uid
;
352 context
->gid
= tsk
->gid
;
353 context
->euid
= tsk
->euid
;
354 context
->suid
= tsk
->suid
;
355 context
->fsuid
= tsk
->fsuid
;
356 context
->egid
= tsk
->egid
;
357 context
->sgid
= tsk
->sgid
;
358 context
->fsgid
= tsk
->fsgid
;
359 context
->personality
= tsk
->personality
;
360 tsk
->audit_context
= NULL
;
364 static inline void audit_free_names(struct audit_context
*context
)
369 if (context
->auditable
370 ||context
->put_count
+ context
->ino_count
!= context
->name_count
) {
371 printk(KERN_ERR
"%s:%d(:%d): major=%d in_syscall=%d"
372 " name_count=%d put_count=%d"
373 " ino_count=%d [NOT freeing]\n",
375 context
->serial
, context
->major
, context
->in_syscall
,
376 context
->name_count
, context
->put_count
,
378 for (i
= 0; i
< context
->name_count
; i
++) {
379 printk(KERN_ERR
"names[%d] = %p = %s\n", i
,
380 context
->names
[i
].name
,
381 context
->names
[i
].name
?: "(null)");
388 context
->put_count
= 0;
389 context
->ino_count
= 0;
392 for (i
= 0; i
< context
->name_count
; i
++) {
393 char *p
= context
->names
[i
].ctx
;
394 context
->names
[i
].ctx
= NULL
;
396 if (context
->names
[i
].name
)
397 __putname(context
->names
[i
].name
);
399 context
->name_count
= 0;
403 mntput(context
->pwdmnt
);
405 context
->pwdmnt
= NULL
;
408 static inline void audit_free_aux(struct audit_context
*context
)
410 struct audit_aux_data
*aux
;
412 while ((aux
= context
->aux
)) {
413 if (aux
->type
== AUDIT_AVC_PATH
) {
414 struct audit_aux_data_path
*axi
= (void *)aux
;
418 if ( aux
->type
== AUDIT_IPC
) {
419 struct audit_aux_data_ipcctl
*axi
= (void *)aux
;
424 context
->aux
= aux
->next
;
429 static inline void audit_zero_context(struct audit_context
*context
,
430 enum audit_state state
)
432 uid_t loginuid
= context
->loginuid
;
434 memset(context
, 0, sizeof(*context
));
435 context
->state
= state
;
436 context
->loginuid
= loginuid
;
439 static inline struct audit_context
*audit_alloc_context(enum audit_state state
)
441 struct audit_context
*context
;
443 if (!(context
= kmalloc(sizeof(*context
), GFP_KERNEL
)))
445 audit_zero_context(context
, state
);
450 * audit_alloc - allocate an audit context block for a task
453 * Filter on the task information and allocate a per-task audit context
454 * if necessary. Doing so turns on system call auditing for the
455 * specified task. This is called from copy_process, so no lock is
458 int audit_alloc(struct task_struct
*tsk
)
460 struct audit_context
*context
;
461 enum audit_state state
;
463 if (likely(!audit_enabled
))
464 return 0; /* Return if not auditing. */
466 state
= audit_filter_task(tsk
);
467 if (likely(state
== AUDIT_DISABLED
))
470 if (!(context
= audit_alloc_context(state
))) {
471 audit_log_lost("out of memory in audit_alloc");
475 /* Preserve login uid */
476 context
->loginuid
= -1;
477 if (current
->audit_context
)
478 context
->loginuid
= current
->audit_context
->loginuid
;
480 tsk
->audit_context
= context
;
481 set_tsk_thread_flag(tsk
, TIF_SYSCALL_AUDIT
);
485 static inline void audit_free_context(struct audit_context
*context
)
487 struct audit_context
*previous
;
491 previous
= context
->previous
;
492 if (previous
|| (count
&& count
< 10)) {
494 printk(KERN_ERR
"audit(:%d): major=%d name_count=%d:"
495 " freeing multiple contexts (%d)\n",
496 context
->serial
, context
->major
,
497 context
->name_count
, count
);
499 audit_free_names(context
);
500 audit_free_aux(context
);
505 printk(KERN_ERR
"audit: freed %d contexts\n", count
);
508 static void audit_log_task_context(struct audit_buffer
*ab
)
513 len
= security_getprocattr(current
, "current", NULL
, 0);
520 ctx
= kmalloc(len
, GFP_KERNEL
);
524 len
= security_getprocattr(current
, "current", ctx
, len
);
528 audit_log_format(ab
, " subj=%s", ctx
);
534 audit_panic("error in audit_log_task_context");
538 static void audit_log_task_info(struct audit_buffer
*ab
, struct task_struct
*tsk
)
540 char name
[sizeof(tsk
->comm
)];
541 struct mm_struct
*mm
= tsk
->mm
;
542 struct vm_area_struct
*vma
;
546 get_task_comm(name
, tsk
);
547 audit_log_format(ab
, " comm=");
548 audit_log_untrustedstring(ab
, name
);
551 down_read(&mm
->mmap_sem
);
554 if ((vma
->vm_flags
& VM_EXECUTABLE
) &&
556 audit_log_d_path(ab
, "exe=",
557 vma
->vm_file
->f_dentry
,
558 vma
->vm_file
->f_vfsmnt
);
563 up_read(&mm
->mmap_sem
);
565 audit_log_task_context(ab
);
568 static void audit_log_exit(struct audit_context
*context
, struct task_struct
*tsk
)
571 struct audit_buffer
*ab
;
572 struct audit_aux_data
*aux
;
577 ab
= audit_log_start(context
, GFP_KERNEL
, AUDIT_SYSCALL
);
579 return; /* audit_panic has been called */
580 audit_log_format(ab
, "arch=%x syscall=%d",
581 context
->arch
, context
->major
);
582 if (context
->personality
!= PER_LINUX
)
583 audit_log_format(ab
, " per=%lx", context
->personality
);
584 if (context
->return_valid
)
585 audit_log_format(ab
, " success=%s exit=%ld",
586 (context
->return_valid
==AUDITSC_SUCCESS
)?"yes":"no",
587 context
->return_code
);
588 if (tsk
->signal
&& tsk
->signal
->tty
&& tsk
->signal
->tty
->name
)
589 tty
= tsk
->signal
->tty
->name
;
593 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
594 " pid=%d auid=%u uid=%u gid=%u"
595 " euid=%u suid=%u fsuid=%u"
596 " egid=%u sgid=%u fsgid=%u tty=%s",
606 context
->euid
, context
->suid
, context
->fsuid
,
607 context
->egid
, context
->sgid
, context
->fsgid
, tty
);
608 audit_log_task_info(ab
, tsk
);
611 for (aux
= context
->aux
; aux
; aux
= aux
->next
) {
613 ab
= audit_log_start(context
, GFP_KERNEL
, aux
->type
);
615 continue; /* audit_panic has been called */
619 struct audit_aux_data_ipcctl
*axi
= (void *)aux
;
621 " qbytes=%lx iuid=%u igid=%u mode=%x obj=%s",
622 axi
->qbytes
, axi
->uid
, axi
->gid
, axi
->mode
, axi
->ctx
);
625 case AUDIT_SOCKETCALL
: {
627 struct audit_aux_data_socketcall
*axs
= (void *)aux
;
628 audit_log_format(ab
, "nargs=%d", axs
->nargs
);
629 for (i
=0; i
<axs
->nargs
; i
++)
630 audit_log_format(ab
, " a%d=%lx", i
, axs
->args
[i
]);
633 case AUDIT_SOCKADDR
: {
634 struct audit_aux_data_sockaddr
*axs
= (void *)aux
;
636 audit_log_format(ab
, "saddr=");
637 audit_log_hex(ab
, axs
->a
, axs
->len
);
640 case AUDIT_AVC_PATH
: {
641 struct audit_aux_data_path
*axi
= (void *)aux
;
642 audit_log_d_path(ab
, "path=", axi
->dentry
, axi
->mnt
);
649 if (context
->pwd
&& context
->pwdmnt
) {
650 ab
= audit_log_start(context
, GFP_KERNEL
, AUDIT_CWD
);
652 audit_log_d_path(ab
, "cwd=", context
->pwd
, context
->pwdmnt
);
656 for (i
= 0; i
< context
->name_count
; i
++) {
657 unsigned long ino
= context
->names
[i
].ino
;
658 unsigned long pino
= context
->names
[i
].pino
;
660 ab
= audit_log_start(context
, GFP_KERNEL
, AUDIT_PATH
);
662 continue; /* audit_panic has been called */
664 audit_log_format(ab
, "item=%d", i
);
666 audit_log_format(ab
, " name=");
667 if (context
->names
[i
].name
)
668 audit_log_untrustedstring(ab
, context
->names
[i
].name
);
670 audit_log_format(ab
, "(null)");
672 if (pino
!= (unsigned long)-1)
673 audit_log_format(ab
, " parent=%lu", pino
);
674 if (ino
!= (unsigned long)-1)
675 audit_log_format(ab
, " inode=%lu", ino
);
676 if ((pino
!= (unsigned long)-1) || (ino
!= (unsigned long)-1))
677 audit_log_format(ab
, " dev=%02x:%02x mode=%#o"
678 " ouid=%u ogid=%u rdev=%02x:%02x",
679 MAJOR(context
->names
[i
].dev
),
680 MINOR(context
->names
[i
].dev
),
681 context
->names
[i
].mode
,
682 context
->names
[i
].uid
,
683 context
->names
[i
].gid
,
684 MAJOR(context
->names
[i
].rdev
),
685 MINOR(context
->names
[i
].rdev
));
686 if (context
->names
[i
].ctx
) {
687 audit_log_format(ab
, " obj=%s",
688 context
->names
[i
].ctx
);
696 * audit_free - free a per-task audit context
697 * @tsk: task whose audit context block to free
699 * Called from copy_process and do_exit
701 void audit_free(struct task_struct
*tsk
)
703 struct audit_context
*context
;
705 context
= audit_get_context(tsk
, 0, 0);
706 if (likely(!context
))
709 /* Check for system calls that do not go through the exit
710 * function (e.g., exit_group), then free context block.
711 * We use GFP_ATOMIC here because we might be doing this
712 * in the context of the idle thread */
713 /* that can happen only if we are called from do_exit() */
714 if (context
->in_syscall
&& context
->auditable
)
715 audit_log_exit(context
, tsk
);
717 audit_free_context(context
);
721 * audit_syscall_entry - fill in an audit record at syscall entry
722 * @tsk: task being audited
723 * @arch: architecture type
724 * @major: major syscall type (function)
725 * @a1: additional syscall register 1
726 * @a2: additional syscall register 2
727 * @a3: additional syscall register 3
728 * @a4: additional syscall register 4
730 * Fill in audit context at syscall entry. This only happens if the
731 * audit context was created when the task was created and the state or
732 * filters demand the audit context be built. If the state from the
733 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
734 * then the record will be written at syscall exit time (otherwise, it
735 * will only be written if another part of the kernel requests that it
738 void audit_syscall_entry(int arch
, int major
,
739 unsigned long a1
, unsigned long a2
,
740 unsigned long a3
, unsigned long a4
)
742 struct task_struct
*tsk
= current
;
743 struct audit_context
*context
= tsk
->audit_context
;
744 enum audit_state state
;
749 * This happens only on certain architectures that make system
750 * calls in kernel_thread via the entry.S interface, instead of
751 * with direct calls. (If you are porting to a new
752 * architecture, hitting this condition can indicate that you
753 * got the _exit/_leave calls backward in entry.S.)
757 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
759 * This also happens with vm86 emulation in a non-nested manner
760 * (entries without exits), so this case must be caught.
762 if (context
->in_syscall
) {
763 struct audit_context
*newctx
;
767 "audit(:%d) pid=%d in syscall=%d;"
768 " entering syscall=%d\n",
769 context
->serial
, tsk
->pid
, context
->major
, major
);
771 newctx
= audit_alloc_context(context
->state
);
773 newctx
->previous
= context
;
775 tsk
->audit_context
= newctx
;
777 /* If we can't alloc a new context, the best we
778 * can do is to leak memory (any pending putname
779 * will be lost). The only other alternative is
780 * to abandon auditing. */
781 audit_zero_context(context
, context
->state
);
784 BUG_ON(context
->in_syscall
|| context
->name_count
);
789 context
->arch
= arch
;
790 context
->major
= major
;
791 context
->argv
[0] = a1
;
792 context
->argv
[1] = a2
;
793 context
->argv
[2] = a3
;
794 context
->argv
[3] = a4
;
796 state
= context
->state
;
797 if (state
== AUDIT_SETUP_CONTEXT
|| state
== AUDIT_BUILD_CONTEXT
)
798 state
= audit_filter_syscall(tsk
, context
, &audit_filter_list
[AUDIT_FILTER_ENTRY
]);
799 if (likely(state
== AUDIT_DISABLED
))
803 context
->ctime
= CURRENT_TIME
;
804 context
->in_syscall
= 1;
805 context
->auditable
= !!(state
== AUDIT_RECORD_CONTEXT
);
809 * audit_syscall_exit - deallocate audit context after a system call
810 * @tsk: task being audited
811 * @valid: success/failure flag
812 * @return_code: syscall return value
814 * Tear down after system call. If the audit context has been marked as
815 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
816 * filtering, or because some other part of the kernel write an audit
817 * message), then write out the syscall information. In call cases,
818 * free the names stored from getname().
820 void audit_syscall_exit(int valid
, long return_code
)
822 struct task_struct
*tsk
= current
;
823 struct audit_context
*context
;
825 context
= audit_get_context(tsk
, valid
, return_code
);
827 if (likely(!context
))
830 if (context
->in_syscall
&& context
->auditable
)
831 audit_log_exit(context
, tsk
);
833 context
->in_syscall
= 0;
834 context
->auditable
= 0;
836 if (context
->previous
) {
837 struct audit_context
*new_context
= context
->previous
;
838 context
->previous
= NULL
;
839 audit_free_context(context
);
840 tsk
->audit_context
= new_context
;
842 audit_free_names(context
);
843 audit_free_aux(context
);
844 tsk
->audit_context
= context
;
849 * audit_getname - add a name to the list
852 * Add a name to the list of audit names for this context.
853 * Called from fs/namei.c:getname().
855 void audit_getname(const char *name
)
857 struct audit_context
*context
= current
->audit_context
;
859 if (!context
|| IS_ERR(name
) || !name
)
862 if (!context
->in_syscall
) {
864 printk(KERN_ERR
"%s:%d(:%d): ignoring getname(%p)\n",
865 __FILE__
, __LINE__
, context
->serial
, name
);
870 BUG_ON(context
->name_count
>= AUDIT_NAMES
);
871 context
->names
[context
->name_count
].name
= name
;
872 context
->names
[context
->name_count
].ino
= (unsigned long)-1;
873 ++context
->name_count
;
875 read_lock(¤t
->fs
->lock
);
876 context
->pwd
= dget(current
->fs
->pwd
);
877 context
->pwdmnt
= mntget(current
->fs
->pwdmnt
);
878 read_unlock(¤t
->fs
->lock
);
883 /* audit_putname - intercept a putname request
884 * @name: name to intercept and delay for putname
886 * If we have stored the name from getname in the audit context,
887 * then we delay the putname until syscall exit.
888 * Called from include/linux/fs.h:putname().
890 void audit_putname(const char *name
)
892 struct audit_context
*context
= current
->audit_context
;
895 if (!context
->in_syscall
) {
897 printk(KERN_ERR
"%s:%d(:%d): __putname(%p)\n",
898 __FILE__
, __LINE__
, context
->serial
, name
);
899 if (context
->name_count
) {
901 for (i
= 0; i
< context
->name_count
; i
++)
902 printk(KERN_ERR
"name[%d] = %p = %s\n", i
,
903 context
->names
[i
].name
,
904 context
->names
[i
].name
?: "(null)");
911 ++context
->put_count
;
912 if (context
->put_count
> context
->name_count
) {
913 printk(KERN_ERR
"%s:%d(:%d): major=%d"
914 " in_syscall=%d putname(%p) name_count=%d"
917 context
->serial
, context
->major
,
918 context
->in_syscall
, name
, context
->name_count
,
926 void audit_inode_context(int idx
, const struct inode
*inode
)
928 struct audit_context
*context
= current
->audit_context
;
929 const char *suffix
= security_inode_xattr_getsuffix();
936 len
= security_inode_getsecurity(inode
, suffix
, NULL
, 0, 0);
937 if (len
== -EOPNOTSUPP
)
942 ctx
= kmalloc(len
, GFP_KERNEL
);
946 len
= security_inode_getsecurity(inode
, suffix
, ctx
, len
, 0);
950 kfree(context
->names
[idx
].ctx
);
951 context
->names
[idx
].ctx
= ctx
;
957 audit_panic("error in audit_inode_context");
964 * audit_inode - store the inode and device from a lookup
965 * @name: name being audited
966 * @inode: inode being audited
967 * @flags: lookup flags (as used in path_lookup())
969 * Called from fs/namei.c:path_lookup().
971 void __audit_inode(const char *name
, const struct inode
*inode
, unsigned flags
)
974 struct audit_context
*context
= current
->audit_context
;
976 if (!context
->in_syscall
)
978 if (context
->name_count
979 && context
->names
[context
->name_count
-1].name
980 && context
->names
[context
->name_count
-1].name
== name
)
981 idx
= context
->name_count
- 1;
982 else if (context
->name_count
> 1
983 && context
->names
[context
->name_count
-2].name
984 && context
->names
[context
->name_count
-2].name
== name
)
985 idx
= context
->name_count
- 2;
987 /* FIXME: how much do we care about inodes that have no
988 * associated name? */
989 if (context
->name_count
>= AUDIT_NAMES
- AUDIT_NAMES_RESERVED
)
991 idx
= context
->name_count
++;
992 context
->names
[idx
].name
= NULL
;
994 ++context
->ino_count
;
997 context
->names
[idx
].dev
= inode
->i_sb
->s_dev
;
998 context
->names
[idx
].mode
= inode
->i_mode
;
999 context
->names
[idx
].uid
= inode
->i_uid
;
1000 context
->names
[idx
].gid
= inode
->i_gid
;
1001 context
->names
[idx
].rdev
= inode
->i_rdev
;
1002 audit_inode_context(idx
, inode
);
1003 if ((flags
& LOOKUP_PARENT
) && (strcmp(name
, "/") != 0) &&
1004 (strcmp(name
, ".") != 0)) {
1005 context
->names
[idx
].ino
= (unsigned long)-1;
1006 context
->names
[idx
].pino
= inode
->i_ino
;
1008 context
->names
[idx
].ino
= inode
->i_ino
;
1009 context
->names
[idx
].pino
= (unsigned long)-1;
1014 * audit_inode_child - collect inode info for created/removed objects
1015 * @dname: inode's dentry name
1016 * @inode: inode being audited
1017 * @pino: inode number of dentry parent
1019 * For syscalls that create or remove filesystem objects, audit_inode
1020 * can only collect information for the filesystem object's parent.
1021 * This call updates the audit context with the child's information.
1022 * Syscalls that create a new filesystem object must be hooked after
1023 * the object is created. Syscalls that remove a filesystem object
1024 * must be hooked prior, in order to capture the target inode during
1025 * unsuccessful attempts.
1027 void __audit_inode_child(const char *dname
, const struct inode
*inode
,
1031 struct audit_context
*context
= current
->audit_context
;
1033 if (!context
->in_syscall
)
1036 /* determine matching parent */
1038 for (idx
= 0; idx
< context
->name_count
; idx
++)
1039 if (context
->names
[idx
].pino
== pino
) {
1041 const char *name
= context
->names
[idx
].name
;
1042 int dlen
= strlen(dname
);
1043 int nlen
= name
? strlen(name
) : 0;
1048 /* disregard trailing slashes */
1049 n
= name
+ nlen
- 1;
1050 while ((*n
== '/') && (n
> name
))
1053 /* find last path component */
1057 else if (n
> name
) {
1064 if (strncmp(n
, dname
, dlen
) == 0)
1065 goto update_context
;
1068 /* catch-all in case match not found */
1069 idx
= context
->name_count
++;
1070 context
->names
[idx
].name
= NULL
;
1071 context
->names
[idx
].pino
= pino
;
1073 context
->ino_count
++;
1078 context
->names
[idx
].ino
= inode
->i_ino
;
1079 context
->names
[idx
].dev
= inode
->i_sb
->s_dev
;
1080 context
->names
[idx
].mode
= inode
->i_mode
;
1081 context
->names
[idx
].uid
= inode
->i_uid
;
1082 context
->names
[idx
].gid
= inode
->i_gid
;
1083 context
->names
[idx
].rdev
= inode
->i_rdev
;
1084 audit_inode_context(idx
, inode
);
1089 * auditsc_get_stamp - get local copies of audit_context values
1090 * @ctx: audit_context for the task
1091 * @t: timespec to store time recorded in the audit_context
1092 * @serial: serial value that is recorded in the audit_context
1094 * Also sets the context as auditable.
1096 void auditsc_get_stamp(struct audit_context
*ctx
,
1097 struct timespec
*t
, unsigned int *serial
)
1100 ctx
->serial
= audit_serial();
1101 t
->tv_sec
= ctx
->ctime
.tv_sec
;
1102 t
->tv_nsec
= ctx
->ctime
.tv_nsec
;
1103 *serial
= ctx
->serial
;
1108 * audit_set_loginuid - set a task's audit_context loginuid
1109 * @task: task whose audit context is being modified
1110 * @loginuid: loginuid value
1114 * Called (set) from fs/proc/base.c::proc_loginuid_write().
1116 int audit_set_loginuid(struct task_struct
*task
, uid_t loginuid
)
1118 if (task
->audit_context
) {
1119 struct audit_buffer
*ab
;
1121 ab
= audit_log_start(NULL
, GFP_KERNEL
, AUDIT_LOGIN
);
1123 audit_log_format(ab
, "login pid=%d uid=%u "
1124 "old auid=%u new auid=%u",
1125 task
->pid
, task
->uid
,
1126 task
->audit_context
->loginuid
, loginuid
);
1129 task
->audit_context
->loginuid
= loginuid
;
1135 * audit_get_loginuid - get the loginuid for an audit_context
1136 * @ctx: the audit_context
1138 * Returns the context's loginuid or -1 if @ctx is NULL.
1140 uid_t
audit_get_loginuid(struct audit_context
*ctx
)
1142 return ctx
? ctx
->loginuid
: -1;
1145 static char *audit_ipc_context(struct kern_ipc_perm
*ipcp
)
1147 struct audit_context
*context
= current
->audit_context
;
1151 if (likely(!context
))
1154 len
= security_ipc_getsecurity(ipcp
, NULL
, 0);
1155 if (len
== -EOPNOTSUPP
)
1160 ctx
= kmalloc(len
, GFP_ATOMIC
);
1164 len
= security_ipc_getsecurity(ipcp
, ctx
, len
);
1172 audit_panic("error in audit_ipc_context");
1178 * audit_ipc_perms - record audit data for ipc
1179 * @qbytes: msgq bytes
1180 * @uid: msgq user id
1181 * @gid: msgq group id
1182 * @mode: msgq mode (permissions)
1184 * Returns 0 for success or NULL context or < 0 on error.
1186 int audit_ipc_perms(unsigned long qbytes
, uid_t uid
, gid_t gid
, mode_t mode
, struct kern_ipc_perm
*ipcp
)
1188 struct audit_aux_data_ipcctl
*ax
;
1189 struct audit_context
*context
= current
->audit_context
;
1191 if (likely(!context
))
1194 ax
= kmalloc(sizeof(*ax
), GFP_ATOMIC
);
1198 ax
->qbytes
= qbytes
;
1202 ax
->ctx
= audit_ipc_context(ipcp
);
1204 ax
->d
.type
= AUDIT_IPC
;
1205 ax
->d
.next
= context
->aux
;
1206 context
->aux
= (void *)ax
;
1211 * audit_socketcall - record audit data for sys_socketcall
1212 * @nargs: number of args
1215 * Returns 0 for success or NULL context or < 0 on error.
1217 int audit_socketcall(int nargs
, unsigned long *args
)
1219 struct audit_aux_data_socketcall
*ax
;
1220 struct audit_context
*context
= current
->audit_context
;
1222 if (likely(!context
))
1225 ax
= kmalloc(sizeof(*ax
) + nargs
* sizeof(unsigned long), GFP_KERNEL
);
1230 memcpy(ax
->args
, args
, nargs
* sizeof(unsigned long));
1232 ax
->d
.type
= AUDIT_SOCKETCALL
;
1233 ax
->d
.next
= context
->aux
;
1234 context
->aux
= (void *)ax
;
1239 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
1240 * @len: data length in user space
1241 * @a: data address in kernel space
1243 * Returns 0 for success or NULL context or < 0 on error.
1245 int audit_sockaddr(int len
, void *a
)
1247 struct audit_aux_data_sockaddr
*ax
;
1248 struct audit_context
*context
= current
->audit_context
;
1250 if (likely(!context
))
1253 ax
= kmalloc(sizeof(*ax
) + len
, GFP_KERNEL
);
1258 memcpy(ax
->a
, a
, len
);
1260 ax
->d
.type
= AUDIT_SOCKADDR
;
1261 ax
->d
.next
= context
->aux
;
1262 context
->aux
= (void *)ax
;
1267 * audit_avc_path - record the granting or denial of permissions
1268 * @dentry: dentry to record
1269 * @mnt: mnt to record
1271 * Returns 0 for success or NULL context or < 0 on error.
1273 * Called from security/selinux/avc.c::avc_audit()
1275 int audit_avc_path(struct dentry
*dentry
, struct vfsmount
*mnt
)
1277 struct audit_aux_data_path
*ax
;
1278 struct audit_context
*context
= current
->audit_context
;
1280 if (likely(!context
))
1283 ax
= kmalloc(sizeof(*ax
), GFP_ATOMIC
);
1287 ax
->dentry
= dget(dentry
);
1288 ax
->mnt
= mntget(mnt
);
1290 ax
->d
.type
= AUDIT_AVC_PATH
;
1291 ax
->d
.next
= context
->aux
;
1292 context
->aux
= (void *)ax
;
1297 * audit_signal_info - record signal info for shutting down audit subsystem
1298 * @sig: signal value
1299 * @t: task being signaled
1301 * If the audit subsystem is being terminated, record the task (pid)
1302 * and uid that is doing that.
1304 void audit_signal_info(int sig
, struct task_struct
*t
)
1306 extern pid_t audit_sig_pid
;
1307 extern uid_t audit_sig_uid
;
1309 if (unlikely(audit_pid
&& t
->tgid
== audit_pid
)) {
1310 if (sig
== SIGTERM
|| sig
== SIGHUP
) {
1311 struct audit_context
*ctx
= current
->audit_context
;
1312 audit_sig_pid
= current
->pid
;
1314 audit_sig_uid
= ctx
->loginuid
;
1316 audit_sig_uid
= current
->uid
;