1 // Copyright 2024 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/memory/protected_memory.h"
6
7 #include <sys/mman.h>
8
9 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_ANDROID)
10 #include <sys/resource.h>
11 #endif // BUILDFLAG(IS_LINUX)
12
13 #if BUILDFLAG(IS_MAC)
14 #include <mach/mach.h>
15 #include <mach/mach_vm.h>
16 #endif // BUILDFLAG(IS_MAC)
17
18 #include "base/bits.h"
19 #include "base/memory/page_size.h"
20
21 namespace base {
22
23 #if BUILDFLAG(PROTECTED_MEMORY_ENABLED)
24 namespace {
25
SetMemory(void * start,void * end,int prot)26 bool SetMemory(void* start, void* end, int prot) {
27 CHECK(end > start);
28 const uintptr_t page_start =
29 bits::AlignDown(reinterpret_cast<uintptr_t>(start), GetPageSize());
30 return mprotect(reinterpret_cast<void*>(page_start),
31 reinterpret_cast<uintptr_t>(end) - page_start, prot) == 0;
32 }
33
34 } // namespace
35
36 namespace internal {
37 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_ANDROID)
CheckMemoryReadOnly(const void * ptr)38 void CheckMemoryReadOnly(const void* ptr) {
39 const uintptr_t page_start =
40 bits::AlignDown(reinterpret_cast<uintptr_t>(ptr), GetPageSize());
41
42 // Note: We've casted away const here, which should not be meaningful since
43 // if the memory is written to we will abort immediately.
44 int result =
45 getrlimit(RLIMIT_NPROC, reinterpret_cast<struct rlimit*>(page_start));
46 CHECK(result == -1 && errno == EFAULT);
47 }
48 #elif BUILDFLAG(IS_MAC)
49 void CheckMemoryReadOnly(const void* ptr) {
50 mach_port_t object_name;
51 vm_region_basic_info_64 region_info;
52 mach_vm_size_t size = 1;
53 mach_msg_type_number_t count = VM_REGION_BASIC_INFO_COUNT_64;
54
55 kern_return_t kr = mach_vm_region(
56 mach_task_self(), reinterpret_cast<mach_vm_address_t*>(&ptr), &size,
57 VM_REGION_BASIC_INFO_64, reinterpret_cast<vm_region_info_t>(®ion_info),
58 &count, &object_name);
59 CHECK(kr == KERN_SUCCESS && region_info.protection == VM_PROT_READ);
60 }
61 #endif
62 } // namespace internal
63
SetMemoryReadWrite(void * start,void * end)64 bool AutoWritableMemoryBase::SetMemoryReadWrite(void* start, void* end) {
65 return SetMemory(start, end, PROT_READ | PROT_WRITE);
66 }
67
SetMemoryReadOnly(void * start,void * end)68 bool AutoWritableMemoryBase::SetMemoryReadOnly(void* start, void* end) {
69 return SetMemory(start, end, PROT_READ);
70 }
71 #endif // BUILDFLAG(PROTECTED_MEMORY_ENABLED)
72 } // namespace base
73