8 min read#android#kernel#kernelsu#root

Rooting stock Android emulators with KernelSU, without touching the kernel

Per-AVD kernelsu.ko builds that root stock x86_64 Android Studio images from Android 13 to 17, with KernelSU, KernelSU-Next or FinaSU.

imkamaran/avd-kernelsu-x86_64-MultiSU

KernelSU / KernelSU-Next / FinaSU LKM for Android Studio AVDs (x86_64)

20kernelsu-next-v3.4.2

On this page

  1. Why the upstream modules don’t just work
  2. Supported emulator images
  3. Rooting an emulator
  4. 1. Get the tool
  5. 2. See your emulators and their kernels
  6. 3. Patch into a separate ramdisk
  7. 4. Cold boot with it
  8. Things that will trip you up
  9. A bug that would show up about once in 32,768 boots
  10. FinaSU support
  11. A web GUI, if you’d rather click
  12. Starting it
  13. A typical first run
  14. Locked down, because it runs commands
  15. Or one script on Windows
  16. SUSFS preview
  17. Credits

Testing root modules on a physical phone is slow: flash, reboot, wait, repeat. An Android Studio emulator is far quicker to wipe and rebuild, but getting KernelSU onto one has usually meant swapping the emulator’s kernel for a custom GKI build.

avd-kernelsu-x86_64-MultiSU takes a different route. It keeps the stock kernel and patches only the system image’s ramdisk.img, so that kernelsu.ko gets loaded at boot (KernelSU’s LKM mode). It works with three root solutions: KernelSU, KernelSU-Next and FinaSU.

Why the upstream modules don’t just work

KernelSU attaches prebuilt kernel modules to every release, so why not load one of those?

On arm64 phones, GKI kernels promise a stable Kernel Module Interface: a module built for one KMI generation loads on any kernel of that generation. x86_64 GKI kernels make no such promise. The upstream modules are built from the latest DDK source, and on emulator kernels they either refuse to load (Android 14) or trip kCFI and panic the kernel (Android 15).

So this project builds kernelsu.ko separately for each emulator kernel, from exactly what that kernel was built from:

Supported emulator images

Android Kernel Build ID
13 (API 33) 5.15.119-android13-8 10871489
14 (API 34) 6.1.23-android14-4 9964412
15 (API 35) 6.6.30-android15-8 11987101
16 (API 36.0) 6.6.66-android15-8 13070261
16 QPR (API 36.1) 6.12.38-android16-5 13996879
17 (API 37.0, and API 37.2 with 16 KB pages) 6.12.81-android16-6 16167562
17 (API 37.1, 16 KB pages) 6.12.69-android16-6 15053784

These are x86_64 google_apis_playstore images at their latest SDK revision. Other variants of the same API level (google_apis, default, -extNN) can ship a different kernel. The patch tool refuses any kernel it doesn’t have a module for, rather than guessing.

Rooting an emulator

Everything goes through one file, avd-ksu.py, which needs only Python 3.8+ and its standard library.

1. Get the tool

curl -LO https://github.com/imkamaran/avd-kernelsu-x86_64-MultiSU/releases/latest/download/avd-ksu.py

On Windows:

Invoke-WebRequest https://github.com/imkamaran/avd-kernelsu-x86_64-MultiSU/releases/latest/download/avd-ksu.py -OutFile avd-ksu.py

2. See your emulators and their kernels

python avd-ksu.py list

3. Patch into a separate ramdisk

# KernelSU, latest release
python avd-ksu.py patch --avd API35-GAPPS --out ksu-API35.img

# or pin a variant and version
python avd-ksu.py patch --avd API35-GAPPS --variant kernelsu-next --version v3.4.2 --out ksun-API35.img
python avd-ksu.py patch --avd API35-GAPPS --variant finasu --version v1.5.0 --out fina-API35.img

The first run downloads the kernelsu.ko for your kernel, plus the ksud and ksuinit binaries for your computer, from the repository’s releases. It checks them against SHA256SUMS and caches them in ~/.cache/avd-ksu.

Why --out? The SDK’s ramdisk.img is shared by every emulator that uses the same system image. Writing the patched copy to its own file leaves the SDK untouched, so rooted and stock emulators can live side by side, and undoing it is just booting without -ramdisk. (Patching the SDK ramdisk in place also works, and avd-ksu.py restore puts the original back.)

4. Cold boot with it

emulator -avd API35-GAPPS -ramdisk ksu-API35.img -no-snapshot-load

Install and open the manager APK of the same version you patched with, close the emulator, and start it again with the same command. The manager should now show Working (LKM), and apps can request root.

Things that will trip you up

A bug that would show up about once in 32,768 boots

The most interesting fix in the build is for a bug you would almost never see.

On x86_64 kernels 6.2 and newer, module_finalize() scans a module for kCFI caller sequences. If it finds 41 ba or eb 0c 14 bytes in front of an indirect call, it treats the next four bytes as a kCFI type hash and rewrites them.

In front of a __nocfi call, those bytes aren’t a kCFI sequence. They’re just whatever code happens to sit there. In six places in KernelSU, they come from a relocation, so they depend on where in memory the module was loaded. In ksu_hook_execve_common(), an unlucky load address (estimated at no more than 1 in 32,768) would make the kernel corrupt the code right before the call to the original execve.

The fix, x86_64-kcfi-icall-trampoline.patch, routes every unchecked indirect call through a single noinline __nocfi trampoline, ksu_icall8(). Its call always has the same fixed bytes in front of it. CI also checks every built module and fails if any indirect call has bytes in front of it that the kernel could mistake for a kCFI sequence.

FinaSU support

My fork’s main addition is FinaSU, a KernelSU fork with kernel-level su, an overlayfs module system, a built-in Zygisk engine, a built-in Shizuku backend and SUSFS support. Its kernel code builds the same kernelsu.ko with the same x86_64 syscall dispatcher config, so it’s patched exactly like the other two. Two things needed extra work:

So far, root is confirmed on Android 17 (API 37.0): su -c id returns uid=0 ... context=u:r:ksu:s0 and the manager recognises the module. The 16 KB fix hasn’t been boot-tested yet, and Android 13 to 16 QPR builds pass the build checks but haven’t been booted.

A web GUI, if you’d rather click

The repo also comes with AVD Root Panel, a local web control panel for avd-ksu.py. It lists your emulators and their kernels, and lets you pick a root solution and version, patch, boot, check root and install the manager APK, all without typing a command.

It runs on Python’s built-in HTTP server, so there’s nothing to install. It also never re-implements anything: every patch, status, restore and SUSFS action runs avd-ksu.py itself, so the panel does exactly what the command line does. The full output of each action is shown in the page and saved under GUI/logs/.

AVD Root Panel showing the emulator list, the patch options, the run controls and the root check

Screenshots are from a demo setup with three example emulators: a supported one (Pixel 7, API 37.0), a 16 KB page-size image, and one whose kernel has no module. Click any screenshot to enlarge it.

Starting it

It needs Python 3.8+, the Android SDK (Emulator and Platform-Tools) and a clone of the repo, since it uses the avd-ksu.py one folder up.

Then open http://127.0.0.1:8765/. Emulators started from the panel keep running after you stop it.

A typical first run

Select the emulator, choose the variant and version, and press Patch & boot. Install and open the manager of the same version with Install APK…, then Stop, Start again and Check root.

The page warns you before anything that can’t work: patching an emulator whose kernel has no module, KernelSU-Next on a 16 KB image, or patching in place, which roots every emulator that uses that system image.

Check root results next to the Activity panel with the live output of the patch Kernel log of an emulator started from the panel, filtered to the KernelSU lines The panel in dark mode

Left to right: Check root with the live output of the patch, the kernel log filtered to KernelSU lines, and dark mode (it follows your system theme).

Locked down, because it runs commands

A web page that can run commands on your computer is an obvious target, so the panel is deliberately strict:

Or one script on Windows

scripts/check-root.bat does the whole check in one go. It patches into a separate image, cold boots it, waits for the boot (or a kernel panic), and writes check-root.log and the full kernel log next to itself. Double-click it and pick an emulator, or pass the emulator, variant and version:

check-root.bat Pixel_7 finasu v1.5.0

SUSFS preview

kernelsu-next-susfs is a preview variant with an x86_64 port of SUSFS’s guard module. It hides paths fully on Android 15 to 17. On Android 13 and 14 it only partly works, because those kernels lack the IBT landing pads SUSFS’s syscall hooks need.

Credits

This is a fork of leemikepop/avd-kernelsu-x86_64, which built the per-emulator KernelSU and KernelSU-Next pipeline and the SUSFS preview. Thanks also to 5ec1cff’s write-up on KernelSU on AVDs, the x86_64 LKM work in KernelSU #3562 and #3570, nzaar9/avd-kernelsu, Ylarod’s DDK images, and LyraVoid for FinaSU.

The full details, including every patch and how CI builds each module, are in the README.

All posts