diff --git a/MdePkg/Library/BaseUeImageLib/BaseUeImageLib.inf b/MdePkg/Library/BaseUeImageLib/BaseUeImageLib.inf index ca9a630734..df15f1a2bd 100644 --- a/MdePkg/Library/BaseUeImageLib/BaseUeImageLib.inf +++ b/MdePkg/Library/BaseUeImageLib/BaseUeImageLib.inf @@ -30,3 +30,4 @@ [FixedPcd] gEfiMdePkgTokenSpaceGuid.PcdImageLoaderRelocTypePolicy gEfiMdePkgTokenSpaceGuid.PcdDebugRaisePropertyMask + gEfiMdePkgTokenSpaceGuid.PcdImageLoaderRtRelocAllowTargetMismatch diff --git a/MdePkg/Library/BaseUeImageLib/UeImageLib.c b/MdePkg/Library/BaseUeImageLib/UeImageLib.c index 087b074135..55c0d7bbef 100644 --- a/MdePkg/Library/BaseUeImageLib/UeImageLib.c +++ b/MdePkg/Library/BaseUeImageLib/UeImageLib.c @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -23,6 +24,8 @@ struct UE_LOADER_RUNTIME_CONTEXT_ { UINT8 Machine; UINT8 Reserved[7]; + UINT32 FixupSize; + UINT64 *FixupData; UINT32 RelocTableSize; UINT8 RelocTable[]; }; @@ -457,7 +460,9 @@ InternalApplyRelocation ( IN UINT8 Machine, IN UINT16 RelocType, IN UINT32 *RelocTarget, - IN UINT64 Adjust + IN UINT64 Adjust, + OUT UINT64 *FixupData, + IN BOOLEAN IsRuntime ) { BOOLEAN Overflow; @@ -497,8 +502,23 @@ InternalApplyRelocation ( // Relocate the target instruction. // FixupValue.Value32 = ReadUnaligned32 (Fixup); + // + // If the Image relocation target value mismatches, skip or abort. + // + // if (IsRuntime && (FixupValue.Value32 != (UINT32)*FixupData)) { + // if (PcdGetBool (PcdImageLoaderRtRelocAllowTargetMismatch)) { + // return RETURN_SUCCESS; + // } + // + // return RETURN_VOLUME_CORRUPTED; + // } + FixupValue.Value32 += (UINT32) Adjust; WriteUnaligned32 (Fixup, FixupValue.Value32); + + // if (!IsRuntime) { + // *FixupData = FixupValue.Value32; + // } } else { ASSERT (RelocType == UeReloc64); @@ -515,8 +535,23 @@ InternalApplyRelocation ( // Relocate target the instruction. // FixupValue.Value64 = ReadUnaligned64 (Fixup); + // + // If the Image relocation target value mismatches, skip or abort. + // + if (IsRuntime && (FixupValue.Value64 != *FixupData)) { + if (PcdGetBool (PcdImageLoaderRtRelocAllowTargetMismatch)) { + return RETURN_SUCCESS; + } + + return RETURN_VOLUME_CORRUPTED; + } + FixupValue.Value64 += Adjust; WriteUnaligned64 (Fixup, FixupValue.Value64); + + if (!IsRuntime) { + *FixupData = FixupValue.Value64; + } } } else { #if 0 @@ -668,7 +703,9 @@ InternaRelocateImage ( IN CONST VOID *RelocTable, IN UINT32 RelocTableSize, IN BOOLEAN Chaining, - IN UINT64 BaseAddress + IN UINT64 BaseAddress, + OUT UINT64 *FixupData, + IN BOOLEAN IsRuntime ) { RETURN_STATUS Status; @@ -769,8 +806,12 @@ InternaRelocateImage ( Machine, RelocType, &RelocTarget, - Adjust + Adjust, + FixupData, + IsRuntime ); + + ++FixupData; } if (RETURN_ERROR (Status)) { @@ -851,6 +892,12 @@ UeRelocateImage ( Context->FileBuffer + Context->LoadTablesFileOffset, Context->RelocTableSize ); + + RuntimeContext->FixupSize = Context->RelocTableSize / sizeof (UINT16) * sizeof (UINT64); + RuntimeContext->FixupData = AllocateRuntimeZeroPool (RuntimeContext->FixupSize); + if (RuntimeContext->FixupData == NULL) { + ASSERT (FALSE); + } } return InternaRelocateImage ( @@ -861,7 +908,9 @@ UeRelocateImage ( RelocTable, Context->RelocTableSize, Chaining, - BaseAddress + BaseAddress, + RuntimeContext->FixupData, + FALSE ); } @@ -884,7 +933,9 @@ UeRelocateImageForRuntime ( RuntimeContext->RelocTable, RuntimeContext->RelocTableSize, FALSE, - BaseAddress + BaseAddress, + RuntimeContext->FixupData, + TRUE ); } diff --git a/OvmfPkg/OvmfPkgIa32X64.fdf b/OvmfPkg/OvmfPkgIa32X64.fdf index d0be75d479..19edf3d8b9 100644 --- a/OvmfPkg/OvmfPkgIa32X64.fdf +++ b/OvmfPkg/OvmfPkgIa32X64.fdf @@ -459,7 +459,7 @@ FILE FV_IMAGE = 9E21FD93-9C72-4c15-8C4B-E77F1DB2D792 { [Rule.Common.DXE_RUNTIME_DRIVER] FILE DRIVER = $(NAMED_GUID) { DXE_DEPEX DXE_DEPEX Optional $(INF_OUTPUT)/$(MODULE_NAME).depex - PE32 PE32 $(INF_OUTPUT)/$(MODULE_NAME).efi + UE UE $(INF_OUTPUT)/$(MODULE_NAME).efi UI STRING="$(MODULE_NAME)" Optional VERSION STRING="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER) } diff --git a/OvmfPkg/OvmfPkgX64.fdf b/OvmfPkg/OvmfPkgX64.fdf index 19a374a25e..c17cf9eb24 100644 --- a/OvmfPkg/OvmfPkgX64.fdf +++ b/OvmfPkg/OvmfPkgX64.fdf @@ -491,7 +491,7 @@ FILE FV_IMAGE = 9E21FD93-9C72-4c15-8C4B-E77F1DB2D792 { [Rule.Common.DXE_RUNTIME_DRIVER] FILE DRIVER = $(NAMED_GUID) { DXE_DEPEX DXE_DEPEX Optional $(INF_OUTPUT)/$(MODULE_NAME).depex - PE32 PE32 $(INF_OUTPUT)/$(MODULE_NAME).efi + UE UE $(INF_OUTPUT)/$(MODULE_NAME).efi UI STRING="$(MODULE_NAME)" Optional VERSION STRING="$(INF_VERSION)" Optional BUILD_NUM=$(BUILD_NUMBER) }