Skip to content

Commit

Permalink
fix: Security vulnerability of sudo session enablement (#2162)
Browse files Browse the repository at this point in the history
<!--
Please precisely, concisely, and concretely describe what this PR changes, the rationale behind codes,
and how it affects the users and other developers.
-->

Fixes #2151, follow-up of #1530.

This PR fixes the security vulnerability in  that was implemented using environment variables.

**Checklist:** (if applicable)

- [x] Milestone metadata specifying the target backport version
- [x] Mention to the original issue
- [x] Installer updates including:
  - New mandatory config options

Backported-from: main (24.09)
Backported-to: 24.03
Backport-of: 2162
  • Loading branch information
jopemachine committed May 27, 2024
1 parent c9cb953 commit c1dcae5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions changes/2162.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix security vulnerability for `sudo_session_enabled`
17 changes: 17 additions & 0 deletions src/ai/backend/agent/docker/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,23 @@ async def start_container(
await writer.write(f"{k}={v}\n")

await container.start()

if self.internal_data.get("sudo_session_enabled", False):
exec = await container.exec(
[
# file ownership is guaranteed to be set as root:root since command is executed on behalf of root user
"sh",
"-c",
'mkdir -p /etc/sudoers.d && echo "work ALL=(ALL:ALL) NOPASSWD:ALL" > /etc/sudoers.d/01-bai-work',
],
user="root",
)
shell_response = await exec.start(detach=True)
if shell_response:
raise ContainerCreationError(
container_id=cid,
message=f"sudoers provision failed: {shell_response.decode()}",
)
except asyncio.CancelledError:
if container is not None:
raise ContainerCreationError(
Expand Down
3 changes: 3 additions & 0 deletions src/ai/backend/manager/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,9 @@ async def enqueue_session(
internal_data = {} if internal_data is None else internal_data
internal_data.update(dotfile_data)

if sudo_session_enabled:
internal_data["sudo_session_enabled"] = True

hook_result = await self.hook_plugin_ctx.dispatch(
"PRE_ENQUEUE_SESSION",
(session_id, session_name, access_key),
Expand Down
5 changes: 0 additions & 5 deletions src/ai/backend/runner/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,6 @@ else
setsid ssh-add /home/work/.ssh/id_rsa < /dev/null
fi

# Enable sudo
if [ "$SUDO_SESSION_ENABLED" = "1" ]; then
echo "work ALL=(ALL:ALL) NOPASSWD:ALL" >> /etc/sudoers
fi

echo "Generate random alpha-numeric password"
if [ ! -f "$HOME/.password" ]; then
/opt/kernel/su-exec $USER_ID:$GROUP_ID /opt/backend.ai/bin/python -s /opt/kernel/fantompass.py > "$HOME/.password"
Expand Down

0 comments on commit c1dcae5

Please sign in to comment.