U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 1/1] fs/squashfs: avoid illegal free() in sqfs_opendir()
@ 2025-04-14 13:19 Heinrich Schuchardt
  2025-04-14 13:35 ` Joao Marcos Costa
  2025-04-22 13:58 ` Tom Rini
  0 siblings, 2 replies; 3+ messages in thread
From: Heinrich Schuchardt @ 2025-04-14 13:19 UTC (permalink / raw)
  To: Joao Marcos Costa
  Cc: Thomas Petazzoni, Miquel Raynal, Richard Weinberger,
	Andrea della Porta, u-boot, Heinrich Schuchardt

* Use calloc() to allocate token_list. This avoids an illegal free if
  sqfs_tokenize() fails.
* Do not iterate over token_list if it has not been allocated.

Addresses-Coverity-ID: 510453:  Null pointer dereferences  (FORWARD_NULL)
Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
---
v2:
	Fix typo in subject line
---
 fs/squashfs/sqfs.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/fs/squashfs/sqfs.c b/fs/squashfs/sqfs.c
index 7c364686f14..2dcdd60f683 100644
--- a/fs/squashfs/sqfs.c
+++ b/fs/squashfs/sqfs.c
@@ -949,7 +949,7 @@ static int sqfs_opendir_nest(const char *filename, struct fs_dir_stream **dirsp)
 		goto out;
 	}
 
-	token_list = malloc(token_count * sizeof(char *));
+	token_list = calloc(token_count, sizeof(char *));
 	if (!token_list) {
 		ret = -EINVAL;
 		goto out;
@@ -987,9 +987,11 @@ static int sqfs_opendir_nest(const char *filename, struct fs_dir_stream **dirsp)
 	*dirsp = (struct fs_dir_stream *)dirs;
 
 out:
-	for (j = 0; j < token_count; j++)
-		free(token_list[j]);
-	free(token_list);
+	if (token_list) {
+		for (j = 0; j < token_count; j++)
+			free(token_list[j]);
+		free(token_list);
+	}
 	free(pos_list);
 	free(path);
 	if (ret) {
-- 
2.48.1


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

* Re: [PATCH v2 1/1] fs/squashfs: avoid illegal free() in sqfs_opendir()
  2025-04-14 13:19 [PATCH v2 1/1] fs/squashfs: avoid illegal free() in sqfs_opendir() Heinrich Schuchardt
@ 2025-04-14 13:35 ` Joao Marcos Costa
  2025-04-22 13:58 ` Tom Rini
  1 sibling, 0 replies; 3+ messages in thread
From: Joao Marcos Costa @ 2025-04-14 13:35 UTC (permalink / raw)
  To: Heinrich Schuchardt, Joao Marcos Costa
  Cc: Thomas Petazzoni, Miquel Raynal, Richard Weinberger,
	Andrea della Porta, u-boot

Hello,

On 4/14/25 15:19, Heinrich Schuchardt wrote:
> * Use calloc() to allocate token_list. This avoids an illegal free if
>    sqfs_tokenize() fails.
> * Do not iterate over token_list if it has not been allocated.
> 
> Addresses-Coverity-ID: 510453:  Null pointer dereferences  (FORWARD_NULL)
> Signed-off-by: Heinrich Schuchardt <heinrich.schuchardt@canonical.com>
> ---
> v2:
> 	Fix typo in subject line
> ---
>   fs/squashfs/sqfs.c | 10 ++++++----
>   1 file changed, 6 insertions(+), 4 deletions(-)
> 
> diff --git a/fs/squashfs/sqfs.c b/fs/squashfs/sqfs.c
> index 7c364686f14..2dcdd60f683 100644
> --- a/fs/squashfs/sqfs.c
> +++ b/fs/squashfs/sqfs.c
> @@ -949,7 +949,7 @@ static int sqfs_opendir_nest(const char *filename, struct fs_dir_stream **dirsp)
>   		goto out;
>   	}
>   
> -	token_list = malloc(token_count * sizeof(char *));
> +	token_list = calloc(token_count, sizeof(char *));
>   	if (!token_list) {
>   		ret = -EINVAL;
>   		goto out;
> @@ -987,9 +987,11 @@ static int sqfs_opendir_nest(const char *filename, struct fs_dir_stream **dirsp)
>   	*dirsp = (struct fs_dir_stream *)dirs;
>   
>   out:
> -	for (j = 0; j < token_count; j++)
> -		free(token_list[j]);
> -	free(token_list);
> +	if (token_list) {
> +		for (j = 0; j < token_count; j++)
> +			free(token_list[j]);
> +		free(token_list);
> +	}
>   	free(pos_list);
>   	free(path);
>   	if (ret) {

Reviewed-by: Joao Marcos Costa <joaomarcos.costa@bootlin.com>
-- 
Best regards,
João Marcos Costa

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

* Re: [PATCH v2 1/1] fs/squashfs: avoid illegal free() in sqfs_opendir()
  2025-04-14 13:19 [PATCH v2 1/1] fs/squashfs: avoid illegal free() in sqfs_opendir() Heinrich Schuchardt
  2025-04-14 13:35 ` Joao Marcos Costa
@ 2025-04-22 13:58 ` Tom Rini
  1 sibling, 0 replies; 3+ messages in thread
From: Tom Rini @ 2025-04-22 13:58 UTC (permalink / raw)
  To: Joao Marcos Costa, Heinrich Schuchardt
  Cc: Thomas Petazzoni, Miquel Raynal, Richard Weinberger,
	Andrea della Porta, u-boot

On Mon, 14 Apr 2025 15:19:24 +0200, Heinrich Schuchardt wrote:

> * Use calloc() to allocate token_list. This avoids an illegal free if
>   sqfs_tokenize() fails.
> * Do not iterate over token_list if it has not been allocated.
> 
> 

Applied to u-boot/master, thanks!

[1/1] fs/squashfs: avoid illegal free() in sqfs_opendir()
      commit: 185fdf5e94731df05748b1c576effb52ff7a3ec5
-- 
Tom



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

end of thread, other threads:[~2025-04-22 13:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-14 13:19 [PATCH v2 1/1] fs/squashfs: avoid illegal free() in sqfs_opendir() Heinrich Schuchardt
2025-04-14 13:35 ` Joao Marcos Costa
2025-04-22 13:58 ` Tom Rini

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