All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] init: verify that function is initcall_t at compile-time
@ 2021-05-21  7:26 Marco Elver
  2021-05-21 16:36 ` Sami Tolvanen
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Marco Elver @ 2021-05-21  7:26 UTC (permalink / raw)
  To: elver
  Cc: linux-kernel, keescook, samitolvanen, ojeda, johan, akpm,
	masahiroy, joe, Arnd Bergmann, Paul E. McKenney,
	Nathan Chancellor

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/20210514140015.2944744-1-arnd@kernel.org

Signed-off-by: Marco Elver <elver@google.com>
Reviewed-by: Miguel Ojeda <ojeda@kernel.org>
Reviewed-by: Nathan Chancellor <nathan@kernel.org>
Tested-by: Nathan Chancellor <nathan@kernel.org>
---
 include/linux/init.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/include/linux/init.h b/include/linux/init.h
index 045ad1650ed1..d82b4b2e1d25 100644
--- a/include/linux/init.h
+++ b/include/linux/init.h
@@ -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 			\
-- 
2.31.1.818.g46aad6cb9e-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] init: verify that function is initcall_t at compile-time
  2021-05-21  7:26 [PATCH] init: verify that function is initcall_t at compile-time Marco Elver
@ 2021-05-21 16:36 ` Sami Tolvanen
  2021-05-21 17:32 ` Paul E. McKenney
  2021-05-21 17:59 ` Kees Cook
  2 siblings, 0 replies; 4+ messages in thread
From: Sami Tolvanen @ 2021-05-21 16:36 UTC (permalink / raw)
  To: Marco Elver
  Cc: LKML, Kees Cook, ojeda, johan, Andrew Morton, Masahiro Yamada,
	Joe Perches, Arnd Bergmann, Paul E. McKenney, Nathan Chancellor

Hi Marco,

On Fri, May 21, 2021 at 12:26 AM Marco Elver <elver@google.com> wrote:
>
> 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/20210514140015.2944744-1-arnd@kernel.org
>
> Signed-off-by: Marco Elver <elver@google.com>
> Reviewed-by: Miguel Ojeda <ojeda@kernel.org>
> Reviewed-by: Nathan Chancellor <nathan@kernel.org>
> Tested-by: Nathan Chancellor <nathan@kernel.org>
> ---
>  include/linux/init.h | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/include/linux/init.h b/include/linux/init.h
> index 045ad1650ed1..d82b4b2e1d25 100644
> --- a/include/linux/init.h
> +++ b/include/linux/init.h
> @@ -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!

Reviewed-by: Sami Tolvanen <samitolvanen@google.com>

Sami

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] init: verify that function is initcall_t at compile-time
  2021-05-21  7:26 [PATCH] init: verify that function is initcall_t at compile-time Marco Elver
  2021-05-21 16:36 ` Sami Tolvanen
@ 2021-05-21 17:32 ` Paul E. McKenney
  2021-05-21 17:59 ` Kees Cook
  2 siblings, 0 replies; 4+ messages in thread
From: Paul E. McKenney @ 2021-05-21 17:32 UTC (permalink / raw)
  To: Marco Elver
  Cc: linux-kernel, keescook, samitolvanen, ojeda, johan, akpm,
	masahiroy, joe, Arnd Bergmann, Nathan Chancellor

On Fri, May 21, 2021 at 09:26:10AM +0200, Marco Elver wrote:
> 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/20210514140015.2944744-1-arnd@kernel.org
> 
> Signed-off-by: Marco Elver <elver@google.com>
> Reviewed-by: Miguel Ojeda <ojeda@kernel.org>
> Reviewed-by: Nathan Chancellor <nathan@kernel.org>
> Tested-by: Nathan Chancellor <nathan@kernel.org>

Tested-by: Paul E. McKenney <paulmck@kernel.org>

> ---
>  include/linux/init.h | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/include/linux/init.h b/include/linux/init.h
> index 045ad1650ed1..d82b4b2e1d25 100644
> --- a/include/linux/init.h
> +++ b/include/linux/init.h
> @@ -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 			\
> -- 
> 2.31.1.818.g46aad6cb9e-goog
> 

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] init: verify that function is initcall_t at compile-time
  2021-05-21  7:26 [PATCH] init: verify that function is initcall_t at compile-time Marco Elver
  2021-05-21 16:36 ` Sami Tolvanen
  2021-05-21 17:32 ` Paul E. McKenney
@ 2021-05-21 17:59 ` Kees Cook
  2 siblings, 0 replies; 4+ messages in thread
From: Kees Cook @ 2021-05-21 17:59 UTC (permalink / raw)
  To: Marco Elver
  Cc: Kees Cook, Paul E. McKenney, linux-kernel, ojeda, akpm, joe,
	johan, masahiroy, Nathan Chancellor, Arnd Bergmann, samitolvanen

On Fri, 21 May 2021 09:26:10 +0200, Marco Elver wrote:
> 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/20210514140015.2944744-1-arnd@kernel.org

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

-- 
Kees Cook


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-05-21 18:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-21  7:26 [PATCH] init: verify that function is initcall_t at compile-time Marco Elver
2021-05-21 16:36 ` Sami Tolvanen
2021-05-21 17:32 ` Paul E. McKenney
2021-05-21 17:59 ` Kees Cook

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.