grub_gzio_read_real() uses grub_errno to identify decompression failures
when the function completes.  Its callees in gzio.c are void-returning
functions that set grub_errno on their exit paths.

A Fedora 38 machine was observed to fail `multiboot2 /xen.gz` with
"premature end of file".  xen.gz was well formed and could be
successfully gunzip-ed in Linux.

The TPM is flaky and produces errors when the verifier tries to extend
PCRs with measurements.  Logs show "Unkown TPM error" and grub_errno is
set to GRUB_ERR_UNKNOWN_DEVICE.  This pre-existing grub_errno causes an
otherwise successful grub_gzio_read_real() call to return error.

Add a grub_debug_errno() macro to print existing grub_errno messages at
the start of the function, and reset grub_errno to avoid such errors.

Signed-off-by: Jason Andryuk <jandr...@gmail.com>
---
v2:
Add grub_debug_errno helper
Separate debug from reset.
---
 grub-core/io/gzio.c | 4 ++++
 include/grub/err.h  | 9 +++++++++
 2 files changed, 13 insertions(+)

diff --git a/grub-core/io/gzio.c b/grub-core/io/gzio.c
index ca9355751..883df0011 100644
--- a/grub-core/io/gzio.c
+++ b/grub-core/io/gzio.c
@@ -1294,6 +1294,10 @@ grub_gzio_read_real (grub_gzio_t gzio, grub_off_t offset,
 {
   grub_ssize_t ret = 0;
 
+  /* Avoid spurious failures on exit when grub_errno is already set. */
+  grub_debug_errno();
+  grub_errno = GRUB_ERR_NONE;
+
   /* Do we reset decompression to the beginning of the file?  */
   if (gzio->saved_offset > offset + WSIZE)
     initialize_tables (gzio);
diff --git a/include/grub/err.h b/include/grub/err.h
index 1c07034cd..f6948ecf8 100644
--- a/include/grub/err.h
+++ b/include/grub/err.h
@@ -96,4 +96,13 @@ extern int EXPORT_VAR(grub_err_printed_errors);
 int grub_err_printf (const char *fmt, ...)
      __attribute__ ((format (GNU_PRINTF, 1, 2)));
 
+#define grub_debug_errno() \
+do { \
+  if ( grub_errno != GRUB_ERR_NONE ) \
+    { \
+      grub_dprintf ("errno", "%s: clearing pre-exising errmsg %s\n", \
+                    __func__, grub_errmsg); \
+    } \
+} while (0);
+
 #endif /* ! GRUB_ERR_HEADER */
-- 
2.41.0


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

Reply via email to