This repository was archived by the owner on Feb 12, 2026. It is now read-only.
forked from JingMatrix/LSPosed
-
Notifications
You must be signed in to change notification settings - Fork 38
Add new KernelSU method support #14
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
05c049d
Add new KernelSU method support
frknkrc44 252ac61
Move kernelsu defines into ksu.h, try to fix leaks
frknkrc44 3423d6e
Add new lines to increase readability
frknkrc44 b081f8d
Fix a possible memory leak
frknkrc44 d8d3356
Format the code a bit
frknkrc44 87ecb15
Invert ksu_is_in_denylist logic
frknkrc44 44de658
Use goto instead of early return in ksu_*
frknkrc44 25bd2a3
Remove some ksu.d comments
frknkrc44 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| apache/build | ||
| apache/local/generated | ||
| .project | ||
| .settings | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,11 +26,13 @@ | |
| #include <sys/prctl.h> | ||
| #include <sys/wait.h> | ||
| #include <sys/stat.h> | ||
| #include <sys/syscall.h> | ||
| #include <stdlib.h> | ||
| #include <unistd.h> | ||
|
|
||
| #include <string> | ||
|
|
||
| #include "ksu.h" | ||
| #include "logging.h" | ||
| #include "packagename.h" | ||
|
|
||
|
|
@@ -78,33 +80,92 @@ bool exec_command(char *buf, size_t len, const char *file, const char *argv[]) { | |
| return true; | ||
| } | ||
|
|
||
| #define KERNEL_SU_OPTION (int)0xdeadbeef | ||
| #define KERNELSU_CMD_GET_VERSION 2 | ||
| #define KERNELSU_CMD_UID_SHOULD_UMOUNT 13 | ||
|
|
||
| /* | ||
| * Root implementation indicator | ||
| * | ||
| * -1 - None | ||
| * 1 - KernelSU | ||
| * 2 - Magisk | ||
| * 3 - APatch | ||
| */ | ||
| static int root_impl = -1; | ||
|
|
||
| static int ksu_fd = -1; | ||
|
|
||
| static bool prepare_ksu_fd() { | ||
| if (ksu_fd >= 0) goto ksu_fd_return_success; | ||
|
|
||
| LOGD("Prepare KSU fd: Trying to send ksu option"); | ||
|
|
||
| syscall(SYS_reboot, KSU_INSTALL_MAGIC1, KSU_INSTALL_MAGIC2, 0, (void*)&ksu_fd); | ||
|
|
||
| LOGD("Prepare KSU fd: %x", ksu_fd); | ||
|
|
||
| return ksu_fd >= 0; | ||
|
|
||
| ksu_fd_return_success: | ||
| return true; | ||
| } | ||
|
|
||
| static void ksu_close_fd() { | ||
| if (ksu_fd >= 0) { | ||
| close(ksu_fd); | ||
| ksu_fd = -1; | ||
| } | ||
| } | ||
|
|
||
| static bool ksu_get_existence() { | ||
| int version = 0; | ||
| int reply_ok = 0; | ||
|
|
||
| if (prepare_ksu_fd()) { | ||
| struct ksu_get_info_cmd g_version {}; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto (new line separation). Moreover, no need of the {} |
||
|
|
||
| syscall(SYS_ioctl, ksu_fd, KSU_IOCTL_GET_INFO, &g_version); | ||
|
|
||
| if (g_version.version > 0) { | ||
| LOGD("KernelSU version: %d (IOCTL)", g_version.version); | ||
|
|
||
| root_impl = 1; | ||
|
|
||
| goto ksu_exist_return_success; | ||
| } else { | ||
| ksu_close_fd(); | ||
| } | ||
| } | ||
|
|
||
| prctl((signed int)KERNEL_SU_OPTION, KERNELSU_CMD_GET_VERSION, &version, 0, &reply_ok); | ||
|
|
||
| if (version != 0) { | ||
| LOGD("KernelSU version: %d", version); | ||
| LOGD("KernelSU version: %d (PRCTL)", version); | ||
|
|
||
| root_impl = 1; | ||
|
|
||
| return true; | ||
| goto ksu_exist_return_success; | ||
| } | ||
|
|
||
| return false; | ||
|
|
||
| ksu_exist_return_success: | ||
| return true; | ||
| } | ||
|
|
||
| static bool ksu_is_in_denylist(uid_t app_uid) { | ||
| bool umount = false; | ||
|
|
||
| int reply_ok = 0; | ||
|
|
||
| if (prepare_ksu_fd()) { | ||
| struct ksu_uid_should_umount_cmd cmd = { app_uid }; | ||
|
|
||
| syscall(SYS_ioctl, ksu_fd, KSU_IOCTL_UID_SHOULD_UMOUNT, &cmd); | ||
|
|
||
| umount = !!cmd.should_umount; | ||
| goto ksu_return_umount; | ||
| } | ||
|
|
||
| prctl((signed int)KERNEL_SU_OPTION, KERNELSU_CMD_UID_SHOULD_UMOUNT, app_uid, &umount, &reply_ok); | ||
|
|
||
| ksu_return_umount: | ||
| return umount; | ||
| } | ||
|
|
||
|
|
@@ -337,7 +398,7 @@ bool apatch_uid_should_umount(const char *const process) { | |
| } | ||
|
|
||
| extern "C" JNIEXPORT jboolean JNICALL | ||
| Java_org_lsposed_lspd_service_DenylistManager_isInDenylist(JNIEnv *env, jobject, jstring appName) { | ||
| Java_org_lsposed_lspd_service_DenylistManager_isInDenylist(JNIEnv *env, jclass, jstring appName) { | ||
| const char *app_name = env->GetStringUTFChars(appName, nullptr); | ||
| if (app_name == nullptr) { | ||
| LOGE("Failed to get app name string"); | ||
|
|
@@ -406,7 +467,7 @@ Java_org_lsposed_lspd_service_DenylistManager_isInDenylist(JNIEnv *env, jobject, | |
| } | ||
|
|
||
| extern "C" JNIEXPORT jboolean JNICALL | ||
| Java_org_lsposed_lspd_service_DenylistManager_isInDenylistFromClasspathDir(JNIEnv *env, jobject, jstring classpathDirArg) { | ||
| Java_org_lsposed_lspd_service_DenylistManager_isInDenylistFromClasspathDir(JNIEnv *env, jclass, jstring classpathDirArg) { | ||
| const char *classpath_dir_arg = env->GetStringUTFChars(classpathDirArg, nullptr); | ||
|
|
||
| if (classpath_dir_arg == nullptr) { | ||
|
|
@@ -478,4 +539,9 @@ Java_org_lsposed_lspd_service_DenylistManager_isInDenylistFromClasspathDir(JNIEn | |
|
|
||
| app_in_denylist: | ||
| return JNI_TRUE; | ||
| } | ||
| } | ||
|
|
||
| extern "C" JNIEXPORT void JNICALL | ||
| Java_org_lsposed_lspd_service_DenylistManager_clearFd(JNIEnv *env, jclass) { | ||
| ksu_close_fd(); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /* | ||
| * This file is part of ReLSPosed. | ||
| * | ||
| * ReLSPosed is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * ReLSPosed is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with ReLSPosed. If not, see <https://www.gnu.org/licenses/>. | ||
| * | ||
| * Copyright (C) 2023 The PerformanC Organization Contributors | ||
| */ | ||
| // ------------------------------------------------------------------------------------------------------------- | ||
| /* INFO: Code below contains parts of the code of the KernelSU's Daemon. It is protected by its GPL-3.0 license. | ||
| See https://github.com/tiann/KernelSU repository for more details. */ | ||
| // ------------------------------------------------------------------------------------------------------------- | ||
|
|
||
|
|
||
| #ifndef RELSPOSED_KSU_H | ||
| #define RELSPOSED_KSU_H | ||
|
|
||
| #include <stdint.h> | ||
| #include <sys/ioctl.h> | ||
|
|
||
| struct ksu_get_info_cmd { | ||
| uint32_t version; | ||
| uint32_t flags; | ||
| uint32_t features; | ||
| }; | ||
|
|
||
| struct ksu_uid_should_umount_cmd { | ||
| uint32_t uid; | ||
| uint8_t should_umount; | ||
| }; | ||
|
|
||
| #define KERNEL_SU_OPTION (int)0xdeadbeef | ||
| #define KERNELSU_CMD_GET_VERSION 2 | ||
| #define KERNELSU_CMD_UID_SHOULD_UMOUNT 13 | ||
|
|
||
| #define KSU_INSTALL_MAGIC1 0xDEADBEEF | ||
| #define KSU_INSTALL_MAGIC2 0xCAFEBABE | ||
|
|
||
| #define KSU_IOCTL_GET_INFO _IOC(_IOC_READ, 'K', 2, 0) | ||
| #define KSU_IOCTL_UID_SHOULD_UMOUNT _IOC(_IOC_READ | _IOC_WRITE, 'K', 9, 0) | ||
|
|
||
| #endif //RELSPOSED_KSU_H |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto (new line separation)