Index: lib/sanitizer_common/sanitizer_common.h =================================================================== --- lib/sanitizer_common/sanitizer_common.h +++ lib/sanitizer_common/sanitizer_common.h @@ -378,7 +378,7 @@ void ReportErrorSummary(const char *error_type, const StackTrace *trace, const char *alt_tool_name = nullptr); -void ReportMmapWriteExec(); +void ReportMmapWriteExec(int prot); // Math #if SANITIZER_WINDOWS && !defined(__clang__) && !defined(__GNUC__) Index: lib/sanitizer_common/sanitizer_common_interceptors.inc =================================================================== --- lib/sanitizer_common/sanitizer_common_interceptors.inc +++ lib/sanitizer_common/sanitizer_common_interceptors.inc @@ -35,6 +35,7 @@ // COMMON_INTERCEPTOR_MEMMOVE_IMPL // COMMON_INTERCEPTOR_MEMCPY_IMPL // COMMON_INTERCEPTOR_MMAP_IMPL +// COMMON_INTERCEPTOR_MPROTECT_IMPL // COMMON_INTERCEPTOR_COPY_STRING // COMMON_INTERCEPTOR_STRNDUP_IMPL //===----------------------------------------------------------------------===// @@ -274,6 +275,11 @@ { return REAL(mmap)(addr, sz, prot, flags, fd, off); } #endif +#ifndef COMMON_INTERCEPTOR_MPROTECT_IMPL +#define COMMON_INTERCEPTOR_MPROTECT_IMPL(ctx, mprotect, addr, sz, prot) \ + { return REAL(mprotect)(addr, sz, prot); } +#endif + #ifndef COMMON_INTERCEPTOR_COPY_STRING #define COMMON_INTERCEPTOR_COPY_STRING(ctx, to, from, size) {} #endif @@ -6888,13 +6894,25 @@ OFF_T off) { void *ctx; if (common_flags()->detect_write_exec) - ReportMmapWriteExec(); + ReportMmapWriteExec(prot); if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) return (void *)internal_mmap(addr, sz, prot, flags, fd, off); COMMON_INTERCEPTOR_ENTER(ctx, mmap, addr, sz, prot, flags, fd, off); COMMON_INTERCEPTOR_MMAP_IMPL(ctx, mmap, addr, sz, prot, flags, fd, off); } -#define INIT_MMAP COMMON_INTERCEPT_FUNCTION(mmap); + +INTERCEPTOR(int, mprotect, void *addr, SIZE_T sz, int prot) { + void *ctx; + if (common_flags()->detect_write_exec) + ReportMmapWriteExec(prot); + if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) + return (int)internal_mprotect(addr, sz, prot); + COMMON_INTERCEPTOR_ENTER(ctx, mprotect, addr, sz, prot); + COMMON_INTERCEPTOR_MPROTECT_IMPL(ctx, mprotect, addr, sz, prot); +} +#define INIT_MMAP \ + COMMON_INTERCEPT_FUNCTION(mmap); \ + COMMON_INTERCEPT_FUNCTION(mprotect); #else #define INIT_MMAP #endif @@ -6904,7 +6922,7 @@ OFF64_T off) { void *ctx; if (common_flags()->detect_write_exec) - ReportMmapWriteExec(); + ReportMmapWriteExec(prot); if (COMMON_INTERCEPTOR_NOTHING_IS_INITIALIZED) return (void *)internal_mmap(addr, sz, prot, flags, fd, off); COMMON_INTERCEPTOR_ENTER(ctx, mmap64, addr, sz, prot, flags, fd, off); Index: lib/sanitizer_common/sanitizer_common_libcdep.cc =================================================================== --- lib/sanitizer_common/sanitizer_common_libcdep.cc +++ lib/sanitizer_common/sanitizer_common_libcdep.cc @@ -24,6 +24,8 @@ #if SANITIZER_POSIX #include "sanitizer_posix.h" + +#include #endif namespace __sanitizer { @@ -81,8 +83,11 @@ #endif } -void ReportMmapWriteExec() { +void ReportMmapWriteExec(int prot) { #if !SANITIZER_GO && !SANITIZER_ANDROID + if ((prot & (PROT_WRITE | PROT_EXEC)) != (PROT_WRITE | PROT_EXEC)) + return; + ScopedErrorReportLock l; SanitizerCommonDecorator d; Index: test/sanitizer_common/TestCases/Linux/mmap_write_exec.cpp =================================================================== --- test/sanitizer_common/TestCases/Linux/mmap_write_exec.cpp +++ test/sanitizer_common/TestCases/Linux/mmap_write_exec.cpp @@ -12,4 +12,8 @@ char *p = (char *)mmap(0, 1024, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); // CHECK: WARNING: {{.*}}Sanitizer: writable-executable page usage + char *q = (char *)mmap(p, 64, PROT_READ | PROT_WRITE, + MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0); + (void)mprotect(q, 64, PROT_WRITE | PROT_EXEC); + // CHECK: WARNING: {{.*}}Sanitizer: writable-executable page usage }