Repository navigation
Fix input mutation in mount option helpers - #13433
Conversation
There was a problem hiding this comment.
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/RemoveIDMapOptionto avoid mutating input slices via a sharedfilterMountOptionshelper. - Updates
readonlyMountsto clone the mounts slice before applying readonly option transformations. - Fixes tests to deep-clone
[]Mount(includingOptions) 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.
| // 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") |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Yes, it seems expected.
Better to have a comment line for explicitness though.
| out = copyMounts(mounts) | ||
| out = slices.Clone(mounts) | ||
| } | ||
| out[i].Options = append(out[i].Options[:j], out[i].Options[j+1:]...) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
| if opt == "volatile" || opt == "fsync=volatile" { | ||
| if out == nil { | ||
| out = copyMounts(mounts) | ||
| out = slices.Clone(mounts) |
There was a problem hiding this comment.
note that slices.Clone returns nil of the input was nil; would the line below panic in that case?
There was a problem hiding this comment.
If mounts is nil, we won't even reach this point though?
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]>
2d3287a to
165abaf
Compare
There was a problem hiding this comment.
🟡 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
readonlyMounts,RemoveVolatileOption,RemoveIDMapOptionhelpers 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.