1From 2cc99c9cdc8fde5e92e34f9655829449cebd3e00 Mon Sep 17 00:00:00 2001
2From: Dmitry Goldin <dgoldin+lkml@protonmail.ch>
3Date: Fri, 4 Oct 2019 10:40:07 +0000
4Subject: kheaders: make headers archive reproducible
5
6In commit 43d8ce9d65a5 ("Provide in-kernel headers to make
7extending kernel easier") a new mechanism was introduced, for kernels
8>=5.2, which embeds the kernel headers in the kernel image or a module
9and exposes them in procfs for use by userland tools.
10
11The archive containing the header files has nondeterminism caused by
12header files metadata. This patch normalizes the metadata and utilizes
13KBUILD_BUILD_TIMESTAMP if provided and otherwise falls back to the
14default behaviour.
15
16In commit f7b101d33046 ("kheaders: Move from proc to sysfs") it was
17modified to use sysfs and the script for generation of the archive was
18renamed to what is being patched.
19
20Signed-off-by: Dmitry Goldin <dgoldin+lkml@protonmail.ch>
21Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
22Reviewed-by: Joel Fernandes (Google) <joel@joelfernandes.org>
23Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
24
25---
26
27nixos note: This patch is from
28https://git.kernel.org/pub/scm/linux/kernel/git/masahiroy/linux-kbuild.git/commit/?h=fixes&id=2cc99c9cdc8fde5e92e34f9655829449cebd3e00
29I commented out the documentation part here, so that it easily applies
30to linux 5.2 and 5.3, which does not ship with the reproducible build
31documentation yet, which only was introduced recently.
32
33---
34 Documentation/kbuild/reproducible-builds.rst | 13 +++++++++----
35 kernel/gen_kheaders.sh | 5 ++++-
36 2 files changed, 13 insertions(+), 5 deletions(-)
37
38#diff --git a/Documentation/kbuild/reproducible-builds.rst b/Documentation/kbuild/reproducible-builds.rst
39#index ab92e98c89c8..503393854e2e 100644
40# --- a/Documentation/kbuild/reproducible-builds.rst
41#+++ b/Documentation/kbuild/reproducible-builds.rst
42#@@ -16,16 +16,21 @@ the kernel may be unreproducible, and how to avoid them.
43# Timestamps
44# ----------
45#
46#-The kernel embeds a timestamp in two places:
47#+The kernel embeds timestamps in three places:
48#
49# * The version string exposed by ``uname()`` and included in
50# ``/proc/version``
51#
52# * File timestamps in the embedded initramfs
53#
54#-By default the timestamp is the current time. This must be overridden
55#-using the `KBUILD_BUILD_TIMESTAMP`_ variable. If you are building
56#-from a git commit, you could use its commit date.
57#+* If enabled via ``CONFIG_IKHEADERS``, file timestamps of kernel
58#+ headers embedded in the kernel or respective module,
59#+ exposed via ``/sys/kernel/kheaders.tar.xz``
60#+
61#+By default the timestamp is the current time and in the case of
62#+``kheaders`` the various files' modification times. This must
63#+be overridden using the `KBUILD_BUILD_TIMESTAMP`_ variable.
64#+If you are building from a git commit, you could use its commit date.
65#
66# The kernel does *not* use the ``__DATE__`` and ``__TIME__`` macros,
67# and enables warnings if they are used. If you incorporate external
68diff --git a/kernel/gen_kheaders.sh b/kernel/gen_kheaders.sh
69index 9ff449888d9c..aff79e461fc9 100755
70--- a/kernel/gen_kheaders.sh
71+++ b/kernel/gen_kheaders.sh
72@@ -71,7 +71,10 @@ done | cpio --quiet -pd $cpio_dir >/dev/null 2>&1
73 find $cpio_dir -type f -print0 |
74 xargs -0 -P8 -n1 perl -pi -e 'BEGIN {undef $/;}; s/\/\*((?!SPDX).)*?\*\///smg;'
75
76-tar -Jcf $tarfile -C $cpio_dir/ . > /dev/null
77+# Create archive and try to normalize metadata for reproducibility
78+tar "${KBUILD_BUILD_TIMESTAMP:+--mtime=$KBUILD_BUILD_TIMESTAMP}" \
79+ --owner=0 --group=0 --sort=name --numeric-owner \
80+ -Jcf $tarfile -C $cpio_dir/ . > /dev/null
81
82 echo "$src_files_md5" > kernel/kheaders.md5
83 echo "$obj_files_md5" >> kernel/kheaders.md5
84--
85cgit 1.2-0.3.lf.el7
86