1e5dd7070Spatrick //=== CastToStructChecker.cpp ----------------------------------*- C++ -*--===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick // This files defines CastToStructChecker, a builtin checker that checks for
10e5dd7070Spatrick // cast from non-struct pointer to struct pointer and widening struct data cast.
11e5dd7070Spatrick // This check corresponds to CWE-588.
12e5dd7070Spatrick //
13e5dd7070Spatrick //===----------------------------------------------------------------------===//
14e5dd7070Spatrick
15e5dd7070Spatrick #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
16e5dd7070Spatrick #include "clang/AST/RecursiveASTVisitor.h"
17e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
18e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/Checker.h"
19e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/CheckerManager.h"
20e5dd7070Spatrick #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
21e5dd7070Spatrick
22e5dd7070Spatrick using namespace clang;
23e5dd7070Spatrick using namespace ento;
24e5dd7070Spatrick
25e5dd7070Spatrick namespace {
26e5dd7070Spatrick class CastToStructVisitor : public RecursiveASTVisitor<CastToStructVisitor> {
27e5dd7070Spatrick BugReporter &BR;
28e5dd7070Spatrick const CheckerBase *Checker;
29e5dd7070Spatrick AnalysisDeclContext *AC;
30e5dd7070Spatrick
31e5dd7070Spatrick public:
CastToStructVisitor(BugReporter & B,const CheckerBase * Checker,AnalysisDeclContext * A)32e5dd7070Spatrick explicit CastToStructVisitor(BugReporter &B, const CheckerBase *Checker,
33e5dd7070Spatrick AnalysisDeclContext *A)
34e5dd7070Spatrick : BR(B), Checker(Checker), AC(A) {}
35e5dd7070Spatrick bool VisitCastExpr(const CastExpr *CE);
36e5dd7070Spatrick };
37e5dd7070Spatrick }
38e5dd7070Spatrick
VisitCastExpr(const CastExpr * CE)39e5dd7070Spatrick bool CastToStructVisitor::VisitCastExpr(const CastExpr *CE) {
40e5dd7070Spatrick const Expr *E = CE->getSubExpr();
41e5dd7070Spatrick ASTContext &Ctx = AC->getASTContext();
42e5dd7070Spatrick QualType OrigTy = Ctx.getCanonicalType(E->getType());
43e5dd7070Spatrick QualType ToTy = Ctx.getCanonicalType(CE->getType());
44e5dd7070Spatrick
45e5dd7070Spatrick const PointerType *OrigPTy = dyn_cast<PointerType>(OrigTy.getTypePtr());
46e5dd7070Spatrick const PointerType *ToPTy = dyn_cast<PointerType>(ToTy.getTypePtr());
47e5dd7070Spatrick
48e5dd7070Spatrick if (!ToPTy || !OrigPTy)
49e5dd7070Spatrick return true;
50e5dd7070Spatrick
51e5dd7070Spatrick QualType OrigPointeeTy = OrigPTy->getPointeeType();
52e5dd7070Spatrick QualType ToPointeeTy = ToPTy->getPointeeType();
53e5dd7070Spatrick
54e5dd7070Spatrick if (!ToPointeeTy->isStructureOrClassType())
55e5dd7070Spatrick return true;
56e5dd7070Spatrick
57e5dd7070Spatrick // We allow cast from void*.
58e5dd7070Spatrick if (OrigPointeeTy->isVoidType())
59e5dd7070Spatrick return true;
60e5dd7070Spatrick
61e5dd7070Spatrick // Now the cast-to-type is struct pointer, the original type is not void*.
62e5dd7070Spatrick if (!OrigPointeeTy->isRecordType()) {
63e5dd7070Spatrick SourceRange Sr[1] = {CE->getSourceRange()};
64e5dd7070Spatrick PathDiagnosticLocation Loc(CE, BR.getSourceManager(), AC);
65e5dd7070Spatrick BR.EmitBasicReport(
66e5dd7070Spatrick AC->getDecl(), Checker, "Cast from non-struct type to struct type",
67e5dd7070Spatrick categories::LogicError, "Casting a non-structure type to a structure "
68e5dd7070Spatrick "type and accessing a field can lead to memory "
69e5dd7070Spatrick "access errors or data corruption.",
70e5dd7070Spatrick Loc, Sr);
71e5dd7070Spatrick } else {
72e5dd7070Spatrick // Don't warn when size of data is unknown.
73e5dd7070Spatrick const auto *U = dyn_cast<UnaryOperator>(E);
74e5dd7070Spatrick if (!U || U->getOpcode() != UO_AddrOf)
75e5dd7070Spatrick return true;
76e5dd7070Spatrick
77e5dd7070Spatrick // Don't warn for references
78e5dd7070Spatrick const ValueDecl *VD = nullptr;
79e5dd7070Spatrick if (const auto *SE = dyn_cast<DeclRefExpr>(U->getSubExpr()))
80e5dd7070Spatrick VD = SE->getDecl();
81e5dd7070Spatrick else if (const auto *SE = dyn_cast<MemberExpr>(U->getSubExpr()))
82e5dd7070Spatrick VD = SE->getMemberDecl();
83e5dd7070Spatrick if (!VD || VD->getType()->isReferenceType())
84e5dd7070Spatrick return true;
85e5dd7070Spatrick
86e5dd7070Spatrick if (ToPointeeTy->isIncompleteType() ||
87e5dd7070Spatrick OrigPointeeTy->isIncompleteType())
88e5dd7070Spatrick return true;
89e5dd7070Spatrick
90e5dd7070Spatrick // Warn when there is widening cast.
91e5dd7070Spatrick unsigned ToWidth = Ctx.getTypeInfo(ToPointeeTy).Width;
92e5dd7070Spatrick unsigned OrigWidth = Ctx.getTypeInfo(OrigPointeeTy).Width;
93e5dd7070Spatrick if (ToWidth <= OrigWidth)
94e5dd7070Spatrick return true;
95e5dd7070Spatrick
96e5dd7070Spatrick PathDiagnosticLocation Loc(CE, BR.getSourceManager(), AC);
97e5dd7070Spatrick BR.EmitBasicReport(AC->getDecl(), Checker, "Widening cast to struct type",
98e5dd7070Spatrick categories::LogicError,
99e5dd7070Spatrick "Casting data to a larger structure type and accessing "
100e5dd7070Spatrick "a field can lead to memory access errors or data "
101e5dd7070Spatrick "corruption.",
102e5dd7070Spatrick Loc, CE->getSourceRange());
103e5dd7070Spatrick }
104e5dd7070Spatrick
105e5dd7070Spatrick return true;
106e5dd7070Spatrick }
107e5dd7070Spatrick
108e5dd7070Spatrick namespace {
109e5dd7070Spatrick class CastToStructChecker : public Checker<check::ASTCodeBody> {
110e5dd7070Spatrick public:
checkASTCodeBody(const Decl * D,AnalysisManager & Mgr,BugReporter & BR) const111e5dd7070Spatrick void checkASTCodeBody(const Decl *D, AnalysisManager &Mgr,
112e5dd7070Spatrick BugReporter &BR) const {
113e5dd7070Spatrick CastToStructVisitor Visitor(BR, this, Mgr.getAnalysisDeclContext(D));
114e5dd7070Spatrick Visitor.TraverseDecl(const_cast<Decl *>(D));
115e5dd7070Spatrick }
116e5dd7070Spatrick };
117e5dd7070Spatrick } // end anonymous namespace
118e5dd7070Spatrick
registerCastToStructChecker(CheckerManager & mgr)119e5dd7070Spatrick void ento::registerCastToStructChecker(CheckerManager &mgr) {
120e5dd7070Spatrick mgr.registerChecker<CastToStructChecker>();
121e5dd7070Spatrick }
122e5dd7070Spatrick
shouldRegisterCastToStructChecker(const CheckerManager & mgr)123*ec727ea7Spatrick bool ento::shouldRegisterCastToStructChecker(const CheckerManager &mgr) {
124e5dd7070Spatrick return true;
125e5dd7070Spatrick }
126