Skip to content

Commit b06da11

Browse files
committed
Stricter bound checks to ensure buffer sizes are sufficient
1 parent 0f40302 commit b06da11

4 files changed

Lines changed: 68 additions & 1 deletion

File tree

src/test/TestTransforms.cpp

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ limitations under the License.
2020
#include "../types.hpp"
2121
#include "../util/strings.hpp"
2222
#include "../transform/AliasCodec.hpp"
23+
#include "../transform/BWT.hpp"
24+
#include "../transform/BWTS.hpp"
2325
#include "../transform/EXECodec.hpp"
2426
#include "../transform/FSDCodec.hpp"
2527
#include "../transform/LZCodec.hpp"
@@ -494,6 +496,53 @@ static int testZRLTMalformed()
494496
return 0;
495497
}
496498

499+
static int testTransformCapacityValidation()
500+
{
501+
cout << endl
502+
<< "Transform capacity validation" << endl;
503+
504+
Context ctx;
505+
ctx.putInt("bsVersion", BS_VERSION);
506+
kanzi::byte src[2] = { kanzi::byte(1), kanzi::byte(2) };
507+
kanzi::byte dst[2] = { kanzi::byte(0x7E), kanzi::byte(0x7E) };
508+
509+
{
510+
BWT tf;
511+
SliceArray<kanzi::byte> input(src, 1, 0);
512+
SliceArray<kanzi::byte> output(dst, 2, 0);
513+
514+
if (tf.forward(input, output, 2) != false) {
515+
cout << "BWT should reject oversized input count" << endl;
516+
return 1;
517+
}
518+
}
519+
520+
{
521+
BWTS tf;
522+
SliceArray<kanzi::byte> input(src, 2, 0);
523+
SliceArray<kanzi::byte> output(dst, 0, 0);
524+
525+
if (tf.forward(input, output, 1) != false) {
526+
cout << "BWTS should reject oversized output count" << endl;
527+
return 1;
528+
}
529+
}
530+
531+
{
532+
SBRT tf(SBRT::MODE_RANK, ctx);
533+
SliceArray<kanzi::byte> input(src, 2, 1);
534+
SliceArray<kanzi::byte> output(dst, 2, 0);
535+
536+
if (tf.forward(input, output, 2) != false) {
537+
cout << "SBRT should reject oversized remaining input count" << endl;
538+
return 1;
539+
}
540+
}
541+
542+
cout << "Transform capacity validation passed" << endl;
543+
return 0;
544+
}
545+
497546
static Transform<kanzi::byte>* getByteTransform(string name, Context& ctx)
498547
{
499548
if (name.compare("SRT") == 0)
@@ -999,6 +1048,11 @@ int TestTransforms_main(int argc, const char* argv[])
9991048

10001049
res = testZRLTMalformed();
10011050

1051+
if (res != 0)
1052+
return res;
1053+
1054+
res = testTransformCapacityValidation();
1055+
10021056
if (res != 0)
10031057
return res;
10041058

src/transform/BWT.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,10 @@ bool BWT::forward(SliceArray<kanzi::byte>& input, SliceArray<kanzi::byte>& outpu
100100
if (!SliceArray<kanzi::byte>::isValid(output))
101101
throw invalid_argument("BWT: Invalid output block");
102102

103-
if (count > MAX_BLOCK_SIZE)
103+
if ((count < 0) ||
104+
(count > input._length - input._index) ||
105+
(count > output._length - output._index) ||
106+
(count > MAX_BLOCK_SIZE))
104107
return false;
105108

106109
if (count == 1) {

src/transform/BWTS.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ bool BWTS::forward(SliceArray<kanzi::byte>& input, SliceArray<kanzi::byte>& outp
3636
if (!SliceArray<kanzi::byte>::isValid(output))
3737
throw invalid_argument("BWTS: Invalid output block");
3838

39+
if ((count < 0) ||
40+
(count > input._length - input._index) ||
41+
(count > output._length - output._index))
42+
return false;
43+
3944
if (count > MAX_BLOCK_SIZE) {
4045
// Not a recoverable error: instead of silently fail the transform,
4146
// issue a fatal error.

src/transform/SBRT.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ bool SBRT::forward(SliceArray<kanzi::byte>& input, SliceArray<kanzi::byte>& outp
5454
if (!SliceArray<kanzi::byte>::isValid(output))
5555
throw std::invalid_argument("SBRT: Invalid output block");
5656

57+
if ((count < 0) ||
58+
(count > input._length - input._index) ||
59+
(count > output._length - output._index))
60+
return false;
61+
5762
// Aliasing
5863
const kanzi::byte* src = &input._array[input._index];
5964
kanzi::byte* dst = &output._array[output._index];

0 commit comments

Comments
 (0)