Skip to contentSkip to conversation

ubifs: avoid possible NULL dereference

PATCH Superseded Heinrich Schuchardt 4 participants 10 messages Nov 21, 2017 – Nov 24, 2017 UBI file system (UBIFS)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ubifs: avoid possible NULL dereference

If 'file' cannot be allocated due to an out of memory situation, do not dereference it.

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.

---
 fs/ubifs/ubifs.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
fs/ubifs/ubifs.c +2 -3
index 4465523d5f..313dee0579 100644
@@ -403,8 +403,7 @@ static int ubifs_finddir(struct super_block *sb, char *dirname,
 	dir = kzalloc(sizeof(struct inode), 0);
 	if (!file || !dentry || !dir) {
 		printf("%s: Error, no memory for malloc!\n", __func__);
-		err = -ENOMEM;
-		goto out;
+		goto out_nomem;
 	}
 
 	dir->i_sb = sb;
@@ -424,7 +423,6 @@ static int ubifs_finddir(struct super_block *sb, char *dirname,
 		err = PTR_ERR(dent);
 		goto out;
 	}
-
 	file->f_pos = key_hash_flash(c, &dent->key);
 	file->private_data = dent;
 
@@ -463,6 +461,7 @@ out:
 
 out_free:
 	kfree(file->private_data);
+out_nomem:
 	free(file);
 	free(dentry);
 	free(dir);
Signature · 1 line
2.11.0
#1 lore raw

PATCH 1/1 ubifs: avoid possible NULL dereference

Quoted 29 lines · Heinrich Schuchardt

If 'file' cannot be allocated due to an out of memory situation, do not dereference it.

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.

---
 fs/ubifs/ubifs.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
fs/ubifs/ubifs.c +1 -2
index 4465523d5f..313dee0579 100644
@@ -403,8 +403,7 @@ static int ubifs_finddir(struct super_block *sb, char *dirname,
 	dir = kzalloc(sizeof(struct inode), 0);
 	if (!file || !dentry || !dir) {
 		printf("%s: Error, no memory for malloc!\n", __func__);
-		err = -ENOMEM;
-		goto out;
+		goto out_nomem;
 	}
 
 	dir->i_sb = sb;

fs/ubifs/ubifs.c static int ubifs_finddir(struct super_block *sb, char *dirname, · line 428

Quoted 7 lines
fs/ubifs/ubifs.c +0 -1
@@ -424,7 +423,6 @@ static int ubifs_finddir(struct super_block *sb, char *dirname,
 		err = PTR_ERR(dent);
 		goto out;
 	}
-
 	file->f_pos = key_hash_flash(c, &dent->key);
 	file->private_data = dent;
 

This hunk should be probably droped.

fs/ubifs/ubifs.c out: · line 463

Quoted 4 lines
fs/ubifs/ubifs.c +0 -0
@@ -463,6 +461,7 @@ out:
 
 out_free:
 	kfree(file->private_data);

Now question is why is file->private_data ever assigned as it seems nothing works with it.

Quoted 11 lines
fs/ubifs/ubifs.c +1 -0
+out_nomem:
 	free(file);
 	free(dentry);
 	free(dir);
Signature · 6 lines
2.11.0

_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
https://lists.denx.de/listinfo/u-boot
#2 lore raw

PATCH 1/1 ubifs: avoid possible NULL dereference

Quoted 40 lines · Ladislav Michl
Quoted 29 lines · Heinrich Schuchardt

If 'file' cannot be allocated due to an out of memory situation, do not dereference it.

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.

---
  fs/ubifs/ubifs.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
fs/ubifs/ubifs.c +1 -2
index 4465523d5f..313dee0579 100644
@@ -403,8 +403,7 @@ static int ubifs_finddir(struct super_block *sb, char *dirname,
  	dir = kzalloc(sizeof(struct inode), 0);
  	if (!file || !dentry || !dir) {
  		printf("%s: Error, no memory for malloc!\n", __func__);
-		err = -ENOMEM;
-		goto out;
+		goto out_nomem;
  	}
 
  	dir->i_sb = sb;

fs/ubifs/ubifs.c static int ubifs_finddir(struct super_block *sb, char *dirname, · line 428

Quoted 7 lines
fs/ubifs/ubifs.c +0 -1
@@ -424,7 +423,6 @@ static int ubifs_finddir(struct super_block *sb, char *dirname,
  		err = PTR_ERR(dent);
  		goto out;
  	}
-
  	file->f_pos = key_hash_flash(c, &dent->key);
  	file->private_data = dent;
 

This hunk should be probably droped.

Quoted 7 lines

fs/ubifs/ubifs.c out: · line 463

Quoted 4 lines
fs/ubifs/ubifs.c +0 -0
@@ -463,6 +461,7 @@ out:
 
  out_free:
  	kfree(file->private_data);

Now question is why is file->private_data ever assigned as it seems nothing works with it.

Good catch.

Do we need file and dentry at all in the function?

Unfortunately the file cannot be compiled on x86 cf.
https://lists.denx.de/pipermail/u-boot/2017-November/312166.html

Best regards

Heinrich

Quoted 13 lines
Quoted 12 lines
fs/ubifs/ubifs.c +1 -0
+out_nomem:
  	free(file);
  	free(dentry);
  	free(dir);
Signature · 6 lines
2.11.0

_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
https://lists.denx.de/listinfo/u-boot
#3 lore raw

PATCH 1/1 ubifs: avoid possible NULL dereference

Quoted 48 lines · Heinrich Schuchardt
Quoted 38 lines · Ladislav Michl
Quoted 28 lines · Heinrich Schuchardt

If 'file' cannot be allocated due to an out of memory situation, do not dereference it.

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.

---
  fs/ubifs/ubifs.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
fs/ubifs/ubifs.c +1 -2
index 4465523d5f..313dee0579 100644
@@ -403,8 +403,7 @@ static int ubifs_finddir(struct super_block *sb, char *dirname,
  	dir = kzalloc(sizeof(struct inode), 0);
  	if (!file || !dentry || !dir) {
  		printf("%s: Error, no memory for malloc!\n", __func__);
-		err = -ENOMEM;
-		goto out;
+		goto out_nomem;
  	}
  	dir->i_sb = sb;

fs/ubifs/ubifs.c static int ubifs_finddir(struct super_block *sb, char *dirname, · line 428

Quoted 7 lines
fs/ubifs/ubifs.c +0 -1
@@ -424,7 +423,6 @@ static int ubifs_finddir(struct super_block *sb, char *dirname,
  		err = PTR_ERR(dent);
  		goto out;
  	}
-
  	file->f_pos = key_hash_flash(c, &dent->key);
  	file->private_data = dent;
 

This hunk should be probably droped.

Quoted 6 lines

fs/ubifs/ubifs.c out: · line 462

Quoted 3 lines
fs/ubifs/ubifs.c +0 -0
@@ -463,6 +461,7 @@ out:
  out_free:
  	kfree(file->private_data);

Now question is why is file->private_data ever assigned as it seems nothing works with it.

Quoted 3 lines

Good catch.

Do we need file and dentry at all in the function?

Does not seem so. Also what f_pos is good for is unclear.

Quoted 2 lines

Unfortunately the file cannot be compiled on x86 cf.
https://lists.denx.de/pipermail/u-boot/2017-November/312166.html

I could give it a try on ARM board. Care to cook an untested patch?

Quoted 18 lines

Best regards

Heinrich

Quoted 13 lines
Quoted 12 lines
fs/ubifs/ubifs.c +1 -0
+out_nomem:
  	free(file);
  	free(dentry);
  	free(dir);
Signature · 6 lines
2.11.0

_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
https://lists.denx.de/listinfo/u-boot
#4 lore raw

PATCH 1/1 ubifs: avoid possible NULL dereference

In reply to Heinrich Schuchardt

Dear Heinrich,

Quoted 37 lines · In message <[email protected]> you wrote:

If 'file' cannot be allocated due to an out of memory situation, do not dereference it.

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.

---
 fs/ubifs/ubifs.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
fs/ubifs/ubifs.c +1 -3
index 4465523d5f..313dee0579 100644
@@ -403,8 +403,7 @@ static int ubifs_finddir(struct super_block *sb, char *dirname,
 	dir = kzalloc(sizeof(struct inode), 0);
 	if (!file || !dentry || !dir) {
 		printf("%s: Error, no memory for malloc!\n", __func__);
-		err = -ENOMEM;
-		goto out;
+		goto out_nomem;
 	}
 
 	dir->i_sb = sb;
@@ -424,7 +423,6 @@ static int ubifs_finddir(struct super_block *sb, char *dirname,
 		err = PTR_ERR(dent);
 		goto out;
 	}
-
 	file->f_pos = key_hash_flash(c, &dent->key);
 	file->private_data = dent;
 

fs/ubifs/ubifs.c out: · line 467

Quoted 7 lines
fs/ubifs/ubifs.c +1 -0
@@ -463,6 +461,7 @@ out:
 
 out_free:
 	kfree(file->private_data);
+out_nomem:
 	free(file);
 	free(dentry);
 	free(dir);

Should you not keep the "err = -ENOMEM;" setting? Otherwise there is no indivcation that an error happened.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
There are no data that cannot be plotted on a straight  line  if  the
axis are chosen correctly.
#5 lore raw

PATCH 1/1 ubifs: avoid possible NULL dereference

Quoted 41 lines · Wolfgang Denk

Dear Heinrich,

Quoted 38 lines · In message <[email protected]> you wrote:

If 'file' cannot be allocated due to an out of memory situation, do not dereference it.

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.

---
 fs/ubifs/ubifs.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
fs/ubifs/ubifs.c +1 -3
index 4465523d5f..313dee0579 100644
@@ -403,8 +403,7 @@ static int ubifs_finddir(struct super_block *sb, char *dirname,
 	dir = kzalloc(sizeof(struct inode), 0);
 	if (!file || !dentry || !dir) {
 		printf("%s: Error, no memory for malloc!\n", __func__);
-		err = -ENOMEM;
-		goto out;
+		goto out_nomem;
 	}
 
 	dir->i_sb = sb;
@@ -424,7 +423,6 @@ static int ubifs_finddir(struct super_block *sb, char *dirname,
 		err = PTR_ERR(dent);
 		goto out;
 	}
-
 	file->f_pos = key_hash_flash(c, &dent->key);
 	file->private_data = dent;
 
@@ -463,6 +461,7 @@ out:
 
Quoted 9 lines

fs/ubifs/ubifs.c out: · line 467

Quoted 6 lines
fs/ubifs/ubifs.c +1 -0
 out_free:
 	kfree(file->private_data);
+out_nomem:
 	free(file);
 	free(dentry);
 	free(dir);

Should you not keep the "err = -ENOMEM;" setting? Otherwise there is no indivcation that an error happened.

It is not obvious from the patch, but value of err is later discarded. It serves sole purpose of printing debug notice.

Quoted 15 lines

Best regards,

Wolfgang Denk

Signature · 9 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
There are no data that cannot be plotted on a straight  line  if  the
axis are chosen correctly.
_______________________________________________
U-Boot mailing list
U-Boot at lists.denx.de
https://lists.denx.de/listinfo/u-boot
#6 lore raw

PATCH 1/1 ubifs: avoid possible NULL dereference

Dear Ladislav,

Quoted 13 lines · In message <20171121212222.ryicwv6tyh5rye2e@lenoch> you wrote:
Quoted 13 lines
Quoted 13 lines
fs/ubifs/ubifs.c +1 -2
index 4465523d5f..313dee0579 100644
@@ -403,8 +403,7 @@ static int ubifs_finddir(struct super_block *sb, char *dirname,
 	dir = kzalloc(sizeof(struct inode), 0);
 	if (!file || !dentry || !dir) {
 		printf("%s: Error, no memory for malloc!\n", __func__);
-		err = -ENOMEM;
-		goto out;
+		goto out_nomem;
 	}

...

Quoted 5 lines
Quoted 2 lines

Should you not keep the "err = -ENOMEM;" setting? Otherwise there is no indivcation that an error happened.

It is not obvious from the patch, but value of err is later discarded. It serves sole purpose of printing debug notice.

So apparently we have a number of places in U-Boot where fatal errors (running out of memory) are just ignored and we continue as if nothing happened?

THis is short-sighted at best. One day Pump Six will fail.

This is giving me the creepes.

Best regards,

Wolfgang Denk

Signature · 6 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Why don't you have a Linux partition installed so you can be  working
in  a  programmer-friendly environment instead of a keep-gates'-bank-
account-happy one? :-)                            -- Tom Christiansen
#7 lore raw

PATCH 1/1 ubifs: avoid possible NULL dereference

Quoted 20 lines · Wolfgang Denk

Dear Ladislav,

Quoted 13 lines · In message <20171121212222.ryicwv6tyh5rye2e@lenoch> you wrote:
Quoted 13 lines
Quoted 13 lines
fs/ubifs/ubifs.c +1 -2
index 4465523d5f..313dee0579 100644
@@ -403,8 +403,7 @@ static int ubifs_finddir(struct super_block *sb, char *dirname,
 	dir = kzalloc(sizeof(struct inode), 0);
 	if (!file || !dentry || !dir) {
 		printf("%s: Error, no memory for malloc!\n", __func__);
-		err = -ENOMEM;
-		goto out;
+		goto out_nomem;
 	}

...

Quoted 3 lines
Quoted 2 lines

Should you not keep the "err = -ENOMEM;" setting? Otherwise there is no indivcation that an error happened.

Quoted 6 lines
Quoted 2 lines

It is not obvious from the patch, but value of err is later discarded. It serves sole purpose of printing debug notice.

So apparently we have a number of places in U-Boot where fatal errors (running out of memory) are just ignored and we continue as if nothing happened?

While I have to admit this code is not an example of clean coding, it prints notice when trying to manipulate with file.

fs/ubifs/ubifs.c as whole needs to be revisited, above patch just caused shit hitting the fan.

Quoted 3 lines

THis is short-sighted at best. One day Pump Six will fail.

This is giving me the creepes.

I was just pointing to the fact, that above mentioned patch does not make it any worse. Btw, initial commit is even more amazing:

+ if (file)
+ free(file);
+ if (dentry)
+ free(dentry);
+ if (dir)
+ free(dir);
+
+ if (file->private_data)
+ kfree(file->private_data);

+       file->private_data = NULL;
+       file->f_pos = 2;
+       return 0;

.oO(I guess it is safe not pointing where above snippet is comming from, but will review whole file as I'm going to use it for new board I have to suport)

Best regards,
ladis

#8 lore raw

PATCH 1/1 ubifs: avoid possible NULL dereference

Hello Ladislav,

Sorry for digging into it so late...

Am 22.11.2017 um 09:25 schrieb Ladislav Michl:

Quoted 28 lines · Wolfgang Denk
Quoted 20 lines

Dear Ladislav,

Quoted 13 lines · In message <20171121212222.ryicwv6tyh5rye2e@lenoch> you wrote:
Quoted 13 lines
Quoted 13 lines
fs/ubifs/ubifs.c +1 -2
index 4465523d5f..313dee0579 100644
@@ -403,8 +403,7 @@ static int ubifs_finddir(struct super_block *sb, char *dirname,
  	dir = kzalloc(sizeof(struct inode), 0);
  	if (!file || !dentry || !dir) {
  		printf("%s: Error, no memory for malloc!\n", __func__);
-		err = -ENOMEM;
-		goto out;
+		goto out_nomem;
  	}

...

Quoted 3 lines
Quoted 2 lines

Should you not keep the "err = -ENOMEM;" setting? Otherwise there is no indivcation that an error happened.

Quoted 6 lines
Quoted 2 lines

It is not obvious from the patch, but value of err is later discarded. It serves sole purpose of printing debug notice.

So apparently we have a number of places in U-Boot where fatal errors (running out of memory) are just ignored and we continue as if nothing happened?

Quoted 5 lines

While I have to admit this code is not an example of clean coding, it prints notice when trying to manipulate with file.

fs/ubifs/ubifs.c as whole needs to be revisited, above patch just caused shit hitting the fan.

Yes, full ack ... we do need here a full review of this code.

Quoted 20 lines
Quoted 3 lines

THis is short-sighted at best. One day Pump Six will fail.

This is giving me the creepes.

I was just pointing to the fact, that above mentioned patch does not make it any worse. Btw, initial commit is even more amazing:

(quoted diff) +12 -0
+       if (file)
+               free(file);
+       if (dentry)
+               free(dentry);
+       if (dir)
+               free(dir);
+
+       if (file->private_data)
+               kfree(file->private_data);
+       file->private_data = NULL;
+       file->f_pos = 2;
+       return 0;
Quoted 3 lines

.oO(I guess it is safe not pointing where above snippet is comming from, but will review whole file as I'm going to use it for new board I have to suport)

Many thanks!

bye,
Heiko

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52   Fax: +49-8142-66989-80   Email: hs at denx.de
#9 lore raw

PATCH 1/1 ubifs: avoid possible NULL dereference

In reply to Wolfgang Denk
Quoted 41 lines · Wolfgang Denk

Dear Heinrich,

Quoted 38 lines · In message <[email protected]> you wrote:

If 'file' cannot be allocated due to an out of memory situation, do not dereference it.

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.

---
  fs/ubifs/ubifs.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)
fs/ubifs/ubifs.c +1 -3
index 4465523d5f..313dee0579 100644
@@ -403,8 +403,7 @@ static int ubifs_finddir(struct super_block *sb, char *dirname,
  	dir = kzalloc(sizeof(struct inode), 0);
  	if (!file || !dentry || !dir) {
  		printf("%s: Error, no memory for malloc!\n", __func__);
-		err = -ENOMEM;
-		goto out;
+		goto out_nomem;
  	}
 
  	dir->i_sb = sb;
@@ -424,7 +423,6 @@ static int ubifs_finddir(struct super_block *sb, char *dirname,
  		err = PTR_ERR(dent);
  		goto out;
  	}
-
  	file->f_pos = key_hash_flash(c, &dent->key);
  	file->private_data = dent;
 
@@ -463,6 +461,7 @@ out:
 
Quoted 9 lines

fs/ubifs/ubifs.c out: · line 467

Quoted 6 lines
fs/ubifs/ubifs.c +1 -0
  out_free:
  	kfree(file->private_data);
+out_nomem:
  	free(file);
  	free(dentry);
  	free(dir);

Should you not keep the "err = -ENOMEM;" setting? Otherwise there is no indivcation that an error happened.

err is a local variable it is not returned by the function. It is only used to show a debug message which does not make sense in this case.

The "inidcation" is returning 0 and printf("%s: Error, no memory for malloc!\n", __func__)

Best regards

Heinrich

Quoted 5 lines

Best regards,

Wolfgang Denk