ReactOS 0.4.15-dev-7958-gcd0bb1a
ZwMapViewOfSection.c File Reference
#include <kmt_test.h>
Include dependency graph for ZwMapViewOfSection.c:

Go to the source code of this file.

Macros

#define IGNORE   -99
 
#define NEW_CONTENT   "NewContent"
 
#define NEW_CONTENT_LEN   sizeof(NEW_CONTENT)
 
#define TestMapView(SectionHandle, ProcessHandle, BaseAddress2, ZeroBits, CommitSize, SectionOffset, ViewSize2, InheritDisposition, AllocationType, Win32Protect, MapStatus, UnmapStatus)
 
#define MmTestMapView(Object, ProcessHandle, BaseAddress2, ZeroBits, CommitSize, SectionOffset, ViewSize2, InheritDisposition, AllocationType, Win32Protect, MapStatus, UnmapStatus)
 
#define CheckObject(Handle, Pointers, Handles)
 

Functions

static VOID KmtInitTestFiles (PHANDLE ReadOnlyFile, PHANDLE WriteOnlyFile, PHANDLE ExecutableFile)
 
static VOID SimpleErrorChecks (HANDLE FileHandleReadOnly, HANDLE FileHandleWriteOnly, HANDLE ExecutableImg)
 
static VOID AdvancedErrorChecks (HANDLE FileHandleReadOnly, HANDLE FileHandleWriteOnly)
 
static SIZE_T CompareFileContents (HANDLE FileHandle, ULONG BufferLength, PVOID Buffer)
 
static VOID NTAPI SystemProcessWorker (PVOID StartContext)
 
static VOID BehaviorChecks (HANDLE FileHandleReadOnly, HANDLE FileHandleWriteOnly)
 
static VOID PageFileBehaviorChecks ()
 
 START_TEST (ZwMapViewOfSection)
 

Variables

static UNICODE_STRING FileReadOnlyPath = RTL_CONSTANT_STRING(L"\\SystemRoot\\system32\\ntdll.dll")
 
static UNICODE_STRING NtosImgPath = RTL_CONSTANT_STRING(L"\\SystemRoot\\system32\\ntoskrnl.exe")
 
static UNICODE_STRING WritableFilePath = RTL_CONSTANT_STRING(L"\\SystemRoot\\kmtest-MmSection.txt")
 
static UNICODE_STRING SharedSectionName = RTL_CONSTANT_STRING(L"\\BaseNamedObjects\\kmtest-SharedSection")
 
const char TestString []
 
const ULONG TestStringSize
 
static OBJECT_ATTRIBUTES NtdllObject
 
static OBJECT_ATTRIBUTES KmtestFileObject
 
static OBJECT_ATTRIBUTES NtoskrnlFileObject
 

Macro Definition Documentation

◆ CheckObject

#define CheckObject (   Handle,
  Pointers,
  Handles 
)
Value:
do \
{ \
Status = ZwQueryObject(Handle, ObjectBasicInformation, \
&ObjectInfo, sizeof ObjectInfo, NULL); \
ok_eq_hex(Status, STATUS_SUCCESS); \
ok_eq_ulong(ObjectInfo.PointerCount, Pointers); \
ok_eq_ulong(ObjectInfo.HandleCount, Handles); \
} while (0) \
@ ObjectBasicInformation
Definition: DriverTester.h:54
#define NULL
Definition: types.h:112
ULONG Handle
Definition: gdb_input.c:15
Status
Definition: gdiplustypes.h:25
#define STATUS_SUCCESS
Definition: shellext.h:65

Definition at line 50 of file ZwMapViewOfSection.c.

◆ IGNORE

#define IGNORE   -99

Definition at line 10 of file ZwMapViewOfSection.c.

◆ MmTestMapView

#define MmTestMapView (   Object,
  ProcessHandle,
  BaseAddress2,
  ZeroBits,
  CommitSize,
  SectionOffset,
  ViewSize2,
  InheritDisposition,
  AllocationType,
  Win32Protect,
  MapStatus,
  UnmapStatus 
)
Value:
do \
{ \
ok_eq_hex(Status, MapStatus); \
{ \
if (UnmapStatus != IGNORE) ok_eq_hex(Status, UnmapStatus); \
*BaseAddress2 = NULL; \
*ViewSize2 = 0; \
} \
} while (0) \
NTSTATUS NTAPI MmUnmapViewOfSection(IN PEPROCESS Process, IN PVOID BaseAddress)
Definition: section.c:3117
#define IGNORE
#define ok_eq_hex(value, expected)
Definition: apitest.h:77
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T _In_ SECTION_INHERIT InheritDisposition
Definition: mmfuncs.h:409
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T _In_ SECTION_INHERIT _In_ ULONG _In_ ULONG Win32Protect
Definition: mmfuncs.h:412
_In_ HANDLE ProcessHandle
Definition: mmfuncs.h:403
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR ZeroBits
Definition: mmfuncs.h:405
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID * BaseAddress
Definition: mmfuncs.h:404
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T _In_ SECTION_INHERIT _In_ ULONG AllocationType
Definition: mmfuncs.h:410
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER SectionOffset
Definition: mmfuncs.h:407
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T CommitSize
Definition: mmfuncs.h:406
NTSTATUS NTAPI MmMapViewOfSection(IN PVOID SectionObject, IN PEPROCESS Process, IN OUT PVOID *BaseAddress, IN ULONG_PTR ZeroBits, IN SIZE_T CommitSize, IN OUT PLARGE_INTEGER SectionOffset OPTIONAL, IN OUT PSIZE_T ViewSize, IN SECTION_INHERIT InheritDisposition, IN ULONG AllocationType, IN ULONG Protect)
Definition: section.c:3996
_Must_inspect_result_ _In_ WDFCOLLECTION _In_ WDFOBJECT Object

Definition at line 37 of file ZwMapViewOfSection.c.

◆ NEW_CONTENT

#define NEW_CONTENT   "NewContent"

Definition at line 11 of file ZwMapViewOfSection.c.

◆ NEW_CONTENT_LEN

#define NEW_CONTENT_LEN   sizeof(NEW_CONTENT)

Definition at line 12 of file ZwMapViewOfSection.c.

◆ TestMapView

#define TestMapView (   SectionHandle,
  ProcessHandle,
  BaseAddress2,
  ZeroBits,
  CommitSize,
  SectionOffset,
  ViewSize2,
  InheritDisposition,
  AllocationType,
  Win32Protect,
  MapStatus,
  UnmapStatus 
)
Value:
do \
{ \
Status = ZwMapViewOfSection(SectionHandle, ProcessHandle, BaseAddress2, ZeroBits, CommitSize, SectionOffset, ViewSize2, InheritDisposition, AllocationType, Win32Protect); \
ok_eq_hex(Status, MapStatus); \
{ \
Status = ZwUnmapViewOfSection(ProcessHandle, BaseAddress); \
if (UnmapStatus != IGNORE) ok_eq_hex(Status, UnmapStatus); \
*BaseAddress2 = NULL; \
*ViewSize2 = 0; \
} \
} while (0) \

Definition at line 24 of file ZwMapViewOfSection.c.

Function Documentation

◆ AdvancedErrorChecks()

static VOID AdvancedErrorChecks ( HANDLE  FileHandleReadOnly,
HANDLE  FileHandleWriteOnly 
)
static

Definition at line 233 of file ZwMapViewOfSection.c.

234{
237 HANDLE FileSectionHandle;
240 SIZE_T ViewSize = 0;
242
243 MaximumSize.QuadPart = TestStringSize;
244 //Used for parameters working on file-based section
245 Status = ZwCreateSection(&FileSectionHandle, SECTION_ALL_ACCESS, NULL, &MaximumSize, PAGE_READWRITE, SEC_COMMIT, FileHandleWriteOnly);
247
248 Status = ObReferenceObjectByHandle(FileSectionHandle,
250 NULL,
253 NULL);
254
256
257 //Bypassing Zw function calls mean bypassing the alignment checks which are not crucial for the branches being tested here
258
259 //test first conditional branch
260 ViewSize = -1;
262
263 //test second conditional branch
264 ViewSize = 1;
265 SectionOffset.QuadPart = TestStringSize;
267
269 ZwClose(FileSectionHandle);
270}
#define MmTestMapView(Object, ProcessHandle, BaseAddress2, ZeroBits, CommitSize, SectionOffset, ViewSize2, InheritDisposition, AllocationType, Win32Protect, MapStatus, UnmapStatus)
const ULONG TestStringSize
LONG NTSTATUS
Definition: precomp.h:26
_Must_inspect_result_ _Outptr_ PVOID * SectionObject
Definition: fsrtlfuncs.h:860
#define KernelMode
Definition: asm.h:34
_In_ ACCESS_MASK _In_opt_ POBJECT_ATTRIBUTES _In_opt_ PLARGE_INTEGER MaximumSize
Definition: mmfuncs.h:362
_In_ HANDLE _Outptr_result_bytebuffer_ ViewSize PVOID _In_ ULONG_PTR _In_ SIZE_T _Inout_opt_ PLARGE_INTEGER _Inout_ PSIZE_T ViewSize
Definition: mmfuncs.h:408
#define SEC_COMMIT
Definition: mmtypes.h:100
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
#define PAGE_READWRITE
Definition: nt_native.h:1304
#define SECTION_ALL_ACCESS
Definition: nt_native.h:1293
@ ViewUnmap
Definition: nt_native.h:1279
#define STANDARD_RIGHTS_ALL
Definition: nt_native.h:69
#define MEM_RESERVE
Definition: nt_native.h:1314
#define STATUS_INVALID_VIEW_SIZE
Definition: ntstatus.h:268
NTSTATUS NTAPI ObReferenceObjectByHandle(IN HANDLE Handle, IN ACCESS_MASK DesiredAccess, IN POBJECT_TYPE ObjectType, IN KPROCESSOR_MODE AccessMode, OUT PVOID *Object, OUT POBJECT_HANDLE_INFORMATION HandleInformation OPTIONAL)
Definition: obref.c:494
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define ObDereferenceObject
Definition: obfuncs.h:203
#define PsGetCurrentProcess
Definition: psfuncs.h:17

Referenced by START_TEST().

◆ BehaviorChecks()

static VOID BehaviorChecks ( HANDLE  FileHandleReadOnly,
HANDLE  FileHandleWriteOnly 
)
static

Definition at line 351 of file ZwMapViewOfSection.c.

352{
356 HANDLE WriteSectionHandle;
357 HANDLE SysThreadHandle;
361 SIZE_T Match;
362 SIZE_T ViewSize = 0;
363
365 MaximumSize.QuadPart = TestStringSize;
366 SectionOffset.QuadPart = 0;
367
368 Status = ZwCreateSection(&WriteSectionHandle, SECTION_ALL_ACCESS, &ObjectAttributes, &MaximumSize, PAGE_READWRITE, SEC_COMMIT, FileHandleWriteOnly);
369 CheckObject(WriteSectionHandle, 3, 1);
370 ok(NT_SUCCESS(Status), "Error creating write section from file. Error = %p\n", Status);
371
372 //check for section reading/writing by comparing section content to a well-known value.
373 Status = ZwMapViewOfSection(WriteSectionHandle, NtCurrentProcess() ,&BaseAddress, 0, 0, &SectionOffset, &ViewSize, ViewUnmap, 0, PAGE_READWRITE);
374 CheckObject(WriteSectionHandle, 3, 1);
375 if (!skip(NT_SUCCESS(Status), "Error mapping view with READ/WRITE priv. Error = %p\n", Status))
376 {
379
380 //now check writing to section
382
385
386 //check to see if the contents have been flushed to the actual file on disk.
387 Match = CompareFileContents(FileHandleWriteOnly, NEW_CONTENT_LEN, NEW_CONTENT);
389
390 //bring everything back to normal
392
393 //Initiate an external thread to modify the file
396 if (!skip(NT_SUCCESS(Status), "Error creating System thread. Error = %p\n", Status))
397 {
399 if (!skip(NT_SUCCESS(Status), "Error getting reference to System thread when testing file-backed section\n"))
400 {
401 //wait until the system thread actually terminates
403
404 //no longer need the thread object
406
407 //test for bi-directional access to the shared page file
410
411 //bring everything back to normal, again
413 }
414 }
415
416 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
417 }
418
419 //Try to write to read-only mapped view
421 ViewSize = 0;
422 SectionOffset.QuadPart = 0;
423 Status = ZwMapViewOfSection(WriteSectionHandle, NtCurrentProcess(), &BaseAddress, 0, 0, &SectionOffset, &ViewSize, ViewUnmap, 0, PAGE_READONLY);
424 if (!skip(NT_SUCCESS(Status), "Error mapping view with READ priv. Error = %p\n", Status))
425 {
428
432
433 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
434 }
435
436 //try to access forbidden memory
438 ViewSize = 0;
440 Status = ZwMapViewOfSection(WriteSectionHandle, NtCurrentProcess(), &BaseAddress, 0, 0, &SectionOffset, &ViewSize, ViewUnmap, 0, PAGE_NOACCESS);
442 {
446
447 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
448 }
449
450 //try to access guarded memory
452 ViewSize = 0;
454 Status = ZwMapViewOfSection(WriteSectionHandle, NtCurrentProcess(), &BaseAddress, 0, 0, &SectionOffset, &ViewSize, ViewUnmap, 0, PAGE_GUARD | PAGE_READWRITE);
456 {
460
464
465 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
466 }
467
468 ZwClose(WriteSectionHandle);
469
470 //section created with sec_reserve should not be commited.
472 ViewSize = 0;
474 Status = ZwCreateSection(&WriteSectionHandle, SECTION_ALL_ACCESS, &ObjectAttributes, &MaximumSize, PAGE_READWRITE, SEC_RESERVE, FileHandleWriteOnly);
475 if (!skip(NT_SUCCESS(Status), "Error creating page file section. Error = %p\n", Status))
476 {
477 Status = ZwMapViewOfSection(WriteSectionHandle, NtCurrentProcess(), &BaseAddress, 0, TestStringSize, &SectionOffset, &ViewSize, ViewUnmap, MEM_RESERVE, PAGE_READWRITE);
478 if (!skip(NT_SUCCESS(Status), "Error mapping page file view. Error = %p\n", Status))
479 {
480 //check also the SEC_COMMIT flag
481 /* This test proves that MSDN is once again wrong
482 * msdn.microsoft.com/en-us/library/windows/hardware/aa366537.aspx states that SEC_RESERVE
483 * should cause the allocated memory for the view to be reserved but in fact it is always committed.
484 * It fails also on windows.
485 */
487 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
488 }
489
490 ZwClose(WriteSectionHandle);
491 }
492}
static VOID NTAPI SystemProcessWorker(PVOID StartContext)
static SIZE_T CompareFileContents(HANDLE FileHandle, ULONG BufferLength, PVOID Buffer)
static UNICODE_STRING SharedSectionName
#define CheckObject(Handle, Pointers, Handles)
#define NEW_CONTENT_LEN
#define NEW_CONTENT
#define ok_eq_size(value, expected)
Definition: apitest.h:69
#define ok(value,...)
Definition: atltest.h:57
#define skip(...)
Definition: atltest.h:64
BOOL Error
Definition: chkdsk.c:66
IN PUNICODE_STRING IN POBJECT_ATTRIBUTES ObjectAttributes
Definition: conport.c:36
#define FALSE
Definition: types.h:117
#define PAGE_READONLY
Definition: compat.h:138
#define KeWaitForSingleObject(pEvt, foo, a, b, c)
Definition: env_spec_w32.h:478
#define RtlCompareMemory(s1, s2, l)
Definition: env_spec_w32.h:465
#define PAGE_SIZE
Definition: env_spec_w32.h:49
GLdouble n
Definition: glext.h:7729
GLenum GLenum GLenum GLenum mapping
Definition: glext.h:9031
GLfloat GLfloat p
Definition: glext.h:8902
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
#define KmtStartSeh()
Definition: kmt_test.h:282
#define Test_NtQueryVirtualMemory(BaseAddress, Size, AllocationType, ProtectionType)
Definition: kmt_test.h:72
#define KmtEndSeh(ExpectedStatus)
Definition: kmt_test.h:288
if(dx< 0)
Definition: linetemp.h:194
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
@ ThreadObject
Definition: ketypes.h:412
#define THREAD_ALL_ACCESS
Definition: nt_native.h:1339
#define SEC_RESERVE
Definition: nt_native.h:1323
#define NtCurrentProcess()
Definition: nt_native.h:1657
#define MEM_COMMIT
Definition: nt_native.h:1313
#define PAGE_NOACCESS
Definition: nt_native.h:1302
#define PAGE_GUARD
Definition: nt_native.h:1310
POBJECT_TYPE PsThreadType
Definition: thread.c:20
NTSTATUS NTAPI PsCreateSystemThread(OUT PHANDLE ThreadHandle, IN ACCESS_MASK DesiredAccess, IN POBJECT_ATTRIBUTES ObjectAttributes, IN HANDLE ProcessHandle, IN PCLIENT_ID ClientId, IN PKSTART_ROUTINE StartRoutine, IN PVOID StartContext)
Definition: thread.c:602
#define STATUS_ACCESS_VIOLATION
Definition: ntstatus.h:242
#define STATUS_GUARD_PAGE_VIOLATION
Definition: ntstatus.h:182
Definition: fci.c:127
Definition: module.h:576
Definition: parser.c:56
EH_STD::basic_string< char, EH_STD::char_traits< char >, eh_allocator(char) > TestString
Definition: test_string.cpp:30
#define RtlCopyMemory(Destination, Source, Length)
Definition: typedefs.h:263
@ Executive
Definition: ketypes.h:415
ret QuadPart
Definition: rtlfuncs.h:3089

Referenced by START_TEST().

◆ CompareFileContents()

static SIZE_T CompareFileContents ( HANDLE  FileHandle,
ULONG  BufferLength,
PVOID  Buffer 
)
static

Definition at line 274 of file ZwMapViewOfSection.c.

275{
279 PVOID FileContent;
280 SIZE_T Match;
281
282 Match = 0;
283 ByteOffset.QuadPart = 0;
284
285 FileContent = ExAllocatePoolWithTag(PagedPool, BufferLength, 'Test');
286 if (!skip((FileContent != NULL), "Error allocating memory for FileContent\n"))
287 {
288 Status = ZwReadFile(FileHandle, NULL, NULL, NULL, &IoStatusBlock, FileContent, BufferLength, &ByteOffset, NULL);
291
292 Match = 0;
293 Match = RtlCompareMemory(FileContent, Buffer, BufferLength);
294 ExFreePoolWithTag(FileContent, 'Test');
295 }
296
297 return Match;
298}
#define ok_eq_ulongptr(value, expected)
Definition: apitest.h:71
Definition: bufpool.h:45
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define PagedPool
Definition: env_spec_w32.h:308
IN PDCB IN PCCB IN VBO IN OUT PULONG OUT PDIRENT OUT PBCB OUT PVBO ByteOffset
Definition: fatprocs.h:731
_Must_inspect_result_ _In_opt_ PFLT_INSTANCE _Out_ PHANDLE FileHandle
Definition: fltkernel.h:1231
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1109
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
_Must_inspect_result_ _In_ WDFDEVICE _In_ DEVICE_REGISTRY_PROPERTY _In_ ULONG BufferLength
Definition: wdfdevice.h:3771

Referenced by BehaviorChecks().

◆ KmtInitTestFiles()

static VOID KmtInitTestFiles ( PHANDLE  ReadOnlyFile,
PHANDLE  WriteOnlyFile,
PHANDLE  ExecutableFile 
)
static

Definition at line 62 of file ZwMapViewOfSection.c.

63{
67
68 //INIT THE READ-ONLY FILE
71 ok(*ReadOnlyFile != NULL, "Couldn't acquire READONLY handle\n");
72
73 //INIT THE EXECUTABLE FILE
76 ok(*ExecutableFile != NULL, "Couldn't acquire EXECUTE handle\n");
77
78 //INIT THE WRITE-ONLY FILE
79 //TODO: Delete the file when the tests are all executed
83 ok(*WriteOnlyFile != NULL, "WriteOnlyFile is NULL\n");
84 if (!skip(*WriteOnlyFile != NULL, "No WriteOnlyFile\n"))
85 {
86 FileOffset.QuadPart = 0;
87 Status = ZwWriteFile(*WriteOnlyFile, NULL, NULL, NULL, &IoStatusBlock, (PVOID)TestString, TestStringSize, &FileOffset, NULL);
88 ok(Status == STATUS_SUCCESS || Status == STATUS_PENDING, "Status = 0x%08lx\n", Status);
89 Status = ZwWaitForSingleObject(*WriteOnlyFile, FALSE, NULL);
92 }
93}
static OBJECT_ATTRIBUTES KmtestFileObject
static OBJECT_ATTRIBUTES NtdllObject
#define FILE_NON_DIRECTORY_FILE
Definition: constants.h:492
#define FILE_DELETE_ON_CLOSE
Definition: constants.h:494
_In_ PFCB _In_ LONGLONG FileOffset
Definition: cdprocs.h:160
#define GENERIC_READ
Definition: compat.h:135
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
#define FILE_SHARE_READ
Definition: compat.h:136
#define FILE_OPEN
Definition: from_kernel.h:54
#define FILE_SUPERSEDE
Definition: from_kernel.h:53
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define SYNCHRONIZE
Definition: nt_native.h:61
#define FILE_CREATED
Definition: nt_native.h:770
#define GENERIC_WRITE
Definition: nt_native.h:90
#define GENERIC_EXECUTE
Definition: nt_native.h:91
#define STATUS_PENDING
Definition: ntstatus.h:82

Referenced by START_TEST().

◆ PageFileBehaviorChecks()

static VOID PageFileBehaviorChecks ( )
static

Definition at line 497 of file ZwMapViewOfSection.c.

498{
500 LARGE_INTEGER MaxSectionSize;
502 HANDLE PageFileSectionHandle;
505 SIZE_T Match;
508
509 MaxSectionSize.QuadPart = TestStringSize;
510 SectionOffset.QuadPart = 0;
511 PageFileSectionHandle = INVALID_HANDLE_VALUE;
515
516 //test memory sharing between 2 different processes
517 Status = ZwCreateSection(&PageFileSectionHandle, SECTION_ALL_ACCESS, &ObjectAttributes, &MaxSectionSize, PAGE_READWRITE, SEC_COMMIT, NULL);
518 if (!skip(NT_SUCCESS(Status), "Error creating page file section. Error = %p\n", Status))
519 {
520 CheckObject(PageFileSectionHandle, 3, 1);
521 Status = ZwMapViewOfSection(PageFileSectionHandle, NtCurrentProcess(), &BaseAddress, 0, TestStringSize, &SectionOffset, &ViewSize, ViewUnmap, 0, PAGE_READWRITE);
522 if (!skip(NT_SUCCESS(Status), "Error mapping page file view. Error = %p\n", Status))
523 {
524 HANDLE SysThreadHandle;
525
526 CheckObject(PageFileSectionHandle, 3, 1);
527
528 //check also the SEC_COMMIT flag
530
532
535
536 if (!skip(NT_SUCCESS(Status), "Error creating System thread. Error = %p\n", Status))
537 {
539 if (!skip(NT_SUCCESS(Status), "Error getting reference to System thread when testing pagefile-backed section\n"))
540 {
541 //wait until the system thread actually terminates
543
544 //no longer need the thread object
546
547 //test for bi-directional access to the shared page file
550 }
551 }
552 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
553 }
554 ZwClose(PageFileSectionHandle);
555 }
556}
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
LONGLONG QuadPart
Definition: typedefs.h:114

Referenced by START_TEST().

◆ SimpleErrorChecks()

static VOID SimpleErrorChecks ( HANDLE  FileHandleReadOnly,
HANDLE  FileHandleWriteOnly,
HANDLE  ExecutableImg 
)
static

Definition at line 97 of file ZwMapViewOfSection.c.

98{
100 HANDLE WriteSectionHandle;
101 HANDLE ReadOnlySection;
102 HANDLE PageFileSectionHandle;
105 SIZE_T AllocSize = TestStringSize;
106 SIZE_T ViewSize = 0;
108 PVOID AllocBase = NULL;
109 MaximumSize.QuadPart = TestStringSize;
110
111 //Used for parameters working on file-based section
112 Status = ZwCreateSection(&WriteSectionHandle, SECTION_ALL_ACCESS, NULL, &MaximumSize, PAGE_READWRITE, SEC_COMMIT, FileHandleWriteOnly);
114
115 Status = ZwCreateSection(&ReadOnlySection, SECTION_ALL_ACCESS, NULL, &MaximumSize, PAGE_READONLY, SEC_COMMIT, FileHandleReadOnly);
117
118 //Used for parameters taking effect only on page-file backed section
120 Status = ZwCreateSection(&PageFileSectionHandle, SECTION_ALL_ACCESS, NULL, &MaximumSize, PAGE_READWRITE, SEC_COMMIT, NULL);
122
123 MaximumSize.QuadPart = TestStringSize;
124
125 //section handle
130
131 //process handle
132 TestMapView(WriteSectionHandle, (HANDLE)(ULONG_PTR)0xDEADBEEFDEADBEEFull, &BaseAddress, 0, 0, NULL, &ViewSize, ViewUnmap, 0, PAGE_READWRITE, STATUS_INVALID_HANDLE, IGNORE);
134
135 //base address
136 BaseAddress = (PVOID)(ULONG_PTR)0x00567A20;
138
139 BaseAddress = (PVOID)(ULONG_PTR)0x60000000;
141
142 BaseAddress = (PVOID)((char *)MmSystemRangeStart + 200);
144
145 //invalid section handle AND unaligned base address
146 BaseAddress = (PVOID)(ULONG_PTR)0x00567A20;
148
149 //invalid process handle AND unaligned base address
150 BaseAddress = (PVOID)(ULONG_PTR)0x00567A20;
151 TestMapView(WriteSectionHandle, (HANDLE)(ULONG_PTR)0xDEADBEEFDEADBEEFull, &BaseAddress, 0, 0, NULL, &ViewSize, ViewUnmap, 0, PAGE_READWRITE, STATUS_INVALID_HANDLE, IGNORE);
152
153 //try mapping section to an already mapped address
154 Status = ZwAllocateVirtualMemory(NtCurrentProcess(), &AllocBase, 0, &AllocSize, MEM_COMMIT, PAGE_READWRITE);
155 if (!skip(NT_SUCCESS(Status), "Cannot allocate memory\n"))
156 {
157 BaseAddress = AllocBase;
159 Status = ZwFreeVirtualMemory(NtCurrentProcess(), &AllocBase, &AllocSize, MEM_RELEASE);
161 }
162
163 //zero bits
170
171 //commit size
178
179 //section offset
180 SectionOffset.QuadPart = 0;
182 ok_eq_ulonglong(SectionOffset.QuadPart, 0);
183
184 SectionOffset.QuadPart = 0x00040211; //MSDN is wrong, in w2k3 the ZwMapViewOfSection doesn't align offsets automatically
186
187 SectionOffset.QuadPart = -1;
189
190 //View Size
192
193 ViewSize = -1;
195
199
202
205
206 //allocation type
211
212 //win32protect
224
225 ZwClose(WriteSectionHandle);
226 ZwClose(PageFileSectionHandle);
227 ZwClose(ReadOnlySection);
228}
#define TestMapView(SectionHandle, ProcessHandle, BaseAddress2, ZeroBits, CommitSize, SectionOffset, ViewSize2, InheritDisposition, AllocationType, Win32Protect, MapStatus, UnmapStatus)
#define ok_eq_ulonglong(value, expected)
Definition: apitest.h:65
#define MM_ALLOCATION_GRANULARITY
Definition: mmtypes.h:36
#define PAGE_WRITECOPY
Definition: nt_native.h:1305
#define PAGE_EXECUTE_READ
Definition: nt_native.h:1307
#define PAGE_EXECUTE
Definition: nt_native.h:1306
#define PAGE_EXECUTE_WRITECOPY
Definition: nt_native.h:1309
#define MEM_LARGE_PAGES
Definition: nt_native.h:1322
#define MEM_RELEASE
Definition: nt_native.h:1316
#define MmSystemRangeStart
Definition: mm.h:32
#define STATUS_MAPPED_ALIGNMENT
Definition: ntstatus.h:676
#define STATUS_INVALID_HANDLE
Definition: ntstatus.h:245
#define STATUS_SECTION_PROTECTION
Definition: ntstatus.h:314
#define STATUS_INVALID_PARAMETER_9
Definition: ntstatus.h:483
#define STATUS_INVALID_PARAMETER_4
Definition: ntstatus.h:478
#define STATUS_NO_MEMORY
Definition: ntstatus.h:260
#define STATUS_CONFLICTING_ADDRESSES
Definition: ntstatus.h:261
#define STATUS_INVALID_PAGE_PROTECTION
Definition: ntstatus.h:305
#define STATUS_INVALID_PARAMETER_3
Definition: ntstatus.h:477
#define STATUS_INVALID_PARAMETER_5
Definition: ntstatus.h:479
#define STATUS_OBJECT_TYPE_MISMATCH
Definition: ntstatus.h:273
void * PVOID
Definition: typedefs.h:50
uint32_t ULONG_PTR
Definition: typedefs.h:65

Referenced by START_TEST().

◆ START_TEST()

START_TEST ( ZwMapViewOfSection  )

Definition at line 559 of file ZwMapViewOfSection.c.

560{
561 HANDLE FileHandleReadOnly = NULL;
562 HANDLE FileHandleWriteOnly = NULL;
563 HANDLE ExecutableFileHandle = NULL;
564
568
569 KmtInitTestFiles(&FileHandleReadOnly, &FileHandleWriteOnly, &ExecutableFileHandle);
570
571 SimpleErrorChecks(FileHandleReadOnly, FileHandleWriteOnly, ExecutableFileHandle);
572 AdvancedErrorChecks(FileHandleReadOnly, FileHandleWriteOnly);
573 BehaviorChecks(FileHandleReadOnly, FileHandleWriteOnly);
575
576 if (FileHandleReadOnly)
577 ZwClose(FileHandleReadOnly);
578
579 if (FileHandleWriteOnly)
580 ZwClose(FileHandleWriteOnly);
581
582 if (ExecutableFileHandle)
583 ZwClose(ExecutableFileHandle);
584}
static OBJECT_ATTRIBUTES NtoskrnlFileObject
static VOID KmtInitTestFiles(PHANDLE ReadOnlyFile, PHANDLE WriteOnlyFile, PHANDLE ExecutableFile)
static UNICODE_STRING FileReadOnlyPath
static VOID AdvancedErrorChecks(HANDLE FileHandleReadOnly, HANDLE FileHandleWriteOnly)
static VOID SimpleErrorChecks(HANDLE FileHandleReadOnly, HANDLE FileHandleWriteOnly, HANDLE ExecutableImg)
static UNICODE_STRING WritableFilePath
static VOID BehaviorChecks(HANDLE FileHandleReadOnly, HANDLE FileHandleWriteOnly)
static VOID PageFileBehaviorChecks()
static UNICODE_STRING NtosImgPath

◆ SystemProcessWorker()

static VOID NTAPI SystemProcessWorker ( PVOID  StartContext)
static

Definition at line 304 of file ZwMapViewOfSection.c.

305{
308 HANDLE SectionHandle;
310 SIZE_T Match;
313
314 UNREFERENCED_PARAMETER(StartContext);
315
318 SectionOffset.QuadPart = 0;
319
322 if (!skip(NT_SUCCESS(Status), "Error acquiring handle to section. Error = %p\n", Status))
323 {
324 CheckObject(SectionHandle, 4, 2);
325 Status = ZwMapViewOfSection(SectionHandle, NtCurrentProcess(), &BaseAddress, 0, TestStringSize, &SectionOffset, &ViewSize, ViewUnmap, 0, PAGE_READWRITE);
326
327 //make sure ZwMapViewofSection doesn't touch the section ref counts.
328 CheckObject(SectionHandle, 4, 2);
329
330 if (!skip(NT_SUCCESS(Status), "Error mapping page file view in system process. Error = %p\n", Status))
331 {
334
336 ZwUnmapViewOfSection(NtCurrentProcess(), BaseAddress);
337
338 //make sure ZwMapViewofSection doesn't touch the section ref counts.
339 CheckObject(SectionHandle, 4, 2);
340 }
341
342 ZwClose(SectionHandle);
343 }
344
346}
NTSYSAPI NTSTATUS NTAPI ZwOpenSection(_Out_ PHANDLE SectionHandle, _In_ ACCESS_MASK DesiredAccess, _In_ POBJECT_ATTRIBUTES ObjectAttributes)
#define UNREFERENCED_PARAMETER(P)
Definition: ntbasedef.h:317
NTSTATUS NTAPI PsTerminateSystemThread(IN NTSTATUS ExitStatus)
Definition: kill.c:1145

Referenced by BehaviorChecks(), and PageFileBehaviorChecks().

Variable Documentation

◆ FileReadOnlyPath

UNICODE_STRING FileReadOnlyPath = RTL_CONSTANT_STRING(L"\\SystemRoot\\system32\\ntdll.dll")
static

Definition at line 14 of file ZwMapViewOfSection.c.

Referenced by START_TEST().

◆ KmtestFileObject

OBJECT_ATTRIBUTES KmtestFileObject
static

Definition at line 21 of file ZwMapViewOfSection.c.

Referenced by KmtInitTestFiles(), and START_TEST().

◆ NtdllObject

OBJECT_ATTRIBUTES NtdllObject
static

Definition at line 20 of file ZwMapViewOfSection.c.

Referenced by KmtInitTestFiles(), and START_TEST().

◆ NtosImgPath

UNICODE_STRING NtosImgPath = RTL_CONSTANT_STRING(L"\\SystemRoot\\system32\\ntoskrnl.exe")
static

Definition at line 15 of file ZwMapViewOfSection.c.

Referenced by START_TEST().

◆ NtoskrnlFileObject

OBJECT_ATTRIBUTES NtoskrnlFileObject
static

Definition at line 22 of file ZwMapViewOfSection.c.

Referenced by START_TEST().

◆ SharedSectionName

UNICODE_STRING SharedSectionName = RTL_CONSTANT_STRING(L"\\BaseNamedObjects\\kmtest-SharedSection")
static

◆ TestString

const char TestString[]
extern

Definition at line 16 of file ZwAllocateVirtualMemory.c.

◆ TestStringSize

◆ WritableFilePath

UNICODE_STRING WritableFilePath = RTL_CONSTANT_STRING(L"\\SystemRoot\\kmtest-MmSection.txt")
static

Definition at line 16 of file ZwMapViewOfSection.c.

Referenced by START_TEST().