mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] modpost: prevent leak when early return no suffix .o in read_symbols()
@ 2026-06-24  4:47 Robertus Diawan Chris
  2026-06-30  2:33 ` Nathan Chancellor
  2026-07-24  5:07 ` Nicolas Schier
  0 siblings, 2 replies; 3+ messages in thread
From: Robertus Diawan Chris @ 2026-06-24  4:47 UTC (permalink / raw)
  To: nathan, nsc; +Cc: linux-kbuild, linux-kernel, linux-kernel-mentees, skhan, me

The allocation for elf info symsearch and hdr from parse_elf() haven't
been released when return because of modname didn't have suffix ".o".
And it seems like the suffix ".o" check did not depends on parse_elf()
to succeed first. So, move the suffix ".o" check before checking
parse_elf() result to prevent resource leak when the modname didn't have
suffix ".o" and return early.

This is reported by Coverity Scan as "Resource leak".

Fixes: 8c9ce89c5b63 ("modpost: simplify mod->name allocation")
Signed-off-by: Robertus Diawan Chris <robertusdchris@gmail.com>
---
v1 -> v2:
- Move the suffix ".o" check before parse_elf() instead of adding
  parse_elf_finish() in the suffix ".o" check because the suffix ".o"
  check did not depends on parse_elf() and to minimize overhead handling
  the check (suggested by Nathan Chancellor).
- To make the subject title represents the new change better, change the
  subject title from "modpost: release allocation when early return no
  suffix .o in read_symbols()" to "modpost: prevent leak when early
  return no suffix .o in read_symbols()".

v1:
https://lore.kernel.org/all/20260610052550.187006-1-robertusdchris@gmail.com/

 scripts/mod/modpost.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/scripts/mod/modpost.c b/scripts/mod/modpost.c
index abbcd3fc1394..6eb2714521a7 100644
--- a/scripts/mod/modpost.c
+++ b/scripts/mod/modpost.c
@@ -1580,14 +1580,14 @@ static void read_symbols(const char *modname)
 	struct elf_info info = { };
 	Elf_Sym *sym;
 
-	if (!parse_elf(&info, modname))
-		return;
-
 	if (!strends(modname, ".o")) {
 		error("%s: filename must be suffixed with .o\n", modname);
 		return;
 	}
 
+	if (!parse_elf(&info, modname))
+		return;
+
 	/* strip trailing .o */
 	mod = new_module(modname, strlen(modname) - strlen(".o"));
 

base-commit: 8cd9520d35a6c38db6567e97dd93b1f11f185dc6
-- 
2.54.0


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

* Re: [PATCH v2] modpost: prevent leak when early return no suffix .o in read_symbols()
  2026-06-24  4:47 [PATCH v2] modpost: prevent leak when early return no suffix .o in read_symbols() Robertus Diawan Chris
@ 2026-06-30  2:33 ` Nathan Chancellor
  2026-07-24  5:07 ` Nicolas Schier
  1 sibling, 0 replies; 3+ messages in thread
From: Nathan Chancellor @ 2026-06-30  2:33 UTC (permalink / raw)
  To: Robertus Diawan Chris
  Cc: nathan, nsc, linux-kbuild, linux-kernel, linux-kernel-mentees, skhan, me

On Wed, 24 Jun 2026 11:47:42 +0700, Robertus Diawan Chris <robertusdchris@gmail.com> wrote:
> The allocation for elf info symsearch and hdr from parse_elf() haven't
> been released when return because of modname didn't have suffix ".o".
> And it seems like the suffix ".o" check did not depends on parse_elf()
> to succeed first. So, move the suffix ".o" check before checking
> parse_elf() result to prevent resource leak when the modname didn't have
> suffix ".o" and return early.
> 
> [...]

Reviewed-by: Nathan Chancellor <nathan@kernel.org>

-- 
Cheers,
Nathan


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

* Re: [PATCH v2] modpost: prevent leak when early return no suffix .o in read_symbols()
  2026-06-24  4:47 [PATCH v2] modpost: prevent leak when early return no suffix .o in read_symbols() Robertus Diawan Chris
  2026-06-30  2:33 ` Nathan Chancellor
@ 2026-07-24  5:07 ` Nicolas Schier
  1 sibling, 0 replies; 3+ messages in thread
From: Nicolas Schier @ 2026-07-24  5:07 UTC (permalink / raw)
  To: nathan, Robertus Diawan Chris
  Cc: linux-kbuild, linux-kernel, linux-kernel-mentees, skhan, me

On Wed, 24 Jun 2026 11:47:42 +0700, Robertus Diawan Chris wrote:
> modpost: prevent leak when early return no suffix .o in read_symbols()

Applied to kbuild/linux.git (kbuild-next-unstable), thanks!

[1/1] modpost: prevent leak when early return no suffix .o in read_symbols()
      https://git.kernel.org/kbuild/c/f2308dd4

Please look out for regression or issue reports or other follow up
comments, as they may result in the patch/series getting dropped,
reverted or modified (e.g. trailers).

Patches applied to the kbuild-next-unstable branch are accepted pending
wider testing in linux-next and any post-commit review; they will
generally be moved to the kbuild-next branch in about a week if no
issues are found.

Best regards,
-- 
Nicolas



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

end of thread, other threads:[~2026-07-24  5:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-06-24  4:47 [PATCH v2] modpost: prevent leak when early return no suffix .o in read_symbols() Robertus Diawan Chris
2026-06-30  2:33 ` Nathan Chancellor
2026-07-24  5:07 ` Nicolas Schier

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®