@@ -255,7 +255,9 @@ static int pe_verify_digests(FILE_FORMAT_CTX *ctx, PKCS7 *p7)
255255 SpcIndirectDataContent_free (idc );
256256 return 0 ; /* FAILED */
257257 }
258- if (spc_extract_digest_safe (idc , mdbuf , & mdtype ) < 0 ) {
258+ if (spc_indirect_data_content_get_digest (idc , mdbuf , & mdtype ) < 0 ) {
259+ fprintf (stderr , "Failed to extract message digest from signature\n\n" );
260+ OPENSSL_free (ph );
259261 SpcIndirectDataContent_free (idc );
260262 return 0 ; /* FAILED */
261263 }
@@ -920,6 +922,8 @@ static u_char *pe_page_hash_calc(int *rphlen, FILE_FORMAT_CTX *ctx, int phtype)
920922 char * sections ;
921923 const EVP_MD * md = EVP_get_digestbynid (phtype );
922924 BIO * bhash ;
925+ uint32_t filebound ;
926+ size_t pphlen_sz , sections_factor ;
923927
924928 /* NumberOfSections indicates the size of the section table,
925929 * which immediately follows the headers, can be up to 65535 under Vista and later */
@@ -961,8 +965,29 @@ static u_char *pe_page_hash_calc(int *rphlen, FILE_FORMAT_CTX *ctx, int phtype)
961965 fprintf (stderr , "Corrupted optional header size: 0x%08X\n" , opthdr_size );
962966 return NULL ; /* FAILED */
963967 }
968+ /* Validate that pagesize >= hdrsize to prevent integer underflow */
969+ if (pagesize < hdrsize ) {
970+ fprintf (stderr , "Page size (0x%08X) is smaller than header size (0x%08X)\n" ,
971+ pagesize , hdrsize );
972+ return NULL ; /* FAILED */
973+ }
964974 pphlen = 4 + EVP_MD_size (md );
965- phlen = pphlen * (3 + (int )nsections + (int )(ctx -> pe_ctx -> fileend / pagesize ));
975+
976+ /* Use size_t arithmetic and check for overflow */
977+ pphlen_sz = (size_t )pphlen ;
978+ sections_factor = 3 + (size_t )nsections + ((size_t )ctx -> pe_ctx -> fileend / pagesize );
979+
980+ /* Check for multiplication overflow */
981+ if (sections_factor > SIZE_MAX / pphlen_sz ) {
982+ fprintf (stderr , "Page hash allocation size would overflow\n" );
983+ return NULL ; /* FAILED */
984+ }
985+ phlen = (int )(pphlen_sz * sections_factor );
986+ /* Sanity limit - page hash shouldn't exceed reasonable size (16 MB) */
987+ if (phlen < 0 || (size_t )phlen > SIZE_16M ) {
988+ fprintf (stderr , "Page hash size exceeds limit: %d\n" , phlen );
989+ return NULL ; /* FAILED */
990+ }
966991
967992 bhash = BIO_new (BIO_f_md ());
968993#if defined(__GNUC__ )
@@ -1008,14 +1033,24 @@ static u_char *pe_page_hash_calc(int *rphlen, FILE_FORMAT_CTX *ctx, int phtype)
10081033 BIO_gets (bhash , (char * )res + 4 , EVP_MD_size (md ));
10091034 BIO_free_all (bhash );
10101035 sections = ctx -> options -> indata + ctx -> pe_ctx -> header_size + 24 + opthdr_size ;
1036+ /* Determine the file boundary for section data validation */
1037+ filebound = ctx -> pe_ctx -> sigpos ? ctx -> pe_ctx -> sigpos : ctx -> pe_ctx -> fileend ;
10111038 for (i = 0 ; i < nsections ; i ++ ) {
1012- /* Resource Table address and size */
1039+ /* SizeOfRawData and PointerToRawData from section header */
10131040 rs = GET_UINT32_LE (sections + 16 );
10141041 ro = GET_UINT32_LE (sections + 20 );
10151042 if (rs == 0 || rs >= UINT32_MAX ) {
10161043 sections += 40 ;
10171044 continue ;
10181045 }
1046+ /* Validate section bounds against file size to prevent OOB read */
1047+ if (ro >= filebound || rs > filebound - ro ) {
1048+ fprintf (stderr , "Section %d has invalid bounds: offset=0x%08X, size=0x%08X, fileend=0x%08X\n" ,
1049+ i , ro , rs , filebound );
1050+ OPENSSL_free (zeroes );
1051+ OPENSSL_free (res );
1052+ return NULL ; /* FAILED */
1053+ }
10191054 for (l = 0 ; l < rs ; l += pagesize , pi ++ ) {
10201055 PUT_UINT32_LE (ro + l , res + pi * pphlen );
10211056 bhash = BIO_new (BIO_f_md ());
@@ -1088,6 +1123,10 @@ static int pe_verify_page_hash(FILE_FORMAT_CTX *ctx, u_char *ph, int phlen, int
10881123 if (!ph )
10891124 return 1 ; /* OK */
10901125 cph = pe_page_hash_calc (& cphlen , ctx , phtype );
1126+ if (!cph ) {
1127+ fprintf (stderr , "Page hash verification failed: could not calculate page hash\n" );
1128+ return 0 ; /* FAILED */
1129+ }
10911130 mdok = (phlen == cphlen ) && !memcmp (ph , cph , (size_t )phlen );
10921131 printf ("Page hash algorithm : %s\n" , OBJ_nid2sn (phtype ));
10931132 if (ctx -> options -> verbose ) {
0 commit comments