Repository navigation
Conversation
…t and enforce allowlists (#66359) ## Description A `network="public"` sandbox could reach everything its pod reaches. From a sandbox in a CoreWeave pod running a Ray head, GCS, the Ray client server, the raylet, the object manager, the dashboard agent, the Kubernetes API, the kubelet, and 169.254.169.254 all accepted connections. Ray's ports take no token unless the cluster runs with `RAY_AUTH_MODE=token`, and slirp4netns relays sandbox flows through the pod's own sockets, so a pod NetworkPolicy can't single them out. This PR filters every `public` sandbox's egress with an nftables ruleset in its private network namespace, loaded after slirp4netns brings the tap up and before runsc starts. A failed load fails the create. - `cidr_allowlist` defaults to `["internet"]`, public addresses only. Entries are addresses or networks with optional ports (`10.0.0.5:8000`, `internet:443`). - A denylist runs before any list: link-local (instance metadata), loopback, Alibaba Cloud's and Azure's metadata endpoints, the Kubernetes API Service, and `RAY_SANDBOX_EGRESS_DENY`. Ray doesn't track its nodes, as pcmoritz suggested: `internet` leaves them out while their addresses are internal, which the docs call a best effort, and an operator who allows internal networks lists the nodes' network in `RAY_SANDBOX_EGRESS_DENY`, which also covers nodes the autoscaler adds. - `set_egress_allowlist`, on `SandboxRuntime` and on the actor that `sandbox.create()` returns, replaces any public sandbox's ruleset in one `nft -f`, entering its namespaces through fds checked against the ones recorded at creation. - With `dns` set, the create sends each resolver one UDP byte from inside the sandbox and fails, naming the resolver, if the send fails. On Cilium, which translates the cluster DNS Service IP before the filter sees it, every lookup failed instead. - The gRPC facade maps `block_network`, `outbound_cidr_allowlist`, and `TaskSetNetworkAccess`; open access means `internet`. - The REST API answers 403 to `network="host"`, `"sandbox"`, or `rootless=false` unless `allowed_network_modes` / `allow_rootful` allow them, and `build_app` requires `RAY_SANDBOX_API_TOKEN` unless `allow_unauthenticated` is set. Behavior changes: `public` needs `nft` on the node, no longer reaches private networks without a list, and fails to create when a `dns` resolver is unreachable. REST deployments need a token. ### Why nftables inside the sandbox netns is the right enforcement point Inside the sandbox's private user + net namespace, the host-side launch script has CAP_NET_ADMIN over that child network namespace (without needing CAP_NET_ADMIN on the Kubernetes worker pod itself). runsc runs inside that child netns with --network=host. That means gVisor's hostinet creates real Linux kernel AF_INET/AF_INET6 sockets inside the child netns, so every packet from the sandbox passes through the Linux kernel's nf_tables output chain before reaching slirp4netns's tap0. **Why sandbox code cannot tamper with the rules (even with CAP_NET_ADMIN):** gVisor's Sentry intercepts all syscalls inside the sandbox. Even if the container process has CAP_NET_ADMIN or CAP_NET_RAW, gVisor's hostinet (without --net-raw) never forwards AF_NETLINK (NETLINK_NETFILTER / NETLINK_ROUTE) or AF_PACKET sockets to the host kernel. Running nft flush ruleset or iptables -F inside the sandbox talks only to gVisor's internal Sentry stub, never touching the host kernel netns where the rules live. ### Results TCP connects from a sandbox in a CoreWeave (Cilium) pod running a Ray head, 5 s timeout, measured on dddd170: | Destination | Default | `ALLOW_ALL_CIDRS` | + `RAY_SANDBOX_EGRESS_DENY=<node>,<API server>` | | --- | --- | --- | --- | | GCS, Ray client server, raylet, object manager, dashboard | timeout | open | timeout | | 169.254.169.254:80 | timeout | timeout | timeout | | Kubernetes API (Service and endpoint) | timeout | open | timeout | | 1.1.1.1:443, DNS | open | open | open | Allow-all reaches the Ray head's ports because Ray doesn't track its nodes, and it reaches the API Service because Cilium rewrites it to the API server's address at connect time; `RAY_SANDBOX_EGRESS_DENY` closes both, and the docs say so. Root in a sandbox with `CAP_NET_ADMIN` and `CAP_NET_RAW` ran `nft flush ruleset` and `iptables -F`: both failed with "Protocol not supported", and GCS stayed closed. How to review: `config.py`, `_internal/egress_guard.py`, `backend/gvisor.py` (`_render_egress_ruleset`, the run command, `set_egress_allowlist`, `_unreachable_resolver`), `http/host.py` and `http/grpc_facade.py`, `http/schemas.py` and `http/app.py`, then the docs. Skip the `_pb2.py` files. ## Related issues Follows #65839. Found through SkyRL's SWE-bench Multimodal runs on Ray Sandbox (skyrl-project/SkyRL#2467). #66782 and #67010 inherit the default. #66471's restore path needs the same nft install. ## Additional information - Not a duplicate: none of the 20 open sandbox PRs adds egress filtering or a default deny. - Tests on a CoreWeave pod (Cilium, runsc release-20261012.0, root), rebased on master 7073066: `TEST_SANDBOX=1 pytest tests/test_gvisor_backend.py tests/test_image_manager.py tests/test_image_utils.py tests/test_sandbox_config.py tests/test_sandbox_runtime.py tests/test_egress_guard.py -k "not netns and not test_create_sandbox_helper"`: 167 passed, 1 skipped. `-k netns`: 22 passed. `test_create_sandbox_helper`: 1 passed. `tests/test_sandbox.py`: 7 passed. `http/tests`: 167 passed, 10 skipped. With the Modal SDK 1.5.5 installed on macOS, which CI doesn't have, `http/tests`: 175 passed, 2 skipped (the gVisor integration tests). On the pod, `dns=["10.16.0.10"]`, the cluster DNS Service, now fails the create with "Operation not permitted" and still resolves with `ALLOW_ALL_CIDRS`; the check adds about 14 ms to a create. pre-commit passes. This round wasn't rerun as uid 1000. - I used Claude Code to help write and test this change. --------- Signed-off-by: xyuzh <[email protected]> Co-authored-by: Philipp Moritz <[email protected]>
|
This pull request has been automatically marked as stale because it has not had You can always ask for help on our discussion forum or Ray's public slack channel. If you'd like to keep this open, just leave any comment, and the stale label will be removed. |
…t and enforce allowlists (ray-project#66359) ## Description A `network="public"` sandbox could reach everything its pod reaches. From a sandbox in a CoreWeave pod running a Ray head, GCS, the Ray client server, the raylet, the object manager, the dashboard agent, the Kubernetes API, the kubelet, and 169.254.169.254 all accepted connections. Ray's ports take no token unless the cluster runs with `RAY_AUTH_MODE=token`, and slirp4netns relays sandbox flows through the pod's own sockets, so a pod NetworkPolicy can't single them out. This PR filters every `public` sandbox's egress with an nftables ruleset in its private network namespace, loaded after slirp4netns brings the tap up and before runsc starts. A failed load fails the create. - `cidr_allowlist` defaults to `["internet"]`, public addresses only. Entries are addresses or networks with optional ports (`10.0.0.5:8000`, `internet:443`). - A denylist runs before any list: link-local (instance metadata), loopback, Alibaba Cloud's and Azure's metadata endpoints, the Kubernetes API Service, and `RAY_SANDBOX_EGRESS_DENY`. Ray doesn't track its nodes, as pcmoritz suggested: `internet` leaves them out while their addresses are internal, which the docs call a best effort, and an operator who allows internal networks lists the nodes' network in `RAY_SANDBOX_EGRESS_DENY`, which also covers nodes the autoscaler adds. - `set_egress_allowlist`, on `SandboxRuntime` and on the actor that `sandbox.create()` returns, replaces any public sandbox's ruleset in one `nft -f`, entering its namespaces through fds checked against the ones recorded at creation. - With `dns` set, the create sends each resolver one UDP byte from inside the sandbox and fails, naming the resolver, if the send fails. On Cilium, which translates the cluster DNS Service IP before the filter sees it, every lookup failed instead. - The gRPC facade maps `block_network`, `outbound_cidr_allowlist`, and `TaskSetNetworkAccess`; open access means `internet`. - The REST API answers 403 to `network="host"`, `"sandbox"`, or `rootless=false` unless `allowed_network_modes` / `allow_rootful` allow them, and `build_app` requires `RAY_SANDBOX_API_TOKEN` unless `allow_unauthenticated` is set. Behavior changes: `public` needs `nft` on the node, no longer reaches private networks without a list, and fails to create when a `dns` resolver is unreachable. REST deployments need a token. ### Why nftables inside the sandbox netns is the right enforcement point Inside the sandbox's private user + net namespace, the host-side launch script has CAP_NET_ADMIN over that child network namespace (without needing CAP_NET_ADMIN on the Kubernetes worker pod itself). runsc runs inside that child netns with --network=host. That means gVisor's hostinet creates real Linux kernel AF_INET/AF_INET6 sockets inside the child netns, so every packet from the sandbox passes through the Linux kernel's nf_tables output chain before reaching slirp4netns's tap0. **Why sandbox code cannot tamper with the rules (even with CAP_NET_ADMIN):** gVisor's Sentry intercepts all syscalls inside the sandbox. Even if the container process has CAP_NET_ADMIN or CAP_NET_RAW, gVisor's hostinet (without --net-raw) never forwards AF_NETLINK (NETLINK_NETFILTER / NETLINK_ROUTE) or AF_PACKET sockets to the host kernel. Running nft flush ruleset or iptables -F inside the sandbox talks only to gVisor's internal Sentry stub, never touching the host kernel netns where the rules live. ### Results TCP connects from a sandbox in a CoreWeave (Cilium) pod running a Ray head, 5 s timeout, measured on dddd170: | Destination | Default | `ALLOW_ALL_CIDRS` | + `RAY_SANDBOX_EGRESS_DENY=<node>,<API server>` | | --- | --- | --- | --- | | GCS, Ray client server, raylet, object manager, dashboard | timeout | open | timeout | | 169.254.169.254:80 | timeout | timeout | timeout | | Kubernetes API (Service and endpoint) | timeout | open | timeout | | 1.1.1.1:443, DNS | open | open | open | Allow-all reaches the Ray head's ports because Ray doesn't track its nodes, and it reaches the API Service because Cilium rewrites it to the API server's address at connect time; `RAY_SANDBOX_EGRESS_DENY` closes both, and the docs say so. Root in a sandbox with `CAP_NET_ADMIN` and `CAP_NET_RAW` ran `nft flush ruleset` and `iptables -F`: both failed with "Protocol not supported", and GCS stayed closed. How to review: `config.py`, `_internal/egress_guard.py`, `backend/gvisor.py` (`_render_egress_ruleset`, the run command, `set_egress_allowlist`, `_unreachable_resolver`), `http/host.py` and `http/grpc_facade.py`, `http/schemas.py` and `http/app.py`, then the docs. Skip the `_pb2.py` files. ## Related issues Follows ray-project#65839. Found through SkyRL's SWE-bench Multimodal runs on Ray Sandbox (skyrl-project/SkyRL#2467). ray-project#66782 and ray-project#67010 inherit the default. ray-project#66471's restore path needs the same nft install. ## Additional information - Not a duplicate: none of the 20 open sandbox PRs adds egress filtering or a default deny. - Tests on a CoreWeave pod (Cilium, runsc release-20261012.0, root), rebased on master 7073066: `TEST_SANDBOX=1 pytest tests/test_gvisor_backend.py tests/test_image_manager.py tests/test_image_utils.py tests/test_sandbox_config.py tests/test_sandbox_runtime.py tests/test_egress_guard.py -k "not netns and not test_create_sandbox_helper"`: 167 passed, 1 skipped. `-k netns`: 22 passed. `test_create_sandbox_helper`: 1 passed. `tests/test_sandbox.py`: 7 passed. `http/tests`: 167 passed, 10 skipped. With the Modal SDK 1.5.5 installed on macOS, which CI doesn't have, `http/tests`: 175 passed, 2 skipped (the gVisor integration tests). On the pod, `dns=["10.16.0.10"]`, the cluster DNS Service, now fails the create with "Operation not permitted" and still resolves with `ALLOW_ALL_CIDRS`; the check adds about 14 ms to a create. pre-commit passes. This round wasn't rerun as uid 1000. - I used Claude Code to help write and test this change. --------- Signed-off-by: xyuzh <[email protected]> Co-authored-by: Philipp Moritz <[email protected]> Signed-off-by: Pawel Beza <[email protected]> Co-authored-by: Copilot <[email protected]> Copilot-Session: 4b9df5c4-6ec3-4e1f-ac3e-54f320ebd8c4
This PR introduces the foundation for a modal compatible sandboxing API to ray to create and interact with sandboxes through a Modal like surface. Signed-off-by: paa1750 <[email protected]> Co-authored-by: Claude <[email protected]>
Description This PR adds support for a modal like Image builder that supports building image filesystems through APIs like pip_install(), run_commands(), etc. It implements the python API surface and the backend image builder and filesystem integration to support starting sandboxes with custom built images Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: paa1750 <[email protected]>
sunnylovestiramisu
left a comment
There was a problem hiding this comment.
This bundles unmerged #66782 (bb61f1ef5) plus ~5.5k non-test LOC (3dd4ae365) spanning image-cache lock refactors, an EROFS reader/layer applier, a core gVisor ImageBuilder, a Sandbox.create cancellation fix, and the Modal Image DSL. Once the base slices from #66782 land, please unstack bb61f1ef5 and split 3dd4ae365 into smaller PRs:
- Image-cache
.build-lockssplit, reproduciblemkfs.erofsflags, andinvalidate_cached_imagecache-key fix (_internal/image_utils.py,image_manager.py+ tests) - Pure-Python
ErofsImagereader, incremental layer applier, andImageBuildPlanschema (_internal/erofs_reader.py,_internal/image_layers.py,image_build.py,exceptions.py, two-pass layer eviction in_internal/image_utils.py+ tests; after 1) - Core gVisor
export_rootfs_upper,ImageBuilder, andSandboxRuntime.build_image(backend/base.py,backend/gvisor.py,config.py,image_manager.py,image_builder.py,runtime.py+ tests; after 2, independent ofmodal/) - Reclaim in-flight actor and container when
modal.Sandbox.createis cancelled (modal/sandbox.py,modal/_actor.py,modal/tests/test_modal_create_cancel.py; parallel with 1-3) - Modal
Imagebuild DSL, build-context packaging, and docs (modal/_image_build.py,modal/image.py,modal/_resolved_image.py,modal/exception.py,modal/sandbox.py,modal/_actor.py,doc/source/core/sandboxes.md+ Modal image build tests; after 3 and 4)
## Description `readonly=True` is the default today, and it buys little. A read-only sandbox boots no faster than a writable one (27 ms both), and it's slower once it does anything: with an explicit `workdir` it needs a kernel overlay and takes 87 ms to create, and Python's stdlib imports take 203 ms instead of 45 ms because nothing can cache `.pyc` files. On a worker that can't mount a kernel overlay, a read-only sandbox with a `workdir` already ran writable, with a warning. The mode also needs special cases across the package, which keep complicating other sandbox PRs. This PR removes the option. Every sandbox writes to a private overlay over its cached image, which is discarded with the sandbox. That deletes: - the host-backed scratch `workdir` and the `/tmp` tmpfs, - the kernel overlay for missing mount points and the writable-root fallback (`needs_rootfs_overlay` is hooks only now), - `_seed_mountpoints` in the image build, since runsc creates mount points in the sandbox's overlay, - the `workdir` branches in `disk` capping (`--overlay2 all:`), checkpoint, and restore. About 600 lines net. Read `image_manager.py` and `backend/gvisor.py` first. Most of the rest is tests and docs. Behavior changes: - The root filesystem is writable by default. A `workdir` is a plain directory in the overlay, and `/tmp` is on the root filesystem. - `readonly=False` passed to `create()`, `restore()`, `Sandbox`, or `SandboxRuntime` is ignored with a `RayDeprecationWarning`, and the HTTP API ignores `readonly: false`, so callers that pass it keep working. `readonly=True` raises a `ValueError`, and the HTTP API answers `422`, because ignoring it would hand the caller a writable sandbox. - `disk` works without a `workdir`, and counts the `workdir` too. - A relative `workdir` raises a `ValueError`, or gets a `422` over HTTP, instead of failing when the container starts. - Restore refuses a checkpoint of a `readonly=True` sandbox, whose saved state depends on the read-only root. Checkpoints of writable sandboxes restore as before. Restore also refuses a different `workdir`, which runsc rejects anyway. - Without `disk`, any sandbox can now fill the node's disk. A node-wide default cap is a follow-up. - `commit()` from #67972 works on every sandbox, so its read-only check is gone. Its new layer leaves out the mount points runsc creates when the base image lacks them, such as an empty `/etc/hosts`. ## Related issues None filed. This comes from review feedback that the read-only mode complicates other sandbox PRs. ## Additional information - Not a duplicate: no open PR removes the option. It overlaps #67810 (its `_seed_erofs_image` pass can go), #67076 (the fallback's warning), and #67077 and #67728 (`readonly` in the warm-pool key). #67972 merged since, and the merge with master drops its read-only check. #66782 and #67010 pass `readonly=False`, which still works. - Numbers: measured on master on November 9 with `python:3.10-slim`, medians of 10 rounds. - Tests: new tests cover the writable default, `disk` with and without a `workdir`, the ignored `readonly=False` and the refused `readonly=True` in Python and over HTTP, the refused read-only checkpoint, and an image with only `/bin` and `/lib`, which boots with host mounts, `/etc/hosts`, and `/etc/resolv.conf`. I also booted that image from a kernel overlay with a hook, and as a non-root user. - Tested in a privileged container with runsc release-20261026.0, as root. `pytest python/ray/experimental/sandbox/tests`: 651 passed, 39 skipped (no GPU, `erofsfuse`, or delegated cgroup there), 3 failed. The 3 are GPU and CDI tests that fail the same way on master in that container, whose Ray core predates `get_cdi_kind`. `pytest python/ray/experimental/sandbox/http/tests`: 185 passed, 10 skipped. The skipped ones are the Modal SDK tests, which passed on the first commit with the SDK installed. pre-commit and the API param coverage lint pass. - I used Claude Code to help write and test this change. --------- Signed-off-by: xyuzh <[email protected]>
Description
This PR adds support for a modal like Image builder that supports building image filesystems
through APIs like pip_install(), run_commands(), etc. It implements the python API surface and
the backend image builder and filesystem integration to support starting sandboxes with custom
built images
Related issues
stacked on #66782