Skip to contentSkip to conversation

init: verify that function is initcall_t at compile-time

PATCH Applied Marco Elver 4 participants 4 messages May 21, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 init: verify that function is initcall_t at compile-time

In the spirit of making it hard to misuse an interface, add a compile-time assertion in the CONFIG_HAVE_ARCH_PREL32_RELOCATIONS case to verify the initcall function matches initcall_t, because the inline asm bypasses any type-checking the compiler would otherwise do. This will help developers catch incorrect API use in all configurations.

A recent example of this is:
https://lkml.kernel.org/r/[email protected]

---
 include/linux/init.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/init.h +2 -1
index 045ad1650ed1..d82b4b2e1d25 100644
@@ -242,7 +242,8 @@ extern bool initcall_debug;
 	asm(".section	\"" __sec "\", \"a\"		\n"	\
 	    __stringify(__name) ":			\n"	\
 	    ".long	" __stringify(__stub) " - .	\n"	\
-	    ".previous					\n");
+	    ".previous					\n");	\
+	static_assert(__same_type(initcall_t, &fn));
 #else
 #define ____define_initcall(fn, __unused, __name, __sec)	\
 	static initcall_t __name __used 			\
Signature · 1 line
2.31.1.818.g46aad6cb9e-goog
Reviewed-by on v1 1/1
#1 lore raw

Hi Marco,

Quoted 23 lines · Marco Elver

In the spirit of making it hard to misuse an interface, add a compile-time assertion in the CONFIG_HAVE_ARCH_PREL32_RELOCATIONS case to verify the initcall function matches initcall_t, because the inline asm bypasses any type-checking the compiler would otherwise do. This will help developers catch incorrect API use in all configurations.

A recent example of this is:
https://lkml.kernel.org/r/[email protected]

---
 include/linux/init.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/init.h +0 -0
index 045ad1650ed1..d82b4b2e1d25 100644

include/linux/init.h extern bool initcall_debug; · line 249

Quoted 9 lines
include/linux/init.h +2 -1
@@ -242,7 +242,8 @@ extern bool initcall_debug;
        asm(".section   \"" __sec "\", \"a\"            \n"     \
            __stringify(__name) ":                      \n"     \
            ".long      " __stringify(__stub) " - .     \n"     \
-           ".previous                                  \n");
+           ".previous                                  \n");   \
+       static_assert(__same_type(initcall_t, &fn));
 #else
 #define ____define_initcall(fn, __unused, __name, __sec)       \
        static initcall_t __name __used                         \

This looks like a nice improvement, thank you for sending the patch!

Sami

Tested-by on v1 1/1
#2 lore raw
In reply to Marco Elver
Quoted 6 lines · Marco Elver

In the spirit of making it hard to misuse an interface, add a compile-time assertion in the CONFIG_HAVE_ARCH_PREL32_RELOCATIONS case to verify the initcall function matches initcall_t, because the inline asm bypasses any type-checking the compiler would otherwise do. This will help developers catch incorrect API use in all configurations.

Quoted 7 lines

A recent example of this is:
https://lkml.kernel.org/r/[email protected]

Quoted 21 lines
---
 include/linux/init.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
include/linux/init.h +2 -1
index 045ad1650ed1..d82b4b2e1d25 100644
@@ -242,7 +242,8 @@ extern bool initcall_debug;
 	asm(".section	\"" __sec "\", \"a\"		\n"	\
 	    __stringify(__name) ":			\n"	\
 	    ".long	" __stringify(__stub) " - .	\n"	\
-	    ".previous					\n");
+	    ".previous					\n");	\
+	static_assert(__same_type(initcall_t, &fn));
 #else
 #define ____define_initcall(fn, __unused, __name, __sec)	\
 	static initcall_t __name __used 			\
Signature · 1 line
2.31.1.818.g46aad6cb9e-goog
Applied
#3 lore raw
In reply to Marco Elver
Quoted 8 lines · Marco Elver

In the spirit of making it hard to misuse an interface, add a compile-time assertion in the CONFIG_HAVE_ARCH_PREL32_RELOCATIONS case to verify the initcall function matches initcall_t, because the inline asm bypasses any type-checking the compiler would otherwise do. This will help developers catch incorrect API use in all configurations.

A recent example of this is:
https://lkml.kernel.org/r/[email protected]

Since this touches on CFI, I've taken this patch.

Applied to for-next/clang/features, thanks!

[1/1] init: verify that function is initcall_t at compile-time

      https://git.kernel.org/kees/c/72a12a91a634
Signature · 1 line
Kees Cook