Skip to content

Commit 501a14f

Browse files
committed
Update portable executable and import table validation (case 1336618)
Update the SectionAlignment and FileAlignment validate to match the ms docs... https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#optional-header-windows-specific-fields-image-only "SectionAlignment The alignment (in bytes) of sections when they are loaded into memory It must be greater than or equal to FileAlignment. The default is the page size for the architecture." "FileAlignment The alignment factor (in bytes) that is used to align the raw data of sections in the image file. The value should be a power of 2 between 512 and 64 K, inclusive. The default is 512. If the SectionAlignment is less than the architecture's page size, then FileAlignment must match SectionAlignment." For the import table use a case insensitive compare for the mscoree.dll file name. Fix case 1336618: Scripting: Fix "Loading assembly failed ... File does not contain a valid CIL image" errors
1 parent c445787 commit 501a14f

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

mono/metadata/metadata-verify.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ static void
448448
verify_pe_optional_header (VerifyContext *ctx)
449449
{
450450
guint32 offset = pe_header_offset (ctx);
451-
guint32 header_size, file_alignment;
451+
guint32 header_size, section_alignment, file_alignment;
452452
const char *pe_header = ctx->data + offset;
453453
const char *pe_optional_header = pe_header + 20;
454454

@@ -484,13 +484,19 @@ verify_pe_optional_header (VerifyContext *ctx)
484484
/* LAMESPEC MS plays around this value and ignore it during validation
485485
if (read32 (pe_optional_header + 28) != 0x400000)
486486
ADD_ERROR (ctx, g_strdup_printf ("Invalid Image base %x", read32 (pe_optional_header + 28)));*/
487-
if (read32 (pe_optional_header + 32) != 0x2000)
488-
ADD_ERROR (ctx, g_strdup_printf ("Invalid Section Aligmnent %x", read32 (pe_optional_header + 32)));
487+
section_alignment = read32(pe_optional_header + 32);
489488
file_alignment = read32 (pe_optional_header + 36);
490-
if (file_alignment != 0x200 && file_alignment != 0x1000)
489+
490+
// a power of 2 between 512 and 64 K, inclusive
491+
if (file_alignment != 0x200 && file_alignment != 0x400 && file_alignment != 0x800 && file_alignment != 0x1000 &&
492+
file_alignment != 0x2000 && file_alignment != 0x4000 && file_alignment != 0x8000 && file_alignment != 0x10000)
491493
ADD_ERROR (ctx, g_strdup_printf ("Invalid file Aligmnent %x", file_alignment));
492494
/* All the junk in the middle is irrelevant, specially for mono. */
493495

496+
// must be greater than or equal to FileAlignment
497+
if (section_alignment < file_alignment)
498+
ADD_ERROR(ctx, g_strdup_printf("Invalid Section Aligmnent %x", read32(pe_optional_header + 32)));
499+
494500
if (header_size != 224 + ctx->pe64)
495501
ADD_ERROR (ctx, g_strdup_printf ("Invalid optional header size %d", header_size));
496502

@@ -622,6 +628,7 @@ verify_import_table (VerifyContext *ctx)
622628
guint32 offset = it.translated_offset;
623629
const char *ptr = ctx->data + offset;
624630
guint32 name_rva, ilt_rva, iat_rva;
631+
char mscoreeBuff[SIZE_OF_MSCOREE + 1];
625632

626633
// Having no import table is structurally valid
627634
if (it.rva == 0 && it.size == 0)
@@ -654,8 +661,12 @@ verify_import_table (VerifyContext *ctx)
654661
g_assert (name_rva != INVALID_OFFSET);
655662
ptr = ctx->data + name_rva;
656663

657-
if (memcmp ("mscoree.dll", ptr, SIZE_OF_MSCOREE))
658-
ADD_ERROR (ctx, g_strdup_printf ("Invalid Import Table Name: '%s'", ptr));
664+
if (memcmp("mscoree.dll", ptr, SIZE_OF_MSCOREE)) {
665+
memcpy(mscoreeBuff, ptr, SIZE_OF_MSCOREE);
666+
mscoreeBuff[SIZE_OF_MSCOREE] = 0;
667+
if (g_strcasecmp ("mscoree.dll", mscoreeBuff))
668+
ADD_ERROR(ctx, g_strdup_printf("Invalid Import Table Name: '%s'", ptr));
669+
}
659670
}
660671

661672
if (ilt_rva) {

0 commit comments

Comments
 (0)