From 569183efe9abd19e1a0e9a1ed1afa305777c3888 Mon Sep 17 00:00:00 2001 From: liankong Date: Fri, 11 Apr 2025 15:19:18 +0800 Subject: [PATCH 1/7] =?UTF-8?q?=E6=B7=BB=E5=8A=A0super=5Faccess=E6=9C=BA?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/kpm/super_access.c | 249 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 249 insertions(+) diff --git a/kernel/kpm/super_access.c b/kernel/kpm/super_access.c index e69de29b..166c3388 100644 --- a/kernel/kpm/super_access.c +++ b/kernel/kpm/super_access.c @@ -0,0 +1,249 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include /* 包含 ARM64 重定位类型定义 */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "kpm.h" +#include "compact.h" +#include // 需要包含 offsetof 宏 + +// 结构体成员元数据 +struct DynamicStructMember { + const char* name; + size_t size; + size_t offset; +}; + +// 结构体元数据(包含总大小) +struct DynamicStructInfo { + const char* name; + size_t count; + size_t total_size; + struct DynamicStructMember* members; +}; + +// 定义结构体元数据的宏(直接使用 struct 名称) +#define DYNAMIC_STRUCT_BEGIN(struct_name) \ + static struct DynamicStructMember struct_name##_members[] = { + +#define DEFINE_MEMBER(struct_name, member) \ + { \ + .name = #member, \ + .size = sizeof(((struct struct_name*)0)->member), \ + .offset = offsetof(struct struct_name, member) \ + }, + +#define DYNAMIC_STRUCT_END(struct_name) \ + }; \ + static struct DynamicStructInfo struct_name##_info = { \ + .name = #struct_name, \ + .count = sizeof(struct_name##_members) / sizeof(struct DynamicStructMember), \ + .total_size = sizeof(struct struct_name), \ + .members = struct_name##_members \ + }; + +// ================================================================================== + +#include +#include + +// 定义元数据 +DYNAMIC_STRUCT_BEGIN(mount) + DEFINE_MEMBER(mount, mnt_parent) + DEFINE_MEMBER(mount, mnt) + DEFINE_MEMBER(mount, mnt_id) + DEFINE_MEMBER(mount, mnt_group_id) + DEFINE_MEMBER(mount, mnt_expiry_mark) + DEFINE_MEMBER(mount, mnt_master) + DEFINE_MEMBER(mount, mnt_devname) +DYNAMIC_STRUCT_END(mount) + +DYNAMIC_STRUCT_BEGIN(vfsmount) + DEFINE_MEMBER(vfsmount, mnt_root) + DEFINE_MEMBER(vfsmount, mnt_sb) + DEFINE_MEMBER(vfsmount, mnt_flags) +DYNAMIC_STRUCT_END(vfsmount) + +DYNAMIC_STRUCT_BEGIN(mnt_namespace) + DEFINE_MEMBER(mnt_namespace, count) + DEFINE_MEMBER(mnt_namespace, ns) + DEFINE_MEMBER(mnt_namespace, root) + DEFINE_MEMBER(mnt_namespace, seq) + DEFINE_MEMBER(mnt_namespace, mounts) +DYNAMIC_STRUCT_END(mnt_namespace) + +#include + +#ifdef CONFIG_KPROBES + +DYNAMIC_STRUCT_BEGIN(kprobe) + DEFINE_MEMBER(kprobe, addr) + DEFINE_MEMBER(kprobe, symbol_name) + DEFINE_MEMBER(kprobe, offset) + DEFINE_MEMBER(kprobe, pre_handler) + DEFINE_MEMBER(kprobe, post_handler) + DEFINE_MEMBER(kprobe, fault_handler) + DEFINE_MEMBER(kprobe, break_handler) + DEFINE_MEMBER(kprobe, flags) +DYNAMIC_STRUCT_END(kprobe) + +#endif + +#include +#include + +DYNAMIC_STRUCT_BEGIN(vm_area_struct) + DEFINE_MEMBER(vm_area_struct,vm_start) + DEFINE_MEMBER(vm_area_struct,vm_end) + DEFINE_MEMBER(vm_area_struct,vm_flags) + DEFINE_MEMBER(vm_area_struct,anon_vma) + DEFINE_MEMBER(vm_area_struct,vm_pgoff) + DEFINE_MEMBER(vm_area_struct,vm_file) + DEFINE_MEMBER(vm_area_struct,vm_private_data) + #ifdef CONFIG_ANON_VMA_NAME + DEFINE_MEMBER(vm_area_struct, anon_name) + #endif + DEFINE_MEMBER(vm_area_struct, vm_ops) +DYNAMIC_STRUCT_END(vm_area_struct) + +DYNAMIC_STRUCT_BEGIN(vm_operations_struct) + DEFINE_MEMBER(vm_operations_struct, open) + DEFINE_MEMBER(vm_operations_struct, close) + DEFINE_MEMBER(vm_operations_struct, name) + DEFINE_MEMBER(vm_operations_struct, access) +DYNAMIC_STRUCT_END(vm_operations_struct) + +#include + +DYNAMIC_STRUCT_BEGIN(netlink_kernel_cfg) + DEFINE_MEMBER(netlink_kernel_cfg, groups) + DEFINE_MEMBER(netlink_kernel_cfg, flags) + DEFINE_MEMBER(netlink_kernel_cfg, input) + DEFINE_MEMBER(netlink_kernel_cfg, cb_mutex) + DEFINE_MEMBER(netlink_kernel_cfg, bind) + DEFINE_MEMBER(netlink_kernel_cfg, unbind) + DEFINE_MEMBER(netlink_kernel_cfg, compare) +DYNAMIC_STRUCT_END(netlink_kernel_cfg) + +// ===================================================================================================================== + +#define STRUCT_INFO(name) &(name##_info) + +static +struct DynamicStructInfo* dynamic_struct_infos[] = { + STRUCT_INFO(mount), + STRUCT_INFO(vfsmount), + STRUCT_INFO(mnt_namespace), + #ifdef CONFIG_KPROBES + STRUCT_INFO(kprobe) + #endif + STRUCT_INFO(vm_area_struct), + STRUCT_INFO(vm_operations_struct), + STRUCT_INFO(netlink_kernel_cfg) +}; + +// return 0 if successful +// return -1 if struct not defined +int sukisu_super_find_struct( + const char* struct_name, + size_t* out_size, + int* out_members +) { + for(size_t i = 0; i < (sizeof(dynamic_struct_infos) / sizeof(dynamic_struct_infos[0])); i++) { + struct DynamicStructInfo* info = dynamic_struct_infos[i]; + if(strcmp(struct_name, info->name) == 0) { + if(out_size) + *out_size = info->total_size; + if(out_members) + *out_members = info->count; + return 0; + } + } + return -1; +} +EXPORT_SYMBOL(sukisu_super_find_struct); + +// Dynamic access struct +// return 0 if successful +// return -1 if struct not defined +// return -2 if member not defined +int sukisu_super_access ( + const char* struct_name, + const char* member_name, + size_t* out_offset, + size_t* out_size +) { + for(size_t i = 0; i < (sizeof(dynamic_struct_infos) / sizeof(dynamic_struct_infos[0])); i++) { + struct DynamicStructInfo* info = dynamic_struct_infos[i]; + if(strcmp(struct_name, info->name) == 0) { + for (size_t i1 = 0; i1 < info->count; i1++) { + if (strcmp(info->members[i1].name, member_name) == 0) { + if(out_offset) + *out_offset = info->members[i].offset; + if(out_size) + *out_size = info->members[i].size; + return 0; + } + } + return -2; + } + } + return -1; +} +EXPORT_SYMBOL(sukisu_super_access); + +// 动态 container_of 宏 +#define DYNAMIC_CONTAINER_OF(offset, member_ptr) ({ \ + (offset != (size_t)-1) ? (void*)((char*)(member_ptr) - offset) : NULL; \ +}) + +// Dynamic container_of +// return 0 if success +// return -1 if current struct not defined +// return -2 if target member not defined +int sukisu_super_container_of( + const char* struct_name, + const char* member_name, + void* ptr, + void** out_ptr +) { + if(ptr == NULL) { + return -3; + } + for(size_t i = 0; i < (sizeof(dynamic_struct_infos) / sizeof(dynamic_struct_infos[0])); i++) { + struct DynamicStructInfo* info = dynamic_struct_infos[i]; + if(strcmp(struct_name, info->name) == 0) { + for (size_t i1 = 0; i1 < info->count; i1++) { + if (strcmp(info->members[i1].name, member_name) == 0) { + *out_ptr = (void*) DYNAMIC_CONTAINER_OF(info->members[i1].offset, ptr); + return 0; + } + } + return -2; + } + } + return -1; +} +EXPORT_SYMBOL(sukisu_super_container_of); + From e3750ccd51d25801bd71f3e1777b45f7fadc9b06 Mon Sep 17 00:00:00 2001 From: liankong Date: Sat, 12 Apr 2025 10:35:08 +0800 Subject: [PATCH 2/7] =?UTF-8?q?=E5=AE=8C=E5=96=84super=5Faccess?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/kpm/Makefile | 8 +++++++- kernel/kpm/compact.c | 15 ++++++++++++++- kernel/kpm/super_access.c | 5 +++-- kernel/kpm/super_access.h | 39 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+), 4 deletions(-) diff --git a/kernel/kpm/Makefile b/kernel/kpm/Makefile index 10cf7d44..09fc900f 100644 --- a/kernel/kpm/Makefile +++ b/kernel/kpm/Makefile @@ -1,2 +1,8 @@ obj-y += kpm.o -obj-y += compact.o \ No newline at end of file +obj-y += compact.o +obj-y += super_access.c + +ccflags-y += -Wno-implicit-function-declaration -Wno-strict-prototypes -Wno-int-conversion -Wno-gcc-compat +ccflags-y += -Wno-declaration-after-statement -Wno-unused-function + +# End diff --git a/kernel/kpm/compact.c b/kernel/kpm/compact.c index 01f00e80..9bece752 100644 --- a/kernel/kpm/compact.c +++ b/kernel/kpm/compact.c @@ -27,6 +27,7 @@ #include "kpm.h" #include "compact.h" #include "../allowlist.h" +#include "../manager.h" unsigned long sukisu_compact_find_symbol(const char* name); @@ -49,6 +50,16 @@ int sukisu_is_uid_should_umount(uid_t uid) { return ksu_uid_should_umount(uid) ? 1 : 0; } +static +int sukisu_is_current_uid_manager() { + return is_manager(); +} + +static +uid_t sukisu_get_manager_uid() { + return ksu_manager_uid; +} + // ====================================================================== struct CompactAddressSymbol { @@ -62,7 +73,9 @@ static struct CompactAddressSymbol address_symbol [] = { { "is_run_in_sukisu_ultra", (void*)1 }, { "is_su_allow_uid", &sukisu_is_su_allow_uid }, { "get_ap_mod_exclude", &sukisu_get_ap_mod_exclude }, - { "is_uid_should_umount", &sukisu_is_uid_should_umount } + { "is_uid_should_umount", &sukisu_is_uid_should_umount }, + { "is_current_uid_manager", &sukisu_is_current_uid_manager }, + { "get_manager_uid", &sukisu_get_manager_uid } }; unsigned long sukisu_compact_find_symbol(const char* name) { diff --git a/kernel/kpm/super_access.c b/kernel/kpm/super_access.c index 166c3388..a106eb5f 100644 --- a/kernel/kpm/super_access.c +++ b/kernel/kpm/super_access.c @@ -26,7 +26,8 @@ #include #include "kpm.h" #include "compact.h" -#include // 需要包含 offsetof 宏 +#include +#include // 结构体成员元数据 struct DynamicStructMember { @@ -156,7 +157,7 @@ struct DynamicStructInfo* dynamic_struct_infos[] = { STRUCT_INFO(vfsmount), STRUCT_INFO(mnt_namespace), #ifdef CONFIG_KPROBES - STRUCT_INFO(kprobe) + STRUCT_INFO(kprobe), #endif STRUCT_INFO(vm_area_struct), STRUCT_INFO(vm_operations_struct), diff --git a/kernel/kpm/super_access.h b/kernel/kpm/super_access.h index e69de29b..2514be89 100644 --- a/kernel/kpm/super_access.h +++ b/kernel/kpm/super_access.h @@ -0,0 +1,39 @@ +#ifndef __SUKISU_SUPER_ACCESS_H +#define __SUKISU_SUPER_ACCESS_H + +#include +#include +#include "kpm.h" +#include "compact.h" + +// return 0 if successful +// return -1 if struct not defined +int sukisu_super_find_struct( + const char* struct_name, + size_t* out_size, + int* out_members +); + +// Dynamic access struct +// return 0 if successful +// return -1 if struct not defined +// return -2 if member not defined +int sukisu_super_access ( + const char* struct_name, + const char* member_name, + size_t* out_offset, + size_t* out_size +); + +// Dynamic container_of +// return 0 if success +// return -1 if current struct not defined +// return -2 if target member not defined +int sukisu_super_container_of( + const char* struct_name, + const char* member_name, + void* ptr, + void** out_ptr +); + +#endif \ No newline at end of file From 314d3ef97af3602ce5df1be219229c6595e27565 Mon Sep 17 00:00:00 2001 From: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com> Date: Sat, 12 Apr 2025 14:44:50 +0800 Subject: [PATCH 3/7] =?UTF-8?q?=E6=9B=B4=E6=96=B0Makefile=E4=BB=A5?= =?UTF-8?q?=E4=BD=BF=E7=94=A8super=5Faccess.o=E6=9B=BF=E4=BB=A3super=5Facc?= =?UTF-8?q?ess.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/kpm/Makefile | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/kernel/kpm/Makefile b/kernel/kpm/Makefile index 09fc900f..3f75542d 100644 --- a/kernel/kpm/Makefile +++ b/kernel/kpm/Makefile @@ -1,8 +1,6 @@ obj-y += kpm.o obj-y += compact.o -obj-y += super_access.c +obj-y += super_access.o ccflags-y += -Wno-implicit-function-declaration -Wno-strict-prototypes -Wno-int-conversion -Wno-gcc-compat -ccflags-y += -Wno-declaration-after-statement -Wno-unused-function - -# End +ccflags-y += -Wno-declaration-after-statement -Wno-unused-function \ No newline at end of file From d45aa8197e59c517a0e8bb5f36753156fd176b67 Mon Sep 17 00:00:00 2001 From: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com> Date: Sat, 12 Apr 2025 14:54:10 +0800 Subject: [PATCH 4/7] Remove unnecessary header file fs/mount.h and use linux/mount.h instead. --- kernel/kpm/super_access.c | 1 - 1 file changed, 1 deletion(-) diff --git a/kernel/kpm/super_access.c b/kernel/kpm/super_access.c index a106eb5f..d9313a4e 100644 --- a/kernel/kpm/super_access.c +++ b/kernel/kpm/super_access.c @@ -66,7 +66,6 @@ struct DynamicStructInfo { // ================================================================================== -#include #include // 定义元数据 From a68d5e8bbeb61830a1a6ee9dff3ae225d2bd92f5 Mon Sep 17 00:00:00 2001 From: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com> Date: Sat, 12 Apr 2025 15:02:22 +0800 Subject: [PATCH 5/7] =?UTF-8?q?=E5=9C=A8super=5Faccess.c=E4=B8=AD=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=AF=B9linux/nsproxy.h=E7=9A=84=E5=BC=95=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/kpm/super_access.c | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/kpm/super_access.c b/kernel/kpm/super_access.c index d9313a4e..1438676b 100644 --- a/kernel/kpm/super_access.c +++ b/kernel/kpm/super_access.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include From 562b9624d71d2eee6ac0f2065286798fe0f62f40 Mon Sep 17 00:00:00 2001 From: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com> Date: Sat, 12 Apr 2025 15:49:00 +0800 Subject: [PATCH 6/7] =?UTF-8?q?=E7=A7=BB=E9=99=A4=E5=AF=B9linux/nsproxy.h?= =?UTF-8?q?=E7=9A=84=E5=BC=95=E7=94=A8=EF=BC=8C=E5=B9=B6=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E5=AF=B9../fs/mount.h=E7=9A=84=E5=BC=95=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/kpm/super_access.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/kernel/kpm/super_access.c b/kernel/kpm/super_access.c index 1438676b..bc4cd768 100644 --- a/kernel/kpm/super_access.c +++ b/kernel/kpm/super_access.c @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include @@ -67,6 +66,7 @@ struct DynamicStructInfo { // ================================================================================== +#include <../fs/mount.h> #include // 定义元数据 @@ -105,7 +105,6 @@ DYNAMIC_STRUCT_BEGIN(kprobe) DEFINE_MEMBER(kprobe, pre_handler) DEFINE_MEMBER(kprobe, post_handler) DEFINE_MEMBER(kprobe, fault_handler) - DEFINE_MEMBER(kprobe, break_handler) DEFINE_MEMBER(kprobe, flags) DYNAMIC_STRUCT_END(kprobe) From d6084aeca14a9fc306e4d7bd7770c2e4cee5e4f7 Mon Sep 17 00:00:00 2001 From: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com> Date: Sat, 12 Apr 2025 16:09:13 +0800 Subject: [PATCH 7/7] =?UTF-8?q?=E5=9C=A8super=5Faccess.c=E4=B8=AD=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E5=AF=B9linux/version.h=E7=9A=84=E5=BC=95=E7=94=A8?= =?UTF-8?q?=EF=BC=8C=E5=B9=B6=E5=AE=9A=E4=B9=89KERNEL=5FVERSION=5F6=5F6?= =?UTF-8?q?=E5=AE=8F=EF=BC=8C=E4=BB=A5=E6=94=AF=E6=8C=81=E4=B8=8D=E5=90=8C?= =?UTF-8?q?=E5=86=85=E6=A0=B8=E7=89=88=E6=9C=AC=E7=9A=84=E6=9D=A1=E4=BB=B6?= =?UTF-8?q?=E7=BC=96=E8=AF=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kernel/kpm/super_access.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/kernel/kpm/super_access.c b/kernel/kpm/super_access.c index bc4cd768..1fd2491a 100644 --- a/kernel/kpm/super_access.c +++ b/kernel/kpm/super_access.c @@ -66,6 +66,11 @@ struct DynamicStructInfo { // ================================================================================== +#include + +#define KERNEL_VERSION_6_6 KERNEL_VERSION(6, 6, 0) + + #include <../fs/mount.h> #include @@ -87,27 +92,29 @@ DYNAMIC_STRUCT_BEGIN(vfsmount) DYNAMIC_STRUCT_END(vfsmount) DYNAMIC_STRUCT_BEGIN(mnt_namespace) - DEFINE_MEMBER(mnt_namespace, count) DEFINE_MEMBER(mnt_namespace, ns) DEFINE_MEMBER(mnt_namespace, root) DEFINE_MEMBER(mnt_namespace, seq) DEFINE_MEMBER(mnt_namespace, mounts) +#if LINUX_VERSION_CODE < KERNEL_VERSION_6_6 + DEFINE_MEMBER(mnt_namespace, count) +#endif DYNAMIC_STRUCT_END(mnt_namespace) #include #ifdef CONFIG_KPROBES - DYNAMIC_STRUCT_BEGIN(kprobe) DEFINE_MEMBER(kprobe, addr) DEFINE_MEMBER(kprobe, symbol_name) DEFINE_MEMBER(kprobe, offset) DEFINE_MEMBER(kprobe, pre_handler) DEFINE_MEMBER(kprobe, post_handler) +#if LINUX_VERSION_CODE < KERNEL_VERSION_6_6 DEFINE_MEMBER(kprobe, fault_handler) +#endif DEFINE_MEMBER(kprobe, flags) DYNAMIC_STRUCT_END(kprobe) - #endif #include @@ -143,7 +150,9 @@ DYNAMIC_STRUCT_BEGIN(netlink_kernel_cfg) DEFINE_MEMBER(netlink_kernel_cfg, cb_mutex) DEFINE_MEMBER(netlink_kernel_cfg, bind) DEFINE_MEMBER(netlink_kernel_cfg, unbind) +#if LINUX_VERSION_CODE < KERNEL_VERSION_6_6 DEFINE_MEMBER(netlink_kernel_cfg, compare) +#endif DYNAMIC_STRUCT_END(netlink_kernel_cfg) // =====================================================================================================================