From f46e3191713ead4324aacc3b89a737f321d97bb2 Mon Sep 17 00:00:00 2001 From: DevomB Date: Thu, 29 Oct 2026 04:32:19 -0700 Subject: [PATCH] A camera grant passes over a node that offers no colour picture An infrared face-login camera is usually a second function of the same USB camera: a video4linux device of its own, so its capture node is index 0 like the colour camera's, and only the numbering kept it from being granted. camera_in now takes the lowest numbered index-0 node that offers a format Firefox reads (MJPEG, YUYV, NV12 and the like), asked of the driver with VIDIOC_ENUM_FMT, which answers from what uvcvideo parsed at probe without powering the camera; when none does, the lowest numbered, as before. The unit test lays out a colour and an infrared camera in one USB device with the infrared one numbered lowest. The camera suite's new case loads two vivid stand-ins, the lower one multi-planar, and checks the grant takes the other. --- build/guest-tests/camera-check.sh | 18 ++++++++++ compartments/kryptikd/src/camera.rs | 42 ++++++++++++++++++++--- compartments/kryptikd/src/camera/tests.rs | 25 +++++++++++--- docs/design/camera.md | 19 ++++++---- docs/user-guide.md | 6 ++-- tools/image/zones-test.sh | 3 +- 6 files changed, 94 insertions(+), 19 deletions(-) diff --git a/build/guest-tests/camera-check.sh b/build/guest-tests/camera-check.sh index d6e0d9b2..3b1969bd 100755 --- a/build/guest-tests/camera-check.sh +++ b/build/guest-tests/camera-check.sh @@ -159,5 +159,23 @@ else fail "camera-gone-ends-grant" "the grant: rc ${rc}: ${said}" fi +# Of two cameras, the first that offers a picture a browser reads: vivid.0's node offers only +# multi-planar formats, which Firefox does not read, so the grant passes it for vivid.1's, as it +# passes an infrared camera's grey. +modprobe -r vivid 2>/dev/null +if modprobe vivid n_devs=2 node_types=0x1,0x1 multiplanar=2,1 2> "$LOG/vivid-two.err"; then + first="$(ls /sys/devices/platform/vivid.0/video4linux 2>&1)" + want="$(ls /sys/devices/platform/vivid.1/video4linux 2>&1)" + said="$(as_user "kryptik-launch --camera-grant untrusted" 2>&1)"; rc=$? + if [[ "$rc" -eq 0 && "$(holder)" == "zone untrusted holds "*" (${want})" ]]; then + pass "camera-colour-first" "${said}: vivid.1's ${want}, not vivid.0's ${first}" + else + fail "camera-colour-first" "rc ${rc}: ${said}; $(holder); vivid.0's ${first}, vivid.1's ${want}" + fi + as_user "kryptik-launch --camera-revoke" > /dev/null 2>&1 +else + fail "camera-colour-first" "$(oneline < "$LOG/vivid-two.err")" +fi + modprobe -r vivid uvcvideo 2>/dev/null echo "CT END" diff --git a/compartments/kryptikd/src/camera.rs b/compartments/kryptikd/src/camera.rs index fc80f44e..33b1f506 100644 --- a/compartments/kryptikd/src/camera.rs +++ b/compartments/kryptikd/src/camera.rs @@ -114,8 +114,10 @@ fn kept(sys: &Path, run: &Path) -> bool { /// The camera's node in `sys`, standing for /sys: of the video4linux devices that are the first /// node of their device (`index` 0, a UVC camera's capture node and not its metadata one), the -/// lowest numbered; its name, major and minor, and what the device calls itself. -pub fn camera_in(sys: &Path) -> Option<(String, u32, u32, String)> { +/// lowest numbered that `colour` passes, else the lowest numbered; its name, major and minor, and +/// what the device calls itself. An infrared camera inside the same USB camera is a device of its +/// own, so its capture node is `index` 0 too. +pub fn camera_in(sys: &Path, colour: impl Fn(&str) -> bool) -> Option<(String, u32, u32, String)> { let class = sys.join("class/video4linux"); let mut found: Vec<(u32, String)> = fs::read_dir(&class).ok()?.flatten() .filter_map(|e| { @@ -125,7 +127,8 @@ pub fn camera_in(sys: &Path) -> Option<(String, u32, u32, String)> { }) .collect(); found.sort(); - let (_, node) = found.into_iter().next()?; + let at = found.iter().position(|(_, n)| colour(n.as_str())).unwrap_or(0); + let (_, node) = found.into_iter().nth(at)?; let dev = read_trim(&class.join(&node).join("dev"))?; let (major, minor) = dev.split_once(':')?; let name: String = read_trim(&class.join(&node).join("name")).unwrap_or_default() @@ -134,6 +137,37 @@ pub fn camera_in(sys: &Path) -> Option<(String, u32, u32, String)> { Some((node, major.parse().ok()?, minor.parse().ok()?, name)) } +/// Whether /dev/NODE offers a picture a browser shows, not only an infrared camera's grey. The +/// driver answers from the formats it read at probe, and opening the node does not power the +/// camera; a node that will not open offers none. +fn offers_colour(node: &str) -> bool { + #[allow(dead_code)] + #[repr(C)] + struct FmtDesc { + index: u32, + kind: u32, + flags: u32, + description: [u8; 32], + pixelformat: u32, + mbus_code: u32, + reserved: [u32; 3], + } + const VIDIOC_ENUM_FMT: libc::c_ulong = 0xc040_5602; + const CAPTURE: u32 = 1; + let mut open = fs::OpenOptions::new(); + open.read(true).custom_flags(libc::O_NONBLOCK | libc::O_CLOEXEC); + let Ok(f) = open.open(Path::new("/dev").join(node)) else { + return false; + }; + (0..64) + .map_while(|index| { + let mut d = FmtDesc { index, kind: CAPTURE, flags: 0, description: [0; 32], pixelformat: 0, mbus_code: 0, reserved: [0; 3] }; + // SAFETY: d is the v4l2_fmtdesc the ioctl fills, on a descriptor we own. + (unsafe { libc::ioctl(f.as_raw_fd(), VIDIOC_ENUM_FMT as _, &mut d) } == 0).then_some(d.pixelformat) + }) + .any(|p| matches!(&p.to_le_bytes(), b"MJPG" | b"JPEG" | b"YUYV" | b"UYVY" | b"NV12" | b"YU12" | b"YV12")) +} + /// A video4linux node's path as udev names it in an event (`%p`): its place in `sys`, under /sys. fn devpath_of(sys: &Path, node: &str) -> Option { let at = fs::canonicalize(sys.join("class/video4linux").join(node)).ok()?; @@ -264,7 +298,7 @@ pub fn grant(zone: &crate::zone::Zone) -> Result { // A camera's node appears once its driver has probed it, which takes a moment. let deadline = Instant::now() + Duration::from_secs(3); let (node, major, minor, name) = loop { - if let Some(found) = camera_in(Path::new("/sys")) { + if let Some(found) = camera_in(Path::new("/sys"), offers_colour) { break found; } if Instant::now() > deadline { diff --git a/compartments/kryptikd/src/camera/tests.rs b/compartments/kryptikd/src/camera/tests.rs index 8a6efca0..74479f6d 100644 --- a/compartments/kryptikd/src/camera/tests.rs +++ b/compartments/kryptikd/src/camera/tests.rs @@ -29,17 +29,34 @@ const UVC: &str = "pci0000:00/0000:00:14.0/usb3/3-6/3-6:1.0"; #[test] fn the_camera_is_the_first_capture_node() { let s = sys("uvc"); - assert_eq!(camera_in(&s), None, "no node, no camera"); + assert_eq!(camera_in(&s, |_| true), None, "no node, no camera"); // A UVC camera: its capture node, then its metadata node. node(&s, UVC, "usb", "video1", 1, "81:1", "Integrated Camera: Integrated C"); - assert_eq!(camera_in(&s), None, "a metadata node is no camera"); + assert_eq!(camera_in(&s, |_| true), None, "a metadata node is no camera"); node(&s, UVC, "usb", "video0", 0, "81:0", "Integrated Camera: Integrated C"); - assert_eq!(camera_in(&s), Some(("video0".into(), 81, 0, "Integrated Camera: Integrated C".into()))); + assert_eq!(camera_in(&s, |_| true), Some(("video0".into(), 81, 0, "Integrated Camera: Integrated C".into()))); // Numbered as numbers: video10 comes after video2. node(&s, "platform/vivid.0", "platform", "video10", 0, "81:10", "vivid"); node(&s, "platform/vivid.1", "platform", "video2", 0, "81:2", "\u{1b}[2Jvivid"); let _ = fs::remove_file(s.join("class/video4linux/video0")); - assert_eq!(camera_in(&s), Some(("video2".into(), 81, 2, "[2Jvivid".into())), "a name shows no terminal escape"); + assert_eq!(camera_in(&s, |_| true), Some(("video2".into(), 81, 2, "[2Jvivid".into())), "a name shows no terminal escape"); + let _ = fs::remove_dir_all(&s); +} + +/// A colour camera and an infrared one in one USB camera, each a device with its own capture +/// node, as a laptop's colour and infrared module lists them; here the infrared one numbers lowest. +#[test] +fn the_camera_is_the_first_that_offers_colour() { + let s = sys("ir"); + let ir = UVC.replace(":1.0", ":1.2"); + node(&s, &ir, "usb", "video0", 0, "81:0", "HP 5MP Camera: HP IR Camera"); + node(&s, &ir, "usb", "video1", 1, "81:1", "HP 5MP Camera: HP IR Camera"); + node(&s, UVC, "usb", "video2", 0, "81:2", "HP 5MP Camera: HP 5MP Camera"); + node(&s, UVC, "usb", "video3", 1, "81:3", "HP 5MP Camera: HP 5MP Camera"); + let colour = |n: &str| n == "video2"; + assert_eq!(camera_in(&s, colour), Some(("video2".into(), 81, 2, "HP 5MP Camera: HP 5MP Camera".into()))); + assert_eq!(camera_in(&s, |_| false).map(|c| c.0).as_deref(), Some("video0"), "none offers colour: the lowest"); + assert_eq!(camera_in(&s, |n| n == "video1").map(|c| c.0).as_deref(), Some("video0"), "a metadata node never"); let _ = fs::remove_dir_all(&s); } diff --git a/docs/design/camera.md b/docs/design/camera.md index 601e8bb7..efea27ae 100644 --- a/docs/design/camera.md +++ b/docs/design/camera.md @@ -111,6 +111,9 @@ itself waits for the tests on physical hardware. `work` finds no camera; the chrome's window names `personal`; - taken away while granted (`vivid` unloaded), the grant ends and the node goes; +- of two stand-ins, the grant passes over the lower numbered, whose node + offers only multi-planar formats, which Firefox does not read, for the + other; - `k` alone takes it back, and so does the screen locking: the probe's open descriptor fails at its next read, and the node is gone; - a zone whose file refuses the camera is never offered it. @@ -118,13 +121,15 @@ itself waits for the tests on physical hardware. ## Not covered - MIPI cameras behind Intel's IPU6 and AMD's image processor (below), and - infrared cameras. Of several cameras, a grant gives the zone the lowest - numbered node that is its device's first (`index` 0), which a camera's - metadata node is not - (`camera_in` in `camera.rs`). On a laptop whose infrared face-login camera - is a USB device of its own, that node may be the infrared camera's, which - gives a call no picture. Preferring a node that offers a colour format - waits until a laptop shows it. + infrared cameras. An infrared face-login camera is usually a second + function of the same USB camera: a video4linux device of its own, whose + capture node is `index` 0 like the colour camera's. Of several cameras, a + grant gives the zone the lowest numbered node that is its device's first + (`index` 0), which a camera's metadata node is not, and that offers a + colour format (MJPEG, YUYV, NV12 and the like), which an infrared camera + giving grey frames alone does not; when none does, the lowest numbered + (`camera_in` in `camera.rs`). An infrared camera that offers a colour + format too may still be the one given, and gives a call no picture. - Which USB devices are accepted at all. - Screen sharing, which is the compositor's question, not the camera's. diff --git a/docs/user-guide.md b/docs/user-guide.md index 28cee8cb..3380ce6e 100644 --- a/docs/user-guide.md +++ b/docs/user-guide.md @@ -714,9 +714,9 @@ leaves out serial numbers, hardware addresses and UUIDs. planned for October 2026. QEMU has no lid, no microphone or brightness keys, no backlight, no built-in panel and no camera of the USB Video Class, so those parts wait for it. -- Of a laptop's cameras, `k` gives the zone the first the kernel lists. On - a laptop whose infrared face-login camera is a USB device of its own, - that may be the infrared one, which shows no picture. +- Of a laptop's cameras, `k` gives the zone the first that offers a colour + picture. An infrared face-login camera that offers a colour format too + may be the one given, and shows no picture. - Laptop sound goes through the HD Audio driver and USB audio. On many recent laptops part of it does not work: built-in microphones on Intel's or AMD's audio processor, all built-in sound on Intel and AMD laptops diff --git a/tools/image/zones-test.sh b/tools/image/zones-test.sh index ae47b205..e34b030e 100755 --- a/tools/image/zones-test.sh +++ b/tools/image/zones-test.sh @@ -183,7 +183,8 @@ T2C="$(txt)" [[ "$rc" -eq 0 ]] && green "the camera rounds ran to their end" || red "the camera rounds did not run to their end" for name in camera-not-by-alias camera-not-granted camera-refused-vault camera-refused-net camera-granted \ camera-zone-captures camera-ends-with-zone camera-other-zone-none camera-revoked \ - camera-revoked-kept-descriptor camera-granted-again-while-running camera-ends-with-zone-again camera-gone-ends-grant; do + camera-revoked-kept-descriptor camera-granted-again-while-running camera-ends-with-zone-again camera-gone-ends-grant \ + camera-colour-first; do grep -q "CT PASS ${name}" <<<"$T2C" && green "guest: ${name}" || red "guest: ${name} (not passed)" done grep 'CT FAIL' <<<"$T2C" | sed 's/^/ /'