From 6c770da67a14833e5202898360167c33d0b7eb18 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 28 Feb 2026 14:19:12 +0100 Subject: Use constant-time string comparision for password hash checking MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- asroot.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'asroot.c') diff --git a/asroot.c b/asroot.c index b15ea46..28b7014 100644 --- a/asroot.c +++ b/asroot.c @@ -128,6 +128,17 @@ read_passphrase(int fd) #endif +static int +consttime_streq(const char *a, const char *b) +{ + size_t i; + int ret = 1; + for (i = 0; a[i] && b[i]; i++) + ret &= a[i] == b[i]; + return ret & !a[i] && !b[i]; +} + + static void check_password(void) { @@ -257,7 +268,7 @@ again: #endif free(passphrase); - if (strcmp(got, expected)) { + if (!consttime_streq(got, expected)) { fprintf(stderr, "%s: incorrect password, please try again\n", argv0); #if RETRY_SLEEP > 0 tcsetattr(fd, TCSAFLUSH, &stty_sleep); -- cgit v1.2.3-70-g09d2