17330f729Sjoerg // MmapWriteExecChecker.cpp - Check for the prot argument -----------------===//
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 checker tests the 3rd argument of mmap's calls to check if
107330f729Sjoerg // it is writable and executable in the same time. It's somehow
117330f729Sjoerg // an optional checker since for example in JIT libraries it is pretty common.
127330f729Sjoerg //
137330f729Sjoerg //===----------------------------------------------------------------------===//
147330f729Sjoerg
157330f729Sjoerg #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
167330f729Sjoerg
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/CallEvent.h"
217330f729Sjoerg #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
227330f729Sjoerg
237330f729Sjoerg using namespace clang;
247330f729Sjoerg using namespace ento;
257330f729Sjoerg using llvm::APSInt;
267330f729Sjoerg
277330f729Sjoerg namespace {
287330f729Sjoerg class MmapWriteExecChecker : public Checker<check::PreCall> {
297330f729Sjoerg CallDescription MmapFn;
307330f729Sjoerg CallDescription MprotectFn;
317330f729Sjoerg static int ProtWrite;
327330f729Sjoerg static int ProtExec;
337330f729Sjoerg static int ProtRead;
347330f729Sjoerg mutable std::unique_ptr<BugType> BT;
357330f729Sjoerg public:
MmapWriteExecChecker()367330f729Sjoerg MmapWriteExecChecker() : MmapFn("mmap", 6), MprotectFn("mprotect", 3) {}
377330f729Sjoerg void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
387330f729Sjoerg int ProtExecOv;
397330f729Sjoerg int ProtReadOv;
407330f729Sjoerg };
417330f729Sjoerg }
427330f729Sjoerg
437330f729Sjoerg int MmapWriteExecChecker::ProtWrite = 0x02;
447330f729Sjoerg int MmapWriteExecChecker::ProtExec = 0x04;
457330f729Sjoerg int MmapWriteExecChecker::ProtRead = 0x01;
467330f729Sjoerg
checkPreCall(const CallEvent & Call,CheckerContext & C) const477330f729Sjoerg void MmapWriteExecChecker::checkPreCall(const CallEvent &Call,
487330f729Sjoerg CheckerContext &C) const {
497330f729Sjoerg if (Call.isCalled(MmapFn) || Call.isCalled(MprotectFn)) {
507330f729Sjoerg SVal ProtVal = Call.getArgSVal(2);
517330f729Sjoerg Optional<nonloc::ConcreteInt> ProtLoc = ProtVal.getAs<nonloc::ConcreteInt>();
527330f729Sjoerg int64_t Prot = ProtLoc->getValue().getSExtValue();
537330f729Sjoerg if (ProtExecOv != ProtExec)
547330f729Sjoerg ProtExec = ProtExecOv;
557330f729Sjoerg if (ProtReadOv != ProtRead)
567330f729Sjoerg ProtRead = ProtReadOv;
577330f729Sjoerg
587330f729Sjoerg // Wrong settings
597330f729Sjoerg if (ProtRead == ProtExec)
607330f729Sjoerg return;
617330f729Sjoerg
627330f729Sjoerg if ((Prot & (ProtWrite | ProtExec)) == (ProtWrite | ProtExec)) {
637330f729Sjoerg if (!BT)
647330f729Sjoerg BT.reset(new BugType(this, "W^X check fails, Write Exec prot flags set", "Security"));
657330f729Sjoerg
667330f729Sjoerg ExplodedNode *N = C.generateNonFatalErrorNode();
677330f729Sjoerg if (!N)
687330f729Sjoerg return;
697330f729Sjoerg
707330f729Sjoerg auto Report = std::make_unique<PathSensitiveBugReport>(
717330f729Sjoerg *BT, "Both PROT_WRITE and PROT_EXEC flags are set. This can "
727330f729Sjoerg "lead to exploitable memory regions, which could be overwritten "
737330f729Sjoerg "with malicious code", N);
747330f729Sjoerg Report->addRange(Call.getArgSourceRange(2));
757330f729Sjoerg C.emitReport(std::move(Report));
767330f729Sjoerg }
777330f729Sjoerg }
787330f729Sjoerg }
797330f729Sjoerg
registerMmapWriteExecChecker(CheckerManager & mgr)807330f729Sjoerg void ento::registerMmapWriteExecChecker(CheckerManager &mgr) {
817330f729Sjoerg MmapWriteExecChecker *Mwec =
827330f729Sjoerg mgr.registerChecker<MmapWriteExecChecker>();
837330f729Sjoerg Mwec->ProtExecOv =
847330f729Sjoerg mgr.getAnalyzerOptions()
857330f729Sjoerg .getCheckerIntegerOption(Mwec, "MmapProtExec");
867330f729Sjoerg Mwec->ProtReadOv =
877330f729Sjoerg mgr.getAnalyzerOptions()
887330f729Sjoerg .getCheckerIntegerOption(Mwec, "MmapProtRead");
897330f729Sjoerg }
907330f729Sjoerg
shouldRegisterMmapWriteExecChecker(const CheckerManager & mgr)91*e038c9c4Sjoerg bool ento::shouldRegisterMmapWriteExecChecker(const CheckerManager &mgr) {
927330f729Sjoerg return true;
937330f729Sjoerg }
94