Zhongxing Xu | ceeb02d | 2009-11-06 13:30:44 +0000 | [diff] [blame] | 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 | |
| 15 | #include "GRExprEngineInternalChecks.h" |
| 16 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
| 17 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
| 18 | #include "clang/Analysis/PathSensitive/CheckerVisitor.h" |
| 19 | |
| 20 | using namespace clang; |
| 21 | |
| 22 | namespace { |
| 23 | class VISIBILITY_HIDDEN ReturnPointerRangeChecker : |
| 24 | public CheckerVisitor<ReturnPointerRangeChecker> { |
| 25 | BuiltinBug *BT; |
| 26 | public: |
| 27 | ReturnPointerRangeChecker() : BT(0) {} |
| 28 | static void *getTag(); |
| 29 | void PreVisitReturnStmt(CheckerContext &C, const ReturnStmt *RS); |
| 30 | }; |
| 31 | } |
| 32 | |
| 33 | void clang::RegisterReturnPointerRangeChecker(GRExprEngine &Eng) { |
| 34 | Eng.registerCheck(new ReturnPointerRangeChecker()); |
| 35 | } |
| 36 | |
| 37 | void *ReturnPointerRangeChecker::getTag() { |
| 38 | static int x = 0; return &x; |
| 39 | } |
| 40 | |
| 41 | void ReturnPointerRangeChecker::PreVisitReturnStmt(CheckerContext &C, |
| 42 | const ReturnStmt *RS) { |
| 43 | const GRState *state = C.getState(); |
| 44 | |
| 45 | const Expr *RetE = RS->getRetValue(); |
| 46 | if (!RetE) |
| 47 | return; |
| 48 | |
| 49 | SVal V = state->getSVal(RetE); |
| 50 | const MemRegion *R = V.getAsRegion(); |
Zhongxing Xu | b991f48 | 2009-11-11 11:55:54 +0000 | [diff] [blame] | 51 | if (!R) |
| 52 | return; |
| 53 | |
| 54 | R = R->StripCasts(); |
| 55 | if (!R) |
| 56 | return; |
Zhongxing Xu | ceeb02d | 2009-11-06 13:30:44 +0000 | [diff] [blame] | 57 | |
| 58 | const ElementRegion *ER = dyn_cast_or_null<ElementRegion>(R); |
| 59 | if (!ER) |
Ted Kremenek | 6f516f5 | 2009-11-06 20:16:31 +0000 | [diff] [blame] | 60 | return; |
Zhongxing Xu | ceeb02d | 2009-11-06 13:30:44 +0000 | [diff] [blame] | 61 | |
| 62 | DefinedOrUnknownSVal &Idx = cast<DefinedOrUnknownSVal>(ER->getIndex()); |
| 63 | |
Zhongxing Xu | b991f48 | 2009-11-11 11:55:54 +0000 | [diff] [blame] | 64 | // FIXME: All of this out-of-bounds checking should eventually be refactored |
| 65 | // into a common place. |
Zhongxing Xu | ceeb02d | 2009-11-06 13:30:44 +0000 | [diff] [blame] | 66 | |
Zhongxing Xu | e884ff8 | 2009-11-12 02:48:32 +0000 | [diff] [blame] | 67 | DefinedOrUnknownSVal NumElements |
| 68 | = C.getStoreManager().getSizeInElements(state, ER->getSuperRegion()); |
Zhongxing Xu | ceeb02d | 2009-11-06 13:30:44 +0000 | [diff] [blame] | 69 | |
| 70 | const GRState *StInBound = state->AssumeInBound(Idx, NumElements, true); |
| 71 | const GRState *StOutBound = state->AssumeInBound(Idx, NumElements, false); |
| 72 | if (StOutBound && !StInBound) { |
| 73 | ExplodedNode *N = C.GenerateNode(RS, StOutBound, true); |
| 74 | |
| 75 | if (!N) |
| 76 | return; |
| 77 | |
Zhongxing Xu | b991f48 | 2009-11-11 11:55:54 +0000 | [diff] [blame] | 78 | // FIXME: This bug correspond to CWE-466. Eventually we should have bug |
| 79 | // types explicitly reference such exploit categories (when applicable). |
Zhongxing Xu | ceeb02d | 2009-11-06 13:30:44 +0000 | [diff] [blame] | 80 | if (!BT) |
Ted Kremenek | 6f516f5 | 2009-11-06 20:16:31 +0000 | [diff] [blame] | 81 | BT = new BuiltinBug("Return of pointer value outside of expected range", |
Zhongxing Xu | b991f48 | 2009-11-11 11:55:54 +0000 | [diff] [blame] | 82 | "Returned pointer value points outside the original object " |
| 83 | "(potential buffer overflow)"); |
Ted Kremenek | 6f516f5 | 2009-11-06 20:16:31 +0000 | [diff] [blame] | 84 | |
Zhongxing Xu | b991f48 | 2009-11-11 11:55:54 +0000 | [diff] [blame] | 85 | // FIXME: It would be nice to eventually make this diagnostic more clear, |
| 86 | // e.g., by referencing the original declaration or by saying *why* this |
| 87 | // reference is outside the range. |
Ted Kremenek | 6f516f5 | 2009-11-06 20:16:31 +0000 | [diff] [blame] | 88 | |
Zhongxing Xu | ceeb02d | 2009-11-06 13:30:44 +0000 | [diff] [blame] | 89 | // Generate a report for this bug. |
| 90 | RangedBugReport *report = |
Benjamin Kramer | d02e232 | 2009-11-14 12:08:24 +0000 | [diff] [blame] | 91 | new RangedBugReport(*BT, BT->getDescription(), N); |
Zhongxing Xu | ceeb02d | 2009-11-06 13:30:44 +0000 | [diff] [blame] | 92 | |
Ted Kremenek | 6f516f5 | 2009-11-06 20:16:31 +0000 | [diff] [blame] | 93 | report->addRange(RetE->getSourceRange()); |
| 94 | |
Zhongxing Xu | ceeb02d | 2009-11-06 13:30:44 +0000 | [diff] [blame] | 95 | C.EmitReport(report); |
| 96 | } |
| 97 | } |