|
| 1 | +#include "atexit.hpp" |
| 2 | + |
| 3 | +#include "elf_util.h" |
| 4 | +#include "logging.h" |
| 5 | + |
| 6 | +template <typename T> |
| 7 | +inline T *getExportedFieldPointer(const SandHook::ElfImg &libc, |
| 8 | + const char *name) { |
| 9 | + auto *addr = reinterpret_cast<T *>(libc.getSymbAddress(name)); |
| 10 | + |
| 11 | + return addr == nullptr ? nullptr : addr; |
| 12 | +} |
| 13 | + |
| 14 | +namespace Atexit { |
| 15 | + |
| 16 | +bool AtexitArray::append_entry(const AtexitEntry &entry) { |
| 17 | + if (size_ >= capacity_ && !expand_capacity()) |
| 18 | + return false; |
| 19 | + |
| 20 | + size_t idx = size_++; |
| 21 | + |
| 22 | + set_writable(true, idx, 1); |
| 23 | + array_[idx] = entry; |
| 24 | + ++total_appends_; |
| 25 | + set_writable(false, idx, 1); |
| 26 | + |
| 27 | + return true; |
| 28 | +} |
| 29 | +// Extract an entry and return it. |
| 30 | +AtexitEntry AtexitArray::extract_entry(size_t idx) { |
| 31 | + AtexitEntry result = array_[idx]; |
| 32 | + |
| 33 | + set_writable(true, idx, 1); |
| 34 | + array_[idx] = {}; |
| 35 | + ++extracted_count_; |
| 36 | + set_writable(false, idx, 1); |
| 37 | + |
| 38 | + return result; |
| 39 | +} |
| 40 | + |
| 41 | +void AtexitArray::recompact() { |
| 42 | + if (!needs_recompaction()) { |
| 43 | + LOGD("needs_recompaction returns false"); |
| 44 | + // return; |
| 45 | + } |
| 46 | + |
| 47 | + set_writable(true, 0, size_); |
| 48 | + |
| 49 | + // Optimization: quickly skip over the initial non-null entries. |
| 50 | + size_t src = 0, dst = 0; |
| 51 | + while (src < size_ && array_[src].fn != nullptr) { |
| 52 | + ++src; |
| 53 | + ++dst; |
| 54 | + } |
| 55 | + |
| 56 | + // Shift the non-null entries forward, and zero out the removed entries at the |
| 57 | + // end of the array. |
| 58 | + for (; src < size_; ++src) { |
| 59 | + const AtexitEntry entry = array_[src]; |
| 60 | + array_[src] = {}; |
| 61 | + if (entry.fn != nullptr) { |
| 62 | + array_[dst++] = entry; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // If the table uses fewer pages, clean the pages at the end. |
| 67 | + size_t old_bytes = page_end_of_index(size_); |
| 68 | + size_t new_bytes = page_end_of_index(dst); |
| 69 | + if (new_bytes < old_bytes) { |
| 70 | + madvise(reinterpret_cast<char *>(array_) + new_bytes, old_bytes - new_bytes, |
| 71 | + MADV_DONTNEED); |
| 72 | + } |
| 73 | + |
| 74 | + set_writable(false, 0, size_); |
| 75 | + |
| 76 | + size_ = dst; |
| 77 | + extracted_count_ = 0; |
| 78 | +} |
| 79 | + |
| 80 | +// Use mprotect to make the array writable or read-only. Returns true on |
| 81 | +// success. Making the array read-only could protect against either |
| 82 | +// unintentional or malicious corruption of the array. |
| 83 | +void AtexitArray::set_writable(bool writable, size_t start_idx, |
| 84 | + size_t num_entries) { |
| 85 | + if (array_ == nullptr) |
| 86 | + return; |
| 87 | + |
| 88 | + const size_t start_byte = page_start_of_index(start_idx); |
| 89 | + const size_t stop_byte = page_end_of_index(start_idx + num_entries); |
| 90 | + const size_t byte_len = stop_byte - start_byte; |
| 91 | + |
| 92 | + const int prot = PROT_READ | (writable ? PROT_WRITE : 0); |
| 93 | + if (mprotect(reinterpret_cast<char *>(array_) + start_byte, byte_len, prot) != |
| 94 | + 0) { |
| 95 | + PLOGE("mprotect failed on atexit array: %m"); |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// Approximately double the capacity. Returns true if successful (no overflow). |
| 100 | +// AtexitEntry is smaller than a page, but this function should still be correct |
| 101 | +// even if AtexitEntry were larger than one. |
| 102 | +bool AtexitArray::next_capacity(size_t capacity, size_t *result) { |
| 103 | + if (capacity == 0) { |
| 104 | + *result = page_end(sizeof(AtexitEntry)) / sizeof(AtexitEntry); |
| 105 | + return true; |
| 106 | + } |
| 107 | + size_t num_bytes; |
| 108 | + if (__builtin_mul_overflow(page_end_of_index(capacity), 2, &num_bytes)) { |
| 109 | + PLOGE("__cxa_atexit: capacity calculation overflow"); |
| 110 | + return false; |
| 111 | + } |
| 112 | + *result = num_bytes / sizeof(AtexitEntry); |
| 113 | + return true; |
| 114 | +} |
| 115 | + |
| 116 | +bool AtexitArray::expand_capacity() { |
| 117 | + size_t new_capacity; |
| 118 | + if (!next_capacity(capacity_, &new_capacity)) |
| 119 | + return false; |
| 120 | + const size_t new_capacity_bytes = page_end_of_index(new_capacity); |
| 121 | + |
| 122 | + set_writable(true, 0, capacity_); |
| 123 | + |
| 124 | + bool result = false; |
| 125 | + void *new_pages; |
| 126 | + if (array_ == nullptr) { |
| 127 | + new_pages = mmap(nullptr, new_capacity_bytes, PROT_READ | PROT_WRITE, |
| 128 | + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
| 129 | + } else { |
| 130 | + // mremap fails if the source buffer crosses a boundary between two VMAs. |
| 131 | + // When a single array element is modified, the kernel should split then |
| 132 | + // rejoin the buffer's VMA. |
| 133 | + new_pages = mremap(array_, page_end_of_index(capacity_), new_capacity_bytes, |
| 134 | + MREMAP_MAYMOVE); |
| 135 | + } |
| 136 | + if (new_pages == MAP_FAILED) { |
| 137 | + PLOGE("__cxa_atexit: mmap/mremap failed to allocate %zu bytes: %m", |
| 138 | + new_capacity_bytes); |
| 139 | + } else { |
| 140 | + result = true; |
| 141 | + prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, new_pages, new_capacity_bytes, |
| 142 | + "atexit handlers"); |
| 143 | + array_ = static_cast<AtexitEntry *>(new_pages); |
| 144 | + capacity_ = new_capacity; |
| 145 | + } |
| 146 | + set_writable(false, 0, capacity_); |
| 147 | + return result; |
| 148 | +} |
| 149 | + |
| 150 | +AtexitArray *findAtexitArray() { |
| 151 | + SandHook::ElfImg libc("libc.so"); |
| 152 | + auto p_array = getExportedFieldPointer<AtexitEntry *>(libc, "_ZL7g_array.0"); |
| 153 | + auto p_size = getExportedFieldPointer<size_t>(libc, "_ZL7g_array.1"); |
| 154 | + auto p_extracted_count = |
| 155 | + getExportedFieldPointer<size_t>(libc, "_ZL7g_array.2"); |
| 156 | + auto p_capacity = getExportedFieldPointer<size_t>(libc, "_ZL7g_array.3"); |
| 157 | + auto p_total_appends = |
| 158 | + getExportedFieldPointer<uint64_t>(libc, "_ZL7g_array.4"); |
| 159 | + |
| 160 | + if (p_array == nullptr || p_size == nullptr || p_extracted_count == nullptr || |
| 161 | + p_capacity == nullptr || p_total_appends == nullptr) { |
| 162 | + LOGD("failed to find exported g_array fields in memory"); |
| 163 | + return nullptr; |
| 164 | + } |
| 165 | + |
| 166 | + return reinterpret_cast<AtexitArray *>(p_array); |
| 167 | +} |
| 168 | + |
| 169 | +} // namespace Atexit |
0 commit comments