Skip to content

Commit 30b3d93

Browse files
authored
Merge pull request #164 from CoreTrace/154-computed-stack-index
fix(stack-buffer): bound x + C and x - C indexes by the bounds of x
2 parents e46c5d2 + 6f978cd commit 30b3d93

2 files changed

Lines changed: 101 additions & 0 deletions

File tree

‎src/analysis/StackBufferAnalysis.cpp‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <optional>
88
#include <unordered_map>
99

10+
#include <llvm/ADT/APInt.h>
1011
#include <llvm/ADT/SmallPtrSet.h>
1112
#include <llvm/Analysis/ValueTracking.h>
1213
#include <llvm/BinaryFormat/Dwarf.h>
@@ -648,6 +649,33 @@ namespace ctrace::stack::analysis
648649

649650
// The `i != N` loop guard is the one branch shape ProgramPointRanges cannot turn
650651
// into an interval; with a constant init and step it still bounds the index.
652+
/// Shifts @p range, the bounds of x, into those of @p op, x + @p offset. Fails when a
653+
/// value of x may make the sum wrap, which a missing bound on that side allows.
654+
static bool shiftRangeWithoutWrap(IntRange& range, std::int64_t offset,
655+
const llvm::BinaryOperator& op, IntReading reading)
656+
{
657+
const unsigned width = op.getType()->getIntegerBitWidth();
658+
if (width >= 64)
659+
return false;
660+
const std::int64_t min = reading == IntReading::Unsigned
661+
? 0
662+
: llvm::APInt::getSignedMinValue(width).getSExtValue();
663+
const std::int64_t max =
664+
reading == IntReading::Unsigned
665+
? static_cast<std::int64_t>(llvm::APInt::getMaxValue(width).getZExtValue())
666+
: llvm::APInt::getSignedMaxValue(width).getSExtValue();
667+
// Going down, the lowest x must stay above min; going up, the highest below max.
668+
if (offset < 0 && (!range.hasLower || range.lower + offset < min))
669+
return false;
670+
if (offset > 0 && (!range.hasUpper || range.upper + offset > max))
671+
return false;
672+
if (range.hasLower)
673+
range.lower += offset;
674+
if (range.hasUpper)
675+
range.upper += offset;
676+
return true;
677+
}
678+
651679
static std::optional<IntRange> deriveNeLoopGuardRange(const llvm::BasicBlock& target,
652680
const llvm::Value* key)
653681
{
@@ -949,6 +977,29 @@ namespace ctrace::stack::analysis
949977
}
950978

951979
// 5) Variable index case: test[i] / ptr[i]
980+
// An index computed as x + C or x - C has the bounds of x shifted by C: look
981+
// for those of x, and shift them once found.
982+
const auto* offsetOp = dyn_cast<BinaryOperator>(baseIdxVal);
983+
const auto* offsetConst =
984+
offsetOp ? dyn_cast<ConstantInt>(offsetOp->getOperand(1)) : nullptr;
985+
std::int64_t offset = 0;
986+
if (offsetConst && offsetConst->getBitWidth() <= 64 &&
987+
!offsetConst->isMinValue(true) &&
988+
(offsetOp->getOpcode() == Instruction::Add ||
989+
offsetOp->getOpcode() == Instruction::Sub))
990+
{
991+
offset = offsetOp->getOpcode() == Instruction::Add
992+
? offsetConst->getSExtValue()
993+
: -offsetConst->getSExtValue();
994+
baseIdxVal = offsetOp->getOperand(0);
995+
while (auto* cast = dyn_cast<CastInst>(baseIdxVal))
996+
{
997+
indexReading =
998+
isa<ZExtInst>(cast) ? IntReading::Unsigned : IntReading::Signed;
999+
baseIdxVal = cast->getOperand(0);
1000+
}
1001+
}
1002+
9521003
// Check whether we have a range for the base value (i, not the cast)
9531004
const Value* key = baseIdxVal;
9541005

@@ -987,6 +1038,10 @@ namespace ctrace::stack::analysis
9871038
hasRange = R.hasLower || R.hasUpper;
9881039
}
9891040

1041+
if (hasRange && offset != 0 &&
1042+
!shiftRangeWithoutWrap(R, offset, *offsetOp, indexReading))
1043+
hasRange = false;
1044+
9901045
if (!hasRange)
9911046
continue;
9921047

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
//
3+
// A stack index computed as n + C or n - C takes its bounds from those of n (#154), as n itself
4+
// does: the write may land before buf (NegativeStackIndex, CWE-124) or past its end
5+
// (StackBufferOverflow, CWE-121).
6+
7+
// n > -5: n - 1 may be -5.
8+
char computed_negative(int n)
9+
{
10+
char buf[8] = {0};
11+
if (n > -5 && n < 8)
12+
buf[n - 1] = 1;
13+
return buf[0];
14+
}
15+
16+
// n < 8: n + 1 may be 8.
17+
char computed_past_end(int n)
18+
{
19+
char buf[8] = {0};
20+
if (n >= 0 && n < 8)
21+
buf[n + 1] = 1;
22+
return buf[0];
23+
}
24+
25+
// 0 < n < 8: n - 1 stays within buf.
26+
char computed_in_bounds(int n)
27+
{
28+
char buf[8] = {0};
29+
if (n > 0 && n < 8)
30+
buf[n - 1] = 1;
31+
return buf[0];
32+
}
33+
34+
// at line 12, column 20
35+
// [!!] potential negative index on variable 'buf' (size 8)
36+
// ↳ alias path: buf
37+
// ↳ inferred lower bound for index expression: -5 (index may be < 0)
38+
// ↳ (this is a write access)
39+
40+
// at line 21, column 20
41+
// [ !!Warn ] potential stack buffer overflow on variable 'buf' (size 8)
42+
// ↳ alias path: buf
43+
// ↳ index variable may go up to 8 (array last valid index: 7)
44+
// ↳ (this is a write access)
45+
46+
// strict-expectation-details: true

0 commit comments

Comments
 (0)