diff mbox series

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

Message ID 20241021203846.930-1-esalomatkina@ispras.ru (mailing list archive)
State New
Headers show
Series [v2] sumversion: Fix a memory leak in get_src_version() | expand

Commit Message

Elena Salomatkina Oct. 21, 2024, 8:38 p.m. UTC
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>
---
v2: Rename 'orig' to 'pos'

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

Comments

Masahiro Yamada Oct. 22, 2024, 6:24 p.m. UTC | #1
On Tue, Oct 22, 2024 at 5:39 AM Elena Salomatkina
<esalomatkina@ispras.ru> wrote:
>
> 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>
> ---
> v2: Rename 'orig' to 'pos'

No, I did not ask this. Please read my previous email.

 'p' or 'pos' : advances every time strsep() is called
 'buf'  : hold the allocated buffer and is passed to free()




>
>  scripts/mod/sumversion.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/scripts/mod/sumversion.c b/scripts/mod/sumversion.c
> index 6bf9caca0968..03774957d8b9 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];
> @@ -393,7 +393,7 @@ void get_src_version(const char *modname, char sum[], unsigned sumlen)
>         /* objects for a module are listed in the first line of *.mod file. */
>         snprintf(filelist, sizeof(filelist), "%s.mod", modname);
>
> -       buf = read_text_file(filelist);
> +       pos = buf = read_text_file(filelist);
>
>         md4_init(&md);
>         while ((fname = strsep(&buf, "\n"))) {
> @@ -406,5 +406,5 @@ void get_src_version(const char *modname, char sum[], unsigned sumlen)
>
>         md4_final_ascii(&md, sum, sumlen);
>  free:
> -       free(buf);
> +       free(pos);
>  }
> --
> 2.33.0
>
diff mbox series

Patch

diff --git a/scripts/mod/sumversion.c b/scripts/mod/sumversion.c
index 6bf9caca0968..03774957d8b9 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];
@@ -393,7 +393,7 @@  void get_src_version(const char *modname, char sum[], unsigned sumlen)
 	/* objects for a module are listed in the first line of *.mod file. */
 	snprintf(filelist, sizeof(filelist), "%s.mod", modname);
 
-	buf = read_text_file(filelist);
+	pos = buf = read_text_file(filelist);
 
 	md4_init(&md);
 	while ((fname = strsep(&buf, "\n"))) {
@@ -406,5 +406,5 @@  void get_src_version(const char *modname, char sum[], unsigned sumlen)
 
 	md4_final_ascii(&md, sum, sumlen);
 free:
-	free(buf);
+	free(pos);
 }