From 3dc21d387ba3506275a775ba79663ecf88e794a9 Mon Sep 17 00:00:00 2001 From: Alexandre Julliard Date: Wed, 13 Dec 2006 13:06:12 +0100 Subject: [PATCH] ntdll: Map the PE header up to the specified size, and clear the rest of the page. --- dlls/kernel32/tests/loader.c | 6 ------ dlls/ntdll/virtual.c | 9 ++++++--- server/mapping.c | 4 ++-- 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/dlls/kernel32/tests/loader.c b/dlls/kernel32/tests/loader.c index 48771ba8406..7ae70f32dd1 100644 --- a/dlls/kernel32/tests/loader.c +++ b/dlls/kernel32/tests/loader.c @@ -360,12 +360,6 @@ START_TEST(loader) start = (const char *)hlib + nt_header.OptionalHeader.SizeOfHeaders; size = ALIGN_SIZE((ULONG_PTR)start, si.dwPageSize) - (ULONG_PTR)start; - /* remove next 'if' and todo_wine once Wine is fixed */ - if (nt_header.OptionalHeader.SizeOfHeaders < nt_header.OptionalHeader.FileAlignment) - { -todo_wine ok(!memcmp(start, filler, size), "%d: header alignment is not cleared\n", i); - } - else ok(!memcmp(start, filler, size), "%d: header alignment is not cleared\n", i); } diff --git a/dlls/ntdll/virtual.c b/dlls/ntdll/virtual.c index 2b1ff241ffe..ff16adcd3a1 100644 --- a/dlls/ntdll/virtual.c +++ b/dlls/ntdll/virtual.c @@ -974,11 +974,12 @@ static NTSTATUS map_image( HANDLE hmapping, int fd, char *base, SIZE_T total_siz status = STATUS_INVALID_IMAGE_FORMAT; /* generic error */ if (!st.st_size) goto error; header_size = min( header_size, st.st_size ); - if (map_file_into_view( view, fd, 0, header_size, 0, VPROT_COMMITTED | VPROT_READ, + if (map_file_into_view( view, fd, 0, header_size, 0, VPROT_COMMITTED | VPROT_READ | VPROT_WRITECOPY, removable ) != STATUS_SUCCESS) goto error; dos = (IMAGE_DOS_HEADER *)ptr; nt = (IMAGE_NT_HEADERS *)(ptr + dos->e_lfanew); header_end = ptr + ROUND_SIZE( 0, header_size ); + memset( ptr + header_size, 0, header_end - (ptr + header_size) ); if ((char *)(nt + 1) > header_end) goto error; sec = (IMAGE_SECTION_HEADER*)((char*)&nt->OptionalHeader+nt->FileHeader.SizeOfOptionalHeader); if ((char *)(sec + nt->FileHeader.NumberOfSections) > header_end) goto error; @@ -1067,8 +1068,8 @@ static NTSTATUS map_image( HANDLE hmapping, int fd, char *base, SIZE_T total_siz end = sec->VirtualAddress + ROUND_SIZE( sec->VirtualAddress, map_size ); if (sec->VirtualAddress > total_size || end > total_size || end < sec->VirtualAddress) { - ERR_(module)( "Section %.8s too large (%x+%lx/%lx)\n", - sec->Name, sec->VirtualAddress, map_size, total_size ); + WARN_(module)( "Section %.8s too large (%x+%lx/%lx)\n", + sec->Name, sec->VirtualAddress, map_size, total_size ); goto error; } @@ -1166,6 +1167,8 @@ static NTSTATUS map_image( HANDLE hmapping, int fd, char *base, SIZE_T total_siz /* set the image protections */ + VIRTUAL_SetProt( view, ptr, ROUND_SIZE( 0, header_size ), VPROT_COMMITTED | VPROT_READ ); + sec = (IMAGE_SECTION_HEADER*)((char *)&nt->OptionalHeader+nt->FileHeader.SizeOfOptionalHeader); for (i = 0; i < nt->FileHeader.NumberOfSections; i++, sec++) { diff --git a/server/mapping.c b/server/mapping.c index d526e759841..052ea4153b8 100644 --- a/server/mapping.c +++ b/server/mapping.c @@ -243,11 +243,11 @@ static int get_image_params( struct mapping *mapping ) mapping->size = ROUND_SIZE( nt.OptionalHeader.SizeOfImage ); mapping->base = (void *)nt.OptionalHeader.ImageBase; - mapping->header_size = pos + size; + mapping->header_size = max( pos + size, nt.OptionalHeader.SizeOfHeaders ); mapping->protect = VPROT_IMAGE; /* sanity check */ - if (mapping->header_size > mapping->size) goto error; + if (pos + size > mapping->size) goto error; free( sec ); release_object( fd );