path_get refactor and implementation of missing path_ hostcalls on Windows (#41)

* Move path_get outside of sys module

* Add implementation of readlinkat

* Clean up path_open; use OpenOptions as much as possible

* Enable close_preopen test

* Implement path_create_directory; fix path_open

* Refactor path concatenation onto a descriptor

* Implement path_remove_directory

* Implement path_unlink_file

* Rewrite path_open using specific access mask

* Fix error mapping when unlinking file

* Fix readlinkat to pass nofollow_errors testcase

* Clean up winerror to WASI conversion

* Spoof creating dangling symlinks on windows (hacky!)

* Add positive testcase for readlink

* Implement path_readlink (for nonzero buffers for now)

* Clean up

* Add Symlink struct immitating *nix symlink

* Fix path_readlink

* Augment interesting_paths testcase with trailing slashes example

* Encapsulate path_get return value as PathGet struct

* Remove dangling symlink emulation

* Extract dangling symlinks into its own testcase

This way, we can re-enable nofollow_errors testcase
on Windows also.

* Return __WASI_ENOTCAPABLE if user lacks perms to symlink
This commit is contained in:
Jakub Konka
2019-08-08 17:06:01 +02:00
committed by GitHub
parent 8ea7a983d8
commit e18175c556
19 changed files with 757 additions and 690 deletions

View File

@@ -1,7 +1,10 @@
#![allow(non_camel_case_types)]
use super::fs_helpers::path_get;
use crate::ctx::WasiCtx;
use crate::fdentry::{Descriptor, FdEntry};
use crate::memory::*;
use crate::sys::fdentry_impl::determine_type_rights;
use crate::sys::hostcalls_impl::fs_helpers::path_open_rights;
use crate::sys::{errno_from_host, host_impl, hostcalls_impl};
use crate::{host, wasm32, Result};
use log::trace;
@@ -242,9 +245,10 @@ pub(crate) fn fd_seek(
host::__WASI_WHENCE_SET => SeekFrom::Start(offset as u64),
_ => return Err(host::__WASI_EINVAL),
};
let host_newoffset = fd
.seek(pos)
.map_err(|err| err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host))?;
let host_newoffset = fd.seek(pos).map_err(|err| {
log::debug!("fd_seek error={:?}", err);
err.raw_os_error().map_or(host::__WASI_EIO, errno_from_host)
})?;
trace!(" | *newoffset={:?}", host_newoffset);
@@ -487,8 +491,9 @@ pub(crate) fn path_create_directory(
let dirfd = wasi_ctx
.get_fd_entry(dirfd, rights, 0)
.and_then(|fe| fe.fd_object.descriptor.as_file())?;
let resolved = path_get(dirfd, 0, path, false)?;
hostcalls_impl::path_create_directory(dirfd, path)
hostcalls_impl::path_create_directory(resolved)
}
pub(crate) fn path_link(
@@ -529,8 +534,10 @@ pub(crate) fn path_link(
let new_dirfd = wasi_ctx
.get_fd_entry(new_dirfd, host::__WASI_RIGHT_PATH_LINK_TARGET, 0)
.and_then(|fe| fe.fd_object.descriptor.as_file())?;
let resolved_old = path_get(old_dirfd, 0, old_path, false)?;
let resolved_new = path_get(new_dirfd, 0, new_path, false)?;
hostcalls_impl::path_link(old_dirfd, new_dirfd, old_path, new_path)
hostcalls_impl::path_link(resolved_old, resolved_new)
}
pub(crate) fn path_open(
@@ -559,6 +566,9 @@ pub(crate) fn path_open(
fd_out_ptr
);
// pre-encode fd_out_ptr to -1 in case of error in opening a path
enc_fd_byref(memory, fd_out_ptr, wasm32::__wasi_fd_t::max_value())?;
let dirfd = dec_fd(dirfd);
let dirflags = dec_lookupflags(dirflags);
let oflags = dec_oflags(oflags);
@@ -566,6 +576,17 @@ pub(crate) fn path_open(
let fs_rights_inheriting = dec_rights(fs_rights_inheriting);
let fs_flags = dec_fdflags(fs_flags);
let path = dec_slice_of::<u8>(memory, path_ptr, path_len).and_then(host::path_from_slice)?;
trace!(" | (path_ptr,path_len)='{}'", path);
let (needed_base, needed_inheriting) =
path_open_rights(fs_rights_base, fs_rights_inheriting, oflags, fs_flags);
let dirfd = wasi_ctx
.get_fd_entry(dirfd, needed_base, needed_inheriting)
.and_then(|fe| fe.fd_object.descriptor.as_file())?;
let resolved = path_get(dirfd, dirflags, path, oflags & host::__WASI_O_CREAT != 0)?;
// which open mode do we need?
let read = fs_rights_base & (host::__WASI_RIGHT_FD_READ | host::__WASI_RIGHT_FD_READDIR) != 0;
let write = fs_rights_base
@@ -575,37 +596,18 @@ pub(crate) fn path_open(
| host::__WASI_RIGHT_FD_FILESTAT_SET_SIZE)
!= 0;
// which rights are needed on the dirfd?
let needed_base = host::__WASI_RIGHT_PATH_OPEN;
let needed_inheriting = fs_rights_base | fs_rights_inheriting;
let fd = hostcalls_impl::path_open(resolved, read, write, oflags, fs_flags)?;
let path = dec_slice_of::<u8>(memory, path_ptr, path_len).and_then(host::path_from_slice)?;
// Determine the type of the new file descriptor and which rights contradict with this type
let (_ty, max_base, max_inheriting) = determine_type_rights(&fd)?;
let mut fe = FdEntry::from(fd)?;
fe.rights_base &= max_base;
fe.rights_inheriting &= max_inheriting;
let guest_fd = wasi_ctx.insert_fd_entry(fe)?;
trace!(" | (path_ptr,path_len)='{}'", path);
trace!(" | *fd={:?}", guest_fd);
hostcalls_impl::path_open(
wasi_ctx,
dirfd,
dirflags,
path,
oflags,
read,
write,
needed_base,
needed_inheriting,
fs_flags,
)
.and_then(|fe| {
let guest_fd = wasi_ctx.insert_fd_entry(fe)?;
trace!(" | *fd={:?}", guest_fd);
enc_fd_byref(memory, fd_out_ptr, guest_fd)
})
.or_else(|err| {
enc_fd_byref(memory, fd_out_ptr, wasm32::__wasi_fd_t::max_value())?;
Err(err)
})
enc_fd_byref(memory, fd_out_ptr, guest_fd)
}
pub(crate) fn fd_readdir(
@@ -676,10 +678,11 @@ pub(crate) fn path_readlink(
let dirfd = wasi_ctx
.get_fd_entry(dirfd, host::__WASI_RIGHT_PATH_READLINK, 0)
.and_then(|fe| fe.fd_object.descriptor.as_file())?;
let resolved = path_get(dirfd, 0, &path, false)?;
let mut buf = dec_slice_of_mut::<u8>(memory, buf_ptr, buf_len)?;
let host_bufused = hostcalls_impl::path_readlink(dirfd, &path, &mut buf)?;
let host_bufused = hostcalls_impl::path_readlink(resolved, &mut buf)?;
trace!(" | (buf_ptr,*buf_used)={:?}", buf);
trace!(" | *buf_used={:?}", host_bufused);
@@ -723,8 +726,10 @@ pub(crate) fn path_rename(
let new_dirfd = wasi_ctx
.get_fd_entry(new_dirfd, host::__WASI_RIGHT_PATH_RENAME_TARGET, 0)
.and_then(|fe| fe.fd_object.descriptor.as_file())?;
let resolved_old = path_get(old_dirfd, 0, old_path, false)?;
let resolved_new = path_get(new_dirfd, 0, new_path, false)?;
hostcalls_impl::path_rename(old_dirfd, old_path, new_dirfd, new_path)
hostcalls_impl::path_rename(resolved_old, resolved_new)
}
pub(crate) fn fd_filestat_get(
@@ -825,8 +830,8 @@ pub(crate) fn path_filestat_get(
let dirfd = wasi_ctx
.get_fd_entry(dirfd, host::__WASI_RIGHT_PATH_FILESTAT_GET, 0)
.and_then(|fe| fe.fd_object.descriptor.as_file())?;
let host_filestat = hostcalls_impl::path_filestat_get(dirfd, dirflags, path)?;
let resolved = path_get(dirfd, dirflags, path, false)?;
let host_filestat = hostcalls_impl::path_filestat_get(resolved, dirflags)?;
trace!(" | *filestat_ptr={:?}", host_filestat);
@@ -867,8 +872,9 @@ pub(crate) fn path_filestat_set_times(
let dirfd = wasi_ctx
.get_fd_entry(dirfd, host::__WASI_RIGHT_PATH_FILESTAT_SET_TIMES, 0)
.and_then(|fe| fe.fd_object.descriptor.as_file())?;
let resolved = path_get(dirfd, dirflags, path, false)?;
hostcalls_impl::path_filestat_set_times(dirfd, dirflags, path, st_atim, st_mtim, fst_flags)
hostcalls_impl::path_filestat_set_times(resolved, dirflags, st_atim, st_mtim, fst_flags)
}
pub(crate) fn path_symlink(
@@ -901,8 +907,9 @@ pub(crate) fn path_symlink(
let dirfd = wasi_ctx
.get_fd_entry(dirfd, host::__WASI_RIGHT_PATH_SYMLINK, 0)
.and_then(|fe| fe.fd_object.descriptor.as_file())?;
let resolved_new = path_get(dirfd, 0, new_path, false)?;
hostcalls_impl::path_symlink(dirfd, old_path, new_path)
hostcalls_impl::path_symlink(old_path, resolved_new)
}
pub(crate) fn path_unlink_file(
@@ -927,8 +934,9 @@ pub(crate) fn path_unlink_file(
let dirfd = wasi_ctx
.get_fd_entry(dirfd, host::__WASI_RIGHT_PATH_UNLINK_FILE, 0)
.and_then(|fe| fe.fd_object.descriptor.as_file())?;
let resolved = path_get(dirfd, 0, path, false)?;
hostcalls_impl::path_unlink_file(dirfd, path)
hostcalls_impl::path_unlink_file(resolved)
}
pub(crate) fn path_remove_directory(
@@ -953,8 +961,9 @@ pub(crate) fn path_remove_directory(
let dirfd = wasi_ctx
.get_fd_entry(dirfd, host::__WASI_RIGHT_PATH_REMOVE_DIRECTORY, 0)
.and_then(|fe| fe.fd_object.descriptor.as_file())?;
let resolved = path_get(dirfd, 0, path, false)?;
hostcalls_impl::path_remove_directory(dirfd, path)
hostcalls_impl::path_remove_directory(resolved)
}
pub(crate) fn fd_prestat_get(

View File

@@ -0,0 +1,197 @@
#![allow(non_camel_case_types)]
use crate::sys::hostcalls_impl::fs_helpers::*;
use crate::sys::{errno_from_host, host_impl};
use crate::{host, Result};
use std::fs::File;
use std::path::{Component, Path};
pub(crate) struct PathGet {
dirfd: File,
path: String,
}
impl PathGet {
pub(crate) fn dirfd(&self) -> &File {
&self.dirfd
}
pub(crate) fn path(&self) -> &str {
&self.path
}
}
/// Normalizes a path to ensure that the target path is located under the directory provided.
///
/// This is a workaround for not having Capsicum support in the OS.
pub(crate) fn path_get(
dirfd: &File,
dirflags: host::__wasi_lookupflags_t,
path: &str,
needs_final_component: bool,
) -> Result<PathGet> {
const MAX_SYMLINK_EXPANSIONS: usize = 128;
if path.contains('\0') {
// if contains NUL, return EILSEQ
return Err(host::__WASI_EILSEQ);
}
let dirfd = dirfd.try_clone().map_err(|err| {
err.raw_os_error()
.map_or(host::__WASI_EBADF, errno_from_host)
})?;
// Stack of directory file descriptors. Index 0 always corresponds with the directory provided
// to this function. Entering a directory causes a file descriptor to be pushed, while handling
// ".." entries causes an entry to be popped. Index 0 cannot be popped, as this would imply
// escaping the base directory.
let mut dir_stack = vec![dirfd];
// Stack of paths left to process. This is initially the `path` argument to this function, but
// any symlinks we encounter are processed by pushing them on the stack.
let mut path_stack = vec![path.to_owned()];
// Track the number of symlinks we've expanded, so we can return `ELOOP` after too many.
let mut symlink_expansions = 0;
// TODO: rewrite this using a custom posix path type, with a component iterator that respects
// trailing slashes. This version does way too much allocation, and is way too fiddly.
loop {
match path_stack.pop() {
Some(cur_path) => {
log::debug!("cur_path = {:?}", cur_path);
let ends_with_slash = cur_path.ends_with("/");
let mut components = Path::new(&cur_path).components();
let head = match components.next() {
None => return Err(host::__WASI_ENOENT),
Some(p) => p,
};
let tail = components.as_path();
if tail.components().next().is_some() {
let mut tail = host_impl::path_from_host(tail.as_os_str())?;
if ends_with_slash {
tail.push_str("/");
}
path_stack.push(tail);
}
match head {
Component::Prefix(_) | Component::RootDir => {
// path is absolute!
return Err(host::__WASI_ENOTCAPABLE);
}
Component::CurDir => {
// "." so skip
}
Component::ParentDir => {
// ".." so pop a dir
let _ = dir_stack.pop().ok_or(host::__WASI_ENOTCAPABLE)?;
// we're not allowed to pop past the original directory
if dir_stack.is_empty() {
return Err(host::__WASI_ENOTCAPABLE);
}
}
Component::Normal(head) => {
let mut head = host_impl::path_from_host(head)?;
if ends_with_slash {
// preserve trailing slash
head.push_str("/");
}
if !path_stack.is_empty() || (ends_with_slash && !needs_final_component) {
match openat(dir_stack.last().ok_or(host::__WASI_ENOTCAPABLE)?, &head) {
Ok(new_dir) => {
dir_stack.push(new_dir);
}
Err(e)
if e == host::__WASI_ELOOP
|| e == host::__WASI_EMLINK
|| e == host::__WASI_ENOTDIR =>
// Check to see if it was a symlink. Linux indicates
// this with ENOTDIR because of the O_DIRECTORY flag.
{
// attempt symlink expansion
let mut link_path = readlinkat(
dir_stack.last().ok_or(host::__WASI_ENOTCAPABLE)?,
&head,
)?;
symlink_expansions += 1;
if symlink_expansions > MAX_SYMLINK_EXPANSIONS {
return Err(host::__WASI_ELOOP);
}
if head.ends_with("/") {
link_path.push_str("/");
}
log::debug!(
"attempted symlink expansion link_path={:?}",
link_path
);
path_stack.push(link_path);
}
Err(e) => {
return Err(e);
}
}
continue;
} else if ends_with_slash
|| (dirflags & host::__WASI_LOOKUP_SYMLINK_FOLLOW) != 0
{
// if there's a trailing slash, or if `LOOKUP_SYMLINK_FOLLOW` is set, attempt
// symlink expansion
match readlinkat(
dir_stack.last().ok_or(host::__WASI_ENOTCAPABLE)?,
&head,
) {
Ok(mut link_path) => {
symlink_expansions += 1;
if symlink_expansions > MAX_SYMLINK_EXPANSIONS {
return Err(host::__WASI_ELOOP);
}
if head.ends_with("/") {
link_path.push_str("/");
}
log::debug!(
"attempted symlink expansion link_path={:?}",
link_path
);
path_stack.push(link_path);
continue;
}
Err(e) => {
if e != host::__WASI_EINVAL && e != host::__WASI_ENOENT {
return Err(e);
}
}
}
}
// not a symlink, so we're done;
return Ok(PathGet {
dirfd: dir_stack.pop().ok_or(host::__WASI_ENOTCAPABLE)?,
path: head,
});
}
}
}
None => {
// no further components to process. means we've hit a case like "." or "a/..", or if the
// input path has trailing slashes and `needs_final_component` is not set
return Ok(PathGet {
dirfd: dir_stack.pop().ok_or(host::__WASI_ENOTCAPABLE)?,
path: String::from("."),
});
}
}
}
}

View File

@@ -1,5 +1,7 @@
mod fs;
mod fs_helpers;
mod misc;
pub(crate) use self::fs::*;
pub(crate) use self::fs_helpers::PathGet;
pub(crate) use self::misc::*;