ReactOS 0.4.15-dev-5664-g3bf4ef6
ZwCreateSection.c
Go to the documentation of this file.
1/*
2* PROJECT: ReactOS kernel-mode tests
3* LICENSE: GPLv2+ - See COPYING in the top level directory
4* PURPOSE: Kernel-Mode Test Suite ZwCreateSection
5* PROGRAMMER: Nikolay Borisov <nib9@aber.ac.uk>
6*/
7
8#include <kmt_test.h>
9
10#define IGNORE -999
11#define NO_HANDLE_CLOSE -998
12#define _4mb 4194304
13extern const char TestString[];
14extern const ULONG TestStringSize;
15static UNICODE_STRING FileReadOnlyPath = RTL_CONSTANT_STRING(L"\\SystemRoot\\system32\\ntdll.dll");
16static UNICODE_STRING WritableFilePath = RTL_CONSTANT_STRING(L"\\SystemRoot\\kmtest-MmSection.txt");
17static UNICODE_STRING CalcImgPath = RTL_CONSTANT_STRING(L"\\SystemRoot\\system32\\calc.exe");
21
22#define CREATE_SECTION(Handle, DesiredAccess, Attributes, Size, SectionPageProtection, AllocationAttributes, FileHandle, RetStatus, CloseRetStatus) do \
23{ \
24 Status = ZwCreateSection(&Handle, DesiredAccess, Attributes, &Size, SectionPageProtection, AllocationAttributes, FileHandle); \
25 ok_eq_hex(Status, RetStatus); \
26 if (NT_SUCCESS(Status)) \
27 { \
28 if (CloseRetStatus != NO_HANDLE_CLOSE) \
29 { \
30 Status = ZwClose(Handle); \
31 Handle = NULL; \
32 if (CloseRetStatus != IGNORE) ok_eq_hex(Status, CloseRetStatus); \
33 } \
34 } \
35} while (0)
36
37#define TestMapView(SectionHandle, ProcessHandle, BaseAddress2, ZeroBits, CommitSize, SectionOffset, ViewSize2, InheritDisposition, AllocationType, Win32Protect, MapStatus, UnmapStatus) do \
38{ \
39 Status = ZwMapViewOfSection(SectionHandle, ProcessHandle, BaseAddress2, ZeroBits, CommitSize, SectionOffset, ViewSize2, InheritDisposition, AllocationType, Win32Protect); \
40 ok_eq_hex(Status, MapStatus); \
41 if (NT_SUCCESS(Status)) \
42 { \
43 Status = ZwUnmapViewOfSection(ProcessHandle, BaseAddress); \
44 if (UnmapStatus != IGNORE) ok_eq_hex(Status, UnmapStatus); \
45 *BaseAddress2 = NULL; \
46 *ViewSize2 = 0; \
47 } \
48} while (0)
49
50#define CheckObject(Handle, Pointers, Handles) do \
51{ \
52 PUBLIC_OBJECT_BASIC_INFORMATION ObjectInfo; \
53 Status = ZwQueryObject(Handle, ObjectBasicInformation, \
54 &ObjectInfo, sizeof ObjectInfo, NULL); \
55 ok_eq_hex(Status, STATUS_SUCCESS); \
56 ok_eq_ulong(ObjectInfo.PointerCount, Pointers); \
57 ok_eq_ulong(ObjectInfo.HandleCount, Handles); \
58} while (0)
59
60
61#define CheckSection(SectionHandle, SectionFlag, SectionSize, RetStatus) do \
62{ \
63 SECTION_BASIC_INFORMATION Sbi; \
64 NTSTATUS Status; \
65 Status = ZwQuerySection(SectionHandle, SectionBasicInformation, \
66 &Sbi, sizeof Sbi, NULL); \
67 ok_eq_hex(Status, RetStatus); \
68 if (RetStatus == STATUS_SUCCESS && NT_SUCCESS(Status)) \
69 { \
70 ok_eq_pointer(Sbi.BaseAddress, NULL); \
71 ok_eq_longlong(Sbi.Size.QuadPart, SectionSize); \
72 ok_eq_hex(Sbi.Attributes, SectionFlag | SEC_FILE); \
73 } \
74} while (0)
75
76static
77VOID
78FileSectionViewPermissionCheck(HANDLE ReadOnlyFile, HANDLE WriteOnlyFile, HANDLE ExecutableFile)
79{
81 HANDLE SectionHandle = NULL;
83 SIZE_T ViewSize = 0;
85
86 MaximumSize.QuadPart = TestStringSize;
87
88 //READ-ONLY FILE COMBINATIONS
93 ZwClose(SectionHandle);
94
99 ZwClose(SectionHandle);
100
105 ZwClose(SectionHandle);
106
111 ZwClose(SectionHandle);
112
117 ZwClose(SectionHandle);
118
123 ZwClose(SectionHandle);
124
129 ZwClose(SectionHandle);
130
135 ZwClose(SectionHandle);
136
141 ZwClose(SectionHandle);
142
147 ZwClose(SectionHandle);
148
153 ZwClose(SectionHandle);
154
159 ZwClose(SectionHandle);
160
161 //WRITE-ONLY FILE COMBINATIONS
166 ZwClose(SectionHandle);
167
172 ZwClose(SectionHandle);
173
178 ZwClose(SectionHandle);
179
184 ZwClose(SectionHandle);
185
190 ZwClose(SectionHandle);
191
196 ZwClose(SectionHandle);
197
202 ZwClose(SectionHandle);
203
208 ZwClose(SectionHandle);
209
214 ZwClose(SectionHandle);
215
220 ZwClose(SectionHandle);
221
226 ZwClose(SectionHandle);
227
232 ZwClose(SectionHandle);
233
234 //EXECUTE ONLY FILE
239
244
249}
250
251static
252VOID
253KmtInitTestFiles(PHANDLE ReadOnlyFile, PHANDLE WriteOnlyFile, PHANDLE ExecutableFile)
254{
258
259 //INIT THE READ-ONLY FILE
262 ok(*ReadOnlyFile != NULL, "Couldn't acquire READONLY handle\n");
263
264 //INIT THE EXECUTABLE FILE
267 ok(*ExecutableFile != NULL, "Couldn't acquire EXECUTE handle\n");
268
269 //INIT THE WRITE-ONLY FILE
270 //NB: this file is deleted at the end of basic behavior checks
274 ok(*WriteOnlyFile != NULL, "WriteOnlyFile is NULL\n");
275 if (!skip(*WriteOnlyFile != NULL, "No WriteOnlyFile\n"))
276 {
277 FileOffset.QuadPart = 0;
278 Status = ZwWriteFile(*WriteOnlyFile, NULL, NULL, NULL, &IoStatusBlock, (PVOID)TestString, TestStringSize, &FileOffset, NULL);
279 ok(Status == STATUS_SUCCESS || Status == STATUS_PENDING, "Status = 0x%08lx\n", Status);
280 Status = ZwWaitForSingleObject(*WriteOnlyFile, FALSE, NULL);
283 }
284}
285
286static
287VOID
288SimpleErrorChecks(HANDLE FileHandleReadOnly, HANDLE FileHandleWriteOnly, HANDLE FileHandleExecuteOnly)
289{
291 HANDLE Section = NULL;
292 OBJECT_ATTRIBUTES ObjectAttributesReadOnly;
293 OBJECT_ATTRIBUTES ObjectAttributesWriteOnly;
294 OBJECT_ATTRIBUTES InvalidObjectAttributes;
296 FILE_STANDARD_INFORMATION FileStandardInfo;
298 UNICODE_STRING SectReadOnly = RTL_CONSTANT_STRING(L"\\BaseNamedObjects\\KmtTestReadSect");
299 UNICODE_STRING SectWriteOnly = RTL_CONSTANT_STRING(L"\\BaseNamedObjects\\KmtTestWriteSect");
300 UNICODE_STRING InvalidObjectString = RTL_CONSTANT_STRING(L"THIS/IS/INVALID");
301
302 MaximumSize.QuadPart = TestStringSize;
303
304 InitializeObjectAttributes(&ObjectAttributesReadOnly, &SectReadOnly, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
305 InitializeObjectAttributes(&ObjectAttributesWriteOnly, &SectWriteOnly, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
306 InitializeObjectAttributes(&InvalidObjectAttributes, &InvalidObjectString, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
307
309 //PAGE FILE BACKED SECTION
310 //DESIRED ACCESS TESTS
314
315 //OBJECT ATTRIBUTES
318
319 //MAXIMUM SIZE
320 MaximumSize.QuadPart = -1;
322
323 MaximumSize.QuadPart = 0;
325
326 //division by zero in ROS
327 if (!skip(SharedUserData->LargePageMinimum > 0, "LargePageMinimum is 0\n"))
328 {
329 MaximumSize.QuadPart = (_4mb / SharedUserData->LargePageMinimum) * SharedUserData->LargePageMinimum; //4mb
331 }
332
333 MaximumSize.QuadPart = TestStringSize;
334
335 //SECTION PAGE PROTECTION
345
346 //ALLOCATION ATTRIBUTES
359
361 //NORMAL FILE-BACKED SECTION
362
363 //DESIRED ACCESS TESTS
364 CREATE_SECTION(Section, SECTION_ALL_ACCESS, &ObjectAttributesReadOnly, MaximumSize, PAGE_READONLY, SEC_COMMIT, FileHandleReadOnly, STATUS_SUCCESS, STATUS_SUCCESS);
365 CREATE_SECTION(Section, SECTION_ALL_ACCESS, &ObjectAttributesWriteOnly, MaximumSize, PAGE_WRITECOPY, SEC_COMMIT, FileHandleWriteOnly, STATUS_SUCCESS, STATUS_SUCCESS);
366 CREATE_SECTION(Section, SECTION_MAP_WRITE, &ObjectAttributesReadOnly, MaximumSize, PAGE_READONLY, SEC_COMMIT, FileHandleReadOnly, STATUS_SUCCESS, STATUS_SUCCESS);
367 CREATE_SECTION(Section, SECTION_MAP_READ, &ObjectAttributesWriteOnly, MaximumSize, PAGE_WRITECOPY, SEC_COMMIT, FileHandleWriteOnly, STATUS_SUCCESS, STATUS_SUCCESS);
368
369 //Object Attributes
371 CREATE_SECTION(Section, SECTION_ALL_ACCESS, &InvalidObjectAttributes, MaximumSize, PAGE_READONLY, SEC_COMMIT, FileHandleReadOnly, STATUS_OBJECT_PATH_SYNTAX_BAD, IGNORE);
372
373 //MAXIMUM SIZE
374 MaximumSize.QuadPart = TestStringSize - 100;
376
377 MaximumSize.QuadPart = -1;
379
380 MaximumSize.QuadPart = TestStringSize + 1;
382
383 MaximumSize.QuadPart = 0;
385
386 //SECTION PAGE PROTECTION
396
397 //allocation type
410
412 //EXECUTABLE IMAGE
413 CREATE_SECTION(Section, SECTION_MAP_READ, &ObjectAttributesWriteOnly, MaximumSize, PAGE_WRITECOPY, SEC_IMAGE, FileHandleExecuteOnly, STATUS_SUCCESS, STATUS_SUCCESS);
414 CREATE_SECTION(Section, SECTION_MAP_EXECUTE, &ObjectAttributesWriteOnly, MaximumSize, PAGE_WRITECOPY, SEC_IMAGE, FileHandleExecuteOnly, STATUS_SUCCESS, STATUS_SUCCESS);
415
416 //DESIRED ACCESS TESTS
417 CREATE_SECTION(Section, SECTION_ALL_ACCESS, &ObjectAttributesReadOnly, MaximumSize, PAGE_READONLY, SEC_COMMIT, FileHandleExecuteOnly, STATUS_SUCCESS, STATUS_SUCCESS);
418 CREATE_SECTION(Section, SECTION_ALL_ACCESS, &ObjectAttributesWriteOnly, MaximumSize, PAGE_WRITECOPY, SEC_COMMIT, FileHandleExecuteOnly, STATUS_SUCCESS, STATUS_SUCCESS);
419 CREATE_SECTION(Section, SECTION_MAP_WRITE, &ObjectAttributesReadOnly, MaximumSize, PAGE_READONLY, SEC_COMMIT, FileHandleExecuteOnly, STATUS_SUCCESS, STATUS_SUCCESS);
420 CREATE_SECTION(Section, SECTION_MAP_READ, &ObjectAttributesWriteOnly, MaximumSize, PAGE_WRITECOPY, SEC_COMMIT, FileHandleExecuteOnly, STATUS_SUCCESS, STATUS_SUCCESS);
421
422 //Object Attributes
424 CREATE_SECTION(Section, SECTION_ALL_ACCESS, &InvalidObjectAttributes, MaximumSize, PAGE_READONLY, SEC_COMMIT, FileHandleExecuteOnly, STATUS_OBJECT_PATH_SYNTAX_BAD, IGNORE);
425
426 //MaximumSize
427 Status = ZwQueryInformationFile(FileHandleExecuteOnly, &IoStatusBlock, &FileStandardInfo, sizeof(FILE_STANDARD_INFORMATION), FileStandardInformation);
428 if (!skip(NT_SUCCESS(Status), "Cannot query file information\n"))
429 {
430 //as big as file
431 MaximumSize = FileStandardInfo.EndOfFile;
433
434 //less than file
435 MaximumSize.QuadPart = FileStandardInfo.EndOfFile.QuadPart - 2;
437
438 //larger than file
439 MaximumSize.QuadPart = FileStandardInfo.EndOfFile.QuadPart + 2;
441
442 //0
443 MaximumSize.QuadPart = 0;
445
446 //-1 (very big number)
447 MaximumSize.QuadPart = -1;
449 }
450}
451
452static
453VOID
455{
457 HANDLE Section = NULL;
460 Length.QuadPart = TestStringSize;
461
462 //mimic lack of section support for a particular file as well.
464 if (!skip(NT_SUCCESS(Status), "Cannot reference object by handle\n"))
465 {
466 PSECTION_OBJECT_POINTERS Pointers = FileObject->SectionObjectPointer;
467
468 FileObject->SectionObjectPointer = NULL;
470 FileObject->SectionObjectPointer = Pointers;
472 }
473
474 Length.QuadPart = TestStringSize;
476 CheckObject(Section, 2, 1);
477 CheckSection(Section, SEC_FILE, Length.QuadPart, STATUS_SUCCESS);
478 ZwClose(Section); //manually close it due to NO_HANDLE_CLOSE in CREATE_SECTION
479
480 //section length should be set to that of file
481 Length.QuadPart = 0;
484 ZwClose(Section);
485
486 //create a smaller section than file
487 Length.QuadPart = TestStringSize - 100;
490 ZwClose(Section);
491}
492
493
494START_TEST(ZwCreateSection)
495{
496 HANDLE FileHandleReadOnly = NULL;
497 HANDLE FileHandleWriteOnly = NULL;
498 HANDLE FileHandleExecuteOnly = NULL;
499
503
504 KmtInitTestFiles(&FileHandleReadOnly, &FileHandleWriteOnly, &FileHandleExecuteOnly);
505
506 if (!skip(FileHandleReadOnly && FileHandleWriteOnly && FileHandleExecuteOnly, "Missing one or more file handles\n"))
507 {
508 FileSectionViewPermissionCheck(FileHandleReadOnly, FileHandleWriteOnly, FileHandleExecuteOnly);
509 SimpleErrorChecks(FileHandleReadOnly, FileHandleWriteOnly, FileHandleExecuteOnly);
510 BasicBehaviorChecks(FileHandleWriteOnly);
511 }
512
513 if (FileHandleReadOnly)
514 ZwClose(FileHandleReadOnly);
515
516 if (FileHandleWriteOnly)
517 {
518 ZwClose(FileHandleWriteOnly);
520 }
521
522 if (FileHandleExecuteOnly)
523 ZwClose(FileHandleExecuteOnly);
524}
#define ok_eq_hex(value, expected)
Definition: NtContinue.c:18
#define CheckSection(SectionHandle, SectionFlag, SectionSize, RetStatus)
static OBJECT_ATTRIBUTES CalcFileObject
static VOID KmtInitTestFiles(PHANDLE ReadOnlyFile, PHANDLE WriteOnlyFile, PHANDLE ExecutableFile)
#define _4mb
static UNICODE_STRING FileReadOnlyPath
static VOID BasicBehaviorChecks(HANDLE FileHandle)
#define TestMapView(SectionHandle, ProcessHandle, BaseAddress2, ZeroBits, CommitSize, SectionOffset, ViewSize2, InheritDisposition, AllocationType, Win32Protect, MapStatus, UnmapStatus)
#define CheckObject(Handle, Pointers, Handles)
static VOID SimpleErrorChecks(HANDLE FileHandleReadOnly, HANDLE FileHandleWriteOnly, HANDLE FileHandleExecuteOnly)
static UNICODE_STRING WritableFilePath
const ULONG TestStringSize
#define CREATE_SECTION(Handle, DesiredAccess, Attributes, Size, SectionPageProtection, AllocationAttributes, FileHandle, RetStatus, CloseRetStatus)
#define IGNORE
static OBJECT_ATTRIBUTES KmtestFileObject
static UNICODE_STRING CalcImgPath
static VOID FileSectionViewPermissionCheck(HANDLE ReadOnlyFile, HANDLE WriteOnlyFile, HANDLE ExecutableFile)
#define NO_HANDLE_CLOSE
static OBJECT_ATTRIBUTES NtdllObject
#define ok(value,...)
Definition: atltest.h:57
#define skip(...)
Definition: atltest.h:64
#define START_TEST(x)
Definition: atltest.h:75
LONG NTSTATUS
Definition: precomp.h:26
#define FILE_NON_DIRECTORY_FILE
Definition: constants.h:492
_In_ PFCB _In_ LONGLONG FileOffset
Definition: cdprocs.h:160
#define NULL
Definition: types.h:112
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
#define PAGE_READONLY
Definition: compat.h:138
#define SECTION_MAP_READ
Definition: compat.h:139
#define GENERIC_READ
Definition: compat.h:135
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
#define FILE_SHARE_READ
Definition: compat.h:136
_Must_inspect_result_ _In_opt_ PFLT_INSTANCE _Out_ PHANDLE FileHandle
Definition: fltkernel.h:1231
#define FILE_OPEN
Definition: from_kernel.h:54
#define FILE_SUPERSEDE
Definition: from_kernel.h:53
Status
Definition: gdiplustypes.h:25
#define OBJ_KERNEL_HANDLE
Definition: winternl.h:231
#define OBJ_CASE_INSENSITIVE
Definition: winternl.h:228
POBJECT_TYPE IoFileObjectType
Definition: iomgr.c:36
#define ok_eq_ulongptr(value, expected)
Definition: kmt_test.h:251
unsigned int ULONG
Definition: retypes.h:1
static OUT PIO_STATUS_BLOCK IoStatusBlock
Definition: pipe.c:75
#define InitializeObjectAttributes(p, n, a, r, s)
Definition: reg.c:106
#define KernelMode
Definition: asm.h:34
NTSYSAPI NTSTATUS NTAPI ZwDeleteFile(_In_ POBJECT_ATTRIBUTES ObjectAttributes)
_In_ ACCESS_MASK _In_opt_ POBJECT_ATTRIBUTES _In_opt_ PLARGE_INTEGER MaximumSize
Definition: mmfuncs.h:362
_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 ViewSize
Definition: mmfuncs.h:408
#define SEC_NOCACHE
Definition: mmtypes.h:100
#define SEC_COMMIT
Definition: mmtypes.h:99
#define SEC_IMAGE
Definition: mmtypes.h:96
#define SEC_FILE
Definition: mmtypes.h:95
#define SEC_LARGE_PAGES
Definition: mmtypes.h:102
NTSYSAPI NTSTATUS NTAPI ZwClose(_In_ HANDLE Handle)
#define SECTION_MAP_EXECUTE
Definition: nt_native.h:1290
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define SYNCHRONIZE
Definition: nt_native.h:61
#define PAGE_WRITECOPY
Definition: nt_native.h:1305
#define SECTION_MAP_WRITE
Definition: nt_native.h:1288
#define PAGE_READWRITE
Definition: nt_native.h:1304
#define PAGE_EXECUTE_READ
Definition: nt_native.h:1307
#define SECTION_ALL_ACCESS
Definition: nt_native.h:1293
#define PAGE_EXECUTE
Definition: nt_native.h:1306
#define SEC_RESERVE
Definition: nt_native.h:1323
#define FILE_CREATED
Definition: nt_native.h:770
@ ViewUnmap
Definition: nt_native.h:1279
#define STANDARD_RIGHTS_ALL
Definition: nt_native.h:69
#define PAGE_EXECUTE_WRITECOPY
Definition: nt_native.h:1309
#define GENERIC_WRITE
Definition: nt_native.h:90
#define GENERIC_EXECUTE
Definition: nt_native.h:91
#define PAGE_EXECUTE_READWRITE
Definition: nt_native.h:1308
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
PVOID *typedef PHANDLE
Definition: ntsecpkg.h:455
#define STATUS_SECTION_PROTECTION
Definition: ntstatus.h:314
#define STATUS_OBJECT_PATH_SYNTAX_BAD
Definition: ntstatus.h:295
#define STATUS_INVALID_PARAMETER_4
Definition: ntstatus.h:478
#define STATUS_INVALID_IMAGE_NOT_MZ
Definition: ntstatus.h:539
#define STATUS_PENDING
Definition: ntstatus.h:82
#define STATUS_INVALID_FILE_FOR_SECTION
Definition: ntstatus.h:269
#define STATUS_INVALID_PARAMETER_6
Definition: ntstatus.h:480
#define STATUS_INVALID_PAGE_PROTECTION
Definition: ntstatus.h:305
#define STATUS_SECTION_TOO_BIG
Definition: ntstatus.h:300
#define L(x)
Definition: ntvdm.h:50
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
#define FileStandardInformation
Definition: propsheet.cpp:61
#define SharedUserData
#define STATUS_SUCCESS
Definition: shellext.h:65
EH_STD::basic_string< char, EH_STD::char_traits< char >, eh_allocator(char) > TestString
Definition: test_string.cpp:30
#define RTL_CONSTANT_STRING(s)
Definition: tunneltest.c:14
ULONG_PTR SIZE_T
Definition: typedefs.h:80
LONGLONG QuadPart
Definition: typedefs.h:114
_In_ WDFREQUEST _In_ WDFFILEOBJECT FileObject
Definition: wdfdevice.h:550
* PFILE_OBJECT
Definition: iotypes.h:1998
#define ObDereferenceObject
Definition: obfuncs.h:203
#define ZwCurrentProcess()