libstrat: only hold sm sessions open when needed

This commit is contained in:
Michael Scire 2019-04-22 12:40:35 -07:00
parent 4d6bf21f9c
commit 79bc9bf8d8
4 changed files with 119 additions and 55 deletions

View File

@ -13,47 +13,47 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#pragma once #pragma once
#include <switch.h> #include <switch.h>
#include "mitm_query_service.hpp" #include "mitm_query_service.hpp"
#include "sm_mitm.h" #include "sm_mitm.h"
#include "mitm_session.hpp" #include "mitm_session.hpp"
#include "../utilities.hpp"
void RegisterMitmServerQueryHandle(Handle query_h, ServiceObjectHolder &&service); void RegisterMitmServerQueryHandle(Handle query_h, ServiceObjectHolder &&service);
template <typename T, auto MakeShared> template <typename T, auto MakeShared>
class MitmServer : public IWaitable { class MitmServer : public IWaitable {
static_assert(std::is_base_of<IMitmServiceObject, T>::value, "MitM Service Objects must derive from IMitmServiceObject"); static_assert(std::is_base_of<IMitmServiceObject, T>::value, "MitM Service Objects must derive from IMitmServiceObject");
private: private:
Handle port_handle; Handle port_handle;
unsigned int max_sessions; unsigned int max_sessions;
char mitm_name[9]; char mitm_name[9];
public: public:
MitmServer(const char *service_name, unsigned int max_s) : port_handle(0), max_sessions(max_s) { MitmServer(const char *service_name, unsigned int max_s) : port_handle(0), max_sessions(max_s) {
Handle query_h = 0; Handle query_h = 0;
Result rc = smMitMInitialize();
if (R_FAILED(rc)) { DoWithSmMitmSession([&]() {
fatalSimple(rc); strncpy(mitm_name, service_name, 8);
} mitm_name[8] = '\x00';
if (R_FAILED(smMitMInstall(&this->port_handle, &query_h, mitm_name))) {
strncpy(mitm_name, service_name, 8);
mitm_name[8] = '\x00';
if (R_FAILED((rc = smMitMInstall(&this->port_handle, &query_h, mitm_name)))) {
fatalSimple(rc);
}
RegisterMitmServerQueryHandle(query_h, std::move(ServiceObjectHolder(std::move(std::make_shared<MitmQueryService<T>>()))));
smMitMExit();
}
virtual ~MitmServer() override {
if (this->port_handle) {
if (R_FAILED(smMitMUninstall(this->mitm_name))) {
std::abort(); std::abort();
} }
});
RegisterMitmServerQueryHandle(query_h, std::move(ServiceObjectHolder(std::move(std::make_shared<MitmQueryService<T>>()))));
}
virtual ~MitmServer() override {
if (this->port_handle) {
DoWithSmMitmSession([&]() {
if (R_FAILED(smMitMUninstall(this->mitm_name))) {
std::abort();
}
});
svcCloseHandle(port_handle); svcCloseHandle(port_handle);
} }
} }
@ -61,12 +61,12 @@ class MitmServer : public IWaitable {
SessionManagerBase *GetSessionManager() { SessionManagerBase *GetSessionManager() {
return static_cast<SessionManagerBase *>(this->GetManager()); return static_cast<SessionManagerBase *>(this->GetManager());
} }
/* IWaitable */ /* IWaitable */
virtual Handle GetHandle() override { virtual Handle GetHandle() override {
return this->port_handle; return this->port_handle;
} }
virtual Result HandleSignaled(u64 timeout) override { virtual Result HandleSignaled(u64 timeout) override {
/* If this server's port was signaled, accept a new session. */ /* If this server's port was signaled, accept a new session. */
Handle session_h; Handle session_h;
@ -74,27 +74,22 @@ class MitmServer : public IWaitable {
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
return rc; return rc;
} }
/* Create a forward service for this instance. */ /* Create a forward service for this instance. */
std::shared_ptr<Service> forward_service(new Service(), [](Service *s) { std::shared_ptr<Service> forward_service(new Service(), [](Service *s) {
/* Custom deleter to ensure service is open as long as necessary. */ /* Custom deleter to ensure service is open as long as necessary. */
serviceClose(s); serviceClose(s);
delete s; delete s;
}); });
rc = smMitMInitialize();
if (R_FAILED(rc)) {
fatalSimple(rc);
}
u64 client_pid; u64 client_pid;
if (R_FAILED(smMitMAcknowledgeSession(forward_service.get(), &client_pid, mitm_name))) { DoWithSmMitmSession([&]() {
/* TODO: Panic. */ if (R_FAILED(smMitMAcknowledgeSession(forward_service.get(), &client_pid, mitm_name))) {
} std::abort();
}
smMitMExit(); });
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)));
return ResultSuccess; return ResultSuccess;
} }

View File

@ -13,12 +13,13 @@
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#pragma once #pragma once
#include <switch.h> #include <switch.h>
#include "iwaitable.hpp" #include "iwaitable.hpp"
#include "ipc.hpp" #include "ipc.hpp"
#include "utilities.hpp"
template<typename T, auto MakeShared> template<typename T, auto MakeShared>
class IServer : public IWaitable { class IServer : public IWaitable {
@ -26,11 +27,11 @@ class IServer : public IWaitable {
protected: protected:
Handle port_handle; Handle port_handle;
unsigned int max_sessions; unsigned int max_sessions;
public: public:
IServer(unsigned int max_s) : port_handle(0), max_sessions(max_s) { } IServer(unsigned int max_s) : port_handle(0), max_sessions(max_s) { }
virtual ~IServer() { virtual ~IServer() {
if (port_handle) { if (port_handle) {
svcCloseHandle(port_handle); svcCloseHandle(port_handle);
} }
@ -39,12 +40,12 @@ class IServer : public IWaitable {
SessionManagerBase *GetSessionManager() { SessionManagerBase *GetSessionManager() {
return static_cast<SessionManagerBase *>(this->GetManager()); return static_cast<SessionManagerBase *>(this->GetManager());
} }
/* IWaitable */ /* IWaitable */
virtual Handle GetHandle() override { virtual Handle GetHandle() override {
return this->port_handle; return this->port_handle;
} }
virtual Result HandleSignaled(u64 timeout) override { virtual Result HandleSignaled(u64 timeout) override {
/* If this server's port was signaled, accept a new session. */ /* If this server's port was signaled, accept a new session. */
Handle session_h; Handle session_h;
@ -52,24 +53,26 @@ class IServer : public IWaitable {
if (R_FAILED(rc)) { if (R_FAILED(rc)) {
return rc; return rc;
} }
this->GetSessionManager()->AddSession(session_h, std::move(ServiceObjectHolder(std::move(MakeShared())))); this->GetSessionManager()->AddSession(session_h, std::move(ServiceObjectHolder(std::move(MakeShared()))));
return ResultSuccess; return ResultSuccess;
} }
}; };
template <typename T, auto MakeShared = std::make_shared<T>> template <typename T, auto MakeShared = std::make_shared<T>>
class ServiceServer : public IServer<T, MakeShared> { class ServiceServer : public IServer<T, MakeShared> {
public: public:
ServiceServer(const char *service_name, unsigned int max_s) : IServer<T, MakeShared>(max_s) { ServiceServer(const char *service_name, unsigned int max_s) : IServer<T, MakeShared>(max_s) {
if (R_FAILED(smRegisterService(&this->port_handle, service_name, false, this->max_sessions))) { DoWithSmSession([&]() {
/* TODO: Panic. */ if (R_FAILED(smRegisterService(&this->port_handle, service_name, false, this->max_sessions))) {
} std::abort();
}
});
} }
}; };
template <typename T, auto MakeShared = std::make_shared<T>> template <typename T, auto MakeShared = std::make_shared<T>>
class ExistingPortServer : public IServer<T, MakeShared> { class ExistingPortServer : public IServer<T, MakeShared> {
public: public:
ExistingPortServer(Handle port_h, unsigned int max_s) : IServer<T, MakeShared>(max_s) { ExistingPortServer(Handle port_h, unsigned int max_s) : IServer<T, MakeShared>(max_s) {
this->port_handle = port_h; this->port_handle = port_h;
@ -77,9 +80,9 @@ class ExistingPortServer : public IServer<T, MakeShared> {
}; };
template <typename T, auto MakeShared = std::make_shared<T>> template <typename T, auto MakeShared = std::make_shared<T>>
class ManagedPortServer : public IServer<T, MakeShared> { class ManagedPortServer : public IServer<T, MakeShared> {
public: public:
ManagedPortServer(const char *service_name, unsigned int max_s) : IServer<T, MakeShared>(max_s) { ManagedPortServer(const char *service_name, unsigned int max_s) : IServer<T, MakeShared>(max_s) {
if (R_FAILED(svcManageNamedPort(&this->port_handle, service_name, this->max_sessions))) { if (R_FAILED(svcManageNamedPort(&this->port_handle, service_name, this->max_sessions))) {
/* TODO: panic */ /* TODO: panic */
} }

View File

@ -18,6 +18,9 @@
#include <switch.h> #include <switch.h>
#include <cstdlib> #include <cstdlib>
#include "hossynch.hpp"
#include "mitm/sm_mitm.h"
static inline void RebootToRcm() { static inline void RebootToRcm() {
SecmonArgs args = {0}; SecmonArgs args = {0};
args.X[0] = 0xC3000401; /* smcSetConfig */ args.X[0] = 0xC3000401; /* smcSetConfig */
@ -97,3 +100,36 @@ static inline bool IsRcmBugPatched() {
} }
return rcm_bug_patched; return rcm_bug_patched;
} }
HosRecursiveMutex &GetSmSessionMutex();
HosRecursiveMutex &GetSmMitmSessionMutex();
template<typename F>
static void DoWithSmSession(F f) {
std::scoped_lock<HosRecursiveMutex &> lk(GetSmSessionMutex());
{
Result rc;
if (R_SUCCEEDED((rc = smInitialize()))) {
f();
} else {
/* TODO: fatalSimple(rc); ? */
std::abort();
}
smExit();
}
}
template<typename F>
static void DoWithSmMitmSession(F f) {
std::scoped_lock<HosRecursiveMutex &> lk(GetSmMitmSessionMutex());
{
Result rc;
if (R_SUCCEEDED((rc = smMitMInitialize()))) {
f();
} else {
/* TODO: fatalSimple(rc); ? */
std::abort();
}
smMitMExit();
}
}

30
source/utilities.cpp Normal file
View File

@ -0,0 +1,30 @@
/*
* 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 <switch.h>
#include <stratosphere.hpp>
static HosRecursiveMutex g_sm_session_lock;
static HosRecursiveMutex g_sm_mitm_session_lock;
HosRecursiveMutex &GetSmSessionMutex() {
return g_sm_session_lock;
}
HosRecursiveMutex &GetSmMitmSessionMutex() {
return g_sm_mitm_session_lock;
}