From 7381e5937209178fa94694888d6a721ab0d5a124 Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Sat, 7 Nov 2020 20:30:51 -0800 Subject: [PATCH] cmd/fscrypt: fix race condition in getPassphraseKey() Set the terminal to raw mode *before* printing the prompt. Otherwise the user (or the automated test) might enter the passphrase before the terminal gets put into raw mode. This is needed for some of the CLI tests to pass reliably in Travis CI. --- cmd/fscrypt/keys.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/cmd/fscrypt/keys.go b/cmd/fscrypt/keys.go index 77e3900..33461ec 100644 --- a/cmd/fscrypt/keys.go +++ b/cmd/fscrypt/keys.go @@ -86,9 +86,6 @@ func (p passphraseReader) Read(buf []byte) (int, error) { // passphrase into a key. If we are not reading from a terminal, just read into // the passphrase into the key normally. func getPassphraseKey(prompt string) (*crypto.Key, error) { - if !quietFlag.Value { - fmt.Print(prompt) - } // Only disable echo if stdin is actually a terminal. if terminal.IsTerminal(stdinFd) { @@ -102,6 +99,10 @@ func getPassphraseKey(prompt string) (*crypto.Key, error) { }() } + if !quietFlag.Value { + fmt.Print(prompt) + } + return crypto.NewKeyFromReader(passphraseReader{}) } -- 2.39.5