Skip to contentSkip to conversation

Improving grub-mkstandalone for reproducible build

PATCH New Michael Chang via Grub-devel 2 participants 4 messages Dec 6, 2023 – Dec 11, 2023 Kbuild

lore
Share & tools
lore
#0 lore raw

Enclosed is the description from openSUSE bugzilla entry:

While working on reproducible builds for openSUSE, I found that our grub2 package's /usr/share/grub2/x86_64-xen/grub.xen varies across builds.

I identified 2 issues: The tar contains changing (octal) mtime values. The tar stores files in random filesystem readdir order.

The first issue produces such diffs:

usr/share/grub2/x86_64-xen/grub.xen (objdump) +1 -1
@@ -4896,7 +4896,7 @@
  01319f 00000000 30303030 36303000 30303031  ....0000600.0001
  0131af 37353000 30303031 37353000 30303030  750.0001750.0000
  0131bf 30303033 34353000 31343533 31323035  0003450.14531205
- 0131cf 35303300 30303135 30313320 30000000  503.0015013 0...
+ 0131cf 36313200 30303135 30313420 30000000  612.0015014 0...
  0131df 00000000 00000000 00000000 00000000  ................
  0131ef 00000000 00000000 00000000 00000000  ................
 

and the second issue produced this diff:

  01311f 00000000 00000000 01000000 00000000  ................
  01312f 00000000 00000000 01000000 08b42600  ..............&.
  01313f 626f6f74 2f677275 622f7838 365f3634  boot/grub/x86_64
- 01314f 2d78656e 2f646973 6b2e6d6f 64000000  -xen/disk.mod...
+ 01314f 2d78656e 2f6c7378 656e2e6d 6f640000  -xen/lsxen.mod..
  01315f 00000000 00000000 00000000 00000000  ................

The second issue probably comes from grub_util_fd_readdir that would need to collect+sort entries before further processing.

Furthermore, both patches were developed to address the aforementioned issues individually. We hope to contribute them to upstream if the enhancement sounds appealing to others as well.

Thanks.

Michael Chang (2):
mkstandalone: ensure stable timestamps for generated images
mkstandalone: ensure deterministic tar file creation by sorting
contents

 util/grub-mkstandalone.c | 36 ++++++++++++++++++++++++++++--------
 1 file changed, 28 insertions(+), 8 deletions(-)
Signature · 7 lines
2.43.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#1 lore raw

PATCH 1/2 mkstandalone: ensure stable timestamps for generated images

This change mirrors a previous fix [1] but is specific to images generated by grub-mkstandalone.

The former fix (85a7be241) focused on utilizing a stable timestamp during binary generation in the util/mkimage context. This commit extends that approach to the images produced by grub-mkstandalone, ensuring consistency and stability in timestamps across all generated binaries.

[1] 85a7be241 util/mkimage: Use stable timestamp when generating binaries.

---
 util/grub-mkstandalone.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
util/grub-mkstandalone.c +5 -5
index bdbeea6a6..8e1229925 100644
@@ -30,6 +30,9 @@
 #pragma GCC diagnostic error "-Wmissing-prototypes"
 #pragma GCC diagnostic error "-Wmissing-declarations"
 
+/* use 2015-01-01T00:00:00+0000 as a stock timestamp */
+#define STABLE_EMBEDDING_TIMESTAMP 1420070400
+
 static char *output_image;
 static char **files;
 static int nfiles;
@@ -184,7 +187,6 @@ add_tar_file (const char *from,
   struct head hd;
   grub_util_fd_t in;
   ssize_t r;
-  grub_uint32_t mtime = 0;
   grub_uint32_t size;
 
   COMPILE_TIME_ASSERT (sizeof (hd) == 512);
@@ -192,8 +194,6 @@ add_tar_file (const char *from,
   if (grub_util_is_special_file (from))
     return;
 
-  mtime = grub_util_get_mtime (from);
-
   optr = tcn = xmalloc (strlen (to) + 1);
   for (iptr = to; *iptr == '/'; iptr++);
   for (; *iptr; iptr++)
@@ -234,7 +234,7 @@ add_tar_file (const char *from,
       memcpy (hd.gid, "0001750", 7);
 
       set_tar_value (hd.size, optr - tcn, 12);
-      set_tar_value (hd.mtime, mtime, 12);
+      set_tar_value (hd.mtime, STABLE_EMBEDDING_TIMESTAMP, 12);
       hd.typeflag = 'L';
       memcpy (hd.magic, MAGIC, sizeof (hd.magic));
       memcpy (hd.uname, "grub", 4);
@@ -264,7 +264,7 @@ add_tar_file (const char *from,
   memcpy (hd.gid, "0001750", 7);
 
   set_tar_value (hd.size, size, 12);
-  set_tar_value (hd.mtime, mtime, 12);
+  set_tar_value (hd.mtime, STABLE_EMBEDDING_TIMESTAMP, 12);
   hd.typeflag = '0';
   memcpy (hd.magic, MAGIC, sizeof (hd.magic));
   memcpy (hd.uname, "grub", 4);
Signature · 7 lines
2.43.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#2 lore raw

PATCH 2/2 mkstandalone: ensure deterministic tar file creation by sorting contents

In reply to Michael Chang via Grub-devel

The add_tar_files() function currently iterates through a directory's content using readdir(), which doesn't guarantee a specific order. This lack of deterministic behavior impacts reproducibility in the build process.

This commit resolves the issue by introducing sorting functionality. The list retrieved by readdir() is now sorted alphabetically before incorporation into the tar archive, ensuring consistent and predictable file ordering within the archive.

---
 util/grub-mkstandalone.c | 26 +++++++++++++++++++++++---
 1 file changed, 23 insertions(+), 3 deletions(-)
util/grub-mkstandalone.c +23 -3
index 8e1229925..e4b5bcab4 100644
@@ -205,22 +205,42 @@ add_tar_file (const char *from,
     {
       grub_util_fd_dir_t d;
       grub_util_fd_dirent_t de;
+      char **from_files;
+      grub_size_t alloc = 8, used = 0;
+      grub_size_t i;
 
       d = grub_util_fd_opendir (from);
 
+      from_files = xmalloc (alloc * sizeof (*from_files));
       while ((de = grub_util_fd_readdir (d)))
 	{
-	  char *fp, *tfp;
 	  if (strcmp (de->d_name, ".") == 0)
 	    continue;
 	  if (strcmp (de->d_name, "..") == 0)
 	    continue;
-	  fp = grub_util_path_concat (2, from, de->d_name);
-	  tfp = xasprintf ("%s/%s", to, de->d_name);
+	  if (alloc <= used)
+	    {
+	      alloc <<= 1;
+	      from_files = xrealloc (from_files, alloc * sizeof (*from_files));
+	    }
+	  from_files[used++] = xstrdup(de->d_name);
+	}
+      qsort (from_files, used, sizeof (*from_files), grub_qsort_strcmp);
+
+      for (i = 0; i < used; i++)
+	{
+	  char *fp, *tfp;
+
+	  fp = grub_util_path_concat (2, from, from_files[i]);
+	  tfp = xasprintf ("%s/%s", to, from_files[i]);
 	  add_tar_file (fp, tfp);
+	  free (tfp);
 	  free (fp);
+	  free (from_files[i]);
 	}
+
       grub_util_fd_closedir (d);
+      free (from_files);
       free (tcn);
       return;
     }
Signature · 7 lines
2.43.0


_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel
#3 lore raw
In reply to Michael Chang via Grub-devel
Quoted 37 lines · Michael Chang via Grub-devel

Enclosed is the description from openSUSE bugzilla entry:

While working on reproducible builds for openSUSE, I found that our grub2 package's /usr/share/grub2/x86_64-xen/grub.xen varies across builds.

I identified 2 issues: The tar contains changing (octal) mtime values. The tar stores files in random filesystem readdir order.

The first issue produces such diffs:

usr/share/grub2/x86_64-xen/grub.xen (objdump) +1 -1
@@ -4896,7 +4896,7 @@
  01319f 00000000 30303030 36303000 30303031  ....0000600.0001
  0131af 37353000 30303031 37353000 30303030  750.0001750.0000
  0131bf 30303033 34353000 31343533 31323035  0003450.14531205
- 0131cf 35303300 30303135 30313320 30000000  503.0015013 0...
+ 0131cf 36313200 30303135 30313420 30000000  612.0015014 0...
  0131df 00000000 00000000 00000000 00000000  ................
  0131ef 00000000 00000000 00000000 00000000  ................
 

and the second issue produced this diff:

  01311f 00000000 00000000 01000000 00000000  ................
  01312f 00000000 00000000 01000000 08b42600  ..............&.
  01313f 626f6f74 2f677275 622f7838 365f3634  boot/grub/x86_64
- 01314f 2d78656e 2f646973 6b2e6d6f 64000000  -xen/disk.mod...
+ 01314f 2d78656e 2f6c7378 656e2e6d 6f640000  -xen/lsxen.mod..
  01315f 00000000 00000000 00000000 00000000  ................

The second issue probably comes from grub_util_fd_readdir that would need to collect+sort entries before further processing.

Furthermore, both patches were developed to address the aforementioned issues individually. We hope to contribute them to upstream if the enhancement sounds appealing to others as well.

Quoted 6 lines

Thanks.

Michael Chang (2):
mkstandalone: ensure stable timestamps for generated images
mkstandalone: ensure deterministic tar file creation by sorting
contents

For both patches Reviewed-by: Daniel Kiper <[email protected]>...

Daniel

_______________________________________________
Grub-devel mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/grub-devel