Skip to contentSkip to conversation

initramfs: Check timestamp to prevent broken cpio archive

PATCH In review Nicolas Schier 2 participants 2 messages Oct 12, 2021 – Oct 13, 2021 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 initramfs: Check timestamp to prevent broken cpio archive

Cpio format reserves 8 bytes for an ASCII representation of a time_t timestamp. While 2106-02-07 06:28:15 UTC (time_t = 0xffffffff) is still some years in the future, a poorly chosen date string for KBUILD_BUILD_TIMESTAMP, converted into seconds since the epoch, might lead to exceeded cpio timestamp limits that result in a broken cpio archive. Add timestamp checks to prevent overrun of the 8-byte cpio header field.

My colleague Thomas Kühnel discovered the behaviour, when we accidentally fed SOURCE_DATE_EPOCH to KBUILD_BUILD_TIMESTAMP as is: some timestamps (e.g. 1607420928 = 2021-12-08 9:48:48 UTC) will be interpreted by date as a valid date specification of science fictional times (here: year 160742). Even though this is bad input for KBUILD_BUILD_TIMESTAMP, it should not break the initramfs cpio format.

---
 usr/gen_init_cpio.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
usr/gen_init_cpio.c +16 -0
index 03b21189d58b..97aeeecf7386 100644
@@ -320,6 +320,12 @@ static int cpio_mkfile(const char *name, const char *location,
 		goto error;
 	}
 
+	if (buf.st_mtime > 0xffffffff) {
+		fprintf(stderr, "%s: Timestamp exceeds maximum cpio timestamp, clipping.\n",
+			location);
+		buf.st_mtime = 0xffffffff;
+	}
+
 	filebuf = malloc(buf.st_size);
 	if (!filebuf) {
 		fprintf (stderr, "out of memory\n");
@@ -551,6 +557,16 @@ int main (int argc, char *argv[])
 		}
 	}
 
+	/*
+	 * Timestamps after 2106-02-07 06:28:15 UTC have an ascii hex time_t
+	 * representation that exceeds 8 chars and breaks the cpio header
+	 * specification.
+	 */
+	if (default_mtime > 0xffffffff) {
+		fprintf(stderr, "ERROR: Timestamp too large for cpio format\n");
+		exit(1);
+	}
+
 	if (argc - optind != 1) {
 		usage(argv[0]);
 		exit(1);
Signature · 1 line
2.30.1
#1 lore raw
Quoted 15 lines · Nicolas Schier

Cpio format reserves 8 bytes for an ASCII representation of a time_t timestamp. While 2106-02-07 06:28:15 UTC (time_t = 0xffffffff) is still some years in the future, a poorly chosen date string for KBUILD_BUILD_TIMESTAMP, converted into seconds since the epoch, might lead to exceeded cpio timestamp limits that result in a broken cpio archive. Add timestamp checks to prevent overrun of the 8-byte cpio header field.

My colleague Thomas Kühnel discovered the behaviour, when we accidentally fed SOURCE_DATE_EPOCH to KBUILD_BUILD_TIMESTAMP as is: some timestamps (e.g. 1607420928 = 2021-12-08 9:48:48 UTC) will be interpreted by date as a valid date specification of science fictional times (here: year 160742). Even though this is bad input for KBUILD_BUILD_TIMESTAMP, it should not break the initramfs cpio format.

Quoted 3 lines

---

Applied to linux-kbuild. Thanks.

Quoted 40 lines
 usr/gen_init_cpio.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
usr/gen_init_cpio.c +16 -1
index 03b21189d58b..97aeeecf7386 100644
@@ -320,6 +320,12 @@ static int cpio_mkfile(const char *name, const char *location,
                goto error;
        }
 
+       if (buf.st_mtime > 0xffffffff) {
+               fprintf(stderr, "%s: Timestamp exceeds maximum cpio timestamp, clipping.\n",
+                       location);
+               buf.st_mtime = 0xffffffff;
+       }
+
        filebuf = malloc(buf.st_size);
        if (!filebuf) {
                fprintf (stderr, "out of memory\n");
@@ -551,6 +557,16 @@ int main (int argc, char *argv[])
                }
        }
 
+       /*
+        * Timestamps after 2106-02-07 06:28:15 UTC have an ascii hex time_t
+        * representation that exceeds 8 chars and breaks the cpio header
+        * specification.
+        */
+       if (default_mtime > 0xffffffff) {
+               fprintf(stderr, "ERROR: Timestamp too large for cpio format\n");
+               exit(1);
+       }
+
        if (argc - optind != 1) {
                usage(argv[0]);
                exit(1);
--

2.30.1

Signature · 2 lines
Best Regards
Masahiro Yamada