1 //== ReturnPointerRangeChecker.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 ReturnPointerRangeChecker, which is a path-sensitive check
10 // which looks for an out-of-bound pointer being returned to callers.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
15 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
16 #include "clang/StaticAnalyzer/Core/Checker.h"
17 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
18 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicExtent.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
21 
22 using namespace clang;
23 using namespace ento;
24 
25 namespace {
26 class ReturnPointerRangeChecker :
27     public Checker< check::PreStmt<ReturnStmt> > {
28   mutable std::unique_ptr<BuiltinBug> BT;
29 
30 public:
31     void checkPreStmt(const ReturnStmt *RS, CheckerContext &C) const;
32 };
33 }
34 
35 void ReturnPointerRangeChecker::checkPreStmt(const ReturnStmt *RS,
36                                              CheckerContext &C) const {
37   ProgramStateRef state = C.getState();
38 
39   const Expr *RetE = RS->getRetValue();
40   if (!RetE)
41     return;
42 
43   SVal V = C.getSVal(RetE);
44   const MemRegion *R = V.getAsRegion();
45 
46   const ElementRegion *ER = dyn_cast_or_null<ElementRegion>(R);
47   if (!ER)
48     return;
49 
50   DefinedOrUnknownSVal Idx = ER->getIndex().castAs<DefinedOrUnknownSVal>();
51   // Zero index is always in bound, this also passes ElementRegions created for
52   // pointer casts.
53   if (Idx.isZeroConstant())
54     return;
55 
56   // FIXME: All of this out-of-bounds checking should eventually be refactored
57   // into a common place.
58   DefinedOrUnknownSVal ElementCount = getDynamicElementCount(
59       state, ER->getSuperRegion(), C.getSValBuilder(), ER->getValueType());
60 
61   // We assume that the location after the last element in the array is used as
62   // end() iterator. Reporting on these would return too many false positives.
63   if (Idx == ElementCount)
64     return;
65 
66   ProgramStateRef StInBound = state->assumeInBound(Idx, ElementCount, true);
67   ProgramStateRef StOutBound = state->assumeInBound(Idx, ElementCount, false);
68   if (StOutBound && !StInBound) {
69     ExplodedNode *N = C.generateErrorNode(StOutBound);
70 
71     if (!N)
72       return;
73 
74     // FIXME: This bug correspond to CWE-466.  Eventually we should have bug
75     // types explicitly reference such exploit categories (when applicable).
76     if (!BT)
77       BT.reset(new BuiltinBug(
78           this, "Buffer overflow",
79           "Returned pointer value points outside the original object "
80           "(potential buffer overflow)"));
81 
82     // FIXME: It would be nice to eventually make this diagnostic more clear,
83     // e.g., by referencing the original declaration or by saying *why* this
84     // reference is outside the range.
85 
86     // Generate a report for this bug.
87     auto report =
88         std::make_unique<PathSensitiveBugReport>(*BT, BT->getDescription(), N);
89 
90     report->addRange(RetE->getSourceRange());
91     C.emitReport(std::move(report));
92   }
93 }
94 
95 void ento::registerReturnPointerRangeChecker(CheckerManager &mgr) {
96   mgr.registerChecker<ReturnPointerRangeChecker>();
97 }
98 
99 bool ento::shouldRegisterReturnPointerRangeChecker(const CheckerManager &mgr) {
100   return true;
101 }
102