Files
moby/daemon/containerfs_linux_test.go
Paweł Gronowski 64a22d80b9 daemon/copy: Fix symlink escape in mount destination creation
Use os.Root to scope all filesystem operations in createIfNotExists to
the container root directory.

This prevents a TOCTOU attack where a container process swaps a path
component with a symlink between GetResourcePath resolution and
directory/file creation, which could allow writing to arbitrary host
paths outside the container.

Signed-off-by: Paweł Gronowski <pawel.gronowski@docker.com>
2026-05-18 14:50:33 +02:00

46 lines
1.2 KiB
Go

package daemon
import (
"os"
"path/filepath"
"testing"
"gotest.tools/v3/assert"
)
func TestCreateIfNotExists(t *testing.T) {
t.Run("directory", func(t *testing.T) {
dir := t.TempDir()
root, err := os.OpenRoot(dir)
assert.NilError(t, err)
defer root.Close()
err = createIfNotExists(root, "tocreate", true)
assert.NilError(t, err)
fileinfo, err := os.Stat(filepath.Join(dir, "tocreate"))
assert.NilError(t, err, "Did not create destination")
assert.Assert(t, fileinfo.IsDir(), "Should have been a dir, seems it's not")
err = createIfNotExists(root, "tocreate", true)
assert.NilError(t, err, "Should not fail if already exists")
})
t.Run("file", func(t *testing.T) {
dir := t.TempDir()
root, err := os.OpenRoot(dir)
assert.NilError(t, err)
defer root.Close()
err = createIfNotExists(root, "file/to/create", false)
assert.NilError(t, err)
fileinfo, err := os.Stat(filepath.Join(dir, "file/to/create"))
assert.NilError(t, err, "Did not create destination")
assert.Assert(t, !fileinfo.IsDir(), "Should have been a file, but created a directory")
err = createIfNotExists(root, "file/to/create", false)
assert.NilError(t, err, "Should not fail if already exists")
})
}