clang 20.0.0git
MmapWriteExecChecker.cpp
Go to the documentation of this file.
1// MmapWriteExecChecker.cpp - Check for the prot argument -----------------===//
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 checker tests the 3rd argument of mmap's calls to check if
10// it is writable and executable in the same time. It's somehow
11// an optional checker since for example in JIT libraries it is pretty common.
12//
13//===----------------------------------------------------------------------===//
14
16
25
26using namespace clang;
27using namespace ento;
28
29namespace {
30class MmapWriteExecChecker
31 : public Checker<check::ASTDecl<TranslationUnitDecl>, check::PreCall> {
32 CallDescription MmapFn{CDM::CLibrary, {"mmap"}, 6};
33 CallDescription MprotectFn{CDM::CLibrary, {"mprotect"}, 3};
34 const BugType BT{this, "W^X check fails, Write Exec prot flags set",
35 "Security"};
36
37 // Default values are used if definition of the flags is not found.
38 mutable int ProtRead = 0x01;
39 mutable int ProtWrite = 0x02;
40 mutable int ProtExec = 0x04;
41
42public:
43 void checkASTDecl(const TranslationUnitDecl *TU, AnalysisManager &Mgr,
44 BugReporter &BR) const;
45 void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
46};
47}
48
49void MmapWriteExecChecker::checkASTDecl(const TranslationUnitDecl *TU,
50 AnalysisManager &Mgr,
51 BugReporter &BR) const {
52 Preprocessor &PP = Mgr.getPreprocessor();
53 const std::optional<int> FoundProtRead = tryExpandAsInteger("PROT_READ", PP);
54 const std::optional<int> FoundProtWrite =
55 tryExpandAsInteger("PROT_WRITE", PP);
56 const std::optional<int> FoundProtExec = tryExpandAsInteger("PROT_EXEC", PP);
57 if (FoundProtRead && FoundProtWrite && FoundProtExec) {
58 ProtRead = *FoundProtRead;
59 ProtWrite = *FoundProtWrite;
60 ProtExec = *FoundProtExec;
61 }
62}
63
64void MmapWriteExecChecker::checkPreCall(const CallEvent &Call,
65 CheckerContext &C) const {
66 if (matchesAny(Call, MmapFn, MprotectFn)) {
67 SVal ProtVal = Call.getArgSVal(2);
68 auto ProtLoc = ProtVal.getAs<nonloc::ConcreteInt>();
69 if (!ProtLoc)
70 return;
71 int64_t Prot = ProtLoc->getValue().getSExtValue();
72
73 if ((Prot & ProtWrite) && (Prot & ProtExec)) {
74 ExplodedNode *N = C.generateNonFatalErrorNode();
75 if (!N)
76 return;
77
78 auto Report = std::make_unique<PathSensitiveBugReport>(
79 BT,
80 "Both PROT_WRITE and PROT_EXEC flags are set. This can "
81 "lead to exploitable memory regions, which could be overwritten "
82 "with malicious code",
83 N);
84 Report->addRange(Call.getArgSourceRange(2));
85 C.emitReport(std::move(Report));
86 }
87 }
88}
89
90void ento::registerMmapWriteExecChecker(CheckerManager &Mgr) {
91 Mgr.registerChecker<MmapWriteExecChecker>();
92}
93
94bool ento::shouldRegisterMmapWriteExecChecker(const CheckerManager &) {
95 return true;
96}
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
Definition: Preprocessor.h:137
The top declaration context.
Definition: Decl.h:84
Preprocessor & getPreprocessor() override
BugReporter is a utility class for generating PathDiagnostics for analysis.
Definition: BugReporter.h:585
A CallDescription is a pattern that can be used to match calls based on the qualified name and the ar...
Represents an abstract call to a function or method along a particular path.
Definition: CallEvent.h:153
CHECKER * registerChecker(AT &&... Args)
Used to register checkers.
SVal - This represents a symbolic expression, which can be either an L-value or an R-value.
Definition: SVals.h:55
std::optional< T > getAs() const
Convert to the specified SVal type, returning std::nullopt if this SVal is not of the desired type.
Definition: SVals.h:86
Value representing integer constant.
Definition: SVals.h:297
std::optional< int > tryExpandAsInteger(StringRef Macro, const Preprocessor &PP)
Try to parse the value of a defined preprocessor macro.
The JSON file list parser is used to communicate input to InstallAPI.
long int64_t