2022-11-29 19:44:44

by Nathan Chancellor

[permalink] [raw]
Subject: [PATCH 2/2] modpost: Include '.text.*' in TEXT_SECTIONS

Commit 6c730bfc894f ("modpost: handle -ffunction-sections") added
".text.*" to the OTHER_TEXT_SECTIONS macro to fix certain section
mismatch warnings. Unfortunately, this makes it impossible for modpost
to warn about section mismatchs with LTO, which implies
'-ffunction-sections', as all functions are put in their own
'.text.<func_name>' sections, which may still reference functions in
sections they are not supposed to, such as __init.

Fix this by moving ".text.*" into TEXT_SECTIONS, so that configurations
with '-ffunction-sections' will see warnings about mismatched sections.

Link: https://lore.kernel.org/[email protected]/
Reported-by: Vincent Donnefort <[email protected]>
Signed-off-by: Nathan Chancellor <[email protected]>
---
scripts/mod/modpost.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index 2c80da0220c3..c861beabc128 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -823,10 +823,10 @@ static void check_section(const char *modname, struct elf_info *elf,
#define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS

#define DATA_SECTIONS ".data", ".data.rel"
-#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
+#define TEXT_SECTIONS ".text", ".text.*", ".sched.text", \
".kprobes.text", ".cpuidle.text", ".noinstr.text"
#define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
- ".fixup", ".entry.text", ".exception.text", ".text.*", \
+ ".fixup", ".entry.text", ".exception.text", \
".coldtext", ".softirqentry.text"

#define INIT_SECTIONS ".init.*"
--
2.38.1


2022-11-30 13:14:24

by Vincent Donnefort

[permalink] [raw]
Subject: Re: [PATCH 2/2] modpost: Include '.text.*' in TEXT_SECTIONS

On Tue, Nov 29, 2022 at 12:01:23PM -0700, Nathan Chancellor wrote:
> Commit 6c730bfc894f ("modpost: handle -ffunction-sections") added
> ".text.*" to the OTHER_TEXT_SECTIONS macro to fix certain section
> mismatch warnings. Unfortunately, this makes it impossible for modpost
> to warn about section mismatchs with LTO, which implies
> '-ffunction-sections', as all functions are put in their own
> '.text.<func_name>' sections, which may still reference functions in
> sections they are not supposed to, such as __init.
>
> Fix this by moving ".text.*" into TEXT_SECTIONS, so that configurations
> with '-ffunction-sections' will see warnings about mismatched sections.
>
> Link: https://lore.kernel.org/[email protected]/
> Reported-by: Vincent Donnefort <[email protected]>
> Signed-off-by: Nathan Chancellor <[email protected]>

Thanks!

Tested-by: Vincent Donnefort <[email protected]>

> ---
> scripts/mod/modpost.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
> index 2c80da0220c3..c861beabc128 100644
> --- a/scripts/mod/modpost.c
> +++ b/scripts/mod/modpost.c
> @@ -823,10 +823,10 @@ static void check_section(const char *modname, struct elf_info *elf,
> #define ALL_EXIT_SECTIONS EXIT_SECTIONS, ALL_XXXEXIT_SECTIONS
>
> #define DATA_SECTIONS ".data", ".data.rel"
> -#define TEXT_SECTIONS ".text", ".text.unlikely", ".sched.text", \
> +#define TEXT_SECTIONS ".text", ".text.*", ".sched.text", \
> ".kprobes.text", ".cpuidle.text", ".noinstr.text"
> #define OTHER_TEXT_SECTIONS ".ref.text", ".head.text", ".spinlock.text", \
> - ".fixup", ".entry.text", ".exception.text", ".text.*", \
> + ".fixup", ".entry.text", ".exception.text", \
> ".coldtext", ".softirqentry.text"
>
> #define INIT_SECTIONS ".init.*"
> --
> 2.38.1
>

2022-12-02 14:59:25

by Alexander Lobakin

[permalink] [raw]
Subject: Re: [PATCH 2/2] modpost: Include '.text.*' in TEXT_SECTIONS

From: Nathan Chancellor <[email protected]>
Date: Tue, 29 Nov 2022 12:01:23 -0700

> Commit 6c730bfc894f ("modpost: handle -ffunction-sections") added
> ".text.*" to the OTHER_TEXT_SECTIONS macro to fix certain section
> mismatch warnings. Unfortunately, this makes it impossible for modpost
> to warn about section mismatchs with LTO, which implies
> '-ffunction-sections', as all functions are put in their own
> '.text.<func_name>' sections, which may still reference functions in
> sections they are not supposed to, such as __init.
>
> Fix this by moving ".text.*" into TEXT_SECTIONS, so that configurations
> with '-ffunction-sections' will see warnings about mismatched sections.
>
> Link: https://lore.kernel.org/[email protected]/
> Reported-by: Vincent Donnefort <[email protected]>
> Signed-off-by: Nathan Chancellor <[email protected]>

This revealed a couple issues in the FG-KASLR kernel. None of them
are false-positive although FG-KASLR doesn't merge text.* into one
section in the final vmlinux. Nice!

Reviewed-and-tested-by: Alexander Lobakin <[email protected]>

> ---
> scripts/mod/modpost.c | 4 ++--
> 1 file changed, 2 insertions(+), 2 deletions(-)

[...]

> --
> 2.38.1

Thanks,
Olek