[PATCH] ssh-keygen: don't fclose(stdin) when a KRL spec is read from '-'

Muhammad Bilal meatuni001 at gmail.com
Fri Aug 28 08:08:46 AEST 2026


update_krl_from_file() (ssh-keygen.c) accepts '-' as a KRL spec
filename to mean 'read from stdin'. When it does, it replaces its
local 'path' variable with the string "(standard input)" (for
logging) before reading anything:

    if (strcmp(path, "-") == 0) {
        krl_spec = stdin;
        free(path);
        path = xstrdup("(standard input)");
    } ...

At the end of the function, the intended guard against closing
stdin was:

    if (strcmp(path, "-") != 0)
        fclose(krl_spec);

but by this point 'path' no longer holds "-" -- it was overwritten
above -- so the comparison is always true and fclose(krl_spec), i.e.
fclose(stdin), runs on exactly the input path it was meant to
protect.

This adds an explicit 'from_stdin' flag set at the point stdin is
selected, and checks that instead of re-deriving the same fact from
a string that has already been mutated.

Confirmed with strace -e trace=close: before this change, ssh-keygen
-k -u -f out.krl - (reading a KRL spec from stdin) shows an explicit
close(0) syscall; after this change it does not. -Q -l output on the
resulting KRL is unaffected in either case.
---
 ssh-keygen.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/ssh-keygen.c b/ssh-keygen.c
index d19c491dc..2fd5e12f2 100644
--- a/ssh-keygen.c
+++ b/ssh-keygen.c
@@ -2258,10 +2258,12 @@ update_krl_from_file(struct passwd *pw, const char *file, int wild_ca,
 	size_t blen = 0, linesize = 0;
 	unsigned long long serial, serial2;
 	int i, was_explicit_key, was_sha1, was_sha256, was_hash, r;
+	int from_stdin = 0;
 	FILE *krl_spec;
 
 	path = tilde_expand_filename(file, pw->pw_uid);
 	if (strcmp(path, "-") == 0) {
+		from_stdin = 1;
 		krl_spec = stdin;
 		free(path);
 		path = xstrdup("(standard input)");
@@ -2398,7 +2400,7 @@ update_krl_from_file(struct passwd *pw, const char *file, int wild_ca,
 			sshkey_free(key);
 		}
 	}
-	if (strcmp(path, "-") != 0)
+	if (!from_stdin)
 		fclose(krl_spec);
 	free(line);
 	free(path);
-- 
2.43.0



More information about the openssh-unix-dev mailing list