Skip to content

chore: add procfs fallback for android findProcess#2639

Merged
wwqgtxx merged 1 commit into
MetaCubeX:Alphafrom
ValanisZz:feat/find-process-backend-procfs
Mar 26, 2026
Merged

chore: add procfs fallback for android findProcess#2639
wwqgtxx merged 1 commit into
MetaCubeX:Alphafrom
ValanisZz:feat/find-process-backend-procfs

Conversation

@ValanisZz

Copy link
Copy Markdown

Summary

  • Add find-process-backend (auto | netlink | procfs) to control how the local socket owner is resolved for process-based rules.
  • On Android (especially recent releases), netlink INET_DIAG can fail or return UID 0 / empty process info for some apps, so process rules become unreliable.
  • Procfs path reads /proc/net/tcp, tcp6, udp, udp6, matches primarily by local port when TUN metadata source IP does not match the kernel row (e.g. fake-ip / gateway IP).
  • Skip inode == 0 rows for port-only matches to avoid TIME_WAIT false positives.
  • If inode-based /proc/<pid>/fd resolution fails but UID is known, fall back to resolving the process/package name by UID (typical on Android where all app processes share one UID).

Problem

On Android 16, users running Mihomo under root (e.g. KernelSU Next, SUSFS, TUN + redirect) observed unstable process identification:

  • Netlink sometimes returns errors (e.g. NLMSG_ERROR) or UID 0 with no process name, while ss still shows the correct UID/PID.
  • With TUN + fake-ip, metadata often carries a gateway/fake source IP (e.g. 198.18.0.1) that does not appear as local_address in /proc/net/tcp for the real app socket, so naive IP+port matching fails.
  • Fast connections can leave only TIME_WAIT rows with misleading UID/inode when lookup runs late.

Repro / validation environment (reported): OnePlus 15, Android 16, KernelSU Next + SUSFS, Mihomo in any mode with process-based rules.

Solution

  1. Introduce find-process-backend in general config (same level as find-process-mode, not under tun:).
  2. auto (default): try netlink first; on error or UID 0, retry via procfs.
  3. procfs: use /proc/net/* only (bypass netlink).
  4. netlink: preserve previous behavior (netlink only).
  5. After UID/inode resolution, resolve process name by inode; if that fails and UID ≠ 0, resolve by scanning /proc for that UID (Android package/cmdline).

Changes

  • component/process/find_process_backend.goFindProcessBackend type, defaults, Get/Set, text unmarshaling.
  • component/process/process_linux_procfs.go — parse /proc/net/tcp* / udp*, port-first + exact IP when possible, skip inode=0 for port-only rows.
  • component/process/process_linux.go — backend switch, netlink → procfs fallback, resolveProcessNameByUID.
  • config/config.gofind-process-backend on General / RawConfig, default auto, wire into parsed general.
  • hub/executor/executor.go — apply backend on config load / GetGeneral.
  • hub/route/configs.go — REST PATCH support for find-process-backend.

Usage

find-process-mode: always   # or strict, as needed
find-process-backend: auto  # auto | netlink | procfs

Testing

  • go test ./... and go build -tags with_gvisor pass.
  • Manually verified on OnePlus 15 / Android 16 / KernelSU Next + SUSFS: process-based rules match more consistently with auto or procfs; Chrome and other apps no longer randomly appear as UID 0 when procfs path is used.

@ValanisZz ValanisZz changed the title feat(process): add find-process-backend with procfs fallback on Android feat(process): add find-process-backend with procfs fallback on Linux Mar 25, 2026
@wwqgtxx

wwqgtxx commented Mar 26, 2026

Copy link
Copy Markdown
Collaborator

Thank you for your contribution; we believe adding procfs fallback is very meaningful.

However, there's a small question: is it really necessary to add a find-process-backend configuration item for this?

From your description, auto seems to be working well.

@ValanisZz

ValanisZz commented Mar 26, 2026

Copy link
Copy Markdown
Author

@wwqgtxx yes, it is necessary. find-process-backend: auto mode uses both netlink and procfs in order. Without procfs lots of apps drops to UID 0 and cannot be processed by the rules associated with PROCESS-NAME

Actually you can add procfs fallback as default behaviour for app UID detection without adding new config find-process-backend. I dont think that someone needs using netlink mode only but still

@wwqgtxx

wwqgtxx commented Mar 26, 2026

Copy link
Copy Markdown
Collaborator

To simplify future maintenance, I think we don't need to add a new configuration item; simply adding fallback paths to the existing code will suffice.

Furthermore, these fallbacks should only work on GOOS==android to avoid affecting results on other platforms.

@ValanisZz

Copy link
Copy Markdown
Author

Yeah, fallback behaviour would be enough as for me

@wwqgtxx wwqgtxx force-pushed the feat/find-process-backend-procfs branch from ea38bc1 to 617b998 Compare March 26, 2026 09:24
@wwqgtxx wwqgtxx changed the title feat(process): add find-process-backend with procfs fallback on Linux chore: add procfs fallback for android findProcess Mar 26, 2026
@wwqgtxx wwqgtxx force-pushed the feat/find-process-backend-procfs branch from 617b998 to bd22ecd Compare March 26, 2026 09:29
@wwqgtxx wwqgtxx merged commit 9664ca2 into MetaCubeX:Alpha Mar 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants