|
| 1 | +From 442b676110c35caedb57408c4a27a6ef74f4c7db Mon Sep 17 00:00:00 2001 |
| 2 | +From: James Le Cuirot < [email protected]> |
| 3 | +Date: Thu, 24 Oct 2024 14:42:46 +0100 |
| 4 | +Subject: [PATCH 1/2] script/execute: Don't let trailing blank lines determine |
| 5 | + the return code |
| 6 | + |
| 7 | +grub_script_execute_sourcecode() parses and executes code one line at a |
| 8 | +time, updating the return code each time because only the last line |
| 9 | +determines the final status. However, trailing new lines were also |
| 10 | +executed, masking any failure on the previous line. Fix this by only |
| 11 | +trying to execute the command when there is actually one present. |
| 12 | + |
| 13 | +This has presumably never been noticed because this code is not used by |
| 14 | +regular functions, only in special cases like eval and menu entries. The |
| 15 | +latter generally don't return at all, having booted an OS. When failing |
| 16 | +to boot, upstream GRUB triggers the fallback mechanism regardless of the |
| 17 | +return code. |
| 18 | + |
| 19 | +We noticed the problem while using Red Hat's patches, which change this |
| 20 | +behaviour to take account of the return code. In that case, a failure |
| 21 | +takes you back to the menu rather than triggering a fallback. |
| 22 | + |
| 23 | +Signed-off-by: James Le Cuirot < [email protected]> |
| 24 | +--- |
| 25 | + grub-core/script/execute.c | 5 ++++- |
| 26 | + tests/grub_script_eval.in | 10 +++++++++- |
| 27 | + 2 files changed, 13 insertions(+), 2 deletions(-) |
| 28 | + |
| 29 | +diff --git a/grub-core/script/execute.c b/grub-core/script/execute.c |
| 30 | +index c19b4bf70..e369e8318 100644 |
| 31 | +--- a/grub-core/script/execute.c |
| 32 | ++++ b/grub-core/script/execute.c |
| 33 | +@@ -935,7 +935,10 @@ grub_script_execute_sourcecode (const char *source) |
| 34 | + break; |
| 35 | + } |
| 36 | + |
| 37 | +- ret = grub_script_execute (parsed_script); |
| 38 | ++ /* Don't let trailing blank lines determine the return code. */ |
| 39 | ++ if (parsed_script->cmd) |
| 40 | ++ ret = grub_script_execute (parsed_script); |
| 41 | ++ |
| 42 | + grub_script_free (parsed_script); |
| 43 | + grub_free (line); |
| 44 | + } |
| 45 | +diff --git a/tests/grub_script_eval.in b/tests/grub_script_eval.in |
| 46 | +index c97b78d77..9c6211042 100644 |
| 47 | +--- a/tests/grub_script_eval.in |
| 48 | ++++ b/tests/grub_script_eval.in |
| 49 | +@@ -3,4 +3,12 @@ |
| 50 | + eval echo "Hello world" |
| 51 | + valname=tst |
| 52 | + eval $valname=hi |
| 53 | +-echo $tst |
| 54 | +\ No newline at end of file |
| 55 | ++echo $tst |
| 56 | ++ |
| 57 | ++if eval " |
| 58 | ++false |
| 59 | ++"; then |
| 60 | ++ echo should have failed |
| 61 | ++else |
| 62 | ++ echo failed as expected |
| 63 | ++fi |
| 64 | +-- |
| 65 | +2.46.1 |
| 66 | + |
| 67 | + |
| 68 | +From 93eedb6cebe758db09066e084ddd2fb659ebc4fb Mon Sep 17 00:00:00 2001 |
| 69 | +From: James Le Cuirot < [email protected]> |
| 70 | +Date: Thu, 24 Oct 2024 15:00:26 +0100 |
| 71 | +Subject: [PATCH 2/2] normal/menu: Check return code of the script when |
| 72 | + executing a menu entry |
| 73 | + |
| 74 | +Don't rely on grub_errno here because grub_script_execute_new_scope() |
| 75 | +calls grub_print_error(), which always resets grub_errno back to |
| 76 | +GRUB_ERR_NONE. It may also get reset by grub_wait_after_message(). |
| 77 | + |
| 78 | +This problem was observed when a "bad signature" error resulted in the |
| 79 | +menu being redisplayed rather than the fallback mechanism being |
| 80 | +triggered, although another change was also needed to fix it. This only |
| 81 | +happens with Red Hat's patches because upstream GRUB triggers the |
| 82 | +fallback mechanism regardless of the return code. |
| 83 | + |
| 84 | +Signed-off-by: James Le Cuirot < [email protected]> |
| 85 | +--- |
| 86 | + grub-core/normal/menu.c | 4 ++-- |
| 87 | + 1 file changed, 2 insertions(+), 2 deletions(-) |
| 88 | + |
| 89 | +diff --git a/grub-core/normal/menu.c b/grub-core/normal/menu.c |
| 90 | +index cda10fa8b..fd9b8c6d8 100644 |
| 91 | +--- a/grub-core/normal/menu.c |
| 92 | ++++ b/grub-core/normal/menu.c |
| 93 | +@@ -376,14 +376,14 @@ grub_menu_execute_entry(grub_menu_entry_t entry, int auto_boot) |
| 94 | + if (ptr && ptr[0] && ptr[1]) |
| 95 | + grub_env_set ("default", ptr + 1); |
| 96 | + |
| 97 | +- grub_script_execute_new_scope (entry->sourcecode, entry->argc, entry->args); |
| 98 | ++ err = grub_script_execute_new_scope (entry->sourcecode, entry->argc, entry->args); |
| 99 | + |
| 100 | + if (errs_before != grub_err_printed_errors) |
| 101 | + grub_wait_after_message (); |
| 102 | + |
| 103 | + errs_before = grub_err_printed_errors; |
| 104 | + |
| 105 | +- if (grub_errno == GRUB_ERR_NONE && grub_loader_is_loaded ()) |
| 106 | ++ if (err == GRUB_ERR_NONE && grub_loader_is_loaded ()) |
| 107 | + /* Implicit execution of boot, only if something is loaded. */ |
| 108 | + err = grub_command_execute ("boot", 0, 0); |
| 109 | + |
| 110 | +-- |
| 111 | +2.46.1 |
| 112 | + |
0 commit comments