Skip to content

Commit 9822f73

Browse files
ppennaCopilot
andcommitted
lxutil: hard-link to one-character names on Windows
On a Windows host, LxVolume::link and LxVolume::link_stat failed with EOVERFLOW, and created no link, when the new name was one UTF-16 unit long, such as `x`, although longer names worked (microsoft/nvx#2136). A guest's `ln file x` on a virtio-fs share of a Windows directory thus failed, and so would one on a Plan 9 share, which links through the same LxVolume::link. create_link passes NtSetInformationFile a FILE_LINK_INFORMATION built from a header that mirrors the structure up to FileName, 20 bytes with an alignment of 1, followed by the name. NtSetInformationFile refuses a buffer shorter than sizeof(FILE_LINK_INFORMATION), which counts the first FileName unit and the trailing padding, 24 bytes on 64-bit Windows, with STATUS_INFO_LENGTH_MISMATCH, which lxutil maps to EOVERFLOW. A name of one unit made a buffer of 22 bytes. rename does not fail this way: its header starts with the 4-byte union of FILE_RENAME_INFORMATION, so its buffer rounds up to 24 bytes. create_link now pads the name with NULs until the buffer is as long as the structure, as pal's hard_link_relative already sizes its own. FileNameLength still counts only the name, so the link gets exactly that name. link_to_a_one_character_name links a file to `a` through link and to `b` through link_stat, and expects both names to reach the file, with link counts of 2 and 3. Without the fix, its first link fails with EOVERFLOW on Windows 11 Enterprise (build 26300, NTFS); with it, the test passes there, as do the other tests of the lxutil and virtiofs packages. A one-off test, not committed, of the issue's reproduction, a FUSE_LINK through VirtioFs of a file to `xy` and then to `x`, with the standard and the microVM profile, got EOVERFLOW for `x`, and left no host file, at feature/microvm/dev 1633b37, and a link count of 3 with the fix. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 5af753b commit 9822f73

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2398,6 +2398,25 @@ mod tests {
23982398
assert_eq!(stat2.link_count, 3);
23992399
}
24002400

2401+
#[test]
2402+
fn link_to_a_one_character_name() {
2403+
let env = TestEnv::new();
2404+
env.create_file("testfile", "test");
2405+
let stat = env.volume.lstat("testfile").unwrap();
2406+
2407+
// On Windows, a name of one UTF-16 unit makes the shortest
2408+
// FILE_LINK_INFORMATION buffer.
2409+
env.volume.link("testfile", "a").unwrap();
2410+
let stat2 = env.volume.lstat("a").unwrap();
2411+
assert_eq!(stat2.inode_nr, stat.inode_nr);
2412+
assert_eq!(stat2.link_count, 2);
2413+
2414+
let stat2 = env.volume.link_stat("testfile", "b").unwrap();
2415+
assert_eq!(stat2.inode_nr, stat.inode_nr);
2416+
assert_eq!(stat2.link_count, 3);
2417+
assert_eq!(fs::read(env.root_dir.path().join("b")).unwrap(), b"test");
2418+
}
2419+
24012420
#[test]
24022421
fn stat_fs() {
24032422
let env = TestEnv::new();

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,15 @@ pub fn create_link(
12021202
HeaderVec::<FILE_LINK_INFORMATION, u16, 1>::with_capacity(link, new_path.len());
12031203
buffer.extend_tail_from_slice(new_path.as_slice());
12041204

1205+
// NtSetInformationFile fails with STATUS_INFO_LENGTH_MISMATCH for a buffer
1206+
// shorter than the whole FILE_LINK_INFORMATION, whose size counts the first
1207+
// FileName unit and the trailing padding (24 bytes on 64-bit Windows), while
1208+
// the header above stops at FileName. Pad a name of one UTF-16 unit with a
1209+
// NUL that FileNameLength leaves out.
1210+
while buffer.total_byte_len() < size_of::<FileSystem::FILE_LINK_INFORMATION>() {
1211+
buffer.push_tail(0);
1212+
}
1213+
12051214
set_information_file(handle, &buffer)?;
12061215

12071216
Ok(())

0 commit comments

Comments
 (0)