oxedyne/fe2o3/fe2o3_sys/tests/session.rs
11.9 KiB, 2 runs
created by r1870400018:76759, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! Tests for `fe2o3_sys::session`: the check after a drop, the identity lookup and the ownership |
| 2 | //! test, against the running process and real files. |
| 3 | //! |
| 4 | //! The drop itself needs root. Its two arms are ignored here and run as root in the VM (BR-4e S6) with |
| 5 | //! `cargo test --features session -- --ignored`; it drops a child copy of this test binary, so the |
| 6 | //! harness keeps its own ids. |
| 7 | |
| 8 | #![cfg(all(target_os = "linux", feature = "session"))] |
| 9 | |
| 10 | use oxedyne_fe2o3_core::prelude::*; |
| 11 | use oxedyne_fe2o3_sys::session::{ |
| 12 | check_dropped, |
| 13 | drop_to, |
| 14 | strict_entry, |
| 15 | strict_owner, |
| 16 | Identity, |
| 17 | }; |
| 18 | |
| 19 | use std::{ |
| 20 | env, |
| 21 | fs, |
| 22 | os::unix::fs::{ |
| 23 | symlink, |
| 24 | MetadataExt, |
| 25 | }, |
| 26 | path::{ |
| 27 | Path, |
| 28 | PathBuf, |
| 29 | }, |
| 30 | process::Command, |
| 31 | }; |
| 32 | |
| 33 | fn scratch(tag: &str) -> Outcome<PathBuf> { |
| 34 | let dir = env::temp_dir().join(format!("fe2o3-sys-session-{}-{}", tag, std::process::id())); |
| 35 | let _ = fs::remove_dir_all(&dir); |
| 36 | res!(fs::create_dir_all(&dir)); |
| 37 | Ok(dir) |
| 38 | } |
| 39 | |
| 40 | fn is_root() -> Outcome<bool> { |
| 41 | Ok(res!(Identity::current()).uid == 0) |
| 42 | } |
| 43 | |
| 44 | #[test] |
| 45 | fn the_current_identity_passes_its_own_check() -> Outcome<()> { |
| 46 | let me = res!(Identity::current()); |
| 47 | res!(check_dropped(&me)); |
| 48 | Ok(()) |
| 49 | } |
| 50 | |
| 51 | #[test] |
| 52 | fn a_different_uid_fails_the_check() -> Outcome<()> { |
| 53 | let me = res!(Identity::current()); |
| 54 | let other = Identity::new(me.uid.wrapping_add(1), me.gid, me.groups.clone()); |
| 55 | match check_dropped(&other) { |
| 56 | Ok(()) => Err(err!("check_dropped passed uid {} for a process of uid {}.", other.uid, me.uid; Test)), |
| 57 | Err(e) => { |
| 58 | let msg = format!("{}", e); |
| 59 | if msg.contains("did not hold") { Ok(()) } |
| 60 | else { Err(err!("Unexpected refusal: {}", msg; Test)) } |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | #[test] |
| 66 | fn a_different_gid_fails_the_check() -> Outcome<()> { |
| 67 | let me = res!(Identity::current()); |
| 68 | let gid = me.gid.wrapping_add(1); |
| 69 | let mut groups = me.groups.clone(); |
| 70 | groups.push(gid); |
| 71 | let other = Identity::new(me.uid, gid, groups); |
| 72 | if check_dropped(&other).is_ok() { |
| 73 | return Err(err!("check_dropped passed gid {} for a process of gid {}.", gid, me.gid; Test)); |
| 74 | } |
| 75 | Ok(()) |
| 76 | } |
| 77 | |
| 78 | #[test] |
| 79 | fn an_extra_group_fails_the_check() -> Outcome<()> { |
| 80 | let me = res!(Identity::current()); |
| 81 | let extra = (60000..65000).find(|g| !me.groups.contains(g)).unwrap_or(65001); |
| 82 | let mut groups = me.groups.clone(); |
| 83 | groups.push(extra); |
| 84 | let other = Identity::new(me.uid, me.gid, groups); |
| 85 | if check_dropped(&other).is_ok() { |
| 86 | return Err(err!("check_dropped passed with group {} the process lacks.", extra; Test)); |
| 87 | } |
| 88 | Ok(()) |
| 89 | } |
| 90 | |
| 91 | #[test] |
| 92 | fn a_missing_group_fails_the_check() -> Outcome<()> { |
| 93 | let me = res!(Identity::current()); |
| 94 | let Some(drop) = me.groups.iter().copied().find(|g| *g != me.gid) else { |
| 95 | return Ok(()); // Only the primary group: nothing to leave out. |
| 96 | }; |
| 97 | let other = Identity::new(me.uid, me.gid, me.groups.iter().copied().filter(|g| *g != drop).collect()); |
| 98 | if check_dropped(&other).is_ok() { |
| 99 | return Err(err!("check_dropped passed without group {} the process has.", drop; Test)); |
| 100 | } |
| 101 | Ok(()) |
| 102 | } |
| 103 | |
| 104 | #[test] |
| 105 | fn identity_of_the_running_user_matches_its_ids() -> Outcome<()> { |
| 106 | let me = res!(Identity::current()); |
| 107 | let name = res!(res!(nix::unistd::User::from_uid(nix::unistd::Uid::from_raw(me.uid))) |
| 108 | .ok_or_else(|| err!("No password entry for uid {}.", me.uid; Missing))); |
| 109 | let id = res!(Identity::of_user(&name.name)); |
| 110 | if id.uid != me.uid || id.gid != name.gid.as_raw() || !id.groups.contains(&id.gid) { |
| 111 | return Err(err!("of_user('{}') gave {:?} for uid {} gid {}.", name.name, id, me.uid, name.gid; Test)); |
| 112 | } |
| 113 | Ok(()) |
| 114 | } |
| 115 | |
| 116 | #[test] |
| 117 | fn an_unknown_user_is_refused() -> Outcome<()> { |
| 118 | if Identity::of_user("fe2o3-no-such-user-q7").is_ok() { |
| 119 | return Err(err!("of_user found a user that does not exist."; Test)); |
| 120 | } |
| 121 | Ok(()) |
| 122 | } |
| 123 | |
| 124 | #[test] |
| 125 | fn identity_groups_hold_the_gid_once_and_sorted() -> Outcome<()> { |
| 126 | let id = Identity::new(1000, 1000, vec![27, 1000, 27, 4]); |
| 127 | if id.groups != vec![4, 27, 1000] { |
| 128 | return Err(err!("Groups were {:?}.", id.groups; Test)); |
| 129 | } |
| 130 | let id = Identity::new(1000, 1001, vec![4]); |
| 131 | if id.groups != vec![4, 1001] { |
| 132 | return Err(err!("The gid was not added: {:?}.", id.groups; Test)); |
| 133 | } |
| 134 | Ok(()) |
| 135 | } |
| 136 | |
| 137 | #[test] |
| 138 | fn an_unprivileged_drop_fails_loudly() -> Outcome<()> { |
| 139 | if res!(is_root()) { return Ok(()); } |
| 140 | if drop_to(&Identity::new(65534, 65534, vec![65534])).is_ok() { |
| 141 | return Err(err!("A drop without privilege reported success."; Test)); |
| 142 | } |
| 143 | // The failed drop left the process as it was. |
| 144 | res!(check_dropped(&res!(Identity::current()))); |
| 145 | Ok(()) |
| 146 | } |
| 147 | |
| 148 | #[test] |
| 149 | fn root_owned_system_directories_pass() -> Outcome<()> { |
| 150 | res!(strict_owner(Path::new("/usr/bin"))); |
| 151 | res!(strict_owner(Path::new("/"))); |
| 152 | Ok(()) |
| 153 | } |
| 154 | |
| 155 | #[test] |
| 156 | fn a_link_into_root_owned_files_passes_on_both_chains() -> Outcome<()> { |
| 157 | // /bin is a root-owned link to usr/bin on a merged-/usr system, and plain elsewhere. |
| 158 | res!(strict_owner(Path::new("/bin/sh"))); |
| 159 | Ok(()) |
| 160 | } |
| 161 | |
| 162 | #[test] |
| 163 | fn a_root_owned_link_to_a_user_owned_target_is_refused() -> Outcome<()> { |
| 164 | // /dev/fd is a root-owned link to /proc/self/fd, and /proc/<pid> is owned by the test user: |
| 165 | // the named chain passes, so only the resolved chain can refuse it. |
| 166 | let link = Path::new("/dev/fd"); |
| 167 | let m = res!(fs::symlink_metadata(link)); |
| 168 | if !m.file_type().is_symlink() || m.uid() != 0 || res!(is_root()) { |
| 169 | return Err(err!("This test needs /dev/fd to be a root-owned link and a test user other \ |
| 170 | than root."; Test)); |
| 171 | } |
| 172 | match strict_owner(link) { |
| 173 | Ok(()) => Err(err!("strict_owner passed /dev/fd, which resolves under /proc/<pid>."; Test)), |
| 174 | Err(e) => if e.to_string().contains("/proc/") { |
| 175 | Ok(()) |
| 176 | } else { |
| 177 | Err(err!("strict_owner refused /dev/fd for the wrong entry: {}", e; Test)) |
| 178 | }, |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | #[test] |
| 183 | fn a_world_writable_root_directory_is_refused() -> Outcome<()> { |
| 184 | // /tmp is root-owned and mode 1777. |
| 185 | match strict_owner(Path::new("/tmp")) { |
| 186 | Ok(()) => Err(err!("strict_owner passed /tmp."; Test)), |
| 187 | Err(e) => { |
| 188 | let msg = format!("{}", e); |
| 189 | if msg.contains("writable by group or others") && msg.contains("'/tmp'") { Ok(()) } |
| 190 | else { Err(err!("Unexpected refusal: {}", msg; Test)) } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | #[test] |
| 196 | fn a_user_owned_file_is_refused_naming_the_first_bad_entry() -> Outcome<()> { |
| 197 | if res!(is_root()) { return Ok(()); } |
| 198 | let dir = res!(scratch("owner")); |
| 199 | let file = dir.join("cfg"); |
| 200 | res!(fs::write(&file, b"x")); |
| 201 | let out = strict_owner(&file); |
| 202 | let _ = fs::remove_dir_all(&dir); |
| 203 | match out { |
| 204 | Ok(()) => Err(err!("strict_owner passed a file owned by the test user."; Test)), |
| 205 | // The chain stops at the first entry above the file that fails, /tmp or the user's own. |
| 206 | Err(e) => { |
| 207 | let msg = format!("{}", e); |
| 208 | let first = file.ancestors().skip(1).find(|a| msg.contains(&format!("'{}'", a.display()))); |
| 209 | if first.is_some() { Ok(()) } |
| 210 | else { Err(err!("Unexpected refusal: {}", msg; Test)) } |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | #[test] |
| 216 | fn a_user_owned_link_to_a_root_file_is_refused() -> Outcome<()> { |
| 217 | if res!(is_root()) { return Ok(()); } |
| 218 | let home = res!(env::var("HOME").map_err(|_| err!("HOME is not set."; Missing))); |
| 219 | let dir = PathBuf::from(home).join(".cache").join(format!("fe2o3-sys-session-link-{}", std::process::id())); |
| 220 | let _ = fs::remove_dir_all(&dir); |
| 221 | res!(fs::create_dir_all(&dir)); |
| 222 | let link = dir.join("sh"); |
| 223 | res!(symlink("/usr/bin/env", &link)); |
| 224 | let out = strict_owner(&link); |
| 225 | let _ = fs::remove_dir_all(&dir); |
| 226 | match out { |
| 227 | Ok(()) => Err(err!("strict_owner passed a link in a user-owned directory."; Test)), |
| 228 | Err(e) => { |
| 229 | let msg = format!("{}", e); |
| 230 | if msg.contains("not root") { Ok(()) } |
| 231 | else { Err(err!("Unexpected refusal: {}", msg; Test)) } |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | #[test] |
| 237 | fn a_relative_path_or_dotdot_is_refused() -> Outcome<()> { |
| 238 | if strict_owner(Path::new("usr/bin")).is_ok() { |
| 239 | return Err(err!("strict_owner passed a relative path."; Test)); |
| 240 | } |
| 241 | if strict_owner(Path::new("/usr/../usr/bin")).is_ok() { |
| 242 | return Err(err!("strict_owner passed a path with '..'."; Test)); |
| 243 | } |
| 244 | Ok(()) |
| 245 | } |
| 246 | |
| 247 | #[test] |
| 248 | fn the_entry_test_reads_owner_and_mode() -> Outcome<()> { |
| 249 | let p = Path::new("/x"); |
| 250 | res!(strict_entry(p, 0, 0o755, false)); |
| 251 | res!(strict_entry(p, 0, 0o777, true)); // A link's own mode means nothing. |
| 252 | for (uid, mode, link) in [(1000, 0o755, false), (0, 0o775, false), (0, 0o757, false), (1000, 0o777, true)] { |
| 253 | if strict_entry(p, uid, mode, link).is_ok() { |
| 254 | return Err(err!("strict_entry passed uid {} mode {:o} link {}.", uid, mode, link; Test)); |
| 255 | } |
| 256 | } |
| 257 | Ok(()) |
| 258 | } |
| 259 | |
| 260 | const CHILD: &str = "FE2O3_SESSION_DROP_CHILD"; |
| 261 | |
| 262 | /// Runs the named root arm again in a child copy of this binary, which does the drop, so the |
| 263 | /// harness keeps its own ids. |
| 264 | fn run_as_child(name: &str) -> Outcome<()> { |
| 265 | if !res!(is_root()) { |
| 266 | return Err(err!("This arm must run as root."; Test)); |
| 267 | } |
| 268 | let exe = res!(env::current_exe()); |
| 269 | let st = res!(Command::new(exe) |
| 270 | .args(["--exact", name, "--ignored", "--test-threads=1"]) |
| 271 | .env(CHILD, "1") |
| 272 | .status()); |
| 273 | if !st.success() { |
| 274 | return Err(err!("The dropped child in '{}' failed: {}.", name, st; Test)); |
| 275 | } |
| 276 | Ok(()) |
| 277 | } |
| 278 | |
| 279 | #[test] |
| 280 | #[ignore = "needs root: drops a child process to nobody; BR-4e S6 runs it in the VM"] |
| 281 | fn root_drops_to_nobody_and_cannot_return() -> Outcome<()> { |
| 282 | if env::var(CHILD).is_ok() { |
| 283 | let id = res!(Identity::of_user("nobody")); |
| 284 | res!(drop_to(&id)); |
| 285 | let now = res!(Identity::current()); |
| 286 | if now != id { |
| 287 | return Err(err!("After the drop the process is {:?}, not {:?}.", now, id; Test)); |
| 288 | } |
| 289 | if nix::unistd::setuid(nix::unistd::Uid::from_raw(0)).is_ok() { |
| 290 | return Err(err!("uid 0 was taken back after the drop."; Test)); |
| 291 | } |
| 292 | return Ok(()); |
| 293 | } |
| 294 | run_as_child("root_drops_to_nobody_and_cannot_return") |
| 295 | } |
| 296 | |
| 297 | #[test] |
| 298 | #[ignore = "needs root: drops a child process; BR-4e S6 runs it in the VM"] |
| 299 | fn root_drop_that_misses_its_identity_is_refused() -> Outcome<()> { |
| 300 | if env::var(CHILD).is_ok() { |
| 301 | // Every call succeeds, but the groups asked for omit the gid, which the process then |
| 302 | // holds; only the check after the calls can see that the drop missed. |
| 303 | let id = Identity { uid: 65534, gid: 65534, groups: Vec::new() }; |
| 304 | return match drop_to(&id) { |
| 305 | Ok(()) => Err(err!("drop_to passed a drop the process does not match, {:?}.", id; Test)), |
| 306 | Err(_) => Ok(()), |
| 307 | }; |
| 308 | } |
| 309 | run_as_child("root_drop_that_misses_its_identity_is_refused") |
| 310 | } |
| 311 | |
| 312 | #[test] |
| 313 | fn the_account_of_the_current_uid_names_its_home_and_shell() -> Outcome<()> { |
| 314 | let id = res!(Identity::current()); |
| 315 | let a = res!(oxedyne_fe2o3_sys::session::Account::of_uid(id.uid)); |
| 316 | assert_eq!(a.uid, id.uid); |
| 317 | assert_eq!(res!(oxedyne_fe2o3_sys::session::uids())[0], id.uid); |
| 318 | assert!(!a.name.is_empty()); |
| 319 | assert!(a.home.is_absolute(), "home {:?}", a.home); |
| 320 | // A uid no system gives out is refused, not answered with a default. |
| 321 | assert!(oxedyne_fe2o3_sys::session::Account::of_uid(4_000_000_017).is_err()); |
| 322 | Ok(()) |
| 323 | } |
| 324 | |
| 325 | #[test] |
| 326 | fn the_account_of_a_name_is_the_account_of_its_uid() -> Outcome<()> { |
| 327 | let id = res!(Identity::current()); |
| 328 | let by_uid = res!(oxedyne_fe2o3_sys::session::Account::of_uid(id.uid)); |
| 329 | let by_name = res!(oxedyne_fe2o3_sys::session::Account::of_name(&by_uid.name)); |
| 330 | assert_eq!(by_name, by_uid); |
| 331 | let root = res!(oxedyne_fe2o3_sys::session::Account::of_name("root")); |
| 332 | assert_eq!((root.uid, root.gid), (0, 0)); |
| 333 | // A name no system gives out is refused, not answered with a default. |
| 334 | assert!(oxedyne_fe2o3_sys::session::Account::of_name("no-such-user-4000000017").is_err()); |
| 335 | Ok(()) |
| 336 | } |