Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better network sandbox #5283

Merged
merged 9 commits into from
Dec 6, 2024
Merged

Better network sandbox #5283

merged 9 commits into from
Dec 6, 2024

Conversation

maminrayej
Copy link
Contributor

@maminrayej maminrayej commented Dec 5, 2024

Overview

This PR adds a more fine-grained sandbox for the network layer. Previously, the wasm module did not have access to any networking functionality by default and the user could give this access by providing the --net flag to the wasmer client. But this would expose all of host's network stack to the wasm module. Now users can provide a list of of patterns that will act as a whitelist or blacklist. This is done by extending the --net flag:

wasmer run --net=<comma_separated_list_of_rules>

Rule Specification

Each member of the <comma_separated_list_of_rules> can be expressed like:

<rule_kind>:<rule_action>=<rule_expr>

<rule_kind>: dns, ipv4, ipv6

<rule_action>: allow | deny

dns:
<rule_expr>:
{<domain_spec>}:{<port_spec>} (this will be expanded to an outbound IP rule)
<domain_spec>: domain | domain glob | *

ipv4:
<rule_expr>:
<ipv4_specs>:<port_specs>/<in|out>
<ipv4_specs>: <ipv4_spec> | {<ipv4_spec>,}
<ipv4_spec>: ipv4 | ipv4_range | *

ipv6:
<rule_expr>:
<ipv6_specs>:<port_specs>/<in|out>
<ipv6_specs>: <ipv6_spec> | {<ipv6_spec>,}
<ipv6_spec>: ipv6 | ipv6_range | *

<port_specs>: <port_spec> | {<port_specs>,}
<port_spec>: port | start_port-end_port | *

Some examples:

  • Allow a specific domain and port: dns:allow=example.com:80
  • Deny a domain and all its subdomains on all ports: dns:deny=*danger.xyz:*
  • Allow opening ipv4 sockets only on a specific IP and port: ipv4:allow=127.0.0.1:80/in.

Features

Whitelisting and Blacklisting

Each rule can be expressed as an allow (whitelist) or deny (blacklist). A socket or domain is only accessible if at least one rule whitelists it and no rule blacklists it.

Directional Filtering

IP based rules can be either directional by specifying /in or /out postfixes to the rule, or bidirectional which is the default setting for these rules.

Rule Combination

In order to prevent repetition, the parts before and after the : could hold multiple values. For example:

ipv4:deny={127.0.0.1/24, 192.168.1.1/24}:{80, 443}

This is equivalent to:

ipv4:deny=127.0.0.1/24:80,
ipv4:deny=127.0.0.1/24:443,
ipv4:deny=192.168.1.1/24:80,
ipv4:deny=192.168.1.1/24:443

Resolves #5280.

@ibuildthecloud
Copy link

Is there a way to allow all domains? Basically in the syntax what does * mean, or it is even valid? Does * mean any domain and : is any IP?

@ibuildthecloud
Copy link

We couldn't figure out a way to allow only specific IPs and all domains. For example we wanted to allow *:80, *:443. This ends up not being possible because we need to add additionally some pattern for domains like *.com.

lib/cli/src/commands/run/wasi.rs Show resolved Hide resolved
lib/virtual-net/Cargo.toml Show resolved Hide resolved
lib/virtual-net/src/host.rs Show resolved Hide resolved
lib/virtual-net/src/lib.rs Show resolved Hide resolved
@ibuildthecloud
Copy link

I don't think the current design will satisfy these two common use cases.

Use case 1

  • I want to only allow access to example.com

Use case 2

  • I want to only allow access to *:80, *:443

The first fails because there is no way to allow all IPs but just specific domains.
The second falls because there is no way to allow only IPs but all domains.

@maminrayej maminrayej marked this pull request as ready for review December 6, 2024 04:01
@maminrayej maminrayej requested a review from theduke December 6, 2024 04:02
lib/virtual-net/src/ruleset.rs Outdated Show resolved Hide resolved
lib/virtual-net/src/ruleset.rs Show resolved Hide resolved
lib/virtual-net/src/ruleset.rs Outdated Show resolved Hide resolved
lib/virtual-net/src/ruleset.rs Outdated Show resolved Hide resolved
lib/virtual-net/src/ruleset.rs Show resolved Hide resolved
lib/virtual-net/src/host.rs Outdated Show resolved Hide resolved
lib/virtual-net/src/ruleset.rs Outdated Show resolved Hide resolved
lib/virtual-net/src/ruleset.rs Outdated Show resolved Hide resolved
lib/virtual-net/src/ruleset.rs Outdated Show resolved Hide resolved
lib/virtual-net/src/ruleset.rs Show resolved Hide resolved
@maminrayej maminrayej requested a review from theduke December 6, 2024 13:59
@syrusakbary syrusakbary merged commit 40080f3 into main Dec 6, 2024
60 of 68 checks passed
@syrusakbary syrusakbary deleted the better-network-sandbox branch December 6, 2024 16:10
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.

Allow passing explicit flags for --net
4 participants