Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/StaticAnalyzer/Checkers/MmapWriteExecChecker.cpp
Line
Count
Source (jump to first uncovered line)
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
15
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
16
17
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
18
#include "clang/StaticAnalyzer/Core/Checker.h"
19
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
20
#include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
21
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
22
23
using namespace clang;
24
using namespace ento;
25
using llvm::APSInt;
26
27
namespace {
28
class MmapWriteExecChecker : public Checker<check::PreCall> {
29
  CallDescription MmapFn;
30
  CallDescription MprotectFn;
31
  static int ProtWrite;
32
  static int ProtExec;
33
  static int ProtRead;
34
  mutable std::unique_ptr<BugType> BT;
35
public:
36
3
  MmapWriteExecChecker() : MmapFn("mmap", 6), MprotectFn("mprotect", 3) {}
37
  void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
38
  int ProtExecOv;
39
  int ProtReadOv;
40
};
41
}
42
43
int MmapWriteExecChecker::ProtWrite = 0x02;
44
int MmapWriteExecChecker::ProtExec  = 0x04;
45
int MmapWriteExecChecker::ProtRead  = 0x01;
46
47
void MmapWriteExecChecker::checkPreCall(const CallEvent &Call,
48
15
                                         CheckerContext &C) const {
49
15
  if (Call.isCalled(MmapFn) || 
Call.isCalled(MprotectFn)5
) {
50
12
    SVal ProtVal = Call.getArgSVal(2);
51
12
    Optional<nonloc::ConcreteInt> ProtLoc = ProtVal.getAs<nonloc::ConcreteInt>();
52
12
    int64_t Prot = ProtLoc->getValue().getSExtValue();
53
12
    if (ProtExecOv != ProtExec)
54
1
      ProtExec = ProtExecOv;
55
12
    if (ProtReadOv != ProtRead)
56
1
      ProtRead = ProtReadOv;
57
12
58
12
    // Wrong settings
59
12
    if (ProtRead == ProtExec)
60
0
      return;
61
12
62
12
    if ((Prot & (ProtWrite | ProtExec)) == (ProtWrite | ProtExec)) {
63
6
      if (!BT)
64
2
        BT.reset(new BugType(this, "W^X check fails, Write Exec prot flags set", "Security"));
65
6
66
6
      ExplodedNode *N = C.generateNonFatalErrorNode();
67
6
      if (!N)
68
0
        return;
69
6
70
6
      auto Report = llvm::make_unique<BugReport>(
71
6
          *BT, "Both PROT_WRITE and PROT_EXEC flags are set. This can "
72
6
               "lead to exploitable memory regions, which could be overwritten "
73
6
               "with malicious code", N);
74
6
      Report->addRange(Call.getArgSourceRange(2));
75
6
      C.emitReport(std::move(Report));
76
6
    }
77
12
  }
78
15
}
79
80
3
void ento::registerMmapWriteExecChecker(CheckerManager &mgr) {
81
3
  MmapWriteExecChecker *Mwec =
82
3
      mgr.registerChecker<MmapWriteExecChecker>();
83
3
  Mwec->ProtExecOv =
84
3
    mgr.getAnalyzerOptions()
85
3
      .getCheckerIntegerOption(Mwec, "MmapProtExec");
86
3
  Mwec->ProtReadOv =
87
3
    mgr.getAnalyzerOptions()
88
3
      .getCheckerIntegerOption(Mwec, "MmapProtRead");
89
3
}
90
91
3
bool ento::shouldRegisterMmapWriteExecChecker(const LangOptions &LO) {
92
3
  return true;
93
3
}