Files
chandra/docs/face-unlock.md
T

9.5 KiB

Face unlock

Chandra can recognize your face instead of asking for your password, in the style of 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

Setting it up

just install-face          # PAM module, chandra-face, settings, face models (asks for root once)

You need Python 3 with OpenCV 4.5.4 or later (python3-opencv on Debian/Ubuntu/Fedora, python-opencv on Arch), a C compiler and the PAM headers (libpam0g-dev / pam-devel). 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 Settings rows are part of the session, not of install-face: after updating Chandra, run just install (or just install-dev) and 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 a face… asks for your password (in the password dialog), then looks through the camera for about ten seconds and keeps five views of your face. Add another face for glasses, a different light, or a second camera position.
  • 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.
  • Remove (click twice) forgets one face.

Everything Settings does runs chandra-face through run0 (or pkexec), so each change asks for an administrator's password first. From a terminal:

chandra-face enroll --label "Desk"   # add a face
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-face                  # takes it out of every PAM service first

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, /usr/bin/python3)
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

[face]
enabled = yes          # no: the module stays quiet and PAM goes straight on to the password
device = /dev/video0   # the camera; an IR camera is often /dev/video2
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): 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.

Privacy and security

  • Nothing leaves the machine. Camera frames are only held in memory by the root helper for the few seconds it looks; 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: enrolling 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.
  • 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). 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 against a fake chandra-face.

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), and run0/pkexec elevation from Settings on a real systemd session.