From 73552c86c3fd3cbca2ed4efd46bfb10e63b1770d Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Tue, 14 Apr 2020 02:45:28 -0700 Subject: [PATCH] loader: update for 10.0.0 --- .../include/stratosphere/fs/fs_code.hpp | 14 ++- .../include/stratosphere/fssystem.hpp | 1 + .../fssystem/fssystem_acid_sign_key.hpp | 111 ++++++++++++++++++ .../include/stratosphere/ldr/ldr_pm_api.hpp | 1 + .../include/stratosphere/ldr/ldr_types.hpp | 3 +- .../libstratosphere/source/fs/fs_code.cpp | 34 ++++-- .../libstratosphere/source/ldr/ldr_pm_api.cpp | 4 + .../crypto/crypto_rsa_pss_sha256_verifier.hpp | 16 +++ .../crypto/crypto_rsa_pss_verifier.hpp | 73 +++++++++++- .../crypto/impl/crypto_rsa_pss_impl.hpp | 19 +-- .../vapours/results/loader_results.hpp | 2 + .../loader/source/ldr_content_management.cpp | 18 ++- .../loader/source/ldr_content_management.hpp | 18 +++ .../loader/source/ldr_development_manager.cpp | 63 ++++++++++ .../loader/source/ldr_development_manager.hpp | 32 +++++ .../loader/source/ldr_loader_service.cpp | 5 + .../loader/source/ldr_loader_service.hpp | 11 +- stratosphere/loader/source/ldr_main.cpp | 9 ++ stratosphere/loader/source/ldr_meta.cpp | 70 +++++++++-- stratosphere/loader/source/ldr_meta.hpp | 7 +- .../loader/source/ldr_process_creation.cpp | 27 ++++- stratosphere/spl/source/spl_main.cpp | 4 +- 22 files changed, 486 insertions(+), 56 deletions(-) create mode 100644 libraries/libstratosphere/include/stratosphere/fssystem/fssystem_acid_sign_key.hpp create mode 100644 stratosphere/loader/source/ldr_development_manager.cpp create mode 100644 stratosphere/loader/source/ldr_development_manager.hpp diff --git a/libraries/libstratosphere/include/stratosphere/fs/fs_code.hpp b/libraries/libstratosphere/include/stratosphere/fs/fs_code.hpp index cb5caf5a3..6dde7c038 100644 --- a/libraries/libstratosphere/include/stratosphere/fs/fs_code.hpp +++ b/libraries/libstratosphere/include/stratosphere/fs/fs_code.hpp @@ -19,9 +19,17 @@ namespace ams::fs { - Result MountCode(const char *name, const char *path, ncm::ProgramId program_id); + struct CodeInfo { + u8 signature[crypto::Rsa2048PssSha256Verifier::SignatureSize]; + u8 hash[crypto::Rsa2048PssSha256Verifier::HashSize]; + bool is_signed; + u8 reserved[3]; + }; + static_assert(sizeof(CodeInfo) == crypto::Rsa2048PssSha256Verifier::SignatureSize + crypto::Rsa2048PssSha256Verifier::HashSize + 4); - Result MountCodeForAtmosphereWithRedirection(const char *name, const char *path, ncm::ProgramId program_id, bool is_hbl, bool is_specific); - Result MountCodeForAtmosphere(const char *name, const char *path, ncm::ProgramId program_id); + Result MountCode(CodeInfo *out, const char *name, const char *path, ncm::ProgramId program_id); + + Result MountCodeForAtmosphereWithRedirection(CodeInfo *out, const char *name, const char *path, ncm::ProgramId program_id, bool is_hbl, bool is_specific); + Result MountCodeForAtmosphere(CodeInfo *out, const char *name, const char *path, ncm::ProgramId program_id); } diff --git a/libraries/libstratosphere/include/stratosphere/fssystem.hpp b/libraries/libstratosphere/include/stratosphere/fssystem.hpp index 60a703ea8..10385e0e6 100644 --- a/libraries/libstratosphere/include/stratosphere/fssystem.hpp +++ b/libraries/libstratosphere/include/stratosphere/fssystem.hpp @@ -17,6 +17,7 @@ #pragma once #include #include +#include #include #include #include diff --git a/libraries/libstratosphere/include/stratosphere/fssystem/fssystem_acid_sign_key.hpp b/libraries/libstratosphere/include/stratosphere/fssystem/fssystem_acid_sign_key.hpp new file mode 100644 index 000000000..716d50611 --- /dev/null +++ b/libraries/libstratosphere/include/stratosphere/fssystem/fssystem_acid_sign_key.hpp @@ -0,0 +1,111 @@ +/* + * 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 . + */ +#pragma once +#include + +namespace ams::fssystem { + + constexpr inline size_t AcidSignatureKeyGenerationMax = 1; + + constexpr inline size_t AcidSignatureKeyModulusSize = 0x100; + + constexpr inline const u8 AcidSignatureKeyModulusDev[AcidSignatureKeyGenerationMax + 1][AcidSignatureKeyModulusSize] = { + { + 0xD6, 0x34, 0xA5, 0x78, 0x6C, 0x68, 0xCE, 0x5A, 0xC2, 0x37, 0x17, 0xF3, 0x82, 0x45, 0xC6, 0x89, + 0xE1, 0x2D, 0x06, 0x67, 0xBF, 0xB4, 0x06, 0x19, 0x55, 0x6B, 0x27, 0x66, 0x0C, 0xA4, 0xB5, 0x87, + 0x81, 0x25, 0xF4, 0x30, 0xBC, 0x53, 0x08, 0x68, 0xA2, 0x48, 0x49, 0x8C, 0x3F, 0x38, 0x40, 0x9C, + 0xC4, 0x26, 0xF4, 0x79, 0xE2, 0xA1, 0x85, 0xF5, 0x5C, 0x7F, 0x58, 0xBA, 0xA6, 0x1C, 0xA0, 0x8B, + 0x84, 0x16, 0x14, 0x6F, 0x85, 0xD9, 0x7C, 0xE1, 0x3C, 0x67, 0x22, 0x1E, 0xFB, 0xD8, 0xA7, 0xA5, + 0x9A, 0xBF, 0xEC, 0x0E, 0xCF, 0x96, 0x7E, 0x85, 0xC2, 0x1D, 0x49, 0x5D, 0x54, 0x26, 0xCB, 0x32, + 0x7C, 0xF6, 0xBB, 0x58, 0x03, 0x80, 0x2B, 0x5D, 0xF7, 0xFB, 0xD1, 0x9D, 0xC7, 0xC6, 0x2E, 0x53, + 0xC0, 0x6F, 0x39, 0x2C, 0x1F, 0xA9, 0x92, 0xF2, 0x4D, 0x7D, 0x4E, 0x74, 0xFF, 0xE4, 0xEF, 0xE4, + 0x7C, 0x3D, 0x34, 0x2A, 0x71, 0xA4, 0x97, 0x59, 0xFF, 0x4F, 0xA2, 0xF4, 0x66, 0x78, 0xD8, 0xBA, + 0x99, 0x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0xEA, 0xD0, 0x78, 0x78, 0xA0, 0x2E, 0xC6, 0x32, 0x6B + } + }; + + constexpr inline const u8 AcidSignatureKeyModulusProd[AcidSignatureKeyGenerationMax + 1][AcidSignatureKeyModulusSize] = { + { + 0xDD, 0xC8, 0xDD, 0xF2, 0x4E, 0x6D, 0xF0, 0xCA, 0x9E, 0xC7, 0x5D, 0xC7, 0x7B, 0xAD, 0xFE, 0x7D, + 0x23, 0x89, 0x69, 0xB6, 0xF2, 0x06, 0xA2, 0x02, 0x88, 0xE1, 0x55, 0x91, 0xAB, 0xCB, 0x4D, 0x50, + 0x2E, 0xFC, 0x9D, 0x94, 0x76, 0xD6, 0x4C, 0xD8, 0xFF, 0x10, 0xFA, 0x5E, 0x93, 0x0A, 0xB4, 0x57, + 0xAC, 0x51, 0xC7, 0x16, 0x66, 0xF4, 0x1A, 0x54, 0xC2, 0xC5, 0x04, 0x3D, 0x1B, 0xFE, 0x30, 0x20, + 0x8A, 0xAC, 0x6F, 0x6F, 0xF5, 0xC7, 0xB6, 0x68, 0xB8, 0xC9, 0x40, 0x6B, 0x42, 0xAD, 0x11, 0x21, + 0xE7, 0x8B, 0xE9, 0x75, 0x01, 0x86, 0xE4, 0x48, 0x9B, 0x0A, 0x0A, 0xF8, 0x7F, 0xE8, 0x87, 0xF2, + 0x82, 0x01, 0xE6, 0xA3, 0x0F, 0xE4, 0x66, 0xAE, 0x83, 0x3F, 0x4E, 0x9F, 0x5E, 0x01, 0x30, 0xA4, + 0x00, 0xB9, 0x9A, 0xAE, 0x5F, 0x03, 0xCC, 0x18, 0x60, 0xE5, 0xEF, 0x3B, 0x5E, 0x15, 0x16, 0xFE, + 0x1C, 0x82, 0x78, 0xB5, 0x2F, 0x47, 0x7C, 0x06, 0x66, 0x88, 0x5D, 0x35, 0xA2, 0x67, 0x20, 0x10, + 0xE7, 0x6C, 0x43, 0x68, 0xD3, 0xE4, 0x5A, 0x68, 0x2A, 0x5A, 0xE2, 0x6D, 0x73, 0xB0, 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0x41, 0x56, 0x25, 0x17, 0x18, 0x42, 0x4D, 0x8C, 0xAC, 0xA5, 0x7B, 0xDB, 0x42, 0x5D, + 0x59, 0x35, 0x45, 0x5D, 0x8A, 0x02, 0xB5, 0x70, 0xC0, 0x72, 0x35, 0x46, 0xD0, 0x1D, 0x60, 0x01, + 0x4A, 0xCC, 0x1C, 0x46, 0xD3, 0xD6, 0x35, 0x52, 0xD6, 0xE1, 0xF8, 0x3B, 0x5D, 0xEA, 0xDD, 0xB8, + 0xFE, 0x7D, 0x50, 0xCB, 0x35, 0x23, 0x67, 0x8B, 0xB6, 0xE4, 0x74, 0xD2, 0x60, 0xFC, 0xFD, 0x43, + 0xBF, 0x91, 0x08, 0x81, 0xC5, 0x4F, 0x5D, 0x16, 0x9A, 0xC4, 0x9A, 0xC6, 0xF6, 0xF3, 0xE1, 0xF6, + 0x5C, 0x07, 0xAA, 0x71, 0x6C, 0x13, 0xA4, 0xB1, 0xB3, 0x66, 0xBF, 0x90, 0x4C, 0x3D, 0xA2, 0xC4, + 0x0B, 0xB8, 0x3D, 0x7A, 0x8C, 0x19, 0xFA, 0xFF, 0x6B, 0xB9, 0x1F, 0x02, 0xCC, 0xB6, 0xD3, 0x0C, + 0x7D, 0x19, 0x1F, 0x47, 0xF9, 0xC7, 0x40, 0x01, 0xFA, 0x46, 0xEA, 0x0B, 0xD4, 0x02, 0xE0, 0x3D, + 0x30, 0x9A, 0x1A, 0x0F, 0xEA, 0xA7, 0x66, 0x55, 0xF7, 0xCB, 0x28, 0xE2, 0xBB, 0x99, 0xE4, 0x83, + 0xC3, 0x43, 0x03, 0xEE, 0xDC, 0x1F, 0x02, 0x23, 0xDD, 0xD1, 0x2D, 0x39, 0xA4, 0x65, 0x75, 0x03, + 0xEF, 0x37, 0x9C, 0x06, 0xD6, 0xFA, 0xA1, 0x15, 0xF0, 0xDB, 0x17, 0x47, 0x26, 0x4F, 0x49, 0x03 + } + }; + + static_assert(sizeof(AcidSignatureKeyModulusProd) == sizeof(AcidSignatureKeyModulusDev)); + + constexpr inline const u8 AcidSignatureKeyExponent[] = { + 0x01, 0x00, 0x01 + }; + + constexpr inline size_t AcidSignatureKeyExponentSize = util::size(AcidSignatureKeyExponent); + +} diff --git a/libraries/libstratosphere/include/stratosphere/ldr/ldr_pm_api.hpp b/libraries/libstratosphere/include/stratosphere/ldr/ldr_pm_api.hpp index 1b1947557..a541310b7 100644 --- a/libraries/libstratosphere/include/stratosphere/ldr/ldr_pm_api.hpp +++ b/libraries/libstratosphere/include/stratosphere/ldr/ldr_pm_api.hpp @@ -24,6 +24,7 @@ namespace ams::ldr::pm { Result GetProgramInfo(ProgramInfo *out, const ncm::ProgramLocation &loc); Result PinProgram(PinId *out, const ncm::ProgramLocation &loc); Result UnpinProgram(PinId pin_id); + Result SetEnabledProgramVerification(bool enabled); Result HasLaunchedProgram(bool *out, ncm::ProgramId program_id); /* Atmosphere extension API. */ diff --git a/libraries/libstratosphere/include/stratosphere/ldr/ldr_types.hpp b/libraries/libstratosphere/include/stratosphere/ldr/ldr_types.hpp index 462037f40..933fc1d99 100644 --- a/libraries/libstratosphere/include/stratosphere/ldr/ldr_types.hpp +++ b/libraries/libstratosphere/include/stratosphere/ldr/ldr_types.hpp @@ -221,7 +221,8 @@ namespace ams::ldr { }; u32 magic; - u8 reserved_04[8]; + u32 signature_key_generation; + u8 reserved_08[4]; u8 flags; u8 reserved_0D; u8 main_thread_priority; diff --git a/libraries/libstratosphere/source/fs/fs_code.cpp b/libraries/libstratosphere/source/fs/fs_code.cpp index fdc2bd126..c17245ef2 100644 --- a/libraries/libstratosphere/source/fs/fs_code.cpp +++ b/libraries/libstratosphere/source/fs/fs_code.cpp @@ -20,14 +20,15 @@ namespace ams::fs { namespace { - Result OpenCodeFileSystemImpl(std::unique_ptr *out, const char *path, ncm::ProgramId program_id) { + Result OpenCodeFileSystemImpl(CodeInfo *out_code_info, std::unique_ptr *out, const char *path, ncm::ProgramId program_id) { /* Print a path suitable for the remote service. */ fssrv::sf::Path sf_path; R_TRY(FspPathPrintf(std::addressof(sf_path), "%s", path)); /* Open the filesystem using libnx bindings. */ + static_assert(sizeof(CodeInfo) == sizeof(::FsCodeInfo)); ::FsFileSystem fs; - R_TRY(fsldrOpenCodeFileSystem(program_id.value, sf_path.str, std::addressof(fs))); + R_TRY(fsldrOpenCodeFileSystem(reinterpret_cast<::FsCodeInfo *>(out_code_info), program_id.value, sf_path.str, std::addressof(fs))); /* Allocate a new filesystem wrapper. */ auto fsa = std::make_unique(fs); @@ -61,12 +62,12 @@ namespace ams::fs { return OpenPackageFileSystemImpl(out, sf_path.str); } - Result OpenSdCardCodeOrCodeFileSystemImpl(std::unique_ptr *out, const char *path, ncm::ProgramId program_id) { + Result OpenSdCardCodeOrCodeFileSystemImpl(CodeInfo *out_code_info, std::unique_ptr *out, const char *path, ncm::ProgramId program_id) { /* If we can open an sd card code fs, use it. */ R_SUCCEED_IF(R_SUCCEEDED(OpenSdCardCodeFileSystemImpl(out, program_id))); /* Otherwise, fall back to a normal code fs. */ - return OpenCodeFileSystemImpl(out, path, program_id); + return OpenCodeFileSystemImpl(out_code_info, out, path, program_id); } Result OpenHblCodeFileSystemImpl(std::unique_ptr *out) { @@ -226,7 +227,7 @@ namespace ams::fs { public: AtmosphereCodeFileSystem() : initialized(false) { /* ... */ } - Result Initialize(const char *path, ncm::ProgramId program_id, bool is_hbl, bool is_specific) { + Result Initialize(CodeInfo *out_code_info, const char *path, ncm::ProgramId program_id, bool is_hbl, bool is_specific) { AMS_ABORT_UNLESS(!this->initialized); /* If we're hbl, we need to open a hbl fs. */ @@ -238,7 +239,7 @@ namespace ams::fs { /* Open the code filesystem. */ std::unique_ptr fsa; - R_TRY(OpenSdCardCodeOrCodeFileSystemImpl(std::addressof(fsa), path, program_id)); + R_TRY(OpenSdCardCodeOrCodeFileSystemImpl(out_code_info, std::addressof(fsa), path, program_id)); this->code_fs.emplace(std::move(fsa), program_id, is_specific); this->program_id = program_id; @@ -274,7 +275,10 @@ namespace ams::fs { } - Result MountCode(const char *name, const char *path, ncm::ProgramId program_id) { + Result MountCode(CodeInfo *out, const char *name, const char *path, ncm::ProgramId program_id) { + /* Clear the output. */ + std::memset(out, 0, sizeof(*out)); + /* Validate the mount name. */ R_TRY(impl::CheckMountName(name)); @@ -283,13 +287,16 @@ namespace ams::fs { /* Open the code file system. */ std::unique_ptr fsa; - R_TRY(OpenCodeFileSystemImpl(std::addressof(fsa), path, program_id)); + R_TRY(OpenCodeFileSystemImpl(out, std::addressof(fsa), path, program_id)); /* Register. */ return fsa::Register(name, std::move(fsa)); } - Result MountCodeForAtmosphereWithRedirection(const char *name, const char *path, ncm::ProgramId program_id, bool is_hbl, bool is_specific) { + Result MountCodeForAtmosphereWithRedirection(CodeInfo *out, const char *name, const char *path, ncm::ProgramId program_id, bool is_hbl, bool is_specific) { + /* Clear the output. */ + std::memset(out, 0, sizeof(*out)); + /* Validate the mount name. */ R_TRY(impl::CheckMountName(name)); @@ -301,13 +308,16 @@ namespace ams::fs { R_UNLESS(ams_code_fs != nullptr, fs::ResultAllocationFailureInCodeA()); /* Initialize the code file system. */ - R_TRY(ams_code_fs->Initialize(path, program_id, is_hbl, is_specific)); + R_TRY(ams_code_fs->Initialize(out, path, program_id, is_hbl, is_specific)); /* Register. */ return fsa::Register(name, std::move(ams_code_fs)); } - Result MountCodeForAtmosphere(const char *name, const char *path, ncm::ProgramId program_id) { + Result MountCodeForAtmosphere(CodeInfo *out, const char *name, const char *path, ncm::ProgramId program_id) { + /* Clear the output. */ + std::memset(out, 0, sizeof(*out)); + /* Validate the mount name. */ R_TRY(impl::CheckMountName(name)); @@ -316,7 +326,7 @@ namespace ams::fs { /* Open the code file system. */ std::unique_ptr fsa; - R_TRY(OpenSdCardCodeOrCodeFileSystemImpl(std::addressof(fsa), path, program_id)); + R_TRY(OpenSdCardCodeOrCodeFileSystemImpl(out, std::addressof(fsa), path, program_id)); /* Create a wrapper fs. */ auto wrap_fsa = std::make_unique(std::move(fsa), program_id, false); diff --git a/libraries/libstratosphere/source/ldr/ldr_pm_api.cpp b/libraries/libstratosphere/source/ldr/ldr_pm_api.cpp index 4e14611a5..d6d1c190e 100644 --- a/libraries/libstratosphere/source/ldr/ldr_pm_api.cpp +++ b/libraries/libstratosphere/source/ldr/ldr_pm_api.cpp @@ -45,6 +45,10 @@ namespace ams::ldr::pm { return ldrPmAtmosphereGetProgramInfo(reinterpret_cast(out), reinterpret_cast(out_status), reinterpret_cast(&loc)); } + Result SetEnabledProgramVerification(bool enabled) { + return ldrPmSetEnabledProgramVerification(enabled); + } + Result AtmospherePinProgram(PinId *out, const ncm::ProgramLocation &loc, const cfg::OverrideStatus &status) { static_assert(sizeof(*out) == sizeof(u64), "PinId definition!"); static_assert(sizeof(status) == sizeof(CfgOverrideStatus), "CfgOverrideStatus definition!"); diff --git a/libraries/libvapours/include/vapours/crypto/crypto_rsa_pss_sha256_verifier.hpp b/libraries/libvapours/include/vapours/crypto/crypto_rsa_pss_sha256_verifier.hpp index a2136e98f..e25ca38d6 100644 --- a/libraries/libvapours/include/vapours/crypto/crypto_rsa_pss_sha256_verifier.hpp +++ b/libraries/libvapours/include/vapours/crypto/crypto_rsa_pss_sha256_verifier.hpp @@ -42,6 +42,14 @@ namespace ams::crypto { return Rsa2048PssSha256Verifier::Verify(sig, sig_size, mod, mod_size, exp, exp_size, msg, msg_size, work_buf, work_buf_size); } + inline bool VerifyRsa2048PssSha256WithHash(const void *sig, size_t sig_size, const void *mod, size_t mod_size, const void *exp, size_t exp_size, const void *hash, size_t hash_size) { + return Rsa2048PssSha256Verifier::VerifyWithHash(sig, sig_size, mod, mod_size, exp, exp_size, hash, hash_size); + } + + inline bool VerifyRsa2048PssSha256WithHash(const void *sig, size_t sig_size, const void *mod, size_t mod_size, const void *exp, size_t exp_size, const void *hash, size_t hash_size, void *work_buf, size_t work_buf_size) { + return Rsa2048PssSha256Verifier::VerifyWithHash(sig, sig_size, mod, mod_size, exp, exp_size, hash, hash_size, work_buf, work_buf_size); + } + inline bool VerifyRsa4096PssSha256(const void *sig, size_t sig_size, const void *mod, size_t mod_size, const void *exp, size_t exp_size, const void *msg, size_t msg_size) { return Rsa4096PssSha256Verifier::Verify(sig, sig_size, mod, mod_size, exp, exp_size, msg, msg_size); } @@ -50,4 +58,12 @@ namespace ams::crypto { return Rsa4096PssSha256Verifier::Verify(sig, sig_size, mod, mod_size, exp, exp_size, msg, msg_size, work_buf, work_buf_size); } + inline bool VerifyRsa4096PssSha256WithHash(const void *sig, size_t sig_size, const void *mod, size_t mod_size, const void *exp, size_t exp_size, const void *hash, size_t hash_size) { + return Rsa4096PssSha256Verifier::VerifyWithHash(sig, sig_size, mod, mod_size, exp, exp_size, hash, hash_size); + } + + inline bool VerifyRsa4096PssSha256WithHash(const void *sig, size_t sig_size, const void *mod, size_t mod_size, const void *exp, size_t exp_size, const void *hash, size_t hash_size, void *work_buf, size_t work_buf_size) { + return Rsa4096PssSha256Verifier::VerifyWithHash(sig, sig_size, mod, mod_size, exp, exp_size, hash, hash_size, work_buf, work_buf_size); + } + } diff --git a/libraries/libvapours/include/vapours/crypto/crypto_rsa_pss_verifier.hpp b/libraries/libvapours/include/vapours/crypto/crypto_rsa_pss_verifier.hpp index 61b4a4de8..bd7ddcea6 100644 --- a/libraries/libvapours/include/vapours/crypto/crypto_rsa_pss_verifier.hpp +++ b/libraries/libvapours/include/vapours/crypto/crypto_rsa_pss_verifier.hpp @@ -23,13 +23,14 @@ namespace ams::crypto { - template /* requires HashFunction */ + template /* requires HashFunction */ class RsaPssVerifier { NON_COPYABLE(RsaPssVerifier); NON_MOVEABLE(RsaPssVerifier); public: static constexpr size_t HashSize = Hash::HashSize; static constexpr size_t SaltSize = Hash::HashSize; + static constexpr size_t ModulusSize = _ModulusSize; static constexpr size_t SignatureSize = ModulusSize; static constexpr size_t MaximumExponentSize = 3; static constexpr size_t RequiredWorkBufferSize = RsaCalculator::RequiredWorkBufferSize; @@ -70,8 +71,15 @@ namespace ams::crypto { u8 message[SignatureSize]; ON_SCOPE_EXIT { ClearMemory(message, sizeof(message)); }; - return this->calculator.ExpMod(message, signature, SignatureSize) && - impl.Verify(message, sizeof(message), std::addressof(this->hash)); + if (!this->calculator.ExpMod(message, signature, SignatureSize)) { + return false; + } + + u8 calc_hash[Hash::HashSize]; + this->hash.GetHash(calc_hash, sizeof(calc_hash)); + ON_SCOPE_EXIT { ClearMemory(calc_hash, sizeof(calc_hash)); }; + + return impl.Verify(message, sizeof(message), calc_hash, sizeof(calc_hash)); } bool Verify(const void *signature, size_t size, void *work_buf, size_t work_buf_size) { @@ -83,8 +91,47 @@ namespace ams::crypto { u8 message[SignatureSize]; ON_SCOPE_EXIT { ClearMemory(message, sizeof(message)); }; - return this->calculator.ExpMod(message, signature, SignatureSize, work_buf, work_buf_size) && - impl.Verify(message, sizeof(message), std::addressof(this->hash)); + if (!this->calculator.ExpMod(message, signature, SignatureSize, work_buf, work_buf_size)) { + return false; + } + + u8 calc_hash[Hash::HashSize]; + this->hash.GetHash(calc_hash, sizeof(calc_hash)); + ON_SCOPE_EXIT { ClearMemory(calc_hash, sizeof(calc_hash)); }; + + return impl.Verify(message, sizeof(message), calc_hash, sizeof(calc_hash)); + } + + bool VerifyWithHash(const void *signature, size_t size, const void *hash, size_t hash_size) { + AMS_ASSERT(this->state == State::Initialized); + AMS_ASSERT(size == SignatureSize); + ON_SCOPE_EXIT { this->state = State::Done; }; + + impl::RsaPssImpl impl; + u8 message[SignatureSize]; + ON_SCOPE_EXIT { ClearMemory(message, sizeof(message)); }; + + if (!this->calculator.ExpMod(message, signature, SignatureSize)) { + return false; + } + + return impl.Verify(message, sizeof(message), static_cast(hash), hash_size); + } + + bool VerifyWithHash(const void *signature, size_t size, const void *hash, size_t hash_size, void *work_buf, size_t work_buf_size) { + AMS_ASSERT(this->state == State::Initialized); + AMS_ASSERT(size == SignatureSize); + ON_SCOPE_EXIT { this->state = State::Done; }; + + impl::RsaPssImpl impl; + u8 message[SignatureSize]; + ON_SCOPE_EXIT { ClearMemory(message, sizeof(message)); }; + + if (!this->calculator.ExpMod(message, signature, SignatureSize, work_buf, work_buf_size)) { + return false; + } + + return impl.Verify(message, sizeof(message), static_cast(hash), hash_size); } static bool Verify(const void *sig, size_t sig_size, const void *mod, size_t mod_size, const void *exp, size_t exp_size, const void *msg, size_t msg_size) { @@ -104,6 +151,22 @@ namespace ams::crypto { verifier.Update(msg, msg_size); return verifier.Verify(sig, sig_size, work_buf, work_buf_size); } + + static bool VerifyWithHash(const void *sig, size_t sig_size, const void *mod, size_t mod_size, const void *exp, size_t exp_size, const void *hash, size_t hash_size) { + RsaPssVerifier verifier; + if (!verifier.Initialize(mod, mod_size, exp, exp_size)) { + return false; + } + return verifier.VerifyWithHash(sig, sig_size, hash, hash_size); + } + + static bool VerifyWithHash(const void *sig, size_t sig_size, const void *mod, size_t mod_size, const void *exp, size_t exp_size, const void *hash, size_t hash_size, void *work_buf, size_t work_buf_size) { + RsaPssVerifier verifier; + if (!verifier.Initialize(mod, mod_size, exp, exp_size)) { + return false; + } + return verifier.VerifyWithHash(sig, sig_size, hash, hash_size, work_buf, work_buf_size); + } }; } diff --git a/libraries/libvapours/include/vapours/crypto/impl/crypto_rsa_pss_impl.hpp b/libraries/libvapours/include/vapours/crypto/impl/crypto_rsa_pss_impl.hpp index fc0af1041..1ce73e726 100644 --- a/libraries/libvapours/include/vapours/crypto/impl/crypto_rsa_pss_impl.hpp +++ b/libraries/libvapours/include/vapours/crypto/impl/crypto_rsa_pss_impl.hpp @@ -31,19 +31,22 @@ namespace ams::crypto::impl { private: static constexpr u8 TailMagic = 0xBC; private: - static void ComputeHashWithPadding(void *dst, Hash *hash, const void *salt, size_t salt_size) { + static void ComputeHashWithPadding(void *dst, const u8 *user_hash, size_t user_hash_size, const void *salt, size_t salt_size) { + AMS_ASSERT(user_hash_size == HashSize); + /* Initialize our buffer. */ u8 buf[8 + HashSize]; std::memset(buf, 0, 8); - hash->GetHash(buf + 8, HashSize); + std::memcpy(buf + 8, user_hash, HashSize); ON_SCOPE_EXIT { ClearMemory(buf, sizeof(buf)); }; /* Calculate our hash. */ - hash->Initialize(); - hash->Update(buf, sizeof(buf)); - hash->Update(salt, salt_size); - hash->GetHash(dst, HashSize); + Hash hash; + hash.Initialize(); + hash.Update(buf, sizeof(buf)); + hash.Update(salt, salt_size); + hash.GetHash(dst, HashSize); } static void ApplyMGF1(u8 *dst, size_t dst_size, const void *src, size_t src_size) { @@ -71,7 +74,7 @@ namespace ams::crypto::impl { public: RsaPssImpl() { /* ... */ } - bool Verify(u8 *buf, size_t size, Hash *hash) { + bool Verify(u8 *buf, size_t size, const u8 *hash, size_t hash_size) { /* Validate sanity byte. */ bool is_valid = buf[size - 1] == TailMagic; @@ -112,7 +115,7 @@ namespace ams::crypto::impl { u8 cmp_hash[HashSize]; ON_SCOPE_EXIT { ClearMemory(cmp_hash, sizeof(cmp_hash)); }; - ComputeHashWithPadding(cmp_hash, hash, salt, salt_size); + ComputeHashWithPadding(cmp_hash, hash, hash_size, salt, salt_size); is_valid &= IsSameBytes(cmp_hash, h, HashSize); /* Succeed if all our checks succeeded. */ diff --git a/libraries/libvapours/include/vapours/results/loader_results.hpp b/libraries/libvapours/include/vapours/results/loader_results.hpp index 3a4be5917..1ffa7ae13 100644 --- a/libraries/libvapours/include/vapours/results/loader_results.hpp +++ b/libraries/libvapours/include/vapours/results/loader_results.hpp @@ -31,6 +31,8 @@ namespace ams::ldr { R_DEFINE_ERROR_RESULT(NotPinned, 8); R_DEFINE_ERROR_RESULT(InvalidProgramId, 9); R_DEFINE_ERROR_RESULT(InvalidVersion, 10); + R_DEFINE_ERROR_RESULT(InvalidAcidSignature, 11); + R_DEFINE_ERROR_RESULT(InvalidNcaSignature, 12); R_DEFINE_ERROR_RESULT(InsufficientAddressSpace, 51); R_DEFINE_ERROR_RESULT(InvalidNro, 52); diff --git a/stratosphere/loader/source/ldr_content_management.cpp b/stratosphere/loader/source/ldr_content_management.cpp index 8409bf7e4..3fa6a766f 100644 --- a/stratosphere/loader/source/ldr_content_management.cpp +++ b/stratosphere/loader/source/ldr_content_management.cpp @@ -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(); } diff --git a/stratosphere/loader/source/ldr_content_management.hpp b/stratosphere/loader/source/ldr_content_management.hpp index a190b9635..a1972d75d 100644 --- a/stratosphere/loader/source/ldr_content_management.hpp +++ b/stratosphere/loader/source/ldr_content_management.hpp @@ -25,9 +25,13 @@ namespace ams::ldr { private: std::scoped_lock 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. */ diff --git a/stratosphere/loader/source/ldr_development_manager.cpp b/stratosphere/loader/source/ldr_development_manager.cpp new file mode 100644 index 000000000..bb48a635b --- /dev/null +++ b/stratosphere/loader/source/ldr_development_manager.cpp @@ -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 . + */ +#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; + } + +} diff --git a/stratosphere/loader/source/ldr_development_manager.hpp b/stratosphere/loader/source/ldr_development_manager.hpp new file mode 100644 index 000000000..cce92b026 --- /dev/null +++ b/stratosphere/loader/source/ldr_development_manager.hpp @@ -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 . + */ +#pragma once +#include + +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(); + +} diff --git a/stratosphere/loader/source/ldr_loader_service.cpp b/stratosphere/loader/source/ldr_loader_service.cpp index 58d3658f7..a98f21705 100644 --- a/stratosphere/loader/source/ldr_loader_service.cpp +++ b/stratosphere/loader/source/ldr_loader_service.cpp @@ -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); diff --git a/stratosphere/loader/source/ldr_loader_service.hpp b/stratosphere/loader/source/ldr_loader_service.hpp index f750bf1bd..2d5d5942a 100644 --- a/stratosphere/loader/source/ldr_loader_service.hpp +++ b/stratosphere/loader/source/ldr_loader_service.hpp @@ -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 count, const sf::OutPointerArray &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), diff --git a/stratosphere/loader/source/ldr_main.cpp b/stratosphere/loader/source/ldr_main.cpp index 8dba34b66..036ba3ec8 100644 --- a/stratosphere/loader/source/ldr_main.cpp +++ b/stratosphere/loader/source/ldr_main.cpp @@ -14,6 +14,7 @@ * along with this program. If not, see . */ +#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(ProcessManagerServiceName, ProcessManagerMaxSessions))); R_ABORT_UNLESS((g_server_manager.RegisterServer(ShellServiceName, ShellMaxSessions))); diff --git a/stratosphere/loader/source/ldr_meta.cpp b/stratosphere/loader/source/ldr_meta.cpp index 1bfa2a568..94ba0958b 100644 --- a/stratosphere/loader/source/ldr_meta.cpp +++ b/stratosphere/loader/source/ldr_meta.cpp @@ -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(aci) + aci->fah_offset; meta->aci_sac = reinterpret_cast(aci) + aci->sac_offset; meta->aci_kac = reinterpret_cast(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(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(); diff --git a/stratosphere/loader/source/ldr_meta.hpp b/stratosphere/loader/source/ldr_meta.hpp index 7e8c4b11b..3fca88c7f 100644 --- a/stratosphere/loader/source/ldr_meta.hpp +++ b/stratosphere/loader/source/ldr_meta.hpp @@ -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(); } diff --git a/stratosphere/loader/source/ldr_process_creation.cpp b/stratosphere/loader/source/ldr_process_creation.cpp index 69c1898a9..eb68c03f9 100644 --- a/stratosphere/loader/source/ldr_process_creation.cpp +++ b/stratosphere/loader/source/ldr_process_creation.cpp @@ -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(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(); } diff --git a/stratosphere/spl/source/spl_main.cpp b/stratosphere/spl/source/spl_main.cpp index ed5f4eed3..e7236bcc6 100644 --- a/stratosphere/spl/source/spl_main.cpp +++ b/stratosphere/spl/source/spl_main.cpp @@ -98,10 +98,10 @@ namespace { constexpr size_t RandomMaxSessions = 3; constexpr sm::ServiceName DeprecatedServiceName = sm::ServiceName::Encode("spl:"); - constexpr size_t DeprecatedMaxSessions = 12; + constexpr size_t DeprecatedMaxSessions = 13; constexpr sm::ServiceName GeneralServiceName = sm::ServiceName::Encode("spl:"); - constexpr size_t GeneralMaxSessions = 6; + constexpr size_t GeneralMaxSessions = 7; constexpr sm::ServiceName CryptoServiceName = sm::ServiceName::Encode("spl:mig"); constexpr size_t CryptoMaxSessions = 6;