mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Weigang He <geoffreyhe2@gmail.com>
To: Josh Poimboeuf <jpoimboe@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>
Cc: linux-kernel@vger.kernel.org, Weigang He <geoffreyhe2@gmail.com>
Subject: [PATCH] objtool: Fix memory leak in elf_alloc_reloc() on realloc failure
Date: Sun, 18 Jan 2026 06:56:43 +0000	[thread overview]
Message-ID: <20260118065643.924837-1-geoffreyhe2@gmail.com> (raw)

When realloc() fails in elf_alloc_reloc(), the original buffer pointer
is overwritten with NULL before the failure is detected. This causes
the original buffer to become unreachable, resulting in a memory leak.

Fix this by using a temporary variable to hold the realloc() result.
If realloc() fails, free the original buffer and set d_buf to NULL to
maintain the expected error state before returning -1.

This bug is found by my static analysis tool and my code review.

Signed-off-by: Weigang He <geoffreyhe2@gmail.com>
---
 tools/objtool/elf.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c
index 6a8ed9c62323e..e47c5c4f25314 100644
--- a/tools/objtool/elf.c
+++ b/tools/objtool/elf.c
@@ -1521,12 +1521,15 @@ static int elf_alloc_reloc(struct elf *elf, struct section *rsec)
 		memcpy(rsec->data->d_buf, orig_buf,
 		       nr_relocs_old * elf_rela_size(elf));
 	} else {
-		rsec->data->d_buf = realloc(rsec->data->d_buf,
-					    nr_alloc * elf_rela_size(elf));
-		if (!rsec->data->d_buf) {
+		void *new_d_buf = realloc(rsec->data->d_buf,
+					  nr_alloc * elf_rela_size(elf));
+		if (!new_d_buf) {
 			ERROR_GLIBC("realloc");
+			free(rsec->data->d_buf);
+			rsec->data->d_buf = NULL;
 			return -1;
 		}
+		rsec->data->d_buf = new_d_buf;
 	}
 
 	rsec->nr_alloc_relocs = nr_alloc;
-- 
2.34.1


             reply	other threads:[~2026-01-18  6:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-18  6:56 Weigang He [this message]
2026-01-19 12:08 ` Markus Elfring
2026-01-19 12:38 ` Peter Zijlstra

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=20260118065643.924837-1-geoffreyhe2@gmail.com \
    --to=geoffreyhe2@gmail.com \
    --cc=jpoimboe@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterz@infradead.org \
    /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

all inboxes | Powered by JetHome®