diff mbox series

[v3,6/8] common: add AUTOBOOT_FLUSH_STDIN option

Message ID 20210621202352.1250303-7-jaeckel-floss@eyet-services.de
State Superseded
Delegated to: Tom Rini
Headers show
Series common: Introduce crypt-style password support | expand

Commit Message

Steffen Jaeckel June 21, 2021, 8:23 p.m. UTC
The key-sequence based unlock mechanisms are sensitive to junk symbols
that could have been sent to stdin and are still waiting to be retrieved.
Enabling this option will read all symbols off stdin before displaying the
autoboot prompt (and starting to read the password from stdin).

Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
---

(no changes since v1)

 common/Kconfig.boot |  9 +++++++++
 common/autoboot.c   | 11 +++++++++++
 2 files changed, 20 insertions(+)

Comments

Simon Glass June 26, 2021, 6:32 p.m. UTC | #1
On Mon, 21 Jun 2021 at 14:24, Steffen Jaeckel
<jaeckel-floss@eyet-services.de> wrote:
>
> The key-sequence based unlock mechanisms are sensitive to junk symbols
> that could have been sent to stdin and are still waiting to be retrieved.
> Enabling this option will read all symbols off stdin before displaying the
> autoboot prompt (and starting to read the password from stdin).
>
> Signed-off-by: Steffen Jaeckel <jaeckel-floss@eyet-services.de>
> ---
>
> (no changes since v1)
>
>  common/Kconfig.boot |  9 +++++++++
>  common/autoboot.c   | 11 +++++++++++
>  2 files changed, 20 insertions(+)

Reviewed-by: Simon Glass <sjg@chromium.org>
diff mbox series

Patch

diff --git a/common/Kconfig.boot b/common/Kconfig.boot
index e70dcf17cb..d19bc32836 100644
--- a/common/Kconfig.boot
+++ b/common/Kconfig.boot
@@ -790,6 +790,15 @@  config AUTOBOOT_KEYED
 	  U-Boot automatic booting process and bring the device
 	  to the U-Boot prompt for user input.
 
+config AUTOBOOT_FLUSH_STDIN
+	bool "Enable flushing stdin before starting to read the password"
+	depends on AUTOBOOT_KEYED && !SANDBOX
+	help
+	  When this option is enabled stdin buffer will be flushed before
+	  starting to read the password.
+	  This can't be enabled for the sandbox as flushing stdin would
+	  break the autoboot unit tests.
+
 config AUTOBOOT_PROMPT
 	string "Autoboot stop prompt"
 	depends on AUTOBOOT_KEYED
diff --git a/common/autoboot.c b/common/autoboot.c
index 50ab9281e7..1eeabf0b1a 100644
--- a/common/autoboot.c
+++ b/common/autoboot.c
@@ -296,6 +296,15 @@  static int passwd_abort_key(uint64_t etime)
 	return abort;
 }
 
+/**
+ * flush_stdin() - drops all pending characters from stdin
+ */
+static void flush_stdin(void)
+{
+	while (tstc())
+		(void)getchar();
+}
+
 /***************************************************************************
  * Watch for 'delay' seconds for autoboot stop or autoboot delay string.
  * returns: 0 -  no key string, allow autoboot 1 - got key string, abort
@@ -305,6 +314,8 @@  static int abortboot_key_sequence(int bootdelay)
 	int abort;
 	uint64_t etime = endtick(bootdelay);
 
+	if (IS_ENABLED(CONFIG_AUTOBOOT_FLUSH_STDIN))
+		flush_stdin();
 #  ifdef CONFIG_AUTOBOOT_PROMPT
 	/*
 	 * CONFIG_AUTOBOOT_PROMPT includes the %d for all boards.