Exploit databaseDownload repository ZIP
GH-ced46422045980b66bac7722582b6d48
wjnkac/cve-2019-2215-vivo-y66i
Sources for CVE-2019-2215
37 sourcesPreferred order: Exploit-DB, Metasploit, then GitHub repositories by stars. Open preferred source
Repository files6
wjnkac/cve-2019-2215-vivo-y66iRevision 54b4fa98e759
- ↑..Parent directory
- kpscall15-build.sh
- kpscall15.c
- kpscall16-build.sh
- kpscall16.c
- kpscall17-build.sh
- kpscall17.c
Files from this repository share one source choice. Supporting files may cover other vulnerabilities.
main/src/lkm/archive/kpscall16.c
C · 37,882 bytes/*
* kpscall16.c — Step 4.3a:kstorage 最小实现 + 独立回环验证
* (vivo Y66i / msm-3.18.31-perf)
*
* 用户口径(2026-11-08 18:39):依赖顺序是 **kstorage → superkey → allowlist**;
* kstorage 最底层、要先独立验证「存进去能读出来、并发下不丢」,再接 superkey。
*
* ★ 订正:KP 的 `kstorage.c` 不是「纯 C、无内核接口依赖」——它依赖
* vmalloc/vfree/call_rcu/memdup_user/__copy_from_user/__copy_to_user/
* kmalloc/kfree/memcpy。3.18 符号表全有(T),但**不在权威 CRC 池**
* (evbug 的 11 条只有 module_layout、input_ 系列、kmalloc_caches、
* kmem_cache_alloc_trace、kfree、printk)⇒ 正常 UND 会 `no symbol version`。
* ⇒ 本模块用**最小实现**:
* 静态数组(4 组 × 8 槽 × 192B)+ spinlock + 硬编码
* `__copy_from_user(0xffffffc000336f40)` / `__copy_to_user(0xffffffc000337080)`
* **零新增 UND(只 printk)**,`mkloadable318.py` 原样可用。
* 这与 KP 的 RCU + vmalloc 实现在**语义上等价**(覆盖写、offset/len、
* list/remove),差别只在容量固定、无 RCU 回收;足够先把 superkey/
* allowlist 的存储形状定下来。
*
* 命令(uapi/scdefs.h):
* KSTORAGE_ALLOC_GROUP 0x1040 → 新 gid
* KSTORAGE_WRITE 0x1041 → a2=gid a3=did a4=user_ptr a5=(off<<32)|len
* KSTORAGE_READ 0x1042 → 同上,返回实际读到的字节数
* KSTORAGE_LIST_IDS 0x1043 → a2=gid a3=user buf a4=buf 容量,返回条数
* KSTORAGE_REMOVE 0x1044 → a2=gid a3=did
*
* ★ 所有用户指针都走硬编码 `__copy_from_user/__copy_to_user`(返回未拷贝字节数);
* 读用户数据先落到栈上 tmp,**在锁外**做(copy_*_user 可能睡眠)。
* ★ spinlock 若在编译树里被展开成 `_raw_spin_lock`(debug 配置),会多一个 UND;
* 编译后先看 UND 清单,必要时退化为 local_irq_save + 原子自旋。
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/errno.h>
#define DO_SYS_TRUNCATE 0xffffffc0001ad22cUL
/* ★ sucompat 第一步:伪装 su 路径的 "-x" 探测(sys_faccessat) */
#define SYSCALL_FACCESSAT 0xffffffc0001ad628UL
/* ★ B(sucompat 第二步):sys_call_table=0xffffffc000da7000,
* table[221]=sys_execve=0xffffffc0001b5f20;3.18 无 execveat(表项为 0) */
#define SYSCALL_EXECVE 0xffffffc0001b5f20UL
/* ★ 域切换:security_secctx_to_secid("u:r:su:s0") → 写 cred->security->sid
* cred->security @ +0x70;task_security_struct.sid @ +4(osid 在 +0) */
#define A_SECCTX_TO_SECID 0xffffffc0002d95a4UL
#define OFF_CRED_SECURITY 0x70UL
#define OFF_SEC_SID 4UL
#define SU_SCTX_STR "u:r:su:s0"
#define SU_SCTX_LEN 10
#define EXECVE_RET (SYSCALL_EXECVE + 0x10UL)
#define FACCESSAT_RET (SYSCALL_FACCESSAT + 0x10UL)
#define SH_PATH_STR "/system/bin/sh"
#define LEGACY_SU_PATH "/system/bin/su"
#define SC_RET (DO_SYS_TRUNCATE + 0x10UL)
#define A_PREPARE_KERNEL_CRED 0xffffffc0000c0f50UL
#define A_COMMIT_CREDS 0xffffffc0000c0bb0UL
/* ★ kpscall16 新增:prepare_creds / abort_creds(都在 kallsyms 的 cred 一族里) */
#define A_PREPARE_CREDS 0xffffffc0000c0aa4UL
#define A_ABORT_CREDS 0xffffffc0000c0a64UL
/* ★ kpscall16 新增:groups 用内核自己的 API,不猜 group_info 偏移 */
#define A_GROUPS_ALLOC 0xffffffc0000c21e4UL
#define A_SET_CURRENT_GROUPS 0xffffffc0000c22c4UL
/* ★ KP 的 full_cap = CAP_FULL_SET(3.18:两个 32 位字全 1),不是 38 位掩码 */
#define KP_FULL_CAP 0xffffffffffffffffUL
#define A_COPY_FROM_USER 0xffffffc000336f40UL
/* ★ kpscall16:内核自己的 setresuid(0xffffffc0000b4434,kallsyms T) */
#define A_SYS_SETRESUID 0xffffffc0000b4434UL
#define A_COPY_TO_USER 0xffffffc000337080UL
#define OFF_TASK_CRED 0x4c8UL
#define OFF_CRED_UID 0x04UL /* ★ 订正:uid 在 +0x04(+0x14 是 euid) */
#define SC_MAGIC_HI16 0x1158U
#define SC_HELLO 0x1000U
#define SC_BUILD_TIME 0x1007U
#define SC_KP_VER 0x1008U
#define SC_KERNEL_VER 0x1009U
#define SC_SU 0x1010U
#define SC_KS_ALLOC 0x1040U
#define SC_KS_WRITE 0x1041U
#define SC_KS_READ 0x1042U
#define SC_KS_LIST 0x1043U
#define SC_KS_REMOVE 0x1044U
#define SC_SKEY_GET 0x100aU
#define SC_SKEY_SET 0x100bU
#define SC_SU_GET_SAFEMODE 0x1112U
#define SC_DUMP_CRED 0x1ff0U
/* ---- 临时 LKM 形态的 uid 授权(对齐 KP before() + kpscauth.h) ----
* 没有「修补 boot.img 时输入 SuperKey」环节 ⇒ 不以 key 为凭,改以 uid 为凭。
* 未授权调用直接返回 -ENOENT(等价于走原生 truncate),supercall 入口不可见。 */
#define SU_UID_MAX 32
#define MANAGER_UID_INVALID 0xffffffffU
#define SC_SET_MANAGER_UID 0x1ff1U
/* 现场判 PTE 用:内核态 8B 读/写(非白名单命令 ⇒ 仅 is_authed)。
* 不用包内 probe_rd(那是完整 CVE 读原语,跑一次 2m+)。 */
#define SC_DBG_R64 0x1ff2U
#define SC_DBG_W64 0x1ff3U /* 引导:由 root/shell 指定管理端 uid */
#define SC_SU_GRANT_UID 0x1100U
#define SC_SU_REVOKE_UID 0x1101U
#define SC_SU_NUMS 0x1102U
#define SC_SU_LIST 0x1103U
#define SC_SU_PROFILE 0x1104U
#define SC_SU_GET_PATH 0x1110U
/* ---- ② 对表补全:App 其它页面会调、但 kpscall12 未实现的命令 ---- */
#define SC_KLOG 0x1004U
#define SC_SU_TASK 0x1011U
#define SC_KPM_LOAD 0x1020U
#define SC_KPM_UNLOAD 0x1021U
#define SC_KPM_CONTROL 0x1022U
#define SC_KPM_NUMS 0x1030U
#define SC_KPM_LIST 0x1031U
#define SC_KPM_INFO 0x1032U
#define SC_KS_REMOVE_GROUP 0x1045U
#define SC_CONTROL_FEATURE 0x1046U
#define SC_SU_GET_ALLOW_SCTX 0x1105U
#define SC_SU_SET_ALLOW_SCTX 0x1106U
#define SC_SU_RESET_PATH 0x1111U /* 调试:dump 当前 cred 0xa0 字节 */
#define SC_HELLO_MAGIC 0x11581158UL
#define SC_BUILD_TIME_VAL 0x20261108UL
#define SC_KP_VER_VAL 0x0d09UL
#define SC_KERNEL_VER_VAL 0x3121fUL
#define SC_HELLO_MAGIC_STR "0x11581158"
/* 临时 LKM 注入形态:没有「修补 boot.img 时输入 SuperKey」这一环节,
* KP 新版管理端默认用 "su" 作为 key。对齐它。 */
#define KP_BUILD_TIME_STR "2026-11-08 22:00:00"
/* ---- kstorage 最小实现 ---- */
#define KS_GROUPS 4
#define KS_SLOTS 8
#define KS_DLEN 192 /* 够 superkey(0x40) 与 su_profile(0x68) */
struct ks_ent { long did; int dlen; unsigned char data[KS_DLEN]; };
static struct ks_ent ks_tab[KS_GROUPS][KS_SLOTS];
static int ks_used[KS_GROUPS];
static int ks_alloc_next;
typedef void *(*pkcred_t)(void *daemon);
typedef void *(*prepcreds_t)(void);
typedef void (*abortcreds_t)(void *newcred);
typedef int (*commit_t)(void *newcred);
typedef void *(*gall_t)(int gidsetsize);
typedef int (*scg_t)(void *group_info);
typedef unsigned long (*ucopy_t)(void *to, const void *from, unsigned long n);
u64 kphook_jumpback = SC_RET;
u64 kphook_jumpback_fa = FACCESSAT_RET;
u64 kphook_jumpback_ex = EXECVE_RET;
/* ★ 内联自旋锁 v2(2026-11-08 19:10 修:v1 缺 preempt_disable,在 PREEMPT 内核上
* 持锁者被抢占 ⇒ 其他 CPU 永久自旋,实测把 ks_test 卡成 R 状态吃掉一个核。
* v2 = 关中断(msr daifset #2)+ 有限次数自旋 + 超时返回 -EBUSY,永不死锁。 */
static volatile int ks_busy;
static inline int ks_lock(void)
{
int val, one = 1, n;
__asm__ __volatile__("msr daifset, #2" ::: "memory");
/* ★ v3:每次 asm 块只尝试【一次】,失败回 C 层计数 —— v2 把 `cbnz stxr, 1b`
* 写在块内,两 CPU 抢同一地址时会在 asm 里活锁,C 层超时永远不触发。 */
for (n = 0; n < 200000; n++) {
__asm__ __volatile__(
"ldaxr %w0, [%1]\n\t"
"cbnz %w0, 1f\n\t"
"stxr %w0, %w2, [%1]\n\t"
"b 2f\n\t"
"1: mov %w0, #2\n\t"
"2:\n\t"
: "=&r"(val) : "r"(&ks_busy), "r"(one) : "memory");
if (val == 0)
return 0; /* 拿到锁 */
/* val==1: stxr 竞争失败;val==2: 已被持有 —— 都回 C 层重试 */
}
__asm__ __volatile__("msr daifclr, #2" ::: "memory");
return -1;
}
static inline void ks_unlock(void)
{
__asm__ __volatile__(
"stlr wzr, [%0]\n\t"
"msr daifclr, #2\n\t"
:: "r"(&ks_busy) : "memory");
}
/* volatile 逐字节拷贝:避免 GCC 优化成 memcpy/memmove(两者都不在 CRC 池) */
static void ks_copy(volatile unsigned char *d, const volatile unsigned char *s, int n)
{
int i;
for (i = 0; i < n; i++)
d[i] = s[i];
}
/* 当前内核栈页基址(thread_info 在页首,flags 在 +0) */
static inline unsigned long kp_stack_base(void)
{
unsigned long b;
__asm__ __volatile__("mov %0, sp\n\tand %0, %0, #0xffffffffffffc000\n\t"
: "=r"(b) : : "memory");
return b;
}
static inline void *kp_current_task(void)
{
void *t;
__asm__ __volatile__(
"mov %0, sp\n\t"
"and %0, %0, #0xffffffffffffc000\n\t"
"ldr %0, [%0, #16]\n\t"
: "=r"(t) : : "memory");
return t;
}
static long ks_write(long gid, long did, u64 uptr, int off, int len)
{
unsigned char tmp[KS_DLEN];
ucopy_t cfu = (ucopy_t)A_COPY_FROM_USER;
unsigned long left;
int i, slot = -1;
if (gid < 0 || gid >= KS_GROUPS) return -EINVAL;
if (len <= 0 || len > KS_DLEN) return -EINVAL;
if (off < 0 || off + len > KS_DLEN) return -EINVAL;
if (!uptr) return -EFAULT;
/* 锁外读用户数据(可能睡眠)。
* ★ 2026-11-08 19:25 对齐 KP 语义:offset 是【源】偏移(`data + offset`),
* 写入是【整条替换】,落地后 dlen 就是 len —— 不是"目标偏移 + 只增不减"。 */
left = cfu(tmp, (const void *)(uptr + (unsigned long)off), (unsigned long)len);
if (left)
return -EFAULT;
if (ks_lock())
return -EBUSY;
for (i = 0; i < ks_used[gid]; i++) {
if (ks_tab[gid][i].did == did) { slot = i; break; }
}
if (slot < 0) {
if (ks_used[gid] >= KS_SLOTS) { ks_unlock(); return -ENOSPC; }
slot = ks_used[gid]++;
ks_tab[gid][slot].did = did;
ks_tab[gid][slot].dlen = 0;
}
{
volatile unsigned char *dst = ks_tab[gid][slot].data;
const volatile unsigned char *src = tmp;
for (i = 0; i < len; i++)
dst[i] = src[i];
}
ks_tab[gid][slot].dlen = len; /* ★ 整条替换:长度就是 len */
ks_unlock();
return 0;
}
static long ks_read(long gid, long did, u64 uptr, int off, int len)
{
unsigned char tmp[KS_DLEN];
ucopy_t ctu = (ucopy_t)A_COPY_TO_USER;
unsigned long left;
int i, slot = -1, n;
if (gid < 0 || gid >= KS_GROUPS) return -EINVAL;
if (len <= 0 || len > KS_DLEN) return -EINVAL;
if (off < 0 || off > KS_DLEN) return -EINVAL;
if (!uptr) return -EFAULT;
if (ks_lock())
return -EBUSY;
for (i = 0; i < ks_used[gid]; i++) {
if (ks_tab[gid][i].did == did) { slot = i; break; }
}
if (slot < 0) { ks_unlock(); return -ENOENT; }
n = ks_tab[gid][slot].dlen - off;
if (n < 0) n = 0;
if (n > len) n = len;
{
volatile unsigned char *dst = tmp;
const volatile unsigned char *src = ks_tab[gid][slot].data;
for (i = 0; i < n; i++)
dst[i] = src[off + i];
}
ks_unlock();
if (n == 0)
return 0;
left = ctu((void *)uptr, tmp, (unsigned long)n);
if (left)
return -EFAULT;
return n;
}
/* ---- superkey:存 kstorage 的固定 gid/did(gid 3 专用,ALLOC 只到 2) ---- */
#define SKEY_GID 3
#define SKEY_DID 0L
#define SKEY_LEN 0x40
/* 内核侧读 kstorage 到内核 buffer(不碰用户态) */
static int ks_get_copy(int gid, long did, unsigned char *out, int maxlen)
{
int i, slot = -1, n;
if (gid < 0 || gid >= KS_GROUPS) return -EINVAL;
if (ks_lock()) return -EBUSY;
for (i = 0; i < ks_used[gid]; i++)
if (ks_tab[gid][i].did == did) { slot = i; break; }
if (slot < 0) { ks_unlock(); return -ENOENT; }
n = ks_tab[gid][slot].dlen;
if (n > maxlen) n = maxlen;
{
volatile unsigned char *d = out;
const volatile unsigned char *src = ks_tab[gid][slot].data;
for (i = 0; i < n; i++) d[i] = src[i];
}
ks_unlock();
return n;
}
/* 内核侧写 kstorage(整条替换) */
static int ks_put_kernel(int gid, long did, const unsigned char *in, int len)
{
int i, slot = -1;
if (gid < 0 || gid >= KS_GROUPS) return -EINVAL;
if (len <= 0 || len > KS_DLEN) return -EINVAL;
if (ks_lock()) return -EBUSY;
for (i = 0; i < ks_used[gid]; i++)
if (ks_tab[gid][i].did == did) { slot = i; break; }
if (slot < 0) {
if (ks_used[gid] >= KS_SLOTS) { ks_unlock(); return -ENOSPC; }
slot = ks_used[gid]++;
}
{
volatile unsigned char *d = ks_tab[gid][slot].data;
const volatile unsigned char *src = in;
for (i = 0; i < len; i++) d[i] = src[i];
}
ks_tab[gid][slot].did = did;
ks_tab[gid][slot].dlen = len;
ks_unlock();
return 0;
}
/* 读用户态 key 字符串(最多 SKEY_LEN),返回长度;<=0 表示空/错误 */
static int skey_read_user(u64 uptr, unsigned char *out)
{
ucopy_t cfu = (ucopy_t)A_COPY_FROM_USER;
int i;
if (!uptr) return 0;
if (cfu(out, (const void *)uptr, SKEY_LEN)) return -EFAULT;
for (i = 0; i < SKEY_LEN; i++)
if (!out[i]) break;
return i;
}
/* ---- uid 授权表(.data/.bss,静态数组,不引入新 UND) ---- */
static u32 su_uids[SU_UID_MAX];
/* su 路径与 all_allow_sctx:临时形态只做「可读写」,不做真实路径/域切换 */
static char su_path_buf[128] = { '/','s','y','s','t','e','m','/','b','i','n','/','k','p', 0 };
static char allow_sctx_buf[0x60];
/* 把 src(NUL 结尾)写进用户 buf,返回字符串长度;不引入 memset/memcpy */
static int kstr_eq(const char *a, const char *b)
{
int i = 0;
while (a[i] && b[i]) { if (a[i] != b[i]) return -1; i++; }
return a[i] == b[i] ? 0 : -1;
}
static long kstr_to_user(u64 uptr, long ulen, const volatile char *src)
{
long n = 0;
ucopy_t ctu = (ucopy_t)A_COPY_TO_USER;
if (!uptr || ulen <= 0) return -EINVAL;
while (src[n]) n++;
n++; /* 含 NUL */
if (n > ulen) n = ulen;
if (ctu((void *)uptr, (const void *)(unsigned long)src, (unsigned long)n)) return -EFAULT;
return n - 1;
}
/* 从用户态拷字符串到 volatile 内核缓冲(手写循环,防优化成 memset/strcpy) */
static long kstr_from_user(volatile char *dst, int cap, u64 uptr)
{
char tmp[128];
ucopy_t cfu = (ucopy_t)A_COPY_FROM_USER;
int i, n = cap - 1;
if (!uptr) return -EINVAL;
if (n > 127) n = 127;
{ volatile char *tp = tmp; for (i = 0; i <= n; i++) tp[i] = 0; }
if (cfu(tmp, (const void *)uptr, (unsigned long)n)) return -EFAULT;
tmp[n] = 0;
for (i = 0; i < cap; i++) { dst[i] = tmp[i]; if (!tmp[i]) break; }
dst[cap - 1] = 0;
return 0;
}
static int su_uid_n;
static u32 manager_uid = MANAGER_UID_INVALID;
static int is_su_allow_uid(u32 u)
{
int i;
for (i = 0; i < su_uid_n; i++)
if (su_uids[i] == u) return 1;
return 0;
}
static int is_manager_uid(u32 u)
{
return manager_uid != MANAGER_UID_INVALID && manager_uid == u;
}
/* 照抄 kernel/include/kpscauth.h:未 is_authed 的已授权 app 只能发这些命令 */
static int kp_sc_allowed_for_su(u32 c)
{
switch (c) {
case SC_HELLO:
case SC_KP_VER:
case SC_KERNEL_VER:
case SC_BUILD_TIME:
case SC_SU:
case SC_SU_PROFILE:
case SC_SU_GET_PATH:
case SC_SU_GET_SAFEMODE:
return 1;
}
return 0;
}
/* a0 传进来的 key 是否等于当前存的关键字;1=匹配 */
static int skey_match(u64 uptr)
{
unsigned char in[SKEY_LEN], cur[SKEY_LEN];
int n, cn, i;
n = skey_read_user(uptr, in);
if (n <= 0) return 0;
cn = ks_get_copy(SKEY_GID, SKEY_DID, cur, SKEY_LEN);
if (cn <= 0 || cn != n) return 0;
for (i = 0; i < n; i++)
if (in[i] != cur[i]) return 0;
return 1;
}
/* ---- sucompat path probe ----
* 返回 1 = 命中且调用者已授权 ⇒ 让 faccessat 直接返回 0(伪装 "-x" 成立)
* 返回 0 = 未命中 / 未授权 ⇒ 走原 syscall(真实 ENOENT)
* 不伪造 stat 结构、不写用户栈、不改参数,风险最低。 */
noinline long kpsc_path_dispatch(u64 ufilename, u64 mode)
{
char path[128];
volatile char *vp = path;
ucopy_t cfu = (ucopy_t)A_COPY_FROM_USER;
void *t = kp_current_task();
void *c = *(void **)((char *)t + OFF_TASK_CRED);
u32 uid = c ? *(u32 *)((char *)c + OFF_CRED_UID) : MANAGER_UID_INVALID;
int i, hit;
uid = c ? *(u32 *)((char *)c + OFF_CRED_UID) : MANAGER_UID_INVALID;
if (!(uid == 0 || uid == 2000 || is_manager_uid(uid) || is_su_allow_uid(uid)))
return 0; /* ★ 未授权:路径不存在 */
if (!ufilename) return 0;
for (i = 0; i < 128; i++) vp[i] = 0;
/* 只拷 32B:su 路径最长 /debug_ramdisk/su(17B);请求过长会跨页导致
* copy_from_user 返回未拷贝字节数 >0,被误判为失败 */
if (cfu(path, (const void *)ufilename, 32)) return 0;
path[31] = 0;
hit = 0;
{ /* 默认 su_path_buf 与 legacy /system/bin/su 都算 */
const volatile char *a = su_path_buf;
const volatile char *b = LEGACY_SU_PATH;
if (!kstr_eq(path, (const char *)(unsigned long)a)) hit = 1;
if (!hit && !kstr_eq(path, (const char *)(unsigned long)b)) hit = 1;
}
if (!hit) return 0;
return 1;
}
/* ---- B:execve 路径重定向 ----
* 命中:① sys_setresuid(0,0,0) 提权 ② 把 "/system/bin/sh" 写到用户栈
* ③ 返回新的用户态 filename 指针(trampoline 会写回 x0)
* 未命中:返回 0(保持原 x0)
* ★ 不能把 x0 换成本模块 rodata 地址:getname() 之后 do_execveat_common 会
* kfree(bprm->filename),free 到模块地址必 panic。写用户栈则完全对齐 KP
* handle_before_execve 的 copy_to_user_stack 思路。 */
noinline u64 kpsc_exec_dispatch(u64 ufilename)
{
typedef long (*setresuid_t)(u32, u32, u32);
ucopy_t ctu = (ucopy_t)A_COPY_TO_USER;
static const volatile char sh[] = SH_PATH_STR;
unsigned long usp;
unsigned long dst;
u64 n;
if (!kpsc_path_dispatch(ufilename, 0)) return 0; /* 已授权 + su 路径 */
{ setresuid_t sru = (setresuid_t)A_SYS_SETRESUID; sru(0, 0, 0); }
/* ★ 落到 u:r:su:s0(KP commit_su 的做法):内核自己把上下文解析成 sid,
* 再写进**新 cred**(sys_setresuid 已替换 cred,security blob 是独立副本)
* 的 task_security_struct.sid。只改 selinux blob,不动 uid/caps 字段。 */
{
typedef int (*s2s_t)(const char *, u32, u32 *);
s2s_t s2s = (s2s_t)A_SECCTX_TO_SECID;
static const volatile char suctx[] = SU_SCTX_STR;
u32 sid = 0;
int src;
src = s2s((const char *)(unsigned long)suctx, SU_SCTX_LEN, &sid);
if (!src && sid) {
void *t2 = kp_current_task();
void *c2 = *(void **)((char *)t2 + OFF_TASK_CRED);
void *sec = c2 ? *(void **)((char *)c2 + OFF_CRED_SECURITY) : 0;
if (sec) {
*(u32 *)((char *)sec + OFF_SEC_SID) = sid;
printk(KERN_INFO "kpscall16: sid -> %u (%s) rc=0\n", sid, SU_SCTX_STR);
} else {
printk(KERN_INFO "kpscall16: no security blob\n");
}
} else {
printk(KERN_INFO "kpscall16: secctx_to_secid rc=%d sid=%u (keep current domain)\n", src, sid);
}
}
__asm__ __volatile__("mrs %0, sp_el0" : "=r"(usp) : : "memory");
dst = (usp - 32UL) & ~0xFUL;
/* 16 字节含 NUL:"/system/bin/sh" 是 14 字符 */
n = 16;
if (ctu((void *)dst, (const void *)(unsigned long)sh, n)) return 0;
return (u64)dst;
}
noinline long kpsc_dispatch(u64 a0, u64 a1, u64 a2, u64 a3, u64 a4, u64 a5)
{
u32 cmd = (u32)(a1 & 0xFFFF);
void *tsk, *cred;
u32 uid;
prepcreds_t prep;
abortcreds_t ab;
commit_t cc;
gall_t ga;
scg_t sg;
void *newc;
int rc, k, mode;
int is_authed, is_trusted;
u32 cur_uid_saved;
long gid, did, off, len;
(void)a0;
/* ★ 对齐 KP before():先按调用者 uid 判定是否可见。
* 临时形态无预置 SuperKey,is_authed 只来自「本地引导者(root/shell)」或管理端 uid。 */
{
void *at = kp_current_task();
void *ac = *(void **)((char *)at + OFF_TASK_CRED);
u32 cur_uid = ac ? *(u32 *)((char *)ac + OFF_CRED_UID) : MANAGER_UID_INVALID;
is_authed = 0; is_trusted = 0;
if (cur_uid == 0 || cur_uid == 2000) { /* 设备持有者/调试链 */
is_authed = 1; is_trusted = 1;
} else if (is_manager_uid(cur_uid)) {
is_authed = 1; is_trusted = 1;
} else if (is_su_allow_uid(cur_uid)) {
is_trusted = 1;
}
if (!is_trusted) return -ENOENT; /* 未授权 ⇒ supercall 入口不可见 */
if (!is_authed && !kp_sc_allowed_for_su(cmd)) return -EPERM;
cur_uid_saved = cur_uid;
}
gid = (long)a2;
did = (long)a3;
off = (long)((a5 >> 32) & 0xFFFFFFFFUL);
len = (long)(a5 & 0xFFFFFFFFUL);
switch (cmd) {
case SC_HELLO: return (long)SC_HELLO_MAGIC;
case SC_BUILD_TIME: {
/* App 侧 sc_get_build_time(key, buf, len):a2=buf, a3=len。
* 必须把字符串写回用户 buf,只返回数值会让 installedKPTime() 为空。 */
static const char bt[] = KP_BUILD_TIME_STR;
unsigned long n = sizeof(bt);
ucopy_t ctu = (ucopy_t)A_COPY_TO_USER;
if (!a2 || a3 <= 0) return -EINVAL;
if (n > (unsigned long)a3) n = (unsigned long)a3;
if (ctu((void *)a2, bt, n)) return -EFAULT;
return (long)n - 1;
}
case SC_KP_VER: return (long)SC_KP_VER_VAL;
case SC_KERNEL_VER: return (long)SC_KERNEL_VER_VAL;
case SC_KS_ALLOC:
if (ks_alloc_next >= KS_GROUPS - 1) /* gid 3 保留给 superkey */
return -ENOSPC;
return ks_alloc_next++;
case SC_KS_WRITE:
return ks_write(gid, did, a4, (int)off, (int)len);
case SC_KS_READ:
return ks_read(gid, did, a4, (int)off, (int)len);
case SC_KS_LIST: {
int i, n = 0;
ucopy_t ctu = (ucopy_t)A_COPY_TO_USER;
long ids[KS_SLOTS];
if (gid < 0 || gid >= KS_GROUPS) return -EINVAL;
if (ks_lock())
return -EBUSY;
for (i = 0; i < ks_used[gid] && n < KS_SLOTS; i++)
ids[n++] = ks_tab[gid][i].did;
ks_unlock();
if (n > 0 && a3 && ctu((void *)a3, ids, (unsigned long)(n * (int)sizeof(long))))
return -EFAULT;
return n;
}
case SC_KS_REMOVE: {
int i, slot = -1;
if (gid < 0 || gid >= KS_GROUPS) return -EINVAL;
if (ks_lock())
return -EBUSY;
for (i = 0; i < ks_used[gid]; i++)
if (ks_tab[gid][i].did == did) { slot = i; break; }
if (slot < 0) { ks_unlock(); return -ENOENT; }
for (i = slot; i < ks_used[gid] - 1; i++) {
ks_tab[gid][i].did = ks_tab[gid][i + 1].did;
ks_tab[gid][i].dlen = ks_tab[gid][i + 1].dlen;
ks_copy(ks_tab[gid][i].data, ks_tab[gid][i + 1].data, KS_DLEN);
}
ks_used[gid]--;
ks_unlock();
return 0;
}
/* ---- uid 授权管理:对齐 KP supercall() 第二组,需要 is_authed ---- */
case SC_DBG_R64: {
volatile u64 *kp = (volatile u64 *)a2;
u64 v;
if (!a2 || !is_authed) return -EPERM;
v = *kp;
printk(KERN_INFO "kpscall16: R64 [%016llx] = %016llx\n", (unsigned long long)a2,
(unsigned long long)v);
return (long)v;
}
case SC_DBG_W64: {
volatile u64 *kp = (volatile u64 *)a2;
u64 v;
if (!a2 || !is_authed) return -EPERM;
v = *kp;
*kp = a3;
printk(KERN_INFO "kpscall16: W64 [%016llx] %016llx -> %016llx\n",
(unsigned long long)a2, (unsigned long long)v, (unsigned long long)a3);
return 0;
}
case SC_SET_MANAGER_UID:
if (!is_authed) return -EPERM;
manager_uid = (u32)a2;
printk(KERN_INFO "kpscall16: manager uid=%u set by caller uid=%u\n",
manager_uid, cur_uid_saved);
return 0;
case SC_SU_GRANT_UID: {
u32 prof[2] = { 0, 0 };
ucopy_t cfu = (ucopy_t)A_COPY_FROM_USER;
u32 target;
if (!is_authed) return -EPERM;
if (!a2 || cfu(prof, (const void *)a2, 8)) return -EFAULT;
target = prof[0];
if (!target || target == MANAGER_UID_INVALID) return -EINVAL;
if (is_su_allow_uid(target)) return 0;
if (su_uid_n >= SU_UID_MAX) return -ENOSPC;
su_uids[su_uid_n++] = target;
printk(KERN_INFO "kpscall16: grant uid=%u (total=%d)\n", target, su_uid_n);
return 0;
}
case SC_SU_REVOKE_UID: {
u32 target = (u32)a2;
int i, j;
if (!is_authed) return -EPERM;
for (i = 0; i < su_uid_n; i++) {
if (su_uids[i] == target) {
for (j = i; j + 1 < su_uid_n; j++) su_uids[j] = su_uids[j + 1];
su_uid_n--;
return 0;
}
}
return -ENOENT;
}
case SC_SU_NUMS:
if (!is_authed) return -EPERM;
return (long)su_uid_n;
case SC_SU_LIST: {
ucopy_t ctu = (ucopy_t)A_COPY_TO_USER;
int n = su_uid_n;
if (!is_authed) return -EPERM;
if (a3 > 0 && n > (int)a3) n = (int)a3;
if (!a2 || n <= 0) return 0;
if (ctu((void *)a2, su_uids, (unsigned long)n * 4)) return -EFAULT;
return (long)n;
}
case SC_SU_PROFILE: {
unsigned char out[0x68];
ucopy_t ctu = (ucopy_t)A_COPY_TO_USER;
int i;
if ((u32)a2 != cur_uid_saved && !is_authed) return -EPERM;
if (!a3) return -EINVAL;
/* volatile 写:阻止编译器把清零循环优化成 memset(3.18 UND 只许 printk) */
{ volatile unsigned char *op = out;
for (i = 0; i < 0x68; i++) op[i] = 0; }
*(u32 *)(out + 0) = (u32)a2; /* uid */
*(u32 *)(out + 4) = is_su_allow_uid((u32)a2) ? 0 : 0xffffffffU; /* to_uid */
{ /* 域拿不到(§34.2),如实回报 shell 域 */
static const volatile char sc[] = "u:r:shell:s0";
for (i = 0; sc[i]; i++) out[8 + i] = sc[i];
}
if (ctu((void *)a3, out, sizeof(out))) return -EFAULT;
return 0;
}
case SC_SU_GET_PATH:
return kstr_to_user(a2, (long)a3, su_path_buf);
/* ---- ② 对表补全:语义默认值,不引入内核机制 ---- */
case SC_KLOG: {
char m[128];
ucopy_t cfu = (ucopy_t)A_COPY_FROM_USER;
int i;
{ volatile char *mp = m; for (i = 0; i < 128; i++) mp[i] = 0; }
if (a2) cfu(m, (const void *)a2, 127);
printk(KERN_INFO "kpscall16: klog: %s\n", m);
return 0;
}
case SC_SU_TASK: /* 按 pid 提权:临时形态不支持 */
case SC_KPM_LOAD:
case SC_KPM_UNLOAD:
case SC_KPM_CONTROL:
return -ENOSYS;
case SC_KPM_NUMS:
return 0; /* 没有 KPM 子系统 */
case SC_KPM_LIST: {
ucopy_t ctu = (ucopy_t)A_COPY_TO_USER;
char z = 0;
if (a2 && a3 > 0 && ctu((void *)a2, &z, 1)) return -EFAULT;
return 0;
}
case SC_KPM_INFO:
return -ENOENT;
case SC_KS_REMOVE_GROUP: {
int i;
if (gid < 0 || gid >= KS_GROUPS) return -EINVAL;
if (ks_lock()) return -EBUSY;
for (i = 0; i < KS_SLOTS; i++) {
ks_tab[gid][i].did = 0;
ks_tab[gid][i].dlen = 0;
}
ks_used[gid] = 0;
ks_unlock();
return 0;
}
case SC_CONTROL_FEATURE:
return 0; /* 空实现:临时形态无可切换 feature */
case SC_SU_GET_ALLOW_SCTX:
return kstr_to_user(a2, (long)a3, allow_sctx_buf);
case SC_SU_SET_ALLOW_SCTX:
return kstr_from_user(allow_sctx_buf, 0x60, a2);
case SC_SU_RESET_PATH:
return kstr_from_user(su_path_buf, 128, a2);
case SC_SKEY_GET: {
unsigned char cur[SKEY_LEN];
int cn = ks_get_copy(SKEY_GID, SKEY_DID, cur, SKEY_LEN);
if (cn <= 0)
return -ENOENT; /* 还没设置过 */
if (!skey_match(a0)) /* a0 = 调用方传的 key */
return -EPERM;
if (!a2 || (int)a3 < SKEY_LEN)
return -EINVAL;
{
ucopy_t ctu = (ucopy_t)A_COPY_TO_USER;
if (ctu((void *)a2, cur, SKEY_LEN))
return -EFAULT;
}
return SKEY_LEN;
}
case SC_SKEY_SET: {
unsigned char nk[SKEY_LEN];
int n = skey_read_user(a2, nk); /* a2 = new_key */
unsigned char cur[SKEY_LEN];
int cn;
if (n <= 0)
return -EINVAL;
cn = ks_get_copy(SKEY_GID, SKEY_DID, cur, SKEY_LEN);
/* 首次(无 key)信任;已有 key 时必须用旧 key 通过鉴权 */
if (cn > 0 && !skey_match(a0))
return -EPERM;
return ks_put_kernel(SKEY_GID, SKEY_DID, nk, n);
}
case SC_SU_GET_SAFEMODE:
/* apd 的 is_safe_mode:返 0 = 非安全模式;非 0 会被判成 safemode 并跳过后续动作 */
return 0;
case SC_DUMP_CRED: {
/* 调试用:把当前进程 cred 的 0xa0 字节按 5 个 u64 一行打出来,供逐字段对账 */
void *ct = kp_current_task();
void *cc2 = *(void **)((char *)ct + OFF_TASK_CRED);
int q;
printk(KERN_INFO "kpscall16: CREDDUMP ptr=0x%px\n", cc2);
for (q = 0; q < 20; q++) {
u64 v = *(u64 *)((char *)cc2 + (u32)q * 8);
if ((q % 5) == 0)
printk(KERN_INFO "kpscall16: %02x:", q * 8);
printk(KERN_INFO " %016llx", (unsigned long long)v);
if ((q % 5) == 4)
printk(KERN_INFO "\n");
}
return 0;
}
case SC_SU:
break;
default:
return -EINVAL;
}
/* ★ 鉴权已在 dispatch 入口按 uid 完成(对齐 KP before());
* 临时形态没有预置 SuperKey,因此 SU 不再以 key 为凭。
* 旧脚本设置的 kstorage key 仍可用于其它命令(SKEY_GET/SET 等)。 */
tsk = kp_current_task();
cred = *(void **)((char *)tsk + OFF_TASK_CRED);
uid = *(u32 *)((char *)cred + OFF_CRED_UID);
/* ★ kpscall16(2026-11-08 20:45):按 mode 分组构造 cred,用来 bisect
* 「真 uid 迁移后第一个 open() 打崩内核」的根因。
* mode 由 a2 传入(SU 不用 a2,与 kstorage 的 gid 参数不冲突):
* 1 = 只 uid/euid/suid/fsuid
* 2 = + gid/egid/sgid/fsgid
* 3 = + caps 四件(inheritable/permitted/effective/**bset**),值 = KP 的 CAP_FULL_SET
* 4 = + groups 清零(走内核 groups_alloc(0)+set_current_groups)
* 缺省 0 按 3 处理(= 对齐 KP su_cred 的最小完整集)。
* ★ 3.18 无 cap_ambient(4.3+),不能写。 */
/* ★★★ kpscall16(2026-11-08 21:12,§34.11 定论):
* SU 不再手搓 cred —— 本机内核上「由模块代码就地写 cred 字节」会让后续 open/execve 必死,
* 而调用内核自己的 sys_setresuid() 则完全正常(§34.11 mode=7 实测)。
* 另外照 KP 的 commit_common_su 清 seccomp(真实 app 域是 Seccomp: 2)。
*/
{
typedef long (*setresuid_t)(u32, u32, u32);
setresuid_t sru = (setresuid_t)A_SYS_SETRESUID;
unsigned long *thi_flags = (unsigned long *)kp_stack_base();
struct cred *cur = *(struct cred **)((char *)tsk + OFF_TASK_CRED);
u32 to_uid = 0;
long sr;
if (!cur) return -EFAULT;
uid = *(u32 *)((char *)cur + OFF_CRED_UID);
/* to_uid:a2 若像小整数就当 uid;否则当 SuProfile* 读 +4 处的 to_uid */
if (a2 < 0x10000UL) {
to_uid = (u32)a2;
} else {
u32 prof[2] = { 0, 0 };
if (((ucopy_t)A_COPY_FROM_USER)(prof, (void *)a2, 8) == 0)
to_uid = prof[1];
}
if (to_uid > 65535U) to_uid = 0;
/* 清 TIF_SECCOMP(bit8):有 filter 时 open/exec 会被拦 */
if (thi_flags) *thi_flags &= ~(1UL << 8); /* 清 TIF_SECCOMP(bit8) */
sr = sru(to_uid, to_uid, to_uid);
printk(KERN_INFO "kpscall16: SU via setresuid(%u) rc=%ld uid 0x%x -> 0x%x\n",
to_uid, sr, uid,
*(u32 *)((char *)(*(void **)((char *)tsk + OFF_TASK_CRED)) + OFF_CRED_UID));
return sr;
}
}
__asm__(
" .pushsection .text.kphook, \"ax\"\n"
" .balign 16\n"
" .globl kphook_trampoline\n"
" .type kphook_trampoline, %function\n"
"kphook_trampoline:\n"
" lsr w10, w1, #16\n"
" movz w11, #0x1158\n"
" cmp w10, w11\n"
" b.ne .Lnormal\n"
" mov x16, x30\n"
" ldr x9, .Ldisp\n"
" blr x9\n"
" mov x30, x16\n"
" ret\n"
".Lnormal:\n"
" .word 0xa9ba7bfd\n"
" .word 0x910003fd\n"
" .word 0xa9025bf5\n"
" .word 0xaa0103f6\n"
" ldr x9, .Ljb_slot\n"
" ldr x9, [x9]\n"
" br x9\n"
" .balign 8\n"
".Ljb_slot:\n"
" .quad kphook_jumpback\n"
".Ldisp:\n"
" .quad kpsc_dispatch\n"
" .size kphook_trampoline, .-kphook_trampoline\n"
/* ---- sys_faccessat 入口 hook:命中则直接返回 0,未命中重放原 4 条并跳回 +0x10 ---- */
" .balign 16\n"
" .globl kphook_faccessat\n"
" .type kphook_faccessat, %function\n"
"kphook_faccessat:\n"
" sub sp, sp, #64\n"
" stp x0, x1, [sp, #0]\n"
" stp x2, x3, [sp, #16]\n"
" stp x4, x5, [sp, #32]\n"
" str x30, [sp, #48]\n"
" mov x0, x1\n" /* filename */
" mov x1, x2\n" /* mode */
" ldr x9, .Lfa_disp\n"
" blr x9\n" /* 1=命中, 0=走原函数 */
" mov x16, x0\n"
" ldp x2, x3, [sp, #16]\n"
" ldp x4, x5, [sp, #32]\n"
" ldr x30, [sp, #48]\n"
" cbnz x16, .Lfa_hit\n"
" ldp x0, x1, [sp, #0]\n"
" add sp, sp, #64\n"
" .word 0xa9b67bfd\n" /* stp x29,x30,[sp,#-160]! */
" .word 0x721d705f\n" /* tst w2,#0xfffffff8 */
" .word 0x910003fd\n" /* mov x29,sp */
" .word 0xa90153f3\n" /* stp x19,x20,[sp,#16] */
" ldr x9, .Lfa_jb\n"
" ldr x9, [x9]\n"
" br x9\n"
".Lfa_hit:\n"
" add sp, sp, #64\n"
" mov x0, #0\n" /* faccessat 成功 */
" ret\n"
" .balign 8\n"
".Lfa_jb:\n"
" .quad kphook_jumpback_fa\n"
".Lfa_disp:\n"
" .quad kpsc_path_dispatch\n"
" .size kphook_faccessat, .-kphook_faccessat\n"
/* ---- sys_execve 入口:命中时把 x0 换成「用户栈上的 sh 路径」 ---- */
" .balign 16\n"
" .globl kphook_execve\n"
" .type kphook_execve, %function\n"
"kphook_execve:\n"
" sub sp, sp, #64\n"
" stp x0, x1, [sp, #0]\n"
" stp x2, x3, [sp, #16]\n"
" stp x4, x5, [sp, #32]\n"
" str x30, [sp, #48]\n"
" /* x0 已是用户 filename */\n"
" ldr x9, .Lex_disp\n"
" blr x9\n" /* 返回新 x0 或 0 */
" cbz x0, .Lex_restore\n"
" str x0, [sp, #0]\n" /* 命中:覆盖保存的 x0 */
".Lex_restore:\n"
" ldp x2, x3, [sp, #16]\n"
" ldp x4, x5, [sp, #32]\n"
" ldr x30, [sp, #48]\n"
" ldp x0, x1, [sp, #0]\n"
" add sp, sp, #64\n"
" .word 0xa9be7bfd\n" /* stp x29,x30,[sp,#-32]! */
" .word 0x910003fd\n" /* mov x29,sp */
" .word 0xf9000ba1\n" /* str x1,[x29,#16] */
" .word 0xf9000fa2\n" /* str x2,[x29,#24] */
" ldr x9, .Lex_jb\n"
" ldr x9, [x9]\n"
" br x9\n"
" .balign 8\n"
".Lex_jb:\n"
" .quad kphook_jumpback_ex\n"
".Lex_disp:\n"
" .quad kpsc_exec_dispatch\n"
" .size kphook_execve, .-kphook_execve\n"
" .popsection\n"
);
extern unsigned long kphook_trampoline(void);
extern unsigned long kphook_faccessat(void);
extern unsigned long kphook_execve(void);
static int __init kpscall16_init(void)
{
printk(KERN_INFO "kpscall16: trampoline=0x%lx dispatch=0x%lx jumpback=0x%llx\n",
(unsigned long)kphook_trampoline, (unsigned long)kpsc_dispatch,
(unsigned long long)kphook_jumpback);
printk(KERN_INFO "kpscall16: execve_trampoline=0x%lx ex_jumpback=0x%llx\n",
(unsigned long)kphook_execve, (unsigned long long)kphook_jumpback_ex);
printk(KERN_INFO "kpscall16: faccessat_trampoline=0x%lx fa_jumpback=0x%llx\n",
(unsigned long)kphook_faccessat, (unsigned long long)kphook_jumpback_fa);
printk(KERN_INFO "kpscall16: kstorage min %dx%dx%d + superkey(gid3); SU not key-checked yet\n",
KS_GROUPS, KS_SLOTS, KS_DLEN);
return 0;
}
static void __exit kpscall16_exit(void)
{
printk(KERN_INFO "kpscall16: unloaded\n");
}
module_init(kpscall16_init);
module_exit(kpscall16_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("bmax121");
MODULE_DESCRIPTION("KP 3.18 Step 4.3a: kstorage minimal + loopback");