← Back to Pull requests

libpod: honor volume subpath in cp #29842

Merged
pooyanazad merged 12 files (261 lines) on Oct 02, 2026 10:46 UTC
View on GitHub
pooyanazad pooyanazad commented on Sep 25, 2026 22:43 UTC
Author

When copying files from created or stopped containers, podman cp resolves named volumes on the host instead of using the container mount namespace. The host-side resolution currently starts at the volume root and ignores the mount's configured subpath, so cp can read a different file than the one visible inside the container.

Apply the configured volume subpath before resolving the remaining container path. Keep the path within the volume using secure path resolution and add coverage for created, running, and stopped containers.

Fixes: #29840

<!-- Thanks for sending a pull request! -->

Checklist

Ensure you have completed the following checklist for your pull request to be reviewed:
<!-- Use [x] to mark as done, or click the checkbox after opening PR -->

  • I have read and understood our contributing guidelines and will not have more than two open PRs as a new contributor.

  • PR description, commit message, and GitHub comments are human-written, per LLM Policy

  • Certify you wrote the patch or otherwise have the right to pass it on as an open-source patch by signing all commits. (git commit -s). (If needed, use git commit -s --amend). The author email must match the sign-off email address. See CONTRIBUTING.md for more information.

  • Referenced issues using Fixes: #00000 in commit message (if applicable)

  • Tests have been added/updated (or no tests are needed)

  • Documentation has been updated (or no documentation changes are needed)

  • All commits pass make validatepr (format/lint checks)

  • Release note entered in the section below (or None if no user-facing changes)
  • Does this PR introduce a user-facing change?

    <!--
    Write None if users do not need to take action. Otherwise enter the release note below.
    Include "action required" if users need to take action when upgrading.
    -->

    ``release-note
    podman cp now respects named-volume subpaths when copying files from created and stopped containers.
    `

    Validation

  • Focused Docker-backed system test passed: podman cp from named volume subpath

  • Go lint passed with 0 issues.

  • Pre-commit checks passed.

  • git diff --check passed.

  • Full make validatepr did not complete because the validation image was missing the Perl Test::Differences` module.
  • Diff: 261 lines (+198 / -63) across 12 files Reviewer: giuseppe Review duration: 9362m
    All Vouch review confidence checks passed
    Residual risk 0.05 = Change Risk (0.52) × (1 − Review Confidence (0.90)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 261 lines diff across 12 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.88 · 5 substantive comment(s)
    0.88
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 50%
    89%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.333
    Real PR Conversation Timeline
    16 items
    pooyanazad pooyanazad commented on Sep 29, 2026 08:09 UTC
    Author

    Hi! Just a quick follow-up on #29842.

    The CI is green now, and the PR includes regression coverage for both copy-in and copy-out with named-volume subpaths across created, running, and stopped containers.

    Whenever someone has a chance, I’d really appreciate a review. Thanks!

    Honny1 Honny1 commented on Sep 29, 2026 12:38 UTC
    Reviewer

    This PR only fixes named volume subpaths in resolvePath. Image mounts (type=image, subpath=…) still don’t work for podman cp when the container isn’t running. I get ENOENT on created contianer, while running cp works (see repro below).

    ctr="img-cp-test"
    ./bin/podman rm -f "$ctr" 2>/dev/null
    
    ./bin/podman create --name "$ctr" --network=none --mount "type=image,source=alpine,dst=/mnt,subpath=/bin" alpine sleep 3600
    ./bin/podman cp "$ctr:/mnt/ls" /tmp/from-created
    # expect: Error: no such file or directory (container not started)
    
    ./bin/podman start "$ctr"
    ./bin/podman cp "$ctr:/mnt/ls" /tmp/from-running
    # expect: success
    
    ./bin/podman exec "$ctr" ls -l /mnt/ls
    
    ./bin/podman rm -f "$ctr"
    

    Otherwise LGTM. It would be good to also fix this for images.

    giuseppe giuseppe commented on code on Sep 29, 2026 12:53 UTC
    Code Review
    libpod/container_path_resolution.go:1
    @@ -67,6 +67,13 @@ func (c *Container) resolvePath(mountPoint string, containerPath string) (string
     				return "", "", nil, fmt.Errorf("volume %s is not mounted, cannot copy into it", volume.Name())
     			}
     
    +			if subPath != "" {
    +				mountPoint, err = securejoin.SecureJoin(mountPoint, subPath)
    +				if err != nil {
    +					return "", "", nil, err
    +				}
    +			}
    +

    what is preventing mountPoint to be replaced with a symlink after this operation and later mountPoint points on the host?

    This can happen, for example, if the volume is shared

    pooyanazad pooyanazad commented on code on Sep 29, 2026 13:29 UTC
    Code Review
    libpod/container_path_resolution.go:1
    @@ -67,6 +67,13 @@ func (c *Container) resolvePath(mountPoint string, containerPath string) (string
     				return "", "", nil, fmt.Errorf("volume %s is not mounted, cannot copy into it", volume.Name())
     			}
     
    +			if subPath != "" {
    +				mountPoint, err = securejoin.SecureJoin(mountPoint, subPath)
    +				if err != nil {
    +					return "", "", nil, err
    +				}
    +			}
    +

    Good catch, thanks. You’re right that SecureJoin alone left a race here. I’ve pushed a fix that keeps the volume subpath pinned until the stat or copy finishes, and added a regression test for replacing it with a symlink.

    giuseppe giuseppe commented on code on Sep 29, 2026 13:37 UTC
    Code Review
    libpod/container_path_resolution.go:1
    @@ -67,6 +67,13 @@ func (c *Container) resolvePath(mountPoint string, containerPath string) (string
     				return "", "", nil, fmt.Errorf("volume %s is not mounted, cannot copy into it", volume.Name())
     			}
     
    +			if subPath != "" {
    +				mountPoint, err = securejoin.SecureJoin(mountPoint, subPath)
    +				if err != nil {
    +					return "", "", nil, err
    +				}
    +			}
    +

    I wonder if we could just do securejoin.SecureJoin(mountPoint, filepath.Join(subPath, pathRelativeToVolume)): one single securejoin on the original mountPoint

    would that work?

    giuseppe giuseppe commented on code on Sep 29, 2026 13:59 UTC
    Code Review
    libpod/container_path_resolution.go:1
    @@ -81,9 +93,12 @@ func (c *Container) resolvePath(mountPoint string, containerPath string) (string
     			pathRelativeToVolume := strings.TrimPrefix(pathRelativeToContainerMountPoint, searchPath)
     			absolutePathOnTheVolumeMount, err := securejoin.SecureJoin(mountPoint, pathRelativeToVolume)
     			if err != nil {
    -				return "", "", nil, err
    +				if safeMount != nil {
    +					safeMount.Close()
    +				}
    +				return pathResolution{}, err
     			}
    -			return mountPoint, absolutePathOnTheVolumeMount, volume, nil
    +			return pathResolution{root: mountPoint, path: absolutePathOnTheVolumeMount, volume: volume, mount: safeMount}, nil

    we need to make sure all callers of resolvePath are closing the safeMount now to avoid a leak

    pooyanazad pooyanazad commented on code on Sep 29, 2026 14:22 UTC
    Code Review
    libpod/container_path_resolution.go:1
    @@ -67,6 +67,13 @@ func (c *Container) resolvePath(mountPoint string, containerPath string) (string
     				return "", "", nil, fmt.Errorf("volume %s is not mounted, cannot copy into it", volume.Name())
     			}
     
    +			if subPath != "" {
    +				mountPoint, err = securejoin.SecureJoin(mountPoint, subPath)
    +				if err != nil {
    +					return "", "", nil, err
    +				}
    +			}
    +

    I don’t think that closes the race, unfortunately. Even with one SecureJoin, we’d still get a path string that could change before stat or cp uses it. The open file descriptor and bind mount are what keep the selected subpath in place for the operation.
    I confused about where should I answer, so removed twice before :)

    pooyanazad pooyanazad commented on code on Sep 29, 2026 14:23 UTC
    Code Review
    libpod/container_path_resolution.go:1
    @@ -81,9 +93,12 @@ func (c *Container) resolvePath(mountPoint string, containerPath string) (string
     			pathRelativeToVolume := strings.TrimPrefix(pathRelativeToContainerMountPoint, searchPath)
     			absolutePathOnTheVolumeMount, err := securejoin.SecureJoin(mountPoint, pathRelativeToVolume)
     			if err != nil {
    -				return "", "", nil, err
    +				if safeMount != nil {
    +					safeMount.Close()
    +				}
    +				return pathResolution{}, err
     			}
    -			return mountPoint, absolutePathOnTheVolumeMount, volume, nil
    +			return pathResolution{root: mountPoint, path: absolutePathOnTheVolumeMount, volume: volume, mount: safeMount}, nil

    Good point. I went through the callers: both copy-in and copy-out keep the mount until the copy is done, and close it on errors too. resolvePath also closes it if the final SecureJoin fails. I don’t see a path where it gets left open.

    pooyanazad pooyanazad commented on Sep 29, 2026 14:51 UTC
    Author

    @Honny1 Thanks for checking this and for the repro. You’re right about image mounts. I’d like to keep this PR focused on #29840 and track the image-mount case separately.

    Honny1 Honny1 commented on Sep 30, 2026 15:37 UTC
    Contributor

    @Honny1 Thanks for checking this and for the repro. You’re right about image mounts. I’d like to keep this PR focused on #29840 and track the image-mount case separately.

    Sure, just please create an issue for that case. I don't want to forget about that.

    pooyanazad pooyanazad commented on Sep 30, 2026 15:51 UTC
    Author

    > @Honny1 Thanks for checking this and for the repro. You’re right about image mounts. I’d like to keep this PR focused on #29840 and track the image-mount case separately.

    > Sure, just please create an issue for that case. I don't want to forget about that.

    Hello, created:
    https://github.com/podman-container-tools/podman/issues/29857

    Honny1 Honny1 commented on Sep 30, 2026 15:56 UTC
    Contributor

    Thanks, it seems it will need to be rebased on upstream main.

    pooyanazad pooyanazad commented on Sep 30, 2026 16:37 UTC
    Author

    @Honny1 Thanks for the heads-up. I’ve rebased on upstream main and resolved the conflicts. Could you please review the PR so we can get it merged?

    TomSweeneyRedHat TomSweeneyRedHat commented on Oct 01, 2026 00:29 UTC
    Contributor

    LGTM
    once @giuseppe and @pooyanazad come to agreement on the securejoin question.

    giuseppe giuseppe approved these changes on Oct 01, 2026 13:10 UTC
    Approved Reviewer

    LGTM

    Reviewers
    Assignees
    pooyanazad pooyanazad
    Labels
    risk: low confidence: 89.6% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52.4% (MEDIUM) · Higher = larger blast radius
    52.4%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 89.6% · Lower = rushed or shallow review
    89.6%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 5.5% · Safe to merge
    5.5%