# Face unlock Chandra can recognize your face instead of asking for your password, in the style of [Howdy](https://github.com/boltgolt/howdy): a PAM module that looks through the camera when PAM asks for authentication, and lets PAM go on to the password when it doesn't see you. It works wherever PAM is used: Chandra's password dialog (polkit, run0, pkexec), sudo in a terminal, and any other service you add it to. ![The password dialog looking for a face](screenshots/face-scanning.png) ## Setting it up ```sh just install # comes with the rest of Chandra (asks for root once) just install --pam # ... and turns it on for polkit-1 and sudo right away ``` `just install` installs the PAM module, `chandra-face`, its settings and the face models along with the session; `--no-face` leaves them out. Without a C compiler or the PAM headers it warns and installs everything else. Nothing from the distro is needed beyond a C compiler and the PAM headers (`libpam0g-dev` / `pam-devel`) and [pixi](https://pixi.sh): Python, OpenCV and numpy come from a pixi environment (`face/pixi.toml` + `face/pixi.lock`) that `just install` makes in `/usr/local/lib/chandra/face-env`, owned by root. It is deliberately not the checkout's `.pixi`: the PAM module runs that Python as root, so it must not be anywhere a user can write. The models (OpenCV's YuNet face detector and SFace recognizer, 37 MB) are downloaded from the opencv_zoo repository and checked against their SHA-256 sums. The environment takes about 450 MB. The Settings rows are part of the session: after `just install`, log in again so the compositor, its host and the Settings module are the new ones. (`just reload` alone isn't enough: an older host doesn't serve the `face-unlock` capability.) Then in **Settings > Session > Face unlock**: - **Add or remove faces…** opens the Face unlock window (below). **Remove…** on a face opens it with that face marked. - **Use my face for passwords** puts the face first in the PAM services `polkit-1` (the password dialog) and `sudo`, after the password. Turning it off takes it out again (one password). ### The Face unlock window ![The Face unlock window: live picture, two snaps, a saved face](screenshots/face-window.png) - The camera starts at once, with no password: a live picture with a box round the face it sees. - **Snap** (or Space) keeps the picture in view and shows it below; snap a few: straight on, turned a little, with and without glasses. Click a snap to drop it. A snap that can't be used says why ("No face in view", "More than one face in view", "Come closer to the camera"). - **Remove** / **Keep** on a saved face marks it for removal, or not. - **Apply** (or Enter) saves the snaps as one new face and removes the marked ones, in one step: the password dialog asks once. While it asks, the window steps aside (a note at the top says it is waiting) so the dialog, Chandra's or another polkit agent's window, gets the keyboard and the pointer. If the password isn't given, the window comes back with the snaps and marks as they were and says so. **Cancel** (Esc) changes nothing. Why root at all: the face data is root's (`/var/lib/chandra-face`, 0600) and PAM trusts it to let you into sudo and polkit without a password. If you could write it as yourself, so could any program running as you, and it could quietly add a face (or a photo) of its choosing and then use sudo. So only the change needs root, and only once: the camera and the pictures stay on your side, and only the face data of the snaps you kept crosses to the root step. (Removing a face only takes access away, so it could be allowed without a password; it isn't yet, to keep one rule.) From a terminal (each root command asks for the password itself): ```sh chandra-face enroll --label "Desk" # add a face (looks for ten seconds, keeps five views) chandra-face list chandra-face test # look now and print the similarity scores chandra-face remove 2 chandra-face clear # forget every face chandra-face pam enable polkit-1 sudo chandra-face pam disable # polkit-1 and sudo by default chandra-face status # what is set up, what is missing just uninstall [--purge] # takes it out of every PAM service first (--purge: faces too) ``` ## In the password dialog While the face module looks, the dialog shows a face row with a sweeping ring: "Looking for your face…". The password field stays usable: what you type, and Enter, wait until PAM asks for the password, so you can type it right away if you know the camera won't see you. When the face matches, the row says "Face recognized" and the dialog closes. When it doesn't ("Face not recognized", "The camera picture is too dark", "No face in view"), the row says why and the password prompt comes up as usual. After a wrong password the next attempt tries the face again. The dialog understands Howdy's messages too ("Attempting facial authentication", "Identified face as…", "Failure, timeout reached", "Face detection image too dark"), so if you already use Howdy, keep `pam_howdy.so` and the dialog shows its progress the same way; Settings says which PAM services use Howdy. Faces you enrolled with Howdy are managed with `sudo howdy add`/`remove`. ## PAM `chandra-face pam enable SERVICE` adds this line before the service's first `auth` (or `@include`) line in `/etc/pam.d/SERVICE` (copying a vendor file from `/usr/lib/pam.d` first, and keeping the original as `SERVICE.chandra-face.bak`): ``` -auth sufficient pam_chandra_face.so # added by chandra-face ``` `sufficient` means a match ends the auth stack with success, and anything else (no match, no camera, nothing enrolled, an error) is ignored, so the next line, your password, always still works. The leading `-` makes PAM skip the line quietly if the module is ever uninstalled. To edit by hand instead, put that line at the top of the auth section; module options: | option | meaning | | --- | --- | | `timeout=N` | the helper gets N + 6 seconds in all before it is killed (default 4; the camera's own timeout is in face.conf) | | `config=PATH` | another settings file (default `/etc/chandra/face.conf`) | | `helper=PATH`, `python=PATH` | another chandra-face / Python (default `/usr/local/lib/chandra/chandra-face` and the face-env's python3; both must be root's) | | `quiet` | don't send the "looking" / "not recognized" messages | | `debug` | the helper's stderr goes to the application's stderr; skipped helpers are logged | The module skips the face (quietly) for remote sessions (`PAM_RHOST`, or `SSH_CONNECTION` in the environment), when the laptop lid is closed, and when the user has no face enrolled. It is not a good idea for `login` or a display manager on the first login: a face doesn't unlock the GNOME keyring or KWallet, which need the password. ## Settings: /etc/chandra/face.conf ```ini [face] enabled = yes # no: the module stays quiet and PAM goes straight on to the password device = /dev/v4l/by-id/usb-EMEET_...-video-index0 # the camera (by-id: /dev/videoN numbers change); an IR camera is often index1 timeout = 4 # seconds to look before giving up threshold = 0.42 # SFace cosine similarity needed (OpenCV suggests 0.363; higher is stricter) matches = 2 # frames that must match dark = 25 # skip frames darker than this (IR emitters take a moment to light up) min_face = 80 # smallest face to use, in pixels require_ir = no # yes: accept only grey (infrared) frames enroll_frames = 5 store = /var/lib/chandra-face models = /usr/local/share/chandra/face ``` `chandra-face test` prints the score of each frame, which helps pick a threshold for your camera. ## How it works - `face/pam_chandra_face.c` (the PAM module): forks `python3 -I chandra-face compare --user USER` as root with a clean environment (stdin and stderr on /dev/null), forwards each line it prints to the application's conversation as `PAM_TEXT_INFO` or `PAM_ERROR_MSG`, and maps its exit status to `PAM_SUCCESS` (0), `PAM_AUTH_ERR` (1) or `PAM_AUTHINFO_UNAVAIL` (anything else). The helper must be root's and not writable by others, or it is not run. - `face/chandra-face` (Python, on the face-env's interpreter): finds faces with YuNet, turns the largest one into a 128-number SFace embedding, and compares it with the stored ones (cosine similarity). It also enrolls, lists, removes, edits PAM files, reports status and downloads the models. Root commands run by another user re-run themselves through run0 (or pkexec when systemd isn't PID 1). - The compositor (`compositor/shell/face.mbt`) runs `chandra-face status` as you to fill the Settings rows (labels and dates only), and the changes on a socket so it never waits on them; the session-settings module reaches it through the `face-unlock` capability (`wit/capabilities.wit`). The password dialog (`compositor/polkit/dialog.mbt`, `face_message`) turns the module's messages into the face row. - The Face unlock window (`compositor/host/face_window.mbt`, drawn by the compositor like the password dialog) talks to `chandra-face camera`, run as you on a socket: it sends `frame W H BX BY BW BH LEN` + RGB bytes (320 px wide, about 12 a second) and `snap ID 96 96 LEN` + RGB (the face, cropped), and reads `snap`, `drop ID`, `apply REMOVE-IDS`, `quit`. The snaps' embeddings stay in that process; on `apply` it closes the camera and runs `chandra-face apply` through run0/pkexec with `{"add": [{"label", "vectors"}], "remove": [ids]}` on its stdin, which checks the data (sizes, finite numbers, the same length as the faces already saved) and writes the store once. The window's pictures are host images (`raster.image-pixels`), reused and dropped when it closes. ## Privacy and security - **Nothing leaves the machine.** Camera frames are only held in memory, by the root helper for the few seconds it looks, and by the Face unlock window (its camera process and its images) while it is open; they are never written to disk or sent anywhere. The only network access is the one-time model download. - **What is stored**: per user, `/var/lib/chandra-face/USER/faces.json` (root, mode 0600) holds the embeddings (feature vectors, not pictures) and `faces.list` (0644) only the labels and dates that Settings shows. The helper refuses a store, config or model that is a symlink, not root's, or writable by group or others. - **Changing faces needs an administrator**: saving runs as root after polkit's password check, so a program running as you can't quietly add someone else's face to your account. The face data the window hands over comes from your own camera process: the password is the check, as it is for anything else that root runs for you. With face unlock already on for polkit-1, the face itself can answer that password dialog (the window closes the camera first so it can). - **2D face recognition can be fooled.** A good photo or video of you held up to a normal (RGB) webcam may be accepted: there is no liveness or depth check. An infrared camera (most "Windows Hello" laptops have one) is much harder to fool with a photo or a screen; set `device` to it and `require_ir = yes`. Twins and close relatives may also be accepted. Treat face unlock as a convenience that is weaker than a good password; don't use it where that matters (it is never added to `login` or SSH by Settings). - **The password always works**: the module is `sufficient`, skipped when it fails or is missing, and killed if it hangs. ## Tests `just test-face` runs `face/test_face.py` with a fake camera (a folder of PGM frames) and a fake recognizer, so it needs no camera, OpenCV or models: enrollment, recognition, the reasons for failing, permission checks on the store, PAM file editing (including Debian's `@include common-auth`), and the window's camera session (live pictures, snaps, drops, one apply that adds and removes, bad apply data refused). `just test-face --pam` (as root, with `pamtester`) also builds the PAM module and runs it through a throwaway PAM service: a match succeeds, a stranger falls through, remote and unenrolled users are skipped quietly, a hanging helper is killed, a writable helper is not run. With `CHANDRA_FACE_MODELS=DIR` the OpenCV backend is checked on frames without a face. On the compositor side, `compositor/polkit/polkit_test.mbt` covers the message parsing and the dialog's face states, `compositor/host/auth_test.mbt` drives the dialog through a mock helper (`CHANDRA_AUTH_SHOTS=dir` saves `14-face-scanning.png` and `15-face-failed.png`), and `compositor/host/face_test.mbt` drives Settings > Session and the Face unlock window (Remove… opens it with the face marked, Space snaps, Enter applies once, Esc cancels) against a fake chandra-face (`CHANDRA_AUTH_SHOTS=dir` saves `17-face-window.png`). Not verified here: a real camera (frames came from image files; with OpenCV's real models, a rotated and rescaled photo of the enrolled face scored 0.92 and a different person -0.18; the window's screenshot is the real pipeline on a photo), and run0/pkexec elevation (with the apply data on stdin) on a real systemd session.