| 1 |
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 OR ISC |
| 3 |
|
| 4 |
#include <openssl/base.h> |
| 5 |
|
| 6 |
#include "fork_ube_detect.h" |
| 7 |
#include "vm_ube_detect.h" |
| 8 |
|
| 9 |
#include "internal.h" |
| 10 |
#include "../internal.h" |
| 11 |
|
| 12 |
// What is a uniqueness-breaking event (UBE)? |
| 13 |
// AWS-LC manages state that must be unique on each usage. For example, the |
| 14 |
// CTR-DRBG state must be unique on each usage, otherwise the generation |
| 15 |
// function will produce duplicated values. We name such state "unique state" |
| 16 |
// or "unique memory" if referring directly to memory. Before using a unique |
| 17 |
// state, we must make sure to randomize it to preserve its uniqueness. |
| 18 |
// |
| 19 |
// When/how to randomize state depends on the context. For example, the CTR-DRBG |
| 20 |
// state, that is managed by AWS-LC, will randomize itself before usage during |
| 21 |
// normal operation. But there are events where this assumption is violated. We |
| 22 |
// call such events "uniqueness breaking events" (UBE). Forking an address space |
| 23 |
// is an example of an UBE. |
| 24 |
// |
| 25 |
// By detecting UBE's we can ensure that unique state is properly randomized. |
| 26 |
// AWS-LC is currently able to detect two different type of UBE's that would |
| 27 |
// violate usage requirements of a unique state. The two UBE types are: |
| 28 |
// Forking and VM resuming. Note, however, that detection methods rely on |
| 29 |
// technology that is unique to a platform. Hence, support for UBE detection |
| 30 |
// also depends on the platform AWS-LC is executed on. |
| 31 |
// |
| 32 |
// This file implements and manages the general machinery to detect an UBE. This |
| 33 |
// should be used by the rest of the code-base to implement proper randomization |
| 34 |
// of unique states before usage. |
| 35 |
// |
| 36 |
// ATTENTION: Before attempting to re-write this logic and add more mechanisms |
| 37 |
// review P269907867 carefully. |
| 38 |
|
| 39 |
static CRYPTO_once_t ube_state_initialize_once = CRYPTO_ONCE_INIT; |
| 40 |
static CRYPTO_once_t ube_detection_unavailable_once = CRYPTO_ONCE_INIT; |
| 41 |
static struct CRYPTO_STATIC_MUTEX ube_lock = CRYPTO_STATIC_MUTEX_INIT; |
| 42 |
// Locking for testing-specific code. Don't use |ube_lock| to avoid any |
| 43 |
// potential for deadlocks. |
| 44 |
static struct CRYPTO_STATIC_MUTEX ube_testing_lock = CRYPTO_STATIC_MUTEX_INIT; |
| 45 |
static uint8_t ube_detection_unavailable = 0; |
| 46 |
static uint8_t allow_mocked_detection = 0; |
| 47 |
|
| 48 |
static uint64_t override_fork_generation_number = 0; |
| 49 |
void set_fork_ube_generation_number_FOR_TESTING(uint64_t fork_gn) { |
| 50 |
CRYPTO_STATIC_MUTEX_lock_write(&ube_testing_lock); |
| 51 |
override_fork_generation_number = fork_gn; |
| 52 |
CRYPTO_STATIC_MUTEX_unlock_write(&ube_testing_lock); |
| 53 |
} |
| 54 |
|
| 55 |
static uint32_t override_vm_ube_generation_number = 0; |
| 56 |
void set_vm_ube_generation_number_FOR_TESTING(uint32_t vm_ube_gn) { |
| 57 |
CRYPTO_STATIC_MUTEX_lock_write(&ube_testing_lock); |
| 58 |
override_vm_ube_generation_number = vm_ube_gn; |
| 59 |
CRYPTO_STATIC_MUTEX_unlock_write(&ube_testing_lock); |
| 60 |
} |
| 61 |
|
| 62 |
static int get_vm_ube_generation_number(uint32_t *gn) { |
| 63 |
if (allow_mocked_detection == 1) { |
| 64 |
CRYPTO_STATIC_MUTEX_lock_read(&ube_testing_lock); |
| 65 |
*gn = override_vm_ube_generation_number; |
| 66 |
CRYPTO_STATIC_MUTEX_unlock_read(&ube_testing_lock); |
| 67 |
return 1; |
| 68 |
} |
| 69 |
|
| 70 |
return CRYPTO_get_vm_ube_generation(gn); |
| 71 |
} |
| 72 |
|
| 73 |
static int get_fork_generation_number(uint64_t *gn) { |
| 74 |
if (allow_mocked_detection == 1) { |
| 75 |
CRYPTO_STATIC_MUTEX_lock_read(&ube_testing_lock); |
| 76 |
*gn = override_fork_generation_number; |
| 77 |
CRYPTO_STATIC_MUTEX_unlock_read(&ube_testing_lock); |
| 78 |
return 1; |
| 79 |
} |
| 80 |
|
| 81 |
uint64_t fork_gn = CRYPTO_get_fork_ube_generation(); |
| 82 |
if (fork_gn == 0) { |
| 83 |
return 0; |
| 84 |
} |
| 85 |
|
| 86 |
*gn = fork_gn; |
| 87 |
return 1; |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
// The UBE generation number is shared for the entire address space. One could |
| 92 |
// implement a per-thread UBE generation number. However, this could be |
| 93 |
// inefficient for global unique states if care is not taken. Because every |
| 94 |
// thread only have visibility on their own per-thread generation number, we |
| 95 |
// could trigger a new randomization of the unique state per-thread. While a |
| 96 |
// single randomization of the global unique state should be sufficient. |
| 97 |
struct ube_state { |
| 98 |
uint64_t generation_number; |
| 99 |
uint64_t cached_fork_gn; |
| 100 |
uint32_t cached_vm_ube_gn; |
| 101 |
}; |
| 102 |
static struct ube_state ube_global_state = { 0, 0, 0 }; |
| 103 |
|
| 104 |
// Convenience object that makes it easier to extend the number of detection |
| 105 |
// methods without having to modify function signatures. |
| 106 |
struct detection_gn { |
| 107 |
#define NUMBER_OF_DETECTION_GENERATION_NUMBERS 2 |
| 108 |
uint64_t current_fork_gn; |
| 109 |
uint32_t current_vm_ube_gn; |
| 110 |
}; |
| 111 |
|
| 112 |
// set_ube_detection_unavailable_once is the single mutation point of |
| 113 |
// |ube_detection_unavailable|. Sets the variable to 1 (true). |
| 114 |
static void set_ube_detection_unavailable_once(void) { |
| 115 |
ube_detection_unavailable = 1; |
| 116 |
} |
| 117 |
|
| 118 |
// ube_failed is a convenience function to synchronize mutation of |
| 119 |
// |ube_detection_unavailable|. In practice, synchronization is not strictly |
| 120 |
// needed because currently the mutation is only ever assigning 1 to the |
| 121 |
// variable. |
| 122 |
static void ube_failed(void) { |
| 123 |
CRYPTO_once(&ube_detection_unavailable_once, set_ube_detection_unavailable_once); |
| 124 |
} |
| 125 |
|
| 126 |
// ube_state_initialize initializes the global state |ube_global_state|. |
| 127 |
static void ube_state_initialize(void) { |
| 128 |
|
| 129 |
ube_global_state.generation_number = 0; |
| 130 |
int ret_fork_gn = get_fork_generation_number( |
| 131 |
&(ube_global_state.cached_fork_gn)); |
| 132 |
int ret_vm_ube_gn = get_vm_ube_generation_number( |
| 133 |
&(ube_global_state.cached_vm_ube_gn)); |
| 134 |
|
| 135 |
if (ret_fork_gn == 0 || ret_vm_ube_gn == 0) { |
| 136 |
ube_failed(); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
// ube_update_state updates the global state |ube_global_state| with the |
| 141 |
// generation numbers loaded into |current_detection_gn| and increments the UBE |
| 142 |
// generation number. |
| 143 |
static void ube_update_state(struct detection_gn *current_detection_gn) { |
| 144 |
|
| 145 |
ube_global_state.generation_number += 1; |
| 146 |
|
| 147 |
// Make sure we cache all new generation numbers. Otherwise, we might detect |
| 148 |
// a fork UBE but, in fact, both a fork and vm_ube UBE occurred. Then next |
| 149 |
// time we enter, a redundant reseed will be emitted. |
| 150 |
ube_global_state.cached_fork_gn = current_detection_gn->current_fork_gn; |
| 151 |
ube_global_state.cached_vm_ube_gn = current_detection_gn->current_vm_ube_gn; |
| 152 |
} |
| 153 |
|
| 154 |
// ube_get_detection_generation_numbers loads the current detection generation |
| 155 |
// numbers into |current_detection_gn|. |
| 156 |
// |
| 157 |
// Returns 1 on success and 0 otherwise. The 0 return value means that a |
| 158 |
// detection method we expected to be available, is in fact not. |
| 159 |
static int ube_get_detection_generation_numbers( |
| 160 |
struct detection_gn *current_detection_gn) { |
| 161 |
|
| 162 |
// An attempt to prevent a situation where new detection methods are added but |
| 163 |
// we forget to load them. |
| 164 |
OPENSSL_STATIC_ASSERT(NUMBER_OF_DETECTION_GENERATION_NUMBERS == 2, |
| 165 |
not_handling_the_exact_number_of_detection_generation_numbers); |
| 166 |
|
| 167 |
int ret_detect_gn = get_fork_generation_number( |
| 168 |
&(current_detection_gn->current_fork_gn)); |
| 169 |
int ret_vm_ube_gn = get_vm_ube_generation_number( |
| 170 |
&(current_detection_gn->current_vm_ube_gn)); |
| 171 |
|
| 172 |
if (ret_detect_gn == 0 || ret_vm_ube_gn == 0) { |
| 173 |
return 0; |
| 174 |
} |
| 175 |
|
| 176 |
return 1; |
| 177 |
} |
| 178 |
|
| 179 |
// ube_is_detected computes whether a UBE has been detected or not by comparing |
| 180 |
// the cached detection generation numbers with the current detection generation |
| 181 |
// numbers. The current generation numbers must be loaded into |
| 182 |
// |current_detection_gn| before calling this function. |
| 183 |
// |
| 184 |
// Returns 1 if UBE has been detected and 0 if no UBE has been detected. |
| 185 |
// |
| 186 |
// ATTENTION: Before attempting to re-write this logic and add more mechanisms |
| 187 |
// review P269907867 carefully. |
| 188 |
static int ube_is_detected(struct detection_gn *current_detection_gn) { |
| 189 |
|
| 190 |
if (ube_global_state.cached_fork_gn != current_detection_gn->current_fork_gn || |
| 191 |
ube_global_state.cached_vm_ube_gn != current_detection_gn->current_vm_ube_gn) { |
| 192 |
return 1; |
| 193 |
} |
| 194 |
return 0; |
| 195 |
} |
| 196 |
|
| 197 |
int CRYPTO_get_ube_generation_number(uint64_t *current_generation_number) { |
| 198 |
|
| 199 |
GUARD_PTR(current_generation_number); |
| 200 |
|
| 201 |
CRYPTO_once(&ube_state_initialize_once, ube_state_initialize); |
| 202 |
|
| 203 |
// If something failed at an earlier point short-circuit immediately. This |
| 204 |
// saves work in case the UBE detection is not supported. The check below |
| 205 |
// must be done after attempting to initialize the UBE state. Because |
| 206 |
// initialization might fail and we can short-circuit here. |
| 207 |
// |
| 208 |
// |ube_detection_unavailable| and |allow_mocked_detection| are both global |
| 209 |
// variables that can be mutated in multiple threads. They are uint8_t typed |
| 210 |
// though and we assume read and write to them is atomic. This path is hot. |
| 211 |
// The assumption avoids us having to acquire (up to) two read locks that |
| 212 |
// would probably never be contended anyway. That would not block other |
| 213 |
// threads, but cost cycles regardless. |
| 214 |
if (ube_detection_unavailable == 1 && |
| 215 |
allow_mocked_detection != 1) { |
| 216 |
return 0; |
| 217 |
} |
| 218 |
|
| 219 |
struct detection_gn current_detection_gn = { 0, 0 }; |
| 220 |
|
| 221 |
// First read generation numbers for each supported detection method. We do |
| 222 |
// not mutate |ube_global_state|. So, a read lock is sufficient at this point. |
| 223 |
// Each individual detection method will have their own concurrency controls |
| 224 |
// if needed. |
| 225 |
|
| 226 |
if (ube_get_detection_generation_numbers(¤t_detection_gn) != 1) { |
| 227 |
ube_failed(); |
| 228 |
return 0; |
| 229 |
} |
| 230 |
CRYPTO_STATIC_MUTEX_lock_read(&ube_lock); |
| 231 |
if (ube_is_detected(¤t_detection_gn) == 0) { |
| 232 |
// No UBE detected, so just grab UBE generation number from the state. |
| 233 |
*current_generation_number = ube_global_state.generation_number; |
| 234 |
CRYPTO_STATIC_MUTEX_unlock_read(&ube_lock); |
| 235 |
return 1; |
| 236 |
} |
| 237 |
CRYPTO_STATIC_MUTEX_unlock_read(&ube_lock); |
| 238 |
|
| 239 |
// Reaching this point means that an UBE has been detected. We must now |
| 240 |
// synchronize an update to the UBE generation number. To avoid redundant |
| 241 |
// reseeds, we must ensure the generation number is only incremented once for |
| 242 |
// all UBE's that might have happened. Therefore, first take a write lock but |
| 243 |
// before mutating the state, check for an UBE again. Checking again ensures |
| 244 |
// that only one thread increments the UBE generation number, because the |
| 245 |
// cached detection method generation numbers have been updated by the thread |
| 246 |
// that had the first entry. |
| 247 |
|
| 248 |
CRYPTO_STATIC_MUTEX_lock_write(&ube_lock); |
| 249 |
if (ube_get_detection_generation_numbers(¤t_detection_gn) != 1) { |
| 250 |
ube_failed(); |
| 251 |
CRYPTO_STATIC_MUTEX_unlock_write(&ube_lock); |
| 252 |
return 0; |
| 253 |
} |
| 254 |
if (ube_is_detected(¤t_detection_gn) == 0) { |
| 255 |
// Another thread already updated the global state. Just load the UBE |
| 256 |
// generation number instead. |
| 257 |
*current_generation_number = ube_global_state.generation_number; |
| 258 |
CRYPTO_STATIC_MUTEX_unlock_write(&ube_lock); |
| 259 |
return 1; |
| 260 |
} |
| 261 |
|
| 262 |
// Okay, we are really the first to update the state after detecting an UBE. |
| 263 |
ube_update_state(¤t_detection_gn); |
| 264 |
*current_generation_number = ube_global_state.generation_number; |
| 265 |
CRYPTO_STATIC_MUTEX_unlock_write(&ube_lock); |
| 266 |
|
| 267 |
return 1; |
| 268 |
} |
| 269 |
|
| 270 |
// Synchronize writing to |allow_mocked_detection|. But only to more easily |
| 271 |
// reason about ordering. They are supposed to only be used in testing code |
| 272 |
// and called, initially, from a single-threaded process at initialization time. |
| 273 |
// We generally don't care about any contention that might happen. |
| 274 |
|
| 275 |
void allow_mocked_ube_detection_FOR_TESTING(void) { |
| 276 |
CRYPTO_STATIC_MUTEX_lock_write(&ube_testing_lock); |
| 277 |
allow_mocked_detection = 1; |
| 278 |
CRYPTO_STATIC_MUTEX_unlock_write(&ube_testing_lock); |
| 279 |
} |
| 280 |
|
| 281 |
void disable_mocked_ube_detection_FOR_TESTING(void) { |
| 282 |
CRYPTO_STATIC_MUTEX_lock_write(&ube_testing_lock); |
| 283 |
allow_mocked_detection = 0; |
| 284 |
CRYPTO_STATIC_MUTEX_unlock_write(&ube_testing_lock); |
| 285 |
|
| 286 |
set_fork_ube_generation_number_FOR_TESTING(0); |
| 287 |
set_vm_ube_generation_number_FOR_TESTING(0); |
| 288 |
} |