MmapWriteExecChecker.cpp 3.2 KB

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