Skip to content

Commit 3c1b583

Browse files
peterschmidt85Andrey Cheptsovclaude
authored
Fix SELinux denials on SSH fleet provisioning (#3702)
On SELinux-enforcing hosts (RHEL, Rocky), files moved from /tmp retain their original SELinux context. systemd (init_t) cannot read files with user_tmp_t or unconfined_u context, causing the shim service to fail. Fix by adding chcon after mv to set correct SELinux contexts for the service file (systemd_unit_file_t) and env file (etc_t). The chcon is a no-op on non-SELinux systems via 2>/dev/null || true. Also replace mv with cp+rm for the shim binary download to ensure correct context in /usr/local/bin/. Co-authored-by: Andrey Cheptsov <andrey.cheptsov@github.com> Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 77f3be1 commit 3c1b583

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

src/dstack/_internal/core/backends/base/compute.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ def get_shim_pre_start_commands(
906906
f"dlpath=$(sudo mktemp -t {DSTACK_SHIM_BINARY_NAME}.XXXXXXXXXX)",
907907
# -sS -- disable progress meter and warnings, but still show errors (unlike bare -s)
908908
f'sudo curl -sS --compressed --connect-timeout 60 --max-time 240 --retry 1 --output "$dlpath" "{url}"',
909-
f'sudo mv "$dlpath" {dstack_shim_binary_path}',
909+
f'sudo cp "$dlpath" {dstack_shim_binary_path} && sudo rm "$dlpath"',
910910
f"sudo chmod +x {dstack_shim_binary_path}",
911911
f"sudo mkdir {dstack_working_dir} -p",
912912
]

src/dstack/_internal/server/services/ssh_fleets/provisioning.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ def upload_envs(client: paramiko.SSHClient, working_dir: str, envs: Dict[str, st
7373
tmp_file_path = f"/tmp/{DSTACK_SHIM_ENV_FILE}"
7474
sftp_upload(client, tmp_file_path, dot_env)
7575
try:
76-
cmd = f"sudo mkdir -p {working_dir} && sudo mv {tmp_file_path} {working_dir}/"
76+
dest = f"{working_dir}/{DSTACK_SHIM_ENV_FILE}"
77+
cmd = (
78+
f"sudo mkdir -p {working_dir} && sudo mv {tmp_file_path} {dest}"
79+
f" && {{ sudo chcon system_u:object_r:etc_t:s0 {dest} 2>/dev/null || true; }}"
80+
)
7781
_, stdout, stderr = client.exec_command(cmd, timeout=20)
7882
out = stdout.read().strip().decode()
7983
err = stderr.read().strip().decode()
@@ -148,6 +152,7 @@ def run_shim_as_systemd_service(
148152
try:
149153
cmd = """\
150154
sudo mv /tmp/dstack-shim.service /etc/systemd/system/dstack-shim.service && \
155+
{ sudo chcon system_u:object_r:systemd_unit_file_t:s0 /etc/systemd/system/dstack-shim.service 2>/dev/null || true; } && \
151156
sudo systemctl daemon-reload && \
152157
sudo systemctl --quiet enable dstack-shim && \
153158
sudo systemctl restart dstack-shim

0 commit comments

Comments
 (0)