Skip to contentSkip to conversation

lib/LzmaEnc: Validate 'len' before subtracting

PATCH Superseded Lidong Chen via Grub-devel 2 participants 4 messages Jun 10, 2025 – Jun 11, 2025

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/LzmaEnc: Validate 'len' before subtracting

In LzmaEnc_CodeOneBlock(), both GetOptimumFast() and GetOptimum() returns a value of greater or equal to 1, which is assigned to 'len'. But since LZMA_MATCH_LEN_MIN == 2, 'len' should be validated before performing "len - LZMA_MATCH_LEN_MIN" to avoid underflow when 'len' equals to 1.

Fixed: CID 51508

---
 grub-core/lib/LzmaEnc.c | 5 +++++
 1 file changed, 5 insertions(+)
grub-core/lib/LzmaEnc.c +5 -0
index 52b331558..d74e96303 100644
@@ -1880,6 +1880,11 @@ static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize
         UInt32 posSlot, lenToPosState;
         RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
         p->state = kMatchNextStates[p->state];
+	if (len < LZMA_MATCH_LEN_MIN)
+	  {
+	    p->result = SZ_ERROR_DATA;
+	    return CheckErrors(p);
+	  }
         LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
         pos -= LZMA_NUM_REPS;
         GetPosSlot(pos, posSlot);
Signature · 7 lines
2.34.1


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#1 lore raw
Quoted 22 lines · Lidong Chen via Grub-devel

In LzmaEnc_CodeOneBlock(), both GetOptimumFast() and GetOptimum() returns a value of greater or equal to 1, which is assigned to 'len'. But since LZMA_MATCH_LEN_MIN == 2, 'len' should be validated before performing "len - LZMA_MATCH_LEN_MIN" to avoid underflow when 'len' equals to 1.

Fixed: CID 51508

---
 grub-core/lib/LzmaEnc.c | 5 +++++
 1 file changed, 5 insertions(+)
grub-core/lib/LzmaEnc.c +0 -0
index 52b331558..d74e96303 100644
@@ -1880,6 +1880,11 @@ static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p,

Bool useLimits, UInt32 maxPackSize

         UInt32 posSlot, lenToPosState;
         RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
         p->state = kMatchNextStates[p->state];

grub-core/lib/LzmaEnc.c static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize · line 1887

Quoted 5 lines
grub-core/lib/LzmaEnc.c +5 -0
+	if (len < LZMA_MATCH_LEN_MIN)
+	  {
+	    p->result = SZ_ERROR_DATA;
+	    return CheckErrors(p);
+	  }

Hi Lidong Chen,

please fix the indentation issue in if condition.

thanks,
sudhakar

Quoted 4 lines
         LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN,
posState, !p->fastMode, p->ProbPrices);
         pos -= LZMA_NUM_REPS;
         GetPosSlot(pos, posSlot);

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#2 lore raw
In reply to Lidong Chen via Grub-devel
Quoted 5 lines · Lidong Chen

In LzmaEnc_CodeOneBlock(), both GetOptimumFast() and GetOptimum() returns a value of greater or equal to 1, which is assigned to 'len'. But since LZMA_MATCH_LEN_MIN == 2, 'len' should be validated before performing "len - LZMA_MATCH_LEN_MIN" to avoid underflow when 'len' equals to 1.

It seems odd that these internal calls will produce values for len that the calling code can't use and it ends in an error. Does this happen when the input data is bad/malformed/etc? Is it considered an error condition down in those functions when they result in len being set to 1 or 2?

Thanks
Ross

Quoted 14 lines

Fixed: CID 51508

---
  grub-core/lib/LzmaEnc.c | 5 +++++
  1 file changed, 5 insertions(+)
grub-core/lib/LzmaEnc.c +0 -0
index 52b331558..d74e96303 100644
@@ -1880,6 +1880,11 @@ static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize
          UInt32 posSlot, lenToPosState;

grub-core/lib/LzmaEnc.c static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize · line 1890

Quoted 10 lines
grub-core/lib/LzmaEnc.c +5 -0
          RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
          p->state = kMatchNextStates[p->state];
+	if (len < LZMA_MATCH_LEN_MIN)
+	  {
+	    p->result = SZ_ERROR_DATA;
+	    return CheckErrors(p);
+	  }
          LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
          pos -= LZMA_NUM_REPS;
          GetPosSlot(pos, posSlot);

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel

#3 lore raw
Quoted 12 lines · Ross Philipson
Quoted 5 lines · Lidong Chen

In LzmaEnc_CodeOneBlock(), both GetOptimumFast() and GetOptimum() returns a value of greater or equal to 1, which is assigned to 'len'. But since LZMA_MATCH_LEN_MIN == 2, 'len' should be validated before performing "len - LZMA_MATCH_LEN_MIN" to avoid underflow when 'len' equals to 1.

It seems odd that these internal calls will produce values for len that the calling code can't use and it ends in an error. Does this happen when the input data is bad/malformed/etc? Is it considered an error condition down in those functions when they result in len being set to 1 or 2?

It looks to me that the call to GetOptimum() or GetOptimumFast() returns an encoding decision, not the condition of the input data (line 1840 & 1871).

    if (p->fastMode)
      len = GetOptimumFast(p, &pos);
    else
      len = GetOptimum(p, nowPos32, &pos);

    if (len == 1 && pos == 0xFFFFFFFF)
    {
1840  p->state = kLiteralNextStates[p->state];
    }
    else
    {
      if (pos < LZMA_NUM_REPS)
      {
           if (len == 1)
1871      p->state = kShortRepNextStates[p->state];
      }
      else
      {
        LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
      }
    }

Thanks,
Lidong

Quoted 17 lines

Thanks
Ross

Quoted 13 lines

Fixed: CID 51508

---
 grub-core/lib/LzmaEnc.c | 5 +++++
 1 file changed, 5 insertions(+)
grub-core/lib/LzmaEnc.c +0 -0
index 52b331558..d74e96303 100644
@@ -1880,6 +1880,11 @@ static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize
         UInt32 posSlot, lenToPosState;
         RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
         p->state = kMatchNextStates[p->state];
Quoted 9 lines

grub-core/lib/LzmaEnc.c static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize · line 1890

Quoted 8 lines
grub-core/lib/LzmaEnc.c +5 -0
+ if (len < LZMA_MATCH_LEN_MIN)
+  {
+    p->result = SZ_ERROR_DATA;
+    return CheckErrors(p);
+  }
         LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
         pos -= LZMA_NUM_REPS;
         GetPosSlot(pos, posSlot);

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel