sysfs: disallow absolute symlinks (#2324)
Signed-off-by: Nuno Cruces <ncruces@users.noreply.github.com>
This commit is contained in:
@@ -5,6 +5,7 @@ package sysfs
|
|||||||
import (
|
import (
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
|
|
||||||
experimentalsys "github.com/tetratelabs/wazero/experimental/sys"
|
experimentalsys "github.com/tetratelabs/wazero/experimental/sys"
|
||||||
)
|
)
|
||||||
@@ -34,6 +35,11 @@ func (d *dirFS) Chmod(path string, perm fs.FileMode) experimentalsys.Errno {
|
|||||||
|
|
||||||
// Symlink implements the same method as documented on sys.FS
|
// Symlink implements the same method as documented on sys.FS
|
||||||
func (d *dirFS) Symlink(oldName, link string) experimentalsys.Errno {
|
func (d *dirFS) Symlink(oldName, link string) experimentalsys.Errno {
|
||||||
|
// Creating a symlink with an absolute path string fails with a "not permitted" error.
|
||||||
|
// https://github.com/WebAssembly/wasi-filesystem/blob/v0.2.0/path-resolution.md#symlinks
|
||||||
|
if path.IsAbs(oldName) {
|
||||||
|
return experimentalsys.EPERM
|
||||||
|
}
|
||||||
// Note: do not resolve `oldName` relative to this dirFS. The link result is always resolved
|
// Note: do not resolve `oldName` relative to this dirFS. The link result is always resolved
|
||||||
// when dereference the `link` on its usage (e.g. readlink, read, etc).
|
// when dereference the `link` on its usage (e.g. readlink, read, etc).
|
||||||
// https://github.com/bytecodealliance/cap-std/blob/v1.0.4/cap-std/src/fs/dir.rs#L404-L409
|
// https://github.com/bytecodealliance/cap-std/blob/v1.0.4/cap-std/src/fs/dir.rs#L404-L409
|
||||||
|
|||||||
@@ -747,6 +747,7 @@ func TestDirFS_Symlink(t *testing.T) {
|
|||||||
|
|
||||||
testFS := DirFS(tmpDir)
|
testFS := DirFS(tmpDir)
|
||||||
|
|
||||||
|
require.EqualErrno(t, sys.EPERM, testFS.Symlink("/test.txt", "sub/test.txt"))
|
||||||
require.EqualErrno(t, sys.EEXIST, testFS.Symlink("sub/test.txt", "sub/test.txt"))
|
require.EqualErrno(t, sys.EEXIST, testFS.Symlink("sub/test.txt", "sub/test.txt"))
|
||||||
// Non-existing old name is allowed.
|
// Non-existing old name is allowed.
|
||||||
require.EqualErrno(t, 0, testFS.Symlink("non-existing", "aa"))
|
require.EqualErrno(t, 0, testFS.Symlink("non-existing", "aa"))
|
||||||
|
|||||||
Reference in New Issue
Block a user