Compare commits

..
1 Commits
Author SHA1 Message Date
sneak 6f2538cfd2 Wipe memguard buffers on every exit, restore echo on Ctrl-C (closes #35)
check / check (push) Successful in 1m4s
Entry() now returns the exit code and only main calls os.Exit, so the
deferred memguard.Purge() in Entry() runs on success and on error;
before, os.Exit(1) skipped every deferred Destroy().

SIGINT and SIGTERM go through memguard's handler, which wipes every
buffer and exits with status 1. The passphrase prompt turns terminal
echo off until its read returns, and the handler exits before that, so
on Ctrl-C the handler first restores the terminal settings saved at
startup. It leaves the terminal alone on SIGTERM, which can reach a
background process, and changing the terminal from the background would
stop the process.

Model: opus-5-5
2026-10-03 12:15:47 +00:00
2 changed files with 12 additions and 14 deletions
+3 -4
View File
@@ -28,10 +28,9 @@ Bring the repo into policy compliance in one commit:
- 2026-10-03: Key material is wiped on every exit: `Entry()` returns - 2026-10-03: Key material is wiped on every exit: `Entry()` returns
the exit code after its deferred `memguard.Purge()` has run, and only the exit code after its deferred `memguard.Purge()` has run, and only
`main` calls `os.Exit`. SIGINT and SIGTERM go through memguard's `main` calls `os.Exit`. SIGINT and SIGTERM go through memguard's
handler, which wipes every buffer before exiting; when the process is handler, which wipes every buffer before exiting; on Ctrl-C it first
in the terminal's foreground process group it first restores the restores the terminal settings from startup, so an interrupted
terminal settings from startup, so an interrupted passphrase prompt no passphrase prompt no longer leaves echo off.
longer leaves echo off.
- 2026-10-02: A plain `docker build .` builds again: the size tests - 2026-10-02: A plain `docker build .` builds again: the size tests
skip a case that needs more locked memory than the process can skip a case that needs more locked memory than the process can
lock, and run every case under `script/cibuild`. The image stamps the lock, and run every case under `script/cibuild`. The image stamps the
+9 -10
View File
@@ -2,11 +2,11 @@ package cli
import ( import (
"os" "os"
"syscall"
"git.eeqj.de/sneak/secret/internal/secret" "git.eeqj.de/sneak/secret/internal/secret"
"github.com/awnumar/memguard" "github.com/awnumar/memguard"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"golang.org/x/sys/unix"
"golang.org/x/term" "golang.org/x/term"
) )
@@ -16,17 +16,16 @@ import (
func Entry() int { func Entry() int {
// On SIGINT or SIGTERM memguard runs this function, wipes every buffer // On SIGINT or SIGTERM memguard runs this function, wipes every buffer
// and exits with status 1. The passphrase prompt turns terminal echo // and exits with status 1. The passphrase prompt turns terminal echo
// off until the read finishes, so a signal there would leave echo off. // off until the read finishes, so Ctrl-C there would leave echo off.
// Only a process in the terminal's foreground process group may reset // Ctrl-C means this process is in the terminal's foreground and may
// it: one in the background that tries is stopped instead of exiting. // reset it; doing that from the background would stop the process.
terminalState, terminalErr := term.GetState(unix.Stdin) terminalState, terminalErr := term.GetState(syscall.Stdin)
memguard.CatchSignal(func(os.Signal) { memguard.CatchSignal(func(sig os.Signal) {
foreground, err := unix.IoctlGetInt(unix.Stdin, unix.TIOCGPGRP) if sig == os.Interrupt && terminalErr == nil {
if terminalErr == nil && err == nil && foreground == unix.Getpgrp() { _ = term.Restore(syscall.Stdin, terminalState)
_ = term.Restore(unix.Stdin, terminalState)
} }
}, os.Interrupt, unix.SIGTERM) }, os.Interrupt, syscall.SIGTERM)
defer memguard.Purge() defer memguard.Purge()