ReturnPointerRangeChecker.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. //== ReturnPointerRangeChecker.cpp ------------------------------*- C++ -*--==//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file defines ReturnPointerRangeChecker, which is a path-sensitive check
  11. // which looks for an out-of-bound pointer being returned to callers.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "InternalChecks.h"
  15. #include "clang/StaticAnalyzer/BugReporter/BugType.h"
  16. #include "clang/StaticAnalyzer/PathSensitive/CheckerVisitor.h"
  17. #include "clang/StaticAnalyzer/PathSensitive/ExprEngine.h"
  18. using namespace clang;
  19. using namespace ento;
  20. namespace {
  21. class ReturnPointerRangeChecker :
  22. public CheckerVisitor<ReturnPointerRangeChecker> {
  23. BuiltinBug *BT;
  24. public:
  25. ReturnPointerRangeChecker() : BT(0) {}
  26. static void *getTag();
  27. void PreVisitReturnStmt(CheckerContext &C, const ReturnStmt *RS);
  28. };
  29. }
  30. void ento::RegisterReturnPointerRangeChecker(ExprEngine &Eng) {
  31. Eng.registerCheck(new ReturnPointerRangeChecker());
  32. }
  33. void *ReturnPointerRangeChecker::getTag() {
  34. static int x = 0; return &x;
  35. }
  36. void ReturnPointerRangeChecker::PreVisitReturnStmt(CheckerContext &C,
  37. const ReturnStmt *RS) {
  38. const GRState *state = C.getState();
  39. const Expr *RetE = RS->getRetValue();
  40. if (!RetE)
  41. return;
  42. SVal V = state->getSVal(RetE);
  43. const MemRegion *R = V.getAsRegion();
  44. const ElementRegion *ER = dyn_cast_or_null<ElementRegion>(R);
  45. if (!ER)
  46. return;
  47. DefinedOrUnknownSVal Idx = cast<DefinedOrUnknownSVal>(ER->getIndex());
  48. // Zero index is always in bound, this also passes ElementRegions created for
  49. // pointer casts.
  50. if (Idx.isZeroConstant())
  51. return;
  52. // FIXME: All of this out-of-bounds checking should eventually be refactored
  53. // into a common place.
  54. DefinedOrUnknownSVal NumElements
  55. = C.getStoreManager().getSizeInElements(state, ER->getSuperRegion(),
  56. ER->getValueType());
  57. const GRState *StInBound = state->assumeInBound(Idx, NumElements, true);
  58. const GRState *StOutBound = state->assumeInBound(Idx, NumElements, false);
  59. if (StOutBound && !StInBound) {
  60. ExplodedNode *N = C.generateSink(StOutBound);
  61. if (!N)
  62. return;
  63. // FIXME: This bug correspond to CWE-466. Eventually we should have bug
  64. // types explicitly reference such exploit categories (when applicable).
  65. if (!BT)
  66. BT = new BuiltinBug("Return of pointer value outside of expected range",
  67. "Returned pointer value points outside the original object "
  68. "(potential buffer overflow)");
  69. // FIXME: It would be nice to eventually make this diagnostic more clear,
  70. // e.g., by referencing the original declaration or by saying *why* this
  71. // reference is outside the range.
  72. // Generate a report for this bug.
  73. RangedBugReport *report =
  74. new RangedBugReport(*BT, BT->getDescription(), N);
  75. report->addRange(RetE->getSourceRange());
  76. C.EmitReport(report);
  77. }
  78. }