_patchHarmony icon indicating copy to clipboard operation
_patchHarmony copied to clipboard

6.1.145 MTK (vivo MT6989): panic in droid_lkm_ipcns_install → ns_capable → cap_capable when starting a container

Open xcj0125 opened this issue 4 weeks ago • 8 comments

Summary

On a vivo/MTK Android 16 device (kernel 6.1.145-android14-11-maybe-dirty) the v2.0.0 build panics as soon as a container is started. setns(CLONE_NEWIPC) reaches droid_lkm_ipcns_install(), which goes into ns_capable() → cap_capable() and dereferences NULL at offset 0xe0. MTK mrdump grabs the dump and the device reboots (ro.boot.bootreason=kernel_panic). The exact same device + rootfs + config works fine on v1.0.4 (54acb1b), so this looks like a 2.0.0 regression.

Device / kernel

  • vivo iQOO Pad2 Pro (iPA2475 / DPD2329M), MT6989 (DT: MT8796), arm64
  • Android 16 (BP2A.250605.031.A3), user/release-keys
  • kernel 6.1.145-android14-11-maybe-dirty
  • vermagic 6.1.145-android14-11-maybe-dirty SMP preempt mod_unload modversions vivo aarch64
  • CONFIG_CFI_CLANG=y, MODVERSIONS=y, MODULE_SCMVERSION=y, SELinux enforcing, KernelSU-family root
  • missing: CONFIG_SYSVIPC, CONFIG_POSIX_MQUEUE, CONFIG_PID_NS, CONFIG_IPC_NS, CONFIG_USER_NS (this is the device the module exists for)

Build

  • commit 9f65632 (v2.0.0), ghcr.io/ylarod/ddk-min:android14-6.1-20260313
  • make VER=android14-6.1 DLKM_OPT=m

DLKM_OPT=m is forced because the new Makefile only builds droid_lkm_vendor.ko when UTS_RELEASE is 6.6/6.12 — on 6.1 the vendor module is otherwise absent from the zip. (Minor separate issue; the code itself compiles fine on 6.1 and has been in use here since v1.0.4.)

config.conf:

modules=droid_lkm droid_lkm_vendor
droid_lkm=verbose=0 mqueue=1 nspid=1 nspid_append=1
droid_lkm_vendor=ghost=1 ghost_match=mali_kbase

What works

  • both .ko insert cleanly (1 s after boot, module service.sh with /data/adb/phdroid/auto present)
  • droidspaces check → [✓] All required features found!
  • /proc/sysvipc/{msg,sem,shm} exist

Crash

droidspaces --name=Ubuntu26 start → immediate panic (pstore console-ramoops-0):

Unable to handle kernel NULL pointer dereference at virtual address 00000000000000e0
Mem abort info:
  ESR = 0x0000000096000006
  EC = 0x25: DABT (current EL), IL = 32 bits
  FSC = 0x06: level 2 translation fault
Data abort info:
  ISV = 0, ISS = 0x00000006
Internal error: Oops: 0000000096000006 [#1] PREEMPT SMP
CPU: 7 PID: 15885 Comm: droidspaces Tainted: P WC OE 6.1.145-android14-11-maybe-dirty #1
Hardware name: MT8796 (DT)
Call trace:
  dump_backtrace
  mrdump_common_die+0x24c [mrdump]
  ipanic_die+0x20 [mrdump]
  notify_die
  die
  __do_kernel_fault
  do_page_fault
  do_translation_fault
  do_mem_abort
  el1_abort
  el1h_64_sync_handler
  cap_capable+0x10/0x6c
  ns_capable+0x30/0x70
  droid_lkm_ipcns_install+0x40/0xc4 [droid_lkm]
  __arm64_sys_setns+0x16c/0x570
  invoke_syscall
  el0_svc

Guess at root cause

droid_lkm_ipcns_install() passes the shim IPC namespace to ns_capable(). On this kernel the fabricated namespace appears to lack a valid user_ns (or ns_common/user_ns offsets resolve differently on this vendor build), so cap_capable() dereferences NULL around 0xe0 — cred->user_ns comparison / ns->level read.

Possible mitigations:

  • skip the capability check when the target ns is one of our shim namespaces, or
  • make sure the fabricated struct ipc_namespace carries ns_common + user_ns (and level) initialised consistently with what this kernel expects, or
  • use the anchor-resolved offsets instead of the compile-time ones on that path.

I can rebuild and test any patch quickly (DDK android14-6.1, same workflow as above), and I have the full ramoops if more detail is useful.

xcj0125 avatar Nov 06 '26 16:11 xcj0125

Thanks for the issue! Could you please provide the dmesg log. droid_lkm_vendor is unnecessary for iQOO.

Dere3046 avatar Nov 07 '26 05:11 Dere3046

Fixed, at least I think.

Dere3046 avatar Nov 10 '26 22:11 Dere3046

Thanks! I re-fetched but origin/main is still 9f65632 (2026-11-05, "one mod") — no new commit, no other branch, and no release. That's the exact build that panics here, so there's nothing new for me to test yet.

Could you push the fix (or point me at the branch/commit)? Also, if it helps narrow it down: the NULL deref is at cap_capable+0x10 from ns_capable(ns->user_ns, CAP_SYS_ADMIN) in droid_lkm_ipcns_install(), i.e. the fabricated ipc_namespace's user_ns looks wrong at runtime. struct ipc_namespace is __randomize_layout — is the vivo build RANDSTRUCT'd, or is the fix to drop that capability check on the shim ns?

Either way I'll rebuild (DDK android14-6.1, same vermagic workflow) and test the moment there's something to pull.

xcj0125 avatar Nov 10 '26 22:11 xcj0125

Thanks I need BOOT image of your device to verify my hypothesis, I think I know what issue.

Dere3046 avatar Nov 10 '26 22:11 Dere3046

Here's the boot image from the affected device.

boot_b_iPA2475_6.1.145.img — raw dump of /dev/block/by-name/boot_b (active slot is _b), 100663296 bytes (96 MiB), taken read-only with dd, nothing modified on the device.

  • sha256: 4259af70fe4f66827c0de5f5c079fc05f8a6ee251e18f5fa2aafcdc19388b505
  • download: https://github.com/xcj0125/_patchHarmony/releases/download/boot-iqoopad2pro-6.1.145/boot_b_iPA2475_6.1.145.img

Sanity checks on the dump (matching what you'd see with unpack_bootimg):

  • ANDROID! header, LZ4-legacy kernel (02 21 4c 18) at 0x1000
  • AVB0 vbmeta appended after the kernel/ramdisk area
  • vermagic string present in the image: 6.1.145-android14-11-maybe-dirty SMP preempt mod_unload
  • /proc/version: Linux version 6.1.145-android14-11-maybe-dirty (build-user@build-host) (Android (10087095, +pgo, +bolt, +lto, -mlgo, clang 17.0.2) #1 SMP PREEMPT
  • no boot-level patching here — root on this device is KernelSU-family in LKM mode, so the GKI kernel inside this boot.img is exactly the one that panics

Also on hand if it helps: init_boot_b (8 MiB), vendor_boot_b (128 MiB), the full ramoops/pstore from the panic, and dmesg from a clean boot. Say the word and I'll dump/upload any of them.

One ask in return: origin/main is still 9f65632 with no new commit, branch or release, so there's nothing to rebuild against yet. If the fix lives somewhere else, a commit/branch pointer would let me re-test immediately (DDK android14-6.1, same vermagic workflow).

xcj0125 avatar Nov 11 '26 00:11 xcj0125

Dug into this on the device side — I think I found the real cause, and it is not RANDSTRUCT (sorry, my own guess in the issue was wrong).

Two more artifacts attached (pulled read-only as root, nothing written to the device):

  • pad-config.gz — /proc/config.gz of the running kernel
  • pad-vmlinux.btf — /sys/kernel/btf/vmlinux (CONFIG_DEBUG_INFO_BTF=y)

1. Not RANDSTRUCT

CONFIG_RANDSTRUCT_NONE=y — __randomize_layout compiles to nothing on this build, so the layout is the plain 6.1 one.

2. The layout this kernel actually has

pahole -F btf -C ipc_namespace pad-vmlinux.btf:

struct ipc_ids             ids[3];        /*    0   840 */   (sizeof(ipc_ids) = 280)
int                        sem_ctls[4];   /*  840    16 */
...
struct ctl_table_header *  ipc_sysctls;   /* 1240     8 */
struct user_namespace *    user_ns;       /* 1248     8 */   <- 0x4e0
struct ucounts *           ucounts;       /* 1256     8 */
struct llist_node          mnt_llist;     /* 1264     8 */
struct ns_common           ns;            /* 1272    24 */   <- 0x4f8
sizeof = 1296

3. What the installed droid_lkm.ko (2.0.0, 9f65632) was compiled against

8cdc: ldr   x22, [x0, #8]          ; nsset->nsproxy
8ce0: sub   x19, x1, #0x4e0        ; container_of(new, struct ipc_namespace, ns)  <- compile-time offsetof(ns) = 0x4e0
8cec: ldur  x0, [x21, #-24]        ; "ns->user_ns" = *(new - 0x18)  -> 0x4c8 in the module's layout
8cf0: mov   w1, #0x15              ; CAP_SYS_ADMIN
8cf4: bl    ns_capable
...
8d14: str   x19, [x22, #16]        ; nsproxy->ipc_ns = <computed base>

Cross-checks that the module is otherwise fine on this kernel: nsproxy->ipc_ns is at +16 and cred->user_ns at +136 (0x88) on both sides — so the module's nsproxy and cred offsets match the device exactly. The only thing that has drifted is ipc_namespace, by 0x18: module expects ns @ 0x4e0, this kernel has ns @ 0x4f8.

4. That explains the panic exactly

*(new - 0x18) = byte 0x4c8, which on this kernel is inside struct ctl_table_set ipc_set (0x478..0x4d8) — a zero word. So ns_capable() gets targ_ns == NULL, and cap_capable() does if (ns->level ...), with offsetof(struct user_namespace, level) = 224 = 0xe0. Hence: NULL pointer dereference at 0xe0 in cap_capable+0x10 — the fault address is literally the level offset, i.e. targ_ns was NULL, not a wrong-but-valid pointer.

5. Where the 0x18 comes from

24 = 3 × 8, i.e. sizeof(struct ipc_ids) is 8 bytes smaller in the build you use (272 vs 280 here), times ids[3]. On this kernel ipc_ids is 280 with rhashtable key_ht = 176 — worth diffing sizeof(ipc_ids) / sizeof(rhashtable) / rw_semaphore between your DDK build and this config. (Device config also has CONFIG_SYSVIPC, CONFIG_POSIX_MQUEUE, CONFIG_IPC_NS, CONFIG_PID_NS, CONFIG_USER_NS all off — pad-config.gz has the full set.)

6. Suggested fix

Hardcoding 0x4e0 will keep breaking on the next vendor config. Since CONFIG_DEBUG_INFO_BTF=y here (and on GKI generally), resolve at runtime instead of compile time:

  • btf_find_by_name_kind("ipc_namespace", BTF_KIND_STRUCT) → btf_member_bit_offset() for ns, user_ns (and user_namespace.level/parent if the shim needs them)
  • fall back to the current compiled-in constants when BTF isn't available

I can rebuild (DDK android14-6.1, same vermagic workflow) and test as soon as there's a commit/branch — with these offsets I can also sanity-check the result before flashing anything.

xcj0125 avatar Nov 11 '26 01:11 xcj0125

Fixed

Dere3046 avatar Nov 11 '26 11:11 Dere3046

Tested e9b1885 ("fix 6.1") on the device from the report — no more panic. Details:

Build: DDK android14-6.1, commit e9b1885, make VER=android14-6.1 DLKM_OPT=m. vermagic: 6.1.145-android14-11-maybe-dirty SMP preempt mod_unload modversions vivo aarch64 (byte-identical to the device).

What changed on the wire: droid_lkm.ko no longer references any rhashtable* symbol (nm -u → 0 matches), and ipc_init_ids() no longer calls the kernel's out-of-line rhashtable_init(). That was the remaining out-of-line writer whose sizeof(struct rhashtable) came from the running kernel while the object was sized by the build — with the compiled-in ipc_namespace here 24 bytes smaller than the device's (sizeof 1272 vs 1296, ns @1248 vs @1272), that write ran past the module's own field.

Test (stock 6.1.145 MT6989, KernelSU LKM root, nothing else changed):

# boot, module service.sh insmod
droid_lkm        741376 2 - Live (OE)
droid_lkm_vendor 487424 0 - Live (OE)

droidspaces check -> [✓] All required features found!

droidspaces -n Ubuntu26 start (autostart on boot, exercises setns(CLONE_NEWIPC)
                              -> droid_lkm_ipcns_install)
$ uname -a    -> 6.1.145-android14-11-maybe-dirty #1 SMP PREEMPT aarch64
$ readlink /proc/1/ns/ipc -> ipc:[4026536211]
$ ls /proc/sysvipc  -> msg sem shm
$ ipcmk -M 1024     -> ok
$ vulkaninfo --summary | grep deviceName -> Mali-G720-Immortalis MC12
dmesg: 0 matches for "Unable to handle" / "Internal error" / "kernel panic"

So the panic path (ns_capable() deref at +0xe0) is gone, SysV IPC + mqueue work under the shim namespace, and the droid_lkm_vendor ghost/wrap path still binds (fgp hook fires from kbase_context_common_init in mali_kbase_mt6989_r44, GPU visible inside the container) — i.e. on this vivo build the vendor module is still required for GPU inside containers, contrary to the earlier "unnecessary for iQOO" note.

One caveat worth a follow-up, not blocking: droid_lkm_ipcns_install() still dereferences ns->user_ns / nsproxy->ipc_ns with allocator-dependent offsets, and the module's ipc_ids/ipc_namespace layout is still 24 bytes narrower than this device's. It only holds as long as the namespace handed to install is one the module allocated. If a vendor kernel ever hands a kernel-allocated ipc_namespace through that path (copy_ipcs), the same class of drift returns. Doing user_ns via droid_lkm_ipcns_off() there too (or BTF at init) would close it.

Happy to re-test any further commit on the same workflow.

xcj0125 avatar Nov 11 '26 19:11 xcj0125