Skip to content
This repository has been archived by the owner on Jan 1, 2024. It is now read-only.

Commit

Permalink
improved elf handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Strelsky committed Jun 30, 2023
1 parent d60c9d6 commit 0ec82cd
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
4 changes: 4 additions & 0 deletions include/dbg/dbg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ class ProcessInfoIterator {
iterator end() const {
return pids.end();
}

size_t length() const {
return pids.length();
}
};

inline ProcessInfoIterator getProcesses() {
Expand Down
15 changes: 13 additions & 2 deletions libhijacker/source/dbg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ int __attribute__((noinline)) mdbg_call(DbgArg1 &arg1, DbgArg2 &arg2, DbgArg3 &a
int res = sceKernelDlsym(0x2001, "get_authinfo", (void **) &addr);
if (res > -1 && addr) {
_mdbg = (p_mdbg_call)(addr + 7);
} else {
puts("failed to get get_authinfo for mdbg_call");
}
}

Expand All @@ -98,8 +100,17 @@ IdArray getAllPids() {
UniquePtr<int[]> buf{new int[length]};
DbgGetPidsArg arg2{buf.get(), length};
DbgArg3 arg3{};
mdbg_call(arg1, arg2, arg3);
return {buf.get(), arg3.length};
int res = mdbg_call(arg1, arg2, arg3);
if (arg3.length != 0) {
return {buf.get(), arg3.length};
}
int err = errno;
if (res == 0) {
res = arg3.err;
}
printf("dbg::getAllPids failed %d %s\n", res, strerror(res));
printf("errno %d %s\n", err, strerror(err));
return nullptr;
}

IdArray getAllTids(int pid) {
Expand Down
27 changes: 17 additions & 10 deletions libhijacker/source/elf/elf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,34 +255,32 @@ bool Elf::parseDynamicTable() {

if (strtab == nullptr) [[unlikely]] {
puts("strtab not found");
return false;
}

if (strtabLength == 0) [[unlikely]] {
puts("strtab size not found");
if (strtabLength == 0 && strtab != nullptr) [[unlikely]] {
puts("strtab size not found but strtab exists");
return false;
}

if (symtabLength == 0) [[unlikely]] {
puts("symtab size not found");
return false;
}

if (symtab == nullptr) [[unlikely]] {
puts("symtab not found");
return false;
}

if (relatbl == nullptr) [[unlikely]] {
// should this be allowed?
puts("rela table not found");
return false;
}

if (plt == nullptr) [[unlikely]] {
// should this be allowed?
puts("plt table not found");
return false;
}

if (symtab == nullptr || strtab == nullptr) [[unlikely]] {
// don't need to proceed
return true;
}

List<String> names{};
Expand Down Expand Up @@ -759,6 +757,7 @@ bool Elf::launch() {
if (!processPltRelocations()) [[unlikely]] {
return false;
}

uintptr_t args = setupKernelRW();
if (args == 0) [[unlikely]] {
return false;
Expand All @@ -771,7 +770,6 @@ bool Elf::launch() {

puts("starting");

// TODO release the buffer from memory
return start(args);
}

Expand All @@ -792,6 +790,9 @@ bool Elf::start(uintptr_t args) {
}

uintptr_t Elf::getSymbolAddress(const Elf64_Rela *__restrict rel) const {
if (symtab == nullptr || strtab == nullptr) [[unlikely]] {
return true;
}
const Elf64_Sym *__restrict sym = symtab + ELF64_R_SYM(rel->r_info);
if (sym->st_value != 0) {
// the symbol exists in our elf
Expand All @@ -810,6 +811,9 @@ uintptr_t Elf::getSymbolAddress(const Elf64_Rela *__restrict rel) const {
}

bool Elf::processRelocations() {
if (relatbl == nullptr) [[unlikely]] {
return true;
}
uint8_t *const image = data.get() + textOffset;
const size_t length = relaLength;
for (size_t i = 0; i < length; i++) {
Expand Down Expand Up @@ -850,6 +854,9 @@ bool Elf::processRelocations() {
}

bool Elf::processPltRelocations() {
if (plt == nullptr) [[unlikely]] {
return true;
}
uint8_t *const image = data.get() + textOffset;
const size_t length = pltLength;
for (size_t i = 0; i < length; i++) {
Expand Down
10 changes: 9 additions & 1 deletion spawner/source/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "backtrace.hpp"
#include "dbg.hpp"
#include "dbg/dbg.hpp"
#include "elf/elf.hpp"
#include "kernel.hpp"
#include "hijacker.hpp"
#include "kernel/kernel.hpp"
#include "util.hpp"
#include <unistd.h>

Expand Down Expand Up @@ -35,7 +37,6 @@ class FileDescriptor {

void close() {
if (fd != -1) {
__builtin_printf("closing socket %d\n", fd);
::close(fd);
fd = -1;
}
Expand Down Expand Up @@ -194,6 +195,13 @@ int main() {
initStdout();
//clearFramePointer();
puts("main entered");

auto processes = dbg::getProcesses();
if (processes.length() == 0) {
puts("This kernel version is not yet supported :(");
return -1;
}

auto spawner = Spawner::getSpawner("SceRedisServer");
if (spawner == nullptr) {
puts("failed to get spawner for SceRedisServer");
Expand Down
2 changes: 2 additions & 0 deletions test_elf/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,6 @@ target_sources(${PROJECT_NAME} PRIVATE ${SrcFiles})
target_link_directories (${PROJECT_NAME} PUBLIC ${D_CWD})

target_link_libraries (${PROJECT_NAME} PUBLIC SceLibcInternal kernel_sys)
set(CMAKE_EXE_LINKER_FLAGS "-fuse-ld=lld -v -Xlinker -pie -Xlinker --gc-sections -Xlinker -zmax-page-size=16384 -Xlinker -zcommon-page-size=16384 -Xlinker -T ${CMAKE_CURRENT_SOURCE_DIR}/linker.x -Wl,--build-id=none -Wl,-z,norelro")
set (CMAKE_C_LINKER_WRAPPER_FLAG "-Xlinker" " ")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

0 comments on commit 0ec82cd

Please sign in to comment.