CastToStructChecker.cpp 4.4 KB

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