summaryrefslogtreecommitdiffstats
path: root/modules/slock/default.nix
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-12-04 01:56:12 +0000
committertv <tv@krebsco.de>2026-12-04 15:44:35 +0000
commitf269fdc2626228534c39292c288a63c19a4c779d (patch)
tree48d88407f3c1285b02b0233e8e7f4b1c491dcd23 /modules/slock/default.nix
parent6d2f40458c8ffd623f29ff6722cdcd9fc8c57415 (diff)
slock: password hash from enstate, else setuid
The hash no longer goes into the store. With enstate, it can come from the slot secrets/slock.hashed-password: then slock runs as its user, with that hash. Without a hash, the credential falls back to "none" and slock runs setuid root, reading /etc/shadow. A validator for text/x-crypt-hash checks the hash the way slock uses it, with crypt. The slot is optional, so enstate leaves out a hash the validator objects to, and slock runs setuid instead of locking with a hash no password matches.
Diffstat (limited to 'modules/slock/default.nix')
-rw-r--r--modules/slock/default.nix91
1 files changed, 91 insertions, 0 deletions
diff --git a/modules/slock/default.nix b/modules/slock/default.nix
new file mode 100644
index 00000000..4ac1a1eb
--- /dev/null
+++ b/modules/slock/default.nix
@@ -0,0 +1,91 @@
+# slock as a system service, so the OOM killer leaves it alone, started
+# by its user through polkit. With enstate, the password hash can come
+# from the slot secrets/slock.hashed-password: then slock runs as its
+# user, with that hash. Without a hash, slock runs setuid root and reads
+# /etc/shadow, like nixpkgs' programs.slock.
+{ config, lib, mylib, pkgs, ... }: let
+ cfg = config.hrm.slock;
+
+ slot = config.enstate.channels.secrets.slots."slock.hashed-password";
+
+ preload = pkgs.runCommandCC "slock-preload.so" {} /* sh */ ''
+ gcc -Wall -Wextra -Werror -shared -fPIC -o $out ${./preload.c}
+ '';
+
+ validate = pkgs.runCommandCC "slock-validate" {
+ buildInputs = [ pkgs.libxcrypt ];
+ } /* sh */ ''
+ gcc -Wall -Wextra -Werror -o $out ${./validate.c} -lcrypt
+ '';
+in {
+ options.hrm.slock = {
+ enable = lib.mkEnableOption "hrm.slock";
+ package = lib.mkOption {
+ default = pkgs.writeDashBin "slock" ''
+ set -efu
+ display=''${DISPLAY#:}
+ service=slock-$LOGNAME@$display.service
+ exec ${pkgs.systemd}/bin/systemctl start "$service"
+ '';
+ type = lib.types.package;
+ };
+ user = lib.mkOption {
+ type = mylib.types.user;
+ };
+ };
+ config = lib.mkIf cfg.enable (lib.mkMerge [
+ {
+ security.polkit.extraConfig = /* js */ ''
+ polkit.addRule(function(action, subject) {
+ if (action.id === "org.freedesktop.systemd1.manage-units" &&
+ subject.user === ${builtins.toJSON cfg.user.name} &&
+ /^slock-${cfg.user.name}@[0-9]+\.service$/.test(action.lookup("unit")) ) {
+ return polkit.Result.YES;
+ }
+ });
+ '';
+ security.wrappers.slock = {
+ setuid = true;
+ owner = "root";
+ group = "root";
+ source = "${pkgs.slock}/bin/slock";
+ };
+ systemd.services."slock-${cfg.user.name}@" = {
+ environment = {
+ DISPLAY = ":%I";
+ };
+ restartIfChanged = false;
+ serviceConfig = {
+ ExecStart = pkgs.writeDash "slock-start" ''
+ if test "$(${pkgs.coreutils}/bin/cat "$CREDENTIALS_DIRECTORY/hashed-password")" != none; then
+ LD_PRELOAD=${preload} exec ${pkgs.slock}/bin/slock
+ fi
+ # Setuid slock opens the display as root.
+ ${pkgs.xhost}/bin/xhost +SI:localuser:root >/dev/null
+ exec /run/wrappers/bin/slock
+ '';
+ # The fallback when there's no hash: LoadCredential comes first.
+ SetCredential = [ "hashed-password:none" ];
+ OOMScoreAdjust = -1000;
+ Restart = "on-failure";
+ RestartSec = "100ms";
+ StartLimitBurst = 0;
+ SyslogIdentifier = "slock";
+ User = cfg.user.name;
+ };
+ };
+ }
+ (lib.mkIf config.enstate.enable {
+ # A hash the validator objects to is left out, so slock runs setuid.
+ enstate.types."text/x-crypt-hash".validator = validate;
+ enstate.channels.secrets.slots."slock.hashed-password" = {
+ type = "text/x-crypt-hash";
+ required = false;
+ secret = true;
+ };
+ systemd.services."slock-${cfg.user.name}@".serviceConfig.LoadCredential = [
+ "hashed-password:${slot.path}"
+ ];
+ })
+ ]);
+}