Skip to contentSkip to conversation

Fix size comparison bug and use flexible array

PATCH Superseded Gustavo A. R. Silva 4 participants 10 messages Jul 30, 2021 – Aug 10, 2021

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover Fix size comparison bug and use flexible array

Hi all,

This small series aims to fix a size comparison bug and replace a one-element array with a flexible-array member.

Thanks

Changes in v2:
- Use flexible array and adjust relational operator in patch 1.

Gustavo A. R. Silva (2):
media: staging/intel-ipu3: css: Fix wrong size comparison
media: staging/intel-ipu3: css: Use the struct_size() helper

 drivers/staging/media/ipu3/ipu3-css-fw.c | 7 +++----
 drivers/staging/media/ipu3/ipu3-css-fw.h | 2 +-
 2 files changed, 4 insertions(+), 5 deletions(-)
Signature · 1 line
2.27.0
#1 lore raw

PATCH v2 1/2 media: staging/intel-ipu3: css: Fix wrong size comparison

There is a wrong comparison of the total size of the loaded firmware css->fw->size with the size of a pointer to struct imgu_fw_header.

Fix this by using the right operand 'struct imgu_fw_header' for sizeof, instead of 'struct imgu_fw_header *' and turn binary_header into a flexible-array member. Also, adjust the relational operator to be '<=' instead of '<', as it seems that the intention of the comparison is to determine if the loaded firmware contains any 'struct imgu_fw_info' items in the binary_header[] array than merely the file_header (struct imgu_fw_bi_file_h).

The replacement of the one-element array with a flexible-array member also help with the ongoing efforts to globally enable -Warray-bounds and get us closer to being able to tighten the FORTIFY_SOURCE routines on memcpy().

Fixes 09d290f0ba21 ("media: staging/intel-ipu3: css: Add support for firmware management")

---

It'd be just great if someone that knows this code better can confirm these changes are correct. In particular the adjustment of the relational operator. Thanks!

Changes in v2:
- Use flexible array and adjust relational operator, accordingly.
- Update changelog text.

 drivers/staging/media/ipu3/ipu3-css-fw.c | 2 +-
 drivers/staging/media/ipu3/ipu3-css-fw.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/staging/media/ipu3/ipu3-css-fw.c +1 -1
index 45aff76198e2..630cb5186b48 100644
@@ -124,7 +124,7 @@ int imgu_css_fw_init(struct imgu_css *css)
 	/* Check and display fw header info */
 
 	css->fwp = (struct imgu_fw_header *)css->fw->data;
-	if (css->fw->size < sizeof(struct imgu_fw_header *) ||
+	if (css->fw->size <= sizeof(struct imgu_fw_header) ||
 	    css->fwp->file_header.h_size != sizeof(struct imgu_fw_bi_file_h))
 		goto bad_fw;
 	if (sizeof(struct imgu_fw_bi_file_h) +
drivers/staging/media/ipu3/ipu3-css-fw.h +1 -1
index 3c078f15a295..c0bc57fd678a 100644
@@ -171,7 +171,7 @@ struct imgu_fw_bi_file_h {
 
 struct imgu_fw_header {
 	struct imgu_fw_bi_file_h file_header;
-	struct imgu_fw_info binary_header[1];	/* binary_nr items */
+	struct imgu_fw_info binary_header[];	/* binary_nr items */
 };
 
 /******************* Firmware functions *******************/
Signature · 1 line
2.27.0
maintainer
#2 lore raw

Re: [PATCH v2 1/2] media: staging/intel-ipu3: css: Fix wrong size comparison

Hi Gustavo,

I missed you already had sent v2...

Quoted 21 lines · Gustavo A. R. Silva

There is a wrong comparison of the total size of the loaded firmware css->fw->size with the size of a pointer to struct imgu_fw_header.

Fix this by using the right operand 'struct imgu_fw_header' for sizeof, instead of 'struct imgu_fw_header *' and turn binary_header into a flexible-array member. Also, adjust the relational operator to be '<=' instead of '<', as it seems that the intention of the comparison is to determine if the loaded firmware contains any 'struct imgu_fw_info' items in the binary_header[] array than merely the file_header (struct imgu_fw_bi_file_h).

The replacement of the one-element array with a flexible-array member also help with the ongoing efforts to globally enable -Warray-bounds and get us closer to being able to tighten the FORTIFY_SOURCE routines on memcpy().

Fixes 09d290f0ba21 ("media: staging/intel-ipu3: css: Add support for firmware management")
Quoted 8 lines

---

It'd be just great if someone that knows this code better can confirm these changes are correct. In particular the adjustment of the relational operator. Thanks!

Changes in v2:
- Use flexible array and adjust relational operator, accordingly.

The operator was just correct. The check is just there to see the firmware is at least as large as the struct as which it is being accessed.

Quoted 32 lines

- Update changelog text.

 drivers/staging/media/ipu3/ipu3-css-fw.c | 2 +-
 drivers/staging/media/ipu3/ipu3-css-fw.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/staging/media/ipu3/ipu3-css-fw.c +1 -1
index 45aff76198e2..630cb5186b48 100644
@@ -124,7 +124,7 @@ int imgu_css_fw_init(struct imgu_css *css)
 	/* Check and display fw header info */
 
 	css->fwp = (struct imgu_fw_header *)css->fw->data;
-	if (css->fw->size < sizeof(struct imgu_fw_header *) ||
+	if (css->fw->size <= sizeof(struct imgu_fw_header) ||
 	    css->fwp->file_header.h_size != sizeof(struct imgu_fw_bi_file_h))
 		goto bad_fw;
 	if (sizeof(struct imgu_fw_bi_file_h) +
drivers/staging/media/ipu3/ipu3-css-fw.h +1 -1
index 3c078f15a295..c0bc57fd678a 100644
@@ -171,7 +171,7 @@ struct imgu_fw_bi_file_h {
 
 struct imgu_fw_header {
 	struct imgu_fw_bi_file_h file_header;
-	struct imgu_fw_info binary_header[1];	/* binary_nr items */
+	struct imgu_fw_info binary_header[];	/* binary_nr items */
 };
 
 /******************* Firmware functions *******************/
Signature · 3 lines
Regards,

Sakari Ailus
#3 lore raw

Re: [PATCH v2 1/2] media: staging/intel-ipu3: css: Fix wrong size comparison

Hi Sakari,

Quoted 32 lines · Sakari Ailus

Hi Gustavo,

I missed you already had sent v2...

Quoted 27 lines · Gustavo A. R. Silva

There is a wrong comparison of the total size of the loaded firmware css->fw->size with the size of a pointer to struct imgu_fw_header.

Fix this by using the right operand 'struct imgu_fw_header' for sizeof, instead of 'struct imgu_fw_header *' and turn binary_header into a flexible-array member. Also, adjust the relational operator to be '<=' instead of '<', as it seems that the intention of the comparison is to determine if the loaded firmware contains any 'struct imgu_fw_info' items in the binary_header[] array than merely the file_header (struct imgu_fw_bi_file_h).

The replacement of the one-element array with a flexible-array member also help with the ongoing efforts to globally enable -Warray-bounds and get us closer to being able to tighten the FORTIFY_SOURCE routines on memcpy().

Fixes 09d290f0ba21 ("media: staging/intel-ipu3: css: Add support for firmware management")

---

It'd be just great if someone that knows this code better can confirm these changes are correct. In particular the adjustment of the relational operator. Thanks!

Quoted 5 lines
Quoted 2 lines

Changes in v2:
- Use flexible array and adjust relational operator, accordingly.

The operator was just correct. The check is just there to see the firmware is at least as large as the struct as which it is being accessed.

I'm a bit confused, so based on your reply to v1 of this series, this patch is now correct, right?

The operator in v1 _was_ correct as long as the one-element array wasn't transformed into a flexible array, right?

Notice that generally speaking flexible-array members don't occupy space in the containing structure:

$ pahole -C imgu_fw_header drivers/staging/media/ipu3/ipu3-css-fw.o
struct imgu_fw_header {
	struct imgu_fw_bi_file_h   file_header;          /*     0    72 */
	/* --- cacheline 1 boundary (64 bytes) was 8 bytes ago --- */
	struct imgu_fw_info        binary_header[] __attribute__((__aligned__(8))); /*    72     0 */
	/* size: 72, cachelines: 2, members: 2 */
	/* forced alignments: 1 */
	/* last cacheline: 8 bytes */
} __attribute__((__aligned__(8)));
$ pahole -C imgu_fw_header drivers/staging/media/ipu3/ipu3-css-fw.o
struct imgu_fw_header {
	struct imgu_fw_bi_file_h   file_header;          /*     0    72 */
	/* --- cacheline 1 boundary (64 bytes) was 8 bytes ago --- */
	struct imgu_fw_info        binary_header[1] __attribute__((__aligned__(8))); /*    72  1200 */
	/* size: 1272, cachelines: 20, members: 2 */
	/* forced alignments: 1 */
	/* last cacheline: 56 bytes */
} __attribute__((__aligned__(8)));

So, now that the flexible array transformation is included in the same patch as the bugfix, the operator is changed from '<' to '<='

Can you please confirm if this v2 is now correct? if so, it'd be great to have your Reviewed-by tag. :)

Thanks!
--
Gustavo

Quoted 34 lines
Quoted 33 lines

- Update changelog text.

 drivers/staging/media/ipu3/ipu3-css-fw.c | 2 +-
 drivers/staging/media/ipu3/ipu3-css-fw.h | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/staging/media/ipu3/ipu3-css-fw.c +1 -1
index 45aff76198e2..630cb5186b48 100644
@@ -124,7 +124,7 @@ int imgu_css_fw_init(struct imgu_css *css)
 	/* Check and display fw header info */
 
 	css->fwp = (struct imgu_fw_header *)css->fw->data;
-	if (css->fw->size < sizeof(struct imgu_fw_header *) ||
+	if (css->fw->size <= sizeof(struct imgu_fw_header) ||
 	    css->fwp->file_header.h_size != sizeof(struct imgu_fw_bi_file_h))
 		goto bad_fw;
 	if (sizeof(struct imgu_fw_bi_file_h) +
drivers/staging/media/ipu3/ipu3-css-fw.h +1 -1
index 3c078f15a295..c0bc57fd678a 100644
@@ -171,7 +171,7 @@ struct imgu_fw_bi_file_h {
 
 struct imgu_fw_header {
 	struct imgu_fw_bi_file_h file_header;
-	struct imgu_fw_info binary_header[1];	/* binary_nr items */
+	struct imgu_fw_info binary_header[];	/* binary_nr items */
 };
 
 /******************* Firmware functions *******************/
#4 lore raw

Re: [PATCH v2 1/2] media: staging/intel-ipu3: css: Fix wrong size comparison

I missed that you change < sizeof() to <= sizeof()... Sakari is right that it should be < sizeof(). <= doesn't work at all ever. If you wanted a higher limit then the next limit woule be:

	if (css->fw->size < struct_size(css->fwp, binary_header, 1))

regards,
dan carpenter

maintainer
#5 lore raw

Re: [PATCH v2 1/2] media: staging/intel-ipu3: css: Fix wrong size comparison

In reply to Gustavo A. R. Silva

Hi Gustavo,

Apologies for the delay.

Quoted 67 lines · Gustavo A. R. Silva

Hi Sakari,

Quoted 32 lines · Sakari Ailus

Hi Gustavo,

I missed you already had sent v2...

Quoted 27 lines · Gustavo A. R. Silva

There is a wrong comparison of the total size of the loaded firmware css->fw->size with the size of a pointer to struct imgu_fw_header.

Fix this by using the right operand 'struct imgu_fw_header' for sizeof, instead of 'struct imgu_fw_header *' and turn binary_header into a flexible-array member. Also, adjust the relational operator to be '<=' instead of '<', as it seems that the intention of the comparison is to determine if the loaded firmware contains any 'struct imgu_fw_info' items in the binary_header[] array than merely the file_header (struct imgu_fw_bi_file_h).

The replacement of the one-element array with a flexible-array member also help with the ongoing efforts to globally enable -Warray-bounds and get us closer to being able to tighten the FORTIFY_SOURCE routines on memcpy().

Fixes 09d290f0ba21 ("media: staging/intel-ipu3: css: Add support for firmware management")

---

It'd be just great if someone that knows this code better can confirm these changes are correct. In particular the adjustment of the relational operator. Thanks!

Quoted 5 lines
Quoted 2 lines

Changes in v2:
- Use flexible array and adjust relational operator, accordingly.

The operator was just correct. The check is just there to see the firmware is at least as large as the struct as which it is being accessed.

I'm a bit confused, so based on your reply to v1 of this series, this patch is now correct, right?

The operator in v1 _was_ correct as long as the one-element array wasn't transformed into a flexible array, right?

Notice that generally speaking flexible-array members don't occupy space in the containing structure:

$ pahole -C imgu_fw_header drivers/staging/media/ipu3/ipu3-css-fw.o
struct imgu_fw_header {
	struct imgu_fw_bi_file_h   file_header;          /*     0    72 */
	/* --- cacheline 1 boundary (64 bytes) was 8 bytes ago --- */
	struct imgu_fw_info        binary_header[] __attribute__((__aligned__(8))); /*    72     0 */
	/* size: 72, cachelines: 2, members: 2 */
	/* forced alignments: 1 */
	/* last cacheline: 8 bytes */
} __attribute__((__aligned__(8)));
$ pahole -C imgu_fw_header drivers/staging/media/ipu3/ipu3-css-fw.o
struct imgu_fw_header {
	struct imgu_fw_bi_file_h   file_header;          /*     0    72 */
	/* --- cacheline 1 boundary (64 bytes) was 8 bytes ago --- */
	struct imgu_fw_info        binary_header[1] __attribute__((__aligned__(8))); /*    72  1200 */
Quoted 7 lines
	/* size: 1272, cachelines: 20, members: 2 */
	/* forced alignments: 1 */
	/* last cacheline: 56 bytes */
} __attribute__((__aligned__(8)));

So, now that the flexible array transformation is included in the same patch as the bugfix, the operator is changed from '<' to '<='

'<' is correct since you only need as much data as the struct you're about to access is large, not a byte more than that. As Dan noted.

I think you could add a check for binary_nr is at least one.

Signature · 3 lines
Kind regards,

Sakari Ailus
#6 lore raw

Re: [PATCH v2 1/2] media: staging/intel-ipu3: css: Fix wrong size comparison

Hi Sakari,

Please, see my comments below...

Quoted 77 lines · Sakari Ailus

Hi Gustavo,

Apologies for the delay.

Quoted 72 lines · Gustavo A. R. Silva

Hi Sakari,

Quoted 32 lines · Sakari Ailus

Hi Gustavo,

I missed you already had sent v2...

Quoted 27 lines · Gustavo A. R. Silva

There is a wrong comparison of the total size of the loaded firmware css->fw->size with the size of a pointer to struct imgu_fw_header.

Fix this by using the right operand 'struct imgu_fw_header' for sizeof, instead of 'struct imgu_fw_header *' and turn binary_header into a flexible-array member. Also, adjust the relational operator to be '<=' instead of '<', as it seems that the intention of the comparison is to determine if the loaded firmware contains any 'struct imgu_fw_info' items in the binary_header[] array than merely the file_header (struct imgu_fw_bi_file_h).

The replacement of the one-element array with a flexible-array member also help with the ongoing efforts to globally enable -Warray-bounds and get us closer to being able to tighten the FORTIFY_SOURCE routines on memcpy().

Fixes 09d290f0ba21 ("media: staging/intel-ipu3: css: Add support for firmware management")

---

It'd be just great if someone that knows this code better can confirm these changes are correct. In particular the adjustment of the relational operator. Thanks!

Quoted 5 lines
Quoted 2 lines

Changes in v2:
- Use flexible array and adjust relational operator, accordingly.

The operator was just correct. The check is just there to see the firmware is at least as large as the struct as which it is being accessed.

I'm a bit confused, so based on your reply to v1 of this series, this patch is now correct, right?

The operator in v1 _was_ correct as long as the one-element array wasn't transformed into a flexible array, right?

Notice that generally speaking flexible-array members don't occupy space in the containing structure:

$ pahole -C imgu_fw_header drivers/staging/media/ipu3/ipu3-css-fw.o
struct imgu_fw_header {
	struct imgu_fw_bi_file_h   file_header;          /*     0    72 */
	/* --- cacheline 1 boundary (64 bytes) was 8 bytes ago --- */
	struct imgu_fw_info        binary_header[] __attribute__((__aligned__(8))); /*    72     0 */
	/* size: 72, cachelines: 2, members: 2 */
	/* forced alignments: 1 */
	/* last cacheline: 8 bytes */
} __attribute__((__aligned__(8)));
$ pahole -C imgu_fw_header drivers/staging/media/ipu3/ipu3-css-fw.o
struct imgu_fw_header {
	struct imgu_fw_bi_file_h   file_header;          /*     0    72 */
	/* --- cacheline 1 boundary (64 bytes) was 8 bytes ago --- */
	struct imgu_fw_info        binary_header[1] __attribute__((__aligned__(8))); /*    72  1200 */
	/* size: 1272, cachelines: 20, members: 2 */
	/* forced alignments: 1 */
	/* last cacheline: 56 bytes */
} __attribute__((__aligned__(8)));
Quoted 7 lines
Quoted 2 lines

So, now that the flexible array transformation is included in the same patch as the bugfix, the operator is changed from '<' to '<='

'<' is correct since you only need as much data as the struct you're about to access is large, not a byte more than that. As Dan noted.

I think you could add a check for binary_nr is at least one.

If we need to check that binary_nr is at least one, then this would be the right change:

        css->fwp = (struct imgu_fw_header *)css->fw->data;
-       if (css->fw->size < sizeof(struct imgu_fw_header *) ||
+       if (css->fw->size < struct_size(css->fwp, binary_header, 1) ||
            css->fwp->file_header.h_size != sizeof(struct imgu_fw_bi_file_h))
                goto bad_fw;

--
Gustavo

maintainer
#7 lore raw

Re: [PATCH v2 1/2] media: staging/intel-ipu3: css: Fix wrong size comparison

Quoted 94 lines · Gustavo A. R. Silva

Hi Sakari,

Please, see my comments below...

Quoted 77 lines · Sakari Ailus

Hi Gustavo,

Apologies for the delay.

Quoted 72 lines · Gustavo A. R. Silva

Hi Sakari,

Quoted 32 lines · Sakari Ailus

Hi Gustavo,

I missed you already had sent v2...

Quoted 27 lines · Gustavo A. R. Silva

There is a wrong comparison of the total size of the loaded firmware css->fw->size with the size of a pointer to struct imgu_fw_header.

Fix this by using the right operand 'struct imgu_fw_header' for sizeof, instead of 'struct imgu_fw_header *' and turn binary_header into a flexible-array member. Also, adjust the relational operator to be '<=' instead of '<', as it seems that the intention of the comparison is to determine if the loaded firmware contains any 'struct imgu_fw_info' items in the binary_header[] array than merely the file_header (struct imgu_fw_bi_file_h).

The replacement of the one-element array with a flexible-array member also help with the ongoing efforts to globally enable -Warray-bounds and get us closer to being able to tighten the FORTIFY_SOURCE routines on memcpy().

Fixes 09d290f0ba21 ("media: staging/intel-ipu3: css: Add support for firmware management")

---

It'd be just great if someone that knows this code better can confirm these changes are correct. In particular the adjustment of the relational operator. Thanks!

Quoted 5 lines
Quoted 2 lines

Changes in v2:
- Use flexible array and adjust relational operator, accordingly.

The operator was just correct. The check is just there to see the firmware is at least as large as the struct as which it is being accessed.

I'm a bit confused, so based on your reply to v1 of this series, this patch is now correct, right?

The operator in v1 _was_ correct as long as the one-element array wasn't transformed into a flexible array, right?

Notice that generally speaking flexible-array members don't occupy space in the containing structure:

$ pahole -C imgu_fw_header drivers/staging/media/ipu3/ipu3-css-fw.o
struct imgu_fw_header {
	struct imgu_fw_bi_file_h   file_header;          /*     0    72 */
	/* --- cacheline 1 boundary (64 bytes) was 8 bytes ago --- */
	struct imgu_fw_info        binary_header[] __attribute__((__aligned__(8))); /*    72     0 */
	/* size: 72, cachelines: 2, members: 2 */
	/* forced alignments: 1 */
	/* last cacheline: 8 bytes */
} __attribute__((__aligned__(8)));
$ pahole -C imgu_fw_header drivers/staging/media/ipu3/ipu3-css-fw.o
struct imgu_fw_header {
	struct imgu_fw_bi_file_h   file_header;          /*     0    72 */
	/* --- cacheline 1 boundary (64 bytes) was 8 bytes ago --- */
	struct imgu_fw_info        binary_header[1] __attribute__((__aligned__(8))); /*    72  1200 */
	/* size: 1272, cachelines: 20, members: 2 */
	/* forced alignments: 1 */
	/* last cacheline: 56 bytes */
} __attribute__((__aligned__(8)));
Quoted 7 lines
Quoted 2 lines

So, now that the flexible array transformation is included in the same patch as the bugfix, the operator is changed from '<' to '<='

'<' is correct since you only need as much data as the struct you're about to access is large, not a byte more than that. As Dan noted.

I think you could add a check for binary_nr is at least one.

If we need to check that binary_nr is at least one, then this would be the right change:

        css->fwp = (struct imgu_fw_header *)css->fw->data;

drivers/staging/media/ipu3/ipu3-css-fw.c int imgu_css_fw_init(struct imgu_css *css) · line 129

Quoted 4 lines
drivers/staging/media/ipu3/ipu3-css-fw.c +1 -1
-       if (css->fw->size < sizeof(struct imgu_fw_header *) ||
+       if (css->fw->size < struct_size(css->fwp, binary_header, 1) ||
            css->fwp->file_header.h_size != sizeof(struct imgu_fw_bi_file_h))
                goto bad_fw;

There's already a check the space required for the array of binary_nr is there. But not the number itself.

Signature · 1 line
Sakari Ailus
#8 lore raw

Re: [PATCH v2 1/2] media: staging/intel-ipu3: css: Fix wrong size comparison

Quoted 94 lines · Sakari Ailus
Quoted 93 lines · Gustavo A. R. Silva

Hi Sakari,

Please, see my comments below...

Quoted 77 lines · Sakari Ailus

Hi Gustavo,

Apologies for the delay.

Quoted 72 lines · Gustavo A. R. Silva

Hi Sakari,

Quoted 32 lines · Sakari Ailus

Hi Gustavo,

I missed you already had sent v2...

Quoted 27 lines · Gustavo A. R. Silva

There is a wrong comparison of the total size of the loaded firmware css->fw->size with the size of a pointer to struct imgu_fw_header.

Fix this by using the right operand 'struct imgu_fw_header' for sizeof, instead of 'struct imgu_fw_header *' and turn binary_header into a flexible-array member. Also, adjust the relational operator to be '<=' instead of '<', as it seems that the intention of the comparison is to determine if the loaded firmware contains any 'struct imgu_fw_info' items in the binary_header[] array than merely the file_header (struct imgu_fw_bi_file_h).

The replacement of the one-element array with a flexible-array member also help with the ongoing efforts to globally enable -Warray-bounds and get us closer to being able to tighten the FORTIFY_SOURCE routines on memcpy().

Fixes 09d290f0ba21 ("media: staging/intel-ipu3: css: Add support for firmware management")

---

It'd be just great if someone that knows this code better can confirm these changes are correct. In particular the adjustment of the relational operator. Thanks!

Quoted 5 lines
Quoted 2 lines

Changes in v2:
- Use flexible array and adjust relational operator, accordingly.

The operator was just correct. The check is just there to see the firmware is at least as large as the struct as which it is being accessed.

I'm a bit confused, so based on your reply to v1 of this series, this patch is now correct, right?

The operator in v1 _was_ correct as long as the one-element array wasn't transformed into a flexible array, right?

Notice that generally speaking flexible-array members don't occupy space in the containing structure:

$ pahole -C imgu_fw_header drivers/staging/media/ipu3/ipu3-css-fw.o
struct imgu_fw_header {
	struct imgu_fw_bi_file_h   file_header;          /*     0    72 */
	/* --- cacheline 1 boundary (64 bytes) was 8 bytes ago --- */
	struct imgu_fw_info        binary_header[] __attribute__((__aligned__(8))); /*    72     0 */
	/* size: 72, cachelines: 2, members: 2 */
	/* forced alignments: 1 */
	/* last cacheline: 8 bytes */
} __attribute__((__aligned__(8)));
$ pahole -C imgu_fw_header drivers/staging/media/ipu3/ipu3-css-fw.o
struct imgu_fw_header {
	struct imgu_fw_bi_file_h   file_header;          /*     0    72 */
	/* --- cacheline 1 boundary (64 bytes) was 8 bytes ago --- */
	struct imgu_fw_info        binary_header[1] __attribute__((__aligned__(8))); /*    72  1200 */
	/* size: 1272, cachelines: 20, members: 2 */
	/* forced alignments: 1 */
	/* last cacheline: 56 bytes */
} __attribute__((__aligned__(8)));
Quoted 7 lines
Quoted 2 lines

So, now that the flexible array transformation is included in the same patch as the bugfix, the operator is changed from '<' to '<='

'<' is correct since you only need as much data as the struct you're about to access is large, not a byte more than that. As Dan noted.

I think you could add a check for binary_nr is at least one.

If we need to check that binary_nr is at least one, then this would be the right change:

Quoted 8 lines

Review

Quoted 5 lines
        css->fwp = (struct imgu_fw_header *)css->fw->data;
(quoted diff) +1 -1
-       if (css->fw->size < sizeof(struct imgu_fw_header *) ||
+       if (css->fw->size < struct_size(css->fwp, binary_header, 1) ||
            css->fwp->file_header.h_size != sizeof(struct imgu_fw_bi_file_h))
                goto bad_fw;

There's already a check the space required for the array of binary_nr is there. But not the number itself.

Yep; and that is for the upper limit.

The whole fix would be this:

drivers/staging/media/ipu3/ipu3-css-fw.c +3 -4
index 45aff76198e2..8830f42f2b12 100644
@@ -124,12 +124,11 @@ int imgu_css_fw_init(struct imgu_css *css)
        /* Check and display fw header info */
 
        css->fwp = (struct imgu_fw_header *)css->fw->data;
-       if (css->fw->size < sizeof(struct imgu_fw_header *) ||
+       if (css->fw->size < struct_size(css->fwp, binary_header, 1) ||
            css->fwp->file_header.h_size != sizeof(struct imgu_fw_bi_file_h))
                goto bad_fw;
-       if (sizeof(struct imgu_fw_bi_file_h) +
-           css->fwp->file_header.binary_nr * sizeof(struct imgu_fw_info) >
-           css->fw->size)
+       if (struct_size((struct imgu_fw_header *)0, binary_header,
+                       css->fwp->file_header.binary_nr) > css->fw->size)
                goto bad_fw;
 
        dev_info(dev, "loaded firmware version %.64s, %u binaries, %zu bytes\n",
drivers/staging/media/ipu3/ipu3-css-fw.h +1 -1
index 3c078f15a295..c0bc57fd678a 100644
@@ -171,7 +171,7 @@ struct imgu_fw_bi_file_h {
 
 struct imgu_fw_header {
        struct imgu_fw_bi_file_h file_header;
-       struct imgu_fw_info binary_header[1];   /* binary_nr items */
+       struct imgu_fw_info binary_header[];    /* binary_nr items */
 };
 
 /******************* Firmware functions *******************/

Notice that "css->fw->size < struct_size(css->fwp, binary_header, 1)" with binary_header declared as a flexible-array member is equivalent to "css->fw->size < sizeof(struct imgu_fw_header)" with binary_header declared as a one-element array.

--
Gustavo

#9 lore raw

PATCH v2 2/2 media: staging/intel-ipu3: css: Use the struct_size() helper

In reply to Gustavo A. R. Silva

Make use of the struct_size() helper instead of an open-coded version.

---
Changes in v2:
- Move the replacement of one-element array with a flexible-array member to patch 1 of the series and update changelog text, accordingly.

 drivers/staging/media/ipu3/ipu3-css-fw.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/staging/media/ipu3/ipu3-css-fw.c +2 -3
index 630cb5186b48..b9c850fc9fe4 100644
@@ -127,9 +127,8 @@ int imgu_css_fw_init(struct imgu_css *css)
 	if (css->fw->size <= sizeof(struct imgu_fw_header) ||
 	    css->fwp->file_header.h_size != sizeof(struct imgu_fw_bi_file_h))
 		goto bad_fw;
-	if (sizeof(struct imgu_fw_bi_file_h) +
-	    css->fwp->file_header.binary_nr * sizeof(struct imgu_fw_info) >
-	    css->fw->size)
+	if (struct_size(css->fwp, binary_header,
+			css->fwp->file_header.binary_nr) > css->fw->size)
 		goto bad_fw;
 
 	dev_info(dev, "loaded firmware version %.64s, %u binaries, %zu bytes\n",
Signature · 1 line
2.27.0