Skip to content

Commit

Permalink
Switch cryptor to leaf only
Browse files Browse the repository at this point in the history
  • Loading branch information
knot126 committed Nov 6, 2024
1 parent ab1587c commit fd39e01
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
20 changes: 8 additions & 12 deletions jni/cryptor.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#ifdef BUILD_CIPHER
#ifndef USE_LEAF
#error Cryptor can only be built when using Leaf
#endif
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
Expand Down Expand Up @@ -67,26 +70,19 @@ size_t KNCipher_writeInternal(QiFileOutputStream *stream, char *buffer, size_t l
return 1;
}

void KNCipherInit(void *libsmashhit) {
void KNCipherInit(struct android_app *app, Leaf *leaf) {
// TODO these names vary by arch
#if defined(__ARM_ARCH_7A__)
void *readInternal = dlsym(libsmashhit, "_ZN17QiFileInputStream12readInternalEPcj");
void *writeInternal = dlsym(libsmashhit, "_ZN18QiFileOutputStream13writeInternalEPKcj");
void *readInternal = KNGetSymbolAddr("_ZN17QiFileInputStream12readInternalEPcj");
void *writeInternal = KNGetSymbolAddr("_ZN18QiFileOutputStream13writeInternalEPKcj");
#elif defined(__aarch64__)
void *readInternal = dlsym(libsmashhit, "_ZN17QiFileInputStream12readInternalEPcm");
void *writeInternal = dlsym(libsmashhit, "_ZN18QiFileOutputStream13writeInternalEPKcm");
void *readInternal = KNGetSymbolAddr("_ZN17QiFileInputStream12readInternalEPcm");
void *writeInternal = KNGetSymbolAddr("_ZN18QiFileOutputStream13writeInternalEPKcm");
#else
#error Unsupported platform
#endif

// WARNING set_memory_protection with exec doesnt work on modern android even
// if its RX, why!?
set_memory_protection(readInternal, 16, KN_MEM_READ_WRITE);
replace_function(readInternal, &KNCipher_readInternal);
set_memory_protection(readInternal, 16, KN_MEM_READ_RUN);

set_memory_protection(writeInternal, 16, KN_MEM_READ_WRITE);
replace_function(writeInternal, &KNCipher_writeInternal);
set_memory_protection(writeInternal, 16, KN_MEM_READ_RUN);
}
#endif
14 changes: 7 additions & 7 deletions jni/peekpoke.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,13 @@ int knEnablePeekPoke(lua_State *script) {
lua_register(script, "knInvertBranch", knInvertBranch);

// Types
lua_pushinteger(script, KN_TYPE_ADDR); lua_setglobal(script, "KN_TYPE_ADDR");
lua_pushinteger(script, KN_TYPE_BOOL); lua_setglobal(script, "KN_TYPE_BOOL");
lua_pushinteger(script, KN_TYPE_SHORT); lua_setglobal(script, "KN_TYPE_SHORT");
lua_pushinteger(script, KN_TYPE_INT); lua_setglobal(script, "KN_TYPE_INT");
lua_pushinteger(script, KN_TYPE_FLOAT); lua_setglobal(script, "KN_TYPE_FLOAT");
lua_pushinteger(script, KN_TYPE_STRING); lua_setglobal(script, "KN_TYPE_STRING");
lua_pushinteger(script, KN_TYPE_BYTES); lua_setglobal(script, "KN_TYPE_BYTES");
knLuaPushEnum(script, KN_TYPE_ADDR);
knLuaPushEnum(script, KN_TYPE_BOOL);
knLuaPushEnum(script, KN_TYPE_SHORT);
knLuaPushEnum(script, KN_TYPE_INT);
knLuaPushEnum(script, KN_TYPE_FLOAT);
knLuaPushEnum(script, KN_TYPE_STRING);
knLuaPushEnum(script, KN_TYPE_BYTES);

// Memory protections
knLuaPushEnum(script, KN_MEM_READ_WRITE);
Expand Down
12 changes: 7 additions & 5 deletions jni/shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,15 @@ void KNInitLua(struct android_app *app, Leaf *leaf) {
gAndroidExternalDataPath = strdup(app->activity->externalDataPath);
}

#ifdef BUILD_CIPHER
void KNCipherInit(struct android_app *app, Leaf *leaf);
#endif

ModuleInitFunc gModuleInitFuncs[] = {
KNInitLua,
#ifdef BUILD_CIPHER
KNCipherInit,
#endif
NULL,
};

Expand Down Expand Up @@ -215,11 +222,6 @@ void android_main(struct android_app *app) {
return;
}

#ifdef BUILD_CIPHER
void KNCipherInit(void *libsmashhit);
KNCipherInit(gLibsmashhitHandle);
#endif

__android_log_print(ANDROID_LOG_INFO, TAG, "Calling android_main at <0x%p> with app at <0x%p>", (void*)func, (void*)app);

func(app);
Expand Down

0 comments on commit fd39e01

Please sign in to comment.