From b6684ff84519bb95c34f68ccaf68134964547a18 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Sun, 20 Jan 2019 15:59:09 -0800 Subject: [PATCH] sm: Add sm:dmnt query extension --- docs/modules/sm.md | 56 +++++++++++++++++++ stratosphere/sm/source/sm_dmnt_service.cpp | 33 +++++++++++ stratosphere/sm/source/sm_dmnt_service.hpp | 40 +++++++++++++ stratosphere/sm/source/sm_main.cpp | 12 ++++ stratosphere/sm/source/sm_manager_service.hpp | 1 - stratosphere/sm/source/sm_registration.cpp | 48 ++++++++++++++++ stratosphere/sm/source/sm_registration.hpp | 6 ++ stratosphere/sm/source/sm_types.hpp | 15 ++++- 8 files changed, 209 insertions(+), 2 deletions(-) create mode 100644 stratosphere/sm/source/sm_dmnt_service.cpp create mode 100644 stratosphere/sm/source/sm_dmnt_service.hpp diff --git a/docs/modules/sm.md b/docs/modules/sm.md index 8852975f8..4d04fe3cf 100644 --- a/docs/modules/sm.md +++ b/docs/modules/sm.md @@ -20,6 +20,18 @@ interface nn::sm::detail::IUserInterface is sm: { } ``` +Additionally, an interface `sm:dmnt` has been created to allow a debug monitor to query sm's state. + +Its SwIPC definition follows. +``` +interface nn::sm::detail::IDebugMonitorInterface is sm:dmnt { + [65000] AtmosphereGetServiceRecord(ServiceName name) -> SmServiceRecord; + [65001] AtmosphereListServiceRecords(u64 offset) -> buffer, u64 count; + [65002] AtmosphereGetServiceRecordSize() -> u64 record_size; +} +``` + + #### AtmosphereInstallMitm This command alters the registration for the named service, in order to allow services to intercept communication between client processes and their intended services. It is used by [fs_mitm](fs_mitm.md). @@ -58,6 +70,50 @@ This command is used internally by the Stratosphere implementation of the [loade If the given process ID refers to a kernel internal process, error code 0x1015 is returned. This command requires that the session be initialized, returning error code 0x415 if it is not. +#### AtmosphereGetServiceRecordSize + +Retrieves `sizeof(SmServiceRecord)` for a service. The current format of `SmServiceRecord` structure follows. + +``` +struct SmServiceRecord { + uint64_t service_name; + uint64_t owner_pid; + uint64_t max_sessions; + uint64_t mitm_pid; + uint64_t mitm_waiting_ack_pid; + bool is_light; + bool mitm_waiting_ack; +}; +``` + +#### AtmosphereGetServiceRecord + +Retrieves a service registration record for a service. + +#### AtmosphereListServiceRecords + +Provides a list of service registrations records. + +The command will return an array of `SmServiceRecord`s, skipping `offset` records. The number of records returned is indicated by `count`. +If `count` is less than the size of the buffer divided by `sizeof(SmServiceRecord)` (the buffer was not completely filled), the end of the service registration list has been reached. Otherwise, client code +should increment `offset` by `count` and call again. Client code should retrieve a record size using `AtmosphereGetServiceRecordSize`, and either make sure that the size of a record matches what it expects, +or should make sure to use the correct size as the stride while iterating over the array of returned records. Example pseudocode is shown below. + +``` +offset = 0; +record_size = AtmosphereGetServiceRecordSize(); +do { + SmServiceRecord records[16]; + count = AtmosphereListServiceRecords(offset, buffer(records)); + for (i = 0; i < count; i++) { + SmServiceRecord record = {0}; + memcpy(&record, &records[i], min(record_size, sizeof(SmServiceRecord)); + /* process record */ + offset++; + } +} while(count == sizeof(records) / record_size); +``` + ### Minimum Session Limit When a service is registered, the sysmodule registering it must specify a limit on the number of sessions that are allowed to be active for that service at a time. This is used to ensure that services like `fs-pr`, `fs-ldr`, and `ldr:pm` can only be connected to once, adding an additional layer of safety over the regular service verification to ensure that those services are only connected to by the highly priveleged process they are intended to be used by. diff --git a/stratosphere/sm/source/sm_dmnt_service.cpp b/stratosphere/sm/source/sm_dmnt_service.cpp new file mode 100644 index 000000000..5428ab7a7 --- /dev/null +++ b/stratosphere/sm/source/sm_dmnt_service.cpp @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2018 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include "sm_dmnt_service.hpp" +#include "sm_registration.hpp" + +Result DmntService::AtmosphereGetRecord(Out record, SmServiceName service) { + return Registration::GetServiceRecord(smEncodeName(service.name), record.GetPointer()); +} + +void DmntService::AtmosphereListRecords(OutBuffer records, Out out_count, u64 offset) { + Registration::ListServiceRecords(offset, records.num_elements, records.buffer, out_count.GetPointer()); +} + +void DmntService::AtmosphereGetRecordSize(Out record_size) { + record_size.SetValue(sizeof(SmServiceRecord)); +} + diff --git a/stratosphere/sm/source/sm_dmnt_service.hpp b/stratosphere/sm/source/sm_dmnt_service.hpp new file mode 100644 index 000000000..66a71d728 --- /dev/null +++ b/stratosphere/sm/source/sm_dmnt_service.hpp @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2018 Atmosphère-NX + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#pragma once +#include +#include +#include "sm_types.hpp" + +enum DmntServiceCmd { + Dmnt_Cmd_AtmosphereGetRecord = 65000, + Dmnt_Cmd_AtmosphereListRecords = 65001, + Dmnt_Cmd_AtmosphereGetRecordSize = 65002, +}; + +class DmntService final : public IServiceObject { + private: + /* Actual commands. */ + virtual Result AtmosphereGetRecord(Out record, SmServiceName service); + virtual void AtmosphereListRecords(OutBuffer records, Out out_count, u64 offset); + virtual void AtmosphereGetRecordSize(Out record_size); + public: + DEFINE_SERVICE_DISPATCH_TABLE { + MakeServiceCommandMeta(), + MakeServiceCommandMeta(), + MakeServiceCommandMeta(), + }; +}; diff --git a/stratosphere/sm/source/sm_main.cpp b/stratosphere/sm/source/sm_main.cpp index 60ceb3c45..f97c34283 100644 --- a/stratosphere/sm/source/sm_main.cpp +++ b/stratosphere/sm/source/sm_main.cpp @@ -24,6 +24,7 @@ #include "sm_manager_service.hpp" #include "sm_user_service.hpp" +#include "sm_dmnt_service.hpp" #include "sm_registration.hpp" extern "C" { @@ -82,6 +83,17 @@ int main(int argc, char **argv) } server_manager->AddWaitable(new ExistingPortServer(smm_h, 1)); + + /*===== ATMOSPHERE EXTENSION =====*/ + /* Create sm:dmnt manually. */ + Handle smdmnt_h; + if (R_FAILED(Registration::RegisterServiceForSelf(smEncodeName("sm:dmnt"), 1, false, &smdmnt_h))) { + /* TODO: Panic. */ + while (1) { } + } + + server_manager->AddWaitable(new ExistingPortServer(smm_h, 1));; + /*================================*/ /* Loop forever, servicing our services. */ server_manager->Process(); diff --git a/stratosphere/sm/source/sm_manager_service.hpp b/stratosphere/sm/source/sm_manager_service.hpp index 454be7edb..bb71e58b6 100644 --- a/stratosphere/sm/source/sm_manager_service.hpp +++ b/stratosphere/sm/source/sm_manager_service.hpp @@ -23,7 +23,6 @@ enum ManagerServiceCmd { Manager_Cmd_RegisterProcess = 0, Manager_Cmd_UnregisterProcess = 1, - Manager_Cmd_AtmosphereEndInitDefers = 65000, Manager_Cmd_AtmosphereHasMitm = 65001, }; diff --git a/stratosphere/sm/source/sm_registration.cpp b/stratosphere/sm/source/sm_registration.cpp index 3401f800f..c9957e315 100644 --- a/stratosphere/sm/source/sm_registration.cpp +++ b/stratosphere/sm/source/sm_registration.cpp @@ -562,3 +562,51 @@ Result Registration::AssociatePidTidForMitm(u64 pid, u64 tid) { } return 0x0; } + +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) { + return 0xC15; + } + + 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))) { + return 0xC15; + } + + Registration::Service *target_service = GetService(service); + if (target_service == NULL) { + return 0xE15; + } + + ConvertServiceToRecord(target_service, out); + return 0x0; +} + +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; +} diff --git a/stratosphere/sm/source/sm_registration.hpp b/stratosphere/sm/source/sm_registration.hpp index 8c4d72660..7d7ef5961 100644 --- a/stratosphere/sm/source/sm_registration.hpp +++ b/stratosphere/sm/source/sm_registration.hpp @@ -17,6 +17,8 @@ #pragma once #include +#include "sm_types.hpp" + #define REGISTRATION_LIST_MAX_PROCESS (0x40) #define REGISTRATION_LIST_MAX_SERVICE (0x100) #define REGISTRATION_MAX_SAC_SIZE (0x200) @@ -81,4 +83,8 @@ class Registration { static Result UninstallMitmForPid(u64 pid, u64 service); static Result AcknowledgeMitmSessionForPid(u64 pid, u64 service, Handle *out, u64 *out_pid); static Result AssociatePidTidForMitm(u64 pid, u64 tid); + + static void ConvertServiceToRecord(Registration::Service *service, SmServiceRecord *record); + static Result GetServiceRecord(u64 service, SmServiceRecord *out); + static void ListServiceRecords(u64 offset, u64 max_out, SmServiceRecord *out, u64 *out_count); }; diff --git a/stratosphere/sm/source/sm_types.hpp b/stratosphere/sm/source/sm_types.hpp index 6ac9bce92..a8e0453a1 100644 --- a/stratosphere/sm/source/sm_types.hpp +++ b/stratosphere/sm/source/sm_types.hpp @@ -19,4 +19,17 @@ struct SmServiceName { char name[sizeof(u64)]; }; -static_assert(__alignof__(SmServiceName) == 1, "SmServiceName definition!"); \ No newline at end of file +static_assert(__alignof__(SmServiceName) == 1, "SmServiceName definition!"); + +/* For Debug Monitor extensions. */ +struct SmServiceRecord { + u64 service_name; + u64 owner_pid; + u64 max_sessions; + u64 mitm_pid; + u64 mitm_waiting_ack_pid; + bool is_light; + bool mitm_waiting_ack; +}; + +static_assert(sizeof(SmServiceRecord) == 0x30, "SmServiceRecord definition!"); \ No newline at end of file