Skip to contentSkip to conversation

fortify: Include more details when reporting overflows

PATCH In review Kees Cook 1 participant 2 messages Feb 16, 2024

Subsystems (5)
lore
Share & tools
lore

Kees Cook

author
#0 lore raw

PATCH 1/1 fortify: Include more details when reporting overflows

When a memcpy() would exceed the length of an entire structure, no detailed WARN would be emitted, making debugging a bit more challenging. Similarly, other buffer overflow reports would have no size information reported.

Always warn for memcpy() overflows, but distinguish between the two cases in the message before continuing (warn-only) or blocking the copy (hard-fail). Additionally add size information to existing overflow reports.

---
Cc: Gustavo A. R. Silva <[email protected]>
Cc: Nathan Chancellor <[email protected]>
Cc: [email protected]

---
 arch/arm/boot/compressed/misc.c |  2 +-
 arch/arm/boot/compressed/misc.h |  2 +-
 arch/x86/boot/compressed/misc.c |  2 +-
 include/linux/fortify-string.h  | 96 ++++++++++++++++++++-------------
 lib/string_helpers.c            |  5 +-
 5 files changed, 64 insertions(+), 43 deletions(-)
arch/arm/boot/compressed/misc.c +1 -1
index 6b4baa6a9a50..c8b978c0cc5a 100644
@@ -154,7 +154,7 @@ decompress_kernel(unsigned long output_start, unsigned long free_mem_ptr_p,
 		putstr(" done, booting the kernel.\n");
 }
 
-void fortify_panic(const char *name)
+void fortify_panic(const char *name, size_t avail, size_t size)
 {
 	error("detected buffer overflow");
 }
arch/arm/boot/compressed/misc.h +1 -1
index 6da00a26ac08..efe3d40d3e61 100644
@@ -10,7 +10,7 @@ void __div0(void);
 void
 decompress_kernel(unsigned long output_start, unsigned long free_mem_ptr_p,
 		  unsigned long free_mem_ptr_end_p, int arch_id);
-void fortify_panic(const char *name);
+void fortify_panic(const char *name, size_t avail, size_t size);
 int atags_to_fdt(void *atag_list, void *fdt, int total_space);
 uint32_t fdt_check_mem_start(uint32_t mem_start, const void *fdt);
 int do_decompress(u8 *input, int len, u8 *output, void (*error)(char *x));
arch/x86/boot/compressed/misc.c +1 -1
index b99e08e6815b..c382419f1e4e 100644
@@ -496,7 +496,7 @@ asmlinkage __visible void *extract_kernel(void *rmode, unsigned char *output)
 	return output + entry_offset;
 }
 
-void fortify_panic(const char *name)
+void fortify_panic(const char *name, size_t avail, size_t size)
 {
 	error("detected buffer overflow");
 }
include/linux/fortify-string.h +58 -38

224 lines not on this page · raw message

lib/string_helpers.c +3 -2
index 606c3099013f..0661c563101e 100644
@@ -1008,9 +1008,10 @@ EXPORT_SYMBOL(__read_overflow2_field);
 void __write_overflow_field(size_t avail, size_t wanted) { }
 EXPORT_SYMBOL(__write_overflow_field);
 
-void fortify_panic(const char *name)
+void fortify_panic(const char *name, size_t avail, size_t size)
 {
-	pr_emerg("detected buffer overflow in %s\n", name);
+	pr_emerg("detected buffer overflow in %s (%zu > %zu)\n",
+		 name, size, avail);
 	BUG();
 }
 EXPORT_SYMBOL(fortify_panic);
Signature · 1 line
2.34.1
author
#1 lore raw
Quoted 11 lines · Kees Cook

When a memcpy() would exceed the length of an entire structure, no detailed WARN would be emitted, making debugging a bit more challenging. Similarly, other buffer overflow reports would have no size information reported.

Always warn for memcpy() overflows, but distinguish between the two cases in the message before continuing (warn-only) or blocking the copy (hard-fail). Additionally add size information to existing overflow reports.

This will need a v2 ... something in my manipulations is triggering a bizarre warning in Clang:

../fs/dlm/rcom.c:490:13: error: member reference type 'int' is not a pointer
490 | memcpy(rc->rc_buf, rc_in->rc_buf, sizeof(struct rcom_lock));
| ~~ ^
../include/linux/fortify-string.h:636:47: note: expanded from macro 'memcpy'
636 | #define memcpy(p, q, s) __fortify_memcpy_chk(p, q, s, \
| ^
../include/linux/fortify-string.h:591:20: note: expanded from macro '__fortify_memcpy_chk'
591 | __underlying_##op(p, q, __fortify_size); \
| ^

I'll track it down...

--
Kees Cook