linux-modules.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] module: .strtab must be null terminated
@ 2024-10-19 14:14 Tobias Stoeckmann
  2024-10-21 19:54 ` Luis Chamberlain
  0 siblings, 1 reply; 3+ messages in thread
From: Tobias Stoeckmann @ 2024-10-19 14:14 UTC (permalink / raw)
  To: mcgrof; +Cc: linux-modules, linux-kernel

The string table must be NUL-terminated, just like the section name table.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
---
In order to create a proof of concept, which I can't get into a simple
script right now, it's easiest to move '.strtab' to the end of the module
file, write as many 'A' characters at the end as required according to the
section size, and try to insert the module.

In dmesg, you can see lines like:

```
poc: Unknown symbol AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\xc6\xe9<\xfe\xff\xff\x8b
\xff\xc1\xe9\x03\xf3H\xa5\xeb\xa1I\x8b~ \xe8 (err -2)
```
---
 kernel/module/main.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/kernel/module/main.c b/kernel/module/main.c
index 49b9bca9d..9c5b373a7 100644
--- a/kernel/module/main.c
+++ b/kernel/module/main.c
@@ -1847,6 +1847,18 @@ 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 string table must be NUL-terminated, as required
+	 * by the spec. This makes strcmp and pr_* calls that access
+	 * strings in the section safe.
+	 */
+	strhdr = &info->sechdrs[info->index.str];
+	if (strhdr->sh_size > 0 && info->strtab[strhdr->sh_size - 1] != '\0') {
+		pr_err("module %s: string table isn't null terminated\n",
+		       info->name ?: "(missing .modinfo section or name field)");
+		goto no_exec;
+	}
+
 	/*
 	 * The ".gnu.linkonce.this_module" ELF section is special. It is
 	 * what modpost uses to refer to __this_module and let's use rely
--
2.47.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] module: .strtab must be null terminated
@ 2024-10-19 15:25 Alexey Dobriyan
  0 siblings, 0 replies; 3+ messages in thread
From: Alexey Dobriyan @ 2024-10-19 15:25 UTC (permalink / raw)
  To: Tobias Stoeckmann; +Cc: mcgrof, linux-modules, linux-kernel

> +	strhdr = &info->sechdrs[info->index.str];
> +	if (strhdr->sh_size > 0 && info->strtab[strhdr->sh_size - 1] != '\0') {
> +		pr_err("module %s: string table isn't null terminated\n",
> +		       info->name ?: "(missing .modinfo section or name field)");
> +		goto no_exec;
> +	}

Reviewed-off-by: Alexey Dobriyan <adobriyan@gmail.com>

The _very_ correct test is "there must be NUL between last index used
and the end of the section somewhere".

.shstrtab has the same overly restrictive test:

	if (info->secstrings[strhdr->sh_size - 1] != '\0') {

But, of course, it doesn't matter in practice.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] module: .strtab must be null terminated
  2024-10-19 14:14 [PATCH] module: .strtab must be null terminated Tobias Stoeckmann
@ 2024-10-21 19:54 ` Luis Chamberlain
  0 siblings, 0 replies; 3+ messages in thread
From: Luis Chamberlain @ 2024-10-21 19:54 UTC (permalink / raw)
  To: Tobias Stoeckmann; +Cc: linux-modules, linux-kernel

On Sat, Oct 19, 2024 at 04:14:40PM +0200, Tobias Stoeckmann wrote:
> The string table must be NUL-terminated, just like the section name table.
> 
> Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
> ---
> In order to create a proof of concept, which I can't get into a simple
> script right now, it's easiest to move '.strtab' to the end of the module
> file, write as many 'A' characters at the end as required according to the
> section size, and try to insert the module.
> 
> In dmesg, you can see lines like:
> 
> ```
> poc: Unknown symbol AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\xc6\xe9<\xfe\xff\xff\x8b
> \xff\xc1\xe9\x03\xf3H\xa5\xeb\xa1I\x8b~ \xe8 (err -2)
> ```
> ---

Thanks can you post rebased aginst modules-next on the modules tree [0].
But please consider sending both patches and the new test one in a
series, I'll reply shortly about the other patch.

[0] https://git.kernel.org/pub/scm/linux/kernel/git/modules/linux.git/

  Luisk

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-10-21 19:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-19 14:14 [PATCH] module: .strtab must be null terminated Tobias Stoeckmann
2024-10-21 19:54 ` Luis Chamberlain
  -- strict thread matches above, loose matches on Subject: below --
2024-10-19 15:25 Alexey Dobriyan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).