[PATCH v3] sumversion: Fix a memory leak in get_src_version()

Elena Salomatkina posted 1 patch 1 month ago
scripts/mod/sumversion.c | 7 ++++---
1 file changed, 4 insertions(+), 3 deletions(-)
[PATCH v3] sumversion: Fix a memory leak in get_src_version()
Posted by Elena Salomatkina 1 month ago
strsep() modifies its first argument - buf.
If an error occurs in the parsing loop, an invalid pointer
will be passed to the free() function.
Make the pointer passed to free() match the return value of
read_text_file().

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

Fixes: 9413e7640564 ("kbuild: split the second line of *.mod into *.usyms")
Signed-off-by: Elena Salomatkina <esalomatkina@ispras.ru>
---
v3: Use 'pos' as the moving pointer
v2: Rename 'orig' to 'pos'

 scripts/mod/sumversion.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/scripts/mod/sumversion.c b/scripts/mod/sumversion.c
index 6bf9caca0968..9b552c6efb40 100644
--- a/scripts/mod/sumversion.c
+++ b/scripts/mod/sumversion.c
@@ -385,7 +385,7 @@ static int parse_source_files(const char *objfile, struct md4_ctx *md)
 /* Calc and record src checksum. */
 void get_src_version(const char *modname, char sum[], unsigned sumlen)
 {
-	char *buf;
+	char *buf, *pos;
 	struct md4_ctx md;
 	char *fname;
 	char filelist[PATH_MAX + 1];
@@ -394,9 +394,10 @@ void get_src_version(const char *modname, char sum[], unsigned sumlen)
 	snprintf(filelist, sizeof(filelist), "%s.mod", modname);
 
 	buf = read_text_file(filelist);
-
+	pos = buf;
+
 	md4_init(&md);
-	while ((fname = strsep(&buf, "\n"))) {
+	while ((fname = strsep(&pos, "\n"))) {
 		if (!*fname)
 			continue;
 		if (!(is_static_library(fname)) &&
-- 
2.33.0
Re: [PATCH v3] sumversion: Fix a memory leak in get_src_version()
Posted by Masahiro Yamada 1 month ago
On Wed, Oct 23, 2024 at 6:38 AM Elena Salomatkina
<esalomatkina@ispras.ru> wrote:
>
> strsep() modifies its first argument - buf.
> If an error occurs in the parsing loop, an invalid pointer

I deleted "If an error occurs in the parsing loop," because
this is _always_ a memory leak.

Applied to linux-kbuild/fixes. Thanks.

> will be passed to the free() function.
> Make the pointer passed to free() match the return value of
> read_text_file().
>
> Found by Linux Verification Center (linuxtesting.org) with SVACE.
>
> Fixes: 9413e7640564 ("kbuild: split the second line of *.mod into *.usyms")
> Signed-off-by: Elena Salomatkina <esalomatkina@ispras.ru>
> ---
> v3: Use 'pos' as the moving pointer
> v2: Rename 'orig' to 'pos'
>
>  scripts/mod/sumversion.c | 7 ++++---
>  1 file changed, 4 insertions(+), 3 deletions(-)
>
> diff --git a/scripts/mod/sumversion.c b/scripts/mod/sumversion.c
> index 6bf9caca0968..9b552c6efb40 100644
> --- a/scripts/mod/sumversion.c
> +++ b/scripts/mod/sumversion.c
> @@ -385,7 +385,7 @@ static int parse_source_files(const char *objfile, struct md4_ctx *md)
>  /* Calc and record src checksum. */
>  void get_src_version(const char *modname, char sum[], unsigned sumlen)
>  {
> -       char *buf;
> +       char *buf, *pos;
>         struct md4_ctx md;
>         char *fname;
>         char filelist[PATH_MAX + 1];
> @@ -394,9 +394,10 @@ void get_src_version(const char *modname, char sum[], unsigned sumlen)
>         snprintf(filelist, sizeof(filelist), "%s.mod", modname);
>
>         buf = read_text_file(filelist);
> -
> +       pos = buf;
> +
>         md4_init(&md);
> -       while ((fname = strsep(&buf, "\n"))) {
> +       while ((fname = strsep(&pos, "\n"))) {
>                 if (!*fname)
>                         continue;
>                 if (!(is_static_library(fname)) &&
> --
> 2.33.0
>


-- 
Best Regards
Masahiro Yamada