Skip to contentSkip to conversation

spl: spl_load: fix comparison between negative error code and unsigned size

PATCH In review Daniel Palmer 4 participants 4 messages Aug 31, 2024 – Sep 11, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spl: spl_load: fix comparison between negative error code and unsigned size

read could be a negative error value but size in spl_image is unsigned so when they are compared read is used as if it's a unsigned value and if it's negative it'll most likely be bigger than size and the result will be true and _spl_load() will return 0 to the caller.

This results in the caller to _spl_load() not seeing that an error happened as it should and continuing as if the load was completed when it might not have been.

Check if read is negative and return it's value if it is before comparing against size in spl_image.

---
 include/spl_load.h | 4 ++++
 1 file changed, 4 insertions(+)
include/spl_load.h +4 -0
index 1c2b296c0a2c..7de834f402b8 100644
@@ -83,6 +83,10 @@ static inline int _spl_load(struct spl_image_info *spl_image,
 
 	read = info->read(info, offset + image_offset, size,
 			  map_sysmem(spl_image->load_addr - overhead, size));
+
+	if (read < 0)
+		return read;
+
 	return read < spl_image->size ? -EIO : 0;
 }
 
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw

Hi Daniel,

Quoted 10 lines · Daniel Palmer

read could be a negative error value but size in spl_image is unsigned so when they are compared read is used as if it's a unsigned value and if it's negative it'll most likely be bigger than size and the result will be true and _spl_load() will return 0 to the caller.

This results in the caller to _spl_load() not seeing that an error happened as it should and continuing as if the load was completed when it might not have been.

Quoted 7 lines

Check if read is negative and return it's value if it is before comparing against size in spl_image.

---
 include/spl_load.h | 4 ++++
 1 file changed, 4 insertions(+)
Quoted 19 lines
include/spl_load.h +4 -1
index 1c2b296c0a2c..7de834f402b8 100644
@@ -83,6 +83,10 @@ static inline int _spl_load(struct spl_image_info *spl_image,
 
        read = info->read(info, offset + image_offset, size,
                          map_sysmem(spl_image->load_addr - overhead, size));
+
+       if (read < 0)
+               return read;
+
        return read < spl_image->size ? -EIO : 0;
 }
 
--

2.43.0

#2 lore raw
In reply to Daniel Palmer
Quoted 10 lines · Daniel Palmer

read could be a negative error value but size in spl_image is unsigned so when they are compared read is used as if it's a unsigned value and if it's negative it'll most likely be bigger than size and the result will be true and _spl_load() will return 0 to the caller.

This results in the caller to _spl_load() not seeing that an error happened as it should and continuing as if the load was completed when it might not have been.

[...]

Applied to u-boot/next, thanks!

Signature · 1 line
Tom
#3 lore raw
In reply to Daniel Palmer
Quoted 4 lines · Daniel Palmer

read could be a negative error value but size in spl_image is unsigned so when they are compared read is used as if it's a unsigned value and if it's negative it'll most likely be bigger than size and the result will be true and _spl_load() will return 0 to the caller.

Then cast spl_image->size to long.

Quoted 27 lines

This results in the caller to _spl_load() not seeing that an error happened as it should and continuing as if the load was completed when it might not have been.

Check if read is negative and return it's value if it is before comparing against size in spl_image.

---
  include/spl_load.h | 4 ++++
  1 file changed, 4 insertions(+)
include/spl_load.h +4 -0
index 1c2b296c0a2c..7de834f402b8 100644
@@ -83,6 +83,10 @@ static inline int _spl_load(struct spl_image_info *spl_image,
 
  	read = info->read(info, offset + image_offset, size,
  			  map_sysmem(spl_image->load_addr - overhead, size));
+
+	if (read < 0)
+		return read;
+
  	return read < spl_image->size ? -EIO : 0;
  }