Skip to content

Commit 0b641f0

Browse files
committed
mount: honor a claimed transform as a chain suffix
A claimed transform was recorded but never actually left unapplied: every parsed transformer in a mount's chain was applied in full regardless of what the caller had claimed, so a shim advertising it performs "mkdir" in "format/mkdir/overlay" never got the chance to, since the manager silently applied it anyway. This predates the shim capability protocol — it was reachable before through the "format/*" annotation convention — but this series turns it into a documented, supported way for a shim to claim a transform, so a shim relying on it (at least one is: Kata's EROFS snapshotter shim, per its annotation usage) would find its claim silently ignored. A claimed transform can only be honored as a suffix of the chain: each transform's input is the previous one's output, so an outer, unclaimed transform must still be applied even when an inner one is claimed, since the inner one cannot run without it. planActivation now tracks, per mount, how much of its chain — starting from the beginning — the manager must apply itself: through the last unclaimed transform, which by construction covers every transform before it too. Applying only that prefix and returning the true, contiguous claimed suffix to the caller is what actually makes the claim meaningful. This is only observable for mounts[firstSystemMount], the one mount partially transformed by the manager and returned to the caller to finish; every earlier mount is fully internal to the manager and was, and remains, always fully transformed regardless of any claim. While changing this expression, also guard it against the case where firstSystemMount reaches len(mounts) — every mount consumed inside the manager, with no system mount left to transform — which indexes out of range today. This is a distinct, pre-existing latent panic (for example, reachable via WithTemporary combined with a transformed mount type), tracked separately from the claim-handling fix above; guarding it here only prevents a crash newly reachable by touching this line, it does not address the mount.ActivationInfo.System non-empty invariant that scenario also violates. Signed-off-by: Derek McGowan <[email protected]>
1 parent 8ba69fa commit 0b641f0

3 files changed

Lines changed: 107 additions & 9 deletions

File tree

‎core/mount/manager/manager.go‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,17 @@ func (mm *mountManager) Activate(ctx context.Context, name string, mounts []moun
408408
mounted = append(mounted, active)
409409
}
410410

411-
// If first system mount is converted, fill in the format
412-
if mountConv != nil {
413-
for _, tr := range mountConv[firstSystemMount] {
411+
// If the first system mount has transforms, apply the ones the caller
412+
// has not claimed. A claim can only be honored as a suffix of the
413+
// chain: applyCount always covers at least every transform up to and
414+
// including the last unclaimed one, since each depends on the last's
415+
// output; see planActivation.
416+
//
417+
// firstSystemMount can reach len(mounts) when every mount is handled
418+
// inside the manager (for example, every mount claimed via Temporary);
419+
// there is then no system mount left to transform.
420+
if mountConv != nil && firstSystemMount < len(mounts) {
421+
for _, tr := range mountConv[firstSystemMount][:plan.applyCount[firstSystemMount]] {
414422
newM, err := tr.Transform(ctx, mounts[firstSystemMount], mounted)
415423
if err != nil {
416424
return mount.ActivationInfo{}, err

‎core/mount/manager/plan.go‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,18 @@ type activationPlan struct {
4242
// prefix.
4343
transforms [][]mount.Transformer
4444

45+
// applyCount[i] is how many of transforms[i], from the start, the
46+
// manager must apply itself: through the last transform in the chain the
47+
// caller did not claim. Meaningful only for mounts[firstSystemMount],
48+
// the one mount returned to the caller with some of its own transforms
49+
// possibly still unapplied; every earlier mount's transforms are always
50+
// applied in full; they are internal to the manager and never exposed
51+
// to the caller. A transform, once applied, resolves the input the next
52+
// one in the chain depends on, so a run of claimed transforms can only
53+
// be honored as a suffix: transforms strictly after the last unclaimed
54+
// one in the chain.
55+
applyCount []int
56+
4557
// handlers[i] is the Handler to use for mounts[i], or nil to perform a
4658
// plain system mount.
4759
handlers []mount.Handler
@@ -51,6 +63,16 @@ type activationPlan struct {
5163
// must apply and whether the manager or the caller performs the mount
5264
// itself, honoring the mount types and transforms config.AllowMountTypes and
5365
// config.AllowTransforms claim.
66+
//
67+
// A claimed transform is honored only for mounts[firstSystemMount] in the
68+
// result, and only as a suffix of its transform chain: transforms apply
69+
// outside-in, so an inner transform's input depends on an outer, unclaimed
70+
// transform's output, and the manager must still apply that outer transform
71+
// even though the caller performs the suffix itself. For example, in
72+
// "format/mkdir/overlay", claiming "mkdir" causes the manager to apply
73+
// "format" and hand the caller "mkdir/overlay" to finish; claiming "format"
74+
// alone does nothing, since "mkdir" cannot run without it having already
75+
// run.
5476
func planActivation(ctx context.Context, mounts []mount.Mount, config mount.ActivateOptions, transforms map[string]mount.Transformer, handlers map[string]mount.Handler) activationPlan {
5577
shouldTransform := func(p string, t string) bool {
5678
return !slices.Contains(config.AllowTransforms, p) && !slices.Contains(config.AllowMountTypes, t)
@@ -73,7 +95,10 @@ func planActivation(ctx context.Context, mounts []mount.Mount, config mount.Acti
7395
break
7496
}
7597

76-
if shouldTransform(transformType, mounts[i].Type) { //nolint:gosec // G602: i is bounded by range mounts
98+
// mustApply reports whether the manager itself must apply this
99+
// transform, as opposed to the caller having claimed it.
100+
mustApply := shouldTransform(transformType, mounts[i].Type) //nolint:gosec // G602: i is bounded by range mounts
101+
if mustApply {
77102
// At least everything before this must be mounted
78103
// by the mount manager
79104
plan.firstSystemMount = i
@@ -85,11 +110,21 @@ func planActivation(ctx context.Context, mounts []mount.Mount, config mount.Acti
85110
if plan.transforms == nil {
86111
plan.transforms = make([][]mount.Transformer, len(mounts))
87112
}
113+
if plan.applyCount == nil {
114+
plan.applyCount = make([]int, len(mounts))
115+
}
88116

89117
plan.transforms[i] = append(plan.transforms[i], typeTransformer{
90118
Transformer: tr,
91119
mountType: mt,
92120
})
121+
if mustApply {
122+
// Every transform up to and including this one must be
123+
// applied by the manager: this one because it is unclaimed,
124+
// and every one before it because this one's input depends
125+
// on their output.
126+
plan.applyCount[i] = len(plan.transforms[i])
127+
}
93128

94129
mountType = mt
95130
}

‎core/mount/manager/plan_test.go‎

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,30 +65,47 @@ func TestPlanActivationTransformChain(t *testing.T) {
6565
name string
6666
opts []mount.ActivateOpt
6767
expectFirstSystem int
68+
expectApplyCount int
6869
}{
6970
{
70-
name: "nothing claimed requires the manager",
71+
// Nothing claimed: the manager must apply the whole chain, the
72+
// same as it always has.
73+
name: "nothing claimed applies the whole chain",
7174
expectFirstSystem: 0,
75+
expectApplyCount: 2,
7276
},
7377
{
74-
name: "a claimed transform still requires the manager for now",
78+
// Claiming the innermost transform lets the manager stop after
79+
// the one before it: mkdir is left for the caller.
80+
name: "innermost transform claimed is honored",
7581
opts: []mount.ActivateOpt{mount.WithAllowTransform("mkdir")},
7682
expectFirstSystem: 0,
83+
expectApplyCount: 1,
7784
},
7885
{
79-
name: "every transform claimed no longer requires the manager",
86+
// Claiming the outermost transform alone cannot be honored:
87+
// mkdir's input depends on format having already run.
88+
name: "outermost transform claimed alone is not honored",
89+
opts: []mount.ActivateOpt{mount.WithAllowTransform("format")},
90+
expectFirstSystem: 0,
91+
expectApplyCount: 2,
92+
},
93+
{
94+
name: "both transforms claimed applies neither",
8095
opts: []mount.ActivateOpt{
8196
mount.WithAllowTransform("format"),
8297
mount.WithAllowTransform("mkdir"),
8398
},
8499
expectFirstSystem: -1,
100+
expectApplyCount: 0,
85101
},
86102
{
87103
// Claiming the full literal mount type is equivalent to
88104
// claiming every transform in its chain.
89-
name: "full mount type claimed no longer requires the manager",
105+
name: "full mount type claimed applies neither",
90106
opts: []mount.ActivateOpt{mount.WithAllowMountType("format/mkdir/overlay")},
91107
expectFirstSystem: -1,
108+
expectApplyCount: 0,
92109
},
93110
} {
94111
t.Run(tc.name, func(t *testing.T) {
@@ -99,11 +116,33 @@ func TestPlanActivationTransformChain(t *testing.T) {
99116

100117
plan := planActivation(context.Background(), mounts, config, testTransforms, nil)
101118
assert.Equal(t, tc.expectFirstSystem, plan.firstSystemMount)
102-
require.Len(t, plan.transforms[0], 2)
119+
if tc.expectFirstSystem >= 0 {
120+
require.NotNil(t, plan.applyCount)
121+
assert.Equal(t, tc.expectApplyCount, plan.applyCount[0])
122+
require.Len(t, plan.transforms[0], 2)
123+
}
103124
})
104125
}
105126
}
106127

128+
// TestPlanActivationClaimedGapInMiddle covers a three transform chain where
129+
// an unclaimed transform sits between two claimed ones: the middle one still
130+
// forces the manager to apply everything up to and including it, regardless
131+
// of the outer claim, and only the true suffix after it is left claimed.
132+
func TestPlanActivationClaimedGapInMiddle(t *testing.T) {
133+
mounts := []mount.Mount{{Type: "format/mkfs/mkdir/overlay"}}
134+
config := mount.ActivateOptions{}
135+
mount.WithAllowTransform("format")(&config)
136+
mount.WithAllowTransform("mkdir")(&config)
137+
// mkfs is left unclaimed.
138+
139+
plan := planActivation(context.Background(), mounts, config, testTransforms, nil)
140+
require.Equal(t, 0, plan.firstSystemMount)
141+
require.Len(t, plan.transforms[0], 3)
142+
// format(0) and mkfs(1) must be applied; mkdir(2) is left to the caller.
143+
assert.Equal(t, 2, plan.applyCount[0])
144+
}
145+
107146
func TestPlanActivationUnknownTransform(t *testing.T) {
108147
mounts := []mount.Mount{{Type: "unknown/overlay"}}
109148
plan := planActivation(context.Background(), mounts, mount.ActivateOptions{}, testTransforms, nil)
@@ -173,3 +212,19 @@ func TestPlanActivationTemporary(t *testing.T) {
173212
require.Len(t, plan.handlers, 1)
174213
assert.Equal(t, 1, plan.firstSystemMount)
175214
}
215+
216+
func TestPlanActivationTemporaryIgnoresTransformClaims(t *testing.T) {
217+
mounts := []mount.Mount{{Type: "format/mkdir/overlay"}}
218+
config := mount.ActivateOptions{}
219+
mount.WithTemporary(&config)
220+
mount.WithAllowTransform("format")(&config)
221+
mount.WithAllowTransform("mkdir")(&config)
222+
223+
plan := planActivation(context.Background(), mounts, config, testTransforms, nil)
224+
// Temporary forces this, the only mount, to be handled, advancing
225+
// firstSystemMount past it. It does not change how much of a transform
226+
// chain is required, which is governed solely by what is claimed: both
227+
// transforms here are claimed, so applyCount stays at 0.
228+
assert.Equal(t, 1, plan.firstSystemMount)
229+
assert.Equal(t, 0, plan.applyCount[0])
230+
}

0 commit comments

Comments
 (0)