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/>.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
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 = {};
|
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;
|
|
|
|
|
|
|
|
/* Set title id. */
|
|
|
|
out_proc_info->title_id = npdm->aci0->title_id;
|
|
|
|
|
|
|
|
/* Set process category. */
|
|
|
|
out_proc_info->process_category = npdm->header->process_category;
|
|
|
|
|
|
|
|
/* Copy reslimit handle raw. */
|
|
|
|
out_proc_info->reslimit_h = reslimit_h;
|
|
|
|
|
|
|
|
/* 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-01-31 12:46:20 +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
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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-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;
|
2018-07-29 23:35:43 +00:00
|
|
|
bool mounted_code = false;
|
2018-04-23 19:33:56 +00:00
|
|
|
Result rc;
|
|
|
|
|
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);
|
2018-04-23 19:33:56 +00:00
|
|
|
if (target_process == NULL) {
|
2019-03-28 22:06:50 +00:00
|
|
|
return ResultLoaderProcessNotRegistered;
|
2018-04-23 19:33:56 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 02:05:22 +00:00
|
|
|
/* Mount the title's exefs. */
|
2018-07-28 02:53:20 +00:00
|
|
|
if (target_process->tid_sid.storage_id != FsStorageId_None) {
|
|
|
|
rc = ContentManagement::MountCodeForTidSid(&target_process->tid_sid);
|
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
return rc;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2018-04-24 02:05:22 +00:00
|
|
|
/* Load the process's NPDM. */
|
2018-04-24 08:08:11 +00:00
|
|
|
rc = NpdmUtils::LoadNpdmFromCache(target_process->tid_sid.title_id, &npdm_info);
|
2018-04-23 19:33:56 +00:00
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
goto CREATE_PROCESS_END;
|
|
|
|
}
|
|
|
|
|
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-03-28 22:06:50 +00:00
|
|
|
rc = ResultLoaderInvalidProgramId;
|
2018-04-23 19:33:56 +00:00
|
|
|
goto CREATE_PROCESS_END;
|
|
|
|
}
|
|
|
|
|
2018-04-24 02:05:22 +00:00
|
|
|
/* Validate that the ACI0 Kernel Capabilities are valid and restricted by the ACID Kernel Capabilities. */
|
2018-04-24 08:08:11 +00:00
|
|
|
rc = NpdmUtils::ValidateCapabilities((u32 *)npdm_info.acid_kac, npdm_info.acid->kac_size/sizeof(u32), (u32 *)npdm_info.aci0_kac, npdm_info.aci0->kac_size/sizeof(u32));
|
2018-04-24 00:25:03 +00:00
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
goto CREATE_PROCESS_END;
|
|
|
|
}
|
2018-04-23 19:33:56 +00:00
|
|
|
|
2018-04-24 02:05:22 +00:00
|
|
|
/* Read in all NSO headers, see what NSOs are present. */
|
2018-04-24 08:08:11 +00:00
|
|
|
rc = NsoUtils::LoadNsoHeaders(npdm_info.aci0->title_id);
|
2018-04-24 02:05:22 +00:00
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
goto CREATE_PROCESS_END;
|
|
|
|
}
|
2018-04-23 19:33:56 +00:00
|
|
|
|
2018-04-24 02:05:22 +00:00
|
|
|
/* Validate that the set of NSOs to be loaded is correct. */
|
|
|
|
rc = NsoUtils::ValidateNsoLoadSet();
|
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
goto CREATE_PROCESS_END;
|
|
|
|
}
|
2018-04-23 19:33:56 +00:00
|
|
|
|
2018-04-24 08:08:11 +00:00
|
|
|
/* Initialize the ProcessInfo. */
|
|
|
|
rc = ProcessCreation::InitializeProcessInfo(&npdm_info, reslimit_h, arg_flags, &process_info);
|
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
goto CREATE_PROCESS_END;
|
|
|
|
}
|
2018-06-25 03:39:05 +00:00
|
|
|
|
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. */
|
2018-04-24 23:56:32 +00:00
|
|
|
rc = NsoUtils::CalculateNsoLoadExtents(process_info.process_flags, launch_item != NULL ? launch_item->arg_size : 0, &nso_extents);
|
2018-04-24 10:41:29 +00:00
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
goto CREATE_PROCESS_END;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
2018-05-01 22:49:20 +00:00
|
|
|
|
2018-04-24 08:12:19 +00:00
|
|
|
/* Call svcCreateProcess(). */
|
2018-04-24 09:14:40 +00:00
|
|
|
rc = svcCreateProcess(&process_h, &process_info, (u32 *)npdm_info.aci0_kac, npdm_info.aci0->kac_size/sizeof(u32));
|
2018-04-24 08:12:19 +00:00
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
goto CREATE_PROCESS_END;
|
|
|
|
}
|
2018-04-23 19:33:56 +00:00
|
|
|
|
2018-04-24 23:56:32 +00:00
|
|
|
|
2018-04-24 12:01:32 +00:00
|
|
|
/* Load all NSOs into Process memory, and set permissions accordingly. */
|
2018-04-24 23:56:32 +00:00
|
|
|
if (launch_item != NULL) {
|
2018-04-24 12:25:31 +00:00
|
|
|
rc = NsoUtils::LoadNsosIntoProcessMemory(process_h, npdm_info.aci0->title_id, &nso_extents, (u8 *)launch_item->args, launch_item->arg_size);
|
2018-04-24 12:01:32 +00:00
|
|
|
} else {
|
2018-04-24 12:25:31 +00:00
|
|
|
rc = NsoUtils::LoadNsosIntoProcessMemory(process_h, npdm_info.aci0->title_id, &nso_extents, NULL, 0);
|
|
|
|
}
|
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
goto CREATE_PROCESS_END;
|
2018-04-24 12:01:32 +00:00
|
|
|
}
|
2018-04-23 19:33:56 +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++) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2018-04-23 19:33:56 +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-29 05:39:39 +00:00
|
|
|
rc = ResultSuccess;
|
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-03-24 01:27:53 +00:00
|
|
|
|
|
|
|
CREATE_PROCESS_END:
|
2018-07-29 23:35:43 +00:00
|
|
|
if (mounted_code) {
|
|
|
|
if (R_SUCCEEDED(rc) && target_process->tid_sid.storage_id != FsStorageId_None) {
|
|
|
|
rc = ContentManagement::UnmountCode();
|
|
|
|
} else {
|
|
|
|
ContentManagement::UnmountCode();
|
|
|
|
}
|
2018-04-23 19:33:56 +00:00
|
|
|
}
|
2019-03-24 01:27:53 +00:00
|
|
|
|
2018-04-24 12:25:31 +00:00
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
*out_process_h = process_h;
|
|
|
|
} else {
|
|
|
|
svcCloseHandle(process_h);
|
|
|
|
}
|
2018-04-23 19:33:56 +00:00
|
|
|
return rc;
|
2018-04-22 01:52:49 +00:00
|
|
|
}
|