1 /* auditsc.c -- System-call auditing support -*- linux-c -*-
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>
38 #include <linux/audit.h>
39 #include <linux/personality.h>
40 #include <linux/time.h>
41 #include <asm/unistd.h>
44 1 = put_count checking
45 2 = verbose put_count checking
49 /* No syscall auditing will take place unless audit_enabled != 0. */
50 extern int audit_enabled
;
52 /* AUDIT_NAMES is the number of slots we reserve in the audit_context
53 * for saving names from getname(). */
54 #define AUDIT_NAMES 20
56 /* AUDIT_NAMES_RESERVED is the number of slots we reserve in the
57 * audit_context from being used for nameless inodes from
59 #define AUDIT_NAMES_RESERVED 7
61 /* At task start time, the audit_state is set in the audit_context using
62 a per-task filter. At syscall entry, the audit_state is augmented by
63 the syscall filter. */
65 AUDIT_DISABLED
, /* Do not create per-task audit_context.
66 * No syscall-specific audit records can
68 AUDIT_SETUP_CONTEXT
, /* Create the per-task audit_context,
69 * but don't necessarily fill it in at
70 * syscall entry time (i.e., filter
72 AUDIT_BUILD_CONTEXT
, /* Create the per-task audit_context,
73 * and always fill it in at syscall
74 * entry time. This makes a full
75 * syscall record available if some
76 * other part of the kernel decides it
77 * should be recorded. */
78 AUDIT_RECORD_CONTEXT
/* Create the per-task audit_context,
79 * always fill it in at syscall entry
80 * time, and always write out the audit
81 * record at syscall exit time. */
84 /* When fs/namei.c:getname() is called, we store the pointer in name and
85 * we don't let putname() free it (instead we free all of the saved
86 * pointers at syscall exit time).
88 * Further, in fs/namei.c:path_lookup() we store the inode and device. */
99 struct audit_aux_data
{
100 struct audit_aux_data
*next
;
104 #define AUDIT_AUX_IPCPERM 0
106 struct audit_aux_data_ipcctl
{
107 struct audit_aux_data d
;
109 unsigned long qbytes
;
116 /* The per-task audit context. */
117 struct audit_context
{
118 int in_syscall
; /* 1 if task is in a syscall */
119 enum audit_state state
;
120 unsigned int serial
; /* serial number for record */
121 struct timespec ctime
; /* time of syscall entry */
122 uid_t loginuid
; /* login uid (identity) */
123 int major
; /* syscall number */
124 unsigned long argv
[4]; /* syscall arguments */
125 int return_valid
; /* return code is valid */
126 int return_code
;/* syscall return code */
127 int auditable
; /* 1 if record should be written */
129 struct audit_names names
[AUDIT_NAMES
];
130 struct audit_context
*previous
; /* For nested syscalls */
131 struct audit_aux_data
*aux
;
133 /* Save things to print about task_struct */
135 uid_t uid
, euid
, suid
, fsuid
;
136 gid_t gid
, egid
, sgid
, fsgid
;
137 unsigned long personality
;
146 /* There are three lists of rules -- one to search at task creation
147 * time, one to search at syscall entry time, and another to search at
148 * syscall exit time. */
149 static LIST_HEAD(audit_tsklist
);
150 static LIST_HEAD(audit_entlist
);
151 static LIST_HEAD(audit_extlist
);
154 struct list_head list
;
156 struct audit_rule rule
;
159 /* Check to see if two rules are identical. It is called from
160 * audit_del_rule during AUDIT_DEL. */
161 static int audit_compare_rule(struct audit_rule
*a
, struct audit_rule
*b
)
165 if (a
->flags
!= b
->flags
)
168 if (a
->action
!= b
->action
)
171 if (a
->field_count
!= b
->field_count
)
174 for (i
= 0; i
< a
->field_count
; i
++) {
175 if (a
->fields
[i
] != b
->fields
[i
]
176 || a
->values
[i
] != b
->values
[i
])
180 for (i
= 0; i
< AUDIT_BITMASK_SIZE
; i
++)
181 if (a
->mask
[i
] != b
->mask
[i
])
187 /* Note that audit_add_rule and audit_del_rule are called via
188 * audit_receive() in audit.c, and are protected by
189 * audit_netlink_sem. */
190 static inline int audit_add_rule(struct audit_entry
*entry
,
191 struct list_head
*list
)
193 if (entry
->rule
.flags
& AUDIT_PREPEND
) {
194 entry
->rule
.flags
&= ~AUDIT_PREPEND
;
195 list_add_rcu(&entry
->list
, list
);
197 list_add_tail_rcu(&entry
->list
, list
);
202 static void audit_free_rule(struct rcu_head
*head
)
204 struct audit_entry
*e
= container_of(head
, struct audit_entry
, rcu
);
208 /* Note that audit_add_rule and audit_del_rule are called via
209 * audit_receive() in audit.c, and are protected by
210 * audit_netlink_sem. */
211 static inline int audit_del_rule(struct audit_rule
*rule
,
212 struct list_head
*list
)
214 struct audit_entry
*e
;
216 /* Do not use the _rcu iterator here, since this is the only
217 * deletion routine. */
218 list_for_each_entry(e
, list
, list
) {
219 if (!audit_compare_rule(rule
, &e
->rule
)) {
220 list_del_rcu(&e
->list
);
221 call_rcu(&e
->rcu
, audit_free_rule
);
225 return -EFAULT
; /* No matching rule */
229 /* Copy rule from user-space to kernel-space. Called during
231 static int audit_copy_rule(struct audit_rule
*d
, struct audit_rule
*s
)
235 if (s
->action
!= AUDIT_NEVER
236 && s
->action
!= AUDIT_POSSIBLE
237 && s
->action
!= AUDIT_ALWAYS
)
239 if (s
->field_count
< 0 || s
->field_count
> AUDIT_MAX_FIELDS
)
243 d
->action
= s
->action
;
244 d
->field_count
= s
->field_count
;
245 for (i
= 0; i
< d
->field_count
; i
++) {
246 d
->fields
[i
] = s
->fields
[i
];
247 d
->values
[i
] = s
->values
[i
];
249 for (i
= 0; i
< AUDIT_BITMASK_SIZE
; i
++) d
->mask
[i
] = s
->mask
[i
];
253 int audit_receive_filter(int type
, int pid
, int uid
, int seq
, void *data
)
256 struct audit_entry
*entry
;
261 /* The *_rcu iterators not needed here because we are
262 always called with audit_netlink_sem held. */
263 list_for_each_entry(entry
, &audit_tsklist
, list
)
264 audit_send_reply(pid
, seq
, AUDIT_LIST
, 0, 1,
265 &entry
->rule
, sizeof(entry
->rule
));
266 list_for_each_entry(entry
, &audit_entlist
, list
)
267 audit_send_reply(pid
, seq
, AUDIT_LIST
, 0, 1,
268 &entry
->rule
, sizeof(entry
->rule
));
269 list_for_each_entry(entry
, &audit_extlist
, list
)
270 audit_send_reply(pid
, seq
, AUDIT_LIST
, 0, 1,
271 &entry
->rule
, sizeof(entry
->rule
));
272 audit_send_reply(pid
, seq
, AUDIT_LIST
, 1, 1, NULL
, 0);
275 if (!(entry
= kmalloc(sizeof(*entry
), GFP_KERNEL
)))
277 if (audit_copy_rule(&entry
->rule
, data
)) {
281 flags
= entry
->rule
.flags
;
282 if (!err
&& (flags
& AUDIT_PER_TASK
))
283 err
= audit_add_rule(entry
, &audit_tsklist
);
284 if (!err
&& (flags
& AUDIT_AT_ENTRY
))
285 err
= audit_add_rule(entry
, &audit_entlist
);
286 if (!err
&& (flags
& AUDIT_AT_EXIT
))
287 err
= audit_add_rule(entry
, &audit_extlist
);
290 flags
=((struct audit_rule
*)data
)->flags
;
291 if (!err
&& (flags
& AUDIT_PER_TASK
))
292 err
= audit_del_rule(data
, &audit_tsklist
);
293 if (!err
&& (flags
& AUDIT_AT_ENTRY
))
294 err
= audit_del_rule(data
, &audit_entlist
);
295 if (!err
&& (flags
& AUDIT_AT_EXIT
))
296 err
= audit_del_rule(data
, &audit_extlist
);
306 /* Compare a task_struct with an audit_rule. Return 1 on match, 0
308 static int audit_filter_rules(struct task_struct
*tsk
,
309 struct audit_rule
*rule
,
310 struct audit_context
*ctx
,
311 enum audit_state
*state
)
315 for (i
= 0; i
< rule
->field_count
; i
++) {
316 u32 field
= rule
->fields
[i
] & ~AUDIT_NEGATE
;
317 u32 value
= rule
->values
[i
];
322 result
= (tsk
->pid
== value
);
325 result
= (tsk
->uid
== value
);
328 result
= (tsk
->euid
== value
);
331 result
= (tsk
->suid
== value
);
334 result
= (tsk
->fsuid
== value
);
337 result
= (tsk
->gid
== value
);
340 result
= (tsk
->egid
== value
);
343 result
= (tsk
->sgid
== value
);
346 result
= (tsk
->fsgid
== value
);
349 result
= (tsk
->personality
== value
);
353 if (ctx
&& ctx
->return_valid
)
354 result
= (ctx
->return_code
== value
);
357 if (ctx
&& ctx
->return_valid
)
358 result
= (ctx
->return_code
>= 0);
362 for (j
= 0; j
< ctx
->name_count
; j
++) {
363 if (MAJOR(ctx
->names
[j
].dev
)==value
) {
372 for (j
= 0; j
< ctx
->name_count
; j
++) {
373 if (MINOR(ctx
->names
[j
].dev
)==value
) {
382 for (j
= 0; j
< ctx
->name_count
; j
++) {
383 if (ctx
->names
[j
].ino
== value
) {
393 result
= (ctx
->loginuid
== value
);
400 result
= (ctx
->argv
[field
-AUDIT_ARG0
]==value
);
404 if (rule
->fields
[i
] & AUDIT_NEGATE
)
409 switch (rule
->action
) {
410 case AUDIT_NEVER
: *state
= AUDIT_DISABLED
; break;
411 case AUDIT_POSSIBLE
: *state
= AUDIT_BUILD_CONTEXT
; break;
412 case AUDIT_ALWAYS
: *state
= AUDIT_RECORD_CONTEXT
; break;
417 /* At process creation time, we can determine if system-call auditing is
418 * completely disabled for this task. Since we only have the task
419 * structure at this point, we can only check uid and gid.
421 static enum audit_state
audit_filter_task(struct task_struct
*tsk
)
423 struct audit_entry
*e
;
424 enum audit_state state
;
427 list_for_each_entry_rcu(e
, &audit_tsklist
, list
) {
428 if (audit_filter_rules(tsk
, &e
->rule
, NULL
, &state
)) {
434 return AUDIT_BUILD_CONTEXT
;
437 /* At syscall entry and exit time, this filter is called if the
438 * audit_state is not low enough that auditing cannot take place, but is
439 * also not high enough that we already know we have to write and audit
440 * record (i.e., the state is AUDIT_SETUP_CONTEXT or AUDIT_BUILD_CONTEXT).
442 static enum audit_state
audit_filter_syscall(struct task_struct
*tsk
,
443 struct audit_context
*ctx
,
444 struct list_head
*list
)
446 struct audit_entry
*e
;
447 enum audit_state state
;
448 int word
= AUDIT_WORD(ctx
->major
);
449 int bit
= AUDIT_BIT(ctx
->major
);
452 list_for_each_entry_rcu(e
, list
, list
) {
453 if ((e
->rule
.mask
[word
] & bit
) == bit
454 && audit_filter_rules(tsk
, &e
->rule
, ctx
, &state
)) {
460 return AUDIT_BUILD_CONTEXT
;
463 /* This should be called with task_lock() held. */
464 static inline struct audit_context
*audit_get_context(struct task_struct
*tsk
,
468 struct audit_context
*context
= tsk
->audit_context
;
470 if (likely(!context
))
472 context
->return_valid
= return_valid
;
473 context
->return_code
= return_code
;
475 if (context
->in_syscall
&& !context
->auditable
) {
476 enum audit_state state
;
477 state
= audit_filter_syscall(tsk
, context
, &audit_extlist
);
478 if (state
== AUDIT_RECORD_CONTEXT
)
479 context
->auditable
= 1;
482 context
->pid
= tsk
->pid
;
483 context
->uid
= tsk
->uid
;
484 context
->gid
= tsk
->gid
;
485 context
->euid
= tsk
->euid
;
486 context
->suid
= tsk
->suid
;
487 context
->fsuid
= tsk
->fsuid
;
488 context
->egid
= tsk
->egid
;
489 context
->sgid
= tsk
->sgid
;
490 context
->fsgid
= tsk
->fsgid
;
491 context
->personality
= tsk
->personality
;
492 tsk
->audit_context
= NULL
;
496 static inline void audit_free_names(struct audit_context
*context
)
501 if (context
->auditable
502 ||context
->put_count
+ context
->ino_count
!= context
->name_count
) {
503 printk(KERN_ERR
"audit.c:%d(:%d): major=%d in_syscall=%d"
504 " name_count=%d put_count=%d"
505 " ino_count=%d [NOT freeing]\n",
507 context
->serial
, context
->major
, context
->in_syscall
,
508 context
->name_count
, context
->put_count
,
510 for (i
= 0; i
< context
->name_count
; i
++)
511 printk(KERN_ERR
"names[%d] = %p = %s\n", i
,
512 context
->names
[i
].name
,
513 context
->names
[i
].name
);
519 context
->put_count
= 0;
520 context
->ino_count
= 0;
523 for (i
= 0; i
< context
->name_count
; i
++)
524 if (context
->names
[i
].name
)
525 __putname(context
->names
[i
].name
);
526 context
->name_count
= 0;
529 static inline void audit_free_aux(struct audit_context
*context
)
531 struct audit_aux_data
*aux
;
533 while ((aux
= context
->aux
)) {
534 context
->aux
= aux
->next
;
539 static inline void audit_zero_context(struct audit_context
*context
,
540 enum audit_state state
)
542 uid_t loginuid
= context
->loginuid
;
544 memset(context
, 0, sizeof(*context
));
545 context
->state
= state
;
546 context
->loginuid
= loginuid
;
549 static inline struct audit_context
*audit_alloc_context(enum audit_state state
)
551 struct audit_context
*context
;
553 if (!(context
= kmalloc(sizeof(*context
), GFP_KERNEL
)))
555 audit_zero_context(context
, state
);
559 /* Filter on the task information and allocate a per-task audit context
560 * if necessary. Doing so turns on system call auditing for the
561 * specified task. This is called from copy_process, so no lock is
563 int audit_alloc(struct task_struct
*tsk
)
565 struct audit_context
*context
;
566 enum audit_state state
;
568 if (likely(!audit_enabled
))
569 return 0; /* Return if not auditing. */
571 state
= audit_filter_task(tsk
);
572 if (likely(state
== AUDIT_DISABLED
))
575 if (!(context
= audit_alloc_context(state
))) {
576 audit_log_lost("out of memory in audit_alloc");
580 /* Preserve login uid */
581 context
->loginuid
= -1;
582 if (current
->audit_context
)
583 context
->loginuid
= current
->audit_context
->loginuid
;
585 tsk
->audit_context
= context
;
586 set_tsk_thread_flag(tsk
, TIF_SYSCALL_AUDIT
);
590 static inline void audit_free_context(struct audit_context
*context
)
592 struct audit_context
*previous
;
596 previous
= context
->previous
;
597 if (previous
|| (count
&& count
< 10)) {
599 printk(KERN_ERR
"audit(:%d): major=%d name_count=%d:"
600 " freeing multiple contexts (%d)\n",
601 context
->serial
, context
->major
,
602 context
->name_count
, count
);
604 audit_free_names(context
);
605 audit_free_aux(context
);
610 printk(KERN_ERR
"audit: freed %d contexts\n", count
);
613 static void audit_log_exit(struct audit_context
*context
)
616 struct audit_buffer
*ab
;
618 ab
= audit_log_start(context
);
620 return; /* audit_panic has been called */
621 audit_log_format(ab
, "syscall=%d", context
->major
);
622 if (context
->personality
!= PER_LINUX
)
623 audit_log_format(ab
, " per=%lx", context
->personality
);
624 if (context
->return_valid
)
625 audit_log_format(ab
, " exit=%d", context
->return_code
);
627 " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
628 " pid=%d loginuid=%d uid=%d gid=%d"
629 " euid=%d suid=%d fsuid=%d"
630 " egid=%d sgid=%d fsgid=%d",
640 context
->euid
, context
->suid
, context
->fsuid
,
641 context
->egid
, context
->sgid
, context
->fsgid
);
643 while (context
->aux
) {
644 struct audit_aux_data
*aux
;
646 ab
= audit_log_start(context
);
648 continue; /* audit_panic has been called */
651 context
->aux
= aux
->next
;
653 audit_log_format(ab
, "auxitem=%d", aux
->type
);
655 case AUDIT_AUX_IPCPERM
: {
656 struct audit_aux_data_ipcctl
*axi
= (void *)aux
;
658 " qbytes=%lx uid=%d gid=%d mode=%x",
659 axi
->qbytes
, axi
->uid
, axi
->gid
, axi
->mode
);
666 for (i
= 0; i
< context
->name_count
; i
++) {
667 ab
= audit_log_start(context
);
669 continue; /* audit_panic has been called */
670 audit_log_format(ab
, "item=%d", i
);
671 if (context
->names
[i
].name
)
672 audit_log_format(ab
, " name=%s",
673 context
->names
[i
].name
);
674 if (context
->names
[i
].ino
!= (unsigned long)-1)
675 audit_log_format(ab
, " inode=%lu dev=%02x:%02x mode=%#o"
676 " uid=%d gid=%d rdev=%02x:%02x",
677 context
->names
[i
].ino
,
678 MAJOR(context
->names
[i
].dev
),
679 MINOR(context
->names
[i
].dev
),
680 context
->names
[i
].mode
,
681 context
->names
[i
].uid
,
682 context
->names
[i
].gid
,
683 MAJOR(context
->names
[i
].rdev
),
684 MINOR(context
->names
[i
].rdev
));
689 /* Free a per-task audit context. Called from copy_process and
690 * __put_task_struct. */
691 void audit_free(struct task_struct
*tsk
)
693 struct audit_context
*context
;
696 context
= audit_get_context(tsk
, 0, 0);
699 if (likely(!context
))
702 /* Check for system calls that do not go through the exit
703 * function (e.g., exit_group), then free context block. */
704 if (context
->in_syscall
&& context
->auditable
)
705 audit_log_exit(context
);
707 audit_free_context(context
);
710 /* Compute a serial number for the audit record. Audit records are
711 * written to user-space as soon as they are generated, so a complete
712 * audit record may be written in several pieces. The timestamp of the
713 * record and this serial number are used by the user-space daemon to
714 * determine which pieces belong to the same audit record. The
715 * (timestamp,serial) tuple is unique for each syscall and is live from
716 * syscall entry to syscall exit.
718 * Atomic values are only guaranteed to be 24-bit, so we count down.
720 * NOTE: Another possibility is to store the formatted records off the
721 * audit context (for those records that have a context), and emit them
722 * all at syscall exit. However, this could delay the reporting of
723 * significant errors until syscall exit (or never, if the system
725 static inline unsigned int audit_serial(void)
727 static atomic_t serial
= ATOMIC_INIT(0xffffff);
731 a
= atomic_read(&serial
);
732 if (atomic_dec_and_test(&serial
))
733 atomic_set(&serial
, 0xffffff);
734 b
= atomic_read(&serial
);
735 } while (b
!= a
- 1);
740 /* Fill in audit context at syscall entry. This only happens if the
741 * audit context was created when the task was created and the state or
742 * filters demand the audit context be built. If the state from the
743 * per-task filter or from the per-syscall filter is AUDIT_RECORD_CONTEXT,
744 * then the record will be written at syscall exit time (otherwise, it
745 * will only be written if another part of the kernel requests that it
747 void audit_syscall_entry(struct task_struct
*tsk
, int major
,
748 unsigned long a1
, unsigned long a2
,
749 unsigned long a3
, unsigned long a4
)
751 struct audit_context
*context
= tsk
->audit_context
;
752 enum audit_state state
;
756 /* This happens only on certain architectures that make system
757 * calls in kernel_thread via the entry.S interface, instead of
758 * with direct calls. (If you are porting to a new
759 * architecture, hitting this condition can indicate that you
760 * got the _exit/_leave calls backward in entry.S.)
764 * ppc64 yes (see arch/ppc64/kernel/misc.S)
766 * This also happens with vm86 emulation in a non-nested manner
767 * (entries without exits), so this case must be caught.
769 if (context
->in_syscall
) {
770 struct audit_context
*newctx
;
772 #if defined(__NR_vm86) && defined(__NR_vm86old)
773 /* vm86 mode should only be entered once */
774 if (major
== __NR_vm86
|| major
== __NR_vm86old
)
779 "audit(:%d) pid=%d in syscall=%d;"
780 " entering syscall=%d\n",
781 context
->serial
, tsk
->pid
, context
->major
, major
);
783 newctx
= audit_alloc_context(context
->state
);
785 newctx
->previous
= context
;
787 tsk
->audit_context
= newctx
;
789 /* If we can't alloc a new context, the best we
790 * can do is to leak memory (any pending putname
791 * will be lost). The only other alternative is
792 * to abandon auditing. */
793 audit_zero_context(context
, context
->state
);
796 BUG_ON(context
->in_syscall
|| context
->name_count
);
801 context
->major
= major
;
802 context
->argv
[0] = a1
;
803 context
->argv
[1] = a2
;
804 context
->argv
[2] = a3
;
805 context
->argv
[3] = a4
;
807 state
= context
->state
;
808 if (state
== AUDIT_SETUP_CONTEXT
|| state
== AUDIT_BUILD_CONTEXT
)
809 state
= audit_filter_syscall(tsk
, context
, &audit_entlist
);
810 if (likely(state
== AUDIT_DISABLED
))
813 context
->serial
= audit_serial();
814 context
->ctime
= CURRENT_TIME
;
815 context
->in_syscall
= 1;
816 context
->auditable
= !!(state
== AUDIT_RECORD_CONTEXT
);
819 /* Tear down after system call. If the audit context has been marked as
820 * auditable (either because of the AUDIT_RECORD_CONTEXT state from
821 * filtering, or because some other part of the kernel write an audit
822 * message), then write out the syscall information. In call cases,
823 * free the names stored from getname(). */
824 void audit_syscall_exit(struct task_struct
*tsk
, int return_code
)
826 struct audit_context
*context
;
828 get_task_struct(tsk
);
830 context
= audit_get_context(tsk
, 1, return_code
);
833 /* Not having a context here is ok, since the parent may have
834 * called __put_task_struct. */
835 if (likely(!context
))
838 if (context
->in_syscall
&& context
->auditable
)
839 audit_log_exit(context
);
841 context
->in_syscall
= 0;
842 context
->auditable
= 0;
843 if (context
->previous
) {
844 struct audit_context
*new_context
= context
->previous
;
845 context
->previous
= NULL
;
846 audit_free_context(context
);
847 tsk
->audit_context
= new_context
;
849 audit_free_names(context
);
850 audit_free_aux(context
);
851 audit_zero_context(context
, context
->state
);
852 tsk
->audit_context
= context
;
854 put_task_struct(tsk
);
857 /* Add a name to the list. Called from fs/namei.c:getname(). */
858 void audit_getname(const char *name
)
860 struct audit_context
*context
= current
->audit_context
;
862 if (!context
|| IS_ERR(name
) || !name
)
865 if (!context
->in_syscall
) {
867 printk(KERN_ERR
"%s:%d(:%d): ignoring getname(%p)\n",
868 __FILE__
, __LINE__
, context
->serial
, name
);
873 BUG_ON(context
->name_count
>= AUDIT_NAMES
);
874 context
->names
[context
->name_count
].name
= name
;
875 context
->names
[context
->name_count
].ino
= (unsigned long)-1;
876 ++context
->name_count
;
879 /* Intercept a putname request. Called from
880 * include/linux/fs.h:putname(). If we have stored the name from
881 * getname in the audit context, then we delay the putname until syscall
883 void audit_putname(const char *name
)
885 struct audit_context
*context
= current
->audit_context
;
888 if (!context
->in_syscall
) {
890 printk(KERN_ERR
"%s:%d(:%d): __putname(%p)\n",
891 __FILE__
, __LINE__
, context
->serial
, name
);
892 if (context
->name_count
) {
894 for (i
= 0; i
< context
->name_count
; i
++)
895 printk(KERN_ERR
"name[%d] = %p = %s\n", i
,
896 context
->names
[i
].name
,
897 context
->names
[i
].name
);
904 ++context
->put_count
;
905 if (context
->put_count
> context
->name_count
) {
906 printk(KERN_ERR
"%s:%d(:%d): major=%d"
907 " in_syscall=%d putname(%p) name_count=%d"
910 context
->serial
, context
->major
,
911 context
->in_syscall
, name
, context
->name_count
,
919 /* Store the inode and device from a lookup. Called from
920 * fs/namei.c:path_lookup(). */
921 void audit_inode(const char *name
, const struct inode
*inode
)
924 struct audit_context
*context
= current
->audit_context
;
926 if (!context
->in_syscall
)
928 if (context
->name_count
929 && context
->names
[context
->name_count
-1].name
930 && context
->names
[context
->name_count
-1].name
== name
)
931 idx
= context
->name_count
- 1;
932 else if (context
->name_count
> 1
933 && context
->names
[context
->name_count
-2].name
934 && context
->names
[context
->name_count
-2].name
== name
)
935 idx
= context
->name_count
- 2;
937 /* FIXME: how much do we care about inodes that have no
938 * associated name? */
939 if (context
->name_count
>= AUDIT_NAMES
- AUDIT_NAMES_RESERVED
)
941 idx
= context
->name_count
++;
942 context
->names
[idx
].name
= NULL
;
944 ++context
->ino_count
;
947 context
->names
[idx
].ino
= inode
->i_ino
;
948 context
->names
[idx
].dev
= inode
->i_sb
->s_dev
;
949 context
->names
[idx
].mode
= inode
->i_mode
;
950 context
->names
[idx
].uid
= inode
->i_uid
;
951 context
->names
[idx
].gid
= inode
->i_gid
;
952 context
->names
[idx
].rdev
= inode
->i_rdev
;
955 void audit_get_stamp(struct audit_context
*ctx
,
956 struct timespec
*t
, int *serial
)
959 t
->tv_sec
= ctx
->ctime
.tv_sec
;
960 t
->tv_nsec
= ctx
->ctime
.tv_nsec
;
961 *serial
= ctx
->serial
;
969 extern int audit_set_type(struct audit_buffer
*ab
, int type
);
971 int audit_set_loginuid(struct audit_context
*ctx
, uid_t loginuid
)
974 struct audit_buffer
*ab
;
976 ab
= audit_log_start(NULL
);
978 audit_log_format(ab
, "login pid=%d uid=%u "
979 "old loginuid=%u new loginuid=%u",
980 ctx
->pid
, ctx
->uid
, ctx
->loginuid
, loginuid
);
981 audit_set_type(ab
, AUDIT_LOGIN
);
984 ctx
->loginuid
= loginuid
;
989 uid_t
audit_get_loginuid(struct audit_context
*ctx
)
991 return ctx
? ctx
->loginuid
: -1;
994 int audit_ipc_perms(unsigned long qbytes
, uid_t uid
, gid_t gid
, mode_t mode
)
996 struct audit_aux_data_ipcctl
*ax
;
997 struct audit_context
*context
= current
->audit_context
;
999 if (likely(!context
))
1002 ax
= kmalloc(sizeof(*ax
), GFP_KERNEL
);
1006 ax
->qbytes
= qbytes
;
1011 ax
->d
.type
= AUDIT_AUX_IPCPERM
;
1012 ax
->d
.next
= context
->aux
;
1013 context
->aux
= (void *)ax
;