2018-09-07 16:00:13 +01:00
|
|
|
/*
|
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2018-06-10 02:33:22 +01:00
|
|
|
#pragma once
|
|
|
|
#include <switch.h>
|
|
|
|
#include <stratosphere.hpp>
|
2018-06-10 08:17:00 +01:00
|
|
|
#include "imitmserviceobject.hpp"
|
2018-06-10 02:33:22 +01:00
|
|
|
|
2018-06-15 00:50:01 +01:00
|
|
|
#include "mitm_query_service.hpp"
|
2018-06-10 02:33:22 +01:00
|
|
|
#include "mitm_server.hpp"
|
2018-06-15 00:50:01 +01:00
|
|
|
#include "fsmitm_worker.hpp"
|
2018-06-10 02:33:22 +01:00
|
|
|
|
|
|
|
#include "debug.hpp"
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class MitMServer;
|
|
|
|
|
|
|
|
template <typename T>
|
2018-06-12 23:00:09 +01:00
|
|
|
class MitMSession final : public ISession<T> {
|
2018-06-10 08:17:00 +01:00
|
|
|
static_assert(std::is_base_of<IMitMServiceObject, T>::value, "MitM Service Objects must derive from IMitMServiceObject");
|
2018-06-10 02:33:22 +01:00
|
|
|
|
|
|
|
/* This will be for the actual session. */
|
|
|
|
Service forward_service;
|
2018-06-12 23:00:09 +01:00
|
|
|
IpcParsedCommand cur_out_r;
|
2018-06-19 19:07:31 +01:00
|
|
|
u32 mitm_domain_id = 0;
|
2018-06-15 00:50:01 +01:00
|
|
|
bool got_first_message;
|
2018-06-12 23:00:09 +01:00
|
|
|
|
2018-06-10 02:33:22 +01:00
|
|
|
public:
|
2018-06-19 19:07:31 +01:00
|
|
|
MitMSession<T>(MitMServer<T> *s, Handle s_h, Handle c_h, const char *srv) : ISession<T>(s, s_h, c_h, NULL, 0), got_first_message(false) {
|
2018-06-12 23:00:09 +01:00
|
|
|
this->server = s;
|
|
|
|
this->server_handle = s_h;
|
|
|
|
this->client_handle = c_h;
|
2018-06-10 02:33:22 +01:00
|
|
|
if (R_FAILED(smMitMGetService(&forward_service, srv))) {
|
|
|
|
/* TODO: Panic. */
|
|
|
|
}
|
2018-06-19 19:07:31 +01:00
|
|
|
size_t pointer_buffer_size = 0;
|
|
|
|
if (R_FAILED(ipcQueryPointerBufferSize(forward_service.handle, &pointer_buffer_size))) {
|
2018-06-10 02:33:22 +01:00
|
|
|
/* TODO: Panic. */
|
|
|
|
}
|
2018-06-15 00:50:01 +01:00
|
|
|
this->service_object = std::make_shared<T>(&forward_service);
|
2018-06-19 19:07:31 +01:00
|
|
|
this->pointer_buffer.resize(pointer_buffer_size);
|
2018-06-10 02:33:22 +01:00
|
|
|
}
|
2018-06-19 19:07:31 +01:00
|
|
|
MitMSession<T>(MitMServer<T> *s, Handle s_h, Handle c_h, Handle f_h) : ISession<T>(s, s_h, c_h, NULL, 0), got_first_message(true) {
|
2018-06-12 23:00:09 +01:00
|
|
|
this->server = s;
|
|
|
|
this->server_handle = s_h;
|
|
|
|
this->client_handle = c_h;
|
2018-06-15 00:50:01 +01:00
|
|
|
serviceCreate(&this->forward_service, f_h);
|
2018-06-19 19:07:31 +01:00
|
|
|
size_t pointer_buffer_size = 0;
|
|
|
|
if (R_FAILED(ipcQueryPointerBufferSize(forward_service.handle, &pointer_buffer_size))) {
|
2018-06-15 00:50:01 +01:00
|
|
|
/* TODO: Panic. */
|
|
|
|
}
|
|
|
|
this->service_object = std::make_shared<T>(&forward_service);
|
2018-06-19 19:07:31 +01:00
|
|
|
this->pointer_buffer.resize(pointer_buffer_size);
|
2018-06-10 02:33:22 +01:00
|
|
|
}
|
|
|
|
|
2018-06-12 23:00:09 +01:00
|
|
|
virtual ~MitMSession() {
|
|
|
|
serviceClose(&forward_service);
|
2018-06-10 02:33:22 +01:00
|
|
|
}
|
|
|
|
|
2018-06-12 23:00:09 +01:00
|
|
|
Result handle_message(IpcParsedCommand &r) override {
|
|
|
|
IpcCommand c;
|
|
|
|
ipcInitialize(&c);
|
|
|
|
u64 cmd_id = ((u32 *)r.Raw)[2];
|
|
|
|
Result retval = 0xF601;
|
|
|
|
|
|
|
|
cur_out_r.NumHandles = 0;
|
2018-06-10 02:33:22 +01:00
|
|
|
|
2018-06-12 23:00:09 +01:00
|
|
|
Log(armGetTls(), 0x100);
|
2018-06-15 00:50:01 +01:00
|
|
|
|
2018-06-12 23:00:09 +01:00
|
|
|
u32 *cmdbuf = (u32 *)armGetTls();
|
2018-06-15 00:50:01 +01:00
|
|
|
if (r.CommandType == IpcCommandType_Close) {
|
|
|
|
Reboot();
|
|
|
|
}
|
|
|
|
|
2018-06-12 23:00:09 +01:00
|
|
|
if (r.CommandType == IpcCommandType_Request || r.CommandType == IpcCommandType_RequestWithContext) {
|
2018-06-15 00:50:01 +01:00
|
|
|
std::shared_ptr<IServiceObject> obj;
|
2018-10-16 21:33:45 +01:00
|
|
|
if (r.IsDomainRequest) {
|
|
|
|
obj = this->domain->get_domain_object(r.InThisObjectId);
|
|
|
|
if (obj != nullptr && r.InMessageType == DomainMessageType_Close) {
|
|
|
|
if (r.InThisObjectId == this->mitm_domain_id) {
|
2018-06-15 00:50:01 +01:00
|
|
|
Reboot();
|
|
|
|
}
|
2018-10-16 21:33:45 +01:00
|
|
|
this->domain->delete_object(r.InThisObjectId);
|
2018-06-12 23:00:09 +01:00
|
|
|
struct {
|
|
|
|
u64 magic;
|
|
|
|
u64 result;
|
|
|
|
} *o_resp;
|
|
|
|
|
|
|
|
o_resp = (decltype(o_resp)) ipcPrepareHeaderForDomain(&c, sizeof(*o_resp), 0);
|
2018-10-16 21:33:45 +01:00
|
|
|
*(DomainResponseHeader *)((uintptr_t)o_resp - sizeof(DomainResponseHeader)) = {0};
|
2018-06-12 23:00:09 +01:00
|
|
|
o_resp->magic = SFCO_MAGIC;
|
|
|
|
o_resp->result = 0x0;
|
|
|
|
Log(armGetTls(), 0x100);
|
|
|
|
return o_resp->result;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
obj = this->service_object;
|
2018-06-10 02:33:22 +01:00
|
|
|
}
|
2018-06-15 00:50:01 +01:00
|
|
|
if (obj != nullptr) {
|
2018-06-19 19:07:31 +01:00
|
|
|
retval = obj->dispatch(r, c, cmd_id, (u8 *)this->pointer_buffer.data(), this->pointer_buffer.size());
|
2018-06-12 23:00:09 +01:00
|
|
|
if (R_SUCCEEDED(retval)) {
|
2018-10-16 21:33:45 +01:00
|
|
|
if (r.IsDomainRequest) {
|
|
|
|
/* We never work with out object ids, so this should be fine. */
|
|
|
|
ipcParseDomainResponse(&cur_out_r, 0);
|
2018-06-12 23:00:09 +01:00
|
|
|
} else {
|
|
|
|
ipcParse(&cur_out_r);
|
2018-06-10 08:17:00 +01:00
|
|
|
}
|
2018-06-12 23:00:09 +01:00
|
|
|
return retval;
|
2018-06-10 02:33:22 +01:00
|
|
|
}
|
2018-06-12 23:00:09 +01:00
|
|
|
}
|
|
|
|
} else if (r.CommandType == IpcCommandType_Control || r.CommandType == IpcCommandType_ControlWithContext) {
|
|
|
|
/* Ipc Clone Current Object. */
|
|
|
|
retval = serviceIpcDispatch(&forward_service);
|
|
|
|
Log(armGetTls(), 0x100);
|
|
|
|
if (R_SUCCEEDED(retval)) {
|
|
|
|
ipcParse(&cur_out_r);
|
|
|
|
struct {
|
|
|
|
u64 magic;
|
|
|
|
u64 result;
|
|
|
|
} *resp = (decltype(resp))cur_out_r.Raw;
|
|
|
|
retval = resp->result;
|
2018-06-15 00:50:01 +01:00
|
|
|
if ((cmd_id == IpcCtrl_Cmd_CloneCurrentObject || cmd_id == IpcCtrl_Cmd_CloneCurrentObjectEx)) {
|
2018-06-10 02:33:22 +01:00
|
|
|
if (R_SUCCEEDED(retval)) {
|
2018-06-12 23:00:09 +01:00
|
|
|
Handle s_h;
|
|
|
|
Handle c_h;
|
|
|
|
Result rc;
|
|
|
|
if (R_FAILED((rc = svcCreateSession(&s_h, &c_h, 0, 0)))) {
|
|
|
|
fatalSimple(rc);
|
|
|
|
}
|
|
|
|
|
2018-06-15 00:50:01 +01:00
|
|
|
if (cur_out_r.NumHandles != 1) {
|
|
|
|
Reboot();
|
|
|
|
}
|
|
|
|
|
|
|
|
MitMSession<T> *new_sess = new MitMSession<T>((MitMServer<T> *)this->server, s_h, c_h, cur_out_r.Handles[0]);
|
|
|
|
new_sess->service_object = this->service_object;
|
2018-06-28 06:02:06 +01:00
|
|
|
|
2018-06-12 23:00:09 +01:00
|
|
|
if (this->is_domain) {
|
|
|
|
new_sess->is_domain = true;
|
2018-06-15 00:50:01 +01:00
|
|
|
new_sess->domain = this->domain;
|
2018-06-12 23:00:09 +01:00
|
|
|
new_sess->mitm_domain_id = this->mitm_domain_id;
|
|
|
|
new_sess->forward_service.type = this->forward_service.type;
|
|
|
|
new_sess->forward_service.object_id = this->forward_service.object_id;
|
|
|
|
}
|
|
|
|
this->get_manager()->add_waitable(new_sess);
|
|
|
|
ipcSendHandleMove(&c, c_h);
|
2018-06-10 02:33:22 +01:00
|
|
|
struct {
|
|
|
|
u64 magic;
|
|
|
|
u64 result;
|
2018-06-12 23:00:09 +01:00
|
|
|
} *o_resp;
|
2018-06-10 02:33:22 +01:00
|
|
|
|
2018-06-12 23:00:09 +01:00
|
|
|
o_resp = (decltype(o_resp)) ipcPrepareHeader(&c, sizeof(*o_resp));
|
|
|
|
o_resp->magic = SFCO_MAGIC;
|
|
|
|
o_resp->result = 0x0;
|
2018-06-10 02:33:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-12 23:00:09 +01:00
|
|
|
Log(armGetTls(), 0x100);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 0xF601 --> Dispatch onwards. */
|
|
|
|
if (retval == 0xF601) {
|
|
|
|
/* Patch PID Descriptor, if relevant. */
|
|
|
|
if (r.HasPid) {
|
|
|
|
/* [ctrl 0] [ctrl 1] [handle desc 0] [pid low] [pid high] */
|
|
|
|
cmdbuf[4] = 0xFFFE0000UL | (cmdbuf[4] & 0xFFFFUL);
|
|
|
|
}
|
2018-06-10 02:33:22 +01:00
|
|
|
|
2018-06-12 23:00:09 +01:00
|
|
|
Log(armGetTls(), 0x100);
|
|
|
|
retval = serviceIpcDispatch(&forward_service);
|
|
|
|
if (R_SUCCEEDED(retval)) {
|
2018-10-16 21:33:45 +01:00
|
|
|
if (r.IsDomainRequest) {
|
|
|
|
/* We never work with out object ids, so this should be fine. */
|
|
|
|
ipcParseDomainResponse(&cur_out_r, 0);
|
2018-06-12 23:00:09 +01:00
|
|
|
} else {
|
|
|
|
ipcParse(&cur_out_r);
|
2018-06-10 08:17:00 +01:00
|
|
|
}
|
2018-06-12 23:00:09 +01:00
|
|
|
|
|
|
|
struct {
|
|
|
|
u64 magic;
|
|
|
|
u64 result;
|
|
|
|
} *resp = (decltype(resp))cur_out_r.Raw;
|
|
|
|
|
|
|
|
retval = resp->result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Log(armGetTls(), 0x100);
|
|
|
|
Log(&cmd_id, sizeof(u64));
|
|
|
|
u64 retval_for_log = retval;
|
|
|
|
Log(&retval_for_log, sizeof(u64));
|
|
|
|
if (R_FAILED(retval)) {
|
2018-06-28 06:02:06 +01:00
|
|
|
//Reboot();
|
2018-06-15 00:50:01 +01:00
|
|
|
}
|
|
|
|
|
2018-06-12 23:00:09 +01:00
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
void postprocess(IpcParsedCommand &r, u64 cmd_id) override {
|
|
|
|
if (this->active_object == this->service_object && (r.CommandType == IpcCommandType_Request || r.CommandType == IpcCommandType_RequestWithContext)) {
|
|
|
|
IpcCommand c;
|
|
|
|
ipcInitialize(&c);
|
2018-06-19 19:07:31 +01:00
|
|
|
this->service_object->postprocess(cur_out_r, c, cmd_id, (u8 *)this->pointer_buffer.data(), this->pointer_buffer.size());
|
2018-06-12 23:00:09 +01:00
|
|
|
} else if (r.CommandType == IpcCommandType_Control || r.CommandType == IpcCommandType_ControlWithContext) {
|
|
|
|
if (cmd_id == IpcCtrl_Cmd_ConvertCurrentObjectToDomain) {
|
|
|
|
this->is_domain = true;
|
2018-06-15 00:50:01 +01:00
|
|
|
this->domain = std::make_shared<DomainOwner>();
|
2018-06-12 23:00:09 +01:00
|
|
|
struct {
|
|
|
|
u64 magic;
|
|
|
|
u64 result;
|
|
|
|
u32 domain_id;
|
|
|
|
} *resp = (decltype(resp))cur_out_r.Raw;
|
|
|
|
Result rc;
|
2018-06-15 00:50:01 +01:00
|
|
|
if (R_FAILED((rc = this->domain->set_object(this->service_object, resp->domain_id)))) {
|
2018-06-12 23:00:09 +01:00
|
|
|
fatalSimple(rc);
|
2018-06-10 02:33:22 +01:00
|
|
|
}
|
2018-06-12 23:00:09 +01:00
|
|
|
this->mitm_domain_id = resp->domain_id;
|
|
|
|
this->forward_service.type = ServiceType_Domain;
|
|
|
|
this->forward_service.object_id = resp->domain_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cleanup() override {
|
|
|
|
/* Clean up copy handles. */
|
|
|
|
for (unsigned int i = 0; i < cur_out_r.NumHandles; i++) {
|
|
|
|
if (cur_out_r.WasHandleCopied[i]) {
|
|
|
|
svcCloseHandle(cur_out_r.Handles[i]);
|
2018-06-10 02:33:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-19 19:07:31 +01:00
|
|
|
};
|