mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2024-11-23 04:12:02 +00:00
loader: use libstratosphere randomness
This commit is contained in:
parent
87f7a6ebdc
commit
5c9d0f05b1
6 changed files with 4 additions and 97 deletions
|
@ -1 +1 @@
|
||||||
Subproject commit 163d9259a365c3a89c1a837b28e7609d45795e40
|
Subproject commit 880bce9092fbef0bcbf101b8ec2e3d2c5af3fb98
|
|
@ -18,7 +18,6 @@
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
|
|
||||||
#include "ldr_map.hpp"
|
#include "ldr_map.hpp"
|
||||||
#include "ldr_random.hpp"
|
|
||||||
|
|
||||||
Result MapUtils::LocateSpaceForMap(u64 *out, u64 out_size) {
|
Result MapUtils::LocateSpaceForMap(u64 *out, u64 out_size) {
|
||||||
if (kernelAbove200()) {
|
if (kernelAbove200()) {
|
||||||
|
@ -142,7 +141,7 @@ Result MapUtils::MapCodeMemoryForProcessModern(Handle process_h, u64 base_addres
|
||||||
u64 try_address;
|
u64 try_address;
|
||||||
for (unsigned int i = 0; i < 0x200; i++) {
|
for (unsigned int i = 0; i < 0x200; i++) {
|
||||||
while (true) {
|
while (true) {
|
||||||
try_address = address_space.addspace_base + (RandomUtils::GetRandomU64((u64)(address_space.addspace_size - size) >> 12) << 12);
|
try_address = address_space.addspace_base + (StratosphereRandomUtils::GetRandomU64((u64)(address_space.addspace_size - size) >> 12) << 12);
|
||||||
if (address_space.heap_size && (address_space.heap_base <= try_address + size - 1 && try_address <= address_space.heap_end - 1)) {
|
if (address_space.heap_size && (address_space.heap_base <= try_address + size - 1 && try_address <= address_space.heap_end - 1)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -179,7 +178,7 @@ Result MapUtils::MapCodeMemoryForProcessDeprecated(Handle process_h, bool is_64_
|
||||||
|
|
||||||
u64 try_address;
|
u64 try_address;
|
||||||
for (unsigned int i = 0; i < 0x200; i++) {
|
for (unsigned int i = 0; i < 0x200; i++) {
|
||||||
try_address = addspace_base + (RandomUtils::GetRandomU64((u64)(addspace_size - size) >> 12) << 12);
|
try_address = addspace_base + (StratosphereRandomUtils::GetRandomU64((u64)(addspace_size - size) >> 12) << 12);
|
||||||
rc = svcMapProcessCodeMemory(process_h, try_address, base_address, size);
|
rc = svcMapProcessCodeMemory(process_h, try_address, base_address, size);
|
||||||
if (rc != ResultKernelInvalidMemoryState) {
|
if (rc != ResultKernelInvalidMemoryState) {
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -23,7 +23,6 @@
|
||||||
#include "ldr_nro.hpp"
|
#include "ldr_nro.hpp"
|
||||||
#include "ldr_registration.hpp"
|
#include "ldr_registration.hpp"
|
||||||
#include "ldr_map.hpp"
|
#include "ldr_map.hpp"
|
||||||
#include "ldr_random.hpp"
|
|
||||||
|
|
||||||
Result NroUtils::ValidateNrrHeader(NrrHeader *header, u64 size, u64 title_id_min) {
|
Result NroUtils::ValidateNrrHeader(NrrHeader *header, u64 size, u64 title_id_min) {
|
||||||
if (header->magic != MAGIC_NRR0) {
|
if (header->magic != MAGIC_NRR0) {
|
||||||
|
|
|
@ -21,7 +21,6 @@
|
||||||
#include "lz4.h"
|
#include "lz4.h"
|
||||||
#include "ldr_nso.hpp"
|
#include "ldr_nso.hpp"
|
||||||
#include "ldr_map.hpp"
|
#include "ldr_map.hpp"
|
||||||
#include "ldr_random.hpp"
|
|
||||||
#include "ldr_patcher.hpp"
|
#include "ldr_patcher.hpp"
|
||||||
#include "ldr_content_management.hpp"
|
#include "ldr_content_management.hpp"
|
||||||
|
|
||||||
|
@ -226,7 +225,7 @@ Result NsoUtils::CalculateNsoLoadExtents(u32 addspace_type, u32 args_size, NsoLo
|
||||||
|
|
||||||
u64 aslr_slide = 0;
|
u64 aslr_slide = 0;
|
||||||
if (addspace_type & 0x20) {
|
if (addspace_type & 0x20) {
|
||||||
aslr_slide = RandomUtils::GetRandomU64((addspace_size - extents->total_size) >> 21) << 21;
|
aslr_slide = StratosphereRandomUtils::GetRandomU64((addspace_size - extents->total_size) >> 21) << 21;
|
||||||
}
|
}
|
||||||
|
|
||||||
extents->base_address = addspace_start + aslr_slide;
|
extents->base_address = addspace_start + aslr_slide;
|
||||||
|
|
|
@ -1,65 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (c) 2018-2019 Atmosphère-NX
|
|
||||||
*
|
|
||||||
* This program is free software; you can redistribute it and/or modify it
|
|
||||||
* under the terms and conditions of the GNU General Public License,
|
|
||||||
* version 2, as published by the Free Software Foundation.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
||||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
||||||
* more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <switch.h>
|
|
||||||
|
|
||||||
#include "ldr_random.hpp"
|
|
||||||
|
|
||||||
/* Official HOS uses TinyMT. This is high effort. Let's just use XorShift. */
|
|
||||||
/* https://en.wikipedia.org/wiki/Xorshift */
|
|
||||||
|
|
||||||
static u32 g_random_state[4] = {0};
|
|
||||||
static bool g_has_initialized = false;
|
|
||||||
|
|
||||||
static void EnsureRandomState() {
|
|
||||||
if (g_has_initialized) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Retrieve process entropy with svcGetInfo. */
|
|
||||||
u64 val = 0;
|
|
||||||
for (unsigned int i = 0; i < 4; i++) {
|
|
||||||
if (R_FAILED(svcGetInfo(&val, 0xB, 0, i))) {
|
|
||||||
/* TODO: Panic? */
|
|
||||||
}
|
|
||||||
g_random_state[i] = val & 0xFFFFFFFF;
|
|
||||||
}
|
|
||||||
|
|
||||||
g_has_initialized = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
u32 RandomUtils::GetNext() {
|
|
||||||
EnsureRandomState();
|
|
||||||
u32 s, t = g_random_state[3];
|
|
||||||
t ^= t << 11;
|
|
||||||
t ^= t >> 8;
|
|
||||||
g_random_state[3] = g_random_state[2]; g_random_state[2] = g_random_state[1]; g_random_state[1] = (s = g_random_state[0]);
|
|
||||||
t ^= s;
|
|
||||||
t ^= s >> 19;
|
|
||||||
g_random_state[0] = t;
|
|
||||||
return t;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* These are slightly biased, but I think that's totally okay. */
|
|
||||||
u32 RandomUtils::GetRandomU32(u32 max) {
|
|
||||||
return GetNext() % max;
|
|
||||||
}
|
|
||||||
|
|
||||||
u64 RandomUtils::GetRandomU64(u64 max) {
|
|
||||||
u64 val = GetNext();
|
|
||||||
val |= ((u64)GetNext()) << 32;
|
|
||||||
return val % max;
|
|
||||||
}
|
|
|
@ -1,25 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright (c) 2018-2019 Atmosphère-NX
|
|
||||||
*
|
|
||||||
* This program is free software; you can redistribute it and/or modify it
|
|
||||||
* under the terms and conditions of the GNU General Public License,
|
|
||||||
* version 2, as published by the Free Software Foundation.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope it will be useful, but WITHOUT
|
|
||||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
||||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
||||||
* more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
#include <switch.h>
|
|
||||||
|
|
||||||
class RandomUtils {
|
|
||||||
public:
|
|
||||||
static u32 GetNext();
|
|
||||||
static u32 GetRandomU32(u32 max);
|
|
||||||
static u64 GetRandomU64(u64 max);
|
|
||||||
};
|
|
Loading…
Reference in a new issue