-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
cmake_minimum_required(VERSION 3.10) | ||
|
||
project(qtiesepmpatch) | ||
|
||
set(CMAKE_C_STANDARD 11) | ||
set(CMAKE_CXX_STANDARD 17) | ||
|
||
add_library(qtiesepmpatch SHARED | ||
inject_entry.c ipc/ipc_request_handler.cpp) | ||
|
||
if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") | ||
SET(CLANG_CXX_EXTRA_OPT "-Werror=unknown-warning-option -Werror=format-invalid-specifier -Werror=call-to-pure-virtual-from-ctor-dtor") | ||
SET(CLANG_C_EXTRA_OPT "-Werror=format-invalid-specifier") | ||
else () | ||
SET(CLANG_CXX_EXTRA_OPT "") | ||
SET(CLANG_C_EXTRA_OPT "") | ||
endif () | ||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CLANG_CXX_EXTRA_OPT} -fPIC -Werror=delete-non-virtual-dtor -Werror=return-type -Werror=non-virtual-dtor -Wno-invalid-offsetof") | ||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CLANG_C_EXTRA_OPT} -fPIC -Werror=return-type") | ||
|
||
set(CMAKE_CXX_IMPLICIT_LINK_LIBRARIES "") | ||
set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "") | ||
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-allow-shlib-undefined,--no-undefined") | ||
|
||
set_target_properties(qtiesepmpatch PROPERTIES | ||
LINKER_LANGUAGE C | ||
CXX_EXTENSIONS OFF | ||
POSITION_INDEPENDENT_CODE ON | ||
) | ||
|
||
target_compile_definitions(qtiesepmpatch PRIVATE NCI_HOST_VERSION=\"${NCI_HOST_VERSION}\") | ||
|
||
target_link_libraries(qtiesepmpatch basehalpatch ${ANDROID_LIBS}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#ifndef NCI_HOST_VERSION | ||
#error Please define macro NCI_HOST_VERSION in CMakeLists.txt | ||
#endif | ||
|
||
#include <stddef.h> | ||
#include <errno.h> | ||
|
||
#include "../libbasehalpatch/ipc/inject_io_init.h" | ||
|
||
__attribute__((used, section("NCI_HOST_VERSION"), visibility("default"))) | ||
const char g_nci_host_version[] = NCI_HOST_VERSION; | ||
|
||
// called by daemon with ptrace | ||
__attribute__((noinline, visibility("default"))) | ||
void *qti_esepm_patch_inject_init(int fd) { | ||
(void) g_nci_host_version; | ||
if (fd < 0) { | ||
return (void *) -EINVAL; | ||
} | ||
if (initElfHeaderInfo("libqtiesepmpatch.so", &qti_esepm_patch_inject_init) == 0) { | ||
return (void *) -EBADE; | ||
} | ||
return (void *) (size_t) BaseHalPatchInitSocket(fd); | ||
} |
96 changes: 96 additions & 0 deletions
96
app/src/main/cpp/libqtiesepmpatch/ipc/ipc_request_handler.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// | ||
// Created by kinit on 2021-11-18. | ||
// | ||
#include <cstring> | ||
#include <unistd.h> | ||
|
||
#include "ipc_requests.h" | ||
#include "../../libbasehalpatch/ipc/request_handler.h" | ||
#include "../../libbasehalpatch/hook/hook_proc_symbols.h" | ||
|
||
using namespace halpatch; | ||
using namespace halpatch::qtiesepm; | ||
|
||
static volatile bool sInitialized = false; | ||
static volatile int sNciDeviceFd = -1; | ||
|
||
const char *const NCI_DEVICE_NAME = "/dev/nq-nci"; | ||
|
||
namespace halpatchhook::callback { | ||
|
||
void afterHook_read(ssize_t result, int fd, const void *buffer, size_t size) {} | ||
|
||
void afterHook_write(ssize_t result, int fd, const void *buffer, size_t size) {} | ||
|
||
void afterHook_open(int result, const char *name, int flags, uint32_t mode) { | ||
if (result > 0 && name != nullptr && strncmp(NCI_DEVICE_NAME, name, strlen(NCI_DEVICE_NAME)) == 0) { | ||
sNciDeviceFd = result; | ||
} | ||
} | ||
|
||
void afterHook_close(int result, int fd) { | ||
if (fd == sNciDeviceFd) { | ||
sNciDeviceFd = -1; | ||
} | ||
} | ||
|
||
void afterHook_ioctl(int result, int fd, unsigned long int request, uint64_t arg) {} | ||
|
||
void afterHook_select(int result, int nfds, void *readfds, void *writefds, void *exceptfds, void *timeout) {} | ||
|
||
} | ||
|
||
void handleGetVersionRequest(uint32_t requestId, const void *, uint32_t) { | ||
const char *version = NCI_HOST_VERSION; | ||
HalResponse response = {requestId, 0, 0, static_cast<uint32_t>(strlen(version))}; | ||
sendResponsePacket(response, version); | ||
} | ||
|
||
void handleGetHookStatusRequest(uint32_t requestId, const void *payload, uint32_t payloadSize) { | ||
HalResponse response = {requestId, sInitialized, 0, 0}; | ||
sendResponsePacket(response, nullptr); | ||
} | ||
|
||
void handleGetNciFdRequest(uint32_t requestId, const void *payload, uint32_t payloadSize) { | ||
HalResponse response = {requestId, uint32_t(sNciDeviceFd), 0, 0}; | ||
sendResponsePacket(response, nullptr); | ||
} | ||
|
||
void handleInitPltHookRequest(uint32_t requestId, const void *payload, uint32_t payloadSize) { | ||
HalResponse response = {requestId, 0, 0, 0}; | ||
const auto *originHookProcedure = reinterpret_cast<const OriginHookProcedure *>(payload); | ||
if (payloadSize != sizeof(OriginHookProcedure) || originHookProcedure == nullptr | ||
|| originHookProcedure->struct_size != sizeof(OriginHookProcedure)) { | ||
sendResponseError(requestId, HalRequestErrorCode::ERR_INVALID_ARGUMENT, | ||
"invalid payload OriginHookProcedure"); | ||
} else { | ||
int result = hook_sym_init(originHookProcedure); | ||
if (result == 0) { | ||
sInitialized = true; | ||
} | ||
response.result = result; | ||
sendResponsePacket(response, nullptr); | ||
} | ||
} | ||
|
||
int handleRequestPacket(const HalRequest &request, const void *payload) { | ||
uint32_t requestId = request.id; | ||
auto requestCode = static_cast<RequestId>(request.requestCode); | ||
uint32_t payloadSize = request.payloadSize; | ||
switch (requestCode) { | ||
case RequestId::GET_VERSION: | ||
handleGetVersionRequest(requestId, payload, payloadSize); | ||
return 0; | ||
case RequestId::GET_HOOK_STATUS: | ||
handleGetHookStatusRequest(requestId, payload, payloadSize); | ||
return 0; | ||
case RequestId::INIT_PLT_HOOK: | ||
handleInitPltHookRequest(requestId, payload, payloadSize); | ||
return 0; | ||
case RequestId::GET_NCI_FD: | ||
handleGetNciFdRequest(requestId, payload, payloadSize); | ||
return 0; | ||
default: | ||
return -1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// Created by kinit on 2021-11-18. | ||
// | ||
|
||
#ifndef NCI_HOST_NATIVES_IPC_REQUESTS_H | ||
#define NCI_HOST_NATIVES_IPC_REQUESTS_H | ||
|
||
#include <cstdint> | ||
|
||
namespace halpatch::qtiesepm { | ||
|
||
enum class RequestId : uint32_t { | ||
GET_VERSION = 0x1, | ||
GET_HOOK_STATUS = 0x40, | ||
INIT_PLT_HOOK = 0x41, | ||
GET_NCI_FD = 0x61, | ||
}; | ||
|
||
} | ||
|
||
#endif //NCI_HOST_NATIVES_IPC_REQUESTS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters