Skip to content

Commit 8e3306e

Browse files
ppennaCopilot
andcommitted
lxutil: keep O_TRUNC from making a Windows handle writable
On Windows, LxVolume::open asked for FILE_WRITE_DATA whenever the open flags held O_TRUNC, since Windows truncates only through a handle with write access, and the LxFile then kept that handle, and recorded that access, for its lifetime. So the file that an O_RDONLY | O_TRUNC or O_NOACCESS | O_TRUNC open returned could be written with pwrite and truncated with set_attr, unlike a Linux descriptor with the same flags, which fails such a write with EBADF and such a truncation with EINVAL; and O_APPEND | O_TRUNC got FILE_WRITE_DATA back, so its writes went to their offsets rather than to the end of the file. A virtio-fs guest could thus keep writing a file through a handle that it had opened read-only (microsoft/nvx#1609), and a microVM restore, which reopens the handle without O_TRUNC, silently took that access away. open_flags_to_access_mask now derives the access of the open file from its access mode and O_APPEND alone. LxVolume::open truncates an existing file through the file's handle when that access includes FILE_WRITE_DATA, and otherwise through a handle reopened with FILE_WRITE_DATA for the truncation alone, which it then closes. The open still needs write access to truncate, as on Linux, and fails with EACCES without it. truncating_open_keeps_the_access_mode opens an existing file with O_RDONLY | O_TRUNC and with O_NOACCESS | O_TRUNC, and creates one with O_RDONLY | O_CREAT | O_TRUNC, and checks that each open leaves the file empty but that a pwrite through it fails with EBADF and a truncate with EINVAL. truncating_append_open_stays_append_only checks that two writes at offset 0 through O_WRONLY | O_APPEND, with and without O_TRUNC, both append. Without the fix, at feature/microvm/dev b51f915 on Windows, the pwrite through the O_RDONLY | O_TRUNC file succeeds, and the writes through the O_WRONLY | O_APPEND | O_TRUNC one overwrite each other; with it, both tests and the package's others pass there. Both pass on Linux (Azure Linux 3.0, nvx-kvm-test) with and without the fix, since the host kernel gives a descriptor these semantics. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent b1c57d5 commit 8e3306e

3 files changed

Lines changed: 65 additions & 8 deletions

File tree

‎vm/devices/support/fs/lxutil/src/lib.rs‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,62 @@ mod tests {
14521452
assert_eq!(file.fstat().unwrap().file_size, 5);
14531453
}
14541454

1455+
#[test]
1456+
fn truncating_open_keeps_the_access_mode() {
1457+
// Linux truncates a file that O_TRUNC opens even when the access mode does not allow
1458+
// writing, but the descriptor keeps that access mode, so it can neither write nor
1459+
// truncate the file. Windows truncates only through a handle with write access, which
1460+
// the descriptor must not keep.
1461+
let env = TestEnv::new();
1462+
for flags in [lx::O_RDONLY, lx::O_NOACCESS] {
1463+
env.create_file("testfile", "hello");
1464+
let file = env
1465+
.volume
1466+
.open("testfile", flags | lx::O_TRUNC, None)
1467+
.unwrap();
1468+
assert_eq!(file.fstat().unwrap().file_size, 0);
1469+
assert_eq!(file.pwrite(b"guest", 0, 0).unwrap_err().value(), lx::EBADF);
1470+
assert_eq!(file.truncate(5, 0).unwrap_err().value(), lx::EINVAL);
1471+
assert_eq!(fs::read(env.root_dir.path().join("testfile")).unwrap(), b"");
1472+
}
1473+
1474+
// The same holds when the open creates the file, which needs no truncation.
1475+
let file = env
1476+
.volume
1477+
.open(
1478+
"created",
1479+
lx::O_RDONLY | lx::O_CREAT | lx::O_TRUNC,
1480+
Some(LxCreateOptions::new(0o666, 0, 0)),
1481+
)
1482+
.unwrap();
1483+
assert_eq!(file.pwrite(b"guest", 0, 0).unwrap_err().value(), lx::EBADF);
1484+
assert_eq!(file.truncate(5, 0).unwrap_err().value(), lx::EINVAL);
1485+
assert_eq!(fs::read(env.root_dir.path().join("created")).unwrap(), b"");
1486+
}
1487+
1488+
#[test]
1489+
fn truncating_append_open_stays_append_only() {
1490+
// Linux writes through a descriptor opened with O_APPEND at the end of the file,
1491+
// whatever the offset, including after O_TRUNC truncated the file at open.
1492+
let env = TestEnv::new();
1493+
for (flags, expected) in [
1494+
(lx::O_WRONLY | lx::O_APPEND, b"helloabcd".as_slice()),
1495+
(
1496+
lx::O_WRONLY | lx::O_APPEND | lx::O_TRUNC,
1497+
b"abcd".as_slice(),
1498+
),
1499+
] {
1500+
env.create_file("testfile", "hello");
1501+
let file = env.volume.open("testfile", flags, None).unwrap();
1502+
assert_eq!(file.pwrite(b"ab", 0, 0).unwrap(), 2);
1503+
assert_eq!(file.pwrite(b"cd", 0, 0).unwrap(), 2);
1504+
assert_eq!(
1505+
fs::read(env.root_dir.path().join("testfile")).unwrap(),
1506+
expected
1507+
);
1508+
}
1509+
}
1510+
14551511
#[test]
14561512
fn read_dir() {
14571513
let env = TestEnv::new();

‎vm/devices/support/fs/lxutil/src/windows/mod.rs‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,9 +322,15 @@ impl LxVolume {
322322
Err(err) => return Err(err),
323323
};
324324
// O_TRUNC can't be handled with FILE_OVERWRITE because that clears metadata, so handle
325-
// it here.
325+
// it here. Linux truncates even when the access mode does not allow writing, while Windows
326+
// needs a handle with write access; use one only for the truncation, so that the file,
327+
// like a Linux descriptor, keeps the access of its access mode.
326328
if flags & lx::O_TRUNC != 0 && create_result != WindowsProgramming::FILE_CREATED as usize {
327-
fs::truncate(&file, 0)?;
329+
if desired_access.contains(W32Fs::FILE_WRITE_DATA) {
330+
fs::truncate(&file, 0)?;
331+
} else {
332+
fs::truncate(&util::reopen_file(&file, W32Fs::FILE_WRITE_DATA)?, 0)?;
333+
}
328334
}
329335

330336
Ok(LxFile {

‎vm/devices/support/fs/lxutil/src/windows/util.rs‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ pub fn dos_to_nt_path(
501501
Ok(path)
502502
}
503503

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

519-
if flags & lx::O_TRUNC != 0 {
520-
// Truncate on Linux is allowed with O_RDONLY, but requires write access on Windows.
521-
mask |= W32Fs::FILE_WRITE_DATA;
522-
}
523-
524519
mask
525520
}
526521

0 commit comments

Comments
 (0)