From bce85b14c042f1815aa6c8a6ec92467ecbe55b1e Mon Sep 17 00:00:00 2001 From: bmax Date: Sun, 6 Oct 2024 15:42:31 +0800 Subject: [PATCH] fix su list, opt user init --- kernel/patch/android/gen/user_init.c | 2 +- kernel/patch/android/user_init.sh | 5 ++++- kernel/patch/android/userd.c | 8 ++++---- kernel/patch/common/sucompat.c | 20 +++++++++++--------- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/kernel/patch/android/gen/user_init.c b/kernel/patch/android/gen/user_init.c index b625bf1..4dafabd 100644 --- a/kernel/patch/android/gen/user_init.c +++ b/kernel/patch/android/gen/user_init.c @@ -1 +1 @@ -static const char user_init[] = "#!/bin/sh\n\nKPMS_DIR=\"/data/adb/ap/kpms/\"\nMAGISK_POLICY_PATH=\"/data/adb/ap/bin/magiskpolicy\"\nSUPERCMD=\"truncate\"\nMAGISK_SCTX=\"u:r:magisk:s0\"\nAPD_PATH=\"/data/adb/apd\"\n\nskey=\"$1\"\nevent=\"$2\"\n\nLOG_FILE=\"/dev/user_init_\"\"$event\"\".log\"\n\nexec >>$LOG_FILE 2>&1\n\nset -x\n\nload_modules() {\n for dir in \"$KPMS_DIR/*\"; do\n if [ ! -d \"$dir\" ]; then continue; fi\n if [ -e \"$dir/disable\" ]; then continue; fi\n main_sh=\"$dir/main.sh\"\n if [ -e \"$main_sh\" ]; then\n touch \"$dir/disable\"\n echo \"loading $dir/main.sh ...\"\n . \"$main_sh\"\n rm -f \"$dir/disable\"\n else\n echo \"Error: $main_sh not found in $dir\"\n fi\n done\n}\n\nhandle() {\n $SUPERCMD $skey event $event \"before\"\n case \"$event\" in\n \"early-init\" | \"init\" | \"late-init\") ;;\n \"post-fs-data\")\n $MAGISK_POLICY_PATH --magisk --live\n load_modules $skey $event\n $SUPERCMD $skey -Z $MAGISK_SCTX exec $APD_PATH -s $skey $event\n ;;\n \"services\")\n $SUPERCMD $skey -Z $MAGISK_SCTX exec $APD_PATH -s $skey $event\n ;;\n \"boot-completed\")\n $SUPERCMD $skey -Z $MAGISK_SCTX exec $APD_PATH -s $skey $event\n $SUPERCMD su -Z $MAGISK_SCTX exec $APD_PATH uid-listener &\n ;;\n *)\n echo \"unknown user_init event: $event\"\n ;;\n esac\n $SUPERCMD $skey event $event \"after\"\n}\n\nhandle\n"; +static const char user_init[] = "#!/bin/sh\n\nKPMS_DIR=\"/data/adb/ap/kpms/\"\nMAGISK_POLICY_PATH=\"/data/adb/ap/bin/magiskpolicy\"\nSUPERCMD=\"truncate\"\nMAGISK_SCTX=\"u:r:magisk:s0\"\nAPD_PATH=\"/data/adb/apd\"\nDEV_LOG_DIR=\"/dev/user_init_log/\"\n\nskey=\"$1\"\nevent=\"$2\"\n\nmkdir -p \"$DEV_LOG_DIR\"\n\nLOG_FILE=\"$DEV_LOG_DIR\"\"$event\"\n\nexec >>$LOG_FILE 2>&1\n\nset -x\n\nload_modules() {\n for dir in \"$KPMS_DIR/*\"; do\n if [ ! -d \"$dir\" ]; then continue; fi\n if [ -e \"$dir/disable\" ]; then continue; fi\n main_sh=\"$dir/main.sh\"\n if [ -e \"$main_sh\" ]; then\n touch \"$dir/disable\"\n echo \"loading $dir/main.sh ...\"\n . \"$main_sh\"\n rm -f \"$dir/disable\"\n else\n echo \"Error: $main_sh not found in $dir\"\n fi\n done\n}\n\nhandle() {\n $SUPERCMD $skey event $event \"before\"\n case \"$event\" in\n \"early-init\" | \"init\" | \"late-init\") ;;\n \"post-fs-data\")\n $MAGISK_POLICY_PATH --magisk --live\n load_modules $skey $event\n $SUPERCMD $skey -Z $MAGISK_SCTX exec $APD_PATH -s $skey $event\n ;;\n \"services\")\n $SUPERCMD $skey -Z $MAGISK_SCTX exec $APD_PATH -s $skey $event\n ;;\n \"boot-completed\")\n $SUPERCMD $skey -Z $MAGISK_SCTX exec $APD_PATH -s $skey $event\n $SUPERCMD su -Z $MAGISK_SCTX exec $APD_PATH uid-listener &\n ;;\n *)\n echo \"unknown user_init event: $event\"\n ;;\n esac\n $SUPERCMD $skey event $event \"after\"\n}\n\nhandle\n"; diff --git a/kernel/patch/android/user_init.sh b/kernel/patch/android/user_init.sh index 3546e26..77fe51a 100644 --- a/kernel/patch/android/user_init.sh +++ b/kernel/patch/android/user_init.sh @@ -5,11 +5,14 @@ MAGISK_POLICY_PATH="/data/adb/ap/bin/magiskpolicy" SUPERCMD="truncate" MAGISK_SCTX="u:r:magisk:s0" APD_PATH="/data/adb/apd" +DEV_LOG_DIR="/dev/user_init_log/" skey="$1" event="$2" -LOG_FILE="/dev/user_init_""$event"".log" +mkdir -p "$DEV_LOG_DIR" + +LOG_FILE="$DEV_LOG_DIR""$event" exec >>$LOG_FILE 2>&1 diff --git a/kernel/patch/android/userd.c b/kernel/patch/android/userd.c index 2c20299..a259752 100644 --- a/kernel/patch/android/userd.c +++ b/kernel/patch/android/userd.c @@ -38,12 +38,12 @@ #define ADB_FLODER "/data/adb/" #define AP_DIR "/data/adb/ap/" +#define DEV_LOG_DIR "/dev/user_init_log/" #define AP_BIN_DIR AP_DIR "bin/" #define AP_LOG_DIR AP_DIR "log/" #define AP_MAGISKPOLICY_PATH AP_BIN_DIR "magiskpolicy" #define MAGISK_SCTX "u:r:magisk:s0" #define USER_INIT_SH_PATH "/dev/user_init.sh" -#define USER_INIT_LOG "/dev/user_init*.log" #include "gen/user_init.c" @@ -62,9 +62,9 @@ static const char user_rc_data[] = { // "on property:vold.decrypt=trigger_restart_framework\n" " exec -- " SUPERCMD " su exec " USER_INIT_SH_PATH " %s services\n" "on property:sys.boot_completed=1\n" - // " rm " REPLACE_RC_FILE "\n" - // " rm " USER_INIT_SH_PATH "\n" - // " rm " USER_INIT_LOG "\n" + " rm " REPLACE_RC_FILE "\n" + " rm " USER_INIT_SH_PATH "\n" + " mv -f " DEV_LOG_DIR AP_LOG_DIR "\n" " exec -- " SUPERCMD " su exec " USER_INIT_SH_PATH " %s boot-completed\n" "" }; diff --git a/kernel/patch/common/sucompat.c b/kernel/patch/common/sucompat.c index 9a00f10..638eb7c 100644 --- a/kernel/patch/common/sucompat.c +++ b/kernel/patch/common/sucompat.c @@ -95,25 +95,27 @@ static int allow_uids_cb(struct kstorage *kstorage, void *udata) { int is_user; uid_t *out_uids; + int idx; int out_num; } *up = (typeof(up))udata; - struct su_profile *profile = (struct su_profile *)kstorage->data; + if (up->idx >= up->out_num) { + return -ENOBUFS; + } - int num = 0; + struct su_profile *profile = (struct su_profile *)kstorage->data; if (up->is_user) { - int cprc = compat_copy_to_user(up->out_uids + num, &profile->uid, sizeof(uid_t)); - logkfd("uid: %d\n", profile->uid); + int cprc = compat_copy_to_user(up->out_uids + up->idx, &profile->uid, sizeof(uid_t)); if (cprc <= 0) { logkfd("compat_copy_to_user error: %d", cprc); return cprc; } } else { - up->out_uids[num] = profile->uid; + up->out_uids[up->idx] = profile->uid; } - num++; + up->idx++; return 0; } @@ -125,8 +127,9 @@ int su_allow_uids(int is_user, uid_t *out_uids, int out_num) { int iu; uid_t *up; - int un; - } udata = { is_user, out_uids, out_num }; + int idx; + int out_num; + } udata = { is_user, out_uids, 0, out_num }; on_each_kstorage_elem(su_kstorage_gid, allow_uids_cb, &udata); return rc; } @@ -150,7 +153,6 @@ int su_allow_uid_profile(int is_user, uid_t uid, struct su_profile *out_profile) logkfd("compat_copy_to_user error: %d", rc); goto out; } - logkfd("%d %d %s\n", profile->uid, profile->to_uid, profile->scontext); } else { memcpy(out_profile, profile, sizeof(struct su_profile)); }