Skip to content

fix: observe unsent bytes on Android, where SIOCOUTQNSD is denied - #11

Merged
Nemu-x merged 3 commits into
mainfrom
fix/android-send-observation
Oct 29, 2026
Merged

Nemu-x merged 3 commits into
mainfrom
fix/android-send-observation

Conversation

@Nemu-x

@Nemu-x Nemu-x commented Oct 29, 2026

Copy link
Copy Markdown
Owner

On Android, SELinux allows apps only the socket ioctls in unpriv_sock_ioctls; SIOCOUTQNSD (0x894b) is in priv_sock_ioctls and denied to every app (Android 8+, Termux too). Every send-queue observation read it, so the TCP fake, disorder and SplitWaitSent failed at connect time on Android while PlatformCapabilities() reported them supported.

  • Fallback per socket: SIOCOUTQNSD first; on EACCES/EPERM → getsockopt(TCP_INFO) tcpi_notsent_bytes (offset 144 on every arch; linux/386 via socketcall); on a short TCP_INFO (kernel < 4.6) or another failure, Sent is unsupported for that socket with the reason kept. Other ioctl errors stay errors. The result is cached per connection, so the denied ioctl is tried (and audit-logged) once.
  • Fail before sending: fake, disorder and split-wait sent check the socket's observation before the first piece and return ErrFakeUnsupported / ErrDisorderUnsupported / ErrSplitWaitUnsupported wrapping the errno; nothing is substituted. SplitWaitAcked keeps working on SIOCOUTQ alone.
  • Honest capabilities: on Android PlatformCapabilities() reports FakeTCP, Disorder and Ack.Sent as false, listed in Unsupported and in a new Capabilities.Conditional; ProbeCapabilities(conn) resolves them on a live socket; ProbeConn reports the socket's own mechanism (SIOCOUTQ/SIOCOUTQNSD, SIOCOUTQ/TCP_INFO, SIOCOUTQ) and SentErr. desync-probe resolves them over a loopback connection, so Termux runs keep the fake and disorder presets.
  • Callers: presets.Available(PlatformCapabilities()) now drops fake/disorder on Android; callers that want them (Citrus, the mihomo patch) should treat Conditional methods as allowed in config validation and rely on the per-connection error.
  • Linux sockets where SIOCOUTQNSD is denied by seccomp/LSM take the same fallback. README gets an Android column; docs/connection-io.md a section "Android send-queue observation".

Tests: TCP_INFO layout and parsing (both byte orders, lengths 0–280), the fallback chain with a stub kernel, the denied ioctl tried once, capability reporting, every method under the denial with and without TCP_INFO (7 cases × tcp4/tcp6: server reads 0 bytes, error wraps the sentinel and EACCES), and the 30 siocoutqnsd-denied/... low-hop drop subtests as root. Kernel cross-check: with a full peer window SIOCOUTQNSD and tcpi_notsent_bytes both read 983094.

Checks: gofmt; vet on Go 1.26.10 and 1.20.14 for windows, linux (amd64/386/arm/arm64), darwin, android/arm64; Windows tests (both Go); Docker Linux -race as root with DESYNC_REQUIRE_LOWHOP=1, Go 1.20, GOARCH=386, and as an unprivileged user. Android 16 emulator (kernel 6.6, Enforcing): the test binaries pass in adb shell and inside a debuggable app via run-as (u:r:runas_app), with avc: denied { ioctl } ioctlcmd=0x894b once per connection and the probe reporting SIOCOUTQ/TCP_INFO, fake and disorder available. Not verified: a physical device, a normally launched untrusted_app, a real kernel < 4.6, seccomp on every vendor build.

Android's SELinux policy lets apps issue SIOCOUTQ but not SIOCOUTQNSD
(priv_sock_ioctls), so every Linux ack observer failed with EACCES and
the TCP fake, disorder and SplitWaitSent failed at connect time.

When SIOCOUTQNSD fails with EACCES or EPERM, read tcpi_notsent_bytes
from getsockopt(TCP_INFO) (offset 144 on every arch, Linux 4.6+; via
socketcall on linux/386). A kernel whose TCP_INFO ends before the field
leaves the socket without Sent observation. The choice is made once per
connection and shared by split wait, disorder and the fake.

Methods that need unsent bytes now check the socket before the first
piece is written and fail with ErrSplitWaitUnsupported,
ErrDisorderUnsupported or ErrFakeUnsupported (wrapping the errno),
never with a substitute.

PlatformCapabilities on Android no longer claims SplitWaitSent, the TCP
fake or disorder; it lists them in the new Capabilities.Conditional.
ProbeCapabilities resolves them on a live socket, and ProbeConn reports
the per-socket mechanism and ConnCapabilities.SentErr.
@Nemu-x
Nemu-x merged commit 673cbef into main Oct 29, 2026
19 checks passed
@Nemu-x
Nemu-x deleted the fix/android-send-observation branch October 29, 2026 23:29
Nemu-x added a commit to Nemu-x/ClashFest that referenced this pull request Oct 29, 2026
Patch 0021 is SlothClash's regenerated 0023, byte-identical: it pins
github.com/Nemu-x/desync main at 673cbef
(v0.2.1-0.20261029232908-673cbefddb80, Nemu-x/desync#11; it becomes
v0.2.1 once tagged) and makes the desync outbound accept a method the
library lists in Capabilities.Conditional (split-wait sent, disorder,
fake on Android, where apps may not read SIOCOUTQNSD), logging once per
outbound that it is verified per connection. Both Go modules require
the same version, synced with go list -mod=mod.

The bridge's desyncCapabilities now carries conditional and probed:
desync.ProbeCapabilities of a loopback connection, run once per
process, so the UI sees what a socket of the app can really do. The
preset catalog and the desync-only profile generator filter by that
probe (falling back to the platform report), so Android keeps the
fake, disorder and split-wait sent presets where TCP_INFO provides
unsent bytes.
Nemu-x added a commit to Nemu-x/SlothClash that referenced this pull request Oct 29, 2026
Patch 0023 now pins github.com/Nemu-x/desync main at 673cbef
(v0.2.1-0.20261029232908-673cbefddb80, Nemu-x/desync#11); it becomes
v0.2.1 once tagged.

Android denies apps SIOCOUTQNSD, so the library no longer claims
split-wait sent, disorder and the TCP fake there: it lists them in
Unsupported and in Capabilities.Conditional, and a socket without
TCP_INFO unsent bytes fails before sending. As in Citrus, the desync
outbound accepts a method listed in Conditional and logs once per
outbound that it is verified per connection; tests cover an
Android-like capability report with and without Conditional. YAML keys
are unchanged and identical to Citrus.
Nemu-x added a commit to Nemu-x/SlothClash that referenced this pull request Oct 30, 2026
Patch 0023 now pins github.com/Nemu-x/desync main at 673cbef
(v0.2.1-0.20261029232908-673cbefddb80, Nemu-x/desync#11); it becomes
v0.2.1 once tagged.

Android denies apps SIOCOUTQNSD, so the library no longer claims
split-wait sent, disorder and the TCP fake there: it lists them in
Unsupported and in Capabilities.Conditional, and a socket without
TCP_INFO unsent bytes fails before sending. As in Citrus, the desync
outbound accepts a method listed in Conditional and logs once per
outbound that it is verified per connection; tests cover an
Android-like capability report with and without Conditional. YAML keys
are unchanged and identical to Citrus.
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.

1 participant