module: validate string table section types
PATCH 1/1 module: validate string table section types
In elf_validity_cache_sechdrs, section sizes and offsets are validated, unless the section type is SHT_NULL or SHT_NOBITS.
Later, elf_validity_cache_secstrings and elf_validity_cache_index_str access the section name table (.shstrtab) and symbol string table (.strtab) headers without first ensuring that their types are SHT_STRTAB. If a section type is SHT_NULL or SHT_NOBITS, sh_offset has not been validated and may reference out-of-bounds memory when dereferenced in elf_validity_cache_secstrings or elf_validity_cache_strtab.
Validate that both string section headers are of type SHT_STRTAB before caching them.
--- kernel/module/main.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)
kernel/module/main.c +13 -1
@@ -2011,6 +2011,7 @@ static int elf_validity_cache_sechdrs(struct load_info *info)* Specifically checks: * * * Section name table index is inbounds of section headers +* * Section name table type is SHT_STRTAB * * Section name table is not empty * * Section name table is NUL terminated * * All section name offsets are inbounds of the section
@@ -2038,6 +2039,11 @@ static int elf_validity_cache_secstrings(struct load_info *info) strhdr = &info->sechdrs[info->hdr->e_shstrndx]; +if (strhdr->sh_type !=SHT_STRTAB ) { +pr_err ("Invalid ELF section name table type: %u\n" , strhdr->sh_type); +return -ENOEXEC ; + } +/* * The section name table must be NUL-terminated, as required * by the spec. This makes strcmp and pr_* calls that access
@@ -2204,7 +2210,7 @@ static int elf_validity_cache_index_sym(struct load_info *info)* 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. +* in-bounds and of type SHT_STRTAB, and caches it. * * Return: %0 if valid, %-ENOEXEC on failure. */
@@ -2218,6 +2224,12 @@ static int elf_validity_cache_index_str(struct load_info *info)return -ENOEXEC ; } +if (info->sechdrs[str_idx].sh_type !=SHT_STRTAB ) { +pr_err ("Invalid ELF symbol string table type: %u\n" , + info->sechdrs[str_idx].sh_type); +return -ENOEXEC ; + } + info->index.str = str_idx;return 0 ; }
Signature · 1 line
2.55.0.795.g602f6c329a-goog
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
Pre-existing issues:
- [High] The patch fixes an out-of-bounds read vulnerability for string tables by ensuring they are of type SHT_STRTAB, but fails to address the exact same vulnerability class for several other ELF sections (e.g., .modinfo, .gnu.linkonce.this_module, __version_ext_names, __ksymtab).
- [High] Iterating over sections in lookup functions triggers an out-of-bounds string read for SHT_NULL sections due to missing sh_name bounds checking.
- [High] Validation logic for section headers accesses sechdrs[0] out of bounds if e_shnum is 0 and e_shoff is near the end of the file.
--
--- Patch [1]: [PATCH] module: validate string table section types --- commit e8e815fedf365febe70f5870d807616d37946281
Author: Thiébaud Weksteen <[email protected]>
module: validate string table section types
In elf_validity_cache_sechdrs, section sizes and offsets are validated, unless the section type is SHT_NULL or SHT_NOBITS.
Later, elf_validity_cache_secstrings and elf_validity_cache_index_str access the section name table (.shstrtab) and symbol string table (.strtab) headers without first ensuring that their types are SHT_STRTAB. If a section type is SHT_NULL or SHT_NOBITS, sh_offset has not been validated and may reference out-of-bounds memory when dereferenced in elf_validity_cache_secstrings or elf_validity_cache_strtab.
Validate that both string section headers are of type SHT_STRTAB before caching them.
kernel/module/main.c static int elf_validity_cache_sechdrs(struct load_info *info)
Quoted 5 lines
kernel/module/main.c +0
-0@@ -2011,6 +2011,7 @@ static int elf_validity_cache_sechdrs(struct load_info *info)
[Severity: High]
This is a pre-existing issue, but does the validation logic in elf_validity_cache_sechdrs() access sechdrs out of bounds if e_shnum is 0 and e_shoff is near the end of the file?
The overflow check evaluates to 0 > positive if e_shnum is 0 and e_shoff is less than info->len, allowing the check to incorrectly pass:
elf_validity_cache_sechdrs() {
...
if (info->hdr->e_shoff >= info->len
|| (info->hdr->e_shnum * sizeof(Elf_Shdr) >
info->len - info->hdr->e_shoff)) {
...
}The code then sets sechdrs and blindly accesses sechdrs[0], reading past the end of the user-provided module buffer:
elf_validity_cache_sechdrs() {
...
if (sechdrs[0].sh_type != SHT_NULL
|| sechdrs[0].sh_size != 0
|| sechdrs[0].sh_addr != 0) {
...
}Quoted 6 lines
kernel/module/main.c +1
-0* Specifically checks: * * * Section name table index is inbounds of section headers +* * Section name table type is SHT_STRTAB * * Section name table is not empty * * Section name table is NUL terminated
[ ... ]
kernel/module/main.c static int elf_validity_cache_secstrings(struct load_info *info) · line 2046
Quoted 9 lines
kernel/module/main.c +5
-0@@ -2038,6 +2039,11 @@ static int elf_validity_cache_secstrings(struct load_info *info) strhdr = &info->sechdrs[info->hdr->e_shstrndx]; +if (strhdr->sh_type !=SHT_STRTAB ) { +pr_err ("Invalid ELF section name table type: %u\n" , strhdr->sh_type); +return -ENOEXEC ; + } +
[Severity: High]
This is a pre-existing issue, but does iterating over sections in lookup functions trigger an out-of-bounds string read for SHT_NULL sections due to missing sh_name bounds checking?
In elf_validity_cache_secstrings(), bounds validation of sh_name against the string table size is explicitly skipped for SHT_NULL sections:
elf_validity_cache_secstrings() {
...
if (shdr->sh_type == SHT_NULL)
continue;
...
}Later, section lookup functions like find_any_unique_sec() iterate over all sections and blindly pass the unvalidated info->secstrings pointer to strcmp():
find_any_unique_sec() {
...
if (strcmp(info->secstrings + info->sechdrs[i].sh_name,
name) == 0) {
...
}Could this cause an immediate out-of-bounds memory read when strcmp() executes if an attacker sets a huge sh_name on a SHT_NULL section?
[ ... ]
kernel/module/main.c static int elf_validity_cache_index_str(struct load_info *info) · line 2232
Quoted 10 lines
kernel/module/main.c +6
-0@@ -2218,6 +2224,12 @@ static int elf_validity_cache_index_str(struct load_info *info)return -ENOEXEC ; } +if (info->sechdrs[str_idx].sh_type !=SHT_STRTAB ) { +pr_err ("Invalid ELF symbol string table type: %u\n" , + info->sechdrs[str_idx].sh_type); +return -ENOEXEC ; + } +
[Severity: High]
This is a pre-existing issue, but while this patch fixes an out-of-bounds read vulnerability for string tables by ensuring they are of type SHT_STRTAB, does the same vulnerability class exist for other ELF sections?
Since elf_validity_cache_sechdrs() skips bounds checking of sh_offset and sh_size for SHT_NULL and SHT_NOBITS sections, lookup functions can match sections by name while ignoring sh_type.
For example, elf_validity_cache_index_info() looks up .modinfo by name and caches it without validating its sh_type:
elf_validity_cache_index_info() {
...
info_idx = find_any_unique_sec(info, ".modinfo");
...
}Similarly, in elf_validity_cache_index_mod(), the .gnu.linkonce.this_module section rejects SHT_NOBITS but fails to reject SHT_NULL:
elf_validity_cache_index_mod() {
...
if (shdr->sh_type == SHT_NOBITS) {
...
}And in move_module(), the loop skips SHT_NOBITS but calls memcpy for SHT_NULL:
move_module() {
...
memcpy(dest, (void *)shdr->sh_addr, shdr->sh_size);
...
}Could a malicious module supply these sections with type SHT_NULL and an arbitrary, unvalidated sh_offset, leading to out-of-bounds reads or copies?
Quoted 3 lines
kernel/module/main.c +0
-0info->index.str = str_idx;return 0 ; }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Petr Pavlu
maintainerQuoted 11 lines · Thiébaud Weksteen
In
elf_validity_cache_sechdrs, section sizes and offsets are validated, unless the section type isSHT_NULLorSHT_NOBITS.Later,
elf_validity_cache_secstringsandelf_validity_cache_index_straccess the section name table (.shstrtab) and symbol string table (.strtab) headers without first ensuring that their types areSHT_STRTAB. If a section type isSHT_NULLorSHT_NOBITS,sh_offsethas not been validated and may reference out-of-bounds memory when dereferenced inelf_validity_cache_secstringsorelf_validity_cache_strtab.
Quoted 2 lines
Validate that both string section headers are of type
SHT_STRTABbefore caching them.
The module loader should normally at least get through the signature and blacklist checks without crashing due to a corrupted module ELF file. Failing to validate the offset+size of .shstrtab means the module loader could crash before the blacklist check, so I believe it is useful to add this validation.
How did you run into this issue? Was it observed in practice with the GNU or LLVM toolchain, or with some manually crafted module?
Signature · 2 lines
Thanks, Petr
Quoted 16 lines · Petr Pavlu
Quoted 11 lines · Thiébaud Weksteen
In
elf_validity_cache_sechdrs, section sizes and offsets are validated, unless the section type isSHT_NULLorSHT_NOBITS.Later,
elf_validity_cache_secstringsandelf_validity_cache_index_straccess the section name table (.shstrtab) and symbol string table (.strtab) headers without first ensuring that their types areSHT_STRTAB. If a section type isSHT_NULLorSHT_NOBITS,sh_offsethas not been validated and may reference out-of-bounds memory when dereferenced inelf_validity_cache_secstringsorelf_validity_cache_strtab.Quoted 2 lines
Validate that both string section headers are of type
SHT_STRTABbefore caching them.
Quoted 8 lines
The module loader should normally at least get through the signature and blacklist checks without crashing due to a corrupted module ELF file. Failing to validate the offset+size of .shstrtab means the module loader could crash before the blacklist check, so I believe it is useful to add this validation.
How did you run into this issue? Was it observed in practice with the GNU or LLVM toolchain, or with some manually crafted module?
Thanks for the review Petr. I found the issue while reading the code. I am working on a separate commit that reuses some of the ELF parsing logic (in a different subsystem, with simpler assertions). I was able to confirm with a basic PoC (reusing a valid .ko and replacing the type and offset of .shstrtab).
Quoted 23 lines · Thiébaud Weksteen
Quoted 22 lines · Petr Pavlu
Quoted 11 lines · Thiébaud Weksteen
In
elf_validity_cache_sechdrs, section sizes and offsets are validated, unless the section type isSHT_NULLorSHT_NOBITS.Later,
elf_validity_cache_secstringsandelf_validity_cache_index_straccess the section name table (.shstrtab) and symbol string table (.strtab) headers without first ensuring that their types areSHT_STRTAB. If a section type isSHT_NULLorSHT_NOBITS,sh_offsethas not been validated and may reference out-of-bounds memory when dereferenced inelf_validity_cache_secstringsorelf_validity_cache_strtab.Quoted 2 lines
Validate that both string section headers are of type
SHT_STRTABbefore caching them.The module loader should normally at least get through the signature and blacklist checks without crashing due to a corrupted module ELF file. Failing to validate the offset+size of .shstrtab means the module loader could crash before the blacklist check, so I believe it is useful to add this validation.
Quoted 8 lines
Quoted 2 lines
How did you run into this issue? Was it observed in practice with the GNU or LLVM toolchain, or with some manually crafted module?
Thanks for the review Petr. I found the issue while reading the code. I am working on a separate commit that reuses some of the ELF parsing logic (in a different subsystem, with simpler assertions). I was able to confirm with a basic PoC (reusing a valid .ko and replacing the type and offset of .shstrtab).
Thanks for the explanation. The change looks ok to me.
-- Petr
…
Quoted 2 lines
Validate that both string section headers are of type
SHT_STRTABbefore caching them.
How do you think about to add any tags (like “Fixes” and “Cc”) accordingly?
Regards,
Markus
Aaron Tomlin
reviewerQuoted 5 lines · Markus Elfring
…
Quoted 2 lines
Validate that both string section headers are of type
SHT_STRTABbefore caching them.How do you think about to add any tags (like “Fixes” and “Cc”) accordingly?
+1
Signature · 1 line
Aaron Tomlin
Quoted 7 lines · Aaron Tomlin
Quoted 5 lines · Markus Elfring
…
Quoted 2 lines
Validate that both string section headers are of type
SHT_STRTABbefore caching them.How do you think about to add any tags (like “Fixes” and “Cc”) accordingly?
Thanks for the suggestion. I can't seem to find any relevant commit for Fixes. There areec2a29593c83e ("module: harden ELF info handling") and potentially 3c5700aeabd87 ("module: Factor out elf_validity_cache_secstrings") Both of these commits already intended to harden the parsing. I don't think there is any regression here.
As for Cc:, I can't think of anyone who should be explicitly added. If you have any suggestions, please chime in.
Petr Pavlu
maintainerQuoted 7 lines · Thiébaud Weksteen
Quoted 6 lines · Aaron Tomlin
Quoted 4 lines · Markus Elfring
…
Quoted 2 lines
Validate that both string section headers are of type
SHT_STRTABbefore caching them.
Quoted 8 lines
Quoted 1 line
Quoted 1 line
How do you think about to add any tags (like “Fixes” and “Cc”) accordingly?
Thanks for the suggestion. I can't seem to find any relevant commit for Fixes. There are
ec2a29593c83e("module: harden ELF info handling") and potentially3c5700aeabd87("module: Factor outelf_validity_cache_secstrings") Both of these commits already intended to harden the parsing. I don't think there is any regression here.
I agree.
Quoted 2 lines
As for Cc:, I can't think of anyone who should be explicitly added. If you have any suggestions, please chime in.
I'll add 'Cc: [email protected]' when queuing the patch.
Signature · 2 lines
Thanks, Petr
Quoted 68 lines · Thiébaud Weksteen
In
elf_validity_cache_sechdrs, section sizes and offsets are validated, unless the section type isSHT_NULLorSHT_NOBITS.Later,
elf_validity_cache_secstringsandelf_validity_cache_index_straccess the section name table (.shstrtab) and symbol string table (.strtab) headers without first ensuring that their types areSHT_STRTAB. If a section type isSHT_NULLorSHT_NOBITS,sh_offsethas not been validated and may reference out-of-bounds memory when dereferenced inelf_validity_cache_secstringsorelf_validity_cache_strtab.Validate that both string section headers are of type
SHT_STRTABbefore caching them.Signed-off-by Thiébaud Weksteen <[email protected]>--- kernel/module/main.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-)kernel/module/main.c +13
-1@@ -2011,6 +2011,7 @@ static int elf_validity_cache_sechdrs(struct load_info *info)* Specifically checks: * * * Section name table index is inbounds of section headers +* * Section name table type is SHT_STRTAB * * Section name table is not empty * * Section name table is NUL terminated * * All section name offsets are inbounds of the section @@ -2038,6 +2039,11 @@ static int elf_validity_cache_secstrings(struct load_info *info) strhdr = &info->sechdrs[info->hdr->e_shstrndx]; +if (strhdr->sh_type !=SHT_STRTAB ) { +pr_err ("Invalid ELF section name table type: %u\n" , strhdr->sh_type); +return -ENOEXEC ; + } +/* * The section name table must be NUL-terminated, as required * by the spec. This makes strcmp and pr_* calls that access @@ -2204,7 +2210,7 @@ static int elf_validity_cache_index_sym(struct load_info *info)* 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. +* in-bounds and of type SHT_STRTAB, and caches it. * * Return: %0 if valid, %-ENOEXEC on failure. */ @@ -2218,6 +2224,12 @@ static int elf_validity_cache_index_str(struct load_info *info)return -ENOEXEC ; } +if (info->sechdrs[str_idx].sh_type !=SHT_STRTAB ) { +pr_err ("Invalid ELF symbol string table type: %u\n" , + info->sechdrs[str_idx].sh_type); +return -ENOEXEC ; + } + info->index.str = str_idx;return 0 ; }Signature · 1 line
2.55.0.795.g602f6c329a-goog
Signature · 1 line
Aaron Tomlin
Petr Pavlu
maintainerQuoted 11 lines · Thiébaud Weksteen
In
elf_validity_cache_sechdrs, section sizes and offsets are validated, unless the section type isSHT_NULLorSHT_NOBITS.Later,
elf_validity_cache_secstringsandelf_validity_cache_index_straccess the section name table (.shstrtab) and symbol string table (.strtab) headers without first ensuring that their types areSHT_STRTAB. If a section type isSHT_NULLorSHT_NOBITS,sh_offsethas not been validated and may reference out-of-bounds memory when dereferenced inelf_validity_cache_secstringsorelf_validity_cache_strtab.
Quoted 4 lines
Validate that both string section headers are of type
SHT_STRTABbefore caching them.Signed-off-by Thiébaud Weksteen <[email protected]>
I've queued the patch on modules-next for v7.3-rc1.
Signature · 2 lines
Thanks, Petr