Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions build/guest-tests/camera-check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
42 changes: 38 additions & 4 deletions compartments/kryptikd/src/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand All @@ -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()
Expand All @@ -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<String> {
let at = fs::canonicalize(sys.join("class/video4linux").join(node)).ok()?;
Expand Down Expand Up @@ -264,7 +298,7 @@ pub fn grant(zone: &crate::zone::Zone) -> Result<Grant, String> {
// 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 {
Expand Down
25 changes: 21 additions & 4 deletions compartments/kryptikd/src/camera/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
19 changes: 12 additions & 7 deletions docs/design/camera.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,20 +111,25 @@ 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.

## 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.

Expand Down
6 changes: 3 additions & 3 deletions docs/user-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion tools/image/zones-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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/^/ /'
Expand Down
Loading