17330f729Sjoerg //===- OSObjectCStyleCast.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 file defines OSObjectCStyleCast checker, which checks for C-style casts
107330f729Sjoerg // of OSObjects. Such casts almost always indicate a code smell,
117330f729Sjoerg // as an explicit static or dynamic cast should be used instead.
127330f729Sjoerg //===----------------------------------------------------------------------===//
137330f729Sjoerg
147330f729Sjoerg #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
157330f729Sjoerg #include "clang/ASTMatchers/ASTMatchFinder.h"
167330f729Sjoerg #include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h"
177330f729Sjoerg #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
187330f729Sjoerg #include "clang/StaticAnalyzer/Core/Checker.h"
197330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
207330f729Sjoerg #include "llvm/Support/Debug.h"
217330f729Sjoerg
227330f729Sjoerg using namespace clang;
237330f729Sjoerg using namespace ento;
247330f729Sjoerg using namespace ast_matchers;
257330f729Sjoerg
267330f729Sjoerg namespace {
27*e038c9c4Sjoerg static constexpr const char *const WarnAtNode = "WarnAtNode";
28*e038c9c4Sjoerg static constexpr const char *const WarnRecordDecl = "WarnRecordDecl";
297330f729Sjoerg
307330f729Sjoerg class OSObjectCStyleCastChecker : public Checker<check::ASTCodeBody> {
317330f729Sjoerg public:
32*e038c9c4Sjoerg void checkASTCodeBody(const Decl *D, AnalysisManager &AM,
337330f729Sjoerg BugReporter &BR) const;
347330f729Sjoerg };
35*e038c9c4Sjoerg } // namespace
36*e038c9c4Sjoerg
37*e038c9c4Sjoerg namespace clang {
38*e038c9c4Sjoerg namespace ast_matchers {
AST_MATCHER_P(StringLiteral,mentionsBoundType,std::string,BindingID)39*e038c9c4Sjoerg AST_MATCHER_P(StringLiteral, mentionsBoundType, std::string, BindingID) {
40*e038c9c4Sjoerg return Builder->removeBindings([this, &Node](const BoundNodesMap &Nodes) {
41*e038c9c4Sjoerg const auto &BN = Nodes.getNode(this->BindingID);
42*e038c9c4Sjoerg if (const auto *ND = BN.get<NamedDecl>()) {
43*e038c9c4Sjoerg return ND->getName() != Node.getString();
44*e038c9c4Sjoerg }
45*e038c9c4Sjoerg return true;
46*e038c9c4Sjoerg });
47*e038c9c4Sjoerg }
48*e038c9c4Sjoerg } // end namespace ast_matchers
49*e038c9c4Sjoerg } // end namespace clang
507330f729Sjoerg
emitDiagnostics(const BoundNodes & Nodes,BugReporter & BR,AnalysisDeclContext * ADC,const OSObjectCStyleCastChecker * Checker)517330f729Sjoerg static void emitDiagnostics(const BoundNodes &Nodes,
527330f729Sjoerg BugReporter &BR,
537330f729Sjoerg AnalysisDeclContext *ADC,
547330f729Sjoerg const OSObjectCStyleCastChecker *Checker) {
557330f729Sjoerg const auto *CE = Nodes.getNodeAs<CastExpr>(WarnAtNode);
56*e038c9c4Sjoerg const CXXRecordDecl *RD = Nodes.getNodeAs<CXXRecordDecl>(WarnRecordDecl);
57*e038c9c4Sjoerg assert(CE && RD);
587330f729Sjoerg
597330f729Sjoerg std::string Diagnostics;
607330f729Sjoerg llvm::raw_string_ostream OS(Diagnostics);
61*e038c9c4Sjoerg OS << "C-style cast of an OSObject is prone to type confusion attacks; "
62*e038c9c4Sjoerg << "use 'OSRequiredCast' if the object is definitely of type '"
63*e038c9c4Sjoerg << RD->getNameAsString() << "', or 'OSDynamicCast' followed by "
64*e038c9c4Sjoerg << "a null check if unsure",
657330f729Sjoerg
667330f729Sjoerg BR.EmitBasicReport(
677330f729Sjoerg ADC->getDecl(),
687330f729Sjoerg Checker,
697330f729Sjoerg /*Name=*/"OSObject C-Style Cast",
70*e038c9c4Sjoerg categories::SecurityError,
717330f729Sjoerg OS.str(),
727330f729Sjoerg PathDiagnosticLocation::createBegin(CE, BR.getSourceManager(), ADC),
737330f729Sjoerg CE->getSourceRange());
747330f729Sjoerg }
757330f729Sjoerg
hasTypePointingTo(DeclarationMatcher DeclM)76*e038c9c4Sjoerg static decltype(auto) hasTypePointingTo(DeclarationMatcher DeclM) {
777330f729Sjoerg return hasType(pointerType(pointee(hasDeclaration(DeclM))));
787330f729Sjoerg }
797330f729Sjoerg
checkASTCodeBody(const Decl * D,AnalysisManager & AM,BugReporter & BR) const80*e038c9c4Sjoerg void OSObjectCStyleCastChecker::checkASTCodeBody(const Decl *D,
81*e038c9c4Sjoerg AnalysisManager &AM,
827330f729Sjoerg BugReporter &BR) const {
837330f729Sjoerg
847330f729Sjoerg AnalysisDeclContext *ADC = AM.getAnalysisDeclContext(D);
857330f729Sjoerg
867330f729Sjoerg auto DynamicCastM = callExpr(callee(functionDecl(hasName("safeMetaCast"))));
87*e038c9c4Sjoerg // 'allocClassWithName' allocates an object with the given type.
88*e038c9c4Sjoerg // The type is actually provided as a string argument (type's name).
89*e038c9c4Sjoerg // This makes the following pattern possible:
90*e038c9c4Sjoerg //
91*e038c9c4Sjoerg // Foo *object = (Foo *)allocClassWithName("Foo");
92*e038c9c4Sjoerg //
93*e038c9c4Sjoerg // While OSRequiredCast can be used here, it is still not a useful warning.
94*e038c9c4Sjoerg auto AllocClassWithNameM = callExpr(
95*e038c9c4Sjoerg callee(functionDecl(hasName("allocClassWithName"))),
96*e038c9c4Sjoerg // Here we want to make sure that the string argument matches the
97*e038c9c4Sjoerg // type in the cast expression.
98*e038c9c4Sjoerg hasArgument(0, stringLiteral(mentionsBoundType(WarnRecordDecl))));
997330f729Sjoerg
100*e038c9c4Sjoerg auto OSObjTypeM =
101*e038c9c4Sjoerg hasTypePointingTo(cxxRecordDecl(isDerivedFrom("OSMetaClassBase")));
1027330f729Sjoerg auto OSObjSubclassM = hasTypePointingTo(
103*e038c9c4Sjoerg cxxRecordDecl(isDerivedFrom("OSObject")).bind(WarnRecordDecl));
1047330f729Sjoerg
105*e038c9c4Sjoerg auto CastM =
106*e038c9c4Sjoerg cStyleCastExpr(
107*e038c9c4Sjoerg allOf(OSObjSubclassM,
108*e038c9c4Sjoerg hasSourceExpression(
109*e038c9c4Sjoerg allOf(OSObjTypeM,
110*e038c9c4Sjoerg unless(anyOf(DynamicCastM, AllocClassWithNameM))))))
111*e038c9c4Sjoerg .bind(WarnAtNode);
1127330f729Sjoerg
113*e038c9c4Sjoerg auto Matches =
114*e038c9c4Sjoerg match(stmt(forEachDescendant(CastM)), *D->getBody(), AM.getASTContext());
1157330f729Sjoerg for (BoundNodes Match : Matches)
1167330f729Sjoerg emitDiagnostics(Match, BR, ADC, this);
1177330f729Sjoerg }
1187330f729Sjoerg
registerOSObjectCStyleCast(CheckerManager & Mgr)1197330f729Sjoerg void ento::registerOSObjectCStyleCast(CheckerManager &Mgr) {
1207330f729Sjoerg Mgr.registerChecker<OSObjectCStyleCastChecker>();
1217330f729Sjoerg }
1227330f729Sjoerg
shouldRegisterOSObjectCStyleCast(const CheckerManager & mgr)123*e038c9c4Sjoerg bool ento::shouldRegisterOSObjectCStyleCast(const CheckerManager &mgr) {
1247330f729Sjoerg return true;
1257330f729Sjoerg }
126