Skip to content

Conversation

@doringeman
Copy link
Contributor

Require callers to explicitly opt-in to root privileges when executing commands in containers. This avoids running all execs as root, which could violate security expectations for containers that define a non-root default user.

Based on #511 (comment).

Require callers to explicitly opt-in to root privileges when executing commands in containers. This avoids running all execs as root, which could violate security expectations for containers that define a non-root default user.

Signed-off-by: Dorin Geman <[email protected]>
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @doringeman, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a significant security enhancement by modifying the execInContainer function to require explicit opt-in for root privileges. This change ensures that commands executed within containers default to a non-root user, preventing unintended root execution and improving the overall security posture of container operations.

Highlights

  • Refactored execInContainer function: The execInContainer function now includes an asRoot boolean parameter, requiring callers to explicitly opt-in for root privileges when executing commands within a container.
  • Default non-root execution: The default behavior of execInContainer has been changed so that commands are no longer run as root by default, enhancing security by aligning with container best practices.
  • Updated existing calls: All existing calls to execInContainer have been updated to pass the new asRoot parameter. Commands related to Docker configuration (e.g., mkdir, chown, chmod) now explicitly run as non-root, while update-ca-certificates explicitly runs as root.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a beneficial security enhancement by refactoring execInContainer to require an explicit asRoot parameter, preventing commands from running with root privileges by default. The implementation of this change is sound. However, I've identified two instances where this new parameter is used incorrectly in copyDockerConfigToContainer. The commands being executed require root privileges because they use chown, but they are called with asRoot: false. This will lead to errors in containers with a non-root default user. My review provides specific suggestions to correct these calls.

// Ensure the .docker directory exists
mkdirCmd := "mkdir -p /home/modelrunner/.docker && chown modelrunner:modelrunner /home/modelrunner/.docker"
if err := execInContainer(ctx, dockerClient, containerID, mkdirCmd); err != nil {
if err := execInContainer(ctx, dockerClient, containerID, mkdirCmd, false); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The mkdirCmd includes a chown command, which requires root privileges to execute. With asRoot set to false, this command will fail if the container's default user is not root. To ensure the directory ownership can be set as intended, this command should be executed with root privileges.

Suggested change
if err := execInContainer(ctx, dockerClient, containerID, mkdirCmd, false); err != nil {
if err := execInContainer(ctx, dockerClient, containerID, mkdirCmd, true); err != nil {

// Set correct ownership and permissions
chmodCmd := "chown modelrunner:modelrunner /home/modelrunner/.docker/config.json && chmod 600 /home/modelrunner/.docker/config.json"
if err := execInContainer(ctx, dockerClient, containerID, chmodCmd); err != nil {
if err := execInContainer(ctx, dockerClient, containerID, chmodCmd, false); err != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Similar to the command for creating the directory, this chmodCmd also includes chown, which requires root privileges. Setting asRoot to false will cause this command to fail if the container is not running as root. To ensure the file's ownership and permissions are correctly set, this should be run as root.

Suggested change
if err := execInContainer(ctx, dockerClient, containerID, chmodCmd, false); err != nil {
if err := execInContainer(ctx, dockerClient, containerID, chmodCmd, true); err != nil {

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `cmd/cli/pkg/standalone/containers.go:71-74` </location>
<code_context>
 	// Ensure the .docker directory exists
 	mkdirCmd := "mkdir -p /home/modelrunner/.docker && chown modelrunner:modelrunner /home/modelrunner/.docker"
-	if err := execInContainer(ctx, dockerClient, containerID, mkdirCmd); err != nil {
+	if err := execInContainer(ctx, dockerClient, containerID, mkdirCmd, false); err != nil {
 		return err
 	}
</code_context>

<issue_to_address>
**issue (bug_risk):** Running chown/chmod without root is likely to fail and silently change behavior compared to the previous implementation.

Before this change, `execInContainer` always ran as root, so these `chown`/`chmod` calls would normally succeed. With `asRoot` now set to `false`, they run as the container’s default user, and `chown` (and sometimes `chmod` on root-owned paths) will often fail without error handling for permission issues. Please either run these commands with `asRoot = true` or rework the ownership/permission handling so it doesn’t depend on root, to avoid a behavioral regression that only shows up on some environments.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +71 to 74
if err := execInContainer(ctx, dockerClient, containerID, mkdirCmd, false); err != nil {
return err
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (bug_risk): Running chown/chmod without root is likely to fail and silently change behavior compared to the previous implementation.

Before this change, execInContainer always ran as root, so these chown/chmod calls would normally succeed. With asRoot now set to false, they run as the container’s default user, and chown (and sometimes chmod on root-owned paths) will often fail without error handling for permission issues. Please either run these commands with asRoot = true or rework the ownership/permission handling so it doesn’t depend on root, to avoid a behavioral regression that only shows up on some environments.

Copy link
Contributor

@ilopezluna ilopezluna left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@doringeman doringeman merged commit 3ac8e33 into docker:main Dec 15, 2025
9 checks passed
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.

2 participants