Linux Modules
 help / color / mirror / Atom feed
From: Laxman Acharya Padhya <acharyalaxman8848@gmail.com>
To: samitolvanen@google.com, mcgrof@kernel.org, petr.pavlu@suse.com,
	da.gomez@kernel.org
Cc: atomlin@atomlin.com, linux-modules@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH] module: reject malformed .modinfo sections
Date: Tue, 28 Jul 2026 23:18:18 +0545	[thread overview]
Message-ID: <20260728173318.63736-1-acharyalaxman8848@gmail.com> (raw)

get_modinfo() and module_next_tag_pair() parse .modinfo as a sequence of NUL-terminated tag=value strings. elf_validity_cache_index_info() caches the .modinfo section index and immediately calls get_modinfo() to find the module name for early error reporting, but it does not verify that the section is non-empty or NUL-terminated first.

A malformed module can therefore make the loader read past the end of .modinfo while searching for a tag.

Reject empty .modinfo sections and sections that are not NUL-terminated before parsing them.

Signed-off-by: Laxman Acharya Padhya <acharyalaxman8848@gmail.com>
---
 kernel/module/main.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/kernel/module/main.c b/kernel/module/main.c
index 46dd8d25a..7450319f9 100644
--- a/kernel/module/main.c
+++ b/kernel/module/main.c
@@ -2083,6 +2083,8 @@ static int elf_validity_cache_secstrings(struct load_info *info)
 static int elf_validity_cache_index_info(struct load_info *info)
 {
 	int info_idx;
+	Elf_Shdr *info_shdr;
+	char *modinfo;
 
 	info_idx = find_any_unique_sec(info, ".modinfo");
 
@@ -2096,6 +2098,18 @@ static int elf_validity_cache_index_info(struct load_info *info)
 	}
 
 	info->index.info = info_idx;
+	info_shdr = &info->sechdrs[info_idx];
+	modinfo = (char *)info->hdr + info_shdr->sh_offset;
+
+	if (info_shdr->sh_size == 0) {
+		pr_err("empty .modinfo section\n");
+		return -ENOEXEC;
+	}
+	if (modinfo[info_shdr->sh_size - 1] != '\0') {
+		pr_err(".modinfo section isn't NUL terminated\n");
+		return -ENOEXEC;
+	}
+
 	/* Try to find a name early so we can log errors with a module name */
 	info->name = get_modinfo(info, "name");
 
-- 
2.51.2


             reply	other threads:[~2026-07-28 17:33 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-28 17:33 Laxman Acharya Padhya [this message]
2026-07-28 17:47 ` [PATCH] module: reject malformed .modinfo sections sashiko-bot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260728173318.63736-1-acharyalaxman8848@gmail.com \
    --to=acharyalaxman8848@gmail.com \
    --cc=atomlin@atomlin.com \
    --cc=da.gomez@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-modules@vger.kernel.org \
    --cc=mcgrof@kernel.org \
    --cc=petr.pavlu@suse.com \
    --cc=samitolvanen@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox