All of lore.kernel.org
 help / color / mirror / Atom feed
From: Richard Genoud <richard.genoud@posteo.net>
To: u-boot@lists.denx.de
Subject: [PATCH v2 08/28] fs/squashfs: sqfs_search_dir: fix memory leaks
Date: Tue,  3 Nov 2020 12:11:06 +0100	[thread overview]
Message-ID: <20201103111126.23600-9-richard.genoud@posteo.net> (raw)
In-Reply-To: <20201103111126.23600-1-richard.genoud@posteo.net>

path, target, res, rem and sym_tokens were not free on error nor success.

Signed-off-by: Richard Genoud <richard.genoud@posteo.net>
---
 fs/squashfs/sqfs.c | 64 ++++++++++++++++++++++++++++++++++++----------
 1 file changed, 51 insertions(+), 13 deletions(-)

diff --git a/fs/squashfs/sqfs.c b/fs/squashfs/sqfs.c
index 78893b5c85d..1714306e747 100644
--- a/fs/squashfs/sqfs.c
+++ b/fs/squashfs/sqfs.c
@@ -434,7 +434,7 @@ static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
 {
 	struct squashfs_super_block *sblk = ctxt.sblk;
 	char *path, *target, **sym_tokens, *res, *rem;
-	int j, ret, new_inode_number, offset;
+	int j, ret = 0, new_inode_number, offset;
 	struct squashfs_symlink_inode *sym;
 	struct squashfs_ldir_inode *ldir;
 	struct squashfs_dir_inode *dir;
@@ -442,6 +442,12 @@ static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
 	struct fs_dirent *dent;
 	unsigned char *table;
 
+	res = NULL;
+	rem = NULL;
+	path = NULL;
+	target = NULL;
+	sym_tokens = NULL;
+
 	dirsp = (struct fs_dir_stream *)dirs;
 
 	/* Start by root inode */
@@ -477,7 +483,8 @@ static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
 	for (j = 0; j < token_count; j++) {
 		if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
 			printf("** Cannot find directory. **\n");
-			return -EINVAL;
+			ret = -EINVAL;
+			goto out;
 		}
 
 		while (!sqfs_readdir(dirsp, &dent)) {
@@ -490,7 +497,8 @@ static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
 
 		if (ret) {
 			printf("** Cannot find directory. **\n");
-			return -EINVAL;
+			ret = -EINVAL;
+			goto out;
 		}
 
 		/* Redefine inode as the found token */
@@ -507,40 +515,63 @@ static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
 			sym = (struct squashfs_symlink_inode *)table;
 			/* Get first j + 1 tokens */
 			path = sqfs_concat_tokens(token_list, j + 1);
+			if (!path) {
+				ret = -ENOMEM;
+				goto out;
+			}
 			/* Resolve for these tokens */
 			target = sqfs_resolve_symlink(sym, path);
+			if (!target) {
+				ret = -ENOMEM;
+				goto out;
+			}
 			/* Join remaining tokens */
 			rem = sqfs_concat_tokens(token_list + j + 1, token_count -
 						 j - 1);
+			if (!rem) {
+				ret = -ENOMEM;
+				goto out;
+			}
 			/* Concatenate remaining tokens and symlink's target */
 			res = malloc(strlen(rem) + strlen(target) + 1);
+			if (!res) {
+				ret = -ENOMEM;
+				goto out;
+			}
 			strcpy(res, target);
 			res[strlen(target)] = '/';
 			strcpy(res + strlen(target) + 1, rem);
 			token_count = sqfs_count_tokens(res);
 
-			if (token_count < 0)
-				return -EINVAL;
+			if (token_count < 0) {
+				ret = -EINVAL;
+				goto out;
+			}
 
 			sym_tokens = malloc(token_count * sizeof(char *));
-			if (!sym_tokens)
-				return -EINVAL;
+			if (!sym_tokens) {
+				ret = -EINVAL;
+				goto out;
+			}
 
 			/* Fill tokens list */
 			ret = sqfs_tokenize(sym_tokens, token_count, res);
-			if (ret)
-				return -EINVAL;
+			if (ret) {
+				ret = -EINVAL;
+				goto out;
+			}
 			free(dirs->entry);
 			dirs->entry = NULL;
 
 			ret = sqfs_search_dir(dirs, sym_tokens, token_count,
 					      m_list, m_count);
-			return ret;
+			goto out;
 		} else if (!sqfs_is_dir(get_unaligned_le16(&dir->inode_type))) {
 			printf("** Cannot find directory. **\n");
 			free(dirs->entry);
 			dirs->entry = NULL;
-			return -EINVAL;
+			ret = -EINVAL;
+			goto out;
 		}
 
 		/* Check if it is an extended dir. */
@@ -560,7 +591,8 @@ static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
 			printf("Empty directory.\n");
 			free(dirs->entry);
 			dirs->entry = NULL;
-			return SQFS_EMPTY_DIR;
+			ret = SQFS_EMPTY_DIR;
+			goto out;
 		}
 
 		dirs->table += SQFS_DIR_HEADER_SIZE;
@@ -579,7 +611,13 @@ static int sqfs_search_dir(struct squashfs_dir_stream *dirs, char **token_list,
 	else
 		memcpy(&dirs->i_ldir, ldir, sizeof(*ldir));
 
-	return 0;
+out:
+	free(res);
+	free(rem);
+	free(path);
+	free(target);
+	free(sym_tokens);
+	return ret;
 }
 
 /*

  parent reply	other threads:[~2020-11-03 11:11 UTC|newest]

Thread overview: 82+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-03 11:10 [PATCH v2 00/28] fs/squashfs: fix memory leaks and introduce exists() function Richard Genoud
2020-11-03 11:10 ` [PATCH v2 01/28] fs/squashfs: fix board hang-up when calling .exists() Richard Genoud
2020-11-03 12:31   ` João Marcos Costa
2020-11-20  1:36   ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 02/28] fs/squashfs: sqfs_opendir: fix some memory leaks and dangling pointers Richard Genoud
2020-11-20  1:36   ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 03/28] fs/squashfs: sqfs_opendir: simplify error handling Richard Genoud
2020-11-03 12:33   ` João Marcos Costa
2020-11-20  1:36   ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 04/28] fs/squashfs: sqfs_closedir: fix memory leak Richard Genoud
2020-11-03 12:35   ` João Marcos Costa
2020-11-20  1:36   ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 05/28] fs/squashfs: sqfs_split_path: fix memory leak and dangling pointers Richard Genoud
2020-11-03 12:37   ` João Marcos Costa
2020-11-20  1:36   ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 06/28] fs/squashfs: sqfs_read_directory_table: fix memory leak Richard Genoud
2020-11-03 12:38   ` João Marcos Costa
2020-11-20  1:36   ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 07/28] fs/squashfs: sqfs_search_dir: fix dangling pointer Richard Genoud
2020-11-20  1:36   ` Tom Rini
2020-11-03 11:11 ` Richard Genoud [this message]
2020-11-03 12:39   ` [PATCH v2 08/28] fs/squashfs: sqfs_search_dir: fix memory leaks João Marcos Costa
2020-11-20  1:37   ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 09/28] fs/squashfs: sqfs_read_inode_table: fix dangling pointer Richard Genoud
2020-11-20  1:37   ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 10/28] fs/squashfs: sqfs_concat_tokens: check if malloc succeeds Richard Genoud
2020-11-03 12:40   ` João Marcos Costa
2020-11-20  1:37   ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 11/28] fs/squashfs: sqfs_size: fix dangling pointer dirs->entry Richard Genoud
2020-11-20  1:37   ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 12/28] fs/squashfs: sqfs_size: remove useless sqfs_closedir() Richard Genoud
2020-11-20  1:37   ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 13/28] fs/squashfs: sqfs_read: fix dangling pointer dirs->entry Richard Genoud
2020-11-20  1:37   ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 14/28] fs/squashfs: sqfs_read: remove useless sqfs_closedir() Richard Genoud
2020-11-20  1:37   ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 15/28] fs/squashfs: sqfs_read: fix memory leak Richard Genoud
2020-11-03 12:41   ` João Marcos Costa
2020-11-20  1:37   ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 16/28] fs/squashfs: sqfs_read: fix another " Richard Genoud
2020-11-20  1:37   ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 17/28] fs/squashfs: sqfs_frag_lookup: simplify error handling Richard Genoud
2020-11-03 12:44   ` João Marcos Costa
2020-11-20  1:37   ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 18/28] fs/squashfs: sqfs_get_abs_path: fix error check Richard Genoud
2020-11-03 12:44   ` João Marcos Costa
2020-11-20  1:37   ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 19/28] fs/squashfs: sqfs_get_abs_path: fix possible memory leak on error Richard Genoud
2020-11-03 12:44   ` João Marcos Costa
2020-11-20  1:38   ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 20/28] fs/squashfs: sqfs_read: fix memory leak on finfo.blk_sizes Richard Genoud
2020-11-03 12:45   ` João Marcos Costa
2020-11-20  1:38   ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 21/28] fs/squashfs: sqfs_probe: fix possible memory leak on error Richard Genoud
2020-11-03 12:46   ` João Marcos Costa
2020-11-20  1:38   ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 22/28] fs/squashfs: sqfs_close/sqfs_read_sblk: set ctxt.sblk to NULL after free Richard Genoud
2020-11-20  1:35   ` Tom Rini
2020-11-20 14:39     ` Richard Genoud
2020-11-03 11:11 ` [PATCH v2 23/28] fs/squashfs: sqfs_probe: reset cur_dev/cur_part_info to NULL on error Richard Genoud
2020-11-20  1:38   ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 24/28] fs/squashfs: sqfs_probe: use sqfs_decompressor_init() return value Richard Genoud
2020-11-03 12:46   ` João Marcos Costa
2020-11-20  1:38   ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 25/28] fs/squashfs: sqfs_read: don't write beyond buffer size Richard Genoud
2020-11-20  1:38   ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 26/28] fs/squashfs: sqfs_read: remove buggy offset functionality Richard Genoud
2020-11-20  1:38   ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 27/28] fs/squashfs: sqfs_read: fragmented files are not supported Richard Genoud
2020-11-20  1:35   ` Tom Rini
2020-11-25  8:58     ` Richard Genoud
2021-01-27 15:15       ` Simon Glass
2021-02-04 23:31         ` João Marcos Costa
2021-05-08 21:51           ` Simon Glass
2021-05-11 13:04             ` Richard Genoud
2021-05-17  0:44               ` João Marcos Costa
2021-05-17 13:47                 ` Tom Rini
2020-11-03 11:11 ` [PATCH v2 28/28] fs/squashfs: implement exists() function Richard Genoud
2020-11-03 12:48   ` João Marcos Costa
2020-11-20  1:38   ` Tom Rini
2020-11-03 11:18 ` [PATCH v2 00/28] fs/squashfs: fix memory leaks and introduce " Richard Genoud
2020-11-03 13:08   ` João Marcos Costa

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=20201103111126.23600-9-richard.genoud@posteo.net \
    --to=richard.genoud@posteo.net \
    --cc=u-boot@lists.denx.de \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.