@@ -1338,22 +1338,32 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
13381338
13391339 /* Get the file size */
13401340 if (stat (CMD .cert_chain_file , & file_stat ) == 0 ) {
1341- const uint32_t required_space = header_required_size (is_diff ,
1342- (uint32_t )file_stat .st_size , secondary_key_sz );
1343-
1344- /* If the current header size is too small, increase it */
1345- if (CMD .header_sz < required_space ) {
1346- /* Round up to nearest power of 2 that can hold the chain */
1347- const uint32_t min_header_size = 256 ;
1348- uint32_t new_size = min_header_size ;
1349- while (new_size < required_space ) {
1350- new_size *= 2 ;
1351- }
1341+ off_t chain_file_sz = file_stat .st_size ;
1342+ uint32_t required_space ;
1343+
1344+ if ((chain_file_sz < 0 ) ||
1345+ ((uintmax_t )chain_file_sz > (uintmax_t )UINT32_MAX )) {
1346+ printf ("Warning: certificate chain file size is invalid (%jd)\n" ,
1347+ (intmax_t )chain_file_sz );
1348+ }
1349+ else {
1350+ required_space = header_required_size (is_diff ,
1351+ (uint32_t )chain_file_sz , secondary_key_sz );
1352+
1353+ /* If the current header size is too small, increase it */
1354+ if (CMD .header_sz < required_space ) {
1355+ /* Round up to nearest power of 2 that can hold the chain */
1356+ const uint32_t min_header_size = 256 ;
1357+ uint32_t new_size = min_header_size ;
1358+ while (new_size < required_space ) {
1359+ new_size *= 2 ;
1360+ }
13521361
1353- printf ("Increasing header size from %u to %u bytes to fit "
1354- "certificate chain\n" ,
1355- CMD .header_sz , new_size );
1356- CMD .header_sz = new_size ;
1362+ printf ("Increasing header size from %u to %u bytes to fit "
1363+ "certificate chain\n" ,
1364+ CMD .header_sz , new_size );
1365+ CMD .header_sz = new_size ;
1366+ }
13571367 }
13581368 }
13591369 else {
@@ -1496,7 +1506,15 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
14961506 goto failure ;
14971507 }
14981508
1499- cert_chain_sz = file_stat .st_size ;
1509+ if ((file_stat .st_size < 0 ) ||
1510+ ((uintmax_t )file_stat .st_size > (uintmax_t )UINT32_MAX )) {
1511+ printf ("Error: Invalid certificate chain file size (%jd)\n" ,
1512+ (intmax_t )file_stat .st_size );
1513+ fclose (f );
1514+ f = NULL ;
1515+ goto failure ;
1516+ }
1517+ cert_chain_sz = (uint32_t )file_stat .st_size ;
15001518
15011519 if (cert_chain_sz > (uint32_t )UINT16_MAX ) {
15021520 printf ("Error: Certificate chain too large for TLV encoding "
0 commit comments