Skip to content

Commit 60b444f

Browse files
committed
Do not allow mounting to machine dir /tmp
the destination machine mount overwrote /tmp. Here I have added a sanity check. I also moved the volume parsing and check earlier in the init function so that one does not have to endure the decompression and clean up of the machine image for cli parsing. Fixes: containers#18230 Signed-off-by: Brent Baude <[email protected]>
1 parent 7f27a66 commit 60b444f

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

pkg/machine/e2e/init_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,20 @@ var _ = Describe("podman machine init", func() {
7777
badMemSession, err := mb.setCmd(badMem.withMemory(uint(total))).run()
7878
Expect(err).ToNot(HaveOccurred())
7979
Expect(badMemSession).To(Exit(125))
80+
81+
// Check that mounting to /tmp is caught
82+
tmpVol := initMachine{}
83+
tmpVolSession, err := mb.setCmd(tmpVol.withVolume("/whatever:/tmp")).run()
84+
Expect(err).ToNot(HaveOccurred())
85+
Expect(tmpVolSession).To(Exit(125))
86+
Expect(tmpVolSession.errorToString()).To(ContainSubstring("Error: machine mount destination cannot be /tmp: consider another location or a subdirectory of /tmp"))
87+
88+
// Mounting to /tmp/ <-- trailing slash is also caught
89+
tmpVolTrailer := initMachine{}
90+
tmpVolTrailerSession, err := mb.setCmd(tmpVolTrailer.withVolume("/whatever:/tmp/")).run()
91+
Expect(err).ToNot(HaveOccurred())
92+
Expect(tmpVolTrailerSession).To(Exit(125))
93+
Expect(tmpVolTrailerSession.errorToString()).To(ContainSubstring("Error: machine mount destination cannot be /tmp: consider another location or a subdirectory of /tmp"))
8094
})
8195

8296
It("simple init", func() {

pkg/machine/shim/host.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,19 @@ func Init(opts machineDefine.InitOptions, mp vmconfigs.VMProvider) error {
118118
createOpts.UserModeNetworking = *umn
119119
}
120120

121+
// Mounts
122+
if mp.VMType() != machineDefine.WSLVirt {
123+
mc.Mounts = CmdLineVolumesToMounts(opts.Volumes, mp.MountType())
124+
}
125+
126+
for _, mnt := range mc.Mounts {
127+
// Issue #18230 ... cannot mount over /tmp
128+
mountTarget, _ := strings.CutSuffix(mnt.Target, "/")
129+
if mountTarget == "/tmp" {
130+
return errors.New("machine mount destination cannot be /tmp: consider another location or a subdirectory of /tmp")
131+
}
132+
}
133+
121134
// Get Image
122135
// TODO This needs rework bigtime; my preference is most of below of not living in here.
123136
// ideally we could get a func back that pulls the image, and only do so IF everything works because
@@ -251,11 +264,6 @@ func Init(opts machineDefine.InitOptions, mp vmconfigs.VMProvider) error {
251264
}
252265
ignBuilder.WithUnit(readyUnit)
253266

254-
// Mounts
255-
if mp.VMType() != machineDefine.WSLVirt {
256-
mc.Mounts = CmdLineVolumesToMounts(opts.Volumes, mp.MountType())
257-
}
258-
259267
// TODO AddSSHConnectionToPodmanSocket could take an machineconfig instead
260268
if err := connection.AddSSHConnectionsToPodmanSocket(mc.HostUser.UID, mc.SSH.Port, mc.SSH.IdentityPath, mc.Name, mc.SSH.RemoteUsername, opts); err != nil {
261269
return err

0 commit comments

Comments
 (0)