Skip to content

Commit d53bb80

Browse files
committed
[io] More checks to the buffer properties of TKey and TBuffer
thanks to @manop55555 (cherry picked from commit 7a1aedd) (cherry picked from commit 105c8e7)
1 parent 7de9949 commit d53bb80

2 files changed

Lines changed: 46 additions & 3 deletions

File tree

io/io/src/TKey.cxx

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ const static TString gTDirectoryString("TDirectory");
8484
std::atomic<UInt_t> keyAbsNumber{0};
8585

8686
ClassImp(TKey);
87+
namespace {
88+
bool CheckKeyObjLenOverflow(const char *methodName, Int_t keyLen, Int_t objLen)
89+
{
90+
constexpr auto maxInt_t = std::numeric_limits<Int_t>::max();
91+
if (keyLen > (maxInt_t - objLen)) {
92+
Error(methodName, "fObjlen (%d) + fKeylen (%d) > max int (%d): cannot continue to read the key buffer.", objLen,
93+
keyLen, maxInt_t);
94+
return true;
95+
}
96+
return false;
97+
}
98+
} // namespace
8799

88100
////////////////////////////////////////////////////////////////////////////////
89101
/// TKey default constructor.
@@ -1262,9 +1274,9 @@ void TKey::ReadKeyBuffer(char *&buffer)
12621274
return;
12631275
}
12641276

1265-
constexpr auto maxInt_t = std::numeric_limits<Int_t>::max();
1266-
if (fKeylen > (maxInt_t - fObjlen)) {
1267-
Error("ReadKeyBuffer", "fObjlen (%d) + fKeylen (%d) > max int (%d): cannot continue to read the key buffer.", fObjlen, fKeylen, maxInt_t);
1277+
if(CheckKeyObjLenOverflow("ReadKeyBuffer", fKeylen, fObjlen)){
1278+
fKeylen = 0;
1279+
fObjlen = 0;
12681280
MakeZombie();
12691281
return;
12701282
}
@@ -1443,6 +1455,10 @@ void TKey::Streamer(TBuffer &b)
14431455
MakeZombie();
14441456
fNbytes = 0;
14451457
}
1458+
if (CheckKeyObjLenOverflow("Streamer", fKeylen, fObjlen)) {
1459+
MakeZombie();
1460+
return;
1461+
}
14461462

14471463
} else {
14481464
b << fNbytes;

tree/tree/src/TBasket.cxx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*************************************************************************/
1010

1111
#include <chrono>
12+
#include <limits>
1213

1314
#include "TBasket.h"
1415
#include "TBuffer.h"
@@ -32,6 +33,7 @@ const UInt_t kDisplacementMask = 0xFF000000; // In the streamer the two highest
3233
// the fEntryOffset are used to stored displacement.
3334

3435
ClassImp(TBasket);
36+
constexpr auto gMaxInt_t = std::numeric_limits<Int_t>::max();
3537

3638
/** \class TBasket
3739
\ingroup tree
@@ -576,6 +578,31 @@ Int_t TBasket::ReadBasketBuffers(Long64_t pos, Int_t len, TFile *file)
576578
memcpy(rawCompressedBuffer, fBufferRef->Buffer(), len);
577579
}
578580
}
581+
// Sanitize nbytes and lengths
582+
if (fKeylen < 0) {
583+
Error("ReadBasketBuffers", "The value of fKeylen is incorrect (%d) ; trying to recover by setting it to zero", fKeylen);
584+
MakeZombie();
585+
fKeylen = 0;
586+
return 1;
587+
}
588+
if (fObjlen < 0) {
589+
Error("ReadBasketBuffers", "The value of fObjlen is incorrect (%d) ; trying to recover by setting it to zero", fObjlen);
590+
MakeZombie();
591+
fObjlen = 0;
592+
return 1;
593+
}
594+
if (fNbytes < 0) {
595+
Error("ReadBasketBuffers", "The value of fNbytes is incorrect (%d) ; trying to recover by setting it to zero", fNbytes);
596+
MakeZombie();
597+
fNbytes = 0;
598+
return 1;
599+
}
600+
if (fKeylen > (gMaxInt_t - fObjlen)) {
601+
Error("ReadBasketBuffers", "fObjlen (%d) + fKeylen (%d) > max int (%d): cannot continue to read the key buffer.",
602+
fObjlen, fKeylen, gMaxInt_t);
603+
MakeZombie();
604+
return 1;
605+
}
579606

580607
// Initialize buffer to hold the uncompressed data
581608
// Note that in previous versions we didn't allocate buffers until we verified

0 commit comments

Comments
 (0)