sm: refactor to use R_TRY

This commit is contained in:
Michael Scire 2019-06-17 09:17:53 -07:00
parent a0cf3bbed8
commit dfcba5e6d4
10 changed files with 157 additions and 181 deletions

View File

@ -13,7 +13,7 @@
* 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/>.
*/ */
#include <switch.h> #include <switch.h>
#include <stratosphere.hpp> #include <stratosphere.hpp>
#include "sm_dmnt_service.hpp" #include "sm_dmnt_service.hpp"

View File

@ -13,7 +13,7 @@
* 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 <stratosphere.hpp> #include <stratosphere.hpp>

View File

@ -13,7 +13,7 @@
* 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/>.
*/ */
#include <cstdlib> #include <cstdlib>
#include <cstdint> #include <cstdint>
#include <cstring> #include <cstring>
@ -35,7 +35,7 @@ extern "C" {
#define INNER_HEAP_SIZE 0x20000 #define INNER_HEAP_SIZE 0x20000
size_t nx_inner_heap_size = INNER_HEAP_SIZE; size_t nx_inner_heap_size = INNER_HEAP_SIZE;
char nx_inner_heap[INNER_HEAP_SIZE]; char nx_inner_heap[INNER_HEAP_SIZE];
void __libnx_initheap(void); void __libnx_initheap(void);
void __appInit(void); void __appInit(void);
void __appExit(void); void __appExit(void);
@ -67,7 +67,7 @@ void __libnx_initheap(void) {
void __appInit(void) { void __appInit(void) {
SetFirmwareVersionForLibnx(); SetFirmwareVersionForLibnx();
/* We must do no service setup here, because we are sm. */ /* We must do no service setup here, because we are sm. */
} }
@ -81,34 +81,34 @@ void __appExit(void) {
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
consoleDebugInit(debugDevice_SVC); consoleDebugInit(debugDevice_SVC);
/* TODO: What's a good timeout value to use here? */ /* TODO: What's a good timeout value to use here? */
auto server_manager = new WaitableManager(1); auto server_manager = new WaitableManager(1);
/* Create sm:, (and thus allow things to register to it). */ /* Create sm:, (and thus allow things to register to it). */
server_manager->AddWaitable(new ManagedPortServer<UserService>("sm:", 0x40)); server_manager->AddWaitable(new ManagedPortServer<UserService>("sm:", 0x40));
/* Create sm:m manually. */ /* Create sm:m manually. */
Handle smm_h; Handle smm_h;
if (R_FAILED(Registration::RegisterServiceForSelf(smEncodeName("sm:m"), 1, false, &smm_h))) { if (R_FAILED(Registration::RegisterServiceForSelf(smEncodeName("sm:m"), 1, false, &smm_h))) {
std::abort(); std::abort();
} }
server_manager->AddWaitable(new ExistingPortServer<ManagerService>(smm_h, 1)); server_manager->AddWaitable(new ExistingPortServer<ManagerService>(smm_h, 1));
/*===== ATMOSPHERE EXTENSION =====*/ /*===== ATMOSPHERE EXTENSION =====*/
/* Create sm:dmnt manually. */ /* Create sm:dmnt manually. */
Handle smdmnt_h; Handle smdmnt_h;
if (R_FAILED(Registration::RegisterServiceForSelf(smEncodeName("sm:dmnt"), 1, false, &smdmnt_h))) { if (R_FAILED(Registration::RegisterServiceForSelf(smEncodeName("sm:dmnt"), 1, false, &smdmnt_h))) {
std::abort(); std::abort();
} }
server_manager->AddWaitable(new ExistingPortServer<DmntService>(smm_h, 1));; server_manager->AddWaitable(new ExistingPortServer<DmntService>(smm_h, 1));;
/*================================*/ /*================================*/
/* Loop forever, servicing our services. */ /* Loop forever, servicing our services. */
server_manager->Process(); server_manager->Process();
/* Cleanup. */ /* Cleanup. */
delete server_manager; delete server_manager;
return 0; return 0;

View File

@ -13,7 +13,7 @@
* 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/>.
*/ */
#include <switch.h> #include <switch.h>
#include <stratosphere.hpp> #include <stratosphere.hpp>
#include "sm_manager_service.hpp" #include "sm_manager_service.hpp"

View File

@ -13,7 +13,7 @@
* 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 <stratosphere.hpp> #include <stratosphere.hpp>
@ -22,7 +22,7 @@
enum ManagerServiceCmd { enum ManagerServiceCmd {
Manager_Cmd_RegisterProcess = 0, Manager_Cmd_RegisterProcess = 0,
Manager_Cmd_UnregisterProcess = 1, Manager_Cmd_UnregisterProcess = 1,
Manager_Cmd_AtmosphereEndInitDefers = 65000, Manager_Cmd_AtmosphereEndInitDefers = 65000,
Manager_Cmd_AtmosphereHasMitm = 65001, Manager_Cmd_AtmosphereHasMitm = 65001,
}; };
@ -38,7 +38,7 @@ class ManagerService final : public IServiceObject {
DEFINE_SERVICE_DISPATCH_TABLE { DEFINE_SERVICE_DISPATCH_TABLE {
MakeServiceCommandMeta<Manager_Cmd_RegisterProcess, &ManagerService::RegisterProcess>(), MakeServiceCommandMeta<Manager_Cmd_RegisterProcess, &ManagerService::RegisterProcess>(),
MakeServiceCommandMeta<Manager_Cmd_UnregisterProcess, &ManagerService::UnregisterProcess>(), MakeServiceCommandMeta<Manager_Cmd_UnregisterProcess, &ManagerService::UnregisterProcess>(),
MakeServiceCommandMeta<Manager_Cmd_AtmosphereEndInitDefers, &ManagerService::AtmosphereEndInitDefers>(), MakeServiceCommandMeta<Manager_Cmd_AtmosphereEndInitDefers, &ManagerService::AtmosphereEndInitDefers>(),
MakeServiceCommandMeta<Manager_Cmd_AtmosphereHasMitm, &ManagerService::AtmosphereHasMitm>(), MakeServiceCommandMeta<Manager_Cmd_AtmosphereHasMitm, &ManagerService::AtmosphereHasMitm>(),
}; };

View File

@ -13,7 +13,7 @@
* 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/>.
*/ */
#include <switch.h> #include <switch.h>
#include <algorithm> #include <algorithm>
#include <stratosphere.hpp> #include <stratosphere.hpp>
@ -62,7 +62,7 @@ bool Registration::ShouldInitDefer(u64 service) {
if (g_end_init_defers) { if (g_end_init_defers) {
return false; return false;
} }
/* This is a mechanism by which certain services will always be deferred until sm:m receives a special command. */ /* This is a mechanism by which certain services will always be deferred until sm:m receives a special command. */
/* This can be extended with more services as needed at a later date. */ /* This can be extended with more services as needed at a later date. */
constexpr u64 FSP_SRV = EncodeNameConstant("fsp-srv"); constexpr u64 FSP_SRV = EncodeNameConstant("fsp-srv");
@ -196,11 +196,11 @@ Result Registration::RegisterProcess(u64 pid, u8 *acid_sac, size_t acid_sac_size
if (proc == NULL) { if (proc == NULL) {
return ResultSmInsufficientProcesses; return ResultSmInsufficientProcesses;
} }
if (aci0_sac_size && !ValidateSacAgainstRestriction(acid_sac, acid_sac_size, aci0_sac, aci0_sac_size)) { if (aci0_sac_size && !ValidateSacAgainstRestriction(acid_sac, acid_sac_size, aci0_sac, aci0_sac_size)) {
return ResultSmNotAllowed; return ResultSmNotAllowed;
} }
proc->pid = pid; proc->pid = pid;
proc->sac_size = aci0_sac_size; proc->sac_size = aci0_sac_size;
std::copy(aci0_sac, aci0_sac + aci0_sac_size, proc->sac); std::copy(aci0_sac, aci0_sac + aci0_sac_size, proc->sac);
@ -212,7 +212,7 @@ Result Registration::UnregisterProcess(u64 pid) {
if (proc == NULL) { if (proc == NULL) {
return ResultSmInvalidClient; return ResultSmInvalidClient;
} }
proc->pid = 0; proc->pid = 0;
return ResultSuccess; return ResultSuccess;
} }
@ -233,7 +233,7 @@ Result Registration::GetServiceHandle(u64 pid, u64 service, Handle *out) {
/* Note: This defers the result until later. */ /* Note: This defers the result until later. */
return ResultServiceFrameworkRequestDeferredByUser; return ResultServiceFrameworkRequestDeferredByUser;
} }
*out = 0; *out = 0;
Result rc; Result rc;
if (target_service->mitm_pid == 0 || target_service->mitm_pid == pid) { if (target_service->mitm_pid == 0 || target_service->mitm_pid == pid) {
@ -262,7 +262,7 @@ Result Registration::GetServiceHandle(u64 pid, u64 service, Handle *out) {
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
if (resp->should_mitm) { if (resp->should_mitm) {
rc = svcConnectToPort(&target_service->mitm_fwd_sess_h, target_service->port_h); rc = svcConnectToPort(&target_service->mitm_fwd_sess_h, target_service->port_h);
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
rc = svcConnectToPort(out, target_service->mitm_port_h); rc = svcConnectToPort(out, target_service->mitm_port_h);
if (R_SUCCEEDED(rc)) { if (R_SUCCEEDED(rc)) {
target_service->mitm_waiting_ack_pid = pid; target_service->mitm_waiting_ack_pid = pid;
@ -281,22 +281,23 @@ Result Registration::GetServiceHandle(u64 pid, u64 service, Handle *out) {
rc = svcConnectToPort(out, target_service->port_h); rc = svcConnectToPort(out, target_service->port_h);
} }
} }
if (R_FAILED(rc)) { /* Convert Kernel result to SM result. */
if ((rc & 0x3FFFFF) == ResultKernelOutOfSessions) { R_TRY_CATCH(rc) {
R_CATCH(ResultKernelOutOfSessions) {
return ResultSmInsufficientSessions; return ResultSmInsufficientSessions;
} }
} } R_END_TRY_CATCH;
return rc; return ResultSuccess;
} }
Result Registration::GetServiceForPid(u64 pid, u64 service, Handle *out) { Result Registration::GetServiceForPid(u64 pid, u64 service, Handle *out) {
if (!service) { if (!service) {
return ResultSmInvalidServiceName; return ResultSmInvalidServiceName;
} }
u64 service_name_len = GetServiceNameLength(service); u64 service_name_len = GetServiceNameLength(service);
/* If the service has bytes after a null terminator, that's no good. */ /* If the service has bytes after a null terminator, that's no good. */
if (service_name_len != 8 && (service >> (8 * service_name_len))) { if (service_name_len != 8 && (service >> (8 * service_name_len))) {
return ResultSmInvalidServiceName; return ResultSmInvalidServiceName;
@ -310,18 +311,18 @@ Result Registration::GetServiceForPid(u64 pid, u64 service, Handle *out) {
if (GetRuntimeFirmwareVersion() >= FirmwareVersion_800 && service == EncodeNameConstant("apm:p")) { if (GetRuntimeFirmwareVersion() >= FirmwareVersion_800 && service == EncodeNameConstant("apm:p")) {
return ResultSmNotAllowed; return ResultSmNotAllowed;
} }
if (!IsInitialProcess(pid)) { if (!IsInitialProcess(pid)) {
Registration::Process *proc = GetProcessForPid(pid); Registration::Process *proc = GetProcessForPid(pid);
if (proc == NULL) { if (proc == NULL) {
return ResultSmInvalidClient; return ResultSmInvalidClient;
} }
if (!IsValidForSac(proc->sac, proc->sac_size, service, false)) { if (!IsValidForSac(proc->sac, proc->sac_size, service, false)) {
return ResultSmNotAllowed; return ResultSmNotAllowed;
} }
} }
return GetServiceHandle(pid, service, out); return GetServiceHandle(pid, service, out);
} }
@ -329,29 +330,29 @@ Result Registration::RegisterServiceForPid(u64 pid, u64 service, u64 max_session
if (!service) { if (!service) {
return ResultSmInvalidServiceName; return ResultSmInvalidServiceName;
} }
u64 service_name_len = GetServiceNameLength(service); u64 service_name_len = GetServiceNameLength(service);
/* If the service has bytes after a null terminator, that's no good. */ /* If the service has bytes after a null terminator, that's no good. */
if (service_name_len != 8 && (service >> (8 * service_name_len))) { if (service_name_len != 8 && (service >> (8 * service_name_len))) {
return ResultSmInvalidServiceName; return ResultSmInvalidServiceName;
} }
if (!IsInitialProcess(pid)) { if (!IsInitialProcess(pid)) {
Registration::Process *proc = GetProcessForPid(pid); Registration::Process *proc = GetProcessForPid(pid);
if (proc == NULL) { if (proc == NULL) {
return ResultSmInvalidClient; return ResultSmInvalidClient;
} }
if (!IsValidForSac(proc->sac, proc->sac_size, service, true)) { if (!IsValidForSac(proc->sac, proc->sac_size, service, true)) {
return ResultSmNotAllowed; return ResultSmNotAllowed;
} }
} }
if (HasService(service)) { if (HasService(service)) {
return ResultSmAlreadyRegistered; return ResultSmAlreadyRegistered;
} }
#ifdef SM_MINIMUM_SESSION_LIMIT #ifdef SM_MINIMUM_SESSION_LIMIT
if (max_sessions < SM_MINIMUM_SESSION_LIMIT) { if (max_sessions < SM_MINIMUM_SESSION_LIMIT) {
max_sessions = SM_MINIMUM_SESSION_LIMIT; max_sessions = SM_MINIMUM_SESSION_LIMIT;
@ -362,35 +363,30 @@ Result Registration::RegisterServiceForPid(u64 pid, u64 service, u64 max_session
if (free_service == NULL) { if (free_service == NULL) {
return ResultSmInsufficientServices; return ResultSmInsufficientServices;
} }
*out = 0; *out = 0;
*free_service = (const Registration::Service){0}; *free_service = (const Registration::Service){0};
Result rc = svcCreatePort(out, &free_service->port_h, max_sessions, is_light, (char *)&free_service->service_name); R_TRY(svcCreatePort(out, &free_service->port_h, max_sessions, is_light, (char *)&free_service->service_name));
if (R_SUCCEEDED(rc)) { free_service->service_name = service;
free_service->service_name = service; free_service->owner_pid = pid;
free_service->owner_pid = pid; free_service->max_sessions = max_sessions;
free_service->max_sessions = max_sessions; free_service->is_light = is_light;
free_service->is_light = is_light;
} return ResultSuccess;
return rc;
} }
Result Registration::RegisterServiceForSelf(u64 service, u64 max_sessions, bool is_light, Handle *out) { Result Registration::RegisterServiceForSelf(u64 service, u64 max_sessions, bool is_light, Handle *out) {
u64 pid; u64 pid;
Result rc = svcGetProcessId(&pid, CUR_PROCESS_HANDLE); R_TRY(svcGetProcessId(&pid, CUR_PROCESS_HANDLE));
if (R_FAILED(rc)) {
return rc;
}
u64 service_name_len = GetServiceNameLength(service); u64 service_name_len = GetServiceNameLength(service);
/* If the service has bytes after a null terminator, that's no good. */ /* If the service has bytes after a null terminator, that's no good. */
if (service_name_len != 8 && (service >> (8 * service_name_len))) { if (service_name_len != 8 && (service >> (8 * service_name_len))) {
return ResultSmInvalidServiceName; return ResultSmInvalidServiceName;
} }
if (HasService(service)) { if (HasService(service)) {
return ResultSmAlreadyRegistered; return ResultSmAlreadyRegistered;
} }
@ -400,38 +396,36 @@ Result Registration::RegisterServiceForSelf(u64 service, u64 max_sessions, bool
max_sessions = SM_MINIMUM_SESSION_LIMIT; max_sessions = SM_MINIMUM_SESSION_LIMIT;
} }
#endif #endif
Registration::Service *free_service = GetFreeService(); Registration::Service *free_service = GetFreeService();
if (free_service == NULL) { if (free_service == NULL) {
return ResultSmInsufficientServices; return ResultSmInsufficientServices;
} }
*out = 0; *out = 0;
*free_service = (const Registration::Service){0}; *free_service = (const Registration::Service){0};
rc = svcCreatePort(out, &free_service->port_h, max_sessions, is_light, (char *)&free_service->service_name); R_TRY(svcCreatePort(out, &free_service->port_h, max_sessions, is_light, (char *)&free_service->service_name));
if (R_SUCCEEDED(rc)) { free_service->service_name = service;
free_service->service_name = service; free_service->owner_pid = pid;
free_service->owner_pid = pid; free_service->max_sessions = max_sessions;
free_service->max_sessions = max_sessions; free_service->is_light = is_light;
free_service->is_light = is_light;
} return ResultSuccess;
return rc;
} }
Result Registration::UnregisterServiceForPid(u64 pid, u64 service) { Result Registration::UnregisterServiceForPid(u64 pid, u64 service) {
if (!service) { if (!service) {
return ResultSmInvalidServiceName; return ResultSmInvalidServiceName;
} }
u64 service_name_len = GetServiceNameLength(service); u64 service_name_len = GetServiceNameLength(service);
/* If the service has bytes after a null terminator, that's no good. */ /* If the service has bytes after a null terminator, that's no good. */
if (service_name_len != 8 && (service >> (8 * service_name_len))) { if (service_name_len != 8 && (service >> (8 * service_name_len))) {
return ResultSmInvalidServiceName; return ResultSmInvalidServiceName;
} }
Registration::Service *target_service = GetService(service); Registration::Service *target_service = GetService(service);
if (target_service == NULL) { if (target_service == NULL) {
return ResultSmNotRegistered; return ResultSmNotRegistered;
@ -440,7 +434,7 @@ Result Registration::UnregisterServiceForPid(u64 pid, u64 service) {
if (!IsInitialProcess(pid) && target_service->owner_pid != pid) { if (!IsInitialProcess(pid) && target_service->owner_pid != pid) {
return ResultSmNotAllowed; return ResultSmNotAllowed;
} }
svcCloseHandle(target_service->port_h); svcCloseHandle(target_service->port_h);
svcCloseHandle(target_service->mitm_port_h); svcCloseHandle(target_service->mitm_port_h);
svcCloseHandle(target_service->mitm_query_h); svcCloseHandle(target_service->mitm_query_h);
@ -453,60 +447,59 @@ Result Registration::InstallMitmForPid(u64 pid, u64 service, Handle *out, Handle
if (!service) { if (!service) {
return ResultSmInvalidServiceName; return ResultSmInvalidServiceName;
} }
u64 service_name_len = GetServiceNameLength(service); u64 service_name_len = GetServiceNameLength(service);
/* If the service has bytes after a null terminator, that's no good. */ /* If the service has bytes after a null terminator, that's no good. */
if (service_name_len != 8 && (service >> (8 * service_name_len))) { if (service_name_len != 8 && (service >> (8 * service_name_len))) {
return ResultSmInvalidServiceName; return ResultSmInvalidServiceName;
} }
/* Verify we're allowed to mitm the service. */ /* Verify we're allowed to mitm the service. */
if (!IsInitialProcess(pid)) { if (!IsInitialProcess(pid)) {
Registration::Process *proc = GetProcessForPid(pid); Registration::Process *proc = GetProcessForPid(pid);
if (proc == NULL) { if (proc == NULL) {
return ResultSmInvalidClient; return ResultSmInvalidClient;
} }
if (!IsValidForSac(proc->sac, proc->sac_size, service, true)) { if (!IsValidForSac(proc->sac, proc->sac_size, service, true)) {
return ResultSmNotAllowed; return ResultSmNotAllowed;
} }
} }
/* Verify the service exists. */ /* Verify the service exists. */
Registration::Service *target_service = GetService(service); Registration::Service *target_service = GetService(service);
if (target_service == NULL) { if (target_service == NULL) {
return ResultServiceFrameworkRequestDeferredByUser; return ResultServiceFrameworkRequestDeferredByUser;
} }
/* Verify the service isn't already being mitm'd. */ /* Verify the service isn't already being mitm'd. */
if (target_service->mitm_pid != 0) { if (target_service->mitm_pid != 0) {
return ResultSmAlreadyRegistered; return ResultSmAlreadyRegistered;
} }
*out = 0; *out = 0;
u64 x = 0; u64 x = 0;
Result rc = svcCreatePort(out, &target_service->mitm_port_h, target_service->max_sessions, target_service->is_light, (char *)&x); R_TRY(svcCreatePort(out, &target_service->mitm_port_h, target_service->max_sessions, target_service->is_light, (char *)&x));
R_TRY(svcCreateSession(query_out, &target_service->mitm_query_h, 0, 0));
if (R_SUCCEEDED(rc) && R_SUCCEEDED((rc = svcCreateSession(query_out, &target_service->mitm_query_h, 0, 0)))) {
target_service->mitm_pid = pid; target_service->mitm_pid = pid;
}
return ResultSuccess;
return rc;
} }
Result Registration::UninstallMitmForPid(u64 pid, u64 service) { Result Registration::UninstallMitmForPid(u64 pid, u64 service) {
if (!service) { if (!service) {
return ResultSmInvalidServiceName; return ResultSmInvalidServiceName;
} }
u64 service_name_len = GetServiceNameLength(service); u64 service_name_len = GetServiceNameLength(service);
/* If the service has bytes after a null terminator, that's no good. */ /* If the service has bytes after a null terminator, that's no good. */
if (service_name_len != 8 && (service >> (8 * service_name_len))) { if (service_name_len != 8 && (service >> (8 * service_name_len))) {
return ResultSmInvalidServiceName; return ResultSmInvalidServiceName;
} }
Registration::Service *target_service = GetService(service); Registration::Service *target_service = GetService(service);
if (target_service == NULL) { if (target_service == NULL) {
return ResultSmNotRegistered; return ResultSmNotRegistered;
@ -515,7 +508,7 @@ Result Registration::UninstallMitmForPid(u64 pid, u64 service) {
if (!IsInitialProcess(pid) && target_service->mitm_pid != pid) { if (!IsInitialProcess(pid) && target_service->mitm_pid != pid) {
return ResultSmNotAllowed; return ResultSmNotAllowed;
} }
svcCloseHandle(target_service->mitm_port_h); svcCloseHandle(target_service->mitm_port_h);
svcCloseHandle(target_service->mitm_query_h); svcCloseHandle(target_service->mitm_query_h);
target_service->mitm_pid = 0; target_service->mitm_pid = 0;
@ -526,14 +519,14 @@ Result Registration::AcknowledgeMitmSessionForPid(u64 pid, u64 service, Handle *
if (!service) { if (!service) {
return ResultSmInvalidServiceName; return ResultSmInvalidServiceName;
} }
u64 service_name_len = GetServiceNameLength(service); u64 service_name_len = GetServiceNameLength(service);
/* If the service has bytes after a null terminator, that's no good. */ /* If the service has bytes after a null terminator, that's no good. */
if (service_name_len != 8 && (service >> (8 * service_name_len))) { if (service_name_len != 8 && (service >> (8 * service_name_len))) {
return ResultSmInvalidServiceName; return ResultSmInvalidServiceName;
} }
Registration::Service *target_service = GetService(service); Registration::Service *target_service = GetService(service);
if (target_service == NULL) { if (target_service == NULL) {
return ResultSmNotRegistered; return ResultSmNotRegistered;
@ -542,7 +535,7 @@ Result Registration::AcknowledgeMitmSessionForPid(u64 pid, u64 service, Handle *
if ((!IsInitialProcess(pid) && target_service->mitm_pid != pid) || !target_service->mitm_waiting_ack) { if ((!IsInitialProcess(pid) && target_service->mitm_pid != pid) || !target_service->mitm_waiting_ack) {
return ResultSmNotAllowed; return ResultSmNotAllowed;
} }
*out = target_service->mitm_fwd_sess_h; *out = target_service->mitm_fwd_sess_h;
*out_pid = target_service->mitm_waiting_ack_pid; *out_pid = target_service->mitm_waiting_ack_pid;
target_service->mitm_fwd_sess_h = 0; target_service->mitm_fwd_sess_h = 0;
@ -586,26 +579,26 @@ Result Registration::GetServiceRecord(u64 service, SmServiceRecord *out) {
if (!service) { if (!service) {
return ResultSmInvalidServiceName; return ResultSmInvalidServiceName;
} }
u64 service_name_len = GetServiceNameLength(service); u64 service_name_len = GetServiceNameLength(service);
/* If the service has bytes after a null terminator, that's no good. */ /* If the service has bytes after a null terminator, that's no good. */
if (service_name_len != 8 && (service >> (8 * service_name_len))) { if (service_name_len != 8 && (service >> (8 * service_name_len))) {
return ResultSmInvalidServiceName; return ResultSmInvalidServiceName;
} }
Registration::Service *target_service = GetService(service); Registration::Service *target_service = GetService(service);
if (target_service == NULL) { if (target_service == NULL) {
return ResultSmNotRegistered; return ResultSmNotRegistered;
} }
ConvertServiceToRecord(target_service, out); ConvertServiceToRecord(target_service, out);
return ResultSuccess; return ResultSuccess;
} }
void Registration::ListServiceRecords(u64 offset, u64 max_out, SmServiceRecord *out, u64 *out_count) { void Registration::ListServiceRecords(u64 offset, u64 max_out, SmServiceRecord *out, u64 *out_count) {
u64 count = 0; u64 count = 0;
for (auto it = g_service_list.begin(); it != g_service_list.end() && count < max_out; it++) { for (auto it = g_service_list.begin(); it != g_service_list.end() && count < max_out; it++) {
if (it->service_name != 0) { if (it->service_name != 0) {
if (offset > 0) { if (offset > 0) {
@ -616,6 +609,6 @@ void Registration::ListServiceRecords(u64 offset, u64 max_out, SmServiceRecord *
} }
} }
} }
*out_count = count; *out_count = count;
} }

View File

@ -13,7 +13,7 @@
* 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>
@ -31,30 +31,30 @@ class Registration {
u64 sac_size; u64 sac_size;
u8 sac[REGISTRATION_MAX_SAC_SIZE]; u8 sac[REGISTRATION_MAX_SAC_SIZE];
}; };
struct Service { struct Service {
u64 service_name; u64 service_name;
u64 owner_pid; u64 owner_pid;
Handle port_h; Handle port_h;
/* Debug. */ /* Debug. */
u64 max_sessions; u64 max_sessions;
bool is_light; bool is_light;
/* Extension. */ /* Extension. */
u64 mitm_pid; u64 mitm_pid;
Handle mitm_port_h; Handle mitm_port_h;
Handle mitm_query_h; Handle mitm_query_h;
bool mitm_waiting_ack; bool mitm_waiting_ack;
u64 mitm_waiting_ack_pid; u64 mitm_waiting_ack_pid;
Handle mitm_fwd_sess_h; Handle mitm_fwd_sess_h;
}; };
/* Utilities. */ /* Utilities. */
static void EndInitDefers(); static void EndInitDefers();
static bool ShouldInitDefer(u64 service); static bool ShouldInitDefer(u64 service);
static Registration::Process *GetProcessForPid(u64 pid); static Registration::Process *GetProcessForPid(u64 pid);
static Registration::Process *GetFreeProcess(); static Registration::Process *GetFreeProcess();
static Registration::Service *GetService(u64 service); static Registration::Service *GetService(u64 service);
@ -64,11 +64,11 @@ class Registration {
static void CacheInitialProcessIdLimits(); static void CacheInitialProcessIdLimits();
static bool IsInitialProcess(u64 pid); static bool IsInitialProcess(u64 pid);
static u64 GetInitialProcessId(); static u64 GetInitialProcessId();
/* Process management. */ /* Process management. */
static Result RegisterProcess(u64 pid, u8 *acid_sac, size_t acid_sac_size, u8 *aci0_sac, size_t aci0_sac_size); static Result RegisterProcess(u64 pid, u8 *acid_sac, size_t acid_sac_size, u8 *aci0_sac, size_t aci0_sac_size);
static Result UnregisterProcess(u64 pid); static Result UnregisterProcess(u64 pid);
/* Service management. */ /* Service management. */
static bool HasService(u64 service); static bool HasService(u64 service);
static bool HasMitm(u64 service); static bool HasMitm(u64 service);
@ -77,13 +77,13 @@ class Registration {
static Result RegisterServiceForPid(u64 pid, u64 service, u64 max_sessions, bool is_light, Handle *out); static Result RegisterServiceForPid(u64 pid, u64 service, u64 max_sessions, bool is_light, Handle *out);
static Result RegisterServiceForSelf(u64 service, u64 max_sessions, bool is_light, Handle *out); static Result RegisterServiceForSelf(u64 service, u64 max_sessions, bool is_light, Handle *out);
static Result UnregisterServiceForPid(u64 pid, u64 service); static Result UnregisterServiceForPid(u64 pid, u64 service);
/* Extension. */ /* Extension. */
static Result InstallMitmForPid(u64 pid, u64 service, Handle *out, Handle *query_out); static Result InstallMitmForPid(u64 pid, u64 service, Handle *out, Handle *query_out);
static Result UninstallMitmForPid(u64 pid, u64 service); static Result UninstallMitmForPid(u64 pid, u64 service);
static Result AcknowledgeMitmSessionForPid(u64 pid, u64 service, Handle *out, u64 *out_pid); static Result AcknowledgeMitmSessionForPid(u64 pid, u64 service, Handle *out, u64 *out_pid);
static Result AssociatePidTidForMitm(u64 pid, u64 tid); static Result AssociatePidTidForMitm(u64 pid, u64 tid);
static void ConvertServiceToRecord(Registration::Service *service, SmServiceRecord *record); static void ConvertServiceToRecord(Registration::Service *service, SmServiceRecord *record);
static Result GetServiceRecord(u64 service, SmServiceRecord *out); static Result GetServiceRecord(u64 service, SmServiceRecord *out);
static void ListServiceRecords(u64 offset, u64 max_out, SmServiceRecord *out, u64 *out_count); static void ListServiceRecords(u64 offset, u64 max_out, SmServiceRecord *out, u64 *out_count);

View File

@ -13,7 +13,7 @@
* 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
struct SmServiceName { struct SmServiceName {
char name[sizeof(u64)]; char name[sizeof(u64)];

View File

@ -13,7 +13,7 @@
* 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/>.
*/ */
#include <switch.h> #include <switch.h>
#include <stratosphere.hpp> #include <stratosphere.hpp>
#include "sm_user_service.hpp" #include "sm_user_service.hpp"
@ -27,99 +27,82 @@ Result UserService::Initialize(PidDescriptor pid) {
Result UserService::GetService(Out<MovedHandle> out_h, SmServiceName service) { Result UserService::GetService(Out<MovedHandle> out_h, SmServiceName service) {
Handle session_h = 0; Handle session_h = 0;
Result rc = ResultSmInvalidClient;
#ifdef SM_ENABLE_SMHAX
if (!this->has_initialized) { if (!this->has_initialized) {
rc = Registration::GetServiceForPid(Registration::GetInitialProcessId(), smEncodeName(service.name), &session_h); return ResultSmInvalidClient;
} }
#endif
if (this->has_initialized) { R_TRY(Registration::GetServiceForPid(this->pid, smEncodeName(service.name), &session_h));
rc = Registration::GetServiceForPid(this->pid, smEncodeName(service.name), &session_h);
} out_h.SetValue(session_h);
return ResultSuccess;
if (R_SUCCEEDED(rc)) {
out_h.SetValue(session_h);
}
return rc;
} }
Result UserService::RegisterService(Out<MovedHandle> out_h, SmServiceName service, u32 max_sessions, bool is_light) { Result UserService::RegisterService(Out<MovedHandle> out_h, SmServiceName service, u32 max_sessions, bool is_light) {
Handle service_h = 0; Handle service_h = 0;
Result rc = ResultSmInvalidClient;
#ifdef SM_ENABLE_SMHAX
if (!this->has_initialized) { if (!this->has_initialized) {
rc = Registration::RegisterServiceForPid(Registration::GetInitialProcessId(), smEncodeName(service.name), max_sessions, (is_light & 1) != 0, &service_h); return ResultSmInvalidClient;
} }
#endif
if (this->has_initialized) { R_TRY(Registration::RegisterServiceForPid(this->pid, smEncodeName(service.name), max_sessions, (is_light & 1) != 0, &service_h));
rc = Registration::RegisterServiceForPid(this->pid, smEncodeName(service.name), max_sessions, (is_light & 1) != 0, &service_h);
} out_h.SetValue(service_h);
return ResultSuccess;
if (R_SUCCEEDED(rc)) {
out_h.SetValue(service_h);
}
return rc;
} }
Result UserService::UnregisterService(SmServiceName service) { Result UserService::UnregisterService(SmServiceName service) {
Result rc = ResultSmInvalidClient;
#ifdef SM_ENABLE_SMHAX
if (!this->has_initialized) { if (!this->has_initialized) {
rc = Registration::UnregisterServiceForPid(Registration::GetInitialProcessId(), smEncodeName(service.name)); return ResultSmInvalidClient;
} }
#endif
if (this->has_initialized) { return Registration::UnregisterServiceForPid(this->pid, smEncodeName(service.name));
rc = Registration::UnregisterServiceForPid(this->pid, smEncodeName(service.name));
}
return rc;
} }
Result UserService::AtmosphereInstallMitm(Out<MovedHandle> srv_h, Out<MovedHandle> qry_h, SmServiceName service) { Result UserService::AtmosphereInstallMitm(Out<MovedHandle> srv_h, Out<MovedHandle> qry_h, SmServiceName service) {
Handle service_h = 0; Handle service_h = 0;
Handle query_h = 0; Handle query_h = 0;
Result rc = ResultSmInvalidClient;
if (this->has_initialized) { if (!this->has_initialized) {
rc = Registration::InstallMitmForPid(this->pid, smEncodeName(service.name), &service_h, &query_h); return ResultSmInvalidClient;
} }
if (R_SUCCEEDED(rc)) { R_TRY(Registration::InstallMitmForPid(this->pid, smEncodeName(service.name), &service_h, &query_h));
srv_h.SetValue(service_h);
qry_h.SetValue(query_h); srv_h.SetValue(service_h);
} qry_h.SetValue(query_h);
return rc; return ResultSuccess;
} }
Result UserService::AtmosphereUninstallMitm(SmServiceName service) { Result UserService::AtmosphereUninstallMitm(SmServiceName service) {
Result rc = ResultSmInvalidClient; if (!this->has_initialized) {
if (this->has_initialized) { return ResultSmInvalidClient;
rc = Registration::UninstallMitmForPid(this->pid, smEncodeName(service.name));
} }
return rc;
return Registration::UninstallMitmForPid(this->pid, smEncodeName(service.name));
} }
Result UserService::AtmosphereAcknowledgeMitmSession(Out<u64> client_pid, Out<MovedHandle> fwd_h, SmServiceName service) { Result UserService::AtmosphereAcknowledgeMitmSession(Out<u64> client_pid, Out<MovedHandle> fwd_h, SmServiceName service) {
Result rc = ResultSmInvalidClient;
Handle out_fwd_h = 0; Handle out_fwd_h = 0;
if (this->has_initialized) {
rc = Registration::AcknowledgeMitmSessionForPid(this->pid, smEncodeName(service.name), &out_fwd_h, client_pid.GetPointer()); if (!this->has_initialized) {
return ResultSmInvalidClient;
} }
if (R_SUCCEEDED(rc)) { R_TRY(Registration::AcknowledgeMitmSessionForPid(this->pid, smEncodeName(service.name), &out_fwd_h, client_pid.GetPointer()));
fwd_h.SetValue(out_fwd_h);
} fwd_h.SetValue(out_fwd_h);
return ResultSuccess;
return rc;
} }
Result UserService::AtmosphereAssociatePidTidForMitm(u64 pid, u64 tid) { Result UserService::AtmosphereAssociatePidTidForMitm(u64 pid, u64 tid) {
Result rc = ResultSmInvalidClient; if (!this->has_initialized) {
if (this->has_initialized) { return ResultSmInvalidClient;
if (Registration::IsInitialProcess(pid)) {
rc = ResultSmNotAllowed;
} else {
rc = Registration::AssociatePidTidForMitm(pid, tid);
}
} }
return rc;
if (Registration::IsInitialProcess(pid)) {
return ResultSmNotAllowed;
}
return Registration::AssociatePidTidForMitm(pid, tid);
} }

View File

@ -13,7 +13,7 @@
* 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 <stratosphere.hpp> #include <stratosphere.hpp>
@ -24,7 +24,7 @@ enum UserServiceCmd {
User_Cmd_GetService = 1, User_Cmd_GetService = 1,
User_Cmd_RegisterService = 2, User_Cmd_RegisterService = 2,
User_Cmd_UnregisterService = 3, User_Cmd_UnregisterService = 3,
User_Cmd_AtmosphereInstallMitm = 65000, User_Cmd_AtmosphereInstallMitm = 65000,
User_Cmd_AtmosphereUninstallMitm = 65001, User_Cmd_AtmosphereUninstallMitm = 65001,
User_Cmd_AtmosphereAssociatePidTidForMitm = 65002, User_Cmd_AtmosphereAssociatePidTidForMitm = 65002,
@ -35,13 +35,13 @@ class UserService final : public IServiceObject {
private: private:
u64 pid = U64_MAX; u64 pid = U64_MAX;
bool has_initialized = false; bool has_initialized = false;
/* Actual commands. */ /* Actual commands. */
virtual Result Initialize(PidDescriptor pid); virtual Result Initialize(PidDescriptor pid);
virtual Result GetService(Out<MovedHandle> out_h, SmServiceName service); virtual Result GetService(Out<MovedHandle> out_h, SmServiceName service);
virtual Result RegisterService(Out<MovedHandle> out_h, SmServiceName service, u32 max_sessions, bool is_light); virtual Result RegisterService(Out<MovedHandle> out_h, SmServiceName service, u32 max_sessions, bool is_light);
virtual Result UnregisterService(SmServiceName service); virtual Result UnregisterService(SmServiceName service);
/* Atmosphere commands. */ /* Atmosphere commands. */
virtual Result AtmosphereInstallMitm(Out<MovedHandle> srv_h, Out<MovedHandle> qry_h, SmServiceName service); virtual Result AtmosphereInstallMitm(Out<MovedHandle> srv_h, Out<MovedHandle> qry_h, SmServiceName service);
virtual Result AtmosphereUninstallMitm(SmServiceName service); virtual Result AtmosphereUninstallMitm(SmServiceName service);
@ -53,7 +53,7 @@ class UserService final : public IServiceObject {
MakeServiceCommandMeta<User_Cmd_GetService, &UserService::GetService>(), MakeServiceCommandMeta<User_Cmd_GetService, &UserService::GetService>(),
MakeServiceCommandMeta<User_Cmd_RegisterService, &UserService::RegisterService>(), MakeServiceCommandMeta<User_Cmd_RegisterService, &UserService::RegisterService>(),
MakeServiceCommandMeta<User_Cmd_UnregisterService, &UserService::UnregisterService>(), MakeServiceCommandMeta<User_Cmd_UnregisterService, &UserService::UnregisterService>(),
#ifdef SM_ENABLE_MITM #ifdef SM_ENABLE_MITM
MakeServiceCommandMeta<User_Cmd_AtmosphereInstallMitm, &UserService::AtmosphereInstallMitm>(), MakeServiceCommandMeta<User_Cmd_AtmosphereInstallMitm, &UserService::AtmosphereInstallMitm>(),
MakeServiceCommandMeta<User_Cmd_AtmosphereUninstallMitm, &UserService::AtmosphereUninstallMitm>(), MakeServiceCommandMeta<User_Cmd_AtmosphereUninstallMitm, &UserService::AtmosphereUninstallMitm>(),