Skip to contentSkip to conversation

kern/efi: Adding efi-watchdog command

PATCH Superseded Erwan Velu 3 participants 7 messages Aug 30, 2021 – Sep 2, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Erwan Velu

author
#0 lore raw

PATCH 1/1 kern/efi: Adding efi-watchdog command

This patch got written by Arthur Mesh from Juniper (now at Apple Sec team). It was extracted from https://lists.gnu.org/archive/html/grub-devel/2015-09/msg00065.html

Since this email, the this patch was :
- rebased against the current tree
- added a default timeout value

This commit adds a new command efi-watchdog to manage efi watchdogs.

On server platforms, this allow grub to reset the host automatically if it wasn't able to succeed at booting in a given timeframe. This usually covers the following issues :
- net boot is too slow and never ends
- grub is unable to find a proper configuration and fails

If EFI_WATCHDOG_MINUTES is set a compile time, this enable the watchdog behavior at the early init of GRUB meaning that even if grub is not able to load its configuration file, the watchdog will be triggered automatically.

Please note that watchdog only covers GRUB itself.
Additional hardware watchdog are required if some want to protect the early operating system loading phase.

By default grub disable the watchdog and so this patch. Therefore, this commit have no impact on grub's behavior.

This patch is used in production for month on a 50K server platform with success.

---
 docs/grub.texi            | 15 ++++++++++
 grub-core/kern/efi/init.c | 63 ++++++++++++++++++++++++++++++++++++++-
 include/grub/efi/efi.h    |  2 ++
 3 files changed, 79 insertions(+), 1 deletion(-)
docs/grub.texi +15 -0
index f8b4b3b21a7f..b52161a19b3b 100644
@@ -3991,6 +3991,7 @@ you forget a command, you can run the command @command{help}
 * distrust::                    Remove a pubkey from trusted keys
 * drivemap::                    Map a drive to another
 * echo::                        Display a line of text
+* efi-watchdog::                Manipulate EFI watchdog
 * eval::                        Evaluate agruments as GRUB commands
 * export::                      Export an environment variable
 * false::                       Do nothing, unsuccessfully
@@ -4442,6 +4443,20 @@ When interpreting backslash escapes, backslash followed by any other
 character will print that character.
 @end deffn
 
+@node efi-watchdog
+@subsection efi-watchdog
+
+@deffn Command efi-watchdog @option{enable}|@option{disable} code timeout
+Enable or disable the system's watchdog timer.
+Only available in EFI targeted GRUB.
+
+If @option{enable} is used, the @var{code} is logged upon
+watchdog timeout event. The UEFI BIOS reserves codes 0x0000 to 0xFFFF.
+The @var{timeout} represents number of seconds to set the watchdog timeout to.
+When the watchdog exceed the timeout, the system is reset.
+
+if @option{disable} is used, the EFI watchdog is disarmed.
+@end deffn
 
 @node eval
 @subsection eval
grub-core/kern/efi/init.c +62 -1

88 lines not on this page · raw message

include/grub/efi/efi.h +2 -0
index 83d958f9945e..372f995b74f4 100644
@@ -124,4 +124,6 @@ struct grub_net_card;
 grub_efi_handle_t
 grub_efinet_get_device_handle (struct grub_net_card *card);
 
+/* EFI Watchdog armed by grub, in minutes */
+#define EFI_WATCHDOG_MINUTES 0
 #endif /* ! GRUB_EFI_EFI_HEADER */
Signature · 1 line
2.25.1
#1 lore raw
Quoted 10 lines · Erwan Velu

This patch got written by Arthur Mesh from Juniper (now at Apple Sec team). It was extracted from https://lists.gnu.org/archive/html/grub-devel/2015-09/msg00065.html

Since this email, the this patch was :
- rebased against the current tree
- added a default timeout value

This commit adds a new command efi-watchdog to manage efi watchdogs.

On server platforms, this allow grub to reset the host automatically

s/allow grub/allows the GRUB/g

Quoted 4 lines

if it wasn't able to succeed at booting in a given timeframe. This usually covers the following issues :
- net boot is too slow and never ends
- grub is unable to find a proper configuration and fails

s/grub/the GRUB/

Quoted 4 lines

If EFI_WATCHDOG_MINUTES is set a compile time, this enable the watchdog behavior at the early init of GRUB meaning that even if grub is not able to load its configuration file, the watchdog will be triggered automatically.

This should not happen at compile time. Please take a look at commit 968de8c23 (shim_lock: Only skip loading shim_lock verifier with explicit consent). It is good example how to do such things during the GRUB image generation. Though it does not solve a problem how to enable watchdog early for UEFI Secure Boot signed images...

Quoted 3 lines

Please note that watchdog only covers GRUB itself.
Additional hardware watchdog are required if some want to protect the early operating system loading phase.

Is watchdog disabled when ExitBootServices() is called?

Quoted 5 lines

By default grub disable the watchdog and so this patch. Therefore, this commit have no impact on grub's behavior.

This patch is used in production for month on a 50K server platform with success.

Here you should add Signed-off-by of original author.

Quoted 20 lines
---
 docs/grub.texi            | 15 ++++++++++
 grub-core/kern/efi/init.c | 63 ++++++++++++++++++++++++++++++++++++++-
 include/grub/efi/efi.h    |  2 ++
 3 files changed, 79 insertions(+), 1 deletion(-)
docs/grub.texi +1 -0
index f8b4b3b21a7f..b52161a19b3b 100644
@@ -3991,6 +3991,7 @@ you forget a command, you can run the command @command{help}
 * distrust::                    Remove a pubkey from trusted keys
 * drivemap::                    Map a drive to another
 * echo::                        Display a line of text
+* efi-watchdog::                Manipulate EFI watchdog
 * eval::                        Evaluate agruments as GRUB commands
 * export::                      Export an environment variable
 * false::                       Do nothing, unsuccessfully

docs/grub.texi When interpreting backslash escapes, backslash followed by any other · line 4449

Quoted 7 lines
docs/grub.texi +4 -0
@@ -4442,6 +4443,20 @@ When interpreting backslash escapes, backslash followed by any other
 character will print that character.
 @end deffn
 
+@node efi-watchdog
+@subsection efi-watchdog
+
+@deffn Command efi-watchdog @option{enable}|@option{disable} code timeout

Could you use -e|-d instead of enable/disable. Additionally, "code" and "timeout" are only valid for enable. So, I think you should put square brackets around them.

And it seems to me you should reverse order of "code" and "timeout".

Could not we assume defaults for "code" and "timeout" if they are not given? Or at least suggest defaults in the documentation?

docs/grub.texi When interpreting backslash escapes, backslash followed by any other · line 4454

Quoted 5 lines
docs/grub.texi +5 -0
+Enable or disable the system's watchdog timer.
+Only available in EFI targeted GRUB.
+
+If @option{enable} is used, the @var{code} is logged upon
+watchdog timeout event. The UEFI BIOS reserves codes 0x0000 to 0xFFFF.

s/UEFI BIOS/UEFI/

And "reserves" for what? Cannot you use them? Or should you use these codes because they are reserved for you? Anyway, this requires clarification. And it would be nice to know where these codes come from?

docs/grub.texi When interpreting backslash escapes, backslash followed by any other · line 4456

Quoted 2 lines
docs/grub.texi +2 -0
+The @var{timeout} represents number of seconds to set the watchdog timeout to.
+When the watchdog exceed the timeout, the system is reset.

Are there any limitations on timeout value? If yes they should be specified here.

Quoted 19 lines
docs/grub.texi +2 -0
+if @option{disable} is used, the EFI watchdog is disarmed.
+@end deffn
 
 @node eval
 @subsection eval
grub-core/kern/efi/init.c +2 -0
index 7facacf09c7b..c8cda3854ce7 100644
@@ -28,6 +28,8 @@
 #include <grub/mm.h>
 #include <grub/kernel.h>
 #include <grub/stack_protector.h>
+#include <grub/extcmd.h>
+#include <grub/command.h>
 
 #ifdef GRUB_STACK_PROTECTOR
 

grub-core/kern/efi/init.c stack_protector_init (void) · line 93

Quoted 10 lines
grub-core/kern/efi/init.c +7 -0
@@ -82,6 +84,60 @@ stack_protector_init (void)
 
 grub_addr_t grub_modbase;
 
+static grub_command_t cmd_list;
+
+static grub_err_t
+grub_cmd_efi_watchdog (grub_command_t cmd  __attribute__ ((unused)),
+                      int argc, char **args)
+{
+    long input;

s/long/unsigned long/

grub-core/kern/efi/init.c stack_protector_init (void) · line 96

Quoted 3 lines
grub-core/kern/efi/init.c +3 -0
+    grub_efi_status_t status;
+    grub_efi_uintn_t timeout;
+    grub_efi_uint64_t code;

Improper indention here and below. Please take a look at grub-core/kern/efi/sb.c. It contains good examples how code should be formatted.

grub-core/kern/efi/init.c stack_protector_init (void) · line 102

Quoted 4 lines
grub-core/kern/efi/init.c +4 -0
+    if (argc < 1)
+       return grub_error (GRUB_ERR_BAD_ARGUMENT,
+           N_("usage: efi-watchdog (enable|disable) <code> <timeout>"));
+    if (grub_strcasecmp (args[0], "enable") == 0) {

Would not be simpler to use extcmd interface instead here? Please take a look at grub-core/term/terminfo.c:grub_cmd_terminfo(). And now I think you should add "-c" option for code and "-t" for timeout. Then you even do not need "-d". The "-t 0" should disable watchdog instead.

grub-core/kern/efi/init.c stack_protector_init (void) · line 108

Quoted 5 lines
grub-core/kern/efi/init.c +5 -0
+       if (argc != 3)
+           return grub_error (GRUB_ERR_BAD_ARGUMENT,
+                              N_("usage: efi-watchdog enable <code> <timeout>"));
+
+       input = grub_strtol (args[1], 0, 0);

I think this should be grub_strtoul(). And please catch all errors from this function. It means you cannot ignore endptr value returned.

Additionally, IMO base should be explicitly set to 16 here.

grub-core/kern/efi/init.c stack_protector_init (void) · line 115

Quoted 6 lines
grub-core/kern/efi/init.c +6 -0
+       if (input >= 0) {
+           code = input;
+       } else {
+           return grub_error (GRUB_ERR_BAD_ARGUMENT,
+                              N_("<code> must be non-negative"));
+       }

Hmmm... Where do you parse and set timeout?

Quoted 28 lines
grub-core/kern/efi/init.c +24 -0
+    } else if (grub_strcasecmp (args[0], "disable") == 0) {
+
+       if (argc != 1)
+           return grub_error (GRUB_ERR_BAD_ARGUMENT,
+                              N_("usage: efi-watchdog disable"));
+       timeout = 0;
+       code = 0;
+
+    } else {
+       return grub_error (GRUB_ERR_BAD_ARGUMENT,
+           N_("usage: efi-watchdog (enable|disable) <code> <timeout>"));
+    }
+
+    status = efi_call_4 (grub_efi_system_table->boot_services->set_watchdog_timer,
+                        timeout, code, sizeof(L"GRUB"), L"GRUB");
+
+    if (status != GRUB_EFI_SUCCESS)
+       return grub_error (GRUB_ERR_BUG,
+                          N_("Unexpected UEFI SetWatchdogTimer() error"));
+    else
+       return GRUB_ERR_NONE;
+}
+
+
 void
 grub_efi_init (void)
 {

grub-core/kern/efi/init.c grub_efi_init (void) · line 166

Quoted 7 lines
grub-core/kern/efi/init.c +2 -1
@@ -105,10 +161,14 @@ grub_efi_init (void)
       grub_shim_lock_verifier_setup ();
     }
 
+  grub_printf("grub %s: Arming EFI watchdog at %d minutes\n", PACKAGE_VERSION, EFI_WATCHDOG_MINUTES);
   efi_call_4 (grub_efi_system_table->boot_services->set_watchdog_timer,
-	      0, 0, 0, NULL);
+              60*EFI_WATCHDOG_MINUTES, 0, sizeof(L"GRUB"), L"GRUB");

Why do you use minutes here if you use seconds above? I think you should use seconds everywhere.

Daniel

#2 lore raw

Le 01/09/2021 à 18:24, Daniel Kiper a écrit :

First of all, thanks for your extensive review, I'll fix all this in a V2.

[...]

Quoted 1 line

Here you should add Signed-off-by of original author.

Yeah, I was wondering that but the only email I have was from his previous company.

I have no idea what email I should use so its useful.

[....]

#3 lore raw
Quoted 3 lines · Erwan Velu

Le 01/09/2021 à 18:24, Daniel Kiper a écrit :

First of all, thanks for your extensive review, I'll fix all this in a V2.

Cool!

Quoted 7 lines

[...]

Quoted 1 line

Here you should add Signed-off-by of original author.

Yeah, I was wondering that but the only email I have was from his previous company.

I have no idea what email I should use so its useful.

I thought you know where he works. So, maybe you will be able to find his current email address too. If you find it please ask him which address should be used in this patch. If you are not able to get his current email address please use one from original patch.

Daniel

author
#4 lore raw

[...]
The ext_cmd change is cool except the linker isn't happy as it cannot find the associated symbols.
I asked a question on the IRC channel on this point.
I'm not familiar with the dependencies definition in this project, where should I work to get ext_cmd being linked with kern/efi ?
I tried inside grub-core/Makefile.core.def without much luck for now. I even wonder if we can linked modules with kern.
If you can point me in the right direction for this. I checked some other commits around ext_cmd but none changes the makefiles.

I also forgot to mention, your review was high quality for a newcomer. You point to the sample commits and that's very useful to see the expectations. Thanks for that.

Quoted 8 lines
Quoted 5 lines

[...]

Quoted 1 line

Here you should add Signed-off-by of original author.

Yeah, I was wondering that but the only email I have was from his

previous

Quoted 2 lines

company.

Quoted 8 lines
Quoted 1 line

I have no idea what email I should use so its useful.

I thought you know where he works. So, maybe you will be able to find his current email address too. If you find it please ask him which address should be used in this patch. If you are not able to get his current email address please use one from original patch.

I just have his linked profile but locked as I'm not premium.

I'll use the one I found.

#5 lore raw
Quoted 10 lines · Erwan Velu

[...]
The ext_cmd change is cool except the linker isn't happy as it cannot find the associated symbols.
I asked a question on the IRC channel on this point.
I'm not familiar with the dependencies definition in this project, where should I work to get ext_cmd being linked with kern/efi ?
I tried inside grub-core/Makefile.core.def without much luck for now. I even wonder if we can linked modules with kern.
If you can point me in the right direction for this. I checked some other commits around ext_cmd but none changes the makefiles.

Ugh... Sorry, I forgot extcmd interface is not available in the GRUB kernel. So, please ignore this comment.

Quoted 3 lines

I also forgot to mention, your review was high quality for a newcomer. You point to the sample commits and that's very useful to see the expectations. Thanks for that.

Nice to hear that. Thank you!

Daniel

author
#6 lore raw
In reply to Erwan Velu
Quoted 5 lines · Erwan Velu

Le 01/09/2021 à 18:24, Daniel Kiper a écrit :

First of all, thanks for your extensive review, I'll fix all this in a V2.

[...]

Just sent the V2, hope you'll enjoy this version of the patch. I learned many things on some GRUB internals and also simplified a lot the interface.