Skip to contentSkip to conversation

IHEX bug fix and some improvements

PATCH New Andrey Smirnov 1 participant 6 messages Dec 21, 2018

lore
Share & tools
lore
#0 lore raw

cover IHEX bug fix and some improvements

Everyone:

This series is a big-fix to a problem I encountered while using code in <linux/ihex.h> bundled with a number of small improvmements. The bug fix is in commit:

[PATCH 2/5] ihex: Check if zero-length record is at the end of the

Small improvmentes are in the rest and all of them are purely optional. Hopefully each commit is self-explanatory.

Feedback is welcome!

Thanks,
Andrey Smirnov

NOTE: This series is expected to be applied on top of [fimrware-patch]

[firmware-patch] lkml.kernel.org/r/[email protected]

Andrey Smirnov (5):
ihex: Share code between ihex_validate_fw() and ihex_next_binrec()
ihex: Check if zero-length record is at the end of the blob
ihex: Simplify next record offset calculation tools/firmware/ihex2fw: Simplify next record offset calculation tools/firmware/ihex2fw: Replace explicit alignment with ALIGN

 include/linux/ihex.h     | 29 +++++++++++++++++++----------
 tools/firmware/ihex2fw.c | 17 ++++++++++++++---
 2 files changed, 33 insertions(+), 13 deletions(-)
Signature · 1 line
2.19.1
#1 lore raw

PATCH 1/5 ihex: Share code between ihex_validate_fw() and ihex_next_binrec()

Convert both ihex_validate_fw() and ihex_next_binrec() to use a helper function to calculate next record offest. This way we only have one place implementing next record offset calculation logic. No functional change intended.

Cc Chris Healy <[email protected]>
Cc Kyle McMartin <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc David Woodhouse <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc linux-kernel <[email protected]>
---
 include/linux/ihex.h | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
include/linux/ihex.h +13 -8
index 75c194391869..9c701521176b 100644
@@ -23,29 +23,34 @@ struct ihex_binrec {
 
 /* Find the next record, taking into account the 4-byte alignment */
 static inline const struct ihex_binrec *
-ihex_next_binrec(const struct ihex_binrec *rec)
+__ihex_next_binrec(const struct ihex_binrec *rec)
 {
 	int next = ((be16_to_cpu(rec->len) + 5) & ~3) - 2;
 	rec = (void *)&rec->data[next];
 
+	return rec;
+}
+
+static inline const struct ihex_binrec *
+ihex_next_binrec(const struct ihex_binrec *rec)
+{
+	rec = __ihex_next_binrec(rec);
+
 	return be16_to_cpu(rec->len) ? rec : NULL;
 }
 
 /* Check that ihex_next_binrec() won't take us off the end of the image... */
 static inline int ihex_validate_fw(const struct firmware *fw)
 {
-	const struct ihex_binrec *rec;
-	size_t ofs = 0;
+	const struct ihex_binrec *end, *rec;
 
-	while (ofs <= fw->size - sizeof(*rec)) {
-		rec = (void *)&fw->data[ofs];
+	rec = (const void *)fw->data;
+	end = (const void *)&fw->data[fw->size - sizeof(*end)];
 
+	for (; rec <= end; rec = __ihex_next_binrec(rec)) {
 		/* Zero length marks end of records */
 		if (!be16_to_cpu(rec->len))
 			return 0;
-
-		/* Point to next record... */
-		ofs += (sizeof(*rec) + be16_to_cpu(rec->len) + 3) & ~3;
 	}
 	return -EINVAL;
 }
Signature · 1 line
2.19.1
#2 lore raw

PATCH 2/5 ihex: Check if zero-length record is at the end of the blob

In reply to Andrey Smirnov

When verifying the validity of IHEX file we need to make sure that zero-length record we found is located at the end of the file. Not doing that could result in an invalid file with a bogus zero-length in the middle short-circuiting the check and being reported as valid.

Cc Chris Healy <[email protected]>
Cc Kyle McMartin <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc David Woodhouse <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc linux-kernel <[email protected]>
---
 include/linux/ihex.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/ihex.h +1 -1
index 9c701521176b..9130f307a420 100644
@@ -49,7 +49,7 @@ static inline int ihex_validate_fw(const struct firmware *fw)
 
 	for (; rec <= end; rec = __ihex_next_binrec(rec)) {
 		/* Zero length marks end of records */
-		if (!be16_to_cpu(rec->len))
+		if (rec == end && !be16_to_cpu(rec->len))
 			return 0;
 	}
 	return -EINVAL;
Signature · 1 line
2.19.1
#3 lore raw

PATCH 3/5 ihex: Simplify next record offset calculation

In reply to Andrey Smirnov

Next record calucaltion can be reduced to a much more tivial ALIGN operation as follows:

1. Splitting 5 into 2 + 3 we get

next = ((be16_to_cpu(rec->len) + 2 + 3) & ~3) - 2 (1)

2. Using ALIGN macro we reduce (1) to:

ALIGN(be16_to_cpu(rec->len) + 2, 4) - 2 (2)

3. Subsituting 'next' in original next record calucation we get:

(void *)&rec->data[ALIGN(be16_to_cpu(rec->len) + 2, 4) - 2] (3)

4. Converting array index to pointer arithmetic we convert (3) into:

(void *)rec + sizeof(*rec) +
ALIGN(be16_to_cpu(rec->len) + 2, 4) - 2 (4)

5. Subsituting sizeof(*rec) with its value, 6, and substracting 2, in (4) we get:

(void *)rec + ALIGN(be16_to_cpu(rec->len) + 2, 4) + 4 (5)

6. Since ALIGN(X, 4) + 4 == ALIGN(X + 4, 4), (5) can be converted to:

(void *)rec + ALIGN(be16_to_cpu(rec->len) + 6, 4) (6)

5. Subsituting 6 in (6) to sizeof(*rec) we get:

(void *)rec + ALIGN(be16_to_cpu(rec->len) + sizeof(*rec), 4) (7)

Using expression (7) should make it more clear that next record is located by adding full size of the current record (payload + auxiliary data) aligned to 4 bytes, to the location of the current one. No functional change intended.

Cc Chris Healy <[email protected]>
Cc Kyle McMartin <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc David Woodhouse <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc linux-kernel <[email protected]>
---
 include/linux/ihex.h | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
include/linux/ihex.h +7 -3
index 9130f307a420..98cb5ce0b0a0 100644
@@ -21,14 +21,18 @@ struct ihex_binrec {
 	uint8_t data[0];
 } __attribute__((packed));
 
+static inline uint16_t ihex_binrec_size(const struct ihex_binrec *p)
+{
+	return be16_to_cpu(p->len) + sizeof(*p);
+}
+
 /* Find the next record, taking into account the 4-byte alignment */
 static inline const struct ihex_binrec *
 __ihex_next_binrec(const struct ihex_binrec *rec)
 {
-	int next = ((be16_to_cpu(rec->len) + 5) & ~3) - 2;
-	rec = (void *)&rec->data[next];
+	const void *p = rec;
 
-	return rec;
+	return p + ALIGN(ihex_binrec_size(rec), 4);
 }
 
 static inline const struct ihex_binrec *
Signature · 1 line
2.19.1
#4 lore raw

PATCH 4/5 tools/firmware/ihex2fw: Simplify next record offset calculation

In reply to Andrey Smirnov

We can convert original expression for 'writelen" to use ALIGN as follows:

    (p->len + 9) & ~3 => (p->len + 6 + 3) & ~3 => ALIGN(p->len + 6, 4)

Now, subsituting "p->len + 6" with "p->len + sizeof(p->addr) + sizeof(p->len)" we end up with the same expression as used by kernel couterpart in linux/ihex.h:

    ALIGN(p->len + sizeof(p->addr) + sizeof(p->len), 4)

That is a full size of the record, aligned to 4 bytes. No functional change intended.

Cc Chris Healy <[email protected]>
Cc Kyle McMartin <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc David Woodhouse <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc linux-kernel <[email protected]>
---
 tools/firmware/ihex2fw.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
tools/firmware/ihex2fw.c +10 -1
index b58dd061e978..e081cef730d8 100644
@@ -24,6 +24,10 @@
 #include <getopt.h>
 
 
+#define __ALIGN_KERNEL_MASK(x, mask)	(((x) + (mask)) & ~(mask))
+#define __ALIGN_KERNEL(x, a)		__ALIGN_KERNEL_MASK(x, (typeof(x))(a) - 1)
+#define ALIGN(x, a)			__ALIGN_KERNEL((x), (a))
+
 struct ihex_binrec {
 	struct ihex_binrec *next; /* not part of the real data structure */
         uint32_t addr;
@@ -259,13 +263,18 @@ static void file_record(struct ihex_binrec *record)
 	*p = record;
 }
 
+static uint16_t ihex_binrec_size(struct ihex_binrec *p)
+{
+	return p->len + sizeof(p->addr) + sizeof(p->len);
+}
+
 static int output_records(int outfd)
 {
 	unsigned char zeroes[6] = {0, 0, 0, 0, 0, 0};
 	struct ihex_binrec *p = records;
 
 	while (p) {
-		uint16_t writelen = (p->len + 9) & ~3;
+		uint16_t writelen = ALIGN(ihex_binrec_size(p), 4);
 
 		p->addr = htonl(p->addr);
 		p->len = htons(p->len);
Signature · 1 line
2.19.1
#5 lore raw

PATCH 5/5 tools/firmware/ihex2fw: Replace explicit alignment with ALIGN

In reply to Andrey Smirnov

(X + 3) & ~3 is the same as ALIGN(X, 4), so replace all of the instances of the formwer in the code with the latter. While at it, introduce a helper variable 'record_size' to avoid duplicating length calculatin code. No functional change intended.

Cc Chris Healy <[email protected]>
Cc Kyle McMartin <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Masahiro Yamada <[email protected]>
Cc David Woodhouse <[email protected]>
Cc Greg Kroah-Hartman <[email protected]>
Cc linux-kernel <[email protected]>
---
 tools/firmware/ihex2fw.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
tools/firmware/ihex2fw.c +4 -2
index e081cef730d8..8925b60e51f5 100644
@@ -135,6 +135,7 @@ int main(int argc, char **argv)
 static int process_ihex(uint8_t *data, ssize_t size)
 {
 	struct ihex_binrec *record;
+	size_t record_size;
 	uint32_t offset = 0;
 	uint32_t data32;
 	uint8_t type, crc = 0, crcbyte = 0;
@@ -161,12 +162,13 @@ static int process_ihex(uint8_t *data, ssize_t size)
 		len <<= 8;
 		len += hex(data + i, &crc); i += 2;
 	}
-	record = malloc((sizeof (*record) + len + 3) & ~3);
+	record_size = ALIGN(sizeof(*record) + len, 4);
+	record = malloc(record_size);
 	if (!record) {
 		fprintf(stderr, "out of memory for records\n");
 		return -ENOMEM;
 	}
-	memset(record, 0, (sizeof(*record) + len + 3) & ~3);
+	memset(record, 0, record_size);
 	record->len = len;
 
 	/* now check if we have enough data to read everything */
Signature · 1 line
2.19.1