[coreboot-gerrit] New patch to review for coreboot: f678db7 chromeos: Reverse FMAP signature constant to avoid having it in .rodata

Stefan Reinauer (stefan.reinauer@coreboot.org) gerrit at coreboot.org
Fri Apr 10 22:02:49 CEST 2015


Stefan Reinauer (stefan.reinauer at coreboot.org) just uploaded a new patch set to gerrit, which you can find at http://review.coreboot.org/9562

-gerrit

commit f678db79ff9030daf4b93c21ebf59923ac623e75
Author: Julius Werner <jwerner at chromium.org>
Date:   Wed Jan 14 13:12:10 2015 -0800

    chromeos: Reverse FMAP signature constant to avoid having it in .rodata
    
    Even though coreboot always hardcodes the FMAP offset, the same is not
    possible for all other tools that manipulate ROM images. Some need to
    manually find the FMAP by searching for it's magic number (ASCII
    "__FMAP__"). If we do something like 'memcmp(fmap_buffer, "__FMAP__",
    ...) in coreboot code, it has the unfortunate side effect that the
    compiler will output that very same magic number as a constant in the
    .rodata section to compare against. Other tools may mistake this for the
    "real" FMAP location and get confused.
    
    This patch reverses the constant defined in coreboot and changes the
    only use of it correspondingly. It is not impossible but extremely
    unlikely (at the current state of the art) that any compiler would be
    clever enough to understand this pattern and optimize it back to a
    straight memcmp() (GCC 4.9 definitely doesn't), so it should solve the
    problem at least for another few years/decades.
    
    BRANCH=veyron
    BUG=chromium:447051
    TEST=Made sure the new binaries actually contain "__PAMF__" in their
    .rodata. Booted Pinky. Independently corrupted both the first and the
    last byte of the FMAP signature with a hex editor and confirmed that
    signature check fails in both cases.
    
    Change-Id: I314b5e7e4d78352f409e73a3ed0e71d1b56fe774
    Signed-off-by: Stefan Reinauer <reinauer at chromium.org>
    Original-Commit-Id: 1359d2d4502eb34a043dffab35cf4a5b033ed65a
    Original-Change-Id: I725652ef2a77f7f99884b46498428c3d68cd0945
    Original-Signed-off-by: Julius Werner <jwerner at chromium.org>
    Original-Reviewed-on: https://chromium-review.googlesource.com/240723
    Original-Reviewed-by: Daisuke Nojiri <dnojiri at chromium.org>
    Original-Reviewed-by: David Hendricks <dhendrix at chromium.org>
---
 src/vendorcode/google/chromeos/fmap.c | 13 +++++++++----
 src/vendorcode/google/chromeos/fmap.h |  2 +-
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/src/vendorcode/google/chromeos/fmap.c b/src/vendorcode/google/chromeos/fmap.c
index 50b0248..21d439e 100644
--- a/src/vendorcode/google/chromeos/fmap.c
+++ b/src/vendorcode/google/chromeos/fmap.c
@@ -26,10 +26,15 @@
 
 static int is_fmap_signature_valid(const struct fmap *fmap)
 {
-	if (memcmp(fmap, FMAP_SIGNATURE, sizeof(FMAP_SIGNATURE) - 1)) {
-		printk(BIOS_ERR, "No FMAP found at %p.\n", fmap);
-		return 1;
-	}
+	const char reversed_sig[] = FMAP_REVERSED_SIGNATURE;
+	const char *p2 = reversed_sig + sizeof(FMAP_REVERSED_SIGNATURE) - 2;
+	const char *p1 = (char *)fmap;
+
+	while (p2 >= reversed_sig)
+		if (*p1++ != *p2--) {
+			printk(BIOS_ERR, "No FMAP found at %p.\n", fmap);
+			return 1;
+		}
 
 	printk(BIOS_DEBUG, "FMAP: Found \"%s\" version %d.%d at %p.\n",
 	       fmap->name, fmap->ver_major, fmap->ver_minor, fmap);
diff --git a/src/vendorcode/google/chromeos/fmap.h b/src/vendorcode/google/chromeos/fmap.h
index a3d2abd..05d3fb6 100644
--- a/src/vendorcode/google/chromeos/fmap.h
+++ b/src/vendorcode/google/chromeos/fmap.h
@@ -38,7 +38,7 @@
 
 #include <stdint.h>
 
-#define FMAP_SIGNATURE		"__FMAP__"
+#define FMAP_REVERSED_SIGNATURE	"__PAMF__" /* avoid magic number in .rodata */
 #define FMAP_VER_MAJOR		1	/* this header's FMAP minor version */
 #define FMAP_VER_MINOR		1	/* this header's FMAP minor version */
 #define FMAP_STRLEN		32	/* maximum length for strings, */



More information about the coreboot-gerrit mailing list