pmm: Add option to randomise memory contents at boot-up

This commit is contained in:
mintsuki 2021-07-09 14:40:44 +02:00
parent 757cc792fd
commit cd59bb5097
3 changed files with 42 additions and 0 deletions

View File

@ -96,6 +96,13 @@ void stage3_common(void) {
char *verbose_str = config_get_value(NULL, 0, "VERBOSE");
verbose = verbose_str != NULL && strcmp(verbose_str, "yes") == 0;
char *randomise_mem_str = config_get_value(NULL, 0, "RANDOMISE_MEMORY");
if (randomise_mem_str == NULL)
randomise_mem_str = config_get_value(NULL, 0, "RANDOMIZE_MEMORY");
bool randomise_mem = randomise_mem_str != NULL && strcmp(randomise_mem_str, "yes") == 0;
if (randomise_mem)
pmm_randomise_memory();
if (verbose) {
print("Boot drive: %x\n", boot_volume->index);
print("Boot partition: %d\n", boot_volume->partition);

34
stage23/mm/pmm.c Normal file
View File

@ -0,0 +1,34 @@
#include <mm/pmm.h>
#include <lib/rand.h>
#include <lib/print.h>
void pmm_randomise_memory(void) {
print("pmm: Randomising memory contents...");
for (size_t i = 0; i < memmap_entries; i++) {
if (memmap[i].type != MEMMAP_USABLE)
continue;
uint8_t *ptr = (void *)(uintptr_t)memmap[i].base;
size_t len = memmap[i].length;
for (size_t j = 0;;) {
uint32_t random = rand32();
uint8_t *rnd_data = (void *)&random;
if (j >= len)
break;
ptr[j++] = rnd_data[0];
if (j >= len)
break;
ptr[j++] = rnd_data[1];
if (j >= len)
break;
ptr[j++] = rnd_data[2];
if (j >= len)
break;
ptr[j++] = rnd_data[3];
}
}
print("\n");
}

View File

@ -30,6 +30,7 @@ struct e820_entry_t *get_memmap(size_t *entries);
struct e820_entry_t *get_raw_memmap(size_t *entry_count);
void print_memmap(struct e820_entry_t *mm, size_t size);
bool memmap_alloc_range(uint64_t base, uint64_t length, uint32_t type, bool free_only, bool panic, bool simulation, bool new_entry);
void pmm_randomise_memory(void);
void *ext_mem_alloc(size_t count);
void *ext_mem_alloc_type(size_t count, uint32_t type);