Skip to contentSkip to conversation

Patch for auditing framework

PATCH Rik Faith 1 participant 3 messages Feb 25, 2004 – Feb 26, 2004 1 message not in the archive

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

Rik Faith

author
#0 lore raw

Enclosed is a patch against 2.6.3-bk7 that provides a system call auditing framework usable by SELinux. Comments and testing would be greatly appreciated.

The main goal of this work was to integrate a low-overhead system-call and logging infrastructure with SELinux. There are two main parts, one that is always on (generic logging) and one that you can disable at boot- or run-time (per-system-call auditing).

Logging:
1) Uses a netlink socket for communication with user-space. All messages are logged via the netlink socket if a user-space daemon is listening. If not, the messages are logged via printk to the syslog daemon (by default).
2) Messages can be dropped (optionally) based on message rate or memory use (this isn't fully integrated into the selinux/avc.c part of the patch, but the avc.c code that currently does this can be eliminated).
3) When some part of the kernel generates part of an audit record, the partial record is sent immediately to user-space, AND the system call "auditable" flag is automatically set for that call -- thereby producing extra information at syscall exit (including some file names that might make it easier to debug policy -- see below).

System-call auditing:
1) At task-creation time, an audit context is allocated and linked off the task structure.
2) At syscall entry time, if the audit context exists, information is filled in (syscall number, timestamp, but not arguments).
3) During the system call, calls to getname() and path_lookup() are intercepted. These routines are called when the kernel is actually looking up information that will be used to make the decision about whether the syscall will succeed or fail. A great effort has been made to avoid copying the information that getname generates (a pointer is stored instead), since getname is already making a kernel-private copy of the information. [Note that storing copies of arguments requires complexity and overhead that you arguably don't need. With this patch, for example, if chroot("foo") fails because you are not root, "foo" will not appear in the audit record because the kernel determined the syscall cannot proceed before it ever needed to look up "foo".]
4) At syscall exit time, if the "auditable" flag has been set (e.g., because SELinux generated an avc record), the syscall-part of the audit record is generated, including file names and inode numbers. Some of this information is currently complementary to the information that avc.c generates (e.g., file names and some inode numbers), but some is less complete (e.g., getname doesn't return a fully-qualified path, and this patch does not add the overhead of determining one). [Note that the complete audit record comes to userspace in pieces, which eliminates the need to store messages for long periods inside the kernel.]
5) At task-exit time, the audit context is destroyed.

At steps 1, 2, and 4, simple filtering can be done (e.g., a heavy database role uid might have syscall auditing disabled for performance reasons). The filtering is probably more simple than what is needed, so I'd appreciate feedback here. On the other hand, the framework should rely on SELinux to provide most of the decisions about whether or not to audit a particular event.

A few (obvious) things are not yet implemented:
1) Filtering on syscall personality (i.e., i386 calls on x86_64).
2) Identity tracking (this should be done via the netlink socket, similar to how user-space messages (AUDIT_USER) are handled now).
3) Multi-platform hooks (currently this has only been tested on i386, x86_64, and ppc64).
4) Documentation.

For the brave, I have also attached a small user-space test program and pseudo-daemon (it logs to stdout). No man pages, but some online help. E.g., you can try:
Window 1:
./auditd
Window 2 (you don't have to have the daemon running to try this, but enabled has to be 1):
./auditctl -s
./auditctl -a entry,always -S open
ls
./auditctl -d entry,always -S open

More recent versions may also be available from:
http://people.redhat.com/faith/audit/

arch/i386/kernel/entry.S +3 -3
diff -rupP --exclude-from=ignore linux-2.6.3-pristine/arch/i386/kernel/entry.S linux-2.6.3/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 +8 -0
diff -rupP --exclude-from=ignore linux-2.6.3-pristine/arch/i386/kernel/ptrace.c linux-2.6.3/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,13 @@ 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.3-pristine/arch/ppc64/kernel/entry.S linux-2.6.3/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.3-pristine/arch/ppc64/kernel/ptrace.c linux-2.6.3/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.3-pristine/arch/x86_64/ia32/ia32entry.S linux-2.6.3/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.3-pristine/arch/x86_64/kernel/entry.S linux-2.6.3/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.3-pristine/arch/x86_64/kernel/ptrace.c linux-2.6.3/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.3-pristine/fs/namei.c linux-2.6.3/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))
+            audit_getname(result);
 	return result;
 }
 
@@ -860,6 +863,8 @@ walk_init_root(const char *name, struct 
 
 int 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 path_lookup(const char *name, unsign
 	}
 	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.3-pristine/include/asm-i386/thread_info.h linux-2.6.3/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.3-pristine/include/asm-ppc64/thread_info.h linux-2.6.3/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.3-pristine/include/asm-x86_64/thread_info.h linux-2.6.3/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 +201 -0

201 lines not on this page · raw message

include/linux/fs.h +9 -1
diff -rupP --exclude-from=ignore linux-2.6.3-pristine/include/linux/fs.h linux-2.6.3/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;
@@ -1131,7 +1132,14 @@ 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))
+#define putname(name)                                                   \
+    do {                                                                \
+        if (unlikely(current->audit_context))                           \
+            audit_putname(name);                                        \
+        else                                                            \
+            __putname(name);                                            \
+    } while (0)
 
 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.3-pristine/include/linux/netlink.h linux-2.6.3/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.3-pristine/include/linux/sched.h linux-2.6.3/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.3-pristine/init/Kconfig linux-2.6.3/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 +747 -0

747 lines not on this page · raw message

kernel/auditsc.c +852 -0

852 lines not on this page · raw message

kernel/fork.c +8 -1
diff -rupP --exclude-from=ignore linux-2.6.3-pristine/kernel/fork.c linux-2.6.3/kernel/fork.c
@@ -30,6 +30,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>
@@ -84,6 +85,7 @@ 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);
@@ -935,13 +937,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)))
@@ -1076,6 +1081,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.3-pristine/kernel/Makefile linux-2.6.3/kernel/Makefile
@@ -20,6 +20,8 @@ obj-$(CONFIG_BSD_PROCESS_ACCT) += acct.o
 obj-$(CONFIG_COMPAT) += compat.o
 obj-$(CONFIG_IKCONFIG) += configs.o
 obj-$(CONFIG_IKCONFIG_PROC) += configs.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 +61 -89

318 lines not on this page · raw message

security/selinux/hooks.c +4 -4
diff -rupP --exclude-from=ignore linux-2.6.3-pristine/security/selinux/hooks.c linux-2.6.3/security/selinux/hooks.c
@@ -1299,7 +1299,7 @@ static int post_create(struct inode *dir
 	if (sbsec->behavior == SECURITY_FS_USE_XATTR &&
 	    inode->i_op->setxattr) {
 		/* Use extended attributes. */
-		rc = security_sid_to_context(newsid, &context, &len);
+		rc = security_sid_to_context(newsid, &context, &len, GFP_KERNEL);
 		if (rc) {
 			printk(KERN_WARNING "post_create:  sid_to_context "
 			       "failed, rc=%d (dev=%s ino=%ld)\n",
@@ -2230,7 +2230,7 @@ static int selinux_inode_getsecurity(str
 	if (strcmp(name, XATTR_SELINUX_SUFFIX))
 		return -EOPNOTSUPP;
 
-	rc = security_sid_to_context(isec->sid, &context, &len);
+	rc = security_sid_to_context(isec->sid, &context, &len, GFP_KERNEL);
 	if (rc)
 		return rc;
 
@@ -3076,7 +3076,7 @@ static int selinux_socket_getpeersec(str
 
 	ssec = sock->sk->sk_security;
 	
-	err = security_sid_to_context(ssec->peer_sid, &scontext, &scontext_len);
+	err = security_sid_to_context(ssec->peer_sid, &scontext, &scontext_len, GFP_KERNEL);
 	if (err)
 		goto out;
 
@@ -3741,7 +3741,7 @@ static int selinux_getprocattr(struct ta
 	if (!sid)
 		return 0;
 
-	error = security_sid_to_context(sid, &context, &len);
+	error = security_sid_to_context(sid, &context, &len, GFP_KERNEL);
 	if (error)
 		return error;
 	if (len > size) {
security/selinux/include/avc.h +4 -3
diff -rupP --exclude-from=ignore linux-2.6.3-pristine/security/selinux/include/avc.h linux-2.6.3/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/include/security.h +1 -1
diff -rupP --exclude-from=ignore linux-2.6.3-pristine/security/selinux/include/security.h linux-2.6.3/security/selinux/include/security.h
@@ -45,7 +45,7 @@ int security_change_sid(u32 ssid, u32 ts
 	u16 tclass, u32 *out_sid);
 
 int security_sid_to_context(u32 sid, char **scontext,
-	u32 *scontext_len);
+	u32 *scontext_len, int gfp_mask);
 
 int security_context_to_sid(char *scontext, u32 scontext_len,
 	u32 *out_sid);
security/selinux/selinuxfs.c +3 -3
diff -rupP --exclude-from=ignore linux-2.6.3-pristine/security/selinux/selinuxfs.c linux-2.6.3/security/selinux/selinuxfs.c
@@ -457,7 +457,7 @@ static ssize_t sel_write_create(struct f
 	if (length < 0)
 		goto out2;
 
-	length = security_sid_to_context(newsid, &newcon, &len);
+	length = security_sid_to_context(newsid, &newcon, &len, GFP_KERNEL);
 	if (length < 0)
 		goto out2;
 
@@ -518,7 +518,7 @@ static ssize_t sel_write_relabel(struct 
 	if (length < 0)
 		goto out2;
 
-	length = security_sid_to_context(newsid, &newcon, &len);
+	length = security_sid_to_context(newsid, &newcon, &len, GFP_KERNEL);
 	if (length < 0)
 		goto out2;
 
@@ -577,7 +577,7 @@ static ssize_t sel_write_user(struct fil
 	length = sprintf(buf, "%u", nsids) + 1;
 	ptr = buf + length;
 	for (i = 0; i < nsids; i++) {
-		rc = security_sid_to_context(sids[i], &newcon, &len);
+		rc = security_sid_to_context(sids[i], &newcon, &len, GFP_KERNEL);
 		if (rc) {
 			length = rc;
 			goto out3;
security/selinux/ss/services.c +3 -2
diff -rupP --exclude-from=ignore linux-2.6.3-pristine/security/selinux/ss/services.c linux-2.6.3/security/selinux/ss/services.c
@@ -371,12 +371,13 @@ int context_struct_to_string(struct cont
  * @sid: security identifier, SID
  * @scontext: security context
  * @scontext_len: length in bytes
+ * @gfp_mask: mask for kmalloc (e.g., GFP_ATOMIC, GFP_KERNEL)
  *
  * Write the string representation of the context associated with @sid
  * into a dynamically allocated string of the correct size.  Set @scontext
  * to point to this string and set @scontext_len to the length of the string.
  */
-int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len)
+int security_sid_to_context(u32 sid, char **scontext, u32 *scontext_len, int gfp_mask)
 {
 	struct context *context;
 	int rc = 0;
@@ -386,7 +387,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_mask);
 			strcpy(scontextp, initial_sid_to_string[sid]);
 			*scontext = scontextp;
 			goto out;

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#2 lore raw
Quoted 11 lines · Stephen Smalley
Quoted 3 lines · Rik Faith

Enclosed is a patch against 2.6.3-bk7 that provides a system call auditing framework usable by SELinux. Comments and testing would be greatly appreciated.

I'm not sure we actually need to extend the security_sid_to_context interface, as opposed to just always using GFP_ATOMIC for that allocation. It already uses GFP_ATOMIC within the helper function (context_struct_to_string), since the policy rdlock has been taken, so there is no obvious reason not to change the !ss_initialized case to also use GFP_ATOMIC.

I have made this change. A new patch is at:

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

(Also note that you have to boot with audit=1 to get syscall auditing.)

I'm CC'ing the list because this patch also includes a fix to a length computation that will make the audit record output much more readable:

kernel/audit.c +3 -3
diff -rupP --exclude-from=ignore linux-2.6.3-old/kernel/audit.c linux-2.6.3/kernel/audit.c
@@ -585,17 +585,17 @@ void audit_log_d_path(struct audit_buffe
 	char *p;
 	int  len, avail;
 	
-	if (prefix) audit_log_format(ab, "%s", prefix);
+	if (prefix) audit_log_format(ab, " %s", prefix);
 
 	if (ab->len > 128)
 		audit_log_move(ab);
 	avail = sizeof(ab->tmp) - ab->len;
 	p = d_path(dentry, vfsmnt, ab->tmp + ab->len, avail);
 	if (p == ERR_PTR(-ENAMETOOLONG)) {
-		audit_log_format(ab,"<toolong>");
+		audit_log_format(ab, "<toolong>");
 	} else {
 				/* path isn't at start of buffer */
-		len        = p - (ab->tmp + ab->len);
+		len        = (ab->tmp + sizeof(ab->tmp) - 1) - p;
 		memmove(ab->tmp + ab->len, p, len);
 		ab->len   += len;
 		ab->total += len;

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.

author
#3 lore raw
In reply to Rik Faith
Quoted 11 lines · Rik Faith

For the brave, I have also attached a small user-space test program and pseudo-daemon (it logs to stdout). No man pages, but some online help. E.g., you can try:
Window 1:
./auditd
Window 2 (you don't have to have the daemon running to try this, but enabled has to be 1):
./auditctl -s
./auditctl -a entry,always -S open
ls
./auditctl -d entry,always -S open

The tarball was missing a file, so there is a new version available:

    http://people.redhat.com/faith/audit/auditd-0.2b.tar.gz

Also, to build the example program, you have to pass the location of the patched kernel headers to configure, e.g.:

    ./configure --with-audith=/wrk/selinux/linux-2.6.3/include

Where "/wrk/selinux/linux-2.6.3/include" is the path to the audit.h file.

--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.