From 826b7499712ac5c8556b2b0c25b3613085fddbd2 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Wed, 30 Apr 2025 20:24:22 -0700 Subject: [PATCH] kern: support null resource limit in KSecureSystemResource --- .../include/mesosphere/kern_k_system_resource.hpp | 6 +++++- .../source/kern_k_system_resource.cpp | 15 ++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/libraries/libmesosphere/include/mesosphere/kern_k_system_resource.hpp b/libraries/libmesosphere/include/mesosphere/kern_k_system_resource.hpp index d0ba5ca10..628d1724b 100644 --- a/libraries/libmesosphere/include/mesosphere/kern_k_system_resource.hpp +++ b/libraries/libmesosphere/include/mesosphere/kern_k_system_resource.hpp @@ -94,7 +94,11 @@ namespace ams::kern { static void PostDestroy(uintptr_t arg) { MESOSPHERE_UNUSED(arg); /* ... */ } ALWAYS_INLINE size_t CalculateRequiredSecureMemorySize() const { - return CalculateRequiredSecureMemorySize(m_resource_size, m_resource_pool); + if (m_resource_limit != nullptr) { + return CalculateRequiredSecureMemorySize(m_resource_size, m_resource_pool); + } else { + return 0; + } } ALWAYS_INLINE size_t GetSize() const { return m_resource_size; } diff --git a/libraries/libmesosphere/source/kern_k_system_resource.cpp b/libraries/libmesosphere/source/kern_k_system_resource.cpp index 876d6129d..fc4876252 100644 --- a/libraries/libmesosphere/source/kern_k_system_resource.cpp +++ b/libraries/libmesosphere/source/kern_k_system_resource.cpp @@ -59,7 +59,9 @@ namespace ams::kern { memory_reservation.Commit(); /* Open reference to our resource limit. */ - m_resource_limit->Open(); + if (m_resource_limit != nullptr) { + m_resource_limit->Open(); + } /* Set ourselves as initialized. */ m_is_initialized = true; @@ -76,11 +78,14 @@ namespace ams::kern { /* Free our secure memory. */ KSystemControl::FreeSecureMemory(m_resource_address, m_resource_size, m_resource_pool); - /* Release the memory reservation. */ - m_resource_limit->Release(ams::svc::LimitableResource_PhysicalMemoryMax, this->CalculateRequiredSecureMemorySize()); + /* Clean up our resource usage. */ + if (m_resource_limit != nullptr) { + /* Release the memory reservation. */ + m_resource_limit->Release(ams::svc::LimitableResource_PhysicalMemoryMax, this->CalculateRequiredSecureMemorySize()); - /* Close reference to our resource limit. */ - m_resource_limit->Close(); + /* Close reference to our resource limit. */ + m_resource_limit->Close(); + } } size_t KSecureSystemResource::CalculateRequiredSecureMemorySize(size_t size, KMemoryManager::Pool pool) {