Skip to contentSkip to conversation

erofs: Prevent entering an infinite loop when i is 0

PATCH In review liujinbao1 3 participants 6 messages Aug 29, 2024 – Sep 5, 2024 EROFS file system

lore
Share & tools
lore

liujinbao1

author
#0 lore raw

PATCH v2 1/1 erofs: Prevent entering an infinite loop when i is 0

From: liujinbao1 <[email protected]>

When i=0 and err is not equal to 0,
the while(-1) loop will enter into an
infinite loop. This patch avoids this issue

---
 fs/erofs/decompressor.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
fs/erofs/decompressor.c +5 -5
index c2253b6a5416..672f097966fa 100644
@@ -534,18 +534,18 @@ int z_erofs_parse_cfgs(struct super_block *sb, struct erofs_super_block *dsb)
 
 int __init z_erofs_init_decompressor(void)
 {
-	int i, err;
+	int i, err = 0;
 
 	for (i = 0; i < Z_EROFS_COMPRESSION_MAX; ++i) {
 		err = z_erofs_decomp[i] ? z_erofs_decomp[i]->init() : 0;
-		if (err) {
-			while (--i)
+		if (err && i) {
+			while (i--)
 				if (z_erofs_decomp[i])
 					z_erofs_decomp[i]->exit();
-			return err;
+			break;
 		}
 	}
-	return 0;
+	return err;
 }
 

(1) The use of "break" is to enable a jump out of the for loop, otherwise it may not be able to exit the loop.
(2) --i should be changed to i-- because when i is equal to 1, the "while (--i)" statement would exit the loop prematurely.

and sorry for the delay in the response.

#2 lore raw
In reply to liujinbao1

Hi Liujinbao,

Quoted 19 lines · On Thu, Aug 29, 2024 at 5:24 AM liujinbao1 <[email protected]> wrote:

From: liujinbao1 <[email protected]>

When i=0 and err is not equal to 0,
the while(-1) loop will enter into an
infinite loop. This patch avoids this issue

---
 fs/erofs/decompressor.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
fs/erofs/decompressor.c +0 -0
index c2253b6a5416..672f097966fa 100644
@@ -534,18 +534,18 @@ int z_erofs_parse_cfgs(struct super_block *sb, struct erofs_super_block *dsb)
 
 int __init z_erofs_init_decompressor(void)

fs/erofs/decompressor.c int z_erofs_parse_cfgs(struct super_block *sb, struct erofs_super_block *dsb) · line 542

Quoted 10 lines
fs/erofs/decompressor.c +3 -3
 {
-       int i, err;
+       int i, err = 0;
 
        for (i = 0; i < Z_EROFS_COMPRESSION_MAX; ++i) {
                err = z_erofs_decomp[i] ? z_erofs_decomp[i]->init() : 0;
-               if (err) {
-                       while (--i)
+               if (err && i) {
+                       while (i--)

Actually there is a subtle bug in this fix. We will never enter the if block here when i=0 and err is set which we were trying to fix. This will cause z_erofs_decomp[0]->init() error to get masked and we will continue the outer for loop (i.e. when i=0 and err is set).

Yes original code had the bug but probably simpler and readable fix could be

fs/erofs/decompressor.c +1 -1
index c2253b6a5416..abf2db2ba10c 100644
@@ -539,7 +539,7 @@ int __init z_erofs_init_decompressor(void)
        for (i = 0; i < Z_EROFS_COMPRESSION_MAX; ++i) {
                err = z_erofs_decomp[i] ? z_erofs_decomp[i]->init() : 0;
                if (err) {
-                       while (--i)
+                       while (i-- > 0)
                                if (z_erofs_decomp[i])
                                        z_erofs_decomp[i]->exit();
                        return err;

Let me know if you want me to send a fix with your Reported-by or you can send quick v3.

Thanks,
Sandeep.

Quoted 17 lines
fs/erofs/decompressor.c +2 -2
                                if (z_erofs_decomp[i])
                                        z_erofs_decomp[i]->exit();
-                       return err;
+                       break;
                }
        }
-       return 0;
+       return err;
 }
 
 (1) The use of "break" is to enable a jump out of the for loop,

otherwise it may not be able to exit the loop.
(2) --i should be changed to i-- because when i is equal to 1, the "while (--i)" statement would exit the loop prematurely.

and sorry for the delay in the response.

#3 lore raw

Hi Sandeep,

Quoted 25 lines · Sandeep Dhavale

Hi Liujinbao,

Quoted 23 lines · On Thu, Aug 29, 2024 at 5:24 AM liujinbao1 <[email protected]> wrote:

From: liujinbao1 <[email protected]>

When i=0 and err is not equal to 0,
the while(-1) loop will enter into an
infinite loop. This patch avoids this issue

---
  fs/erofs/decompressor.c | 10 +++++-----
  1 file changed, 5 insertions(+), 5 deletions(-)
fs/erofs/decompressor.c +1 -1
index c2253b6a5416..672f097966fa 100644
@@ -534,18 +534,18 @@ int z_erofs_parse_cfgs(struct super_block *sb, struct erofs_super_block *dsb)
 
  int __init z_erofs_init_decompressor(void)
  {
-       int i, err;
+       int i, err = 0;
 
Quoted 10 lines

fs/erofs/decompressor.c int __init z_erofs_init_decompressor(void)

Quoted 6 lines
fs/erofs/decompressor.c +2 -2
         for (i = 0; i < Z_EROFS_COMPRESSION_MAX; ++i) {
                 err = z_erofs_decomp[i] ? z_erofs_decomp[i]->init() : 0;
-               if (err) {
-                       while (--i)
+               if (err && i) {
+                       while (i--)

Actually there is a subtle bug in this fix. We will never enter the if block here when i=0 and err is set which we were trying to fix. This will cause z_erofs_decomp[0]->init() error to get masked and we will continue the outer for loop (i.e. when i=0 and err is set).

Thanks for catching this!

Yes, that needs to be resolved, and I will replace the patch to the new version.

Thanks,
Gao Xiang

Quoted 23 lines

Yes original code had the bug but probably simpler and readable fix could be

fs/erofs/decompressor.c +1 -1
index c2253b6a5416..abf2db2ba10c 100644
@@ -539,7 +539,7 @@ int __init z_erofs_init_decompressor(void)
         for (i = 0; i < Z_EROFS_COMPRESSION_MAX; ++i) {
                 err = z_erofs_decomp[i] ? z_erofs_decomp[i]->init() : 0;
                 if (err) {
-                       while (--i)
+                       while (i-- > 0)
                                 if (z_erofs_decomp[i])
                                         z_erofs_decomp[i]->exit();
                         return err;

Let me know if you want me to send a fix with your Reported-by or you can send quick v3.

Thanks,
Sandeep.

#4 lore raw
Quoted 39 lines · Gao Xiang

Hi Sandeep,

Quoted 35 lines · Sandeep Dhavale

Hi Liujinbao,

Quoted 19 lines · On Thu, Aug 29, 2024 at 5:24 AM liujinbao1 <[email protected]> wrote:

From: liujinbao1 <[email protected]>

When i=0 and err is not equal to 0,
the while(-1) loop will enter into an
infinite loop. This patch avoids this issue

---
  fs/erofs/decompressor.c | 10 +++++-----

  1 file changed, 5 insertions(+), 5 deletions(-)
fs/erofs/decompressor.c +0 -0
index c2253b6a5416..672f097966fa 100644
@@ -534,18 +534,18 @@ int z_erofs_parse_cfgs(struct super_block *sb, struct erofs_super_block *dsb)
 
   int __init z_erofs_init_decompressor(void)

fs/erofs/decompressor.c int z_erofs_parse_cfgs(struct super_block *sb, struct erofs_super_block *dsb) · line 542

Quoted 10 lines
fs/erofs/decompressor.c +3 -3
   {
-       int i, err;
+       int i, err = 0;
 
          for (i = 0; i < Z_EROFS_COMPRESSION_MAX; ++i) {
                  err = z_erofs_decomp[i] ? z_erofs_decomp[i]->init() : 0;
-               if (err) {
-                       while (--i)
+               if (err && i) {
+                       while (i--)

Actually there is a subtle bug in this fix. We will never enter the if block here when i=0 and err is set which we were trying to fix. This will cause z_erofs_decomp[0]->init() error to get masked and we will continue the outer for loop (i.e. when i=0 and err is set).

Ping? could anyone submit a proper fix for this?

Or just

fs/erofs/decompressor.c +2 -2
index c2253b6a5416..dfb77f4e68b4 100644
@@ -539,8 +539,8 @@ int __init z_erofs_init_decompressor(void)
        for (i = 0; i < Z_EROFS_COMPRESSION_MAX; ++i) {
                err = z_erofs_decomp[i] ? z_erofs_decomp[i]->init() : 0;
                if (err) {
-                       while (--i)
-                               if (z_erofs_decomp[i])
+                       while (i)
+                               if (z_erofs_decomp[--i])
                                        z_erofs_decomp[i]->exit();
                        return err;
                }

to avoid underflowed i (although it should have no real impact.)

Thanks,
Gao Xiang

#5 lore raw
Quoted 30 lines · Gao Xiang
Quoted 28 lines · Gao Xiang

Hi Sandeep,

Quoted 25 lines · Sandeep Dhavale

Hi Liujinbao,

Quoted 23 lines · On Thu, Aug 29, 2024 at 5:24 AM liujinbao1 <[email protected]> wrote:

From: liujinbao1 <[email protected]>

When i=0 and err is not equal to 0,
the while(-1) loop will enter into an
infinite loop. This patch avoids this issue

---
  fs/erofs/decompressor.c | 10 +++++-----
  1 file changed, 5 insertions(+), 5 deletions(-)
fs/erofs/decompressor.c +1 -1
index c2253b6a5416..672f097966fa 100644
@@ -534,18 +534,18 @@ int z_erofs_parse_cfgs(struct super_block *sb, struct erofs_super_block *dsb)
 
  int __init z_erofs_init_decompressor(void)
  {
-       int i, err;
+       int i, err = 0;
 
Quoted 12 lines
Quoted 11 lines
Quoted 10 lines

fs/erofs/decompressor.c int z_erofs_parse_cfgs(struct super_block *sb, struct erofs_super_block *dsb) · line 542

Quoted 6 lines
fs/erofs/decompressor.c +2 -2
         for (i = 0; i < Z_EROFS_COMPRESSION_MAX; ++i) {
                 err = z_erofs_decomp[i] ? z_erofs_decomp[i]->init() : 0;
-               if (err) {
-                       while (--i)
+               if (err && i) {
+                       while (i--)

Actually there is a subtle bug in this fix. We will never enter the if block here when i=0 and err is set which we were trying to fix. This will cause z_erofs_decomp[0]->init() error to get masked and we will continue the outer for loop (i.e. when i=0 and err is set).

Hi Gao,

Quoted 3 lines

Ping? could anyone submit a proper fix for this?

Yes, that needs to be resolved, and I will replace the patch to the new version.

I think I misunderstood this as you are amending it.

Quoted 7 lines

Or just

fs/erofs/decompressor.c +0 -0
index c2253b6a5416..dfb77f4e68b4 100644

fs/erofs/decompressor.c int __init z_erofs_init_decompressor(void) · line 544

Quoted 8 lines
fs/erofs/decompressor.c +2 -2
@@ -539,8 +539,8 @@ int __init z_erofs_init_decompressor(void)
         for (i = 0; i < Z_EROFS_COMPRESSION_MAX; ++i) {
                 err = z_erofs_decomp[i] ? z_erofs_decomp[i]->init() : 0;
                 if (err) {
-                       while (--i)
-                               if (z_erofs_decomp[i])
+                       while (i)
+                               if (z_erofs_decomp[--i])
                                         z_erofs_decomp[i]->exit();

Even though logically same, decrementing i in the middle of 3 lines referencing it does not feel very readable to me.
A below version is more traditional as changing the cond in the while() check.

fs/erofs/decompressor.c +1 -1
index c2253b6a5416..eb318c7ddd80 100644
@@ -539,7 +539,7 @@ int __init z_erofs_init_decompressor(void)
        for (i = 0; i < Z_EROFS_COMPRESSION_MAX; ++i) {
                err = z_erofs_decomp[i] ? z_erofs_decomp[i]->init() : 0;
                if (err) {
-                       while (--i)
+                       while (i--)
                                if (z_erofs_decomp[i])
                                        z_erofs_decomp[i]->exit();
                        return err;

I will send this in a formal patch in few mins as v3, if you feel ok, please add it. If you want to stick to your version, no problem, I understand that it is subjective.

Thanks,
Sandeep.

Quoted 7 lines
fs/erofs/decompressor.c +0 -0
                         return err;
                 }

to avoid underflowed i (although it should have no real impact.)

Thanks,
Gao Xiang