Skip to content

Commit

Permalink
Ranged random support
Browse files Browse the repository at this point in the history
  • Loading branch information
djphoenix committed May 19, 2019
1 parent b1ad924 commit 6e998c8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/kernlib/include/kernlib/rand.hpp
Expand Up @@ -10,4 +10,8 @@ class RAND {
static uint64_t _get64();
public:
template<typename T> static inline T get() { return static_cast<T>(_get64()); }
template<typename T> static inline T get(T max) { return get(0, max); }
template<typename T> static inline T get(T min, T max) {
return min + (get<T>() % (max - min));
}
};
4 changes: 2 additions & 2 deletions src/platform/pagetable.cpp
Expand Up @@ -102,7 +102,7 @@ void Pagetable::init() {
EFI::getImageHandle(), &EFI::GUID_LoadedImageProtocol,
reinterpret_cast<void**>(&loaded_image));

uintptr_t ptbase = 0x600000 + (RAND::get<uintptr_t>() & 0x3FFF000);
uintptr_t ptbase = RAND::get<uintptr_t>(0x800, 0x10000) << 12;

pagetable = static_cast<PTE*>(efiAllocatePage(ptbase, ST));
efiMapPage(pagetable, nullptr, ST, 0);
Expand Down Expand Up @@ -135,7 +135,7 @@ void Pagetable::init() {
static const size_t pdpe_num = 64;
static const size_t ptsz = (3 + pdpe_num) * 0x1000;

uintptr_t ptbase = 0x600000 - ptsz + ((RAND::get<uintptr_t>() & 0x3FFF) << 12);
uintptr_t ptbase = RAND::get<uintptr_t>(0x800, 0x8000 - (3 + pdpe_num)) << 12;

PTE *newpt = reinterpret_cast<PTE*>(ptbase);
Memory::fill(newpt, 0, ptsz);
Expand Down
4 changes: 2 additions & 2 deletions src/process/process.cpp
Expand Up @@ -12,8 +12,8 @@ Process::Process() {
id = size_t(-1);
pagetable = nullptr;
entry = 0;
_aslrCode = ((RAND::get<uintptr_t>() << 12) & 0x7FFFFFFF000) | 0x40000000000;
_aslrStack = ((RAND::get<uintptr_t>() << 12) & 0x7FFFFFFF000) | 0x80000000000;
_aslrCode = RAND::get<uintptr_t>(0x80000000llu, 0x100000000llu) << 12;
_aslrStack = RAND::get<uintptr_t>(0x40000000llu, 0x80000000llu) << 12;
}
Process::~Process() {
if (pagetable != nullptr) {
Expand Down

0 comments on commit 6e998c8

Please sign in to comment.