Skip to content

Fix input mutation in mount option helpers - #13433

Merged
dmcgowan merged 6 commits into
containerd:mainfrom
vvoland:mounts-nomutate
Sep 9, 2026
Merged

dmcgowan merged 6 commits into
containerd:mainfrom
vvoland:mounts-nomutate

Conversation

@vvoland

@vvoland vvoland commented May 19, 2026

Copy link
Copy Markdown
Contributor

readonlyMounts, RemoveVolatileOption, RemoveIDMapOption helpers operated on their input slices directly, unexpectedly mutating the caller's data.

The existing no-mutation test assertions also suffered from the same shallow-copy problem, so they could not detect the corruption.

@vvoland vvoland self-assigned this May 19, 2026
Copilot AI balanced review requested due to automatic review settings May 19, 2026 16:10
@github-project-automation github-project-automation Bot moved this to Needs Triage in Pull Request Review May 19, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes unintended caller-data mutation in mount option helper functions under core/mount, and strengthens tests to correctly detect mutations by using deep clones of mount option slices.

Changes:

  • Refactors RemoveVolatileOption / RemoveIDMapOption to avoid mutating input slices via a shared filterMountOptions helper.
  • Updates readonlyMounts to clone the mounts slice before applying readonly option transformations.
  • Fixes tests to deep-clone []Mount (including Options) before assertions; adds coverage for multiple volatile options and for ID map option removal.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
core/mount/temp.go Reworks option-removal helpers to be non-mutating using a common filter function.
core/mount/mount.go Ensures readonlyMounts does not mutate the caller’s []Mount slice.
core/mount/mount_test.go Updates tests to deep-clone inputs and adds additional cases to catch slice mutation.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread core/mount/temp.go
// RemoveIDMapOption copies and removes the uidmap/gidmap options on any of the mounts using it.
func RemoveIDMapOption(mounts []Mount) []Mount {
return filterMountOptions(mounts, func(_ Mount, opt string) bool {
return !strings.HasPrefix(opt, "uidmap") && !strings.HasPrefix(opt, "gidmap")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmm I think this is expected?

If there's other option with "uidmap" prefix it's probably not an accident and is actually related to the id map which this func is supposed to strip.

fuse-overlayfs actually has options like (g/u)idmapping so they also were handled with this code: https://github.com/containers/fuse-overlayfs/blob/main/fuse-overlayfs.1.md

@AkihiroSuda any thoughts?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, it seems expected.
Better to have a comment line for explicitness though.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added comment

@vvoland
vvoland marked this pull request as ready for review May 19, 2026 17:06
Comment thread core/mount/temp.go Resolved
@vvoland
vvoland requested a review from djdongjin July 17, 2026 09:11

@djdongjin djdongjin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

Comment thread core/mount/temp.go
out = copyMounts(mounts)
out = slices.Clone(mounts)
}
out[i].Options = append(out[i].Options[:j], out[i].Options[j+1:]...)

@djdongjin djdongjin Jul 18, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think the new impl also fixed a bug here: if there are 2 options to be filtered out, existing impl will eat one more option when it removes the 2nd one.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

actually it will remove the wrong one when removing the 2nd option, because m.Options[j] is now at out[i].Options[j-1] after the 1st option is removed

@vvoland vvoland moved this from Needs Triage to Needs Reviewers in Pull Request Review Jul 21, 2026
Comment thread core/mount/temp.go Outdated
if opt == "volatile" || opt == "fsync=volatile" {
if out == nil {
out = copyMounts(mounts)
out = slices.Clone(mounts)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

note that slices.Clone returns nil of the input was nil; would the line below panic in that case?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If mounts is nil, we won't even reach this point though?

@thaJeztah thaJeztah left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@github-project-automation github-project-automation Bot moved this from Needs Reviewers to Review In Progress in Pull Request Review Sep 8, 2026
@thaJeztah

Copy link
Copy Markdown
Member

@djdongjin @dmcgowan @AkihiroSuda PTAL

@thaJeztah thaJeztah added this to the 2.4 milestone Sep 8, 2026
@AkihiroSuda
AkihiroSuda requested a balanced review from Copilot September 8, 2026 13:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

The focused implementation preserves behavior while preventing input mutation, with appropriate regression coverage.

Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

The custom copyMounts helper is equivalent to stdlib's slices.Clone.

Signed-off-by: Paweł Gronowski <[email protected]>
readonlyMounts was modifying the caller's mounts slice in place by
writing directly to mounts[i].Options.
This caused WithReadonlyTempMount to mutate the caller's mount data.

Clone the mounts slice before modification so the original is preserved.

Signed-off-by: Paweł Gronowski <[email protected]>
…IDMapOption

slices.Clone only shallow-copies the []Mount slice, so cloned entries
still share their Options backing arrays with the originals.

Clone each mount's Options slice before modifying it.

Also fix the tests: the previous no-mutation assertions used
slices.Clone for the "original" snapshot, which has the same shallow
copy problem.

Signed-off-by: Paweł Gronowski <[email protected]>
RemoveVolatileOption and RemoveIDMapOption both need to drop selected
mount options without mutating caller input.

Extract the lazy copy and filtering loop into a shared helper so both
paths keep the same behavior and future fixes apply consistently.

Signed-off-by: Paweł Gronowski <[email protected]>
The filtered mount helpers documented copy semantics, but no-op inputs
could return the original mount and option slices. That made the
caller observe different aliasing behavior depending on whether an
option was removed.

Always build a copied result for filtered mount options and cover the
no-alias contract in the mount helper tests.

Signed-off-by: Paweł Gronowski <[email protected]>
Filtered mount option helpers only need to allocate when they remove
an option. No-op inputs can safely return the original slices because
the observable contract is that inputs are not mutated.

Document that behavior and keep the no-alias assertions scoped to
cases where the returned mounts differ from the input.

Signed-off-by: Paweł Gronowski <[email protected]>
Copilot AI review requested due to automatic review settings September 8, 2026 14:02
@vvoland
vvoland requested a review from AkihiroSuda September 8, 2026 14:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Unmodified mount options can still alias caller-owned slices, and the tests currently miss later entries.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Review details
  • Files reviewed: 3/3 changed files
  • Comments generated: 2
  • Review effort level: Balanced

Comment thread core/mount/mount_test.go Resolved
Comment thread core/mount/temp.go Resolved
@dmcgowan
dmcgowan added this pull request to the merge queue Sep 9, 2026
@dmcgowan dmcgowan moved this from Review In Progress to Merge on Green in Pull Request Review Sep 9, 2026
Merged via the queue into containerd:main with commit 1d2dd9e Sep 9, 2026
97 of 98 checks passed
@github-project-automation github-project-automation Bot moved this from Merge on Green to Done in Pull Request Review Sep 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

7 participants