Extended MODVERSIONS Support
Subsystems (4)
v5 cover Extended MODVERSIONS Support
This patch series is intended for use alongside the Implement MODVERSIONS for RUST [1] series as a replacement for the symbol name hashing approach used there to enable RUST and MODVERSIONS at the same time.
Elsewhere, we've seen a desire for long symbol name support for LTO symbol names [2], and the previous series came up [3] as a possible solution rather than hashing, which some have objected [4] to.
This series adds a MODVERSIONS format which uses a section per column. This avoids userspace tools breaking if we need to make a similar change to the format in the future - we would do so by adding a new section, rather than editing the struct definition. In the new format, the name section is formatted as a concatenated sequence of NUL-terminated strings, which allows for arbitrary length names.
Currently, this series emits both the extended format and the current format on all modules, and prefers the extended format when checking if present. I'm open to various other policies via Kconfig knobs, but this seemed like a good initial default.
The refactor to MODVERSIONS is prefixed to this series as result of an explicit request [5] by Luis in response to the original patchset.
If you are testing this patch alongside RUST by manually removing the !MODVERSIONS restriction (this series doesn't remove it, because the CRCs don't mean what we'd want them to yet, we need the DWARF patch for that) and have kernel hardening enabled, you may need the CPU Mitigations [6] series. Without it, the foo.mod.o file produced by the C compiler will reference __x86_return_thunk, but foo.o will not. This means that the version table will not contain a version for __x86_return_thunk, but foo.ko will reference it, which will result in a version check failure.
[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lore.kernel.org/lkml/[email protected]/
[4] https://lore.kernel.org/lkml/[email protected]/
[5] https://lore.kernel.org/lkml/ZVZNh%[email protected]/
[6] https://lore.kernel.org/all/[email protected]/
Changes in v5:
- Addresses Sami's comments from v3 that I missed in v4 (missing early return, extra parens)
v4: https://lore.kernel.org/asahi/[email protected]/
- Fix incorrect dot munging in PPC
v3: https://lore.kernel.org/lkml/[email protected]/T/
- Split up the module verification refactor into smaller patches, per Greg K-H's suggestion.
v2: https://lore.kernel.org/all/[email protected]/
- Add loading/verification refactor before modifying, per Luis's request
v1: https://lore.kernel.org/rust-for-linux/[email protected]/
Matthew Maurer (16):
module: Take const arg in validate_section_offset
module: Factor out elf_validity_ehdr
module: Factor out elf_validity_cache_sechdrs
module: Factor out elf_validity_cache_secstrings
module: Factor out elf_validity_cache_index_info
module: Factor out elf_validity_cache_index_mod
module: Factor out elf_validity_cache_index_sym
module: Factor out elf_validity_cache_index_str
module: Group section index calculations together
module: Factor out elf_validity_cache_strtab
module: Additional validation in elf_validity_cache_strtab
module: Reformat struct for code style
export_report: Rehabilitate script
modules: Support extended MODVERSIONS info
modpost: Produce extended modversion information
export_report: Use new version info format
arch/powerpc/kernel/module_64.c | 23 +- kernel/module/internal.h | 18 +- kernel/module/main.c | 647 ++++++++++++++++++++++++-------- kernel/module/version.c | 45 +++ scripts/export_report.pl | 17 +- scripts/mod/modpost.c | 39 +- 6 files changed, 628 insertions(+), 161 deletions(-)
Signature · 1 line
2.46.1.824.gd892dcdcdd-goog
PATCH v5 1/16 module: Take const arg in validate_section_offset
validate_section_offset doesn't modify the info passed in. Make this clear by adjusting the type signature.
--- kernel/module/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/module/main.c +1 -1
@@ -1645,7 +1645,7 @@ bool __weak module_exit_section(const char *name)return strstarts (name,".exit" ); } -static int validate_section_offset (struct load_info *info,Elf_Shdr *shdr) +static int validate_section_offset (const struct load_info *info,Elf_Shdr *shdr) {#if defined(CONFIG_64BIT )unsigned long long secend;
Signature · 1 line
2.46.1.824.gd892dcdcdd-goog
PATCH v5 2/16 module: Factor out elf_validity_ehdr
In reply to Matthew MaurerFactor out verification of the ELF header and document what is checked.
--- kernel/module/main.c | 70 +++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 23 deletions(-)
kernel/module/main.c +47 -23
@@ -1664,6 +1664,50 @@ static int validate_section_offset(const struct load_info *info, Elf_Shdr *shdr)return 0 ; } +/** +* elf_validity_ehdr() - Checks an ELF header for module validity +* @info: Load info containing the ELF header to check +* +* Checks whether an ELF header could belong to a valid module. Checks: +* +* * ELF header is within the data the user provided +* * ELF magic is present +* * It is relocatable (not final linked, not core file, etc.) +* * The header's machine type matches what the architecture expects. +* * Optional arch-specific hook for other properties +* - module_elf_check_arch() is currently only used by PPC to check +* ELF ABI version, but may be used by others in the future. +* +* Return: %0 if valid, %-ENOEXEC on failure. +*/ +static int elf_validity_ehdr (const struct load_info *info) +{ +if (info->len <sizeof (*(info->hdr))) { +pr_err ("Invalid ELF header len %lu\n" , info->len); +return -ENOEXEC ; + } +if (memcmp (info->hdr->e_ident,ELFMAG ,SELFMAG ) !=0 ) { +pr_err ("Invalid ELF header magic: != %s\n" ,ELFMAG ); +return -ENOEXEC ; + } +if (info->hdr->e_type !=ET_REL ) { +pr_err ("Invalid ELF header type: %u != %u\n" , + info->hdr->e_type,ET_REL ); +return -ENOEXEC ; + } +if (!elf_check_arch (info->hdr)) { +pr_err ("Invalid architecture in ELF header: %u\n" , + info->hdr->e_machine); +return -ENOEXEC ; + } +if (!module_elf_check_arch (info->hdr)) { +pr_err ("Invalid module architecture in ELF header: %u\n" , + info->hdr->e_machine); +return -ENOEXEC ; + } +return 0 ; +} +/* * Check userspace passed ELF module against our expectations, and cache * useful variables for further processing as we go.
@@ -1693,30 +1737,10 @@ static int elf_validity_cache_copy(struct load_info *info, int flags)unsigned int num_info_secs =0 , info_idx;unsigned int num_sym_secs =0 , sym_idx; -if (info->len <sizeof (*(info->hdr))) { -pr_err ("Invalid ELF header len %lu\n" , info->len); - goto no_exec; - } + err =elf_validity_ehdr (info); +if (err <0 ) +return err; -if (memcmp (info->hdr->e_ident,ELFMAG ,SELFMAG ) !=0 ) { -pr_err ("Invalid ELF header magic: != %s\n" ,ELFMAG ); - goto no_exec; - } -if (info->hdr->e_type !=ET_REL ) { -pr_err ("Invalid ELF header type: %u != %u\n" , - info->hdr->e_type,ET_REL ); - goto no_exec; - } -if (!elf_check_arch (info->hdr)) { -pr_err ("Invalid architecture in ELF header: %u\n" , - info->hdr->e_machine); - goto no_exec; - } -if (!module_elf_check_arch (info->hdr)) { -pr_err ("Invalid module architecture in ELF header: %u\n" , - info->hdr->e_machine); - goto no_exec; - }if (info->hdr->e_shentsize !=sizeof (Elf_Shdr)) {pr_err ("Invalid ELF section header size\n" ); goto no_exec;
Signature · 1 line
2.46.1.824.gd892dcdcdd-goog
PATCH v5 3/16 module: Factor out elf_validity_cache_sechdrs
In reply to Matthew MaurerFactor out and document the validation of section headers.
Because we now validate all section offsets and lengths before accessing them, we can remove the ad-hoc checks.
--- kernel/module/main.c | 125 ++++++++++++++++++++++++++++--------------- 1 file changed, 82 insertions(+), 43 deletions(-)
kernel/module/main.c +82 -43
@@ -1708,6 +1708,87 @@ static int elf_validity_ehdr(const struct load_info *info)return 0 ; } +/** +* elf_validity_cache_sechdrs() - Cache section headers if valid +* @info: Load info to compute section headers from +* +* Checks: +* +* * ELF header is valid (see elf_validity_ehdr()) +* * Section headers are the size we expect +* * Section array fits in the user provided data +* * Section index 0 is NULL +* * Section contents are inbounds +* +* Then updates @info with a &load_info->sechdrs pointer if valid. +* +* Return: %0 if valid, negative error code if validation failed. +*/ +static int elf_validity_cache_sechdrs (struct load_info *info) +{ +Elf_Shdr *sechdrs; +Elf_Shdr *shdr; +int i; +int err; + + err =elf_validity_ehdr (info); +if (err <0 ) +return err; + +if (info->hdr->e_shentsize !=sizeof (Elf_Shdr)) { +pr_err ("Invalid ELF section header size\n" ); +return -ENOEXEC ; + } + +/* +* e_shnum is 16 bits, and sizeof(Elf_Shdr) is +* known and small. So e_shnum * sizeof(Elf_Shdr) +* will not overflow unsigned long on any platform. +*/ +if (info->hdr->e_shoff >= info->len + || (info->hdr->e_shnum *sizeof (Elf_Shdr) > + info->len - info->hdr->e_shoff)) { +pr_err ("Invalid ELF section header overflow\n" ); +return -ENOEXEC ; + } + + sechdrs = (void *)info->hdr + info->hdr->e_shoff; + +/* +* The code assumes that section 0 has a length of zero and +* an addr of zero, so check for it. +*/ +if (sechdrs[0 ].sh_type !=SHT_NULL + || sechdrs[0 ].sh_size !=0 + || sechdrs[0 ].sh_addr !=0 ) { +pr_err ("ELF Spec violation: section 0 type(%d)!=SH_NULL or non-zero len or addr\n" , + sechdrs[0 ].sh_type); +return -ENOEXEC ; + } + +/* Validate contents are inbounds */ +for (i =1 ; i < info->hdr->e_shnum; i++) { + shdr = &sechdrs[i]; +switch (shdr->sh_type) { +case SHT_NULL : +case SHT_NOBITS : +/* No contents, offset/size don't mean anything */ +continue ; +default : + err =validate_section_offset (info, shdr); +if (err <0 ) { +pr_err ("Invalid ELF section in module (section %u type %u)\n" , + i, shdr->sh_type); +return err; + } + } + } + + info->sechdrs = sechdrs; + +return 0 ; +} +/* * Check userspace passed ELF module against our expectations, and cache * useful variables for further processing as we go.
@@ -1737,29 +1818,10 @@ static int elf_validity_cache_copy(struct load_info *info, int flags)unsigned int num_info_secs =0 , info_idx;unsigned int num_sym_secs =0 , sym_idx; - err =elf_validity_ehdr (info); + err =elf_validity_cache_sechdrs (info);if (err <0 )return err; -if (info->hdr->e_shentsize !=sizeof (Elf_Shdr)) { -pr_err ("Invalid ELF section header size\n" ); - goto no_exec; - } - -/* -* e_shnum is 16 bits, and sizeof(Elf_Shdr) is -* known and small. So e_shnum * sizeof(Elf_Shdr) -* will not overflow unsigned long on any platform. -*/ -if (info->hdr->e_shoff >= info->len - || (info->hdr->e_shnum *sizeof (Elf_Shdr) > - info->len - info->hdr->e_shoff)) { -pr_err ("Invalid ELF section header overflow\n" ); - goto no_exec; - } - - info->sechdrs = (void *)info->hdr + info->hdr->e_shoff; -/* * Verify if the section name table index is valid. */
@@ -1772,11 +1834,6 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) } strhdr = &info->sechdrs[info->hdr->e_shstrndx]; - err =validate_section_offset (info, strhdr); -if (err <0 ) { -pr_err ("Invalid ELF section hdr(type %u)\n" , strhdr->sh_type); -return err; - }/* * The section name table must be NUL-terminated, as required
@@ -1793,18 +1850,6 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) goto no_exec; } -/* -* The code assumes that section 0 has a length of zero and -* an addr of zero, so check for it. -*/ -if (info->sechdrs[0 ].sh_type !=SHT_NULL - || info->sechdrs[0 ].sh_size !=0 - || info->sechdrs[0 ].sh_addr !=0 ) { -pr_err ("ELF Spec violation: section 0 type(%d)!=SH_NULL or non-zero len or addr\n" , - info->sechdrs[0 ].sh_type); - goto no_exec; - } -for (i =1 ; i < info->hdr->e_shnum; i++) { shdr = &info->sechdrs[i];switch (shdr->sh_type) {
@@ -1823,12 +1868,6 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) sym_idx = i; fallthrough;default : - err =validate_section_offset (info, shdr); -if (err <0 ) { -pr_err ("Invalid ELF section in module (section %u type %u)\n" , - i, shdr->sh_type); -return err; - }if (strcmp (info->secstrings + shdr->sh_name,".gnu.linkonce.this_module" ) ==0 ) { num_mod_secs++;
Signature · 1 line
2.46.1.824.gd892dcdcdd-goog
PATCH v5 4/16 module: Factor out elf_validity_cache_secstrings
In reply to Matthew MaurerFactor out the validation of section names.
There are two behavioral changes:
1. Previously, we did not validate non-SHF_ALLOC sections. This may have once been safe, as find_sec skips non-SHF_ALLOC sections, but find_any_sec, which will be used to load BTF if that is enabled, ignores the SHF_ALLOC flag. Since there's no need to support invalid section names, validate all of them, not just SHF_ALLOC sections.
2. Section names were validated *after* accessing them for the purposes of detecting ".modinfo" and ".gnu.linkonce.this_module". They are now checked prior to the access, which could avoid bad accesses with malformed modules.
--- kernel/module/main.c | 106 ++++++++++++++++++++++++++++--------------- 1 file changed, 69 insertions(+), 37 deletions(-)
kernel/module/main.c +69 -37
@@ -1789,6 +1789,71 @@ static int elf_validity_cache_sechdrs(struct load_info *info)return 0 ; } +/** +* elf_validity_cache_secstrings() - Caches section names if valid +* @info: Load info to cache section names from. Must have valid sechdrs. +* +* Specifically checks: +* +* * Section name table index is inbounds of section headers +* * Section name table is not empty +* * Section name table is NUL terminated +* * All section name offsets are inbounds of the section +* +* Then updates @info with a &load_info->secstrings pointer if valid. +* +* Return: %0 if valid, negative error code if validation failed. +*/ +static int elf_validity_cache_secstrings (struct load_info *info) +{ +Elf_Shdr *strhdr, *shdr; +char *secstrings; +int i; + +/* +* Verify if the section name table index is valid. +*/ +if (info->hdr->e_shstrndx ==SHN_UNDEF + || info->hdr->e_shstrndx >= info->hdr->e_shnum) { +pr_err ("Invalid ELF section name index: %d || e_shstrndx (%d) >= e_shnum (%d)\n" , + info->hdr->e_shstrndx, info->hdr->e_shstrndx, + info->hdr->e_shnum); +return -ENOEXEC ; + } + + strhdr = &info->sechdrs[info->hdr->e_shstrndx]; + +/* +* The section name table must be NUL-terminated, as required +* by the spec. This makes strcmp and pr_* calls that access +* strings in the section safe. +*/ + secstrings = (void *)info->hdr + strhdr->sh_offset; +if (strhdr->sh_size ==0 ) { +pr_err ("empty section name table\n" ); +return -ENOEXEC ; + } +if (secstrings[strhdr->sh_size -1 ] !='\0' ) { +pr_err ("ELF Spec violation: section name table isn't null terminated\n" ); +return -ENOEXEC ; + } + +for (i =0 ; i < info->hdr->e_shnum; i++) { + shdr = &info->sechdrs[i]; +/* SHT_NULL means sh_name has an undefined value */ +if (shdr->sh_type ==SHT_NULL ) +continue ; +if (shdr->sh_name >= strhdr->sh_size) { +pr_err ("Invalid ELF section name in module (section %u type %u)\n" , + i, shdr->sh_type); +return -ENOEXEC ; + } + } + + info->secstrings = secstrings; +return 0 ; +} +/* * Check userspace passed ELF module against our expectations, and cache * useful variables for further processing as we go.
@@ -1812,7 +1877,7 @@ static int elf_validity_cache_sechdrs(struct load_info *info)static int elf_validity_cache_copy (struct load_info *info,int flags) {unsigned int i; -Elf_Shdr *shdr, *strhdr; +Elf_Shdr *shdr;int err;unsigned int num_mod_secs =0 , mod_idx;unsigned int num_info_secs =0 , info_idx;
@@ -1821,34 +1886,9 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) err =elf_validity_cache_sechdrs (info);if (err <0 )return err; - -/* -* Verify if the section name table index is valid. -*/ -if (info->hdr->e_shstrndx ==SHN_UNDEF - || info->hdr->e_shstrndx >= info->hdr->e_shnum) { -pr_err ("Invalid ELF section name index: %d || e_shstrndx (%d) >= e_shnum (%d)\n" , - info->hdr->e_shstrndx, info->hdr->e_shstrndx, - info->hdr->e_shnum); - goto no_exec; - } - - strhdr = &info->sechdrs[info->hdr->e_shstrndx]; - -/* -* The section name table must be NUL-terminated, as required -* by the spec. This makes strcmp and pr_* calls that access -* strings in the section safe. -*/ - info->secstrings = (void *)info->hdr + strhdr->sh_offset; -if (strhdr->sh_size ==0 ) { -pr_err ("empty section name table\n" ); - goto no_exec; - } -if (info->secstrings[strhdr->sh_size -1 ] !='\0' ) { -pr_err ("ELF Spec violation: section name table isn't null terminated\n" ); - goto no_exec; - } + err =elf_validity_cache_secstrings (info); +if (err <0 ) +return err;for (i =1 ; i < info->hdr->e_shnum; i++) { shdr = &info->sechdrs[i];
@@ -1877,14 +1917,6 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) num_info_secs++; info_idx = i; } - -if (shdr->sh_flags &SHF_ALLOC ) { -if (shdr->sh_name >= strhdr->sh_size) { -pr_err ("Invalid ELF section name in module (section %u type %u)\n" , - i, shdr->sh_type); -return -ENOEXEC ; - } - }break ; } }
Signature · 1 line
2.46.1.824.gd892dcdcdd-goog
PATCH v5 5/16 module: Factor out elf_validity_cache_index_info
In reply to Matthew MaurerCentralize .modinfo detection and property validation.
--- kernel/module/main.c | 82 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 68 insertions(+), 14 deletions(-)
kernel/module/main.c +68 -14
@@ -195,6 +195,38 @@ static unsigned int find_sec(const struct load_info *info, const char *name)return 0 ; } +/** +* find_any_unique_sec() - Find a unique section index by name +* @info: Load info for the module to scan +* @name: Name of the section we're looking for +* +* Locates a unique section by name. Ignores SHF_ALLOC. +* +* Return: Section index if found uniquely, zero if absent, negative count +* of total instances if multiple were found. +*/ +static int find_any_unique_sec (const struct load_info *info,const char *name) +{ +unsigned int idx; +unsigned int count =0 ; +int i; + +for (i =1 ; i < info->hdr->e_shnum; i++) { +if (strcmp (info->secstrings + info->sechdrs[i].sh_name, + name) ==0 ) { + count++; + idx = i; + } + } +if (count ==1 ) { +return idx; + }else if (count ==0 ) { +return 0 ; + }else { +return -count; + } +} +/* Find a module section, or NULL. */ static void *section_addr (const struct load_info *info,const char *name) {
@@ -1854,6 +1886,39 @@ static int elf_validity_cache_secstrings(struct load_info *info)return 0 ; } +/** +* elf_validity_cache_index_info() - Validate and cache modinfo section +* @info: Load info to populate the modinfo index on. +* Must have &load_info->sechdrs and &load_info->secstrings populated +* +* Checks that if there is a .modinfo section, it is unique. +* Then, it caches its index in &load_info->index.info. +* Finally, it tries to populate the name to improve error messages. +* +* Return: %0 if valid, %-ENOEXEC if multiple modinfo sections were found. +*/ +static int elf_validity_cache_index_info (struct load_info *info) +{ +int info_idx; + + info_idx =find_any_unique_sec (info,".modinfo" ); + +if (info_idx ==0 ) +/* Early return, no .modinfo */ +return 0 ; + +if (info_idx <0 ) { +pr_err ("Only one .modinfo section must exist.\n" ); +return -ENOEXEC ; + } + + info->index.info = info_idx; +/* Try to find a name early so we can log errors with a module name */ + info->name =get_modinfo (info,"name" ); + +return 0 ; +} +/* * Check userspace passed ELF module against our expectations, and cache * useful variables for further processing as we go.
@@ -1880,13 +1945,15 @@ static int elf_validity_cache_copy(struct load_info *info, int flags)Elf_Shdr *shdr;int err;unsigned int num_mod_secs =0 , mod_idx; -unsigned int num_info_secs =0 , info_idx;unsigned int num_sym_secs =0 , sym_idx; err =elf_validity_cache_sechdrs (info);if (err <0 )return err; err =elf_validity_cache_secstrings (info); +if (err <0 ) +return err; + err =elf_validity_cache_index_info (info);if (err <0 )return err;
@@ -1912,24 +1979,11 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) ".gnu.linkonce.this_module") == 0) { num_mod_secs++; mod_idx = i; - } else if (strcmp(info->secstrings + shdr->sh_name, - ".modinfo") == 0) { - num_info_secs++; - info_idx = i; }break ; } } -if (num_info_secs >1 ) { -pr_err ("Only one .modinfo section must exist.\n" ); - goto no_exec; - }else if (num_info_secs ==1 ) { -/* Try to find a name early so we can log errors with a module name */ - info->index.info = info_idx; - info->name =get_modinfo (info,"name" ); - } -if (num_sym_secs !=1 ) {pr_warn ("%s: module has no symbols (stripped?)\n" , info->name ?:"(missing .modinfo section or name field)" );
Signature · 1 line
2.46.1.824.gd892dcdcdd-goog
PATCH v5 6/16 module: Factor out elf_validity_cache_index_mod
In reply to Matthew MaurerCentralize .gnu.linkonce.this_module detection and property validation.
--- kernel/module/main.c | 129 ++++++++++++++++++++++--------------------- 1 file changed, 67 insertions(+), 62 deletions(-)
kernel/module/main.c +67 -62
@@ -1919,6 +1919,68 @@ static int elf_validity_cache_index_info(struct load_info *info)return 0 ; } +/** +* elf_validity_cache_index_mod() - Validates and caches this_module section +* @info: Load info to cache this_module on. +* Must have &load_info->sechdrs and &load_info->secstrings populated +* +* The ".gnu.linkonce.this_module" ELF section is special. It is what modpost +* uses to refer to __this_module and let's use rely on THIS_MODULE to point +* to &__this_module properly. The kernel's modpost declares it on each +* modules's *.mod.c file. If the struct module of the kernel changes a full +* kernel rebuild is required. +* +* We have a few expectations for this special section, this function +* validates all this for us: +* +* * The section has contents +* * The section is unique +* * We expect the kernel to always have to allocate it: SHF_ALLOC +* * The section size must match the kernel's run time's struct module +* size +* +* If all checks pass, the index will be cached in &load_info->index.mod +* +* Return: %0 on validation success, %-ENOEXEC on failure +*/ +static int elf_validity_cache_index_mod (struct load_info *info) +{ +Elf_Shdr *shdr; +int mod_idx; + + mod_idx =find_any_unique_sec (info,".gnu.linkonce.this_module" ); +if (mod_idx <=0 ) { +pr_err ("module %s: Exactly one .gnu.linkonce.this_module section must exist.\n" , + info->name ?:"(missing .modinfo section or name field)" ); +return -ENOEXEC ; + } + + shdr = &info->sechdrs[mod_idx]; + +if (shdr->sh_type ==SHT_NOBITS ) { +pr_err ("module %s: .gnu.linkonce.this_module section must have a size set\n" , + info->name ?:"(missing .modinfo section or name field)" ); +return -ENOEXEC ; + } + +if (!(shdr->sh_flags &SHF_ALLOC )) { +pr_err ("module %s: .gnu.linkonce.this_module must occupy memory during process execution\n" , + info->name ?:"(missing .modinfo section or name field)" ); +return -ENOEXEC ; + } + +if (shdr->sh_size !=sizeof (struct module )) { +pr_err ("module %s: .gnu.linkonce.this_module section size must match the kernel's built struct module size at run time\n" , + info->name ?:"(missing .modinfo section or name field)" ); +return -ENOEXEC ; + } + + info->index.mod = mod_idx; + +return 0 ; +} + +/* * Check userspace passed ELF module against our expectations, and cache * useful variables for further processing as we go.
@@ -1944,7 +2006,6 @@ static int elf_validity_cache_copy(struct load_info *info, int flags)unsigned int i;Elf_Shdr *shdr;int err; -unsigned int num_mod_secs =0 , mod_idx;unsigned int num_sym_secs =0 , sym_idx; err =elf_validity_cache_sechdrs (info);
@@ -1954,16 +2015,15 @@ static int elf_validity_cache_copy(struct load_info *info, int flags)if (err <0 )return err; err =elf_validity_cache_index_info (info); +if (err <0 ) +return err; + err =elf_validity_cache_index_mod (info);if (err <0 )return err;for (i =1 ; i < info->hdr->e_shnum; i++) { shdr = &info->sechdrs[i]; -switch (shdr->sh_type) { -case SHT_NULL : -case SHT_NOBITS : -continue ; -case SHT_SYMTAB : +if (shdr->sh_type ==SHT_SYMTAB ) {if (shdr->sh_link ==SHN_UNDEF || shdr->sh_link >= info->hdr->e_shnum) { pr_err("Invalid ELF sh_link!=SHN_UNDEF(%d) or (sh_link(%d) >= hdr->e_shnum(%d)\n",
@@ -1973,14 +2033,6 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) } num_sym_secs++; sym_idx = i; - fallthrough; -default : -if (strcmp (info->secstrings + shdr->sh_name, -".gnu.linkonce.this_module" ) ==0 ) { - num_mod_secs++; - mod_idx = i; - } -break ; } }
@@ -1996,55 +2048,8 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) info->index.str = shdr->sh_link; info->strtab = (char *)info->hdr + info->sechdrs[info->index.str].sh_offset; -/* -* The ".gnu.linkonce.this_module" ELF section is special. It is -* what modpost uses to refer to __this_module and let's use rely -* on THIS_MODULE to point to &__this_module properly. The kernel's -* modpost declares it on each modules's *.mod.c file. If the struct -* module of the kernel changes a full kernel rebuild is required. -* -* We have a few expectaions for this special section, the following -* code validates all this for us: -* -* o Only one section must exist -* o We expect the kernel to always have to allocate it: SHF_ALLOC -* o The section size must match the kernel's run time's struct module -* size -*/ -if (num_mod_secs !=1 ) { -pr_err ("module %s: Only one .gnu.linkonce.this_module section must exist.\n" , - info->name ?:"(missing .modinfo section or name field)" ); - goto no_exec; - } - - shdr = &info->sechdrs[mod_idx]; - -/* -* This is already implied on the switch above, however let's be -* pedantic about it. -*/ -if (shdr->sh_type ==SHT_NOBITS ) { -pr_err ("module %s: .gnu.linkonce.this_module section must have a size set\n" , - info->name ?:"(missing .modinfo section or name field)" ); - goto no_exec; - } - -if (!(shdr->sh_flags &SHF_ALLOC )) { -pr_err ("module %s: .gnu.linkonce.this_module must occupy memory during process execution\n" , - info->name ?:"(missing .modinfo section or name field)" ); - goto no_exec; - } - -if (shdr->sh_size !=sizeof (struct module )) { -pr_err ("module %s: .gnu.linkonce.this_module section size must match the kernel's built struct module size at run time\n" , - info->name ?:"(missing .modinfo section or name field)" ); - goto no_exec; - } - - info->index.mod = mod_idx; -/* This is temporary: point mod into copy of data. */ - info->mod = (void *)info->hdr + shdr->sh_offset; + info->mod = (void *)info->hdr + info->sechdrs[info->index.mod].sh_offset;/* * If we didn't load the .modinfo 'name' field earlier, fall back to
Signature · 1 line
2.46.1.824.gd892dcdcdd-goog
PATCH v5 7/16 module: Factor out elf_validity_cache_index_sym
In reply to Matthew MaurerCentralize symbol table detection and property validation.
--- kernel/module/main.c | 73 ++++++++++++++++++++++++++------------------ 1 file changed, 44 insertions(+), 29 deletions(-)
kernel/module/main.c +44 -29
@@ -1980,6 +1980,39 @@ static int elf_validity_cache_index_mod(struct load_info *info)return 0 ; } +/** +* elf_validity_cache_index_sym() - Validate and cache symtab index +* @info: Load info to cache symtab index in. +* Must have &load_info->sechdrs and &load_info->secstrings populated. +* +* Checks that there is exactly one symbol table, then caches its index in +* &load_info->index.sym. +* +* Return: %0 if valid, %-ENOEXEC on failure. +*/ +static int elf_validity_cache_index_sym (struct load_info *info) +{ +unsigned int sym_idx; +unsigned int num_sym_secs =0 ; +int i; + +for (i =1 ; i < info->hdr->e_shnum; i++) { +if (info->sechdrs[i].sh_type ==SHT_SYMTAB ) { + num_sym_secs++; + sym_idx = i; + } + } + +if (num_sym_secs !=1 ) { +pr_warn ("%s: module has no symbols (stripped?)\n" , + info->name ?:"(missing .modinfo section or name field)" ); +return -ENOEXEC ; + } + + info->index.sym = sym_idx; + +return 0 ; +}/* * Check userspace passed ELF module against our expectations, and cache
@@ -2003,10 +2036,8 @@ static int elf_validity_cache_index_mod(struct load_info *info)*/ static int elf_validity_cache_copy (struct load_info *info,int flags) { -unsigned int i; -Elf_Shdr *shdr;int err; -unsigned int num_sym_secs =0 , sym_idx; +int str_idx; err =elf_validity_cache_sechdrs (info);if (err <0 )
@@ -2018,34 +2049,21 @@ static int elf_validity_cache_copy(struct load_info *info, int flags)if (err <0 )return err; err =elf_validity_cache_index_mod (info); +if (err <0 ) +return err; + err =elf_validity_cache_index_sym (info);if (err <0 )return err; -for (i =1 ; i < info->hdr->e_shnum; i++) { - shdr = &info->sechdrs[i]; -if (shdr->sh_type ==SHT_SYMTAB ) { -if (shdr->sh_link ==SHN_UNDEF - || shdr->sh_link >= info->hdr->e_shnum) { -pr_err ("Invalid ELF sh_link!=SHN_UNDEF(%d) or (sh_link(%d) >= hdr->e_shnum(%d)\n" , - shdr->sh_link, shdr->sh_link, - info->hdr->e_shnum); - goto no_exec; - } - num_sym_secs++; - sym_idx = i; - } - } - -if (num_sym_secs !=1 ) { -pr_warn ("%s: module has no symbols (stripped?)\n" , - info->name ?:"(missing .modinfo section or name field)" ); - goto no_exec; + str_idx = info->sechdrs[info->index.sym].sh_link; +if (str_idx ==SHN_UNDEF || str_idx >= info->hdr->e_shnum) { +pr_err ("Invalid ELF sh_link!=SHN_UNDEF(%d) or (sh_link(%d) >= hdr->e_shnum(%d)\n" , + str_idx, str_idx, info->hdr->e_shnum); +return -ENOEXEC ; } -/* Sets internal symbols and strings. */ - info->index.sym = sym_idx; - shdr = &info->sechdrs[sym_idx]; - info->index.str = shdr->sh_link; +/* Sets internal strings. */ + info->index.str = str_idx; info->strtab = (char *)info->hdr + info->sechdrs[info->index.str].sh_offset;/* This is temporary: point mod into copy of data. */
@@ -2066,9 +2084,6 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) info->index.pcpu =find_pcpusec (info);return 0 ; - -no_exec: -return -ENOEXEC ; }#define COPY_CHUNK_SIZE (16*PAGE_SIZE)
Signature · 1 line
2.46.1.824.gd892dcdcdd-goog
PATCH v5 8/16 module: Factor out elf_validity_cache_index_str
In reply to Matthew MaurerPull out index validation for the symbol string section.
Note that this does not validate the *contents* of the string table, only shape and presence of the section.
--- kernel/module/main.c | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-)
kernel/module/main.c +28 -9
@@ -2014,6 +2014,31 @@ static int elf_validity_cache_index_sym(struct load_info *info)return 0 ; } +/** +* elf_validity_cache_index_str() - Validate and cache strtab index +* @info: Load info to cache strtab index in. +* Must have &load_info->sechdrs and &load_info->secstrings populated. +* Must have &load_info->index.sym populated. +* +* Looks at the symbol table's associated string table, makes sure it is +* in-bounds, and caches it. +* +* Return: %0 if valid, %-ENOEXEC on failure. +*/ +static int elf_validity_cache_index_str (struct load_info *info) +{ +unsigned int str_idx = info->sechdrs[info->index.sym].sh_link; + +if (str_idx ==SHN_UNDEF || str_idx >= info->hdr->e_shnum) { +pr_err ("Invalid ELF sh_link!=SHN_UNDEF(%d) or (sh_link(%d) >= hdr->e_shnum(%d)\n" , + str_idx, str_idx, info->hdr->e_shnum); +return -ENOEXEC ; + } + + info->index.str = str_idx; +return 0 ; +} +/* * Check userspace passed ELF module against our expectations, and cache * useful variables for further processing as we go.
@@ -2037,7 +2062,6 @@ static int elf_validity_cache_index_sym(struct load_info *info)static int elf_validity_cache_copy (struct load_info *info,int flags) {int err; -int str_idx; err =elf_validity_cache_sechdrs (info);if (err <0 )
@@ -2054,16 +2078,11 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) err =elf_validity_cache_index_sym (info);if (err <0 )return err; - - str_idx = info->sechdrs[info->index.sym].sh_link; -if (str_idx ==SHN_UNDEF || str_idx >= info->hdr->e_shnum) { -pr_err ("Invalid ELF sh_link!=SHN_UNDEF(%d) or (sh_link(%d) >= hdr->e_shnum(%d)\n" , - str_idx, str_idx, info->hdr->e_shnum); -return -ENOEXEC ; - } + err =elf_validity_cache_index_str (info); +if (err <0 ) +return err;/* Sets internal strings. */ - info->index.str = str_idx; info->strtab = (char *)info->hdr + info->sechdrs[info->index.str].sh_offset;/* This is temporary: point mod into copy of data. */
Signature · 1 line
2.46.1.824.gd892dcdcdd-goog
PATCH v5 9/16 module: Group section index calculations together
In reply to Matthew MaurerGroup all the index detection together to make the parent function easier to read.
--- kernel/module/main.c | 68 +++++++++++++++++++++++++++++++++----------- 1 file changed, 51 insertions(+), 17 deletions(-)
kernel/module/main.c +51 -17
@@ -2039,6 +2039,56 @@ static int elf_validity_cache_index_str(struct load_info *info)return 0 ; } +/** +* elf_validity_cache_index() - Resolve, validate, cache section indices +* @info: Load info to read from and update. +* &load_info->sechdrs and &load_info->secstrings must be populated. +* @flags: Load flags, relevant to suppress version loading, see +* uapi/linux/module.h +* +* Populates &load_info->index, validating as it goes. +* See child functions for per-field validation: +* +* * elf_validity_cache_index_info() +* * elf_validity_cache_index_mod() +* * elf_validity_cache_index_sym() +* * elf_validity_cache_index_str() +* +* If versioning is not suppressed via flags, load the version index from +* a section called "__versions" with no validation. +* +* If CONFIG_SMP is enabled, load the percpu section by name with no +* validation. +* +* Return: 0 on success, negative error code if an index failed validation. +*/ +static int elf_validity_cache_index (struct load_info *info,int flags) +{ +int err; + + err =elf_validity_cache_index_info (info); +if (err <0 ) +return err; + err =elf_validity_cache_index_mod (info); +if (err <0 ) +return err; + err =elf_validity_cache_index_sym (info); +if (err <0 ) +return err; + err =elf_validity_cache_index_str (info); +if (err <0 ) +return err; + +if (flags &MODULE_INIT_IGNORE_MODVERSIONS ) + info->index.vers =0 ;/* Pretend no __versions section! */ +else + info->index.vers =find_sec (info,"__versions" ); + + info->index.pcpu =find_pcpusec (info); + +return 0 ; +} +/* * Check userspace passed ELF module against our expectations, and cache * useful variables for further processing as we go.
@@ -2069,16 +2119,7 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) err =elf_validity_cache_secstrings (info);if (err <0 )return err; - err =elf_validity_cache_index_info (info); -if (err <0 ) -return err; - err =elf_validity_cache_index_mod (info); -if (err <0 ) -return err; - err =elf_validity_cache_index_sym (info); -if (err <0 ) -return err; - err =elf_validity_cache_index_str (info); + err =elf_validity_cache_index (info, flags);if (err <0 )return err;
@@ -2095,13 +2136,6 @@ static int elf_validity_cache_copy(struct load_info *info, int flags)if (!info->name) info->name = info->mod->name; -if (flags &MODULE_INIT_IGNORE_MODVERSIONS ) - info->index.vers =0 ;/* Pretend no __versions section! */ -else - info->index.vers =find_sec (info,"__versions" ); - - info->index.pcpu =find_pcpusec (info); -return 0 ; }
Signature · 1 line
2.46.1.824.gd892dcdcdd-goog
PATCH v5 10/16 module: Factor out elf_validity_cache_strtab
In reply to Matthew MaurerThis patch only moves the existing strtab population to a function. Validation comes in a following patch, this is split out to make the new validation checks more clearly separated.
--- kernel/module/main.c | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-)
kernel/module/main.c +20 -3
@@ -2089,6 +2089,23 @@ static int elf_validity_cache_index(struct load_info *info, int flags)return 0 ; } +/** +* elf_validity_cache_strtab() - Cache symbol string table +* @info: Load info to read from and update. +* Must have &load_info->sechdrs and &load_info->secstrings populated. +* Must have &load_info->index populated. +* +* Return: 0 on success, negative error code if a check failed. +*/ +static int elf_validity_cache_strtab (struct load_info *info) +{ +Elf_Shdr *str_shdr = &info->sechdrs[info->index.str]; +char *strtab = (char *)info->hdr + str_shdr->sh_offset; + + info->strtab = strtab; +return 0 ; +} +/* * Check userspace passed ELF module against our expectations, and cache * useful variables for further processing as we go.
@@ -2122,9 +2139,9 @@ static int elf_validity_cache_copy(struct load_info *info, int flags) err =elf_validity_cache_index (info, flags);if (err <0 )return err; - -/* Sets internal strings. */ - info->strtab = (char *)info->hdr + info->sechdrs[info->index.str].sh_offset; + err =elf_validity_cache_strtab (info); +if (err <0 ) +return err;/* This is temporary: point mod into copy of data. */ info->mod = (void *)info->hdr + info->sechdrs[info->index.mod].sh_offset;
Signature · 1 line
2.46.1.824.gd892dcdcdd-goog
PATCH v5 11/16 module: Additional validation in elf_validity_cache_strtab
In reply to Matthew MaurerValidate properties of the strtab that are depended on elsewhere, but were previously unchecked:
* String table nonempty (offset 0 is valid)
* String table has a leading NUL (offset 0 corresponds to "")
* String table is NUL terminated (strfoo functions won't run out of the table while reading).
* All symbols names are inbounds of the string table.
--- kernel/module/main.c | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-)
kernel/module/main.c +37 -1
@@ -2090,17 +2090,53 @@ static int elf_validity_cache_index(struct load_info *info, int flags) }/** -* elf_validity_cache_strtab() - Cache symbol string table +* elf_validity_cache_strtab() - Validate and cache symbol string table * @info: Load info to read from and update. * Must have &load_info->sechdrs and &load_info->secstrings populated. * Must have &load_info->index populated. * +* Checks: +* +* * The string table is not empty. +* * The string table starts and ends with NUL (required by ELF spec). +* * Every &Elf_Sym->st_name offset in the symbol table is inbounds of the +* string table. +* +* And caches the pointer as &load_info->strtab in @info. +* * Return: 0 on success, negative error code if a check failed. */ static int elf_validity_cache_strtab (struct load_info *info) {Elf_Shdr *str_shdr = &info->sechdrs[info->index.str]; +Elf_Shdr *sym_shdr = &info->sechdrs[info->index.sym];char *strtab = (char *)info->hdr + str_shdr->sh_offset; +Elf_Sym *syms = (void *)info->hdr + sym_shdr->sh_offset; +int i; + +if (str_shdr->sh_size ==0 ) { +pr_err ("empty symbol string table\n" ); +return -ENOEXEC ; + } +if (strtab[0 ] !='\0' ) { +pr_err ("symbol string table missing leading NUL\n" ); +return -ENOEXEC ; + } +if (strtab[str_shdr->sh_size -1 ] !='\0' ) { +pr_err ("symbol string table isn't NUL terminated\n" ); +return -ENOEXEC ; + } + +/* +* Now that we know strtab is correctly structured, check symbol +* starts are inbounds before they're used later. +*/ +for (i =0 ; i < sym_shdr->sh_size /sizeof (*syms); i++) { +if (syms[i].st_name >= str_shdr->sh_size) { +pr_err ("symbol name out of bounds in string table" ); +return -ENOEXEC ; + } + } info->strtab = strtab;return 0 ;
Signature · 1 line
2.46.1.824.gd892dcdcdd-goog
PATCH v5 12/16 module: Reformat struct for code style
In reply to Matthew MaurerUsing commas to declare struct members makes adding new members to this struct not as nice with patch management.
--- kernel/module/internal.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/module/internal.h +6 -1
@@ -80,7 +80,12 @@ struct load_info {unsigned int used_pages;#endif struct { -unsigned int sym, str, mod, vers, info, pcpu; +unsigned int sym; +unsigned int str; +unsigned int mod; +unsigned int vers; +unsigned int info; +unsigned int pcpu; } index; };
Signature · 1 line
2.46.1.824.gd892dcdcdd-goog
PATCH v5 13/16 export_report: Rehabilitate script
In reply to Matthew Maurer* modules.order has .o files when in a build dir, support this
* .mod.c source layout has changed, update regexes to match
* Add a stage 3, to be more robust against additional .mod.c content
--- scripts/export_report.pl | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
scripts/export_report.pl +7 -2
@@ -55,6 +55,7 @@ sub collectcfiles { open my $fh, '< modules.order' or die "cannot open modules.order: $!\n"; while (<$fh>) { s/\.ko$/.mod.c/; + s/\.o$/.mod.c/; push (@file, $_) } close($fh);
@@ -120,10 +121,14 @@ foreach my $thismod (@allcfiles) { next; } if ($state == 1) { - $state = 2 if ($_ =~ /__attribute__\(\(section\("__versions"\)\)\)/); + $state = 2 if ($_ =~ /__used __section\("__versions"\)/); next; } if ($state == 2) { + if ( $_ =~ /};/ ) { + $state = 3; + next; + } if ( $_ !~ /0x[0-9a-f]+,/ ) { next; }
@@ -133,7 +138,7 @@ foreach my $thismod (@allcfiles) { push(@{$MODULE{$thismod}} , $sym); } } - if ($state != 2) { + if ($state != 3) { warn "WARNING:$thismod is not built with CONFIG_MODVERSIONS enabled\n"; $modversion_warnings++; }
Signature · 1 line
2.46.1.824.gd892dcdcdd-goog
Luis Chamberlain
maintainerRe: [PATCH v5 13/16] export_report: Rehabilitate script
Quoted 1 line · Matthew Maurer
* modules.order has .o files when in a build dir, support this
The commit log is not clear, is it that it's always had *.o files, and you're adding them now, why? Why is the .ko search now removed?
Quoted 1 line
*
.mod.csource layout has changed,
When, why did this change not happen at that time?
Quoted 1 line
update regexes to match
Why did this not break anything before ? Is this fixing something, or is it prep work?
Quoted 1 line
* Add a stage 3, to be more robust against additional
.mod.ccontent
Future .mod.c changes?
Luis
Re: [PATCH v5 13/16] export_report: Rehabilitate script
Quoted 6 lines · Luis Chamberlain
Quoted 1 line · Matthew Maurer
* modules.order has .o files when in a build dir, support this
The commit log is not clear, is it that it's always had *.o files, and you're adding them now, why? Why is the .ko search now removed?
The script was broken when I found it, but it was a script that analyzed MODVERSIONS, so I tried to ensure it would still work with my changes. This necessitated rehabilitating it first. I did not touch .modules.order files, but they contained .o and so this script wouldn't run correctly.
Quoted 4 lines
Quoted 2 lines
*
.mod.csource layout has changed,When, why did this change not happen at that time?
It was changed for readability [1] back in 2019. I assume the change did not happen at that time because this script is rarely run. If we'd prefer to discard this patch and ignore the script instead (or remove it?), that's fine.
[1]: https://lore.kernel.org/all/[email protected]/
Quoted 9 lines
Quoted 2 lines
update regexes to match
Why did this not break anything before ? Is this fixing something, or is it prep work?
Quoted 1 line
* Add a stage 3, to be more robust against additional
.mod.ccontentFuture
.mod.cchanges?
The rest of this series adds additional .mod.c content to support the string names. This stage 3 is intended to prevent that from causing the script to choke.
Quoted 2 lines
Luis
Luis Chamberlain
maintainerRe: [PATCH v5 13/16] export_report: Rehabilitate script
Quoted 9 lines · Matthew Maurer
Quoted 6 lines · Luis Chamberlain
Quoted 1 line · Matthew Maurer
* modules.order has .o files when in a build dir, support this
The commit log is not clear, is it that it's always had *.o files, and you're adding them now, why? Why is the .ko search now removed?
The script was broken when I found it,
My point is that commit messages should describe all this.
Quoted 3 lines
but it was a script that
analyzed MODVERSIONS, so I tried to ensure it would still work with my changes.
OK yes please add this informaiton into the comit log.
Also expand on it as to to *why* this was not a critical issue.
Quoted 3 lines
This necessitated rehabilitating it first. I did not touch
.modules.orderfiles, but they contained.oand so this script wouldn't run correctly.
Just elaborate on the commit log. We cannot guess what is happening, and so bringing clarity into exactly what you are doing and *why* is helpful to determine the impact of not applying this.
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
*
.mod.csource layout has changed,When, why did this change not happen at that time?
It was changed for readability [1] back in 2019. I assume the change did not happen at that time because this script is rarely run.
This is crucial information to include in the commit log. When *would* you use it? So that use case was broken since 2019? What negative effects has this implicated?
Quoted 3 lines
If we'd
prefer to discard this patch and ignore the script instead (or remove it?), that's fine.
The feedback is for you to improve the commit logs, not to tell you to not do something. We cannot read your mind, and this smelled like a fix, but without a true evaluation and documentation of the impact of not having this in place. These things need to be thought out and written in the comit log.
Quoted 9 lines
[1]: https://lore.kernel.org/all/[email protected]/
Quoted 6 lines
Quoted 2 lines
update regexes to match
Why did this not break anything before ? Is this fixing something, or is it prep work?
Quoted 7 lines
Quoted 3 lines
Quoted 1 line
* Add a stage 3, to be more robust against additional
.mod.ccontentFuture
.mod.cchanges?The rest of this series adds additional
.mod.ccontent to support the string names. This stage 3 is intended to prevent that from causing the script to choke.
The commit log should clarify all this.
Luis
PATCH v5 14/16 modules: Support extended MODVERSIONS info
In reply to Matthew MaurerAdds a new format for MODVERSIONS which stores each field in a separate ELF section. This initially adds support for variable length names, but could later be used to add additional fields to MODVERSIONS in a backwards compatible way if needed. Any new fields will be ignored by old user tooling, unlike the current format where user tooling cannot tolerate adjustments to the format (for example making the name field longer).
Since PPC munges its version records to strip leading dots, we reproduce the munging for the new format. Other architectures do not appear to have architecture-specific usage of this information.
--- arch/powerpc/kernel/module_64.c | 23 ++++++++- kernel/module/internal.h | 11 ++++ kernel/module/main.c | 92 ++++++++++++++++++++++++++++++--- kernel/module/version.c | 45 ++++++++++++++++ 4 files changed, 161 insertions(+), 10 deletions(-)
arch/powerpc/kernel/module_64.c +21 -2
@@ -355,6 +355,23 @@ static void dedotify_versions(struct modversion_info *vers, } } +static void dedotify_ext_version_names (char *str_seq,unsigned long size) +{ +unsigned long out =0 ; +unsigned long in; +char last ='\0' ; + +for (in =0 ; in < size; in++) { +/* Skip one leading dot */ +if (last =='\0' && str_seq[in] =='.' ) + in++; + last = str_seq[in]; + str_seq[out++] = last; + } +/* Zero the trailing portion of the names table for robustness */ +memset (&str_seq[out],0 , size - out); +} +/* * Undefined symbols which refer to .funcname, hack to funcname. Make .TOC. * seem to be defined (value set later).
@@ -424,10 +441,12 @@ int module_frob_arch_sections(Elf64_Ehdr *hdr, me->arch.toc_section = i;if (sechdrs[i].sh_addralign <8 ) sechdrs[i].sh_addralign =8 ; - } -else if (strcmp (secstrings+sechdrs[i].sh_name,"__versions" )==0 ) + }else if (strcmp (secstrings + sechdrs[i].sh_name,"__versions" ) ==0 )dedotify_versions ((void *)hdr + sechdrs[i].sh_offset, sechdrs[i].sh_size); +else if (strcmp (secstrings + sechdrs[i].sh_name,"__version_ext_names" ) ==0 ) +dedotify_ext_version_names ((void *)hdr + sechdrs[i].sh_offset, + sechdrs[i].sh_size);if (sechdrs[i].sh_type ==SHT_SYMTAB ) dedotify((void *)hdr + sechdrs[i].sh_offset,
kernel/module/internal.h +11 -0
@@ -86,6 +86,8 @@ struct load_info {unsigned int vers;unsigned int info;unsigned int pcpu; +unsigned int vers_ext_crc; +unsigned int vers_ext_name; } index; };
@@ -389,6 +391,15 @@ void module_layout(struct module *mod, struct modversion_info *ver, struct kerne struct kernel_symbol *ks, struct tracepoint * const *tp);int check_modstruct_version (const struct load_info *info,struct module *mod);int same_magic (const char *amagic,const char *bmagic,bool has_crcs); +struct modversion_info_ext { +size_t remaining; +const s32 *crc; +const char *name; +}; +void modversion_ext_start (const struct load_info *info,struct modversion_info_ext *ver); +void modversion_ext_advance (struct modversion_info_ext *ver); +#define for_each_modversion_info_ext (ver, info) \ + for (modversion_ext_start(info, &ver); ver.remaining > 0; modversion_ext_advance(&ver))#else /* !CONFIG_MODVERSIONS */ static inline int check_version(const struct load_info *info, const char *symname,
kernel/module/main.c +84 -8
116 lines not on this page · raw message
kernel/module/version.c +45 -0
62 lines not on this page · raw message
Signature · 1 line
2.46.1.824.gd892dcdcdd-goog
Re: [PATCH v5 14/16] modules: Support extended MODVERSIONS info
Le 26/09/2024 à 01:38, Matthew Maurer a écrit :
Quoted 31 lines
Adds a new format for MODVERSIONS which stores each field in a separate ELF section. This initially adds support for variable length names, but could later be used to add additional fields to MODVERSIONS in a backwards compatible way if needed. Any new fields will be ignored by old user tooling, unlike the current format where user tooling cannot tolerate adjustments to the format (for example making the name field longer).
Since PPC munges its version records to strip leading dots, we reproduce the munging for the new format. Other architectures do not appear to have architecture-specific usage of this information.
Signed-off-by Matthew Maurer <[email protected]>--- arch/powerpc/kernel/module_64.c | 23 ++++++++- kernel/module/internal.h | 11 ++++ kernel/module/main.c | 92 ++++++++++++++++++++++++++++++--- kernel/module/version.c | 45 ++++++++++++++++ 4 files changed, 161 insertions(+), 10 deletions(-)arch/powerpc/kernel/module_64.c +3
-0@@ -355,6 +355,23 @@ static void dedotify_versions(struct modversion_info *vers, } } +static void dedotify_ext_version_names (char *str_seq,unsigned long size) +{ +unsigned long out =0 ;
arch/powerpc/kernel/module_64.c static void dedotify_versions(struct modversion_info *vers, · line 370
Quoted 10 lines
arch/powerpc/kernel/module_64.c +10
-0+unsigned long in; +char last ='\0' ; + +for (in =0 ; in < size; in++) { +/* Skip one leading dot */ +if (last =='\0' && str_seq[in] =='.' ) + in++; + last = str_seq[in]; + str_seq[out++] = last; + }
Why do you need a loop here ?
Can't you just do something like:
if (str_seq[0] == '.') memmove(str_seq, str_seq + 1, size);
arch/powerpc/kernel/module_64.c static void dedotify_versions(struct modversion_info *vers, · line 372
Quoted 2 lines
arch/powerpc/kernel/module_64.c +2
-0+/* Zero the trailing portion of the names table for robustness */ +memset (&str_seq[out],0 , size - out);
This seems unneeded.
arch/powerpc/kernel/module_64.c static void dedotify_versions(struct modversion_info *vers, · line 377
Quoted 5 lines
arch/powerpc/kernel/module_64.c +2
-0+} +/* * Undefined symbols which refer to .funcname, hack to funcname. Make .TOC. * seem to be defined (value set later).
Christophe
Sami Tolvanen
maintainerRe: [PATCH v5 14/16] modules: Support extended MODVERSIONS info
Quoted 48 lines · Christophe Leroy
Quoted 31 lines · Matthew Maurer
Adds a new format for MODVERSIONS which stores each field in a separate ELF section. This initially adds support for variable length names, but could later be used to add additional fields to MODVERSIONS in a backwards compatible way if needed. Any new fields will be ignored by old user tooling, unlike the current format where user tooling cannot tolerate adjustments to the format (for example making the name field longer).
Since PPC munges its version records to strip leading dots, we reproduce the munging for the new format. Other architectures do not appear to have architecture-specific usage of this information.
Signed-off-by Matthew Maurer <[email protected]>--- arch/powerpc/kernel/module_64.c | 23 ++++++++- kernel/module/internal.h | 11 ++++ kernel/module/main.c | 92 ++++++++++++++++++++++++++++++--- kernel/module/version.c | 45 ++++++++++++++++ 4 files changed, 161 insertions(+), 10 deletions(-)arch/powerpc/kernel/module_64.c +3
-0@@ -355,6 +355,23 @@ static void dedotify_versions(struct modversion_info *vers, } } +static void dedotify_ext_version_names (char *str_seq,unsigned long size) +{ +unsigned long out =0 ;arch/powerpc/kernel/module_64.c static void dedotify_versions(struct modversion_info *vers, · line 370
Quoted 10 lines
arch/powerpc/kernel/module_64.c +10
-0+unsigned long in; +char last ='\0' ; + +for (in =0 ; in < size; in++) { +/* Skip one leading dot */ +if (last =='\0' && str_seq[in] =='.' ) + in++; + last = str_seq[in]; + str_seq[out++] = last; + }Why do you need a loop here ?
Quoted 4 lines
Can't you just do something like:
if (str_seq[0] == '.') memmove(str_seq, str_seq + 1, size);
I initially had the same thought, but it's because this is is a sequence of multiple null-terminated strings, and we need to dedotify all of them, not just the first one. Here's an example:
https://godbolt.org/z/avMGnd48M
Quoted 4 lines
arch/powerpc/kernel/module_64.c static void dedotify_versions(struct modversion_info *vers, · line 372
Quoted 2 lines
arch/powerpc/kernel/module_64.c +2
-0+/* Zero the trailing portion of the names table for robustness */ +memset (&str_seq[out],0 , size - out);This seems unneeded.
Strictly speaking it shouldn't be needed, but I think it's still good hygiene to not leave another null-terminated fragment at the end.
Sami
Luis Chamberlain
maintainerRe: [PATCH v5 14/16] modules: Support extended MODVERSIONS info
In reply to Matthew MaurerQuoted 8 lines · Matthew Maurer
Adds a new format for MODVERSIONS which stores each field in a separate ELF section. This initially adds support for variable length names, but could later be used to add additional fields to MODVERSIONS in a backwards compatible way if needed. Any new fields will be ignored by old user tooling, unlike the current format where user tooling cannot tolerate adjustments to the format (for example making the name field longer).
Quoted 5 lines
Since PPC munges its version records to strip leading dots, we reproduce the munging for the new format. Other architectures do not appear to have architecture-specific usage of this information.
Signed-off-by Matthew Maurer <[email protected]>
I'm all for the ELF validation work so far, all that was nice, thanks for all that tidying up. This however is not considering when we really need all this at all, and not making it specific to the build times when such things are needed. That is, yes I'd like to see the need for this clearly explicitly defined through Kconfig, a *select FOO_FEATURE* for when this is needed. No need to extend a module with bloat if we don't need it, likewise if a kernel was built without needing those things, why bloat the modules with the extra information?
Luis
Re: [PATCH v5 14/16] modules: Support extended MODVERSIONS info
Quoted 16 lines · Luis Chamberlain
Quoted 8 lines · Matthew Maurer
Adds a new format for MODVERSIONS which stores each field in a separate ELF section. This initially adds support for variable length names, but could later be used to add additional fields to MODVERSIONS in a backwards compatible way if needed. Any new fields will be ignored by old user tooling, unlike the current format where user tooling cannot tolerate adjustments to the format (for example making the name field longer).
Quoted 5 lines
Since PPC munges its version records to strip leading dots, we reproduce the munging for the new format. Other architectures do not appear to have architecture-specific usage of this information.
Signed-off-by Matthew Maurer <[email protected]>
Quoted 8 lines
I'm all for the ELF validation work so far, all that was nice, thanks for all that tidying up. This however is not considering when we really need all this at all, and not making it specific to the build times when such things are needed. That is, yes I'd like to see the need for this clearly explicitly defined through
Kconfig, a *select FOO_FEATURE* for when this is needed. No need to extend a module with bloat if we don't need it, likewise if a kernel was built without needing those things, why bloat the modules with the extra information?
To make sure I understand what you're asking for, are you suggesting:
1. A config flag for supporting parsing the extended format
2. A config flag for supporting parsing the existing format
3. A config flag for putting the extended format into produced modules
4. A config flag for putting the existing format into produced modules or some combination of the above?
I'm currently reading this as #3, but figured I'd ask to be certain.
Quoted 2 lines
Luis
Luis Chamberlain
maintainerRe: [PATCH v5 14/16] modules: Support extended MODVERSIONS info
Quoted 26 lines · Matthew Maurer
Quoted 16 lines · Luis Chamberlain
Quoted 8 lines · Matthew Maurer
Adds a new format for MODVERSIONS which stores each field in a separate ELF section. This initially adds support for variable length names, but could later be used to add additional fields to MODVERSIONS in a backwards compatible way if needed. Any new fields will be ignored by old user tooling, unlike the current format where user tooling cannot tolerate adjustments to the format (for example making the name field longer).
Quoted 5 lines
Since PPC munges its version records to strip leading dots, we reproduce the munging for the new format. Other architectures do not appear to have architecture-specific usage of this information.
Signed-off-by Matthew Maurer <[email protected]>Quoted 8 lines
I'm all for the ELF validation work so far, all that was nice, thanks for all that tidying up. This however is not considering when we really need all this at all, and not making it specific to the build times when such things are needed. That is, yes I'd like to see the need for this clearly explicitly defined through
Kconfig, a *select FOO_FEATURE* for when this is needed. No need to extend a module with bloat if we don't need it, likewise if a kernel was built without needing those things, why bloat the modules with the extra information?
Quoted 8 lines
To make sure I understand what you're asking for, are you suggesting:
1. A config flag for supporting parsing the extended format
2. A config flag for supporting parsing the existing format
3. A config flag for putting the extended format into produced modules
4. A config flag for putting the existing format into produced modules or some combination of the above?I'm currently reading this as #3, but figured I'd ask to be certain.
3), but if your kernel build does not require these extra things, then a simple if !(IS_ENABLED) sanity check could be put in place to avoid processing the information if the kernel didn't need it. It's a one line change. So at run time, we build the same kernel with all that code in, but it makes no sense to be processing modules with that stuff if kernels did not need it.
Luis
Luis Chamberlain
maintainerRe: [PATCH v5 14/16] modules: Support extended MODVERSIONS info
Also, just as I asked Sami, coould you split this up into patch sets? One with all the cleanups and elf validation code shifts. And then the other code. That will let me pick up quickly the first patch set.
Luis
Luis Chamberlain
maintainerRe: [PATCH v5 14/16] modules: Support extended MODVERSIONS info
Quoted 4 lines · Luis Chamberlain
Also, just as I asked Sami, coould you split this up into patch sets? One with all the cleanups and elf validation code shifts. And then the other code. That will let me pick up quickly the first patch set.
Oh and if you can think of ways to enhance our test covereage on all this as I noted to Sami, it would be greatly appreciated.
Luis
Re: [PATCH v5 14/16] modules: Support extended MODVERSIONS info
So, the basic things I can think of to test here are:
1. The kernel can still load the previous MODVERSIONS format
2. The kernel can load the new MODVERSIONS format
3. If we artificially tweak a CRC in the previous format, it will fail to load.
4. If we artificially tweak a CRC in the new format, it will fail to load.
5. With CONFIG_EXTENDED_MODVERSIONS enabled, the kernel will build and load modules with long symbol names, with MODVERSIONS enabled.
Is there anything else you were thinking of here, or are those the kinds of checks you were envisioning?
Quoted 11 lines · Luis Chamberlain
Quoted 4 lines · Luis Chamberlain
Also, just as I asked Sami, coould you split this up into patch sets? One with all the cleanups and elf validation code shifts. And then the other code. That will let me pick up quickly the first patch set.
Oh and if you can think of ways to enhance our test covereage on all this as I noted to Sami, it would be greatly appreciated.
Luis
Luis Chamberlain
maintainerRe: [PATCH v5 14/16] modules: Support extended MODVERSIONS info
Quoted 11 lines · Matthew Maurer
So, the basic things I can think of to test here are:
1. The kernel can still load the previous MODVERSIONS format
2. The kernel can load the new MODVERSIONS format
3. If we artificially tweak a CRC in the previous format, it will fail to load.
4. If we artificially tweak a CRC in the new format, it will fail to load.
5. WithCONFIG_EXTENDED_MODVERSIONSenabled, the kernel will build and load modules with long symbol names, with MODVERSIONS enabled.Is there anything else you were thinking of here, or are those the kinds of checks you were envisioning?
That sounds great. Yeah, the above would be great to test. A while ago I wrote a new modules selftests in order to test possible improvements on find_symbol() but I also did this due to push the limits of the numbers of symbols we could support. I wrote all this to also test the possible 64-bit alignment benefits of __ksymtab_ sections on architectures without CONFIG_HAVE_ARCH_PREL32_RELOCATIONS (e.g. ppc64, ppc64le, parisc, s390x,...). But come to think of it, you might be able to easily leverage this to also just test long symbols by self generated symbols as another test case. In case its useful to you I've put this in a rebased branch 20241016-modules-symtab branch. Feel free to use as you see fit.
I forget what we concluded on Helge Deller's alignement patches, I think there was an idea on how to address the alignment through other means.
[0] https://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux.git/log/?h=20241016-modules-symtab
Luis
Re: [PATCH v5 14/16] modules: Support extended MODVERSIONS info
Le 17/10/2024 à 01:21, Luis Chamberlain a écrit :
Quoted 13 lines · Matthew Maurer
Quoted 11 lines
So, the basic things I can think of to test here are:
1. The kernel can still load the previous MODVERSIONS format
2. The kernel can load the new MODVERSIONS format
3. If we artificially tweak a CRC in the previous format, it will fail to load.
4. If we artificially tweak a CRC in the new format, it will fail to load.
5. WithCONFIG_EXTENDED_MODVERSIONSenabled, the kernel will build and load modules with long symbol names, with MODVERSIONS enabled.Is there anything else you were thinking of here, or are those the kinds of checks you were envisioning?
Quoted 11 lines
That sounds great. Yeah, the above would be great to test. A while ago I wrote a new modules selftests in order to test possible improvements on
find_symbol()but I also did this due to push the limits of the numbers of symbols we could support. I wrote all this to also test the possible 64-bit alignment benefits of__ksymtab_sections on architectures withoutCONFIG_HAVE_ARCH_PREL32_RELOCATIONS(e.g. ppc64, ppc64le, parisc, s390x,...). But come to think of it, you might be able to easily leverage this to also just test long symbols by self generated symbols as another test case. In case its useful to you I've put this in a rebased branch 20241016-modules-symtab branch. Feel free to use as you see fit.
By reading this, I discovered that was initially added to powerpc by commit 271ca788774a ("arch: enable relative relocations for arm64, power and x86") and then removed due to problem with modules, see commit ff69279a44e9 ("powerpc: disable support for relative ksymtab references")
Wouldn't it be better to try and fix modules and activate again CONFIG_HAVE_ARCH_PREL32_RELOCATIONS on powerpc ?
Christophe
Re: [PATCH v5 14/16] modules: Support extended MODVERSIONS info
In reply to Luis ChamberlainHi Luis,
Quoted 8 lines · Luis Chamberlain
That sounds great. Yeah, the above would be great to test. A while ago I wrote a new modules selftests in order to test possible improvements on
find_symbol()but I also did this due to push the limits of the numbers of symbols we could support. I wrote all this to also test the possible 64-bit alignment benefits of__ksymtab_sections on architectures withoutCONFIG_HAVE_ARCH_PREL32_RELOCATIONS(e.g. ppc64, ppc64le, parisc, s390x,...). [....]
Quoted 4 lines
I forget what we concluded on Helge Deller's alignement patches, I think there was an idea on how to address the alignment through other means.
[0] https://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux.git/log/?h=20241016-modules-symtab
I stumbled upon the unaligned-memory-access.rst document [1]. Please read it, as it is a really good document, and the section "Why unaligned access is bad" states:
It should be obvious from the above that if your code causes unaligned memory accesses to happen, your code will not work correctly on certain platforms and will cause performance problems on others.
With this in mind, you really should apply both of my alignment patches which you currently carry in [0].
For parisc I partly solved the issue by fixing the arch-specific kernel unalignment handler, but every time module sections are stored unaligned, it triggers performance degregation on parisc (and other sensitive platforms).
I suggest you apply them unconditionally.
Helge
Luis Chamberlain
maintainerRe: [PATCH v5 14/16] modules: Support extended MODVERSIONS info
Quoted 30 lines · Helge Deller
Hi Luis,
Quoted 8 lines · Luis Chamberlain
That sounds great. Yeah, the above would be great to test. A while ago I wrote a new modules selftests in order to test possible improvements on
find_symbol()but I also did this due to push the limits of the numbers of symbols we could support. I wrote all this to also test the possible 64-bit alignment benefits of__ksymtab_sections on architectures withoutCONFIG_HAVE_ARCH_PREL32_RELOCATIONS(e.g. ppc64, ppc64le, parisc, s390x,...). [....]Quoted 4 lines
I forget what we concluded on Helge Deller's alignement patches, I think there was an idea on how to address the alignment through other means.
[0] https://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux.git/log/?h=20241016-modules-symtab
I stumbled upon the
unaligned-memory-access.rstdocument [1]. Please read it, as it is a really good document, and the section "Why unaligned access is bad" states:
It should be obvious from the above that if your code causes unaligned memory accesses to happen, your code will not work correctly on certain platforms and will cause performance problems on others.With this in mind, you really should apply both of my alignment patches which you currently carry in [0].
For parisc I partly solved the issue by fixing the arch-specific kernel unalignment handler, but every time module sections are stored unaligned, it triggers performance degregation on parisc (and other sensitive platforms).
Quoted 5 lines
I suggest you apply them unconditionally.
Helge
You're right, I've just referred to that doc and pushed to the new linux modules [2] modules-next branch. This is also great timing so that the work that is ongoing for Rust will take this into consideration as well. I'll just post the test I wrote as separate thing but it surely can be used to help test some of this later.
[2] git://git.kernel.org/pub/scm/linux/kernel/git/modules/linux.git
Luis
Luis Chamberlain
maintainerRe: [PATCH v5 14/16] modules: Support extended MODVERSIONS info
Quoted 37 lines · Luis Chamberlain
Quoted 30 lines · Helge Deller
Hi Luis,
Quoted 8 lines · Luis Chamberlain
That sounds great. Yeah, the above would be great to test. A while ago I wrote a new modules selftests in order to test possible improvements on
find_symbol()but I also did this due to push the limits of the numbers of symbols we could support. I wrote all this to also test the possible 64-bit alignment benefits of__ksymtab_sections on architectures withoutCONFIG_HAVE_ARCH_PREL32_RELOCATIONS(e.g. ppc64, ppc64le, parisc, s390x,...). [....]Quoted 4 lines
I forget what we concluded on Helge Deller's alignement patches, I think there was an idea on how to address the alignment through other means.
[0] https://git.kernel.org/pub/scm/linux/kernel/git/mcgrof/linux.git/log/?h=20241016-modules-symtab
I stumbled upon the
unaligned-memory-access.rstdocument [1]. Please read it, as it is a really good document, and the section "Why unaligned access is bad" states:
It should be obvious from the above that if your code causes unaligned memory accesses to happen, your code will not work correctly on certain platforms and will cause performance problems on others.With this in mind, you really should apply both of my alignment patches which you currently carry in [0].
For parisc I partly solved the issue by fixing the arch-specific kernel unalignment handler, but every time module sections are stored unaligned, it triggers performance degregation on parisc (and other sensitive platforms).
Quoted 5 lines
I suggest you apply them unconditionally.
Helge
Quoted 7 lines
You're right, I've just referred to that doc and pushed to the new linux modules [2] modules-next branch. This is also great timing so that the work that is ongoing for Rust will take this into consideration as well. I'll just post the test I wrote as separate thing but it surely can be used to help test some of this later.
[2]
git://git.kernel.org/pub/scm/linux/kernel/git/modules/linux.git
Helge,
I went down memory lane and noted Masahiro asked for this to be done in asm so I droped your patches. Feel free to post a new iteration.
Luis
PATCH v5 15/16 modpost: Produce extended modversion information
In reply to Matthew MaurerGenerate both the existing modversions format and the new extended one when running modpost.
We no longer generate an error on long symbols in modpost, as they can now be appropriately encoded in the extended section. These symbols will be skipped in the previous encoding.
--- scripts/mod/modpost.c | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-)
scripts/mod/modpost.c +36 -3
@@ -1840,15 +1840,48 @@ static void add_versions(struct buffer *b, struct module *mod)continue ; }if (strlen (s->name) >=MODULE_NAME_LEN ) { -error ("too long symbol \"%s\" [%s.ko]\n" , - s->name, mod->name); -break ; +/* this symbol will only be in the extended info */ +continue ; }buf_printf (b,"\t{ %#8x, \"%s\" },\n" , s->crc, s->name); }buf_printf (b,"};\n" ); + +buf_printf (b,"static const s32 ____version_ext_crcs[]\n" ); +buf_printf (b,"__used __section(\"__version_ext_crcs\") = {\n" ); +list_for_each_entry (s, &mod->unresolved_symbols, list) { +if (!s->module) +continue ; +if (!s->crc_valid) { +/* +* We already warned on this when producing the legacy +* modversions table. +*/ +continue ; + } +buf_printf (b,"\t%#8x,\n" , s->crc); + } +buf_printf (b,"};\n" ); + +buf_printf (b,"static const char ____version_ext_names[]\n" ); +buf_printf (b,"__used __section(\"__version_ext_names\") =\n" ); +list_for_each_entry (s, &mod->unresolved_symbols, list) { +if (!s->module) +continue ; +if (!s->crc_valid) { +/* +* We already warned on this when producing the legacy +* modversions table. +* We need to skip its name too, as the indexes in +* both tables need to align. +*/ +continue ; + } +buf_printf (b,"\t\"%s\\0\"\n" , s->name); + } +buf_printf (b,";\n" ); }static void add_depends (struct buffer *b,struct module *mod)
Signature · 1 line
2.46.1.824.gd892dcdcdd-goog
PATCH v5 16/16 export_report: Use new version info format
In reply to Matthew MaurerThe new version info format has a superset of symbols in the old format. Since this is a tool for in-tree modules, we don't need to parse the old one with this tool any longer.
--- scripts/export_report.pl | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-)
scripts/export_report.pl +10 -12
@@ -114,31 +114,29 @@ foreach my $thismod (@allcfiles) { } my $state=0; + # State map: + # 0 - Looking for names + # 1 - Scanning names + # 2 - Done while ( <$module> ) { chomp; if ($state == 0) { - $state = 1 if ($_ =~ /static const struct modversion_info/); + $state = 1 if ($_ =~ /__used __section\("__version_ext_names"\)/); next; } if ($state == 1) { - $state = 2 if ($_ =~ /__used __section\("__versions"\)/); - next; - } - if ($state == 2) { - if ( $_ =~ /};/ ) { - $state = 3; - next; - } - if ( $_ !~ /0x[0-9a-f]+,/ ) { + if ( $_ =~ /;/ ) { + $state = 2; next; } - my $sym = (split /([,"])/,)[4]; + $_ =~ /"(.*)\\0"/; + my $sym = $1; my ($module, $value, $symbol, $gpl) = @{$SYMBOL{$sym}}; $SYMBOL{ $sym } = [ $module, $value+1, $symbol, $gpl]; push(@{$MODULE{$thismod}} , $sym); } } - if ($state != 3) { + if ($state != 2) { warn "WARNING:$thismod is not built with CONFIG_MODVERSIONS enabled\n"; $modversion_warnings++; }
Signature · 1 line
2.46.1.824.gd892dcdcdd-goog
Hi Matt,
Quoted 42 lines · Matthew Maurer
This patch series is intended for use alongside the Implement MODVERSIONS for RUST [1] series as a replacement for the symbol name hashing approach used there to enable RUST and MODVERSIONS at the same time.
Elsewhere, we've seen a desire for long symbol name support for LTO symbol names [2], and the previous series came up [3] as a possible solution rather than hashing, which some have objected [4] to.
This series adds a MODVERSIONS format which uses a section per column. This avoids userspace tools breaking if we need to make a similar change to the format in the future - we would do so by adding a new section, rather than editing the struct definition. In the new format, the name section is formatted as a concatenated sequence of NUL-terminated strings, which allows for arbitrary length names.
Currently, this series emits both the extended format and the current format on all modules, and prefers the extended format when checking if present. I'm open to various other policies via
Kconfigknobs, but this seemed like a good initial default.The refactor to MODVERSIONS is prefixed to this series as result of an explicit request [5] by Luis in response to the original patchset.
If you are testing this patch alongside RUST by manually removing the !MODVERSIONS restriction (this series doesn't remove it, because the CRCs don't mean what we'd want them to yet, we need the DWARF patch for that) and have kernel hardening enabled, you may need the CPU Mitigations [6] series. Without it, the foo.mod.o file produced by the C compiler will reference
__x86_return_thunk, but foo.o will not. This means that the version table will not contain a version for__x86_return_thunk, but foo.ko will reference it, which will result in a version check failure.[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lore.kernel.org/lkml/[email protected]/
[4] https://lore.kernel.org/lkml/[email protected]/
[5] https://lore.kernel.org/lkml/ZVZNh%[email protected]/
[6] https://lore.kernel.org/all/[email protected]/
Quoted 3 lines
Changes in v5:
- Addresses Sami's comments from v3 that I missed in v4 (missing early return, extra parens)
v5 looks good to me, thank you for fixing these issues. I tested this with my latest Rust modversions patches [1] and everything seems to work for me (on x86_64, arm64, and riscv64 w/ clang). For the series:
[1] https://github.com/samitolvanen/linux/commits/rustmodversions-v3
Sami
Quoted 84 lines · Matthew Maurer
This patch series is intended for use alongside the Implement MODVERSIONS for RUST [1] series as a replacement for the symbol name hashing approach used there to enable RUST and MODVERSIONS at the same time.
Elsewhere, we've seen a desire for long symbol name support for LTO symbol names [2], and the previous series came up [3] as a possible solution rather than hashing, which some have objected [4] to.
This series adds a MODVERSIONS format which uses a section per column. This avoids userspace tools breaking if we need to make a similar change to the format in the future - we would do so by adding a new section, rather than editing the struct definition. In the new format, the name section is formatted as a concatenated sequence of NUL-terminated strings, which allows for arbitrary length names.
Currently, this series emits both the extended format and the current format on all modules, and prefers the extended format when checking if present. I'm open to various other policies via
Kconfigknobs, but this seemed like a good initial default.The refactor to MODVERSIONS is prefixed to this series as result of an explicit request [5] by Luis in response to the original patchset.
If you are testing this patch alongside RUST by manually removing the !MODVERSIONS restriction (this series doesn't remove it, because the CRCs don't mean what we'd want them to yet, we need the DWARF patch for that) and have kernel hardening enabled, you may need the CPU Mitigations [6] series. Without it, the foo.mod.o file produced by the C compiler will reference
__x86_return_thunk, but foo.o will not. This means that the version table will not contain a version for__x86_return_thunk, but foo.ko will reference it, which will result in a version check failure.[1] https://lore.kernel.org/all/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/
[3] https://lore.kernel.org/lkml/[email protected]/
[4] https://lore.kernel.org/lkml/[email protected]/
[5] https://lore.kernel.org/lkml/ZVZNh%[email protected]/
[6] https://lore.kernel.org/all/[email protected]/Changes in v5:
- Addresses Sami's comments from v3 that I missed in v4 (missing early return, extra parens)v4: https://lore.kernel.org/asahi/[email protected]/
- Fix incorrect dot munging in PPCv3: https://lore.kernel.org/lkml/[email protected]/T/
- Split up the module verification refactor into smaller patches, per Greg K-H's suggestion.v2: https://lore.kernel.org/all/[email protected]/
- Add loading/verification refactor before modifying, per Luis's requestv1: https://lore.kernel.org/rust-for-linux/[email protected]/
Matthew Maurer (16):
module: Take const arg invalidate_section_offset
module: Factor outelf_validity_ehdr
module: Factor outelf_validity_cache_sechdrs
module: Factor outelf_validity_cache_secstrings
module: Factor outelf_validity_cache_index_info
module: Factor outelf_validity_cache_index_mod
module: Factor outelf_validity_cache_index_sym
module: Factor outelf_validity_cache_index_str
module: Group section index calculations together
module: Factor outelf_validity_cache_strtab
module: Additional validation inelf_validity_cache_strtab
module: Reformat struct for code style
export_report: Rehabilitate script
modules: Support extended MODVERSIONS info
modpost: Produce extended modversion information
export_report: Use new version info formatarch/powerpc/kernel/module_64.c | 23 +- kernel/module/internal.h | 18 +- kernel/module/main.c | 647 ++++++++++++++++++++++++-------- kernel/module/version.c | 45 +++ scripts/export_report.pl | 17 +- scripts/mod/modpost.c | 39 +- 6 files changed, 628 insertions(+), 161 deletions(-)
Quoted 4 lines
--
2.46.1.824.gd892dcdcdd-goog
The series looks straightforward and reasonable to me.
--
真実はいつも一つ!/ Always, there's only one truth!