2022-07-27 16:03:35

by Stanislav Goriainov

[permalink] [raw]
Subject: [PATCH] objtool: Fix memory leak in create_static_call_section()

On some paths of function create_static_call_section() the memory,
allocated for key_name via strdup(), is not freed. Even though this
is a utility function, and all memory will be freed automatically
after the process is finished, there is free(key_sym) in some cases,
so it would be sensible to always deallocate the memory.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes: 1e7e47883830 ("x86/static_call: Add inline static call implementation for x86-64")
Signed-off-by: Stanislav Goriainov <[email protected]>
---
tools/objtool/check.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index b341f8a8c7c5..abbb3ab8b263 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -666,6 +666,7 @@ static int create_static_call_sections(struct objtool_file *file)
if (strncmp(key_name, STATIC_CALL_TRAMP_PREFIX_STR,
STATIC_CALL_TRAMP_PREFIX_LEN)) {
WARN("static_call: trampoline name malformed: %s", key_name);
+ free(key_name);
return -1;
}
tmp = key_name + STATIC_CALL_TRAMP_PREFIX_LEN - STATIC_CALL_KEY_PREFIX_LEN;
@@ -675,6 +676,7 @@ static int create_static_call_sections(struct objtool_file *file)
if (!key_sym) {
if (!opts.module) {
WARN("static_call: can't find static_call_key symbol: %s", tmp);
+ free(key_name);
return -1;
}

-- 2.34.1


2022-07-27 16:24:33

by Peter Zijlstra

[permalink] [raw]
Subject: Re: [PATCH] objtool: Fix memory leak in create_static_call_section()

On Wed, Jul 27, 2022 at 06:48:25PM +0300, Stanislav Goriainov wrote:
> On some paths of function create_static_call_section() the memory,
> allocated for key_name via strdup(), is not freed. Even though this
> is a utility function, and all memory will be freed automatically
> after the process is finished, there is free(key_sym) in some cases,
> so it would be sensible to always deallocate the memory.
>
> Found by Linux Verification Center (linuxtesting.org) with SVACE.
>

This is a run-once die on error program. This is pointless and most
certainly doesn't 'fix' anything.