From 039a28468a1812b8f0662aba62c173e572899841 Mon Sep 17 00:00:00 2001 From: Janne Koskinen Date: Fri, 28 Sep 2018 12:15:58 +0200 Subject: Fix Integrity OS allocator memory attributes Correctly set the attributes when allocation is extending to more than one page. Code spanning multiple pages can now be executed. Task-number: QTBUG-70350 Change-Id: I02af1add274f80befda5662d9670bfd2052c3c52 Reviewed-by: Lars Knoll --- src/3rdparty/masm/stubs/ExecutableAllocator.h | 4 ++-- src/3rdparty/masm/wtf/OSAllocator.h | 2 +- src/3rdparty/masm/wtf/OSAllocatorIntegrity.cpp | 12 ++++++++---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/3rdparty/masm/stubs/ExecutableAllocator.h b/src/3rdparty/masm/stubs/ExecutableAllocator.h index 1ab28588fb..16b17bd3cd 100644 --- a/src/3rdparty/masm/stubs/ExecutableAllocator.h +++ b/src/3rdparty/masm/stubs/ExecutableAllocator.h @@ -123,7 +123,7 @@ struct ExecutableAllocator { } # endif # elif OS(INTEGRITY) - OSAllocator::setMemoryAttributes(addr, /*writable*/ true, /*executable*/ false); + OSAllocator::setMemoryAttributes(addr, size, /*writable*/ true, /*executable*/ false); # else int mode = PROT_READ | PROT_WRITE; if (mprotect(addr, size, mode) != 0) { @@ -159,7 +159,7 @@ struct ExecutableAllocator { } # endif # elif OS(INTEGRITY) - OSAllocator::setMemoryAttributes(addr, /*writable*/ false, /*executable*/ true); + OSAllocator::setMemoryAttributes(addr, size, /*writable*/ false, /*executable*/ true); # else int mode = PROT_READ | PROT_EXEC; if (mprotect(addr, size, mode) != 0) { diff --git a/src/3rdparty/masm/wtf/OSAllocator.h b/src/3rdparty/masm/wtf/OSAllocator.h index 366dd73993..9648a4e08f 100644 --- a/src/3rdparty/masm/wtf/OSAllocator.h +++ b/src/3rdparty/masm/wtf/OSAllocator.h @@ -75,7 +75,7 @@ public: static bool canAllocateExecutableMemory(); #if defined(Q_OS_INTEGRITY) - static void setMemoryAttributes(void* addr, bool writable, bool executable); + static void setMemoryAttributes(void* addr, size_t size, bool writable, bool executable); #endif }; diff --git a/src/3rdparty/masm/wtf/OSAllocatorIntegrity.cpp b/src/3rdparty/masm/wtf/OSAllocatorIntegrity.cpp index 7addf9e5c2..27f72073c4 100644 --- a/src/3rdparty/masm/wtf/OSAllocatorIntegrity.cpp +++ b/src/3rdparty/masm/wtf/OSAllocatorIntegrity.cpp @@ -123,10 +123,14 @@ Error setAttributes(MemoryRegion mr, bool writable, bool executable) return SetMemoryRegionAttributes(mr, attributes); } -void OSAllocator::setMemoryAttributes(void* addr, bool writable, bool executable) +void OSAllocator::setMemoryAttributes(void* addr, size_t size, bool writable, bool executable) { - const MRPair* pair = memoryRegionsContainer.getMRPair((Address)addr); - CheckSuccess(setAttributes(pair->vmr, writable, executable)); + Address addressIterator = Address(addr); + for(int i=0; i<(size + ASP_PAGESIZE -1)/ASP_PAGESIZE; i++) { + const MRPair* pair = memoryRegionsContainer.getMRPair(addressIterator); + CheckSuccess(setAttributes(pair->vmr, writable, executable)); + addressIterator += ASP_PAGESIZE; + } } void* OSAllocator::reserveUncommitted(size_t bytes, Usage usage, bool writable, bool executable) @@ -140,9 +144,9 @@ void* OSAllocator::reserveUncommitted(size_t bytes, Usage usage, bool writable, Address addressIterator = virtualStart; for(int i=0; i<(bytes + ASP_PAGESIZE -1)/ASP_PAGESIZE; i++) { MRPair pair; + pair.start = addressIterator; CheckSuccess(SplitMemoryRegion(VMR, ASP_PAGESIZE, &pair.vmr)); CheckSuccess(setAttributes(pair.vmr, writable, executable)); - pair.start = addressIterator; memoryRegionsContainer.insertMRPair(&pair); addressIterator += ASP_PAGESIZE; -- cgit v1.2.3