Skip to content

Commit

Permalink
Filter process earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
LoveSy committed Nov 12, 2020
1 parent 00363c3 commit 151b275
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.elderdrivers.riru.common.KeepAll;
import com.elderdrivers.riru.edxp.common.BuildConfig;
import com.elderdrivers.riru.edxp.config.ConfigManager;
import com.elderdrivers.riru.edxp.framework.ProcessHelper;
import com.elderdrivers.riru.edxp.util.Utils;

import java.security.AccessController;
Expand All @@ -16,8 +15,6 @@
import java.util.ServiceLoader;
import java.util.concurrent.atomic.AtomicReference;

import static com.elderdrivers.riru.edxp.proxy.BaseProxy.onBlackListed;

@SuppressLint("DefaultLocale")
public class Main implements KeepAll {

Expand All @@ -40,9 +37,6 @@ public static void forkAndSpecializePre(int uid, int gid, int[] gids, int debugF
String niceName, int[] fdsToClose, int[] fdsToIgnore,
boolean startChildZygote, String instructionSet,
String appDataDir) {
if (isBlackListedProcess(uid)) {
return;
}
final EdxpImpl edxp = getEdxpImpl();
if (edxp == null || !edxp.isInitialized()) {
return;
Expand All @@ -67,10 +61,6 @@ public static void forkAndSpecializePre(int uid, int gid, int[] gids, int debugF
}

public static void forkAndSpecializePost(int pid, String appDataDir, String niceName) {
if (isBlackListedProcess(Process.myUid())) {
onBlackListed();
return;
}
final EdxpImpl edxp = getEdxpImpl();
if (edxp == null || !edxp.isInitialized()) {
return;
Expand Down Expand Up @@ -159,11 +149,4 @@ public Void run() {
}
});
}

// TODO do this earlier?
private static boolean isBlackListedProcess(int uid) {
return ProcessHelper.isIsolated(uid)
|| ProcessHelper.isRELROUpdater(uid)
|| ProcessHelper.isWebViewZygote(uid);
}
}

This file was deleted.

2 changes: 1 addition & 1 deletion edxp-core/src/main/cpp/external/yahfa/src/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#define LOGE(...)
#else
#define LOG_TAG "EdXposed"
#ifdef DEBUG
#ifndef NDEBUG
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,LOG_TAG,__VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
Expand Down
28 changes: 28 additions & 0 deletions edxp-core/src/main/cpp/main/include/JNIHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,3 +103,31 @@ ALWAYS_INLINE static int ClearException(JNIEnv *env) {
env->RegisterNatives(class, methods, size); \
if (ClearException(env)) LOGE("RegisterNatives " #class);

class JUTFString {
public:
inline JUTFString(JNIEnv *env, jstring jstr) : JUTFString(env, jstr, nullptr) {
}

inline JUTFString(JNIEnv *env, jstring jstr, const char *default_cstr) : env_(env),
jstr_(jstr) {
if (env_ && jstr_) cstr_ = env_->GetStringUTFChars(jstr, nullptr);
else cstr_ = default_cstr;
}

inline operator const char *() const { return cstr_; }

inline operator const std::string() const { return cstr_; }

inline operator const bool() const { return cstr_ != nullptr; }

inline auto get() const { return cstr_; }

inline ~JUTFString() {
if (env_ && jstr_) env_->ReleaseStringUTFChars(jstr_, cstr_);
}

private:
JNIEnv *env_;
jstring jstr_;
const char *cstr_;
};
123 changes: 74 additions & 49 deletions edxp-core/src/main/cpp/main/src/edxp_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,12 @@
#pragma clang diagnostic ignored "-Wunused-value"

namespace edxp {

Context *Context::instance_ = nullptr;

Context *Context::GetInstance() {
if (instance_ == nullptr) {
instance_ = new Context();
}
return instance_;
}

ALWAYS_INLINE bool Context::IsInitialized() const {
return initialized_;
}

ALWAYS_INLINE Variant Context::GetVariant() const {
return variant_;
}

ALWAYS_INLINE jobject Context::GetCurrentClassLoader() const {
return inject_class_loader_;
}
constexpr int FIRST_ISOLATED_UID = 99000;
constexpr int LAST_ISOLATED_UID = 99999;
constexpr int FIRST_APP_ZYGOTE_ISOLATED_UID = 90000;
constexpr int LAST_APP_ZYGOTE_ISOLATED_UID = 98999;
constexpr int SHARED_RELRO_UID = 1037;
constexpr int PER_USER_RANGE = 100000;

void Context::CallPostFixupStaticTrampolinesCallback(void *class_ptr, jmethodID callback_mid) {
if (UNLIKELY(!callback_mid || !class_linker_class_)) {
Expand Down Expand Up @@ -188,6 +173,28 @@ namespace edxp {
LoadDexAndInit(env, kInjectDexPath);
}

void Context::ReleaseJavaEnv(JNIEnv *env) {
if (UNLIKELY(!instance_)) return;
initialized_ = false;
if (entry_class_) {
env->DeleteGlobalRef(entry_class_);
entry_class_ = nullptr;
}
if (class_linker_class_) {
env->DeleteGlobalRef(class_linker_class_);
class_linker_class_ = nullptr;
}
if (inject_class_loader_) {
env->DeleteGlobalRef(inject_class_loader_);
inject_class_loader_ = nullptr;
}
app_data_dir_ = nullptr;
nice_name_ = nullptr;
vm_ = nullptr;
pre_fixup_static_mid_ = nullptr;
post_fixup_static_mid_ = nullptr;
}


inline void Context::FindAndCall(JNIEnv *env, const char *method_name,
const char *method_sig, ...) const {
Expand All @@ -206,26 +213,6 @@ namespace edxp {
}
}

ALWAYS_INLINE JavaVM *Context::GetJavaVM() const {
return vm_;
}

ALWAYS_INLINE void Context::SetAppDataDir(jstring app_data_dir) {
app_data_dir_ = app_data_dir;
}

ALWAYS_INLINE jstring Context::GetAppDataDir() const {
return app_data_dir_;
}

ALWAYS_INLINE void Context::SetNiceName(jstring nice_name) {
nice_name_ = nice_name;
}

ALWAYS_INLINE jstring Context::GetNiceName() const {
return nice_name_;
}

void
Context::OnNativeForkSystemServerPre(JNIEnv *env, jclass clazz, uid_t uid, gid_t gid,
jintArray gids,
Expand All @@ -252,6 +239,33 @@ namespace edxp {
return 0;
}

bool Context::ShouldSkipInject(JNIEnv *env, jstring nice_name, jstring data_dir, jint uid,
jboolean is_child_zygote) {
const auto app_id = uid % PER_USER_RANGE;
const JUTFString package_name(env, nice_name, "UNKNOWN");
bool skip = false;
if (is_child_zygote) {
skip = true;
LOGW("skip injecting into %s because it's a child zygote", package_name.get());
}

if ((app_id >= FIRST_ISOLATED_UID && app_id <= LAST_ISOLATED_UID) ||
(app_id >= FIRST_APP_ZYGOTE_ISOLATED_UID && app_id <= LAST_APP_ZYGOTE_ISOLATED_UID) ||
app_id == SHARED_RELRO_UID) {
skip = true;
LOGW("skip injecting into %s because it's isolated", package_name.get());
}

// TODO(yujincheng08): check whitelist/blacklist.
const JUTFString dir(env, data_dir);
if(!dir || !ConfigManager::GetInstance()->IsAppNeedHook(dir)) {
skip = true;
LOGW("skip injecting xposed into %s because it's whitelisted/blacklisted",
package_name.get());
}
return skip;
}

void Context::OnNativeForkAndSpecializePre(JNIEnv *env, jclass clazz,
jint uid, jint gid,
jintArray gids,
Expand All @@ -265,21 +279,32 @@ namespace edxp {
jboolean is_child_zygote,
jstring instruction_set,
jstring app_data_dir) {
skip_ = ShouldSkipInject(env, nice_name, app_data_dir, uid, is_child_zygote);
app_data_dir_ = app_data_dir;
nice_name_ = nice_name;
PrepareJavaEnv(env);
FindAndCall(env, "forkAndSpecializePre",
"(II[II[[IILjava/lang/String;Ljava/lang/String;[I[IZLjava/lang/String;Ljava/lang/String;)V",
uid, gid, gids, runtime_flags, rlimits,
mount_external, se_info, nice_name, fds_to_close, fds_to_ignore,
is_child_zygote, instruction_set, app_data_dir);
if(!skip_) {
FindAndCall(env, "forkAndSpecializePre",
"(II[II[[IILjava/lang/String;Ljava/lang/String;[I[IZLjava/lang/String;Ljava/lang/String;)V",
uid, gid, gids, runtime_flags, rlimits,
mount_external, se_info, nice_name, fds_to_close, fds_to_ignore,
is_child_zygote, instruction_set, app_data_dir);
}
}

int Context::OnNativeForkAndSpecializePost(JNIEnv *env, jclass clazz, jint res) {
if (res == 0) {
PrepareJavaEnv(env);
FindAndCall(env, "forkAndSpecializePost", "(ILjava/lang/String;Ljava/lang/String;)V",
res, app_data_dir_, nice_name_);
const JUTFString package_name(env, nice_name_);
if (!skip_) {
PrepareJavaEnv(env);
FindAndCall(env, "forkAndSpecializePost",
"(ILjava/lang/String;Ljava/lang/String;)V",
res, app_data_dir_, nice_name_);
LOGD("injected xposed into %s", package_name.get());
} else {
ReleaseJavaEnv(env);
LOGD("skipped %s", package_name.get());
}
} else {
// in zygote process, res is child zygote pid
// don't print log here, see https://github.com/RikkaApps/Riru/blob/77adfd6a4a6a81bfd20569c910bc4854f2f84f5e/riru-core/jni/main/jni_native_method.cpp#L55-L66
Expand Down
Loading

0 comments on commit 151b275

Please sign in to comment.