Skip to content

Feature request: ability to match connections with unidentified package name in routing rules #4009

Closed

Description

@hdrover

Operating system

Android

System version

Android 16

Installation type

sing-box for Android Graphical Client

If you are using a graphical client, please provide the version of the client.

No response

Version

Description

When an Android app uses SO_BINDTODEVICE / bind() to force traffic through a specific interface (e.g. tun0), ConnectivityManager.getConnectionOwnerUid() fails to find the socket owner. This happens because the Android API internally performs a socket lookup with idiag_if = 0, which does not match sockets bound to a specific interface.

When this happens in sing-box, ProcessInfo remains nil, and the package_name rule item returns false. The connection cannot be matched by any package_name rule — there is no way to distinguish it from normal matched traffic.

Why this matters

Apps in exclude_package are expected to go direct, bypassing the tunnel. However, such an app can force-bind to tun0, sending its traffic into the tunnel despite being excluded. Since getConnectionOwnerUid() fails for these connections, package_name rules cannot identify them, and the traffic ends up going through the proxy — potentially exposing the proxy server IP to a malicious app.

With "Block connections without VPN" enabled at the system level, the only remaining attack vector is binding to tun0. But there is currently no way to route these unidentified connections to direct or block via rules.

Proposal

Add the ability to match connections where the package name could not be determined. Two possible approaches:

  1. Allow an empty string "" in package_name as a special case (minimal change — one line in rule_item_package_name.go):
func (r *PackageNameItem) Match(metadata *adapter.InboundContext) bool {
    if metadata.ProcessInfo == nil || len(metadata.ProcessInfo.AndroidPackageNames) == 0 {
-       return false
+       return r.packageMap[""]
    }
    // ...
}

Usage:

{"package_name": [""], "outbound": "block"}
  1. A separate rule condition, e.g. package_name_unknown: true.

Either approach would give users a way to handle traffic from apps whose package name could not be identified.

Reproduction

curl ifconfig.co
curl --interface wlan0 ifconfig.co
curl --interface tun0 ifconfig.co

Logs

Supporter

Integrity requirements

  • I confirm that I have read the documentation, understand the meaning of all the configuration items I wrote, and did not pile up seemingly useful options or default values.
  • I confirm that I have provided the server and client configuration files and process that can be reproduced locally, instead of a complicated client configuration file that has been stripped of sensitive data.
  • I confirm that I have provided the simplest configuration that can be used to reproduce the error I reported, instead of depending on remote servers, TUN, graphical interface clients, or other closed-source software.
  • I confirm that I have provided the complete configuration files and logs, rather than just providing parts I think are useful out of confidence in my own intelligence.

Activity

  1. hdrover commented on Apr 8, 2026

    @hdrover
    ContributorAuthor

    I'd like to discuss which approach would be better before making a PR. There are two options:

    Option 1: Allow empty string in package_name

    This is a one-line change in a single file (route/rule/rule_item_package_name.go). When ProcessInfo is nil or AndroidPackageNames is empty, instead of returning false, we check if an empty string is in the rule's package map. The advantage is that this is the exact inverse of the existing package name lookup logic — if the normal search can't match a package name, the empty string catches it. There are no edge cases to miss because the check lives in the same place as the original matching. The downside is that it mixes an identifier with a lookup state in one field, and the config looks a bit odd: package_name: ["com.app", ""].

    Option 2: Add a separate package_name_unknown rule

    This requires changes in about 11 files: 3 option structs (route rule, DNS rule, headless rule set), 3 rule constructors (default, DNS, headless), a new matcher file, binary serialization for rule sets, and 3 condition helpers that control whether process search is activated. The advantage is a clean dedicated field with clear semantics. The downside is more code to maintain and a question of behavior on non-Android platforms. If the matcher checks ProcessInfo directly, then on Linux or Windows where AndroidPackageNames is always empty, package_name_unknown would match all traffic. Should it? Or should there be a platform guard so it only works on Android? Also, should it be added for both route rules and DNS rules, or route rules only?

    In both cases the semantics from the user's perspective is the same: the package name could not be determined, regardless of the reason. It could be that the UID lookup failed entirely (like the SO_BINDTODEVICE case), or the UID was found but no package name could be mapped to it. The user doesn't care why — they just want to be able to route traffic when package_name rules won't work, to cover the remaining cases.

  2. hdrover commented on Apr 9, 2026

    @hdrover
    ContributorAuthor

    The maintainer suggested resolving this via package_name_regex instead of the empty string or package_name_unknown approaches. PR is up: #4013

    The solution for matching traffic with an unidentified package name:

    {"package_name_regex": [".*"], "invert": true, "outbound": "block"}
    

    This works because when the package name cannot be determined, ProcessInfo is nil, the regex matcher returns false, and invert flips it to true — matching exactly the "unknown package" case described in this issue.

  3. hdrover commented on Apr 10, 2026

    @hdrover
    ContributorAuthor

    This has been added upstream in 6746fcb ("Add package_name_regex route, DNS and headless rule item"), so the feature itself is now implemented.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions