Skip to content

An open-source, faster Devin alternative

TypeScript

fix(deploy): target sudo executable paths

Igor Zalutski3bb499fa00ca
diff --git a/ci/flue-builder-substrate-probe.py b/ci/flue-builder-substrate-probe.py
index 510c4edf..75f00b02 100755
--- a/ci/flue-builder-substrate-probe.py
+++ b/ci/flue-builder-substrate-probe.py
@@ -455,7 +455,11 @@ if failures:
result = json.loads(str(response.get("stdout", "")))
except json.JSONDecodeError as exc:
raise ProbeError("guest-agent isolation probe returned invalid JSON") from exc
- if not isinstance(result.get("checked"), list) or result.get("failures") != []:
+ if (
+ not isinstance(result.get("checked"), list)
+ or not result["checked"]
+ or result.get("failures") != []
+ ):
raise ProbeError("guest-agent isolation probe returned an invalid result")
def prove_control_paths(self, sandbox_id: str) -> None:
diff --git a/ci/test_flue_builder_substrate_probe.py b/ci/test_flue_builder_substrate_probe.py
index b549616f..4df662ec 100644
--- a/ci/test_flue_builder_substrate_probe.py
+++ b/ci/test_flue_builder_substrate_probe.py
@@ -61,6 +61,17 @@ class SubstrateProbeTest(unittest.TestCase):
},
)
+ def test_sudo_scan_targets_entrypoints_not_package_directories(self) -> None:
+ installer = (PROBE.ROOT / "deploy" / "flue-builder" / "install-snapshot.sh").read_text()
+ entrypoint_scan = (
+ r"find / -xdev \( -type f -o -type l \) "
+ r"\( -name sudo -o -name sudoedit \)"
+ )
+ self.assertGreaterEqual(installer.count(entrypoint_scan), 2)
+ self.assertNotIn("find / -xdev -name sudo", installer)
+ self.assertIn("/usr/share/doc/sudo", installer)
+ self.assertIn("/usr/lib/*/sudo", installer)
+
def test_private_canary_requires_literal_private_or_cgnat_address(self) -> None:
for value in (
"http://10.1.2.3/probe",
@@ -128,6 +139,10 @@ class SubstrateProbeTest(unittest.TestCase):
with self.assertRaises(PROBE.ProbeError):
live.prove_guest_agent_isolation("sb-test")
+ live.exec.return_value = {"exitCode": 0, "stdout": '{"checked":[],"failures":[]}'}
+ with self.assertRaises(PROBE.ProbeError):
+ live.prove_guest_agent_isolation("sb-test")
+
def test_create_body_is_allowlisted_and_final_topology_is_explicit(self) -> None:
live = PROBE.LiveProbe.__new__(PROBE.LiveProbe)
live.snapshot_name = "snapshot-coordinate"
diff --git a/cmd/agent/listen_other.go b/cmd/agent/listen_other.go
index 6edb1c71..b45d5b5f 100644
--- a/cmd/agent/listen_other.go
+++ b/cmd/agent/listen_other.go
@@ -18,7 +18,7 @@ func listenVsock() (net.Listener, error) {
if err != nil {
return nil, err
}
- if err := restrictAgentEndpoint(sockPath); err != nil {
+ if err := restrictAgentEndpointForOwner(sockPath, os.Geteuid(), os.Getegid()); err != nil {
lis.Close()
os.Remove(sockPath)
return nil, err
@@ -36,7 +36,7 @@ func listenPortForPTY(port uint32) (net.Listener, error) {
if err != nil {
return nil, fmt.Errorf("pty unix listen port %d: %w", port, err)
}
- if err := restrictAgentEndpoint(sockPath); err != nil {
+ if err := restrictAgentEndpointForOwner(sockPath, os.Geteuid(), os.Getegid()); err != nil {
lis.Close()
os.Remove(sockPath)
return nil, err
diff --git a/cmd/agent/transport_security.go b/cmd/agent/transport_security.go
index 438cf85e..5dcb5b48 100644
--- a/cmd/agent/transport_security.go
+++ b/cmd/agent/transport_security.go
@@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
+ "syscall"
)
const (
@@ -22,8 +23,23 @@ func isTrustedAgentVsockPeer(cid uint32) bool {
// root agent. Host control continues through the already-open virtio-serial
// device or the host-owned side of the Unix socket.
func restrictAgentEndpoint(path string) error {
+ return restrictAgentEndpointForOwner(path, 0, 0)
+}
+
+func restrictAgentEndpointForOwner(path string, uid int, gid int) error {
+ if err := os.Chown(path, uid, gid); err != nil {
+ return fmt.Errorf("own agent endpoint %s: %w", path, err)
+ }
if err := os.Chmod(path, agentEndpointMode); err != nil {
return fmt.Errorf("restrict agent endpoint %s: %w", path, err)
}
+ info, err := os.Stat(path)
+ if err != nil {
+ return fmt.Errorf("stat agent endpoint %s: %w", path, err)
+ }
+ stat, ok := info.Sys().(*syscall.Stat_t)
+ if !ok || int(stat.Uid) != uid || info.Mode().Perm() != agentEndpointMode {
+ return fmt.Errorf("agent endpoint %s did not retain owner %d and mode %#o", path, uid, agentEndpointMode)
+ }
return nil
}
diff --git a/cmd/agent/transport_security_test.go b/cmd/agent/transport_security_test.go
index 712e09da..a7f17b91 100644
--- a/cmd/agent/transport_security_test.go
+++ b/cmd/agent/transport_security_test.go
@@ -3,6 +3,7 @@ package main
import (
"os"
"path/filepath"
+ "syscall"
"testing"
)
@@ -30,7 +31,7 @@ func TestRestrictAgentEndpointRemovesGuestAccess(t *testing.T) {
if err := os.WriteFile(path, nil, 0666); err != nil {
t.Fatal(err)
}
- if err := restrictAgentEndpoint(path); err != nil {
+ if err := restrictAgentEndpointForOwner(path, os.Geteuid(), os.Getegid()); err != nil {
t.Fatal(err)
}
info, err := os.Stat(path)
@@ -40,4 +41,36 @@ func TestRestrictAgentEndpointRemovesGuestAccess(t *testing.T) {
if got := info.Mode().Perm(); got != agentEndpointMode {
t.Fatalf("endpoint mode = %#o, want %#o", got, agentEndpointMode)
}
+ stat, ok := info.Sys().(*syscall.Stat_t)
+ if !ok {
+ t.Fatal("endpoint stat has no syscall metadata")
+ }
+ if got := int(stat.Uid); got != os.Geteuid() {
+ t.Fatalf("endpoint uid = %d, want agent uid %d", got, os.Geteuid())
+ }
+}
+
+func TestRestrictAgentEndpointRequiresRootOwnership(t *testing.T) {
+ path := filepath.Join(t.TempDir(), "root-agent-endpoint")
+ if err := os.WriteFile(path, nil, 0666); err != nil {
+ t.Fatal(err)
+ }
+ err := restrictAgentEndpoint(path)
+ if os.Geteuid() != 0 {
+ if err == nil {
+ t.Fatal("non-root agent unexpectedly created a root-owned endpoint")
+ }
+ return
+ }
+ if err != nil {
+ t.Fatal(err)
+ }
+ info, err := os.Stat(path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ stat := info.Sys().(*syscall.Stat_t)
+ if stat.Uid != 0 || info.Mode().Perm() != agentEndpointMode {
+ t.Fatalf("root endpoint uid:mode = %d:%#o", stat.Uid, info.Mode().Perm())
+ }
}
diff --git a/deploy/flue-builder/coordinate.json b/deploy/flue-builder/coordinate.json
index 5d330db7..8f0533b2 100644
--- a/deploy/flue-builder/coordinate.json
+++ b/deploy/flue-builder/coordinate.json
@@ -9,10 +9,10 @@
},
"recipe": {
"installer": "install-snapshot.sh",
- "installerSha256": "0e4eeb46adb311a783890cfe8afff90e88dbff5d2e2bef7c3decba0318b911aa",
+ "installerSha256": "8210ac33b9690de36689a97851cd7ac8b29c0d0fc8c25fe0454fbc1486875ce9",
"imageTemplate": "image.json",
"imageTemplateSha256": "e5e8827412e76e6da94f8c2f9b97ddfe25579637905ef4e6d2f7fa563712683f",
- "materializedManifestSha256": "d16ba78183df34ea6cfc55e76a78346430bf78c0222a9150dca032762ce91190"
+ "materializedManifestSha256": "8b11a2f879a2ceedc2bd858019a886f06e9dacec43b719f9cf23447b7989d9a5"
},
"attestation": {
"schemaVersion": 1,
diff --git a/deploy/flue-builder/install-snapshot.sh b/deploy/flue-builder/install-snapshot.sh
index e67cdf57..b0c8d088 100755
--- a/deploy/flue-builder/install-snapshot.sh
+++ b/deploy/flue-builder/install-snapshot.sh
@@ -234,8 +234,8 @@ printf '%s %s\n' "$attestation_sha" "$ATTESTATION" | sha256sum --check --status
[[ ! -w "/opt/opencomputer/bin" ]] || fail "toolchain bin directory is writable"
[[ ! -w "$NODE_ROOT" ]] || fail "Node toolchain is writable"
[[ ! -e /usr/bin/sudo && ! -e /bin/sudo ]] || fail "sudo executable was restored"
-[[ -z "\$(find / -xdev -name sudo -print -quit 2>/dev/null)" ]] \
- || fail "a sudo path remains on the runtime root filesystem"
+[[ -z "\$(find / -xdev \( -type f -o -type l \) \( -name sudo -o -name sudoedit \) -print -quit 2>/dev/null)" ]] \
+ || fail "a sudo executable entrypoint remains on the runtime root filesystem"
[[ -d "$WORKSPACE" && -w "$WORKSPACE" ]] || fail "workspace is not writable"
probe="\$(mktemp "$WORKSPACE/.flue-builder-write.XXXXXX")" \
|| fail "cannot create a workspace file"
@@ -264,10 +264,14 @@ remove_runtime_privilege_surfaces() {
# Sudoers is a rich policy language (aliases, groups, includes, commands).
# Trying to prove every policy path denied is brittle, so the managed image
# removes the setuid entrypoint itself after the last trusted sudo command.
- # Search the root filesystem rather than assuming one package path.
- find / -xdev -name sudo -exec rm -f -- {} + 2>/dev/null
- [[ -z "$(find / -xdev -name sudo -print -quit 2>/dev/null)" ]] \
- || die "could not remove every sudo path"
+ # Search the root filesystem rather than assuming one package path. Remove
+ # regular files and symlinks named sudo/sudoedit, including alternate PATH
+ # locations and the sudoedit entrypoint, while preserving inert package
+ # directories such as /usr/share/doc/sudo and /usr/lib/*/sudo.
+ find / -xdev \( -type f -o -type l \) \( -name sudo -o -name sudoedit \) \
+ -exec rm -f -- {} + 2>/dev/null
+ [[ -z "$(find / -xdev \( -type f -o -type l \) \( -name sudo -o -name sudoedit \) -print -quit 2>/dev/null)" ]] \
+ || die "could not remove every sudo executable entrypoint"
}
harden_agent_transport_nodes() {
diff --git a/deploy/flue-builder/snapshot.py b/deploy/flue-builder/snapshot.py
index 13e1bbb2..6fe66026 100755
--- a/deploy/flue-builder/snapshot.py
+++ b/deploy/flue-builder/snapshot.py
@@ -202,13 +202,23 @@ def check_recipe() -> tuple[dict[str, Any], dict[str, Any]]:
for command in (
'[[ "$(id -G)" == "1000" ]]',
"sudo executable was restored",
- "a sudo path remains on the runtime root filesystem",
+ "a sudo executable entrypoint remains on the runtime root filesystem",
"sha256sum --check --status",
'mktemp "/workspace/.flue-builder-write.XXXXXX"',
):
if command not in runtime_verifier.stdout:
raise SnapshotError(f"generated runtime verifier is missing {command!r}")
+ sudo_entrypoint_scan = (
+ r"find / -xdev \( -type f -o -type l \) "
+ r"\( -name sudo -o -name sudoedit \)"
+ )
+ installer_source = installer_path.read_text()
+ if installer_source.count(sudo_entrypoint_scan) < 2:
+ raise SnapshotError(
+ "finalizer and runtime verifier must scan sudo executable entrypoints without matching directories"
+ )
+
return coordinate, manifest