2009-11-06 21:30:44 +08:00
|
|
|
//== ReturnPointerRangeChecker.cpp ------------------------------*- C++ -*--==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines ReturnPointerRangeChecker, which is a path-sensitive check
|
|
|
|
// which looks for an out-of-bound pointer being returned to callers.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "GRExprEngineInternalChecks.h"
|
2010-03-28 05:19:47 +08:00
|
|
|
#include "clang/Checker/BugReporter/BugType.h"
|
2010-01-25 12:41:41 +08:00
|
|
|
#include "clang/Checker/PathSensitive/CheckerVisitor.h"
|
2010-03-28 05:19:47 +08:00
|
|
|
#include "clang/Checker/PathSensitive/GRExprEngine.h"
|
2009-11-06 21:30:44 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
2009-11-28 14:07:30 +08:00
|
|
|
class ReturnPointerRangeChecker :
|
2009-11-06 21:30:44 +08:00
|
|
|
public CheckerVisitor<ReturnPointerRangeChecker> {
|
|
|
|
BuiltinBug *BT;
|
|
|
|
public:
|
|
|
|
ReturnPointerRangeChecker() : BT(0) {}
|
|
|
|
static void *getTag();
|
|
|
|
void PreVisitReturnStmt(CheckerContext &C, const ReturnStmt *RS);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang::RegisterReturnPointerRangeChecker(GRExprEngine &Eng) {
|
|
|
|
Eng.registerCheck(new ReturnPointerRangeChecker());
|
|
|
|
}
|
|
|
|
|
|
|
|
void *ReturnPointerRangeChecker::getTag() {
|
|
|
|
static int x = 0; return &x;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReturnPointerRangeChecker::PreVisitReturnStmt(CheckerContext &C,
|
|
|
|
const ReturnStmt *RS) {
|
|
|
|
const GRState *state = C.getState();
|
|
|
|
|
|
|
|
const Expr *RetE = RS->getRetValue();
|
|
|
|
if (!RetE)
|
|
|
|
return;
|
|
|
|
|
2010-02-09 00:18:51 +08:00
|
|
|
SVal V = state->getSVal(RetE);
|
2009-11-06 21:30:44 +08:00
|
|
|
const MemRegion *R = V.getAsRegion();
|
2009-11-11 19:55:54 +08:00
|
|
|
if (!R)
|
|
|
|
return;
|
|
|
|
|
|
|
|
R = R->StripCasts();
|
|
|
|
if (!R)
|
|
|
|
return;
|
2009-11-06 21:30:44 +08:00
|
|
|
|
|
|
|
const ElementRegion *ER = dyn_cast_or_null<ElementRegion>(R);
|
|
|
|
if (!ER)
|
2009-11-07 04:16:31 +08:00
|
|
|
return;
|
2009-11-06 21:30:44 +08:00
|
|
|
|
|
|
|
DefinedOrUnknownSVal &Idx = cast<DefinedOrUnknownSVal>(ER->getIndex());
|
|
|
|
|
2009-11-11 19:55:54 +08:00
|
|
|
// FIXME: All of this out-of-bounds checking should eventually be refactored
|
|
|
|
// into a common place.
|
2009-11-06 21:30:44 +08:00
|
|
|
|
2009-11-12 10:48:32 +08:00
|
|
|
DefinedOrUnknownSVal NumElements
|
2010-01-18 16:54:31 +08:00
|
|
|
= C.getStoreManager().getSizeInElements(state, ER->getSuperRegion(),
|
|
|
|
ER->getValueType(C.getASTContext()));
|
2009-11-06 21:30:44 +08:00
|
|
|
|
|
|
|
const GRState *StInBound = state->AssumeInBound(Idx, NumElements, true);
|
|
|
|
const GRState *StOutBound = state->AssumeInBound(Idx, NumElements, false);
|
|
|
|
if (StOutBound && !StInBound) {
|
2009-11-24 06:22:01 +08:00
|
|
|
ExplodedNode *N = C.GenerateSink(StOutBound);
|
2009-11-06 21:30:44 +08:00
|
|
|
|
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
|
2009-11-11 19:55:54 +08:00
|
|
|
// FIXME: This bug correspond to CWE-466. Eventually we should have bug
|
|
|
|
// types explicitly reference such exploit categories (when applicable).
|
2009-11-06 21:30:44 +08:00
|
|
|
if (!BT)
|
2009-11-07 04:16:31 +08:00
|
|
|
BT = new BuiltinBug("Return of pointer value outside of expected range",
|
2009-11-11 19:55:54 +08:00
|
|
|
"Returned pointer value points outside the original object "
|
|
|
|
"(potential buffer overflow)");
|
2009-11-07 04:16:31 +08:00
|
|
|
|
2009-11-11 19:55:54 +08:00
|
|
|
// FIXME: It would be nice to eventually make this diagnostic more clear,
|
|
|
|
// e.g., by referencing the original declaration or by saying *why* this
|
|
|
|
// reference is outside the range.
|
2009-11-07 04:16:31 +08:00
|
|
|
|
2009-11-06 21:30:44 +08:00
|
|
|
// Generate a report for this bug.
|
|
|
|
RangedBugReport *report =
|
2009-11-14 20:08:24 +08:00
|
|
|
new RangedBugReport(*BT, BT->getDescription(), N);
|
2009-11-06 21:30:44 +08:00
|
|
|
|
2009-11-07 04:16:31 +08:00
|
|
|
report->addRange(RetE->getSourceRange());
|
2009-11-06 21:30:44 +08:00
|
|
|
C.EmitReport(report);
|
|
|
|
}
|
|
|
|
}
|