diff options
| -rw-r--r-- | modules/slock.nix | 72 | ||||
| -rw-r--r-- | modules/slock/default.nix | 91 | ||||
| -rw-r--r-- | modules/slock/preload.c | 41 | ||||
| -rw-r--r-- | modules/slock/validate.c | 82 |
4 files changed, 214 insertions, 72 deletions
diff --git a/modules/slock.nix b/modules/slock.nix deleted file mode 100644 index 14513347..00000000 --- a/modules/slock.nix +++ /dev/null @@ -1,72 +0,0 @@ -{ config, lib, mylib, pkgs, ... }: let - cfg = config.hrm.slock; -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 { - 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; - } - }); - ''; - systemd.services."slock-${cfg.user.name}@" = { - environment = { - DISPLAY = ":%I"; - LD_PRELOAD = pkgs.runCommandCC "slock-${cfg.user.name}.so" { - passAsFile = ["text"]; - text = /* c */ '' - #include <shadow.h> - #include <unistd.h> - - static struct spwd entry = { - .sp_namp = "", - .sp_pwdp = - ${mylib.toC config.users.users.${cfg.user.name}.hashedPassword}, - .sp_lstchg = 0, - .sp_min = 0, - .sp_max = 0, - .sp_warn = 0, - .sp_inact = 0, - .sp_expire = 0, - .sp_flag = 0, - }; - - extern struct spwd *getspnam(const char *name) { return &entry; } - extern int setgroups(size_t size, const gid_t *list) { return 0; } - extern int setgid(gid_t gid) { return 0; } - extern int setuid(uid_t uid) { return 0; } - ''; - } /* sh */ '' - gcc -Wall -shared -o $out -xc "$textPath" - ''; - }; - restartIfChanged = false; - serviceConfig = { - ExecStart = "${pkgs.slock}/bin/slock"; - OOMScoreAdjust = -1000; - Restart = "on-failure"; - RestartSec = "100ms"; - StartLimitBurst = 0; - SyslogIdentifier = "slock"; - User = cfg.user.name; - }; - }; - }; -} 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}" + ]; + }) + ]); +} diff --git a/modules/slock/preload.c b/modules/slock/preload.c new file mode 100644 index 00000000..0b7e81d4 --- /dev/null +++ b/modules/slock/preload.c @@ -0,0 +1,41 @@ +// Preloaded into slock, which then runs as its user instead of root: +// getspnam returns the password hash from the credential hashed-password, +// and dropping privileges does nothing. + +#include <shadow.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +static char hash[4096]; + +static struct spwd entry = { + .sp_namp = "", + .sp_pwdp = hash, +}; + +struct spwd *getspnam(const char *name) { + (void) name; + const char *dir = getenv("CREDENTIALS_DIRECTORY"); + char path[4096]; + if (dir == NULL || + snprintf(path, sizeof path, "%s/hashed-password", dir) >= (int) sizeof path) { + return NULL; + } + FILE *f = fopen(path, "r"); + if (f == NULL) { + return NULL; + } + char *line = fgets(hash, sizeof hash, f); + fclose(f); + if (line == NULL) { + return NULL; + } + hash[strcspn(hash, "\n")] = '\0'; + return &entry; +} + +int setgroups(size_t size, const gid_t *list) { (void) size; (void) list; return 0; } +int setgid(gid_t gid) { (void) gid; return 0; } +int setuid(uid_t uid) { (void) uid; return 0; } diff --git a/modules/slock/validate.c b/modules/slock/validate.c new file mode 100644 index 00000000..aabec058 --- /dev/null +++ b/modules/slock/validate.c @@ -0,0 +1,82 @@ +// enstate's validator for text/x-crypt-hash (enstate's docs/design.md, +// 5.2): objects to each slot under IN/channels that preload.c couldn't +// use, i.e. that isn't one line of at most 4095 bytes that crypt accepts +// as a setting. +// +// usage: validate IN OUT + +#include <crypt.h> +#include <dirent.h> +#include <stdio.h> +#include <string.h> + +static void object(const char *channel, const char *slot, const char *message) { + printf("{\"address\": \"%s/%s\", \"message\": \"%s\"}\n", channel, slot, message); +} + +static void check(const char *path, const char *channel, const char *slot) { + char hash[4096]; + FILE *f = fopen(path, "r"); + if (f == NULL) { + object(channel, slot, "cannot be read"); + return; + } + size_t n = fread(hash, 1, sizeof hash, f); + fclose(f); + if (n == sizeof hash) { + object(channel, slot, "is longer than 4095 bytes"); + return; + } + if (n > 0 && hash[n - 1] == '\n') { + n--; + } + hash[n] = '\0'; + if (n == 0) { + object(channel, slot, "is empty"); + return; + } + if (strlen(hash) != n || strchr(hash, '\n') != NULL) { + object(channel, slot, "is not one line"); + return; + } + const char *result = crypt("", hash); + if (result == NULL || result[0] == '*' || strlen(result) != n) { + object(channel, slot, "is not a password hash that crypt accepts"); + } +} + +int main(int argc, char **argv) { + if (argc != 3) { + fprintf(stderr, "usage: validate IN OUT\n"); + return 1; + } + char path[4096]; + snprintf(path, sizeof path, "%s/channels", argv[1]); + DIR *channels = opendir(path); + if (channels == NULL) { + return 0; + } + struct dirent *channel; + while ((channel = readdir(channels)) != NULL) { + if (channel->d_name[0] == '.') { + continue; + } + snprintf(path, sizeof path, "%s/channels/%s", argv[1], channel->d_name); + DIR *slots = opendir(path); + if (slots == NULL) { + continue; + } + struct dirent *slot; + while ((slot = readdir(slots)) != NULL) { + if (slot->d_name[0] == '.') { + continue; + } + snprintf(path, sizeof path, "%s/channels/%s/%s", + argv[1], channel->d_name, slot->d_name); + check(path, channel->d_name, slot->d_name); + } + closedir(slots); + } + closedir(channels); + return 0; +} |
