loader: update for 10.0.0

This commit is contained in:
Michael Scire 2020-04-14 02:45:28 -07:00
parent dd80e1f463
commit 73552c86c3
22 changed files with 486 additions and 56 deletions

View file

@ -24,11 +24,11 @@ namespace ams::ldr {
}
/* ScopedCodeMount functionality. */
ScopedCodeMount::ScopedCodeMount(const ncm::ProgramLocation &loc) : lk(g_scoped_code_mount_lock), has_status(false), mounted_ams(false), mounted_code(false) {
ScopedCodeMount::ScopedCodeMount(const ncm::ProgramLocation &loc) : lk(g_scoped_code_mount_lock), has_status(false), mounted_ams(false), mounted_sd_or_code(false), mounted_code(false) {
this->result = this->Initialize(loc);
}
ScopedCodeMount::ScopedCodeMount(const ncm::ProgramLocation &loc, const cfg::OverrideStatus &o) : lk(g_scoped_code_mount_lock), override_status(o), has_status(true), mounted_ams(false), mounted_code(false) {
ScopedCodeMount::ScopedCodeMount(const ncm::ProgramLocation &loc, const cfg::OverrideStatus &o) : lk(g_scoped_code_mount_lock), override_status(o), has_status(true), mounted_ams(false), mounted_sd_or_code(false), mounted_code(false) {
this->result = this->Initialize(loc);
}
@ -37,6 +37,9 @@ namespace ams::ldr {
if (this->mounted_ams) {
fs::Unmount(AtmosphereCodeMountName);
}
if (this->mounted_sd_or_code) {
fs::Unmount(SdOrCodeMountName);
}
if (this->mounted_code) {
fs::Unmount(CodeMountName);
}
@ -54,12 +57,17 @@ namespace ams::ldr {
}
/* Mount the atmosphere code file system. */
R_TRY(fs::MountCodeForAtmosphereWithRedirection(AtmosphereCodeMountName, content_path, loc.program_id, this->override_status.IsHbl(), this->override_status.IsProgramSpecific()));
R_TRY(fs::MountCodeForAtmosphereWithRedirection(std::addressof(this->ams_code_info), AtmosphereCodeMountName, content_path, loc.program_id, this->override_status.IsHbl(), this->override_status.IsProgramSpecific()));
this->mounted_ams = true;
/* Mount the sd or base code file system. */
R_TRY(fs::MountCodeForAtmosphere(std::addressof(this->sd_or_base_code_info), SdOrCodeMountName, content_path, loc.program_id));
this->mounted_sd_or_code = true;
/* Mount the base code file system. */
R_TRY(fs::MountCodeForAtmosphere(CodeMountName, content_path, loc.program_id));
this->mounted_code = true;
if (R_SUCCEEDED(fs::MountCode(std::addressof(this->base_code_info), CodeMountName, content_path, loc.program_id))) {
this->mounted_code = true;
}
return ResultSuccess();
}

View file

@ -25,9 +25,13 @@ namespace ams::ldr {
private:
std::scoped_lock<os::Mutex> lk;
cfg::OverrideStatus override_status;
fs::CodeInfo ams_code_info;
fs::CodeInfo sd_or_base_code_info;
fs::CodeInfo base_code_info;
Result result;
bool has_status;
bool mounted_ams;
bool mounted_sd_or_code;
bool mounted_code;
public:
ScopedCodeMount(const ncm::ProgramLocation &loc);
@ -42,15 +46,29 @@ namespace ams::ldr {
AMS_ABORT_UNLESS(this->has_status);
return this->override_status;
}
const fs::CodeInfo &GetAtmosphereCodeInfo() const {
return this->ams_code_info;
}
const fs::CodeInfo &GetSdOrBaseCodeInfo() const {
return this->sd_or_base_code_info;
}
const fs::CodeInfo &GetCodeInfo() const {
return this->base_code_info;
}
private:
Result Initialize(const ncm::ProgramLocation &loc);
void EnsureOverrideStatus(const ncm::ProgramLocation &loc);
};
constexpr inline const char * const AtmosphereCodeMountName = "ams-code";
constexpr inline const char * const SdOrCodeMountName = "sd-code";
constexpr inline const char * const CodeMountName = "code";
#define ENCODE_ATMOSPHERE_CODE_PATH(relative) "ams-code:" relative
#define ENCODE_SD_OR_CODE_PATH(relative) "sd-code:" relative
#define ENCODE_CODE_PATH(relative) "code:" relative
/* Redirection API. */

View file

@ -0,0 +1,63 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* 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/>.
*/
#include "ldr_launch_record.hpp"
namespace ams::ldr {
namespace {
bool g_development_for_acid_production_check = false;
bool g_development_for_anti_downgrade_check = false;
bool g_development_for_acid_signature_check = false;
bool g_enabled_program_verification = true;
}
void SetDevelopmentForAcidProductionCheck(bool development) {
g_development_for_acid_production_check = development;
}
void SetDevelopmentForAntiDowngradeCheck(bool development) {
g_development_for_anti_downgrade_check = development;
}
void SetDevelopmentForAcidSignatureCheck(bool development) {
g_development_for_acid_signature_check = development;
}
void SetEnabledProgramVerification(bool enabled) {
if (g_development_for_acid_signature_check) {
g_enabled_program_verification = enabled;
}
}
bool IsDevelopmentForAcidProductionCheck() {
return g_development_for_acid_production_check;
}
bool IsDevelopmentForAntiDowngradeCheck() {
return g_development_for_anti_downgrade_check;
}
bool IsDevelopmentForAcidSignatureCheck() {
return g_development_for_acid_signature_check;
}
bool IsEnabledProgramVerification() {
return g_enabled_program_verification;
}
}

View file

@ -0,0 +1,32 @@
/*
* Copyright (c) 2018-2020 Atmosphère-NX
*
* 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/>.
*/
#pragma once
#include <stratosphere.hpp>
namespace ams::ldr {
/* Development Manager API. */
void SetDevelopmentForAcidProductionCheck(bool development);
void SetDevelopmentForAntiDowngradeCheck(bool development);
void SetDevelopmentForAcidSignatureCheck(bool development);
void SetEnabledProgramVerification(bool enabled);
bool IsDevelopmentForAcidProductionCheck();
bool IsDevelopmentForAntiDowngradeCheck();
bool IsDevelopmentForAcidSignatureCheck();
bool IsEnabledProgramVerification();
}

View file

@ -15,6 +15,7 @@
*/
#include "ldr_arguments.hpp"
#include "ldr_content_management.hpp"
#include "ldr_development_manager.hpp"
#include "ldr_process_creation.hpp"
#include "ldr_launch_record.hpp"
#include "ldr_loader_service.hpp"
@ -95,6 +96,10 @@ namespace ams::ldr {
return ldr::ro::GetProcessModuleInfo(count.GetPointer(), out.GetPointer(), out.GetSize(), process_id);
}
Result LoaderService::SetEnabledProgramVerification(bool enabled) {
ldr::SetEnabledProgramVerification(enabled);
}
/* Atmosphere commands. */
Result LoaderService::AtmosphereRegisterExternalCode(sf::OutMoveHandle out, ncm::ProgramId program_id) {
return fssystem::CreateExternalCode(out.GetHandlePointer(), program_id);

View file

@ -28,6 +28,7 @@ namespace ams::ldr {
virtual Result SetProgramArguments(ncm::ProgramId program_id, const sf::InPointerBuffer &args, u32 args_size);
virtual Result FlushArguments();
virtual Result GetProcessModuleInfo(sf::Out<u32> count, const sf::OutPointerArray<ModuleInfo> &out, os::ProcessId process_id);
virtual Result SetEnabledProgramVerification(bool enabled);
/* Atmosphere commands. */
virtual Result AtmosphereRegisterExternalCode(sf::OutMoveHandle out, ncm::ProgramId program_id);
@ -42,10 +43,11 @@ namespace ams::ldr {
class ProcessManagerInterface final : public LoaderService {
protected:
enum class CommandId {
CreateProcess = 0,
GetProgramInfo = 1,
PinProgram = 2,
UnpinProgram = 3,
CreateProcess = 0,
GetProgramInfo = 1,
PinProgram = 2,
UnpinProgram = 3,
SetEnabledProgramVerification = 4,
AtmosphereHasLaunchedProgram = 65000,
AtmosphereGetProgramInfo = 65001,
@ -57,6 +59,7 @@ namespace ams::ldr {
MAKE_SERVICE_COMMAND_META(GetProgramInfo),
MAKE_SERVICE_COMMAND_META(PinProgram),
MAKE_SERVICE_COMMAND_META(UnpinProgram),
MAKE_SERVICE_COMMAND_META(SetEnabledProgramVerification, hos::Version_10_0_0),
MAKE_SERVICE_COMMAND_META(AtmosphereHasLaunchedProgram),
MAKE_SERVICE_COMMAND_META(AtmosphereGetProgramInfo),

View file

@ -14,6 +14,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ldr_development_manager.hpp"
#include "ldr_loader_service.hpp"
extern "C" {
@ -74,6 +75,7 @@ void __appInit(void) {
R_ABORT_UNLESS(fsInitialize());
lr::Initialize();
R_ABORT_UNLESS(fsldrInitialize());
R_ABORT_UNLESS(splInitialize());
});
ams::CheckApiVersion();
@ -81,6 +83,7 @@ void __appInit(void) {
void __appExit(void) {
/* Cleanup services. */
splExit();
fsldrExit();
lr::Finalize();
fsExit();
@ -112,6 +115,12 @@ namespace {
int main(int argc, char **argv)
{
/* Configure development. */
/* NOTE: Nintendo really does call the getter function three times instead of caching the value. */
ldr::SetDevelopmentForAcidProductionCheck(spl::IsDevelopmentHardware());
ldr::SetDevelopmentForAntiDowngradeCheck(spl::IsDevelopmentHardware());
ldr::SetDevelopmentForAcidSignatureCheck(spl::IsDevelopmentHardware());
/* Add services to manager. */
R_ABORT_UNLESS((g_server_manager.RegisterServer<ldr::pm::ProcessManagerInterface>(ProcessManagerServiceName, ProcessManagerMaxSessions)));
R_ABORT_UNLESS((g_server_manager.RegisterServer<ldr::shell::ShellInterface>(ShellServiceName, ShellMaxSessions)));

View file

@ -15,6 +15,7 @@
*/
#include "ldr_capabilities.hpp"
#include "ldr_content_management.hpp"
#include "ldr_development_manager.hpp"
#include "ldr_meta.hpp"
namespace ams::ldr {
@ -24,6 +25,7 @@ namespace ams::ldr {
/* Convenience definitions. */
constexpr size_t MetaCacheBufferSize = 0x8000;
constexpr inline const char AtmosphereMetaPath[] = ENCODE_ATMOSPHERE_CODE_PATH("/main.npdm");
constexpr inline const char SdOrBaseMetaPath[] = ENCODE_SD_OR_CODE_PATH("/main.npdm");
constexpr inline const char BaseMetaPath[] = ENCODE_CODE_PATH("/main.npdm");
/* Types. */
@ -72,7 +74,10 @@ namespace ams::ldr {
/* Validate magic. */
R_UNLESS(acid->magic == Acid::Magic, ResultInvalidMeta());
/* TODO: Check if retail flag is set if not development hardware. */
/* Validate that the acid is for production if not development. */
if (!IsDevelopmentForAcidProductionCheck()) {
R_UNLESS((acid->flags & Acid::AcidFlag_Production) != 0, ResultInvalidMeta());
}
/* Validate Fac, Sac, Kac. */
R_TRY(ValidateSubregion(sizeof(Acid), size, acid->fac_offset, acid->fac_size));
@ -94,6 +99,39 @@ namespace ams::ldr {
return ResultSuccess();
}
const u8 *GetAcidSignatureModulus(u32 key_generation) {
AMS_ASSERT(key_generation <= fssystem::AcidSignatureKeyGenerationMax);
const u32 used_keygen = (key_generation % (fssystem::AcidSignatureKeyGenerationMax + 1));
if (IsDevelopmentForAcidSignatureCheck()) {
return fssystem::AcidSignatureKeyModulusDev[used_keygen];
} else {
return fssystem::AcidSignatureKeyModulusProd[used_keygen];
}
}
Result ValidateAcidSignature(Meta *meta) {
/* Loader did not check signatures prior to 10.0.0. */
if (hos::GetVersion() < hos::Version_10_0_0) {
meta->is_signed = false;
return ResultSuccess();
}
/* Verify the signature. */
const u8 *sig = meta->acid->signature;
const size_t sig_size = sizeof(meta->acid->signature);
const u8 *mod = GetAcidSignatureModulus(meta->npdm->signature_key_generation);
const size_t mod_size = fssystem::AcidSignatureKeyModulusSize;
const u8 *exp = fssystem::AcidSignatureKeyExponent;
const size_t exp_size = fssystem::AcidSignatureKeyExponentSize;
const u8 *msg = meta->acid->modulus;
const size_t msg_size = meta->acid->size;
const bool is_signature_valid = crypto::VerifyRsa2048PssSha256(sig, sig_size, mod, mod_size, exp, exp_size, msg, msg_size);
R_UNLESS(is_signature_valid || !IsEnabledProgramVerification(), ResultInvalidAcidSignature());
meta->is_signed = is_signature_valid;
return ResultSuccess();
}
Result LoadMetaFromFile(fs::FileHandle file, MetaCache *cache) {
/* Reset cache. */
cache->meta = {};
@ -136,6 +174,8 @@ namespace ams::ldr {
meta->aci_fah = reinterpret_cast<u8 *>(aci) + aci->fah_offset;
meta->aci_sac = reinterpret_cast<u8 *>(aci) + aci->sac_offset;
meta->aci_kac = reinterpret_cast<u8 *>(aci) + aci->kac_offset;
meta->modulus = acid->modulus;
}
return ResultSuccess();
@ -144,7 +184,7 @@ namespace ams::ldr {
}
/* API. */
Result LoadMeta(Meta *out_meta, ncm::ProgramId program_id, const cfg::OverrideStatus &status) {
Result LoadMeta(Meta *out_meta, const ncm::ProgramLocation &loc, const cfg::OverrideStatus &status) {
/* Try to load meta from file. */
fs::FileHandle file;
R_TRY(fs::OpenFile(std::addressof(file), AtmosphereMetaPath, fs::OpenMode_Read));
@ -155,13 +195,13 @@ namespace ams::ldr {
/* Patch meta. Start by setting all program ids to the current program id. */
Meta *meta = &g_meta_cache.meta;
meta->acid->program_id_min = program_id;
meta->acid->program_id_max = program_id;
meta->aci->program_id = program_id;
meta->acid->program_id_min = loc.program_id;
meta->acid->program_id_max = loc.program_id;
meta->aci->program_id = loc.program_id;
/* For HBL, we need to copy some information from the base meta. */
if (status.IsHbl()) {
if (R_SUCCEEDED(fs::OpenFile(std::addressof(file), BaseMetaPath, fs::OpenMode_Read))) {
if (R_SUCCEEDED(fs::OpenFile(std::addressof(file), SdOrBaseMetaPath, fs::OpenMode_Read))) {
ON_SCOPE_EXIT { fs::CloseFile(file); };
if (R_SUCCEEDED(LoadMetaFromFile(file, &g_original_meta_cache))) {
Meta *o_meta = &g_original_meta_cache.meta;
@ -177,19 +217,29 @@ namespace ams::ldr {
caps::SetProgramInfoFlags(program_info_flags, meta->aci_kac, meta->aci->kac_size);
}
}
} else if (hos::GetVersion() >= hos::Version_10_0_0) {
/* If storage id is none, there is no base code filesystem, and thus it is impossible for us to validate. */
if (static_cast<ncm::StorageId>(loc.storage_id) != ncm::StorageId::None) {
R_TRY(fs::OpenFile(std::addressof(file), BaseMetaPath, fs::OpenMode_Read));
ON_SCOPE_EXIT { fs::CloseFile(file); };
R_TRY(LoadMetaFromFile(file, &g_original_meta_cache));
R_TRY(ValidateAcidSignature(&g_original_meta_cache.meta));
meta->modulus = g_original_meta_cache.meta.modulus;
meta->is_signed = g_original_meta_cache.meta.is_signed;
}
}
/* Set output. */
g_cached_program_id = program_id;
g_cached_program_id = loc.program_id;
g_cached_override_status = status;
*out_meta = *meta;
return ResultSuccess();
}
Result LoadMetaFromCache(Meta *out_meta, ncm::ProgramId program_id, const cfg::OverrideStatus &status) {
if (g_cached_program_id != program_id || g_cached_override_status != status) {
return LoadMeta(out_meta, program_id, status);
Result LoadMetaFromCache(Meta *out_meta, const ncm::ProgramLocation &loc, const cfg::OverrideStatus &status) {
if (g_cached_program_id != loc.program_id || g_cached_override_status != status) {
return LoadMeta(out_meta, loc, status);
}
*out_meta = g_meta_cache.meta;
return ResultSuccess();

View file

@ -30,11 +30,14 @@ namespace ams::ldr {
void *aci_fah;
void *aci_sac;
void *aci_kac;
void *modulus;
bool is_signed;
};
/* Meta API. */
Result LoadMeta(Meta *out_meta, ncm::ProgramId program_id, const cfg::OverrideStatus &status);
Result LoadMetaFromCache(Meta *out_meta, ncm::ProgramId program_id, const cfg::OverrideStatus &status);
Result LoadMeta(Meta *out_meta, const ncm::ProgramLocation &loc, const cfg::OverrideStatus &status);
Result LoadMetaFromCache(Meta *out_meta, const ncm::ProgramLocation &loc, const cfg::OverrideStatus &status);
void InvalidateMetaCache();
}

View file

@ -15,6 +15,7 @@
*/
#include "ldr_capabilities.hpp"
#include "ldr_content_management.hpp"
#include "ldr_development_manager.hpp"
#include "ldr_launch_record.hpp"
#include "ldr_meta.hpp"
#include "ldr_patcher.hpp"
@ -86,6 +87,9 @@ namespace ams::ldr {
/* No version verification is done before 8.1.0. */
R_SUCCEED_IF(hos::GetVersion() < hos::Version_8_1_0);
/* No verification is done if development. */
R_SUCCEED_IF(IsDevelopmentForAntiDowngradeCheck());
/* Do version-dependent validation, if compiled to do so. */
#ifdef LDR_VALIDATE_PROCESS_VERSION
const MinimumProgramVersion *entries = nullptr;
@ -210,7 +214,7 @@ namespace ams::ldr {
return ResultSuccess();
}
Result ValidateMeta(const Meta *meta, const ncm::ProgramLocation &loc) {
Result ValidateMeta(const Meta *meta, const ncm::ProgramLocation &loc, const fs::CodeInfo &code_info) {
/* Validate version. */
R_TRY(ValidateProgramVersion(loc.program_id, meta->npdm->version));
@ -221,6 +225,21 @@ namespace ams::ldr {
/* Validate the kernel capabilities. */
R_TRY(caps::ValidateCapabilities(meta->acid_kac, meta->acid->kac_size, meta->aci_kac, meta->aci->kac_size));
/* If we have data to validate, validate it. */
if (code_info.is_signed && meta->is_signed) {
const u8 *sig = code_info.signature;
const size_t sig_size = sizeof(code_info.signature);
const u8 *mod = static_cast<u8 *>(meta->modulus);
const size_t mod_size = crypto::Rsa2048PssSha256Verifier::ModulusSize;
const u8 *exp = fssystem::AcidSignatureKeyExponent;
const size_t exp_size = fssystem::AcidSignatureKeyExponentSize;
const u8 *hsh = code_info.hash;
const size_t hsh_size = sizeof(code_info.hash);
const bool is_signature_valid = crypto::VerifyRsa2048PssSha256WithHash(sig, sig_size, mod, mod_size, exp, exp_size, hsh, hsh_size);
R_UNLESS(is_signature_valid, ResultInvalidNcaSignature());
}
/* All good. */
return ResultSuccess();
}
@ -578,10 +597,10 @@ namespace ams::ldr {
/* Load meta, possibly from cache. */
Meta meta;
R_TRY(LoadMetaFromCache(&meta, loc.program_id, override_status));
R_TRY(LoadMetaFromCache(&meta, loc, override_status));
/* Validate meta. */
R_TRY(ValidateMeta(&meta, loc));
R_TRY(ValidateMeta(&meta, loc, mount.GetCodeInfo()));
/* Load, validate NSOs. */
R_TRY(LoadNsoHeaders(nso_headers, has_nso));
@ -636,7 +655,7 @@ namespace ams::ldr {
{
ScopedCodeMount mount(loc);
R_TRY(mount.GetResult());
R_TRY(LoadMeta(&meta, loc.program_id, mount.GetOverrideStatus()));
R_TRY(LoadMeta(&meta, loc, mount.GetOverrideStatus()));
if (out_status != nullptr) {
*out_status = mount.GetOverrideStatus();
}