aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / crates / aien-kv-store / src / seal_budget_tests.rs
| 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | //! Per-key seal budget (#811). The count is installed through |
| 17 | //! [`testing_install_usage_count`] so a test does not seal `2^20` files. |
| 18 | //! |
| 19 | //! Reproduction: |
| 20 | //! `cargo test -p aien-kv-store --lib seal_budget_tests -- --test-threads=8` |
| 21 | |
| 22 | use super::seal_nonce_tests::{ |
| 23 | assert_pattern_absent, kvc_files, open_at, publish, terminal_for, test_fields, Tmp, |
| 24 | SYNTHETIC_KEY, |
| 25 | }; |
| 26 | use crate::host_fs::HostFsStore; |
| 27 | use crate::identity::NamespaceFields; |
| 28 | use crate::pin::{HolderGuard, PinnedSource}; |
| 29 | use crate::record::HEADER_REGION_LEN; |
| 30 | use crate::seal::{ |
| 31 | self, testing_install_usage_count, testing_read_key_fingerprints, testing_read_usage_count, |
| 32 | testing_usage_path, NonceSource, SealError, SealKey, RECOMMENDED_SEALS_PER_KEY, USAGE_DIR_NAME, |
| 33 | }; |
| 34 | use crate::{chunk_chain, Incarnation, KvStore, Terminal}; |
| 35 | use aien_platform::PlatformError; |
| 36 | use sha2::{Digest, Sha256}; |
| 37 | use std::os::unix::fs::{MetadataExt, PermissionsExt}; |
| 38 | use std::sync::{Arc, Barrier}; |
| 39 | |
| 40 | const BS: usize = 8192; |
| 41 | |
| 42 | fn blank() -> Vec<u8> { |
| 43 | vec![0x11u8; HEADER_REGION_LEN + 32] |
| 44 | } |
| 45 | |
| 46 | fn hex_fingerprint(key: &[u8; 32]) -> String { |
| 47 | let dig = Sha256::digest(key); |
| 48 | let mut hex = String::with_capacity(64); |
| 49 | const NIBBLE: &[u8; 16] = b"0123456789abcdef"; |
| 50 | for byte in dig { |
| 51 | hex.push(NIBBLE[(byte >> 4) as usize] as char); |
| 52 | hex.push(NIBBLE[(byte & 0x0f) as usize] as char); |
| 53 | } |
| 54 | hex |
| 55 | } |
| 56 | |
| 57 | struct FailNonce; |
| 58 | |
| 59 | impl NonceSource for FailNonce { |
| 60 | fn fill_nonce(&self, _out: &mut [u8; seal::NONCE_LEN]) -> Result<(), SealError> { |
| 61 | Err(SealError::Random) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | #[test] |
| 66 | fn seal_budget_refuses_at_the_installed_limit_and_leaves_the_buffer() { |
| 67 | let dir = Tmp::new("at-limit"); |
| 68 | let key = SealKey::from_bytes([0x11; 32]); |
| 69 | testing_install_usage_count(&dir.0, &key, RECOMMENDED_SEALS_PER_KEY).unwrap(); |
| 70 | let mut file = blank(); |
| 71 | let before = file.clone(); |
| 72 | assert_eq!( |
| 73 | key.seal_file(&dir.0, &mut file), |
| 74 | Err(SealError::Budget), |
| 75 | "the 2^20 count is already spent" |
| 76 | ); |
| 77 | assert_eq!(file, before); |
| 78 | assert!(!seal::is_sealed_region(&file[..HEADER_REGION_LEN])); |
| 79 | assert_eq!( |
| 80 | testing_read_usage_count(&dir.0, &key).unwrap(), |
| 81 | RECOMMENDED_SEALS_PER_KEY |
| 82 | ); |
| 83 | } |
| 84 | |
| 85 | #[test] |
| 86 | fn seal_budget_last_allowed_seal_persists_across_a_new_key() { |
| 87 | let dir = Tmp::new("reload"); |
| 88 | let key = SealKey::from_bytes([0x12; 32]); |
| 89 | testing_install_usage_count(&dir.0, &key, RECOMMENDED_SEALS_PER_KEY - 1).unwrap(); |
| 90 | let mut file = blank(); |
| 91 | key.seal_file(&dir.0, &mut file).unwrap(); |
| 92 | assert!(seal::is_sealed_region(&file[..HEADER_REGION_LEN])); |
| 93 | let (region, body) = file.split_at(HEADER_REGION_LEN); |
| 94 | let mut body = body.to_vec(); |
| 95 | key.open_payload(region, &mut body).unwrap(); |
| 96 | assert_eq!(body, vec![0x11u8; 32]); |
| 97 | |
| 98 | let reloaded = SealKey::from_bytes([0x12; 32]); |
| 99 | assert_eq!( |
| 100 | testing_read_usage_count(&dir.0, &reloaded).unwrap(), |
| 101 | RECOMMENDED_SEALS_PER_KEY, |
| 102 | "a new SealKey reads the count from the directory" |
| 103 | ); |
| 104 | let mut next = blank(); |
| 105 | let before = next.clone(); |
| 106 | assert_eq!( |
| 107 | reloaded.seal_file(&dir.0, &mut next), |
| 108 | Err(SealError::Budget) |
| 109 | ); |
| 110 | assert_eq!(next, before); |
| 111 | assert_eq!( |
| 112 | testing_read_usage_count(&dir.0, &reloaded).unwrap(), |
| 113 | RECOMMENDED_SEALS_PER_KEY |
| 114 | ); |
| 115 | } |
| 116 | |
| 117 | #[test] |
| 118 | fn seal_budget_persist_fault_stops_the_seal_and_keeps_the_count() { |
| 119 | let dir = Tmp::new("fault"); |
| 120 | let key = SealKey::from_bytes([0x13; 32]); |
| 121 | testing_install_usage_count(&dir.0, &key, 4).unwrap(); |
| 122 | key.testing_fail_next_usage_persist(); |
| 123 | let mut file = blank(); |
| 124 | let before = file.clone(); |
| 125 | assert_eq!(key.seal_file(&dir.0, &mut file), Err(SealError::UsageStore)); |
| 126 | assert_eq!(file, before); |
| 127 | assert!(!seal::is_sealed_region(&file[..HEADER_REGION_LEN])); |
| 128 | assert_eq!(testing_read_usage_count(&dir.0, &key).unwrap(), 4); |
| 129 | // The fault is one shot. The next seal commits. |
| 130 | key.seal_file(&dir.0, &mut file).unwrap(); |
| 131 | assert_eq!(testing_read_usage_count(&dir.0, &key).unwrap(), 5); |
| 132 | assert!(seal::is_sealed_region(&file[..HEADER_REGION_LEN])); |
| 133 | } |
| 134 | |
| 135 | #[test] |
| 136 | fn seal_budget_usage_path_that_is_a_file_does_not_seal() { |
| 137 | let dir = Tmp::new("not-a-dir"); |
| 138 | let bogus = dir.0.join("not-a-directory"); |
| 139 | std::fs::write(&bogus, b"x").unwrap(); |
| 140 | let key = SealKey::from_bytes([0x14; 32]); |
| 141 | let mut file = blank(); |
| 142 | let before = file.clone(); |
| 143 | assert_eq!(key.seal_file(&bogus, &mut file), Err(SealError::UsageStore)); |
| 144 | assert_eq!(file, before); |
| 145 | assert_eq!(std::fs::read(&bogus).unwrap(), b"x"); |
| 146 | } |
| 147 | |
| 148 | #[test] |
| 149 | fn seal_budget_corrupt_count_is_not_reset_and_does_not_seal() { |
| 150 | let dir = Tmp::new("corrupt"); |
| 151 | let key = SealKey::from_bytes([0x15; 32]); |
| 152 | testing_install_usage_count(&dir.0, &key, 7).unwrap(); |
| 153 | let path = testing_usage_path(&dir.0, &key); |
| 154 | let mut bytes = std::fs::read(&path).unwrap(); |
| 155 | bytes[0] ^= 0x01; |
| 156 | std::fs::write(&path, &bytes).unwrap(); |
| 157 | let mut file = blank(); |
| 158 | let before = file.clone(); |
| 159 | assert_eq!(key.seal_file(&dir.0, &mut file), Err(SealError::UsageStore)); |
| 160 | assert_eq!(file, before); |
| 161 | assert_eq!(std::fs::read(&path).unwrap(), bytes); |
| 162 | } |
| 163 | |
| 164 | #[test] |
| 165 | fn seal_budget_short_buffer_does_not_create_a_count() { |
| 166 | let dir = Tmp::new("short"); |
| 167 | let key = SealKey::from_bytes([0x16; 32]); |
| 168 | let mut short = vec![1u8; 8]; |
| 169 | assert_eq!(key.seal_file(&dir.0, &mut short), Err(SealError::Short)); |
| 170 | assert!(!testing_usage_path(&dir.0, &key).exists()); |
| 171 | assert!( |
| 172 | std::fs::read_dir(&dir.0).unwrap().next().is_none(), |
| 173 | "a short buffer must not create LOCK or a count file" |
| 174 | ); |
| 175 | assert_eq!(testing_read_usage_count(&dir.0, &key).unwrap(), 0); |
| 176 | } |
| 177 | |
| 178 | #[test] |
| 179 | fn seal_budget_random_failure_does_not_consume_a_count() { |
| 180 | let dir = Tmp::new("random"); |
| 181 | let key = SealKey::from_bytes(SYNTHETIC_KEY).with_nonce_source(Arc::new(FailNonce)); |
| 182 | let mut file = blank(); |
| 183 | let before = file.clone(); |
| 184 | assert_eq!(key.seal_file(&dir.0, &mut file), Err(SealError::Random)); |
| 185 | assert_eq!(file, before); |
| 186 | assert_eq!(testing_read_usage_count(&dir.0, &key).unwrap(), 0); |
| 187 | assert!(!testing_usage_path(&dir.0, &key).exists()); |
| 188 | } |
| 189 | |
| 190 | #[test] |
| 191 | fn seal_budget_is_per_key_fingerprint() { |
| 192 | let dir = Tmp::new("two-keys"); |
| 193 | let a = SealKey::from_bytes([0x17; 32]); |
| 194 | let b = SealKey::from_bytes([0x18; 32]); |
| 195 | testing_install_usage_count(&dir.0, &a, RECOMMENDED_SEALS_PER_KEY).unwrap(); |
| 196 | let mut file = blank(); |
| 197 | assert_eq!(a.seal_file(&dir.0, &mut file), Err(SealError::Budget)); |
| 198 | b.seal_file(&dir.0, &mut file).unwrap(); |
| 199 | assert!(seal::is_sealed_region(&file[..HEADER_REGION_LEN])); |
| 200 | assert_eq!( |
| 201 | testing_read_usage_count(&dir.0, &a).unwrap(), |
| 202 | RECOMMENDED_SEALS_PER_KEY |
| 203 | ); |
| 204 | assert_eq!(testing_read_usage_count(&dir.0, &b).unwrap(), 1); |
| 205 | let expect = dir.0.join(hex_fingerprint(&[0x18; 32])); |
| 206 | assert_eq!(testing_usage_path(&dir.0, &b), expect); |
| 207 | let mode = std::fs::metadata(&expect).unwrap().permissions().mode() & 0o777; |
| 208 | assert_eq!(mode, 0o600); |
| 209 | } |
| 210 | |
| 211 | #[test] |
| 212 | fn seal_budget_two_threads_each_commit_a_count() { |
| 213 | let dir = Tmp::new("threads"); |
| 214 | let key = Arc::new(SealKey::from_bytes([0x19; 32])); |
| 215 | let barrier = Arc::new(Barrier::new(2)); |
| 216 | let mut handles = Vec::new(); |
| 217 | for _ in 0..2 { |
| 218 | let key = Arc::clone(&key); |
| 219 | let barrier = Arc::clone(&barrier); |
| 220 | let path = dir.0.clone(); |
| 221 | handles.push(std::thread::spawn(move || { |
| 222 | barrier.wait(); |
| 223 | let mut file = blank(); |
| 224 | key.seal_file(&path, &mut file).unwrap(); |
| 225 | })); |
| 226 | } |
| 227 | for handle in handles { |
| 228 | handle.join().unwrap(); |
| 229 | } |
| 230 | assert_eq!(testing_read_usage_count(&dir.0, &key).unwrap(), 2); |
| 231 | } |
| 232 | |
| 233 | #[test] |
| 234 | fn seal_budget_host_count_survives_reopen_and_then_refuses() { |
| 235 | let root = Tmp::new("host"); |
| 236 | let fields = test_fields(); |
| 237 | let usage = root.0.join(USAGE_DIR_NAME); |
| 238 | let s = open_at(&root.0, Arc::new(SealKey::from_bytes(SYNTHETIC_KEY))); |
| 239 | assert!(usage.is_dir(), "open creates seal-usage"); |
| 240 | let reader = SealKey::from_bytes(SYNTHETIC_KEY); |
| 241 | assert_eq!(testing_read_usage_count(&usage, &reader).unwrap(), 0); |
| 242 | let first = publish(&s, &fields, 1); |
| 243 | assert_eq!(testing_read_usage_count(&usage, &reader).unwrap(), 1); |
| 244 | drop(s); |
| 245 | |
| 246 | let s = open_at(&root.0, Arc::new(SealKey::from_bytes(SYNTHETIC_KEY))); |
| 247 | assert_eq!( |
| 248 | testing_read_usage_count(&usage, &SealKey::from_bytes(SYNTHETIC_KEY)).unwrap(), |
| 249 | 1, |
| 250 | "reopen sees the count from the previous store" |
| 251 | ); |
| 252 | let _second = publish(&s, &fields, 2); |
| 253 | assert_eq!(testing_read_usage_count(&usage, &reader).unwrap(), 2); |
| 254 | drop(s); |
| 255 | |
| 256 | testing_install_usage_count(&usage, &reader, RECOMMENDED_SEALS_PER_KEY).unwrap(); |
| 257 | let s = open_at(&root.0, Arc::new(SealKey::from_bytes(SYNTHETIC_KEY))); |
| 258 | let kept = kvc_files(&root.0); |
| 259 | assert_eq!(kept.len(), 2); |
| 260 | let payload = refused_store(&s, &fields, 3); |
| 261 | assert_eq!(kvc_files(&root.0), kept); |
| 262 | assert!(!s.object_path(&payload.0).exists()); |
| 263 | assert_pattern_absent(&root.0, &payload.1[..64]); |
| 264 | assert_eq!( |
| 265 | testing_read_usage_count(&usage, &reader).unwrap(), |
| 266 | RECOMMENDED_SEALS_PER_KEY |
| 267 | ); |
| 268 | let _ = first; |
| 269 | drop(s); |
| 270 | } |
| 271 | |
| 272 | fn store_usage(tag: &str) -> (Tmp, std::path::PathBuf) { |
| 273 | let root = Tmp::new(tag); |
| 274 | let usage = root.0.join(USAGE_DIR_NAME); |
| 275 | std::fs::create_dir(&usage).unwrap(); |
| 276 | (root, usage) |
| 277 | } |
| 278 | |
| 279 | fn plant_kvc(root: &std::path::Path, shard: &str, name: &str, sealed: bool) { |
| 280 | let dir = root.join("objects").join(shard); |
| 281 | std::fs::create_dir_all(&dir).unwrap(); |
| 282 | let mut region = vec![0u8; HEADER_REGION_LEN]; |
| 283 | if sealed { |
| 284 | let at = crate::record::SEAL_SLOT_OFFSET; |
| 285 | region[at..at + 3].copy_from_slice(&seal::SEAL_MAGIC); |
| 286 | region[at + 3] = seal::SEALED_FORMAT_VERSION; |
| 287 | } |
| 288 | std::fs::write(dir.join(name), region).unwrap(); |
| 289 | } |
| 290 | |
| 291 | #[test] |
| 292 | fn seal_budget_missing_count_beside_a_sealed_object_does_not_seal() { |
| 293 | let (root, usage) = store_usage("occupied"); |
| 294 | plant_kvc( |
| 295 | &root.0, |
| 296 | "ab", |
| 297 | "ab00000000000000000000000000000000000000000000000000000000000000.kvc", |
| 298 | true, |
| 299 | ); |
| 300 | let key = SealKey::from_bytes([0x31; 32]); |
| 301 | let mut file = blank(); |
| 302 | let before = file.clone(); |
| 303 | assert_eq!( |
| 304 | key.seal_file(&usage, &mut file), |
| 305 | Err(SealError::UsageStore), |
| 306 | "a missing counter next to a sealed object must not restart at zero" |
| 307 | ); |
| 308 | assert_eq!(file, before); |
| 309 | assert!(!seal::is_sealed_region(&file[..HEADER_REGION_LEN])); |
| 310 | assert!(!testing_usage_path(&usage, &key).exists()); |
| 311 | } |
| 312 | |
| 313 | #[test] |
| 314 | fn seal_budget_missing_count_on_an_empty_store_starts_at_zero() { |
| 315 | let (root, usage) = store_usage("empty-store"); |
| 316 | std::fs::create_dir_all(root.0.join("objects/00")).unwrap(); |
| 317 | // An old plaintext object is not a seal, so it does not block the zero start. |
| 318 | plant_kvc( |
| 319 | &root.0, |
| 320 | "cd", |
| 321 | "cd00000000000000000000000000000000000000000000000000000000000000.kvc", |
| 322 | false, |
| 323 | ); |
| 324 | let key = SealKey::from_bytes([0x32; 32]); |
| 325 | assert_eq!(testing_read_usage_count(&usage, &key).unwrap(), 0); |
| 326 | let mut file = blank(); |
| 327 | key.seal_file(&usage, &mut file).unwrap(); |
| 328 | assert!(seal::is_sealed_region(&file[..HEADER_REGION_LEN])); |
| 329 | assert_eq!(testing_read_usage_count(&usage, &key).unwrap(), 1); |
| 330 | } |
| 331 | |
| 332 | #[test] |
| 333 | fn seal_budget_leftover_part_is_replaced_when_we_own_it_and_refused_when_linked() { |
| 334 | let (root, usage) = store_usage("part"); |
| 335 | let key = SealKey::from_bytes([0x33; 32]); |
| 336 | testing_install_usage_count(&usage, &key, 4).unwrap(); |
| 337 | let count_path = testing_usage_path(&usage, &key); |
| 338 | let part = count_path.with_extension("part"); |
| 339 | std::fs::write(&part, b"not-a-count").unwrap(); |
| 340 | let mut file = blank(); |
| 341 | key.seal_file(&usage, &mut file).unwrap(); |
| 342 | assert!( |
| 343 | !part.exists(), |
| 344 | "an owned leftover part is renamed onto the count file" |
| 345 | ); |
| 346 | assert_eq!(testing_read_usage_count(&usage, &key).unwrap(), 5); |
| 347 | assert!(!std::fs::read(&count_path) |
| 348 | .unwrap() |
| 349 | .windows(11) |
| 350 | .any(|w| w == b"not-a-count")); |
| 351 | assert!(seal::is_sealed_region(&file[..HEADER_REGION_LEN])); |
| 352 | drop(root); |
| 353 | |
| 354 | let (_root, usage) = store_usage("part-link"); |
| 355 | let key = SealKey::from_bytes([0x34; 32]); |
| 356 | testing_install_usage_count(&usage, &key, 4).unwrap(); |
| 357 | let count_path = testing_usage_path(&usage, &key); |
| 358 | let part = count_path.with_extension("part"); |
| 359 | std::os::unix::fs::symlink(&count_path, &part).unwrap(); |
| 360 | let mut file = blank(); |
| 361 | let before = file.clone(); |
| 362 | assert_eq!(key.seal_file(&usage, &mut file), Err(SealError::UsageStore)); |
| 363 | assert_eq!(file, before); |
| 364 | assert!( |
| 365 | std::fs::symlink_metadata(&part) |
| 366 | .unwrap() |
| 367 | .file_type() |
| 368 | .is_symlink(), |
| 369 | "a linked part is left in place" |
| 370 | ); |
| 371 | assert_eq!(testing_read_usage_count(&usage, &key).unwrap(), 4); |
| 372 | } |
| 373 | |
| 374 | fn fp_bytes(key: &[u8; 32]) -> [u8; 32] { |
| 375 | Sha256::digest(key).into() |
| 376 | } |
| 377 | |
| 378 | #[test] |
| 379 | fn seal_budget_rotation_starts_the_new_key_at_one_and_keeps_the_old_object() { |
| 380 | let (root, usage) = store_usage("rotate"); |
| 381 | let old = SealKey::from_bytes([0x41; 32]); |
| 382 | let new_key = SealKey::from_bytes([0x42; 32]); |
| 383 | let mut file = blank(); |
| 384 | old.seal_file(&usage, &mut file).unwrap(); |
| 385 | assert_eq!(testing_read_usage_count(&usage, &old).unwrap(), 1); |
| 386 | assert_eq!( |
| 387 | testing_read_key_fingerprints(&usage).unwrap(), |
| 388 | Some(vec![fp_bytes(&[0x41; 32])]) |
| 389 | ); |
| 390 | let sealed = file.clone(); |
| 391 | let obj = root |
| 392 | .0 |
| 393 | .join("objects/ab/ab00000000000000000000000000000000000000000000000000000000000000.kvc"); |
| 394 | std::fs::create_dir_all(obj.parent().unwrap()).unwrap(); |
| 395 | std::fs::write(&obj, &sealed).unwrap(); |
| 396 | |
| 397 | let mut next = blank(); |
| 398 | new_key.seal_file(&usage, &mut next).unwrap(); |
| 399 | assert!(seal::is_sealed_region(&next[..HEADER_REGION_LEN])); |
| 400 | assert_eq!(testing_read_usage_count(&usage, &new_key).unwrap(), 1); |
| 401 | assert_eq!(testing_read_usage_count(&usage, &old).unwrap(), 1); |
| 402 | assert_eq!( |
| 403 | testing_read_key_fingerprints(&usage).unwrap(), |
| 404 | Some(vec![fp_bytes(&[0x41; 32]), fp_bytes(&[0x42; 32])]) |
| 405 | ); |
| 406 | assert_eq!( |
| 407 | std::fs::read(&obj).unwrap(), |
| 408 | sealed, |
| 409 | "an old-key object stays until a read drops it" |
| 410 | ); |
| 411 | } |
| 412 | |
| 413 | #[test] |
| 414 | fn seal_budget_same_key_with_the_counter_deleted_fails() { |
| 415 | let (_root, usage) = store_usage("deleted-counter"); |
| 416 | let key = SealKey::from_bytes([0x43; 32]); |
| 417 | let mut file = blank(); |
| 418 | key.seal_file(&usage, &mut file).unwrap(); |
| 419 | let path = testing_usage_path(&usage, &key); |
| 420 | std::fs::remove_file(&path).unwrap(); |
| 421 | assert_eq!( |
| 422 | testing_read_key_fingerprints(&usage).unwrap(), |
| 423 | Some(vec![fp_bytes(&[0x43; 32])]) |
| 424 | ); |
| 425 | let mut again = blank(); |
| 426 | let before = again.clone(); |
| 427 | assert_eq!( |
| 428 | key.seal_file(&usage, &mut again), |
| 429 | Err(SealError::UsageStore) |
| 430 | ); |
| 431 | assert_eq!(again, before); |
| 432 | assert!(!path.exists(), "the refusal does not recreate the counter"); |
| 433 | assert_eq!( |
| 434 | testing_read_key_fingerprints(&usage).unwrap(), |
| 435 | Some(vec![fp_bytes(&[0x43; 32])]) |
| 436 | ); |
| 437 | } |
| 438 | |
| 439 | #[test] |
| 440 | fn seal_budget_legacy_store_refuses_then_rotation_seals() { |
| 441 | let (root, usage) = store_usage("legacy"); |
| 442 | let name = "ab00000000000000000000000000000000000000000000000000000000000000.kvc"; |
| 443 | plant_kvc(&root.0, "ab", name, true); |
| 444 | let obj = root.0.join("objects/ab").join(name); |
| 445 | let old_bytes = std::fs::read(&obj).unwrap(); |
| 446 | assert_eq!(testing_read_key_fingerprints(&usage).unwrap(), None); |
| 447 | |
| 448 | let old = SealKey::from_bytes([0x44; 32]); |
| 449 | let mut file = blank(); |
| 450 | let before = file.clone(); |
| 451 | assert_eq!(old.seal_file(&usage, &mut file), Err(SealError::UsageStore)); |
| 452 | assert_eq!(file, before); |
| 453 | assert!(!testing_usage_path(&usage, &old).exists()); |
| 454 | assert_eq!( |
| 455 | testing_read_key_fingerprints(&usage).unwrap(), |
| 456 | Some(vec![fp_bytes(&[0x44; 32])]), |
| 457 | "the refusal records the key and does not write a counter" |
| 458 | ); |
| 459 | assert_eq!(old.seal_file(&usage, &mut file), Err(SealError::UsageStore)); |
| 460 | assert!(!testing_usage_path(&usage, &old).exists()); |
| 461 | |
| 462 | let new_key = SealKey::from_bytes([0x45; 32]); |
| 463 | let mut next = blank(); |
| 464 | new_key.seal_file(&usage, &mut next).unwrap(); |
| 465 | assert!(seal::is_sealed_region(&next[..HEADER_REGION_LEN])); |
| 466 | assert_eq!(testing_read_usage_count(&usage, &new_key).unwrap(), 1); |
| 467 | assert!(!testing_usage_path(&usage, &old).exists()); |
| 468 | assert_eq!( |
| 469 | testing_read_key_fingerprints(&usage).unwrap(), |
| 470 | Some(vec![fp_bytes(&[0x44; 32]), fp_bytes(&[0x45; 32])]) |
| 471 | ); |
| 472 | assert_eq!(std::fs::read(&obj).unwrap(), old_bytes); |
| 473 | } |
| 474 | |
| 475 | #[test] |
| 476 | #[ignore = "needs passwordless sudo to chown a leftover part file to nobody"] |
| 477 | fn seal_budget_part_owned_by_another_user_is_left_in_place() { |
| 478 | assert!(passwordless_sudo(), "passwordless sudo is not available"); |
| 479 | let (_root, usage) = store_usage("part-other-uid"); |
| 480 | let key = SealKey::from_bytes([0x46; 32]); |
| 481 | testing_install_usage_count(&usage, &key, 4).unwrap(); |
| 482 | let count_path = testing_usage_path(&usage, &key); |
| 483 | let part = count_path.with_extension("part"); |
| 484 | std::fs::write(&part, b"other-user").unwrap(); |
| 485 | let chown = std::process::Command::new("sudo") |
| 486 | .args(["-n", "chown", "nobody:nogroup"]) |
| 487 | .arg(&part) |
| 488 | .status() |
| 489 | .expect("sudo chown"); |
| 490 | assert!(chown.success(), "chown nobody of the leftover part"); |
| 491 | let mut file = blank(); |
| 492 | let before = file.clone(); |
| 493 | assert_eq!(key.seal_file(&usage, &mut file), Err(SealError::UsageStore)); |
| 494 | assert_eq!(file, before); |
| 495 | assert_eq!(std::fs::read(&part).unwrap(), b"other-user"); |
| 496 | assert_eq!(testing_read_usage_count(&usage, &key).unwrap(), 4); |
| 497 | } |
| 498 | |
| 499 | fn passwordless_sudo() -> bool { |
| 500 | match std::process::Command::new("sudo") |
| 501 | .args(["-n", "true"]) |
| 502 | .status() |
| 503 | { |
| 504 | Ok(status) => status.success(), |
| 505 | Err(_) => false, |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | #[test] |
| 510 | fn seal_budget_returning_to_a_listed_key_with_a_deleted_counter_refuses() { |
| 511 | let (_root, usage) = store_usage("return"); |
| 512 | let a = SealKey::from_bytes([0x61; 32]); |
| 513 | let b = SealKey::from_bytes([0x62; 32]); |
| 514 | let mut file = blank(); |
| 515 | a.seal_file(&usage, &mut file).unwrap(); |
| 516 | b.seal_file(&usage, &mut file).unwrap(); |
| 517 | assert_eq!(testing_read_usage_count(&usage, &a).unwrap(), 1); |
| 518 | assert_eq!(testing_read_usage_count(&usage, &b).unwrap(), 1); |
| 519 | let a_path = testing_usage_path(&usage, &a); |
| 520 | std::fs::remove_file(&a_path).unwrap(); |
| 521 | let mut again = blank(); |
| 522 | let before = again.clone(); |
| 523 | assert_eq!(a.seal_file(&usage, &mut again), Err(SealError::UsageStore)); |
| 524 | assert_eq!(again, before); |
| 525 | assert!(!a_path.exists(), "a retired key does not start over"); |
| 526 | assert_eq!(testing_read_usage_count(&usage, &b).unwrap(), 1); |
| 527 | assert_eq!( |
| 528 | testing_read_key_fingerprints(&usage).unwrap(), |
| 529 | Some(vec![fp_bytes(&[0x61; 32]), fp_bytes(&[0x62; 32])]) |
| 530 | ); |
| 531 | } |
| 532 | |
| 533 | #[test] |
| 534 | fn seal_budget_corrupt_fingerprint_list_does_not_consume_a_slot() { |
| 535 | let (_root, usage) = store_usage("bad-list"); |
| 536 | let key = SealKey::from_bytes([0x63; 32]); |
| 537 | let mut file = blank(); |
| 538 | key.seal_file(&usage, &mut file).unwrap(); |
| 539 | assert_eq!(testing_read_usage_count(&usage, &key).unwrap(), 1); |
| 540 | std::fs::write(usage.join(seal::KEY_FINGERPRINTS_NAME), b"not-a-list").unwrap(); |
| 541 | let mut again = blank(); |
| 542 | let before = again.clone(); |
| 543 | assert_eq!( |
| 544 | key.seal_file(&usage, &mut again), |
| 545 | Err(SealError::UsageStore) |
| 546 | ); |
| 547 | assert_eq!(again, before); |
| 548 | assert_eq!( |
| 549 | testing_read_usage_count(&usage, &key).unwrap(), |
| 550 | 1, |
| 551 | "a corrupt list is refused before the count is incremented" |
| 552 | ); |
| 553 | } |
| 554 | |
| 555 | #[test] |
| 556 | fn seal_budget_leftover_list_part_is_replaced_when_owned_and_refused_when_linked() { |
| 557 | let (root, usage) = store_usage("list-part"); |
| 558 | let a = SealKey::from_bytes([0x64; 32]); |
| 559 | let b = SealKey::from_bytes([0x65; 32]); |
| 560 | let mut file = blank(); |
| 561 | a.seal_file(&usage, &mut file).unwrap(); |
| 562 | let part = usage.join(format!("{}.part", seal::KEY_FINGERPRINTS_NAME)); |
| 563 | std::fs::write(&part, b"stale-list-part").unwrap(); |
| 564 | b.seal_file(&usage, &mut file).unwrap(); |
| 565 | assert!( |
| 566 | !part.exists(), |
| 567 | "an owned leftover list part is renamed away" |
| 568 | ); |
| 569 | assert_eq!(testing_read_usage_count(&usage, &b).unwrap(), 1); |
| 570 | let stored = std::fs::read(usage.join(seal::KEY_FINGERPRINTS_NAME)).unwrap(); |
| 571 | assert!(!stored.windows(15).any(|w| w == b"stale-list-part")); |
| 572 | assert_eq!( |
| 573 | testing_read_key_fingerprints(&usage).unwrap(), |
| 574 | Some(vec![fp_bytes(&[0x64; 32]), fp_bytes(&[0x65; 32])]) |
| 575 | ); |
| 576 | drop(root); |
| 577 | |
| 578 | let (_root, usage) = store_usage("list-part-link"); |
| 579 | let a = SealKey::from_bytes([0x66; 32]); |
| 580 | let b = SealKey::from_bytes([0x67; 32]); |
| 581 | let mut file = blank(); |
| 582 | a.seal_file(&usage, &mut file).unwrap(); |
| 583 | let list = usage.join(seal::KEY_FINGERPRINTS_NAME); |
| 584 | let part = usage.join(format!("{}.part", seal::KEY_FINGERPRINTS_NAME)); |
| 585 | std::os::unix::fs::symlink(&list, &part).unwrap(); |
| 586 | let mut next = blank(); |
| 587 | let before = next.clone(); |
| 588 | assert_eq!(b.seal_file(&usage, &mut next), Err(SealError::UsageStore)); |
| 589 | assert_eq!(next, before); |
| 590 | assert!( |
| 591 | std::fs::symlink_metadata(&part) |
| 592 | .unwrap() |
| 593 | .file_type() |
| 594 | .is_symlink(), |
| 595 | "a linked list part is left in place" |
| 596 | ); |
| 597 | assert!(!testing_usage_path(&usage, &b).exists()); |
| 598 | assert_eq!(testing_read_usage_count(&usage, &a).unwrap(), 1); |
| 599 | } |
| 600 | |
| 601 | #[test] |
| 602 | fn seal_budget_deleted_list_keeps_remaining_counters_and_refuses_the_deleted_one() { |
| 603 | let (_root, usage) = store_usage("deleted-list"); |
| 604 | let a = SealKey::from_bytes([0x71; 32]); |
| 605 | let b = SealKey::from_bytes([0x72; 32]); |
| 606 | let mut file = blank(); |
| 607 | a.seal_file(&usage, &mut file).unwrap(); |
| 608 | b.seal_file(&usage, &mut file).unwrap(); |
| 609 | std::fs::remove_file(usage.join(seal::KEY_FINGERPRINTS_NAME)).unwrap(); |
| 610 | let a_path = testing_usage_path(&usage, &a); |
| 611 | std::fs::remove_file(&a_path).unwrap(); |
| 612 | |
| 613 | let mut again = blank(); |
| 614 | let before = again.clone(); |
| 615 | assert_eq!(a.seal_file(&usage, &mut again), Err(SealError::UsageStore)); |
| 616 | assert_eq!(again, before); |
| 617 | assert!( |
| 618 | !a_path.exists(), |
| 619 | "A does not start over after the list is deleted" |
| 620 | ); |
| 621 | assert_eq!(testing_read_usage_count(&usage, &b).unwrap(), 1); |
| 622 | b.seal_file(&usage, &mut again).unwrap(); |
| 623 | assert_eq!(testing_read_usage_count(&usage, &b).unwrap(), 2); |
| 624 | assert!(!a_path.exists()); |
| 625 | assert_eq!( |
| 626 | testing_read_key_fingerprints(&usage).unwrap(), |
| 627 | Some(vec![fp_bytes(&[0x72; 32]), fp_bytes(&[0x71; 32])]), |
| 628 | "the rebuilt list keeps B's counter file and records A" |
| 629 | ); |
| 630 | } |
| 631 | |
| 632 | #[test] |
| 633 | fn seal_budget_truncated_list_does_not_consume_a_slot() { |
| 634 | let (_root, usage) = store_usage("trunc-list"); |
| 635 | let key = SealKey::from_bytes([0x73; 32]); |
| 636 | let mut file = blank(); |
| 637 | key.seal_file(&usage, &mut file).unwrap(); |
| 638 | let list = usage.join(seal::KEY_FINGERPRINTS_NAME); |
| 639 | let mut bytes = std::fs::read(&list).unwrap(); |
| 640 | bytes.truncate(20); |
| 641 | std::fs::write(&list, &bytes).unwrap(); |
| 642 | let mut again = blank(); |
| 643 | let before = again.clone(); |
| 644 | assert_eq!( |
| 645 | key.seal_file(&usage, &mut again), |
| 646 | Err(SealError::UsageStore) |
| 647 | ); |
| 648 | assert_eq!(again, before); |
| 649 | assert_eq!(testing_read_usage_count(&usage, &key).unwrap(), 1); |
| 650 | assert_eq!(std::fs::read(&list).unwrap(), bytes); |
| 651 | } |
| 652 | |
| 653 | #[test] |
| 654 | fn seal_budget_duplicate_list_entries_do_not_consume_a_slot() { |
| 655 | let (_root, usage) = store_usage("dup-list"); |
| 656 | let key = SealKey::from_bytes([0x74; 32]); |
| 657 | let mut file = blank(); |
| 658 | key.seal_file(&usage, &mut file).unwrap(); |
| 659 | let fp = fp_bytes(&[0x74; 32]); |
| 660 | let list = usage.join(seal::KEY_FINGERPRINTS_NAME); |
| 661 | let bytes = list_bytes(&[fp, fp]); |
| 662 | std::fs::write(&list, &bytes).unwrap(); |
| 663 | let mut again = blank(); |
| 664 | let before = again.clone(); |
| 665 | assert_eq!( |
| 666 | key.seal_file(&usage, &mut again), |
| 667 | Err(SealError::UsageStore) |
| 668 | ); |
| 669 | assert_eq!(again, before); |
| 670 | assert_eq!(testing_read_usage_count(&usage, &key).unwrap(), 1); |
| 671 | assert_eq!(std::fs::read(&list).unwrap(), bytes); |
| 672 | } |
| 673 | |
| 674 | #[test] |
| 675 | fn seal_budget_oversized_list_does_not_consume_a_slot() { |
| 676 | let (_root, usage) = store_usage("oversize-list"); |
| 677 | let key = SealKey::from_bytes([0x75; 32]); |
| 678 | let mut file = blank(); |
| 679 | key.seal_file(&usage, &mut file).unwrap(); |
| 680 | let list = usage.join(seal::KEY_FINGERPRINTS_NAME); |
| 681 | let fps: Vec<[u8; 32]> = (0..257u16).map(filler_fp).collect(); |
| 682 | let bytes = list_bytes(&fps); |
| 683 | std::fs::write(&list, &bytes).unwrap(); |
| 684 | let mut again = blank(); |
| 685 | let before = again.clone(); |
| 686 | assert_eq!( |
| 687 | key.seal_file(&usage, &mut again), |
| 688 | Err(SealError::UsageStore) |
| 689 | ); |
| 690 | assert_eq!(again, before); |
| 691 | assert_eq!(testing_read_usage_count(&usage, &key).unwrap(), 1); |
| 692 | assert_eq!(std::fs::read(&list).unwrap(), bytes); |
| 693 | } |
| 694 | |
| 695 | #[test] |
| 696 | fn seal_budget_fingerprint_list_stops_at_256_keys() { |
| 697 | let (_root, usage) = store_usage("cap"); |
| 698 | let key = SealKey::from_bytes([0x76; 32]); |
| 699 | let extra = SealKey::from_bytes([0x77; 32]); |
| 700 | let key_fp = fp_bytes(&[0x76; 32]); |
| 701 | let extra_fp = fp_bytes(&[0x77; 32]); |
| 702 | let mut fps = Vec::with_capacity(256); |
| 703 | for i in 0..255u16 { |
| 704 | let fp = filler_fp(i); |
| 705 | assert_ne!(fp, key_fp); |
| 706 | assert_ne!(fp, extra_fp); |
| 707 | fps.push(fp); |
| 708 | } |
| 709 | fps.push(key_fp); |
| 710 | let list = usage.join(seal::KEY_FINGERPRINTS_NAME); |
| 711 | let full = list_bytes(&fps); |
| 712 | std::fs::write(&list, &full).unwrap(); |
| 713 | testing_install_usage_count(&usage, &key, 3).unwrap(); |
| 714 | let mut file = blank(); |
| 715 | key.seal_file(&usage, &mut file).unwrap(); |
| 716 | assert_eq!(testing_read_usage_count(&usage, &key).unwrap(), 4); |
| 717 | assert_eq!(std::fs::read(&list).unwrap(), full); |
| 718 | |
| 719 | let mut next = blank(); |
| 720 | let before = next.clone(); |
| 721 | assert_eq!( |
| 722 | extra.seal_file(&usage, &mut next), |
| 723 | Err(SealError::UsageStore) |
| 724 | ); |
| 725 | assert_eq!(next, before); |
| 726 | assert!( |
| 727 | !testing_usage_path(&usage, &extra).exists(), |
| 728 | "the 257th key does not get a counter" |
| 729 | ); |
| 730 | assert_eq!(std::fs::read(&list).unwrap(), full); |
| 731 | assert_eq!(testing_read_usage_count(&usage, &key).unwrap(), 4); |
| 732 | } |
| 733 | |
| 734 | #[test] |
| 735 | fn seal_budget_legacy_key_fingerprint_file_is_ignored() { |
| 736 | let (_root, usage) = store_usage("old-marker"); |
| 737 | let marker = usage.join("key-fingerprint"); |
| 738 | std::fs::write(&marker, b"legacy-marker").unwrap(); |
| 739 | let key = SealKey::from_bytes([0x78; 32]); |
| 740 | let mut file = blank(); |
| 741 | key.seal_file(&usage, &mut file).unwrap(); |
| 742 | assert!(seal::is_sealed_region(&file[..HEADER_REGION_LEN])); |
| 743 | assert_eq!(testing_read_usage_count(&usage, &key).unwrap(), 1); |
| 744 | assert_eq!(std::fs::read(&marker).unwrap(), b"legacy-marker"); |
| 745 | assert_eq!( |
| 746 | testing_read_key_fingerprints(&usage).unwrap(), |
| 747 | Some(vec![fp_bytes(&[0x78; 32])]) |
| 748 | ); |
| 749 | } |
| 750 | |
| 751 | #[test] |
| 752 | #[ignore = "needs passwordless sudo to chown seal-usage/key-fingerprints to nobody"] |
| 753 | fn seal_budget_list_owned_by_another_user_is_left_in_place() { |
| 754 | assert!(passwordless_sudo(), "passwordless sudo is not available"); |
| 755 | let (_root, usage) = store_usage("list-other-uid"); |
| 756 | let key = SealKey::from_bytes([0x79; 32]); |
| 757 | let mut file = blank(); |
| 758 | key.seal_file(&usage, &mut file).unwrap(); |
| 759 | let list = usage.join(seal::KEY_FINGERPRINTS_NAME); |
| 760 | let listed = std::fs::read(&list).unwrap(); |
| 761 | let owner = std::fs::metadata(&list).unwrap().uid(); |
| 762 | chown_nobody(&list); |
| 763 | assert_ne!(std::fs::metadata(&list).unwrap().uid(), owner); |
| 764 | let mut again = blank(); |
| 765 | let before = again.clone(); |
| 766 | assert_eq!( |
| 767 | key.seal_file(&usage, &mut again), |
| 768 | Err(SealError::UsageStore) |
| 769 | ); |
| 770 | assert_eq!(again, before); |
| 771 | assert_eq!(sudo_read(&list), listed); |
| 772 | assert_eq!(testing_read_usage_count(&usage, &key).unwrap(), 1); |
| 773 | |
| 774 | let (_root, usage) = store_usage("list-part-other-uid"); |
| 775 | let a = SealKey::from_bytes([0x7a; 32]); |
| 776 | let b = SealKey::from_bytes([0x7b; 32]); |
| 777 | let mut file = blank(); |
| 778 | a.seal_file(&usage, &mut file).unwrap(); |
| 779 | let part = usage.join(format!("{}.part", seal::KEY_FINGERPRINTS_NAME)); |
| 780 | std::fs::write(&part, b"foreign-list-part").unwrap(); |
| 781 | chown_nobody(&part); |
| 782 | let mut next = blank(); |
| 783 | let before = next.clone(); |
| 784 | assert_eq!(b.seal_file(&usage, &mut next), Err(SealError::UsageStore)); |
| 785 | assert_eq!(next, before); |
| 786 | assert_eq!(std::fs::read(&part).unwrap(), b"foreign-list-part"); |
| 787 | assert!(!testing_usage_path(&usage, &b).exists()); |
| 788 | assert_eq!(testing_read_usage_count(&usage, &a).unwrap(), 1); |
| 789 | } |
| 790 | |
| 791 | fn chown_nobody(path: &std::path::Path) { |
| 792 | let chown = std::process::Command::new("sudo") |
| 793 | .args(["-n", "chown", "nobody:nogroup"]) |
| 794 | .arg(path) |
| 795 | .status() |
| 796 | .expect("sudo chown"); |
| 797 | assert!(chown.success(), "chown nobody"); |
| 798 | } |
| 799 | |
| 800 | fn sudo_read(path: &std::path::Path) -> Vec<u8> { |
| 801 | let out = std::process::Command::new("sudo") |
| 802 | .args(["-n", "cat"]) |
| 803 | .arg(path) |
| 804 | .output() |
| 805 | .expect("sudo cat"); |
| 806 | assert!(out.status.success(), "sudo cat"); |
| 807 | out.stdout |
| 808 | } |
| 809 | |
| 810 | fn list_bytes(fps: &[[u8; 32]]) -> Vec<u8> { |
| 811 | let mut buf = Vec::with_capacity(12 + fps.len() * 32); |
| 812 | buf.extend_from_slice(b"AKSL"); |
| 813 | buf.push(1); |
| 814 | buf.extend_from_slice(&[0, 0, 0]); |
| 815 | buf.extend_from_slice(&(fps.len() as u32).to_le_bytes()); |
| 816 | for fp in fps { |
| 817 | buf.extend_from_slice(fp); |
| 818 | } |
| 819 | buf |
| 820 | } |
| 821 | |
| 822 | fn filler_fp(i: u16) -> [u8; 32] { |
| 823 | let mut fp = [0xA5u8; 32]; |
| 824 | fp[0] = 0xF0; |
| 825 | fp[30] = (i >> 8) as u8; |
| 826 | fp[31] = (i & 0xff) as u8; |
| 827 | fp |
| 828 | } |
| 829 | |
| 830 | fn refused_store( |
| 831 | s: &HostFsStore, |
| 832 | fields: &NamespaceFields<'_>, |
| 833 | n: u32, |
| 834 | ) -> (crate::ChunkKey, Vec<u8>) { |
| 835 | let ns = fields.namespace_id().unwrap(); |
| 836 | let toks: Vec<u32> = (0..4).map(|i| n * 100 + i).collect(); |
| 837 | let key = chunk_chain(&ns, 8, &toks).unwrap()[0]; |
| 838 | let header = fields.chunk_header(&ns, key, None, 0, 4, 1).unwrap(); |
| 839 | let payload = vec![0xABu8; BS]; |
| 840 | let mut buf = payload.clone().into_boxed_slice(); |
| 841 | // SAFETY: `buf` outlives `run_until_idle` and `poll` inside `terminal_for`. |
| 842 | let src = |
| 843 | unsafe { PinnedSource::new(buf.as_mut_ptr(), BS, vec![0], 4, HolderGuard::detached()) }; |
| 844 | let job = s |
| 845 | .submit_store(header, src, Incarnation::seq(n as u64)) |
| 846 | .unwrap(); |
| 847 | s.run_until_idle(); |
| 848 | assert_eq!( |
| 849 | terminal_for(s, job), |
| 850 | Terminal::Failed(PlatformError::HardwareFault("seal budget exhausted".into())) |
| 851 | ); |
| 852 | drop(buf); |
| 853 | (key, payload) |
| 854 | } |