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
3 changes: 2 additions & 1 deletion doc/run.md
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,8 @@ unused fields, no object may repeat a field, and arrays and objects nest at most
32 deep, whatever features of `serde_json` other crates of a build enable. The
artifact name, SHA-256 digest, and package metadata still bind it to the layer.
On Windows, whose file names ignore case, the artifact name matches the layer's
in any ASCII case, so a `--layer` path may spell the name in another case.
in any case, of letters beyond ASCII too, as Windows compares file names, so a
`--layer` path may spell the name in another case.

A run is the cold-filesystem bootstrap described in
[the sandbox design](design/4.1-guest-filesystem-and-image-model.md#411-layered-sandbox-root), which nvx-init
Expand Down
1 change: 1 addition & 0 deletions nvx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ windows-sys = { version = "0.61", optional = true, features = [
"Wdk_Foundation",
"Wdk_Storage_FileSystem",
"Win32_Foundation",
"Win32_Globalization",
"Win32_Security",
"Win32_Storage_FileSystem",
"Win32_System_Console",
Expand Down
8 changes: 4 additions & 4 deletions nvx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1083,10 +1083,10 @@ to the crate, for the CLI:
whole layer, compares the digest with the manifest's `artifact_sha256`, and
then refuses a `systemd` package, in the order and with the messages of
`nvx.py`. The manifest's `artifact` must name the layer as the host compares
file names: in any ASCII case on Windows, whose file names ignore case, and
exactly elsewhere. A layer without a manifest passes unread. It reads each
file through the file that it opened, and fails if the file's metadata
changed meanwhile.
file names: on Windows, whose file names ignore case, in any case, of letters
beyond ASCII too, and exactly elsewhere. A layer without a manifest passes
unread. It reads each file through the file that it opened, and fails if the
file's metadata changed meanwhile.
- `DistroBinding::check` compares the layer and its manifest with a binding
from their metadata alone, and returns the layer, open, as a
`CheckedDistro`. It never reads, maps, or hashes either file.
Expand Down
19 changes: 13 additions & 6 deletions nvx/src/host/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
//! verbatim prefix `\\?\` that `canonicalize` gives, and turns `\\?\UNC\` into `\\`, unless only
//! the verbatim path names the object, as for a name that ends in a period or a space.
//! - [`is_named`] compares a file name with the name that the CLI expects, as the host's file
//! systems compare names: in any ASCII case on Windows, whose file names ignore case, and
//! exactly elsewhere.
//! systems compare names: on Windows, whose file names ignore case, in any case, of letters
//! beyond ASCII too, and exactly elsewhere.
//!
//! Host paths that are not valid Unicode are refused, as [`text`] refuses them, since the CLI
//! records them as JSON strings and messages name them.
Expand All @@ -32,6 +32,9 @@ use std::ffi::OsString;
use std::io;
use std::path::{Component, MAIN_SEPARATOR_STR, Path, PathBuf, Prefix};

#[cfg(all(feature = "cli", windows))]
use crate::host::platform;

/// Returns the error for an operating-system string, which messages call `what`, that is not
/// valid Unicode.
fn not_unicode(what: &str) -> io::Error {
Expand Down Expand Up @@ -217,12 +220,16 @@ fn without_verbatim_prefix(path: PathBuf) -> PathBuf {
}

/// Returns whether the file name `name` is `expected`, as the host's file systems compare names:
/// in any ASCII case on Windows, whose file names ignore case, and exactly elsewhere.
/// on Windows, whose file names ignore case, in any case, of letters beyond ASCII too, through
/// the operating system's uppercase table, and exactly elsewhere.
#[cfg(feature = "cli")]
pub(crate) fn is_named(name: &OsStr, expected: &str) -> bool {
if cfg!(windows) {
name.eq_ignore_ascii_case(expected)
} else {
#[cfg(windows)]
{
platform::is_same_file_name(name, expected)
}
#[cfg(not(windows))]
{
name == expected
}
}
Expand Down
30 changes: 30 additions & 0 deletions nvx/src/host/platform/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,36 @@ fn refuse_nul(path: &Path) -> io::Result<()> {
Ok(())
}

/// Returns whether `name` and `expected` are one file name as Windows compares file names: unit
/// for unit, except for case, which `CompareStringOrdinal` ignores through the operating system's
/// uppercase table, for letters beyond ASCII too, so that `É` matches `é` as `E` matches `e`.
#[cfg(feature = "cli")]
pub(crate) fn is_same_file_name(name: &OsStr, expected: &str) -> bool {
use windows_sys::Win32::Globalization::{CSTR_EQUAL, CompareStringOrdinal};

/// The `bIgnoreCase` that asks for a comparison that ignores case: 1, the only true value
/// that the function takes.
const IGNORE_CASE: windows_sys::core::BOOL = 1;
let name: Vec<u16> = name.encode_wide().collect();
let expected: Vec<u16> = expected.encode_utf16().collect();
let (Ok(name_len), Ok(expected_len)) =
(i32::try_from(name.len()), i32::try_from(expected.len()))
else {
return false;
};
// SAFETY: each buffer holds the number of UTF-16 units that the call takes, and the call only
// reads them. A failure returns 0, which is not `CSTR_EQUAL`.
unsafe {
CompareStringOrdinal(
name.as_ptr(),
name_len,
expected.as_ptr(),
expected_len,
IGNORE_CASE,
) == CSTR_EQUAL
}
}

/// Encodes a filesystem path for Win32, resolving it before adding an extended-length prefix
/// when it reaches the legacy directory limit. Existing verbatim and device prefixes are preserved.
fn wide_path(path: &Path) -> io::Result<Vec<u16>> {
Expand Down
4 changes: 2 additions & 2 deletions nvx/src/host/sandbox/distro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//! and every launch applies it, whether or not the layer has a manifest.
//! - Explicit verification, [`DistroBinding::verify`], reads the layer's manifest,
//! `LAYER.manifest.json` beside the layer, if there is one. The manifest must name the layer,
//! as the host compares file names, in any ASCII case on Windows, and its SHA-256 digest, which
//! as the host compares file names, in any case on Windows, and its SHA-256 digest, which
//! verification compares with a hash of the whole layer, and list the layer's packages, of which
//! the distro policy refuses `systemd`. Verification runs where hashing belongs: where a layer
//! is built, installed, or staged, in diagnostics, and in the preflight of `nvx sandbox run` and
Expand Down Expand Up @@ -193,7 +193,7 @@ impl DistroBinding {
/// for `nvx.py`'s outcomes, so a path whose lookup fails counts as none; the check of the
/// binding then refuses that path, as [`check`](Self::check) says. With a manifest, it must
/// be a JSON object of format 1 that names the layer, as the host compares file names, in any
/// ASCII case on Windows, and its SHA-256 digest, with a list of packages that each have a
/// case on Windows, and its SHA-256 digest, with a list of packages that each have a
/// name, of which none is `systemd`. Verification reads the manifest and hashes the whole
/// layer through the files that it opened, and fails if the metadata of either changed while
/// it read them. The binding holds the seals that the files had, and
Expand Down
43 changes: 43 additions & 0 deletions nvx/src/host/sandbox/distro/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,49 @@ fn verification_takes_the_manifest_of_a_layer_whose_path_spells_its_name_in_anot
binding.check(&layer, DISTRO_POLICY_REVISION).unwrap();
}

#[cfg(windows)]
#[test]
fn verification_takes_the_manifest_of_a_layer_whose_path_spells_a_non_ascii_letter_in_another_case()
{
let directory = tempfile::tempdir().unwrap();
let image = directory.path().join("distro-\u{e9}.erofs");
let manifest = directory.path().join("distro-\u{e9}.erofs.manifest.json");
fs::write(&image, b"distro layer\n").unwrap();
let digest: String = Sha256::digest(fs::read(&image).unwrap())
.iter()
.map(|byte| format!("{byte:02x}"))
.collect();
let write_manifest = |artifact: &str| {
fs::write(
&manifest,
format!(
"{{\"format\": 1, \"artifact\": \"{artifact}\", \"artifact_sha256\": \
\"{digest}\", \"packages\": []}}"
),
)
.unwrap();
};
// Windows ignores the case of non-ASCII letters too, so `distro-É.erofs` names the layer
// `distro-é.erofs` and the manifest beside it, whose artifact is `distro-é.erofs`.
write_manifest("distro-\u{e9}.erofs");
let spelled = directory.path().join("distro-\u{c9}.erofs");
let layer = SandboxLayer::new(LayerRole::Distro, spelled.to_str().unwrap(), UUID);
let binding = DistroBinding::verify(&layer).unwrap();
assert_eq!(binding.image, seal_of(&image));
assert_eq!(binding.manifest, Some(seal_of(&manifest)));
binding.check(&layer, DISTRO_POLICY_REVISION).unwrap();
// A manifest that names another letter, `è`, rather than the same one in another case, names
// another artifact.
write_manifest("distro-\u{e8}.erofs");
assert_eq!(
failure(DistroBinding::verify(&layer)),
(
ErrorKind::Unsupported,
"sandbox distro manifest does not match its artifact".to_owned()
)
);
}

#[test]
fn bindings_round_trip_through_their_serde_form() {
let staged = Staged::new(false);
Expand Down
Loading