public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [U-Boot] [PATCH v2 1/1] ubifs: avoid possible NULL dereference
@ 2017-11-21 22:06 Heinrich Schuchardt
  2017-11-21 22:40 ` Ladislav Michl
  0 siblings, 1 reply; 5+ messages in thread
From: Heinrich Schuchardt @ 2017-11-21 22:06 UTC (permalink / raw)
  To: u-boot

If 'file' cannot be allocated due to an out of memory
situation, NULL is dereferenced.

Variables file and dentry are not needed at all.
So let's eliminate them.

When debugging this patch also avoids a misleading message
"cannot find next direntry, error %d" in case of an out of
memory situation. It is sufficent to write
"%s: Error, no memory for malloc!\n" in this case.

Reported-by: Ladislav Michl <ladis@linux-mips.org>
Reported-by: Alex Sadovsky <nable.maininbox@googlemail.com>
Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
---
 fs/ubifs/ubifs.c | 25 ++-----------------------
 1 file changed, 2 insertions(+), 23 deletions(-)

diff --git a/fs/ubifs/ubifs.c b/fs/ubifs/ubifs.c
index 4465523d5f..f3d190c763 100644
--- a/fs/ubifs/ubifs.c
+++ b/fs/ubifs/ubifs.c
@@ -393,29 +393,18 @@ static int ubifs_finddir(struct super_block *sb, char *dirname,
 	union ubifs_key key;
 	struct ubifs_dent_node *dent;
 	struct ubifs_info *c;
-	struct file *file;
-	struct dentry *dentry;
 	struct inode *dir;
 	int ret = 0;
 
-	file = kzalloc(sizeof(struct file), 0);
-	dentry = kzalloc(sizeof(struct dentry), 0);
 	dir = kzalloc(sizeof(struct inode), 0);
-	if (!file || !dentry || !dir) {
+	if (!dir) {
 		printf("%s: Error, no memory for malloc!\n", __func__);
-		err = -ENOMEM;
-		goto out;
+		goto out_free;
 	}
 
 	dir->i_sb = sb;
-	file->f_path.dentry = dentry;
-	file->f_path.dentry->d_parent = dentry;
-	file->f_path.dentry->d_inode = dir;
-	file->f_path.dentry->d_inode->i_ino = root_inum;
 	c = sb->s_fs_info;
 
-	dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
-
 	/* Find the first entry in TNC and save it */
 	lowest_dent_key(c, &key, dir->i_ino);
 	nm.name = NULL;
@@ -425,9 +414,6 @@ static int ubifs_finddir(struct super_block *sb, char *dirname,
 		goto out;
 	}
 
-	file->f_pos = key_hash_flash(c, &dent->key);
-	file->private_data = dent;
-
 	while (1) {
 		dbg_gen("feed '%s', ino %llu, new f_pos %#x",
 			dent->name, (unsigned long long)le64_to_cpu(dent->inum),
@@ -450,10 +436,6 @@ static int ubifs_finddir(struct super_block *sb, char *dirname,
 			err = PTR_ERR(dent);
 			goto out;
 		}
-
-		kfree(file->private_data);
-		file->f_pos = key_hash_flash(c, &dent->key);
-		file->private_data = dent;
 		cond_resched();
 	}
 
@@ -462,9 +444,6 @@ out:
 		dbg_gen("cannot find next direntry, error %d", err);
 
 out_free:
-	kfree(file->private_data);
-	free(file);
-	free(dentry);
 	free(dir);
 
 	return ret;
-- 
2.11.0

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

end of thread, other threads:[~2017-11-22 15:47 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-11-21 22:06 [U-Boot] [PATCH v2 1/1] ubifs: avoid possible NULL dereference Heinrich Schuchardt
2017-11-21 22:40 ` Ladislav Michl
2017-11-22 12:37   ` Heinrich Schuchardt
2017-11-22 13:37     ` Ladislav Michl
2017-11-22 15:47       ` Ladislav Michl

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