fix the tty prompt: restore echo, keep queued input

The old prompt saved termios, disabled ECHO, then re-read termios into the
same struct to restore it - which restored the no-echo state. Because termios
belongs to the terminal, not the fd, the user's shell was left without echo.
TCSAFLUSH also discarded a passphrase that was already queued in the tty.

Save the original state once, restore that copy, and use TCSANOW. Two
script(1) cases now cover the prompt end to end and go red on the old code.
This commit is contained in:
lotherk committed 2026-11-21 16:16:33 +00:00
1 parent 8731e66501
commit d471ec1723
3 files changed
+37 -8

No files matched your search

+8 -2
View File
@@ -11,8 +11,9 @@ those is a design decision, not a commit.
make check # tests/run.sh, generates keys in a tmpdir, no network
make install PREFIX=$HOME/.local # installs bin/sshcrypt
`make check` is the gate: 20 end-to-end cases. It needs `ssh-keygen`, `cmp` and
`python3` (one tamper test). After touching parsing or the envelope, also run
`make check` is the gate: 22 end-to-end cases. It needs `ssh-keygen`, `cmp`,
`python3` (one tamper test) and `script(1)` (two pty cases, skipped if absent).
No network. After touching parsing or the envelope, also run
make clean && make CFLAGS="-O1 -g -fsanitize=address,undefined -fno-omit-frame-pointer" LDFLAGS="-fsanitize=address,undefined" && make check
@@ -50,6 +51,11 @@ you pass; do not remove that `override`.
`keys.c` is the standard `u = (1+y)/(1-y)` map, cross-checked against
libcrypto X25519 in `tests/run.sh`. Do not replace it with an assumption.
* `memmem` is GNU, not POSIX. Use `find_bytes()` in `keys.c`.
* Terminal echo: save termios *before* clearing `ECHO` and restore that saved
copy. Re-reading with `tcgetattr` after disabling restores no-echo, and
termios outlives the fd, so the user's shell is left mute. Use `TCSANOW`,
not `TCSAFLUSH`: the flush discards a passphrase that is already queued in
the tty. Both are covered by the `script(1)` cases in `make check`.
* Every `.c` includes its own headers; missing `<stdio.h>` is a hard error under
`-std=c99 -Wpedantic`.
+13 -6
View File
@@ -33,31 +33,38 @@ static const char *passphrase_env(void)
/* Read a line from /dev/tty with echo off. Never from stdin: that is data. */
static char *prompt_passphrase(const char *path)
{
struct termios old, noecho;
struct termios saved, noecho;
char *line = NULL;
size_t cap = 0;
ssize_t n;
FILE *tty;
int have_termios;
int fd = open("/dev/tty", O_RDWR);
if (fd < 0)
return NULL;
if (tcgetattr(fd, &old) == 0) {
noecho = old;
have_termios = tcgetattr(fd, &saved) == 0;
if (have_termios) {
noecho = saved;
noecho.c_lflag &= ~(tcflag_t)ECHO;
tcsetattr(fd, TCSAFLUSH, &noecho);
/* TCSANOW, not TCSAFLUSH: do not throw away a passphrase that is
* already queued in the tty (piped or pre-typed input). */
(void)tcsetattr(fd, TCSANOW, &noecho);
}
fprintf(stderr, "passphrase for %s: ", path);
fflush(stderr);
tty = fdopen(fd, "r");
if (!tty) {
if (have_termios)
(void)tcsetattr(fd, TCSANOW, &saved);
close(fd);
return NULL;
}
n = getline(&line, &cap, tty);
fputc('\n', stderr);
if (tcgetattr(fd, &old) == 0)
tcsetattr(fd, TCSAFLUSH, &old);
/* restore the saved state, not a fresh read of the no-echo state */
if (have_termios)
(void)tcsetattr(fd, TCSANOW, &saved);
fclose(tty);
if (n <= 0) {
free(line);
+16
View File
@@ -103,6 +103,22 @@ SSHCRYPT_PASSPHRASE=hunter2 "$BIN" -r "$T/pw.pub" -i "$T/msg" -o "$T/pw.asc"
SSHCRYPT_PASSPHRASE=hunter2 "$BIN" -d -k "$T/pw" -i "$T/pw.asc" -o "$T/pw.out"
check "passphrase-protected key round trip" 'cmp -s "$T/msg" "$T/pw.out"'
# --- the interactive prompt, on a real pty ------------------------------------
if command -v script >/dev/null 2>&1; then
printf 'hunter2\n' | script -qec \
"\"$BIN\" -d -k \"$T/pw\" -i \"$T/pw.asc\" -o \"$T/pty.out\"" \
/dev/null >/dev/null
check "passphrase typed on the tty" 'cmp -s "$T/msg" "$T/pty.out"'
out=$(printf 'hunter2\n' | script -qec \
"\"$BIN\" -d -k \"$T/pw\" -i \"$T/pw.asc\" -o /dev/null; stty -a" \
/dev/null)
check "echo is restored after the prompt" \
'printf "%s\n" "$out" | tr " " "\n" | grep -qx echo'
else
printf 'skip: script(1) missing, the tty prompt was not tested\n'
fi
"$BIN" -r "$T/ed" -i "$T/msg" -o "$T/viapriv.asc"
"$BIN" -d -k "$T/ed" -i "$T/viapriv.asc" -o "$T/viapriv.out"
check "encrypting to a private-key path works" 'cmp -s "$T/msg" "$T/viapriv.out"'