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, 2006 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 * POSIX message queue support added by George Wilson <ltcgcw@us.ibm.com>,
35 * The support of additional filter rules compares (>, <, >=, <=) was
36 * added by Dustin Kirkland <dustin.kirkland@us.ibm.com>, 2005.
38 * Modified by Amy Griffis <amy.griffis@hp.com> to collect additional
39 * filesystem information.
41 * Subject and object context labeling support added by <danjones@us.ibm.com>
42 * and <dustin.kirkland@us.ibm.com> for LSPP certification compliance.
45 #include <linux/init.h>
46 #include <asm/types.h>
47 #include <asm/atomic.h>
49 #include <linux/namei.h>
51 #include <linux/module.h>
52 #include <linux/mount.h>
53 #include <linux/socket.h>
54 #include <linux/mqueue.h>
55 #include <linux/audit.h>
56 #include <linux/personality.h>
57 #include <linux/time.h>
58 #include <linux/netlink.h>
59 #include <linux/compiler.h>
60 #include <asm/unistd.h>
61 #include <linux/security.h>
62 #include <linux/list.h>
63 #include <linux/tty.h>
64 #include <linux/binfmts.h>
65 #include <linux/highmem.h>
66 #include <linux/syscalls.h>
67 #include <linux/inotify.h>
68 #include <linux/capability.h>
72 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
73 * for saving names from getname(). */
74 #define AUDIT_NAMES 20
76 /* Indicates that audit should log the full pathname. */
77 #define AUDIT_NAME_FULL -1
79 /* no execve audit message should be longer than this (userspace limits) */
80 #define MAX_EXECVE_AUDIT_LEN 7500
82 /* number of audit rules */
85 /* determines whether we collect data for signals sent */
88 struct audit_cap_data
{
89 kernel_cap_t permitted
;
90 kernel_cap_t inheritable
;
92 unsigned int fE
; /* effective bit of a file capability */
93 kernel_cap_t effective
; /* effective set of a process */
97 /* When fs/namei.c:getname() is called, we store the pointer in name and
98 * we don't let putname() free it (instead we free all of the saved
99 * pointers at syscall exit time).
101 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
104 int name_len
; /* number of name's characters to log */
105 unsigned name_put
; /* call __putname() for this name */
113 struct audit_cap_data fcap
;
114 unsigned int fcap_ver
;
117 struct audit_aux_data
{
118 struct audit_aux_data
*next
;
122 #define AUDIT_AUX_IPCPERM 0
124 /* Number of target pids per aux struct. */
125 #define AUDIT_AUX_PIDS 16
127 struct audit_aux_data_mq_open
{
128 struct audit_aux_data d
;
134 struct audit_aux_data_mq_sendrecv
{
135 struct audit_aux_data d
;
138 unsigned int msg_prio
;
139 struct timespec abs_timeout
;
142 struct audit_aux_data_mq_notify
{
143 struct audit_aux_data d
;
145 struct sigevent notification
;
148 struct audit_aux_data_mq_getsetattr
{
149 struct audit_aux_data d
;
151 struct mq_attr mqstat
;
154 struct audit_aux_data_ipcctl
{
155 struct audit_aux_data d
;
157 unsigned long qbytes
;
164 struct audit_aux_data_execve
{
165 struct audit_aux_data d
;
168 struct mm_struct
*mm
;
171 struct audit_aux_data_socketcall
{
172 struct audit_aux_data d
;
174 unsigned long args
[0];
177 struct audit_aux_data_sockaddr
{
178 struct audit_aux_data d
;
183 struct audit_aux_data_fd_pair
{
184 struct audit_aux_data d
;
188 struct audit_aux_data_pids
{
189 struct audit_aux_data d
;
190 pid_t target_pid
[AUDIT_AUX_PIDS
];
191 uid_t target_auid
[AUDIT_AUX_PIDS
];
192 uid_t target_uid
[AUDIT_AUX_PIDS
];
193 unsigned int target_sessionid
[AUDIT_AUX_PIDS
];
194 u32 target_sid
[AUDIT_AUX_PIDS
];
195 char target_comm
[AUDIT_AUX_PIDS
][TASK_COMM_LEN
];
199 struct audit_tree_refs
{
200 struct audit_tree_refs
*next
;
201 struct audit_chunk
*c
[31];
204 /* The per-task audit context. */
205 struct audit_context
{
206 int dummy
; /* must be the first element */
207 int in_syscall
; /* 1 if task is in a syscall */
208 enum audit_state state
;
209 unsigned int serial
; /* serial number for record */
210 struct timespec ctime
; /* time of syscall entry */
211 int major
; /* syscall number */
212 unsigned long argv
[4]; /* syscall arguments */
213 int return_valid
; /* return code is valid */
214 long return_code
;/* syscall return code */
215 int auditable
; /* 1 if record should be written */
217 struct audit_names names
[AUDIT_NAMES
];
218 char * filterkey
; /* key for rule that triggered record */
220 struct audit_context
*previous
; /* For nested syscalls */
221 struct audit_aux_data
*aux
;
222 struct audit_aux_data
*aux_pids
;
224 /* Save things to print about task_struct */
226 uid_t uid
, euid
, suid
, fsuid
;
227 gid_t gid
, egid
, sgid
, fsgid
;
228 unsigned long personality
;
234 unsigned int target_sessionid
;
236 char target_comm
[TASK_COMM_LEN
];
238 struct audit_tree_refs
*trees
, *first_trees
;
247 #define ACC_MODE(x) ("\004\002\006\006"[(x)&O_ACCMODE])
248 static inline int open_arg(int flags
, int mask
)
250 int n
= ACC_MODE(flags
);
251 if (flags
& (O_TRUNC
| O_CREAT
))
252 n
|= AUDIT_PERM_WRITE
;
256 static int audit_match_perm(struct audit_context
*ctx
, int mask
)
263 switch (audit_classify_syscall(ctx
->arch
, n
)) {
265 if ((mask
& AUDIT_PERM_WRITE
) &&
266 audit_match_class(AUDIT_CLASS_WRITE
, n
))
268 if ((mask
& AUDIT_PERM_READ
) &&
269 audit_match_class(AUDIT_CLASS_READ
, n
))
271 if ((mask
& AUDIT_PERM_ATTR
) &&
272 audit_match_class(AUDIT_CLASS_CHATTR
, n
))
275 case 1: /* 32bit on biarch */
276 if ((mask
& AUDIT_PERM_WRITE
) &&
277 audit_match_class(AUDIT_CLASS_WRITE_32
, n
))
279 if ((mask
& AUDIT_PERM_READ
) &&
280 audit_match_class(AUDIT_CLASS_READ_32
, n
))
282 if ((mask
& AUDIT_PERM_ATTR
) &&
283 audit_match_class(AUDIT_CLASS_CHATTR_32
, n
))
287 return mask
& ACC_MODE(ctx
->argv
[1]);
289 return mask
& ACC_MODE(ctx
->argv
[2]);
290 case 4: /* socketcall */
291 return ((mask
& AUDIT_PERM_WRITE
) && ctx
->argv
[0] == SYS_BIND
);
293 return mask
& AUDIT_PERM_EXEC
;
299 static int audit_match_filetype(struct audit_context
*ctx
, int which
)
301 unsigned index
= which
& ~S_IFMT
;
302 mode_t mode
= which
& S_IFMT
;
307 if (index
>= ctx
->name_count
)
309 if (ctx
->names
[index
].ino
== -1)
311 if ((ctx
->names
[index
].mode
^ mode
) & S_IFMT
)
317 * We keep a linked list of fixed-sized (31 pointer) arrays of audit_chunk *;
318 * ->first_trees points to its beginning, ->trees - to the current end of data.
319 * ->tree_count is the number of free entries in array pointed to by ->trees.
320 * Original condition is (NULL, NULL, 0); as soon as it grows we never revert to NULL,
321 * "empty" becomes (p, p, 31) afterwards. We don't shrink the list (and seriously,
322 * it's going to remain 1-element for almost any setup) until we free context itself.
323 * References in it _are_ dropped - at the same time we free/drop aux stuff.
326 #ifdef CONFIG_AUDIT_TREE
327 static int put_tree_ref(struct audit_context
*ctx
, struct audit_chunk
*chunk
)
329 struct audit_tree_refs
*p
= ctx
->trees
;
330 int left
= ctx
->tree_count
;
332 p
->c
[--left
] = chunk
;
333 ctx
->tree_count
= left
;
342 ctx
->tree_count
= 30;
348 static int grow_tree_refs(struct audit_context
*ctx
)
350 struct audit_tree_refs
*p
= ctx
->trees
;
351 ctx
->trees
= kzalloc(sizeof(struct audit_tree_refs
), GFP_KERNEL
);
357 p
->next
= ctx
->trees
;
359 ctx
->first_trees
= ctx
->trees
;
360 ctx
->tree_count
= 31;
365 static void unroll_tree_refs(struct audit_context
*ctx
,
366 struct audit_tree_refs
*p
, int count
)
368 #ifdef CONFIG_AUDIT_TREE
369 struct audit_tree_refs
*q
;
372 /* we started with empty chain */
373 p
= ctx
->first_trees
;
375 /* if the very first allocation has failed, nothing to do */
380 for (q
= p
; q
!= ctx
->trees
; q
= q
->next
, n
= 31) {
382 audit_put_chunk(q
->c
[n
]);
386 while (n
-- > ctx
->tree_count
) {
387 audit_put_chunk(q
->c
[n
]);
391 ctx
->tree_count
= count
;
395 static void free_tree_refs(struct audit_context
*ctx
)
397 struct audit_tree_refs
*p
, *q
;
398 for (p
= ctx
->first_trees
; p
; p
= q
) {
404 static int match_tree_refs(struct audit_context
*ctx
, struct audit_tree
*tree
)
406 #ifdef CONFIG_AUDIT_TREE
407 struct audit_tree_refs
*p
;
412 for (p
= ctx
->first_trees
; p
!= ctx
->trees
; p
= p
->next
) {
413 for (n
= 0; n
< 31; n
++)
414 if (audit_tree_match(p
->c
[n
], tree
))
419 for (n
= ctx
->tree_count
; n
< 31; n
++)
420 if (audit_tree_match(p
->c
[n
], tree
))
427 /* Determine if any context name data matches a rule's watch data */
428 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
430 static int audit_filter_rules(struct task_struct
*tsk
,
431 struct audit_krule
*rule
,
432 struct audit_context
*ctx
,
433 struct audit_names
*name
,
434 enum audit_state
*state
)
436 int i
, j
, need_sid
= 1;
439 for (i
= 0; i
< rule
->field_count
; i
++) {
440 struct audit_field
*f
= &rule
->fields
[i
];
445 result
= audit_comparator(tsk
->pid
, f
->op
, f
->val
);
450 ctx
->ppid
= sys_getppid();
451 result
= audit_comparator(ctx
->ppid
, f
->op
, f
->val
);
455 result
= audit_comparator(tsk
->uid
, f
->op
, f
->val
);
458 result
= audit_comparator(tsk
->euid
, f
->op
, f
->val
);
461 result
= audit_comparator(tsk
->suid
, f
->op
, f
->val
);
464 result
= audit_comparator(tsk
->fsuid
, f
->op
, f
->val
);
467 result
= audit_comparator(tsk
->gid
, f
->op
, f
->val
);
470 result
= audit_comparator(tsk
->egid
, f
->op
, f
->val
);
473 result
= audit_comparator(tsk
->sgid
, f
->op
, f
->val
);
476 result
= audit_comparator(tsk
->fsgid
, f
->op
, f
->val
);
479 result
= audit_comparator(tsk
->personality
, f
->op
, f
->val
);
483 result
= audit_comparator(ctx
->arch
, f
->op
, f
->val
);
487 if (ctx
&& ctx
->return_valid
)
488 result
= audit_comparator(ctx
->return_code
, f
->op
, f
->val
);
491 if (ctx
&& ctx
->return_valid
) {
493 result
= audit_comparator(ctx
->return_valid
, f
->op
, AUDITSC_SUCCESS
);
495 result
= audit_comparator(ctx
->return_valid
, f
->op
, AUDITSC_FAILURE
);
500 result
= audit_comparator(MAJOR(name
->dev
),
503 for (j
= 0; j
< ctx
->name_count
; j
++) {
504 if (audit_comparator(MAJOR(ctx
->names
[j
].dev
), f
->op
, f
->val
)) {
513 result
= audit_comparator(MINOR(name
->dev
),
516 for (j
= 0; j
< ctx
->name_count
; j
++) {
517 if (audit_comparator(MINOR(ctx
->names
[j
].dev
), f
->op
, f
->val
)) {
526 result
= (name
->ino
== f
->val
);
528 for (j
= 0; j
< ctx
->name_count
; j
++) {
529 if (audit_comparator(ctx
->names
[j
].ino
, f
->op
, f
->val
)) {
537 if (name
&& rule
->watch
->ino
!= (unsigned long)-1)
538 result
= (name
->dev
== rule
->watch
->dev
&&
539 name
->ino
== rule
->watch
->ino
);
543 result
= match_tree_refs(ctx
, rule
->tree
);
548 result
= audit_comparator(tsk
->loginuid
, f
->op
, f
->val
);
550 case AUDIT_SUBJ_USER
:
551 case AUDIT_SUBJ_ROLE
:
552 case AUDIT_SUBJ_TYPE
:
555 /* NOTE: this may return negative values indicating
556 a temporary error. We simply treat this as a
557 match for now to avoid losing information that
558 may be wanted. An error message will also be
562 security_task_getsecid(tsk
, &sid
);
565 result
= security_audit_rule_match(sid
, f
->type
,
574 case AUDIT_OBJ_LEV_LOW
:
575 case AUDIT_OBJ_LEV_HIGH
:
576 /* The above note for AUDIT_SUBJ_USER...AUDIT_SUBJ_CLR
579 /* Find files that match */
581 result
= security_audit_rule_match(
582 name
->osid
, f
->type
, f
->op
,
585 for (j
= 0; j
< ctx
->name_count
; j
++) {
586 if (security_audit_rule_match(
595 /* Find ipc objects that match */
597 struct audit_aux_data
*aux
;
598 for (aux
= ctx
->aux
; aux
;
600 if (aux
->type
== AUDIT_IPC
) {
601 struct audit_aux_data_ipcctl
*axi
= (void *)aux
;
602 if (security_audit_rule_match(axi
->osid
, f
->type
, f
->op
, f
->lsm_rule
, ctx
)) {
616 result
= audit_comparator(ctx
->argv
[f
->type
-AUDIT_ARG0
], f
->op
, f
->val
);
618 case AUDIT_FILTERKEY
:
619 /* ignore this field for filtering */
623 result
= audit_match_perm(ctx
, f
->val
);
626 result
= audit_match_filetype(ctx
, f
->val
);
633 if (rule
->filterkey
&& ctx
)
634 ctx
->filterkey
= kstrdup(rule
->filterkey
, GFP_ATOMIC
);
635 switch (rule
->action
) {
636 case AUDIT_NEVER
: *state
= AUDIT_DISABLED
; break;
637 case AUDIT_ALWAYS
: *state
= AUDIT_RECORD_CONTEXT
; break;
642 /* At process creation time, we can determine if system-call auditing is
643 * completely disabled for this task. Since we only have the task
644 * structure at this point, we can only check uid and gid.
646 static enum audit_state
audit_filter_task(struct task_struct
*tsk
)
648 struct audit_entry
*e
;
649 enum audit_state state
;
652 list_for_each_entry_rcu(e
, &audit_filter_list
[AUDIT_FILTER_TASK
], list
) {
653 if (audit_filter_rules(tsk
, &e
->rule
, NULL
, NULL
, &state
)) {
659 return AUDIT_BUILD_CONTEXT
;
662 /* At syscall entry and exit time, this filter is called if the
663 * audit_state is not low enough that auditing cannot take place, but is
664 * also not high enough that we already know we have to write an audit
665 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
667 static enum audit_state
audit_filter_syscall(struct task_struct
*tsk
,
668 struct audit_context
*ctx
,
669 struct list_head
*list
)
671 struct audit_entry
*e
;
672 enum audit_state state
;
674 if (audit_pid
&& tsk
->tgid
== audit_pid
)
675 return AUDIT_DISABLED
;
678 if (!list_empty(list
)) {
679 int word
= AUDIT_WORD(ctx
->major
);
680 int bit
= AUDIT_BIT(ctx
->major
);
682 list_for_each_entry_rcu(e
, list
, list
) {
683 if ((e
->rule
.mask
[word
] & bit
) == bit
&&
684 audit_filter_rules(tsk
, &e
->rule
, ctx
, NULL
,
692 return AUDIT_BUILD_CONTEXT
;
695 /* At syscall exit time, this filter is called if any audit_names[] have been
696 * collected during syscall processing. We only check rules in sublists at hash
697 * buckets applicable to the inode numbers in audit_names[].
698 * Regarding audit_state, same rules apply as for audit_filter_syscall().
700 enum audit_state
audit_filter_inodes(struct task_struct
*tsk
,
701 struct audit_context
*ctx
)
704 struct audit_entry
*e
;
705 enum audit_state state
;
707 if (audit_pid
&& tsk
->tgid
== audit_pid
)
708 return AUDIT_DISABLED
;
711 for (i
= 0; i
< ctx
->name_count
; i
++) {
712 int word
= AUDIT_WORD(ctx
->major
);
713 int bit
= AUDIT_BIT(ctx
->major
);
714 struct audit_names
*n
= &ctx
->names
[i
];
715 int h
= audit_hash_ino((u32
)n
->ino
);
716 struct list_head
*list
= &audit_inode_hash
[h
];
718 if (list_empty(list
))
721 list_for_each_entry_rcu(e
, list
, list
) {
722 if ((e
->rule
.mask
[word
] & bit
) == bit
&&
723 audit_filter_rules(tsk
, &e
->rule
, ctx
, n
, &state
)) {
730 return AUDIT_BUILD_CONTEXT
;
733 void audit_set_auditable(struct audit_context
*ctx
)
738 static inline struct audit_context
*audit_get_context(struct task_struct
*tsk
,
742 struct audit_context
*context
= tsk
->audit_context
;
744 if (likely(!context
))
746 context
->return_valid
= return_valid
;
749 * we need to fix up the return code in the audit logs if the actual
750 * return codes are later going to be fixed up by the arch specific
753 * This is actually a test for:
754 * (rc == ERESTARTSYS ) || (rc == ERESTARTNOINTR) ||
755 * (rc == ERESTARTNOHAND) || (rc == ERESTART_RESTARTBLOCK)
757 * but is faster than a bunch of ||
759 if (unlikely(return_code
<= -ERESTARTSYS
) &&
760 (return_code
>= -ERESTART_RESTARTBLOCK
) &&
761 (return_code
!= -ENOIOCTLCMD
))
762 context
->return_code
= -EINTR
;
764 context
->return_code
= return_code
;
766 if (context
->in_syscall
&& !context
->dummy
&& !context
->auditable
) {
767 enum audit_state state
;
769 state
= audit_filter_syscall(tsk
, context
, &audit_filter_list
[AUDIT_FILTER_EXIT
]);
770 if (state
== AUDIT_RECORD_CONTEXT
) {
771 context
->auditable
= 1;
775 state
= audit_filter_inodes(tsk
, context
);
776 if (state
== AUDIT_RECORD_CONTEXT
)
777 context
->auditable
= 1;
783 tsk
->audit_context
= NULL
;
787 static inline void audit_free_names(struct audit_context
*context
)
792 if (context
->auditable
793 ||context
->put_count
+ context
->ino_count
!= context
->name_count
) {
794 printk(KERN_ERR
"%s:%d(:%d): major=%d in_syscall=%d"
795 " name_count=%d put_count=%d"
796 " ino_count=%d [NOT freeing]\n",
798 context
->serial
, context
->major
, context
->in_syscall
,
799 context
->name_count
, context
->put_count
,
801 for (i
= 0; i
< context
->name_count
; i
++) {
802 printk(KERN_ERR
"names[%d] = %p = %s\n", i
,
803 context
->names
[i
].name
,
804 context
->names
[i
].name
?: "(null)");
811 context
->put_count
= 0;
812 context
->ino_count
= 0;
815 for (i
= 0; i
< context
->name_count
; i
++) {
816 if (context
->names
[i
].name
&& context
->names
[i
].name_put
)
817 __putname(context
->names
[i
].name
);
819 context
->name_count
= 0;
820 path_put(&context
->pwd
);
821 context
->pwd
.dentry
= NULL
;
822 context
->pwd
.mnt
= NULL
;
825 static inline void audit_free_aux(struct audit_context
*context
)
827 struct audit_aux_data
*aux
;
829 while ((aux
= context
->aux
)) {
830 context
->aux
= aux
->next
;
833 while ((aux
= context
->aux_pids
)) {
834 context
->aux_pids
= aux
->next
;
839 static inline void audit_zero_context(struct audit_context
*context
,
840 enum audit_state state
)
842 memset(context
, 0, sizeof(*context
));
843 context
->state
= state
;
846 static inline struct audit_context
*audit_alloc_context(enum audit_state state
)
848 struct audit_context
*context
;
850 if (!(context
= kmalloc(sizeof(*context
), GFP_KERNEL
)))
852 audit_zero_context(context
, state
);
857 * audit_alloc - allocate an audit context block for a task
860 * Filter on the task information and allocate a per-task audit context
861 * if necessary. Doing so turns on system call auditing for the
862 * specified task. This is called from copy_process, so no lock is
865 int audit_alloc(struct task_struct
*tsk
)
867 struct audit_context
*context
;
868 enum audit_state state
;
870 if (likely(!audit_ever_enabled
))
871 return 0; /* Return if not auditing. */
873 state
= audit_filter_task(tsk
);
874 if (likely(state
== AUDIT_DISABLED
))
877 if (!(context
= audit_alloc_context(state
))) {
878 audit_log_lost("out of memory in audit_alloc");
882 tsk
->audit_context
= context
;
883 set_tsk_thread_flag(tsk
, TIF_SYSCALL_AUDIT
);
887 static inline void audit_free_context(struct audit_context
*context
)
889 struct audit_context
*previous
;
893 previous
= context
->previous
;
894 if (previous
|| (count
&& count
< 10)) {
896 printk(KERN_ERR
"audit(:%d): major=%d name_count=%d:"
897 " freeing multiple contexts (%d)\n",
898 context
->serial
, context
->major
,
899 context
->name_count
, count
);
901 audit_free_names(context
);
902 unroll_tree_refs(context
, NULL
, 0);
903 free_tree_refs(context
);
904 audit_free_aux(context
);
905 kfree(context
->filterkey
);
910 printk(KERN_ERR
"audit: freed %d contexts\n", count
);
913 void audit_log_task_context(struct audit_buffer
*ab
)
920 security_task_getsecid(current
, &sid
);
924 error
= security_secid_to_secctx(sid
, &ctx
, &len
);
926 if (error
!= -EINVAL
)
931 audit_log_format(ab
, " subj=%s", ctx
);
932 security_release_secctx(ctx
, len
);
936 audit_panic("error in audit_log_task_context");
940 EXPORT_SYMBOL(audit_log_task_context
);
942 static void audit_log_task_info(struct audit_buffer
*ab
, struct task_struct
*tsk
)
944 char name
[sizeof(tsk
->comm
)];
945 struct mm_struct
*mm
= tsk
->mm
;
946 struct vm_area_struct
*vma
;
950 get_task_comm(name
, tsk
);
951 audit_log_format(ab
, " comm=");
952 audit_log_untrustedstring(ab
, name
);
955 down_read(&mm
->mmap_sem
);
958 if ((vma
->vm_flags
& VM_EXECUTABLE
) &&
960 audit_log_d_path(ab
, "exe=",
961 &vma
->vm_file
->f_path
);
966 up_read(&mm
->mmap_sem
);
968 audit_log_task_context(ab
);
971 static int audit_log_pid_context(struct audit_context
*context
, pid_t pid
,
972 uid_t auid
, uid_t uid
, unsigned int sessionid
,
975 struct audit_buffer
*ab
;
980 ab
= audit_log_start(context
, GFP_KERNEL
, AUDIT_OBJ_PID
);
984 audit_log_format(ab
, "opid=%d oauid=%d ouid=%d oses=%d", pid
, auid
,
986 if (security_secid_to_secctx(sid
, &ctx
, &len
)) {
987 audit_log_format(ab
, " obj=(none)");
990 audit_log_format(ab
, " obj=%s", ctx
);
991 security_release_secctx(ctx
, len
);
993 audit_log_format(ab
, " ocomm=");
994 audit_log_untrustedstring(ab
, comm
);
1001 * to_send and len_sent accounting are very loose estimates. We aren't
1002 * really worried about a hard cap to MAX_EXECVE_AUDIT_LEN so much as being
1003 * within about 500 bytes (next page boundry)
1005 * why snprintf? an int is up to 12 digits long. if we just assumed when
1006 * logging that a[%d]= was going to be 16 characters long we would be wasting
1007 * space in every audit message. In one 7500 byte message we can log up to
1008 * about 1000 min size arguments. That comes down to about 50% waste of space
1009 * if we didn't do the snprintf to find out how long arg_num_len was.
1011 static int audit_log_single_execve_arg(struct audit_context
*context
,
1012 struct audit_buffer
**ab
,
1015 const char __user
*p
,
1018 char arg_num_len_buf
[12];
1019 const char __user
*tmp_p
= p
;
1020 /* how many digits are in arg_num? 3 is the length of a=\n */
1021 size_t arg_num_len
= snprintf(arg_num_len_buf
, 12, "%d", arg_num
) + 3;
1022 size_t len
, len_left
, to_send
;
1023 size_t max_execve_audit_len
= MAX_EXECVE_AUDIT_LEN
;
1024 unsigned int i
, has_cntl
= 0, too_long
= 0;
1027 /* strnlen_user includes the null we don't want to send */
1028 len_left
= len
= strnlen_user(p
, MAX_ARG_STRLEN
) - 1;
1031 * We just created this mm, if we can't find the strings
1032 * we just copied into it something is _very_ wrong. Similar
1033 * for strings that are too long, we should not have created
1036 if (unlikely((len
== -1) || len
> MAX_ARG_STRLEN
- 1)) {
1038 send_sig(SIGKILL
, current
, 0);
1042 /* walk the whole argument looking for non-ascii chars */
1044 if (len_left
> MAX_EXECVE_AUDIT_LEN
)
1045 to_send
= MAX_EXECVE_AUDIT_LEN
;
1048 ret
= copy_from_user(buf
, tmp_p
, to_send
);
1050 * There is no reason for this copy to be short. We just
1051 * copied them here, and the mm hasn't been exposed to user-
1056 send_sig(SIGKILL
, current
, 0);
1059 buf
[to_send
] = '\0';
1060 has_cntl
= audit_string_contains_control(buf
, to_send
);
1063 * hex messages get logged as 2 bytes, so we can only
1064 * send half as much in each message
1066 max_execve_audit_len
= MAX_EXECVE_AUDIT_LEN
/ 2;
1069 len_left
-= to_send
;
1071 } while (len_left
> 0);
1075 if (len
> max_execve_audit_len
)
1078 /* rewalk the argument actually logging the message */
1079 for (i
= 0; len_left
> 0; i
++) {
1082 if (len_left
> max_execve_audit_len
)
1083 to_send
= max_execve_audit_len
;
1087 /* do we have space left to send this argument in this ab? */
1088 room_left
= MAX_EXECVE_AUDIT_LEN
- arg_num_len
- *len_sent
;
1090 room_left
-= (to_send
* 2);
1092 room_left
-= to_send
;
1093 if (room_left
< 0) {
1096 *ab
= audit_log_start(context
, GFP_KERNEL
, AUDIT_EXECVE
);
1102 * first record needs to say how long the original string was
1103 * so we can be sure nothing was lost.
1105 if ((i
== 0) && (too_long
))
1106 audit_log_format(*ab
, "a%d_len=%zu ", arg_num
,
1107 has_cntl
? 2*len
: len
);
1110 * normally arguments are small enough to fit and we already
1111 * filled buf above when we checked for control characters
1112 * so don't bother with another copy_from_user
1114 if (len
>= max_execve_audit_len
)
1115 ret
= copy_from_user(buf
, p
, to_send
);
1120 send_sig(SIGKILL
, current
, 0);
1123 buf
[to_send
] = '\0';
1125 /* actually log it */
1126 audit_log_format(*ab
, "a%d", arg_num
);
1128 audit_log_format(*ab
, "[%d]", i
);
1129 audit_log_format(*ab
, "=");
1131 audit_log_n_hex(*ab
, buf
, to_send
);
1133 audit_log_format(*ab
, "\"%s\"", buf
);
1134 audit_log_format(*ab
, "\n");
1137 len_left
-= to_send
;
1138 *len_sent
+= arg_num_len
;
1140 *len_sent
+= to_send
* 2;
1142 *len_sent
+= to_send
;
1144 /* include the null we didn't log */
1148 static void audit_log_execve_info(struct audit_context
*context
,
1149 struct audit_buffer
**ab
,
1150 struct audit_aux_data_execve
*axi
)
1153 size_t len
, len_sent
= 0;
1154 const char __user
*p
;
1157 if (axi
->mm
!= current
->mm
)
1158 return; /* execve failed, no additional info */
1160 p
= (const char __user
*)axi
->mm
->arg_start
;
1162 audit_log_format(*ab
, "argc=%d ", axi
->argc
);
1165 * we need some kernel buffer to hold the userspace args. Just
1166 * allocate one big one rather than allocating one of the right size
1167 * for every single argument inside audit_log_single_execve_arg()
1168 * should be <8k allocation so should be pretty safe.
1170 buf
= kmalloc(MAX_EXECVE_AUDIT_LEN
+ 1, GFP_KERNEL
);
1172 audit_panic("out of memory for argv string\n");
1176 for (i
= 0; i
< axi
->argc
; i
++) {
1177 len
= audit_log_single_execve_arg(context
, ab
, i
,
1186 static void audit_log_cap(struct audit_buffer
*ab
, char *prefix
, kernel_cap_t
*cap
)
1190 audit_log_format(ab
, " %s=", prefix
);
1191 CAP_FOR_EACH_U32(i
) {
1192 audit_log_format(ab
, "%08x", cap
->cap
[(_KERNEL_CAPABILITY_U32S
-1) - i
]);
1196 static void audit_log_fcaps(struct audit_buffer
*ab
, struct audit_names
*name
)
1198 kernel_cap_t
*perm
= &name
->fcap
.permitted
;
1199 kernel_cap_t
*inh
= &name
->fcap
.inheritable
;
1202 if (!cap_isclear(*perm
)) {
1203 audit_log_cap(ab
, "cap_fp", perm
);
1206 if (!cap_isclear(*inh
)) {
1207 audit_log_cap(ab
, "cap_fi", inh
);
1212 audit_log_format(ab
, " cap_fe=%d cap_fver=%x", name
->fcap
.fE
, name
->fcap_ver
);
1215 static void audit_log_exit(struct audit_context
*context
, struct task_struct
*tsk
)
1217 int i
, call_panic
= 0;
1218 struct audit_buffer
*ab
;
1219 struct audit_aux_data
*aux
;
1222 /* tsk == current */
1223 context
->pid
= tsk
->pid
;
1225 context
->ppid
= sys_getppid();
1226 context
->uid
= tsk
->uid
;
1227 context
->gid
= tsk
->gid
;
1228 context
->euid
= tsk
->euid
;
1229 context
->suid
= tsk
->suid
;
1230 context
->fsuid
= tsk
->fsuid
;
1231 context
->egid
= tsk
->egid
;
1232 context
->sgid
= tsk
->sgid
;
1233 context
->fsgid
= tsk
->fsgid
;
1234 context
->personality
= tsk
->personality
;
1236 ab
= audit_log_start(context
, GFP_KERNEL
, AUDIT_SYSCALL
);
1238 return; /* audit_panic has been called */
1239 audit_log_format(ab
, "arch=%x syscall=%d",
1240 context
->arch
, context
->major
);
1241 if (context
->personality
!= PER_LINUX
)
1242 audit_log_format(ab
, " per=%lx", context
->personality
);
1243 if (context
->return_valid
)
1244 audit_log_format(ab
, " success=%s exit=%ld",
1245 (context
->return_valid
==AUDITSC_SUCCESS
)?"yes":"no",
1246 context
->return_code
);
1248 spin_lock_irq(&tsk
->sighand
->siglock
);
1249 if (tsk
->signal
&& tsk
->signal
->tty
&& tsk
->signal
->tty
->name
)
1250 tty
= tsk
->signal
->tty
->name
;
1253 spin_unlock_irq(&tsk
->sighand
->siglock
);
1255 audit_log_format(ab
,
1256 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
1257 " ppid=%d pid=%d auid=%u uid=%u gid=%u"
1258 " euid=%u suid=%u fsuid=%u"
1259 " egid=%u sgid=%u fsgid=%u tty=%s ses=%u",
1264 context
->name_count
,
1270 context
->euid
, context
->suid
, context
->fsuid
,
1271 context
->egid
, context
->sgid
, context
->fsgid
, tty
,
1275 audit_log_task_info(ab
, tsk
);
1276 if (context
->filterkey
) {
1277 audit_log_format(ab
, " key=");
1278 audit_log_untrustedstring(ab
, context
->filterkey
);
1280 audit_log_format(ab
, " key=(null)");
1283 for (aux
= context
->aux
; aux
; aux
= aux
->next
) {
1285 ab
= audit_log_start(context
, GFP_KERNEL
, aux
->type
);
1287 continue; /* audit_panic has been called */
1289 switch (aux
->type
) {
1290 case AUDIT_MQ_OPEN
: {
1291 struct audit_aux_data_mq_open
*axi
= (void *)aux
;
1292 audit_log_format(ab
,
1293 "oflag=0x%x mode=%#o mq_flags=0x%lx mq_maxmsg=%ld "
1294 "mq_msgsize=%ld mq_curmsgs=%ld",
1295 axi
->oflag
, axi
->mode
, axi
->attr
.mq_flags
,
1296 axi
->attr
.mq_maxmsg
, axi
->attr
.mq_msgsize
,
1297 axi
->attr
.mq_curmsgs
);
1300 case AUDIT_MQ_SENDRECV
: {
1301 struct audit_aux_data_mq_sendrecv
*axi
= (void *)aux
;
1302 audit_log_format(ab
,
1303 "mqdes=%d msg_len=%zd msg_prio=%u "
1304 "abs_timeout_sec=%ld abs_timeout_nsec=%ld",
1305 axi
->mqdes
, axi
->msg_len
, axi
->msg_prio
,
1306 axi
->abs_timeout
.tv_sec
, axi
->abs_timeout
.tv_nsec
);
1309 case AUDIT_MQ_NOTIFY
: {
1310 struct audit_aux_data_mq_notify
*axi
= (void *)aux
;
1311 audit_log_format(ab
,
1312 "mqdes=%d sigev_signo=%d",
1314 axi
->notification
.sigev_signo
);
1317 case AUDIT_MQ_GETSETATTR
: {
1318 struct audit_aux_data_mq_getsetattr
*axi
= (void *)aux
;
1319 audit_log_format(ab
,
1320 "mqdes=%d mq_flags=0x%lx mq_maxmsg=%ld mq_msgsize=%ld "
1323 axi
->mqstat
.mq_flags
, axi
->mqstat
.mq_maxmsg
,
1324 axi
->mqstat
.mq_msgsize
, axi
->mqstat
.mq_curmsgs
);
1328 struct audit_aux_data_ipcctl
*axi
= (void *)aux
;
1329 audit_log_format(ab
,
1330 "ouid=%u ogid=%u mode=%#o",
1331 axi
->uid
, axi
->gid
, axi
->mode
);
1332 if (axi
->osid
!= 0) {
1335 if (security_secid_to_secctx(
1336 axi
->osid
, &ctx
, &len
)) {
1337 audit_log_format(ab
, " osid=%u",
1341 audit_log_format(ab
, " obj=%s", ctx
);
1342 security_release_secctx(ctx
, len
);
1347 case AUDIT_IPC_SET_PERM
: {
1348 struct audit_aux_data_ipcctl
*axi
= (void *)aux
;
1349 audit_log_format(ab
,
1350 "qbytes=%lx ouid=%u ogid=%u mode=%#o",
1351 axi
->qbytes
, axi
->uid
, axi
->gid
, axi
->mode
);
1354 case AUDIT_EXECVE
: {
1355 struct audit_aux_data_execve
*axi
= (void *)aux
;
1356 audit_log_execve_info(context
, &ab
, axi
);
1359 case AUDIT_SOCKETCALL
: {
1360 struct audit_aux_data_socketcall
*axs
= (void *)aux
;
1361 audit_log_format(ab
, "nargs=%d", axs
->nargs
);
1362 for (i
=0; i
<axs
->nargs
; i
++)
1363 audit_log_format(ab
, " a%d=%lx", i
, axs
->args
[i
]);
1366 case AUDIT_SOCKADDR
: {
1367 struct audit_aux_data_sockaddr
*axs
= (void *)aux
;
1369 audit_log_format(ab
, "saddr=");
1370 audit_log_n_hex(ab
, axs
->a
, axs
->len
);
1373 case AUDIT_FD_PAIR
: {
1374 struct audit_aux_data_fd_pair
*axs
= (void *)aux
;
1375 audit_log_format(ab
, "fd0=%d fd1=%d", axs
->fd
[0], axs
->fd
[1]);
1382 for (aux
= context
->aux_pids
; aux
; aux
= aux
->next
) {
1383 struct audit_aux_data_pids
*axs
= (void *)aux
;
1385 for (i
= 0; i
< axs
->pid_count
; i
++)
1386 if (audit_log_pid_context(context
, axs
->target_pid
[i
],
1387 axs
->target_auid
[i
],
1389 axs
->target_sessionid
[i
],
1391 axs
->target_comm
[i
]))
1395 if (context
->target_pid
&&
1396 audit_log_pid_context(context
, context
->target_pid
,
1397 context
->target_auid
, context
->target_uid
,
1398 context
->target_sessionid
,
1399 context
->target_sid
, context
->target_comm
))
1402 if (context
->pwd
.dentry
&& context
->pwd
.mnt
) {
1403 ab
= audit_log_start(context
, GFP_KERNEL
, AUDIT_CWD
);
1405 audit_log_d_path(ab
, "cwd=", &context
->pwd
);
1409 for (i
= 0; i
< context
->name_count
; i
++) {
1410 struct audit_names
*n
= &context
->names
[i
];
1412 ab
= audit_log_start(context
, GFP_KERNEL
, AUDIT_PATH
);
1414 continue; /* audit_panic has been called */
1416 audit_log_format(ab
, "item=%d", i
);
1419 switch(n
->name_len
) {
1420 case AUDIT_NAME_FULL
:
1421 /* log the full path */
1422 audit_log_format(ab
, " name=");
1423 audit_log_untrustedstring(ab
, n
->name
);
1426 /* name was specified as a relative path and the
1427 * directory component is the cwd */
1428 audit_log_d_path(ab
, " name=", &context
->pwd
);
1431 /* log the name's directory component */
1432 audit_log_format(ab
, " name=");
1433 audit_log_n_untrustedstring(ab
, n
->name
,
1437 audit_log_format(ab
, " name=(null)");
1439 if (n
->ino
!= (unsigned long)-1) {
1440 audit_log_format(ab
, " inode=%lu"
1441 " dev=%02x:%02x mode=%#o"
1442 " ouid=%u ogid=%u rdev=%02x:%02x",
1455 if (security_secid_to_secctx(
1456 n
->osid
, &ctx
, &len
)) {
1457 audit_log_format(ab
, " osid=%u", n
->osid
);
1460 audit_log_format(ab
, " obj=%s", ctx
);
1461 security_release_secctx(ctx
, len
);
1465 audit_log_fcaps(ab
, n
);
1470 /* Send end of event record to help user space know we are finished */
1471 ab
= audit_log_start(context
, GFP_KERNEL
, AUDIT_EOE
);
1475 audit_panic("error converting sid to string");
1479 * audit_free - free a per-task audit context
1480 * @tsk: task whose audit context block to free
1482 * Called from copy_process and do_exit
1484 void audit_free(struct task_struct
*tsk
)
1486 struct audit_context
*context
;
1488 context
= audit_get_context(tsk
, 0, 0);
1489 if (likely(!context
))
1492 /* Check for system calls that do not go through the exit
1493 * function (e.g., exit_group), then free context block.
1494 * We use GFP_ATOMIC here because we might be doing this
1495 * in the context of the idle thread */
1496 /* that can happen only if we are called from do_exit() */
1497 if (context
->in_syscall
&& context
->auditable
)
1498 audit_log_exit(context
, tsk
);
1500 audit_free_context(context
);
1504 * audit_syscall_entry - fill in an audit record at syscall entry
1505 * @tsk: task being audited
1506 * @arch: architecture type
1507 * @major: major syscall type (function)
1508 * @a1: additional syscall register 1
1509 * @a2: additional syscall register 2
1510 * @a3: additional syscall register 3
1511 * @a4: additional syscall register 4
1513 * Fill in audit context at syscall entry. This only happens if the
1514 * audit context was created when the task was created and the state or
1515 * filters demand the audit context be built. If the state from the
1516 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
1517 * then the record will be written at syscall exit time (otherwise, it
1518 * will only be written if another part of the kernel requests that it
1521 void audit_syscall_entry(int arch
, int major
,
1522 unsigned long a1
, unsigned long a2
,
1523 unsigned long a3
, unsigned long a4
)
1525 struct task_struct
*tsk
= current
;
1526 struct audit_context
*context
= tsk
->audit_context
;
1527 enum audit_state state
;
1529 if (unlikely(!context
))
1533 * This happens only on certain architectures that make system
1534 * calls in kernel_thread via the entry.S interface, instead of
1535 * with direct calls. (If you are porting to a new
1536 * architecture, hitting this condition can indicate that you
1537 * got the _exit/_leave calls backward in entry.S.)
1541 * ppc64 yes (see arch/powerpc/platforms/iseries/misc.S)
1543 * This also happens with vm86 emulation in a non-nested manner
1544 * (entries without exits), so this case must be caught.
1546 if (context
->in_syscall
) {
1547 struct audit_context
*newctx
;
1551 "audit(:%d) pid=%d in syscall=%d;"
1552 " entering syscall=%d\n",
1553 context
->serial
, tsk
->pid
, context
->major
, major
);
1555 newctx
= audit_alloc_context(context
->state
);
1557 newctx
->previous
= context
;
1559 tsk
->audit_context
= newctx
;
1561 /* If we can't alloc a new context, the best we
1562 * can do is to leak memory (any pending putname
1563 * will be lost). The only other alternative is
1564 * to abandon auditing. */
1565 audit_zero_context(context
, context
->state
);
1568 BUG_ON(context
->in_syscall
|| context
->name_count
);
1573 context
->arch
= arch
;
1574 context
->major
= major
;
1575 context
->argv
[0] = a1
;
1576 context
->argv
[1] = a2
;
1577 context
->argv
[2] = a3
;
1578 context
->argv
[3] = a4
;
1580 state
= context
->state
;
1581 context
->dummy
= !audit_n_rules
;
1582 if (!context
->dummy
&& (state
== AUDIT_SETUP_CONTEXT
|| state
== AUDIT_BUILD_CONTEXT
))
1583 state
= audit_filter_syscall(tsk
, context
, &audit_filter_list
[AUDIT_FILTER_ENTRY
]);
1584 if (likely(state
== AUDIT_DISABLED
))
1587 context
->serial
= 0;
1588 context
->ctime
= CURRENT_TIME
;
1589 context
->in_syscall
= 1;
1590 context
->auditable
= !!(state
== AUDIT_RECORD_CONTEXT
);
1595 * audit_syscall_exit - deallocate audit context after a system call
1596 * @tsk: task being audited
1597 * @valid: success/failure flag
1598 * @return_code: syscall return value
1600 * Tear down after system call. If the audit context has been marked as
1601 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
1602 * filtering, or because some other part of the kernel write an audit
1603 * message), then write out the syscall information. In call cases,
1604 * free the names stored from getname().
1606 void audit_syscall_exit(int valid
, long return_code
)
1608 struct task_struct
*tsk
= current
;
1609 struct audit_context
*context
;
1611 context
= audit_get_context(tsk
, valid
, return_code
);
1613 if (likely(!context
))
1616 if (context
->in_syscall
&& context
->auditable
)
1617 audit_log_exit(context
, tsk
);
1619 context
->in_syscall
= 0;
1620 context
->auditable
= 0;
1622 if (context
->previous
) {
1623 struct audit_context
*new_context
= context
->previous
;
1624 context
->previous
= NULL
;
1625 audit_free_context(context
);
1626 tsk
->audit_context
= new_context
;
1628 audit_free_names(context
);
1629 unroll_tree_refs(context
, NULL
, 0);
1630 audit_free_aux(context
);
1631 context
->aux
= NULL
;
1632 context
->aux_pids
= NULL
;
1633 context
->target_pid
= 0;
1634 context
->target_sid
= 0;
1635 kfree(context
->filterkey
);
1636 context
->filterkey
= NULL
;
1637 tsk
->audit_context
= context
;
1641 static inline void handle_one(const struct inode
*inode
)
1643 #ifdef CONFIG_AUDIT_TREE
1644 struct audit_context
*context
;
1645 struct audit_tree_refs
*p
;
1646 struct audit_chunk
*chunk
;
1648 if (likely(list_empty(&inode
->inotify_watches
)))
1650 context
= current
->audit_context
;
1652 count
= context
->tree_count
;
1654 chunk
= audit_tree_lookup(inode
);
1658 if (likely(put_tree_ref(context
, chunk
)))
1660 if (unlikely(!grow_tree_refs(context
))) {
1661 printk(KERN_WARNING
"out of memory, audit has lost a tree reference\n");
1662 audit_set_auditable(context
);
1663 audit_put_chunk(chunk
);
1664 unroll_tree_refs(context
, p
, count
);
1667 put_tree_ref(context
, chunk
);
1671 static void handle_path(const struct dentry
*dentry
)
1673 #ifdef CONFIG_AUDIT_TREE
1674 struct audit_context
*context
;
1675 struct audit_tree_refs
*p
;
1676 const struct dentry
*d
, *parent
;
1677 struct audit_chunk
*drop
;
1681 context
= current
->audit_context
;
1683 count
= context
->tree_count
;
1688 seq
= read_seqbegin(&rename_lock
);
1690 struct inode
*inode
= d
->d_inode
;
1691 if (inode
&& unlikely(!list_empty(&inode
->inotify_watches
))) {
1692 struct audit_chunk
*chunk
;
1693 chunk
= audit_tree_lookup(inode
);
1695 if (unlikely(!put_tree_ref(context
, chunk
))) {
1701 parent
= d
->d_parent
;
1706 if (unlikely(read_seqretry(&rename_lock
, seq
) || drop
)) { /* in this order */
1709 /* just a race with rename */
1710 unroll_tree_refs(context
, p
, count
);
1713 audit_put_chunk(drop
);
1714 if (grow_tree_refs(context
)) {
1715 /* OK, got more space */
1716 unroll_tree_refs(context
, p
, count
);
1721 "out of memory, audit has lost a tree reference\n");
1722 unroll_tree_refs(context
, p
, count
);
1723 audit_set_auditable(context
);
1731 * audit_getname - add a name to the list
1732 * @name: name to add
1734 * Add a name to the list of audit names for this context.
1735 * Called from fs/namei.c:getname().
1737 void __audit_getname(const char *name
)
1739 struct audit_context
*context
= current
->audit_context
;
1741 if (IS_ERR(name
) || !name
)
1744 if (!context
->in_syscall
) {
1745 #if AUDIT_DEBUG == 2
1746 printk(KERN_ERR
"%s:%d(:%d): ignoring getname(%p)\n",
1747 __FILE__
, __LINE__
, context
->serial
, name
);
1752 BUG_ON(context
->name_count
>= AUDIT_NAMES
);
1753 context
->names
[context
->name_count
].name
= name
;
1754 context
->names
[context
->name_count
].name_len
= AUDIT_NAME_FULL
;
1755 context
->names
[context
->name_count
].name_put
= 1;
1756 context
->names
[context
->name_count
].ino
= (unsigned long)-1;
1757 context
->names
[context
->name_count
].osid
= 0;
1758 ++context
->name_count
;
1759 if (!context
->pwd
.dentry
) {
1760 read_lock(¤t
->fs
->lock
);
1761 context
->pwd
= current
->fs
->pwd
;
1762 path_get(¤t
->fs
->pwd
);
1763 read_unlock(¤t
->fs
->lock
);
1768 /* audit_putname - intercept a putname request
1769 * @name: name to intercept and delay for putname
1771 * If we have stored the name from getname in the audit context,
1772 * then we delay the putname until syscall exit.
1773 * Called from include/linux/fs.h:putname().
1775 void audit_putname(const char *name
)
1777 struct audit_context
*context
= current
->audit_context
;
1780 if (!context
->in_syscall
) {
1781 #if AUDIT_DEBUG == 2
1782 printk(KERN_ERR
"%s:%d(:%d): __putname(%p)\n",
1783 __FILE__
, __LINE__
, context
->serial
, name
);
1784 if (context
->name_count
) {
1786 for (i
= 0; i
< context
->name_count
; i
++)
1787 printk(KERN_ERR
"name[%d] = %p = %s\n", i
,
1788 context
->names
[i
].name
,
1789 context
->names
[i
].name
?: "(null)");
1796 ++context
->put_count
;
1797 if (context
->put_count
> context
->name_count
) {
1798 printk(KERN_ERR
"%s:%d(:%d): major=%d"
1799 " in_syscall=%d putname(%p) name_count=%d"
1802 context
->serial
, context
->major
,
1803 context
->in_syscall
, name
, context
->name_count
,
1804 context
->put_count
);
1811 static int audit_inc_name_count(struct audit_context
*context
,
1812 const struct inode
*inode
)
1814 if (context
->name_count
>= AUDIT_NAMES
) {
1816 printk(KERN_DEBUG
"name_count maxed, losing inode data: "
1817 "dev=%02x:%02x, inode=%lu\n",
1818 MAJOR(inode
->i_sb
->s_dev
),
1819 MINOR(inode
->i_sb
->s_dev
),
1823 printk(KERN_DEBUG
"name_count maxed, losing inode data\n");
1826 context
->name_count
++;
1828 context
->ino_count
++;
1834 static inline int audit_copy_fcaps(struct audit_names
*name
, const struct dentry
*dentry
)
1836 struct cpu_vfs_cap_data caps
;
1839 memset(&name
->fcap
.permitted
, 0, sizeof(kernel_cap_t
));
1840 memset(&name
->fcap
.inheritable
, 0, sizeof(kernel_cap_t
));
1847 rc
= get_vfs_caps_from_disk(dentry
, &caps
);
1851 name
->fcap
.permitted
= caps
.permitted
;
1852 name
->fcap
.inheritable
= caps
.inheritable
;
1853 name
->fcap
.fE
= !!(caps
.magic_etc
& VFS_CAP_FLAGS_EFFECTIVE
);
1854 name
->fcap_ver
= (caps
.magic_etc
& VFS_CAP_REVISION_MASK
) >> VFS_CAP_REVISION_SHIFT
;
1860 /* Copy inode data into an audit_names. */
1861 static void audit_copy_inode(struct audit_names
*name
, const struct dentry
*dentry
,
1862 const struct inode
*inode
)
1864 name
->ino
= inode
->i_ino
;
1865 name
->dev
= inode
->i_sb
->s_dev
;
1866 name
->mode
= inode
->i_mode
;
1867 name
->uid
= inode
->i_uid
;
1868 name
->gid
= inode
->i_gid
;
1869 name
->rdev
= inode
->i_rdev
;
1870 security_inode_getsecid(inode
, &name
->osid
);
1871 audit_copy_fcaps(name
, dentry
);
1875 * audit_inode - store the inode and device from a lookup
1876 * @name: name being audited
1877 * @dentry: dentry being audited
1879 * Called from fs/namei.c:path_lookup().
1881 void __audit_inode(const char *name
, const struct dentry
*dentry
)
1884 struct audit_context
*context
= current
->audit_context
;
1885 const struct inode
*inode
= dentry
->d_inode
;
1887 if (!context
->in_syscall
)
1889 if (context
->name_count
1890 && context
->names
[context
->name_count
-1].name
1891 && context
->names
[context
->name_count
-1].name
== name
)
1892 idx
= context
->name_count
- 1;
1893 else if (context
->name_count
> 1
1894 && context
->names
[context
->name_count
-2].name
1895 && context
->names
[context
->name_count
-2].name
== name
)
1896 idx
= context
->name_count
- 2;
1898 /* FIXME: how much do we care about inodes that have no
1899 * associated name? */
1900 if (audit_inc_name_count(context
, inode
))
1902 idx
= context
->name_count
- 1;
1903 context
->names
[idx
].name
= NULL
;
1905 handle_path(dentry
);
1906 audit_copy_inode(&context
->names
[idx
], dentry
, inode
);
1910 * audit_inode_child - collect inode info for created/removed objects
1911 * @dname: inode's dentry name
1912 * @dentry: dentry being audited
1913 * @parent: inode of dentry parent
1915 * For syscalls that create or remove filesystem objects, audit_inode
1916 * can only collect information for the filesystem object's parent.
1917 * This call updates the audit context with the child's information.
1918 * Syscalls that create a new filesystem object must be hooked after
1919 * the object is created. Syscalls that remove a filesystem object
1920 * must be hooked prior, in order to capture the target inode during
1921 * unsuccessful attempts.
1923 void __audit_inode_child(const char *dname
, const struct dentry
*dentry
,
1924 const struct inode
*parent
)
1927 struct audit_context
*context
= current
->audit_context
;
1928 const char *found_parent
= NULL
, *found_child
= NULL
;
1929 const struct inode
*inode
= dentry
->d_inode
;
1932 if (!context
->in_syscall
)
1937 /* determine matching parent */
1941 /* parent is more likely, look for it first */
1942 for (idx
= 0; idx
< context
->name_count
; idx
++) {
1943 struct audit_names
*n
= &context
->names
[idx
];
1948 if (n
->ino
== parent
->i_ino
&&
1949 !audit_compare_dname_path(dname
, n
->name
, &dirlen
)) {
1950 n
->name_len
= dirlen
; /* update parent data in place */
1951 found_parent
= n
->name
;
1956 /* no matching parent, look for matching child */
1957 for (idx
= 0; idx
< context
->name_count
; idx
++) {
1958 struct audit_names
*n
= &context
->names
[idx
];
1963 /* strcmp() is the more likely scenario */
1964 if (!strcmp(dname
, n
->name
) ||
1965 !audit_compare_dname_path(dname
, n
->name
, &dirlen
)) {
1967 audit_copy_inode(n
, NULL
, inode
);
1969 n
->ino
= (unsigned long)-1;
1970 found_child
= n
->name
;
1976 if (!found_parent
) {
1977 if (audit_inc_name_count(context
, parent
))
1979 idx
= context
->name_count
- 1;
1980 context
->names
[idx
].name
= NULL
;
1981 audit_copy_inode(&context
->names
[idx
], NULL
, parent
);
1985 if (audit_inc_name_count(context
, inode
))
1987 idx
= context
->name_count
- 1;
1989 /* Re-use the name belonging to the slot for a matching parent
1990 * directory. All names for this context are relinquished in
1991 * audit_free_names() */
1993 context
->names
[idx
].name
= found_parent
;
1994 context
->names
[idx
].name_len
= AUDIT_NAME_FULL
;
1995 /* don't call __putname() */
1996 context
->names
[idx
].name_put
= 0;
1998 context
->names
[idx
].name
= NULL
;
2002 audit_copy_inode(&context
->names
[idx
], NULL
, inode
);
2004 context
->names
[idx
].ino
= (unsigned long)-1;
2007 EXPORT_SYMBOL_GPL(__audit_inode_child
);
2010 * auditsc_get_stamp - get local copies of audit_context values
2011 * @ctx: audit_context for the task
2012 * @t: timespec to store time recorded in the audit_context
2013 * @serial: serial value that is recorded in the audit_context
2015 * Also sets the context as auditable.
2017 void auditsc_get_stamp(struct audit_context
*ctx
,
2018 struct timespec
*t
, unsigned int *serial
)
2021 ctx
->serial
= audit_serial();
2022 t
->tv_sec
= ctx
->ctime
.tv_sec
;
2023 t
->tv_nsec
= ctx
->ctime
.tv_nsec
;
2024 *serial
= ctx
->serial
;
2028 /* global counter which is incremented every time something logs in */
2029 static atomic_t session_id
= ATOMIC_INIT(0);
2032 * audit_set_loginuid - set a task's audit_context loginuid
2033 * @task: task whose audit context is being modified
2034 * @loginuid: loginuid value
2038 * Called (set) from fs/proc/base.c::proc_loginuid_write().
2040 int audit_set_loginuid(struct task_struct
*task
, uid_t loginuid
)
2042 unsigned int sessionid
= atomic_inc_return(&session_id
);
2043 struct audit_context
*context
= task
->audit_context
;
2045 if (context
&& context
->in_syscall
) {
2046 struct audit_buffer
*ab
;
2048 ab
= audit_log_start(NULL
, GFP_KERNEL
, AUDIT_LOGIN
);
2050 audit_log_format(ab
, "login pid=%d uid=%u "
2051 "old auid=%u new auid=%u"
2052 " old ses=%u new ses=%u",
2053 task
->pid
, task
->uid
,
2054 task
->loginuid
, loginuid
,
2055 task
->sessionid
, sessionid
);
2059 task
->sessionid
= sessionid
;
2060 task
->loginuid
= loginuid
;
2065 * __audit_mq_open - record audit data for a POSIX MQ open
2068 * @u_attr: queue attributes
2070 * Returns 0 for success or NULL context or < 0 on error.
2072 int __audit_mq_open(int oflag
, mode_t mode
, struct mq_attr __user
*u_attr
)
2074 struct audit_aux_data_mq_open
*ax
;
2075 struct audit_context
*context
= current
->audit_context
;
2080 if (likely(!context
))
2083 ax
= kmalloc(sizeof(*ax
), GFP_ATOMIC
);
2087 if (u_attr
!= NULL
) {
2088 if (copy_from_user(&ax
->attr
, u_attr
, sizeof(ax
->attr
))) {
2093 memset(&ax
->attr
, 0, sizeof(ax
->attr
));
2098 ax
->d
.type
= AUDIT_MQ_OPEN
;
2099 ax
->d
.next
= context
->aux
;
2100 context
->aux
= (void *)ax
;
2105 * __audit_mq_timedsend - record audit data for a POSIX MQ timed send
2106 * @mqdes: MQ descriptor
2107 * @msg_len: Message length
2108 * @msg_prio: Message priority
2109 * @u_abs_timeout: Message timeout in absolute time
2111 * Returns 0 for success or NULL context or < 0 on error.
2113 int __audit_mq_timedsend(mqd_t mqdes
, size_t msg_len
, unsigned int msg_prio
,
2114 const struct timespec __user
*u_abs_timeout
)
2116 struct audit_aux_data_mq_sendrecv
*ax
;
2117 struct audit_context
*context
= current
->audit_context
;
2122 if (likely(!context
))
2125 ax
= kmalloc(sizeof(*ax
), GFP_ATOMIC
);
2129 if (u_abs_timeout
!= NULL
) {
2130 if (copy_from_user(&ax
->abs_timeout
, u_abs_timeout
, sizeof(ax
->abs_timeout
))) {
2135 memset(&ax
->abs_timeout
, 0, sizeof(ax
->abs_timeout
));
2138 ax
->msg_len
= msg_len
;
2139 ax
->msg_prio
= msg_prio
;
2141 ax
->d
.type
= AUDIT_MQ_SENDRECV
;
2142 ax
->d
.next
= context
->aux
;
2143 context
->aux
= (void *)ax
;
2148 * __audit_mq_timedreceive - record audit data for a POSIX MQ timed receive
2149 * @mqdes: MQ descriptor
2150 * @msg_len: Message length
2151 * @u_msg_prio: Message priority
2152 * @u_abs_timeout: Message timeout in absolute time
2154 * Returns 0 for success or NULL context or < 0 on error.
2156 int __audit_mq_timedreceive(mqd_t mqdes
, size_t msg_len
,
2157 unsigned int __user
*u_msg_prio
,
2158 const struct timespec __user
*u_abs_timeout
)
2160 struct audit_aux_data_mq_sendrecv
*ax
;
2161 struct audit_context
*context
= current
->audit_context
;
2166 if (likely(!context
))
2169 ax
= kmalloc(sizeof(*ax
), GFP_ATOMIC
);
2173 if (u_msg_prio
!= NULL
) {
2174 if (get_user(ax
->msg_prio
, u_msg_prio
)) {
2181 if (u_abs_timeout
!= NULL
) {
2182 if (copy_from_user(&ax
->abs_timeout
, u_abs_timeout
, sizeof(ax
->abs_timeout
))) {
2187 memset(&ax
->abs_timeout
, 0, sizeof(ax
->abs_timeout
));
2190 ax
->msg_len
= msg_len
;
2192 ax
->d
.type
= AUDIT_MQ_SENDRECV
;
2193 ax
->d
.next
= context
->aux
;
2194 context
->aux
= (void *)ax
;
2199 * __audit_mq_notify - record audit data for a POSIX MQ notify
2200 * @mqdes: MQ descriptor
2201 * @u_notification: Notification event
2203 * Returns 0 for success or NULL context or < 0 on error.
2206 int __audit_mq_notify(mqd_t mqdes
, const struct sigevent __user
*u_notification
)
2208 struct audit_aux_data_mq_notify
*ax
;
2209 struct audit_context
*context
= current
->audit_context
;
2214 if (likely(!context
))
2217 ax
= kmalloc(sizeof(*ax
), GFP_ATOMIC
);
2221 if (u_notification
!= NULL
) {
2222 if (copy_from_user(&ax
->notification
, u_notification
, sizeof(ax
->notification
))) {
2227 memset(&ax
->notification
, 0, sizeof(ax
->notification
));
2231 ax
->d
.type
= AUDIT_MQ_NOTIFY
;
2232 ax
->d
.next
= context
->aux
;
2233 context
->aux
= (void *)ax
;
2238 * __audit_mq_getsetattr - record audit data for a POSIX MQ get/set attribute
2239 * @mqdes: MQ descriptor
2242 * Returns 0 for success or NULL context or < 0 on error.
2244 int __audit_mq_getsetattr(mqd_t mqdes
, struct mq_attr
*mqstat
)
2246 struct audit_aux_data_mq_getsetattr
*ax
;
2247 struct audit_context
*context
= current
->audit_context
;
2252 if (likely(!context
))
2255 ax
= kmalloc(sizeof(*ax
), GFP_ATOMIC
);
2260 ax
->mqstat
= *mqstat
;
2262 ax
->d
.type
= AUDIT_MQ_GETSETATTR
;
2263 ax
->d
.next
= context
->aux
;
2264 context
->aux
= (void *)ax
;
2269 * audit_ipc_obj - record audit data for ipc object
2270 * @ipcp: ipc permissions
2272 * Returns 0 for success or NULL context or < 0 on error.
2274 int __audit_ipc_obj(struct kern_ipc_perm
*ipcp
)
2276 struct audit_aux_data_ipcctl
*ax
;
2277 struct audit_context
*context
= current
->audit_context
;
2279 ax
= kmalloc(sizeof(*ax
), GFP_ATOMIC
);
2283 ax
->uid
= ipcp
->uid
;
2284 ax
->gid
= ipcp
->gid
;
2285 ax
->mode
= ipcp
->mode
;
2286 security_ipc_getsecid(ipcp
, &ax
->osid
);
2287 ax
->d
.type
= AUDIT_IPC
;
2288 ax
->d
.next
= context
->aux
;
2289 context
->aux
= (void *)ax
;
2294 * audit_ipc_set_perm - record audit data for new ipc permissions
2295 * @qbytes: msgq bytes
2296 * @uid: msgq user id
2297 * @gid: msgq group id
2298 * @mode: msgq mode (permissions)
2300 * Returns 0 for success or NULL context or < 0 on error.
2302 int __audit_ipc_set_perm(unsigned long qbytes
, uid_t uid
, gid_t gid
, mode_t mode
)
2304 struct audit_aux_data_ipcctl
*ax
;
2305 struct audit_context
*context
= current
->audit_context
;
2307 ax
= kmalloc(sizeof(*ax
), GFP_ATOMIC
);
2311 ax
->qbytes
= qbytes
;
2316 ax
->d
.type
= AUDIT_IPC_SET_PERM
;
2317 ax
->d
.next
= context
->aux
;
2318 context
->aux
= (void *)ax
;
2322 int audit_bprm(struct linux_binprm
*bprm
)
2324 struct audit_aux_data_execve
*ax
;
2325 struct audit_context
*context
= current
->audit_context
;
2327 if (likely(!audit_enabled
|| !context
|| context
->dummy
))
2330 ax
= kmalloc(sizeof(*ax
), GFP_KERNEL
);
2334 ax
->argc
= bprm
->argc
;
2335 ax
->envc
= bprm
->envc
;
2337 ax
->d
.type
= AUDIT_EXECVE
;
2338 ax
->d
.next
= context
->aux
;
2339 context
->aux
= (void *)ax
;
2345 * audit_socketcall - record audit data for sys_socketcall
2346 * @nargs: number of args
2349 * Returns 0 for success or NULL context or < 0 on error.
2351 int audit_socketcall(int nargs
, unsigned long *args
)
2353 struct audit_aux_data_socketcall
*ax
;
2354 struct audit_context
*context
= current
->audit_context
;
2356 if (likely(!context
|| context
->dummy
))
2359 ax
= kmalloc(sizeof(*ax
) + nargs
* sizeof(unsigned long), GFP_KERNEL
);
2364 memcpy(ax
->args
, args
, nargs
* sizeof(unsigned long));
2366 ax
->d
.type
= AUDIT_SOCKETCALL
;
2367 ax
->d
.next
= context
->aux
;
2368 context
->aux
= (void *)ax
;
2373 * __audit_fd_pair - record audit data for pipe and socketpair
2374 * @fd1: the first file descriptor
2375 * @fd2: the second file descriptor
2377 * Returns 0 for success or NULL context or < 0 on error.
2379 int __audit_fd_pair(int fd1
, int fd2
)
2381 struct audit_context
*context
= current
->audit_context
;
2382 struct audit_aux_data_fd_pair
*ax
;
2384 if (likely(!context
)) {
2388 ax
= kmalloc(sizeof(*ax
), GFP_KERNEL
);
2396 ax
->d
.type
= AUDIT_FD_PAIR
;
2397 ax
->d
.next
= context
->aux
;
2398 context
->aux
= (void *)ax
;
2403 * audit_sockaddr - record audit data for sys_bind, sys_connect, sys_sendto
2404 * @len: data length in user space
2405 * @a: data address in kernel space
2407 * Returns 0 for success or NULL context or < 0 on error.
2409 int audit_sockaddr(int len
, void *a
)
2411 struct audit_aux_data_sockaddr
*ax
;
2412 struct audit_context
*context
= current
->audit_context
;
2414 if (likely(!context
|| context
->dummy
))
2417 ax
= kmalloc(sizeof(*ax
) + len
, GFP_KERNEL
);
2422 memcpy(ax
->a
, a
, len
);
2424 ax
->d
.type
= AUDIT_SOCKADDR
;
2425 ax
->d
.next
= context
->aux
;
2426 context
->aux
= (void *)ax
;
2430 void __audit_ptrace(struct task_struct
*t
)
2432 struct audit_context
*context
= current
->audit_context
;
2434 context
->target_pid
= t
->pid
;
2435 context
->target_auid
= audit_get_loginuid(t
);
2436 context
->target_uid
= t
->uid
;
2437 context
->target_sessionid
= audit_get_sessionid(t
);
2438 security_task_getsecid(t
, &context
->target_sid
);
2439 memcpy(context
->target_comm
, t
->comm
, TASK_COMM_LEN
);
2443 * audit_signal_info - record signal info for shutting down audit subsystem
2444 * @sig: signal value
2445 * @t: task being signaled
2447 * If the audit subsystem is being terminated, record the task (pid)
2448 * and uid that is doing that.
2450 int __audit_signal_info(int sig
, struct task_struct
*t
)
2452 struct audit_aux_data_pids
*axp
;
2453 struct task_struct
*tsk
= current
;
2454 struct audit_context
*ctx
= tsk
->audit_context
;
2456 if (audit_pid
&& t
->tgid
== audit_pid
) {
2457 if (sig
== SIGTERM
|| sig
== SIGHUP
|| sig
== SIGUSR1
|| sig
== SIGUSR2
) {
2458 audit_sig_pid
= tsk
->pid
;
2459 if (tsk
->loginuid
!= -1)
2460 audit_sig_uid
= tsk
->loginuid
;
2462 audit_sig_uid
= tsk
->uid
;
2463 security_task_getsecid(tsk
, &audit_sig_sid
);
2465 if (!audit_signals
|| audit_dummy_context())
2469 /* optimize the common case by putting first signal recipient directly
2470 * in audit_context */
2471 if (!ctx
->target_pid
) {
2472 ctx
->target_pid
= t
->tgid
;
2473 ctx
->target_auid
= audit_get_loginuid(t
);
2474 ctx
->target_uid
= t
->uid
;
2475 ctx
->target_sessionid
= audit_get_sessionid(t
);
2476 security_task_getsecid(t
, &ctx
->target_sid
);
2477 memcpy(ctx
->target_comm
, t
->comm
, TASK_COMM_LEN
);
2481 axp
= (void *)ctx
->aux_pids
;
2482 if (!axp
|| axp
->pid_count
== AUDIT_AUX_PIDS
) {
2483 axp
= kzalloc(sizeof(*axp
), GFP_ATOMIC
);
2487 axp
->d
.type
= AUDIT_OBJ_PID
;
2488 axp
->d
.next
= ctx
->aux_pids
;
2489 ctx
->aux_pids
= (void *)axp
;
2491 BUG_ON(axp
->pid_count
>= AUDIT_AUX_PIDS
);
2493 axp
->target_pid
[axp
->pid_count
] = t
->tgid
;
2494 axp
->target_auid
[axp
->pid_count
] = audit_get_loginuid(t
);
2495 axp
->target_uid
[axp
->pid_count
] = t
->uid
;
2496 axp
->target_sessionid
[axp
->pid_count
] = audit_get_sessionid(t
);
2497 security_task_getsecid(t
, &axp
->target_sid
[axp
->pid_count
]);
2498 memcpy(axp
->target_comm
[axp
->pid_count
], t
->comm
, TASK_COMM_LEN
);
2505 * audit_core_dumps - record information about processes that end abnormally
2506 * @signr: signal value
2508 * If a process ends with a core dump, something fishy is going on and we
2509 * should record the event for investigation.
2511 void audit_core_dumps(long signr
)
2513 struct audit_buffer
*ab
;
2515 uid_t auid
= audit_get_loginuid(current
);
2516 unsigned int sessionid
= audit_get_sessionid(current
);
2521 if (signr
== SIGQUIT
) /* don't care for those */
2524 ab
= audit_log_start(NULL
, GFP_KERNEL
, AUDIT_ANOM_ABEND
);
2525 audit_log_format(ab
, "auid=%u uid=%u gid=%u ses=%u",
2526 auid
, current
->uid
, current
->gid
, sessionid
);
2527 security_task_getsecid(current
, &sid
);
2532 if (security_secid_to_secctx(sid
, &ctx
, &len
))
2533 audit_log_format(ab
, " ssid=%u", sid
);
2535 audit_log_format(ab
, " subj=%s", ctx
);
2536 security_release_secctx(ctx
, len
);
2539 audit_log_format(ab
, " pid=%d comm=", current
->pid
);
2540 audit_log_untrustedstring(ab
, current
->comm
);
2541 audit_log_format(ab
, " sig=%ld", signr
);