1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include <errno.h> 20 #include <string.h> 21 #include <sys/cdefs.h> 22 #include <sys/mman.h> 23 #include <sys/user.h> 24 25 #include <async_safe/log.h> 26 27 #include "platform/bionic/macros.h" 28 #include "platform/bionic/page.h" 29 30 template <typename T> 31 union WriteProtectedContents { 32 T value; 33 char padding[max_android_page_size()]; 34 35 WriteProtectedContents() = default; 36 BIONIC_DISALLOW_COPY_AND_ASSIGN(WriteProtectedContents); 37 } __attribute__((aligned(max_android_page_size()))); 38 39 // Write protected wrapper class that aligns its contents to a page boundary, 40 // and sets the memory protection to be non-writable, except when being modified 41 // explicitly. 42 template <typename T> 43 class WriteProtected { 44 public: 45 static_assert(sizeof(T) < max_android_page_size(), 46 "WriteProtected only supports contents up to max_android_page_size()"); 47 48 WriteProtected() = default; 49 BIONIC_DISALLOW_COPY_AND_ASSIGN(WriteProtected); 50 initialize()51 void initialize() { 52 // Not strictly necessary, but this will hopefully segfault if we initialize 53 // multiple times by accident. 54 memset(contents_addr(), 0, sizeof(contents)); 55 set_protection(PROT_READ); 56 } 57 58 const T* operator->() { 59 return &contents_addr()->value; 60 } 61 62 const T& operator*() { 63 return contents_addr()->value; 64 } 65 66 template <typename Mutator> mutate(Mutator mutator)67 void mutate(Mutator mutator) { 68 set_protection(PROT_READ | PROT_WRITE); 69 mutator(&contents_addr()->value); 70 set_protection(PROT_READ); 71 } 72 73 private: 74 WriteProtectedContents<T> contents; 75 contents_addr()76 WriteProtectedContents<T>* contents_addr() { 77 auto addr = &contents; 78 // Hide the fact that we're returning the address of contents from the compiler. 79 // Otherwise it may generate code assuming alignment of 64KB even though the 80 // variable is only guaranteed to have 4KB alignment. 81 __asm__ __volatile__("" : "+r"(addr)); 82 return addr; 83 } 84 set_protection(int prot)85 void set_protection(int prot) { 86 auto addr = contents_addr(); 87 #if __has_feature(hwaddress_sanitizer) 88 // The mprotect system call does not currently untag pointers, so do it 89 // ourselves. 90 addr = untag_address(addr); 91 #endif 92 if (mprotect(reinterpret_cast<void*>(addr), max_android_page_size(), prot) == -1) { 93 async_safe_fatal("WriteProtected mprotect %x failed: %s", prot, strerror(errno)); 94 } 95 } 96 }; 97