Skip to content

Commit cfdb721

Browse files
authored
Merge pull request #2241 from microsoft/agents/issue-2240-layer-name-unicode-case
sandbox: match a --layer name's non-ASCII letters in any case on Windows
2 parents 621de94 + 055c4a3 commit cfdb721

7 files changed

Lines changed: 95 additions & 13 deletions

File tree

‎doc/run.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,8 @@ unused fields, no object may repeat a field, and arrays and objects nest at most
11511151
32 deep, whatever features of `serde_json` other crates of a build enable. The
11521152
artifact name, SHA-256 digest, and package metadata still bind it to the layer.
11531153
On Windows, whose file names ignore case, the artifact name matches the layer's
1154-
in any ASCII case, so a `--layer` path may spell the name in another case.
1154+
in any case, of letters beyond ASCII too, as Windows compares file names, so a
1155+
`--layer` path may spell the name in another case.
11551156

11561157
A run is the cold-filesystem bootstrap described in
11571158
[the sandbox design](design/4.1-guest-filesystem-and-image-model.md#411-layered-sandbox-root), which nvx-init

‎nvx/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ windows-sys = { version = "0.61", optional = true, features = [
5454
"Wdk_Foundation",
5555
"Wdk_Storage_FileSystem",
5656
"Win32_Foundation",
57+
"Win32_Globalization",
5758
"Win32_Security",
5859
"Win32_Storage_FileSystem",
5960
"Win32_System_Console",

‎nvx/README.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,10 +1083,10 @@ to the crate, for the CLI:
10831083
whole layer, compares the digest with the manifest's `artifact_sha256`, and
10841084
then refuses a `systemd` package, in the order and with the messages of
10851085
`nvx.py`. The manifest's `artifact` must name the layer as the host compares
1086-
file names: in any ASCII case on Windows, whose file names ignore case, and
1087-
exactly elsewhere. A layer without a manifest passes unread. It reads each
1088-
file through the file that it opened, and fails if the file's metadata
1089-
changed meanwhile.
1086+
file names: on Windows, whose file names ignore case, in any case, of letters
1087+
beyond ASCII too, and exactly elsewhere. A layer without a manifest passes
1088+
unread. It reads each file through the file that it opened, and fails if the
1089+
file's metadata changed meanwhile.
10901090
- `DistroBinding::check` compares the layer and its manifest with a binding
10911091
from their metadata alone, and returns the layer, open, as a
10921092
`CheckedDistro`. It never reads, maps, or hashes either file.

‎nvx/src/host/paths.rs‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
//! verbatim prefix `\\?\` that `canonicalize` gives, and turns `\\?\UNC\` into `\\`, unless only
2121
//! the verbatim path names the object, as for a name that ends in a period or a space.
2222
//! - [`is_named`] compares a file name with the name that the CLI expects, as the host's file
23-
//! systems compare names: in any ASCII case on Windows, whose file names ignore case, and
24-
//! exactly elsewhere.
23+
//! systems compare names: on Windows, whose file names ignore case, in any case, of letters
24+
//! beyond ASCII too, and exactly elsewhere.
2525
//!
2626
//! Host paths that are not valid Unicode are refused, as [`text`] refuses them, since the CLI
2727
//! records them as JSON strings and messages name them.
@@ -32,6 +32,9 @@ use std::ffi::OsString;
3232
use std::io;
3333
use std::path::{Component, MAIN_SEPARATOR_STR, Path, PathBuf, Prefix};
3434

35+
#[cfg(all(feature = "cli", windows))]
36+
use crate::host::platform;
37+
3538
/// Returns the error for an operating-system string, which messages call `what`, that is not
3639
/// valid Unicode.
3740
fn not_unicode(what: &str) -> io::Error {
@@ -217,12 +220,16 @@ fn without_verbatim_prefix(path: PathBuf) -> PathBuf {
217220
}
218221

219222
/// Returns whether the file name `name` is `expected`, as the host's file systems compare names:
220-
/// in any ASCII case on Windows, whose file names ignore case, and exactly elsewhere.
223+
/// on Windows, whose file names ignore case, in any case, of letters beyond ASCII too, through
224+
/// the operating system's uppercase table, and exactly elsewhere.
221225
#[cfg(feature = "cli")]
222226
pub(crate) fn is_named(name: &OsStr, expected: &str) -> bool {
223-
if cfg!(windows) {
224-
name.eq_ignore_ascii_case(expected)
225-
} else {
227+
#[cfg(windows)]
228+
{
229+
platform::is_same_file_name(name, expected)
230+
}
231+
#[cfg(not(windows))]
232+
{
226233
name == expected
227234
}
228235
}

‎nvx/src/host/platform/windows/mod.rs‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,36 @@ fn refuse_nul(path: &Path) -> io::Result<()> {
7171
Ok(())
7272
}
7373

74+
/// Returns whether `name` and `expected` are one file name as Windows compares file names: unit
75+
/// for unit, except for case, which `CompareStringOrdinal` ignores through the operating system's
76+
/// uppercase table, for letters beyond ASCII too, so that `É` matches `é` as `E` matches `e`.
77+
#[cfg(feature = "cli")]
78+
pub(crate) fn is_same_file_name(name: &OsStr, expected: &str) -> bool {
79+
use windows_sys::Win32::Globalization::{CSTR_EQUAL, CompareStringOrdinal};
80+
81+
/// The `bIgnoreCase` that asks for a comparison that ignores case: 1, the only true value
82+
/// that the function takes.
83+
const IGNORE_CASE: windows_sys::core::BOOL = 1;
84+
let name: Vec<u16> = name.encode_wide().collect();
85+
let expected: Vec<u16> = expected.encode_utf16().collect();
86+
let (Ok(name_len), Ok(expected_len)) =
87+
(i32::try_from(name.len()), i32::try_from(expected.len()))
88+
else {
89+
return false;
90+
};
91+
// SAFETY: each buffer holds the number of UTF-16 units that the call takes, and the call only
92+
// reads them. A failure returns 0, which is not `CSTR_EQUAL`.
93+
unsafe {
94+
CompareStringOrdinal(
95+
name.as_ptr(),
96+
name_len,
97+
expected.as_ptr(),
98+
expected_len,
99+
IGNORE_CASE,
100+
) == CSTR_EQUAL
101+
}
102+
}
103+
74104
/// Encodes a filesystem path for Win32, resolving it before adding an extended-length prefix
75105
/// when it reaches the legacy directory limit. Existing verbatim and device prefixes are preserved.
76106
fn wide_path(path: &Path) -> io::Result<Vec<u16>> {

‎nvx/src/host/sandbox/distro.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//! and every launch applies it, whether or not the layer has a manifest.
1111
//! - Explicit verification, [`DistroBinding::verify`], reads the layer's manifest,
1212
//! `LAYER.manifest.json` beside the layer, if there is one. The manifest must name the layer,
13-
//! as the host compares file names, in any ASCII case on Windows, and its SHA-256 digest, which
13+
//! as the host compares file names, in any case on Windows, and its SHA-256 digest, which
1414
//! verification compares with a hash of the whole layer, and list the layer's packages, of which
1515
//! the distro policy refuses `systemd`. Verification runs where hashing belongs: where a layer
1616
//! is built, installed, or staged, in diagnostics, and in the preflight of `nvx sandbox run` and
@@ -193,7 +193,7 @@ impl DistroBinding {
193193
/// for `nvx.py`'s outcomes, so a path whose lookup fails counts as none; the check of the
194194
/// binding then refuses that path, as [`check`](Self::check) says. With a manifest, it must
195195
/// be a JSON object of format 1 that names the layer, as the host compares file names, in any
196-
/// ASCII case on Windows, and its SHA-256 digest, with a list of packages that each have a
196+
/// case on Windows, and its SHA-256 digest, with a list of packages that each have a
197197
/// name, of which none is `systemd`. Verification reads the manifest and hashes the whole
198198
/// layer through the files that it opened, and fails if the metadata of either changed while
199199
/// it read them. The binding holds the seals that the files had, and

‎nvx/src/host/sandbox/distro/tests.rs‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,49 @@ fn verification_takes_the_manifest_of_a_layer_whose_path_spells_its_name_in_anot
191191
binding.check(&layer, DISTRO_POLICY_REVISION).unwrap();
192192
}
193193

194+
#[cfg(windows)]
195+
#[test]
196+
fn verification_takes_the_manifest_of_a_layer_whose_path_spells_a_non_ascii_letter_in_another_case()
197+
{
198+
let directory = tempfile::tempdir().unwrap();
199+
let image = directory.path().join("distro-\u{e9}.erofs");
200+
let manifest = directory.path().join("distro-\u{e9}.erofs.manifest.json");
201+
fs::write(&image, b"distro layer\n").unwrap();
202+
let digest: String = Sha256::digest(fs::read(&image).unwrap())
203+
.iter()
204+
.map(|byte| format!("{byte:02x}"))
205+
.collect();
206+
let write_manifest = |artifact: &str| {
207+
fs::write(
208+
&manifest,
209+
format!(
210+
"{{\"format\": 1, \"artifact\": \"{artifact}\", \"artifact_sha256\": \
211+
\"{digest}\", \"packages\": []}}"
212+
),
213+
)
214+
.unwrap();
215+
};
216+
// Windows ignores the case of non-ASCII letters too, so `distro-É.erofs` names the layer
217+
// `distro-é.erofs` and the manifest beside it, whose artifact is `distro-é.erofs`.
218+
write_manifest("distro-\u{e9}.erofs");
219+
let spelled = directory.path().join("distro-\u{c9}.erofs");
220+
let layer = SandboxLayer::new(LayerRole::Distro, spelled.to_str().unwrap(), UUID);
221+
let binding = DistroBinding::verify(&layer).unwrap();
222+
assert_eq!(binding.image, seal_of(&image));
223+
assert_eq!(binding.manifest, Some(seal_of(&manifest)));
224+
binding.check(&layer, DISTRO_POLICY_REVISION).unwrap();
225+
// A manifest that names another letter, `è`, rather than the same one in another case, names
226+
// another artifact.
227+
write_manifest("distro-\u{e8}.erofs");
228+
assert_eq!(
229+
failure(DistroBinding::verify(&layer)),
230+
(
231+
ErrorKind::Unsupported,
232+
"sandbox distro manifest does not match its artifact".to_owned()
233+
)
234+
);
235+
}
236+
194237
#[test]
195238
fn bindings_round_trip_through_their_serde_form() {
196239
let staged = Staged::new(false);

0 commit comments

Comments
 (0)