Skip to contentSkip to conversation

Light-weight Auditing Framework

PATCH In review Rik Faith 5 participants 12 messages Mar 11, 2004 – Mar 25, 2004

Subsystems (8)
Networking VFS MM core Scheduler +4 more subsystems
lore
Share & tools
lore

Rik Faith

author
#0 lore raw

PATCH 1/1 Light-weight Auditing Framework

Below is a patch against 2.6.4 that provides a low-overhead system-call auditing framework for Linux that is usable by LSM components (e.g., SELinux). This is an update of the patch discussed in this thread:

    http://marc.theaimsgroup.com/?t=107815888100001&r=1&w=2

In brief, it provides for netlink-based logging of audit records that have been generated in other parts of the kernel (e.g., SELinux) as well as the ability to audit system calls, either independently (using simple filtering) or as a compliment to the audit record that another part of the kernel generated.

The main goals were to provide system call auditing with 1) as low overhead as possible, and 2) without duplicating functionality that is already provided by SELinux (and/or other security infrastructures). This framework will work "stand-alone", but is not designed to provide, e.g., CAPP functionality without another security component in place.

This updated patch includes changes from feedback I have received, including the ability to compile without CONFIG_NET (and better use of tabs, so use -w if you diff against the older patch).

Please see http://people.redhat.com/faith/audit/ for an early example user-space client (auditd-0.4.tar.gz) and instructions on how to try it.

My future intentions at the kernel level include improving filtering (e.g., syscall personality/exit codes) and syscall support for more architectures. First, though, I'm going to work on documentation, a (real) audit daemon, and patches for other user-space tools so that people can play with the framework and understand how it can be used with and without SELinux.

 arch/i386/kernel/entry.S         |    6 
 arch/i386/kernel/ptrace.c        |    9 
 arch/ppc64/kernel/entry.S        |   15 
 arch/ppc64/kernel/ptrace.c       |   27 -
 arch/x86_64/ia32/ia32entry.S     |   12 
 arch/x86_64/kernel/entry.S       |   21 
 arch/x86_64/kernel/ptrace.c      |   23 -
 fs/namei.c                       |   15 
 include/asm-i386/thread_info.h   |    5 
 include/asm-ppc64/thread_info.h  |    3 
 include/asm-x86_64/thread_info.h |    5 
 include/linux/audit.h            |  202 +++++++++
 include/linux/fs.h               |   14 
 include/linux/netlink.h          |    1 
 include/linux/sched.h            |    5 
 init/Kconfig                     |   20 
 kernel/Makefile                  |    2 
 kernel/audit.c                   |  813 ++++++++++++++++++++++++++++++++++++
 kernel/auditsc.c                 |  869 +++++++++++++++++++++++++++++++++++++++
 kernel/fork.c                    |   10 
 security/selinux/avc.c           |  158 ++-----
 security/selinux/include/avc.h   |    7 
 security/selinux/ss/services.c   |    2 
 23 files changed, 2108 insertions(+), 136 deletions(-)
arch/i386/kernel/entry.S +3 -3
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/arch/i386/kernel/entry.S linux-2.6.4/arch/i386/kernel/entry.S
@@ -264,7 +264,7 @@ sysenter_past_esp:
 	cmpl $(nr_syscalls), %eax
 	jae syscall_badsys
 
-	testb $_TIF_SYSCALL_TRACE,TI_FLAGS(%ebp)
+	testb $_TIF_SYSCALL_T_OR_A,TI_FLAGS(%ebp)
 	jnz syscall_trace_entry
 	call *sys_call_table(,%eax,4)
 	movl %eax,EAX(%esp)
@@ -287,7 +287,7 @@ ENTRY(system_call)
 	cmpl $(nr_syscalls), %eax
 	jae syscall_badsys
 					# system call tracing in operation
-	testb $_TIF_SYSCALL_TRACE,TI_FLAGS(%ebp)
+	testb $_TIF_SYSCALL_T_OR_A,TI_FLAGS(%ebp)
 	jnz syscall_trace_entry
 syscall_call:
 	call *sys_call_table(,%eax,4)
@@ -354,7 +354,7 @@ syscall_trace_entry:
 	# perform syscall exit tracing
 	ALIGN
 syscall_exit_work:
-	testb $_TIF_SYSCALL_TRACE, %cl
+	testb $_TIF_SYSCALL_T_OR_A, %cl
 	jz work_pending
 	sti				# could let do_syscall_trace() call
 					# schedule() instead
arch/i386/kernel/ptrace.c +9 -0
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/arch/i386/kernel/ptrace.c linux-2.6.4/arch/i386/kernel/ptrace.c
@@ -14,6 +14,7 @@
 #include <linux/ptrace.h>
 #include <linux/user.h>
 #include <linux/security.h>
+#include <linux/audit.h>
 
 #include <asm/uaccess.h>
 #include <asm/pgtable.h>
@@ -524,6 +525,14 @@ out:
 __attribute__((regparm(3)))
 void do_syscall_trace(struct pt_regs *regs, int entryexit)
 {
+	if (unlikely(current->audit_context)) {
+		if (!entryexit)
+			audit_syscall_entry(current, regs->orig_eax,
+					    regs->ebx);
+		else
+			audit_syscall_exit(current);
+	}
+
 	if (!test_thread_flag(TIF_SYSCALL_TRACE))
 		return;
 	if (!(current->ptrace & PT_PTRACED))
arch/ppc64/kernel/entry.S +8 -7
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/arch/ppc64/kernel/entry.S linux-2.6.4/arch/ppc64/kernel/entry.S
@@ -95,7 +95,7 @@ _GLOBAL(DoSyscall)
 #endif /* SHOW_SYSCALLS */
 	clrrdi	r10,r1,THREAD_SHIFT
 	ld	r10,TI_FLAGS(r10)
-	andi.	r11,r10,_TIF_SYSCALL_TRACE
+	andi.	r11,r10,_TIF_SYSCALL_T_OR_A
 	bne-	50f
 	cmpli	0,r0,NR_syscalls
 	bge-	66f
@@ -151,7 +151,8 @@ _GLOBAL(ret_from_syscall_1)
 	b	22b
         
 /* Traced system call support */
-50:	bl	.do_syscall_trace
+50:	addi	r3,r1,STACK_FRAME_OVERHEAD
+	bl	.do_syscall_trace_enter
 	ld	r0,GPR0(r1)	/* Restore original registers */
 	ld	r3,GPR3(r1)
 	ld	r4,GPR4(r1)
@@ -201,7 +202,7 @@ _GLOBAL(ret_from_syscall_2)
 	oris	r10,r10,0x1000
 	std	r10,_CCR(r1)
 60:	std	r3,GPR3(r1)	/* Update return value */
-	bl	.do_syscall_trace
+	bl	.do_syscall_trace_leave
 	b	.ret_from_except
 66:	li	r3,ENOSYS
 	b	57b
@@ -234,14 +235,14 @@ _GLOBAL(ppc64_rt_sigreturn)
 
 80:	clrrdi	r4,r1,THREAD_SHIFT
 	ld	r4,TI_FLAGS(r4)
-	andi.	r4,r4,_TIF_SYSCALL_TRACE
+	andi.	r4,r4,_TIF_SYSCALL_T_OR_A
 	bne-	81f
 	cmpi	0,r3,0
 	bge	.ret_from_except
 	b	.ret_from_syscall_1
 81:	cmpi	0,r3,0
 	blt	.ret_from_syscall_2
-	bl	.do_syscall_trace
+	bl	.do_syscall_trace_leave
 	b	.ret_from_except
 
 /*
@@ -327,9 +328,9 @@ _GLOBAL(ret_from_fork)
 	bl	.schedule_tail
 	clrrdi	r4,r1,THREAD_SHIFT
 	ld	r4,TI_FLAGS(r4)
-	andi.	r4,r4,_TIF_SYSCALL_TRACE
+	andi.	r4,r4,_TIF_SYSCALL_T_OR_A
 	beq+	.ret_from_except
-	bl	.do_syscall_trace
+	bl	.do_syscall_trace_leave
 	b	.ret_from_except
 
 _GLOBAL(ret_from_except)
arch/ppc64/kernel/ptrace.c +22 -5
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/arch/ppc64/kernel/ptrace.c linux-2.6.4/arch/ppc64/kernel/ptrace.c
@@ -26,6 +26,7 @@
 #include <linux/ptrace.h>
 #include <linux/user.h>
 #include <linux/security.h>
+#include <linux/audit.h>
 
 #include <asm/uaccess.h>
 #include <asm/page.h>
@@ -286,12 +287,8 @@ out:
 	return ret;
 }
 
-void do_syscall_trace(void)
+static void do_syscall_trace(void)
 {
-	if (!test_thread_flag(TIF_SYSCALL_TRACE))
-		return;
-	if (!(current->ptrace & PT_PTRACED))
-		return;
 	/* the 0x80 provides a way for the tracing parent to distinguish
 	   between a syscall stop and SIGTRAP delivery */
 	ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
@@ -307,3 +304,23 @@ void do_syscall_trace(void)
 		current->exit_code = 0;
 	}
 }
+
+void do_syscall_trace_enter(struct pt_regs *regs)
+{
+	if (unlikely(current->audit_context))
+		audit_syscall_entry(current, regs->gpr[0], regs->gpr[3]);
+
+	if (test_thread_flag(TIF_SYSCALL_TRACE)
+	    && (current->ptrace & PT_PTRACED))
+		do_syscall_trace();
+}
+
+void do_syscall_trace_leave(void)
+{
+	if (unlikely(current->audit_context))
+		audit_syscall_exit(current);
+
+	if (test_thread_flag(TIF_SYSCALL_TRACE)
+	    && (current->ptrace & PT_PTRACED))
+		do_syscall_trace();
+}
arch/x86_64/ia32/ia32entry.S +6 -6
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/arch/x86_64/ia32/ia32entry.S linux-2.6.4/arch/x86_64/ia32/ia32entry.S
@@ -69,8 +69,8 @@ ENTRY(ia32_cstar_target)
 	.quad 1b,cstar_badarg
 	.previous	
 	GET_THREAD_INFO(%r10)
-	bt  $TIF_SYSCALL_TRACE,threadinfo_flags(%r10)
-	jc  ia32_tracesys
+	testl  $_TIF_SYSCALL_T_OR_A,threadinfo_flags(%r10)
+	jnz  ia32_tracesys
 cstar_do_call:	
 	cmpl $IA32_NR_syscalls,%eax
 	jae  ia32_badsys
@@ -99,7 +99,7 @@ cstar_tracesys:	
 	SAVE_REST
 	movq $-ENOSYS,RAX(%rsp)	/* really needed? */
 	movq %rsp,%rdi        /* &pt_regs -> arg1 */
-	call syscall_trace
+	call syscall_trace_enter
 	LOAD_ARGS ARGOFFSET  /* reload args from stack in case ptrace changed it */
 	RESTORE_REST
 	jmp cstar_do_call
@@ -141,8 +141,8 @@ ENTRY(ia32_syscall)
 	   this could be a problem. */
 	SAVE_ARGS
 	GET_THREAD_INFO(%r10)
-	bt $TIF_SYSCALL_TRACE,threadinfo_flags(%r10)
-	jc ia32_tracesys
+	testl $_TIF_SYSCALL_T_OR_A,threadinfo_flags(%r10)
+	jnz ia32_tracesys
 ia32_do_syscall:	
 	cmpl $(IA32_NR_syscalls),%eax
 	jae  ia32_badsys
@@ -155,7 +155,7 @@ ia32_tracesys:			 
 	SAVE_REST
 	movq $-ENOSYS,RAX(%rsp)	/* really needed? */
 	movq %rsp,%rdi        /* &pt_regs -> arg1 */
-	call syscall_trace
+	call syscall_trace_enter
 	LOAD_ARGS ARGOFFSET  /* reload args from stack in case ptrace changed it */
 	RESTORE_REST
 	jmp ia32_do_syscall
arch/x86_64/kernel/entry.S +11 -10
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/arch/x86_64/kernel/entry.S linux-2.6.4/arch/x86_64/kernel/entry.S
@@ -131,8 +131,8 @@ ENTRY(ret_from_fork)
 	CFI_DEFAULT_STACK
 	call schedule_tail
 	GET_THREAD_INFO(%rcx)
-	bt $TIF_SYSCALL_TRACE,threadinfo_flags(%rcx)
-	jc rff_trace
+	testl $_TIF_SYSCALL_T_OR_A,threadinfo_flags(%rcx)
+	jnz rff_trace
 rff_action:	
 	RESTORE_REST
 	testl $3,CS-ARGOFFSET(%rsp)	# from kernel_thread?
@@ -143,7 +143,7 @@ rff_action:	
 	jmp ret_from_sys_call
 rff_trace:
 	movq %rsp,%rdi
-	call syscall_trace
+	call syscall_trace_leave
 	GET_THREAD_INFO(%rcx)	
 	jmp rff_action
 	CFI_ENDPROC
@@ -185,8 +185,8 @@ ENTRY(system_call)
 	movq  %rax,ORIG_RAX-ARGOFFSET(%rsp) 
 	movq  %rcx,RIP-ARGOFFSET(%rsp)  
 	GET_THREAD_INFO(%rcx)
-	bt    $TIF_SYSCALL_TRACE,threadinfo_flags(%rcx) 
-	jc    tracesys
+	testl $_TIF_SYSCALL_T_OR_A,threadinfo_flags(%rcx) 
+	jnz tracesys
 	cmpq $__NR_syscall_max,%rax
 	ja badsys
 	movq %r10,%rcx
@@ -244,7 +244,7 @@ tracesys:			 
 	movq $-ENOSYS,RAX(%rsp)
 	FIXUP_TOP_OF_STACK %rdi
 	movq %rsp,%rdi
-	call syscall_trace
+	call syscall_trace_enter
 	LOAD_ARGS ARGOFFSET  /* reload args from stack in case ptrace changed it */
 	RESTORE_REST
 	cmpq $__NR_syscall_max,%rax
@@ -254,7 +254,7 @@ tracesys:			 
 	movq %rax,RAX-ARGOFFSET(%rsp)
 1:	SAVE_REST
 	movq %rsp,%rdi
-	call syscall_trace
+	call syscall_trace_leave
 	RESTORE_TOP_OF_STACK %rbx
 	RESTORE_REST
 	jmp ret_from_sys_call
@@ -297,13 +297,14 @@ int_very_careful:
 	sti
 	SAVE_REST
 	/* Check for syscall exit trace */	
-	bt $TIF_SYSCALL_TRACE,%edx
-	jnc int_signal
+	testl $_TIF_SYSCALL_T_OR_A,%edx
+	jz int_signal
 	pushq %rdi
 	leaq 8(%rsp),%rdi	# &ptregs -> arg1	
-	call syscall_trace
+	call syscall_trace_leave
 	popq %rdi
 	btr  $TIF_SYSCALL_TRACE,%edi
+	btr  $TIF_SYSCALL_AUDIT,%edi
 	jmp int_restore_rest
 	
 int_signal:
arch/x86_64/kernel/ptrace.c +22 -1
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/arch/x86_64/kernel/ptrace.c linux-2.6.4/arch/x86_64/kernel/ptrace.c
@@ -16,6 +16,7 @@
 #include <linux/ptrace.h>
 #include <linux/user.h>
 #include <linux/security.h>
+#include <linux/audit.h>
 
 #include <asm/uaccess.h>
 #include <asm/pgtable.h>
@@ -486,7 +487,7 @@ out:
 	return ret;
 }
 
-asmlinkage void syscall_trace(struct pt_regs *regs)
+static void syscall_trace(struct pt_regs *regs)
 {
 
 #if 0
@@ -513,3 +514,23 @@ asmlinkage void syscall_trace(struct pt_
 		current->exit_code = 0;
 	}
 }
+
+asmlinkage void syscall_trace_enter(struct pt_regs *regs)
+{
+	if (unlikely(current->audit_context))
+		audit_syscall_entry(current, regs->orig_rax, regs->rdi);
+
+	if (test_thread_flag(TIF_SYSCALL_TRACE)
+	    && (current->ptrace & PT_PTRACED))
+		syscall_trace(regs);
+}
+
+asmlinkage void syscall_trace_leave(struct pt_regs *regs)
+{
+	if (unlikely(current->audit_context))
+		audit_syscall_exit(current);
+	
+	if (test_thread_flag(TIF_SYSCALL_TRACE)
+	    && (current->ptrace & PT_PTRACED))
+		syscall_trace(regs);
+}
fs/namei.c +13 -2
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/fs/namei.c linux-2.6.4/fs/namei.c
@@ -26,6 +26,7 @@
 #include <linux/personality.h>
 #include <linux/security.h>
 #include <linux/mount.h>
+#include <linux/audit.h>
 #include <asm/namei.h>
 #include <asm/uaccess.h>
 
@@ -141,10 +142,12 @@ char * getname(const char __user * filen
 
 		result = tmp;
 		if (retval < 0) {
-			putname(tmp);
+			__putname(tmp);
 			result = ERR_PTR(retval);
 		}
 	}
+	if (unlikely(current->audit_context) && !IS_ERR(result) && result)
+		audit_getname(result);
 	return result;
 }
 
@@ -860,6 +863,8 @@ walk_init_root(const char *name, struct 
 
 int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd)
 {
+	int retval;
+
 	nd->last_type = LAST_ROOT; /* if there are only slashes... */
 	nd->flags = flags;
 
@@ -882,7 +887,13 @@ int fastcall path_lookup(const char *nam
 	}
 	read_unlock(&current->fs->lock);
 	current->total_link_count = 0;
-	return link_path_walk(name, nd);
+	retval = link_path_walk(name, nd);
+	if (unlikely(current->audit_context
+		     && nd && nd->dentry && nd->dentry->d_inode))
+		audit_inode(name,
+			    nd->dentry->d_inode->i_ino,
+			    nd->dentry->d_inode->i_rdev);
+	return retval;
 }
 
 /*
include/asm-i386/thread_info.h +4 -1
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/include/asm-i386/thread_info.h linux-2.6.4/include/asm-i386/thread_info.h
@@ -133,6 +133,7 @@ static inline struct thread_info *curren
 #define TIF_NEED_RESCHED	3	/* rescheduling necessary */
 #define TIF_SINGLESTEP		4	/* restore singlestep on return to user mode */
 #define TIF_IRET		5	/* return with iret */
+#define TIF_SYSCALL_AUDIT	7	/* syscall auditing active */
 #define TIF_POLLING_NRFLAG	16	/* true if poll_idle() is polling TIF_NEED_RESCHED */
 
 #define _TIF_SYSCALL_TRACE	(1<<TIF_SYSCALL_TRACE)
@@ -141,9 +142,11 @@ static inline struct thread_info *curren
 #define _TIF_NEED_RESCHED	(1<<TIF_NEED_RESCHED)
 #define _TIF_SINGLESTEP		(1<<TIF_SINGLESTEP)
 #define _TIF_IRET		(1<<TIF_IRET)
+#define _TIF_SYSCALL_AUDIT	(1<<TIF_SYSCALL_AUDIT)
+#define _TIF_SYSCALL_T_OR_A	(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT)
 #define _TIF_POLLING_NRFLAG	(1<<TIF_POLLING_NRFLAG)
 
-#define _TIF_WORK_MASK		0x0000FFFE	/* work to do on interrupt/exception return */
+#define _TIF_WORK_MASK		0x0000FF7E	/* work to do on interrupt/exception return */
 #define _TIF_ALLWORK_MASK	0x0000FFFF	/* work to do on any return to u-space */
 
 /*
include/asm-ppc64/thread_info.h +3 -0
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/include/asm-ppc64/thread_info.h linux-2.6.4/include/asm-ppc64/thread_info.h
@@ -97,6 +97,7 @@ static inline struct thread_info *curren
 #define TIF_32BIT		5	/* 32 bit binary */
 #define TIF_RUN_LIGHT		6	/* iSeries run light */
 #define TIF_ABI_PENDING		7	/* 32/64 bit switch needed */
+#define TIF_SYSCALL_AUDIT	8	/* syscall auditing active */
 
 /* as above, but as bit values */
 #define _TIF_SYSCALL_TRACE	(1<<TIF_SYSCALL_TRACE)
@@ -107,6 +108,8 @@ static inline struct thread_info *curren
 #define _TIF_32BIT		(1<<TIF_32BIT)
 #define _TIF_RUN_LIGHT		(1<<TIF_RUN_LIGHT)
 #define _TIF_ABI_PENDING	(1<<TIF_ABI_PENDING)
+#define _TIF_SYSCALL_AUDIT	(1<<TIF_SYSCALL_AUDIT)
+#define _TIF_SYSCALL_T_OR_A	(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT)
 
 #define _TIF_USER_WORK_MASK	(_TIF_NOTIFY_RESUME | _TIF_SIGPENDING | \
 				 _TIF_NEED_RESCHED)
include/asm-x86_64/thread_info.h +4 -1
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/include/asm-x86_64/thread_info.h linux-2.6.4/include/asm-x86_64/thread_info.h
@@ -102,6 +102,7 @@ static inline struct thread_info *stack_
 #define TIF_NEED_RESCHED	3	/* rescheduling necessary */
 #define TIF_SINGLESTEP		4	/* reenable singlestep on user return*/
 #define TIF_IRET		5	/* force IRET */
+#define TIF_SYSCALL_AUDIT	7	/* syscall auditing active */
 #define TIF_POLLING_NRFLAG	16	/* true if poll_idle() is polling TIF_NEED_RESCHED */
 #define TIF_IA32		17	/* 32bit process */ 
 #define TIF_FORK		18	/* ret_from_fork */
@@ -113,12 +114,14 @@ static inline struct thread_info *stack_
 #define _TIF_SINGLESTEP		(1<<TIF_SINGLESTEP)
 #define _TIF_NEED_RESCHED	(1<<TIF_NEED_RESCHED)
 #define _TIF_IRET		(1<<TIF_IRET)
+#define _TIF_SYSCALL_AUDIT	(1<<TIF_SYSCALL_AUDIT)
+#define _TIF_SYSCALL_T_OR_A	(_TIF_SYSCALL_TRACE|_TIF_SYSCALL_AUDIT)
 #define _TIF_POLLING_NRFLAG	(1<<TIF_POLLING_NRFLAG)
 #define _TIF_IA32		(1<<TIF_IA32)
 #define _TIF_FORK		(1<<TIF_FORK)
 #define _TIF_ABI_PENDING	(1<<TIF_ABI_PENDING)
 
-#define _TIF_WORK_MASK		0x0000FFFE	/* work to do on interrupt/exception return */
+#define _TIF_WORK_MASK		0x0000FF7E	/* work to do on interrupt/exception return */
 #define _TIF_ALLWORK_MASK	0x0000FFFF	/* work to do on any return to u-space */
 
 #define PREEMPT_ACTIVE     0x4000000
include/linux/audit.h +202 -0

202 lines not on this page · raw message

include/linux/fs.h +13 -1
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/include/linux/fs.h linux-2.6.4/include/linux/fs.h
@@ -20,6 +20,7 @@
 #include <linux/radix-tree.h>
 #include <linux/kobject.h>
 #include <asm/atomic.h>
+#include <linux/audit.h>
 
 struct iovec;
 struct nameidata;
@@ -1128,7 +1129,18 @@ extern char * getname(const char __user 
 extern void vfs_caches_init(unsigned long);
 
 #define __getname()	kmem_cache_alloc(names_cachep, SLAB_KERNEL)
-#define putname(name)	kmem_cache_free(names_cachep, (void *)(name))
+#define __putname(name) kmem_cache_free(names_cachep, (void *)(name))
+#ifndef CONFIG_AUDITSYSCALL
+#define putname(name)   __putname(name)
+#else
+#define putname(name)							\
+	do {								\
+		if (unlikely(current->audit_context))			\
+			audit_putname(name);				\
+		else							\
+			__putname(name);				\
+	} while (0)
+#endif
 
 extern int register_blkdev(unsigned int, const char *);
 extern int unregister_blkdev(unsigned int, const char *);
include/linux/netlink.h +1 -0
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/include/linux/netlink.h linux-2.6.4/include/linux/netlink.h
@@ -13,6 +13,7 @@
 #define NETLINK_XFRM		6	/* ipsec */
 #define NETLINK_SELINUX		7	/* SELinux event notifications */
 #define NETLINK_ARPD		8
+#define NETLINK_AUDIT		9	/* auditing */
 #define NETLINK_ROUTE6		11	/* af_inet6 route comm channel */
 #define NETLINK_IP6_FW		13
 #define NETLINK_DNRTMSG		14	/* DECnet routing messages */
include/linux/sched.h +4 -1
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/include/linux/sched.h linux-2.6.4/include/linux/sched.h
@@ -358,6 +358,8 @@ int set_current_groups(struct group_info
     ((gi)->blocks[(i)/NGROUPS_PER_BLOCK][(i)%NGROUPS_PER_BLOCK])
 
 
+struct audit_context;		/* See audit.c */
+
 struct task_struct {
 	volatile long state;	/* -1 unrunnable, 0 runnable, >0 stopped */
 	struct thread_info *thread_info;
@@ -467,7 +469,8 @@ struct task_struct {
 	sigset_t *notifier_mask;
 	
 	void *security;
-
+	struct audit_context *audit_context;
+	
 /* Thread group tracking */
    	u32 parent_exec_id;
    	u32 self_exec_id;
init/Kconfig +20 -0
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/init/Kconfig linux-2.6.4/init/Kconfig
@@ -120,6 +120,26 @@ config SYSCTL
 	  building a kernel for install/rescue disks or your system is very
 	  limited in memory.
 
+config AUDIT
+	bool "Auditing support"
+	default y if SECURITY_SELINUX
+	default n
+	help
+	  Enable auditing infrastructure that can be used with another
+	  kernel subsystem, such as SELinux (which requires this for
+	  logging of avc messages output).  Does not do system-call
+	  auditing without CONFIG_AUDITSYSCALL.
+
+config AUDITSYSCALL
+	bool "Enable system-call auditing support"
+	depends on AUDIT && (X86 || PPC64)
+	default y if SECURITY_SELINUX
+	default n
+	help
+	  Enable low-overhead system-call auditing infrastructure that
+	  can be used independently or with another kernel subsystem,
+	  such as SELinux.
+
 config LOG_BUF_SHIFT
 	int "Kernel log buffer size (16 => 64KB, 17 => 128KB)" if DEBUG_KERNEL
 	range 12 20
kernel/audit.c +813 -0

813 lines not on this page · raw message

kernel/auditsc.c +869 -0

869 lines not on this page · raw message

kernel/fork.c +9 -1
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/kernel/fork.c linux-2.6.4/kernel/fork.c
@@ -31,6 +31,7 @@
 #include <linux/futex.h>
 #include <linux/ptrace.h>
 #include <linux/mount.h>
+#include <linux/audit.h>
 
 #include <asm/pgtable.h>
 #include <asm/pgalloc.h>
@@ -85,6 +86,8 @@ void __put_task_struct(struct task_struc
 	WARN_ON(atomic_read(&tsk->usage));
 	WARN_ON(tsk == current);
 
+	if (unlikely(tsk->audit_context))
+		audit_free(tsk);
 	security_task_free(tsk);
 	free_uid(tsk->user);
 	put_group_info(tsk->group_info);
@@ -945,13 +948,16 @@ struct task_struct *copy_process(unsigne
 	p->start_time = get_jiffies_64();
 	p->security = NULL;
 	p->io_context = NULL;
+	p->audit_context = NULL;
 
 	retval = -ENOMEM;
 	if ((retval = security_task_alloc(p)))
 		goto bad_fork_cleanup;
+	if ((retval = audit_alloc(p)))
+		goto bad_fork_cleanup_security;
 	/* copy all the process information */
 	if ((retval = copy_semundo(clone_flags, p)))
-		goto bad_fork_cleanup_security;
+		goto bad_fork_cleanup_audit;
 	if ((retval = copy_files(clone_flags, p)))
 		goto bad_fork_cleanup_semundo;
 	if ((retval = copy_fs(clone_flags, p)))
@@ -1086,6 +1092,8 @@ bad_fork_cleanup_files:
 	exit_files(p); /* blocking */
 bad_fork_cleanup_semundo:
 	exit_sem(p);
+bad_fork_cleanup_audit:
+	audit_free(p);
 bad_fork_cleanup_security:
 	security_task_free(p);
 bad_fork_cleanup:
kernel/Makefile +2 -0
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/kernel/Makefile linux-2.6.4/kernel/Makefile
@@ -21,6 +21,8 @@ obj-$(CONFIG_COMPAT) += compat.o
 obj-$(CONFIG_IKCONFIG) += configs.o
 obj-$(CONFIG_IKCONFIG_PROC) += configs.o
 obj-$(CONFIG_STOP_MACHINE) += stop_machine.o
+obj-$(CONFIG_AUDIT) += audit.o
+obj-$(CONFIG_AUDITSYSCALL) += auditsc.o
 
 ifneq ($(CONFIG_IA64),y)
 # According to Alan Modra <[email protected]>, the -fno-omit-frame-pointer is
security/selinux/avc.c +65 -93

321 lines not on this page · raw message

security/selinux/include/avc.h +4 -3
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/security/selinux/include/avc.h linux-2.6.4/security/selinux/include/avc.h
@@ -114,9 +114,10 @@ static inline void avc_cache_stats_add(i
 /*
  * AVC display support
  */
-void avc_dump_av(u16 tclass, u32 av);
-void avc_dump_query(u32 ssid, u32 tsid, u16 tclass);
-void avc_dump_cache(char *tag);
+struct audit_buffer;
+void avc_dump_av(struct audit_buffer *ab, u16 tclass, u32 av);
+void avc_dump_query(struct audit_buffer *ab, u32 ssid, u32 tsid, u16 tclass);
+void avc_dump_cache(struct audit_buffer *ab, char *tag);
 
 /*
  * AVC operations
security/selinux/ss/services.c +1 -1
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/security/selinux/ss/services.c linux-2.6.4/security/selinux/ss/services.c
@@ -386,7 +386,7 @@ int security_sid_to_context(u32 sid, cha
 			char *scontextp;
 
 			*scontext_len = strlen(initial_sid_to_string[sid]) + 1;
-			scontextp = kmalloc(*scontext_len,GFP_KERNEL);
+			scontextp = kmalloc(*scontext_len,GFP_ATOMIC);
 			strcpy(scontextp, initial_sid_to_string[sid]);
 			*scontext = scontextp;
 			goto out;
#1 lore raw
Quoted 4 lines · Rik Faith

Below is a patch against 2.6.4 that provides a low-overhead system-call auditing framework for Linux that is usable by LSM components (e.g., SELinux). This is an update of the patch discussed in this thread:

    http://marc.theaimsgroup.com/?t=107815888100001&r=1&w=2

Just FYI, I have no objection to the changes in this patch to the SELinux module; we would be glad to have SELinux use this auditing framework if it is accepted into the mainline kernel. I suppose we can separately submit a patch to remove the avc_ratelimit code from the SELinux avc, as that should be handled by the audit framework itself.

Signature · 2 lines
Stephen Smalley <[email protected]>
National Security Agency
#2 lore raw
In reply to Rik Faith
Quoted 4 lines · Rik Faith

Below is a patch against 2.6.4 that provides a low-overhead system-call
auditing framework for Linux that is usable by LSM components (e.g., SELinux).

Thanks Rik.

This is not my area, but based on the earlier discussions, and on RH's intent to distribute and support the code and on its overall footprint and upon Stephen's words I shall proceed with this.

This patch gets non-trivial rejects against x86_64-update.patch, mainly in thread_info.h. Also note that arch/x86_64/ia32/ia32entry.S has gained another usage of TIF_SYSCALL_TRACE. Could you please rework and retest it on top of

ftp://ftp.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.4/2.6.4-mm1/broken-out/x86_64-update.patch

Or you can wait a day or so - we should merge the x86_64 patch into Linus's tree later this week.

author
#3 lore raw
Quoted 6 lines · Andrew Morton

This patch gets non-trivial rejects against x86_64-update.patch, mainly in thread_info.h. Also note that arch/x86_64/ia32/ia32entry.S has gained another usage of TIF_SYSCALL_TRACE. Could you please rework and retest it on top of

ftp://ftp.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.4/2.6.4-mm1/broken-out/x86_64-update.patch

Please see

    http://people.redhat.com/faith/audit/audit-20040311.2217.patch

for an update that applies in the face of x86_64-update.patch. I've tested this on ia32 and x86_64.

author
#4 lore raw
Quoted 7 lines · Rik Faith
Quoted 5 lines · Andrew Morton

This patch gets non-trivial rejects against x86_64-update.patch, mainly in thread_info.h. Also note that arch/x86_64/ia32/ia32entry.S has gained another usage of TIF_SYSCALL_TRACE. Could you please rework and retest it on top of

Quoted 6 lines
Quoted 1 line

ftp://ftp.kernel.org/pub/linux/kernel/people/akpm/patches/2.6/2.6.4/2.6.4-mm1/broken-out/x86_64-update.patch

Please see

    http://people.redhat.com/faith/audit/audit-20040311.2217.patch

for an update that applies in the face of x86_64-update.patch. I've tested this on ia32 and x86_64.

Please see

    http://people.redhat.com/faith/audit/audit-20040312.1006.patch

for an update against 2.6.4-bk1. This includes a bug fix to a bug I recently introduced, so please don't use the .2217 patch. Thanks.

#5 lore raw
In reply to Andrew Morton
Quoted 3 lines · Andrew Morton

This is not my area, but based on the earlier discussions, and on RH's intent to distribute and support the code and on its overall footprint and upon Stephen's words I shall proceed with this.

Do we know whether this code is a good fit for most people who want audit support?

I would have expected more feedback on this, e.g. from other companies/people/projects who have been working towards similar goals.

- James

Signature · 2 lines
James Morris
<[email protected]>
#6 lore raw
In reply to Rik Faith
Quoted 4 lines · Rik Faith

Below is a patch against 2.6.4 that provides a low-overhead system-call auditing framework for Linux that is usable by LSM components (e.g., SELinux). This is an update of the patch discussed in this thread:

    http://marc.theaimsgroup.com/?t=107815888100001&r=1&w=2

[ . . . ]

Great application of RCU -- audit rules should not change often, but could be referenced quite frequently!

A couple of comments:

o I don't see any rcu_read_lock() or rcu_read_unlock() calls.
These are needed on the read side in order to (1) let the people reading the code know the extent of the read-side critical section and (2) disable preemption in CONFIG_PREEMPT kernels. Without the former, someone will end up putting a blocking primitive in the wrong place. Without the latter, the kernel will do the dirty work all by itself. Either way, you get breakage.

For example, I suspect that audit_filter_task() needs to read as follows:

static enum audit_state audit_filter_task(struct task_struct *tsk)

{
	struct audit_entry *e; 
	enum audit_state   state;

	rcu_read_lock();
	list_for_each_entry_rcu(e, &audit_tsklist, list) {
		if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
			rcu_read_unlock();
			return state;
		}
	}
	rcu_read_unlock();
	return AUDIT_BUILD_CONTEXT;
}

Alternatively, you could put the rcu_read_lock() and rcu_read_unlock() around the single call to audit_filter_task() from audit_alloc().

All of the list_for_each_.*_rcu() macros need to be enclosed by rcu_read_lock() and rcu_read_unlock() calls.

o Presumably something surrounding netlink_kernel_create()
ensures that only one instance of audit_del_rule() will be executing at a given time. If not, some locking is needed.

Once this locking is present, the list_for_each_entry_rcu() in audit_del_rule() should be changed to list_for_each_entry(), as it cannot race with deletion, since it -is- deletion. The list_del_rcu() is correct, and should remain.

If you are using some sort of implicit locking, then please inject a clue...

o The audit_add_rule() function also needs something to prevent
races with other audit_add_rule() and audit_del_rule() instances. Again, this might be happening somehow in the netlink_kernel_create() mechanism, but I don't immediately see it. Then again, I do not claim to fully understand how the netlink_kernel_create() mechanism functions.

Again, looks like a promising application of RCU!

Thanx, Paul

author
#7 lore raw
Quoted 1 line · Paul E. McKenney

o I don't see any rcu_read_lock() or rcu_read_unlock() calls.

Fixed.

Quoted 4 lines

o Presumably something surrounding netlink_kernel_create()
ensures that only one instance of audit_del_rule() will be executing at a given time. If not, some locking is needed.

I was unable to find anything, so I added a semaphore to the receive routine, and comments to the add and delete routines.

Quoted 3 lines

o The audit_add_rule() function also needs something to prevent
races with other audit_add_rule() and audit_del_rule() instances.

This is also handled by the semaphore.

Thanks for your comments! This patch is against 2.6.5-rc1-mm1. It also corrects a problem that could trigger a BUG() near boot time.

 audit.c   |    9 +++++++++
 auditsc.c |   38 +++++++++++++++++++++++++++++---------
 2 files changed, 38 insertions(+), 9 deletions(-)
kernel/audit.c +9 -0
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/kernel/audit.c linux-2.6.4/kernel/audit.c
@@ -111,6 +111,11 @@ static LIST_HEAD(audit_tsklist);
 static LIST_HEAD(audit_entlist);
 static LIST_HEAD(audit_extlist);
 
+/* The netlink socket is only to be read by 1 CPU, which lets us assume
+ * that list additions and deletions never happen simultaneiously in
+ * auditsc.c */
+static DECLARE_MUTEX(audit_netlink_sem);
+
 /* AUDIT_BUFSIZ is the size of the temporary buffer used for formatting
  * audit records.  Since printk uses a 1024 byte buffer, this buffer
  * should be at least that large. */
@@ -427,6 +432,9 @@ static void audit_receive(struct sock *s
 {
 	struct sk_buff  *skb;
 
+	if (down_trylock(&audit_netlink_sem))
+		return;
+	
 				/* FIXME: this must not cause starvation */
 	while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
 		if (audit_receive_skb(skb) && skb->len)
@@ -434,6 +442,7 @@ static void audit_receive(struct sock *s
 		else
 			kfree_skb(skb);
 	}
+	up(&audit_netlink_sem);
 }
 
 /* Move data from tmp buffer into an skb.  This is an extra copy, and
kernel/auditsc.c +29 -9
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/kernel/auditsc.c linux-2.6.4/kernel/auditsc.c
@@ -158,6 +158,9 @@ static int audit_compare_rule(struct aud
 	return 0;
 }
 
+/* Note that audit_add_rule and audit_del_rule are called via
+ * audit_receive() in audit.c, and are protected by
+ * audit_netlink_sem. */
 static inline int audit_add_rule(struct audit_entry *entry,
 				 struct list_head *list)
 {
@@ -175,12 +178,17 @@ static void audit_free_rule(void *arg)
 	kfree(arg);
 }
 
+/* Note that audit_add_rule and audit_del_rule are called via
+ * audit_receive() in audit.c, and are protected by
+ * audit_netlink_sem. */
 static inline int audit_del_rule(struct audit_rule *rule,
 				 struct list_head *list)
 {
 	struct audit_entry  *e;
 
-	list_for_each_entry_rcu(e, list, list) {
+	/* Do not use the _rcu iterator here, since this is the only
+	 * deletion routine. */
+	list_for_each_entry(e, list, list) {
 		if (!audit_compare_rule(rule, &e->rule)) {
 			list_del_rcu(&e->list);
 			call_rcu(&e->rcu, audit_free_rule, e);
@@ -223,6 +231,7 @@ int audit_receive_filter(int type, int p
 
 	switch (type) {
 	case AUDIT_LIST:
+		rcu_read_lock();
 		list_for_each_entry_rcu(entry, &audit_tsklist, list)
 			audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
 					 &entry->rule, sizeof(entry->rule));
@@ -232,6 +241,7 @@ int audit_receive_filter(int type, int p
 		list_for_each_entry_rcu(entry, &audit_extlist, list)
 			audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
 					 &entry->rule, sizeof(entry->rule));
+		rcu_read_unlock();
 		audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
 		break;
 	case AUDIT_ADD:
@@ -382,10 +392,14 @@ static enum audit_state audit_filter_tas
 	struct audit_entry *e;
 	enum audit_state   state;
 
+	rcu_read_lock();
 	list_for_each_entry_rcu(e, &audit_tsklist, list) {
-		if (audit_filter_rules(tsk, &e->rule, NULL, &state))
+		if (audit_filter_rules(tsk, &e->rule, NULL, &state)) {
+			rcu_read_unlock();
 			return state;
+		}
 	}
+	rcu_read_unlock();
 	return AUDIT_BUILD_CONTEXT;
 }
 
@@ -403,11 +417,15 @@ static enum audit_state audit_filter_sys
 	int		   word = AUDIT_WORD(ctx->major);
 	int		   bit  = AUDIT_BIT(ctx->major);
 
+	rcu_read_lock();
 	list_for_each_entry_rcu(e, list, list) {
 		if ((e->rule.mask[word] & bit) == bit
- 		    && audit_filter_rules(tsk, &e->rule, ctx, &state))
+ 		    && audit_filter_rules(tsk, &e->rule, ctx, &state)) {
+			rcu_read_unlock();
 			return state;
+		}
 	}
+	rcu_read_unlock();
 	return AUDIT_BUILD_CONTEXT;
 }
 
@@ -753,8 +771,8 @@ void audit_getname(const char *name)
 	BUG_ON(!context);
 	if (!context->in_syscall) {
 #if AUDIT_DEBUG == 2
-		printk(KERN_ERR "audit.c:%d(:%d): ignoring getname(%p)\n",
-		       __LINE__, context->serial, name);
+		printk(KERN_ERR "%s:%d(:%d): ignoring getname(%p)\n",
+		       __FILE__, __LINE__, context->serial, name);
 		dump_stack();
 #endif
 		return;
@@ -777,8 +795,8 @@ void audit_putname(const char *name)
 	BUG_ON(!context);
 	if (!context->in_syscall) {
 #if AUDIT_DEBUG == 2
-		printk(KERN_ERR "audit.c:%d(:%d): __putname(%p)\n",
-		       __LINE__, context->serial, name);
+		printk(KERN_ERR "%s:%d(:%d): __putname(%p)\n",
+		       __FILE__, __LINE__, context->serial, name);
 		if (context->name_count) {
 			int i;
 			for (i = 0; i < context->name_count; i++)
@@ -793,10 +811,10 @@ void audit_putname(const char *name)
 	else {
 		++context->put_count;
 		if (context->put_count > context->name_count) {
-			printk(KERN_ERR "audit.c:%d(:%d): major=%d"
+			printk(KERN_ERR "%s:%d(:%d): major=%d"
 			       " in_syscall=%d putname(%p) name_count=%d"
 			       " put_count=%d\n",
-			       __LINE__,
+			       __FILE__, __LINE__,
 			       context->serial, context->major,
 			       context->in_syscall, name, context->name_count,
 			       context->put_count);
@@ -813,6 +831,8 @@ void audit_inode(const char *name, unsig
 	int idx;
 	struct audit_context *context = current->audit_context;
 
+	if (!context->in_syscall)
+		return;
 	if (context->name_count
 	    && context->names[context->name_count-1].name
 	    && context->names[context->name_count-1].name == name)
#8 lore raw
Quoted 5 lines · Rik Faith
Quoted 1 line · Paul E. McKenney

o I don't see any rcu_read_lock() or rcu_read_unlock() calls.

Fixed.

Much improved!

Since audit_receive_filter() is only called with audit_netlink_sem held, it cannot race with either audit_del_rule() or audit_add_rule(), so the list_for_each_entry_rcu()s may be replaced by list_for_each_entry()s, and the rcu_read_{un,}lock()s removed.

Not a fatal problem, just a bit of unnecessary overhead.

The others look good!

Quoted 7 lines
Quoted 4 lines

o Presumably something surrounding netlink_kernel_create()
ensures that only one instance of audit_del_rule() will be executing at a given time. If not, some locking is needed.

I was unable to find anything, so I added a semaphore to the receive routine, and comments to the add and delete routines.

Looks good!

Quoted 5 lines
Quoted 3 lines

o The audit_add_rule() function also needs something to prevent
races with other audit_add_rule() and audit_del_rule() instances.

This is also handled by the semaphore.

Ditto!

Quoted 6 lines

Thanks for your comments! This patch is against 2.6.5-rc1-mm1. It also corrects a problem that could trigger a BUG() near boot time.

 audit.c   |    9 +++++++++
 auditsc.c |   38 +++++++++++++++++++++++++++++---------
 2 files changed, 38 insertions(+), 9 deletions(-)

Glad to help!

Thanx, Paul

author
#9 lore raw
Quoted 7 lines · Paul E. McKenney
Quoted 5 lines · Rik Faith
Quoted 1 line · Paul E. McKenney

o I don't see any rcu_read_lock() or rcu_read_unlock() calls.

Fixed.

Quoted 8 lines

Much improved!

Since audit_receive_filter() is only called with audit_netlink_sem held, it cannot race with either audit_del_rule() or audit_add_rule(), so the list_for_each_entry_rcu()s may be replaced by list_for_each_entry()s, and the rcu_read_{un,}lock()s removed.

Not a fatal problem, just a bit of unnecessary overhead.

A fix for this is part of the attached patch.

Other features of the attached patch are:
1) generalized the ability to test for inequality
2) added syscall exit status reporting and testing
3) added ability to report and test first 4 syscall arguments (this adds a large amount of flexibility for little cost; not implemented or tested on ppc64)
4) added ability to report and test personality

User-space demo program enhanced for new fields and inequality testing:
http://people.redhat.com/faith/audit/auditd-0.5.tar.gz

This patch is against 2.6.5-rc2, but also applies against 2.6.5-rc2-mm1.

 arch/i386/kernel/ptrace.c   |    5 -
 arch/ppc64/kernel/ptrace.c  |    6 +
 arch/x86_64/kernel/ptrace.c |   11 +-
 include/linux/audit.h       |   19 +++-
 kernel/auditsc.c            |  170 ++++++++++++++++++++++++++------------------
 5 files changed, 126 insertions(+), 85 deletions(-)
arch/i386/kernel/ptrace.c +3 -2
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/arch/i386/kernel/ptrace.c linux-2.6.4/arch/i386/kernel/ptrace.c
@@ -528,9 +528,10 @@ void do_syscall_trace(struct pt_regs *re
 	if (unlikely(current->audit_context)) {
 		if (!entryexit)
 			audit_syscall_entry(current, regs->orig_eax,
-					    regs->ebx);
+					    regs->ebx, regs->ecx,
+					    regs->edx, regs->esi);
 		else
-			audit_syscall_exit(current);
+			audit_syscall_exit(current, regs->eax);
 	}
 
 	if (!test_thread_flag(TIF_SYSCALL_TRACE))
arch/ppc64/kernel/ptrace.c +4 -2
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/arch/ppc64/kernel/ptrace.c linux-2.6.4/arch/ppc64/kernel/ptrace.c
@@ -308,7 +308,9 @@ static void do_syscall_trace(void)
 void do_syscall_trace_enter(struct pt_regs *regs)
 {
 	if (unlikely(current->audit_context))
-		audit_syscall_entry(current, regs->gpr[0], regs->gpr[3]);
+		audit_syscall_entry(current, regs->gpr[0],
+				    regs->gpr[3], regs->gpr[4],
+				    regs->gpr[5], regs->gpr[6]);
 
 	if (test_thread_flag(TIF_SYSCALL_TRACE)
 	    && (current->ptrace & PT_PTRACED))
@@ -318,7 +320,7 @@ void do_syscall_trace_enter(struct pt_re
 void do_syscall_trace_leave(void)
 {
 	if (unlikely(current->audit_context))
-		audit_syscall_exit(current);
+		audit_syscall_exit(current, 0);	/* FIXME: pass pt_regs */
 
 	if (test_thread_flag(TIF_SYSCALL_TRACE)
 	    && (current->ptrace & PT_PTRACED))
arch/x86_64/kernel/ptrace.c +4 -7
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/arch/x86_64/kernel/ptrace.c linux-2.6.4/arch/x86_64/kernel/ptrace.c
@@ -497,11 +497,6 @@ static void syscall_trace(struct pt_regs
 	       current_thread_info()->flags, current->ptrace); 
 #endif
 
-	if (!test_thread_flag(TIF_SYSCALL_TRACE))
-		return; 
-	if (!(current->ptrace & PT_PTRACED))
-		return;
-	
 	ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
 				? 0x80 : 0));
 	/*
@@ -518,7 +513,9 @@ static void syscall_trace(struct pt_regs
 asmlinkage void syscall_trace_enter(struct pt_regs *regs)
 {
 	if (unlikely(current->audit_context))
-		audit_syscall_entry(current, regs->orig_rax, regs->rdi);
+		audit_syscall_entry(current, regs->orig_rax,
+				    regs->rdi, regs->rsi,
+				    regs->rdx, regs->r10);
 
 	if (test_thread_flag(TIF_SYSCALL_TRACE)
 	    && (current->ptrace & PT_PTRACED))
@@ -528,7 +525,7 @@ asmlinkage void syscall_trace_enter(stru
 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
 {
 	if (unlikely(current->audit_context))
-		audit_syscall_exit(current);
+		audit_syscall_exit(current, regs->rax);
 
 	if (test_thread_flag(TIF_SYSCALL_TRACE)
 	    && (current->ptrace & PT_PTRACED))
include/linux/audit.h +14 -5
diff -rupP --exclude-from=ignore linux-2.6.4-pristine/include/linux/audit.h linux-2.6.4/include/linux/audit.h
@@ -66,6 +66,7 @@
 #define AUDIT_SGID	7
 #define AUDIT_FSGID	8
 #define AUDIT_LOGINUID	9
+#define AUDIT_PERS	10
 
 				/* These are ONLY useful when checking
 				 * at syscall exit time (AUDIT_AT_EXIT). */
@@ -73,7 +74,14 @@
 #define AUDIT_DEVMINOR	101
 #define AUDIT_INODE	102
 #define AUDIT_EXIT	103
-#define AUDIT_NOTEXIT	104
+#define AUDIT_SUCCESS   104	/* exit >= 0; value ignored */
+
+#define AUDIT_ARG0      200
+#define AUDIT_ARG1      (AUDIT_ARG0+1)
+#define AUDIT_ARG2      (AUDIT_ARG0+2)
+#define AUDIT_ARG3      (AUDIT_ARG0+3)
+
+#define AUDIT_NEGATE    0x80000000
 
 
 /* Status symbols */
@@ -134,8 +142,9 @@ struct audit_context;
 extern int  audit_alloc(struct task_struct *task);
 extern void audit_free(struct task_struct *task);
 extern void audit_syscall_entry(struct task_struct *task,
-				int major, int minor);
-extern void audit_syscall_exit(struct task_struct *task);
+				int major, unsigned long a0, unsigned long a1,
+				unsigned long a2, unsigned long a3);
+extern void audit_syscall_exit(struct task_struct *task, int return_code);
 extern void audit_getname(const char *name);
 extern void audit_putname(const char *name);
 extern void audit_inode(const char *name, unsigned long ino, dev_t rdev);
@@ -149,8 +158,8 @@ extern int  audit_set_loginuid(struct au
 #else
 #define audit_alloc(t) ({ 0; })
 #define audit_free(t) do { ; } while (0)
-#define audit_syscall_entry(t,a,b) do { ; } while (0)
-#define audit_syscall_exit(t) do { ; } while (0)
+#define audit_syscall_entry(t,a,b,c,d,e) do { ; } while (0)
+#define audit_syscall_exit(t,r) do { ; } while (0)
 #define audit_getname(n) do { ; } while (0)
 #define audit_putname(n) do { ; } while (0)
 #define audit_inode(n,i,d) do { ; } while (0)
kernel/auditsc.c +101 -69

299 lines not on this page · raw message

#11 lore raw
In reply to Rik Faith

Looks good to me from an RCU viewpoint!

Thanx, Paul

Quoted 483 lines · Rik Faith
Quoted 7 lines · Paul E. McKenney
Quoted 5 lines · Rik Faith
Quoted 1 line · Paul E. McKenney

o I don't see any rcu_read_lock() or rcu_read_unlock() calls.

Fixed.

Quoted 8 lines

Much improved!

Since audit_receive_filter() is only called with audit_netlink_sem held, it cannot race with either audit_del_rule() or audit_add_rule(), so the list_for_each_entry_rcu()s may be replaced by list_for_each_entry()s, and the rcu_read_{un,}lock()s removed.

Not a fatal problem, just a bit of unnecessary overhead.

A fix for this is part of the attached patch.

Other features of the attached patch are:
1) generalized the ability to test for inequality
2) added syscall exit status reporting and testing
3) added ability to report and test first 4 syscall arguments (this adds a large amount of flexibility for little cost; not implemented or tested on ppc64)
4) added ability to report and test personality

User-space demo program enhanced for new fields and inequality testing:
http://people.redhat.com/faith/audit/auditd-0.5.tar.gz

This patch is against 2.6.5-rc2, but also applies against 2.6.5-rc2-mm1.

 arch/i386/kernel/ptrace.c   |    5 -
 arch/ppc64/kernel/ptrace.c  |    6 +
 arch/x86_64/kernel/ptrace.c |   11 +-
 include/linux/audit.h       |   19 +++-
 kernel/auditsc.c            |  170 ++++++++++++++++++++++++++------------------
 5 files changed, 126 insertions(+), 85 deletions(-)

diff -rupP --exclude-from=ignore linux-2.6.4-pristine/arch/i386/kernel/ptrace.c linux-2.6.4/arch/i386/kernel/ptrace.c

arch/i386/kernel/ptrace.c +3 -2
@@ -528,9 +528,10 @@ void do_syscall_trace(struct pt_regs *re
 	if (unlikely(current->audit_context)) {
 		if (!entryexit)
 			audit_syscall_entry(current, regs->orig_eax,
-					    regs->ebx);
+					    regs->ebx, regs->ecx,
+					    regs->edx, regs->esi);
 		else
-			audit_syscall_exit(current);
+			audit_syscall_exit(current, regs->eax);
 	}
 
 	if (!test_thread_flag(TIF_SYSCALL_TRACE))

diff -rupP --exclude-from=ignore linux-2.6.4-pristine/arch/ppc64/kernel/ptrace.c linux-2.6.4/arch/ppc64/kernel/ptrace.c

arch/ppc64/kernel/ptrace.c +4 -2
@@ -308,7 +308,9 @@ static void do_syscall_trace(void)
 void do_syscall_trace_enter(struct pt_regs *regs)
 {
 	if (unlikely(current->audit_context))
-		audit_syscall_entry(current, regs->gpr[0], regs->gpr[3]);
+		audit_syscall_entry(current, regs->gpr[0],
+				    regs->gpr[3], regs->gpr[4],
+				    regs->gpr[5], regs->gpr[6]);
 
 	if (test_thread_flag(TIF_SYSCALL_TRACE)
 	    && (current->ptrace & PT_PTRACED))
@@ -318,7 +320,7 @@ void do_syscall_trace_enter(struct pt_re
 void do_syscall_trace_leave(void)
 {
 	if (unlikely(current->audit_context))
-		audit_syscall_exit(current);
+		audit_syscall_exit(current, 0);	/* FIXME: pass pt_regs */
 
 	if (test_thread_flag(TIF_SYSCALL_TRACE)
 	    && (current->ptrace & PT_PTRACED))

diff -rupP --exclude-from=ignore linux-2.6.4-pristine/arch/x86_64/kernel/ptrace.c linux-2.6.4/arch/x86_64/kernel/ptrace.c

arch/x86_64/kernel/ptrace.c +4 -7
@@ -497,11 +497,6 @@ static void syscall_trace(struct pt_regs
 	       current_thread_info()->flags, current->ptrace); 
 #endif
 
-	if (!test_thread_flag(TIF_SYSCALL_TRACE))
-		return; 
-	if (!(current->ptrace & PT_PTRACED))
-		return;
-	
 	ptrace_notify(SIGTRAP | ((current->ptrace & PT_TRACESYSGOOD)
 				? 0x80 : 0));
 	/*
@@ -518,7 +513,9 @@ static void syscall_trace(struct pt_regs
 asmlinkage void syscall_trace_enter(struct pt_regs *regs)
 {
 	if (unlikely(current->audit_context))
-		audit_syscall_entry(current, regs->orig_rax, regs->rdi);
+		audit_syscall_entry(current, regs->orig_rax,
+				    regs->rdi, regs->rsi,
+				    regs->rdx, regs->r10);
 
 	if (test_thread_flag(TIF_SYSCALL_TRACE)
 	    && (current->ptrace & PT_PTRACED))
@@ -528,7 +525,7 @@ asmlinkage void syscall_trace_enter(stru
 asmlinkage void syscall_trace_leave(struct pt_regs *regs)
 {
 	if (unlikely(current->audit_context))
-		audit_syscall_exit(current);
+		audit_syscall_exit(current, regs->rax);
 
 	if (test_thread_flag(TIF_SYSCALL_TRACE)
 	    && (current->ptrace & PT_PTRACED))

diff -rupP --exclude-from=ignore linux-2.6.4-pristine/include/linux/audit.h linux-2.6.4/include/linux/audit.h

include/linux/audit.h +14 -5
@@ -66,6 +66,7 @@
 #define AUDIT_SGID	7
 #define AUDIT_FSGID	8
 #define AUDIT_LOGINUID	9
+#define AUDIT_PERS	10
 
 				/* These are ONLY useful when checking
 				 * at syscall exit time (AUDIT_AT_EXIT). */
@@ -73,7 +74,14 @@
 #define AUDIT_DEVMINOR	101
 #define AUDIT_INODE	102
 #define AUDIT_EXIT	103
-#define AUDIT_NOTEXIT	104
+#define AUDIT_SUCCESS   104	/* exit >= 0; value ignored */
+
+#define AUDIT_ARG0      200
+#define AUDIT_ARG1      (AUDIT_ARG0+1)
+#define AUDIT_ARG2      (AUDIT_ARG0+2)
+#define AUDIT_ARG3      (AUDIT_ARG0+3)
+
+#define AUDIT_NEGATE    0x80000000
 
 
 /* Status symbols */
@@ -134,8 +142,9 @@ struct audit_context;
 extern int  audit_alloc(struct task_struct *task);
 extern void audit_free(struct task_struct *task);
 extern void audit_syscall_entry(struct task_struct *task,
-				int major, int minor);
-extern void audit_syscall_exit(struct task_struct *task);
+				int major, unsigned long a0, unsigned long a1,
+				unsigned long a2, unsigned long a3);
+extern void audit_syscall_exit(struct task_struct *task, int return_code);
 extern void audit_getname(const char *name);
 extern void audit_putname(const char *name);
 extern void audit_inode(const char *name, unsigned long ino, dev_t rdev);
@@ -149,8 +158,8 @@ extern int  audit_set_loginuid(struct au
 #else
 #define audit_alloc(t) ({ 0; })
 #define audit_free(t) do { ; } while (0)
-#define audit_syscall_entry(t,a,b) do { ; } while (0)
-#define audit_syscall_exit(t) do { ; } while (0)
+#define audit_syscall_entry(t,a,b,c,d,e) do { ; } while (0)
+#define audit_syscall_exit(t,r) do { ; } while (0)
 #define audit_getname(n) do { ; } while (0)
 #define audit_putname(n) do { ; } while (0)
 #define audit_inode(n,i,d) do { ; } while (0)

diff -rupP --exclude-from=ignore linux-2.6.4-pristine/kernel/auditsc.c linux-2.6.4/kernel/auditsc.c

kernel/auditsc.c +101 -69
@@ -99,7 +99,9 @@ struct audit_context {
 	struct timespec	    ctime;      /* time of syscall entry */
 	uid_t		    loginuid;   /* login uid (identity) */
 	int		    major;      /* syscall number */
-	int		    minor;      /* function for multiplex syscalls */
+	unsigned long	    argv[4];    /* syscall arguments */
+	int		    return_valid; /* return code is valid */
+	int		    return_code;/* syscall return code */
 	int		    auditable;  /* 1 if record should be written */
 	int		    name_count;
 	struct audit_names  names[AUDIT_NAMES];
@@ -109,6 +111,7 @@ struct audit_context {
 	pid_t		    pid;
 	uid_t		    uid, euid, suid, fsuid;
 	gid_t		    gid, egid, sgid, fsgid;
+	unsigned long	    personality;
 
 #if AUDIT_DEBUG
 	int		    put_count;
@@ -231,17 +234,17 @@ int audit_receive_filter(int type, int p
 
 	switch (type) {
 	case AUDIT_LIST:
-		rcu_read_lock();
-		list_for_each_entry_rcu(entry, &audit_tsklist, list)
+		/* The *_rcu iterators not needed here because we are
+		   always called with audit_netlink_sem held. */ 
+		list_for_each_entry(entry, &audit_tsklist, list)
 			audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
 					 &entry->rule, sizeof(entry->rule));
-		list_for_each_entry_rcu(entry, &audit_entlist, list)
+		list_for_each_entry(entry, &audit_entlist, list)
 			audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
 					 &entry->rule, sizeof(entry->rule));
-		list_for_each_entry_rcu(entry, &audit_extlist, list)
+		list_for_each_entry(entry, &audit_extlist, list)
 			audit_send_reply(pid, seq, AUDIT_LIST, 0, 1,
 					 &entry->rule, sizeof(entry->rule));
-		rcu_read_unlock();
 		audit_send_reply(pid, seq, AUDIT_LIST, 1, 1, NULL, 0);
 		break;
 	case AUDIT_ADD:
@@ -286,94 +289,100 @@ static int audit_filter_rules(struct tas
 			      enum audit_state *state)
 {
 	int i, j;
-	int found;
 
 	for (i = 0; i < rule->field_count; i++) {
-		u32 value = rule->values[i];
+		u32 field  = rule->fields[i] & ~AUDIT_NEGATE;
+		u32 value  = rule->values[i];
+		int result = 0;
 
-		switch (rule->fields[i]) {
+		switch (field) {
 		case AUDIT_PID:
-			if (tsk->pid   != value)
-				return 0;
+			result = (tsk->pid == value);
 			break;
 		case AUDIT_UID:
-			if (tsk->uid   != value)
-				return 0;
+			result = (tsk->uid == value);
 			break;
 		case AUDIT_EUID:
-			if (tsk->euid  != value)
-				return 0;
+			result = (tsk->euid == value);
 			break;
 		case AUDIT_SUID:
-			if (tsk->suid  != value)
-				return 0;
+			result = (tsk->suid == value);
 			break;
 		case AUDIT_FSUID:
-			if (tsk->fsuid != value)
-				return 0;
+			result = (tsk->fsuid == value);
 			break;
 		case AUDIT_GID:
-			if (tsk->gid   != value)
-				return 0;
+			result = (tsk->gid == value);
 			break;
 		case AUDIT_EGID:
-			if (tsk->egid  != value)
-				return 0;
+			result = (tsk->egid == value);
 			break;
 		case AUDIT_SGID:
-			if (tsk->sgid  != value)
-				return 0;
+			result = (tsk->sgid == value);
 			break;
 		case AUDIT_FSGID:
-			if (tsk->fsgid != value)
-				return 0;
+			result = (tsk->fsgid == value);
 			break;
+		case AUDIT_PERS:
+			result = (tsk->personality == value);
+			break;
+
 		case AUDIT_EXIT:
-			if (tsk->exit_code!=value)
-				return 0;
+			if (ctx && ctx->return_valid)
+				result = (ctx->return_code == value);
 			break;
-		case AUDIT_NOTEXIT:
-			if (tsk->exit_code==value)
-				return 0;
+		case AUDIT_SUCCESS:
+			if (ctx && ctx->return_valid)
+				result = (ctx->return_code >= 0);
 			break;
 		case AUDIT_DEVMAJOR:
-			if (!ctx)
-				return 0;
-			found = 0;
-			for (j = 0; !found && j < ctx->name_count; j++) {
-				if (MAJOR(ctx->names[j].rdev) == value)
-					++found;
+			if (ctx) {
+				for (j = 0; j < ctx->name_count; j++) {
+					if (MAJOR(ctx->names[j].rdev)==value) {
+						++result;
+						break;
+					}
+				}
 			}
-			if (!found)
-				return 0;
 			break;
 		case AUDIT_DEVMINOR:
-			if (!ctx)
-				return 0;
-			found = 0;
-			for (j = 0; !found && j < ctx->name_count; j++) {
-				if (MINOR(ctx->names[j].rdev) == value)
-					++found;
+			if (ctx) {
+				for (j = 0; j < ctx->name_count; j++) {
+					if (MINOR(ctx->names[j].rdev)==value) {
+						++result;
+						break;
+					}
+				}
 			}
-			if (!found)
-				return 0;
 			break;
 		case AUDIT_INODE:
-			if (!ctx)
-				return 0;
-			found = 0;
-			for (j = 0; !found && j < ctx->name_count; j++) {
-				if (ctx->names[j].ino == value)
-					++found;
+			if (ctx) {
+				for (j = 0; j < ctx->name_count; j++) {
+					if (MINOR(ctx->names[j].ino)==value) {
+						++result;
+						break;
+					}
+				}
 			}
-			if (!found)
-				return 0;
 			break;
 		case AUDIT_LOGINUID:
-			if (!ctx || ctx->loginuid != value)
-				return 0;
+			result = 0;
+			if (ctx)
+				result = (ctx->loginuid == value);
+			break;
+		case AUDIT_ARG0:
+		case AUDIT_ARG1:
+		case AUDIT_ARG2:
+		case AUDIT_ARG3:
+			if (ctx)
+				result = (ctx->argv[field-AUDIT_ARG0]==value);
 			break;
 		}
+
+		if (rule->fields[i] & AUDIT_NEGATE)
+			result = !result;
+		if (!result)
+			return 0;
 	}
 	switch (rule->action) {
 	case AUDIT_NEVER:    *state = AUDIT_DISABLED;	    break;
@@ -430,12 +439,17 @@ static enum audit_state audit_filter_sys
 }
 
 /* This should be called with task_lock() held. */
-static inline struct audit_context *audit_get_context(struct task_struct *tsk)
+static inline struct audit_context *audit_get_context(struct task_struct *tsk,
+						      int return_valid,
+						      int return_code)
 {
 	struct audit_context *context = tsk->audit_context;
 
 	if (likely(!context))
 		return NULL;
+	context->return_valid = return_valid;
+	context->return_code  = return_code;
+
 	if (context->in_syscall && !context->auditable) {
 		enum audit_state state;
 		state = audit_filter_syscall(tsk, context, &audit_extlist);
@@ -452,6 +466,7 @@ static inline struct audit_context *audi
 	context->egid = tsk->egid;
 	context->sgid = tsk->sgid;
 	context->fsgid = tsk->fsgid;
+	context->personality = tsk->personality;
 	tsk->audit_context = NULL;
 	return context;
 }
@@ -565,14 +580,25 @@ static inline void audit_free_context(st
 static void audit_log_exit(struct audit_context *context)
 {
 	int i;
+	struct audit_buffer *ab;
 
-	audit_log(context,
-		  "syscall=%d,0x%x items=%d"
+	ab = audit_log_start(context);
+	if (!ab)
+		return;		/* audit_panic has been called */
+	audit_log_format(ab, "syscall=%d", context->major);
+	if (context->personality != PER_LINUX)
+		audit_log_format(ab, " per=%lx", context->personality);
+	if (context->return_valid)
+		audit_log_format(ab, " exit=%u", context->return_code);
+	audit_log_format(ab,
+		  " a0=%lx a1=%lx a2=%lx a3=%lx items=%d"
 		  " pid=%d loginuid=%d uid=%d gid=%d"
 		  " euid=%d suid=%d fsuid=%d"
 		  " egid=%d sgid=%d fsgid=%d",
-		  context->major,
-		  context->minor,
+		  context->argv[0],
+		  context->argv[1],
+		  context->argv[2],
+		  context->argv[3],
 		  context->name_count,
 		  context->pid,
 		  context->loginuid,
@@ -580,8 +606,9 @@ static void audit_log_exit(struct audit_
 		  context->gid,
 		  context->euid, context->suid, context->fsuid,
 		  context->egid, context->sgid, context->fsgid);
+	audit_log_end(ab);
 	for (i = 0; i < context->name_count; i++) {
-		struct audit_buffer  *ab = audit_log_start(context);
+		ab = audit_log_start(context);
 		if (!ab)
 			continue; /* audit_panic has been called */
 		audit_log_format(ab, "item=%d", i);
@@ -608,7 +635,7 @@ void audit_free(struct task_struct *tsk)
 	struct audit_context *context;
 
 	task_lock(tsk);
-	context = audit_get_context(tsk);
+	context = audit_get_context(tsk, 0, 0);
 	task_unlock(tsk);
 
 	if (likely(!context))
@@ -659,7 +686,9 @@ static inline unsigned int audit_serial(
  * then the record will be written at syscall exit time (otherwise, it
  * will only be written if another part of the kernel requests that it
  * be written). */
-void audit_syscall_entry(struct task_struct *tsk, int major, int minor)
+void audit_syscall_entry(struct task_struct *tsk, int major,
+			 unsigned long a1, unsigned long a2,
+			 unsigned long a3, unsigned long a4)
 {
 	struct audit_context *context = tsk->audit_context;
 	enum audit_state     state;
@@ -712,7 +741,10 @@ void audit_syscall_entry(struct task_str
 		return;
 
 	context->major      = major;
-	context->minor      = minor; /* Only valid for some calls */
+	context->argv[0]    = a1;
+	context->argv[1]    = a2;
+	context->argv[2]    = a3;
+	context->argv[3]    = a4;
 
 	state = context->state;
 	if (state == AUDIT_SETUP_CONTEXT || state == AUDIT_BUILD_CONTEXT)
@@ -731,13 +763,13 @@ void audit_syscall_entry(struct task_str
  * filtering, or because some other part of the kernel write an audit
  * message), then write out the syscall information.  In call cases,
  * free the names stored from getname(). */
-void audit_syscall_exit(struct task_struct *tsk)
+void audit_syscall_exit(struct task_struct *tsk, int return_code)
 {
 	struct audit_context *context;
 
 	get_task_struct(tsk);
 	task_lock(tsk);
-	context = audit_get_context(tsk);
+	context = audit_get_context(tsk, 1, return_code);
 	task_unlock(tsk);
 
 	/* Not having a context here is ok, since the parent may have