Skip to content

Commit da21991

Browse files
Javagedesmergify[bot]
authored andcommitted
BaseTools: GenFw: auto-set nxcompat flag
Automatically set the nxcompat flag in the DLL Characteristics field of the Optional Header of the PE32+ image. For this flag to be set automatically, the section alignment must be evenly divisible by 4K (EFI_PAGE_SIZE) and no section must be executable and writable. Adds a command line flag to GenFw, --nonxcompat, to ensure the IMAGE_DLLCHARACTERISTICS_NX_COMPAT bit is not set, even if all requirements are met. Updates the manual for GenFw to include the new flag. Cc: Rebecca Cran <rebecca@bsdio.com> Cc: Liming Gao <gaoliming@byosoft.com.cn> Cc: Bob Feng <bob.c.feng@intel.com> Cc: Yuwei Chen <yuwei.chen@intel.com> Signed-off-by: Joey Vagedes <joeyvagedes@gmail.com> Acked-by: Liming Gao <gaoliming@byosoft.com.cn> Reviewed-by: Rebecca Cran <rebecca@bsdio.com>
1 parent e53c618 commit da21991

File tree

2 files changed

+248
-381
lines changed

2 files changed

+248
-381
lines changed

BaseTools/Source/C/GenFw/GenFw.c

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ UINT32 mImageSize = 0;
8686
UINT32 mOutImageType = FW_DUMMY_IMAGE;
8787
BOOLEAN mIsConvertXip = FALSE;
8888
BOOLEAN mExportFlag = FALSE;
89+
BOOLEAN mNoNxCompat = FALSE;
8990

9091
STATIC
9192
EFI_STATUS
@@ -281,6 +282,9 @@ Routine Description:
281282
write export table into PE-COFF.\n\
282283
This option can be used together with -e.\n\
283284
It doesn't work for other options.\n");
285+
fprintf (stdout, " --nonxcompat Do not set the IMAGE_DLLCHARACTERISTICS_NX_COMPAT bit \n\
286+
of the optional header in the PE header even if the \n\
287+
requirements are met.\n");
284288
fprintf (stdout, " -v, --verbose Turn on verbose output with informational messages.\n");
285289
fprintf (stdout, " -q, --quiet Disable all messages except key message and fatal error\n");
286290
fprintf (stdout, " -d, --debug level Enable debug messages, at input debug level.\n");
@@ -441,6 +445,59 @@ Routine Description:
441445
return STATUS_SUCCESS;
442446
}
443447

448+
/**
449+
450+
Checks if the Pe image is nxcompat compliant.
451+
452+
Must meet the following conditions:
453+
1. The PE is 64bit
454+
2. The section alignment is evenly divisible by 4k
455+
3. No section is writable and executable.
456+
457+
@param PeHdr - The PE header
458+
459+
@retval TRUE - The PE is nx compat compliant
460+
@retval FALSE - The PE is not nx compat compliant
461+
462+
**/
463+
STATIC
464+
BOOLEAN
465+
IsNxCompatCompliant (
466+
EFI_IMAGE_OPTIONAL_HEADER_UNION *PeHdr
467+
)
468+
{
469+
EFI_IMAGE_SECTION_HEADER *SectionHeader;
470+
UINT32 Index;
471+
UINT32 Mask;
472+
473+
// Must have an optional header to perform verification
474+
if (PeHdr->Pe32.FileHeader.SizeOfOptionalHeader == 0) {
475+
return FALSE;
476+
}
477+
478+
// Verify PE is 64 bit
479+
if (!(PeHdr->Pe32.OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC)) {
480+
return FALSE;
481+
}
482+
483+
// Verify Section Alignment is divisible by 4K
484+
if (!((PeHdr->Pe32Plus.OptionalHeader.SectionAlignment % EFI_PAGE_SIZE) == 0)) {
485+
return FALSE;
486+
}
487+
488+
// Verify sections are not Write & Execute
489+
Mask = EFI_IMAGE_SCN_MEM_EXECUTE | EFI_IMAGE_SCN_MEM_WRITE;
490+
SectionHeader = (EFI_IMAGE_SECTION_HEADER *) ((UINT8 *) &(PeHdr->Pe32Plus.OptionalHeader) + PeHdr->Pe32Plus.FileHeader.SizeOfOptionalHeader);
491+
for (Index = 0; Index < PeHdr->Pe32Plus.FileHeader.NumberOfSections; Index ++, SectionHeader ++) {
492+
if ((SectionHeader->Characteristics & Mask) == Mask) {
493+
return FALSE;
494+
}
495+
}
496+
497+
// Passed all requirements, return TRUE
498+
return TRUE;
499+
}
500+
444501
VOID
445502
SetHiiResourceHeader (
446503
UINT8 *HiiBinData,
@@ -1452,6 +1509,13 @@ Routine Description:
14521509
continue;
14531510
}
14541511

1512+
if (stricmp (argv[0], "--nonxcompat") == 0) {
1513+
mNoNxCompat = TRUE;
1514+
argc --;
1515+
argv ++;
1516+
continue;
1517+
}
1518+
14551519
if (argv[0][0] == '-') {
14561520
Error (NULL, 0, 1000, "Unknown option", argv[0]);
14571521
goto Finish;
@@ -2458,6 +2522,11 @@ Routine Description:
24582522
TEImageHeader.BaseOfCode = Optional64->BaseOfCode;
24592523
TEImageHeader.ImageBase = (UINT64) (Optional64->ImageBase);
24602524

2525+
// Set NxCompat flag
2526+
if (IsNxCompatCompliant (PeHdr) && !mNoNxCompat) {
2527+
Optional64->DllCharacteristics |= IMAGE_DLLCHARACTERISTICS_NX_COMPAT;
2528+
}
2529+
24612530
if (Optional64->NumberOfRvaAndSizes > EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC) {
24622531
TEImageHeader.DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress = Optional64->DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC].VirtualAddress;
24632532
TEImageHeader.DataDirectory[EFI_TE_IMAGE_DIRECTORY_ENTRY_BASERELOC].Size = Optional64->DataDirectory[EFI_IMAGE_DIRECTORY_ENTRY_BASERELOC].Size;

0 commit comments

Comments
 (0)