mirror of
https://github.com/moby/moby.git
synced 2026-06-24 08:48:23 +00:00
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>
46 lines
1.2 KiB
Go
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")
|
|
})
|
|
}
|