add: Magisk variant status; improve: KSU detection

This commit adds the Magisk variant to module description, and also improves KernelSU detection by requiring the userspace part of it to be installed, AKA "ksud".
This commit is contained in:
ThePedroo
2024-10-24 16:51:23 -03:00
parent 380ef011a1
commit 135ebbb9ba
12 changed files with 245 additions and 222 deletions
+18 -10
View File
@@ -6,10 +6,11 @@
#include "../constants.h"
#include "../utils.h"
#include "common.h"
#include "apatch.h"
enum RootImplState apatch_get_existence(void) {
void apatch_get_existence(struct root_impl_state *state) {
struct stat s;
if (stat("/data/adb/apd", &s) != 0) {
if (errno != ENOENT) {
@@ -17,7 +18,9 @@ enum RootImplState apatch_get_existence(void) {
}
errno = 0;
return Inexistent;
state->state = Inexistent;
return;
}
char *PATH = getenv("PATH");
@@ -25,13 +28,17 @@ enum RootImplState apatch_get_existence(void) {
LOGE("Failed to get PATH environment variable: %s\n", strerror(errno));
errno = 0;
return Inexistent;
state->state = Inexistent;
return;
}
if (strstr(PATH, "/data/adb/ap/bin") == NULL) {
LOGE("APatch's APD binary is not in PATH\n");
return Inexistent;
state->state = Inexistent;
return;
}
char apatch_version[32];
@@ -41,16 +48,17 @@ enum RootImplState apatch_get_existence(void) {
LOGE("Failed to execute apd binary: %s\n", strerror(errno));
errno = 0;
return Inexistent;
state->state = Inexistent;
return;
}
int version = atoi(apatch_version + strlen("apd "));
if (version == 0) return Abnormal;
if (version >= MIN_APATCH_VERSION && version <= 999999) return Supported;
if (version >= 1 && version <= MIN_APATCH_VERSION - 1) return TooOld;
return Inexistent;
if (version == 0) state->state = Abnormal;
else if (version >= MIN_APATCH_VERSION && version <= 999999) state->state = Supported;
else if (version >= 1 && version <= MIN_APATCH_VERSION - 1) state->state = TooOld;
else state->state = Abnormal;
}
struct package_config {
+1 -1
View File
@@ -3,7 +3,7 @@
#include "../constants.h"
enum RootImplState apatch_get_existence(void);
void apatch_get_existence(struct root_impl_state *state);
bool apatch_uid_granted_root(uid_t uid);
+36 -12
View File
@@ -9,15 +9,34 @@
#include "common.h"
static enum RootImpl ROOT_IMPL = None;
static struct root_impl impl;
void root_impls_setup(void) {
if (ksu_get_existence() == Supported) ROOT_IMPL = KernelSU;
else if (apatch_get_existence() == Supported) ROOT_IMPL = APatch;
else if (magisk_get_existence() == Supported) ROOT_IMPL = Magisk;
else ROOT_IMPL = None;
struct root_impl_state state_ksu;
ksu_get_existence(&state_ksu);
switch (ROOT_IMPL) {
struct root_impl_state state_apatch;
apatch_get_existence(&state_apatch);
struct root_impl_state state_magisk;
magisk_get_existence(&state_magisk);
/* INFO: Check if it's only one supported, if not, it's multile and that's bad.
Remember that true here is equal to the integer 1. */
if ((state_ksu.state == Supported ? 1 : 0) + (state_apatch.state == Supported ? 1 : 0) + (state_magisk.state == Supported ? 1 : 0) >= 2) {
impl.impl = Multiple;
} else if (state_ksu.state == Supported) {
impl.impl = KernelSU;
} else if (state_apatch.state == Supported) {
impl.impl = APatch;
} else if (state_magisk.state == Supported) {
impl.impl = Magisk;
impl.variant = state_magisk.variant;
} else {
impl.impl = None;
}
switch (impl.impl) {
case None: {
LOGI("No root implementation found.\n");
@@ -39,19 +58,24 @@ void root_impls_setup(void) {
break;
}
case Magisk: {
LOGI("Magisk root implementation found.\n");
if (state_magisk.variant == 0) {
LOGI("Magisk Official root implementation found.\n");
} else {
LOGI("Magisk Kitsune root implementation found.\n");
}
break;
}
}
}
enum RootImpl get_impl(void) {
return ROOT_IMPL;
void get_impl(struct root_impl *uimpl) {
uimpl->impl = impl.impl;
uimpl->variant = impl.variant;
}
bool uid_granted_root(uid_t uid) {
switch (get_impl()) {
switch (impl.impl) {
case KernelSU: {
return ksu_uid_granted_root(uid);
}
@@ -68,7 +92,7 @@ bool uid_granted_root(uid_t uid) {
}
bool uid_should_umount(uid_t uid) {
switch (get_impl()) {
switch (impl.impl) {
case KernelSU: {
return ksu_uid_should_umount(uid);
}
@@ -85,7 +109,7 @@ bool uid_should_umount(uid_t uid) {
}
bool uid_is_manager(uid_t uid) {
switch (get_impl()) {
switch (impl.impl) {
case KernelSU: {
return ksu_uid_is_manager(uid);
}
+16 -2
View File
@@ -1,9 +1,11 @@
#ifndef COMMON_H
#define COMMON_H
#include <stdint.h>
#include "../constants.h"
enum RootImpl {
enum root_impls {
None,
Multiple,
KernelSU,
@@ -11,9 +13,21 @@ enum RootImpl {
Magisk
};
struct root_impl_state {
enum RootImplState state;
uint8_t variant;
};
struct root_impl {
enum root_impls impl;
uint8_t variant;
};
#define LONGEST_ROOT_IMPL_NAME sizeof("Magisk Kitsune")
void root_impls_setup(void);
enum RootImpl get_impl(void);
void get_impl(struct root_impl *uimpl);
bool uid_granted_root(uid_t uid);
+22 -5
View File
@@ -6,6 +6,7 @@
#include "../constants.h"
#include "../utils.h"
#include "common.h"
#include "kernelsu.h"
@@ -19,15 +20,31 @@
#define CMD_UID_GRANTED_ROOT 12
#define CMD_UID_SHOULD_UMOUNT 13
enum RootImplState ksu_get_existence(void) {
void ksu_get_existence(struct root_impl_state *state) {
int version = 0;
prctl((signed int)KERNEL_SU_OPTION, CMD_GET_VERSION, &version, 0, 0);
if (version == 0) return Inexistent;
if (version >= MIN_KSU_VERSION && version <= MAX_KSU_VERSION) return Supported;
if (version >= 1 && version <= MIN_KSU_VERSION - 1) return TooOld;
if (version == 0) state->state = Abnormal;
else if (version >= MIN_KSU_VERSION && version <= MAX_KSU_VERSION) {
/* INFO: Some custom kernels for custom ROMs have pre-installed KernelSU.
Some users don't want to use KernelSU, but, for example, Magisk.
This if allows this to happen, as it checks if "ksud" exists,
which in case it doesn't, it won't be considered as supported. */
struct stat s;
if (stat("/data/adb/ksud", &s) == -1) {
if (errno != ENOENT) {
LOGE("Failed to stat KSU daemon: %s\n", strerror(errno));
}
errno = 0;
state->state = Abnormal;
return Abnormal;
return;
}
state->state = Supported;
}
else if (version >= 1 && version <= MIN_KSU_VERSION - 1) state->state = TooOld;
else state->state = Abnormal;
}
bool ksu_uid_granted_root(uid_t uid) {
+1 -1
View File
@@ -3,7 +3,7 @@
#include "../constants.h"
enum RootImplState ksu_get_existence(void);
void ksu_get_existence(struct root_impl_state *state);
bool ksu_uid_granted_root(uid_t uid);
+21 -7
View File
@@ -10,6 +10,7 @@
#include "../constants.h"
#include "../utils.h"
#include "common.h"
#include "magisk.h"
@@ -30,7 +31,7 @@ enum magisk_variants variant = Official;
/* INFO: Longest path */
static char path_to_magisk[sizeof(DEBUG_RAMDISK_MAGISK)];
enum RootImplState magisk_get_existence(void) {
void magisk_get_existence(struct root_impl_state *state) {
struct stat s;
if (stat(SBIN_MAGISK, &s) != 0) {
if (errno != ENOENT) {
@@ -50,7 +51,9 @@ enum RootImplState magisk_get_existence(void) {
}
errno = 0;
return Inexistent;
state->state = Inexistent;
return;
}
/* INFO: /debug_ramdisk/magisk64 (or 32) doesn't exist but /debug_ramdisk/magisk does */
@@ -71,11 +74,20 @@ enum RootImplState magisk_get_existence(void) {
LOGE("Failed to execute magisk binary: %s\n", strerror(errno));
errno = 0;
return Abnormal;
state->state = Abnormal;
return;
}
state->variant = (uint8_t)Official;
for (unsigned long i = 0; i < sizeof(supported_variants) / sizeof(supported_variants[0]); i++) {
if (strstr(magisk_info, supported_variants[i])) variant = (enum magisk_variants)(i + 1);
if (strstr(magisk_info, supported_variants[i])) {
variant = (enum magisk_variants)(i + 1);
state->variant = (uint8_t)variant;
break;
}
}
argv[1] = "-V";
@@ -85,11 +97,13 @@ enum RootImplState magisk_get_existence(void) {
LOGE("Failed to execute magisk binary: %s\n", strerror(errno));
errno = 0;
return Abnormal;
state->state = Abnormal;
return;
}
if (atoi(magisk_version) >= MIN_MAGISK_VERSION) return Supported;
else return TooOld;
if (atoi(magisk_version) >= MIN_MAGISK_VERSION) state->state = Supported;
else state->state = TooOld;
}
bool magisk_uid_granted_root(uid_t uid) {
+1 -1
View File
@@ -8,7 +8,7 @@ enum magisk_variants {
Kitsune
};
enum RootImplState magisk_get_existence(void);
void magisk_get_existence(struct root_impl_state *state);
bool magisk_uid_granted_root(uid_t uid);