module: Make .static_call_sites read-only after init
Petr Pavlu
maintainerv2 cover module: Make .static_call_sites read-only after init
Section .static_call_sites holds data structures that need to be sorted and processed only at module load time. The section is never modified afterwards. Make it therefore read-only after module initialization to avoid any (non-)accidental modifications.
Changes since v1 [1]:
* Rebase the patches. The kernel now has commit 110b1e070f1d ("module:
Don't fail module loading when setting ro_after_init section RO failed") which addresses a previous problem with handling ro_after_init sections.
[1] https://lore.kernel.org/linux-modules/[email protected]/
Petr Pavlu (3):
module: Constify parameters of module_enforce_rwx_sections()
module: Add a separate function to mark sections as read-only after
init
module: Make .static_call_sites read-only after init
kernel/module/internal.h | 7 ++++-- kernel/module/main.c | 18 +++------------ kernel/module/strict_rwx.c | 47 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 53 insertions(+), 19 deletions(-)
base-commit: 848e076317446f9c663771ddec142d7c2eb4cb43
Signature · 1 line
2.43.0
Petr Pavlu
maintainerPATCH v2 1/3 module: Constify parameters of module_enforce_rwx_sections()
Minor cleanup, this is a non-functional change.
--- kernel/module/internal.h | 5 +++-- kernel/module/strict_rwx.c | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-)
kernel/module/internal.h +3 -2
@@ -333,8 +333,9 @@ int module_enable_rodata_ro(const struct module *mod);int module_enable_rodata_ro_after_init (const struct module *mod);int module_enable_data_nx (const struct module *mod);int module_enable_text_rox (const struct module *mod); -int module_enforce_rwx_sections (Elf_Ehdr *hdr,Elf_Shdr *sechdrs, -char *secstrings,struct module *mod); +int module_enforce_rwx_sections (const Elf_Ehdr *hdr,const Elf_Shdr *sechdrs, +const char *secstrings, +const struct module *mod);#ifdef CONFIG_MODULE_SIG int module_sig_check (struct load_info *info,int flags);
kernel/module/strict_rwx.c +3 -2
@@ -86,8 +86,9 @@ int module_enable_data_nx(const struct module *mod)return 0 ; } -int module_enforce_rwx_sections (Elf_Ehdr *hdr,Elf_Shdr *sechdrs, -char *secstrings,struct module *mod) +int module_enforce_rwx_sections (const Elf_Ehdr *hdr,const Elf_Shdr *sechdrs, +const char *secstrings, +const struct module *mod) {const unsigned long shf_wx =SHF_WRITE |SHF_EXECINSTR ;int i;
Signature · 1 line
2.43.0
Petr Pavlu
maintainerPATCH v2 2/3 module: Add a separate function to mark sections as read-only after init
In reply to Petr PavluMove the logic to mark special sections as read-only after module initialization into a separate function, along other related code in strict_rwx.c. Use a table with names of such sections to make it easier to add more.
--- kernel/module/internal.h | 2 ++ kernel/module/main.c | 18 +++--------------- kernel/module/strict_rwx.c | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 15 deletions(-)
kernel/module/internal.h +2 -0
@@ -336,6 +336,8 @@ int module_enable_text_rox(const struct module *mod);int module_enforce_rwx_sections (const Elf_Ehdr *hdr,const Elf_Shdr *sechdrs,const char *secstrings,const struct module *mod); +void module_mark_ro_after_init (const Elf_Ehdr *hdr,Elf_Shdr *sechdrs, +const char *secstrings);#ifdef CONFIG_MODULE_SIG int module_sig_check (struct load_info *info,int flags);
kernel/module/main.c +3 -15
@@ -2791,7 +2791,6 @@ core_param(module_blacklist, module_blacklist, charp, 0400);static struct module *layout_and_allocate (struct load_info *info,int flags) {struct module *mod; -unsigned int ndx;int err;/* Allow arches to frob section contents and sizes. */
@@ -2809,22 +2808,11 @@ static struct module *layout_and_allocate(struct load_info *info, int flags) info->sechdrs[info->index.pcpu].sh_flags &= ~(unsigned long )SHF_ALLOC ;/* -* Mark ro_after_init section with SHF_RO_AFTER_INIT so that -* layout_sections() can put it in the right place. +* Mark relevant sections as SHF_RO_AFTER_INIT so layout_sections() can +* put them in the right place. * Note: ro_after_init sections also have SHF_{WRITE,ALLOC} set. */ - ndx =find_sec (info,".data..ro_after_init" ); -if (ndx) - info->sechdrs[ndx].sh_flags |=SHF_RO_AFTER_INIT ; -/* -* Mark the __jump_table section as ro_after_init as well: these data -* structures are never modified, with the exception of entries that -* refer to code in the __init section, which are annotated as such -* at module load time. -*/ - ndx =find_sec (info,"__jump_table" ); -if (ndx) - info->sechdrs[ndx].sh_flags |=SHF_RO_AFTER_INIT ; +module_mark_ro_after_init (info->hdr, info->sechdrs, info->secstrings);/* * Determine total sizes, and put offsets in sh_entsize. For now
kernel/module/strict_rwx.c +33 -0
@@ -106,3 +106,36 @@ int module_enforce_rwx_sections(const Elf_Ehdr *hdr, const Elf_Shdr *sechdrs,return 0 ; } + +static const char *const ro_after_init[] = { +/* +* Section .data..ro_after_init holds data explicitly annotated by +* __ro_after_init. +*/ +".data..ro_after_init" , + +/* +* Section __jump_table holds data structures that are never modified, +* with the exception of entries that refer to code in the __init +* section, which are marked as such at module load time. +*/ +"__jump_table" , +}; + +void module_mark_ro_after_init (const Elf_Ehdr *hdr,Elf_Shdr *sechdrs, +const char *secstrings) +{ +int i, j; + +for (i =1 ; i < hdr->e_shnum; i++) { +Elf_Shdr *shdr = &sechdrs[i]; + +for (j =0 ; j <ARRAY_SIZE (ro_after_init); j++) { +if (strcmp (secstrings + shdr->sh_name, + ro_after_init[j]) ==0 ) { + shdr->sh_flags |=SHF_RO_AFTER_INIT ; +break ; + } + } + } +}
Signature · 1 line
2.43.0
Petr Pavlu
maintainerPATCH v2 3/3 module: Make .static_call_sites read-only after init
In reply to Petr PavluSection .static_call_sites holds data structures that need to be sorted and processed only at module load time. This initial processing happens in static_call_add_module(), which is invoked as a callback to the MODULE_STATE_COMING notification from prepare_coming_module().
The section is never modified afterwards. Make it therefore read-only after module initialization to avoid any (non-)accidental modifications.
--- kernel/module/strict_rwx.c | 9 +++++++++ 1 file changed, 9 insertions(+)
kernel/module/strict_rwx.c +9 -0
@@ -120,6 +120,15 @@ static const char *const ro_after_init[] = {* section, which are marked as such at module load time. */ "__jump_table" , + +#ifdef CONFIG_HAVE_STATIC_CALL_INLINE +/* +* Section .static_call_sites holds data structures that need to be +* sorted and processed at module load time but are never modified +* afterwards. +*/ + ".static_call_sites", +#endif }; void module_mark_ro_after_init(const Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
Signature · 1 line
2.43.0
Le 06/03/2025 à 14:13, Petr Pavlu a écrit :
Quoted 7 lines
Section .static_call_sites holds data structures that need to be sorted and processed only at module load time. This initial processing happens in
static_call_add_module(), which is invoked as a callback to theMODULE_STATE_COMINGnotification fromprepare_coming_module().The section is never modified afterwards. Make it therefore read-only after module initialization to avoid any (non-)accidental modifications.
Maybe this suggestion is stupid, I didn't investigate the feasability but: why don't we group everything that is ro_after_init in a single section just like we do in vmlinux ? That would avoid having to add every new possible section in the C code.
Like we have in asm-generic/vmlinux.lds.h:
#define RO_AFTER_INIT_DATA \ . = ALIGN(8); \ __start_ro_after_init = .; \ *(.data..ro_after_init) \ JUMP_TABLE_DATA \ STATIC_CALL_DATA \ __end_ro_after_init = .;
Quoted 26 lines
Signed-off-by Petr Pavlu <[email protected]>--- kernel/module/strict_rwx.c | 9 +++++++++ 1 file changed, 9 insertions(+)kernel/module/strict_rwx.c +9
-0@@ -120,6 +120,15 @@ static const char *const ro_after_init[] = {* section, which are marked as such at module load time. */ "__jump_table" , + +#ifdef CONFIG_HAVE_STATIC_CALL_INLINE +/* +* Section .static_call_sites holds data structures that need to be +* sorted and processed at module load time but are never modified +* afterwards. +*/ + ".static_call_sites", +#endif }; void module_mark_ro_after_init(const Elf_Ehdr *hdr, Elf_Shdr *sechdrs,
Sami Tolvanen
maintainerQuoted 18 lines · Christophe Leroy
Le 06/03/2025 à 14:13, Petr Pavlu a écrit :
Quoted 7 lines
Section .static_call_sites holds data structures that need to be sorted and processed only at module load time. This initial processing happens in
static_call_add_module(), which is invoked as a callback to theMODULE_STATE_COMINGnotification fromprepare_coming_module().The section is never modified afterwards. Make it therefore read-only after module initialization to avoid any (non-)accidental modifications.
Maybe this suggestion is stupid, I didn't investigate the feasability but:
why don't we group everything that isro_after_initin a single section just like we do in vmlinux ? That would avoid having to add every new possible section in the C code.Like we have in
asm-generic/vmlinux.lds.h:
Quoted 7 lines
#define RO_AFTER_INIT_DATA \ . = ALIGN(8); \ __start_ro_after_init = .; \ *(.data..ro_after_init) \ JUMP_TABLE_DATA \ STATIC_CALL_DATA \ __end_ro_after_init = .;
I like this idea. Grouping the sections in the module linker script feels cleaner than having an array of section names in the code. To be fair, I think this code predates v5.10, where scripts/module.lds.S was first added.
Sami
Petr Pavlu
maintainerQuoted 25 lines · Sami Tolvanen
Quoted 16 lines · Christophe Leroy
Quoted 7 lines · Petr Pavlu
Section .static_call_sites holds data structures that need to be sorted and processed only at module load time. This initial processing happens in
static_call_add_module(), which is invoked as a callback to theMODULE_STATE_COMINGnotification fromprepare_coming_module().The section is never modified afterwards. Make it therefore read-only after module initialization to avoid any (non-)accidental modifications.
Maybe this suggestion is stupid, I didn't investigate the feasability but:
why don't we group everything that isro_after_initin a single section just like we do in vmlinux ? That would avoid having to add every new possible section in the C code.Like we have in
asm-generic/vmlinux.lds.h:Quoted 7 lines
#define RO_AFTER_INIT_DATA \ . = ALIGN(8); \ __start_ro_after_init = .; \ *(.data..ro_after_init) \ JUMP_TABLE_DATA \ STATIC_CALL_DATA \ __end_ro_after_init = .;
Quoted 4 lines
I like this idea. Grouping the sections in the module linker script feels cleaner than having an array of section names in the code. To be fair, I think this code predates v5.10, where
scripts/module.lds.Swas first added.
I agree in principle. I like that the information about ro_after_init sections for vmlinux and modules would be in the same source form, in linker scripts. This could eventually allow us to share the definition of ro_after_init sections between vmlinux and modules.
The problem is however how to find the location of the __jump_table and static_call_sites data. In vmlinux, as a final binary, they are annotated with start and end symbols. In modules, as relocatable files, the approach is to rely on them being separate sections, see function find_module_sections().
I could add start+end symbols for __jump_table and static_call_sites data in scripts/module.lds.S and use them by the module loader, but this would create an inconsistency in how various data is looked up. Another problem is that I can't find a way to tell the linker to add these symbols only if the specific data is actually present.
Signature · 2 lines
Cheers, Petr
Hi Petr,
Quoted 43 lines · Petr Pavlu
Quoted 25 lines · Sami Tolvanen
Quoted 16 lines · Christophe Leroy
Quoted 7 lines · Petr Pavlu
Section .static_call_sites holds data structures that need to be sorted and processed only at module load time. This initial processing happens in
static_call_add_module(), which is invoked as a callback to theMODULE_STATE_COMINGnotification fromprepare_coming_module().The section is never modified afterwards. Make it therefore read-only after module initialization to avoid any (non-)accidental modifications.
Maybe this suggestion is stupid, I didn't investigate the feasability but:
why don't we group everything that isro_after_initin a single section just like we do in vmlinux ? That would avoid having to add every new possible section in the C code.Like we have in
asm-generic/vmlinux.lds.h:Quoted 7 lines
#define RO_AFTER_INIT_DATA \ . = ALIGN(8); \ __start_ro_after_init = .; \ *(.data..ro_after_init) \ JUMP_TABLE_DATA \ STATIC_CALL_DATA \ __end_ro_after_init = .;Quoted 4 lines
I like this idea. Grouping the sections in the module linker script feels cleaner than having an array of section names in the code. To be fair, I think this code predates v5.10, where
scripts/module.lds.Swas first added.I agree in principle. I like that the information about
ro_after_initsections for vmlinux and modules would be in the same source form, in linker scripts. This could eventually allow us to share the definition ofro_after_initsections between vmlinux and modules.The problem is however how to find the location of the
__jump_tableandstatic_call_sitesdata. In vmlinux, as a final binary, they are annotated with start and end symbols. In modules, as relocatable files, the approach is to rely on them being separate sections, see functionfind_module_sections().
Quoted 3 lines
I could add start+end symbols for
__jump_tableandstatic_call_sitesdata inscripts/module.lds.Sand use them by the module loader, but this would create an inconsistency in how various data is looked up.
That's a fair point. Perhaps it makes sense to keep these sections separate for consistency, and look into cleaning this up later if needed.
Quoted 2 lines
Another problem is that I can't find a way to tell the linker to add these symbols only if the specific data is actually present.
You can use the preprocessor to add the symbols only if the relevant kernel config is present, similarly to how STATIC_CALL_DATA is defined in include/asm-generic/vmlinux.lds.h.
In any case, the code looks correct to me. For the series:
Sami
Petr Pavlu
maintainerQuoted 54 lines · Sami Tolvanen
Hi Petr,
Quoted 43 lines · Petr Pavlu
Quoted 25 lines · Sami Tolvanen
Quoted 16 lines · Christophe Leroy
Quoted 7 lines · Petr Pavlu
Section .static_call_sites holds data structures that need to be sorted and processed only at module load time. This initial processing happens in
static_call_add_module(), which is invoked as a callback to theMODULE_STATE_COMINGnotification fromprepare_coming_module().The section is never modified afterwards. Make it therefore read-only after module initialization to avoid any (non-)accidental modifications.
Maybe this suggestion is stupid, I didn't investigate the feasability but:
why don't we group everything that isro_after_initin a single section just like we do in vmlinux ? That would avoid having to add every new possible section in the C code.Like we have in
asm-generic/vmlinux.lds.h:Quoted 7 lines
#define RO_AFTER_INIT_DATA \ . = ALIGN(8); \ __start_ro_after_init = .; \ *(.data..ro_after_init) \ JUMP_TABLE_DATA \ STATIC_CALL_DATA \ __end_ro_after_init = .;Quoted 4 lines
I like this idea. Grouping the sections in the module linker script feels cleaner than having an array of section names in the code. To be fair, I think this code predates v5.10, where
scripts/module.lds.Swas first added.I agree in principle. I like that the information about
ro_after_initsections for vmlinux and modules would be in the same source form, in linker scripts. This could eventually allow us to share the definition ofro_after_initsections between vmlinux and modules.The problem is however how to find the location of the
__jump_tableandstatic_call_sitesdata. In vmlinux, as a final binary, they are annotated with start and end symbols. In modules, as relocatable files, the approach is to rely on them being separate sections, see functionfind_module_sections().Quoted 3 lines
I could add start+end symbols for
__jump_tableandstatic_call_sitesdata inscripts/module.lds.Sand use them by the module loader, but this would create an inconsistency in how various data is looked up.That's a fair point. Perhaps it makes sense to keep these sections separate for consistency, and look into cleaning this up later if needed.
Quoted 6 lines
Quoted 2 lines
Another problem is that I can't find a way to tell the linker to add these symbols only if the specific data is actually present.
You can use the preprocessor to add the symbols only if the relevant kernel config is present, similarly to how
STATIC_CALL_DATAis defined ininclude/asm-generic/vmlinux.lds.h.
Right, that works but only statically. Let's say I update module.lds.S to:
SECTIONS {
[...]
.data..ro_after_init {
*(.data..ro_after_init)
__start___jump_table = .;
*(__jump_table)
__end___jump_table = .;
#ifdef CONFIG_HAVE_STATIC_CALL_INLINE
__start_static_call_sites = .;
*(.static_call_sites)
__end_static_call_sites = .;
#endif
}
}What I had in mind is that you can configure the kernel with CONFIG_HAVE_STATIC_CALL_INLINE, but some modules might not contain any static calls and so wouldn't have a .static_call_sites section. When using the above script, such modules would still end up with unnecessary symbols __start_static_call_sites and __end_static_call_sites defining an empty range. Worse, if the module doesn't contain any ro_after_init data, the added symbols would force the linker to create an empty .data..ro_after_init section.
Quoted 4 lines
In any case, the code looks correct to me. For the series:
Reviewed-by Sami Tolvanen <[email protected]>
Signature · 2 lines
Thanks, Petr
Quoted 11 lines · Petr Pavlu
Section .static_call_sites holds data structures that need to be sorted and processed only at module load time. The section is never modified afterwards. Make it therefore read-only after module initialization to avoid any (non-)accidental modifications.
Changes since v1 [1]:
* Rebase the patches. The kernel now has commit110b1e070f1d("module:
Don't fail module loading when settingro_after_initsection RO failed") which addresses a previous problem with handlingro_after_initsections.[1] https://lore.kernel.org/linux-modules/[email protected]/
Luis
Quoted 18 lines · Petr Pavlu
Section .static_call_sites holds data structures that need to be sorted and processed only at module load time. The section is never modified afterwards. Make it therefore read-only after module initialization to avoid any (non-)accidental modifications.
Changes since v1 [1]:
* Rebase the patches. The kernel now has commit110b1e070f1d("module:
Don't fail module loading when settingro_after_initsection RO failed") which addresses a previous problem with handlingro_after_initsections.[1] https://lore.kernel.org/linux-modules/[email protected]/
Petr Pavlu (3):
module: Constify parameters ofmodule_enforce_rwx_sections()
module: Add a separate function to mark sections as read-only after
init
module: Make .static_call_sites read-only after init
Quoted 4 lines
kernel/module/internal.h | 7 ++++-- kernel/module/main.c | 18 +++------------ kernel/module/strict_rwx.c | 47 ++++++++++++++++++++++++++++++++++++-- 3 files changed, 53 insertions(+), 19 deletions(-)
Queued now on modules-next, for 6.16-rc1.
-- Petr