2023-12-21 12:17:08

by Michael Ellerman

[permalink] [raw]
Subject: Re: [PATCH 1/3] init: Declare rodata_enabled and mark_rodata_ro() at all time

Cc +Kees

Christophe Leroy <[email protected]> writes:
> Declaring rodata_enabled and mark_rodata_ro() at all time
> helps removing related #ifdefery in C files.
>
> Signed-off-by: Christophe Leroy <[email protected]>
> ---
> include/linux/init.h | 4 ----
> init/main.c | 21 +++++++--------------
> 2 files changed, 7 insertions(+), 18 deletions(-)
>
> diff --git a/include/linux/init.h b/include/linux/init.h
> index 01b52c9c7526..d2b47be38a07 100644
> --- a/include/linux/init.h
> +++ b/include/linux/init.h
> @@ -168,12 +168,8 @@ extern initcall_entry_t __initcall_end[];
>
> extern struct file_system_type rootfs_fs_type;
>
> -#if defined(CONFIG_STRICT_KERNEL_RWX) || defined(CONFIG_STRICT_MODULE_RWX)
> extern bool rodata_enabled;
> -#endif
> -#ifdef CONFIG_STRICT_KERNEL_RWX
> void mark_rodata_ro(void);
> -#endif
>
> extern void (*late_time_init)(void);
>
> diff --git a/init/main.c b/init/main.c
> index e24b0780fdff..807df08c501f 100644
> --- a/init/main.c
> +++ b/init/main.c
> @@ -1396,10 +1396,9 @@ static int __init set_debug_rodata(char *str)
> early_param("rodata", set_debug_rodata);
> #endif
>
> -#ifdef CONFIG_STRICT_KERNEL_RWX
> static void mark_readonly(void)
> {
> - if (rodata_enabled) {
> + if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX) && rodata_enabled) {
> /*
> * load_module() results in W+X mappings, which are cleaned
> * up with call_rcu(). Let's make sure that queued work is
> @@ -1409,20 +1408,14 @@ static void mark_readonly(void)
> rcu_barrier();
> mark_rodata_ro();
> rodata_test();
> - } else
> + } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) {
> pr_info("Kernel memory protection disabled.\n");
> + } else if (IS_ENABLED(CONFIG_ARCH_HAS_STRICT_KERNEL_RWX)) {
> + pr_warn("Kernel memory protection not selected by kernel config.\n");
> + } else {
> + pr_warn("This architecture does not have kernel memory protection.\n");
> + }
> }
> -#elif defined(CONFIG_ARCH_HAS_STRICT_KERNEL_RWX)
> -static inline void mark_readonly(void)
> -{
> - pr_warn("Kernel memory protection not selected by kernel config.\n");
> -}
> -#else
> -static inline void mark_readonly(void)
> -{
> - pr_warn("This architecture does not have kernel memory protection.\n");
> -}
> -#endif
>
> void __weak free_initmem(void)
> {
> --
> 2.41.0


2023-12-22 05:35:35

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH 1/3] init: Declare rodata_enabled and mark_rodata_ro() at all time



On December 21, 2023 4:16:56 AM PST, Michael Ellerman <[email protected]> wrote:
>Cc +Kees
>
>Christophe Leroy <[email protected]> writes:
>> Declaring rodata_enabled and mark_rodata_ro() at all time
>> helps removing related #ifdefery in C files.
>>
>> Signed-off-by: Christophe Leroy <[email protected]>
>> ---
>> include/linux/init.h | 4 ----
>> init/main.c | 21 +++++++--------------
>> 2 files changed, 7 insertions(+), 18 deletions(-)
>>
>> diff --git a/include/linux/init.h b/include/linux/init.h
>> index 01b52c9c7526..d2b47be38a07 100644
>> --- a/include/linux/init.h
>> +++ b/include/linux/init.h
>> @@ -168,12 +168,8 @@ extern initcall_entry_t __initcall_end[];
>>
>> extern struct file_system_type rootfs_fs_type;
>>
>> -#if defined(CONFIG_STRICT_KERNEL_RWX) || defined(CONFIG_STRICT_MODULE_RWX)
>> extern bool rodata_enabled;
>> -#endif
>> -#ifdef CONFIG_STRICT_KERNEL_RWX
>> void mark_rodata_ro(void);
>> -#endif
>>
>> extern void (*late_time_init)(void);
>>
>> diff --git a/init/main.c b/init/main.c
>> index e24b0780fdff..807df08c501f 100644
>> --- a/init/main.c
>> +++ b/init/main.c
>> @@ -1396,10 +1396,9 @@ static int __init set_debug_rodata(char *str)
>> early_param("rodata", set_debug_rodata);
>> #endif
>>
>> -#ifdef CONFIG_STRICT_KERNEL_RWX
>> static void mark_readonly(void)
>> {
>> - if (rodata_enabled) {
>> + if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX) && rodata_enabled) {

I think this will break without rodata_enabled actual existing on other architectures. (Only declaration was made visible, not the definition, which is above here and still behind ifdefs?)

-Kees

>> /*
>> * load_module() results in W+X mappings, which are cleaned
>> * up with call_rcu(). Let's make sure that queued work is
>> @@ -1409,20 +1408,14 @@ static void mark_readonly(void)
>> rcu_barrier();
>> mark_rodata_ro();
>> rodata_test();
>> - } else
>> + } else if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX)) {
>> pr_info("Kernel memory protection disabled.\n");
>> + } else if (IS_ENABLED(CONFIG_ARCH_HAS_STRICT_KERNEL_RWX)) {
>> + pr_warn("Kernel memory protection not selected by kernel config.\n");
>> + } else {
>> + pr_warn("This architecture does not have kernel memory protection.\n");
>> + }
>> }
>> -#elif defined(CONFIG_ARCH_HAS_STRICT_KERNEL_RWX)
>> -static inline void mark_readonly(void)
>> -{
>> - pr_warn("Kernel memory protection not selected by kernel config.\n");
>> -}
>> -#else
>> -static inline void mark_readonly(void)
>> -{
>> - pr_warn("This architecture does not have kernel memory protection.\n");
>> -}
>> -#endif
>>
>> void __weak free_initmem(void)
>> {
>> --
>> 2.41.0

--
Kees Cook

2023-12-22 18:23:31

by Christophe Leroy

[permalink] [raw]
Subject: Re: [PATCH 1/3] init: Declare rodata_enabled and mark_rodata_ro() at all time



Le 22/12/2023 à 06:35, Kees Cook a écrit :
> [Vous ne recevez pas souvent de courriers de [email protected]. Découvrez pourquoi ceci est important à https://aka.ms/LearnAboutSenderIdentification ]
>
> On December 21, 2023 4:16:56 AM PST, Michael Ellerman <[email protected]> wrote:
>> Cc +Kees
>>
>> Christophe Leroy <[email protected]> writes:
>>> Declaring rodata_enabled and mark_rodata_ro() at all time
>>> helps removing related #ifdefery in C files.
>>>
>>> Signed-off-by: Christophe Leroy <[email protected]>
>>> ---
>>> include/linux/init.h | 4 ----
>>> init/main.c | 21 +++++++--------------
>>> 2 files changed, 7 insertions(+), 18 deletions(-)
>>>
>>> diff --git a/include/linux/init.h b/include/linux/init.h
>>> index 01b52c9c7526..d2b47be38a07 100644
>>> --- a/include/linux/init.h
>>> +++ b/include/linux/init.h
>>> @@ -168,12 +168,8 @@ extern initcall_entry_t __initcall_end[];
>>>
>>> extern struct file_system_type rootfs_fs_type;
>>>
>>> -#if defined(CONFIG_STRICT_KERNEL_RWX) || defined(CONFIG_STRICT_MODULE_RWX)
>>> extern bool rodata_enabled;
>>> -#endif
>>> -#ifdef CONFIG_STRICT_KERNEL_RWX
>>> void mark_rodata_ro(void);
>>> -#endif
>>>
>>> extern void (*late_time_init)(void);
>>>
>>> diff --git a/init/main.c b/init/main.c
>>> index e24b0780fdff..807df08c501f 100644
>>> --- a/init/main.c
>>> +++ b/init/main.c
>>> @@ -1396,10 +1396,9 @@ static int __init set_debug_rodata(char *str)
>>> early_param("rodata", set_debug_rodata);
>>> #endif
>>>
>>> -#ifdef CONFIG_STRICT_KERNEL_RWX
>>> static void mark_readonly(void)
>>> {
>>> - if (rodata_enabled) {
>>> + if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX) && rodata_enabled) {
>
> I think this will break without rodata_enabled actual existing on other architectures. (Only declaration was made visible, not the definition, which is above here and still behind ifdefs?)

The compiler constant-folds IS_ENABLED(CONFIG_STRICT_KERNEL_RWX).
When it is false, the second part is dropped.

Exemple:

bool test(void)
{
if (IS_ENABLED(CONFIG_STRICT_KERNEL_RWX) && rodata_enabled)
return true;
else
return false;
}

With CONFIG_STRICT_KERNEL_RWX set, it directly returns the content of
rodata_enabled:

00000160 <test>:
160: 3d 20 00 00 lis r9,0
162: R_PPC_ADDR16_HA rodata_enabled
164: 88 69 00 00 lbz r3,0(r9)
166: R_PPC_ADDR16_LO rodata_enabled
168: 4e 80 00 20 blr

With CONFIG_STRICT_KERNEL_RWX unset, it returns 0 and doesn't reference
rodata_enabled at all:

000000bc <test>:
bc: 38 60 00 00 li r3,0
c0: 4e 80 00 20 blr

Many places in the kernel use this approach to minimise amount of #ifdefs.

Christophe