Josh Gao | 93c0f5e | 2015-10-06 11:08:13 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | 5e62b34 | 2018-10-25 11:00:00 -0700 | [diff] [blame] | 17 | #pragma once |
Josh Gao | 93c0f5e | 2015-10-06 11:08:13 -0700 | [diff] [blame] | 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 | |
Christopher Ferris | 7a3681e | 2017-04-24 17:48:32 -0700 | [diff] [blame] | 25 | #include <async_safe/log.h> |
| 26 | |
Josh Gao | 4956c37 | 2019-12-19 16:35:51 -0800 | [diff] [blame] | 27 | #include "platform/bionic/macros.h" |
Kalesh Singh | 2bdf22e | 2023-08-11 15:15:55 -0700 | [diff] [blame^] | 28 | #include "platform/bionic/page.h" |
Josh Gao | 93c0f5e | 2015-10-06 11:08:13 -0700 | [diff] [blame] | 29 | |
| 30 | template <typename T> |
| 31 | union WriteProtectedContents { |
| 32 | T value; |
Kalesh Singh | 2bdf22e | 2023-08-11 15:15:55 -0700 | [diff] [blame^] | 33 | char padding[max_page_size()]; |
Josh Gao | 93c0f5e | 2015-10-06 11:08:13 -0700 | [diff] [blame] | 34 | |
| 35 | WriteProtectedContents() = default; |
Elliott Hughes | 5e62b34 | 2018-10-25 11:00:00 -0700 | [diff] [blame] | 36 | BIONIC_DISALLOW_COPY_AND_ASSIGN(WriteProtectedContents); |
Kalesh Singh | 2bdf22e | 2023-08-11 15:15:55 -0700 | [diff] [blame^] | 37 | } __attribute__((aligned(max_page_size()))); |
Josh Gao | 93c0f5e | 2015-10-06 11:08:13 -0700 | [diff] [blame] | 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 { |
Elliott Hughes | 1e381a2 | 2020-06-08 08:41:27 -0700 | [diff] [blame] | 44 | public: |
Kalesh Singh | 2bdf22e | 2023-08-11 15:15:55 -0700 | [diff] [blame^] | 45 | static_assert(sizeof(T) < max_page_size(), |
| 46 | "WriteProtected only supports contents up to max_page_size()"); |
Josh Gao | 93c0f5e | 2015-10-06 11:08:13 -0700 | [diff] [blame] | 47 | static_assert(__is_pod(T), "WriteProtected only supports POD contents"); |
| 48 | |
Josh Gao | 93c0f5e | 2015-10-06 11:08:13 -0700 | [diff] [blame] | 49 | WriteProtected() = default; |
Elliott Hughes | 5e62b34 | 2018-10-25 11:00:00 -0700 | [diff] [blame] | 50 | BIONIC_DISALLOW_COPY_AND_ASSIGN(WriteProtected); |
Josh Gao | 93c0f5e | 2015-10-06 11:08:13 -0700 | [diff] [blame] | 51 | |
| 52 | void initialize() { |
| 53 | // Not strictly necessary, but this will hopefully segfault if we initialize |
| 54 | // multiple times by accident. |
| 55 | memset(&contents, 0, sizeof(contents)); |
Elliott Hughes | 1e381a2 | 2020-06-08 08:41:27 -0700 | [diff] [blame] | 56 | set_protection(PROT_READ); |
Josh Gao | 93c0f5e | 2015-10-06 11:08:13 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | const T* operator->() { |
| 60 | return &contents.value; |
| 61 | } |
| 62 | |
| 63 | const T& operator*() { |
| 64 | return contents.value; |
| 65 | } |
| 66 | |
| 67 | template <typename Mutator> |
| 68 | void mutate(Mutator mutator) { |
Elliott Hughes | 1e381a2 | 2020-06-08 08:41:27 -0700 | [diff] [blame] | 69 | set_protection(PROT_READ | PROT_WRITE); |
Josh Gao | 93c0f5e | 2015-10-06 11:08:13 -0700 | [diff] [blame] | 70 | mutator(&contents.value); |
Elliott Hughes | 1e381a2 | 2020-06-08 08:41:27 -0700 | [diff] [blame] | 71 | set_protection(PROT_READ); |
| 72 | } |
| 73 | |
| 74 | private: |
| 75 | WriteProtectedContents<T> contents; |
| 76 | |
| 77 | void set_protection(int prot) { |
| 78 | auto addr = &contents; |
| 79 | #if __has_feature(hwaddress_sanitizer) |
| 80 | // The mprotect system call does not currently untag pointers, so do it |
| 81 | // ourselves. |
| 82 | addr = untag_address(addr); |
| 83 | #endif |
Kalesh Singh | 2bdf22e | 2023-08-11 15:15:55 -0700 | [diff] [blame^] | 84 | if (mprotect(reinterpret_cast<void*>(addr), max_page_size(), prot) == -1) { |
Elliott Hughes | 1e381a2 | 2020-06-08 08:41:27 -0700 | [diff] [blame] | 85 | async_safe_fatal("WriteProtected mprotect %x failed: %s", prot, strerror(errno)); |
Josh Gao | 93c0f5e | 2015-10-06 11:08:13 -0700 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | }; |