Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions vm/devices/support/fs/lxutil/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,62 @@ mod tests {
assert_eq!(file.fstat().unwrap().file_size, 5);
}

#[test]
fn truncating_open_keeps_the_access_mode() {
// Linux truncates a file that O_TRUNC opens even when the access mode does not allow
// writing, but the descriptor keeps that access mode, so it can neither write nor
// truncate the file. Windows truncates only through a handle with write access, which
// the descriptor must not keep.
let env = TestEnv::new();
for flags in [lx::O_RDONLY, lx::O_NOACCESS] {
env.create_file("testfile", "hello");
let file = env
.volume
.open("testfile", flags | lx::O_TRUNC, None)
.unwrap();
assert_eq!(file.fstat().unwrap().file_size, 0);
assert_eq!(file.pwrite(b"guest", 0, 0).unwrap_err().value(), lx::EBADF);
assert_eq!(file.truncate(5, 0).unwrap_err().value(), lx::EINVAL);
assert_eq!(fs::read(env.root_dir.path().join("testfile")).unwrap(), b"");
}

// The same holds when the open creates the file, which needs no truncation.
let file = env
.volume
.open(
"created",
lx::O_RDONLY | lx::O_CREAT | lx::O_TRUNC,
Some(LxCreateOptions::new(0o666, 0, 0)),
)
.unwrap();
assert_eq!(file.pwrite(b"guest", 0, 0).unwrap_err().value(), lx::EBADF);
assert_eq!(file.truncate(5, 0).unwrap_err().value(), lx::EINVAL);
assert_eq!(fs::read(env.root_dir.path().join("created")).unwrap(), b"");
}

#[test]
fn truncating_append_open_stays_append_only() {
// Linux writes through a descriptor opened with O_APPEND at the end of the file,
// whatever the offset, including after O_TRUNC truncated the file at open.
let env = TestEnv::new();
for (flags, expected) in [
(lx::O_WRONLY | lx::O_APPEND, b"helloabcd".as_slice()),
(
lx::O_WRONLY | lx::O_APPEND | lx::O_TRUNC,
b"abcd".as_slice(),
),
] {
env.create_file("testfile", "hello");
let file = env.volume.open("testfile", flags, None).unwrap();
assert_eq!(file.pwrite(b"ab", 0, 0).unwrap(), 2);
assert_eq!(file.pwrite(b"cd", 0, 0).unwrap(), 2);
assert_eq!(
fs::read(env.root_dir.path().join("testfile")).unwrap(),
expected
);
}
}

#[test]
fn read_dir() {
let env = TestEnv::new();
Expand Down
10 changes: 8 additions & 2 deletions vm/devices/support/fs/lxutil/src/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,15 @@ impl LxVolume {
Err(err) => return Err(err),
};
// O_TRUNC can't be handled with FILE_OVERWRITE because that clears metadata, so handle
// it here.
// it here. Linux truncates even when the access mode does not allow writing, while Windows
// needs a handle with write access; use one only for the truncation, so that the file,
// like a Linux descriptor, keeps the access of its access mode.
if flags & lx::O_TRUNC != 0 && create_result != WindowsProgramming::FILE_CREATED as usize {
fs::truncate(&file, 0)?;
if desired_access.contains(W32Fs::FILE_WRITE_DATA) {
fs::truncate(&file, 0)?;
} else {
fs::truncate(&util::reopen_file(&file, W32Fs::FILE_WRITE_DATA)?, 0)?;
}
}

Ok(LxFile {
Expand Down
7 changes: 1 addition & 6 deletions vm/devices/support/fs/lxutil/src/windows/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ pub fn dos_to_nt_path(
Ok(path)
}

// Convert Linux open flags to a Windows access mask.
// Convert Linux open flags to the Windows access mask of the open file.
pub fn open_flags_to_access_mask(flags: i32) -> W32Fs::FILE_ACCESS_RIGHTS {
let mut mask = MINIMUM_PERMISSIONS;
mask |= match flags & lx::O_ACCESS_MASK {
Expand All @@ -516,11 +516,6 @@ pub fn open_flags_to_access_mask(flags: i32) -> W32Fs::FILE_ACCESS_RIGHTS {
mask &= !W32Fs::FILE_WRITE_DATA;
}

if flags & lx::O_TRUNC != 0 {
// Truncate on Linux is allowed with O_RDONLY, but requires write access on Windows.
mask |= W32Fs::FILE_WRITE_DATA;
}

mask
}

Expand Down
8 changes: 8 additions & 0 deletions vm/devices/virtio/virtiofs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,14 @@ impl Fuse for VirtioFs {
return Err(lx::Error::E2BIG);
}
let file = self.get_file(request.node_id(), arg.fh)?;
// Like Linux's write(2), refuse a handle that the guest did not open
// for writing, before refusing a file that it may not write.
if !matches!(
file.open_flags() as i32 & lx::O_ACCESS_MASK,
lx::O_WRONLY | lx::O_RDWR
) {
return Err(lx::Error::EBADF);
}
self.check_writable(file.inode())?;
file.write(data, arg.offset, request.uid())
}
Expand Down
37 changes: 37 additions & 0 deletions vm/devices/virtio/virtiofs/src/microvm/policy_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,43 @@ impl Share {
}
}

#[test]
fn writes_need_a_handle_open_for_writing() {
let share = Share::new();
let fs = share.fs(&[], &[], &["out"]);
let out = lookup(&fs, "out").unwrap();
let existing = lookup(&fs, "out/existing").unwrap();

// Like Linux's write(2), a write through a handle that the guest did not
// open for writing fails with EBADF, whether FUSE_OPEN or FUSE_CREATE
// opened it, even with O_TRUNC, for which a Windows host needs write
// access.
for flags in [lx::O_RDONLY, lx::O_RDONLY | lx::O_TRUNC] {
let fh = open(&fs, existing, flags).unwrap();
assert_eq!(error(write(&fs, existing, fh, b"guest")), lx::Error::EBADF);
release(&fs, existing, fh);
}
let flags = lx::O_RDONLY | lx::O_CREAT | lx::O_TRUNC;
let (file, fh) = create_with_flags(&fs, out, "created", flags).unwrap();
assert_eq!(error(write(&fs, file, fh, b"guest")), lx::Error::EBADF);
release(&fs, file, fh);

// That error comes before the policy's EROFS outside the writable paths.
let log = lookup(&fs, "build.log").unwrap();
let fh = open(&fs, log, lx::O_RDONLY).unwrap();
assert_eq!(error(write(&fs, log, fh, b"guest")), lx::Error::EBADF);
release(&fs, log, fh);

let fh = open(&fs, existing, lx::O_WRONLY).unwrap();
assert_eq!(write(&fs, existing, fh, b"guest").unwrap(), 5);
release(&fs, existing, fh);

assert_eq!(share.read("out/existing"), "guest");
assert_eq!(share.read("out/created"), "");
assert_eq!(share.read("build.log"), "log");
share.assert_protected_paths_unchanged();
}

#[test]
fn writes_succeed_only_under_writable_paths() {
let share = Share::new();
Expand Down
Loading