2019-06-21 02:23:40 +01:00
|
|
|
/*
|
2020-01-24 10:10:40 +00:00
|
|
|
* Copyright (c) 2018-2020 Atmosphère-NX
|
2019-06-21 02:23:40 +01: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/>.
|
|
|
|
*/
|
2020-05-11 23:02:10 +01:00
|
|
|
#include <stratosphere.hpp>
|
2019-06-21 02:23:40 +01:00
|
|
|
#include "sm_service_manager.hpp"
|
2020-12-31 08:29:06 +00:00
|
|
|
#include "sm_wait_list.hpp"
|
2019-06-21 02:23:40 +01:00
|
|
|
|
2019-10-24 10:30:10 +01:00
|
|
|
namespace ams::sm::impl {
|
2019-06-21 02:23:40 +01:00
|
|
|
|
|
|
|
namespace {
|
2020-12-31 08:29:06 +00:00
|
|
|
|
2019-06-21 02:23:40 +01:00
|
|
|
/* Constexpr definitions. */
|
2019-07-12 06:23:23 +01:00
|
|
|
static constexpr size_t ProcessCountMax = 0x40;
|
2021-02-23 22:31:24 +00:00
|
|
|
static constexpr size_t ServiceCountMax = 0x100 + 0x10; /* Extra 0x10 services over Nintendo for homebrew. */
|
2019-07-12 06:23:23 +01:00
|
|
|
static constexpr size_t FutureMitmCountMax = 0x20;
|
2019-06-21 02:23:40 +01:00
|
|
|
static constexpr size_t AccessControlSizeMax = 0x200;
|
|
|
|
|
2020-12-31 08:29:06 +00:00
|
|
|
constexpr auto InitiallyDeferredServiceName = ServiceName::Encode("fsp-srv");
|
|
|
|
|
2019-06-21 02:23:40 +01:00
|
|
|
/* Types. */
|
|
|
|
struct ProcessInfo {
|
2019-10-28 04:43:01 +00:00
|
|
|
os::ProcessId process_id;
|
|
|
|
ncm::ProgramId program_id;
|
2019-11-21 12:03:19 +00:00
|
|
|
cfg::OverrideStatus override_status;
|
2019-06-21 02:23:40 +01:00
|
|
|
size_t access_control_size;
|
|
|
|
u8 access_control[AccessControlSizeMax];
|
2019-06-21 07:34:59 +01:00
|
|
|
|
|
|
|
ProcessInfo() {
|
|
|
|
this->Free();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Free() {
|
2019-10-28 04:43:01 +00:00
|
|
|
this->process_id = os::InvalidProcessId;
|
|
|
|
this->program_id = ncm::InvalidProgramId;
|
2019-11-21 12:03:19 +00:00
|
|
|
this->override_status = {};
|
2019-06-21 07:34:59 +01:00
|
|
|
this->access_control_size = 0;
|
|
|
|
std::memset(this->access_control, 0, sizeof(this->access_control));
|
|
|
|
}
|
2019-06-21 02:23:40 +01:00
|
|
|
};
|
|
|
|
|
2019-07-04 06:57:49 +01:00
|
|
|
/* Forward declaration, for use in ServiceInfo. */
|
2019-11-21 12:03:19 +00:00
|
|
|
void GetMitmProcessInfo(MitmProcessInfo *out, os::ProcessId process_id);
|
2019-07-04 06:57:49 +01:00
|
|
|
|
2019-06-21 02:23:40 +01:00
|
|
|
struct ServiceInfo {
|
|
|
|
ServiceName name;
|
2019-10-28 04:43:01 +00:00
|
|
|
os::ProcessId owner_process_id;
|
|
|
|
os::ProcessId mitm_process_id;
|
2021-01-17 15:55:32 +00:00
|
|
|
os::ProcessId mitm_waiting_ack_process_id;
|
2019-09-28 02:04:58 +01:00
|
|
|
os::ManagedHandle mitm_port_h;
|
|
|
|
os::ManagedHandle mitm_query_h;
|
2021-01-17 15:55:32 +00:00
|
|
|
os::ManagedHandle port_h;
|
2019-09-28 02:04:58 +01:00
|
|
|
os::ManagedHandle mitm_fwd_sess_h;
|
2021-01-17 15:55:32 +00:00
|
|
|
s32 max_sessions;
|
|
|
|
bool is_light;
|
|
|
|
bool mitm_waiting_ack;
|
2019-06-21 07:34:59 +01:00
|
|
|
|
|
|
|
ServiceInfo() {
|
|
|
|
this->Free();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Free() {
|
|
|
|
/* Close any open handles. */
|
2019-06-21 07:51:15 +01:00
|
|
|
this->port_h.Clear();
|
|
|
|
this->mitm_port_h.Clear();
|
|
|
|
this->mitm_query_h.Clear();
|
|
|
|
this->mitm_fwd_sess_h.Clear();
|
2019-06-21 07:34:59 +01:00
|
|
|
|
2019-06-21 07:51:15 +01:00
|
|
|
/* Reset all other members. */
|
2019-06-21 07:34:59 +01:00
|
|
|
this->name = InvalidServiceName;
|
2019-10-28 04:43:01 +00:00
|
|
|
this->owner_process_id = os::InvalidProcessId;
|
2019-06-21 07:34:59 +01:00
|
|
|
this->max_sessions = 0;
|
|
|
|
this->is_light = false;
|
2019-10-28 04:43:01 +00:00
|
|
|
this->mitm_process_id = os::InvalidProcessId;
|
2019-06-21 07:34:59 +01:00
|
|
|
this->mitm_waiting_ack = false;
|
2019-10-28 04:43:01 +00:00
|
|
|
this->mitm_waiting_ack_process_id = os::InvalidProcessId;
|
2019-06-21 07:34:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void FreeMitm() {
|
|
|
|
/* Close mitm handles. */
|
2019-06-21 07:51:15 +01:00
|
|
|
this->mitm_port_h.Clear();
|
|
|
|
this->mitm_query_h.Clear();
|
2019-06-21 07:34:59 +01:00
|
|
|
|
|
|
|
/* Reset mitm members. */
|
2019-10-28 04:43:01 +00:00
|
|
|
this->mitm_process_id = os::InvalidProcessId;
|
2019-06-21 07:34:59 +01:00
|
|
|
}
|
|
|
|
|
2019-11-21 12:03:19 +00:00
|
|
|
void AcknowledgeMitmSession(MitmProcessInfo *out_info, Handle *out_hnd) {
|
2019-06-21 07:34:59 +01:00
|
|
|
/* Copy to output. */
|
2019-11-21 12:03:19 +00:00
|
|
|
GetMitmProcessInfo(out_info, this->mitm_waiting_ack_process_id);
|
2019-06-21 07:51:15 +01:00
|
|
|
*out_hnd = this->mitm_fwd_sess_h.Move();
|
2019-06-21 07:34:59 +01:00
|
|
|
this->mitm_waiting_ack = false;
|
2019-10-28 04:43:01 +00:00
|
|
|
this->mitm_waiting_ack_process_id = os::InvalidProcessId;
|
2019-06-21 07:34:59 +01:00
|
|
|
}
|
2019-06-21 02:23:40 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
class AccessControlEntry {
|
|
|
|
private:
|
|
|
|
const u8 *entry;
|
2019-06-21 07:34:59 +01:00
|
|
|
size_t capacity;
|
2019-06-21 02:23:40 +01:00
|
|
|
public:
|
2019-06-21 07:34:59 +01:00
|
|
|
AccessControlEntry(const void *e, size_t c) : entry(reinterpret_cast<const u8 *>(e)), capacity(c) {
|
2019-06-21 02:23:40 +01:00
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
|
|
|
|
AccessControlEntry GetNextEntry() const {
|
2019-06-21 07:34:59 +01:00
|
|
|
return AccessControlEntry(this->entry + this->GetSize(), this->capacity - this->GetSize());
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetSize() const {
|
|
|
|
return this->GetServiceNameSize() + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t GetServiceNameSize() const {
|
|
|
|
return (this->entry[0] & 7) + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ServiceName GetServiceName() const {
|
|
|
|
return ServiceName::Encode(reinterpret_cast<const char *>(this->entry + 1), this->GetServiceNameSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsHost() const {
|
|
|
|
return (this->entry[0] & 0x80) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsWildcard() const {
|
|
|
|
return this->entry[this->GetServiceNameSize()] == '*';
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsValid() const {
|
|
|
|
/* Validate that we can access data. */
|
2019-06-21 07:34:59 +01:00
|
|
|
if (this->entry == nullptr || this->capacity == 0) {
|
2019-06-21 02:23:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Validate that the size is correct. */
|
2019-06-21 07:34:59 +01:00
|
|
|
return this->GetSize() <= this->capacity;
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class InitialProcessIdLimits {
|
|
|
|
private:
|
2019-10-11 10:15:14 +01:00
|
|
|
os::ProcessId min;
|
|
|
|
os::ProcessId max;
|
2019-06-21 02:23:40 +01:00
|
|
|
public:
|
|
|
|
InitialProcessIdLimits() {
|
2019-07-04 06:57:49 +01:00
|
|
|
/* Retrieve process limits. */
|
|
|
|
cfg::GetInitialProcessRange(&this->min, &this->max);
|
2019-06-21 02:23:40 +01:00
|
|
|
|
|
|
|
/* Ensure range is sane. */
|
2020-02-23 07:05:14 +00:00
|
|
|
AMS_ABORT_UNLESS(this->min <= this->max);
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
bool IsInitialProcess(os::ProcessId process_id) const {
|
2020-02-23 07:05:14 +00:00
|
|
|
AMS_ABORT_UNLESS(process_id != os::InvalidProcessId);
|
2019-10-28 04:43:01 +00:00
|
|
|
return this->min <= process_id && process_id <= this->max;
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Static members. */
|
|
|
|
ProcessInfo g_process_list[ProcessCountMax];
|
|
|
|
ServiceInfo g_service_list[ServiceCountMax];
|
2019-07-12 06:23:23 +01:00
|
|
|
ServiceName g_future_mitm_list[FutureMitmCountMax];
|
2019-06-21 02:23:40 +01:00
|
|
|
InitialProcessIdLimits g_initial_process_id_limits;
|
|
|
|
bool g_ended_initial_defers;
|
|
|
|
|
|
|
|
/* Helper functions for interacting with processes/services. */
|
2019-10-28 04:43:01 +00:00
|
|
|
ProcessInfo *GetProcessInfo(os::ProcessId process_id) {
|
2019-06-21 02:23:40 +01:00
|
|
|
for (size_t i = 0; i < ProcessCountMax; i++) {
|
2019-10-28 04:43:01 +00:00
|
|
|
if (g_process_list[i].process_id == process_id) {
|
2019-06-21 02:23:40 +01:00
|
|
|
return &g_process_list[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ProcessInfo *GetFreeProcessInfo() {
|
2019-10-11 10:15:14 +01:00
|
|
|
return GetProcessInfo(os::InvalidProcessId);
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
bool HasProcessInfo(os::ProcessId process_id) {
|
|
|
|
return GetProcessInfo(process_id) != nullptr;
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
bool IsInitialProcess(os::ProcessId process_id) {
|
|
|
|
return g_initial_process_id_limits.IsInitialProcess(process_id);
|
2019-07-12 06:23:23 +01:00
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
constexpr inline bool IsValidProcessId(os::ProcessId process_id) {
|
|
|
|
return process_id != os::InvalidProcessId;
|
2019-07-12 06:23:23 +01:00
|
|
|
}
|
|
|
|
|
2019-06-21 02:23:40 +01:00
|
|
|
ServiceInfo *GetServiceInfo(ServiceName service_name) {
|
|
|
|
for (size_t i = 0; i < ServiceCountMax; i++) {
|
|
|
|
if (g_service_list[i].name == service_name) {
|
|
|
|
return &g_service_list[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ServiceInfo *GetFreeServiceInfo() {
|
|
|
|
return GetServiceInfo(InvalidServiceName);
|
|
|
|
}
|
|
|
|
|
2019-06-21 07:34:59 +01:00
|
|
|
bool HasServiceInfo(ServiceName service) {
|
|
|
|
return GetServiceInfo(service) != nullptr;
|
|
|
|
}
|
|
|
|
|
2019-07-12 06:23:23 +01:00
|
|
|
bool HasMitm(ServiceName service) {
|
|
|
|
const ServiceInfo *service_info = GetServiceInfo(service);
|
2019-10-28 04:43:01 +00:00
|
|
|
return service_info != nullptr && IsValidProcessId(service_info->mitm_process_id);
|
2019-07-12 06:23:23 +01:00
|
|
|
}
|
|
|
|
|
2019-11-21 12:03:19 +00:00
|
|
|
void GetMitmProcessInfo(MitmProcessInfo *out_info, os::ProcessId process_id) {
|
2019-07-12 06:23:23 +01:00
|
|
|
/* Anything that can request a mitm session must have a process info. */
|
2019-10-28 04:43:01 +00:00
|
|
|
const auto process_info = GetProcessInfo(process_id);
|
2020-02-23 07:05:14 +00:00
|
|
|
AMS_ABORT_UNLESS(process_info != nullptr);
|
2019-11-21 12:03:19 +00:00
|
|
|
|
|
|
|
/* Write to output. */
|
|
|
|
out_info->process_id = process_id;
|
|
|
|
out_info->program_id = process_info->program_id;
|
|
|
|
out_info->override_status = process_info->override_status;
|
2019-07-12 06:23:23 +01:00
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
bool IsMitmDisallowed(ncm::ProgramId program_id) {
|
|
|
|
/* Mitm used on certain programs can prevent the boot process from completing. */
|
2019-07-12 06:23:23 +01:00
|
|
|
/* TODO: Is there a way to do this that's less hardcoded? Needs design thought. */
|
2020-03-08 08:06:23 +00:00
|
|
|
return program_id == ncm::SystemProgramId::Loader ||
|
|
|
|
program_id == ncm::SystemProgramId::Pm ||
|
|
|
|
program_id == ncm::SystemProgramId::Spl ||
|
|
|
|
program_id == ncm::SystemProgramId::Boot ||
|
|
|
|
program_id == ncm::SystemProgramId::Ncm ||
|
|
|
|
program_id == ncm::AtmosphereProgramId::Mitm ||
|
|
|
|
program_id == ncm::SystemProgramId::Creport;
|
2019-07-12 06:23:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result AddFutureMitmDeclaration(ServiceName service) {
|
|
|
|
for (size_t i = 0; i < FutureMitmCountMax; i++) {
|
|
|
|
if (g_future_mitm_list[i] == InvalidServiceName) {
|
|
|
|
g_future_mitm_list[i] = service;
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-07-12 06:23:23 +01:00
|
|
|
}
|
|
|
|
}
|
2019-10-24 09:40:44 +01:00
|
|
|
return sm::ResultOutOfServices();
|
2019-07-12 06:23:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool HasFutureMitmDeclaration(ServiceName service) {
|
|
|
|
for (size_t i = 0; i < FutureMitmCountMax; i++) {
|
|
|
|
if (g_future_mitm_list[i] == service) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearFutureMitmDeclaration(ServiceName service) {
|
|
|
|
for (size_t i = 0; i < FutureMitmCountMax; i++) {
|
|
|
|
if (g_future_mitm_list[i] == service) {
|
|
|
|
g_future_mitm_list[i] = InvalidServiceName;
|
|
|
|
}
|
|
|
|
}
|
2020-12-31 08:29:06 +00:00
|
|
|
|
|
|
|
/* This might undefer some requests. */
|
|
|
|
TriggerResume(service);
|
2019-07-12 06:23:23 +01:00
|
|
|
}
|
|
|
|
|
2019-06-21 02:23:40 +01:00
|
|
|
void GetServiceInfoRecord(ServiceRecord *out_record, const ServiceInfo *service_info) {
|
2019-10-28 04:43:01 +00:00
|
|
|
out_record->service = service_info->name;
|
|
|
|
out_record->owner_process_id = service_info->owner_process_id;
|
|
|
|
out_record->max_sessions = service_info->max_sessions;
|
|
|
|
out_record->mitm_process_id = service_info->mitm_process_id;
|
|
|
|
out_record->mitm_waiting_ack_process_id = service_info->mitm_waiting_ack_process_id;
|
|
|
|
out_record->is_light = service_info->is_light;
|
|
|
|
out_record->mitm_waiting_ack = service_info->mitm_waiting_ack;
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result ValidateAccessControl(AccessControlEntry access_control, ServiceName service, bool is_host, bool is_wildcard) {
|
|
|
|
/* Iterate over all entries in the access control, checking to see if we have a match. */
|
|
|
|
while (access_control.IsValid()) {
|
|
|
|
if (access_control.IsHost() == is_host) {
|
2019-10-24 09:40:44 +01:00
|
|
|
bool is_valid = true;
|
|
|
|
|
2019-06-21 02:23:40 +01:00
|
|
|
if (access_control.IsWildcard() == is_wildcard) {
|
|
|
|
/* Check for exact match. */
|
2019-10-24 09:40:44 +01:00
|
|
|
is_valid &= access_control.GetServiceName() == service;
|
2019-06-21 02:23:40 +01:00
|
|
|
} else if (access_control.IsWildcard()) {
|
|
|
|
/* Also allow fuzzy match for wildcard. */
|
|
|
|
ServiceName ac_service = access_control.GetServiceName();
|
2019-10-24 09:40:44 +01:00
|
|
|
is_valid &= std::memcmp(&ac_service, &service, access_control.GetServiceNameSize() - 1) == 0;
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
2019-10-24 09:40:44 +01:00
|
|
|
|
2020-03-08 08:06:23 +00:00
|
|
|
R_SUCCEED_IF(is_valid);
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
access_control = access_control.GetNextEntry();
|
|
|
|
}
|
|
|
|
|
2019-10-24 09:40:44 +01:00
|
|
|
return sm::ResultNotAllowed();
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result ValidateAccessControl(AccessControlEntry restriction, AccessControlEntry access) {
|
|
|
|
/* Ensure that every entry in the access control is allowed by the restriction control. */
|
|
|
|
while (access.IsValid()) {
|
|
|
|
R_TRY(ValidateAccessControl(restriction, access.GetServiceName(), access.IsHost(), access.IsWildcard()));
|
|
|
|
access = access.GetNextEntry();
|
|
|
|
}
|
|
|
|
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result ValidateServiceName(ServiceName service) {
|
|
|
|
/* Service names must be non-empty. */
|
2019-10-24 09:40:44 +01:00
|
|
|
R_UNLESS(service.name[0] != 0, sm::ResultInvalidServiceName());
|
2019-06-21 02:23:40 +01:00
|
|
|
|
|
|
|
/* Get name length. */
|
2019-06-21 07:34:59 +01:00
|
|
|
size_t name_len;
|
|
|
|
for (name_len = 1; name_len < sizeof(service); name_len++) {
|
2019-06-21 02:23:40 +01:00
|
|
|
if (service.name[name_len] == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Names must be all-zero after they end. */
|
|
|
|
while (name_len < sizeof(service)) {
|
2019-10-24 09:40:44 +01:00
|
|
|
R_UNLESS(service.name[name_len++] == 0, sm::ResultInvalidServiceName());
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ShouldDeferForInit(ServiceName service) {
|
|
|
|
/* Once end has been called, we're done. */
|
|
|
|
if (g_ended_initial_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. */
|
2020-12-31 08:29:06 +00:00
|
|
|
return service == InitiallyDeferredServiceName;
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
2019-11-21 12:03:19 +00:00
|
|
|
Result GetMitmServiceHandleImpl(Handle *out, ServiceInfo *service_info, const MitmProcessInfo &client_info) {
|
2019-06-21 02:23:40 +01:00
|
|
|
/* Send command to query if we should mitm. */
|
|
|
|
bool should_mitm;
|
|
|
|
{
|
2019-10-11 10:15:14 +01:00
|
|
|
Service srv { .session = service_info->mitm_query_h.Get() };
|
2019-11-21 12:03:19 +00:00
|
|
|
R_TRY(serviceDispatchInOut(&srv, 65000, client_info, should_mitm));
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If we shouldn't mitm, give normal session. */
|
2019-10-24 09:40:44 +01:00
|
|
|
R_UNLESS(should_mitm, svcConnectToPort(out, service_info->port_h.Get()));
|
2019-06-21 02:23:40 +01:00
|
|
|
|
2019-06-21 07:51:15 +01:00
|
|
|
/* Create both handles. */
|
|
|
|
{
|
2019-09-28 02:04:58 +01:00
|
|
|
os::ManagedHandle fwd_hnd, hnd;
|
2019-06-21 07:51:15 +01:00
|
|
|
R_TRY(svcConnectToPort(fwd_hnd.GetPointer(), service_info->port_h.Get()));
|
|
|
|
R_TRY(svcConnectToPort(hnd.GetPointer(), service_info->mitm_port_h.Get()));
|
|
|
|
service_info->mitm_fwd_sess_h = std::move(fwd_hnd);
|
|
|
|
*out = hnd.Move();
|
|
|
|
}
|
2019-06-21 02:23:40 +01:00
|
|
|
|
2019-11-21 12:03:19 +00:00
|
|
|
service_info->mitm_waiting_ack_process_id = client_info.process_id;
|
2019-06-21 02:23:40 +01:00
|
|
|
service_info->mitm_waiting_ack = true;
|
|
|
|
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
Result GetServiceHandleImpl(Handle *out, ServiceInfo *service_info, os::ProcessId process_id) {
|
2019-06-21 02:23:40 +01:00
|
|
|
/* Clear handle output. */
|
2019-06-21 07:34:59 +01:00
|
|
|
*out = INVALID_HANDLE;
|
2019-06-21 02:23:40 +01:00
|
|
|
|
2019-07-12 06:23:23 +01:00
|
|
|
/* Check if we should return a mitm handle. */
|
2019-10-28 04:43:01 +00:00
|
|
|
if (IsValidProcessId(service_info->mitm_process_id) && service_info->mitm_process_id != process_id) {
|
2019-11-21 12:03:19 +00:00
|
|
|
/* Get mitm process info, ensure that we're allowed to mitm the given program. */
|
|
|
|
MitmProcessInfo client_info;
|
|
|
|
GetMitmProcessInfo(&client_info, process_id);
|
|
|
|
if (!IsMitmDisallowed(client_info.program_id)) {
|
2019-07-12 06:23:23 +01:00
|
|
|
/* We're mitm'd. Assert, because mitm service host dead is an error state. */
|
2020-02-23 07:05:14 +00:00
|
|
|
R_ABORT_UNLESS(GetMitmServiceHandleImpl(out, service_info, client_info));
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-07-12 06:23:23 +01:00
|
|
|
}
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
2019-07-12 06:23:23 +01:00
|
|
|
/* We're not returning a mitm handle, so just return a normal port handle. */
|
|
|
|
return svcConnectToPort(out, service_info->port_h.Get());
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
Result RegisterServiceImpl(Handle *out, os::ProcessId process_id, ServiceName service, size_t max_sessions, bool is_light) {
|
2019-06-21 02:23:40 +01:00
|
|
|
/* Validate service name. */
|
|
|
|
R_TRY(ValidateServiceName(service));
|
|
|
|
|
|
|
|
/* Don't try to register something already registered. */
|
2019-10-24 09:40:44 +01:00
|
|
|
R_UNLESS(!HasServiceInfo(service), sm::ResultAlreadyRegistered());
|
2019-06-21 02:23:40 +01:00
|
|
|
|
|
|
|
/* Get free service. */
|
|
|
|
ServiceInfo *free_service = GetFreeServiceInfo();
|
2019-10-24 09:40:44 +01:00
|
|
|
R_UNLESS(free_service != nullptr, sm::ResultOutOfServices());
|
2019-06-21 02:23:40 +01:00
|
|
|
|
|
|
|
/* Create the new service. */
|
2019-06-21 07:51:15 +01:00
|
|
|
*out = INVALID_HANDLE;
|
2021-02-23 22:21:36 +00:00
|
|
|
Handle server_hnd = INVALID_HANDLE;
|
|
|
|
R_TRY(svcCreatePort(out, std::addressof(server_hnd), max_sessions, is_light, free_service->name.name));
|
2019-06-21 02:23:40 +01:00
|
|
|
|
|
|
|
/* Save info. */
|
|
|
|
free_service->name = service;
|
2019-10-28 04:43:01 +00:00
|
|
|
free_service->owner_process_id = process_id;
|
2019-06-21 02:23:40 +01:00
|
|
|
free_service->max_sessions = max_sessions;
|
|
|
|
free_service->is_light = is_light;
|
2021-02-23 22:21:36 +00:00
|
|
|
*free_service->port_h.GetPointerAndClear() = server_hnd;
|
2019-06-21 02:23:40 +01:00
|
|
|
|
2020-12-31 08:29:06 +00:00
|
|
|
/* This might undefer some requests. */
|
|
|
|
TriggerResume(service);
|
|
|
|
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
2020-09-17 16:24:47 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Client disconnection callback. */
|
|
|
|
void OnClientDisconnected(os::ProcessId process_id) {
|
|
|
|
/* Ensure that the process id is valid. */
|
|
|
|
if (process_id == os::InvalidProcessId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-27 22:36:15 +00:00
|
|
|
/* Unregister all services a client hosts, on attached-client-close. */
|
2020-09-17 16:24:47 +01:00
|
|
|
for (size_t i = 0; i < ServiceCountMax; i++) {
|
|
|
|
if (g_service_list[i].name != InvalidServiceName && g_service_list[i].owner_process_id == process_id) {
|
2021-01-27 22:36:15 +00:00
|
|
|
g_service_list[i].Free();
|
2020-09-17 16:24:47 +01:00
|
|
|
}
|
|
|
|
}
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Process management. */
|
2019-11-21 12:03:19 +00:00
|
|
|
Result RegisterProcess(os::ProcessId process_id, ncm::ProgramId program_id, cfg::OverrideStatus override_status, const void *acid_sac, size_t acid_sac_size, const void *aci_sac, size_t aci_sac_size) {
|
2019-06-21 02:23:40 +01:00
|
|
|
/* Check that access control will fit in the ServiceInfo. */
|
2019-10-24 09:40:44 +01:00
|
|
|
R_UNLESS(aci_sac_size <= AccessControlSizeMax, sm::ResultTooLargeAccessControl());
|
2019-06-21 02:23:40 +01:00
|
|
|
|
|
|
|
/* Get free process. */
|
|
|
|
ProcessInfo *proc = GetFreeProcessInfo();
|
2019-10-24 09:40:44 +01:00
|
|
|
R_UNLESS(proc != nullptr, sm::ResultOutOfProcesses());
|
2019-06-21 02:23:40 +01:00
|
|
|
|
|
|
|
/* Validate restrictions. */
|
2019-10-24 09:40:44 +01:00
|
|
|
R_UNLESS(aci_sac_size != 0, sm::ResultNotAllowed());
|
2019-07-04 06:57:49 +01:00
|
|
|
R_TRY(ValidateAccessControl(AccessControlEntry(acid_sac, acid_sac_size), AccessControlEntry(aci_sac, aci_sac_size)));
|
2019-06-21 02:23:40 +01:00
|
|
|
|
|
|
|
/* Save info. */
|
2019-10-28 04:43:01 +00:00
|
|
|
proc->process_id = process_id;
|
|
|
|
proc->program_id = program_id;
|
2019-11-21 12:03:19 +00:00
|
|
|
proc->override_status = override_status;
|
2019-07-04 06:57:49 +01:00
|
|
|
proc->access_control_size = aci_sac_size;
|
|
|
|
std::memcpy(proc->access_control, aci_sac, proc->access_control_size);
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
Result UnregisterProcess(os::ProcessId process_id) {
|
2019-06-21 02:23:40 +01:00
|
|
|
/* Find the process. */
|
2019-10-28 04:43:01 +00:00
|
|
|
ProcessInfo *proc = GetProcessInfo(process_id);
|
2019-10-24 09:40:44 +01:00
|
|
|
R_UNLESS(proc != nullptr, sm::ResultInvalidClient());
|
2019-06-21 02:23:40 +01:00
|
|
|
|
2019-06-21 07:34:59 +01:00
|
|
|
proc->Free();
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Service management. */
|
|
|
|
Result HasService(bool *out, ServiceName service) {
|
2019-06-25 01:57:49 +01:00
|
|
|
/* Validate service name. */
|
|
|
|
R_TRY(ValidateServiceName(service));
|
|
|
|
|
2019-06-21 02:23:40 +01:00
|
|
|
*out = HasServiceInfo(service);
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
2019-07-03 06:21:47 +01:00
|
|
|
Result WaitService(ServiceName service) {
|
|
|
|
bool has_service = false;
|
|
|
|
R_TRY(impl::HasService(&has_service, service));
|
|
|
|
|
|
|
|
/* Wait until we have the service. */
|
2020-12-31 08:29:06 +00:00
|
|
|
R_SUCCEED_IF(has_service);
|
|
|
|
|
|
|
|
return StartRegisterRetry(service);
|
2019-07-03 06:21:47 +01:00
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
Result GetServiceHandle(Handle *out, os::ProcessId process_id, ServiceName service) {
|
2019-06-21 02:23:40 +01:00
|
|
|
/* Validate service name. */
|
|
|
|
R_TRY(ValidateServiceName(service));
|
|
|
|
|
|
|
|
/* In 8.0.0, Nintendo removed the service apm:p -- however, all homebrew attempts to get */
|
|
|
|
/* a handle to this when calling appletInitialize(). Because hbl has access to all services, */
|
|
|
|
/* This would return true, and homebrew would *wait forever* trying to get a handle to a service */
|
|
|
|
/* that will never register. Thus, in the interest of not breaking every single piece of homebrew */
|
|
|
|
/* we will provide a little first class help. */
|
2019-10-24 09:40:44 +01:00
|
|
|
constexpr ServiceName ApmP = ServiceName::Encode("apm:p");
|
2020-04-14 06:19:44 +01:00
|
|
|
R_UNLESS((hos::GetVersion() < hos::Version_8_0_0) || (service != ApmP), sm::ResultNotAllowed());
|
2019-06-21 02:23:40 +01:00
|
|
|
|
2019-06-21 07:51:15 +01:00
|
|
|
/* Check that the process is registered and allowed to get the service. */
|
2019-10-28 04:43:01 +00:00
|
|
|
if (!IsInitialProcess(process_id)) {
|
|
|
|
ProcessInfo *proc = GetProcessInfo(process_id);
|
2019-10-24 09:40:44 +01:00
|
|
|
R_UNLESS(proc != nullptr, sm::ResultInvalidClient());
|
2019-06-21 02:23:40 +01:00
|
|
|
R_TRY(ValidateAccessControl(AccessControlEntry(proc->access_control, proc->access_control_size), service, false, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get service info. Check to see if we need to defer this until later. */
|
|
|
|
ServiceInfo *service_info = GetServiceInfo(service);
|
2020-12-31 08:29:06 +00:00
|
|
|
if (service_info == nullptr || ShouldDeferForInit(service) || HasFutureMitmDeclaration(service) || service_info->mitm_waiting_ack) {
|
|
|
|
return StartRegisterRetry(service);
|
|
|
|
}
|
2019-06-21 02:23:40 +01:00
|
|
|
|
|
|
|
/* Get a handle from the service info. */
|
2019-10-28 04:43:01 +00:00
|
|
|
R_TRY_CATCH(GetServiceHandleImpl(out, service_info, process_id)) {
|
2019-10-24 09:40:44 +01:00
|
|
|
R_CONVERT(svc::ResultOutOfSessions, sm::ResultOutOfSessions())
|
2019-06-21 02:23:40 +01:00
|
|
|
} R_END_TRY_CATCH;
|
|
|
|
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
Result RegisterService(Handle *out, os::ProcessId process_id, ServiceName service, size_t max_sessions, bool is_light) {
|
2019-06-21 02:23:40 +01:00
|
|
|
/* Validate service name. */
|
|
|
|
R_TRY(ValidateServiceName(service));
|
|
|
|
|
|
|
|
/* Check that the process is registered and allowed to register the service. */
|
2019-10-28 04:43:01 +00:00
|
|
|
if (!IsInitialProcess(process_id)) {
|
|
|
|
ProcessInfo *proc = GetProcessInfo(process_id);
|
2019-10-24 09:40:44 +01:00
|
|
|
R_UNLESS(proc != nullptr, sm::ResultInvalidClient());
|
2019-06-21 02:23:40 +01:00
|
|
|
|
|
|
|
R_TRY(ValidateAccessControl(AccessControlEntry(proc->access_control, proc->access_control_size), service, true, false));
|
|
|
|
}
|
|
|
|
|
2019-10-24 09:40:44 +01:00
|
|
|
R_UNLESS(!HasServiceInfo(service), sm::ResultAlreadyRegistered());
|
2019-10-28 04:43:01 +00:00
|
|
|
return RegisterServiceImpl(out, process_id, service, max_sessions, is_light);
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result RegisterServiceForSelf(Handle *out, ServiceName service, size_t max_sessions) {
|
2019-10-11 10:15:14 +01:00
|
|
|
return RegisterServiceImpl(out, os::GetCurrentProcessId(), service, max_sessions, false);
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
Result UnregisterService(os::ProcessId process_id, ServiceName service) {
|
2019-06-21 02:23:40 +01:00
|
|
|
/* Validate service name. */
|
|
|
|
R_TRY(ValidateServiceName(service));
|
|
|
|
|
|
|
|
/* Check that the process is registered. */
|
2019-10-28 04:43:01 +00:00
|
|
|
if (!IsInitialProcess(process_id)) {
|
|
|
|
R_UNLESS(HasProcessInfo(process_id), sm::ResultInvalidClient());
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure that the service is actually registered. */
|
|
|
|
ServiceInfo *service_info = GetServiceInfo(service);
|
2019-10-24 09:40:44 +01:00
|
|
|
R_UNLESS(service_info != nullptr, sm::ResultNotRegistered());
|
2019-06-21 02:23:40 +01:00
|
|
|
|
|
|
|
/* Check if we have permission to do this. */
|
2019-10-28 04:43:01 +00:00
|
|
|
R_UNLESS(service_info->owner_process_id == process_id, sm::ResultNotAllowed());
|
2019-06-21 02:23:40 +01:00
|
|
|
|
|
|
|
/* Unregister the service. */
|
2019-06-21 07:34:59 +01:00
|
|
|
service_info->Free();
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Mitm extensions. */
|
|
|
|
Result HasMitm(bool *out, ServiceName service) {
|
|
|
|
/* Validate service name. */
|
|
|
|
R_TRY(ValidateServiceName(service));
|
|
|
|
|
|
|
|
const ServiceInfo *service_info = GetServiceInfo(service);
|
2019-10-28 04:43:01 +00:00
|
|
|
*out = service_info != nullptr && IsValidProcessId(service_info->mitm_process_id);
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
2019-07-03 06:21:47 +01:00
|
|
|
Result WaitMitm(ServiceName service) {
|
|
|
|
bool has_mitm = false;
|
|
|
|
R_TRY(impl::HasMitm(&has_mitm, service));
|
|
|
|
|
|
|
|
/* Wait until we have the mitm. */
|
2020-12-31 08:29:06 +00:00
|
|
|
R_SUCCEED_IF(has_mitm);
|
|
|
|
|
|
|
|
return StartRegisterRetry(service);
|
2019-07-03 06:21:47 +01:00
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
Result InstallMitm(Handle *out, Handle *out_query, os::ProcessId process_id, ServiceName service) {
|
2019-06-21 02:23:40 +01:00
|
|
|
/* Validate service name. */
|
|
|
|
R_TRY(ValidateServiceName(service));
|
|
|
|
|
|
|
|
/* Check that the process is registered and allowed to register the service. */
|
2019-10-28 04:43:01 +00:00
|
|
|
if (!IsInitialProcess(process_id)) {
|
|
|
|
ProcessInfo *proc = GetProcessInfo(process_id);
|
2019-10-24 09:40:44 +01:00
|
|
|
R_UNLESS(proc != nullptr, sm::ResultInvalidClient());
|
2019-06-21 02:23:40 +01:00
|
|
|
R_TRY(ValidateAccessControl(AccessControlEntry(proc->access_control, proc->access_control_size), service, true, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Validate that the service exists. */
|
|
|
|
ServiceInfo *service_info = GetServiceInfo(service);
|
2019-10-24 09:40:44 +01:00
|
|
|
|
|
|
|
/* If it doesn't exist, defer until it does. */
|
2020-12-31 08:29:06 +00:00
|
|
|
if (service_info == nullptr) {
|
|
|
|
return StartRegisterRetry(service);
|
|
|
|
}
|
2019-06-21 02:23:40 +01:00
|
|
|
|
|
|
|
/* Validate that the service isn't already being mitm'd. */
|
2019-10-28 04:43:01 +00:00
|
|
|
R_UNLESS(!IsValidProcessId(service_info->mitm_process_id), sm::ResultAlreadyRegistered());
|
2019-06-21 02:23:40 +01:00
|
|
|
|
|
|
|
/* Always clear output. */
|
2019-06-21 07:34:59 +01:00
|
|
|
*out = INVALID_HANDLE;
|
|
|
|
*out_query = INVALID_HANDLE;
|
2019-06-21 02:23:40 +01:00
|
|
|
|
2020-09-07 18:40:43 +01:00
|
|
|
/* If we don't have a future mitm declaration, add one. */
|
|
|
|
/* Client will clear this when ready to process. */
|
|
|
|
bool has_existing_future_declaration = HasFutureMitmDeclaration(service);
|
|
|
|
if (!has_existing_future_declaration) {
|
|
|
|
R_TRY(AddFutureMitmDeclaration(service));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto future_guard = SCOPE_GUARD { if (!has_existing_future_declaration) { ClearFutureMitmDeclaration(service); } };
|
|
|
|
|
2019-06-21 02:23:40 +01:00
|
|
|
/* Create mitm handles. */
|
2019-06-21 07:51:15 +01:00
|
|
|
{
|
2019-09-28 02:04:58 +01:00
|
|
|
os::ManagedHandle hnd, port_hnd, qry_hnd, mitm_qry_hnd;
|
2020-12-10 12:06:02 +00:00
|
|
|
R_TRY(svcCreatePort(hnd.GetPointer(), port_hnd.GetPointer(), service_info->max_sessions, service_info->is_light, service_info->name.name));
|
2019-06-21 07:51:15 +01:00
|
|
|
R_TRY(svcCreateSession(qry_hnd.GetPointer(), mitm_qry_hnd.GetPointer(), 0, 0));
|
|
|
|
|
|
|
|
/* Copy to output. */
|
2019-10-28 04:43:01 +00:00
|
|
|
service_info->mitm_process_id = process_id;
|
2019-06-21 07:51:15 +01:00
|
|
|
service_info->mitm_port_h = std::move(port_hnd);
|
|
|
|
service_info->mitm_query_h = std::move(mitm_qry_hnd);
|
|
|
|
*out = hnd.Move();
|
|
|
|
*out_query = qry_hnd.Move();
|
2020-12-31 08:29:06 +00:00
|
|
|
|
|
|
|
/* This might undefer some requests. */
|
|
|
|
TriggerResume(service);
|
2019-06-21 07:51:15 +01:00
|
|
|
}
|
2019-06-21 02:23:40 +01:00
|
|
|
|
2020-09-07 18:40:43 +01:00
|
|
|
future_guard.Cancel();
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
Result UninstallMitm(os::ProcessId process_id, ServiceName service) {
|
2019-06-21 02:23:40 +01:00
|
|
|
/* Validate service name. */
|
|
|
|
R_TRY(ValidateServiceName(service));
|
|
|
|
|
|
|
|
/* Check that the process is registered. */
|
2019-10-28 04:43:01 +00:00
|
|
|
if (!IsInitialProcess(process_id)) {
|
|
|
|
ProcessInfo *proc = GetProcessInfo(process_id);
|
2019-10-24 09:40:44 +01:00
|
|
|
R_UNLESS(proc != nullptr, sm::ResultInvalidClient());
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Validate that the service exists. */
|
|
|
|
ServiceInfo *service_info = GetServiceInfo(service);
|
2019-10-24 09:40:44 +01:00
|
|
|
R_UNLESS(service_info != nullptr, sm::ResultNotRegistered());
|
2019-06-21 02:23:40 +01:00
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
/* Validate that the client process_id is the mitm process. */
|
|
|
|
R_UNLESS(service_info->mitm_process_id == process_id, sm::ResultNotAllowed());
|
2019-06-21 02:23:40 +01:00
|
|
|
|
|
|
|
/* Free Mitm session info. */
|
2019-06-21 07:34:59 +01:00
|
|
|
service_info->FreeMitm();
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
Result DeclareFutureMitm(os::ProcessId process_id, ServiceName service) {
|
2019-07-12 06:23:23 +01:00
|
|
|
/* Validate service name. */
|
|
|
|
R_TRY(ValidateServiceName(service));
|
|
|
|
|
|
|
|
/* Check that the process is registered and allowed to register the service. */
|
2019-10-28 04:43:01 +00:00
|
|
|
if (!IsInitialProcess(process_id)) {
|
|
|
|
ProcessInfo *proc = GetProcessInfo(process_id);
|
2019-10-24 09:40:44 +01:00
|
|
|
R_UNLESS(proc != nullptr, sm::ResultInvalidClient());
|
2019-07-12 06:23:23 +01:00
|
|
|
R_TRY(ValidateAccessControl(AccessControlEntry(proc->access_control, proc->access_control_size), service, true, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that mitm hasn't already been registered or declared. */
|
2019-10-24 09:40:44 +01:00
|
|
|
R_UNLESS(!HasMitm(service), sm::ResultAlreadyRegistered());
|
|
|
|
R_UNLESS(!HasFutureMitmDeclaration(service), sm::ResultAlreadyRegistered());
|
2019-07-12 06:23:23 +01:00
|
|
|
|
|
|
|
/* Try to forward declare it. */
|
|
|
|
R_TRY(AddFutureMitmDeclaration(service));
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-07-12 06:23:23 +01:00
|
|
|
}
|
|
|
|
|
2020-09-07 18:40:43 +01:00
|
|
|
Result ClearFutureMitm(os::ProcessId process_id, ServiceName service) {
|
|
|
|
/* Validate service name. */
|
|
|
|
R_TRY(ValidateServiceName(service));
|
|
|
|
|
|
|
|
/* Check that the process is registered and allowed to register the service. */
|
|
|
|
if (!IsInitialProcess(process_id)) {
|
|
|
|
ProcessInfo *proc = GetProcessInfo(process_id);
|
|
|
|
R_UNLESS(proc != nullptr, sm::ResultInvalidClient());
|
|
|
|
R_TRY(ValidateAccessControl(AccessControlEntry(proc->access_control, proc->access_control_size), service, true, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that a future mitm declaration is present or we have a mitm. */
|
|
|
|
if (HasMitm(service)) {
|
|
|
|
/* Validate that the service exists. */
|
|
|
|
ServiceInfo *service_info = GetServiceInfo(service);
|
|
|
|
R_UNLESS(service_info != nullptr, sm::ResultNotRegistered());
|
|
|
|
|
|
|
|
/* Validate that the client process_id is the mitm process. */
|
|
|
|
R_UNLESS(service_info->mitm_process_id == process_id, sm::ResultNotAllowed());
|
|
|
|
} else {
|
|
|
|
R_UNLESS(HasFutureMitmDeclaration(service), sm::ResultNotRegistered());
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Clear the forward declaration. */
|
|
|
|
ClearFutureMitmDeclaration(service);
|
|
|
|
return ResultSuccess();
|
|
|
|
}
|
|
|
|
|
2019-11-21 12:03:19 +00:00
|
|
|
Result AcknowledgeMitmSession(MitmProcessInfo *out_info, Handle *out_hnd, os::ProcessId process_id, ServiceName service) {
|
2019-06-21 02:23:40 +01:00
|
|
|
/* Validate service name. */
|
|
|
|
R_TRY(ValidateServiceName(service));
|
|
|
|
|
|
|
|
/* Check that the process is registered. */
|
2019-10-28 04:43:01 +00:00
|
|
|
if (!IsInitialProcess(process_id)) {
|
|
|
|
ProcessInfo *proc = GetProcessInfo(process_id);
|
2019-10-24 09:40:44 +01:00
|
|
|
R_UNLESS(proc != nullptr, sm::ResultInvalidClient());
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Validate that the service exists. */
|
|
|
|
ServiceInfo *service_info = GetServiceInfo(service);
|
2019-10-24 09:40:44 +01:00
|
|
|
R_UNLESS(service_info != nullptr, sm::ResultNotRegistered());
|
2019-06-21 02:23:40 +01:00
|
|
|
|
2019-10-28 04:43:01 +00:00
|
|
|
/* Validate that the client process_id is the mitm process, and that an acknowledgement is waiting. */
|
|
|
|
R_UNLESS(service_info->mitm_process_id == process_id, sm::ResultNotAllowed());
|
|
|
|
R_UNLESS(service_info->mitm_waiting_ack, sm::ResultNotAllowed());
|
2019-06-21 02:23:40 +01:00
|
|
|
|
2019-06-21 07:34:59 +01:00
|
|
|
/* Acknowledge. */
|
2019-11-21 12:03:19 +00:00
|
|
|
service_info->AcknowledgeMitmSession(out_info, out_hnd);
|
2020-12-31 08:29:06 +00:00
|
|
|
|
|
|
|
/* Undefer requests to the session. */
|
|
|
|
TriggerResume(service);
|
|
|
|
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Dmnt record extensions. */
|
|
|
|
Result GetServiceRecord(ServiceRecord *out, ServiceName service) {
|
|
|
|
/* Validate service name. */
|
|
|
|
R_TRY(ValidateServiceName(service));
|
|
|
|
|
|
|
|
/* Validate that the service exists. */
|
|
|
|
const ServiceInfo *service_info = GetServiceInfo(service);
|
2019-10-24 09:40:44 +01:00
|
|
|
R_UNLESS(service_info != nullptr, sm::ResultNotRegistered());
|
2019-06-21 02:23:40 +01:00
|
|
|
|
|
|
|
GetServiceInfoRecord(out, service_info);
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Result ListServiceRecords(ServiceRecord *out, u64 *out_count, u64 offset, u64 max_count) {
|
|
|
|
u64 count = 0;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < ServiceCountMax && count < max_count; i++) {
|
|
|
|
const ServiceInfo *service_info = &g_service_list[i];
|
|
|
|
if (service_info->name != InvalidServiceName) {
|
|
|
|
if (offset == 0) {
|
|
|
|
GetServiceInfoRecord(out++, service_info);
|
|
|
|
count++;
|
|
|
|
} else {
|
|
|
|
offset--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_count = 0;
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Deferral extension (works around FS bug). */
|
|
|
|
Result EndInitialDefers() {
|
|
|
|
g_ended_initial_defers = true;
|
2020-12-31 08:29:06 +00:00
|
|
|
|
|
|
|
/* This might undefer some requests. */
|
|
|
|
TriggerResume(InitiallyDeferredServiceName);
|
|
|
|
|
2019-10-24 09:40:44 +01:00
|
|
|
return ResultSuccess();
|
2019-06-21 02:23:40 +01:00
|
|
|
}
|
|
|
|
|
2019-06-21 02:32:00 +01:00
|
|
|
}
|