[PATCH v2 23/23] x86: dynamic_debug: protect against dynamic debug identifier reuse

From: Rasmus Villemoes
Date: Tue Oct 09 2018 - 07:20:58 EST


Most invocations of DEFINE_DYNAMIC_DEBUG_METADATA happen through
"public" macros such as pr_debug or netdev_dbg, which have been updated
to ensure they pass a unique identifier to use as the name for the
struct _ddebug instance. But it is still possible that someone invokes
DEFINE_DYNAMIC_DEBUG_METADATA directly, or creates a new wrapper macro
that does not do the extra-level-of-macros-passing-on-a__UNIQUE_ID
dance. On x86-64, all subsequent uses of that same identifier would
silently reuse the first instance, which is bad.

But we can catch such cases by defining a guard symbol that is unique
per expansion of DEFINE_DYNAMIC_DEBUG_METADATA. This still allows gcc to
emit multiple copies of some static inline function that has a pr_debug
call (because all such copies would pass the same value of %5 to the
assembler), but prevents repeated naked
DEFINE_DYNAMIC_DEBUG_METADATA(descriptor, "bla") - even with a helpful
error message saying that '"descriptor" used as _ddebug identifer more
than once'.

Acked-by: Jason Baron <jbaron@xxxxxxxxxx>
Signed-off-by: Rasmus Villemoes <linux@xxxxxxxxxxxxxxxxxx>
---
arch/x86/include/asm/dynamic_debug.h | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/arch/x86/include/asm/dynamic_debug.h b/arch/x86/include/asm/dynamic_debug.h
index 350ef6e2feff..69167f40482c 100644
--- a/arch/x86/include/asm/dynamic_debug.h
+++ b/arch/x86/include/asm/dynamic_debug.h
@@ -26,10 +26,15 @@
"\t.long 0 \t# <padding>\n" \
_DPRINTK_ASM_KEY_INIT \
".popsection\n" \
+ ".set "__stringify(name)".ddebug.once, %c5\n" \
+ ".elseif "__stringify(name)".ddebug.once - %c5\n" \
+ ".line "__stringify(__LINE__) " - 1\n" \
+ ".error \"'"__stringify(name)"' used as _ddebug identifier more than once\"\n" \
".endif\n" \
: : "i" (KBUILD_MODNAME), "i" (__func__), \
"i" (__FILE__), "i" (fmt), \
- "i" (_DPRINTK_FLAGS_LINENO_INIT))
+ "i" (_DPRINTK_FLAGS_LINENO_INIT), \
+ "i" (__COUNTER__))

#endif /* _ASM_X86_DYNAMIC_DEBUG_H */

--
2.19.1.3.g1d92a00e68