unlink the converted key when the conversion is interrupted

SIGINT at the passphrase prompt used to leave the 0600 temp file in /dev/shm
until reboot. A handler for INT, TERM and HUP removes it and exits. The test
interrupts a real pty session and checks the temp directory.
This commit is contained in:
lotherk committed 2026-11-21 16:19:34 +00:00
1 parent d471ec1723
commit 5eaa930520
3 files changed
+78 -5

No files matched your search

+10 -5
View File
@@ -11,9 +11,10 @@ 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: 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 check` is the gate: 23 end-to-end cases. It needs `ssh-keygen`, `cmp`,
`python3` (tamper test), `script(1)` (pty cases) and `pgrep` (SIGINT case);
those last ones skip themselves if the tool is missing. 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
@@ -70,8 +71,12 @@ before key derivation: wrong keys must exit 3 and name both fingerprints.
* Exit codes are interface: 0 ok, 1 usage/I-O, 2 malformed input, 3 wrong key or
tampered message.
* Never put plaintext, key material or passphrases in argv, logs or temp files.
Prompt on `/dev/tty` with echo off. Zero secrets with `scr_secure_zero`.
* Never put plaintext, key material or passphrases in argv or logs. The one
temp file, the ssh-keygen conversion, is `0600` on tmpfs, is unlinked as soon
as it has been read, and SIGINT/SIGTERM/SIGHUP unlink it too. Any new path
that writes key material must do the same.
* Prompt on `/dev/tty` with echo off (see the termios trap). Zero secrets with
`scr_secure_zero`.
* Encryption must stay subprocess-free; only OpenSSH private keys may call
`ssh-keygen`.
* Input is buffered whole, capped at 1 GiB. Do not silently raise the cap.
+33
View File
@@ -2,6 +2,7 @@
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -23,6 +24,34 @@ extern char *scr_self_exe;
static char *g_pass;
static const char *g_pass_path;
/* A converted key briefly lives in a temp file. If the user interrupts the
* conversion, remove it instead of leaving key material on disk. */
static char g_tmp_path[512];
static volatile sig_atomic_t g_tmp_active;
static void tmp_signal(int sig)
{
if (g_tmp_active)
(void)unlink(g_tmp_path);
_exit(128 + sig);
}
static void tmp_signals_install(void)
{
static int done;
struct sigaction sa;
if (done)
return;
done = 1;
memset(&sa, 0, sizeof sa);
sa.sa_handler = tmp_signal;
sigemptyset(&sa.sa_mask);
(void)sigaction(SIGINT, &sa, NULL);
(void)sigaction(SIGTERM, &sa, NULL);
(void)sigaction(SIGHUP, &sa, NULL);
}
static const char *passphrase_env(void)
{
const char *p = getenv("SSHCRYPT_PASSPHRASE");
@@ -542,6 +571,9 @@ static char *convert_openssh_key(const char *path, const struct buf *raw)
(void)fchmod(fd, 0600);
scr_write_fd(fd, raw->data, raw->len);
close(fd);
snprintf(g_tmp_path, sizeof g_tmp_path, "%s", tmp);
g_tmp_active = 1;
tmp_signals_install();
if (!env && run_ssh_keygen(tmp, "") == 0)
return tmp;
@@ -595,6 +627,7 @@ int scr_priv_load(const char *path, struct scr_priv *out)
}
if (tmp) {
unlink(tmp);
g_tmp_active = 0;
free(tmp);
}
if (!pkey)
+35
View File
@@ -115,6 +115,41 @@ if command -v script >/dev/null 2>&1; then
/dev/null)
check "echo is restored after the prompt" \
'printf "%s\n" "$out" | tr " " "\n" | grep -qx echo'
# Interrupting the conversion must not leave key material behind.
if command -v pgrep >/dev/null 2>&1; then
tmpd=/dev/shm
[ -w "$tmpd" ] || tmpd=${TMPDIR:-/tmp}
before=$(ls "$tmpd" 2>/dev/null | grep -c '^sshcrypt-' || true)
mkfifo "$T/ttyin"
exec 9<>"$T/ttyin"
script -qec \
"\"$BIN\" -d -k \"$T/pw\" -i \"$T/pw.asc\" -o \"$T/sig.out\"" \
/dev/null <&9 >"$T/sig.log" 2>&1 &
ptyjob=$!
ready=0
i=0
while [ "$i" -lt 200 ]; do
grep -q "passphrase for" "$T/sig.log" 2>/dev/null &&
{ ready=1; break; }
sleep 0.1
i=$((i + 1))
done
if [ "$ready" -eq 1 ]; then
pid=$(pgrep -x sshcrypt | head -1)
[ -n "$pid" ] && kill -INT "$pid" 2>/dev/null
wait "$ptyjob" 2>/dev/null || true
exec 9>&-
after=$(ls "$tmpd" 2>/dev/null |
grep -c '^sshcrypt-' || true)
check "SIGINT at the prompt leaves no temp key" \
'[ "$after" -eq "$before" ]'
else
printf 'skip: the tty prompt did not come up\n'
kill "$ptyjob" 2>/dev/null || true
exec 9>&-
fi
fi
else
printf 'skip: script(1) missing, the tty prompt was not tested\n'
fi