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" |
Benjamin Kramer | 5e2d2c2 | 2010-03-27 21:19:47 +0000 | [diff] [blame] | 16 | #include "clang/Checker/BugReporter/BugType.h" |
Ted Kremenek | 1309f9a | 2010-01-25 04:41:41 +0000 | [diff] [blame] | 17 | #include "clang/Checker/PathSensitive/CheckerVisitor.h" |
Benjamin Kramer | 5e2d2c2 | 2010-03-27 21:19:47 +0000 | [diff] [blame] | 18 | #include "clang/Checker/PathSensitive/GRExprEngine.h" |
Zhongxing Xu | ceeb02d | 2009-11-06 13:30:44 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace clang; |
| 21 | |
| 22 | namespace { |
Kovarththanan Rajaratnam | ba5fb5a | 2009-11-28 06:07:30 +0000 | [diff] [blame] | 23 | class ReturnPointerRangeChecker : |
Zhongxing Xu | ceeb02d | 2009-11-06 13:30:44 +0000 | [diff] [blame] | 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 | |
Ted Kremenek | 1397663 | 2010-02-08 16:18:51 +0000 | [diff] [blame] | 49 | SVal V = state->getSVal(RetE); |
Zhongxing Xu | ceeb02d | 2009-11-06 13:30:44 +0000 | [diff] [blame] | 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 |
Zhongxing Xu | 3ed04d3 | 2010-01-18 08:54:31 +0000 | [diff] [blame] | 68 | = C.getStoreManager().getSizeInElements(state, ER->getSuperRegion(), |
| 69 | ER->getValueType(C.getASTContext())); |
Zhongxing Xu | ceeb02d | 2009-11-06 13:30:44 +0000 | [diff] [blame] | 70 | |
| 71 | const GRState *StInBound = state->AssumeInBound(Idx, NumElements, true); |
| 72 | const GRState *StOutBound = state->AssumeInBound(Idx, NumElements, false); |
| 73 | if (StOutBound && !StInBound) { |
Ted Kremenek | 19d67b5 | 2009-11-23 22:22:01 +0000 | [diff] [blame] | 74 | ExplodedNode *N = C.GenerateSink(StOutBound); |
Zhongxing Xu | ceeb02d | 2009-11-06 13:30:44 +0000 | [diff] [blame] | 75 | |
| 76 | if (!N) |
| 77 | return; |
| 78 | |
Zhongxing Xu | b991f48 | 2009-11-11 11:55:54 +0000 | [diff] [blame] | 79 | // FIXME: This bug correspond to CWE-466. Eventually we should have bug |
| 80 | // types explicitly reference such exploit categories (when applicable). |
Zhongxing Xu | ceeb02d | 2009-11-06 13:30:44 +0000 | [diff] [blame] | 81 | if (!BT) |
Ted Kremenek | 6f516f5 | 2009-11-06 20:16:31 +0000 | [diff] [blame] | 82 | BT = new BuiltinBug("Return of pointer value outside of expected range", |
Zhongxing Xu | b991f48 | 2009-11-11 11:55:54 +0000 | [diff] [blame] | 83 | "Returned pointer value points outside the original object " |
| 84 | "(potential buffer overflow)"); |
Ted Kremenek | 6f516f5 | 2009-11-06 20:16:31 +0000 | [diff] [blame] | 85 | |
Zhongxing Xu | b991f48 | 2009-11-11 11:55:54 +0000 | [diff] [blame] | 86 | // FIXME: It would be nice to eventually make this diagnostic more clear, |
| 87 | // e.g., by referencing the original declaration or by saying *why* this |
| 88 | // reference is outside the range. |
Ted Kremenek | 6f516f5 | 2009-11-06 20:16:31 +0000 | [diff] [blame] | 89 | |
Zhongxing Xu | ceeb02d | 2009-11-06 13:30:44 +0000 | [diff] [blame] | 90 | // Generate a report for this bug. |
| 91 | RangedBugReport *report = |
Benjamin Kramer | d02e232 | 2009-11-14 12:08:24 +0000 | [diff] [blame] | 92 | new RangedBugReport(*BT, BT->getDescription(), N); |
Zhongxing Xu | ceeb02d | 2009-11-06 13:30:44 +0000 | [diff] [blame] | 93 | |
Ted Kremenek | 6f516f5 | 2009-11-06 20:16:31 +0000 | [diff] [blame] | 94 | report->addRange(RetE->getSourceRange()); |
Zhongxing Xu | ceeb02d | 2009-11-06 13:30:44 +0000 | [diff] [blame] | 95 | C.EmitReport(report); |
| 96 | } |
| 97 | } |