Skip to contentSkip to conversation

mmc: fix signed vs unsigned compare in read check in _spl_load()

PATCH In review Franco Venturi 4 participants 4 messages Jul 31, 2024 – Aug 28, 2024

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 mmc: fix signed vs unsigned compare in read check in _spl_load()

Fix signed vs unsigned compare in read check in _spl_load()

Issue: when info->read() returns a negative value because of an error,
the comparison of 'read' (signed) with 'sizeof(*header)'
(unsigned silently converts the negative value into a very
large unsigned value and the check on the error condition
always return false, i.e. the error is not detected
Symptoms: if spl_load_image_fat() is unable to find the file 'uImage',
the SPL phase of the boot process just hangs after displaying the following line:
Trying to boot from MMC1
Fix: cast 'sizeof(*header)' to int so the compare is now between
signed types
Reference: https://stackoverflow.com/questions/17293749/sizeof-operator-in-if-statement

---

(no changes since v1)

 include/spl_load.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/spl_load.h +1 -1
index 1c2b296c0a..83db381202 100644
@@ -22,7 +22,7 @@ static inline int _spl_load(struct spl_image_info *spl_image,
 
 	read = info->read(info, offset, ALIGN(sizeof(*header),
 					      spl_get_bl_len(info)), header);
-	if (read < sizeof(*header))
+	if (read < (int)sizeof(*header))
 		return -EIO;
 
 	if (image_get_magic(header) == FDT_MAGIC) {
Signature · 1 line
2.45.2
Reviewed-by on v2 1/1
#1 lore raw
Quoted 28 lines · Franco Venturi

Fix signed vs unsigned compare in read check in _spl_load()

Issue: when info->read() returns a negative value because of an error,
the comparison of 'read' (signed) with 'sizeof(*header)' (unsigned silently converts the negative value into a very
large unsigned value and the check on the error condition
always return false, i.e. the error is not detected
Symptoms: if spl_load_image_fat() is unable to find the file 'uImage',
the SPL phase of the boot process just hangs after displaying the following line:
Trying to boot from MMC1
Fix: cast 'sizeof(*header)' to int so the compare is now between
signed types
Reference: https://stackoverflow.com/questions/17293749/sizeof-operator-in-if-statement

---

(no changes since v1)

  include/spl_load.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
include/spl_load.h +0 -0
index 1c2b296c0a..83db381202 100644

include/spl_load.h static inline int _spl_load(struct spl_image_info *spl_image, · line 28

Quoted 8 lines
include/spl_load.h +1 -1
@@ -22,7 +22,7 @@ static inline int _spl_load(struct spl_image_info *spl_image,
 
  	read = info->read(info, offset, ALIGN(sizeof(*header),
  					      spl_get_bl_len(info)), header);
-	if (read < sizeof(*header))
+	if (read < (int)sizeof(*header))
  		return -EIO;
 
  	if (image_get_magic(header) == FDT_MAGIC) {
Reviewed-by on v2 1/1
#2 lore raw
In reply to Franco Venturi
Quoted 40 lines

Subject: [PATCH v2] mmc: fix signed vs unsigned compare in read check in _spl_load()

Fix signed vs unsigned compare in read check in _spl_load()

Issue: when info->read() returns a negative value because of an error,
the comparison of 'read' (signed) with 'sizeof(*header)' (unsigned silently converts the negative value into a very large unsigned value and the check on the error condition always return false, i.e. the error is not detected
Symptoms: if spl_load_image_fat() is unable to find the file 'uImage',
the SPL phase of the boot process just hangs after displaying the following line:
Trying to boot from MMC1
Fix: cast 'sizeof(*header)' to int so the compare is now between
signed types
Reference:

---

(no changes since v1)

 include/spl_load.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/spl_load.h index renamed from include/spl_load.h +0 -0

1c2b296c0a..83db381202 100644

include/spl_load.h +1 -1
@@ -22,7 +22,7 @@ static inline int _spl_load(struct spl_image_info
 *spl_image,
 
 	read = info->read(info, offset, ALIGN(sizeof(*header),
 					      spl_get_bl_len(info)),
 header);
-	if (read < sizeof(*header))
+	if (read < (int)sizeof(*header))
 		return -EIO;
Quoted 2 lines

if (image_get_magic(header) == FDT_MAGIC) {
--

#3 lore raw
In reply to Franco Venturi
Quoted 15 lines · Franco Venturi

Fix signed vs unsigned compare in read check in _spl_load()

Issue: when info->read() returns a negative value because of an error,
the comparison of 'read' (signed) with 'sizeof(*header)'
(unsigned silently converts the negative value into a very
large unsigned value and the check on the error condition
always return false, i.e. the error is not detected
Symptoms: if spl_load_image_fat() is unable to find the file 'uImage',
the SPL phase of the boot process just hangs after displaying the following line:
Trying to boot from MMC1
Fix: cast 'sizeof(*header)' to int so the compare is now between
signed types
Reference: https://stackoverflow.com/questions/17293749/sizeof-operator-in-if-statement

Quoted 1 line

[...]

Applied to u-boot/next, thanks!

Signature · 1 line
Tom