#include <openssl/base.h>
#include "fork_detect.h"
#include "snapsafe_detect.h"
#include "internal.h"
#include "../internal.h"
static CRYPTO_once_t ube_state_initialize_once = CRYPTO_ONCE_INIT;
static CRYPTO_once_t ube_detection_unavailable_once = CRYPTO_ONCE_INIT;
static struct CRYPTO_STATIC_MUTEX ube_lock = CRYPTO_STATIC_MUTEX_INIT;
static struct CRYPTO_STATIC_MUTEX ube_testing_lock = CRYPTO_STATIC_MUTEX_INIT;
static uint8_t ube_detection_unavailable = 0;
static uint8_t allow_mocked_detection = 0;
static uint64_t override_fork_generation_number = 0;
void set_fork_generation_number_FOR_TESTING(uint64_t fork_gn) {
CRYPTO_STATIC_MUTEX_lock_write(&ube_testing_lock);
override_fork_generation_number = fork_gn;
CRYPTO_STATIC_MUTEX_unlock_write(&ube_testing_lock);
}
static uint32_t override_snapsafe_generation_number = 0;
void set_snapsafe_generation_number_FOR_TESTING(uint32_t snapsafe_gn) {
CRYPTO_STATIC_MUTEX_lock_write(&ube_testing_lock);
override_snapsafe_generation_number = snapsafe_gn;
CRYPTO_STATIC_MUTEX_unlock_write(&ube_testing_lock);
}
static int get_snapsafe_generation_number(uint32_t *gn) {
if (allow_mocked_detection == 1) {
CRYPTO_STATIC_MUTEX_lock_read(&ube_testing_lock);
*gn = override_snapsafe_generation_number;
CRYPTO_STATIC_MUTEX_unlock_read(&ube_testing_lock);
return 1;
}
return CRYPTO_get_snapsafe_generation(gn);
}
static int get_fork_generation_number(uint64_t *gn) {
if (allow_mocked_detection == 1) {
CRYPTO_STATIC_MUTEX_lock_read(&ube_testing_lock);
*gn = override_fork_generation_number;
CRYPTO_STATIC_MUTEX_unlock_read(&ube_testing_lock);
return 1;
}
uint64_t fork_gn = CRYPTO_get_fork_generation();
if (fork_gn == 0) {
return 0;
}
*gn = fork_gn;
return 1;
}
struct ube_state {
uint64_t generation_number;
uint64_t cached_fork_gn;
uint32_t cached_snapsafe_gn;
};
static struct ube_state ube_global_state = { 0, 0, 0 };
struct detection_gn {
#define NUMBER_OF_DETECTION_GENERATION_NUMBERS 2
uint64_t current_fork_gn;
uint32_t current_snapsafe_gn;
};
static void set_ube_detection_unavailable_once(void) {
ube_detection_unavailable = 1;
}
static void ube_failed(void) {
CRYPTO_once(&ube_detection_unavailable_once, set_ube_detection_unavailable_once);
}
static void ube_state_initialize(void) {
ube_global_state.generation_number = 0;
int ret_fork_gn = get_fork_generation_number(
&(ube_global_state.cached_fork_gn));
int ret_snapsafe_gn = get_snapsafe_generation_number(
&(ube_global_state.cached_snapsafe_gn));
if (ret_fork_gn == 0 || ret_snapsafe_gn == 0) {
ube_failed();
}
}
static void ube_update_state(struct detection_gn *current_detection_gn) {
ube_global_state.generation_number += 1;
ube_global_state.cached_fork_gn = current_detection_gn->current_fork_gn;
ube_global_state.cached_snapsafe_gn = current_detection_gn->current_snapsafe_gn;
}
static int ube_get_detection_generation_numbers(
struct detection_gn *current_detection_gn) {
OPENSSL_STATIC_ASSERT(NUMBER_OF_DETECTION_GENERATION_NUMBERS == 2,
not_handling_the_exact_number_of_detection_generation_numbers);
int ret_detect_gn = get_fork_generation_number(
&(current_detection_gn->current_fork_gn));
int ret_snapsafe_gn = get_snapsafe_generation_number(
&(current_detection_gn->current_snapsafe_gn));
if (ret_detect_gn == 0 || ret_snapsafe_gn == 0) {
return 0;
}
return 1;
}
static int ube_is_detected(struct detection_gn *current_detection_gn) {
if (ube_global_state.cached_fork_gn != current_detection_gn->current_fork_gn ||
ube_global_state.cached_snapsafe_gn != current_detection_gn->current_snapsafe_gn) {
return 1;
}
return 0;
}
int CRYPTO_get_ube_generation_number(uint64_t *current_generation_number) {
GUARD_PTR(current_generation_number);
CRYPTO_once(&ube_state_initialize_once, ube_state_initialize);
if (ube_detection_unavailable == 1 &&
allow_mocked_detection != 1) {
return 0;
}
struct detection_gn current_detection_gn = { 0, 0 };
if (ube_get_detection_generation_numbers(¤t_detection_gn) != 1) {
ube_failed();
return 0;
}
CRYPTO_STATIC_MUTEX_lock_read(&ube_lock);
if (ube_is_detected(¤t_detection_gn) == 0) {
*current_generation_number = ube_global_state.generation_number;
CRYPTO_STATIC_MUTEX_unlock_read(&ube_lock);
return 1;
}
CRYPTO_STATIC_MUTEX_unlock_read(&ube_lock);
CRYPTO_STATIC_MUTEX_lock_write(&ube_lock);
if (ube_get_detection_generation_numbers(¤t_detection_gn) != 1) {
ube_failed();
CRYPTO_STATIC_MUTEX_unlock_write(&ube_lock);
return 0;
}
if (ube_is_detected(¤t_detection_gn) == 0) {
*current_generation_number = ube_global_state.generation_number;
CRYPTO_STATIC_MUTEX_unlock_write(&ube_lock);
return 1;
}
ube_update_state(¤t_detection_gn);
*current_generation_number = ube_global_state.generation_number;
CRYPTO_STATIC_MUTEX_unlock_write(&ube_lock);
return 1;
}
void allow_mocked_ube_detection_FOR_TESTING(void) {
CRYPTO_STATIC_MUTEX_lock_write(&ube_testing_lock);
allow_mocked_detection = 1;
CRYPTO_STATIC_MUTEX_unlock_write(&ube_testing_lock);
}
void disable_mocked_ube_detection_FOR_TESTING(void) {
CRYPTO_STATIC_MUTEX_lock_write(&ube_testing_lock);
allow_mocked_detection = 0;
CRYPTO_STATIC_MUTEX_unlock_write(&ube_testing_lock);
set_fork_generation_number_FOR_TESTING(0);
set_snapsafe_generation_number_FOR_TESTING(0);
}