Skip to content

Implement automatic unlocking though a systemd service #95

@josephlr

Description

@josephlr

Right now, both automatic unlocking of directories and rewrapping of the user's login protector are done though the PAM module pam_fscrypt.so. Specifically the following PAM types do the following things:

  • session
    • tracks of the number of open sessions for that user
    • automatically unlocks user encrypted directories
    • automatically locks user encrypted directories (on the last logout)
  • auth forwards the AUTHTOK object as part of the PAM data (if necessary)
  • password rewarps the login protector (if necessary)

The basic plan is to keep the auth and password types in the pam modules (with modifications), while doing all the stuff we do in session in [email protected].

@ebiggers, @tyhicks: I'd love your feedback on the basic idea (and if it would improve things).
@fancytenseletters, @Minecrell, @sebadoom: This should (hopefully) fix some of the bugs you've reported.

Justification

Bugs #66, #77, #93, #196 (#57 and #34 are also related) seem to be bugs around what order certain operations run in. Ideally, we want all of our setup to happen before any user task starts. Similarly, all our teardown should occur after all user tasks complete. To do this well, we need to integrate with the OS's init service. This is because the init service:

  • knows when the first session of a user begins
  • knows when the last session ends
  • allows us to run fscrypt commands at specific points to prevent things from breaking

eCryptfs has been running into issues with systemd and automatic mounting via PAM modules. See: Arch Bug, Debian Bug, Ubuntu Bug, systemd Bug. pam_mount.so, which can do similar stuff for device encryption, has also had issues.

Details

This change would require changing or adding three things.

  • Remove functionality from pam_fscrypt.so

    • Remove session hooks, keyring linking code, caches code, etc..
    • For auth, if the user has a login protector, use the passphrase to unlock the login protector, and store it in the root user keyring (with type user). We could also prompt the user here to enter their old login password if things have gotten out of sync.
    • For password, if we find a login protector, use the passphrase hashes of AUTHTOK and OLDAUTHTOK to update the login protector to update the login protector
  • Add new commands to fscrypt that must be run as root:

    • fscrypt system start <uid> --unlock=[none|login] [--link-to-root]
      • --unlock adds policies to the user's keyring
        • login gets the login protector key from the root user keyring and uses it to unlock the corresponding policies
      • --link-root-keyring makes sure the user keyring is linked into the root user keyring
    • fscrypt system stop <uid> --lock=[none|login|all] [--drop-caches] [--unlink-from-root]
      • --lock removes policies from the user's keyring:
        • login removes just those protected by the login passphrase
        • all removes all policies in the user's keyring
      • --drop-caches calls DropFilesystemCache after all keys are removed
      • --unlink-from-root removes the user keyring from the root user keyring
  • Add a systemd service to trigger the appropriate commands. For example:

[Unit]
Description=Unlock fscrypt directories with login credentials
Before=user@%i.service

[Service]
Type=oneshot
RemainAfterExit=yes
Slice=user-%i.slice
RemainAfterExit=yes
StandardOutput=jouranal
StandardError=syslog
SyslogIdentifier=fscrypt
ExecStart=/usr/bin/env fscrypt system start %i --unlock=login --link-to-root
ExecStop=/usr/bin/env fscrypt system stop %i --lock=all --drop-caches --unlink-from-root

[Install]
WantedBy=user@%i.service

Benefits

  • Way less PAM code to maintain
    • No more tracking the number of open user sessions
    • No locking/unlocking of directories
    • No keyrings or caching related code
    • Could replace how we currently check the user password eliminating our custom conversation function
    • Keeps the Pluggable Authentication Module focused on authentication related stuff.
  • More stability
    • Should load before a lot of login jobs, preventing bugs with SDDM/GDM
    • Better setup to handle user session failures (right now we just never trigger cleanup)
    • Will eliminate issues with user processes keeping files open (mentioned here).
  • Allows for more complex configuration than the PAM config files
  • Allows behavior in [question] user keyring still linked to root keyring after fscrypt purge command #60 to be easily configurable
  • No ties to systemd in particular. Any init service can call fscrypt system [start|stop] <uid>.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions