blob: 65e81315f095eceb20b4a1cd424dc33263a72284 [file] [log] [blame]
Daniel Marjamaki13264eb2016-09-26 15:17:18 +00001//=== CastToStructChecker.cpp ----------------------------------*- C++ -*--===//
Zhongxing Xuf06c6842009-11-09 08:07:38 +00002//
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
Daniel Marjamaki13264eb2016-09-26 15:17:18 +000011// cast from non-struct pointer to struct pointer and widening struct data cast.
Zhongxing Xuf06c6842009-11-09 08:07:38 +000012// This check corresponds to CWE-588.
13//
14//===----------------------------------------------------------------------===//
15
Argyrios Kyrtzidisa9215282011-02-15 22:55:20 +000016#include "ClangSACheckers.h"
Daniel Marjamaki13264eb2016-09-26 15:17:18 +000017#include "clang/AST/RecursiveASTVisitor.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
Argyrios Kyrtzidis6a5674f2011-03-01 01:16:21 +000019#include "clang/StaticAnalyzer/Core/Checker.h"
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +000020#include "clang/StaticAnalyzer/Core/CheckerManager.h"
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000021#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
Zhongxing Xuf06c6842009-11-09 08:07:38 +000022
23using namespace clang;
Ted Kremenek98857c92010-12-23 07:20:52 +000024using namespace ento;
Zhongxing Xuf06c6842009-11-09 08:07:38 +000025
26namespace {
Daniel Marjamaki13264eb2016-09-26 15:17:18 +000027class CastToStructVisitor : public RecursiveASTVisitor<CastToStructVisitor> {
28 BugReporter &BR;
29 const CheckerBase *Checker;
30 AnalysisDeclContext *AC;
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +000031
Zhongxing Xuf06c6842009-11-09 08:07:38 +000032public:
Daniel Marjamaki13264eb2016-09-26 15:17:18 +000033 explicit CastToStructVisitor(BugReporter &B, const CheckerBase *Checker,
34 AnalysisDeclContext *A)
35 : BR(B), Checker(Checker), AC(A) {}
36 bool VisitCastExpr(const CastExpr *CE);
Zhongxing Xuf06c6842009-11-09 08:07:38 +000037};
38}
39
Daniel Marjamaki13264eb2016-09-26 15:17:18 +000040bool CastToStructVisitor::VisitCastExpr(const CastExpr *CE) {
Zhongxing Xuf06c6842009-11-09 08:07:38 +000041 const Expr *E = CE->getSubExpr();
Daniel Marjamaki13264eb2016-09-26 15:17:18 +000042 ASTContext &Ctx = AC->getASTContext();
Zhongxing Xuf06c6842009-11-09 08:07:38 +000043 QualType OrigTy = Ctx.getCanonicalType(E->getType());
44 QualType ToTy = Ctx.getCanonicalType(CE->getType());
45
John McCall424cec92011-01-19 06:33:43 +000046 const PointerType *OrigPTy = dyn_cast<PointerType>(OrigTy.getTypePtr());
47 const PointerType *ToPTy = dyn_cast<PointerType>(ToTy.getTypePtr());
Zhongxing Xuf06c6842009-11-09 08:07:38 +000048
49 if (!ToPTy || !OrigPTy)
Daniel Marjamaki13264eb2016-09-26 15:17:18 +000050 return true;
Zhongxing Xuf06c6842009-11-09 08:07:38 +000051
52 QualType OrigPointeeTy = OrigPTy->getPointeeType();
53 QualType ToPointeeTy = ToPTy->getPointeeType();
54
Douglas Gregor8385a062010-04-26 21:31:17 +000055 if (!ToPointeeTy->isStructureOrClassType())
Daniel Marjamaki13264eb2016-09-26 15:17:18 +000056 return true;
Zhongxing Xuf06c6842009-11-09 08:07:38 +000057
58 // We allow cast from void*.
59 if (OrigPointeeTy->isVoidType())
Daniel Marjamaki13264eb2016-09-26 15:17:18 +000060 return true;
Zhongxing Xuf06c6842009-11-09 08:07:38 +000061
62 // Now the cast-to-type is struct pointer, the original type is not void*.
63 if (!OrigPointeeTy->isRecordType()) {
Daniel Marjamaki13264eb2016-09-26 15:17:18 +000064 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 = dyn_cast<ValueDecl>(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
Daniel Marjamakicf715bd2017-03-07 19:20:48 +000087 if (ToPointeeTy->isIncompleteType() ||
88 OrigPointeeTy->isIncompleteType())
89 return true;
90
Daniel Marjamaki13264eb2016-09-26 15:17:18 +000091 // 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());
Zhongxing Xuf06c6842009-11-09 08:07:38 +0000104 }
Daniel Marjamaki13264eb2016-09-26 15:17:18 +0000105
106 return true;
Zhongxing Xuf06c6842009-11-09 08:07:38 +0000107}
108
Daniel Marjamaki13264eb2016-09-26 15:17:18 +0000109namespace {
110class CastToStructChecker : public Checker<check::ASTCodeBody> {
111public:
112 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
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +0000120void ento::registerCastToStructChecker(CheckerManager &mgr) {
Argyrios Kyrtzidisdff865d2011-02-23 01:05:36 +0000121 mgr.registerChecker<CastToStructChecker>();
Argyrios Kyrtzidis507ff532011-02-17 21:39:17 +0000122}