1 /*
2  * PROJECT:         ReactOS kernel-mode tests
3  * LICENSE:         GPLv2+ - See COPYING in the top level directory
4  * PURPOSE:         Kernel-Mode Test Suite NtCreateSection test user-mode part
5  * PROGRAMMER:      Pierre Schweitzer <pierre@reactos.org>
6  */
7 
8 #include <kmt_test.h>
9 
10 START_TEST(NtCreateSection)
11 {
12     PVOID Buffer;
13     SIZE_T FileSize;
14     NTSTATUS Status;
15     LARGE_INTEGER MaxFileSize;
16     HANDLE Handle, SectionHandle;
17     IO_STATUS_BLOCK IoStatusBlock;
18     OBJECT_ATTRIBUTES ObjectAttributes;
19     UNICODE_STRING InitOnCreate = RTL_CONSTANT_STRING(L"\\Device\\Kmtest-NtCreateSection\\InitOnCreate");
20     UNICODE_STRING InitOnRW = RTL_CONSTANT_STRING(L"\\Device\\Kmtest-NtCreateSection\\InitOnRW");
21     UNICODE_STRING InvalidInit = RTL_CONSTANT_STRING(L"\\Device\\Kmtest-NtCreateSection\\InvalidInit");
22 
23     KmtLoadDriver(L"NtCreateSection", FALSE);
24     KmtOpenDriver();
25 
26     /* Test 0 */
27     InitializeObjectAttributes(&ObjectAttributes, &InvalidInit, OBJ_CASE_INSENSITIVE, NULL, NULL);
28     Status = NtCreateFile(&Handle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock,
29                           NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_CREATE, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
30     ok_eq_hex(Status, STATUS_SUCCESS);
31 
32     MaxFileSize.QuadPart = 512;
33     Status = NtCreateSection(&SectionHandle, SECTION_ALL_ACCESS, 0, &MaxFileSize,
34                              PAGE_READWRITE, SEC_COMMIT, Handle);
35     ok_eq_hex(Status, STATUS_INVALID_FILE_FOR_SECTION);
36     if (NT_SUCCESS(Status)) NtClose(SectionHandle);
37     Status = NtClose(Handle);
38     ok_eq_hex(Status, STATUS_SUCCESS);
39 
40     /* Test 1 */
41     InitializeObjectAttributes(&ObjectAttributes, &InitOnCreate, OBJ_CASE_INSENSITIVE, NULL, NULL);
42     Status = NtCreateFile(&Handle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock,
43                           NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_CREATE, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
44     ok_eq_hex(Status, STATUS_SUCCESS);
45 
46     MaxFileSize.QuadPart = 512;
47     Status = NtCreateSection(&SectionHandle, SECTION_ALL_ACCESS, 0, &MaxFileSize,
48                              PAGE_READWRITE, SEC_COMMIT, Handle);
49     ok_eq_hex(Status, STATUS_SUCCESS);
50 
51     Buffer = NULL;
52     FileSize = 0;
53     Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &Buffer, 0, 0, 0,
54                                 &FileSize, ViewUnmap, 0, PAGE_READWRITE);
55     ok_eq_hex(Status, STATUS_SUCCESS);
56 
57     KmtStartSeh();
58     ok(((PCHAR)Buffer)[0] == 0, "First byte is not null! %x\n", ((PCHAR)Buffer)[0]);
59     memset(Buffer, 0xBA, 512);
60     KmtEndSeh(STATUS_SUCCESS);
61 
62     Status = NtUnmapViewOfSection(NtCurrentProcess(), Buffer);
63     ok_eq_hex(Status, STATUS_SUCCESS);
64     Status = NtClose(SectionHandle);
65     ok_eq_hex(Status, STATUS_SUCCESS);
66     Status = NtClose(Handle);
67     ok_eq_hex(Status, STATUS_SUCCESS);
68 
69     /* Test 2 */
70     InitializeObjectAttributes(&ObjectAttributes, &InitOnCreate, OBJ_CASE_INSENSITIVE, NULL, NULL);
71     Status = NtCreateFile(&Handle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock,
72                           NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_CREATE, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
73     ok_eq_hex(Status, STATUS_SUCCESS);
74 
75     MaxFileSize.QuadPart = 4096;
76     Status = NtCreateSection(&SectionHandle, SECTION_ALL_ACCESS, 0, &MaxFileSize,
77                              PAGE_READWRITE, SEC_COMMIT, Handle);
78     ok_eq_hex(Status, STATUS_SUCCESS);
79 
80     Buffer = NULL;
81     FileSize = 0;
82     Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &Buffer, 0, 0, 0,
83                                 &FileSize, ViewUnmap, 0, PAGE_READWRITE);
84     ok_eq_hex(Status, STATUS_SUCCESS);
85 
86     KmtStartSeh();
87     ok(((PCHAR)Buffer)[0] == 0, "First byte is not null! %x\n", ((PCHAR)Buffer)[0]);
88     memset(Buffer, 0xBA, 4096);
89     KmtEndSeh(STATUS_SUCCESS);
90 
91     Status = NtUnmapViewOfSection(NtCurrentProcess(), Buffer);
92     ok_eq_hex(Status, STATUS_SUCCESS);
93     Status = NtClose(SectionHandle);
94     ok_eq_hex(Status, STATUS_SUCCESS);
95     Status = NtClose(Handle);
96     ok_eq_hex(Status, STATUS_SUCCESS);
97 
98     /* Test 3 */
99     InitializeObjectAttributes(&ObjectAttributes, &InitOnRW, OBJ_CASE_INSENSITIVE, NULL, NULL);
100     Status = NtCreateFile(&Handle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock,
101                           NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_CREATE, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
102     ok_eq_hex(Status, STATUS_SUCCESS);
103 
104     MaxFileSize.QuadPart = 512;
105     Status = NtCreateSection(&SectionHandle, SECTION_ALL_ACCESS, 0, &MaxFileSize,
106                              PAGE_READWRITE, SEC_COMMIT, Handle);
107     ok_eq_hex(Status, STATUS_SUCCESS);
108 
109     Buffer = NULL;
110     FileSize = 0;
111     Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &Buffer, 0, 0, 0,
112                                 &FileSize, ViewUnmap, 0, PAGE_READWRITE);
113     ok_eq_hex(Status, STATUS_SUCCESS);
114 
115     KmtStartSeh();
116     ok(((PCHAR)Buffer)[0] == 0, "First byte is not null! %x\n", ((PCHAR)Buffer)[0]);
117     memset(Buffer, 0xBA, 512);
118     KmtEndSeh(STATUS_SUCCESS);
119 
120     Status = NtUnmapViewOfSection(NtCurrentProcess(), Buffer);
121     ok_eq_hex(Status, STATUS_SUCCESS);
122     Status = NtClose(SectionHandle);
123     ok_eq_hex(Status, STATUS_SUCCESS);
124     Status = NtClose(Handle);
125     ok_eq_hex(Status, STATUS_SUCCESS);
126 
127     /* Test 4 */
128     InitializeObjectAttributes(&ObjectAttributes, &InitOnRW, OBJ_CASE_INSENSITIVE, NULL, NULL);
129     Status = NtCreateFile(&Handle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock,
130                           NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_CREATE, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
131     ok_eq_hex(Status, STATUS_SUCCESS);
132 
133     MaxFileSize.QuadPart = 4096;
134     Status = NtCreateSection(&SectionHandle, SECTION_ALL_ACCESS, 0, &MaxFileSize,
135                              PAGE_READWRITE, SEC_COMMIT, Handle);
136     ok_eq_hex(Status, STATUS_SUCCESS);
137 
138     Buffer = NULL;
139     FileSize = 0;
140     Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &Buffer, 0, 0, 0,
141                                 &FileSize, ViewUnmap, 0, PAGE_READWRITE);
142     ok_eq_hex(Status, STATUS_SUCCESS);
143 
144     KmtStartSeh();
145     ok(((PCHAR)Buffer)[0] == 0, "First byte is not null! %x\n", ((PCHAR)Buffer)[0]);
146     memset(Buffer, 0xBA, 4096);
147     KmtEndSeh(STATUS_SUCCESS);
148 
149     Status = NtUnmapViewOfSection(NtCurrentProcess(), Buffer);
150     ok_eq_hex(Status, STATUS_SUCCESS);
151     Status = NtClose(SectionHandle);
152     ok_eq_hex(Status, STATUS_SUCCESS);
153     Status = NtClose(Handle);
154     ok_eq_hex(Status, STATUS_SUCCESS);
155 
156     /* Test 10 */
157     InitializeObjectAttributes(&ObjectAttributes, &InvalidInit, OBJ_CASE_INSENSITIVE, NULL, NULL);
158     Status = NtCreateFile(&Handle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock,
159                           NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
160     ok_eq_hex(Status, STATUS_SUCCESS);
161 
162     MaxFileSize.QuadPart = 512;
163     Status = NtCreateSection(&SectionHandle, SECTION_ALL_ACCESS, 0, &MaxFileSize,
164                              PAGE_READWRITE, SEC_COMMIT, Handle);
165     ok_eq_hex(Status, STATUS_INVALID_FILE_FOR_SECTION);
166     if (NT_SUCCESS(Status)) NtClose(SectionHandle);
167     Status = NtClose(Handle);
168     ok_eq_hex(Status, STATUS_SUCCESS);
169 
170     /* Test 11 */
171     InitializeObjectAttributes(&ObjectAttributes, &InitOnCreate, OBJ_CASE_INSENSITIVE, NULL, NULL);
172     Status = NtCreateFile(&Handle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock,
173                           NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
174     ok_eq_hex(Status, STATUS_SUCCESS);
175 
176     MaxFileSize.QuadPart = 512;
177     Status = NtCreateSection(&SectionHandle, SECTION_ALL_ACCESS, 0, &MaxFileSize,
178                              PAGE_READWRITE, SEC_COMMIT, Handle);
179     ok_eq_hex(Status, STATUS_SUCCESS);
180 
181     Buffer = NULL;
182     FileSize = 0;
183     Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &Buffer, 0, 0, 0,
184                                 &FileSize, ViewUnmap, 0, PAGE_READWRITE);
185     ok_eq_hex(Status, STATUS_SUCCESS);
186 
187     KmtStartSeh();
188     ok(((PCHAR)Buffer)[0] == 0, "First byte is not null! %x\n", ((PCHAR)Buffer)[0]);
189     memset(Buffer, 0xBA, 512);
190     KmtEndSeh(STATUS_SUCCESS);
191 
192     Status = NtUnmapViewOfSection(NtCurrentProcess(), Buffer);
193     ok_eq_hex(Status, STATUS_SUCCESS);
194     Status = NtClose(SectionHandle);
195     ok_eq_hex(Status, STATUS_SUCCESS);
196     Status = NtClose(Handle);
197     ok_eq_hex(Status, STATUS_SUCCESS);
198 
199     /* Test 12 */
200     InitializeObjectAttributes(&ObjectAttributes, &InitOnCreate, OBJ_CASE_INSENSITIVE, NULL, NULL);
201     Status = NtCreateFile(&Handle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock,
202                           NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
203     ok_eq_hex(Status, STATUS_SUCCESS);
204 
205     MaxFileSize.QuadPart = 4096;
206     Status = NtCreateSection(&SectionHandle, SECTION_ALL_ACCESS, 0, &MaxFileSize,
207                              PAGE_READWRITE, SEC_COMMIT, Handle);
208     ok_eq_hex(Status, STATUS_SUCCESS);
209 
210     Buffer = NULL;
211     FileSize = 0;
212     Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &Buffer, 0, 0, 0,
213                                 &FileSize, ViewUnmap, 0, PAGE_READWRITE);
214     ok_eq_hex(Status, STATUS_SUCCESS);
215 
216     KmtStartSeh();
217     ok(((PCHAR)Buffer)[0] == 0, "First byte is not null! %x\n", ((PCHAR)Buffer)[0]);
218     memset(Buffer, 0xBA, 4096);
219     KmtEndSeh(STATUS_SUCCESS);
220 
221     Status = NtUnmapViewOfSection(NtCurrentProcess(), Buffer);
222     ok_eq_hex(Status, STATUS_SUCCESS);
223     Status = NtClose(SectionHandle);
224     ok_eq_hex(Status, STATUS_SUCCESS);
225     Status = NtClose(Handle);
226     ok_eq_hex(Status, STATUS_SUCCESS);
227 
228     /* Test 13 */
229     InitializeObjectAttributes(&ObjectAttributes, &InitOnRW, OBJ_CASE_INSENSITIVE, NULL, NULL);
230     Status = NtCreateFile(&Handle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock,
231                           NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
232     ok_eq_hex(Status, STATUS_SUCCESS);
233 
234     MaxFileSize.QuadPart = 512;
235     Status = NtCreateSection(&SectionHandle, SECTION_ALL_ACCESS, 0, &MaxFileSize,
236                              PAGE_READWRITE, SEC_COMMIT, Handle);
237     ok_eq_hex(Status, STATUS_SUCCESS);
238 
239     Buffer = NULL;
240     FileSize = 0;
241     Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &Buffer, 0, 0, 0,
242                                 &FileSize, ViewUnmap, 0, PAGE_READWRITE);
243     ok_eq_hex(Status, STATUS_SUCCESS);
244 
245     KmtStartSeh();
246     ok(((PCHAR)Buffer)[0] == 0, "First byte is not null! %x\n", ((PCHAR)Buffer)[0]);
247     memset(Buffer, 0xBA, 512);
248     KmtEndSeh(STATUS_SUCCESS);
249 
250     Status = NtUnmapViewOfSection(NtCurrentProcess(), Buffer);
251     ok_eq_hex(Status, STATUS_SUCCESS);
252     Status = NtClose(SectionHandle);
253     ok_eq_hex(Status, STATUS_SUCCESS);
254     Status = NtClose(Handle);
255     ok_eq_hex(Status, STATUS_SUCCESS);
256 
257     /* Test 14 */
258     InitializeObjectAttributes(&ObjectAttributes, &InitOnRW, OBJ_CASE_INSENSITIVE, NULL, NULL);
259     Status = NtCreateFile(&Handle, GENERIC_READ | GENERIC_WRITE | SYNCHRONIZE, &ObjectAttributes, &IoStatusBlock,
260                           NULL, FILE_ATTRIBUTE_NORMAL, 0, FILE_OPEN, FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);
261     ok_eq_hex(Status, STATUS_SUCCESS);
262 
263     MaxFileSize.QuadPart = 4096;
264     Status = NtCreateSection(&SectionHandle, SECTION_ALL_ACCESS, 0, &MaxFileSize,
265                              PAGE_READWRITE, SEC_COMMIT, Handle);
266     ok_eq_hex(Status, STATUS_SUCCESS);
267 
268     Buffer = NULL;
269     FileSize = 0;
270     Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &Buffer, 0, 0, 0,
271                                 &FileSize, ViewUnmap, 0, PAGE_READWRITE);
272     ok_eq_hex(Status, STATUS_SUCCESS);
273 
274     KmtStartSeh();
275     ok(((PCHAR)Buffer)[0] == 0, "First byte is not null! %x\n", ((PCHAR)Buffer)[0]);
276     memset(Buffer, 0xBA, 4096);
277     KmtEndSeh(STATUS_SUCCESS);
278 
279     Status = NtUnmapViewOfSection(NtCurrentProcess(), Buffer);
280     ok_eq_hex(Status, STATUS_SUCCESS);
281     Status = NtClose(SectionHandle);
282     ok_eq_hex(Status, STATUS_SUCCESS);
283     Status = NtClose(Handle);
284     ok_eq_hex(Status, STATUS_SUCCESS);
285 
286     KmtCloseDriver();
287     KmtUnloadDriver();
288 }
289