2019-06-24 11:05:51 +02:00
|
|
|
/*
|
2021-10-04 21:59:10 +02:00
|
|
|
* Copyright (c) Atmosphère-NX
|
2019-06-24 11:05:51 +02:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2020-05-12 00:02:10 +02:00
|
|
|
#include <stratosphere.hpp>
|
2019-06-24 11:05:51 +02:00
|
|
|
#include "ro_nro_utils.hpp"
|
2021-10-05 21:22:34 +02:00
|
|
|
#include "ro_map_utils.hpp"
|
2019-06-24 11:05:51 +02:00
|
|
|
|
2019-10-24 11:30:10 +02:00
|
|
|
namespace ams::ro::impl {
|
2019-06-24 11:05:51 +02:00
|
|
|
|
2021-10-04 21:33:09 +02:00
|
|
|
Result MapNro(u64 *out_base_address, os::NativeHandle process_handle, u64 nro_heap_address, u64 nro_heap_size, u64 bss_heap_address, u64 bss_heap_size) {
|
2021-10-05 21:22:34 +02:00
|
|
|
/* Re-map the NRO/BSS as code memory in the destination process. */
|
|
|
|
MappedCodeMemory nro_mcm;
|
|
|
|
MappedCodeMemory bss_mcm;
|
|
|
|
ProcessRegionInfo region_info(process_handle);
|
2019-06-24 11:05:51 +02:00
|
|
|
u64 base_address;
|
2021-10-05 21:22:34 +02:00
|
|
|
{
|
|
|
|
const u64 memory_size = nro_heap_size + bss_heap_size;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < RetrySearchCount; ++i) {
|
|
|
|
/* Get a random address for the nro. */
|
|
|
|
base_address = region_info.GetAslrRegion(memory_size);
|
|
|
|
R_UNLESS(base_address != 0, ro::ResultOutOfAddressSpace());
|
|
|
|
|
|
|
|
/* Map the NRO, retrying if random address was invalid. */
|
|
|
|
MappedCodeMemory tmp_nro_mcm(process_handle, base_address, nro_heap_address, nro_heap_size);
|
|
|
|
R_TRY_CATCH(tmp_nro_mcm.GetResult()) {
|
|
|
|
R_CATCH(svc::ResultInvalidCurrentMemory) { continue; }
|
|
|
|
} R_END_TRY_CATCH;
|
2019-06-24 11:05:51 +02:00
|
|
|
|
2021-10-05 21:22:34 +02:00
|
|
|
/* Handle bss. */
|
|
|
|
if (bss_heap_size > 0) {
|
|
|
|
/* Map BSS, retrying if random address was invalid. */
|
|
|
|
MappedCodeMemory tmp_bss_mcm(process_handle, base_address + nro_heap_size, bss_heap_address, bss_heap_size);
|
|
|
|
R_TRY_CATCH(tmp_bss_mcm.GetResult()) {
|
|
|
|
R_CATCH(svc::ResultInvalidCurrentMemory) { continue; }
|
|
|
|
} R_END_TRY_CATCH;
|
2019-06-24 11:05:51 +02:00
|
|
|
|
2021-10-05 21:22:34 +02:00
|
|
|
/* Check that we can have guard spaces. */
|
|
|
|
if (!region_info.CanEmplaceGuardSpaces(process_handle, base_address, memory_size)) {
|
2019-06-24 11:05:51 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-10-05 21:22:34 +02:00
|
|
|
/* We succeeded, so save the bss memory. */
|
|
|
|
bss_mcm = std::move(tmp_bss_mcm);
|
|
|
|
} else {
|
|
|
|
/* Check that we can have guard spaces. */
|
|
|
|
if (!region_info.CanEmplaceGuardSpaces(process_handle, base_address, memory_size)) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-06-24 11:05:51 +02:00
|
|
|
}
|
|
|
|
|
2021-10-05 21:22:34 +02:00
|
|
|
/* We succeeded, so save the code memory. */
|
|
|
|
nro_mcm = std::move(tmp_nro_mcm);
|
|
|
|
break;
|
2019-06-24 11:05:51 +02:00
|
|
|
}
|
2021-10-05 21:22:34 +02:00
|
|
|
|
|
|
|
R_UNLESS(i != RetrySearchCount, ro::ResultOutOfAddressSpace());
|
2019-06-24 11:05:51 +02:00
|
|
|
}
|
|
|
|
|
2021-10-05 21:22:34 +02:00
|
|
|
/* Cancel the automatic closing of our mappings. */
|
|
|
|
nro_mcm.Cancel();
|
|
|
|
bss_mcm.Cancel();
|
2019-06-24 11:05:51 +02:00
|
|
|
|
|
|
|
*out_base_address = base_address;
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-24 11:05:51 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 21:33:09 +02:00
|
|
|
Result SetNroPerms(os::NativeHandle process_handle, u64 base_address, u64 rx_size, u64 ro_size, u64 rw_size) {
|
2019-06-24 11:05:51 +02:00
|
|
|
const u64 rx_offset = 0;
|
|
|
|
const u64 ro_offset = rx_offset + rx_size;
|
|
|
|
const u64 rw_offset = ro_offset + ro_size;
|
|
|
|
|
2021-10-04 23:54:13 +02:00
|
|
|
R_TRY(svc::SetProcessMemoryPermission(process_handle, base_address + rx_offset, rx_size, svc::MemoryPermission_ReadExecute));
|
|
|
|
R_TRY(svc::SetProcessMemoryPermission(process_handle, base_address + ro_offset, ro_size, svc::MemoryPermission_Read));
|
|
|
|
R_TRY(svc::SetProcessMemoryPermission(process_handle, base_address + rw_offset, rw_size, svc::MemoryPermission_ReadWrite));
|
2019-06-24 11:05:51 +02:00
|
|
|
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-24 11:05:51 +02:00
|
|
|
}
|
|
|
|
|
2021-10-04 21:33:09 +02:00
|
|
|
Result UnmapNro(os::NativeHandle process_handle, u64 base_address, u64 nro_heap_address, u64 bss_heap_address, u64 bss_heap_size, u64 code_size, u64 rw_size) {
|
2019-06-24 11:05:51 +02:00
|
|
|
/* First, unmap bss. */
|
|
|
|
if (bss_heap_size > 0) {
|
2021-10-04 23:54:13 +02:00
|
|
|
R_TRY(svc::UnmapProcessCodeMemory(process_handle, base_address + code_size + rw_size, bss_heap_address, bss_heap_size));
|
2019-06-24 11:05:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Next, unmap .rwdata */
|
|
|
|
if (rw_size > 0) {
|
2021-10-04 23:54:13 +02:00
|
|
|
R_TRY(svc::UnmapProcessCodeMemory(process_handle, base_address + code_size, nro_heap_address + code_size, rw_size));
|
2019-06-24 11:05:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally, unmap .text + .rodata. */
|
2021-10-04 23:54:13 +02:00
|
|
|
R_TRY(svc::UnmapProcessCodeMemory(process_handle, base_address, nro_heap_address, code_size));
|
2019-06-24 11:05:51 +02:00
|
|
|
|
2019-10-24 10:40:44 +02:00
|
|
|
return ResultSuccess();
|
2019-06-24 11:05:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|