mirror of
https://github.com/Atmosphere-NX/Atmosphere.git
synced 2025-05-22 19:05:08 -04:00
loader: completely rewrite.
This commit is contained in:
parent
9217e4c5f9
commit
61fcf5e0f4
49 changed files with 2523 additions and 5817 deletions
|
@ -14,248 +14,698 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <switch.h>
|
||||
#include <algorithm>
|
||||
#include <stratosphere.hpp>
|
||||
#include <limits>
|
||||
#include <stratosphere/map.hpp>
|
||||
#include <stratosphere/rnd.hpp>
|
||||
#include <stratosphere/util.hpp>
|
||||
|
||||
#include "ldr_process_creation.hpp"
|
||||
#include "ldr_registration.hpp"
|
||||
#include "ldr_launch_queue.hpp"
|
||||
#include "ldr_capabilities.hpp"
|
||||
#include "ldr_content_management.hpp"
|
||||
#include "ldr_npdm.hpp"
|
||||
#include "ldr_nso.hpp"
|
||||
#include "ldr_launch_record.hpp"
|
||||
#include "ldr_meta.hpp"
|
||||
#include "ldr_patcher.hpp"
|
||||
#include "ldr_process_creation.hpp"
|
||||
#include "ldr_ro_manager.hpp"
|
||||
|
||||
/* TODO: Move into libstratosphere header? */
|
||||
namespace sts::svc {
|
||||
|
||||
namespace {
|
||||
|
||||
enum CreateProcessFlag : u32 {
|
||||
/* Is 64 bit? */
|
||||
CreateProcessFlag_Is64Bit = (1 << 0),
|
||||
|
||||
/* What kind of address space? */
|
||||
CreateProcessFlag_AddressSpaceShift = 1,
|
||||
CreateProcessFlag_AddressSpaceMask = (7 << CreateProcessFlag_AddressSpaceShift),
|
||||
CreateProcessFlag_AddressSpace32Bit = (ldr::Npdm::AddressSpaceType_32Bit << CreateProcessFlag_AddressSpaceShift),
|
||||
CreateProcessFlag_AddressSpace64BitDeprecated = (ldr::Npdm::AddressSpaceType_64BitDeprecated << CreateProcessFlag_AddressSpaceShift),
|
||||
CreateProcessFlag_AddressSpace32BitWithoutAlias = (ldr::Npdm::AddressSpaceType_32BitWithoutAlias << CreateProcessFlag_AddressSpaceShift),
|
||||
CreateProcessFlag_AddressSpace64Bit = (ldr::Npdm::AddressSpaceType_64Bit << CreateProcessFlag_AddressSpaceShift),
|
||||
|
||||
/* Should JIT debug be done on crash? */
|
||||
CreateProcessFlag_EnableDebug = (1 << 4),
|
||||
|
||||
/* Should ASLR be enabled for the process? */
|
||||
CreateProcessFlag_EnableAslr = (1 << 5),
|
||||
|
||||
/* Is the process an application? */
|
||||
CreateProcessFlag_IsApplication = (1 << 6),
|
||||
|
||||
/* 4.x deprecated: Should use secure memory? */
|
||||
CreateProcessFlag_DeprecatedUseSecureMemory = (1 << 7),
|
||||
|
||||
/* 5.x+ Pool partition type. */
|
||||
CreateProcessFlag_PoolPartitionShift = 7,
|
||||
CreateProcessFlag_PoolPartitionMask = (0xF << CreateProcessFlag_PoolPartitionShift),
|
||||
CreateProcessFlag_PoolPartitionApplication = (ldr::Acid::PoolPartition_Application << CreateProcessFlag_PoolPartitionShift),
|
||||
CreateProcessFlag_PoolPartitionApplet = (ldr::Acid::PoolPartition_Applet << CreateProcessFlag_PoolPartitionShift),
|
||||
CreateProcessFlag_PoolPartitionSystem = (ldr::Acid::PoolPartition_System << CreateProcessFlag_PoolPartitionShift),
|
||||
CreateProcessFlag_PoolPartitionSystemNonSecure = (ldr::Acid::PoolPartition_SystemNonSecure << CreateProcessFlag_PoolPartitionShift),
|
||||
|
||||
/* 7.x+ Should memory allocation be optimized? This requires IsApplication. */
|
||||
CreateProcessFlag_OptimizeMemoryAllocation = (1 << 11),
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
static inline bool IsDisallowedVersion810(const u64 title_id, const u32 version) {
|
||||
return version == 0 &&
|
||||
(title_id == TitleId_Settings ||
|
||||
title_id == TitleId_Bus ||
|
||||
title_id == TitleId_Audio ||
|
||||
title_id == TitleId_NvServices ||
|
||||
title_id == TitleId_Ns ||
|
||||
title_id == TitleId_Ssl ||
|
||||
title_id == TitleId_Es ||
|
||||
title_id == TitleId_Creport ||
|
||||
title_id == TitleId_Ro);
|
||||
}
|
||||
|
||||
Result ProcessCreation::ValidateProcessVersion(u64 title_id, u32 version) {
|
||||
if (GetRuntimeFirmwareVersion() < FirmwareVersion_810) {
|
||||
return ResultSuccess;
|
||||
} else {
|
||||
#ifdef LDR_VALIDATE_PROCESS_VERSION
|
||||
if (IsDisallowedVersion810(title_id, version)) {
|
||||
return ResultLoaderInvalidVersion;
|
||||
} else {
|
||||
namespace sts::ldr {
|
||||
|
||||
namespace {
|
||||
|
||||
/* Convenience defines. */
|
||||
constexpr size_t BaseAddressAlignment = 0x200000;
|
||||
constexpr size_t SystemResourceSizeAlignment = 0x200000;
|
||||
constexpr size_t SystemResourceSizeMax = 0x1FE00000;
|
||||
|
||||
/* Types. */
|
||||
enum NsoIndex {
|
||||
Nso_Rtld = 0,
|
||||
Nso_Main = 1,
|
||||
Nso_SubSdk0 = 2,
|
||||
Nso_SubSdk1 = 3,
|
||||
Nso_SubSdk2 = 4,
|
||||
Nso_SubSdk3 = 5,
|
||||
Nso_SubSdk4 = 6,
|
||||
Nso_SubSdk5 = 7,
|
||||
Nso_SubSdk6 = 8,
|
||||
Nso_SubSdk7 = 9,
|
||||
Nso_SubSdk8 = 10,
|
||||
Nso_SubSdk9 = 11,
|
||||
Nso_Sdk = 12,
|
||||
Nso_Count,
|
||||
};
|
||||
|
||||
constexpr const char *GetNsoName(size_t idx) {
|
||||
if (idx >= Nso_Count) {
|
||||
std::abort();
|
||||
}
|
||||
|
||||
constexpr const char *NsoNames[Nso_Count] = {
|
||||
"rtld",
|
||||
"main",
|
||||
"subsdk0",
|
||||
"subsdk1",
|
||||
"subsdk2",
|
||||
"subsdk3",
|
||||
"subsdk4",
|
||||
"subsdk5",
|
||||
"subsdk6",
|
||||
"subsdk7",
|
||||
"subsdk8",
|
||||
"subsdk9",
|
||||
"sdk",
|
||||
};
|
||||
return NsoNames[idx];
|
||||
}
|
||||
|
||||
struct CreateProcessInfo {
|
||||
char name[12];
|
||||
u32 version;
|
||||
ncm::TitleId title_id;
|
||||
u64 code_address;
|
||||
u32 code_num_pages;
|
||||
u32 flags;
|
||||
Handle reslimit;
|
||||
u32 system_resource_num_pages;
|
||||
};
|
||||
static_assert(sizeof(CreateProcessInfo) == 0x30, "CreateProcessInfo definition!");
|
||||
|
||||
struct ProcessInfo {
|
||||
AutoHandle process_handle;
|
||||
uintptr_t args_address;
|
||||
size_t args_size;
|
||||
uintptr_t nso_address[Nso_Count];
|
||||
size_t nso_size[Nso_Count];
|
||||
};
|
||||
|
||||
/* Global NSO header cache. */
|
||||
bool g_has_nso[Nso_Count];
|
||||
NsoHeader g_nso_headers[Nso_Count];
|
||||
|
||||
/* Helpers. */
|
||||
Result GetProgramInfoFromMeta(ProgramInfo *out, const Meta *meta) {
|
||||
/* Copy basic info. */
|
||||
out->main_thread_priority = meta->npdm->main_thread_priority;
|
||||
out->default_cpu_id = meta->npdm->default_cpu_id;
|
||||
out->main_thread_stack_size = meta->npdm->main_thread_stack_size;
|
||||
out->title_id = meta->aci->title_id;
|
||||
|
||||
/* Copy access controls. */
|
||||
size_t offset = 0;
|
||||
#define COPY_ACCESS_CONTROL(source, which) \
|
||||
({ \
|
||||
const size_t size = meta->source->which##_size; \
|
||||
if (offset + size >= sizeof(out->ac_buffer)) { \
|
||||
return ResultLoaderInternalError; \
|
||||
} \
|
||||
out->source##_##which##_size = size; \
|
||||
std::memcpy(out->ac_buffer + offset, meta->source##_##which, size); \
|
||||
offset += size; \
|
||||
})
|
||||
|
||||
/* Copy all access controls to buffer. */
|
||||
COPY_ACCESS_CONTROL(acid, sac);
|
||||
COPY_ACCESS_CONTROL(aci, sac);
|
||||
COPY_ACCESS_CONTROL(acid, fac);
|
||||
COPY_ACCESS_CONTROL(aci, fah);
|
||||
#undef COPY_ACCESS_CONTROL
|
||||
|
||||
/* Copy flags. */
|
||||
out->flags = GetProgramInfoFlags(meta->acid_kac, meta->acid->kac_size);
|
||||
return ResultSuccess;
|
||||
}
|
||||
#else
|
||||
return ResultSuccess;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
Result ProcessCreation::InitializeProcessInfo(NpdmUtils::NpdmInfo *npdm, Handle reslimit_h, u64 arg_flags, ProcessInfo *out_proc_info) {
|
||||
/* Initialize a ProcessInfo using an npdm. */
|
||||
*out_proc_info = {};
|
||||
|
||||
/* Copy all but last char of name, insert NULL terminator. */
|
||||
std::copy(npdm->header->title_name, npdm->header->title_name + sizeof(out_proc_info->name) - 1, out_proc_info->name);
|
||||
out_proc_info->name[sizeof(out_proc_info->name) - 1] = 0;
|
||||
|
||||
/* Set title id. */
|
||||
out_proc_info->title_id = npdm->aci0->title_id;
|
||||
|
||||
/* Set version. */
|
||||
out_proc_info->version = npdm->header->version;
|
||||
|
||||
/* Copy reslimit handle raw. */
|
||||
out_proc_info->reslimit_h = reslimit_h;
|
||||
|
||||
/* Set IsAddressSpace64Bit, AddressSpaceType. */
|
||||
if (npdm->header->mmu_flags & 8) {
|
||||
/* Invalid Address Space Type. */
|
||||
return ResultLoaderInvalidMeta;
|
||||
}
|
||||
out_proc_info->process_flags = (npdm->header->mmu_flags & 0xF);
|
||||
|
||||
/* Set Bit 4 (?) and EnableAslr based on argument flags. */
|
||||
out_proc_info->process_flags |= ((arg_flags & 3) << 4) ^ 0x20;
|
||||
/* Set UseSystemMemBlocks if application type is 1. */
|
||||
u32 application_type = NpdmUtils::GetApplicationType((u32 *)npdm->aci0_kac, npdm->aci0->kac_size / sizeof(u32));
|
||||
if ((application_type & 3) == 1) {
|
||||
out_proc_info->process_flags |= 0x40;
|
||||
/* 7.0.0+: Set unknown bit related to system resource heap if relevant. */
|
||||
if (GetRuntimeFirmwareVersion() >= FirmwareVersion_700) {
|
||||
if ((npdm->header->mmu_flags & 0x10)) {
|
||||
out_proc_info->process_flags |= 0x800;
|
||||
}
|
||||
bool IsApplet(const Meta *meta) {
|
||||
return (GetProgramInfoFlags(meta->aci_kac, meta->aci->kac_size) & ProgramInfoFlag_ApplicationTypeMask) == ProgramInfoFlag_Applet;
|
||||
}
|
||||
}
|
||||
|
||||
/* 3.0.0+ System Resource Size. */
|
||||
if ((GetRuntimeFirmwareVersion() >= FirmwareVersion_300)) {
|
||||
if (npdm->header->system_resource_size & 0x1FFFFF) {
|
||||
return ResultLoaderInvalidSize;
|
||||
bool IsApplication(const Meta *meta) {
|
||||
return (GetProgramInfoFlags(meta->aci_kac, meta->aci->kac_size) & ProgramInfoFlag_ApplicationTypeMask) == ProgramInfoFlag_Application;
|
||||
}
|
||||
if (npdm->header->system_resource_size) {
|
||||
if ((out_proc_info->process_flags & 6) == 0) {
|
||||
return ResultLoaderInvalidMeta;
|
||||
}
|
||||
if (!(((application_type & 3) == 1) || ((GetRuntimeFirmwareVersion() >= FirmwareVersion_600) && (application_type & 3) == 2))) {
|
||||
return ResultLoaderInvalidMeta;
|
||||
}
|
||||
if (npdm->header->system_resource_size > 0x1FE00000) {
|
||||
return ResultLoaderInvalidMeta;
|
||||
}
|
||||
}
|
||||
out_proc_info->system_resource_num_pages = npdm->header->system_resource_size >> 12;
|
||||
} else {
|
||||
out_proc_info->system_resource_num_pages = 0;
|
||||
}
|
||||
|
||||
/* 5.0.0+ Pool Partition. */
|
||||
if ((GetRuntimeFirmwareVersion() >= FirmwareVersion_500)) {
|
||||
u32 pool_partition_id = (npdm->acid->flags >> 2) & 0xF;
|
||||
switch (pool_partition_id) {
|
||||
case 0: /* Application. */
|
||||
if ((application_type & 3) == 2) {
|
||||
out_proc_info->process_flags |= 0x80;
|
||||
Npdm::AddressSpaceType GetAddressSpaceType(const Meta *meta) {
|
||||
return static_cast<Npdm::AddressSpaceType>((meta->npdm->flags & Npdm::MetaFlag_AddressSpaceTypeMask) >> Npdm::MetaFlag_AddressSpaceTypeShift);
|
||||
}
|
||||
|
||||
Acid::PoolPartition GetPoolPartition(const Meta *meta) {
|
||||
return static_cast<Acid::PoolPartition>((meta->acid->flags & Acid::AcidFlag_PoolPartitionMask) >> Acid::AcidFlag_PoolPartitionShift);
|
||||
}
|
||||
|
||||
constexpr bool IsDisallowedVersion810(const u64 title_id, const u32 version) {
|
||||
return version == 0 &&
|
||||
(title_id == TitleId_Settings ||
|
||||
title_id == TitleId_Bus ||
|
||||
title_id == TitleId_Audio ||
|
||||
title_id == TitleId_NvServices ||
|
||||
title_id == TitleId_Ns ||
|
||||
title_id == TitleId_Ssl ||
|
||||
title_id == TitleId_Es ||
|
||||
title_id == TitleId_Creport ||
|
||||
title_id == TitleId_Ro);
|
||||
}
|
||||
|
||||
Result ValidateTitleVersion(ncm::TitleId title_id, u32 version) {
|
||||
if (GetRuntimeFirmwareVersion() < FirmwareVersion_810) {
|
||||
return ResultSuccess;
|
||||
} else {
|
||||
#ifdef LDR_VALIDATE_PROCESS_VERSION
|
||||
if (IsDisallowedVersion810(static_cast<u64>(title_id), version)) {
|
||||
return ResultLoaderInvalidVersion;
|
||||
} else {
|
||||
return ResultSuccess;
|
||||
}
|
||||
break;
|
||||
case 1: /* Applet. */
|
||||
out_proc_info->process_flags |= 0x80;
|
||||
break;
|
||||
case 2: /* Sysmodule. */
|
||||
out_proc_info->process_flags |= 0x100;
|
||||
break;
|
||||
case 3: /* nvservices. */
|
||||
out_proc_info->process_flags |= 0x180;
|
||||
break;
|
||||
default:
|
||||
return ResultLoaderInvalidMeta;
|
||||
}
|
||||
}
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result ProcessCreation::CreateProcess(Handle *out_process_h, u64 index, char *nca_path, LaunchQueue::LaunchItem *launch_item, u64 arg_flags, Handle reslimit_h) {
|
||||
NpdmUtils::NpdmInfo npdm_info = {};
|
||||
ProcessInfo process_info = {};
|
||||
NsoUtils::NsoLoadExtents nso_extents = {};
|
||||
Registration::Process *target_process;
|
||||
Handle process_h = 0;
|
||||
u64 process_id = 0;
|
||||
|
||||
/* Get the process from the registration queue. */
|
||||
target_process = Registration::GetProcess(index);
|
||||
if (target_process == nullptr) {
|
||||
return ResultLoaderProcessNotRegistered;
|
||||
}
|
||||
|
||||
/* Mount the title's exefs. */
|
||||
bool mounted_code = false;
|
||||
if (target_process->tid_sid.storage_id != FsStorageId_None) {
|
||||
R_TRY(ContentManagement::MountCodeForTidSid(&target_process->tid_sid));
|
||||
mounted_code = true;
|
||||
} else {
|
||||
if (R_SUCCEEDED(ContentManagement::MountCodeNspOnSd(target_process->tid_sid.title_id))) {
|
||||
mounted_code = true;
|
||||
}
|
||||
}
|
||||
ON_SCOPE_EXIT {
|
||||
if (mounted_code) {
|
||||
const Result unmount_res = ContentManagement::UnmountCode();
|
||||
if (target_process->tid_sid.storage_id != FsStorageId_None) {
|
||||
R_ASSERT(unmount_res);
|
||||
#else
|
||||
return ResultSuccess;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/* Load the process's NPDM. */
|
||||
R_TRY(NpdmUtils::LoadNpdmFromCache(target_process->tid_sid.title_id, &npdm_info));
|
||||
Result LoadNsoHeaders(ncm::TitleId title_id, NsoHeader *nso_headers, bool *has_nso) {
|
||||
/* Clear NSOs. */
|
||||
std::memset(nso_headers, 0, sizeof(*nso_headers) * Nso_Count);
|
||||
std::memset(has_nso, 0, sizeof(*has_nso) * Nso_Count);
|
||||
|
||||
/* Validate version. */
|
||||
R_TRY(ValidateProcessVersion(target_process->tid_sid.title_id, npdm_info.header->version));
|
||||
for (size_t i = 0; i < Nso_Count; i++) {
|
||||
FILE *f = nullptr;
|
||||
if (R_SUCCEEDED(OpenCodeFile(f, title_id, GetNsoName(i)))) {
|
||||
ON_SCOPE_EXIT { fclose(f); };
|
||||
/* Read NSO header. */
|
||||
if (fread(nso_headers + i, sizeof(*nso_headers), 1, f) != 1) {
|
||||
return ResultLoaderInvalidNso;
|
||||
}
|
||||
has_nso[i] = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* Validate the title we're loading is what we expect. */
|
||||
if (npdm_info.aci0->title_id < npdm_info.acid->title_id_range_min || npdm_info.aci0->title_id > npdm_info.acid->title_id_range_max) {
|
||||
return ResultLoaderInvalidProgramId;
|
||||
}
|
||||
|
||||
/* Validate that the ACI0 Kernel Capabilities are valid and restricted by the ACID Kernel Capabilities. */
|
||||
const u32 *acid_caps = reinterpret_cast<u32 *>(npdm_info.acid_kac);
|
||||
const u32 *aci0_caps = reinterpret_cast<u32 *>(npdm_info.aci0_kac);
|
||||
const size_t num_acid_caps = npdm_info.acid->kac_size / sizeof(*acid_caps);
|
||||
const size_t num_aci0_caps = npdm_info.aci0->kac_size / sizeof(*aci0_caps);
|
||||
R_TRY(NpdmUtils::ValidateCapabilities(acid_caps, num_acid_caps, aci0_caps, num_aci0_caps));
|
||||
|
||||
/* Read in all NSO headers, see what NSOs are present. */
|
||||
R_TRY(NsoUtils::LoadNsoHeaders(npdm_info.aci0->title_id));
|
||||
|
||||
/* Validate that the set of NSOs to be loaded is correct. */
|
||||
R_TRY(NsoUtils::ValidateNsoLoadSet());
|
||||
|
||||
/* Initialize the ProcessInfo. */
|
||||
R_TRY(ProcessCreation::InitializeProcessInfo(&npdm_info, reslimit_h, arg_flags, &process_info));
|
||||
|
||||
/* Figure out where NSOs will be mapped, and how much space they (and arguments) will take up. */
|
||||
R_TRY(NsoUtils::CalculateNsoLoadExtents(process_info.process_flags, launch_item != nullptr ? launch_item->arg_size : 0, &nso_extents));
|
||||
|
||||
/* Set Address Space information in ProcessInfo. */
|
||||
process_info.code_addr = nso_extents.base_address;
|
||||
process_info.code_num_pages = nso_extents.total_size + 0xFFF;
|
||||
process_info.code_num_pages >>= 12;
|
||||
|
||||
/* Call svcCreateProcess(). */
|
||||
R_TRY(svcCreateProcess(&process_h, &process_info, (u32 *)npdm_info.aci0_kac, npdm_info.aci0->kac_size/sizeof(u32)));
|
||||
auto proc_handle_guard = SCOPE_GUARD {
|
||||
svcCloseHandle(process_h);
|
||||
};
|
||||
|
||||
|
||||
/* Load all NSOs into Process memory, and set permissions accordingly. */
|
||||
{
|
||||
const u8 *launch_args = nullptr;
|
||||
size_t launch_args_size = 0;
|
||||
if (launch_item != nullptr) {
|
||||
launch_args = reinterpret_cast<const u8 *>(launch_item->args);
|
||||
launch_args_size = launch_item->arg_size;
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
R_TRY(NsoUtils::LoadNsosIntoProcessMemory(process_h, npdm_info.aci0->title_id, &nso_extents, launch_args, launch_args_size));
|
||||
}
|
||||
Result ValidateNsoHeaders(const NsoHeader *nso_headers, const bool *has_nso) {
|
||||
/* We must always have a main. */
|
||||
if (!has_nso[Nso_Main]) {
|
||||
return ResultLoaderInvalidNso;
|
||||
}
|
||||
|
||||
/* Update the list of registered processes with the new process. */
|
||||
svcGetProcessId(&process_id, process_h);
|
||||
bool is_64_bit_addspace;
|
||||
if ((GetRuntimeFirmwareVersion() >= FirmwareVersion_200)) {
|
||||
is_64_bit_addspace = (((npdm_info.header->mmu_flags >> 1) & 5) | 2) == 3;
|
||||
} else {
|
||||
is_64_bit_addspace = (npdm_info.header->mmu_flags & 0xE) == 0x2;
|
||||
}
|
||||
Registration::SetProcessIdTidAndIs64BitAddressSpace(index, process_id, npdm_info.aci0->title_id, is_64_bit_addspace);
|
||||
for (unsigned int i = 0; i < NSO_NUM_MAX; i++) {
|
||||
if (NsoUtils::IsNsoPresent(i)) {
|
||||
Registration::AddModuleInfo(index, nso_extents.nso_addresses[i], nso_extents.nso_sizes[i], NsoUtils::GetNsoBuildId(i));
|
||||
/* If we don't have an RTLD, we must only have a main. */
|
||||
if (!has_nso[Nso_Rtld]) {
|
||||
for (size_t i = Nso_Main + 1; i < Nso_Count; i++) {
|
||||
if (has_nso[i]) {
|
||||
return ResultLoaderInvalidNso;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* All NSOs must have zero text offset. */
|
||||
for (size_t i = 0; i < Nso_Count; i++) {
|
||||
if (nso_headers[i].text_dst_offset != 0) {
|
||||
return ResultLoaderInvalidNso;
|
||||
}
|
||||
}
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result ValidateMeta(const Meta *meta, const ncm::TitleLocation &loc) {
|
||||
/* Validate version. */
|
||||
R_TRY(ValidateTitleVersion(loc.title_id, meta->npdm->version));
|
||||
|
||||
/* Validate title id. */
|
||||
if (meta->aci->title_id < meta->acid->title_id_min || meta->aci->title_id > meta->acid->title_id_max) {
|
||||
return ResultLoaderInvalidProgramId;
|
||||
}
|
||||
|
||||
/* Validate the kernel capacilities. */
|
||||
R_TRY(ValidateCapabilities(meta->acid_kac, meta->acid->kac_size, meta->aci_kac, meta->aci->kac_size));
|
||||
|
||||
/* All good. */
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result GetCreateProcessFlags(u32 *out, const Meta *meta, const u32 ldr_flags) {
|
||||
const u8 meta_flags = meta->npdm->flags;
|
||||
|
||||
u32 flags = 0;
|
||||
|
||||
/* Set Is64Bit. */
|
||||
if (meta_flags & Npdm::MetaFlag_Is64Bit) {
|
||||
flags |= svc::CreateProcessFlag_Is64Bit;
|
||||
}
|
||||
|
||||
/* Set AddressSpaceType. */
|
||||
switch (GetAddressSpaceType(meta)) {
|
||||
case Npdm::AddressSpaceType_32Bit:
|
||||
flags |= svc::CreateProcessFlag_AddressSpace32Bit;
|
||||
break;
|
||||
case Npdm::AddressSpaceType_64BitDeprecated:
|
||||
flags |= svc::CreateProcessFlag_AddressSpace64BitDeprecated;
|
||||
break;
|
||||
case Npdm::AddressSpaceType_32BitWithoutAlias:
|
||||
flags |= svc::CreateProcessFlag_AddressSpace32BitWithoutAlias;
|
||||
break;
|
||||
case Npdm::AddressSpaceType_64Bit:
|
||||
flags |= svc::CreateProcessFlag_AddressSpace64Bit;
|
||||
break;
|
||||
default:
|
||||
return ResultLoaderInvalidMeta;
|
||||
}
|
||||
|
||||
/* Set Enable Debug. */
|
||||
if (ldr_flags & CreateProcessFlag_EnableDebug) {
|
||||
flags |= svc::CreateProcessFlag_EnableDebug;
|
||||
}
|
||||
|
||||
/* Set Enable ASLR. */
|
||||
if (!(ldr_flags & CreateProcessFlag_DisableAslr)) {
|
||||
flags |= svc::CreateProcessFlag_EnableAslr;
|
||||
}
|
||||
|
||||
/* Set Is Application. */
|
||||
if (IsApplication(meta)) {
|
||||
flags |= svc::CreateProcessFlag_IsApplication;
|
||||
|
||||
/* 7.0.0+: Set OptimizeMemoryAllocation if relevant. */
|
||||
if (GetRuntimeFirmwareVersion() >= FirmwareVersion_700) {
|
||||
if (meta_flags & Npdm::MetaFlag_OptimizeMemoryAllocation) {
|
||||
flags |= svc::CreateProcessFlag_OptimizeMemoryAllocation;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* 5.0.0+ Set Pool Partition. */
|
||||
if (GetRuntimeFirmwareVersion() >= FirmwareVersion_500) {
|
||||
switch (GetPoolPartition(meta)) {
|
||||
case Acid::PoolPartition_Application:
|
||||
if (IsApplet(meta)) {
|
||||
flags |= svc::CreateProcessFlag_PoolPartitionApplet;
|
||||
} else {
|
||||
flags |= svc::CreateProcessFlag_PoolPartitionApplication;
|
||||
}
|
||||
break;
|
||||
case Acid::PoolPartition_Applet:
|
||||
flags |= svc::CreateProcessFlag_PoolPartitionApplet;
|
||||
break;
|
||||
case Acid::PoolPartition_System:
|
||||
flags |= svc::CreateProcessFlag_PoolPartitionSystem;
|
||||
break;
|
||||
case Acid::PoolPartition_SystemNonSecure:
|
||||
flags |= svc::CreateProcessFlag_PoolPartitionSystemNonSecure;
|
||||
break;
|
||||
default:
|
||||
return ResultLoaderInvalidMeta;
|
||||
}
|
||||
} else if (GetRuntimeFirmwareVersion() >= FirmwareVersion_400) {
|
||||
/* On 4.0.0+, the corresponding bit was simply "UseSecureMemory". */
|
||||
if (meta->acid->flags & Acid::AcidFlag_DeprecatedUseSecureMemory) {
|
||||
flags |= svc::CreateProcessFlag_DeprecatedUseSecureMemory;
|
||||
}
|
||||
}
|
||||
|
||||
*out = flags;
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result GetCreateProcessInfo(CreateProcessInfo *out, const Meta *meta, u32 flags, Handle reslimit_h) {
|
||||
/* Clear output. */
|
||||
std::memset(out, 0, sizeof(*out));
|
||||
|
||||
/* Set name, version, title id, resource limit handle. */
|
||||
std::memcpy(out->name, meta->npdm->title_name, sizeof(out->name) - 1);
|
||||
out->version = meta->npdm->version;
|
||||
out->title_id = meta->aci->title_id;
|
||||
out->reslimit = reslimit_h;
|
||||
|
||||
/* Set flags. */
|
||||
R_TRY(GetCreateProcessFlags(&out->flags, meta, flags));
|
||||
|
||||
/* 3.0.0+ System Resource Size. */
|
||||
if (GetRuntimeFirmwareVersion() >= FirmwareVersion_300) {
|
||||
/* Validate size is aligned. */
|
||||
if (meta->npdm->system_resource_size & (SystemResourceSizeAlignment - 1)) {
|
||||
return ResultLoaderInvalidSize;
|
||||
}
|
||||
/* Validate system resource usage. */
|
||||
if (meta->npdm->system_resource_size) {
|
||||
/* Process must be 64-bit. */
|
||||
if (!(out->flags & svc::CreateProcessFlag_AddressSpace64Bit)) {
|
||||
return ResultLoaderInvalidMeta;
|
||||
}
|
||||
|
||||
/* Process must be application or applet. */
|
||||
if (!IsApplication(meta) && !IsApplet(meta)) {
|
||||
return ResultLoaderInvalidMeta;
|
||||
}
|
||||
|
||||
/* Size must be less than or equal to max. */
|
||||
if (meta->npdm->system_resource_size > SystemResourceSizeMax) {
|
||||
return ResultLoaderInvalidMeta;
|
||||
}
|
||||
}
|
||||
out->system_resource_num_pages = meta->npdm->system_resource_size >> 12;
|
||||
}
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result DecideAddressSpaceLayout(ProcessInfo *out, CreateProcessInfo *out_cpi, const NsoHeader *nso_headers, const bool *has_nso, const args::ArgumentInfo *arg_info) {
|
||||
/* Clear output. */
|
||||
out->args_address = 0;
|
||||
out->args_size = 0;
|
||||
std::memset(out->nso_address, 0, sizeof(out->nso_address));
|
||||
std::memset(out->nso_size, 0, sizeof(out->nso_size));
|
||||
|
||||
size_t total_size = 0;
|
||||
|
||||
/* Calculate base offsets. */
|
||||
for (size_t i = 0; i < Nso_Count; i++) {
|
||||
if (has_nso[i]) {
|
||||
out->nso_address[i] = total_size;
|
||||
const size_t text_end = nso_headers[i].text_dst_offset + nso_headers[i].text_size;
|
||||
const size_t ro_end = nso_headers[i].ro_dst_offset + nso_headers[i].ro_size;
|
||||
const size_t rw_end = nso_headers[i].rw_dst_offset + nso_headers[i].rw_size + nso_headers[i].bss_size;
|
||||
out->nso_size[i] = text_end;
|
||||
out->nso_size[i] = std::max(out->nso_size[i], ro_end);
|
||||
out->nso_size[i] = std::max(out->nso_size[i], rw_end);
|
||||
out->nso_size[i] = (out->nso_size[i] + size_t(0xFFFul)) & ~size_t(0xFFFul);
|
||||
|
||||
total_size += out->nso_size[i];
|
||||
|
||||
if (arg_info != nullptr && arg_info->args_size && !out->args_size) {
|
||||
out->args_address = total_size;
|
||||
out->args_size = 2 * arg_info->args_size + args::ArgumentSizeMax + 2 * sizeof(u32);
|
||||
out->args_size = (out->args_size + size_t(0xFFFul)) & ~size_t(0xFFFul);
|
||||
total_size += out->args_size;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Calculate ASLR. */
|
||||
uintptr_t aslr_start = 0;
|
||||
uintptr_t aslr_size = 0;
|
||||
if (GetRuntimeFirmwareVersion() >= FirmwareVersion_200) {
|
||||
switch (out_cpi->flags & svc::CreateProcessFlag_AddressSpaceMask) {
|
||||
case svc::CreateProcessFlag_AddressSpace32Bit:
|
||||
case svc::CreateProcessFlag_AddressSpace32BitWithoutAlias:
|
||||
aslr_start = map::AslrBase32Bit;
|
||||
aslr_size = map::AslrSize32Bit;
|
||||
break;
|
||||
case svc::CreateProcessFlag_AddressSpace64BitDeprecated:
|
||||
aslr_start = map::AslrBase64BitDeprecated;
|
||||
aslr_size = map::AslrSize64BitDeprecated;
|
||||
break;
|
||||
case svc::CreateProcessFlag_AddressSpace64Bit:
|
||||
aslr_start = map::AslrBase64Bit;
|
||||
aslr_size = map::AslrSize64Bit;
|
||||
break;
|
||||
default:
|
||||
std::abort();
|
||||
}
|
||||
} else {
|
||||
/* On 1.0.0, only 2 address space types existed. */
|
||||
if (out_cpi->flags & svc::CreateProcessFlag_AddressSpace64BitDeprecated) {
|
||||
aslr_start = map::AslrBase64BitDeprecated;
|
||||
aslr_size = map::AslrSize64BitDeprecated;
|
||||
} else {
|
||||
aslr_start = map::AslrBase32Bit;
|
||||
aslr_size = map::AslrSize32Bit;
|
||||
}
|
||||
}
|
||||
if (total_size > aslr_size) {
|
||||
return ResultKernelOutOfMemory;
|
||||
}
|
||||
|
||||
/* Set Create Process output. */
|
||||
uintptr_t aslr_slide = 0;
|
||||
uintptr_t unused_size = (aslr_size - total_size);
|
||||
if (out_cpi->flags & svc::CreateProcessFlag_EnableAslr) {
|
||||
aslr_slide = sts::rnd::GenerateRandomU64(unused_size / BaseAddressAlignment) * BaseAddressAlignment;
|
||||
}
|
||||
|
||||
/* Set out. */
|
||||
aslr_start += aslr_slide;
|
||||
for (size_t i = 0; i < Nso_Count; i++) {
|
||||
if (has_nso[i]) {
|
||||
out->nso_address[i] += aslr_start;
|
||||
}
|
||||
}
|
||||
if (out->args_address) {
|
||||
out->args_address += aslr_start;
|
||||
}
|
||||
|
||||
out_cpi->code_address = aslr_start;
|
||||
out_cpi->code_num_pages = total_size >> 12;
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result CreateProcessImpl(ProcessInfo *out, const Meta *meta, const NsoHeader *nso_headers, const bool *has_nso, const args::ArgumentInfo *arg_info, u32 flags, Handle reslimit_h) {
|
||||
/* Get CreateProcessInfo. */
|
||||
CreateProcessInfo cpi;
|
||||
R_TRY(GetCreateProcessInfo(&cpi, meta, flags, reslimit_h));
|
||||
|
||||
/* Decide on an NSO layout. */
|
||||
R_TRY(DecideAddressSpaceLayout(out, &cpi, nso_headers, has_nso, arg_info));
|
||||
|
||||
/* Actually create process. const_cast necessary because libnx doesn't declare svcCreateProcess with const u32*. */
|
||||
return svcCreateProcess(out->process_handle.GetPointer(), &cpi, const_cast<u32 *>(reinterpret_cast<const u32 *>(meta->aci_kac)), meta->aci->kac_size / sizeof(u32));
|
||||
}
|
||||
|
||||
Result LoadNsoSegment(FILE *f, const NsoHeader::SegmentInfo *segment, size_t file_size, const u8 *file_hash, bool is_compressed, bool check_hash, uintptr_t map_base, uintptr_t map_end) {
|
||||
/* Select read size based on compression. */
|
||||
if (!is_compressed) {
|
||||
file_size = segment->size;
|
||||
}
|
||||
|
||||
/* Validate size. */
|
||||
if (file_size > segment->size || file_size > std::numeric_limits<int>::max() || segment->size > std::numeric_limits<int>::max()) {
|
||||
return ResultLoaderInvalidNso;
|
||||
}
|
||||
|
||||
/* Load data from file. */
|
||||
uintptr_t load_address = is_compressed ? map_end - file_size : map_base;
|
||||
fseek(f, segment->file_offset, SEEK_SET);
|
||||
if (fread(reinterpret_cast<void *>(load_address), file_size, 1, f) != 1) {
|
||||
return ResultLoaderInvalidNso;
|
||||
}
|
||||
|
||||
/* Uncompress if necessary. */
|
||||
if (is_compressed) {
|
||||
if (util::DecompressLZ4(reinterpret_cast<void *>(map_base), segment->size, reinterpret_cast<const void *>(load_address), file_size) != static_cast<int>(segment->size)) {
|
||||
return ResultLoaderInvalidNso;
|
||||
}
|
||||
}
|
||||
|
||||
/* Check hash if necessary. */
|
||||
if (check_hash) {
|
||||
u8 hash[SHA256_HASH_SIZE];
|
||||
sha256CalculateHash(hash, reinterpret_cast<void *>(map_base), segment->size);
|
||||
|
||||
if (std::memcmp(hash, file_hash, sizeof(hash)) != 0) {
|
||||
return ResultLoaderInvalidNso;
|
||||
}
|
||||
}
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result LoadNsoIntoProcessMemory(Handle process_handle, FILE *f, uintptr_t map_address, const NsoHeader *nso_header, uintptr_t nso_address, size_t nso_size) {
|
||||
/* Map and read data from file. */
|
||||
{
|
||||
map::AutoCloseMap mapper(map_address, process_handle, nso_address, nso_size);
|
||||
R_TRY(mapper.GetResult());
|
||||
|
||||
/* Load NSO segments. */
|
||||
R_TRY(LoadNsoSegment(f, &nso_header->segments[NsoHeader::Segment_Text], nso_header->text_compressed_size, nso_header->text_hash, (nso_header->flags & NsoHeader::Flag_CompressedText) != 0,
|
||||
(nso_header->flags & NsoHeader::Flag_CheckHashText) != 0, map_address + nso_header->text_dst_offset, map_address + nso_size));
|
||||
R_TRY(LoadNsoSegment(f, &nso_header->segments[NsoHeader::Segment_Ro], nso_header->ro_compressed_size, nso_header->ro_hash, (nso_header->flags & NsoHeader::Flag_CompressedRo) != 0,
|
||||
(nso_header->flags & NsoHeader::Flag_CheckHashRo) != 0, map_address + nso_header->ro_dst_offset, map_address + nso_size));
|
||||
R_TRY(LoadNsoSegment(f, &nso_header->segments[NsoHeader::Segment_Rw], nso_header->rw_compressed_size, nso_header->rw_hash, (nso_header->flags & NsoHeader::Flag_CompressedRw) != 0,
|
||||
(nso_header->flags & NsoHeader::Flag_CheckHashRw) != 0, map_address + nso_header->rw_dst_offset, map_address + nso_size));
|
||||
|
||||
/* Clear unused space to zero. */
|
||||
const size_t text_end = nso_header->text_dst_offset + nso_header->text_size;
|
||||
const size_t ro_end = nso_header->ro_dst_offset + nso_header->ro_size;
|
||||
const size_t rw_end = nso_header->rw_dst_offset + nso_header->rw_size;
|
||||
std::memset(reinterpret_cast<void *>(map_address), 0, nso_header->text_dst_offset);
|
||||
std::memset(reinterpret_cast<void *>(map_address + text_end), 0, nso_header->ro_dst_offset - text_end);
|
||||
std::memset(reinterpret_cast<void *>(map_address + ro_end), 0, nso_header->rw_dst_offset - ro_end);
|
||||
std::memset(reinterpret_cast<void *>(map_address + rw_end), 0, nso_header->bss_size);
|
||||
|
||||
/* Apply IPS patches. */
|
||||
LocateAndApplyIpsPatchesToModule(nso_header->build_id, map_address, nso_size);
|
||||
}
|
||||
|
||||
/* Set permissions. */
|
||||
const size_t text_size = (static_cast<size_t>(nso_header->text_size) + size_t(0xFFFul)) & ~size_t(0xFFFul);
|
||||
const size_t ro_size = (static_cast<size_t>(nso_header->ro_size) + size_t(0xFFFul)) & ~size_t(0xFFFul);
|
||||
const size_t rw_size = (static_cast<size_t>(nso_header->rw_size + nso_header->bss_size) + size_t(0xFFFul)) & ~size_t(0xFFFul);
|
||||
if (text_size) {
|
||||
R_TRY(svcSetProcessMemoryPermission(process_handle, nso_address + nso_header->text_dst_offset, text_size, Perm_Rx));
|
||||
}
|
||||
if (ro_size) {
|
||||
R_TRY(svcSetProcessMemoryPermission(process_handle, nso_address + nso_header->ro_dst_offset, ro_size, Perm_R));
|
||||
}
|
||||
if (rw_size) {
|
||||
R_TRY(svcSetProcessMemoryPermission(process_handle, nso_address + nso_header->rw_dst_offset, rw_size, Perm_Rw));
|
||||
}
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
Result LoadNsosIntoProcessMemory(const ProcessInfo *process_info, const ncm::TitleId title_id, const NsoHeader *nso_headers, const bool *has_nso, const args::ArgumentInfo *arg_info) {
|
||||
const Handle process_handle = process_info->process_handle.Get();
|
||||
|
||||
/* Load each NSO. */
|
||||
for (size_t i = 0; i < Nso_Count; i++) {
|
||||
if (has_nso[i]) {
|
||||
FILE *f = nullptr;
|
||||
R_TRY(OpenCodeFile(f, title_id, GetNsoName(i)));
|
||||
ON_SCOPE_EXIT { fclose(f); };
|
||||
|
||||
uintptr_t map_address = 0;
|
||||
R_TRY(map::LocateMappableSpace(&map_address, process_info->nso_size[i]));
|
||||
|
||||
R_TRY(LoadNsoIntoProcessMemory(process_handle, f, map_address, nso_headers + i, process_info->nso_address[i], process_info->nso_size[i]));
|
||||
}
|
||||
}
|
||||
|
||||
/* Load arguments, if present. */
|
||||
if (arg_info != nullptr) {
|
||||
/* Write argument data into memory. */
|
||||
{
|
||||
uintptr_t map_address = 0;
|
||||
R_TRY(map::LocateMappableSpace(&map_address, process_info->args_size));
|
||||
|
||||
map::AutoCloseMap mapper(map_address, process_handle, process_info->args_address, process_info->args_size);
|
||||
R_TRY(mapper.GetResult());
|
||||
|
||||
ProgramArguments *args = reinterpret_cast<ProgramArguments *>(map_address);
|
||||
std::memset(args, 0, sizeof(*args));
|
||||
args->allocated_size = process_info->args_size;
|
||||
args->arguments_size = arg_info->args_size;
|
||||
std::memcpy(args->arguments, arg_info->args, arg_info->args_size);
|
||||
}
|
||||
|
||||
/* Set argument region permissions. */
|
||||
R_TRY(svcSetProcessMemoryPermission(process_handle, process_info->args_address, process_info->args_size, Perm_Rw));
|
||||
}
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* Send the pid/tid pair to anyone interested in man-in-the-middle-attacking it. */
|
||||
Registration::AssociatePidTidForMitM(index);
|
||||
/* Process Creation API. */
|
||||
Result CreateProcess(Handle *out, PinId pin_id, const ncm::TitleLocation &loc, const char *path, u32 flags, Handle reslimit_h) {
|
||||
/* Use global storage for NSOs. */
|
||||
NsoHeader *nso_headers = g_nso_headers;
|
||||
bool *has_nso = g_has_nso;
|
||||
const auto arg_info = args::Get(loc.title_id);
|
||||
|
||||
/* If HBL, override HTML document path. */
|
||||
if (ContentManagement::ShouldOverrideContentsWithHBL(target_process->tid_sid.title_id)) {
|
||||
ContentManagement::RedirectHtmlDocumentPathForHbl(target_process->tid_sid.title_id, target_process->tid_sid.storage_id);
|
||||
{
|
||||
/* Mount code. */
|
||||
ScopedCodeMount mount;
|
||||
R_TRY(MountCode(mount, loc));
|
||||
|
||||
/* Load meta, possibly from cache. */
|
||||
Meta meta;
|
||||
R_TRY(LoadMetaFromCache(&meta, loc.title_id));
|
||||
|
||||
/* Validate meta. */
|
||||
R_TRY(ValidateMeta(&meta, loc));
|
||||
|
||||
/* Load, validate NSOs. */
|
||||
R_TRY(LoadNsoHeaders(loc.title_id, nso_headers, has_nso));
|
||||
R_TRY(ValidateNsoHeaders(nso_headers, has_nso));
|
||||
|
||||
/* Actually create process. */
|
||||
ProcessInfo info;
|
||||
R_TRY(CreateProcessImpl(&info, &meta, nso_headers, has_nso, arg_info, flags, reslimit_h));
|
||||
|
||||
/* Load NSOs into process memory. */
|
||||
R_TRY(LoadNsosIntoProcessMemory(&info, loc.title_id, nso_headers, has_nso, arg_info));
|
||||
|
||||
/* Register NSOs with ro manager. */
|
||||
{
|
||||
/* Nintendo doesn't validate this result, but we will. */
|
||||
u64 process_id;
|
||||
R_ASSERT(svcGetProcessId(&process_id, info.process_handle.Get()));
|
||||
|
||||
/* Register new process. */
|
||||
ldr::ro::RegisterProcess(pin_id, process_id, loc.title_id);
|
||||
|
||||
/* Register all NSOs. */
|
||||
for (size_t i = 0; i < Nso_Count; i++) {
|
||||
if (has_nso[i]) {
|
||||
ldr::ro::RegisterModule(pin_id, nso_headers[i].build_id, info.nso_address[i], info.nso_size[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Move the process handle to output. */
|
||||
*out = info.process_handle.Move();
|
||||
}
|
||||
|
||||
/* Note that we've created the title. */
|
||||
SetLaunchedTitle(loc.title_id);
|
||||
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
||||
/* ECS is a one-shot operation, but we don't clear on failure. */
|
||||
ContentManagement::ClearExternalContentSource(target_process->tid_sid.title_id);
|
||||
Result GetProgramInfo(ProgramInfo *out, const ncm::TitleLocation &loc) {
|
||||
Meta meta;
|
||||
|
||||
/* Cancel the process handle guard. */
|
||||
proc_handle_guard.Cancel();
|
||||
/* Load Meta. */
|
||||
{
|
||||
ScopedCodeMount mount;
|
||||
R_TRY(MountCode(mount, loc));
|
||||
R_TRY(LoadMeta(&meta, loc.title_id));
|
||||
}
|
||||
|
||||
return GetProgramInfoFromMeta(out, &meta);
|
||||
}
|
||||
|
||||
/* Write process handle to output. */
|
||||
*out_process_h = process_h;
|
||||
return ResultSuccess;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue