mitm api: remove inconsistent association

This commit is contained in:
Michael Scire 2019-07-03 22:57:15 -07:00
parent ccfadc501b
commit b9e5305273
12 changed files with 62 additions and 78 deletions

View File

@ -25,12 +25,10 @@
class IMitmServiceObject : public IServiceObject {
protected:
std::shared_ptr<Service> 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<Service> s, u64 pid) : forward_service(s), process_id(pid) {
MitmQueryUtils::GetAssociatedTidForPid(this->process_id, &this->title_id);
}
IMitmServiceObject(std::shared_ptr<Service> 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;

View File

@ -19,33 +19,18 @@
#include <stratosphere.hpp>
#include "../ncm.hpp"
namespace MitmQueryUtils {
Result GetAssociatedTidForPid(u64 pid, sts::ncm::TitleId *tid);
void AssociatePidToTid(u64 pid, sts::ncm::TitleId tid);
}
template <typename T>
class MitmQueryService : public IServiceObject {
private:
enum class CommandId {
ShouldMitm = 65000,
AssociatePidToTid = 65001,
};
protected:
void ShouldMitm(Out<bool> 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<bool> 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<T>, ShouldMitm),
MAKE_SERVICE_COMMAND_META(MitmQueryService<T>, AssociatePidToTid),
};
};

View File

@ -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<typename T>
struct MakeSharedMitmHelper {
static constexpr std::shared_ptr<T> Make(std::shared_ptr<Service> forward_srv, u64 client_pid) {
return std::make_shared<T>(forward_srv, client_pid);
static constexpr std::shared_ptr<T> Make(std::shared_ptr<Service> forward_srv, u64 client_pid, sts::ncm::TitleId client_tid) {
return std::make_shared<T>(forward_srv, client_pid, client_tid);
}
};

View File

@ -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. */

View File

@ -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);

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include <mutex>
#include <switch.h>
#include <stratosphere.hpp>
static std::vector<u64> g_known_pids;
static std::vector<u64> 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<u64>(tid));
}

View File

@ -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]);
}
}

View File

@ -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
}

View File

@ -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<u64>(title_id), acid, acid_size, aci, aci_size);
}
Result UnregisterProcess(u64 process_id) {

View File

@ -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);
});
}

View File

@ -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);

View File

@ -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