Skip to contentSkip to conversation

erofs-utils: lib: fix potential overflow issue

PATCH In review Hongzhen Luo 3 participants 5 messages Aug 5, 2024 – Aug 7, 2024 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs-utils: lib: fix potential overflow issue

Coverity-id: 502377

---
 lib/kite_deflate.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/kite_deflate.c +2 -1
index a5ebd66..e52e382 100644
@@ -817,7 +817,8 @@ static const struct kite_matchfinder_cfg {
 /* 9 */ {32, 258, 258, 4096, true},	/* maximum compression */
 };
 
-static int kite_mf_init(struct kite_matchfinder *mf, int wsiz, int level)
+static int kite_mf_init(struct kite_matchfinder *mf, unsigned int wsiz,
+			int level)
 {
 	const struct kite_matchfinder_cfg *cfg;
 
Signature · 1 line
2.43.5
#1 lore raw
Quoted 23 lines · Hongzhen Luo

Coverity-id: 502377

---
 lib/kite_deflate.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/kite_deflate.c +2 -2
index a5ebd66..e52e382 100644
@@ -817,7 +817,8 @@ static const struct kite_matchfinder_cfg {
 /* 9 */ {32, 258, 258, 4096, true},    /* maximum compression */
 };
 
-static int kite_mf_init(struct kite_matchfinder *mf, int wsiz, int level)
+static int kite_mf_init(struct kite_matchfinder *mf, unsigned int wsiz,
+                       int level)
 {
        const struct kite_matchfinder_cfg *cfg;
 
--
Quoted 2 lines

2.43.5

Hi Hongzhen,
Can you please explain to me where the potential overflow is? Checkers can be smart so easy for me to miss.
I see a below check in kitle_me_init()

    if (wsiz > kHistorySize32 || (1 << ilog2(wsiz)) != wsiz)
          return -EINVAL;

So any larger value than kHistorySize32 which is (1U << 15) is already rejected. So what overflow case is this int => unsigned int type conversion solving?

Thanks,
Sandeep.

#2 lore raw

Hi Sandeep,

Quoted 32 lines · Sandeep Dhavale via Linux-erofs
Quoted 23 lines · Hongzhen Luo

Coverity-id: 502377

---
  lib/kite_deflate.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
lib/kite_deflate.c +2 -2
index a5ebd66..e52e382 100644
@@ -817,7 +817,8 @@ static const struct kite_matchfinder_cfg {
  /* 9 */ {32, 258, 258, 4096, true},    /* maximum compression */
  };
 
-static int kite_mf_init(struct kite_matchfinder *mf, int wsiz, int level)
+static int kite_mf_init(struct kite_matchfinder *mf, unsigned int wsiz,
+                       int level)
  {
         const struct kite_matchfinder_cfg *cfg;
 
--
Quoted 2 lines

2.43.5

Hi Hongzhen,
Can you please explain to me where the potential overflow is? Checkers can be smart so easy for me to miss.
I see a below check in kitle_me_init()

Quoted 6 lines
     if (wsiz > kHistorySize32 || (1 << ilog2(wsiz)) != wsiz)
           return -EINVAL;

So any larger value than kHistorySize32 which is (1U << 15) is already rejected. So what overflow case is this int => unsigned int type conversion solving?

The latest coverity scan can be found at:
https://scan.coverity.com/projects/erofs-erofs-utils

If you're interested in the result details, I could send a coverity membership invitation to you so that you could find more comments on the website.

Since coverity reports are important for several vendors, if it's not quite insane, maybe we need to address anyway.

Thanks,
Gao Xiang

Quoted 3 lines

Thanks,
Sandeep.

#3 lore raw
In reply to Sandeep Dhavale via Linux-erofs
Quoted 33 lines · Sandeep Dhavale
Quoted 22 lines · Hongzhen Luo

Coverity-id: 502377

---
  lib/kite_deflate.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
lib/kite_deflate.c +2 -2
index a5ebd66..e52e382 100644
@@ -817,7 +817,8 @@ static const struct kite_matchfinder_cfg {
  /* 9 */ {32, 258, 258, 4096, true},    /* maximum compression */
  };
 
-static int kite_mf_init(struct kite_matchfinder *mf, int wsiz, int level)
+static int kite_mf_init(struct kite_matchfinder *mf, unsigned int wsiz,
+                       int level)
  {
         const struct kite_matchfinder_cfg *cfg;
 
--
Quoted 2 lines

2.43.5

Hi Hongzhen,
Can you please explain to me where the potential overflow is? Checkers can be smart so easy for me to miss.
I see a below check in kitle_me_init()

     if (wsiz > kHistorySize32 || (1 << ilog2(wsiz)) != wsiz)
           return -EINVAL;
Quoted 6 lines

So any larger value than kHistorySize32 which is (1U << 15) is already rejected. So what overflow case is this int => unsigned int type conversion solving?

Thanks,
Sandeep.

Hi Sandeep,

The coverity tool says that for code mf->chain = malloc(sizeof(mf->chain[0]) * wsiz); there is a potential overflow issue:

overflow_const: Expression 4UL * wsiz, which is equal to 18446744065119617024, where wsiz is known to be equal to -2147483648, overflows the type that receives it, an unsigned integer 64 bits wide.

For example, when wsiz is set to -1, it is converted to an unsigned long value of 18446744073709551615, and multiplying this by 4 would lead to an overflow error. Consequently, I have defined wsiz as unsigned int, which has a maximum value of 4294967295. After converting this to unsigned long and multiplying by 4, an overflow will not occur.

In practical applications, however, wsiz would not take on such odd values.

---

Thanks,

Hongzhen Luo