Skip to contentSkip to conversation

experimental kernel patch for FORTIFY_SOURCE in kernel space

PATCH Arjan van de Ven 2 participants 2 messages May 19, 2005 – May 23, 2005

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

Hi,

There is a gcc patch in development (posted to the patches list several times now and getting close to done) that allows gcc to track the size of objects at compile time. The idea is that for code like

void func(char *str)

{
	char buf[20];
	strcpy(buf, str);
}

gcc keeps track of "buf" being 20 bytes in size and then (together with glibc) cause that strcpy() to be replaced by a special strcpy that gets "20" as extra parameter and will cause an abort rather than a buffer overflow if more than 20 bytes get copied.

This works great in Fedora Core 4 for userland (the technology is there in RHEL4 and FC3 as well but not used for everything), and recently I started "porting" this to the kernel.

There is somewhat of an architecture impact so before submitting it on lkml I'd want to ask feedback from more arches than just x86...

Greetings,
Arjan van de Ven

Makefile +1 -1
diff -purN linux-2.6.12-rc4/Makefile linux-fortify/Makefile
@@ -1,7 +1,7 @@
 VERSION = 2
 PATCHLEVEL = 6
 SUBLEVEL = 12
-EXTRAVERSION =-rc4
+EXTRAVERSION =-rc4-fortify
 NAME=Woozy Numbat
 
 # *DOCUMENTATION*
arch/i386/Kconfig.debug +8 -0
diff -purN linux-2.6.12-rc4/arch/i386/Kconfig.debug linux-fortify/arch/i386/Kconfig.debug
@@ -59,6 +59,14 @@ config 4KSTACKS
 	  on the VM subsystem for higher order allocations. This option
 	  will also use IRQ stacks to compensate for the reduced stackspace.
 
+config FORTIFY_SOURCE
+	bool "Enable limited buffer overflow checking"
+	help
+	  If you say Y here the kernel will use a recent gcc feature that
+	  allows several key kernel primitives to check for buffer overflows
+	  when dealing with static buffers. Do not enable this feature unless
+	  you have a very recent gcc (version 4.1 or gccs from FC3, FC4, RHEL4) 
+
 config X86_FIND_SMP_CONFIG
 	bool
 	depends on X86_LOCAL_APIC || X86_VOYAGER
include/asm-i386/string.h +7 -4
diff -purN linux-2.6.12-rc4/include/asm-i386/string.h linux-fortify/include/asm-i386/string.h
@@ -23,11 +23,14 @@
  *		consider these trivial functions to be PD.
  */
 
+
 /* AK: in fact I bet it would be better to move this stuff all out of line.
+ * Arjan: .. or just straight use the gcc built-in versions
  */
 
+
 #define __HAVE_ARCH_STRCPY
-static inline char * strcpy(char * dest,const char *src)
+extern inline char * strcpy(char * dest,const char *src)
 {
 int d0, d1, d2;
 __asm__ __volatile__(
@@ -41,7 +44,7 @@ return dest;
 }
 
 #define __HAVE_ARCH_STRNCPY
-static inline char * strncpy(char * dest,const char *src,size_t count)
+extern inline char * strncpy(char * dest,const char *src,size_t count)
 {
 int d0, d1, d2, d3;
 __asm__ __volatile__(
@@ -60,7 +63,7 @@ return dest;
 }
 
 #define __HAVE_ARCH_STRCAT
-static inline char * strcat(char * dest,const char * src)
+extern inline char * strcat(char * dest,const char * src)
 {
 int d0, d1, d2, d3;
 __asm__ __volatile__(
@@ -77,7 +80,7 @@ return dest;
 }
 
 #define __HAVE_ARCH_STRNCAT
-static inline char * strncat(char * dest,const char * src,size_t count)
+extern inline char * strncat(char * dest,const char * src,size_t count)
 {
 int d0, d1, d2, d3;
 __asm__ __volatile__(
include/asm-i386/uaccess.h +37 -4

57 lines not on this page · raw message

include/linux/compiler-gcc3.h +5 -0
diff -purN linux-2.6.12-rc4/include/linux/compiler-gcc3.h linux-fortify/include/linux/compiler-gcc3.h
@@ -30,3 +30,8 @@
 #define __must_check		__attribute__((warn_unused_result))
 #endif
 
+
+#if  defined(__GNUC_RH_RELEASE__) && __GNUC_MINOR__ >= 4 && __GNUC_PATCHLEVEL__ >=2 
+#define __bos(ptr) __builtin_object_size (ptr, 1)
+#define __bos0(ptr) __builtin_object_size (ptr, 0)
+#endif
include/linux/compiler-gcc4.h +4 -0
diff -purN linux-2.6.12-rc4/include/linux/compiler-gcc4.h linux-fortify/include/linux/compiler-gcc4.h
@@ -14,3 +14,7 @@
 #define __must_check 		__attribute__((warn_unused_result))
 #define __compiler_offsetof(a,b) __builtin_offsetof(a,b)
 
+#if defined(__GNUC_RH_RELEASE__) || __GNUC_MINOR__ >= 1
+#define __bos(ptr) __builtin_object_size (ptr, 1)
+#define __bos0(ptr) __builtin_object_size (ptr, 0)
+#endif
include/linux/compiler.h +9 -0
diff -purN linux-2.6.12-rc4/include/linux/compiler.h linux-fortify/include/linux/compiler.h
@@ -155,4 +155,13 @@ extern void __chk_io_ptr(void __iomem *)
 #define __always_inline inline
 #endif
 
+#ifndef __bos
+#define __bos(x) -1
+#endif
+
+#ifndef __bos0
+#define __bos0(x) -1
+#endif
+
+
 #endif /* __LINUX_COMPILER_H */
include/linux/slab.h +2 -2
diff -purN linux-2.6.12-rc4/include/linux/slab.h linux-fortify/include/linux/slab.h
@@ -73,9 +73,9 @@ struct cache_sizes {
 	kmem_cache_t	*cs_dmacachep;
 };
 extern struct cache_sizes malloc_sizes[];
-extern void *__kmalloc(size_t, unsigned int __nocast);
+extern __attribute__((malloc)) void *__kmalloc(size_t, unsigned int __nocast); 
 
-static inline void *kmalloc(size_t size, unsigned int __nocast flags)
+static inline __attribute__((malloc)) void *kmalloc(size_t size, unsigned int __nocast flags) 
 {
 	if (__builtin_constant_p(size)) {
 		int i = 0;
include/linux/string.h +90 -0

96 lines not on this page · raw message

lib/Makefile +1 -0
diff -purN linux-2.6.12-rc4/lib/Makefile linux-fortify/lib/Makefile
@@ -18,6 +18,7 @@ endif
 lib-$(CONFIG_RWSEM_GENERIC_SPINLOCK) += rwsem-spinlock.o
 lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o
 lib-$(CONFIG_GENERIC_FIND_NEXT_BIT) += find_next_bit.o
+lib-$(CONFIG_FORTIFY_SOURCE) += fortify.o
 obj-$(CONFIG_LOCK_KERNEL) += kernel_lock.o
 
 ifneq ($(CONFIG_HAVE_DEC_LOCK),y) 
lib/fortify.c +292 -0

292 lines not on this page · raw message

lib/string.c +4 -0
diff -purN linux-2.6.12-rc4/lib/string.c linux-fortify/lib/string.c
@@ -91,6 +91,7 @@ EXPORT_SYMBOL(strcpy);
  * count, the remainder of @dest will be padded with %NUL.
  *
  */
+#undef strncpy
 char * strncpy(char * dest,const char *src,size_t count)
 {
 	char *tmp = dest;
@@ -162,6 +163,7 @@ EXPORT_SYMBOL(strcat);
  * Note that in contrast to strncpy, strncat ensures the result is
  * terminated.
  */
+#undef strncat
 char * strncat(char *dest, const char *src, size_t count)
 {
 	char *tmp = dest;
@@ -449,6 +451,7 @@ EXPORT_SYMBOL(strsep);
  *
  * Do not use memset() to access IO space, use memset_io() instead.
  */
+#undef memset
 void * memset(void * s,int c,size_t count)
 {
 	char *xs = (char *) s;
@@ -471,6 +474,7 @@ EXPORT_SYMBOL(memset);
  * You should not use this function to access IO space, use memcpy_toio()
  * or memcpy_fromio() instead.
  */
+#undef memcpy
 void * memcpy(void * dest,const void *src,size_t count)
 {
 	char *tmp = (char *) dest, *s = (char *) src;
#1 lore raw
Quoted 2 lines · Arjan van de Ven

There is somewhat of an architecture impact so before submitting it on lkml I'd want to ask feedback from more arches than just x86...

As discussed in IRC: Put the config option in lib/Kconfig.debug instead of in arch/i386, and we need to do something about the string functions. I just made the versions in lib/string.c weak; I doubt that's the best answer.

Seems to work for me on PPC. Would certainly be nice to make it know about kmalloc() though. I also made __chk_fail use BUG() instead of panic().

arch/i386/Kconfig.debug renamed from arch/i386/Kconfig.debug.fortifyppc +0 -8
@@ -59,14 +59,6 @@ config 4KSTACKS
 	  on the VM subsystem for higher order allocations. This option
 	  will also use IRQ stacks to compensate for the reduced stackspace.
 
-config FORTIFY_SOURCE
-	bool "Enable limited buffer overflow checking"
-	help
-	  If you say Y here the kernel will use a recent gcc feature that
-	  allows several key kernel primitives to check for buffer overflows
-	  when dealing with static buffers. Do not enable this feature unless
-	  you have a very recent gcc (version 4.1 or gccs from FC3, FC4, RHEL4) 
-
 config X86_FIND_SMP_CONFIG
 	bool
 	depends on X86_LOCAL_APIC || X86_VOYAGER
include/asm-ppc/uaccess.h renamed from include/asm-ppc/uaccess.h.fortifyppc +10 -0
@@ -330,8 +330,18 @@ copy_to_user(void __user *to, const void
 	return n;
 }
 
+extern void __chk_fail(void);
+
 static inline unsigned long __copy_from_user(void *to, const void __user *from, unsigned long size)
 {
+#ifdef CONFIG_FORTIFY_SOURCE
+	/* 
+	 * if we know the size of "to" then we can validate that we don't overrun the buffer.
+	 * note that if __nbytes is known at compiletime this check is nicely optimized out
+         */
+	if (__bos0 (to) != (size_t) -1 && size > __bos0 (to))
+		__chk_fail();
+#endif
 	return __copy_tofrom_user((__force void __user *)to, from, size);
 }
 
lib/fortify.c renamed from lib/fortify.c.fortifyppc +1 -1
@@ -29,7 +29,7 @@
 void __chk_fail(void) 
 {
 	printk("** kernel buffer overflow detected via application %s ***\n", current->comm);
-	panic("Aborting!\n");
+	BUG();
 }
 EXPORT_SYMBOL_GPL(__chk_fail);
 
lib/Kconfig.debug renamed from lib/Kconfig.debug.fortifyppc +10 -0
@@ -168,6 +168,16 @@ config DEBUG_FS
 
 	  If unsure, say N.
 
+config FORTIFY_SOURCE
+	bool "Enable limited buffer overflow checking"
+	depends on DEBUG_KERNEL
+	help
+	  If you say Y here the kernel will use a recent gcc feature that
+	  allows several key kernel primitives to check for buffer overflows
+	  when dealing with static buffers. Do not enable this feature unless
+	  you have a very recent gcc (version 4.1 or gccs from FC3, FC4, RHEL4)
+
+
 config FRAME_POINTER
 	bool "Compile the kernel with frame pointers"
 	depends on DEBUG_KERNEL && ((X86 && !X86_64) || CRIS || M68K || M68KNOMMU || FRV)
lib/string.c renamed from lib/string.c.fortifyppc +4 -4
@@ -66,7 +66,7 @@ EXPORT_SYMBOL(strnicmp);
  * @src: Where to copy the string from
  */
 #undef strcpy
-char * strcpy(char * dest,const char *src)
+char *  __attribute__((weak)) strcpy(char * dest,const char *src)
 {
 	char *tmp = dest;
 
@@ -92,7 +92,7 @@ EXPORT_SYMBOL(strcpy);
  *
  */
 #undef strncpy
-char * strncpy(char * dest,const char *src,size_t count)
+char * __attribute__((weak)) strncpy(char * dest,const char *src,size_t count)
 {
 	char *tmp = dest;
 
@@ -139,7 +139,7 @@ EXPORT_SYMBOL(strlcpy);
  * @src: The string to append to it
  */
 #undef strcat
-char * strcat(char * dest, const char * src)
+char * __attribute__((weak)) strcat(char * dest, const char * src)
 {
 	char *tmp = dest;
 
@@ -164,7 +164,7 @@ EXPORT_SYMBOL(strcat);
  * terminated.
  */
 #undef strncat
-char * strncat(char *dest, const char *src, size_t count)
+char * __attribute__((weak)) strncat(char *dest, const char *src, size_t count)
 {
 	char *tmp = dest;
 
Signature · 1 line
dwmw2