sandlock icon indicating copy to clipboard operation
sandlock copied to clipboard

Allow to combine --net-allow and --net-deny

Open congwang-mk opened this issue 4 months ago • 2 comments

Requested by @SuperManito. Currently they are exclusive, because --net-allow means defaulting to deny, --net-deny mean defaulting to allow. Although combining them could be powerful, the logic might be too complicate if we don't design it right. Also, the implementation would be complicate too. We need to evaluate this carefully and maybe come up with a better design.

congwang-mk avatar Jul 08 '26 05:07 congwang-mk

I traced this a bit. I think the simplest semantics are probably:

  • allow only → current allowlist behavior
  • deny only → current denylist behavior
  • both → allowlist minus denylist, so deny wins overlaps

One thing I noticed is that this can’t be done by only removing the exclusivity check: runtime currently picks the deny path and skips the allow policy entirely. Also, HTTP rules can add derived entries into net_allow, so we’d need to distinguish explicit --net-allow from those.

Does that match what you had in mind?

bferanmi806-sketch avatar Sep 11 '26 15:09 bferanmi806-sketch

Thanks for tracing this. Yes, those semantics match what I had in mind:

  • allow only: current allowlist behavior
  • deny only: current denylist behavior
  • both: allowlist minus denylist, deny wins on overlap

Both of your observations are correct, and there are a few more constraints to settle before implementing. Listing them so a PR can address all of them at once.

1. Decide the mode before the HTTP extension, and store it once. extend_net_allow_for_http runs after the exclusivity check (sandbox/builder.rs), so if mode selection keys off the post-extension net_allow, this config silently changes meaning:

--net-deny 10.0.0.0/8 --http-allow api.example.com

Today it is "allow everything except 10/8, plus the HTTP ACL". A naive "both non-empty" rule would turn it into "TCP allowlist of api.example.com only". The mode has to come from the user's explicit specs, and it should live on Sandbox as one field rather than being re-derived from vector emptiness in the three places that do it now: resolved.rs (net_deny: !sandbox.net_deny.is_empty()), landlock.rs (compute_net_mask), and the NetworkState setup in sandbox.rs.

2. Deny must be a second check after the allow verdict, not folded into AllowList. --net-allow '*' resolves to NetworkPolicy::Unrestricted and destination_verdict short-circuits on it, so a deny stored inside the allow variant would never run for that case. Either give NetworkState a per-protocol deny policy that is checked after the allow result, or add a composite variant. The former is simpler.

3. Keep Landlock's port gate in combined mode. compute_net_mask forces the TCP wildcard (drops CONNECT_TCP) whenever net_deny is non-empty. That is only correct for deny-only mode. In combined mode the allow side should still install the per-port Landlock rules; deny is enforced on the on-behalf path where the allow check already runs.

4. policy_fn overrides. effective_network_policy returns per-pid grants early and never consults the static policy. If deny wins, a static --net-deny should also beat a dynamic policy_fn grant, otherwise policy_fn can reopen a denied CIDR. I want that behavior; please apply the deny check after the override resolution too.

5. Bind side. --net-allow-bind and --net-deny-bind have the same exclusivity (builder.rs, compute_net_mask, and the bind_allow_ports guard in sandbox.rs). Apply the same allow-minus-deny rule there in the same PR so the model is uniform.

6. Hostnames. Deny rules stay IP-only. An allow hostname resolving into a denied CIDR is refused, since the verdict runs on the resolved IP. Please add a test for that.

No SDK validates the exclusivity locally (Python and Go pass specs straight to the native builder), so this is core plus docs: README.md, the sandbox-reference, the Python and Go doc comments, and the builder doc comments. builder_rejects_net_allow_and_net_deny_together flips to a positive test.

Happy to review a PR along these lines.

congwang-mk avatar Sep 13 '26 04:09 congwang-mk