|
| 1 | +#ifndef _GNU_SOURCE |
| 2 | +#define _GNU_SOURCE |
| 3 | +#endif |
| 4 | + |
| 5 | +#include "ia2_destructor_runtime.h" |
| 6 | + |
| 7 | +#include "ia2.h" |
| 8 | +#include "ia2_internal.h" |
| 9 | + |
| 10 | +#include <dlfcn.h> |
| 11 | +#include <stdbool.h> |
| 12 | +#include <stddef.h> |
| 13 | +#include <stdint.h> |
| 14 | +#include <stdio.h> |
| 15 | +#include <stdlib.h> |
| 16 | + |
| 17 | +struct ia2_destructor_runtime_entry { |
| 18 | + void (*wrapper)(void); |
| 19 | + const struct ia2_destructor_metadata_record *record; |
| 20 | + uint32_t pkru_value; |
| 21 | +}; |
| 22 | + |
| 23 | +static struct ia2_destructor_runtime_entry *destructor_entries; |
| 24 | +static size_t destructor_entry_count; |
| 25 | +static bool initialized; |
| 26 | + |
| 27 | +static uint32_t compute_union_pkru(int exit_pkey, int target_pkey) { |
| 28 | + uint32_t mask = ~0u; |
| 29 | + mask &= ~0b11u; // Always allow shared key 0. |
| 30 | + mask &= ~(0b11u << (2 * exit_pkey)); |
| 31 | + mask &= ~(0b11u << (2 * target_pkey)); |
| 32 | + return mask; |
| 33 | +} |
| 34 | + |
| 35 | +static void ensure_initialized(void) { |
| 36 | + if (initialized) { |
| 37 | + return; |
| 38 | + } |
| 39 | + initialized = true; |
| 40 | + |
| 41 | + if (ia2_destructor_metadata_count == 0) { |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + destructor_entries = |
| 46 | + calloc(ia2_destructor_metadata_count, sizeof(struct ia2_destructor_runtime_entry)); |
| 47 | + if (!destructor_entries) { |
| 48 | + fprintf(stderr, "ia2: failed to allocate destructor metadata cache\n"); |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + for (unsigned int i = 0; i < ia2_destructor_metadata_count; ++i) { |
| 53 | + const struct ia2_destructor_metadata_record *record = &ia2_destructor_metadata[i]; |
| 54 | + |
| 55 | + dlerror(); |
| 56 | + void *symbol = dlsym(RTLD_DEFAULT, record->wrapper); |
| 57 | + const char *dlerr = dlerror(); |
| 58 | + if (dlerr != NULL || symbol == NULL) { |
| 59 | + fprintf(stderr, "ia2: failed to resolve destructor wrapper '%s': %s\n", |
| 60 | + record->wrapper, dlerr ? dlerr : "symbol not found"); |
| 61 | + continue; |
| 62 | + } |
| 63 | + |
| 64 | + struct ia2_destructor_runtime_entry *entry = &destructor_entries[destructor_entry_count++]; |
| 65 | + entry->wrapper = (void (*)(void))symbol; |
| 66 | + entry->record = record; |
| 67 | + if (record->uses_union_pkru) { |
| 68 | + entry->pkru_value = compute_union_pkru(ia2_destructor_exit_pkey, record->compartment_pkey); |
| 69 | + } else { |
| 70 | + entry->pkru_value = PKRU(record->compartment_pkey); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +void ia2_destructor_runtime_init(void) { |
| 76 | + ensure_initialized(); |
| 77 | +} |
| 78 | + |
| 79 | +bool ia2_destructor_metadata_lookup(void (*wrapper)(void), |
| 80 | + const struct ia2_destructor_metadata_record **out_record, |
| 81 | + uint32_t *out_pkru_value) { |
| 82 | + ensure_initialized(); |
| 83 | + |
| 84 | + if (!wrapper || destructor_entry_count == 0) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + for (size_t i = 0; i < destructor_entry_count; ++i) { |
| 89 | + if (destructor_entries[i].wrapper == wrapper) { |
| 90 | + if (out_record) { |
| 91 | + *out_record = destructor_entries[i].record; |
| 92 | + } |
| 93 | + if (out_pkru_value) { |
| 94 | + *out_pkru_value = destructor_entries[i].pkru_value; |
| 95 | + } |
| 96 | + return true; |
| 97 | + } |
| 98 | + } |
| 99 | + return false; |
| 100 | +} |
| 101 | + |
| 102 | +uint32_t ia2_destructor_pkru_for(void (*wrapper)(void), int target_compartment_pkey) { |
| 103 | + uint32_t pkru_value; |
| 104 | + if (ia2_destructor_metadata_lookup(wrapper, NULL, &pkru_value)) { |
| 105 | + return pkru_value; |
| 106 | + } |
| 107 | + return PKRU(target_compartment_pkey); |
| 108 | +} |
| 109 | + |
| 110 | +// Helper to read/write PKRU, guarded for non-x86 architectures. |
| 111 | +#if defined(__x86_64__) |
| 112 | +static inline uint32_t read_pkru(void) { |
| 113 | + uint32_t pkru; |
| 114 | + __asm__ volatile("xor %%ecx, %%ecx\n" |
| 115 | + "rdpkru\n" |
| 116 | + : "=a"(pkru) |
| 117 | + : |
| 118 | + : "ecx", "edx"); |
| 119 | + return pkru; |
| 120 | +} |
| 121 | + |
| 122 | +static inline void write_pkru(uint32_t pkru) { |
| 123 | + __asm__ volatile("xor %%ecx, %%ecx\n" |
| 124 | + "xor %%edx, %%edx\n" |
| 125 | + "wrpkru\n" |
| 126 | + : |
| 127 | + : "a"(pkru) |
| 128 | + : "ecx", "edx"); |
| 129 | +} |
| 130 | +#endif |
| 131 | + |
| 132 | +uint32_t ia2_destructor_enter(void *wrapper_addr, int target_pkey) { |
| 133 | +#if defined(__x86_64__) |
| 134 | + uint32_t original_pkru = read_pkru(); |
| 135 | + uint32_t desired_pkru = ia2_destructor_pkru_for((void (*)(void))wrapper_addr, target_pkey); |
| 136 | + if (desired_pkru != original_pkru) { |
| 137 | + write_pkru(desired_pkru); |
| 138 | +#ifdef IA2_TRACE_EXIT |
| 139 | + ia2_trace_exit_record((int)ia2_get_compartment(), target_pkey, desired_pkru); |
| 140 | +#endif |
| 141 | + } |
| 142 | + return original_pkru; |
| 143 | +#else |
| 144 | + (void)wrapper_addr; |
| 145 | + (void)target_pkey; |
| 146 | + return 0; |
| 147 | +#endif |
| 148 | +} |
| 149 | + |
| 150 | +void ia2_destructor_leave(uint32_t original_pkru) { |
| 151 | +#if defined(__x86_64__) |
| 152 | + write_pkru(original_pkru); |
| 153 | +#else |
| 154 | + (void)original_pkru; |
| 155 | +#endif |
| 156 | +} |
0 commit comments