Skip to contentSkip to conversation

mkelf32 uninitialized data and reproducible builds

DISCUSSION Trammell Hudson 3 participants 3 messages Aug 8, 2016 – Aug 9, 2016

lore
Share & tools
lore
#0 lore raw

The xen/arch/x86/boot/mkelf32 executable is preventing Xen hypervisors from being reproducibly built. It is using an uninitialized stack buffer for padding after the ehdr and phdr are written to the xen file, which leads to non-deterministic bytes in the binary.

Additionally, the file is then compressed with gzip -9 without the -n or --no-name flag, which leads to the xen.gz file having non-deterministric timestamp bytes in the compressed file.

The xen/Makefile variables XEN_WHOAMI, XEN_DOMAIN, XEN_BUILD_TIME, XEN_BUILD_DATE and XEN_BUILD_HOST are also not reproducible, but since they are defined with ?= it is possible for an outside build script to override them. Perhaps having a flag to set these to a default value would be useful.

Fixing these allows a re-build of the binary to be the same on a given host and avoids undefined behaviour in mkelf32.

Signature · 42 lines
Trammell

diff --recursive -u /home/hudson/build/clean/xen-4.6.3/xen/arch/x86/boot/mkelf32.c ./xen/arch/x86/boot/mkelf32.c
--- /home/hudson/build/clean/xen-4.6.3/xen/arch/x86/boot/mkelf32.c	2016-06-20 08:08:22.000000000 -0400
+++ ./xen/arch/x86/boot/mkelf32.c	2016-08-04 18:02:25.854371605 -0400
@@ -231,14 +231,14 @@
     u32        loadbase, dat_siz, mem_siz;
     char      *inimage, *outimage;
     int        infd, outfd;
-    char       buffer[1024];
+    char       buffer[1024] = {};
     int        bytes, todo, i;
 
-    Elf32_Ehdr in32_ehdr;
-    Elf32_Phdr in32_phdr;
+    Elf32_Ehdr in32_ehdr = {};
+    Elf32_Phdr in32_phdr = {};
 
-    Elf64_Ehdr in64_ehdr;
-    Elf64_Phdr in64_phdr;
+    Elf64_Ehdr in64_ehdr = {};
+    Elf64_Phdr in64_phdr = {};
 
     if ( argc != 5 )
     {
diff --recursive -u /home/hudson/build/clean/xen-4.6.3/xen/Makefile ./xen/Makefile
--- /home/hudson/build/clean/xen-4.6.3/xen/Makefile	2016-06-20 08:08:22.000000000 -0400
+++ ./xen/Makefile	2016-08-04 17:50:04.888970103 -0400
@@ -97,7 +103,7 @@
 	rm -f tags TAGS cscope.files cscope.in.out cscope.out cscope.po.out GTAGS GPATH GRTAGS GSYMS
 
 $(TARGET).gz: $(TARGET)
-	gzip -f -9 < $< > [email protected]
+	gzip -n -f -9 < $< > [email protected]
 	mv [email protected] $@
 
 $(TARGET): delete-unfresh-files

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
#1 lore raw
Quoted 9 lines · Trammell Hudson

The xen/arch/x86/boot/mkelf32 executable is preventing Xen hypervisors from being reproducibly built. It is using an uninitialized stack buffer for padding after the ehdr and phdr are written to the xen file, which leads to non-deterministic bytes in the binary.

Additionally, the file is then compressed with gzip -9 without the -n or --no-name flag, which leads to the xen.gz file having non-deterministric timestamp bytes in the compressed file.

Quoted 8 lines

The xen/Makefile variables XEN_WHOAMI, XEN_DOMAIN, XEN_BUILD_TIME, XEN_BUILD_DATE and XEN_BUILD_HOST are also not reproducible, but since they are defined with ?= it is possible for an outside build script to override them. Perhaps having a flag to set these to a default value would be useful.

Fixing these allows a re-build of the binary to be the same on a given host and avoids undefined behaviour in mkelf32.

That is a nice patch. Sadly it is missing an Signed Off By.

I am wondering if you would be comfortable providing that?

The implication of it is that:

Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I have the right to submit it under the open source license indicated in the file; or

(b) The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate open source license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same open source license (unless I am permitted to submit under a different license), as indicated in the file; or

(c) The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it.

(d) I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the open source license(s) involved.

Quoted 40 lines
Signature · 4 lines
-- 
Trammell

diff --recursive -u /home/hudson/build/clean/xen-4.6.3/xen/arch/x86/boot/mkelf32.c ./xen/arch/x86/boot/mkelf32.c
xen/arch/x86/boot/mkelf32.c renamed from home/hudson/build/clean/xen-4.6.3/xen/arch/x86/boot/mkelf32.c +5 -5
@@ -231,14 +231,14 @@
     u32        loadbase, dat_siz, mem_siz;
     char      *inimage, *outimage;
     int        infd, outfd;
-    char       buffer[1024];
+    char       buffer[1024] = {};
     int        bytes, todo, i;
 
-    Elf32_Ehdr in32_ehdr;
-    Elf32_Phdr in32_phdr;
+    Elf32_Ehdr in32_ehdr = {};
+    Elf32_Phdr in32_phdr = {};
 
-    Elf64_Ehdr in64_ehdr;
-    Elf64_Phdr in64_phdr;
+    Elf64_Ehdr in64_ehdr = {};
+    Elf64_Phdr in64_phdr = {};
 
     if ( argc != 5 )
     {

diff --recursive -u /home/hudson/build/clean/xen-4.6.3/xen/Makefile ./xen/Makefile

xen/Makefile renamed from home/hudson/build/clean/xen-4.6.3/xen/Makefile +1 -1
@@ -97,7 +103,7 @@
 	rm -f tags TAGS cscope.files cscope.in.out cscope.out cscope.po.out GTAGS GPATH GRTAGS GSYMS
 
 $(TARGET).gz: $(TARGET)
-	gzip -f -9 < $< > [email protected]
+	gzip -n -f -9 < $< > [email protected]
 	mv [email protected] $@
 
 $(TARGET): delete-unfresh-files
Quoted 4 lines

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#2 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 08.08.16 at 21:23, <[email protected]> wrote:
Quoted 9 lines · Trammell Hudson

The xen/arch/x86/boot/mkelf32 executable is preventing Xen hypervisors from being reproducibly built. It is using an uninitialized stack buffer for padding after the ehdr and phdr are written to the xen file, which leads to non-deterministic bytes in the binary.

Additionally, the file is then compressed with gzip -9 without the -n or --no-name flag, which leads to the xen.gz file having non-deterministric timestamp bytes in the compressed file.

Quoted 5 lines
Quoted 5 lines

The xen/Makefile variables XEN_WHOAMI, XEN_DOMAIN, XEN_BUILD_TIME, XEN_BUILD_DATE and XEN_BUILD_HOST are also not reproducible, but since they are defined with ?= it is possible for an outside build script to override them. Perhaps having a flag to set these to a default value would be useful.

I think this had been brought up before, and being able to override the values on the make command line (or in the environment) has been deemed good enough.

Quoted 6 lines
Quoted 2 lines

Fixing these allows a re-build of the binary to be the same on a given host and avoids undefined behaviour in mkelf32.

That is a nice patch. Sadly it is missing an Signed Off By.

I am wondering if you would be comfortable providing that?

Additionally it should be against unstable instead of 4.6.3, and ideally would be split into two ones (as the two adjustments have little to do with one another).

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel