17330f729Sjoerg //=== CastToStructChecker.cpp ----------------------------------*- C++ -*--===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // This files defines CastToStructChecker, a builtin checker that checks for
107330f729Sjoerg // cast from non-struct pointer to struct pointer and widening struct data cast.
117330f729Sjoerg // This check corresponds to CWE-588.
127330f729Sjoerg //
137330f729Sjoerg //===----------------------------------------------------------------------===//
147330f729Sjoerg
157330f729Sjoerg #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
167330f729Sjoerg #include "clang/AST/RecursiveASTVisitor.h"
177330f729Sjoerg #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
187330f729Sjoerg #include "clang/StaticAnalyzer/Core/Checker.h"
197330f729Sjoerg #include "clang/StaticAnalyzer/Core/CheckerManager.h"
207330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
217330f729Sjoerg
227330f729Sjoerg using namespace clang;
237330f729Sjoerg using namespace ento;
247330f729Sjoerg
257330f729Sjoerg namespace {
267330f729Sjoerg class CastToStructVisitor : public RecursiveASTVisitor<CastToStructVisitor> {
277330f729Sjoerg BugReporter &BR;
287330f729Sjoerg const CheckerBase *Checker;
297330f729Sjoerg AnalysisDeclContext *AC;
307330f729Sjoerg
317330f729Sjoerg public:
CastToStructVisitor(BugReporter & B,const CheckerBase * Checker,AnalysisDeclContext * A)327330f729Sjoerg explicit CastToStructVisitor(BugReporter &B, const CheckerBase *Checker,
337330f729Sjoerg AnalysisDeclContext *A)
347330f729Sjoerg : BR(B), Checker(Checker), AC(A) {}
357330f729Sjoerg bool VisitCastExpr(const CastExpr *CE);
367330f729Sjoerg };
377330f729Sjoerg }
387330f729Sjoerg
VisitCastExpr(const CastExpr * CE)397330f729Sjoerg bool CastToStructVisitor::VisitCastExpr(const CastExpr *CE) {
407330f729Sjoerg const Expr *E = CE->getSubExpr();
417330f729Sjoerg ASTContext &Ctx = AC->getASTContext();
427330f729Sjoerg QualType OrigTy = Ctx.getCanonicalType(E->getType());
437330f729Sjoerg QualType ToTy = Ctx.getCanonicalType(CE->getType());
447330f729Sjoerg
457330f729Sjoerg const PointerType *OrigPTy = dyn_cast<PointerType>(OrigTy.getTypePtr());
467330f729Sjoerg const PointerType *ToPTy = dyn_cast<PointerType>(ToTy.getTypePtr());
477330f729Sjoerg
487330f729Sjoerg if (!ToPTy || !OrigPTy)
497330f729Sjoerg return true;
507330f729Sjoerg
517330f729Sjoerg QualType OrigPointeeTy = OrigPTy->getPointeeType();
527330f729Sjoerg QualType ToPointeeTy = ToPTy->getPointeeType();
537330f729Sjoerg
547330f729Sjoerg if (!ToPointeeTy->isStructureOrClassType())
557330f729Sjoerg return true;
567330f729Sjoerg
577330f729Sjoerg // We allow cast from void*.
587330f729Sjoerg if (OrigPointeeTy->isVoidType())
597330f729Sjoerg return true;
607330f729Sjoerg
617330f729Sjoerg // Now the cast-to-type is struct pointer, the original type is not void*.
627330f729Sjoerg if (!OrigPointeeTy->isRecordType()) {
637330f729Sjoerg SourceRange Sr[1] = {CE->getSourceRange()};
647330f729Sjoerg PathDiagnosticLocation Loc(CE, BR.getSourceManager(), AC);
657330f729Sjoerg BR.EmitBasicReport(
667330f729Sjoerg AC->getDecl(), Checker, "Cast from non-struct type to struct type",
677330f729Sjoerg categories::LogicError, "Casting a non-structure type to a structure "
687330f729Sjoerg "type and accessing a field can lead to memory "
697330f729Sjoerg "access errors or data corruption.",
707330f729Sjoerg Loc, Sr);
717330f729Sjoerg } else {
727330f729Sjoerg // Don't warn when size of data is unknown.
737330f729Sjoerg const auto *U = dyn_cast<UnaryOperator>(E);
747330f729Sjoerg if (!U || U->getOpcode() != UO_AddrOf)
757330f729Sjoerg return true;
767330f729Sjoerg
777330f729Sjoerg // Don't warn for references
787330f729Sjoerg const ValueDecl *VD = nullptr;
797330f729Sjoerg if (const auto *SE = dyn_cast<DeclRefExpr>(U->getSubExpr()))
807330f729Sjoerg VD = SE->getDecl();
817330f729Sjoerg else if (const auto *SE = dyn_cast<MemberExpr>(U->getSubExpr()))
827330f729Sjoerg VD = SE->getMemberDecl();
837330f729Sjoerg if (!VD || VD->getType()->isReferenceType())
847330f729Sjoerg return true;
857330f729Sjoerg
867330f729Sjoerg if (ToPointeeTy->isIncompleteType() ||
877330f729Sjoerg OrigPointeeTy->isIncompleteType())
887330f729Sjoerg return true;
897330f729Sjoerg
907330f729Sjoerg // Warn when there is widening cast.
917330f729Sjoerg unsigned ToWidth = Ctx.getTypeInfo(ToPointeeTy).Width;
927330f729Sjoerg unsigned OrigWidth = Ctx.getTypeInfo(OrigPointeeTy).Width;
937330f729Sjoerg if (ToWidth <= OrigWidth)
947330f729Sjoerg return true;
957330f729Sjoerg
967330f729Sjoerg PathDiagnosticLocation Loc(CE, BR.getSourceManager(), AC);
977330f729Sjoerg BR.EmitBasicReport(AC->getDecl(), Checker, "Widening cast to struct type",
987330f729Sjoerg categories::LogicError,
997330f729Sjoerg "Casting data to a larger structure type and accessing "
1007330f729Sjoerg "a field can lead to memory access errors or data "
1017330f729Sjoerg "corruption.",
1027330f729Sjoerg Loc, CE->getSourceRange());
1037330f729Sjoerg }
1047330f729Sjoerg
1057330f729Sjoerg return true;
1067330f729Sjoerg }
1077330f729Sjoerg
1087330f729Sjoerg namespace {
1097330f729Sjoerg class CastToStructChecker : public Checker<check::ASTCodeBody> {
1107330f729Sjoerg public:
checkASTCodeBody(const Decl * D,AnalysisManager & Mgr,BugReporter & BR) const1117330f729Sjoerg void checkASTCodeBody(const Decl *D, AnalysisManager &Mgr,
1127330f729Sjoerg BugReporter &BR) const {
1137330f729Sjoerg CastToStructVisitor Visitor(BR, this, Mgr.getAnalysisDeclContext(D));
1147330f729Sjoerg Visitor.TraverseDecl(const_cast<Decl *>(D));
1157330f729Sjoerg }
1167330f729Sjoerg };
1177330f729Sjoerg } // end anonymous namespace
1187330f729Sjoerg
registerCastToStructChecker(CheckerManager & mgr)1197330f729Sjoerg void ento::registerCastToStructChecker(CheckerManager &mgr) {
1207330f729Sjoerg mgr.registerChecker<CastToStructChecker>();
1217330f729Sjoerg }
1227330f729Sjoerg
shouldRegisterCastToStructChecker(const CheckerManager & mgr)123*e038c9c4Sjoerg bool ento::shouldRegisterCastToStructChecker(const CheckerManager &mgr) {
1247330f729Sjoerg return true;
1257330f729Sjoerg }
126