MmapWriteExecChecker.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // MmapWriteExecChecker.cpp - Check for the prot argument -----------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This checker tests the 3rd argument of mmap's calls to check if
  11. // it is writable and executable in the same time. It's somehow
  12. // an optional checker since for example in JIT libraries it is pretty common.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #include "ClangSACheckers.h"
  16. #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
  17. #include "clang/StaticAnalyzer/Core/Checker.h"
  18. #include "clang/StaticAnalyzer/Core/CheckerManager.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. using llvm::APSInt;
  24. namespace {
  25. class MmapWriteExecChecker : public Checker<check::PreCall> {
  26. CallDescription MmapFn;
  27. CallDescription MprotectFn;
  28. static int ProtWrite;
  29. static int ProtExec;
  30. static int ProtRead;
  31. mutable std::unique_ptr<BugType> BT;
  32. public:
  33. MmapWriteExecChecker() : MmapFn("mmap", 6), MprotectFn("mprotect", 3) {}
  34. void checkPreCall(const CallEvent &Call, CheckerContext &C) const;
  35. int ProtExecOv;
  36. int ProtReadOv;
  37. };
  38. }
  39. int MmapWriteExecChecker::ProtWrite = 0x02;
  40. int MmapWriteExecChecker::ProtExec = 0x04;
  41. int MmapWriteExecChecker::ProtRead = 0x01;
  42. void MmapWriteExecChecker::checkPreCall(const CallEvent &Call,
  43. CheckerContext &C) const {
  44. if (Call.isCalled(MmapFn) || Call.isCalled(MprotectFn)) {
  45. SVal ProtVal = Call.getArgSVal(2);
  46. Optional<nonloc::ConcreteInt> ProtLoc = ProtVal.getAs<nonloc::ConcreteInt>();
  47. int64_t Prot = ProtLoc->getValue().getSExtValue();
  48. if (ProtExecOv != ProtExec)
  49. ProtExec = ProtExecOv;
  50. if (ProtReadOv != ProtRead)
  51. ProtRead = ProtReadOv;
  52. // Wrong settings
  53. if (ProtRead == ProtExec)
  54. return;
  55. if ((Prot & (ProtWrite | ProtExec)) == (ProtWrite | ProtExec)) {
  56. if (!BT)
  57. BT.reset(new BugType(this, "W^X check fails, Write Exec prot flags set", "Security"));
  58. ExplodedNode *N = C.generateNonFatalErrorNode();
  59. if (!N)
  60. return;
  61. auto Report = llvm::make_unique<BugReport>(
  62. *BT, "Both PROT_WRITE and PROT_EXEC flags are set. This can "
  63. "lead to exploitable memory regions, which could be overwritten "
  64. "with malicious code", N);
  65. Report->addRange(Call.getArgSourceRange(2));
  66. C.emitReport(std::move(Report));
  67. }
  68. }
  69. }
  70. void ento::registerMmapWriteExecChecker(CheckerManager &mgr) {
  71. MmapWriteExecChecker *Mwec =
  72. mgr.registerChecker<MmapWriteExecChecker>();
  73. Mwec->ProtExecOv =
  74. mgr.getAnalyzerOptions()
  75. .getCheckerIntegerOption("MmapProtExec", 0x04, Mwec);
  76. Mwec->ProtReadOv =
  77. mgr.getAnalyzerOptions()
  78. .getCheckerIntegerOption("MmapProtRead", 0x01, Mwec);
  79. }