Skip to content

Commit 5691f0f

Browse files
committed
Refactor
1 parent 9e895a7 commit 5691f0f

1 file changed

Lines changed: 39 additions & 4 deletions

File tree

crypto/fipsmodule/bcm.c

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,11 @@ int BORINGSSL_integrity_test(void) {
492492
// On ARM64 Windows, ASLR is mandatory. Undo PE base relocations in
493493
// temporary copies of the FIPS sections so the hash is load-address
494494
// independent. See the comment above fips_undo_base_relocations().
495+
//
496+
// IMPORTANT: The PE loader overwrites OptionalHeader.ImageBase in memory
497+
// with the actual load address, so reading it from the in-memory PE header
498+
// always gives delta==0. We must read the *original* ImageBase from the
499+
// DLL file on disk to obtain the true preferred base.
495500
uint8_t *text_copy = NULL;
496501
uint8_t *rodata_copy = NULL;
497502
const uint8_t *text_to_hash = start;
@@ -503,15 +508,45 @@ int BORINGSSL_integrity_test(void) {
503508
GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
504509
(LPCWSTR)start, &fips_module)) {
505510
const uintptr_t actual_base = (uintptr_t)fips_module;
511+
512+
// Read the preferred ImageBase from the DLL file on disk.
513+
uintptr_t preferred_base = actual_base; // fallback: assume no relocation
514+
wchar_t dll_path[MAX_PATH];
515+
if (GetModuleFileNameW(fips_module, dll_path, MAX_PATH) != 0) {
516+
HANDLE hFile = CreateFileW(dll_path, GENERIC_READ, FILE_SHARE_READ,
517+
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
518+
NULL);
519+
if (hFile != INVALID_HANDLE_VALUE) {
520+
IMAGE_DOS_HEADER file_dos_header;
521+
DWORD bytes_read = 0;
522+
if (ReadFile(hFile, &file_dos_header, sizeof(file_dos_header),
523+
&bytes_read, NULL) &&
524+
bytes_read == sizeof(file_dos_header) &&
525+
file_dos_header.e_magic == IMAGE_DOS_SIGNATURE) {
526+
LARGE_INTEGER nt_offset;
527+
nt_offset.QuadPart = file_dos_header.e_lfanew;
528+
if (SetFilePointerEx(hFile, nt_offset, NULL, FILE_BEGIN)) {
529+
IMAGE_NT_HEADERS file_nt_headers;
530+
if (ReadFile(hFile, &file_nt_headers, sizeof(file_nt_headers),
531+
&bytes_read, NULL) &&
532+
bytes_read == sizeof(file_nt_headers) &&
533+
file_nt_headers.Signature == IMAGE_NT_SIGNATURE) {
534+
preferred_base =
535+
(uintptr_t)file_nt_headers.OptionalHeader.ImageBase;
536+
}
537+
}
538+
}
539+
CloseHandle(hFile);
540+
}
541+
}
542+
543+
const intptr_t delta = (intptr_t)(actual_base - preferred_base);
506544
const IMAGE_DOS_HEADER *dos_header =
507545
(const IMAGE_DOS_HEADER *)fips_module;
508546
const IMAGE_NT_HEADERS *nt_headers = (const IMAGE_NT_HEADERS *)(
509547
(const uint8_t *)fips_module + dos_header->e_lfanew);
510-
const uintptr_t preferred_base =
511-
(uintptr_t)nt_headers->OptionalHeader.ImageBase;
512-
const intptr_t delta = (intptr_t)(actual_base - preferred_base);
513548

514-
fprintf(stderr, "FIPS reloc-undo: actual_base=%p preferred_base=%p delta=0x%llx\n",
549+
fprintf(stderr, "FIPS reloc-undo: actual_base=%p preferred_base(file)=%p delta=0x%llx\n",
515550
(const void *)actual_base, (const void *)preferred_base,
516551
(unsigned long long)delta);
517552

0 commit comments

Comments
 (0)