dcavalca / rpms / grub2

Forked from rpms/grub2 3 years ago
Clone

Blame SOURCES/0290-lzma-Make-sure-we-don-t-dereference-past-array.patch

80913e
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
80913e
From: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
80913e
Date: Thu, 9 Jul 2020 03:05:23 +0000
80913e
Subject: [PATCH] lzma: Make sure we don't dereference past array
80913e
80913e
The two dimensional array p->posSlotEncoder[4][64] is being dereferenced
80913e
using the GetLenToPosState() macro which checks if len is less than 5,
80913e
and if so subtracts 2 from it. If len = 0, that is 0 - 2 = 4294967294.
80913e
Obviously we don't want to dereference that far out so we check if the
80913e
position found is greater or equal kNumLenToPosStates (4) and bail out.
80913e
80913e
N.B.: Upstream LZMA 18.05 and later has this function completely rewritten
80913e
without any history.
80913e
80913e
Fixes: CID 51526
80913e
80913e
Signed-off-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
80913e
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
80913e
Upstream-commit-id: f91e043bda4
80913e
---
80913e
 grub-core/lib/LzmaEnc.c | 10 ++++++++--
80913e
 1 file changed, 8 insertions(+), 2 deletions(-)
80913e
80913e
diff --git a/grub-core/lib/LzmaEnc.c b/grub-core/lib/LzmaEnc.c
80913e
index f2ec04a8c28..753e56a95e3 100644
80913e
--- a/grub-core/lib/LzmaEnc.c
80913e
+++ b/grub-core/lib/LzmaEnc.c
80913e
@@ -1877,13 +1877,19 @@ static SRes LzmaEnc_CodeOneBlock(CLzmaEnc *p, Bool useLimits, UInt32 maxPackSize
80913e
       }
80913e
       else
80913e
       {
80913e
-        UInt32 posSlot;
80913e
+        UInt32 posSlot, lenToPosState;
80913e
         RangeEnc_EncodeBit(&p->rc, &p->isRep[p->state], 0);
80913e
         p->state = kMatchNextStates[p->state];
80913e
         LenEnc_Encode2(&p->lenEnc, &p->rc, len - LZMA_MATCH_LEN_MIN, posState, !p->fastMode, p->ProbPrices);
80913e
         pos -= LZMA_NUM_REPS;
80913e
         GetPosSlot(pos, posSlot);
80913e
-        RcTree_Encode(&p->rc, p->posSlotEncoder[GetLenToPosState(len)], kNumPosSlotBits, posSlot);
80913e
+        lenToPosState = GetLenToPosState(len);
80913e
+        if (lenToPosState >= kNumLenToPosStates)
80913e
+        {
80913e
+          p->result = SZ_ERROR_DATA;
80913e
+          return CheckErrors(p);
80913e
+        }
80913e
+        RcTree_Encode(&p->rc, p->posSlotEncoder[lenToPosState], kNumPosSlotBits, posSlot);
80913e
 
80913e
         if (posSlot >= kStartPosModelIndex)
80913e
         {