summaryrefslogtreecommitdiffstats
path: root/modules/slock/preload.c
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/preload.c
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/preload.c')
-rw-r--r--modules/slock/preload.c41
1 files changed, 41 insertions, 0 deletions
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; }