2018-09-07 17:00:13 +02:00
|
|
|
/*
|
2019-04-08 04:00:49 +02:00
|
|
|
* Copyright (c) 2018-2019 Atmosphère-NX
|
2018-09-07 17:00:13 +02:00
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2018-04-22 08:11:57 +02:00
|
|
|
#include <switch.h>
|
|
|
|
#include <algorithm>
|
2018-10-30 01:28:37 +01:00
|
|
|
#include <stratosphere.hpp>
|
2018-04-22 08:11:57 +02:00
|
|
|
#include "sm_registration.hpp"
|
2018-06-19 20:07:31 +02:00
|
|
|
#include "meta_tools.hpp"
|
2018-04-22 08:11:57 +02:00
|
|
|
|
2018-06-19 20:07:31 +02:00
|
|
|
static std::array<Registration::Process, REGISTRATION_LIST_MAX_PROCESS> g_process_list = {0};
|
|
|
|
static std::array<Registration::Service, REGISTRATION_LIST_MAX_SERVICE> g_service_list = {0};
|
2018-04-22 08:11:57 +02:00
|
|
|
|
2018-05-02 07:21:39 +02:00
|
|
|
static u64 g_initial_process_id_low = 0;
|
|
|
|
static u64 g_initial_process_id_high = 0;
|
|
|
|
static bool g_determined_initial_process_ids = false;
|
2018-10-18 01:26:36 +02:00
|
|
|
static bool g_end_init_defers = false;
|
2018-05-02 07:21:39 +02:00
|
|
|
|
2018-05-01 06:27:07 +02:00
|
|
|
u64 GetServiceNameLength(u64 service) {
|
|
|
|
u64 service_name_len = 0;
|
|
|
|
while (service & 0xFF) {
|
|
|
|
service_name_len++;
|
|
|
|
service >>= 8;
|
|
|
|
}
|
|
|
|
return service_name_len;
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:26:36 +02:00
|
|
|
/* Atmosphere extension utilities. */
|
|
|
|
void Registration::EndInitDefers() {
|
|
|
|
g_end_init_defers = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr u64 EncodeNameConstant(const char *name) {
|
|
|
|
u64 service = 0;
|
|
|
|
for (unsigned int i = 0; i < sizeof(service); i++) {
|
|
|
|
if (name[i] == '\x00') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
service |= ((u64)name[i]) << (8 * i);
|
|
|
|
}
|
|
|
|
return service;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Registration::ShouldInitDefer(u64 service) {
|
|
|
|
/* Only enable if compile-time generated. */
|
|
|
|
#ifndef SM_ENABLE_INIT_DEFERS
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (g_end_init_defers) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
constexpr u64 FSP_SRV = EncodeNameConstant("fsp-srv");
|
|
|
|
return service == FSP_SRV;
|
|
|
|
}
|
|
|
|
|
2018-04-22 08:11:57 +02:00
|
|
|
/* Utilities. */
|
|
|
|
Registration::Process *Registration::GetProcessForPid(u64 pid) {
|
2018-06-19 20:07:31 +02:00
|
|
|
auto process_it = std::find_if(g_process_list.begin(), g_process_list.end(), member_equals_fn(&Process::pid, pid));
|
|
|
|
if (process_it == g_process_list.end()) {
|
|
|
|
return nullptr;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
2018-06-19 20:07:31 +02:00
|
|
|
return &*process_it;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Registration::Process *Registration::GetFreeProcess() {
|
|
|
|
return GetProcessForPid(0);
|
|
|
|
}
|
|
|
|
|
2018-05-04 01:24:34 +02:00
|
|
|
Registration::Service *Registration::GetService(u64 service_name) {
|
2018-06-19 20:07:31 +02:00
|
|
|
auto service_it = std::find_if(g_service_list.begin(), g_service_list.end(), member_equals_fn(&Service::service_name, service_name));
|
|
|
|
if (service_it == g_service_list.end()) {
|
|
|
|
return nullptr;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
2018-06-19 20:07:31 +02:00
|
|
|
return &*service_it;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Registration::Service *Registration::GetFreeService() {
|
|
|
|
return GetService(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Registration::IsValidForSac(u8 *sac, size_t sac_size, u64 service, bool is_host) {
|
|
|
|
u8 cur_ctrl;
|
|
|
|
u64 cur_service;
|
2018-04-22 13:13:33 +02:00
|
|
|
u64 service_for_compare;
|
2018-04-22 08:11:57 +02:00
|
|
|
bool cur_is_host;
|
|
|
|
size_t remaining = sac_size;
|
|
|
|
while (remaining) {
|
|
|
|
cur_ctrl = *sac++;
|
|
|
|
remaining--;
|
|
|
|
size_t cur_size = (cur_ctrl & 7) + 1;
|
|
|
|
if (cur_size > remaining) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cur_is_host = (cur_ctrl & 0x80) != 0;
|
|
|
|
cur_service = 0;
|
|
|
|
for (unsigned int i = 0; i < cur_size; i++) {
|
|
|
|
cur_service |= ((u64)sac[i]) << (8 * i);
|
|
|
|
}
|
|
|
|
/* Check if the last byte is a wildcard ('*') */
|
2018-04-22 13:13:33 +02:00
|
|
|
service_for_compare = service;
|
2018-04-22 08:11:57 +02:00
|
|
|
if (sac[cur_size - 1] == '*') {
|
2018-04-22 13:13:33 +02:00
|
|
|
u64 mask = U64_MAX;
|
|
|
|
for (unsigned int i = 0; i < 8 - (cur_size - 1); i++) {
|
|
|
|
mask >>= 8;
|
|
|
|
}
|
2018-04-22 08:11:57 +02:00
|
|
|
/* Mask cur_service, service with 0xFF.. up until the wildcard. */
|
2018-04-22 13:13:33 +02:00
|
|
|
cur_service &= mask;
|
|
|
|
service_for_compare &= mask;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
2018-05-08 11:02:57 +02:00
|
|
|
if (cur_service == service_for_compare && (is_host == cur_is_host)) {
|
2018-04-22 08:11:57 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
sac += cur_size;
|
|
|
|
remaining -= cur_size;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Registration::ValidateSacAgainstRestriction(u8 *r_sac, size_t r_sac_size, u8 *sac, size_t sac_size) {
|
|
|
|
u8 cur_ctrl;
|
|
|
|
u64 cur_service;
|
|
|
|
bool cur_is_host;
|
|
|
|
size_t remaining = sac_size;
|
|
|
|
while (remaining) {
|
|
|
|
cur_ctrl = *sac++;
|
|
|
|
remaining--;
|
|
|
|
size_t cur_size = (cur_ctrl & 7) + 1;
|
|
|
|
if (cur_size > remaining) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cur_is_host = (cur_ctrl & 0x80) != 0;
|
|
|
|
cur_service = 0;
|
|
|
|
for (unsigned int i = 0; i < cur_size; i++) {
|
|
|
|
cur_service |= ((u64)sac[i]) << (8 * i);
|
|
|
|
}
|
|
|
|
if (!IsValidForSac(r_sac, r_sac_size, cur_service, cur_is_host)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sac += cur_size;
|
|
|
|
remaining -= cur_size;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-05-02 07:21:39 +02:00
|
|
|
void Registration::CacheInitialProcessIdLimits() {
|
|
|
|
if (g_determined_initial_process_ids) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-02 07:24:57 +02:00
|
|
|
if (kernelAbove500()) {
|
2018-05-02 07:21:39 +02:00
|
|
|
svcGetSystemInfo(&g_initial_process_id_low, 2, 0, 0);
|
2018-05-08 06:47:55 +02:00
|
|
|
svcGetSystemInfo(&g_initial_process_id_high, 2, 0, 1);
|
2018-05-02 07:21:39 +02:00
|
|
|
} else {
|
|
|
|
g_initial_process_id_low = 0;
|
|
|
|
g_initial_process_id_high = REGISTRATION_INITIAL_PID_MAX;
|
|
|
|
}
|
|
|
|
g_determined_initial_process_ids = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Registration::IsInitialProcess(u64 pid) {
|
|
|
|
CacheInitialProcessIdLimits();
|
|
|
|
return g_initial_process_id_low <= pid && pid <= g_initial_process_id_high;
|
|
|
|
}
|
|
|
|
|
|
|
|
u64 Registration::GetInitialProcessId() {
|
|
|
|
CacheInitialProcessIdLimits();
|
2018-11-30 11:42:48 +01:00
|
|
|
if (IsInitialProcess(1)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2018-05-02 07:21:39 +02:00
|
|
|
return g_initial_process_id_low;
|
|
|
|
}
|
|
|
|
|
2018-04-22 08:11:57 +02:00
|
|
|
/* Process management. */
|
|
|
|
Result Registration::RegisterProcess(u64 pid, u8 *acid_sac, size_t acid_sac_size, u8 *aci0_sac, size_t aci0_sac_size) {
|
|
|
|
if (aci0_sac_size > REGISTRATION_MAX_SAC_SIZE) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmTooLargeAccessControl;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
2019-03-28 23:28:36 +01:00
|
|
|
|
|
|
|
Registration::Process *proc = GetFreeProcess();
|
2018-04-22 08:11:57 +02:00
|
|
|
if (proc == NULL) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmInsufficientProcesses;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aci0_sac_size && !ValidateSacAgainstRestriction(acid_sac, acid_sac_size, aci0_sac, aci0_sac_size)) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmNotAllowed;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
proc->pid = pid;
|
|
|
|
proc->sac_size = aci0_sac_size;
|
|
|
|
std::copy(aci0_sac, aci0_sac + aci0_sac_size, proc->sac);
|
2019-03-29 06:39:39 +01:00
|
|
|
return ResultSuccess;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Result Registration::UnregisterProcess(u64 pid) {
|
|
|
|
Registration::Process *proc = GetProcessForPid(pid);
|
|
|
|
if (proc == NULL) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmInvalidClient;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
proc->pid = 0;
|
2019-03-29 06:39:39 +01:00
|
|
|
return ResultSuccess;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Service management. */
|
|
|
|
bool Registration::HasService(u64 service) {
|
2018-06-19 20:07:31 +02:00
|
|
|
return std::any_of(g_service_list.begin(), g_service_list.end(), member_equals_fn(&Service::service_name, service));
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
|
|
|
|
2018-11-30 11:42:48 +01:00
|
|
|
bool Registration::HasMitm(u64 service) {
|
|
|
|
Registration::Service *target_service = GetService(service);
|
|
|
|
return target_service != NULL && target_service->mitm_pid != 0;
|
|
|
|
}
|
|
|
|
|
2018-06-03 07:38:01 +02:00
|
|
|
Result Registration::GetServiceHandle(u64 pid, u64 service, Handle *out) {
|
2018-04-22 11:02:08 +02:00
|
|
|
Registration::Service *target_service = GetService(service);
|
2018-11-15 23:19:34 +01:00
|
|
|
if (target_service == NULL || ShouldInitDefer(service) || target_service->mitm_waiting_ack) {
|
2018-04-22 11:02:08 +02:00
|
|
|
/* Note: This defers the result until later. */
|
2019-03-29 04:00:16 +01:00
|
|
|
return ResultServiceFrameworkRequestDeferredByUser;
|
2018-04-22 11:02:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*out = 0;
|
2018-06-03 07:38:01 +02:00
|
|
|
Result rc;
|
|
|
|
if (target_service->mitm_pid == 0 || target_service->mitm_pid == pid) {
|
|
|
|
rc = svcConnectToPort(out, target_service->port_h);
|
|
|
|
} else {
|
2018-06-15 01:50:01 +02:00
|
|
|
IpcCommand c;
|
|
|
|
ipcInitialize(&c);
|
|
|
|
struct {
|
|
|
|
u64 magic;
|
|
|
|
u64 cmd_id;
|
|
|
|
u64 pid;
|
|
|
|
} *info = ((decltype(info))ipcPrepareHeader(&c, sizeof(*info)));
|
|
|
|
info->magic = SFCI_MAGIC;
|
|
|
|
info->cmd_id = 65000;
|
|
|
|
info->pid = pid;
|
|
|
|
rc = ipcDispatch(target_service->mitm_query_h);
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
IpcParsedCommand r;
|
|
|
|
ipcParse(&r);
|
|
|
|
struct {
|
|
|
|
u64 magic;
|
|
|
|
u64 result;
|
2018-10-30 01:28:37 +01:00
|
|
|
bool should_mitm;
|
2018-06-15 01:50:01 +02:00
|
|
|
} *resp = ((decltype(resp))r.Raw);
|
|
|
|
rc = resp->result;
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
if (resp->should_mitm) {
|
2018-11-15 23:19:34 +01:00
|
|
|
rc = svcConnectToPort(&target_service->mitm_fwd_sess_h, target_service->port_h);
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
rc = svcConnectToPort(out, target_service->mitm_port_h);
|
|
|
|
if (R_SUCCEEDED(rc)) {
|
|
|
|
target_service->mitm_waiting_ack_pid = pid;
|
|
|
|
target_service->mitm_waiting_ack = true;
|
|
|
|
} else {
|
|
|
|
svcCloseHandle(target_service->mitm_fwd_sess_h);
|
|
|
|
target_service->mitm_fwd_sess_h = 0;
|
|
|
|
}
|
|
|
|
}
|
2018-06-15 01:50:01 +02:00
|
|
|
} else {
|
|
|
|
rc = svcConnectToPort(out, target_service->port_h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
rc = svcConnectToPort(out, target_service->port_h);
|
|
|
|
}
|
2018-06-03 07:38:01 +02:00
|
|
|
}
|
2018-04-22 11:02:08 +02:00
|
|
|
if (R_FAILED(rc)) {
|
2019-03-29 00:57:18 +01:00
|
|
|
if ((rc & 0x3FFFFF) == ResultKernelOutOfSessions) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmInsufficientSessions;
|
2018-04-22 11:02:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2018-04-22 08:11:57 +02:00
|
|
|
Result Registration::GetServiceForPid(u64 pid, u64 service, Handle *out) {
|
|
|
|
if (!service) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmInvalidServiceName;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
|
|
|
|
2018-05-01 06:27:07 +02:00
|
|
|
u64 service_name_len = GetServiceNameLength(service);
|
2018-04-22 08:11:57 +02:00
|
|
|
|
|
|
|
/* If the service has bytes after a null terminator, that's no good. */
|
2018-05-01 06:27:07 +02:00
|
|
|
if (service_name_len != 8 && (service >> (8 * service_name_len))) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmInvalidServiceName;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
|
|
|
|
2018-05-02 07:21:39 +02:00
|
|
|
if (!IsInitialProcess(pid)) {
|
2018-04-22 08:11:57 +02:00
|
|
|
Registration::Process *proc = GetProcessForPid(pid);
|
|
|
|
if (proc == NULL) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmInvalidClient;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsValidForSac(proc->sac, proc->sac_size, service, false)) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmNotAllowed;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-03 07:38:01 +02:00
|
|
|
return GetServiceHandle(pid, service, out);
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Result Registration::RegisterServiceForPid(u64 pid, u64 service, u64 max_sessions, bool is_light, Handle *out) {
|
|
|
|
if (!service) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmInvalidServiceName;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
|
|
|
|
2018-05-01 06:27:07 +02:00
|
|
|
u64 service_name_len = GetServiceNameLength(service);
|
2018-04-22 08:11:57 +02:00
|
|
|
|
|
|
|
/* If the service has bytes after a null terminator, that's no good. */
|
2018-05-01 06:27:07 +02:00
|
|
|
if (service_name_len != 8 && (service >> (8 * service_name_len))) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmInvalidServiceName;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
|
|
|
|
2018-05-02 07:21:39 +02:00
|
|
|
if (!IsInitialProcess(pid)) {
|
2018-04-22 08:11:57 +02:00
|
|
|
Registration::Process *proc = GetProcessForPid(pid);
|
|
|
|
if (proc == NULL) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmInvalidClient;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsValidForSac(proc->sac, proc->sac_size, service, true)) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmNotAllowed;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (HasService(service)) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmAlreadyRegistered;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
|
|
|
|
2018-06-10 09:29:42 +02:00
|
|
|
#ifdef SM_MINIMUM_SESSION_LIMIT
|
|
|
|
if (max_sessions < SM_MINIMUM_SESSION_LIMIT) {
|
|
|
|
max_sessions = SM_MINIMUM_SESSION_LIMIT;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-04-22 08:11:57 +02:00
|
|
|
Registration::Service *free_service = GetFreeService();
|
|
|
|
if (free_service == NULL) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmInsufficientServices;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*out = 0;
|
|
|
|
*free_service = (const Registration::Service){0};
|
|
|
|
Result rc = 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->owner_pid = pid;
|
2018-06-10 03:33:22 +02:00
|
|
|
free_service->max_sessions = max_sessions;
|
|
|
|
free_service->is_light = is_light;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2018-04-22 08:32:58 +02:00
|
|
|
Result Registration::RegisterServiceForSelf(u64 service, u64 max_sessions, bool is_light, Handle *out) {
|
|
|
|
u64 pid;
|
|
|
|
Result rc = svcGetProcessId(&pid, CUR_PROCESS_HANDLE);
|
|
|
|
if (R_FAILED(rc)) {
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2018-05-01 06:27:07 +02:00
|
|
|
u64 service_name_len = GetServiceNameLength(service);
|
2018-04-22 08:32:58 +02:00
|
|
|
|
|
|
|
/* If the service has bytes after a null terminator, that's no good. */
|
2018-05-01 06:27:07 +02:00
|
|
|
if (service_name_len != 8 && (service >> (8 * service_name_len))) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmInvalidServiceName;
|
2018-04-22 08:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (HasService(service)) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmAlreadyRegistered;
|
2018-04-22 08:32:58 +02:00
|
|
|
}
|
2018-06-10 09:29:42 +02:00
|
|
|
|
|
|
|
#ifdef SM_MINIMUM_SESSION_LIMIT
|
|
|
|
if (max_sessions < SM_MINIMUM_SESSION_LIMIT) {
|
|
|
|
max_sessions = SM_MINIMUM_SESSION_LIMIT;
|
|
|
|
}
|
|
|
|
#endif
|
2018-04-22 08:32:58 +02:00
|
|
|
|
|
|
|
Registration::Service *free_service = GetFreeService();
|
|
|
|
if (free_service == NULL) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmInsufficientServices;
|
2018-04-22 08:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*out = 0;
|
|
|
|
*free_service = (const Registration::Service){0};
|
|
|
|
rc = 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->owner_pid = pid;
|
2018-06-03 07:38:01 +02:00
|
|
|
free_service->max_sessions = max_sessions;
|
|
|
|
free_service->is_light = is_light;
|
2018-04-22 08:32:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2018-04-22 08:11:57 +02:00
|
|
|
Result Registration::UnregisterServiceForPid(u64 pid, u64 service) {
|
|
|
|
if (!service) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmInvalidServiceName;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
|
|
|
|
2018-05-01 06:27:07 +02:00
|
|
|
u64 service_name_len = GetServiceNameLength(service);
|
2018-04-22 08:11:57 +02:00
|
|
|
|
|
|
|
/* If the service has bytes after a null terminator, that's no good. */
|
2018-05-01 06:27:07 +02:00
|
|
|
if (service_name_len != 8 && (service >> (8 * service_name_len))) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmInvalidServiceName;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Registration::Service *target_service = GetService(service);
|
|
|
|
if (target_service == NULL) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmNotRegistered;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
|
|
|
|
2018-05-02 07:21:39 +02:00
|
|
|
if (!IsInitialProcess(pid) && target_service->owner_pid != pid) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmNotAllowed;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
svcCloseHandle(target_service->port_h);
|
2018-06-03 07:38:01 +02:00
|
|
|
svcCloseHandle(target_service->mitm_port_h);
|
2018-06-15 01:50:01 +02:00
|
|
|
svcCloseHandle(target_service->mitm_query_h);
|
2018-04-22 08:11:57 +02:00
|
|
|
*target_service = (const Registration::Service){0};
|
2019-03-29 06:39:39 +01:00
|
|
|
return ResultSuccess;
|
2018-04-22 08:11:57 +02:00
|
|
|
}
|
2018-06-03 07:38:01 +02:00
|
|
|
|
|
|
|
|
2018-06-15 01:50:01 +02:00
|
|
|
Result Registration::InstallMitmForPid(u64 pid, u64 service, Handle *out, Handle *query_out) {
|
2018-06-03 07:38:01 +02:00
|
|
|
if (!service) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmInvalidServiceName;
|
2018-06-03 07:38:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 service_name_len = GetServiceNameLength(service);
|
|
|
|
|
|
|
|
/* If the service has bytes after a null terminator, that's no good. */
|
|
|
|
if (service_name_len != 8 && (service >> (8 * service_name_len))) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmInvalidServiceName;
|
2018-06-03 07:38:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify we're allowed to mitm the service. */
|
|
|
|
if (!IsInitialProcess(pid)) {
|
|
|
|
Registration::Process *proc = GetProcessForPid(pid);
|
|
|
|
if (proc == NULL) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmInvalidClient;
|
2018-06-03 07:38:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsValidForSac(proc->sac, proc->sac_size, service, true)) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmNotAllowed;
|
2018-06-03 07:38:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify the service exists. */
|
|
|
|
Registration::Service *target_service = GetService(service);
|
|
|
|
if (target_service == NULL) {
|
2019-03-29 04:00:16 +01:00
|
|
|
return ResultServiceFrameworkRequestDeferredByUser;
|
2018-06-03 07:38:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify the service isn't already being mitm'd. */
|
|
|
|
if (target_service->mitm_pid != 0) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmAlreadyRegistered;
|
2018-06-03 07:38:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
*out = 0;
|
|
|
|
u64 x = 0;
|
|
|
|
Result rc = svcCreatePort(out, &target_service->mitm_port_h, target_service->max_sessions, target_service->is_light, (char *)&x);
|
|
|
|
|
2018-06-15 01:50:01 +02:00
|
|
|
if (R_SUCCEEDED(rc) && R_SUCCEEDED((rc = svcCreateSession(query_out, &target_service->mitm_query_h, 0, 0)))) {
|
2018-06-03 07:38:01 +02:00
|
|
|
target_service->mitm_pid = pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result Registration::UninstallMitmForPid(u64 pid, u64 service) {
|
|
|
|
if (!service) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmInvalidServiceName;
|
2018-06-03 07:38:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 service_name_len = GetServiceNameLength(service);
|
|
|
|
|
|
|
|
/* If the service has bytes after a null terminator, that's no good. */
|
|
|
|
if (service_name_len != 8 && (service >> (8 * service_name_len))) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmInvalidServiceName;
|
2018-06-03 07:38:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Registration::Service *target_service = GetService(service);
|
|
|
|
if (target_service == NULL) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmNotRegistered;
|
2018-06-03 07:38:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsInitialProcess(pid) && target_service->mitm_pid != pid) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmNotAllowed;
|
2018-06-03 07:38:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
svcCloseHandle(target_service->mitm_port_h);
|
2018-06-15 01:50:01 +02:00
|
|
|
svcCloseHandle(target_service->mitm_query_h);
|
2018-06-03 07:38:01 +02:00
|
|
|
target_service->mitm_pid = 0;
|
2019-03-29 06:39:39 +01:00
|
|
|
return ResultSuccess;
|
2018-06-03 07:38:01 +02:00
|
|
|
}
|
2018-06-15 01:50:01 +02:00
|
|
|
|
2018-11-15 23:19:34 +01:00
|
|
|
Result Registration::AcknowledgeMitmSessionForPid(u64 pid, u64 service, Handle *out, u64 *out_pid) {
|
|
|
|
if (!service) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmInvalidServiceName;
|
2018-11-15 23:19:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 service_name_len = GetServiceNameLength(service);
|
|
|
|
|
|
|
|
/* If the service has bytes after a null terminator, that's no good. */
|
|
|
|
if (service_name_len != 8 && (service >> (8 * service_name_len))) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmInvalidServiceName;
|
2018-11-15 23:19:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Registration::Service *target_service = GetService(service);
|
|
|
|
if (target_service == NULL) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmNotRegistered;
|
2018-11-15 23:19:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((!IsInitialProcess(pid) && target_service->mitm_pid != pid) || !target_service->mitm_waiting_ack) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmNotAllowed;
|
2018-11-15 23:19:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
*out = target_service->mitm_fwd_sess_h;
|
|
|
|
*out_pid = target_service->mitm_waiting_ack_pid;
|
|
|
|
target_service->mitm_fwd_sess_h = 0;
|
|
|
|
target_service->mitm_waiting_ack_pid = 0;
|
|
|
|
target_service->mitm_waiting_ack = false;
|
2019-03-29 06:39:39 +01:00
|
|
|
return ResultSuccess;
|
2018-11-15 23:19:34 +01:00
|
|
|
}
|
|
|
|
|
2018-06-15 01:50:01 +02:00
|
|
|
Result Registration::AssociatePidTidForMitm(u64 pid, u64 tid) {
|
|
|
|
for (auto &service : g_service_list) {
|
|
|
|
if (service.mitm_pid) {
|
|
|
|
IpcCommand c;
|
|
|
|
ipcInitialize(&c);
|
|
|
|
struct {
|
|
|
|
u64 magic;
|
|
|
|
u64 cmd_id;
|
|
|
|
u64 pid;
|
|
|
|
u64 tid;
|
|
|
|
} *info = ((decltype(info))ipcPrepareHeader(&c, sizeof(*info)));
|
|
|
|
info->magic = SFCI_MAGIC;
|
|
|
|
info->cmd_id = 65001;
|
|
|
|
info->pid = pid;
|
|
|
|
info->tid = tid;
|
|
|
|
ipcDispatch(service.mitm_query_h);
|
|
|
|
}
|
|
|
|
}
|
2019-03-29 06:39:39 +01:00
|
|
|
return ResultSuccess;
|
2018-06-15 01:50:01 +02:00
|
|
|
}
|
2019-01-21 00:59:09 +01:00
|
|
|
|
|
|
|
void Registration::ConvertServiceToRecord(Registration::Service *service, SmServiceRecord *record) {
|
|
|
|
record->service_name = service->service_name;
|
|
|
|
record->owner_pid = service->owner_pid;
|
|
|
|
record->max_sessions = service->max_sessions;
|
|
|
|
record->mitm_pid = service->mitm_pid;
|
|
|
|
record->mitm_waiting_ack_pid = service->mitm_waiting_ack_pid;
|
|
|
|
record->is_light = service->is_light;
|
|
|
|
record->mitm_waiting_ack = service->mitm_waiting_ack;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result Registration::GetServiceRecord(u64 service, SmServiceRecord *out) {
|
|
|
|
if (!service) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmInvalidServiceName;
|
2019-01-21 00:59:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
u64 service_name_len = GetServiceNameLength(service);
|
|
|
|
|
|
|
|
/* If the service has bytes after a null terminator, that's no good. */
|
|
|
|
if (service_name_len != 8 && (service >> (8 * service_name_len))) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmInvalidServiceName;
|
2019-01-21 00:59:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Registration::Service *target_service = GetService(service);
|
|
|
|
if (target_service == NULL) {
|
2019-03-28 23:28:36 +01:00
|
|
|
return ResultSmNotRegistered;
|
2019-01-21 00:59:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
ConvertServiceToRecord(target_service, out);
|
2019-03-29 06:39:39 +01:00
|
|
|
return ResultSuccess;
|
2019-01-21 00:59:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void Registration::ListServiceRecords(u64 offset, u64 max_out, SmServiceRecord *out, u64 *out_count) {
|
|
|
|
u64 count = 0;
|
|
|
|
|
|
|
|
for (auto it = g_service_list.begin(); it != g_service_list.end() && count < max_out; it++) {
|
|
|
|
if (it->service_name != 0) {
|
|
|
|
if (offset > 0) {
|
|
|
|
offset--;
|
|
|
|
} else {
|
|
|
|
ConvertServiceToRecord(it, out++);
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_count = count;
|
|
|
|
}
|