1 //== ArrayBoundCheckerV2.cpp ------------------------------------*- C++ -*--==// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file defines ArrayBoundCheckerV2, which is a path-sensitive check 10 // which looks for an out-of-bound array element access. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/AST/CharUnits.h" 15 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" 16 #include "clang/StaticAnalyzer/Checkers/Taint.h" 17 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" 18 #include "clang/StaticAnalyzer/Core/Checker.h" 19 #include "clang/StaticAnalyzer/Core/CheckerManager.h" 20 #include "clang/StaticAnalyzer/Core/PathSensitive/APSIntType.h" 21 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 22 #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicExtent.h" 23 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" 24 #include "llvm/ADT/SmallString.h" 25 #include "llvm/Support/raw_ostream.h" 26 #include <optional> 27 28 using namespace clang; 29 using namespace ento; 30 using namespace taint; 31 32 namespace { 33 class ArrayBoundCheckerV2 : 34 public Checker<check::Location> { 35 mutable std::unique_ptr<BuiltinBug> BT; 36 mutable std::unique_ptr<BugType> TaintBT; 37 38 enum OOB_Kind { OOB_Precedes, OOB_Excedes }; 39 40 void reportOOB(CheckerContext &C, ProgramStateRef errorState, 41 OOB_Kind kind) const; 42 void reportTaintOOB(CheckerContext &C, ProgramStateRef errorState, 43 SVal TaintedSVal) const; 44 45 static bool isFromCtypeMacro(const Stmt *S, ASTContext &AC); 46 47 public: 48 void checkLocation(SVal l, bool isLoad, const Stmt *S, 49 CheckerContext &C) const; 50 }; 51 52 // FIXME: Eventually replace RegionRawOffset with this class. 53 class RegionRawOffsetV2 { 54 private: 55 const SubRegion *baseRegion; 56 NonLoc byteOffset; 57 58 public: 59 RegionRawOffsetV2(const SubRegion *base, NonLoc offset) 60 : baseRegion(base), byteOffset(offset) { assert(base); } 61 62 NonLoc getByteOffset() const { return byteOffset; } 63 const SubRegion *getRegion() const { return baseRegion; } 64 65 static std::optional<RegionRawOffsetV2> 66 computeOffset(ProgramStateRef State, SValBuilder &SVB, SVal Location); 67 68 void dump() const; 69 void dumpToStream(raw_ostream &os) const; 70 }; 71 } 72 73 // TODO: once the constraint manager is smart enough to handle non simplified 74 // symbolic expressions remove this function. Note that this can not be used in 75 // the constraint manager as is, since this does not handle overflows. It is 76 // safe to assume, however, that memory offsets will not overflow. 77 // NOTE: callers of this function need to be aware of the effects of overflows 78 // and signed<->unsigned conversions! 79 static std::pair<NonLoc, nonloc::ConcreteInt> 80 getSimplifiedOffsets(NonLoc offset, nonloc::ConcreteInt extent, 81 SValBuilder &svalBuilder) { 82 std::optional<nonloc::SymbolVal> SymVal = offset.getAs<nonloc::SymbolVal>(); 83 if (SymVal && SymVal->isExpression()) { 84 if (const SymIntExpr *SIE = dyn_cast<SymIntExpr>(SymVal->getSymbol())) { 85 llvm::APSInt constant = 86 APSIntType(extent.getValue()).convert(SIE->getRHS()); 87 switch (SIE->getOpcode()) { 88 case BO_Mul: 89 // The constant should never be 0 here, since it the result of scaling 90 // based on the size of a type which is never 0. 91 if ((extent.getValue() % constant) != 0) 92 return std::pair<NonLoc, nonloc::ConcreteInt>(offset, extent); 93 else 94 return getSimplifiedOffsets( 95 nonloc::SymbolVal(SIE->getLHS()), 96 svalBuilder.makeIntVal(extent.getValue() / constant), 97 svalBuilder); 98 case BO_Add: 99 return getSimplifiedOffsets( 100 nonloc::SymbolVal(SIE->getLHS()), 101 svalBuilder.makeIntVal(extent.getValue() - constant), svalBuilder); 102 default: 103 break; 104 } 105 } 106 } 107 108 return std::pair<NonLoc, nonloc::ConcreteInt>(offset, extent); 109 } 110 111 // Evaluate the comparison Value < Threshold with the help of the custom 112 // simplification algorithm defined for this checker. Return a pair of states, 113 // where the first one corresponds to "value below threshold" and the second 114 // corresponds to "value at or above threshold". Returns {nullptr, nullptr} in 115 // the case when the evaluation fails. 116 static std::pair<ProgramStateRef, ProgramStateRef> 117 compareValueToThreshold(ProgramStateRef State, NonLoc Value, NonLoc Threshold, 118 SValBuilder &SVB) { 119 if (auto ConcreteThreshold = Threshold.getAs<nonloc::ConcreteInt>()) { 120 std::tie(Value, Threshold) = getSimplifiedOffsets(Value, *ConcreteThreshold, SVB); 121 } 122 if (auto ConcreteThreshold = Threshold.getAs<nonloc::ConcreteInt>()) { 123 QualType T = Value.getType(SVB.getContext()); 124 if (T->isUnsignedIntegerType() && ConcreteThreshold->getValue().isNegative()) { 125 // In this case we reduced the bound check to a comparison of the form 126 // (symbol or value with unsigned type) < (negative number) 127 // which is always false. We are handling these cases separately because 128 // evalBinOpNN can perform a signed->unsigned conversion that turns the 129 // negative number into a huge positive value and leads to wildly 130 // inaccurate conclusions. 131 return {nullptr, State}; 132 } 133 } 134 auto BelowThreshold = 135 SVB.evalBinOpNN(State, BO_LT, Value, Threshold, SVB.getConditionType()).getAs<NonLoc>(); 136 137 if (BelowThreshold) 138 return State->assume(*BelowThreshold); 139 140 return {nullptr, nullptr}; 141 } 142 143 void ArrayBoundCheckerV2::checkLocation(SVal location, bool isLoad, 144 const Stmt* LoadS, 145 CheckerContext &checkerContext) const { 146 147 // NOTE: Instead of using ProgramState::assumeInBound(), we are prototyping 148 // some new logic here that reasons directly about memory region extents. 149 // Once that logic is more mature, we can bring it back to assumeInBound() 150 // for all clients to use. 151 // 152 // The algorithm we are using here for bounds checking is to see if the 153 // memory access is within the extent of the base region. Since we 154 // have some flexibility in defining the base region, we can achieve 155 // various levels of conservatism in our buffer overflow checking. 156 157 // The header ctype.h (from e.g. glibc) implements the isXXXXX() macros as 158 // #define isXXXXX(arg) (LOOKUP_TABLE[arg] & BITMASK_FOR_XXXXX) 159 // and incomplete analysis of these leads to false positives. As even 160 // accurate reports would be confusing for the users, just disable reports 161 // from these macros: 162 if (isFromCtypeMacro(LoadS, checkerContext.getASTContext())) 163 return; 164 165 ProgramStateRef state = checkerContext.getState(); 166 167 SValBuilder &svalBuilder = checkerContext.getSValBuilder(); 168 const std::optional<RegionRawOffsetV2> &RawOffset = 169 RegionRawOffsetV2::computeOffset(state, svalBuilder, location); 170 171 if (!RawOffset) 172 return; 173 174 NonLoc ByteOffset = RawOffset->getByteOffset(); 175 176 // CHECK LOWER BOUND 177 const MemSpaceRegion *SR = RawOffset->getRegion()->getMemorySpace(); 178 if (!llvm::isa<UnknownSpaceRegion>(SR)) { 179 // A pointer to UnknownSpaceRegion may point to the middle of 180 // an allocated region. 181 182 auto [state_precedesLowerBound, state_withinLowerBound] = 183 compareValueToThreshold(state, ByteOffset, 184 svalBuilder.makeZeroArrayIndex(), svalBuilder); 185 186 if (state_precedesLowerBound && !state_withinLowerBound) { 187 // We know that the index definitely precedes the lower bound. 188 reportOOB(checkerContext, state_precedesLowerBound, OOB_Precedes); 189 return; 190 } 191 192 if (state_withinLowerBound) 193 state = state_withinLowerBound; 194 } 195 196 // CHECK UPPER BOUND 197 DefinedOrUnknownSVal Size = 198 getDynamicExtent(state, RawOffset->getRegion(), svalBuilder); 199 if (auto KnownSize = Size.getAs<NonLoc>()) { 200 auto [state_withinUpperBound, state_exceedsUpperBound] = 201 compareValueToThreshold(state, ByteOffset, *KnownSize, svalBuilder); 202 203 if (state_exceedsUpperBound) { 204 if (!state_withinUpperBound) { 205 // We know that the index definitely exceeds the upper bound. 206 reportOOB(checkerContext, state_exceedsUpperBound, OOB_Excedes); 207 return; 208 } 209 if (isTainted(state, ByteOffset)) { 210 // Both cases are possible, but the index is tainted, so report. 211 reportTaintOOB(checkerContext, state_exceedsUpperBound, ByteOffset); 212 return; 213 } 214 } 215 216 if (state_withinUpperBound) 217 state = state_withinUpperBound; 218 } 219 220 checkerContext.addTransition(state); 221 } 222 223 void ArrayBoundCheckerV2::reportTaintOOB(CheckerContext &checkerContext, 224 ProgramStateRef errorState, 225 SVal TaintedSVal) const { 226 ExplodedNode *errorNode = checkerContext.generateErrorNode(errorState); 227 if (!errorNode) 228 return; 229 230 if (!TaintBT) 231 TaintBT.reset( 232 new BugType(this, "Out-of-bound access", categories::TaintedData)); 233 234 SmallString<256> buf; 235 llvm::raw_svector_ostream os(buf); 236 os << "Out of bound memory access (index is tainted)"; 237 auto BR = 238 std::make_unique<PathSensitiveBugReport>(*TaintBT, os.str(), errorNode); 239 240 // Track back the propagation of taintedness. 241 for (SymbolRef Sym : getTaintedSymbols(errorState, TaintedSVal)) { 242 BR->markInteresting(Sym); 243 } 244 245 checkerContext.emitReport(std::move(BR)); 246 } 247 248 void ArrayBoundCheckerV2::reportOOB(CheckerContext &checkerContext, 249 ProgramStateRef errorState, 250 OOB_Kind kind) const { 251 252 ExplodedNode *errorNode = checkerContext.generateErrorNode(errorState); 253 if (!errorNode) 254 return; 255 256 if (!BT) 257 BT.reset(new BuiltinBug(this, "Out-of-bound access")); 258 259 // FIXME: This diagnostics are preliminary. We should get far better 260 // diagnostics for explaining buffer overruns. 261 262 SmallString<256> buf; 263 llvm::raw_svector_ostream os(buf); 264 os << "Out of bound memory access "; 265 switch (kind) { 266 case OOB_Precedes: 267 os << "(accessed memory precedes memory block)"; 268 break; 269 case OOB_Excedes: 270 os << "(access exceeds upper limit of memory block)"; 271 break; 272 } 273 auto BR = std::make_unique<PathSensitiveBugReport>(*BT, os.str(), errorNode); 274 checkerContext.emitReport(std::move(BR)); 275 } 276 277 bool ArrayBoundCheckerV2::isFromCtypeMacro(const Stmt *S, ASTContext &ACtx) { 278 SourceLocation Loc = S->getBeginLoc(); 279 if (!Loc.isMacroID()) 280 return false; 281 282 StringRef MacroName = Lexer::getImmediateMacroName( 283 Loc, ACtx.getSourceManager(), ACtx.getLangOpts()); 284 285 if (MacroName.size() < 7 || MacroName[0] != 'i' || MacroName[1] != 's') 286 return false; 287 288 return ((MacroName == "isalnum") || (MacroName == "isalpha") || 289 (MacroName == "isblank") || (MacroName == "isdigit") || 290 (MacroName == "isgraph") || (MacroName == "islower") || 291 (MacroName == "isnctrl") || (MacroName == "isprint") || 292 (MacroName == "ispunct") || (MacroName == "isspace") || 293 (MacroName == "isupper") || (MacroName == "isxdigit")); 294 } 295 296 #ifndef NDEBUG 297 LLVM_DUMP_METHOD void RegionRawOffsetV2::dump() const { 298 dumpToStream(llvm::errs()); 299 } 300 301 void RegionRawOffsetV2::dumpToStream(raw_ostream &os) const { 302 os << "raw_offset_v2{" << getRegion() << ',' << getByteOffset() << '}'; 303 } 304 #endif 305 306 /// For a given Location that can be represented as a symbolic expression 307 /// Arr[Idx] (or perhaps Arr[Idx1][Idx2] etc.), return the parent memory block 308 /// Arr and the distance of Location from the beginning of Arr (expressed in a 309 /// NonLoc that specifies the number of CharUnits). Returns nullopt when these 310 /// cannot be determined. 311 std::optional<RegionRawOffsetV2> 312 RegionRawOffsetV2::computeOffset(ProgramStateRef State, SValBuilder &SVB, 313 SVal Location) { 314 QualType T = SVB.getArrayIndexType(); 315 auto Calc = [&SVB, State, T](BinaryOperatorKind Op, NonLoc LHS, NonLoc RHS) { 316 // We will use this utility to add and multiply values. 317 return SVB.evalBinOpNN(State, Op, LHS, RHS, T).getAs<NonLoc>(); 318 }; 319 320 const MemRegion *Region = Location.getAsRegion(); 321 NonLoc Offset = SVB.makeZeroArrayIndex(); 322 323 while (Region) { 324 if (const auto *ERegion = dyn_cast<ElementRegion>(Region)) { 325 if (const auto Index = ERegion->getIndex().getAs<NonLoc>()) { 326 QualType ElemType = ERegion->getElementType(); 327 // If the element is an incomplete type, go no further. 328 if (ElemType->isIncompleteType()) 329 return std::nullopt; 330 331 // Perform Offset += Index * sizeof(ElemType); then continue the offset 332 // calculations with SuperRegion: 333 NonLoc Size = SVB.makeArrayIndex( 334 SVB.getContext().getTypeSizeInChars(ElemType).getQuantity()); 335 if (auto Delta = Calc(BO_Mul, *Index, Size)) { 336 if (auto NewOffset = Calc(BO_Add, Offset, *Delta)) { 337 Offset = *NewOffset; 338 Region = ERegion->getSuperRegion(); 339 continue; 340 } 341 } 342 } 343 } else if (const auto *SRegion = dyn_cast<SubRegion>(Region)) { 344 // NOTE: The dyn_cast<>() is expected to succeed, it'd be very surprising 345 // to see a MemSpaceRegion at this point. 346 // FIXME: We may return with {<Region>, 0} even if we didn't handle any 347 // ElementRegion layers. I think that this behavior was introduced 348 // accidentally by 8a4c760c204546aba566e302f299f7ed2e00e287 in 2011, so 349 // it may be useful to review it in the future. 350 return RegionRawOffsetV2(SRegion, Offset); 351 } 352 return std::nullopt; 353 } 354 return std::nullopt; 355 } 356 357 void ento::registerArrayBoundCheckerV2(CheckerManager &mgr) { 358 mgr.registerChecker<ArrayBoundCheckerV2>(); 359 } 360 361 bool ento::shouldRegisterArrayBoundCheckerV2(const CheckerManager &mgr) { 362 return true; 363 } 364