[v5.6-rhel] libpod: fix workdir MkdirAll() all check

MkdirAll can fail with EEXIST when the path is a symlink and the target
doesn't exist. As such we should ignore the error.

Note there is something fundemantal wrong here with the path access as
it is following the symlink to the host, however it is only for a
stat() so it is not an security issue here.

Fixes: 637c264e2e ("fix issues found by nilness")

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
(cherry picked from commit 7b1be7f177)
Signed-off-by: tomsweeneyredhat <tsweeney@redhat.com>
This commit is contained in:
Paul Holzinger 2025-12-03 16:57:15 +01:00 committed by tomsweeneyredhat
parent 360c775708
commit bd01c413cf
2 changed files with 14 additions and 1 deletions

View file

@ -932,7 +932,10 @@ func (c *Container) resolveWorkDir() error {
// we need to return the full error.
return fmt.Errorf("detecting workdir %q on container %s: %w", workdir, c.ID(), err)
}
if err := os.MkdirAll(resolvedWorkdir, 0755); err != nil {
if err := os.MkdirAll(resolvedWorkdir, 0o755); err != nil {
if errors.Is(err, fs.ErrExist) {
return nil
}
return fmt.Errorf("creating container %s workdir: %w", c.ID(), err)
}

View file

@ -57,4 +57,14 @@ WORKDIR /etc/foobar`, ALPINE)
Expect(session).Should(ExitCleanly())
Expect(session.OutputToString()).To(Equal("/home/foobar"))
})
It("podman run on an image with a symlinked workdir", func() {
dockerfile := fmt.Sprintf(`FROM %s
RUN mkdir /A && ln -s /A /B
WORKDIR /B`, ALPINE)
podmanTest.BuildImage(dockerfile, "test", "false")
session := podmanTest.PodmanExitCleanly("run", "test", "pwd")
Expect(session.OutputToString()).To(Equal("/A"))
})
})