Skip to contentSkip to conversation

fastboot: Fix overflow when calculating chunk size

PATCH In review Sean Anderson 4 participants 7 messages Apr 16, 2021 – May 26, 2021 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fastboot: Fix overflow when calculating chunk size

If a chunk was larger than 4GiB, then chunk_data_sz would overflow and blkcnt would not be calculated correctly. Upgrade it to a u64 and cast its multiplicands as well. Also fix bytes_written while we're at it.

---

 lib/image-sparse.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
lib/image-sparse.c +6 -6
index 187ac28cd3..52c8dcc08c 100644
@@ -55,10 +55,10 @@ int write_sparse_image(struct sparse_storage *info,
 	lbaint_t blk;
 	lbaint_t blkcnt;
 	lbaint_t blks;
-	uint32_t bytes_written = 0;
+	uint64_t bytes_written = 0;
 	unsigned int chunk;
 	unsigned int offset;
-	unsigned int chunk_data_sz;
+	uint64_t chunk_data_sz;
 	uint32_t *fill_buf = NULL;
 	uint32_t fill_val;
 	sparse_header_t *sparse_header;
@@ -132,7 +132,7 @@ int write_sparse_image(struct sparse_storage *info,
 				 sizeof(chunk_header_t));
 		}
 
-		chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
+		chunk_data_sz = ((u64)sparse_header->blk_sz) * chunk_header->chunk_sz;
 		blkcnt = chunk_data_sz / info->blksz;
 		switch (chunk_header->chunk_type) {
 		case CHUNK_TYPE_RAW:
@@ -162,7 +162,7 @@ int write_sparse_image(struct sparse_storage *info,
 				return -1;
 			}
 			blk += blks;
-			bytes_written += blkcnt * info->blksz;
+			bytes_written += ((u64)blkcnt) * info->blksz;
 			total_blocks += chunk_header->chunk_sz;
 			data += chunk_data_sz;
 			break;
@@ -222,7 +222,7 @@ int write_sparse_image(struct sparse_storage *info,
 				blk += blks;
 				i += j;
 			}
-			bytes_written += blkcnt * info->blksz;
+			bytes_written += ((u64)blkcnt) * info->blksz;
 			total_blocks += chunk_data_sz / sparse_header->blk_sz;
 			free(fill_buf);
 			break;
@@ -253,7 +253,7 @@ int write_sparse_image(struct sparse_storage *info,
 
 	debug("Wrote %d blocks, expected to write %d blocks\n",
 	      total_blocks, sparse_header->total_blks);
-	printf("........ wrote %u bytes to '%s'\n", bytes_written, part_name);
+	printf("........ wrote %llu bytes to '%s'\n", bytes_written, part_name);
 
 	if (total_blocks != sparse_header->total_blks) {
 		info->mssg("sparse image write failure", response);
Signature · 1 line
2.25.1
#1 lore raw

PATCH 1/1 fastboot: Fix overflow when calculating chunk size

Hello Sean,

Quoted 9 lines · Sean Anderson

If a chunk was larger than 4GiB, then chunk_data_sz would overflow and blkcnt would not be calculated correctly. Upgrade it to a u64 and cast its multiplicands as well. Also fix bytes_written while we're at it.

---

 lib/image-sparse.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

Wow chunk size larger than 4G!

But patch seems valid, so

bye,
Heiko

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-52   Fax: +49-8142-66989-80   Email: hs at denx.de
#2 lore raw

PATCH 1/1 fastboot: Fix overflow when calculating chunk size

Quoted 7 lines · Heiko Schocher

Hello Sean,

Quoted 4 lines · Sean Anderson

If a chunk was larger than 4GiB, then chunk_data_sz would overflow and blkcnt would not be calculated correctly. Upgrade it to a u64 and cast its multiplicands as well. Also fix bytes_written while we're at it.

Quoted 7 lines
Quoted 5 lines

---

  lib/image-sparse.c | 12 ++++++------
  1 file changed, 6 insertions(+), 6 deletions(-)

Wow chunk size larger than 4G!

Normally filesystems like ext4 put data all around the disk (i.e. the used-block bitmap). However, if you use a log filesystem (like F2FS), then all the data is at the beginning of the disk. In that case, it is fairly easy to end up with a 4GiB or greater "don't care" chunk taking up most of the disk.

--Sean

Quoted 8 lines

But patch seems valid, so

bye,
Heiko

#3 lore raw

PATCH 1/1 fastboot: Fix overflow when calculating chunk size

In reply to Sean Anderson

Hi Lukasz,

Can this make it into 2020.07? Thanks,

--Sean

Quoted 64 lines · Sean Anderson

If a chunk was larger than 4GiB, then chunk_data_sz would overflow and blkcnt would not be calculated correctly. Upgrade it to a u64 and cast its multiplicands as well. Also fix bytes_written while we're at it.

---

  lib/image-sparse.c | 12 ++++++------
  1 file changed, 6 insertions(+), 6 deletions(-)
lib/image-sparse.c +6 -6
index 187ac28cd3..52c8dcc08c 100644
@@ -55,10 +55,10 @@ int write_sparse_image(struct sparse_storage *info,
  	lbaint_t blk;
  	lbaint_t blkcnt;
  	lbaint_t blks;
-	uint32_t bytes_written = 0;
+	uint64_t bytes_written = 0;
  	unsigned int chunk;
  	unsigned int offset;
-	unsigned int chunk_data_sz;
+	uint64_t chunk_data_sz;
  	uint32_t *fill_buf = NULL;
  	uint32_t fill_val;
  	sparse_header_t *sparse_header;
@@ -132,7 +132,7 @@ int write_sparse_image(struct sparse_storage *info,
  				 sizeof(chunk_header_t));
  		}
 
-		chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
+		chunk_data_sz = ((u64)sparse_header->blk_sz) * chunk_header->chunk_sz;
  		blkcnt = chunk_data_sz / info->blksz;
  		switch (chunk_header->chunk_type) {
  		case CHUNK_TYPE_RAW:
@@ -162,7 +162,7 @@ int write_sparse_image(struct sparse_storage *info,
  				return -1;
  			}
  			blk += blks;
-			bytes_written += blkcnt * info->blksz;
+			bytes_written += ((u64)blkcnt) * info->blksz;
  			total_blocks += chunk_header->chunk_sz;
  			data += chunk_data_sz;
  			break;
@@ -222,7 +222,7 @@ int write_sparse_image(struct sparse_storage *info,
  				blk += blks;
  				i += j;
  			}
-			bytes_written += blkcnt * info->blksz;
+			bytes_written += ((u64)blkcnt) * info->blksz;
  			total_blocks += chunk_data_sz / sparse_header->blk_sz;
  			free(fill_buf);
  			break;
@@ -253,7 +253,7 @@ int write_sparse_image(struct sparse_storage *info,
 
  	debug("Wrote %d blocks, expected to write %d blocks\n",
  	      total_blocks, sparse_header->total_blks);
-	printf("........ wrote %u bytes to '%s'\n", bytes_written, part_name);
+	printf("........ wrote %llu bytes to '%s'\n", bytes_written, part_name);
 
  	if (total_blocks != sparse_header->total_blks) {
  		info->mssg("sparse image write failure", response);
#4 lore raw
Quoted 3 lines · Sean Anderson

Hi Lukasz,

Can this make it into 2020.07? Thanks,

ping? Should Tom pick this up instead?

--Sean

Quoted 65 lines

--Sean

Quoted 61 lines · Sean Anderson

If a chunk was larger than 4GiB, then chunk_data_sz would overflow and blkcnt would not be calculated correctly. Upgrade it to a u64 and cast its multiplicands as well. Also fix bytes_written while we're at it.

---

  lib/image-sparse.c | 12 ++++++------

  1 file changed, 6 insertions(+), 6 deletions(-)
lib/image-sparse.c +3 -3
index 187ac28cd3..52c8dcc08c 100644
@@ -55,10 +55,10 @@ int write_sparse_image(struct sparse_storage *info,
       lbaint_t blk;
       lbaint_t blkcnt;
       lbaint_t blks;
-    uint32_t bytes_written = 0;
+    uint64_t bytes_written = 0;
       unsigned int chunk;
       unsigned int offset;
-    unsigned int chunk_data_sz;
+    uint64_t chunk_data_sz;
       uint32_t *fill_buf = NULL;
       uint32_t fill_val;
       sparse_header_t *sparse_header;
@@ -132,7 +132,7 @@ int write_sparse_image(struct sparse_storage *info,
                    sizeof(chunk_header_t));
           }
-        chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
+        chunk_data_sz = ((u64)sparse_header->blk_sz) * chunk_header->chunk_sz;
           blkcnt = chunk_data_sz / info->blksz;
           switch (chunk_header->chunk_type) {

          case CHUNK_TYPE_RAW:

lib/image-sparse.c +3 -3
@@ -162,7 +162,7 @@ int write_sparse_image(struct sparse_storage *info,
                   return -1;
               }
               blk += blks;
-            bytes_written += blkcnt * info->blksz;
+            bytes_written += ((u64)blkcnt) * info->blksz;
               total_blocks += chunk_header->chunk_sz;
               data += chunk_data_sz;
               break;
@@ -222,7 +222,7 @@ int write_sparse_image(struct sparse_storage *info,
                   blk += blks;
                   i += j;
               }
-            bytes_written += blkcnt * info->blksz;
+            bytes_written += ((u64)blkcnt) * info->blksz;
               total_blocks += chunk_data_sz / sparse_header->blk_sz;
               free(fill_buf);
               break;
@@ -253,7 +253,7 @@ int write_sparse_image(struct sparse_storage *info,
       debug("Wrote %d blocks, expected to write %d blocks\n",
             total_blocks, sparse_header->total_blks);
-    printf("........ wrote %u bytes to '%s'\n", bytes_written, part_name);
+    printf("........ wrote %llu bytes to '%s'\n", bytes_written, part_name);
       if (total_blocks != sparse_header->total_blks) {
           info->mssg("sparse image write failure", response);
 
#5 lore raw

Hi Sean,

Quoted 7 lines · Sean Anderson
Quoted 3 lines

Hi Lukasz,

Can this make it into 2020.07? Thanks,

ping? Should Tom pick this up instead?

Yes, Tom please pick it up - as I will not prepare PR sooner than June.

Quoted 62 lines

--Sean

Quoted 60 lines

--Sean

Quoted 56 lines · Sean Anderson

If a chunk was larger than 4GiB, then chunk_data_sz would overflow and blkcnt would not be calculated correctly. Upgrade it to a u64 and cast its multiplicands as well. Also fix bytes_written while we're at it.

---

  lib/image-sparse.c | 12 ++++++------

  1 file changed, 6 insertions(+), 6 deletions(-)
lib/image-sparse.c +3 -3
index 187ac28cd3..52c8dcc08c 100644
@@ -55,10 +55,10 @@ int write_sparse_image(struct sparse_storage
 *info, lbaint_t blk;
       lbaint_t blkcnt;
       lbaint_t blks;
-    uint32_t bytes_written = 0;
+    uint64_t bytes_written = 0;
       unsigned int chunk;
       unsigned int offset;
-    unsigned int chunk_data_sz;
+    uint64_t chunk_data_sz;
       uint32_t *fill_buf = NULL;
       uint32_t fill_val;
       sparse_header_t *sparse_header;
@@ -132,7 +132,7 @@ int write_sparse_image(struct sparse_storage
 *info, sizeof(chunk_header_t));
           }
-        chunk_data_sz = sparse_header->blk_sz *
 chunk_header->chunk_sz;
+        chunk_data_sz = ((u64)sparse_header->blk_sz) *
 chunk_header->chunk_sz; blkcnt = chunk_data_sz / info->blksz;
           switch (chunk_header->chunk_type) {

          case CHUNK_TYPE_RAW:

lib/image-sparse.c +2 -2
@@ -162,7 +162,7 @@ int write_sparse_image(struct sparse_storage
 *info, return -1;
               }
               blk += blks;
-            bytes_written += blkcnt * info->blksz;
+            bytes_written += ((u64)blkcnt) * info->blksz;
               total_blocks += chunk_header->chunk_sz;
               data += chunk_data_sz;
               break;
@@ -222,7 +222,7 @@ int write_sparse_image(struct sparse_storage
 *info, blk += blks;
                   i += j;
               }
-            bytes_written += blkcnt * info->blksz;
+            bytes_written += ((u64)blkcnt) * info->blksz;

              total_blocks += chunk_data_sz /
sparse_header->blk_sz; free(fill_buf);
              break;

lib/image-sparse.c +0 -0
@@ -253,7 +253,7 @@ int write_sparse_image(struct sparse_storage
Quoted 8 lines
Quoted 8 lines

lib/image-sparse.c int write_sparse_image(struct sparse_storage · line 259

Quoted 8 lines
lib/image-sparse.c +1 -1
 *info, debug("Wrote %d blocks, expected to write %d blocks\n",
             total_blocks, sparse_header->total_blks);
-    printf("........ wrote %u bytes to '%s'\n", bytes_written,
 part_name);
+    printf("........ wrote %llu bytes to '%s'\n", bytes_written,
 part_name); if (total_blocks != sparse_header->total_blks) {
           info->mssg("sparse image write failure", response);
 

Best regards,

Lukasz Majewski

--

DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-59 Fax: (+49)-8142-66989-80 Email: [email protected]

#6 lore raw
In reply to Sean Anderson
Quoted 56 lines · Sean Anderson

If a chunk was larger than 4GiB, then chunk_data_sz would overflow and blkcnt would not be calculated correctly. Upgrade it to a u64 and cast its multiplicands as well. Also fix bytes_written while we're at it.

---

 lib/image-sparse.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
lib/image-sparse.c +5 -5
index 187ac28cd3..52c8dcc08c 100644
@@ -55,10 +55,10 @@ int write_sparse_image(struct sparse_storage *info,
 	lbaint_t blk;
 	lbaint_t blkcnt;
 	lbaint_t blks;
-	uint32_t bytes_written = 0;
+	uint64_t bytes_written = 0;
 	unsigned int chunk;
 	unsigned int offset;
-	unsigned int chunk_data_sz;
+	uint64_t chunk_data_sz;
 	uint32_t *fill_buf = NULL;
 	uint32_t fill_val;
 	sparse_header_t *sparse_header;
@@ -132,7 +132,7 @@ int write_sparse_image(struct sparse_storage *info,
 				 sizeof(chunk_header_t));
 		}
 
-		chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
+		chunk_data_sz = ((u64)sparse_header->blk_sz) * chunk_header->chunk_sz;
 		blkcnt = chunk_data_sz / info->blksz;
 		switch (chunk_header->chunk_type) {
 		case CHUNK_TYPE_RAW:
@@ -162,7 +162,7 @@ int write_sparse_image(struct sparse_storage *info,
 				return -1;
 			}
 			blk += blks;
-			bytes_written += blkcnt * info->blksz;
+			bytes_written += ((u64)blkcnt) * info->blksz;
 			total_blocks += chunk_header->chunk_sz;
 			data += chunk_data_sz;
 			break;
@@ -222,7 +222,7 @@ int write_sparse_image(struct sparse_storage *info,
 				blk += blks;
 				i += j;
 			}
-			bytes_written += blkcnt * info->blksz;
+			bytes_written += ((u64)blkcnt) * info->blksz;
 			total_blocks += chunk_data_sz / sparse_header->blk_sz;
 			free(fill_buf);
 			break;

lib/image-sparse.c int write_sparse_image(struct sparse_storage *info, · line 259

Quoted 8 lines
lib/image-sparse.c +1 -1
@@ -253,7 +253,7 @@ int write_sparse_image(struct sparse_storage *info,
 
 	debug("Wrote %d blocks, expected to write %d blocks\n",
 	      total_blocks, sparse_header->total_blks);
-	printf("........ wrote %u bytes to '%s'\n", bytes_written, part_name);
+	printf("........ wrote %llu bytes to '%s'\n", bytes_written, part_name);
 
 	if (total_blocks != sparse_header->total_blks) {
 		info->mssg("sparse image write failure", response);

This results in things like:
pico-dwarf-imx7d: all +506 bss +48 rodata +2 text +456
u-boot: add: 1/0, grow: 1/0 bytes: 452/0 (452)
function old new delta
__aeabi_uldivmod - 392 +392
write_sparse_image 712 772 +60

Which I believe means that some of the division above needs to be converted to use do_div(). Since I can't easily confirm the changes, can you please check in to it? Thanks.

Signature · 1 line
Tom