From b9e53052732699e1f7af8ad061181e3798c3f8fc Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Wed, 3 Jul 2019 22:57:15 -0700 Subject: [PATCH] mitm api: remove inconsistent association --- .../stratosphere/mitm/imitmserviceobject.hpp | 10 ++--- .../stratosphere/mitm/mitm_query_service.hpp | 19 +------- include/stratosphere/mitm/mitm_server.hpp | 9 ++-- include/stratosphere/sm/sm_manager_api.hpp | 3 +- include/stratosphere/sm/sm_mitm_api.hpp | 3 +- source/mitm_query_service.cpp | 43 ------------------- source/sm/sm_ams.c | 4 +- source/sm/sm_ams.h | 2 +- source/sm/sm_manager_api.cpp | 4 +- source/sm/sm_mitm_api.cpp | 4 +- source/sm/smm_ams.c | 38 ++++++++++++++++ source/sm/smm_ams.h | 1 + 12 files changed, 62 insertions(+), 78 deletions(-) delete mode 100644 source/mitm_query_service.cpp diff --git a/include/stratosphere/mitm/imitmserviceobject.hpp b/include/stratosphere/mitm/imitmserviceobject.hpp index e717ad2f..75f4a443 100644 --- a/include/stratosphere/mitm/imitmserviceobject.hpp +++ b/include/stratosphere/mitm/imitmserviceobject.hpp @@ -25,12 +25,10 @@ class IMitmServiceObject : public IServiceObject { protected: std::shared_ptr forward_service; - u64 process_id = 0; - sts::ncm::TitleId title_id = sts::ncm::TitleId::Invalid; + u64 process_id; + sts::ncm::TitleId title_id; public: - IMitmServiceObject(std::shared_ptr s, u64 pid) : forward_service(s), process_id(pid) { - MitmQueryUtils::GetAssociatedTidForPid(this->process_id, &this->title_id); - } + IMitmServiceObject(std::shared_ptr s, u64 pid, sts::ncm::TitleId tid) : forward_service(s), process_id(pid), title_id(tid) { /* ... */ } virtual sts::ncm::TitleId GetTitleId() const { return this->title_id; @@ -42,7 +40,7 @@ class IMitmServiceObject : public IServiceObject { virtual bool IsMitmObject() const override { return true; } - static bool ShouldMitm(u64 pid, u64 tid); + static bool ShouldMitm(u64 pid, sts::ncm::TitleId tid); protected: virtual ~IMitmServiceObject() = default; diff --git a/include/stratosphere/mitm/mitm_query_service.hpp b/include/stratosphere/mitm/mitm_query_service.hpp index 28608c8b..d5969c0f 100644 --- a/include/stratosphere/mitm/mitm_query_service.hpp +++ b/include/stratosphere/mitm/mitm_query_service.hpp @@ -19,33 +19,18 @@ #include #include "../ncm.hpp" -namespace MitmQueryUtils { - Result GetAssociatedTidForPid(u64 pid, sts::ncm::TitleId *tid); - - void AssociatePidToTid(u64 pid, sts::ncm::TitleId tid); -} - template class MitmQueryService : public IServiceObject { private: enum class CommandId { ShouldMitm = 65000, - AssociatePidToTid = 65001, }; protected: - void ShouldMitm(Out should_mitm, u64 pid) { - should_mitm.SetValue(false); - sts::ncm::TitleId tid = sts::ncm::TitleId::Invalid; - if (R_SUCCEEDED(MitmQueryUtils::GetAssociatedTidForPid(pid, &tid))) { - should_mitm.SetValue(T::ShouldMitm(pid, tid)); - } - } - void AssociatePidToTid(u64 pid, sts::ncm::TitleId tid) { - MitmQueryUtils::AssociatePidToTid(pid, tid); + void ShouldMitm(Out should_mitm, u64 process_id, sts::ncm::TitleId title_id) { + should_mitm.SetValue(T::ShouldMitm(process_id, title_id)); } public: DEFINE_SERVICE_DISPATCH_TABLE { MAKE_SERVICE_COMMAND_META(MitmQueryService, ShouldMitm), - MAKE_SERVICE_COMMAND_META(MitmQueryService, AssociatePidToTid), }; }; \ No newline at end of file diff --git a/include/stratosphere/mitm/mitm_server.hpp b/include/stratosphere/mitm/mitm_server.hpp index 20edceb7..2ad64481 100644 --- a/include/stratosphere/mitm/mitm_server.hpp +++ b/include/stratosphere/mitm/mitm_server.hpp @@ -68,9 +68,10 @@ class MitmServer : public IWaitable { }); u64 client_pid; - R_ASSERT(sts::sm::mitm::AcknowledgeSession(forward_service.get(), &client_pid, this->mitm_name)); + sts::ncm::TitleId client_tid; + R_ASSERT(sts::sm::mitm::AcknowledgeSession(forward_service.get(), &client_pid, &client_tid, this->mitm_name)); - this->GetSessionManager()->AddWaitable(new MitmSession(session_h, client_pid, forward_service, MakeShared(forward_service, client_pid))); + this->GetSessionManager()->AddWaitable(new MitmSession(session_h, client_pid, forward_service, MakeShared(forward_service, client_pid, client_tid))); return ResultSuccess; } @@ -78,8 +79,8 @@ class MitmServer : public IWaitable { template struct MakeSharedMitmHelper { - static constexpr std::shared_ptr Make(std::shared_ptr forward_srv, u64 client_pid) { - return std::make_shared(forward_srv, client_pid); + static constexpr std::shared_ptr Make(std::shared_ptr forward_srv, u64 client_pid, sts::ncm::TitleId client_tid) { + return std::make_shared(forward_srv, client_pid, client_tid); } }; diff --git a/include/stratosphere/sm/sm_manager_api.hpp b/include/stratosphere/sm/sm_manager_api.hpp index 8c66c826..c9e01f75 100644 --- a/include/stratosphere/sm/sm_manager_api.hpp +++ b/include/stratosphere/sm/sm_manager_api.hpp @@ -17,11 +17,12 @@ #pragma once #include "sm_types.hpp" +#include "../ncm/ncm_types.hpp" namespace sts::sm::manager { /* Manager API. */ - Result RegisterProcess(u64 process_id, const void *acid, size_t acid_size, const void *aci, size_t aci_size); + Result RegisterProcess(u64 process_id, ncm::TitleId title_id, const void *acid, size_t acid_size, const void *aci, size_t aci_size); Result UnregisterProcess(u64 process_id); /* Atmosphere extensions. */ diff --git a/include/stratosphere/sm/sm_mitm_api.hpp b/include/stratosphere/sm/sm_mitm_api.hpp index a0837c97..6067941e 100644 --- a/include/stratosphere/sm/sm_mitm_api.hpp +++ b/include/stratosphere/sm/sm_mitm_api.hpp @@ -17,6 +17,7 @@ #pragma once #include "sm_types.hpp" +#include "../ncm/ncm_types.hpp" namespace sts::sm::mitm { @@ -24,7 +25,7 @@ namespace sts::sm::mitm { Result InstallMitm(Handle *out_port, Handle *out_query, ServiceName name); Result UninstallMitm(ServiceName name); Result AssociateProcessIdAndTitleId(u64 process_id, u64 title_id); - Result AcknowledgeSession(Service *out_service, u64 *out_pid, ServiceName name); + Result AcknowledgeSession(Service *out_service, u64 *out_pid, ncm::TitleId *out_tid, ServiceName name); Result HasMitm(bool *out, ServiceName name); Result WaitMitm(ServiceName name); diff --git a/source/mitm_query_service.cpp b/source/mitm_query_service.cpp deleted file mode 100644 index e35c5e8c..00000000 --- a/source/mitm_query_service.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright (c) 2018-2019 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 -#include -#include - -static std::vector g_known_pids; -static std::vector g_known_tids; -static HosMutex g_pid_tid_mutex; - -Result MitmQueryUtils::GetAssociatedTidForPid(u64 pid, sts::ncm::TitleId *tid) { - std::scoped_lock lk(g_pid_tid_mutex); - - for (unsigned int i = 0; i < g_known_pids.size(); i++) { - if (g_known_pids[i] == pid) { - *tid = sts::ncm::TitleId{g_known_tids[i]}; - return ResultSuccess; - } - } - - return ResultAtmosphereMitmProcessNotAssociated; -} - -void MitmQueryUtils::AssociatePidToTid(u64 pid, sts::ncm::TitleId tid) { - std::scoped_lock lk(g_pid_tid_mutex); - - g_known_pids.push_back(pid); - g_known_tids.push_back(static_cast(tid)); -} diff --git a/source/sm/sm_ams.c b/source/sm/sm_ams.c index c933172d..0af346ae 100644 --- a/source/sm/sm_ams.c +++ b/source/sm/sm_ams.c @@ -337,7 +337,7 @@ Result smAtmosphereMitmAssociateProcessIdAndTitleId(u64 pid, u64 tid) { return rc; } -Result smAtmosphereMitmAcknowledgeSession(Service *srv_out, u64 *pid_out, const char *name) { +Result smAtmosphereMitmAcknowledgeSession(Service *srv_out, u64 *pid_out, u64 *tid_out, const char *name) { IpcCommand c; ipcInitialize(&c); Service *srv = &g_smMitmSrv; @@ -361,6 +361,7 @@ Result smAtmosphereMitmAcknowledgeSession(Service *srv_out, u64 *pid_out, const u64 magic; u64 result; u64 pid; + u64 tid; } *resp; serviceIpcParse(srv, &r, sizeof(*resp)); @@ -369,6 +370,7 @@ Result smAtmosphereMitmAcknowledgeSession(Service *srv_out, u64 *pid_out, const rc = resp->result; if (R_SUCCEEDED(rc)) { *pid_out = resp->pid; + *tid_out = resp->tid; serviceCreate(srv_out, r.Handles[0]); } } diff --git a/source/sm/sm_ams.h b/source/sm/sm_ams.h index 26f9008d..4f24d6cc 100644 --- a/source/sm/sm_ams.h +++ b/source/sm/sm_ams.h @@ -22,7 +22,7 @@ void smAtmosphereMitmExit(void); Result smAtmosphereMitmInstall(Handle *handle_out, Handle *query_out, const char *name); Result smAtmosphereMitmUninstall(const char *name); Result smAtmosphereMitmAssociateProcessIdAndTitleId(u64 pid, u64 tid); -Result smAtmosphereMitmAcknowledgeSession(Service *srv_out, u64 *pid_out, const char *name); +Result smAtmosphereMitmAcknowledgeSession(Service *srv_out, u64 *pid_out, u64 *tid_out, const char *name); #ifdef __cplusplus } diff --git a/source/sm/sm_manager_api.cpp b/source/sm/sm_manager_api.cpp index dd4818d4..84dd4831 100644 --- a/source/sm/sm_manager_api.cpp +++ b/source/sm/sm_manager_api.cpp @@ -24,8 +24,8 @@ namespace sts::sm::manager { /* Manager API. */ - Result RegisterProcess(u64 process_id, const void *acid, size_t acid_size, const void *aci, size_t aci_size) { - return smManagerRegisterProcess(process_id, acid, acid_size, aci, aci_size); + Result RegisterProcess(u64 process_id, ncm::TitleId title_id, const void *acid, size_t acid_size, const void *aci, size_t aci_size) { + return smManagerAtmosphereRegisterProcess(process_id, static_cast(title_id), acid, acid_size, aci, aci_size); } Result UnregisterProcess(u64 process_id) { diff --git a/source/sm/sm_mitm_api.cpp b/source/sm/sm_mitm_api.cpp index d83518d0..7d490a3d 100644 --- a/source/sm/sm_mitm_api.cpp +++ b/source/sm/sm_mitm_api.cpp @@ -42,9 +42,9 @@ namespace sts::sm::mitm { }); } - Result AcknowledgeSession(Service *out_service, u64 *out_pid, ServiceName name) { + Result AcknowledgeSession(Service *out_service, u64 *out_pid, ncm::TitleId *out_tid, ServiceName name) { return impl::DoWithMitmSession([&]() { - return smAtmosphereMitmAcknowledgeSession(out_service, out_pid, name.name); + return smAtmosphereMitmAcknowledgeSession(out_service, out_pid, &out_tid->value, name.name); }); } diff --git a/source/sm/smm_ams.c b/source/sm/smm_ams.c index 6e7a90da..f5ea6517 100644 --- a/source/sm/smm_ams.c +++ b/source/sm/smm_ams.c @@ -51,6 +51,44 @@ Result smManagerAtmosphereEndInitialDefers(void) { } +Result smManagerAtmosphereRegisterProcess(u64 pid, u64 tid, const void *acid_sac, size_t acid_sac_size, const void *aci_sac, size_t aci_sac_size) { + IpcCommand c; + ipcInitialize(&c); + ipcAddSendBuffer(&c, acid_sac, acid_sac_size, BufferType_Normal); + ipcAddSendBuffer(&c, aci_sac, aci_sac_size, BufferType_Normal); + Service *srv = smManagerGetServiceSession(); + + struct { + u64 magic; + u64 cmd_id; + u64 pid; + u64 tid; + } *raw; + + raw = serviceIpcPrepareHeader(srv, &c, sizeof(*raw)); + raw->magic = SFCI_MAGIC; + raw->cmd_id = 65002; + raw->pid = pid; + raw->tid = tid; + + Result rc = serviceIpcDispatch(srv); + + if (R_SUCCEEDED(rc)) { + IpcParsedCommand r; + struct { + u64 magic; + u64 result; + } *resp; + + serviceIpcParse(srv, &r, sizeof(*resp)); + resp = r.Raw; + + rc = resp->result; + } + + return rc; +} + Result smManagerAtmosphereHasMitm(bool *out, const char* name) { IpcCommand c; ipcInitialize(&c); diff --git a/source/sm/smm_ams.h b/source/sm/smm_ams.h index 2e6e0acd..62a47536 100644 --- a/source/sm/smm_ams.h +++ b/source/sm/smm_ams.h @@ -12,6 +12,7 @@ extern "C" { #endif Result smManagerAtmosphereEndInitialDefers(void); +Result smManagerAtmosphereRegisterProcess(u64 pid, u64 tid, const void *acid_sac, size_t acid_sac_size, const void *aci_sac, size_t aci_sac_size); Result smManagerAtmosphereHasMitm(bool *out, const char* name); #ifdef __cplusplus