From 1b2cf173b366d3b40b6205c130a890a1264952fb Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Wed, 7 Apr 2021 01:11:17 -0700 Subject: [PATCH] kern: add new checks to SetThreadPriority/CoreMask --- .../libmesosphere/source/kern_k_thread.cpp | 10 +++++---- .../source/svc/kern_svc_thread.cpp | 22 ++++++++++++------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/libraries/libmesosphere/source/kern_k_thread.cpp b/libraries/libmesosphere/source/kern_k_thread.cpp index d2d5581dd..fd6ed1637 100644 --- a/libraries/libmesosphere/source/kern_k_thread.cpp +++ b/libraries/libmesosphere/source/kern_k_thread.cpp @@ -599,14 +599,16 @@ namespace ams::kern { KScopedSchedulerLock sl; MESOSPHERE_ASSERT(m_num_core_migration_disables >= 0); - /* If the core id is no-update magic, preserve the ideal core id. */ - if (core_id == ams::svc::IdealCoreNoUpdate) { + /* If we're updating, set our ideal virtual core. */ + if (core_id != ams::svc::IdealCoreNoUpdate) { + m_virtual_ideal_core_id = core_id; + } else { + /* Preserve our ideal core id. */ core_id = m_virtual_ideal_core_id; R_UNLESS(((1ul << core_id) & v_affinity_mask) != 0, svc::ResultInvalidCombination()); } - /* Set the virtual core/affinity mask. */ - m_virtual_ideal_core_id = core_id; + /* Set our affinity mask. */ m_virtual_affinity_mask = v_affinity_mask; /* Translate the virtual core to a physical core. */ diff --git a/libraries/libmesosphere/source/svc/kern_svc_thread.cpp b/libraries/libmesosphere/source/svc/kern_svc_thread.cpp index c887c5f56..3c511008b 100644 --- a/libraries/libmesosphere/source/svc/kern_svc_thread.cpp +++ b/libraries/libmesosphere/source/svc/kern_svc_thread.cpp @@ -132,14 +132,17 @@ namespace ams::kern::svc { /* Get the current process. */ KProcess &process = GetCurrentProcess(); - /* Validate the priority. */ - R_UNLESS(ams::svc::HighestThreadPriority <= priority && priority <= ams::svc::LowestThreadPriority, svc::ResultInvalidPriority()); - R_UNLESS(process.CheckThreadPriority(priority), svc::ResultInvalidPriority()); - /* Get the thread from its handle. */ KScopedAutoObject thread = process.GetHandleTable().GetObject(thread_handle); R_UNLESS(thread.IsNotNull(), svc::ResultInvalidHandle()); + /* Validate the thread is owned by the current process. */ + R_UNLESS(thread->GetOwnerProcess() == GetCurrentProcessPointer(), svc::ResultInvalidHandle()); + + /* Validate the priority. */ + R_UNLESS(ams::svc::HighestThreadPriority <= priority && priority <= ams::svc::LowestThreadPriority, svc::ResultInvalidPriority()); + R_UNLESS(process.CheckThreadPriority(priority), svc::ResultInvalidPriority()); + /* Set the thread priority. */ thread->SetBasePriority(priority); return ResultSuccess(); @@ -157,6 +160,13 @@ namespace ams::kern::svc { } Result SetThreadCoreMask(ams::svc::Handle thread_handle, int32_t core_id, uint64_t affinity_mask) { + /* Get the thread from its handle. */ + KScopedAutoObject thread = GetCurrentProcess().GetHandleTable().GetObject(thread_handle); + R_UNLESS(thread.IsNotNull(), svc::ResultInvalidHandle()); + + /* Validate the thread is owned by the current process. */ + R_UNLESS(thread->GetOwnerProcess() == GetCurrentProcessPointer(), svc::ResultInvalidHandle()); + /* Determine the core id/affinity mask. */ if (core_id == ams::svc::IdealCoreUseProcessValue) { core_id = GetCurrentProcess().GetIdealCoreId(); @@ -175,10 +185,6 @@ namespace ams::kern::svc { } } - /* Get the thread from its handle. */ - KScopedAutoObject thread = GetCurrentProcess().GetHandleTable().GetObject(thread_handle); - R_UNLESS(thread.IsNotNull(), svc::ResultInvalidHandle()); - /* Set the core mask. */ R_TRY(thread->SetCoreMask(core_id, affinity_mask));