blob: 44887b2625da4bf954fd913c22056272e5dac666 [file] [log] [blame]
Zhongxing Xuceeb02d2009-11-06 13:30:44 +00001//== 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
20using namespace clang;
21
22namespace {
23class VISIBILITY_HIDDEN ReturnPointerRangeChecker :
24 public CheckerVisitor<ReturnPointerRangeChecker> {
25 BuiltinBug *BT;
26public:
27 ReturnPointerRangeChecker() : BT(0) {}
28 static void *getTag();
29 void PreVisitReturnStmt(CheckerContext &C, const ReturnStmt *RS);
30};
31}
32
33void clang::RegisterReturnPointerRangeChecker(GRExprEngine &Eng) {
34 Eng.registerCheck(new ReturnPointerRangeChecker());
35}
36
37void *ReturnPointerRangeChecker::getTag() {
38 static int x = 0; return &x;
39}
40
41void 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 Xub991f482009-11-11 11:55:54 +000051 if (!R)
52 return;
53
54 R = R->StripCasts();
55 if (!R)
56 return;
Zhongxing Xuceeb02d2009-11-06 13:30:44 +000057
58 const ElementRegion *ER = dyn_cast_or_null<ElementRegion>(R);
59 if (!ER)
Ted Kremenek6f516f52009-11-06 20:16:31 +000060 return;
Zhongxing Xuceeb02d2009-11-06 13:30:44 +000061
62 DefinedOrUnknownSVal &Idx = cast<DefinedOrUnknownSVal>(ER->getIndex());
63
Zhongxing Xub991f482009-11-11 11:55:54 +000064 // FIXME: All of this out-of-bounds checking should eventually be refactored
65 // into a common place.
Zhongxing Xuceeb02d2009-11-06 13:30:44 +000066
Zhongxing Xue884ff82009-11-12 02:48:32 +000067 DefinedOrUnknownSVal NumElements
68 = C.getStoreManager().getSizeInElements(state, ER->getSuperRegion());
Zhongxing Xuceeb02d2009-11-06 13:30:44 +000069
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 Xub991f482009-11-11 11:55:54 +000078 // FIXME: This bug correspond to CWE-466. Eventually we should have bug
79 // types explicitly reference such exploit categories (when applicable).
Zhongxing Xuceeb02d2009-11-06 13:30:44 +000080 if (!BT)
Ted Kremenek6f516f52009-11-06 20:16:31 +000081 BT = new BuiltinBug("Return of pointer value outside of expected range",
Zhongxing Xub991f482009-11-11 11:55:54 +000082 "Returned pointer value points outside the original object "
83 "(potential buffer overflow)");
Ted Kremenek6f516f52009-11-06 20:16:31 +000084
Zhongxing Xub991f482009-11-11 11:55:54 +000085 // 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 Kremenek6f516f52009-11-06 20:16:31 +000088
Zhongxing Xuceeb02d2009-11-06 13:30:44 +000089 // Generate a report for this bug.
90 RangedBugReport *report =
Benjamin Kramerd02e2322009-11-14 12:08:24 +000091 new RangedBugReport(*BT, BT->getDescription(), N);
Zhongxing Xuceeb02d2009-11-06 13:30:44 +000092
Ted Kremenek6f516f52009-11-06 20:16:31 +000093 report->addRange(RetE->getSourceRange());
94
Zhongxing Xuceeb02d2009-11-06 13:30:44 +000095 C.EmitReport(report);
96 }
97}