Skip to content

Commit d62e4f2

Browse files
author
Peter Zijlstra
committed
x86/bug: Fix BUG_FORMAT vs KASLR
Encoding a relative NULL pointer doesn't work for KASLR, when the whole kernel image gets shifted, the __bug_table and the target string get shifted by the same amount and the relative offset is preserved. However when the target is an absolute 0 value and the __bug_table gets moved about, the end result in a pointer equivalent to kaslr_offset(), not NULL. Notably, this will generate SHN_UNDEF relocations, and Ard would really like to not have those at all. Use the empty string to denote no-string. Suggested-by: Linus Torvalds <torvalds@linuxfoundation.org> Cc: Ard Biesheuvel <ardb@kernel.org> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
1 parent 860238a commit d62e4f2

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

arch/x86/include/asm/bug.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ extern void __WARN_trap(struct bug_entry *bug, ...);
8686
#ifdef CONFIG_DEBUG_BUGVERBOSE_DETAILED
8787
#define WARN_CONDITION_STR(cond_str) cond_str
8888
#else
89-
#define WARN_CONDITION_STR(cond_str) NULL
89+
#define WARN_CONDITION_STR(cond_str) ""
9090
#endif
9191

9292
#define _BUG_FLAGS(cond_str, ins, flags, extra) \
@@ -103,8 +103,12 @@ do { \
103103
} while (0)
104104

105105
#define ARCH_WARN_ASM(file, line, flags, size) \
106+
".pushsection .rodata.str1.1, \"aMS\", @progbits, 1\n" \
107+
"99:\n" \
108+
"\t.string \"\"\n" \
109+
".popsection\n" \
106110
"1:\t " ASM_UD2 "\n" \
107-
_BUG_FLAGS_ASM("0", file, line, flags, size, "")
111+
_BUG_FLAGS_ASM("99b", file, line, flags, size, "")
108112

109113
#else
110114

lib/bug.c

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,17 @@ static const char *bug_get_format(struct bug_entry *bug)
144144
const char *format = NULL;
145145
#ifdef HAVE_ARCH_BUG_FORMAT
146146
#ifdef CONFIG_GENERIC_BUG_RELATIVE_POINTERS
147-
format = (const char *)&bug->format_disp + bug->format_disp;
147+
/*
148+
* Allow an architecture to:
149+
* - relative encode NULL (difficult vs KASLR);
150+
* - use a literal 0 (there are no valid objects inside
151+
* the __bug_table itself to refer to after all);
152+
* - use an empty string.
153+
*/
154+
if (bug->format_disp)
155+
format = (const char *)&bug->format_disp + bug->format_disp;
156+
if (format && format[0] == '\0')
157+
format = NULL;
148158
#else
149159
format = bug->format;
150160
#endif

0 commit comments

Comments
 (0)