ro: update for R_TRY usage

This commit is contained in:
Michael Scire 2019-06-17 14:46:18 -07:00
parent dfcba5e6d4
commit 876d94c338
5 changed files with 96 additions and 181 deletions

@ -1 +1 @@
Subproject commit afcd075354dec43fae882c3ad4d5220336231d04
Subproject commit b5f1ec02b30a3bbf8a8419f8b0cf3646071ebd2b

View file

@ -13,7 +13,7 @@
* 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 <cstdio>
@ -22,7 +22,7 @@
bool MapUtils::CanAddGuardRegions(Handle process_handle, u64 address, u64 size) {
MemoryInfo mem_info;
u32 page_info;
/* Nintendo doesn't validate SVC return values at all. */
/* TODO: Should we allow these to fail? */
if (R_FAILED(svcQueryProcessMemory(&mem_info, &page_info, process_handle, address - 1))) {
@ -34,7 +34,7 @@ bool MapUtils::CanAddGuardRegions(Handle process_handle, u64 address, u64 size)
}
return mem_info.type == MemType_Unmapped && address + size + GuardRegionSize <= mem_info.addr + mem_info.size;
}
return false;
}
@ -46,12 +46,15 @@ Result MapUtils::LocateSpaceForMap(u64 *out, u64 out_size) {
}
}
Result MapUtils::MapCodeMemoryForProcess(MappedCodeMemory &out_mcm, Handle process_handle, bool is_64_bit, u64 base_address, u64 size) {
Result MapUtils::MapCodeMemoryForProcess(MappedCodeMemory &out_mcm, Handle process_handle, u64 base_address, u64 size) {
if (GetRuntimeFirmwareVersion() >= FirmwareVersion_200) {
return MapCodeMemoryForProcessModern(out_mcm, process_handle, base_address, size);
} else {
return MapCodeMemoryForProcessDeprecated(out_mcm, process_handle, is_64_bit, base_address, size);
Result rc = MapCodeMemoryForProcessDeprecated(out_mcm, process_handle, true, base_address, size);
if (R_FAILED(rc)) {
rc = MapCodeMemoryForProcessDeprecated(out_mcm, process_handle, false, base_address, size);
}
return rc;
}
}
@ -60,31 +63,27 @@ Result MapUtils::LocateSpaceForMapModern(u64 *out, u64 out_size) {
AddressSpaceInfo address_space = {};
u32 page_info = 0;
u64 cur_base = 0, cur_end = 0;
Result rc;
if (R_FAILED((rc = GetAddressSpaceInfo(&address_space, CUR_PROCESS_HANDLE)))) {
return rc;
}
R_TRY(GetAddressSpaceInfo(&address_space, CUR_PROCESS_HANDLE));
cur_base = address_space.addspace_base;
rc = ResultKernelOutOfMemory;
cur_end = cur_base + out_size;
if (cur_end <= cur_base) {
return rc;
return ResultKernelOutOfMemory;
}
while (true) {
if (address_space.heap_size && (address_space.heap_base <= cur_end - 1 && cur_base <= address_space.heap_end - 1)) {
/* If we overlap the heap region, go to the end of the heap region. */
if (cur_base == address_space.heap_end) {
return rc;
return ResultKernelOutOfMemory;
}
cur_base = address_space.heap_end;
} else if (address_space.map_size && (address_space.map_base <= cur_end - 1 && cur_base <= address_space.map_end - 1)) {
/* If we overlap the map region, go to the end of the map region. */
if (cur_base == address_space.map_end) {
return rc;
return ResultKernelOutOfMemory;
}
cur_base = address_space.map_end;
} else {
@ -96,67 +95,52 @@ Result MapUtils::LocateSpaceForMapModern(u64 *out, u64 out_size) {
return ResultSuccess;
}
if (mem_info.addr + mem_info.size <= cur_base) {
return rc;
return ResultKernelOutOfMemory;
}
cur_base = mem_info.addr + mem_info.size;
if (cur_base >= address_space.addspace_end) {
return rc;
return ResultKernelOutOfMemory;
}
}
cur_end = cur_base + out_size;
if (cur_base + out_size <= cur_base) {
return rc;
return ResultKernelOutOfMemory;
}
}
}
}
Result MapUtils::LocateSpaceForMapDeprecated(u64 *out, u64 out_size) {
MemoryInfo mem_info = {};
u32 page_info = 0;
Result rc;
u64 cur_base = 0x8000000ULL;
if (R_FAILED((rc = svcQueryMemory(&mem_info, &page_info, cur_base)))) {
return rc;
}
rc = ResultKernelOutOfMemory;
while (true) {
if (mem_info.type == 0x10) {
return rc;
}
do {
R_TRY(svcQueryMemory(&mem_info, &page_info, cur_base));
if (mem_info.type == 0 && mem_info.addr - cur_base + mem_info.size >= out_size) {
*out = cur_base;
return ResultSuccess;
}
u64 mem_end = mem_info.addr + mem_info.size;
if (mem_end < cur_base) {
return rc;
}
if (mem_end >> 31) {
break;
const u64 mem_end = mem_info.addr + mem_info.size;
if (mem_info.type == 0x10 || mem_end < cur_base || (mem_end >> 31)) {
return ResultKernelOutOfMemory;
}
cur_base = mem_end;
if (R_FAILED((rc = svcQueryMemory(&mem_info, &page_info, cur_base)))) {
return rc;
}
}
return rc;
} while (true);
}
Result MapUtils::MapCodeMemoryForProcessModern(MappedCodeMemory &out_mcm, Handle process_handle, u64 base_address, u64 size) {
AddressSpaceInfo address_space = {};
Result rc;
if (R_FAILED((rc = GetAddressSpaceInfo(&address_space, process_handle)))) {
return rc;
}
R_TRY(GetAddressSpaceInfo(&address_space, process_handle));
if (size > address_space.addspace_size) {
return ResultRoInsufficientAddressSpace;
}
u64 try_address;
for (unsigned int i = 0; i < LocateRetryCount; i++) {
while (true) {
@ -170,28 +154,26 @@ Result MapUtils::MapCodeMemoryForProcessModern(MappedCodeMemory &out_mcm, Handle
break;
}
MappedCodeMemory tmp_mcm(process_handle, try_address, base_address, size);
rc = tmp_mcm.GetResult();
if (rc == ResultKernelInvalidMemoryState) {
continue;
}
if (R_FAILED(rc)) {
return rc;
}
R_TRY_CATCH(tmp_mcm.GetResult()) {
R_CATCH(ResultKernelInvalidMemoryState) {
continue;
}
} R_END_TRY_CATCH;
if (!CanAddGuardRegions(process_handle, try_address, size)) {
continue;
}
/* We're done searching. */
out_mcm = std::move(tmp_mcm);
return ResultSuccess;
}
return ResultRoInsufficientAddressSpace;
}
Result MapUtils::MapCodeMemoryForProcessDeprecated(MappedCodeMemory &out_mcm, Handle process_handle, bool is_64_bit, u64 base_address, u64 size) {
Result rc;
u64 addspace_base, addspace_size;
if (is_64_bit) {
addspace_base = 0x8000000ULL;
@ -200,7 +182,7 @@ Result MapUtils::MapCodeMemoryForProcessDeprecated(MappedCodeMemory &out_mcm, Ha
addspace_base = 0x200000ULL;
addspace_size = 0x3FE0000ULL;
}
if (size > addspace_size) {
return ResultRoInsufficientAddressSpace;
}
@ -210,48 +192,35 @@ Result MapUtils::MapCodeMemoryForProcessDeprecated(MappedCodeMemory &out_mcm, Ha
try_address = addspace_base + (StratosphereRandomUtils::GetRandomU64((u64)(addspace_size - size) >> 12) << 12);
MappedCodeMemory tmp_mcm(process_handle, try_address, base_address, size);
rc = tmp_mcm.GetResult();
if (rc == ResultKernelInvalidMemoryState) {
continue;
}
if (R_FAILED(rc)) {
return rc;
}
R_TRY_CATCH(tmp_mcm.GetResult()) {
R_CATCH(ResultKernelInvalidMemoryState) {
continue;
}
} R_END_TRY_CATCH;
if (!CanAddGuardRegions(process_handle, try_address, size)) {
continue;
}
/* We're done searching. */
out_mcm = std::move(tmp_mcm);
return ResultSuccess;
}
return ResultRoInsufficientAddressSpace;
}
Result MapUtils::GetAddressSpaceInfo(AddressSpaceInfo *out, Handle process_h) {
Result rc;
if (R_FAILED((rc = svcGetInfo(&out->heap_base, 4, process_h, 0)))) {
return rc;
}
if (R_FAILED((rc = svcGetInfo(&out->heap_size, 5, process_h, 0)))) {
return rc;
}
if (R_FAILED((rc = svcGetInfo(&out->map_base, 2, process_h, 0)))) {
return rc;
}
if (R_FAILED((rc = svcGetInfo(&out->map_size, 3, process_h, 0)))) {
return rc;
}
if (R_FAILED((rc = svcGetInfo(&out->addspace_base, 12, process_h, 0)))) {
return rc;
}
if (R_FAILED((rc = svcGetInfo(&out->addspace_size, 13, process_h, 0)))) {
return rc;
}
R_TRY(svcGetInfo(&out->heap_base, 4, process_h, 0));
R_TRY(svcGetInfo(&out->heap_size, 5, process_h, 0));
R_TRY(svcGetInfo(&out->map_base, 2, process_h, 0));
R_TRY(svcGetInfo(&out->map_size, 3, process_h, 0));
R_TRY(svcGetInfo(&out->addspace_base, 12, process_h, 0));
R_TRY(svcGetInfo(&out->addspace_size, 13, process_h, 0));
out->heap_end = out->heap_base + out->heap_size;
out->map_end = out->map_base + out->map_size;
out->addspace_end = out->addspace_base + out->addspace_size;
return ResultSuccess;
}
}

View file

@ -131,6 +131,6 @@ class MapUtils {
static Result MapCodeMemoryForProcessModern(MappedCodeMemory &out_mcm, Handle process_handle, u64 base_address, u64 size);
public:
static Result LocateSpaceForMap(u64 *out, u64 out_size);
static Result MapCodeMemoryForProcess(MappedCodeMemory &out_mcm, Handle process_handle, bool is_64_bit, u64 base_address, u64 size);
static Result MapCodeMemoryForProcess(MappedCodeMemory &out_mcm, Handle process_handle, u64 base_address, u64 size);
static bool CanAddGuardRegions(Handle process_handle, u64 address, u64 size);
};

View file

@ -13,7 +13,7 @@
* 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 <cstdio>
#include <algorithm>
@ -24,22 +24,22 @@
Result NrrUtils::ValidateNrrSignature(const NrrHeader *header) {
/* TODO: Implement RSA-2048 PSS..... */
/* TODO: Check PSS fixed-key signature. */
if (false) {
return ResultRoNotAuthorized;
}
/* Check TitleID pattern is valid. */
if ((header->title_id & header->title_id_mask) != header->title_id_pattern) {
return ResultRoNotAuthorized;
}
/* TODO: Check PSS signature over hashes. */
if (false) {
return ResultRoNotAuthorized;
}
return ResultSuccess;
}
@ -50,24 +50,21 @@ Result NrrUtils::ValidateNrr(const NrrHeader *header, u64 size, u64 title_id, Ro
if (header->nrr_size != size) {
return ResultRoInvalidSize;
}
bool ease_nro_restriction = Registration::ShouldEaseNroRestriction();
/* Check signature. */
Result rc = ValidateNrrSignature(header);
if (R_FAILED(rc)) {
if (!ease_nro_restriction) {
return rc;
}
if (!ease_nro_restriction) {
R_TRY(ValidateNrrSignature(header));
}
/* Check title id. */
if (title_id != header->title_id) {
if (!ease_nro_restriction) {
return ResultRoInvalidNrr;
}
}
/* Check type. */
if (GetRuntimeFirmwareVersion() >= FirmwareVersion_700) {
if (!enforce_type || expected_type != static_cast<RoModuleType>(header->nrr_type)) {
@ -76,6 +73,6 @@ Result NrrUtils::ValidateNrr(const NrrHeader *header, u64 size, u64 title_id, Ro
}
}
}
return ResultSuccess;
}

View file

@ -151,10 +151,7 @@ Result Registration::LoadNrr(RoProcessContext *context, u64 title_id, u64 nrr_ad
/* Map. */
NrrHeader *header = nullptr;
u64 mapped_code_address = 0;
Result rc = MapAndValidateNrr(&header, &mapped_code_address, context->process_handle, title_id, nrr_address, nrr_size, expected_type, enforce_type);
if (R_FAILED(rc)) {
return rc;
}
R_TRY(MapAndValidateNrr(&header, &mapped_code_address, context->process_handle, title_id, nrr_address, nrr_size, expected_type, enforce_type));
/* Set NRR info. */
nrr_info->header = header;
@ -237,23 +234,18 @@ Result Registration::LoadNro(u64 *out_address, RoProcessContext *context, u64 nr
nro_info->bss_heap_size = bss_size;
/* Map the NRO. */
Result rc = MapNro(&nro_info->base_address, context->process_handle, nro_address, nro_size, bss_address, bss_size);
if (R_FAILED(rc)) {
return rc;
}
R_TRY(MapNro(&nro_info->base_address, context->process_handle, nro_address, nro_size, bss_address, bss_size));
/* Validate the NRO (parsing region extents). */
u64 rx_size, ro_size, rw_size;
if (R_FAILED((rc = ValidateNro(&nro_info->module_id, &rx_size, &ro_size, &rw_size, context, nro_info->base_address, nro_size, bss_size)))) {
R_TRY_CLEANUP(ValidateNro(&nro_info->module_id, &rx_size, &ro_size, &rw_size, context, nro_info->base_address, nro_size, bss_size), {
UnmapNro(context->process_handle, nro_info->base_address, nro_address, bss_address, bss_size, nro_size, 0);
return rc;
}
});
/* Set NRO perms. */
if (R_FAILED((rc = SetNroPerms(context->process_handle, nro_info->base_address, rx_size, ro_size, rw_size + bss_size)))) {
R_TRY_CLEANUP(SetNroPerms(context->process_handle, nro_info->base_address, rx_size, ro_size, rw_size + bss_size), {
UnmapNro(context->process_handle, nro_info->base_address, nro_address, bss_address, bss_size, rx_size + ro_size, rw_size);
return rc;
}
});
nro_info->code_size = rx_size + ro_size;
nro_info->rw_size = rw_size;
@ -337,20 +329,13 @@ Result Registration::ValidateNro(ModuleId *out_module_id, u64 *out_rx_size, u64
}
Result Registration::SetNroPerms(Handle process_handle, u64 base_address, u64 rx_size, u64 ro_size, u64 rw_size) {
Result rc;
const u64 rx_offset = 0;
const u64 ro_offset = rx_offset + rx_size;
const u64 rw_offset = ro_offset + ro_size;
if (R_FAILED((rc = svcSetProcessMemoryPermission(process_handle, base_address + rx_offset, rx_size, 5)))) {
return rc;
}
if (R_FAILED((rc = svcSetProcessMemoryPermission(process_handle, base_address + ro_offset, ro_size, 1)))) {
return rc;
}
if (R_FAILED((rc = svcSetProcessMemoryPermission(process_handle, base_address + rw_offset, rw_size, 3)))) {
return rc;
}
R_TRY(svcSetProcessMemoryPermission(process_handle, base_address + rx_offset, rx_size, Perm_Rx));
R_TRY(svcSetProcessMemoryPermission(process_handle, base_address + ro_offset, ro_size, Perm_R ));
R_TRY(svcSetProcessMemoryPermission(process_handle, base_address + rw_offset, rw_size, Perm_Rw));
return ResultSuccess;
}
@ -387,19 +372,10 @@ Result Registration::UnloadNro(RoProcessContext *context, u64 nro_address) {
}
Result Registration::MapAndValidateNrr(NrrHeader **out_header, u64 *out_mapped_code_address, Handle process_handle, u64 title_id, u64 nrr_heap_address, u64 nrr_heap_size, RoModuleType expected_type, bool enforce_type) {
Result rc;
MappedCodeMemory nrr_mcm;
/* First, map the NRR. */
if (R_FAILED((rc = MapUtils::MapCodeMemoryForProcess(nrr_mcm, process_handle, true, nrr_heap_address, nrr_heap_size)))) {
if (GetRuntimeFirmwareVersion() < FirmwareVersion_300) {
/* Try mapping as 32-bit, since we might have guessed wrong on < 3.0.0. */
rc = MapUtils::MapCodeMemoryForProcess(nrr_mcm, process_handle, false, nrr_heap_address, nrr_heap_size);
}
if (R_FAILED(rc)) {
return rc;
}
}
R_TRY(MapUtils::MapCodeMemoryForProcess(nrr_mcm, process_handle, nrr_heap_address, nrr_heap_size));
const u64 code_address = nrr_mcm.GetDstAddress();
u64 map_address;
@ -414,9 +390,7 @@ Result Registration::MapAndValidateNrr(NrrHeader **out_header, u64 *out_mapped_c
}
NrrHeader *nrr_header = reinterpret_cast<NrrHeader *>(map_address);
if (R_FAILED((rc = NrrUtils::ValidateNrr(nrr_header, nrr_heap_size, title_id, expected_type, enforce_type)))) {
return rc;
}
R_TRY(NrrUtils::ValidateNrr(nrr_header, nrr_heap_size, title_id, expected_type, enforce_type));
/* Invalidation here actually prevents them from unmapping at scope exit. */
nrr_map.Invalidate();
@ -428,16 +402,11 @@ Result Registration::MapAndValidateNrr(NrrHeader **out_header, u64 *out_mapped_c
}
Result Registration::UnmapNrr(Handle process_handle, const NrrHeader *header, u64 nrr_heap_address, u64 nrr_heap_size, u64 mapped_code_address) {
Result rc = svcUnmapProcessMemory((void *)header, process_handle, mapped_code_address, nrr_heap_size);
if (R_FAILED(rc)) {
return rc;
}
R_TRY(svcUnmapProcessMemory((void *)header, process_handle, mapped_code_address, nrr_heap_size));
return svcUnmapProcessCodeMemory(process_handle, mapped_code_address, nrr_heap_address, nrr_heap_size);
}
Result Registration::MapNro(u64 *out_base_address, Handle process_handle, u64 nro_heap_address, u64 nro_heap_size, u64 bss_heap_address, u64 bss_heap_size) {
Result rc;
MappedCodeMemory nro_mcm;
MappedCodeMemory bss_mcm;
u64 base_address;
@ -446,28 +415,16 @@ Result Registration::MapNro(u64 *out_base_address, Handle process_handle, u64 nr
size_t i = 0;
for (i = 0; i < MapUtils::LocateRetryCount; i++) {
MappedCodeMemory tmp_nro_mcm;
bool is_64_bit = true;
if (R_FAILED((rc = MapUtils::MapCodeMemoryForProcess(tmp_nro_mcm, process_handle, is_64_bit, nro_heap_address, nro_heap_size)))) {
if (GetRuntimeFirmwareVersion() < FirmwareVersion_300) {
/* Try mapping as 32-bit, since we might have guessed wrong on < 3.0.0. */
is_64_bit = false;
rc = MapUtils::MapCodeMemoryForProcess(tmp_nro_mcm, process_handle, is_64_bit, nro_heap_address, nro_heap_size);
}
if (R_FAILED(rc)) {
return rc;
}
}
R_TRY(MapUtils::MapCodeMemoryForProcess(tmp_nro_mcm, process_handle, nro_heap_address, nro_heap_size));
base_address = tmp_nro_mcm.GetDstAddress();
if (bss_heap_size > 0) {
MappedCodeMemory tmp_bss_mcm(process_handle, base_address + nro_heap_size, bss_heap_address, bss_heap_size);
rc = tmp_bss_mcm.GetResult();
if (rc == ResultKernelInvalidMemoryState) {
continue;
}
if (R_FAILED(rc)) {
return rc;
}
R_TRY_CATCH(tmp_bss_mcm.GetResult()) {
R_CATCH(ResultKernelInvalidMemoryState) {
continue;
}
} R_END_TRY_CATCH;
if (!MapUtils::CanAddGuardRegions(process_handle, base_address, nro_heap_size + bss_heap_size)) {
continue;
@ -495,26 +452,18 @@ Result Registration::MapNro(u64 *out_base_address, Handle process_handle, u64 nr
}
Result Registration::UnmapNro(Handle process_handle, u64 base_address, u64 nro_heap_address, u64 bss_heap_address, u64 bss_heap_size, u64 code_size, u64 rw_size) {
Result rc;
/* First, unmap bss. */
if (bss_heap_size > 0) {
if (R_FAILED((rc = svcUnmapProcessCodeMemory(process_handle, base_address + code_size + rw_size, bss_heap_address, bss_heap_size)))) {
return rc;
}
R_TRY(svcUnmapProcessCodeMemory(process_handle, base_address + code_size + rw_size, bss_heap_address, bss_heap_size));
}
/* Next, unmap .rwdata */
if (rw_size > 0) {
if (R_FAILED((rc = svcUnmapProcessCodeMemory(process_handle, base_address + code_size, nro_heap_address + code_size, rw_size)))) {
return rc;
}
R_TRY(svcUnmapProcessCodeMemory(process_handle, base_address + code_size, nro_heap_address + code_size, rw_size));
}
/* Finally, unmap .text + .rodata. */
if (R_FAILED((rc = svcUnmapProcessCodeMemory(process_handle, base_address, nro_heap_address, code_size)))) {
return rc;
}
R_TRY(svcUnmapProcessCodeMemory(process_handle, base_address, nro_heap_address, code_size));
return ResultSuccess;
}