Skip to contentSkip to conversation

initramfs: Parse KBUILD_BUILD_TIMESTAMP as UTC date

PATCH In review Paulo Miguel Almeida 3 participants 3 messages Aug 21, 2023 – Aug 25, 2023

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 initramfs: Parse KBUILD_BUILD_TIMESTAMP as UTC date

When KBUILD_BUILD_TIMESTAMP is specified, the date command will parse it to Unix Epoch time in UTC. However, the date command is timezone-aware so it will convert from the local timezone to UTC first which hits some of the sanity checks added on commit 5efb685bb3af1 ("initramfs: Check negative timestamp to prevent broken cpio archive")

This creates an edge case for the UTC+<N> part of the world. For instance

- In New Zealand (UTC+12:00):

     $ date -d"1970-01-01" +%s
     -43200
     $ make KBUILD_BUILD_TIMESTAMP=1970-01-01
     make[1]: Entering directory '<snip>/linux/'
       GEN     Makefile
       DESCEND objtool
       INSTALL libsubcmd_headers
       CALL    ../scripts/checksyscalls.sh
       GEN     usr/initramfs_data.cpio
     ERROR: Timestamp out of range for cpio format
     make[4]: *** [../usr/Makefile:76: usr/initramfs_data.cpio] Error 1

- In Seattle, WA (UTC-07:00):

     $ date -d"1970-01-01" +%s
     32400
     $ make KBUILD_BUILD_TIMESTAMP=1970-01-01
     <builds fine>

Parse KBUILD_BUILD_TIMESTAMP date string as UTC so no localtime conversion is done, which fixes the edge case aforementioned

---
Changelog:

- v2: Document behaviour and way to override it on Documentation/kbuild/kbuild.rst. (Req: Andrew Donnellan)

- v1: https://lore.kernel.org/lkml/ZMSdUS37BD5b%[email protected]/

---
 Documentation/kbuild/kbuild.rst | 9 ++++++++-
 usr/gen_initramfs.sh            | 2 +-
 2 files changed, 9 insertions(+), 2 deletions(-)
Documentation/kbuild/kbuild.rst +8 -1
index bd906407e307..8c204186c762 100644
@@ -296,7 +296,14 @@ KBUILD_BUILD_TIMESTAMP
 Setting this to a date string overrides the timestamp used in the
 UTS_VERSION definition (uname -v in the running kernel). The value has to
 be a string that can be passed to date -d. The default value
-is the output of the date command at one point during build.
+is the output of the date command at one point during build. E.g.::
+
+    $ make KBUILD_BUILD_TIMESTAMP="1991-08-25"
+
+By default, the value is interpreted as UTC. To override this, append
+the desired timezone. E.g.::
+
+    $ make KBUILD_BUILD_TIMESTAMP="1991-08-25 UTC+03:00"
 
 KBUILD_BUILD_USER, KBUILD_BUILD_HOST
 ------------------------------------
usr/gen_initramfs.sh +1 -1
index 14b5782f961a..a90316d9a080 100755
@@ -221,7 +221,7 @@ while [ $# -gt 0 ]; do
 			shift
 			;;
 		"-d")	# date for file mtimes
-			timestamp="$(date -d"$1" +%s || :)"
+			timestamp="$(date -d"$1" -u +%s || :)"
 			if test -n "$timestamp"; then
 				timestamp="-t $timestamp"
 			fi
Signature · 1 line
2.41.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 30 lines · Paulo Miguel Almeida

When KBUILD_BUILD_TIMESTAMP is specified, the date command will parse it to Unix Epoch time in UTC. However, the date command is timezone-aware so it will convert from the local timezone to UTC first
which hits some of the sanity checks added on commit 5efb685bb3af1 ("initramfs: Check negative timestamp to prevent broken cpio archive")

This creates an edge case for the UTC+<N> part of the world. For instance

 - In New Zealand (UTC+12:00):

     $ date -d"1970-01-01" +%s
     -43200
     $ make KBUILD_BUILD_TIMESTAMP=1970-01-01
     make[1]: Entering directory '<snip>/linux/'
       GEN     Makefile
       DESCEND objtool
       INSTALL libsubcmd_headers
       CALL    ../scripts/checksyscalls.sh
       GEN     usr/initramfs_data.cpio
     ERROR: Timestamp out of range for cpio format
     make[4]: *** [../usr/Makefile:76: usr/initramfs_data.cpio] Error
1

 - In Seattle, WA (UTC-07:00):

     $ date -d"1970-01-01" +%s
     32400
Quoted 8 lines
     $ make KBUILD_BUILD_TIMESTAMP=1970-01-01
     <builds fine>

Parse KBUILD_BUILD_TIMESTAMP date string as UTC so no localtime conversion is done, which fixes the edge case aforementioned

Signed-off-by: Paulo Miguel Almeida
<[email protected]>

Thanks!

Quoted 49 lines

---
Changelog:

- v2: Document behaviour and way to override it on Documentation/kbuild/kbuild.rst. (Req: Andrew Donnellan)

- v1:
https://lore.kernel.org/lkml/ZMSdUS37BD5b%[email protected]/
---
 Documentation/kbuild/kbuild.rst | 9 ++++++++-
 usr/gen_initramfs.sh            | 2 +-

 2 files changed, 9 insertions(+), 2 deletions(-)
Documentation/kbuild/kbuild.rst +0 -0
index bd906407e307..8c204186c762 100644
@@ -296,7 +296,14 @@ KBUILD_BUILD_TIMESTAMP

 Setting this to a date string overrides the timestamp used in the UTS_VERSION definition (uname -v in the running kernel). The value has to
 be a string that can be passed to date -d. The default value

Documentation/kbuild/kbuild.rst +5 -1
-is the output of the date command at one point during build.
+is the output of the date command at one point during build. E.g.::
+
+    $ make KBUILD_BUILD_TIMESTAMP="1991-08-25"
+
+By default, the value is interpreted as UTC. To override this,

append

Documentation/kbuild/kbuild.rst +3 -0
+the desired timezone. E.g.::
+
+    $ make KBUILD_BUILD_TIMESTAMP="1991-08-25 UTC+03:00"


 KBUILD_BUILD_USER, KBUILD_BUILD_HOST
 ------------------------------------

usr/gen_initramfs.sh +0 -0
index 14b5782f961a..a90316d9a080 100755
@@ -221,7 +221,7 @@ while [ $# -gt 0 ]; do

                        shift
                        ;;
                "-d")   # date for file mtimes
-                       timestamp="$(date -d"$1" +%s || :)" +                       timestamp="$(date -d"$1" -u +%s || :)" if test -n "$timestamp"; then timestamp="-t $timestamp" fi

Signature · 2 lines
Andrew Donnellan    OzLabs, ADL Canberra
[email protected]   IBM Australia Limited
Reviewed-by on v2 1/1
#2 lore raw
In reply to Paulo Miguel Almeida
Quoted 50 lines · Paulo Miguel Almeida

When KBUILD_BUILD_TIMESTAMP is specified, the date command will parse it to Unix Epoch time in UTC. However, the date command is timezone-aware so it will convert from the local timezone to UTC first which hits some of the sanity checks added on commit 5efb685bb3af1 ("initramfs: Check negative timestamp to prevent broken cpio archive")

This creates an edge case for the UTC+<N> part of the world. For instance

- In New Zealand (UTC+12:00):

     $ date -d"1970-01-01" +%s
     -43200
     $ make KBUILD_BUILD_TIMESTAMP=1970-01-01
     make[1]: Entering directory '<snip>/linux/'
       GEN     Makefile
       DESCEND objtool
       INSTALL libsubcmd_headers
       CALL    ../scripts/checksyscalls.sh
       GEN     usr/initramfs_data.cpio
     ERROR: Timestamp out of range for cpio format
     make[4]: *** [../usr/Makefile:76: usr/initramfs_data.cpio] Error 1

- In Seattle, WA (UTC-07:00):

     $ date -d"1970-01-01" +%s
     32400
     $ make KBUILD_BUILD_TIMESTAMP=1970-01-01
     <builds fine>

Parse KBUILD_BUILD_TIMESTAMP date string as UTC so no localtime conversion is done, which fixes the edge case aforementioned

---
Changelog:

- v2: Document behaviour and way to override it on Documentation/kbuild/kbuild.rst. (Req: Andrew Donnellan)

- v1: https://lore.kernel.org/lkml/ZMSdUS37BD5b%[email protected]/

---
 Documentation/kbuild/kbuild.rst | 9 ++++++++-
 usr/gen_initramfs.sh            | 2 +-
 2 files changed, 9 insertions(+), 2 deletions(-)
Documentation/kbuild/kbuild.rst +0 -0
index bd906407e307..8c204186c762 100644

Documentation/kbuild/kbuild.rst KBUILD_BUILD_TIMESTAMP · line 304

Quoted 10 lines
Documentation/kbuild/kbuild.rst +6 -1
@@ -296,7 +296,14 @@ KBUILD_BUILD_TIMESTAMP
 Setting this to a date string overrides the timestamp used in the
 UTS_VERSION definition (uname -v in the running kernel). The value has to
 be a string that can be passed to date -d. The default value
-is the output of the date command at one point during build.
+is the output of the date command at one point during build. E.g.::
+
+    $ make KBUILD_BUILD_TIMESTAMP="1991-08-25"
+
+By default, the value is interpreted as UTC. To override this, append
+the desired timezone. E.g.::

This is still not true for IKHEADERS; there we still need to set TZ=UTC:

kernel/gen_kheaders.sh +1 -1
@@ -85,3 +85,3 @@ find $cpio_dir -type f -print0 |
 # Create archive and try to normalize metadata for reproducibility.
-tar "${KBUILD_BUILD_TIMESTAMP:+--mtime=$KBUILD_BUILD_TIMESTAMP}" \
+TZ=UTC tar "${KBUILD_BUILD_TIMESTAMP:+--mtime=$KBUILD_BUILD_TIMESTAMP}" \
     --owner=0 --group=0 --sort=name --numeric-owner \

but I can send such a patch later.