|
1 | 1 | //! Non-transmitting context acquisition for the isolated device-lease fixture. |
2 | | -use std::path::{Path, PathBuf}; |
| 2 | +use std::{ |
| 3 | + io::Read as _, |
| 4 | + os::fd::{AsFd as _, AsRawFd as _}, |
| 5 | + path::{Path, PathBuf}, |
| 6 | +}; |
3 | 7 |
|
4 | 8 | use anyhow::{Context, Result, ensure}; |
5 | 9 | use aya::{ |
6 | 10 | Pod, |
7 | | - maps::{Array, Map, MapData, MapError, MapType, xdp::DevMap}, |
| 11 | + maps::{Array, Map, MapData, MapError, MapType, ProgramArray, xdp::DevMap}, |
8 | 12 | programs::{SchedClassifier, TestRun, TestRunOptions}, |
9 | 13 | }; |
10 | 14 |
|
@@ -106,17 +110,7 @@ fn seed(directory: &Path, ifindex: u32) -> Result<()> { |
106 | 110 | result |
107 | 111 | } |
108 | 112 |
|
109 | | -fn main() -> Result<()> { |
110 | | - ensure!( |
111 | | - std::env::var("UNF_DEVICE_OBSERVATION_ISOLATED_CONTAINER").as_deref() == Ok("yes"), |
112 | | - "isolated fixture opt-in required" |
113 | | - ); |
114 | | - let arguments: Vec<_> = std::env::args_os().skip(1).collect(); |
115 | | - ensure!( |
116 | | - arguments.len() == 2, |
117 | | - "expected private bpffs directory and device index or bindings" |
118 | | - ); |
119 | | - let directory = PathBuf::from(&arguments[0]); |
| 113 | +fn private_directory(directory: &Path) -> Result<()> { |
120 | 114 | ensure!( |
121 | 115 | directory.file_name().is_some_and(|name| name == "bpffs") |
122 | 116 | && directory |
@@ -127,6 +121,160 @@ fn main() -> Result<()> { |
127 | 121 | .starts_with("unf-device-observation.")), |
128 | 122 | "unexpected private pin directory" |
129 | 123 | ); |
| 124 | + Ok(()) |
| 125 | +} |
| 126 | + |
| 127 | +fn frozen_fdinfo(value: &str) -> bool { |
| 128 | + let rows: Vec<_> = value |
| 129 | + .lines() |
| 130 | + .filter_map(|line| line.strip_prefix("frozen:")) |
| 131 | + .collect(); |
| 132 | + rows.len() == 1 && rows[0].trim() == "1" |
| 133 | +} |
| 134 | + |
| 135 | +fn exact_bank_maps(mut actual: Vec<u32>, mut expected: Vec<u32>) -> bool { |
| 136 | + actual.sort_unstable(); |
| 137 | + expected.sort_unstable(); |
| 138 | + expected.len() == 7 |
| 139 | + && expected[0] != 0 |
| 140 | + && expected.windows(2).all(|pair| pair[0] != pair[1]) |
| 141 | + && actual == expected |
| 142 | +} |
| 143 | + |
| 144 | +fn publish(directory: &Path, bank: &Path) -> Result<()> { |
| 145 | + private_directory(bank)?; |
| 146 | + let mut expected_maps = Vec::with_capacity(7); |
| 147 | + let mut held_maps = Vec::with_capacity(6); |
| 148 | + let mut held_tag: Option<Array<MapData, u32>> = None; |
| 149 | + for (name, kind, entries, bytes) in [ |
| 150 | + ("P9LEASECFG", MapType::Array, 1, 160), |
| 151 | + ("P9LEASEPTR", MapType::Array, 1, 8), |
| 152 | + ("P9LEASEDEV", MapType::DevMap, 4, 8), |
| 153 | + ("P9LEASEOWN", MapType::Array, 4, 104), |
| 154 | + ("P9LEASETAG", MapType::Array, 1, 4), |
| 155 | + ] { |
| 156 | + let map = MapData::from_pin(bank.join("maps").join(name))?; |
| 157 | + let info = map.info()?; |
| 158 | + expected_maps.push(info.id()); |
| 159 | + ensure!( |
| 160 | + info.map_type()? == kind |
| 161 | + && info.max_entries() == entries |
| 162 | + && info.key_size() == 4 |
| 163 | + && info.value_size() == bytes, |
| 164 | + "unexpected bank map shape" |
| 165 | + ); |
| 166 | + let mut text = String::new(); |
| 167 | + std::fs::File::open(format!( |
| 168 | + "/proc/self/fdinfo/{}", |
| 169 | + map.fd().as_fd().as_raw_fd() |
| 170 | + ))? |
| 171 | + .take(4097) |
| 172 | + .read_to_string(&mut text)?; |
| 173 | + ensure!( |
| 174 | + text.len() <= 4096 && frozen_fdinfo(&text), |
| 175 | + "bank is not sealed" |
| 176 | + ); |
| 177 | + if name == "P9LEASETAG" { |
| 178 | + held_tag = Some(Array::try_from(Map::Array(map))?); |
| 179 | + } else { |
| 180 | + held_maps.push(map); |
| 181 | + } |
| 182 | + } |
| 183 | + let tag = held_tag.context("missing sealed tag descriptor")?; |
| 184 | + ensure!((1..=2).contains(&tag.get(&0, 0)?), "invalid bank tag"); |
| 185 | + for (name, kind, entries, bytes) in [ |
| 186 | + ("P9LEASECON", MapType::PerCpuArray, 1, 24), |
| 187 | + ("P9LEASESEQ", MapType::Array, 65536, 8), |
| 188 | + ] { |
| 189 | + let map = MapData::from_pin(bank.join("maps").join(name))?; |
| 190 | + let info = map.info()?; |
| 191 | + ensure!( |
| 192 | + info.map_type()? == kind |
| 193 | + && info.max_entries() == entries |
| 194 | + && info.key_size() == 4 |
| 195 | + && info.value_size() == bytes, |
| 196 | + "unexpected bank observer map" |
| 197 | + ); |
| 198 | + expected_maps.push(info.id()); |
| 199 | + held_maps.push(map); |
| 200 | + } |
| 201 | + let data = MapData::from_pin(directory.join("maps/P9LEASENEXT"))?; |
| 202 | + let info = data.info()?; |
| 203 | + ensure!( |
| 204 | + info.map_type()? == MapType::ProgramArray && info.max_entries() == 1, |
| 205 | + "unexpected dispatcher map" |
| 206 | + ); |
| 207 | + let mut dispatch = ProgramArray::try_from(Map::ProgramArray(data))?; |
| 208 | + let program = SchedClassifier::from_pin(bank.join("concurrent"))?; |
| 209 | + let program_info = program.info()?; |
| 210 | + let actual_maps = program_info |
| 211 | + .map_ids()? |
| 212 | + .context("unavailable program map identities")?; |
| 213 | + ensure!( |
| 214 | + exact_bank_maps(actual_maps, expected_maps), |
| 215 | + "classifier does not own this exact bank" |
| 216 | + ); |
| 217 | + let id = program_info.id(); |
| 218 | + dispatch.set(0, program.fd()?, 0)?; |
| 219 | + // Hold every checked descriptor through the publication syscall. A removed |
| 220 | + // pin cannot turn a checked map ID into a replacement object in this gap. |
| 221 | + drop(tag); |
| 222 | + drop(held_maps); |
| 223 | + println!( |
| 224 | + "{}", |
| 225 | + serde_json::json!({"schemaVersion":1,"publishedProgramId":id, |
| 226 | + "scope":"isolated-device-generation","productionAuthority":false}) |
| 227 | + ); |
| 228 | + Ok(()) |
| 229 | +} |
| 230 | + |
| 231 | +fn ledger(directory: &Path) -> Result<()> { |
| 232 | + let data = MapData::from_pin(directory.join("maps/P9LEASESEQ"))?; |
| 233 | + ensure!( |
| 234 | + data.info()?.map_type()? == MapType::Array, |
| 235 | + "unexpected sequence map type" |
| 236 | + ); |
| 237 | + let sequences: Array<_, [u32; 2]> = Array::try_from(Map::Array(data))?; |
| 238 | + ensure!(sequences.len() == 65_536, "unexpected sequence capacity"); |
| 239 | + let mut entries = Vec::new(); |
| 240 | + for key in 0..sequences.len() { |
| 241 | + let value = sequences.get(&key, 0)?; |
| 242 | + if value != [0, 0] { |
| 243 | + ensure!( |
| 244 | + (1..=2).contains(&value[0]) && (1..=2).contains(&value[1]), |
| 245 | + "invalid sequence observation" |
| 246 | + ); |
| 247 | + entries.push([key, value[0], value[1]]); |
| 248 | + } |
| 249 | + } |
| 250 | + println!( |
| 251 | + "{}", |
| 252 | + serde_json::json!({"schemaVersion":1,"capacity":65536, |
| 253 | + "entries":entries,"productionAuthority":false}) |
| 254 | + ); |
| 255 | + Ok(()) |
| 256 | +} |
| 257 | + |
| 258 | +fn main() -> Result<()> { |
| 259 | + ensure!( |
| 260 | + std::env::var("UNF_DEVICE_OBSERVATION_ISOLATED_CONTAINER").as_deref() == Ok("yes"), |
| 261 | + "isolated fixture opt-in required" |
| 262 | + ); |
| 263 | + let arguments: Vec<_> = std::env::args_os().skip(1).collect(); |
| 264 | + ensure!( |
| 265 | + arguments.len() == 2 || (arguments.len() == 3 && arguments[1] == "publish"), |
| 266 | + "expected private bpffs directory and index|bindings|ledger, or publish and a bank directory" |
| 267 | + ); |
| 268 | + let directory = PathBuf::from(&arguments[0]); |
| 269 | + private_directory(&directory)?; |
| 270 | + if arguments[1] == "publish" { |
| 271 | + ensure!(arguments.len() == 3, "publish requires a bank directory"); |
| 272 | + let bank = PathBuf::from(&arguments[2]); |
| 273 | + return publish(&directory, &bank); |
| 274 | + } |
| 275 | + if arguments[1] == "ledger" { |
| 276 | + return ledger(&directory); |
| 277 | + } |
130 | 278 | if arguments[1] == "bindings" { |
131 | 279 | let data = MapData::from_pin(directory.join("maps/P9LEASEDEV"))?; |
132 | 280 | ensure!( |
@@ -169,6 +317,38 @@ fn main() -> Result<()> { |
169 | 317 | mod tests { |
170 | 318 | use super::*; |
171 | 319 |
|
| 320 | + #[test] |
| 321 | + fn sealing_requires_one_exact_frozen_fdinfo_field() { |
| 322 | + assert!(frozen_fdinfo("map_type:\t2\nfrozen:\t1\n")); |
| 323 | + for invalid in [ |
| 324 | + "", |
| 325 | + "frozen:\t0\n", |
| 326 | + "frozen:\t1\nfrozen:\t1\n", |
| 327 | + "notfrozen:\t1\n", |
| 328 | + "frozen:\t10\n", |
| 329 | + ] { |
| 330 | + assert!(!frozen_fdinfo(invalid)); |
| 331 | + } |
| 332 | + } |
| 333 | + |
| 334 | + #[test] |
| 335 | + fn publication_requires_the_exact_distinct_generation_maps() { |
| 336 | + let expected: Vec<_> = (1..=7).collect(); |
| 337 | + assert!(exact_bank_maps((1..=7).rev().collect(), expected.clone())); |
| 338 | + for actual in [ |
| 339 | + vec![], |
| 340 | + (1..=6).collect(), |
| 341 | + (1..=8).collect(), |
| 342 | + (2..=8).collect(), |
| 343 | + vec![1, 2, 3, 4, 5, 6, 6], |
| 344 | + ] { |
| 345 | + assert!(!exact_bank_maps(actual, expected.clone())); |
| 346 | + } |
| 347 | + for invalid in [vec![], vec![0, 1, 2, 3, 4, 5, 6], vec![1, 2, 3, 4, 5, 6, 6]] { |
| 348 | + assert!(!exact_bank_maps(invalid.clone(), invalid)); |
| 349 | + } |
| 350 | + } |
| 351 | + |
172 | 352 | #[test] |
173 | 353 | fn context_exposes_only_the_requested_device_coordinate() { |
174 | 354 | let bytes = context(301).unwrap(); |
|