1*0b57cec5SDimitry Andric //== MIGChecker.cpp - MIG calling convention checker ------------*- C++ -*--==// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric // 9*0b57cec5SDimitry Andric // This file defines MIGChecker, a Mach Interface Generator calling convention 10*0b57cec5SDimitry Andric // checker. Namely, in MIG callback implementation the following rules apply: 11*0b57cec5SDimitry Andric // - When a server routine returns an error code that represents success, it 12*0b57cec5SDimitry Andric // must take ownership of resources passed to it (and eventually release 13*0b57cec5SDimitry Andric // them). 14*0b57cec5SDimitry Andric // - Additionally, when returning success, all out-parameters must be 15*0b57cec5SDimitry Andric // initialized. 16*0b57cec5SDimitry Andric // - When it returns any other error code, it must not take ownership, 17*0b57cec5SDimitry Andric // because the message and its out-of-line parameters will be destroyed 18*0b57cec5SDimitry Andric // by the client that called the function. 19*0b57cec5SDimitry Andric // For now we only check the last rule, as its violations lead to dangerous 20*0b57cec5SDimitry Andric // use-after-free exploits. 21*0b57cec5SDimitry Andric // 22*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 23*0b57cec5SDimitry Andric 24*0b57cec5SDimitry Andric #include "clang/Analysis/AnyCall.h" 25*0b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" 26*0b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" 27*0b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/Checker.h" 28*0b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/CheckerManager.h" 29*0b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h" 30*0b57cec5SDimitry Andric #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" 31*0b57cec5SDimitry Andric 32*0b57cec5SDimitry Andric using namespace clang; 33*0b57cec5SDimitry Andric using namespace ento; 34*0b57cec5SDimitry Andric 35*0b57cec5SDimitry Andric namespace { 36*0b57cec5SDimitry Andric class MIGChecker : public Checker<check::PostCall, check::PreStmt<ReturnStmt>, 37*0b57cec5SDimitry Andric check::EndFunction> { 38*0b57cec5SDimitry Andric BugType BT{this, "Use-after-free (MIG calling convention violation)", 39*0b57cec5SDimitry Andric categories::MemoryError}; 40*0b57cec5SDimitry Andric 41*0b57cec5SDimitry Andric // The checker knows that an out-of-line object is deallocated if it is 42*0b57cec5SDimitry Andric // passed as an argument to one of these functions. If this object is 43*0b57cec5SDimitry Andric // additionally an argument of a MIG routine, the checker keeps track of that 44*0b57cec5SDimitry Andric // information and issues a warning when an error is returned from the 45*0b57cec5SDimitry Andric // respective routine. 46*0b57cec5SDimitry Andric std::vector<std::pair<CallDescription, unsigned>> Deallocators = { 47*0b57cec5SDimitry Andric #define CALL(required_args, deallocated_arg, ...) \ 48*0b57cec5SDimitry Andric {{{__VA_ARGS__}, required_args}, deallocated_arg} 49*0b57cec5SDimitry Andric // E.g., if the checker sees a C function 'vm_deallocate' that is 50*0b57cec5SDimitry Andric // defined on class 'IOUserClient' that has exactly 3 parameters, it knows 51*0b57cec5SDimitry Andric // that argument #1 (starting from 0, i.e. the second argument) is going 52*0b57cec5SDimitry Andric // to be consumed in the sense of the MIG consume-on-success convention. 53*0b57cec5SDimitry Andric CALL(3, 1, "vm_deallocate"), 54*0b57cec5SDimitry Andric CALL(3, 1, "mach_vm_deallocate"), 55*0b57cec5SDimitry Andric CALL(2, 0, "mig_deallocate"), 56*0b57cec5SDimitry Andric CALL(2, 1, "mach_port_deallocate"), 57*0b57cec5SDimitry Andric CALL(1, 0, "device_deallocate"), 58*0b57cec5SDimitry Andric CALL(1, 0, "iokit_remove_connect_reference"), 59*0b57cec5SDimitry Andric CALL(1, 0, "iokit_remove_reference"), 60*0b57cec5SDimitry Andric CALL(1, 0, "iokit_release_port"), 61*0b57cec5SDimitry Andric CALL(1, 0, "ipc_port_release"), 62*0b57cec5SDimitry Andric CALL(1, 0, "ipc_port_release_sonce"), 63*0b57cec5SDimitry Andric CALL(1, 0, "ipc_voucher_attr_control_release"), 64*0b57cec5SDimitry Andric CALL(1, 0, "ipc_voucher_release"), 65*0b57cec5SDimitry Andric CALL(1, 0, "lock_set_dereference"), 66*0b57cec5SDimitry Andric CALL(1, 0, "memory_object_control_deallocate"), 67*0b57cec5SDimitry Andric CALL(1, 0, "pset_deallocate"), 68*0b57cec5SDimitry Andric CALL(1, 0, "semaphore_dereference"), 69*0b57cec5SDimitry Andric CALL(1, 0, "space_deallocate"), 70*0b57cec5SDimitry Andric CALL(1, 0, "space_inspect_deallocate"), 71*0b57cec5SDimitry Andric CALL(1, 0, "task_deallocate"), 72*0b57cec5SDimitry Andric CALL(1, 0, "task_inspect_deallocate"), 73*0b57cec5SDimitry Andric CALL(1, 0, "task_name_deallocate"), 74*0b57cec5SDimitry Andric CALL(1, 0, "thread_deallocate"), 75*0b57cec5SDimitry Andric CALL(1, 0, "thread_inspect_deallocate"), 76*0b57cec5SDimitry Andric CALL(1, 0, "upl_deallocate"), 77*0b57cec5SDimitry Andric CALL(1, 0, "vm_map_deallocate"), 78*0b57cec5SDimitry Andric // E.g., if the checker sees a method 'releaseAsyncReference64()' that is 79*0b57cec5SDimitry Andric // defined on class 'IOUserClient' that takes exactly 1 argument, it knows 80*0b57cec5SDimitry Andric // that the argument is going to be consumed in the sense of the MIG 81*0b57cec5SDimitry Andric // consume-on-success convention. 82*0b57cec5SDimitry Andric CALL(1, 0, "IOUserClient", "releaseAsyncReference64"), 83*0b57cec5SDimitry Andric CALL(1, 0, "IOUserClient", "releaseNotificationPort"), 84*0b57cec5SDimitry Andric #undef CALL 85*0b57cec5SDimitry Andric }; 86*0b57cec5SDimitry Andric 87*0b57cec5SDimitry Andric CallDescription OsRefRetain{"os_ref_retain", 1}; 88*0b57cec5SDimitry Andric 89*0b57cec5SDimitry Andric void checkReturnAux(const ReturnStmt *RS, CheckerContext &C) const; 90*0b57cec5SDimitry Andric 91*0b57cec5SDimitry Andric public: 92*0b57cec5SDimitry Andric void checkPostCall(const CallEvent &Call, CheckerContext &C) const; 93*0b57cec5SDimitry Andric 94*0b57cec5SDimitry Andric // HACK: We're making two attempts to find the bug: checkEndFunction 95*0b57cec5SDimitry Andric // should normally be enough but it fails when the return value is a literal 96*0b57cec5SDimitry Andric // that never gets put into the Environment and ends of function with multiple 97*0b57cec5SDimitry Andric // returns get agglutinated across returns, preventing us from obtaining 98*0b57cec5SDimitry Andric // the return value. The problem is similar to https://reviews.llvm.org/D25326 99*0b57cec5SDimitry Andric // but now we step into it in the top-level function. 100*0b57cec5SDimitry Andric void checkPreStmt(const ReturnStmt *RS, CheckerContext &C) const { 101*0b57cec5SDimitry Andric checkReturnAux(RS, C); 102*0b57cec5SDimitry Andric } 103*0b57cec5SDimitry Andric void checkEndFunction(const ReturnStmt *RS, CheckerContext &C) const { 104*0b57cec5SDimitry Andric checkReturnAux(RS, C); 105*0b57cec5SDimitry Andric } 106*0b57cec5SDimitry Andric 107*0b57cec5SDimitry Andric }; 108*0b57cec5SDimitry Andric } // end anonymous namespace 109*0b57cec5SDimitry Andric 110*0b57cec5SDimitry Andric // A flag that says that the programmer has called a MIG destructor 111*0b57cec5SDimitry Andric // for at least one parameter. 112*0b57cec5SDimitry Andric REGISTER_TRAIT_WITH_PROGRAMSTATE(ReleasedParameter, bool) 113*0b57cec5SDimitry Andric // A set of parameters for which the check is suppressed because 114*0b57cec5SDimitry Andric // reference counting is being performed. 115*0b57cec5SDimitry Andric REGISTER_SET_WITH_PROGRAMSTATE(RefCountedParameters, const ParmVarDecl *) 116*0b57cec5SDimitry Andric 117*0b57cec5SDimitry Andric static const ParmVarDecl *getOriginParam(SVal V, CheckerContext &C, 118*0b57cec5SDimitry Andric bool IncludeBaseRegions = false) { 119*0b57cec5SDimitry Andric // TODO: We should most likely always include base regions here. 120*0b57cec5SDimitry Andric SymbolRef Sym = V.getAsSymbol(IncludeBaseRegions); 121*0b57cec5SDimitry Andric if (!Sym) 122*0b57cec5SDimitry Andric return nullptr; 123*0b57cec5SDimitry Andric 124*0b57cec5SDimitry Andric // If we optimistically assume that the MIG routine never re-uses the storage 125*0b57cec5SDimitry Andric // that was passed to it as arguments when it invalidates it (but at most when 126*0b57cec5SDimitry Andric // it assigns to parameter variables directly), this procedure correctly 127*0b57cec5SDimitry Andric // determines if the value was loaded from the transitive closure of MIG 128*0b57cec5SDimitry Andric // routine arguments in the heap. 129*0b57cec5SDimitry Andric while (const MemRegion *MR = Sym->getOriginRegion()) { 130*0b57cec5SDimitry Andric const auto *VR = dyn_cast<VarRegion>(MR); 131*0b57cec5SDimitry Andric if (VR && VR->hasStackParametersStorage() && 132*0b57cec5SDimitry Andric VR->getStackFrame()->inTopFrame()) 133*0b57cec5SDimitry Andric return cast<ParmVarDecl>(VR->getDecl()); 134*0b57cec5SDimitry Andric 135*0b57cec5SDimitry Andric const SymbolicRegion *SR = MR->getSymbolicBase(); 136*0b57cec5SDimitry Andric if (!SR) 137*0b57cec5SDimitry Andric return nullptr; 138*0b57cec5SDimitry Andric 139*0b57cec5SDimitry Andric Sym = SR->getSymbol(); 140*0b57cec5SDimitry Andric } 141*0b57cec5SDimitry Andric 142*0b57cec5SDimitry Andric return nullptr; 143*0b57cec5SDimitry Andric } 144*0b57cec5SDimitry Andric 145*0b57cec5SDimitry Andric static bool isInMIGCall(CheckerContext &C) { 146*0b57cec5SDimitry Andric const LocationContext *LC = C.getLocationContext(); 147*0b57cec5SDimitry Andric assert(LC && "Unknown location context"); 148*0b57cec5SDimitry Andric 149*0b57cec5SDimitry Andric const StackFrameContext *SFC; 150*0b57cec5SDimitry Andric // Find the top frame. 151*0b57cec5SDimitry Andric while (LC) { 152*0b57cec5SDimitry Andric SFC = LC->getStackFrame(); 153*0b57cec5SDimitry Andric LC = SFC->getParent(); 154*0b57cec5SDimitry Andric } 155*0b57cec5SDimitry Andric 156*0b57cec5SDimitry Andric const Decl *D = SFC->getDecl(); 157*0b57cec5SDimitry Andric 158*0b57cec5SDimitry Andric if (Optional<AnyCall> AC = AnyCall::forDecl(D)) { 159*0b57cec5SDimitry Andric // Even though there's a Sema warning when the return type of an annotated 160*0b57cec5SDimitry Andric // function is not a kern_return_t, this warning isn't an error, so we need 161*0b57cec5SDimitry Andric // an extra sanity check here. 162*0b57cec5SDimitry Andric // FIXME: AnyCall doesn't support blocks yet, so they remain unchecked 163*0b57cec5SDimitry Andric // for now. 164*0b57cec5SDimitry Andric if (!AC->getReturnType(C.getASTContext()) 165*0b57cec5SDimitry Andric .getCanonicalType()->isSignedIntegerType()) 166*0b57cec5SDimitry Andric return false; 167*0b57cec5SDimitry Andric } 168*0b57cec5SDimitry Andric 169*0b57cec5SDimitry Andric if (D->hasAttr<MIGServerRoutineAttr>()) 170*0b57cec5SDimitry Andric return true; 171*0b57cec5SDimitry Andric 172*0b57cec5SDimitry Andric // See if there's an annotated method in the superclass. 173*0b57cec5SDimitry Andric if (const auto *MD = dyn_cast<CXXMethodDecl>(D)) 174*0b57cec5SDimitry Andric for (const auto *OMD: MD->overridden_methods()) 175*0b57cec5SDimitry Andric if (OMD->hasAttr<MIGServerRoutineAttr>()) 176*0b57cec5SDimitry Andric return true; 177*0b57cec5SDimitry Andric 178*0b57cec5SDimitry Andric return false; 179*0b57cec5SDimitry Andric } 180*0b57cec5SDimitry Andric 181*0b57cec5SDimitry Andric void MIGChecker::checkPostCall(const CallEvent &Call, CheckerContext &C) const { 182*0b57cec5SDimitry Andric if (Call.isCalled(OsRefRetain)) { 183*0b57cec5SDimitry Andric // If the code is doing reference counting over the parameter, 184*0b57cec5SDimitry Andric // it opens up an opportunity for safely calling a destructor function. 185*0b57cec5SDimitry Andric // TODO: We should still check for over-releases. 186*0b57cec5SDimitry Andric if (const ParmVarDecl *PVD = 187*0b57cec5SDimitry Andric getOriginParam(Call.getArgSVal(0), C, /*IncludeBaseRegions=*/true)) { 188*0b57cec5SDimitry Andric // We never need to clean up the program state because these are 189*0b57cec5SDimitry Andric // top-level parameters anyway, so they're always live. 190*0b57cec5SDimitry Andric C.addTransition(C.getState()->add<RefCountedParameters>(PVD)); 191*0b57cec5SDimitry Andric } 192*0b57cec5SDimitry Andric return; 193*0b57cec5SDimitry Andric } 194*0b57cec5SDimitry Andric 195*0b57cec5SDimitry Andric if (!isInMIGCall(C)) 196*0b57cec5SDimitry Andric return; 197*0b57cec5SDimitry Andric 198*0b57cec5SDimitry Andric auto I = llvm::find_if(Deallocators, 199*0b57cec5SDimitry Andric [&](const std::pair<CallDescription, unsigned> &Item) { 200*0b57cec5SDimitry Andric return Call.isCalled(Item.first); 201*0b57cec5SDimitry Andric }); 202*0b57cec5SDimitry Andric if (I == Deallocators.end()) 203*0b57cec5SDimitry Andric return; 204*0b57cec5SDimitry Andric 205*0b57cec5SDimitry Andric ProgramStateRef State = C.getState(); 206*0b57cec5SDimitry Andric unsigned ArgIdx = I->second; 207*0b57cec5SDimitry Andric SVal Arg = Call.getArgSVal(ArgIdx); 208*0b57cec5SDimitry Andric const ParmVarDecl *PVD = getOriginParam(Arg, C); 209*0b57cec5SDimitry Andric if (!PVD || State->contains<RefCountedParameters>(PVD)) 210*0b57cec5SDimitry Andric return; 211*0b57cec5SDimitry Andric 212*0b57cec5SDimitry Andric const NoteTag *T = C.getNoteTag([this, PVD](BugReport &BR) -> std::string { 213*0b57cec5SDimitry Andric if (&BR.getBugType() != &BT) 214*0b57cec5SDimitry Andric return ""; 215*0b57cec5SDimitry Andric SmallString<64> Str; 216*0b57cec5SDimitry Andric llvm::raw_svector_ostream OS(Str); 217*0b57cec5SDimitry Andric OS << "Value passed through parameter '" << PVD->getName() 218*0b57cec5SDimitry Andric << "\' is deallocated"; 219*0b57cec5SDimitry Andric return OS.str(); 220*0b57cec5SDimitry Andric }); 221*0b57cec5SDimitry Andric C.addTransition(State->set<ReleasedParameter>(true), T); 222*0b57cec5SDimitry Andric } 223*0b57cec5SDimitry Andric 224*0b57cec5SDimitry Andric // Returns true if V can potentially represent a "successful" kern_return_t. 225*0b57cec5SDimitry Andric static bool mayBeSuccess(SVal V, CheckerContext &C) { 226*0b57cec5SDimitry Andric ProgramStateRef State = C.getState(); 227*0b57cec5SDimitry Andric 228*0b57cec5SDimitry Andric // Can V represent KERN_SUCCESS? 229*0b57cec5SDimitry Andric if (!State->isNull(V).isConstrainedFalse()) 230*0b57cec5SDimitry Andric return true; 231*0b57cec5SDimitry Andric 232*0b57cec5SDimitry Andric SValBuilder &SVB = C.getSValBuilder(); 233*0b57cec5SDimitry Andric ASTContext &ACtx = C.getASTContext(); 234*0b57cec5SDimitry Andric 235*0b57cec5SDimitry Andric // Can V represent MIG_NO_REPLY? 236*0b57cec5SDimitry Andric static const int MigNoReply = -305; 237*0b57cec5SDimitry Andric V = SVB.evalEQ(C.getState(), V, SVB.makeIntVal(MigNoReply, ACtx.IntTy)); 238*0b57cec5SDimitry Andric if (!State->isNull(V).isConstrainedTrue()) 239*0b57cec5SDimitry Andric return true; 240*0b57cec5SDimitry Andric 241*0b57cec5SDimitry Andric // If none of the above, it's definitely an error. 242*0b57cec5SDimitry Andric return false; 243*0b57cec5SDimitry Andric } 244*0b57cec5SDimitry Andric 245*0b57cec5SDimitry Andric void MIGChecker::checkReturnAux(const ReturnStmt *RS, CheckerContext &C) const { 246*0b57cec5SDimitry Andric // It is very unlikely that a MIG callback will be called from anywhere 247*0b57cec5SDimitry Andric // within the project under analysis and the caller isn't itself a routine 248*0b57cec5SDimitry Andric // that follows the MIG calling convention. Therefore we're safe to believe 249*0b57cec5SDimitry Andric // that it's always the top frame that is of interest. There's a slight chance 250*0b57cec5SDimitry Andric // that the user would want to enforce the MIG calling convention upon 251*0b57cec5SDimitry Andric // a random routine in the middle of nowhere, but given that the convention is 252*0b57cec5SDimitry Andric // fairly weird and hard to follow in the first place, there's relatively 253*0b57cec5SDimitry Andric // little motivation to spread it this way. 254*0b57cec5SDimitry Andric if (!C.inTopFrame()) 255*0b57cec5SDimitry Andric return; 256*0b57cec5SDimitry Andric 257*0b57cec5SDimitry Andric if (!isInMIGCall(C)) 258*0b57cec5SDimitry Andric return; 259*0b57cec5SDimitry Andric 260*0b57cec5SDimitry Andric // We know that the function is non-void, but what if the return statement 261*0b57cec5SDimitry Andric // is not there in the code? It's not a compile error, we should not crash. 262*0b57cec5SDimitry Andric if (!RS) 263*0b57cec5SDimitry Andric return; 264*0b57cec5SDimitry Andric 265*0b57cec5SDimitry Andric ProgramStateRef State = C.getState(); 266*0b57cec5SDimitry Andric if (!State->get<ReleasedParameter>()) 267*0b57cec5SDimitry Andric return; 268*0b57cec5SDimitry Andric 269*0b57cec5SDimitry Andric SVal V = C.getSVal(RS); 270*0b57cec5SDimitry Andric if (mayBeSuccess(V, C)) 271*0b57cec5SDimitry Andric return; 272*0b57cec5SDimitry Andric 273*0b57cec5SDimitry Andric ExplodedNode *N = C.generateErrorNode(); 274*0b57cec5SDimitry Andric if (!N) 275*0b57cec5SDimitry Andric return; 276*0b57cec5SDimitry Andric 277*0b57cec5SDimitry Andric auto R = llvm::make_unique<BugReport>( 278*0b57cec5SDimitry Andric BT, 279*0b57cec5SDimitry Andric "MIG callback fails with error after deallocating argument value. " 280*0b57cec5SDimitry Andric "This is a use-after-free vulnerability because the caller will try to " 281*0b57cec5SDimitry Andric "deallocate it again", 282*0b57cec5SDimitry Andric N); 283*0b57cec5SDimitry Andric 284*0b57cec5SDimitry Andric R->addRange(RS->getSourceRange()); 285*0b57cec5SDimitry Andric bugreporter::trackExpressionValue(N, RS->getRetValue(), *R, false); 286*0b57cec5SDimitry Andric C.emitReport(std::move(R)); 287*0b57cec5SDimitry Andric } 288*0b57cec5SDimitry Andric 289*0b57cec5SDimitry Andric void ento::registerMIGChecker(CheckerManager &Mgr) { 290*0b57cec5SDimitry Andric Mgr.registerChecker<MIGChecker>(); 291*0b57cec5SDimitry Andric } 292*0b57cec5SDimitry Andric 293*0b57cec5SDimitry Andric bool ento::shouldRegisterMIGChecker(const LangOptions &LO) { 294*0b57cec5SDimitry Andric return true; 295*0b57cec5SDimitry Andric } 296