mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 0/5] IHEX bug fix and some improvements
@ 2018-12-21  7:28 Andrey Smirnov
  2018-12-21  7:28 ` [PATCH 1/5] ihex: Share code between ihex_validate_fw() and ihex_next_binrec() Andrey Smirnov
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Andrey Smirnov @ 2018-12-21  7:28 UTC (permalink / raw)
  To: linux-kernel
  Cc: Andrey Smirnov, Chris Healy, Kyle McMartin, Andrew Morton,
	Masahiro Yamada, David Woodhouse, Greg Kroah-Hartman

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/20181017182718.32740-1-andrew.smirnov@gmail.com

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(-)

-- 
2.19.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/5] ihex: Share code between ihex_validate_fw() and ihex_next_binrec()
  2018-12-21  7:28 [PATCH 0/5] IHEX bug fix and some improvements Andrey Smirnov
@ 2018-12-21  7:28 ` Andrey Smirnov
  2018-12-21  7:28 ` [PATCH 2/5] ihex: Check if zero-length record is at the end of the blob Andrey Smirnov
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Andrey Smirnov @ 2018-12-21  7:28 UTC (permalink / raw)
  To: linux-kernel
  Cc: Andrey Smirnov, Chris Healy, Kyle McMartin, Andrew Morton,
	Masahiro Yamada, David Woodhouse, Greg Kroah-Hartman

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 <cphealy@gmail.com>
Cc: Kyle McMartin <kyle@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Masahiro Yamada <yamada.masahiro@socionext.com>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-kernel <linux-kernel@vger.kernel.org>
Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 include/linux/ihex.h | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/include/linux/ihex.h b/include/linux/ihex.h
index 75c194391869..9c701521176b 100644
--- a/include/linux/ihex.h
+++ b/include/linux/ihex.h
@@ -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;
 }
-- 
2.19.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 2/5] ihex: Check if zero-length record is at the end of the blob
  2018-12-21  7:28 [PATCH 0/5] IHEX bug fix and some improvements Andrey Smirnov
  2018-12-21  7:28 ` [PATCH 1/5] ihex: Share code between ihex_validate_fw() and ihex_next_binrec() Andrey Smirnov
@ 2018-12-21  7:28 ` Andrey Smirnov
  2018-12-21  7:28 ` [PATCH 3/5] ihex: Simplify next record offset calculation Andrey Smirnov
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Andrey Smirnov @ 2018-12-21  7:28 UTC (permalink / raw)
  To: linux-kernel
  Cc: Andrey Smirnov, Chris Healy, Kyle McMartin, Andrew Morton,
	Masahiro Yamada, David Woodhouse, Greg Kroah-Hartman

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 <cphealy@gmail.com>
Cc: Kyle McMartin <kyle@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Masahiro Yamada <yamada.masahiro@socionext.com>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-kernel <linux-kernel@vger.kernel.org>
Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 include/linux/ihex.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/include/linux/ihex.h b/include/linux/ihex.h
index 9c701521176b..9130f307a420 100644
--- a/include/linux/ihex.h
+++ b/include/linux/ihex.h
@@ -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;
-- 
2.19.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 3/5] ihex: Simplify next record offset calculation
  2018-12-21  7:28 [PATCH 0/5] IHEX bug fix and some improvements Andrey Smirnov
  2018-12-21  7:28 ` [PATCH 1/5] ihex: Share code between ihex_validate_fw() and ihex_next_binrec() Andrey Smirnov
  2018-12-21  7:28 ` [PATCH 2/5] ihex: Check if zero-length record is at the end of the blob Andrey Smirnov
@ 2018-12-21  7:28 ` Andrey Smirnov
  2018-12-21  7:28 ` [PATCH 4/5] tools/firmware/ihex2fw: " Andrey Smirnov
  2018-12-21  7:28 ` [PATCH 5/5] tools/firmware/ihex2fw: Replace explicit alignment with ALIGN Andrey Smirnov
  4 siblings, 0 replies; 6+ messages in thread
From: Andrey Smirnov @ 2018-12-21  7:28 UTC (permalink / raw)
  To: linux-kernel
  Cc: Andrey Smirnov, Chris Healy, Kyle McMartin, Andrew Morton,
	Masahiro Yamada, David Woodhouse, Greg Kroah-Hartman

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 <cphealy@gmail.com>
Cc: Kyle McMartin <kyle@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Masahiro Yamada <yamada.masahiro@socionext.com>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-kernel <linux-kernel@vger.kernel.org>
Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 include/linux/ihex.h | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/include/linux/ihex.h b/include/linux/ihex.h
index 9130f307a420..98cb5ce0b0a0 100644
--- a/include/linux/ihex.h
+++ b/include/linux/ihex.h
@@ -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 *
-- 
2.19.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 4/5] tools/firmware/ihex2fw: Simplify next record offset calculation
  2018-12-21  7:28 [PATCH 0/5] IHEX bug fix and some improvements Andrey Smirnov
                   ` (2 preceding siblings ...)
  2018-12-21  7:28 ` [PATCH 3/5] ihex: Simplify next record offset calculation Andrey Smirnov
@ 2018-12-21  7:28 ` Andrey Smirnov
  2018-12-21  7:28 ` [PATCH 5/5] tools/firmware/ihex2fw: Replace explicit alignment with ALIGN Andrey Smirnov
  4 siblings, 0 replies; 6+ messages in thread
From: Andrey Smirnov @ 2018-12-21  7:28 UTC (permalink / raw)
  To: linux-kernel
  Cc: Andrey Smirnov, Chris Healy, Kyle McMartin, Andrew Morton,
	Masahiro Yamada, David Woodhouse, Greg Kroah-Hartman

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 <cphealy@gmail.com>
Cc: Kyle McMartin <kyle@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Masahiro Yamada <yamada.masahiro@socionext.com>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-kernel <linux-kernel@vger.kernel.org>
Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 tools/firmware/ihex2fw.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/tools/firmware/ihex2fw.c b/tools/firmware/ihex2fw.c
index b58dd061e978..e081cef730d8 100644
--- a/tools/firmware/ihex2fw.c
+++ b/tools/firmware/ihex2fw.c
@@ -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);
-- 
2.19.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 5/5] tools/firmware/ihex2fw: Replace explicit alignment with ALIGN
  2018-12-21  7:28 [PATCH 0/5] IHEX bug fix and some improvements Andrey Smirnov
                   ` (3 preceding siblings ...)
  2018-12-21  7:28 ` [PATCH 4/5] tools/firmware/ihex2fw: " Andrey Smirnov
@ 2018-12-21  7:28 ` Andrey Smirnov
  4 siblings, 0 replies; 6+ messages in thread
From: Andrey Smirnov @ 2018-12-21  7:28 UTC (permalink / raw)
  To: linux-kernel
  Cc: Andrey Smirnov, Chris Healy, Kyle McMartin, Andrew Morton,
	Masahiro Yamada, David Woodhouse, Greg Kroah-Hartman

(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 <cphealy@gmail.com>
Cc: Kyle McMartin <kyle@kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Masahiro Yamada <yamada.masahiro@socionext.com>
Cc: David Woodhouse <dwmw2@infradead.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-kernel <linux-kernel@vger.kernel.org>
Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
 tools/firmware/ihex2fw.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/tools/firmware/ihex2fw.c b/tools/firmware/ihex2fw.c
index e081cef730d8..8925b60e51f5 100644
--- a/tools/firmware/ihex2fw.c
+++ b/tools/firmware/ihex2fw.c
@@ -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 */
-- 
2.19.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2018-12-21  7:29 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-12-21  7:28 [PATCH 0/5] IHEX bug fix and some improvements Andrey Smirnov
2018-12-21  7:28 ` [PATCH 1/5] ihex: Share code between ihex_validate_fw() and ihex_next_binrec() Andrey Smirnov
2018-12-21  7:28 ` [PATCH 2/5] ihex: Check if zero-length record is at the end of the blob Andrey Smirnov
2018-12-21  7:28 ` [PATCH 3/5] ihex: Simplify next record offset calculation Andrey Smirnov
2018-12-21  7:28 ` [PATCH 4/5] tools/firmware/ihex2fw: " Andrey Smirnov
2018-12-21  7:28 ` [PATCH 5/5] tools/firmware/ihex2fw: Replace explicit alignment with ALIGN Andrey Smirnov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®