2018-09-07 15:00:13 +00:00
|
|
|
/*
|
2019-04-08 02:00:49 +00:00
|
|
|
* Copyright (c) 2018-2019 Atmosphère-NX
|
2018-09-07 15:00:13 +00: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/>.
|
|
|
|
*/
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-04-22 01:52:49 +00:00
|
|
|
#include <switch.h>
|
2018-04-24 08:08:11 +00:00
|
|
|
#include <algorithm>
|
2019-01-31 12:46:20 +00:00
|
|
|
#include <stratosphere.hpp>
|
2018-04-22 01:52:49 +00:00
|
|
|
|
|
|
|
#include "ldr_process_creation.hpp"
|
|
|
|
#include "ldr_registration.hpp"
|
|
|
|
#include "ldr_launch_queue.hpp"
|
|
|
|
#include "ldr_content_management.hpp"
|
|
|
|
#include "ldr_npdm.hpp"
|
2018-04-24 02:05:22 +00:00
|
|
|
#include "ldr_nso.hpp"
|
2018-04-22 01:52:49 +00:00
|
|
|
|
2019-06-19 18:51:30 +00:00
|
|
|
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 {
|
|
|
|
return ResultSuccess;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
return ResultSuccess;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-24 08:08:11 +00:00
|
|
|
Result ProcessCreation::InitializeProcessInfo(NpdmUtils::NpdmInfo *npdm, Handle reslimit_h, u64 arg_flags, ProcessInfo *out_proc_info) {
|
|
|
|
/* Initialize a ProcessInfo using an npdm. */
|
2019-04-05 04:05:41 +00:00
|
|
|
*out_proc_info = {};
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-04-24 08:08:11 +00:00
|
|
|
/* 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;
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-04-24 08:08:11 +00:00
|
|
|
/* Set title id. */
|
|
|
|
out_proc_info->title_id = npdm->aci0->title_id;
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2019-06-19 18:51:30 +00:00
|
|
|
/* Set version. */
|
|
|
|
out_proc_info->version = npdm->header->version;
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-04-24 08:08:11 +00:00
|
|
|
/* Copy reslimit handle raw. */
|
|
|
|
out_proc_info->reslimit_h = reslimit_h;
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-04-24 08:08:11 +00:00
|
|
|
/* Set IsAddressSpace64Bit, AddressSpaceType. */
|
|
|
|
if (npdm->header->mmu_flags & 8) {
|
|
|
|
/* Invalid Address Space Type. */
|
2019-03-28 22:06:50 +00:00
|
|
|
return ResultLoaderInvalidMeta;
|
2018-04-24 08:08:11 +00:00
|
|
|
}
|
|
|
|
out_proc_info->process_flags = (npdm->header->mmu_flags & 0xF);
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-04-24 08:08:11 +00:00
|
|
|
/* 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;
|
2019-01-31 12:46:20 +00:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
2018-04-24 08:08:11 +00:00
|
|
|
}
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-04-24 08:08:11 +00:00
|
|
|
/* 3.0.0+ System Resource Size. */
|
2019-05-10 10:25:07 +00:00
|
|
|
if ((GetRuntimeFirmwareVersion() >= FirmwareVersion_300)) {
|
2018-04-24 08:08:11 +00:00
|
|
|
if (npdm->header->system_resource_size & 0x1FFFFF) {
|
2019-03-28 22:06:50 +00:00
|
|
|
return ResultLoaderInvalidSize;
|
2018-04-24 08:08:11 +00:00
|
|
|
}
|
|
|
|
if (npdm->header->system_resource_size) {
|
|
|
|
if ((out_proc_info->process_flags & 6) == 0) {
|
2019-03-28 22:06:50 +00:00
|
|
|
return ResultLoaderInvalidMeta;
|
2018-04-24 08:08:11 +00:00
|
|
|
}
|
2019-05-10 10:25:07 +00:00
|
|
|
if (!(((application_type & 3) == 1) || ((GetRuntimeFirmwareVersion() >= FirmwareVersion_600) && (application_type & 3) == 2))) {
|
2019-03-28 22:06:50 +00:00
|
|
|
return ResultLoaderInvalidMeta;
|
2018-04-24 08:08:11 +00:00
|
|
|
}
|
|
|
|
if (npdm->header->system_resource_size > 0x1FE00000) {
|
2019-03-28 22:06:50 +00:00
|
|
|
return ResultLoaderInvalidMeta;
|
2018-04-24 08:08:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
out_proc_info->system_resource_num_pages = npdm->header->system_resource_size >> 12;
|
|
|
|
} else {
|
|
|
|
out_proc_info->system_resource_num_pages = 0;
|
|
|
|
}
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-04-24 08:08:11 +00:00
|
|
|
/* 5.0.0+ Pool Partition. */
|
2019-05-10 10:25:07 +00:00
|
|
|
if ((GetRuntimeFirmwareVersion() >= FirmwareVersion_500)) {
|
2018-09-19 23:21:46 +00:00
|
|
|
u32 pool_partition_id = (npdm->acid->flags >> 2) & 0xF;
|
2018-04-24 08:08:11 +00:00
|
|
|
switch (pool_partition_id) {
|
|
|
|
case 0: /* Application. */
|
|
|
|
if ((application_type & 3) == 2) {
|
|
|
|
out_proc_info->process_flags |= 0x80;
|
|
|
|
}
|
|
|
|
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:
|
2019-03-28 22:06:50 +00:00
|
|
|
return ResultLoaderInvalidMeta;
|
2018-04-24 08:08:11 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2019-03-29 05:39:39 +00:00
|
|
|
return ResultSuccess;
|
2018-04-24 08:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Result ProcessCreation::CreateProcess(Handle *out_process_h, u64 index, char *nca_path, LaunchQueue::LaunchItem *launch_item, u64 arg_flags, Handle reslimit_h) {
|
2019-04-05 04:05:41 +00:00
|
|
|
NpdmUtils::NpdmInfo npdm_info = {};
|
|
|
|
ProcessInfo process_info = {};
|
|
|
|
NsoUtils::NsoLoadExtents nso_extents = {};
|
2018-04-23 19:33:56 +00:00
|
|
|
Registration::Process *target_process;
|
2018-04-24 09:14:40 +00:00
|
|
|
Handle process_h = 0;
|
2018-04-24 12:25:31 +00:00
|
|
|
u64 process_id = 0;
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-04-24 02:05:22 +00:00
|
|
|
/* Get the process from the registration queue. */
|
2018-04-26 23:03:10 +00:00
|
|
|
target_process = Registration::GetProcess(index);
|
2019-06-17 23:29:09 +00:00
|
|
|
if (target_process == nullptr) {
|
2019-03-28 22:06:50 +00:00
|
|
|
return ResultLoaderProcessNotRegistered;
|
2018-04-23 19:33:56 +00:00
|
|
|
}
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-04-24 02:05:22 +00:00
|
|
|
/* Mount the title's exefs. */
|
2019-06-17 23:29:09 +00:00
|
|
|
bool mounted_code = false;
|
2018-07-28 02:53:20 +00:00
|
|
|
if (target_process->tid_sid.storage_id != FsStorageId_None) {
|
2019-06-17 23:29:09 +00:00
|
|
|
R_TRY(ContentManagement::MountCodeForTidSid(&target_process->tid_sid));
|
2018-07-29 23:35:43 +00:00
|
|
|
mounted_code = true;
|
|
|
|
} else {
|
|
|
|
if (R_SUCCEEDED(ContentManagement::MountCodeNspOnSd(target_process->tid_sid.title_id))) {
|
|
|
|
mounted_code = true;
|
|
|
|
}
|
2018-04-23 19:33:56 +00:00
|
|
|
}
|
2019-06-17 23:29:09 +00:00
|
|
|
ON_SCOPE_EXIT {
|
|
|
|
if (mounted_code) {
|
|
|
|
if (R_FAILED(ContentManagement::UnmountCode()) && target_process->tid_sid.storage_id != FsStorageId_None) {
|
|
|
|
std::abort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-04-24 02:05:22 +00:00
|
|
|
/* Load the process's NPDM. */
|
2019-06-17 23:29:09 +00:00
|
|
|
R_TRY(NpdmUtils::LoadNpdmFromCache(target_process->tid_sid.title_id, &npdm_info));
|
|
|
|
|
2019-06-19 18:51:30 +00:00
|
|
|
/* Validate version. */
|
|
|
|
R_TRY(ValidateProcessVersion(target_process->tid_sid.title_id, npdm_info.header->version));
|
|
|
|
|
2018-04-24 02:05:22 +00:00
|
|
|
/* Validate the title we're loading is what we expect. */
|
2018-04-24 08:08:11 +00:00
|
|
|
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) {
|
2019-06-17 23:29:09 +00:00
|
|
|
return ResultLoaderInvalidProgramId;
|
2018-04-23 19:33:56 +00:00
|
|
|
}
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-04-24 02:05:22 +00:00
|
|
|
/* Validate that the ACI0 Kernel Capabilities are valid and restricted by the ACID Kernel Capabilities. */
|
2019-06-17 23:29:09 +00:00
|
|
|
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));
|
|
|
|
|
2018-04-24 02:05:22 +00:00
|
|
|
/* Read in all NSO headers, see what NSOs are present. */
|
2019-06-17 23:29:09 +00:00
|
|
|
R_TRY(NsoUtils::LoadNsoHeaders(npdm_info.aci0->title_id));
|
|
|
|
|
2018-04-24 02:05:22 +00:00
|
|
|
/* Validate that the set of NSOs to be loaded is correct. */
|
2019-06-17 23:29:09 +00:00
|
|
|
R_TRY(NsoUtils::ValidateNsoLoadSet());
|
|
|
|
|
2018-04-24 08:08:11 +00:00
|
|
|
/* Initialize the ProcessInfo. */
|
2019-06-17 23:29:09 +00:00
|
|
|
R_TRY(ProcessCreation::InitializeProcessInfo(&npdm_info, reslimit_h, arg_flags, &process_info));
|
|
|
|
|
2018-04-24 10:41:29 +00:00
|
|
|
/* Figure out where NSOs will be mapped, and how much space they (and arguments) will take up. */
|
2019-06-17 23:29:09 +00:00
|
|
|
R_TRY(NsoUtils::CalculateNsoLoadExtents(process_info.process_flags, launch_item != nullptr ? launch_item->arg_size : 0, &nso_extents));
|
|
|
|
|
2018-04-24 10:41:29 +00:00
|
|
|
/* 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;
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-04-24 08:12:19 +00:00
|
|
|
/* Call svcCreateProcess(). */
|
2019-06-17 23:29:09 +00:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-04-24 12:01:32 +00:00
|
|
|
/* Load all NSOs into Process memory, and set permissions accordingly. */
|
2019-06-17 23:29:09 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
R_TRY(NsoUtils::LoadNsosIntoProcessMemory(process_h, npdm_info.aci0->title_id, &nso_extents, launch_args, launch_args_size));
|
2018-04-24 12:01:32 +00:00
|
|
|
}
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-04-24 12:25:31 +00:00
|
|
|
/* Update the list of registered processes with the new process. */
|
|
|
|
svcGetProcessId(&process_id, process_h);
|
2018-04-26 20:53:33 +00:00
|
|
|
bool is_64_bit_addspace;
|
2019-05-10 10:25:07 +00:00
|
|
|
if ((GetRuntimeFirmwareVersion() >= FirmwareVersion_200)) {
|
2018-04-26 20:53:33 +00:00
|
|
|
is_64_bit_addspace = (((npdm_info.header->mmu_flags >> 1) & 5) | 2) == 3;
|
2018-04-26 20:51:07 +00:00
|
|
|
} else {
|
2018-04-26 20:53:33 +00:00
|
|
|
is_64_bit_addspace = (npdm_info.header->mmu_flags & 0xE) == 0x2;
|
2018-04-26 20:51:07 +00:00
|
|
|
}
|
2018-05-01 22:49:20 +00:00
|
|
|
Registration::SetProcessIdTidAndIs64BitAddressSpace(index, process_id, npdm_info.aci0->title_id, is_64_bit_addspace);
|
2018-04-24 12:25:31 +00:00
|
|
|
for (unsigned int i = 0; i < NSO_NUM_MAX; i++) {
|
2019-06-17 23:29:09 +00:00
|
|
|
if (NsoUtils::IsNsoPresent(i)) {
|
2019-04-21 01:15:39 +00:00
|
|
|
Registration::AddModuleInfo(index, nso_extents.nso_addresses[i], nso_extents.nso_sizes[i], NsoUtils::GetNsoBuildId(i));
|
2018-04-24 12:25:31 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-17 23:29:09 +00:00
|
|
|
|
2018-06-14 23:50:01 +00:00
|
|
|
/* Send the pid/tid pair to anyone interested in man-in-the-middle-attacking it. */
|
|
|
|
Registration::AssociatePidTidForMitM(index);
|
2019-03-20 14:53:56 +00:00
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
2019-01-07 00:30:05 +00:00
|
|
|
|
|
|
|
/* ECS is a one-shot operation, but we don't clear on failure. */
|
2018-10-25 19:52:01 +00:00
|
|
|
ContentManagement::ClearExternalContentSource(target_process->tid_sid.title_id);
|
2019-03-20 14:53:56 +00:00
|
|
|
|
2019-06-17 23:29:09 +00:00
|
|
|
/* Cancel the process handle guard. */
|
|
|
|
proc_handle_guard.Cancel();
|
2019-03-24 01:27:53 +00:00
|
|
|
|
2019-06-17 23:29:09 +00:00
|
|
|
/* Write process handle to output. */
|
|
|
|
*out_process_h = process_h;
|
|
|
|
return ResultSuccess;
|
2018-04-22 01:52:49 +00:00
|
|
|
}
|