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