fix(zseri/store-ref-scanner): no_std support and runtime panics

This also changes the fuzzing infrastructure from proptest to cargo-fuzz,
and this lead to the discovery of two mishandlings of edge-cases:

* when a "path_to_store" is at the end of the input, it tried to access
  the input slice out-of-bounds (the `just_store` test covers that now)
* non-ASCII characters lead to an out-of-bounds access in HalfBytesMask
  (the `non_ascii` test covers that now)

Change-Id: Icaa2518dcd93e1789a2c0da4cf0fec46016d3bad
Reviewed-on: https://cl.tvl.fyi/c/depot/+/4604
Tested-by: BuildkiteCI
Reviewed-by: zseri <zseri.devel@ytrizja.de>
This commit is contained in:
zseri 2021-12-25 03:17:06 +01:00
parent 5f2b37bdb0
commit f4dddea4c3
11 changed files with 239 additions and 609 deletions

View file

@ -1,10 +1,8 @@
use crate::hbm::HalfBytesMask;
use camino::Utf8PathBuf;
use once_cell::sync::Lazy;
pub struct StoreSpec {
pub struct StoreSpec<'path> {
/// path to store without trailing slash
pub path_to_store: Utf8PathBuf,
pub path_to_store: &'path str,
/// compressed map of allowed ASCII characters in hash part
pub valid_hashbytes: HalfBytesMask,
@ -16,7 +14,7 @@ pub struct StoreSpec {
pub hashbytes_len: u8,
}
impl StoreSpec {
impl StoreSpec<'_> {
pub(crate) fn check_rest(&self, rest: &[u8]) -> bool {
let hbl = self.hashbytes_len.into();
rest.iter()
@ -25,22 +23,18 @@ impl StoreSpec {
.count()
== hbl
}
pub const DFL_NIX2: StoreSpec<'static> = StoreSpec {
path_to_store: "/nix/store",
valid_hashbytes: HalfBytesMask::B32_REVSHA256,
valid_restbytes: HalfBytesMask::DFL_REST,
hashbytes_len: 32,
};
pub const DFL_YZIX1: StoreSpec<'static> = StoreSpec {
path_to_store: "/yzixs",
valid_hashbytes: HalfBytesMask::B64_BLAKE2B256,
valid_restbytes: HalfBytesMask::DFL_REST,
hashbytes_len: 43,
};
}
pub static SPEC_DFL_NIX2: Lazy<StoreSpec> = Lazy::new(|| StoreSpec {
path_to_store: "/nix/store".into(),
valid_hashbytes: HalfBytesMask::B32_REVSHA256,
valid_restbytes: HalfBytesMask::from_bytes(
b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-._?=",
),
hashbytes_len: 32,
});
pub static SPEC_DFL_YZIX1: Lazy<StoreSpec> = Lazy::new(|| StoreSpec {
path_to_store: "/yzixs".into(),
valid_hashbytes: HalfBytesMask::B64_BLAKE2B256,
valid_restbytes: HalfBytesMask::from_bytes(
b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-._?=",
),
hashbytes_len: 43,
});