Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions loader/src/include/daemon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ void Init(const char* path);

std::string GetTmpPath();

int Connect(uint8_t retry);

bool PingHeartbeat();

std::vector<Module> ReadModules();
Expand Down
13 changes: 8 additions & 5 deletions loader/src/injector/module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,29 +331,32 @@ void ZygiskContext::run_modules_post() {

void ZygiskContext::app_specialize_pre() {
uid_t uid = args.app->uid;
// Correct uid for isolated services
if (uid >= AID_ISOLATED_START && uid <= AID_ISOLATED_END && args.app->app_data_dir) {
bool is_isolated_aid = uid >= AID_ISOLATED_START && uid <= AID_ISOLATED_END;
if (is_isolated_aid && args.app->app_data_dir) {
const char *data_dir = nullptr;
data_dir = env->GetStringUTFChars(args.app->app_data_dir, nullptr);
if (data_dir != nullptr) {
struct stat st;
if (stat(data_dir, &st) != -1) {
// Correct uid for isolated services
uid = st.st_uid;
LOGV("identify isolated service [uid:%d, data_dir:%s]", uid, data_dir);
}
LOGV("Found isolated process [uid:%d, data_dir:%s]", uid, data_dir);
env->ReleaseStringUTFChars(args.app->app_data_dir, data_dir);
}
}

if (info_flags == 0) info_flags = zygiskd::GetProcessFlags(uid);
bool skip_zygiskd = is_isolated_aid && zygiskd::Connect(1) == -1;

if (!skip_zygiskd && info_flags == 0) info_flags = zygiskd::GetProcessFlags(uid);

if ((info_flags & UNMOUNT_MASK) == UNMOUNT_MASK) {
LOGI("[%s] is on the denylist", process);
flags |= DO_REVERT_UNMOUNT;
}

flags |= APP_SPECIALIZE;
run_modules_pre();
if (!skip_zygiskd) run_modules_pre();
}

void ZygiskContext::app_specialize_post() {
Expand Down
11 changes: 11 additions & 0 deletions loader/src/ptracer/ptracer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,18 @@ bool inject_on_main(int pid, const char *lib_path) {
}

// Backup the current registers before we start making remote calls.
// It is vital we keep the original PSTATE intact in the backup, so
// the original executable can securely validate its own BTI pad upon final resume.
memcpy(&backup, &regs, sizeof(regs));

#if defined(__aarch64__)
// Clear the BTYPE field (bits 10 and 11) in PSTATE.
// The previous indirect branch from the linker set BTYPE to 0b11.
// If we jump into BTI-protected bionic libraries (like libdl.so) with BTYPE=0b11,
// the CPU will throw a Branch Target Exception (SIGILL).
regs.pstate &= ~(3ULL << 10);
#endif

map = MapInfo::Scan(std::to_string(pid)); // Re-scan maps as they may have changed.
auto local_map = MapInfo::Scan();
auto libc_return_addr = find_module_return_addr(map, "libc.so");
Expand Down
Loading