Skip to content

Commit

Permalink
New way to get realpath on old system
Browse files Browse the repository at this point in the history
  • Loading branch information
yujincheng08 authored and RikkaW committed Mar 22, 2021
1 parent 5f41b0b commit 25a75de
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 54 deletions.
2 changes: 1 addition & 1 deletion riru/src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.4.1)

message("Build type: ${CMAKE_BUILD_TYPE}")

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17)

if (NOT DEFINED RIRU_VERSION_NAME)
message(FATAL_ERROR "RIRU_VERSION_NAME is not set")
Expand Down
52 changes: 9 additions & 43 deletions riru/src/main/cpp/hide_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,6 @@ namespace Hide {
ProtectedDataGuard::FuncType ProtectedDataGuard::ctor = nullptr;
ProtectedDataGuard::FuncType ProtectedDataGuard::dtor = nullptr;

struct link_map {
[[maybe_unused]] ElfW(Addr) l_addr;
char *l_name;
[[maybe_unused]] ElfW(Dyn) *l_ld;
[[maybe_unused]] struct link_map *l_next;
[[maybe_unused]] struct link_map *l_prev;
};

struct soinfo;

soinfo *solist = nullptr;
Expand All @@ -104,8 +96,9 @@ namespace Hide {
}

const char *get_realpath() {
return get_realpath_sym ? get_realpath_sym(this) : ((link_map *) ((uintptr_t) this +
solist_linkmap_offset))->l_name;
return get_realpath_sym ? get_realpath_sym(this) : ((std::string *) (
(uintptr_t) this + solist_realpath_offset))->c_str();

}

static bool setup(const SandHook::ElfImg &linker) {
Expand All @@ -122,26 +115,18 @@ namespace Hide {
return AndroidProp::GetApiLevel() < 26 || get_realpath_sym != nullptr;
}

static size_t solist_next_offset;
#ifdef __LP64__
constexpr static size_t solist_linkmap_offset = 0xd0;
constexpr static size_t solist_realpath_offset = 0x1a8;
inline static size_t solist_next_offset = 0x30;
#else
constexpr static size_t solist_linkmap_offset = 0xfc;
constexpr static size_t solist_realpath_offset = 0x174;
inline static size_t solist_next_offset = 0xa4;
#endif

// since Android 8
static const char *(*get_realpath_sym)(soinfo *);
inline static const char *(*get_realpath_sym)(soinfo *);
};

#ifdef __LP64__
size_t soinfo::solist_next_offset = 0x30;
#else
size_t soinfo::solist_next_offset = 0xa4;
#endif

// since Android 8
const char *(*soinfo::get_realpath_sym)(soinfo *) = nullptr;

bool solist_remove_soinfo(soinfo *si) {
soinfo *prev = nullptr, *trav;
for (trav = solist; trav != nullptr; trav = trav->next()) {
Expand Down Expand Up @@ -248,54 +233,35 @@ namespace Hide {
Hide::RemovePathsFromSolist(names);
}

static void HideFromSoList(const std::set<std::string_view> &names) {
auto callback = [](struct dl_phdr_info *info, size_t size, void *data) {
const auto &names = *((const std::set<std::string_view> *) data);
if (info->dlpi_name && names.count(info->dlpi_name)) {
memset((void *) info->dlpi_name, 0, strlen(info->dlpi_name));
}
return 0;
};
dl_iterate_phdr(callback, (void *) &names);
}

void HideFromSoList() {
auto self_path = Magisk::GetPathForSelfLib("libriru.so");
auto modules = Modules::Get();
std::set<std::string_view> names_to_remove{};
std::set<std::string_view> names_to_wipe{};
for (auto module : Modules::Get()) {
if (strcmp(module->id, MODULE_NAME_CORE) == 0) {
if (Entry::IsSelfUnloadAllowed()) {
LOGD("don't hide self since it will be unloaded");
} else {
names_to_remove.emplace(self_path);
}
names_to_wipe.emplace(self_path);
} else if (module->supportHide) {
if (!module->isLoaded()) {
LOGD("%s is unloaded", module->id);
continue;
}
if (module->apiVersion <= 24) {
if (module->apiVersion < 24) {
LOGW("%s is too old to hide so", module->id);
} else {
names_to_remove.emplace(module->path);
}
names_to_wipe.emplace(module->path);
} else {
LOGD("module %s does not support hide", module->id);
names_to_wipe.emplace(module->path);
}
}

if (AndroidProp::GetApiLevel() >= 23 && !names_to_remove.empty()) {
RemoveFromSoList(names_to_remove);
}

if (!names_to_wipe.empty()) {
HideFromSoList(names_to_wipe);
}
}

void PrepareMapsHideLibrary() {
Expand Down
14 changes: 4 additions & 10 deletions riru/src/main/cpp/util/tinynew.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,20 @@ void operator delete[](void* ptr) {
rather than just eliminate exceptions.
*/

void* operator new(std::size_t size, const std::nothrow_t&) {
void* operator new(std::size_t size, const std::nothrow_t&) noexcept {
return malloc(size);
}

void* operator new[](std::size_t size, const std::nothrow_t&) {
void* operator new[](std::size_t size, const std::nothrow_t&) noexcept {
return malloc(size);
}

void operator delete(void* ptr, const std::nothrow_t&) {
void operator delete(void* ptr, const std::nothrow_t&) noexcept {
free(ptr);
}

void operator delete[](void* ptr, const std::nothrow_t&) {
void operator delete[](void* ptr, const std::nothrow_t&) noexcept {
free(ptr);
}

//eof

__attribute__((__visibility__("default")))
std::terminate_handler __cxa_terminate_handler = abort;

__attribute__((__visibility__("default")))
std::unexpected_handler __cxa_unexpected_handler = abort;

0 comments on commit 25a75de

Please sign in to comment.