Skip to content

Commit

Permalink
wasi: fix symlink-related issues with path handling (#1648)
Browse files Browse the repository at this point in the history
Signed-off-by: Edoardo Vacchi <evacchi@users.noreply.github.com>
  • Loading branch information
evacchi authored Aug 23, 2023
1 parent 1223d11 commit a5b30a5
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 4 deletions.
3 changes: 3 additions & 0 deletions experimental/sys/errno.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
ENOENT
ENOSYS
ENOTDIR
ERANGE
ENOTEMPTY
ENOTSOCK
ENOTSUP
Expand Down Expand Up @@ -79,6 +80,8 @@ func (e Errno) Error() string {
return "functionality not supported"
case ENOTDIR:
return "not a directory or a symbolic link to a directory"
case ERANGE:
return "result too large"
case ENOTEMPTY:
return "directory not empty"
case ENOTSOCK:
Expand Down
2 changes: 2 additions & 0 deletions experimental/sys/syscall_errno.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ func syscallToErrno(err error) (Errno, bool) {
return ENOSYS, true
case syscall.ENOTDIR:
return ENOTDIR, true
case syscall.ERANGE:
return ERANGE, true
case syscall.ENOTEMPTY:
return ENOTEMPTY, true
case syscall.ENOTSOCK:
Expand Down
7 changes: 5 additions & 2 deletions imports/wasi_snapshot_preview1/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"math"
"path"
"strings"
"syscall"
"unsafe"

"github.com/tetratelabs/wazero/api"
Expand Down Expand Up @@ -1463,7 +1462,7 @@ func pathFilestatSetTimesFn(_ context.Context, mod api.Module, params []uint64)
return preopen.Utimens(pathName, atim, mtim)
}
// Otherwise, we need to emulate don't follow by opening the file by path.
if f, errno := preopen.OpenFile(pathName, syscall.O_WRONLY, 0); errno != 0 {
if f, errno := preopen.OpenFile(pathName, experimentalsys.O_WRONLY, 0); errno != 0 {
return errno
} else {
defer f.Close()
Expand Down Expand Up @@ -1797,6 +1796,10 @@ func pathReadlinkFn(_ context.Context, mod api.Module, params []uint64) experime
return errno
}

if len(dst) > int(bufLen) {
return experimentalsys.ERANGE
}

if ok := mem.WriteString(buf, dst); !ok {
return experimentalsys.EFAULT
}
Expand Down
4 changes: 2 additions & 2 deletions imports/wasi_snapshot_preview1/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4238,12 +4238,12 @@ func Test_pathReadlink(t *testing.T) {
{expectedErrno: wasip1.ErrnoInval, bufLen: 100},
{
name: "bufLen too short",
expectedErrno: wasip1.ErrnoFault,
expectedErrno: wasip1.ErrnoRange,
fd: dirFD,
bufLen: 10,
path: destinationPath,
pathLen: uint32(len(destinationPathName)),
buf: math.MaxUint32,
buf: 0,
},
{
name: "path past memory",
Expand Down
2 changes: 2 additions & 0 deletions internal/wasip1/errno.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,8 @@ func ToErrno(errno sys.Errno) Errno {
return ErrnoNosys
case sys.ENOTDIR:
return ErrnoNotdir
case sys.ERANGE:
return ErrnoRange
case sys.ENOTEMPTY:
return ErrnoNotempty
case sys.ENOTSOCK:
Expand Down

0 comments on commit a5b30a5

Please sign in to comment.