Skip to content

Commit c7f6a04

Browse files
ppennaCopilot
andcommitted
lxutil: reject xattr names longer than an NTFS EA name allows
On Windows, lxutil stores a Linux xattr as an NTFS extended attribute named `LX.` and the xattr's name. FILE_GET_EA_INFORMATION and FILE_FULL_EA_INFORMATION give that EA name's length in one byte, but lxutil's limit on xattr names, LX_UTIL_XATTR_NAME_MAX, was u16::MAX - 3 bytes, and lxutil wrote the length with `as u8`, which kept only its low 8 bits (microsoft/nvx#2137). A FUSE_GETXATTR of a 262-byte name that starts with `user.a`, which a guest can send by building the request itself, thus queried the 9-byte EA name `LX.user.a`, 265 bytes modulo 256, and returned the value of the `user.a` attribute. Names of 252 to 261 bytes failed with EINVAL or ENODATA, depending on the operation and the length, rather than with ERANGE, and XATTR_CREATE of the 262-byte name failed with EEXIST. NTFS accepts EA names of at most 254 bytes, so lxutil can store xattr names of at most 251 bytes. LX_UTIL_XATTR_NAME_MAX is now that length. set_name, like get and set, accepts a name of exactly that length rather than rejecting it with `>=`, and returns the EA name's length as the u8 that the headers hold. get, set, and remove thus fail every longer name with ERANGE before it reaches NTFS, as Linux fails names longer than its 255 bytes. The documentation of LxVolume's xattr methods now gives the limit. xattr_name_length sets `user.a`; sets, reads, and removes an attribute whose name has the longest length that the platform allows, 251 bytes on Windows and 255 on Linux; expects every longer name, up to 262 bytes, to fail with ERANGE in get, get of the size, set with no flag, with XATTR_CREATE, and with XATTR_REPLACE, and remove; and expects `user.a` to keep its value. At feature/microvm/dev 1633b37, it fails on Windows 11 Enterprise (build 26300, NTFS) at the first longer name, whose get failed with EINVAL. A one-off test there, not committed, showed the rest: every operation on a 252- or 253-byte name failed with EINVAL; on 254- to 261-byte names, get, XATTR_REPLACE, and remove failed with ENODATA, and set and XATTR_CREATE with EINVAL; and the 262-byte name read `secret`, the value of `user.a`, failed XATTR_CREATE with EEXIST, and failed set, XATTR_REPLACE, and remove with EINVAL. With the fix, the lxutil tests pass on Windows 11 and on Azure Linux 3 (kernel 6.18, ext4). Linux builds do not compile xattr.rs, so the fix leaves Linux's behavior as it was. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 770bc61 commit c7f6a04

2 files changed

Lines changed: 119 additions & 29 deletions

File tree

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

Lines changed: 101 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -500,10 +500,10 @@ impl LxVolume {
500500
/// Extended attribute names are not case sensitive. They are stored as upper case in NTFS but
501501
/// `list_xattr` will report them as lower case for greater compatibility with Linux.
502502
///
503-
/// Extended attribute names are prefixed with "LX.", and have a slightly shorter maximum length
504-
/// limit than Linux. Attribute values are prefixed with a 4-byte header to allow for "empty"
505-
/// values, which NTFS does not normally allow. `get_xattr` and `list_xattr` will strip these
506-
/// prefixes.
503+
/// Extended attribute names are prefixed with "LX.", so they can be at most 251 bytes long
504+
/// rather than 255 as on Linux; longer names fail with `ERANGE`. Attribute values are prefixed
505+
/// with a 4-byte header to allow for "empty" values, which NTFS does not normally allow.
506+
/// `get_xattr` and `list_xattr` will strip these prefixes.
507507
///
508508
/// Security for accessing the various attribute namespaces is not enforced.
509509
///
@@ -531,10 +531,10 @@ impl LxVolume {
531531
/// Extended attribute names are not case sensitive. They are stored as upper case in NTFS but
532532
/// `list_xattr` will report them as lower case for greater compatibility with Linux.
533533
///
534-
/// Extended attribute names are prefixed with "LX.", and have a slightly shorter maximum length
535-
/// limit than Linux. Attribute values are prefixed with a 4-byte header to allow for "empty"
536-
/// values, which NTFS does not normally allow. `get_xattr` and `list_xattr` will strip these
537-
/// prefixes.
534+
/// Extended attribute names are prefixed with "LX.", so they can be at most 251 bytes long
535+
/// rather than 255 as on Linux; longer names fail with `ERANGE`. Attribute values are prefixed
536+
/// with a 4-byte header to allow for "empty" values, which NTFS does not normally allow.
537+
/// `get_xattr` and `list_xattr` will strip these prefixes.
538538
///
539539
/// Security for accessing the various attribute namespaces is not enforced.
540540
pub fn get_xattr(
@@ -557,10 +557,10 @@ impl LxVolume {
557557
/// Extended attribute names are not case sensitive. They are stored as upper case in NTFS but
558558
/// `list_xattr` will report them as lower case for greater compatibility with Linux.
559559
///
560-
/// Extended attribute names are prefixed with "LX.", and have a slightly shorter maximum length
561-
/// limit than Linux. Attribute values are prefixed with a 4-byte header to allow for "empty"
562-
/// values, which NTFS does not normally allow. `get_xattr` and `list_xattr` will strip these
563-
/// prefixes.
560+
/// Extended attribute names are prefixed with "LX.", so they can be at most 251 bytes long
561+
/// rather than 255 as on Linux; longer names fail with `ERANGE`. Attribute values are prefixed
562+
/// with a 4-byte header to allow for "empty" values, which NTFS does not normally allow.
563+
/// `get_xattr` and `list_xattr` will strip these prefixes.
564564
///
565565
/// Security for accessing the various attribute namespaces is not enforced.
566566
pub fn list_xattr(&self, path: impl AsRef<Path>, list: Option<&mut [u8]>) -> lx::Result<usize> {
@@ -574,10 +574,10 @@ impl LxVolume {
574574
/// Extended attribute names are not case sensitive. They are stored as upper case in NTFS but
575575
/// `list_xattr` will report them as lower case for greater compatibility with Linux.
576576
///
577-
/// Extended attribute names are prefixed with "LX.", and have a slightly shorter maximum length
578-
/// limit than Linux. Attribute values are prefixed with a 4-byte header to allow for "empty"
579-
/// values, which NTFS does not normally allow. `get_xattr` and `list_xattr` will strip these
580-
/// prefixes.
577+
/// Extended attribute names are prefixed with "LX.", so they can be at most 251 bytes long
578+
/// rather than 255 as on Linux; longer names fail with `ERANGE`. Attribute values are prefixed
579+
/// with a 4-byte header to allow for "empty" values, which NTFS does not normally allow.
580+
/// `get_xattr` and `list_xattr` will strip these prefixes.
581581
///
582582
/// Security for accessing the various attribute namespaces is not enforced.
583583
pub fn remove_xattr(
@@ -2550,6 +2550,91 @@ mod tests {
25502550
assert_eq!(&buffer[..3], b"baz");
25512551
}
25522552

2553+
#[test]
2554+
fn xattr_name_length() {
2555+
// Linux allows names of up to 255 bytes. On Windows, an attribute is an NTFS extended
2556+
// attribute named "LX." and the attribute's name, and NTFS allows EA names of up to 254
2557+
// bytes.
2558+
const NAME_MAX: usize = if cfg!(windows) { 251 } else { 255 };
2559+
2560+
fn name(len: usize) -> Vec<u8> {
2561+
let mut name = b"user.a".to_vec();
2562+
name.resize(len, b'x');
2563+
name
2564+
}
2565+
2566+
let env = TestEnv::new();
2567+
env.create_file("testfile", "test");
2568+
env.volume
2569+
.set_xattr("testfile", "user.a", b"secret", 0)
2570+
.unwrap();
2571+
2572+
// An attribute with the longest name can be set, read, and removed.
2573+
let longest = name(NAME_MAX);
2574+
let longest = lx::LxStr::from_bytes(&longest);
2575+
env.volume
2576+
.set_xattr("testfile", longest, b"long", 0)
2577+
.unwrap();
2578+
2579+
let mut value = [0u8; 16];
2580+
let size = env
2581+
.volume
2582+
.get_xattr("testfile", longest, Some(&mut value))
2583+
.unwrap();
2584+
2585+
assert_eq!(&value[..size], b"long");
2586+
env.volume.remove_xattr("testfile", longest).unwrap();
2587+
let err = env.volume.get_xattr("testfile", longest, None).unwrap_err();
2588+
2589+
assert_eq!(err.value(), lx::ENODATA);
2590+
2591+
// A longer name fails with ERANGE, rather than reaching an attribute whose name it starts
2592+
// with.
2593+
for len in NAME_MAX + 1..=262 {
2594+
let long = name(len);
2595+
let long = lx::LxStr::from_bytes(&long);
2596+
let results = [
2597+
(
2598+
"get",
2599+
env.volume
2600+
.get_xattr("testfile", long, Some(&mut value))
2601+
.map(drop),
2602+
),
2603+
(
2604+
"get size",
2605+
env.volume.get_xattr("testfile", long, None).map(drop),
2606+
),
2607+
("set", env.volume.set_xattr("testfile", long, b"long", 0)),
2608+
(
2609+
"create",
2610+
env.volume
2611+
.set_xattr("testfile", long, b"long", lx::XATTR_CREATE),
2612+
),
2613+
(
2614+
"replace",
2615+
env.volume
2616+
.set_xattr("testfile", long, b"long", lx::XATTR_REPLACE),
2617+
),
2618+
("remove", env.volume.remove_xattr("testfile", long)),
2619+
];
2620+
2621+
for (operation, result) in results {
2622+
assert_eq!(
2623+
result.map_err(|err| err.value()),
2624+
Err(lx::ERANGE),
2625+
"{operation} of a {len}-byte name"
2626+
);
2627+
}
2628+
}
2629+
2630+
let size = env
2631+
.volume
2632+
.get_xattr("testfile", "user.a", Some(&mut value))
2633+
.unwrap();
2634+
2635+
assert_eq!(&value[..size], b"secret");
2636+
}
2637+
25532638
// This test is disabled in CI on Windows only, because it requires NTFS support for setting
25542639
// the case sensitive directory attribute, which is only enabled if the WSL optional component
25552640
// is installed.

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@ const LX_UTIL_CASE_SENSITIVE: &str = "system.wsl_case_sensitive";
1515

1616
const LX_UTIL_XATTR_NAME_PREFIX: &str = "LX.";
1717
const LX_UTIL_XATTR_NAME_PREFIX_LENGTH: usize = LX_UTIL_XATTR_NAME_PREFIX.len();
18-
const LX_UTIL_XATTR_NAME_MAX: usize = u16::MAX as usize - LX_UTIL_XATTR_NAME_PREFIX_LENGTH;
18+
19+
/// The longest EA name, prefix included, that NTFS accepts. `EaNameLength` is one byte, and NTFS
20+
/// rejects a name of 255 bytes.
21+
const LX_UTILP_EA_NAME_MAX: usize = 254;
22+
const LX_UTIL_XATTR_NAME_MAX: usize = LX_UTILP_EA_NAME_MAX - LX_UTIL_XATTR_NAME_PREFIX_LENGTH;
1923

2024
const LX_UTILP_XATTR_QUERY_RESTART_SCAN: i32 = 0x1;
2125
const LX_UTILP_XATTR_QUERY_RETURN_SINGLE_ENTRY: i32 = 0x2;
@@ -114,26 +118,27 @@ pub fn get_system(handle: &OwnedHandle, name: &str, value: Option<&mut [u8]>) ->
114118
Ok(1)
115119
}
116120

117-
/// Copy the Linux EA attribute prefix and the specified name into the start of the provided buffer.
118-
fn set_name(name: &str, buffer: &mut [u8]) -> lx::Result<usize> {
121+
/// Copy the Linux EA attribute prefix and the specified name into the start of the provided buffer,
122+
/// and return the length of the resulting EA name.
123+
fn set_name(name: &str, buffer: &mut [u8]) -> lx::Result<u8> {
119124
let name_bytes = name.as_bytes();
120125

121-
if name_bytes.len() >= LX_UTIL_XATTR_NAME_MAX {
126+
if name_bytes.len() > LX_UTIL_XATTR_NAME_MAX {
122127
return Err(lx::Error::ERANGE);
123128
}
124129

125-
let total_required = LX_UTIL_XATTR_NAME_PREFIX_LENGTH + name_bytes.len() + 1;
130+
let ea_name_len = LX_UTIL_XATTR_NAME_PREFIX_LENGTH + name_bytes.len();
131+
let total_required = ea_name_len + 1;
126132
if buffer.len() < total_required {
127133
return Err(lx::Error::ERANGE);
128134
}
129135

130136
buffer[..LX_UTIL_XATTR_NAME_PREFIX_LENGTH]
131137
.copy_from_slice(LX_UTIL_XATTR_NAME_PREFIX.as_bytes());
132-
buffer[LX_UTIL_XATTR_NAME_PREFIX_LENGTH..LX_UTIL_XATTR_NAME_PREFIX_LENGTH + name_bytes.len()]
133-
.copy_from_slice(name_bytes);
134-
buffer[LX_UTIL_XATTR_NAME_PREFIX_LENGTH + name_bytes.len()] = 0;
138+
buffer[LX_UTIL_XATTR_NAME_PREFIX_LENGTH..ea_name_len].copy_from_slice(name_bytes);
139+
buffer[ea_name_len] = 0;
135140

136-
Ok(LX_UTIL_XATTR_NAME_PREFIX_LENGTH + name_bytes.len())
141+
ea_name_len.try_into().map_err(|_| lx::Error::ERANGE)
137142
}
138143

139144
/// Queries an EA on a file.
@@ -167,7 +172,7 @@ fn query_ea(
167172

168173
let header = FileGetEaInformation {
169174
next_entry_offset: Unalign::new(0),
170-
ea_name_length: name_len as u8,
175+
ea_name_length: name_len,
171176
};
172177
buffer[..size_of::<FileGetEaInformation>()].copy_from_slice(header.as_bytes());
173178
Some(buffer)
@@ -354,13 +359,13 @@ pub fn set(handle: &OwnedHandle, name: &str, value: &[u8], flags: i32) -> lx::Re
354359
let ea_info = FileFullEaInformation {
355360
next_entry_offset: Unalign::new(0),
356361
flags: 0,
357-
ea_name_length: name_len as u8,
362+
ea_name_length: name_len,
358363
ea_value_length: Unalign::new(value_size as u16),
359364
};
360365
buffer[..size_of::<FileFullEaInformation>()].copy_from_slice(ea_info.as_bytes());
361366

362367
// Set the EA value header.
363-
let ea_value_start = size_of::<FileFullEaInformation>() + name_len + 1;
368+
let ea_value_start = size_of::<FileFullEaInformation>() + usize::from(name_len) + 1;
364369
buffer[ea_value_start..ea_value_start + 4]
365370
.copy_from_slice(&LX_UTILP_EA_VALUE_HEADER.to_le_bytes());
366371

@@ -492,7 +497,7 @@ pub fn remove(handle: &OwnedHandle, name: &str) -> lx::Result<()> {
492497
let ea_info = FileFullEaInformation {
493498
next_entry_offset: Unalign::new(0),
494499
flags: 0,
495-
ea_name_length: name_len as u8,
500+
ea_name_length: name_len,
496501
ea_value_length: Unalign::new(0), // Zero length to remove the attribute
497502
};
498503
buf[..size_of::<FileFullEaInformation>()].copy_from_slice(ea_info.as_bytes());

0 commit comments

Comments
 (0)