f8afc889fd2e6f6d3153f15516153a48333586aa
[reactos.git] / ntoskrnl / io / iomgr / file.c
1 /*
2 * PROJECT: ReactOS Kernel
3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: ntoskrnl/io/iomgr/file.c
5 * PURPOSE: Functions that deal with managing the FILE_OBJECT itself.
6 * PROGRAMMERS: Alex Ionescu (alex.ionescu@reactos.org)
7 * Gunnar Dalsnes
8 * Eric Kohl
9 * Filip Navara (navaraf@reactos.org)
10 * Pierre Schweitzer
11 */
12
13 /* INCLUDES *****************************************************************/
14
15 #include <ntoskrnl.h>
16 #define NDEBUG
17 #include <debug.h>
18
19 extern ERESOURCE IopSecurityResource;
20
21 /* PRIVATE FUNCTIONS *********************************************************/
22
23 VOID
24 NTAPI
25 IopCheckBackupRestorePrivilege(IN PACCESS_STATE AccessState,
26 IN OUT PULONG CreateOptions,
27 IN KPROCESSOR_MODE PreviousMode,
28 IN ULONG Disposition)
29 {
30 ACCESS_MASK DesiredAccess, ReadAccess, WriteAccess;
31 PRIVILEGE_SET Privileges;
32 BOOLEAN AccessGranted, HaveBackupPriv = FALSE, CheckRestore = FALSE;
33 PAGED_CODE();
34
35 /* Don't do anything if privileges were checked already */
36 if (AccessState->Flags & SE_BACKUP_PRIVILEGES_CHECKED) return;
37
38 /* Check if the file was actually opened for backup purposes */
39 if (*CreateOptions & FILE_OPEN_FOR_BACKUP_INTENT)
40 {
41 /* Set the check flag since were doing it now */
42 AccessState->Flags |= SE_BACKUP_PRIVILEGES_CHECKED;
43
44 /* Set the access masks required */
45 ReadAccess = READ_CONTROL |
46 ACCESS_SYSTEM_SECURITY |
47 FILE_GENERIC_READ |
48 FILE_TRAVERSE;
49 WriteAccess = WRITE_DAC |
50 WRITE_OWNER |
51 ACCESS_SYSTEM_SECURITY |
52 FILE_GENERIC_WRITE |
53 FILE_ADD_FILE |
54 FILE_ADD_SUBDIRECTORY |
55 DELETE;
56 DesiredAccess = AccessState->RemainingDesiredAccess;
57
58 /* Check if desired access was the maximum */
59 if (DesiredAccess & MAXIMUM_ALLOWED)
60 {
61 /* Then add all the access masks required */
62 DesiredAccess |= (ReadAccess | WriteAccess);
63 }
64
65 /* Check if the file already exists */
66 if (Disposition & FILE_OPEN)
67 {
68 /* Check if desired access has the read mask */
69 if (ReadAccess & DesiredAccess)
70 {
71 /* Setup the privilege check lookup */
72 Privileges.PrivilegeCount = 1;
73 Privileges.Control = PRIVILEGE_SET_ALL_NECESSARY;
74 Privileges.Privilege[0].Luid = SeBackupPrivilege;
75 Privileges.Privilege[0].Attributes = 0;
76 AccessGranted = SePrivilegeCheck(&Privileges,
77 &AccessState->
78 SubjectSecurityContext,
79 PreviousMode);
80 if (AccessGranted)
81 {
82 /* Remember that backup was allowed */
83 HaveBackupPriv = TRUE;
84
85 /* Append the privileges and update the access state */
86 SeAppendPrivileges(AccessState, &Privileges);
87 AccessState->PreviouslyGrantedAccess |= (DesiredAccess & ReadAccess);
88 AccessState->RemainingDesiredAccess &= ~ReadAccess;
89 DesiredAccess &= ~ReadAccess;
90
91 /* Set backup privilege for the token */
92 AccessState->Flags |= TOKEN_HAS_BACKUP_PRIVILEGE;
93 }
94 }
95 }
96 else
97 {
98 /* Caller is creating the file, check restore privileges later */
99 CheckRestore = TRUE;
100 }
101
102 /* Check if caller wants write access or if it's creating a file */
103 if ((WriteAccess & DesiredAccess) || (CheckRestore))
104 {
105 /* Setup the privilege lookup and do it */
106 Privileges.PrivilegeCount = 1;
107 Privileges.Control = PRIVILEGE_SET_ALL_NECESSARY;
108 Privileges.Privilege[0].Luid = SeRestorePrivilege;
109 Privileges.Privilege[0].Attributes = 0;
110 AccessGranted = SePrivilegeCheck(&Privileges,
111 &AccessState->SubjectSecurityContext,
112 PreviousMode);
113 if (AccessGranted)
114 {
115 /* Remember that privilege was given */
116 HaveBackupPriv = TRUE;
117
118 /* Append the privileges and update the access state */
119 SeAppendPrivileges(AccessState, &Privileges);
120 AccessState->PreviouslyGrantedAccess |= (DesiredAccess & WriteAccess);
121 AccessState->RemainingDesiredAccess &= ~WriteAccess;
122
123 /* Set restore privilege for the token */
124 AccessState->Flags |= TOKEN_HAS_RESTORE_PRIVILEGE;
125 }
126 }
127
128 /* If we don't have the privilege, remove the option */
129 if (!HaveBackupPriv) *CreateOptions &= ~FILE_OPEN_FOR_BACKUP_INTENT;
130 }
131 }
132
133 NTSTATUS
134 NTAPI
135 IopCheckDeviceAndDriver(IN POPEN_PACKET OpenPacket,
136 IN PDEVICE_OBJECT DeviceObject)
137 {
138 /* Make sure the object is valid */
139 if ((IoGetDevObjExtension(DeviceObject)->ExtensionFlags &
140 (DOE_UNLOAD_PENDING |
141 DOE_DELETE_PENDING |
142 DOE_REMOVE_PENDING |
143 DOE_REMOVE_PROCESSED)) ||
144 (DeviceObject->Flags & DO_DEVICE_INITIALIZING))
145 {
146 /* It's unloading or initializing, so fail */
147 DPRINT1("You are seeing this because the following ROS driver: %wZ\n"
148 " sucks. Please fix it's AddDevice Routine\n",
149 &DeviceObject->DriverObject->DriverName);
150 return STATUS_NO_SUCH_DEVICE;
151 }
152 else if ((DeviceObject->Flags & DO_EXCLUSIVE) &&
153 (DeviceObject->ReferenceCount) &&
154 !(OpenPacket->RelatedFileObject) &&
155 !(OpenPacket->Options & IO_ATTACH_DEVICE))
156 {
157 return STATUS_ACCESS_DENIED;
158 }
159
160 else
161 {
162 /* Increase reference count */
163 InterlockedIncrement(&DeviceObject->ReferenceCount);
164 return STATUS_SUCCESS;
165 }
166 }
167
168 VOID
169 NTAPI
170 IopDoNameTransmogrify(IN PIRP Irp,
171 IN PFILE_OBJECT FileObject,
172 IN PREPARSE_DATA_BUFFER DataBuffer)
173 {
174 PWSTR Buffer;
175 USHORT Length;
176 USHORT RequiredLength;
177 PWSTR NewBuffer;
178
179 PAGED_CODE();
180
181 ASSERT(Irp->IoStatus.Status == STATUS_REPARSE);
182 ASSERT(Irp->IoStatus.Information == IO_REPARSE_TAG_MOUNT_POINT);
183 ASSERT(Irp->Tail.Overlay.AuxiliaryBuffer != NULL);
184 ASSERT(DataBuffer != NULL);
185 ASSERT(DataBuffer->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT);
186 ASSERT(DataBuffer->ReparseDataLength < MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
187 ASSERT(DataBuffer->Reserved < MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
188
189 /* First of all, validate data */
190 if (DataBuffer->ReparseDataLength < REPARSE_DATA_BUFFER_HEADER_SIZE ||
191 (DataBuffer->SymbolicLinkReparseBuffer.PrintNameLength +
192 DataBuffer->SymbolicLinkReparseBuffer.SubstituteNameLength +
193 FIELD_OFFSET(REPARSE_DATA_BUFFER, MountPointReparseBuffer.PathBuffer[0])) > MAXIMUM_REPARSE_DATA_BUFFER_SIZE)
194 {
195 Irp->IoStatus.Status = STATUS_IO_REPARSE_DATA_INVALID;
196 }
197
198 /* Everything went right */
199 if (NT_SUCCESS(Irp->IoStatus.Status))
200 {
201 /* Compute buffer & length */
202 Buffer = (PWSTR)((ULONG_PTR)DataBuffer->MountPointReparseBuffer.PathBuffer +
203 DataBuffer->MountPointReparseBuffer.SubstituteNameOffset);
204 Length = DataBuffer->MountPointReparseBuffer.SubstituteNameLength;
205
206 /* Check we don't overflow */
207 if (((ULONG)MAXUSHORT - DataBuffer->Reserved) <= (Length + sizeof(UNICODE_NULL)))
208 {
209 Irp->IoStatus.Status = STATUS_IO_REPARSE_DATA_INVALID;
210 }
211 else
212 {
213 /* Compute how much mem we'll need */
214 RequiredLength = DataBuffer->Reserved + Length + sizeof(UNICODE_NULL);
215
216 /* Check if FileObject can already hold what we need */
217 if (FileObject->FileName.MaximumLength >= RequiredLength)
218 {
219 NewBuffer = FileObject->FileName.Buffer;
220 }
221 else
222 {
223 /* Allocate otherwise */
224 NewBuffer = ExAllocatePoolWithTag(PagedPool, RequiredLength, TAG_IO_NAME);
225 if (NewBuffer == NULL)
226 {
227 Irp->IoStatus.Status = STATUS_INSUFFICIENT_RESOURCES;
228 }
229 }
230 }
231 }
232
233 /* Everything went right */
234 if (NT_SUCCESS(Irp->IoStatus.Status))
235 {
236 /* Copy reserved */
237 if (DataBuffer->Reserved)
238 {
239 RtlMoveMemory((PWSTR)((ULONG_PTR)NewBuffer + Length),
240 (PWSTR)((ULONG_PTR)FileObject->FileName.Buffer + FileObject->FileName.Length - DataBuffer->Reserved),
241 DataBuffer->Reserved);
242 }
243
244 /* Then, buffer */
245 if (Length)
246 {
247 RtlCopyMemory(NewBuffer, Buffer, Length);
248 }
249
250 /* And finally replace buffer if new one was allocated */
251 FileObject->FileName.Length = RequiredLength - sizeof(UNICODE_NULL);
252 if (NewBuffer != FileObject->FileName.Buffer)
253 {
254 if (FileObject->FileName.Buffer)
255 {
256 ExFreePoolWithTag(FileObject->FileName.Buffer, TAG_IO_NAME);
257 }
258
259 FileObject->FileName.Buffer = NewBuffer;
260 FileObject->FileName.MaximumLength = RequiredLength;
261 FileObject->FileName.Buffer[RequiredLength / sizeof(WCHAR) - 1] = UNICODE_NULL;
262 }
263 }
264
265 /* We don't need them anymore - it was allocated by the driver */
266 ExFreePool(DataBuffer);
267 }
268
269 NTSTATUS
270 NTAPI
271 IopParseDevice(IN PVOID ParseObject,
272 IN PVOID ObjectType,
273 IN OUT PACCESS_STATE AccessState,
274 IN KPROCESSOR_MODE AccessMode,
275 IN ULONG Attributes,
276 IN OUT PUNICODE_STRING CompleteName,
277 IN OUT PUNICODE_STRING RemainingName,
278 IN OUT PVOID Context,
279 IN PSECURITY_QUALITY_OF_SERVICE SecurityQos OPTIONAL,
280 OUT PVOID *Object)
281 {
282 POPEN_PACKET OpenPacket = (POPEN_PACKET)Context;
283 PDEVICE_OBJECT OriginalDeviceObject = (PDEVICE_OBJECT)ParseObject;
284 PDEVICE_OBJECT DeviceObject, OwnerDevice;
285 NTSTATUS Status;
286 PFILE_OBJECT FileObject;
287 PVPB Vpb = NULL;
288 PIRP Irp;
289 PIO_STACK_LOCATION StackLoc;
290 IO_SECURITY_CONTEXT SecurityContext;
291 IO_STATUS_BLOCK IoStatusBlock;
292 BOOLEAN DirectOpen = FALSE, OpenCancelled, UseDummyFile;
293 OBJECT_ATTRIBUTES ObjectAttributes;
294 KIRQL OldIrql;
295 PDUMMY_FILE_OBJECT LocalFileObject;
296 PFILE_BASIC_INFORMATION FileBasicInfo;
297 ULONG ReturnLength;
298 KPROCESSOR_MODE CheckMode;
299 BOOLEAN VolumeOpen = FALSE;
300 ACCESS_MASK DesiredAccess, GrantedAccess;
301 BOOLEAN AccessGranted, LockHeld = FALSE;
302 PPRIVILEGE_SET Privileges = NULL;
303 UNICODE_STRING FileString;
304 USHORT Attempt;
305 IOTRACE(IO_FILE_DEBUG, "ParseObject: %p. RemainingName: %wZ\n",
306 ParseObject, RemainingName);
307
308 for (Attempt = 0; Attempt < IOP_MAX_REPARSE_TRAVERSAL; ++Attempt)
309 {
310 /* Assume failure */
311 *Object = NULL;
312
313 /* Validate the open packet */
314 if (!IopValidateOpenPacket(OpenPacket)) return STATUS_OBJECT_TYPE_MISMATCH;
315
316 /* Valide reparse point in case we traversed a mountpoint */
317 if (OpenPacket->TraversedMountPoint)
318 {
319 /* This is a reparse point we understand */
320 ASSERT(OpenPacket->Information == IO_REPARSE_TAG_MOUNT_POINT);
321
322 /* Make sure we're dealing with correct DO */
323 if (OriginalDeviceObject->DeviceType != FILE_DEVICE_DISK &&
324 OriginalDeviceObject->DeviceType != FILE_DEVICE_CD_ROM &&
325 OriginalDeviceObject->DeviceType != FILE_DEVICE_VIRTUAL_DISK &&
326 OriginalDeviceObject->DeviceType != FILE_DEVICE_TAPE)
327 {
328 OpenPacket->FinalStatus = STATUS_IO_REPARSE_DATA_INVALID;
329 return STATUS_IO_REPARSE_DATA_INVALID;
330 }
331 }
332
333 /* Check if we have a related file object */
334 if (OpenPacket->RelatedFileObject)
335 {
336 /* Use the related file object's device object */
337 OriginalDeviceObject = OpenPacket->RelatedFileObject->DeviceObject;
338 }
339
340 /* Validate device status */
341 Status = IopCheckDeviceAndDriver(OpenPacket, OriginalDeviceObject);
342 if (!NT_SUCCESS(Status))
343 {
344 /* We failed, return status */
345 OpenPacket->FinalStatus = Status;
346 return Status;
347 }
348
349 /* Map the generic mask and set the new mapping in the access state */
350 RtlMapGenericMask(&AccessState->RemainingDesiredAccess,
351 &IoFileObjectType->TypeInfo.GenericMapping);
352 RtlMapGenericMask(&AccessState->OriginalDesiredAccess,
353 &IoFileObjectType->TypeInfo.GenericMapping);
354 SeSetAccessStateGenericMapping(AccessState,
355 &IoFileObjectType->TypeInfo.GenericMapping);
356 DesiredAccess = AccessState->RemainingDesiredAccess;
357
358 /* Check what kind of access checks to do */
359 if ((AccessMode != KernelMode) ||
360 (OpenPacket->Options & IO_FORCE_ACCESS_CHECK))
361 {
362 /* Call is from user-mode or kernel is forcing checks */
363 CheckMode = UserMode;
364 }
365 else
366 {
367 /* Call is from the kernel */
368 CheckMode = KernelMode;
369 }
370
371 /* Check privilege for backup or restore operation */
372 IopCheckBackupRestorePrivilege(AccessState,
373 &OpenPacket->CreateOptions,
374 CheckMode,
375 OpenPacket->Disposition);
376
377 /* Check if we are re-parsing */
378 if (((OpenPacket->Override) && !(RemainingName->Length)) ||
379 (AccessState->Flags & SE_BACKUP_PRIVILEGES_CHECKED))
380 {
381 /* Get granted access from the last call */
382 DesiredAccess |= AccessState->PreviouslyGrantedAccess;
383 }
384
385 /* Check if this is a volume open */
386 if ((OpenPacket->RelatedFileObject) &&
387 (OpenPacket->RelatedFileObject->Flags & FO_VOLUME_OPEN) &&
388 !(RemainingName->Length))
389 {
390 /* It is */
391 VolumeOpen = TRUE;
392 }
393
394 /* Now check if we need access checks */
395 if (((AccessMode != KernelMode) ||
396 (OpenPacket->Options & IO_FORCE_ACCESS_CHECK)) &&
397 (!(OpenPacket->RelatedFileObject) || (VolumeOpen)) &&
398 !(OpenPacket->Override))
399 {
400 KeEnterCriticalRegion();
401 ExAcquireResourceSharedLite(&IopSecurityResource, TRUE);
402
403 /* Check if a device object is being parsed */
404 if (!RemainingName->Length)
405 {
406 /* Lock the subject context */
407 SeLockSubjectContext(&AccessState->SubjectSecurityContext);
408 LockHeld = TRUE;
409
410 /* Do access check */
411 AccessGranted = SeAccessCheck(OriginalDeviceObject->
412 SecurityDescriptor,
413 &AccessState->SubjectSecurityContext,
414 LockHeld,
415 DesiredAccess,
416 0,
417 &Privileges,
418 &IoFileObjectType->
419 TypeInfo.GenericMapping,
420 UserMode,
421 &GrantedAccess,
422 &Status);
423 if (Privileges)
424 {
425 /* Append and free the privileges */
426 SeAppendPrivileges(AccessState, Privileges);
427 SeFreePrivileges(Privileges);
428 }
429
430 /* Check if we got access */
431 if (AccessGranted)
432 {
433 /* Update access state */
434 AccessState->PreviouslyGrantedAccess |= GrantedAccess;
435 AccessState->RemainingDesiredAccess &= ~(GrantedAccess |
436 MAXIMUM_ALLOWED);
437 OpenPacket->Override= TRUE;
438 }
439
440 FileString.Length = 8;
441 FileString.MaximumLength = 8;
442 FileString.Buffer = L"File";
443
444 /* Do Audit/Alarm for open operation */
445 SeOpenObjectAuditAlarm(&FileString,
446 OriginalDeviceObject,
447 CompleteName,
448 OriginalDeviceObject->SecurityDescriptor,
449 AccessState,
450 FALSE,
451 AccessGranted,
452 UserMode,
453 &AccessState->GenerateOnClose);
454 }
455 else
456 {
457 /* Check if we need to do traverse validation */
458 if (!(AccessState->Flags & TOKEN_HAS_TRAVERSE_PRIVILEGE) ||
459 ((OriginalDeviceObject->DeviceType == FILE_DEVICE_DISK) ||
460 (OriginalDeviceObject->DeviceType == FILE_DEVICE_CD_ROM)))
461 {
462 /* Check if this is a restricted token */
463 if (!(AccessState->Flags & TOKEN_IS_RESTRICTED))
464 {
465 /* Do the FAST traverse check */
466 AccessGranted = SeFastTraverseCheck(OriginalDeviceObject->SecurityDescriptor,
467 AccessState,
468 FILE_TRAVERSE,
469 UserMode);
470 }
471 else
472 {
473 /* Fail */
474 AccessGranted = FALSE;
475 }
476
477 /* Check if we failed to get access */
478 if (!AccessGranted)
479 {
480 /* Lock the subject context */
481 SeLockSubjectContext(&AccessState->SubjectSecurityContext);
482 LockHeld = TRUE;
483
484 /* Do access check */
485 AccessGranted = SeAccessCheck(OriginalDeviceObject->
486 SecurityDescriptor,
487 &AccessState->SubjectSecurityContext,
488 LockHeld,
489 FILE_TRAVERSE,
490 0,
491 &Privileges,
492 &IoFileObjectType->
493 TypeInfo.GenericMapping,
494 UserMode,
495 &GrantedAccess,
496 &Status);
497 if (Privileges)
498 {
499 /* Append and free the privileges */
500 SeAppendPrivileges(AccessState, Privileges);
501 SeFreePrivileges(Privileges);
502 }
503 }
504
505 /* FIXME: Do Audit/Alarm for traverse check */
506 }
507 else
508 {
509 /* Access automatically granted */
510 AccessGranted = TRUE;
511 }
512 }
513
514 ExReleaseResourceLite(&IopSecurityResource);
515 KeLeaveCriticalRegion();
516
517 /* Check if we hold the lock */
518 if (LockHeld)
519 {
520 /* Release it */
521 SeUnlockSubjectContext(&AccessState->SubjectSecurityContext);
522 }
523
524 /* Check if access failed */
525 if (!AccessGranted)
526 {
527 /* Dereference the device and fail */
528 DPRINT1("Traverse access failed!\n");
529 IopDereferenceDeviceObject(OriginalDeviceObject, FALSE);
530 return STATUS_ACCESS_DENIED;
531 }
532 }
533
534 /* Check if we can simply use a dummy file */
535 UseDummyFile = ((OpenPacket->QueryOnly) || (OpenPacket->DeleteOnly));
536
537 #if 1
538 /* FIXME: Small hack still exists, have to check why...
539 * This is triggered multiple times by usetup and then once per boot.
540 */
541 if (ExpInTextModeSetup &&
542 !(DirectOpen) &&
543 !(RemainingName->Length) &&
544 !(OpenPacket->RelatedFileObject) &&
545 ((wcsstr(CompleteName->Buffer, L"Harddisk")) ||
546 (wcsstr(CompleteName->Buffer, L"Floppy"))) &&
547 !(UseDummyFile))
548 {
549 DPRINT1("Using IopParseDevice() hack. Requested invalid attributes: %lx\n",
550 DesiredAccess & ~(SYNCHRONIZE |
551 FILE_READ_ATTRIBUTES |
552 READ_CONTROL |
553 ACCESS_SYSTEM_SECURITY |
554 WRITE_OWNER |
555 WRITE_DAC));
556 DirectOpen = TRUE;
557 }
558 #endif
559
560 /* Check if this is a direct open */
561 if (!(RemainingName->Length) &&
562 !(OpenPacket->RelatedFileObject) &&
563 ((DesiredAccess & ~(SYNCHRONIZE |
564 FILE_READ_ATTRIBUTES |
565 READ_CONTROL |
566 ACCESS_SYSTEM_SECURITY |
567 WRITE_OWNER |
568 WRITE_DAC)) == 0) &&
569 !(UseDummyFile))
570 {
571 /* Remember this for later */
572 DirectOpen = TRUE;
573 }
574
575 /* Check if we have a related FO that wasn't a direct open */
576 if ((OpenPacket->RelatedFileObject) &&
577 !(OpenPacket->RelatedFileObject->Flags & FO_DIRECT_DEVICE_OPEN))
578 {
579 /* The device object is the one we were given */
580 DeviceObject = ParseObject;
581
582 /* Check if the related FO had a VPB */
583 if (OpenPacket->RelatedFileObject->Vpb)
584 {
585 /* Yes, remember it */
586 Vpb = OpenPacket->RelatedFileObject->Vpb;
587
588 /* Reference it */
589 InterlockedIncrement((PLONG)&Vpb->ReferenceCount);
590
591 /* Check if we were given a specific top level device to use */
592 if (OpenPacket->InternalFlags & IOP_USE_TOP_LEVEL_DEVICE_HINT)
593 {
594 DeviceObject = Vpb->DeviceObject;
595 }
596 }
597 }
598 else
599 {
600 /* Check if it has a VPB */
601 if ((OriginalDeviceObject->Vpb) && !(DirectOpen))
602 {
603 /* Check if the VPB is mounted, and mount it */
604 Vpb = IopCheckVpbMounted(OpenPacket,
605 OriginalDeviceObject,
606 RemainingName,
607 &Status);
608 if (!Vpb) return Status;
609
610 /* Get the VPB's device object */
611 DeviceObject = Vpb->DeviceObject;
612 }
613 else
614 {
615 /* The device object is the one we were given */
616 DeviceObject = OriginalDeviceObject;
617 }
618
619 /* If we weren't given a specific top level device, look for an attached device */
620 if (!(OpenPacket->InternalFlags & IOP_USE_TOP_LEVEL_DEVICE_HINT) &&
621 DeviceObject->AttachedDevice)
622 {
623 /* Get the attached device */
624 DeviceObject = IoGetAttachedDevice(DeviceObject);
625 }
626 }
627
628 if (OpenPacket->InternalFlags & IOP_USE_TOP_LEVEL_DEVICE_HINT)
629 {
630 // FIXME: Verify our device object is good to use
631 ASSERT(DirectOpen == FALSE);
632 }
633
634 /* If we traversed a mount point, reset the information */
635 if (OpenPacket->TraversedMountPoint)
636 {
637 OpenPacket->TraversedMountPoint = FALSE;
638 }
639
640 /* Check if this is a secure FSD */
641 if ((DeviceObject->Characteristics & FILE_DEVICE_SECURE_OPEN) &&
642 ((OpenPacket->RelatedFileObject) || (RemainingName->Length)) &&
643 (!VolumeOpen))
644 {
645 Privileges = NULL;
646 GrantedAccess = 0;
647
648 KeEnterCriticalRegion();
649 ExAcquireResourceSharedLite(&IopSecurityResource, TRUE);
650
651 /* Lock the subject context */
652 SeLockSubjectContext(&AccessState->SubjectSecurityContext);
653
654 /* Do access check */
655 AccessGranted = SeAccessCheck(OriginalDeviceObject->SecurityDescriptor,
656 &AccessState->SubjectSecurityContext,
657 TRUE,
658 DesiredAccess,
659 0,
660 &Privileges,
661 &IoFileObjectType->TypeInfo.GenericMapping,
662 UserMode,
663 &GrantedAccess,
664 &Status);
665 if (Privileges != NULL)
666 {
667 /* Append and free the privileges */
668 SeAppendPrivileges(AccessState, Privileges);
669 SeFreePrivileges(Privileges);
670 }
671
672 /* Check if we got access */
673 if (GrantedAccess)
674 {
675 AccessState->PreviouslyGrantedAccess |= GrantedAccess;
676 AccessState->RemainingDesiredAccess &= ~(GrantedAccess | MAXIMUM_ALLOWED);
677 }
678
679 FileString.Length = 8;
680 FileString.MaximumLength = 8;
681 FileString.Buffer = L"File";
682
683 /* Do Audit/Alarm for open operation
684 * NOTA: we audit target device object
685 */
686 SeOpenObjectAuditAlarm(&FileString,
687 DeviceObject,
688 CompleteName,
689 OriginalDeviceObject->SecurityDescriptor,
690 AccessState,
691 FALSE,
692 AccessGranted,
693 UserMode,
694 &AccessState->GenerateOnClose);
695
696 SeUnlockSubjectContext(&AccessState->SubjectSecurityContext);
697
698 ExReleaseResourceLite(&IopSecurityResource);
699 KeLeaveCriticalRegion();
700
701 /* Check if access failed */
702 if (!AccessGranted)
703 {
704 /* Dereference the device and fail */
705 IopDereferenceDeviceObject(OriginalDeviceObject, FALSE);
706 if (Vpb) IopDereferenceVpbAndFree(Vpb);
707 return STATUS_ACCESS_DENIED;
708 }
709 }
710
711 /* Allocate the IRP */
712 Irp = IoAllocateIrp(DeviceObject->StackSize, TRUE);
713 if (!Irp)
714 {
715 /* Dereference the device and VPB, then fail */
716 IopDereferenceDeviceObject(OriginalDeviceObject, FALSE);
717 if (Vpb) IopDereferenceVpbAndFree(Vpb);
718 return STATUS_INSUFFICIENT_RESOURCES;
719 }
720
721 /* Now set the IRP data */
722 Irp->RequestorMode = AccessMode;
723 Irp->Flags = IRP_CREATE_OPERATION | IRP_SYNCHRONOUS_API | IRP_DEFER_IO_COMPLETION;
724 Irp->Tail.Overlay.Thread = PsGetCurrentThread();
725 Irp->UserIosb = &IoStatusBlock;
726 Irp->MdlAddress = NULL;
727 Irp->PendingReturned = FALSE;
728 Irp->UserEvent = NULL;
729 Irp->Cancel = FALSE;
730 Irp->CancelRoutine = NULL;
731 Irp->Tail.Overlay.AuxiliaryBuffer = NULL;
732
733 /* Setup the security context */
734 SecurityContext.SecurityQos = SecurityQos;
735 SecurityContext.AccessState = AccessState;
736 SecurityContext.DesiredAccess = AccessState->RemainingDesiredAccess;
737 SecurityContext.FullCreateOptions = OpenPacket->CreateOptions;
738
739 /* Get the I/O Stack location */
740 StackLoc = IoGetNextIrpStackLocation(Irp);
741 StackLoc->Control = 0;
742
743 /* Check what kind of file this is */
744 switch (OpenPacket->CreateFileType)
745 {
746 /* Normal file */
747 case CreateFileTypeNone:
748
749 /* Set the major function and EA Length */
750 StackLoc->MajorFunction = IRP_MJ_CREATE;
751 StackLoc->Parameters.Create.EaLength = OpenPacket->EaLength;
752
753 /* Set the flags */
754 StackLoc->Flags = (UCHAR)OpenPacket->Options;
755 StackLoc->Flags |= !(Attributes & OBJ_CASE_INSENSITIVE) ? SL_CASE_SENSITIVE: 0;
756 break;
757
758 /* Named pipe */
759 case CreateFileTypeNamedPipe:
760
761 /* Set the named pipe MJ and set the parameters */
762 StackLoc->MajorFunction = IRP_MJ_CREATE_NAMED_PIPE;
763 StackLoc->Parameters.CreatePipe.Parameters = OpenPacket->ExtraCreateParameters;
764 break;
765
766 /* Mailslot */
767 case CreateFileTypeMailslot:
768
769 /* Set the mailslot MJ and set the parameters */
770 StackLoc->MajorFunction = IRP_MJ_CREATE_MAILSLOT;
771 StackLoc->Parameters.CreateMailslot.Parameters = OpenPacket->ExtraCreateParameters;
772 break;
773 }
774
775 /* Set the common data */
776 Irp->Overlay.AllocationSize = OpenPacket->AllocationSize;
777 Irp->AssociatedIrp.SystemBuffer = OpenPacket->EaBuffer;
778 StackLoc->Parameters.Create.Options = (OpenPacket->Disposition << 24) |
779 (OpenPacket->CreateOptions &
780 0xFFFFFF);
781 StackLoc->Parameters.Create.FileAttributes = OpenPacket->FileAttributes;
782 StackLoc->Parameters.Create.ShareAccess = OpenPacket->ShareAccess;
783 StackLoc->Parameters.Create.SecurityContext = &SecurityContext;
784
785 /* Check if we really need to create an object */
786 if (!UseDummyFile)
787 {
788 ULONG ObjectSize = sizeof(FILE_OBJECT);
789
790 /* Tag on space for a file object extension */
791 if (OpenPacket->InternalFlags & IOP_CREATE_FILE_OBJECT_EXTENSION)
792 ObjectSize += sizeof(FILE_OBJECT_EXTENSION);
793
794 /* Create the actual file object */
795 InitializeObjectAttributes(&ObjectAttributes,
796 NULL,
797 Attributes,
798 NULL,
799 NULL);
800 Status = ObCreateObject(KernelMode,
801 IoFileObjectType,
802 &ObjectAttributes,
803 AccessMode,
804 NULL,
805 ObjectSize,
806 0,
807 0,
808 (PVOID*)&FileObject);
809 if (!NT_SUCCESS(Status))
810 {
811 /* Create failed, free the IRP */
812 IoFreeIrp(Irp);
813
814 /* Dereference the device and VPB */
815 IopDereferenceDeviceObject(OriginalDeviceObject, FALSE);
816 if (Vpb) IopDereferenceVpbAndFree(Vpb);
817
818 /* We failed, return status */
819 OpenPacket->FinalStatus = Status;
820 return Status;
821 }
822
823 /* Clear the file object */
824 RtlZeroMemory(FileObject, sizeof(FILE_OBJECT));
825
826 /* Check if this is Synch I/O */
827 if (OpenPacket->CreateOptions &
828 (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT))
829 {
830 /* Set the synch flag */
831 FileObject->Flags |= FO_SYNCHRONOUS_IO;
832
833 /* Check if it's also alertable */
834 if (OpenPacket->CreateOptions & FILE_SYNCHRONOUS_IO_ALERT)
835 {
836 /* It is, set the alertable flag */
837 FileObject->Flags |= FO_ALERTABLE_IO;
838 }
839 }
840
841 /* Check if this is synch I/O */
842 if (FileObject->Flags & FO_SYNCHRONOUS_IO)
843 {
844 /* Initialize the event */
845 KeInitializeEvent(&FileObject->Lock, SynchronizationEvent, FALSE);
846 }
847
848 /* Check if the caller requested no intermediate buffering */
849 if (OpenPacket->CreateOptions & FILE_NO_INTERMEDIATE_BUFFERING)
850 {
851 /* Set the correct flag for the FSD to read */
852 FileObject->Flags |= FO_NO_INTERMEDIATE_BUFFERING;
853 }
854
855 /* Check if the caller requested write through support */
856 if (OpenPacket->CreateOptions & FILE_WRITE_THROUGH)
857 {
858 /* Set the correct flag for the FSD to read */
859 FileObject->Flags |= FO_WRITE_THROUGH;
860 }
861
862 /* Check if the caller says the file will be only read sequentially */
863 if (OpenPacket->CreateOptions & FILE_SEQUENTIAL_ONLY)
864 {
865 /* Set the correct flag for the FSD to read */
866 FileObject->Flags |= FO_SEQUENTIAL_ONLY;
867 }
868
869 /* Check if the caller believes the file will be only read randomly */
870 if (OpenPacket->CreateOptions & FILE_RANDOM_ACCESS)
871 {
872 /* Set the correct flag for the FSD to read */
873 FileObject->Flags |= FO_RANDOM_ACCESS;
874 }
875
876 /* Check if we were asked to setup a file object extension */
877 if (OpenPacket->InternalFlags & IOP_CREATE_FILE_OBJECT_EXTENSION)
878 {
879 PFILE_OBJECT_EXTENSION FileObjectExtension;
880
881 /* Make sure the file object knows it has an extension */
882 FileObject->Flags |= FO_FILE_OBJECT_HAS_EXTENSION;
883
884 FileObjectExtension = (PFILE_OBJECT_EXTENSION)(FileObject + 1);
885 FileObject->FileObjectExtension = FileObjectExtension;
886
887 /* Add the top level device which we'll send the request to */
888 if (OpenPacket->InternalFlags & IOP_USE_TOP_LEVEL_DEVICE_HINT)
889 {
890 FileObjectExtension->TopDeviceObjectHint = DeviceObject;
891 }
892 }
893 }
894 else
895 {
896 /* Use the dummy object instead */
897 LocalFileObject = OpenPacket->LocalFileObject;
898 RtlZeroMemory(LocalFileObject, sizeof(DUMMY_FILE_OBJECT));
899
900 /* Set it up */
901 FileObject = (PFILE_OBJECT)&LocalFileObject->ObjectHeader.Body;
902 LocalFileObject->ObjectHeader.Type = IoFileObjectType;
903 LocalFileObject->ObjectHeader.PointerCount = 1;
904 }
905
906 /* Setup the file header */
907 FileObject->Type = IO_TYPE_FILE;
908 FileObject->Size = sizeof(FILE_OBJECT);
909 FileObject->RelatedFileObject = OpenPacket->RelatedFileObject;
910 FileObject->DeviceObject = OriginalDeviceObject;
911
912 /* Check if this is a direct device open */
913 if (DirectOpen) FileObject->Flags |= FO_DIRECT_DEVICE_OPEN;
914
915 /* Check if the caller wants case sensitivity */
916 if (!(Attributes & OBJ_CASE_INSENSITIVE))
917 {
918 /* Tell the driver about it */
919 FileObject->Flags |= FO_OPENED_CASE_SENSITIVE;
920 }
921
922 /* Now set the file object */
923 Irp->Tail.Overlay.OriginalFileObject = FileObject;
924 StackLoc->FileObject = FileObject;
925
926 /* Check if the file object has a name */
927 if (RemainingName->Length)
928 {
929 /* Setup the unicode string */
930 FileObject->FileName.MaximumLength = RemainingName->Length +
931 sizeof(WCHAR);
932 FileObject->FileName.Buffer = ExAllocatePoolWithTag(PagedPool,
933 FileObject->
934 FileName.
935 MaximumLength,
936 TAG_IO_NAME);
937 if (!FileObject->FileName.Buffer)
938 {
939 /* Failed to allocate the name, free the IRP */
940 IoFreeIrp(Irp);
941
942 /* Dereference the device object and VPB */
943 IopDereferenceDeviceObject(OriginalDeviceObject, FALSE);
944 if (Vpb) IopDereferenceVpbAndFree(Vpb);
945
946 /* Clear the FO and dereference it */
947 FileObject->DeviceObject = NULL;
948 if (!UseDummyFile) ObDereferenceObject(FileObject);
949
950 /* Fail */
951 return STATUS_INSUFFICIENT_RESOURCES;
952 }
953 }
954
955 /* Copy the name */
956 RtlCopyUnicodeString(&FileObject->FileName, RemainingName);
957
958 /* Initialize the File Object event and set the FO */
959 KeInitializeEvent(&FileObject->Event, NotificationEvent, FALSE);
960 OpenPacket->FileObject = FileObject;
961
962 /* Queue the IRP and call the driver */
963 IopQueueIrpToThread(Irp);
964 Status = IoCallDriver(DeviceObject, Irp);
965 if (Status == STATUS_PENDING)
966 {
967 /* Wait for the driver to complete the create */
968 KeWaitForSingleObject(&FileObject->Event,
969 Executive,
970 KernelMode,
971 FALSE,
972 NULL);
973
974 /* Get the new status */
975 Status = IoStatusBlock.Status;
976 }
977 else
978 {
979 /* We'll have to complete it ourselves */
980 ASSERT(!Irp->PendingReturned);
981 ASSERT(!Irp->MdlAddress);
982
983 /* Handle name change if required */
984 if (Status == STATUS_REPARSE)
985 {
986 /* Check this is a mount point */
987 if (Irp->IoStatus.Information == IO_REPARSE_TAG_MOUNT_POINT)
988 {
989 PREPARSE_DATA_BUFFER ReparseData;
990
991 /* Reparse point attributes were passed by the driver in the auxiliary buffer */
992 ASSERT(Irp->Tail.Overlay.AuxiliaryBuffer != NULL);
993 ReparseData = (PREPARSE_DATA_BUFFER)Irp->Tail.Overlay.AuxiliaryBuffer;
994
995 ASSERT(ReparseData->ReparseTag == IO_REPARSE_TAG_MOUNT_POINT);
996 ASSERT(ReparseData->ReparseDataLength < MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
997 ASSERT(ReparseData->Reserved < MAXIMUM_REPARSE_DATA_BUFFER_SIZE);
998
999 IopDoNameTransmogrify(Irp, FileObject, ReparseData);
1000 }
1001 }
1002
1003 /* Completion happens at APC_LEVEL */
1004 KeRaiseIrql(APC_LEVEL, &OldIrql);
1005
1006 /* Get the new I/O Status block ourselves */
1007 IoStatusBlock = Irp->IoStatus;
1008 Status = IoStatusBlock.Status;
1009
1010 /* Manually signal the even, we can't have any waiters */
1011 FileObject->Event.Header.SignalState = 1;
1012
1013 /* Now that we've signaled the events, de-associate the IRP */
1014 IopUnQueueIrpFromThread(Irp);
1015
1016 /* Check if the IRP had an input buffer */
1017 if ((Irp->Flags & IRP_BUFFERED_IO) &&
1018 (Irp->Flags & IRP_DEALLOCATE_BUFFER))
1019 {
1020 /* Free it. A driver might've tacked one on */
1021 ExFreePool(Irp->AssociatedIrp.SystemBuffer);
1022 }
1023
1024 /* Free the IRP and bring the IRQL back down */
1025 IoFreeIrp(Irp);
1026 KeLowerIrql(OldIrql);
1027 }
1028
1029 /* Copy the I/O Status */
1030 OpenPacket->Information = IoStatusBlock.Information;
1031
1032 /* The driver failed to create the file */
1033 if (!NT_SUCCESS(Status))
1034 {
1035 /* Check if we have a name */
1036 if (FileObject->FileName.Length)
1037 {
1038 /* Free it */
1039 ExFreePoolWithTag(FileObject->FileName.Buffer, TAG_IO_NAME);
1040 FileObject->FileName.Length = 0;
1041 }
1042
1043 /* Clear its device object */
1044 FileObject->DeviceObject = NULL;
1045
1046 /* Save this now because the FO might go away */
1047 OpenCancelled = FileObject->Flags & FO_FILE_OPEN_CANCELLED ?
1048 TRUE : FALSE;
1049
1050 /* Clear the file object in the open packet */
1051 OpenPacket->FileObject = NULL;
1052
1053 /* Dereference the file object */
1054 if (!UseDummyFile) ObDereferenceObject(FileObject);
1055
1056 /* Dereference the device object */
1057 IopDereferenceDeviceObject(OriginalDeviceObject, FALSE);
1058
1059 /* Unless the driver cancelled the open, dereference the VPB */
1060 if (!(OpenCancelled) && (Vpb)) IopDereferenceVpbAndFree(Vpb);
1061
1062 /* Set the status and return */
1063 OpenPacket->FinalStatus = Status;
1064 return Status;
1065 }
1066 else if (Status == STATUS_REPARSE)
1067 {
1068 if (OpenPacket->Information == IO_REPARSE ||
1069 OpenPacket->Information == IO_REPARSE_TAG_MOUNT_POINT)
1070 {
1071 /* Update CompleteName with reparse info which got updated in IopDoNameTransmogrify() */
1072 if (CompleteName->MaximumLength < FileObject->FileName.Length)
1073 {
1074 PWSTR NewCompleteName;
1075
1076 /* Allocate a new buffer for the string */
1077 NewCompleteName = ExAllocatePoolWithTag(PagedPool, FileObject->FileName.Length, TAG_IO_NAME);
1078 if (NewCompleteName == NULL)
1079 {
1080 OpenPacket->FinalStatus = STATUS_INSUFFICIENT_RESOURCES;
1081 return STATUS_INSUFFICIENT_RESOURCES;
1082 }
1083
1084 /* Release the old one */
1085 if (CompleteName->Buffer != NULL)
1086 {
1087 ExFreePoolWithTag(CompleteName->Buffer, 0);
1088 }
1089
1090 /* And setup the new one */
1091 CompleteName->Buffer = NewCompleteName;
1092 CompleteName->MaximumLength = FileObject->FileName.Length;
1093 }
1094
1095 /* Copy our new complete name */
1096 RtlCopyUnicodeString(CompleteName, &FileObject->FileName);
1097
1098 if (OpenPacket->Information == IO_REPARSE_TAG_MOUNT_POINT)
1099 {
1100 OpenPacket->RelatedFileObject = NULL;
1101 }
1102 }
1103
1104 /* Check if we have a name */
1105 if (FileObject->FileName.Length)
1106 {
1107 /* Free it */
1108 ExFreePoolWithTag(FileObject->FileName.Buffer, 0);
1109 FileObject->FileName.Length = 0;
1110 }
1111
1112 /* Clear its device object */
1113 FileObject->DeviceObject = NULL;
1114
1115 /* Clear the file object in the open packet */
1116 OpenPacket->FileObject = NULL;
1117
1118 /* Dereference the file object */
1119 if (!UseDummyFile) ObDereferenceObject(FileObject);
1120
1121 /* Dereference the device object */
1122 IopDereferenceDeviceObject(OriginalDeviceObject, FALSE);
1123
1124 /* Unless the driver cancelled the open, dereference the VPB */
1125 if (Vpb != NULL) IopDereferenceVpbAndFree(Vpb);
1126
1127 if (OpenPacket->Information != IO_REMOUNT)
1128 {
1129 OpenPacket->RelatedFileObject = NULL;
1130
1131 /* Inform we traversed a mount point for later attempt */
1132 if (OpenPacket->Information == IO_REPARSE_TAG_MOUNT_POINT)
1133 {
1134 OpenPacket->TraversedMountPoint = 1;
1135 }
1136
1137 /* In case we override checks, but got this on volume open, fail hard */
1138 if (OpenPacket->Override)
1139 {
1140 KeBugCheckEx(DRIVER_RETURNED_STATUS_REPARSE_FOR_VOLUME_OPEN,
1141 (ULONG_PTR)OriginalDeviceObject,
1142 (ULONG_PTR)DeviceObject,
1143 (ULONG_PTR)CompleteName,
1144 OpenPacket->Information);
1145 }
1146
1147 /* Return to IO/OB so that information can be upgraded */
1148 return STATUS_REPARSE;
1149 }
1150
1151 /* Loop again and reattempt an opening */
1152 continue;
1153 }
1154
1155 break;
1156 }
1157
1158 if (Attempt == IOP_MAX_REPARSE_TRAVERSAL)
1159 return STATUS_UNSUCCESSFUL;
1160
1161 /* Get the owner of the File Object */
1162 OwnerDevice = IoGetRelatedDeviceObject(FileObject);
1163
1164 /*
1165 * It's possible that the device to whom we sent the IRP to
1166 * isn't actually the device that ended opening the file object
1167 * internally.
1168 */
1169 if (OwnerDevice != DeviceObject)
1170 {
1171 /* We have to de-reference the VPB we had associated */
1172 if (Vpb) IopDereferenceVpbAndFree(Vpb);
1173
1174 /* And re-associate with the actual one */
1175 Vpb = FileObject->Vpb;
1176 if (Vpb) InterlockedIncrement((PLONG)&Vpb->ReferenceCount);
1177 }
1178
1179 /* Make sure we are not using a dummy */
1180 if (!UseDummyFile)
1181 {
1182 /* Check if this was a volume open */
1183 if ((!(FileObject->RelatedFileObject) ||
1184 (FileObject->RelatedFileObject->Flags & FO_VOLUME_OPEN)) &&
1185 !(FileObject->FileName.Length))
1186 {
1187 /* All signs point to it, but make sure it was actually an FSD */
1188 if ((OwnerDevice->DeviceType == FILE_DEVICE_DISK_FILE_SYSTEM) ||
1189 (OwnerDevice->DeviceType == FILE_DEVICE_CD_ROM_FILE_SYSTEM) ||
1190 (OwnerDevice->DeviceType == FILE_DEVICE_TAPE_FILE_SYSTEM) ||
1191 (OwnerDevice->DeviceType == FILE_DEVICE_FILE_SYSTEM))
1192 {
1193 /* The owner device is an FSD, so this is a volume open for real */
1194 FileObject->Flags |= FO_VOLUME_OPEN;
1195 }
1196 }
1197
1198 /* Reference the object and set the parse check */
1199 ObReferenceObject(FileObject);
1200 *Object = FileObject;
1201 OpenPacket->FinalStatus = IoStatusBlock.Status;
1202 OpenPacket->ParseCheck = TRUE;
1203 return OpenPacket->FinalStatus;
1204 }
1205 else
1206 {
1207 /* Check if this was a query */
1208 if (OpenPacket->QueryOnly)
1209 {
1210 /* Check if the caller wants basic info only */
1211 if (!OpenPacket->FullAttributes)
1212 {
1213 /* Allocate the buffer */
1214 FileBasicInfo = ExAllocatePoolWithTag(NonPagedPool,
1215 sizeof(*FileBasicInfo),
1216 TAG_IO);
1217 if (FileBasicInfo)
1218 {
1219 /* Do the query */
1220 Status = IoQueryFileInformation(FileObject,
1221 FileBasicInformation,
1222 sizeof(*FileBasicInfo),
1223 FileBasicInfo,
1224 &ReturnLength);
1225 if (NT_SUCCESS(Status))
1226 {
1227 /* Copy the data */
1228 RtlCopyMemory(OpenPacket->BasicInformation,
1229 FileBasicInfo,
1230 ReturnLength);
1231 }
1232
1233 /* Free our buffer */
1234 ExFreePoolWithTag(FileBasicInfo, TAG_IO);
1235 }
1236 else
1237 {
1238 /* Fail */
1239 Status = STATUS_INSUFFICIENT_RESOURCES;
1240 }
1241 }
1242 else
1243 {
1244 /* This is a full query */
1245 Status = IoQueryFileInformation(
1246 FileObject,
1247 FileNetworkOpenInformation,
1248 sizeof(FILE_NETWORK_OPEN_INFORMATION),
1249 OpenPacket->NetworkInformation,
1250 &ReturnLength);
1251 if (!NT_SUCCESS(Status)) ASSERT(Status != STATUS_NOT_IMPLEMENTED);
1252 }
1253 }
1254
1255 /* Delete the file object */
1256 IopDeleteFile(FileObject);
1257
1258 /* Clear out the file */
1259 OpenPacket->FileObject = NULL;
1260
1261 /* Set and return status */
1262 OpenPacket->FinalStatus = Status;
1263 OpenPacket->ParseCheck = TRUE;
1264 return Status;
1265 }
1266 }
1267
1268 NTSTATUS
1269 NTAPI
1270 IopParseFile(IN PVOID ParseObject,
1271 IN PVOID ObjectType,
1272 IN OUT PACCESS_STATE AccessState,
1273 IN KPROCESSOR_MODE AccessMode,
1274 IN ULONG Attributes,
1275 IN OUT PUNICODE_STRING CompleteName,
1276 IN OUT PUNICODE_STRING RemainingName,
1277 IN OUT PVOID Context OPTIONAL,
1278 IN PSECURITY_QUALITY_OF_SERVICE SecurityQos OPTIONAL,
1279 OUT PVOID *Object)
1280 {
1281 PVOID DeviceObject;
1282 POPEN_PACKET OpenPacket = (POPEN_PACKET)Context;
1283
1284 /* Validate the open packet */
1285 if (!IopValidateOpenPacket(OpenPacket)) return STATUS_OBJECT_TYPE_MISMATCH;
1286
1287 /* Get the device object */
1288 DeviceObject = IoGetRelatedDeviceObject(ParseObject);
1289 OpenPacket->RelatedFileObject = ParseObject;
1290
1291 /* Call the main routine */
1292 return IopParseDevice(DeviceObject,
1293 ObjectType,
1294 AccessState,
1295 AccessMode,
1296 Attributes,
1297 CompleteName,
1298 RemainingName,
1299 OpenPacket,
1300 SecurityQos,
1301 Object);
1302 }
1303
1304 VOID
1305 NTAPI
1306 IopDeleteFile(IN PVOID ObjectBody)
1307 {
1308 PFILE_OBJECT FileObject = (PFILE_OBJECT)ObjectBody;
1309 PIRP Irp;
1310 PIO_STACK_LOCATION StackPtr;
1311 NTSTATUS Status;
1312 KEVENT Event;
1313 PDEVICE_OBJECT DeviceObject;
1314 BOOLEAN DereferenceDone = FALSE;
1315 PVPB Vpb;
1316 KIRQL OldIrql;
1317 IOTRACE(IO_FILE_DEBUG, "ObjectBody: %p\n", ObjectBody);
1318
1319 /* Check if the file has a device object */
1320 if (FileObject->DeviceObject)
1321 {
1322 /* Check if this is a direct open or not */
1323 if (FileObject->Flags & FO_DIRECT_DEVICE_OPEN)
1324 {
1325 /* Get the attached device */
1326 DeviceObject = IoGetAttachedDevice(FileObject->DeviceObject);
1327 }
1328 else
1329 {
1330 /* Use the file object's device object */
1331 DeviceObject = IoGetRelatedDeviceObject(FileObject);
1332 }
1333
1334 /* Sanity check */
1335 ASSERT(!(FileObject->Flags & FO_SYNCHRONOUS_IO) ||
1336 (InterlockedExchange((PLONG)&FileObject->Busy, TRUE) == FALSE));
1337
1338 /* Check if the handle wasn't created yet */
1339 if (!(FileObject->Flags & FO_HANDLE_CREATED))
1340 {
1341 /* Send the cleanup IRP */
1342 IopCloseFile(NULL, ObjectBody, 0, 1, 1);
1343 }
1344
1345 /* Clear and set up Events */
1346 KeClearEvent(&FileObject->Event);
1347 KeInitializeEvent(&Event, SynchronizationEvent, FALSE);
1348
1349 /* Allocate an IRP */
1350 Irp = IopAllocateIrpMustSucceed(DeviceObject->StackSize);
1351
1352 /* Set it up */
1353 Irp->UserEvent = &Event;
1354 Irp->UserIosb = &Irp->IoStatus;
1355 Irp->Tail.Overlay.Thread = PsGetCurrentThread();
1356 Irp->Tail.Overlay.OriginalFileObject = FileObject;
1357 Irp->Flags = IRP_CLOSE_OPERATION | IRP_SYNCHRONOUS_API;
1358
1359 /* Set up Stack Pointer Data */
1360 StackPtr = IoGetNextIrpStackLocation(Irp);
1361 StackPtr->MajorFunction = IRP_MJ_CLOSE;
1362 StackPtr->FileObject = FileObject;
1363
1364 /* Queue the IRP */
1365 IopQueueIrpToThread(Irp);
1366
1367 /* Get the VPB and check if this isn't a direct open */
1368 Vpb = FileObject->Vpb;
1369 if ((Vpb) && !(FileObject->Flags & FO_DIRECT_DEVICE_OPEN))
1370 {
1371 /* Dereference the VPB before the close */
1372 InterlockedDecrement((PLONG)&Vpb->ReferenceCount);
1373 }
1374
1375 /* Check if the FS will never disappear by itself */
1376 if (FileObject->DeviceObject->Flags & DO_NEVER_LAST_DEVICE)
1377 {
1378 /* Dereference it */
1379 InterlockedDecrement(&FileObject->DeviceObject->ReferenceCount);
1380 DereferenceDone = TRUE;
1381 }
1382
1383 /* Call the FS Driver */
1384 Status = IoCallDriver(DeviceObject, Irp);
1385 if (Status == STATUS_PENDING)
1386 {
1387 /* Wait for completion */
1388 KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE, NULL);
1389 }
1390
1391 /* De-queue the IRP */
1392 KeRaiseIrql(APC_LEVEL, &OldIrql);
1393 IopUnQueueIrpFromThread(Irp);
1394 KeLowerIrql(OldIrql);
1395
1396 /* Free the IRP */
1397 IoFreeIrp(Irp);
1398
1399 /* Clear the file name */
1400 if (FileObject->FileName.Buffer)
1401 {
1402 ExFreePoolWithTag(FileObject->FileName.Buffer, TAG_IO_NAME);
1403 FileObject->FileName.Buffer = NULL;
1404 }
1405
1406 /* Check if the FO had a completion port */
1407 if (FileObject->CompletionContext)
1408 {
1409 /* Free it */
1410 ObDereferenceObject(FileObject->CompletionContext->Port);
1411 ExFreePool(FileObject->CompletionContext);
1412 }
1413
1414 /* Check if the FO had extension */
1415 if (FileObject->Flags & FO_FILE_OBJECT_HAS_EXTENSION)
1416 {
1417 /* Release filter context structure if any */
1418 FsRtlPTeardownPerFileObjectContexts(FileObject);
1419 }
1420
1421 /* Check if dereference has been done yet */
1422 if (!DereferenceDone)
1423 {
1424 /* Dereference device object */
1425 IopDereferenceDeviceObject(FileObject->DeviceObject, FALSE);
1426 }
1427 }
1428 }
1429
1430 PDEVICE_OBJECT
1431 NTAPI
1432 IopGetDeviceAttachmentBase(IN PDEVICE_OBJECT DeviceObject)
1433 {
1434 PDEVICE_OBJECT PDO = DeviceObject;
1435
1436 /* Go down the stack to attempt to get the PDO */
1437 for (; ((PEXTENDED_DEVOBJ_EXTENSION)PDO->DeviceObjectExtension)->AttachedTo != NULL;
1438 PDO = ((PEXTENDED_DEVOBJ_EXTENSION)PDO->DeviceObjectExtension)->AttachedTo);
1439
1440 return PDO;
1441 }
1442
1443 PDEVICE_OBJECT
1444 NTAPI
1445 IopGetDevicePDO(IN PDEVICE_OBJECT DeviceObject)
1446 {
1447 KIRQL OldIrql;
1448 PDEVICE_OBJECT PDO;
1449
1450 ASSERT(DeviceObject != NULL);
1451
1452 OldIrql = KeAcquireQueuedSpinLock(LockQueueIoDatabaseLock);
1453 /* Get the base DO */
1454 PDO = IopGetDeviceAttachmentBase(DeviceObject);
1455 /* Check whether that's really a PDO and if so, keep it */
1456 if ((PDO->Flags & DO_BUS_ENUMERATED_DEVICE) != DO_BUS_ENUMERATED_DEVICE)
1457 {
1458 PDO = NULL;
1459 }
1460 else
1461 {
1462 ObReferenceObject(PDO);
1463 }
1464 KeReleaseQueuedSpinLock(LockQueueIoDatabaseLock, OldIrql);
1465
1466 return PDO;
1467 }
1468
1469 NTSTATUS
1470 NTAPI
1471 IopSetDeviceSecurityDescriptor(IN PDEVICE_OBJECT DeviceObject,
1472 IN PSECURITY_INFORMATION SecurityInformation,
1473 IN PSECURITY_DESCRIPTOR SecurityDescriptor,
1474 IN POOL_TYPE PoolType,
1475 IN PGENERIC_MAPPING GenericMapping)
1476 {
1477 NTSTATUS Status;
1478 PSECURITY_DESCRIPTOR OldSecurityDescriptor, CachedSecurityDescriptor, NewSecurityDescriptor;
1479
1480 PAGED_CODE();
1481
1482 /* Keep attempting till we find our old SD or fail */
1483 while (TRUE)
1484 {
1485 KeEnterCriticalRegion();
1486 ExAcquireResourceSharedLite(&IopSecurityResource, TRUE);
1487
1488 /* Get our old SD and reference it */
1489 OldSecurityDescriptor = DeviceObject->SecurityDescriptor;
1490 if (OldSecurityDescriptor != NULL)
1491 {
1492 ObReferenceSecurityDescriptor(OldSecurityDescriptor, 1);
1493 }
1494
1495 ExReleaseResourceLite(&IopSecurityResource);
1496 KeLeaveCriticalRegion();
1497
1498 /* Set the SD information */
1499 NewSecurityDescriptor = OldSecurityDescriptor;
1500 Status = SeSetSecurityDescriptorInfo(NULL, SecurityInformation,
1501 SecurityDescriptor, &NewSecurityDescriptor,
1502 PoolType, GenericMapping);
1503
1504 if (!NT_SUCCESS(Status))
1505 {
1506 if (OldSecurityDescriptor != NULL)
1507 {
1508 ObDereferenceSecurityDescriptor(OldSecurityDescriptor, 1);
1509 }
1510
1511 break;
1512 }
1513
1514 /* Add the new DS to the internal cache */
1515 Status = ObLogSecurityDescriptor(NewSecurityDescriptor,
1516 &CachedSecurityDescriptor, 1);
1517 ExFreePool(NewSecurityDescriptor);
1518 if (!NT_SUCCESS(Status))
1519 {
1520 ObDereferenceSecurityDescriptor(OldSecurityDescriptor, 1);
1521 break;
1522 }
1523
1524 KeEnterCriticalRegion();
1525 ExAcquireResourceExclusiveLite(&IopSecurityResource, TRUE);
1526 /* Check if someone changed it in our back */
1527 if (DeviceObject->SecurityDescriptor == OldSecurityDescriptor)
1528 {
1529 /* We're clear, do the swap */
1530 DeviceObject->SecurityDescriptor = CachedSecurityDescriptor;
1531 ExReleaseResourceLite(&IopSecurityResource);
1532 KeLeaveCriticalRegion();
1533
1534 /* And dereference old SD (twice - us + not in use) */
1535 ObDereferenceSecurityDescriptor(OldSecurityDescriptor, 2);
1536
1537 break;
1538 }
1539 ExReleaseResourceLite(&IopSecurityResource);
1540 KeLeaveCriticalRegion();
1541
1542 /* If so, try again */
1543 ObDereferenceSecurityDescriptor(OldSecurityDescriptor, 1);
1544 ObDereferenceSecurityDescriptor(CachedSecurityDescriptor, 1);
1545 }
1546
1547 return Status;
1548 }
1549
1550 NTSTATUS
1551 NTAPI
1552 IopSetDeviceSecurityDescriptors(IN PDEVICE_OBJECT UpperDeviceObject,
1553 IN PDEVICE_OBJECT PhysicalDeviceObject,
1554 IN PSECURITY_INFORMATION SecurityInformation,
1555 IN PSECURITY_DESCRIPTOR SecurityDescriptor,
1556 IN POOL_TYPE PoolType,
1557 IN PGENERIC_MAPPING GenericMapping)
1558 {
1559 PDEVICE_OBJECT CurrentDO = PhysicalDeviceObject, NextDevice;
1560 NTSTATUS Status = STATUS_SUCCESS, TmpStatus;
1561
1562 PAGED_CODE();
1563
1564 ASSERT(PhysicalDeviceObject != NULL);
1565
1566 /* We always reference the DO we're working on */
1567 ObReferenceObject(CurrentDO);
1568
1569 /* Go up from PDO to latest DO */
1570 do
1571 {
1572 /* Attempt to set the new SD on it */
1573 TmpStatus = IopSetDeviceSecurityDescriptor(CurrentDO, SecurityInformation,
1574 SecurityDescriptor, PoolType,
1575 GenericMapping);
1576 /* Was our last one? Remember that status then */
1577 if (CurrentDO == UpperDeviceObject)
1578 {
1579 Status = TmpStatus;
1580 }
1581
1582 /* Try to move to the next DO (and thus, reference it) */
1583 NextDevice = CurrentDO->AttachedDevice;
1584 if (NextDevice)
1585 {
1586 ObReferenceObject(NextDevice);
1587 }
1588
1589 /* Dereference current DO and move to the next one */
1590 ObDereferenceObject(CurrentDO);
1591 CurrentDO = NextDevice;
1592 }
1593 while (CurrentDO != NULL);
1594
1595 return Status;
1596 }
1597
1598 NTSTATUS
1599 NTAPI
1600 IopGetSetSecurityObject(IN PVOID ObjectBody,
1601 IN SECURITY_OPERATION_CODE OperationCode,
1602 IN PSECURITY_INFORMATION SecurityInformation,
1603 IN PSECURITY_DESCRIPTOR SecurityDescriptor,
1604 IN OUT PULONG BufferLength,
1605 IN OUT PSECURITY_DESCRIPTOR *OldSecurityDescriptor,
1606 IN POOL_TYPE PoolType,
1607 IN OUT PGENERIC_MAPPING GenericMapping)
1608 {
1609 IO_STATUS_BLOCK IoStatusBlock;
1610 PIO_STACK_LOCATION StackPtr;
1611 PFILE_OBJECT FileObject;
1612 PDEVICE_OBJECT DeviceObject;
1613 PIRP Irp;
1614 BOOLEAN LocalEvent = FALSE;
1615 KEVENT Event;
1616 NTSTATUS Status = STATUS_SUCCESS;
1617 PAGED_CODE();
1618 IOTRACE(IO_FILE_DEBUG, "ObjectBody: %p\n", ObjectBody);
1619
1620 /* Check if this is a device or file */
1621 if (((PFILE_OBJECT)ObjectBody)->Type == IO_TYPE_DEVICE)
1622 {
1623 /* It's a device */
1624 DeviceObject = (PDEVICE_OBJECT)ObjectBody;
1625 FileObject = NULL;
1626 }
1627 else
1628 {
1629 /* It's a file */
1630 FileObject = (PFILE_OBJECT)ObjectBody;
1631
1632 /* Check if this is a direct open */
1633 if (FileObject->Flags & FO_DIRECT_DEVICE_OPEN)
1634 {
1635 /* Get the Device Object */
1636 DeviceObject = IoGetAttachedDevice(FileObject->DeviceObject);
1637 }
1638 else
1639 {
1640 /* Otherwise, use the direct device*/
1641 DeviceObject = FileObject->DeviceObject;
1642 }
1643 }
1644
1645 /* Check if the request was for a device object */
1646 if (!(FileObject) ||
1647 (!(FileObject->FileName.Length) && !(FileObject->RelatedFileObject)) ||
1648 (FileObject->Flags & FO_DIRECT_DEVICE_OPEN))
1649 {
1650 /* Check what kind of request this was */
1651 if (OperationCode == QuerySecurityDescriptor)
1652 {
1653 return SeQuerySecurityDescriptorInfo(SecurityInformation,
1654 SecurityDescriptor,
1655 BufferLength,
1656 &DeviceObject->SecurityDescriptor);
1657 }
1658 else if (OperationCode == DeleteSecurityDescriptor)
1659 {
1660 /* Simply return success */
1661 return STATUS_SUCCESS;
1662 }
1663 else if (OperationCode == AssignSecurityDescriptor)
1664 {
1665 Status = STATUS_SUCCESS;
1666
1667 /* Make absolutely sure this is a device object */
1668 if (!(FileObject) || !(FileObject->Flags & FO_STREAM_FILE))
1669 {
1670 PSECURITY_DESCRIPTOR CachedSecurityDescriptor;
1671
1672 /* Add the security descriptor in cache */
1673 Status = ObLogSecurityDescriptor(SecurityDescriptor, &CachedSecurityDescriptor, 1);
1674 if (NT_SUCCESS(Status))
1675 {
1676 KeEnterCriticalRegion();
1677 ExAcquireResourceExclusiveLite(&IopSecurityResource, TRUE);
1678
1679 /* Assign the Security Descriptor */
1680 DeviceObject->SecurityDescriptor = CachedSecurityDescriptor;
1681
1682 ExReleaseResourceLite(&IopSecurityResource);
1683 KeLeaveCriticalRegion();
1684 }
1685 }
1686
1687 /* Return status */
1688 return Status;
1689 }
1690 else if (OperationCode == SetSecurityDescriptor)
1691 {
1692 /* Get the Physical Device Object if any */
1693 PDEVICE_OBJECT PDO = IopGetDevicePDO(DeviceObject);
1694
1695 if (PDO != NULL)
1696 {
1697 /* Apply the new SD to any DO in the path from PDO to current DO */
1698 Status = IopSetDeviceSecurityDescriptors(DeviceObject, PDO,
1699 SecurityInformation,
1700 SecurityDescriptor,
1701 PoolType, GenericMapping);
1702 ObDereferenceObject(PDO);
1703 }
1704 else
1705 {
1706 /* Otherwise, just set for ourselves */
1707 Status = IopSetDeviceSecurityDescriptor(DeviceObject,
1708 SecurityInformation,
1709 SecurityDescriptor,
1710 PoolType, GenericMapping);
1711 }
1712
1713 return STATUS_SUCCESS;
1714 }
1715
1716 /* Shouldn't happen */
1717 return STATUS_SUCCESS;
1718 }
1719 else if (OperationCode == DeleteSecurityDescriptor)
1720 {
1721 /* Same as for devices, do nothing */
1722 return STATUS_SUCCESS;
1723 }
1724
1725 /* At this point, we know we're a file. Reference it */
1726 ObReferenceObject(FileObject);
1727
1728 /* Check if we should use Sync IO or not */
1729 if (FileObject->Flags & FO_SYNCHRONOUS_IO)
1730 {
1731 /* Lock the file object */
1732 Status = IopLockFileObject(FileObject, ExGetPreviousMode());
1733 if (Status != STATUS_SUCCESS)
1734 {
1735 ObDereferenceObject(FileObject);
1736 return Status;
1737 }
1738 }
1739 else
1740 {
1741 /* Use local event */
1742 KeInitializeEvent(&Event, SynchronizationEvent, FALSE);
1743 LocalEvent = TRUE;
1744 }
1745
1746 /* Clear the File Object event */
1747 KeClearEvent(&FileObject->Event);
1748
1749 /* Get the device object */
1750 DeviceObject = IoGetRelatedDeviceObject(FileObject);
1751
1752 /* Allocate the IRP */
1753 Irp = IoAllocateIrp(DeviceObject->StackSize, FALSE);
1754 if (!Irp) return IopCleanupFailedIrp(FileObject, NULL, NULL);
1755
1756 /* Set the IRP */
1757 Irp->Tail.Overlay.OriginalFileObject = FileObject;
1758 Irp->Tail.Overlay.Thread = PsGetCurrentThread();
1759 Irp->RequestorMode = ExGetPreviousMode();
1760 Irp->UserIosb = &IoStatusBlock;
1761 Irp->UserEvent = (LocalEvent) ? &Event : NULL;
1762 Irp->Flags = (LocalEvent) ? IRP_SYNCHRONOUS_API : 0;
1763 Irp->Overlay.AsynchronousParameters.UserApcRoutine = NULL;
1764
1765 /* Set Stack Parameters */
1766 StackPtr = IoGetNextIrpStackLocation(Irp);
1767 StackPtr->FileObject = FileObject;
1768
1769 /* Check if this is a query or set */
1770 if (OperationCode == QuerySecurityDescriptor)
1771 {
1772 /* Set the major function and parameters */
1773 StackPtr->MajorFunction = IRP_MJ_QUERY_SECURITY;
1774 StackPtr->Parameters.QuerySecurity.SecurityInformation =
1775 *SecurityInformation;
1776 StackPtr->Parameters.QuerySecurity.Length = *BufferLength;
1777 Irp->UserBuffer = SecurityDescriptor;
1778 }
1779 else
1780 {
1781 /* Set the major function and parameters for a set */
1782 StackPtr->MajorFunction = IRP_MJ_SET_SECURITY;
1783 StackPtr->Parameters.SetSecurity.SecurityInformation =
1784 *SecurityInformation;
1785 StackPtr->Parameters.SetSecurity.SecurityDescriptor =
1786 SecurityDescriptor;
1787 }
1788
1789 /* Queue the IRP */
1790 IopQueueIrpToThread(Irp);
1791
1792 /* Update operation counts */
1793 IopUpdateOperationCount(IopOtherTransfer);
1794
1795 /* Call the Driver */
1796 Status = IoCallDriver(DeviceObject, Irp);
1797
1798 /* Check if this was async I/O */
1799 if (LocalEvent)
1800 {
1801 /* Check if the IRP is pending completion */
1802 if (Status == STATUS_PENDING)
1803 {
1804 /* Wait on the local event */
1805 KeWaitForSingleObject(&Event,
1806 Executive,
1807 KernelMode,
1808 FALSE,
1809 NULL);
1810 Status = IoStatusBlock.Status;
1811 }
1812 }
1813 else
1814 {
1815 /* Check if the IRP is pending completion */
1816 if (Status == STATUS_PENDING)
1817 {
1818 /* Wait on the file object */
1819 KeWaitForSingleObject(&FileObject->Event,
1820 Executive,
1821 KernelMode,
1822 FALSE,
1823 NULL);
1824 Status = FileObject->FinalStatus;
1825 }
1826
1827 /* Release the lock */
1828 IopUnlockFileObject(FileObject);
1829 }
1830
1831 /* This Driver doesn't implement Security, so try to give it a default */
1832 if (Status == STATUS_INVALID_DEVICE_REQUEST)
1833 {
1834 /* Was this a query? */
1835 if (OperationCode == QuerySecurityDescriptor)
1836 {
1837 /* Set a World Security Descriptor */
1838 Status = SeSetWorldSecurityDescriptor(*SecurityInformation,
1839 SecurityDescriptor,
1840 BufferLength);
1841 }
1842 else
1843 {
1844 /* It wasn't a query, so just fake success */
1845 Status = STATUS_SUCCESS;
1846 }
1847 }
1848 else if (OperationCode == QuerySecurityDescriptor)
1849 {
1850 /* Callers usually expect the normalized form */
1851 if (Status == STATUS_BUFFER_OVERFLOW) Status = STATUS_BUFFER_TOO_SMALL;
1852
1853 _SEH2_TRY
1854 {
1855 /* Return length */
1856 *BufferLength = (ULONG)IoStatusBlock.Information;
1857 }
1858 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
1859 {
1860 /* Get the exception code */
1861 Status = _SEH2_GetExceptionCode();
1862 }
1863 _SEH2_END;
1864 }
1865
1866 /* Return Status */
1867 return Status;
1868 }
1869
1870 NTSTATUS
1871 NTAPI
1872 IopQueryName(IN PVOID ObjectBody,
1873 IN BOOLEAN HasName,
1874 OUT POBJECT_NAME_INFORMATION ObjectNameInfo,
1875 IN ULONG Length,
1876 OUT PULONG ReturnLength,
1877 IN KPROCESSOR_MODE PreviousMode)
1878 {
1879 return IopQueryNameInternal(ObjectBody,
1880 HasName,
1881 FALSE,
1882 ObjectNameInfo,
1883 Length,
1884 ReturnLength,
1885 PreviousMode);
1886 }
1887
1888 NTSTATUS
1889 NTAPI
1890 IopQueryNameInternal(IN PVOID ObjectBody,
1891 IN BOOLEAN HasName,
1892 IN BOOLEAN QueryDosName,
1893 OUT POBJECT_NAME_INFORMATION ObjectNameInfo,
1894 IN ULONG Length,
1895 OUT PULONG ReturnLength,
1896 IN KPROCESSOR_MODE PreviousMode)
1897 {
1898 POBJECT_NAME_INFORMATION LocalInfo;
1899 PFILE_NAME_INFORMATION LocalFileInfo;
1900 PFILE_OBJECT FileObject = (PFILE_OBJECT)ObjectBody;
1901 ULONG LocalReturnLength, FileLength;
1902 BOOLEAN LengthMismatch = FALSE;
1903 NTSTATUS Status;
1904 PWCHAR p;
1905 PDEVICE_OBJECT DeviceObject;
1906 BOOLEAN NoObCall;
1907
1908 IOTRACE(IO_FILE_DEBUG, "ObjectBody: %p\n", ObjectBody);
1909
1910 /* Validate length */
1911 if (Length < sizeof(OBJECT_NAME_INFORMATION))
1912 {
1913 /* Wrong length, fail */
1914 *ReturnLength = sizeof(OBJECT_NAME_INFORMATION);
1915 return STATUS_INFO_LENGTH_MISMATCH;
1916 }
1917
1918 /* Allocate Buffer */
1919 LocalInfo = ExAllocatePoolWithTag(PagedPool, Length, TAG_IO);
1920 if (!LocalInfo) return STATUS_INSUFFICIENT_RESOURCES;
1921
1922 /* Query DOS name if the caller asked to */
1923 NoObCall = FALSE;
1924 if (QueryDosName)
1925 {
1926 DeviceObject = FileObject->DeviceObject;
1927
1928 /* In case of a network file system, don't call mountmgr */
1929 if (DeviceObject->DeviceType == FILE_DEVICE_NETWORK_FILE_SYSTEM)
1930 {
1931 /* We'll store separator and terminator */
1932 LocalReturnLength = sizeof(OBJECT_NAME_INFORMATION) + 2 * sizeof(WCHAR);
1933 if (Length < LocalReturnLength)
1934 {
1935 Status = STATUS_BUFFER_OVERFLOW;
1936 }
1937 else
1938 {
1939 LocalInfo->Name.Length = sizeof(WCHAR);
1940 LocalInfo->Name.MaximumLength = sizeof(WCHAR);
1941 LocalInfo->Name.Buffer = (PVOID)((ULONG_PTR)LocalInfo + sizeof(OBJECT_NAME_INFORMATION));
1942 LocalInfo->Name.Buffer[0] = OBJ_NAME_PATH_SEPARATOR;
1943 Status = STATUS_SUCCESS;
1944 }
1945 }
1946 /* Otherwise, call mountmgr to get DOS name */
1947 else
1948 {
1949 Status = IoVolumeDeviceToDosName(DeviceObject, &LocalInfo->Name);
1950 LocalReturnLength = LocalInfo->Name.Length + sizeof(OBJECT_NAME_INFORMATION) + sizeof(WCHAR);
1951 }
1952 }
1953
1954 /* Fall back if querying DOS name failed or if caller never wanted it ;-) */
1955 if (!QueryDosName || !NT_SUCCESS(Status))
1956 {
1957 /* Query the name */
1958 Status = ObQueryNameString(FileObject->DeviceObject,
1959 LocalInfo,
1960 Length,
1961 &LocalReturnLength);
1962 }
1963 else
1964 {
1965 NoObCall = TRUE;
1966 }
1967
1968 if (!NT_SUCCESS(Status) && (Status != STATUS_INFO_LENGTH_MISMATCH))
1969 {
1970 /* Free the buffer and fail */
1971 ExFreePoolWithTag(LocalInfo, TAG_IO);
1972 return Status;
1973 }
1974
1975 /* Get buffer pointer */
1976 p = (PWCHAR)(ObjectNameInfo + 1);
1977
1978 /* Copy the information */
1979 if (QueryDosName && NoObCall)
1980 {
1981 ASSERT(PreviousMode == KernelMode);
1982
1983 /* Copy structure first */
1984 RtlCopyMemory(ObjectNameInfo,
1985 LocalInfo,
1986 (Length >= LocalReturnLength ? sizeof(OBJECT_NAME_INFORMATION) : Length));
1987 /* Name then */
1988 RtlCopyMemory(p, LocalInfo->Name.Buffer,
1989 (Length >= LocalReturnLength ? LocalInfo->Name.Length : Length - sizeof(OBJECT_NAME_INFORMATION)));
1990
1991 if (FileObject->DeviceObject->DeviceType != FILE_DEVICE_NETWORK_FILE_SYSTEM)
1992 {
1993 ExFreePool(LocalInfo->Name.Buffer);
1994 }
1995 }
1996 else
1997 {
1998 RtlCopyMemory(ObjectNameInfo,
1999 LocalInfo,
2000 (LocalReturnLength > Length) ?
2001 Length : LocalReturnLength);
2002 }
2003
2004 /* Set buffer pointer */
2005 ObjectNameInfo->Name.Buffer = p;
2006
2007 /* Advance in buffer */
2008 p += (LocalInfo->Name.Length / sizeof(WCHAR));
2009
2010 /* Check if this already filled our buffer */
2011 if (LocalReturnLength > Length)
2012 {
2013 /* Set the length mismatch to true, so that we can return
2014 * the proper buffer size to the caller later
2015 */
2016 LengthMismatch = TRUE;
2017
2018 /* Save the initial buffer length value */
2019 *ReturnLength = LocalReturnLength;
2020 }
2021
2022 /* Now get the file name buffer and check the length needed */
2023 LocalFileInfo = (PFILE_NAME_INFORMATION)LocalInfo;
2024 FileLength = Length -
2025 LocalReturnLength +
2026 FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
2027
2028 /* Query the File name */
2029 if (PreviousMode == KernelMode &&
2030 BooleanFlagOn(FileObject->Flags, FO_SYNCHRONOUS_IO))
2031 {
2032 Status = IopGetFileInformation(FileObject,
2033 LengthMismatch ? Length : FileLength,
2034 FileNameInformation,
2035 LocalFileInfo,
2036 &LocalReturnLength);
2037 }
2038 else
2039 {
2040 Status = IoQueryFileInformation(FileObject,
2041 FileNameInformation,
2042 LengthMismatch ? Length : FileLength,
2043 LocalFileInfo,
2044 &LocalReturnLength);
2045 }
2046 if (NT_ERROR(Status))
2047 {
2048 /* Allow status that would mean it's not implemented in the storage stack */
2049 if (Status != STATUS_INVALID_PARAMETER && Status != STATUS_INVALID_DEVICE_REQUEST &&
2050 Status != STATUS_NOT_IMPLEMENTED && Status != STATUS_INVALID_INFO_CLASS)
2051 {
2052 ExFreePoolWithTag(LocalInfo, TAG_IO);
2053 return Status;
2054 }
2055
2056 /* In such case, zero output */
2057 LocalReturnLength = FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
2058 LocalFileInfo->FileNameLength = 0;
2059 LocalFileInfo->FileName[0] = OBJ_NAME_PATH_SEPARATOR;
2060 }
2061 else
2062 {
2063 /* We'll at least return the name length */
2064 if (LocalReturnLength < FIELD_OFFSET(FILE_NAME_INFORMATION, FileName))
2065 {
2066 LocalReturnLength = FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
2067 }
2068 }
2069
2070 /* If the provided buffer is too small, return the required size */
2071 if (LengthMismatch)
2072 {
2073 /* Add the required length */
2074 *ReturnLength += LocalFileInfo->FileNameLength;
2075
2076 /* Free the allocated buffer and return failure */
2077 ExFreePoolWithTag(LocalInfo, TAG_IO);
2078 return STATUS_BUFFER_OVERFLOW;
2079 }
2080
2081 /* Now calculate the new lengths left */
2082 FileLength = LocalReturnLength -
2083 FIELD_OFFSET(FILE_NAME_INFORMATION, FileName);
2084 LocalReturnLength = (ULONG)((ULONG_PTR)p -
2085 (ULONG_PTR)ObjectNameInfo +
2086 LocalFileInfo->FileNameLength);
2087
2088 /* Don't copy the name if it's not valid */
2089 if (LocalFileInfo->FileName[0] != OBJ_NAME_PATH_SEPARATOR)
2090 {
2091 /* Free the allocated buffer and return failure */
2092 ExFreePoolWithTag(LocalInfo, TAG_IO);
2093 return STATUS_OBJECT_PATH_INVALID;
2094 }
2095
2096 /* Write the Name and null-terminate it */
2097 RtlCopyMemory(p, LocalFileInfo->FileName, FileLength);
2098 p += (FileLength / sizeof(WCHAR));
2099 *p = UNICODE_NULL;
2100 LocalReturnLength += sizeof(UNICODE_NULL);
2101
2102 /* Return the length needed */
2103 *ReturnLength = LocalReturnLength;
2104
2105 /* Setup the length and maximum length */
2106 FileLength = (ULONG)((ULONG_PTR)p - (ULONG_PTR)ObjectNameInfo);
2107 ObjectNameInfo->Name.Length = (USHORT)FileLength -
2108 sizeof(OBJECT_NAME_INFORMATION);
2109 ObjectNameInfo->Name.MaximumLength = (USHORT)ObjectNameInfo->Name.Length +
2110 sizeof(UNICODE_NULL);
2111
2112 /* Free buffer and return */
2113 ExFreePoolWithTag(LocalInfo, TAG_IO);
2114 return Status;
2115 }
2116
2117 VOID
2118 NTAPI
2119 IopCloseFile(IN PEPROCESS Process OPTIONAL,
2120 IN PVOID ObjectBody,
2121 IN ACCESS_MASK GrantedAccess,
2122 IN ULONG HandleCount,
2123 IN ULONG SystemHandleCount)
2124 {
2125 PFILE_OBJECT FileObject = (PFILE_OBJECT)ObjectBody;
2126 KEVENT Event;
2127 PIRP Irp;
2128 PIO_STACK_LOCATION StackPtr;
2129 NTSTATUS Status;
2130 PDEVICE_OBJECT DeviceObject;
2131 KIRQL OldIrql;
2132 IO_STATUS_BLOCK IoStatusBlock;
2133 IOTRACE(IO_FILE_DEBUG, "ObjectBody: %p\n", ObjectBody);
2134
2135 /* If this isn't the last handle for the current process, quit */
2136 if (HandleCount != 1) return;
2137
2138 /* Check if the file is locked and has more then one handle opened */
2139 if ((FileObject->LockOperation) && (SystemHandleCount != 1))
2140 {
2141 /* Check if this is a direct open or not */
2142 if (BooleanFlagOn(FileObject->Flags, FO_DIRECT_DEVICE_OPEN))
2143 {
2144 /* Get the attached device */
2145 DeviceObject = IoGetAttachedDevice(FileObject->DeviceObject);
2146 }
2147 else
2148 {
2149 /* Get the FO's device */
2150 DeviceObject = IoGetRelatedDeviceObject(FileObject);
2151 }
2152
2153 /* Check if this is a sync FO and lock it */
2154 if (BooleanFlagOn(FileObject->Flags, FO_SYNCHRONOUS_IO))
2155 {
2156 (VOID)IopLockFileObject(FileObject, KernelMode);
2157 }
2158
2159 /* Go the FastIO path if possible, otherwise fall back to IRP */
2160 if (DeviceObject->DriverObject->FastIoDispatch == NULL ||
2161 DeviceObject->DriverObject->FastIoDispatch->FastIoUnlockAll == NULL ||
2162 !DeviceObject->DriverObject->FastIoDispatch->FastIoUnlockAll(FileObject, PsGetCurrentProcess(), &IoStatusBlock, DeviceObject))
2163 {
2164 /* Clear and set up Events */
2165 KeClearEvent(&FileObject->Event);
2166 KeInitializeEvent(&Event, SynchronizationEvent, FALSE);
2167
2168 /* Allocate an IRP */
2169 Irp = IopAllocateIrpMustSucceed(DeviceObject->StackSize);
2170
2171 /* Set it up */
2172 Irp->UserEvent = &Event;
2173 Irp->UserIosb = &Irp->IoStatus;
2174 Irp->Tail.Overlay.Thread = PsGetCurrentThread();
2175 Irp->Tail.Overlay.OriginalFileObject = FileObject;
2176 Irp->RequestorMode = KernelMode;
2177 Irp->Flags = IRP_SYNCHRONOUS_API;
2178 Irp->Overlay.AsynchronousParameters.UserApcRoutine = NULL;
2179 ObReferenceObject(FileObject);
2180
2181 /* Set up Stack Pointer Data */
2182 StackPtr = IoGetNextIrpStackLocation(Irp);
2183 StackPtr->MajorFunction = IRP_MJ_LOCK_CONTROL;
2184 StackPtr->MinorFunction = IRP_MN_UNLOCK_ALL;
2185 StackPtr->FileObject = FileObject;
2186
2187 /* Queue the IRP */
2188 IopQueueIrpToThread(Irp);
2189
2190 /* Call the FS Driver */
2191 Status = IoCallDriver(DeviceObject, Irp);
2192 if (Status == STATUS_PENDING)
2193 {
2194 /* Wait for completion */
2195 KeWaitForSingleObject(&Event, UserRequest, KernelMode, FALSE, NULL);
2196 }
2197
2198 /* IO will unqueue & free for us */
2199 }
2200
2201 /* Release the lock if we were holding it */
2202 if (BooleanFlagOn(FileObject->Flags, FO_SYNCHRONOUS_IO))
2203 {
2204 IopUnlockFileObject(FileObject);
2205 }
2206 }
2207
2208 /* Make sure this is the last handle */
2209 if (SystemHandleCount != 1) return;
2210
2211 /* Check if this is a direct open or not */
2212 if (FileObject->Flags & FO_DIRECT_DEVICE_OPEN)
2213 {
2214 /* Get the attached device */
2215 DeviceObject = IoGetAttachedDevice(FileObject->DeviceObject);
2216 }
2217 else
2218 {
2219 /* Get the FO's device */
2220 DeviceObject = IoGetRelatedDeviceObject(FileObject);
2221 }
2222
2223 /* Set the handle created flag */
2224 FileObject->Flags |= FO_HANDLE_CREATED;
2225
2226 /* Check if this is a sync FO and lock it */
2227 if (Process != NULL &&
2228 BooleanFlagOn(FileObject->Flags, FO_SYNCHRONOUS_IO))
2229 {
2230 (VOID)IopLockFileObject(FileObject, KernelMode);
2231 }
2232
2233 /* Clear and set up Events */
2234 KeClearEvent(&FileObject->Event);
2235 KeInitializeEvent(&Event, SynchronizationEvent, FALSE);
2236
2237 /* Allocate an IRP */
2238 Irp = IopAllocateIrpMustSucceed(DeviceObject->StackSize);
2239
2240 /* Set it up */
2241 Irp->UserEvent = &Event;
2242 Irp->UserIosb = &Irp->IoStatus;
2243 Irp->Tail.Overlay.Thread = PsGetCurrentThread();
2244 Irp->Tail.Overlay.OriginalFileObject = FileObject;
2245 Irp->Overlay.AsynchronousParameters.UserApcRoutine = NULL;
2246 Irp->Flags = IRP_CLOSE_OPERATION | IRP_SYNCHRONOUS_API;
2247
2248 /* Set up Stack Pointer Data */
2249 StackPtr = IoGetNextIrpStackLocation(Irp);
2250 StackPtr->MajorFunction = IRP_MJ_CLEANUP;
2251 StackPtr->FileObject = FileObject;
2252
2253 /* Queue the IRP */
2254 IopQueueIrpToThread(Irp);
2255
2256 /* Update operation counts */
2257 IopUpdateOperationCount(IopOtherTransfer);
2258
2259 /* Call the FS Driver */
2260 Status = IoCallDriver(DeviceObject, Irp);
2261 if (Status == STATUS_PENDING)
2262 {
2263 /* Wait for completion */
2264 KeWaitForSingleObject(&Event, UserRequest, KernelMode, FALSE, NULL);
2265 }
2266
2267 /* Unqueue the IRP */
2268 KeRaiseIrql(APC_LEVEL, &OldIrql);
2269 IopUnQueueIrpFromThread(Irp);
2270 KeLowerIrql(OldIrql);
2271
2272 /* Free the IRP */
2273 IoFreeIrp(Irp);
2274
2275 /* Release the lock if we were holding it */
2276 if (Process != NULL &&
2277 BooleanFlagOn(FileObject->Flags, FO_SYNCHRONOUS_IO))
2278 {
2279 IopUnlockFileObject(FileObject);
2280 }
2281 }
2282
2283 NTSTATUS
2284 NTAPI
2285 IopQueryAttributesFile(IN POBJECT_ATTRIBUTES ObjectAttributes,
2286 IN FILE_INFORMATION_CLASS FileInformationClass,
2287 IN ULONG FileInformationSize,
2288 OUT PVOID FileInformation)
2289 {
2290 NTSTATUS Status;
2291 KPROCESSOR_MODE AccessMode = ExGetPreviousMode();
2292 DUMMY_FILE_OBJECT LocalFileObject;
2293 FILE_NETWORK_OPEN_INFORMATION NetworkOpenInfo;
2294 HANDLE Handle;
2295 OPEN_PACKET OpenPacket;
2296 BOOLEAN IsBasic;
2297 PAGED_CODE();
2298 IOTRACE(IO_FILE_DEBUG, "Class: %lx\n", FileInformationClass);
2299
2300 /* Check if the caller was user mode */
2301 if (AccessMode != KernelMode)
2302 {
2303 /* Protect probe in SEH */
2304 _SEH2_TRY
2305 {
2306 /* Probe the buffer */
2307 ProbeForWrite(FileInformation, FileInformationSize, sizeof(ULONG));
2308 }
2309 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
2310 {
2311 /* Return the exception code */
2312 _SEH2_YIELD(return _SEH2_GetExceptionCode());
2313 }
2314 _SEH2_END;
2315 }
2316
2317 /* Check if this is a basic or full request */
2318 IsBasic = (FileInformationSize == sizeof(FILE_BASIC_INFORMATION));
2319
2320 /* Setup the Open Packet */
2321 RtlZeroMemory(&OpenPacket, sizeof(OPEN_PACKET));
2322 OpenPacket.Type = IO_TYPE_OPEN_PACKET;
2323 OpenPacket.Size = sizeof(OPEN_PACKET);
2324 OpenPacket.CreateOptions = FILE_OPEN_REPARSE_POINT;
2325 OpenPacket.ShareAccess = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
2326 OpenPacket.Disposition = FILE_OPEN;
2327 OpenPacket.BasicInformation = IsBasic ? FileInformation : NULL;
2328 OpenPacket.NetworkInformation = IsBasic ? &NetworkOpenInfo :
2329 (AccessMode != KernelMode) ?
2330 &NetworkOpenInfo : FileInformation;
2331 OpenPacket.QueryOnly = TRUE;
2332 OpenPacket.FullAttributes = IsBasic ? FALSE : TRUE;
2333 OpenPacket.LocalFileObject = &LocalFileObject;
2334
2335 /* Update the operation count */
2336 IopUpdateOperationCount(IopOtherTransfer);
2337
2338 /*
2339 * Attempt opening the file. This will call the I/O Parse Routine for
2340 * the File Object (IopParseDevice) which will use the dummy file obejct
2341 * send the IRP to its device object. Note that we have two statuses
2342 * to worry about: the Object Manager's status (in Status) and the I/O
2343 * status, which is in the Open Packet's Final Status, and determined
2344 * by the Parse Check member.
2345 */
2346 Status = ObOpenObjectByName(ObjectAttributes,
2347 NULL,
2348 AccessMode,
2349 NULL,
2350 FILE_READ_ATTRIBUTES,
2351 &OpenPacket,
2352 &Handle);
2353 if (OpenPacket.ParseCheck == FALSE)
2354 {
2355 /* Parse failed */
2356 DPRINT("IopQueryAttributesFile failed for '%wZ' with 0x%lx\n",
2357 ObjectAttributes->ObjectName, Status);
2358 return Status;
2359 }
2360 else
2361 {
2362 /* Use the Io status */
2363 Status = OpenPacket.FinalStatus;
2364 }
2365
2366 /* Check if we were succesful and this was user mode and a full query */
2367 if ((NT_SUCCESS(Status)) && (AccessMode != KernelMode) && !(IsBasic))
2368 {
2369 /* Enter SEH for copy */
2370 _SEH2_TRY
2371 {
2372 /* Copy the buffer back */
2373 RtlCopyMemory(FileInformation,
2374 &NetworkOpenInfo,
2375 FileInformationSize);
2376 }
2377 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
2378 {
2379 /* Get exception code */
2380 Status = _SEH2_GetExceptionCode();
2381 }
2382 _SEH2_END;
2383 }
2384
2385 /* Return status */
2386 return Status;
2387 }
2388
2389 NTSTATUS
2390 NTAPI
2391 IopAcquireFileObjectLock(
2392 _In_ PFILE_OBJECT FileObject,
2393 _In_ KPROCESSOR_MODE WaitMode,
2394 _In_ BOOLEAN Alertable,
2395 _Out_ PBOOLEAN LockFailed)
2396 {
2397 NTSTATUS Status;
2398
2399 PAGED_CODE();
2400
2401 InterlockedIncrement((PLONG)&FileObject->Waiters);
2402
2403 Status = STATUS_SUCCESS;
2404 do
2405 {
2406 if (!InterlockedExchange((PLONG)&FileObject->Busy, TRUE))
2407 {
2408 break;
2409 }
2410 Status = KeWaitForSingleObject(&FileObject->Lock,
2411 Executive,
2412 WaitMode,
2413 Alertable,
2414 NULL);
2415 } while (Status == STATUS_SUCCESS);
2416
2417 InterlockedDecrement((PLONG)&FileObject->Waiters);
2418 if (Status == STATUS_SUCCESS)
2419 {
2420 ObReferenceObject(FileObject);
2421 *LockFailed = FALSE;
2422 }
2423 else
2424 {
2425 if (!FileObject->Busy && FileObject->Waiters)
2426 {
2427 KeSetEvent(&FileObject->Lock, IO_NO_INCREMENT, FALSE);
2428 }
2429 *LockFailed = TRUE;
2430 }
2431
2432 return Status;
2433 }
2434
2435 PVOID
2436 NTAPI
2437 IoGetFileObjectFilterContext(IN PFILE_OBJECT FileObject)
2438 {
2439 if (FileObject->Flags & FO_FILE_OBJECT_HAS_EXTENSION)
2440 {
2441 UNIMPLEMENTED;
2442 /* FIXME: return NULL for the moment ~ */
2443 return NULL;
2444 }
2445
2446 return NULL;
2447 }
2448
2449 NTSTATUS
2450 NTAPI
2451 IoChangeFileObjectFilterContext(IN PFILE_OBJECT FileObject,
2452 IN PVOID FilterContext,
2453 IN BOOLEAN Define)
2454 {
2455 if (!(FileObject->Flags & FO_FILE_OBJECT_HAS_EXTENSION))
2456 {
2457 return STATUS_INVALID_PARAMETER;
2458 }
2459
2460 UNIMPLEMENTED;
2461
2462 return STATUS_NOT_IMPLEMENTED;
2463 }
2464
2465 NTSTATUS
2466 NTAPI
2467 IopCreateFile(OUT PHANDLE FileHandle,
2468 IN ACCESS_MASK DesiredAccess,
2469 IN POBJECT_ATTRIBUTES ObjectAttributes,
2470 OUT PIO_STATUS_BLOCK IoStatusBlock,
2471 IN PLARGE_INTEGER AllocationSize OPTIONAL,
2472 IN ULONG FileAttributes,
2473 IN ULONG ShareAccess,
2474 IN ULONG Disposition,
2475 IN ULONG CreateOptions,
2476 IN PVOID EaBuffer OPTIONAL,
2477 IN ULONG EaLength,
2478 IN CREATE_FILE_TYPE CreateFileType,
2479 IN PVOID ExtraCreateParameters OPTIONAL,
2480 IN ULONG Options,
2481 IN ULONG Flags,
2482 IN PDEVICE_OBJECT DeviceObject OPTIONAL)
2483 {
2484 KPROCESSOR_MODE AccessMode;
2485 HANDLE LocalHandle = 0;
2486 LARGE_INTEGER SafeAllocationSize;
2487 NTSTATUS Status = STATUS_SUCCESS;
2488 PNAMED_PIPE_CREATE_PARAMETERS NamedPipeCreateParameters;
2489 POPEN_PACKET OpenPacket;
2490 ULONG EaErrorOffset;
2491 PAGED_CODE();
2492
2493 IOTRACE(IO_FILE_DEBUG, "FileName: %wZ\n", ObjectAttributes->ObjectName);
2494
2495
2496 /* Check if we have no parameter checking to do */
2497 if (Options & IO_NO_PARAMETER_CHECKING)
2498 {
2499 /* Then force kernel-mode access to avoid checks */
2500 AccessMode = KernelMode;
2501 }
2502 else
2503 {
2504 /* Otherwise, use the actual mode */
2505 AccessMode = ExGetPreviousMode();
2506 }
2507
2508 /* Check if we need to do parameter checking */
2509 if ((AccessMode != KernelMode) || (Options & IO_CHECK_CREATE_PARAMETERS))
2510 {
2511 /* Validate parameters */
2512 if (FileAttributes & ~FILE_ATTRIBUTE_VALID_FLAGS)
2513 {
2514 DPRINT1("File Create 'FileAttributes' Parameter contains invalid flags!\n");
2515 return STATUS_INVALID_PARAMETER;
2516 }
2517
2518 if (ShareAccess & ~FILE_SHARE_VALID_FLAGS)
2519 {
2520 DPRINT1("File Create 'ShareAccess' Parameter contains invalid flags!\n");
2521 return STATUS_INVALID_PARAMETER;
2522 }
2523
2524 if (Disposition > FILE_MAXIMUM_DISPOSITION)
2525 {
2526 DPRINT1("File Create 'Disposition' Parameter is out of range!\n");
2527 return STATUS_INVALID_PARAMETER;
2528 }
2529
2530 if (CreateOptions & ~FILE_VALID_OPTION_FLAGS)
2531 {
2532 DPRINT1("File Create 'CreateOptions' parameter contains invalid flags!\n");
2533 return STATUS_INVALID_PARAMETER;
2534 }
2535
2536 if ((CreateOptions & (FILE_SYNCHRONOUS_IO_ALERT | FILE_SYNCHRONOUS_IO_NONALERT)) &&
2537 (!(DesiredAccess & SYNCHRONIZE)))
2538 {
2539 DPRINT1("File Create 'CreateOptions' parameter FILE_SYNCHRONOUS_IO_* requested, but 'DesiredAccess' does not have SYNCHRONIZE!\n");
2540 return STATUS_INVALID_PARAMETER;
2541 }
2542
2543 if ((CreateOptions & FILE_DELETE_ON_CLOSE) && (!(DesiredAccess & DELETE)))
2544 {
2545 DPRINT1("File Create 'CreateOptions' parameter FILE_DELETE_ON_CLOSE requested, but 'DesiredAccess' does not have DELETE!\n");
2546 return STATUS_INVALID_PARAMETER;
2547 }
2548
2549 if ((CreateOptions & (FILE_SYNCHRONOUS_IO_NONALERT | FILE_SYNCHRONOUS_IO_ALERT)) ==
2550 (FILE_SYNCHRONOUS_IO_NONALERT | FILE_SYNCHRONOUS_IO_ALERT))
2551 {
2552 DPRINT1("File Create 'FileAttributes' parameter both FILE_SYNCHRONOUS_IO_NONALERT and FILE_SYNCHRONOUS_IO_ALERT specified!\n");
2553 return STATUS_INVALID_PARAMETER;
2554 }
2555
2556 if ((CreateOptions & FILE_DIRECTORY_FILE) && !(CreateOptions & FILE_NON_DIRECTORY_FILE) &&
2557 (CreateOptions & ~(FILE_DIRECTORY_FILE |
2558 FILE_SYNCHRONOUS_IO_ALERT |
2559 FILE_SYNCHRONOUS_IO_NONALERT |
2560 FILE_WRITE_THROUGH |
2561 FILE_COMPLETE_IF_OPLOCKED |
2562 FILE_OPEN_FOR_BACKUP_INTENT |
2563 FILE_DELETE_ON_CLOSE |
2564 FILE_OPEN_FOR_FREE_SPACE_QUERY |
2565 FILE_OPEN_BY_FILE_ID |
2566 FILE_NO_COMPRESSION |
2567 FILE_OPEN_REPARSE_POINT)))
2568 {
2569 DPRINT1("File Create 'CreateOptions' Parameter has flags incompatible with FILE_DIRECTORY_FILE!\n");
2570 return STATUS_INVALID_PARAMETER;
2571 }
2572
2573 if ((CreateOptions & FILE_DIRECTORY_FILE) && !(CreateOptions & FILE_NON_DIRECTORY_FILE) &&
2574 (Disposition != FILE_CREATE) && (Disposition != FILE_OPEN) && (Disposition != FILE_OPEN_IF))
2575 {
2576 DPRINT1("File Create 'CreateOptions' Parameter FILE_DIRECTORY_FILE requested, but 'Disposition' is not FILE_CREATE/FILE_OPEN/FILE_OPEN_IF!\n");
2577 return STATUS_INVALID_PARAMETER;
2578 }
2579
2580 if ((CreateOptions & FILE_COMPLETE_IF_OPLOCKED) && (CreateOptions & FILE_RESERVE_OPFILTER))
2581 {
2582 DPRINT1("File Create 'CreateOptions' Parameter both FILE_COMPLETE_IF_OPLOCKED and FILE_RESERVE_OPFILTER specified!\n");
2583 return STATUS_INVALID_PARAMETER;
2584 }
2585
2586 if ((CreateOptions & FILE_NO_INTERMEDIATE_BUFFERING) && (DesiredAccess & FILE_APPEND_DATA))
2587 {
2588 DPRINT1("File Create 'CreateOptions' parameter FILE_NO_INTERMEDIATE_BUFFERING requested, but 'DesiredAccess' FILE_APPEND_DATA requires it!\n");
2589 return STATUS_INVALID_PARAMETER;
2590 }
2591
2592 /* Now check if this is a named pipe */
2593 if (CreateFileType == CreateFileTypeNamedPipe)
2594 {
2595 /* Make sure we have extra parameters */
2596 if (!ExtraCreateParameters)
2597 {
2598 DPRINT1("Invalid parameter: ExtraCreateParameters == 0!\n");
2599 return STATUS_INVALID_PARAMETER;
2600 }
2601
2602 /* Get the parameters and validate them */
2603 NamedPipeCreateParameters = ExtraCreateParameters;
2604 if ((NamedPipeCreateParameters->NamedPipeType > FILE_PIPE_MESSAGE_TYPE) ||
2605 (NamedPipeCreateParameters->ReadMode > FILE_PIPE_MESSAGE_MODE) ||
2606 (NamedPipeCreateParameters->CompletionMode > FILE_PIPE_COMPLETE_OPERATION) ||
2607 (ShareAccess & FILE_SHARE_DELETE) ||
2608 ((Disposition < FILE_OPEN) || (Disposition > FILE_OPEN_IF)) ||
2609 (CreateOptions & ~FILE_VALID_PIPE_OPTION_FLAGS))
2610 {
2611 /* Invalid named pipe create */
2612 DPRINT1("Invalid named pipe create\n");
2613 return STATUS_INVALID_PARAMETER;
2614 }
2615 }
2616 else if (CreateFileType == CreateFileTypeMailslot)
2617 {
2618 /* Make sure we have extra parameters */
2619 if (!ExtraCreateParameters)
2620 {
2621 DPRINT1("Invalid parameter: ExtraCreateParameters == 0!\n");
2622 return STATUS_INVALID_PARAMETER;
2623 }
2624
2625 /* Get the parameters and validate them */
2626 if ((ShareAccess & FILE_SHARE_DELETE) ||
2627 !(ShareAccess & ~FILE_SHARE_WRITE) ||
2628 (Disposition != FILE_CREATE) ||
2629 (CreateOptions & ~FILE_VALID_MAILSLOT_OPTION_FLAGS))
2630 {
2631 /* Invalid mailslot create */
2632 DPRINT1("Invalid mailslot create\n");
2633 return STATUS_INVALID_PARAMETER;
2634 }
2635 }
2636 }
2637
2638 /* Allocate the open packet */
2639 OpenPacket = ExAllocatePoolWithTag(NonPagedPool, sizeof(*OpenPacket), 'pOoI');
2640 if (!OpenPacket) return STATUS_INSUFFICIENT_RESOURCES;
2641 RtlZeroMemory(OpenPacket, sizeof(*OpenPacket));
2642
2643 /* Check if the call came from user mode */
2644 if (AccessMode != KernelMode)
2645 {
2646 _SEH2_TRY
2647 {
2648 /* Probe the output parameters */
2649 ProbeForWriteHandle(FileHandle);
2650 ProbeForWriteIoStatusBlock(IoStatusBlock);
2651
2652 /* Probe the allocation size if one was passed in */
2653 if (AllocationSize)
2654 {
2655 SafeAllocationSize = ProbeForReadLargeInteger(AllocationSize);
2656 }
2657 else
2658 {
2659 SafeAllocationSize.QuadPart = 0;
2660 }
2661
2662 /* Make sure it's valid */
2663 if (SafeAllocationSize.QuadPart < 0)
2664 {
2665 RtlRaiseStatus(STATUS_INVALID_PARAMETER);
2666 }
2667
2668 /* Check if EA was passed in */
2669 if ((EaBuffer) && (EaLength))
2670 {
2671 /* Probe it */
2672 ProbeForRead(EaBuffer, EaLength, sizeof(ULONG));
2673
2674 /* And marshall it */
2675 OpenPacket->EaBuffer = ExAllocatePoolWithTag(NonPagedPool,
2676 EaLength,
2677 TAG_EA);
2678 OpenPacket->EaLength = EaLength;
2679 RtlCopyMemory(OpenPacket->EaBuffer, EaBuffer, EaLength);
2680
2681 /* Validate the buffer */
2682 Status = IoCheckEaBufferValidity(OpenPacket->EaBuffer,
2683 EaLength,
2684 &EaErrorOffset);
2685 if (!NT_SUCCESS(Status))
2686 {
2687 /* Undo everything if it's invalid */
2688 DPRINT1("Invalid EA buffer\n");
2689 IoStatusBlock->Status = Status;
2690 IoStatusBlock->Information = EaErrorOffset;
2691 RtlRaiseStatus(Status);
2692 }
2693 }
2694 }
2695 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
2696 {
2697 /* Return the exception code */
2698 if (OpenPacket->EaBuffer != NULL) ExFreePool(OpenPacket->EaBuffer);
2699 ExFreePool(OpenPacket);
2700 _SEH2_YIELD(return _SEH2_GetExceptionCode());
2701 }
2702 _SEH2_END;
2703 }
2704 else
2705 {
2706 /* Check if this is a device attach */
2707 if (CreateOptions & IO_ATTACH_DEVICE_API)
2708 {
2709 /* Set the flag properly */
2710 Options |= IO_ATTACH_DEVICE;
2711 CreateOptions &= ~IO_ATTACH_DEVICE_API;
2712 }
2713
2714 /* Check if we have allocation size */
2715 if (AllocationSize)
2716 {
2717 /* Capture it */
2718 SafeAllocationSize = *AllocationSize;
2719 }
2720 else
2721 {
2722 /* Otherwise, no size */
2723 SafeAllocationSize.QuadPart = 0;
2724 }
2725
2726 /* Check if we have an EA packet */
2727 if ((EaBuffer) && (EaLength))
2728 {
2729 /* Allocate the kernel copy */
2730 OpenPacket->EaBuffer = ExAllocatePoolWithTag(NonPagedPool,
2731 EaLength,
2732 TAG_EA);
2733 if (!OpenPacket->EaBuffer)
2734 {
2735 ExFreePool(OpenPacket);
2736 DPRINT1("Failed to allocate open packet EA buffer\n");
2737 return STATUS_INSUFFICIENT_RESOURCES;
2738 }
2739
2740 /* Copy the data */
2741 OpenPacket->EaLength = EaLength;
2742 RtlCopyMemory(OpenPacket->EaBuffer, EaBuffer, EaLength);
2743
2744 /* Validate the buffer */
2745 Status = IoCheckEaBufferValidity(OpenPacket->EaBuffer,
2746 EaLength,
2747 &EaErrorOffset);
2748 if (!NT_SUCCESS(Status))
2749 {
2750 /* Undo everything if it's invalid */
2751 DPRINT1("Invalid EA buffer\n");
2752 ExFreePool(OpenPacket->EaBuffer);
2753 IoStatusBlock->Status = Status;
2754 IoStatusBlock->Information = EaErrorOffset;
2755 ExFreePool(OpenPacket);
2756 return Status;
2757 }
2758 }
2759 }
2760
2761 /* Setup the Open Packet */
2762 OpenPacket->Type = IO_TYPE_OPEN_PACKET;
2763 OpenPacket->Size = sizeof(*OpenPacket);
2764 OpenPacket->AllocationSize = SafeAllocationSize;
2765 OpenPacket->CreateOptions = CreateOptions;
2766 OpenPacket->FileAttributes = (USHORT)FileAttributes;
2767 OpenPacket->ShareAccess = (USHORT)ShareAccess;
2768 OpenPacket->Options = Options;
2769 OpenPacket->Disposition = Disposition;
2770 OpenPacket->CreateFileType = CreateFileType;
2771 OpenPacket->ExtraCreateParameters = ExtraCreateParameters;
2772 OpenPacket->InternalFlags = Flags;
2773 OpenPacket->TopDeviceObjectHint = DeviceObject;
2774
2775 /* Update the operation count */
2776 IopUpdateOperationCount(IopOtherTransfer);
2777
2778 /*
2779 * Attempt opening the file. This will call the I/O Parse Routine for
2780 * the File Object (IopParseDevice) which will create the object and
2781 * send the IRP to its device object. Note that we have two statuses
2782 * to worry about: the Object Manager's status (in Status) and the I/O
2783 * status, which is in the Open Packet's Final Status, and determined
2784 * by the Parse Check member.
2785 */
2786 Status = ObOpenObjectByName(ObjectAttributes,
2787 NULL,
2788 AccessMode,
2789 NULL,
2790 DesiredAccess,
2791 OpenPacket,
2792 &LocalHandle);
2793
2794 /* Free the EA Buffer */
2795 if (OpenPacket->EaBuffer) ExFreePool(OpenPacket->EaBuffer);
2796
2797 /* Now check for Ob or Io failure */
2798 if (!(NT_SUCCESS(Status)) || (OpenPacket->ParseCheck == FALSE))
2799 {
2800 /* Check if Ob thinks well went well */
2801 if (NT_SUCCESS(Status))
2802 {
2803 /*
2804 * Tell it otherwise. Because we didn't use an ObjectType,
2805 * it incorrectly returned us a handle to God knows what.
2806 */
2807 ZwClose(LocalHandle);
2808 Status = STATUS_OBJECT_TYPE_MISMATCH;
2809 }
2810
2811 /* Now check the Io status */
2812 if (!NT_SUCCESS(OpenPacket->FinalStatus))
2813 {
2814 /* Use this status instead of Ob's */
2815 Status = OpenPacket->FinalStatus;
2816
2817 /* Check if it was only a warning */
2818 if (NT_WARNING(Status))
2819 {
2820 /* Protect write with SEH */
2821 _SEH2_TRY
2822 {
2823 /* In this case, we copy the I/O Status back */
2824 IoStatusBlock->Information = OpenPacket->Information;
2825 IoStatusBlock->Status = OpenPacket->FinalStatus;
2826 }
2827 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
2828 {
2829 /* Get exception code */
2830 Status = _SEH2_GetExceptionCode();
2831 }
2832 _SEH2_END;
2833 }
2834 }
2835 else if ((OpenPacket->FileObject) && (OpenPacket->ParseCheck == FALSE))
2836 {
2837 /*
2838 * This can happen in the very bizarre case where the parse routine
2839 * actually executed more then once (due to a reparse) and ended
2840 * up failing after already having created the File Object.
2841 */
2842 if (OpenPacket->FileObject->FileName.Length)
2843 {
2844 /* It had a name, free it */
2845 ExFreePoolWithTag(OpenPacket->FileObject->FileName.Buffer, TAG_IO_NAME);
2846 }
2847
2848 /* Clear the device object to invalidate the FO, and dereference */
2849 OpenPacket->FileObject->DeviceObject = NULL;
2850 ObDereferenceObject(OpenPacket->FileObject);
2851 }
2852 }
2853 else
2854 {
2855 /* We reached success and have a valid file handle */
2856 OpenPacket->FileObject->Flags |= FO_HANDLE_CREATED;
2857 ASSERT(OpenPacket->FileObject->Type == IO_TYPE_FILE);
2858
2859 /* Enter SEH for write back */
2860 _SEH2_TRY
2861 {
2862 /* Write back the handle and I/O Status */
2863 *FileHandle = LocalHandle;
2864 IoStatusBlock->Information = OpenPacket->Information;
2865 IoStatusBlock->Status = OpenPacket->FinalStatus;
2866
2867 /* Get the Io status */
2868 Status = OpenPacket->FinalStatus;
2869 }
2870 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
2871 {
2872 /* Get the exception status */
2873 Status = _SEH2_GetExceptionCode();
2874 }
2875 _SEH2_END;
2876 }
2877
2878 /* Check if we were 100% successful */
2879 if ((OpenPacket->ParseCheck != FALSE) && (OpenPacket->FileObject))
2880 {
2881 /* Dereference the File Object */
2882 ObDereferenceObject(OpenPacket->FileObject);
2883 }
2884
2885 /* Return status */
2886 ExFreePool(OpenPacket);
2887 return Status;
2888 }
2889
2890 /* FUNCTIONS *****************************************************************/
2891
2892 /*
2893 * @unimplemented
2894 */
2895 NTSTATUS
2896 NTAPI
2897 IoCheckQuerySetFileInformation(IN FILE_INFORMATION_CLASS FileInformationClass,
2898 IN ULONG Length,
2899 IN BOOLEAN SetOperation)
2900 {
2901 UNIMPLEMENTED;
2902 return STATUS_NOT_IMPLEMENTED;
2903 }
2904
2905 /*
2906 * @unimplemented
2907 */
2908 NTSTATUS
2909 NTAPI
2910 IoCheckQuotaBufferValidity(IN PFILE_QUOTA_INFORMATION QuotaBuffer,
2911 IN ULONG QuotaLength,
2912 OUT PULONG ErrorOffset)
2913 {
2914 UNIMPLEMENTED;
2915 return STATUS_NOT_IMPLEMENTED;
2916 }
2917
2918 /*
2919 * @implemented
2920 */
2921 NTSTATUS
2922 NTAPI
2923 IoCreateFile(OUT PHANDLE FileHandle,
2924 IN ACCESS_MASK DesiredAccess,
2925 IN POBJECT_ATTRIBUTES ObjectAttributes,
2926 OUT PIO_STATUS_BLOCK IoStatusBlock,
2927 IN PLARGE_INTEGER AllocationSize OPTIONAL,
2928 IN ULONG FileAttributes,
2929 IN ULONG ShareAccess,
2930 IN ULONG Disposition,
2931 IN ULONG CreateOptions,
2932 IN PVOID EaBuffer OPTIONAL,
2933 IN ULONG EaLength,
2934 IN CREATE_FILE_TYPE CreateFileType,
2935 IN PVOID ExtraCreateParameters OPTIONAL,
2936 IN ULONG Options)
2937 {
2938 PAGED_CODE();
2939
2940 return IopCreateFile(FileHandle,
2941 DesiredAccess,
2942 ObjectAttributes,
2943 IoStatusBlock,
2944 AllocationSize,
2945 FileAttributes,
2946 ShareAccess,
2947 Disposition,
2948 CreateOptions,
2949 EaBuffer,
2950 EaLength,
2951 CreateFileType,
2952 ExtraCreateParameters,
2953 Options,
2954 0,
2955 NULL);
2956 }
2957
2958 /*
2959 * @unimplemented
2960 */
2961 NTSTATUS
2962 NTAPI
2963 IoCreateFileSpecifyDeviceObjectHint(OUT PHANDLE FileHandle,
2964 IN ACCESS_MASK DesiredAccess,
2965 IN POBJECT_ATTRIBUTES ObjectAttributes,
2966 OUT PIO_STATUS_BLOCK IoStatusBlock,
2967 IN PLARGE_INTEGER AllocationSize OPTIONAL,
2968 IN ULONG FileAttributes,
2969 IN ULONG ShareAccess,
2970 IN ULONG Disposition,
2971 IN ULONG CreateOptions,
2972 IN PVOID EaBuffer OPTIONAL,
2973 IN ULONG EaLength,
2974 IN CREATE_FILE_TYPE CreateFileType,
2975 IN PVOID ExtraCreateParameters OPTIONAL,
2976 IN ULONG Options,
2977 IN PVOID DeviceObject)
2978 {
2979 ULONG Flags = 0;
2980
2981 PAGED_CODE();
2982
2983 /* Check if we were passed a device to send the create request to*/
2984 if (DeviceObject)
2985 {
2986 /* We'll tag this request into a file object extension */
2987 Flags = (IOP_CREATE_FILE_OBJECT_EXTENSION | IOP_USE_TOP_LEVEL_DEVICE_HINT);
2988 }
2989
2990 return IopCreateFile(FileHandle,
2991 DesiredAccess,
2992 ObjectAttributes,
2993 IoStatusBlock,
2994 AllocationSize,
2995 FileAttributes,
2996 ShareAccess,
2997 Disposition,
2998 CreateOptions,
2999 EaBuffer,
3000 EaLength,
3001 CreateFileType,
3002 ExtraCreateParameters,
3003 Options | IO_NO_PARAMETER_CHECKING,
3004 Flags,
3005 DeviceObject);
3006 }
3007
3008 /*
3009 * @implemented
3010 */
3011 PFILE_OBJECT
3012 NTAPI
3013 IoCreateStreamFileObjectEx(IN PFILE_OBJECT FileObject OPTIONAL,
3014 IN PDEVICE_OBJECT DeviceObject OPTIONAL,
3015 OUT PHANDLE FileObjectHandle OPTIONAL)
3016 {
3017 PFILE_OBJECT CreatedFileObject;
3018 NTSTATUS Status;
3019 HANDLE FileHandle;
3020 OBJECT_ATTRIBUTES ObjectAttributes;
3021 PAGED_CODE();
3022 IOTRACE(IO_FILE_DEBUG, "FileObject: %p\n", FileObject);
3023
3024 /* Choose Device Object */
3025 if (FileObject) DeviceObject = FileObject->DeviceObject;
3026
3027 /* Reference the device object and initialize attributes */
3028 InterlockedIncrement(&DeviceObject->ReferenceCount);
3029 InitializeObjectAttributes(&ObjectAttributes, NULL, 0, NULL, NULL);
3030
3031 /* Create the File Object */
3032 Status = ObCreateObject(KernelMode,
3033 IoFileObjectType,
3034 &ObjectAttributes,
3035 KernelMode,
3036 NULL,
3037 sizeof(FILE_OBJECT),
3038 sizeof(FILE_OBJECT),
3039 0,
3040 (PVOID*)&CreatedFileObject);
3041 if (!NT_SUCCESS(Status))
3042 {
3043 /* Fail */
3044 IopDereferenceDeviceObject(DeviceObject, FALSE);
3045 ExRaiseStatus(Status);
3046 }
3047
3048 /* Set File Object Data */
3049 RtlZeroMemory(CreatedFileObject, sizeof(FILE_OBJECT));
3050 CreatedFileObject->DeviceObject = DeviceObject;
3051 CreatedFileObject->Type = IO_TYPE_FILE;
3052 CreatedFileObject->Size = sizeof(FILE_OBJECT);
3053 CreatedFileObject->Flags = FO_STREAM_FILE;
3054
3055 /* Initialize the wait event */
3056 KeInitializeEvent(&CreatedFileObject->Event, SynchronizationEvent, FALSE);
3057
3058 /* Insert it to create a handle for it */
3059 Status = ObInsertObject(CreatedFileObject,
3060 NULL,
3061 FILE_READ_DATA,
3062 1,
3063 (PVOID*)&CreatedFileObject,
3064 &FileHandle);
3065 if (!NT_SUCCESS(Status)) ExRaiseStatus(Status);
3066
3067 /* Set the handle created flag */
3068 CreatedFileObject->Flags |= FO_HANDLE_CREATED;
3069 ASSERT(CreatedFileObject->Type == IO_TYPE_FILE);
3070
3071 /* Check if we have a VPB */
3072 if (DeviceObject->Vpb)
3073 {
3074 /* Reference it */
3075 InterlockedIncrement((PLONG)&DeviceObject->Vpb->ReferenceCount);
3076 }
3077
3078 /* Check if the caller wants the handle */
3079 if (FileObjectHandle)
3080 {
3081 /* Return it */
3082 *FileObjectHandle = FileHandle;
3083 ObDereferenceObject(CreatedFileObject);
3084 }
3085 else
3086 {
3087 /* Otherwise, close it */
3088 ObCloseHandle(FileHandle, KernelMode);
3089 }
3090
3091 /* Return the file object */
3092 return CreatedFileObject;
3093 }
3094
3095 /*
3096 * @implemented
3097 */
3098 PFILE_OBJECT
3099 NTAPI
3100 IoCreateStreamFileObject(IN PFILE_OBJECT FileObject,
3101 IN PDEVICE_OBJECT DeviceObject)
3102 {
3103 /* Call the newer function */
3104 return IoCreateStreamFileObjectEx(FileObject, DeviceObject, NULL);
3105 }
3106
3107 /*
3108 * @implemented
3109 */
3110 PFILE_OBJECT
3111 NTAPI
3112 IoCreateStreamFileObjectLite(IN PFILE_OBJECT FileObject OPTIONAL,
3113 IN PDEVICE_OBJECT DeviceObject OPTIONAL)
3114 {
3115 PFILE_OBJECT CreatedFileObject;
3116 NTSTATUS Status;
3117 OBJECT_ATTRIBUTES ObjectAttributes;
3118 PAGED_CODE();
3119 IOTRACE(IO_FILE_DEBUG, "FileObject: %p\n", FileObject);
3120
3121 /* Choose Device Object */
3122 if (FileObject) DeviceObject = FileObject->DeviceObject;
3123
3124 /* Reference the device object and initialize attributes */
3125 InterlockedIncrement(&DeviceObject->ReferenceCount);
3126 InitializeObjectAttributes(&ObjectAttributes, NULL, 0, NULL, NULL);
3127
3128 /* Create the File Object */
3129 Status = ObCreateObject(KernelMode,
3130 IoFileObjectType,
3131 &ObjectAttributes,
3132 KernelMode,
3133 NULL,
3134 sizeof(FILE_OBJECT),
3135 sizeof(FILE_OBJECT),
3136 0,
3137 (PVOID*)&CreatedFileObject);
3138 if (!NT_SUCCESS(Status))
3139 {
3140 /* Fail */
3141 IopDereferenceDeviceObject(DeviceObject, FALSE);
3142 ExRaiseStatus(Status);
3143 }
3144
3145 /* Set File Object Data */
3146 RtlZeroMemory(CreatedFileObject, sizeof(FILE_OBJECT));
3147 CreatedFileObject->DeviceObject = DeviceObject;
3148 CreatedFileObject->Type = IO_TYPE_FILE;
3149 CreatedFileObject->Size = sizeof(FILE_OBJECT);
3150 CreatedFileObject->Flags = FO_STREAM_FILE;
3151
3152 /* Initialize the wait event */
3153 KeInitializeEvent(&CreatedFileObject->Event, SynchronizationEvent, FALSE);
3154
3155 /* Destroy create information */
3156 ObFreeObjectCreateInfoBuffer(OBJECT_TO_OBJECT_HEADER(CreatedFileObject)->
3157 ObjectCreateInfo);
3158 OBJECT_TO_OBJECT_HEADER(CreatedFileObject)->ObjectCreateInfo = NULL;
3159
3160 /* Set the handle created flag */
3161 CreatedFileObject->Flags |= FO_HANDLE_CREATED;
3162 ASSERT(CreatedFileObject->Type == IO_TYPE_FILE);
3163
3164 /* Check if we have a VPB */
3165 if (DeviceObject->Vpb)
3166 {
3167 /* Reference it */
3168 InterlockedIncrement((PLONG)&DeviceObject->Vpb->ReferenceCount);
3169 }
3170
3171 /* Return the file object */
3172 return CreatedFileObject;
3173 }
3174
3175 /*
3176 * @implemented
3177 */
3178 PGENERIC_MAPPING
3179 NTAPI
3180 IoGetFileObjectGenericMapping(VOID)
3181 {
3182 /* Return the mapping */
3183 return &IopFileMapping;
3184 }
3185
3186 /*
3187 * @implemented
3188 */
3189 BOOLEAN
3190 NTAPI
3191 IoIsFileOriginRemote(IN PFILE_OBJECT FileObject)
3192 {
3193 /* Return the flag status */
3194 return FileObject->Flags & FO_REMOTE_ORIGIN ? TRUE : FALSE;
3195 }
3196
3197 /*
3198 * @implemented
3199 */
3200 BOOLEAN
3201 NTAPI
3202 IoFastQueryNetworkAttributes(IN POBJECT_ATTRIBUTES ObjectAttributes,
3203 IN ACCESS_MASK DesiredAccess,
3204 IN ULONG OpenOptions,
3205 OUT PIO_STATUS_BLOCK IoStatus,
3206 OUT PFILE_NETWORK_OPEN_INFORMATION Buffer)
3207 {
3208 NTSTATUS Status;
3209 DUMMY_FILE_OBJECT LocalFileObject;
3210 HANDLE Handle;
3211 OPEN_PACKET OpenPacket;
3212 PAGED_CODE();
3213 IOTRACE(IO_FILE_DEBUG, "FileName: %wZ\n", ObjectAttributes->ObjectName);
3214
3215 /* Setup the Open Packet */
3216 RtlZeroMemory(&OpenPacket, sizeof(OPEN_PACKET));
3217 OpenPacket.Type = IO_TYPE_OPEN_PACKET;
3218 OpenPacket.Size = sizeof(OPEN_PACKET);
3219 OpenPacket.CreateOptions = OpenOptions | FILE_OPEN_REPARSE_POINT;
3220 OpenPacket.ShareAccess = FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE;
3221 OpenPacket.Options = IO_FORCE_ACCESS_CHECK;
3222 OpenPacket.Disposition = FILE_OPEN;
3223 OpenPacket.NetworkInformation = Buffer;
3224 OpenPacket.QueryOnly = TRUE;
3225 OpenPacket.FullAttributes = TRUE;
3226 OpenPacket.LocalFileObject = &LocalFileObject;
3227
3228 /*
3229 * Attempt opening the file. This will call the I/O Parse Routine for
3230 * the File Object (IopParseDevice) which will use the dummy file obejct
3231 * send the IRP to its device object. Note that we have two statuses
3232 * to worry about: the Object Manager's status (in Status) and the I/O
3233 * status, which is in the Open Packet's Final Status, and determined
3234 * by the Parse Check member.
3235 */
3236 Status = ObOpenObjectByName(ObjectAttributes,
3237 NULL,
3238 KernelMode,
3239 NULL,
3240 DesiredAccess,
3241 &OpenPacket,
3242 &Handle);
3243 if (OpenPacket.ParseCheck == FALSE)
3244 {
3245 /* Parse failed */
3246 IoStatus->Status = Status;
3247 }
3248 else
3249 {
3250 /* Use the Io status */
3251 IoStatus->Status = OpenPacket.FinalStatus;
3252 IoStatus->Information = OpenPacket.Information;
3253 }
3254
3255 /* Return success */
3256 return TRUE;
3257 }
3258
3259 /*
3260 * @implemented
3261 */
3262 VOID
3263 NTAPI
3264 IoUpdateShareAccess(IN PFILE_OBJECT FileObject,
3265 OUT PSHARE_ACCESS ShareAccess)
3266 {
3267 PAGED_CODE();
3268
3269 /* Check if the file has an extension */
3270 if (FileObject->Flags & FO_FILE_OBJECT_HAS_EXTENSION)
3271 {
3272 /* Check if caller specified to ignore access checks */
3273 //if (FileObject->FoExtFlags & IO_IGNORE_SHARE_ACCESS_CHECK)
3274 {
3275 /* Don't update share access */
3276 return;
3277 }
3278 }
3279
3280 /* Otherwise, check if there's any access present */
3281 if ((FileObject->ReadAccess) ||
3282 (FileObject->WriteAccess) ||
3283 (FileObject->DeleteAccess))
3284 {
3285 /* Increase the open count */
3286 ShareAccess->OpenCount++;
3287
3288 /* Add new share access */
3289 ShareAccess->Readers += FileObject->ReadAccess;
3290 ShareAccess->Writers += FileObject->WriteAccess;
3291 ShareAccess->Deleters += FileObject->DeleteAccess;
3292 ShareAccess->SharedRead += FileObject->SharedRead;
3293 ShareAccess->SharedWrite += FileObject->SharedWrite;
3294 ShareAccess->SharedDelete += FileObject->SharedDelete;
3295 }
3296 }
3297
3298 /*
3299 * @implemented
3300 */
3301 NTSTATUS
3302 NTAPI
3303 IoCheckShareAccess(IN ACCESS_MASK DesiredAccess,
3304 IN ULONG DesiredShareAccess,
3305 IN PFILE_OBJECT FileObject,
3306 IN PSHARE_ACCESS ShareAccess,
3307 IN BOOLEAN Update)
3308 {
3309 BOOLEAN ReadAccess;
3310 BOOLEAN WriteAccess;
3311 BOOLEAN DeleteAccess;
3312 BOOLEAN SharedRead;
3313 BOOLEAN SharedWrite;
3314 BOOLEAN SharedDelete;
3315 PAGED_CODE();
3316
3317 /* Get access masks */
3318 ReadAccess = (DesiredAccess & (FILE_READ_DATA | FILE_EXECUTE)) != 0;
3319 WriteAccess = (DesiredAccess & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0;
3320 DeleteAccess = (DesiredAccess & DELETE) != 0;
3321
3322 /* Set them in the file object */
3323 FileObject->ReadAccess = ReadAccess;
3324 FileObject->WriteAccess = WriteAccess;
3325 FileObject->DeleteAccess = DeleteAccess;
3326
3327 /* Check if the file has an extension */
3328 if (FileObject->Flags & FO_FILE_OBJECT_HAS_EXTENSION)
3329 {
3330 /* Check if caller specified to ignore access checks */
3331 //if (FileObject->FoExtFlags & IO_IGNORE_SHARE_ACCESS_CHECK)
3332 {
3333 /* Don't check share access */
3334 return STATUS_SUCCESS;
3335 }
3336 }
3337
3338 /* Check if we have any access */
3339 if ((ReadAccess) || (WriteAccess) || (DeleteAccess))
3340 {
3341 /* Get shared access masks */
3342 SharedRead = (DesiredShareAccess & FILE_SHARE_READ) != 0;
3343 SharedWrite = (DesiredShareAccess & FILE_SHARE_WRITE) != 0;
3344 SharedDelete = (DesiredShareAccess & FILE_SHARE_DELETE) != 0;
3345
3346 /* Set them */
3347 FileObject->SharedRead = SharedRead;
3348 FileObject->SharedWrite = SharedWrite;
3349 FileObject->SharedDelete = SharedDelete;
3350
3351 /* Check if the shared access is violated */
3352 if ((ReadAccess &&
3353 (ShareAccess->SharedRead < ShareAccess->OpenCount)) ||
3354 (WriteAccess &&
3355 (ShareAccess->SharedWrite < ShareAccess->OpenCount)) ||
3356 (DeleteAccess &&
3357 (ShareAccess->SharedDelete < ShareAccess->OpenCount)) ||
3358 ((ShareAccess->Readers != 0) && !SharedRead) ||
3359 ((ShareAccess->Writers != 0) && !SharedWrite) ||
3360 ((ShareAccess->Deleters != 0) && !SharedDelete))
3361 {
3362 /* Sharing violation, fail */
3363 return STATUS_SHARING_VIOLATION;
3364 }
3365
3366 /* It's not, check if caller wants us to update it */
3367 if (Update)
3368 {
3369 /* Increase open count */
3370 ShareAccess->OpenCount++;
3371
3372 /* Update shared access */
3373 ShareAccess->Readers += ReadAccess;
3374 ShareAccess->Writers += WriteAccess;
3375 ShareAccess->Deleters += DeleteAccess;
3376 ShareAccess->SharedRead += SharedRead;
3377 ShareAccess->SharedWrite += SharedWrite;
3378 ShareAccess->SharedDelete += SharedDelete;
3379 }
3380 }
3381
3382 /* Validation successful */
3383 return STATUS_SUCCESS;
3384 }
3385
3386 /*
3387 * @implemented
3388 */
3389 VOID
3390 NTAPI
3391 IoRemoveShareAccess(IN PFILE_OBJECT FileObject,
3392 IN PSHARE_ACCESS ShareAccess)
3393 {
3394 PAGED_CODE();
3395
3396 /* Check if the file has an extension */
3397 if (FileObject->Flags & FO_FILE_OBJECT_HAS_EXTENSION)
3398 {
3399 /* Check if caller specified to ignore access checks */
3400 //if (FileObject->FoExtFlags & IO_IGNORE_SHARE_ACCESS_CHECK)
3401 {
3402 /* Don't update share access */
3403 return;
3404 }
3405 }
3406
3407 /* Otherwise, check if there's any access present */
3408 if ((FileObject->ReadAccess) ||
3409 (FileObject->WriteAccess) ||
3410 (FileObject->DeleteAccess))
3411 {
3412 /* Decrement the open count */
3413 ShareAccess->OpenCount--;
3414
3415 /* Remove share access */
3416 ShareAccess->Readers -= FileObject->ReadAccess;
3417 ShareAccess->Writers -= FileObject->WriteAccess;
3418 ShareAccess->Deleters -= FileObject->DeleteAccess;
3419 ShareAccess->SharedRead -= FileObject->SharedRead;
3420 ShareAccess->SharedWrite -= FileObject->SharedWrite;
3421 ShareAccess->SharedDelete -= FileObject->SharedDelete;
3422 }
3423 }
3424
3425 /*
3426 * @implemented
3427 */
3428 VOID
3429 NTAPI
3430 IoSetShareAccess(IN ACCESS_MASK DesiredAccess,
3431 IN ULONG DesiredShareAccess,
3432 IN PFILE_OBJECT FileObject,
3433 OUT PSHARE_ACCESS ShareAccess)
3434 {
3435 BOOLEAN ReadAccess;
3436 BOOLEAN WriteAccess;
3437 BOOLEAN DeleteAccess;
3438 BOOLEAN SharedRead;
3439 BOOLEAN SharedWrite;
3440 BOOLEAN SharedDelete;
3441 BOOLEAN Update = TRUE;
3442 PAGED_CODE();
3443
3444 ReadAccess = (DesiredAccess & (FILE_READ_DATA | FILE_EXECUTE)) != 0;
3445 WriteAccess = (DesiredAccess & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0;
3446 DeleteAccess = (DesiredAccess & DELETE) != 0;
3447
3448 /* Check if the file has an extension */
3449 if (FileObject->Flags & FO_FILE_OBJECT_HAS_EXTENSION)
3450 {
3451 /* Check if caller specified to ignore access checks */
3452 //if (FileObject->FoExtFlags & IO_IGNORE_SHARE_ACCESS_CHECK)
3453 {
3454 /* Don't update share access */
3455 Update = FALSE;
3456 }
3457 }
3458
3459 /* Update basic access */
3460 FileObject->ReadAccess = ReadAccess;
3461 FileObject->WriteAccess = WriteAccess;
3462 FileObject->DeleteAccess = DeleteAccess;
3463
3464 /* Check if we have no access as all */
3465 if (!(ReadAccess) && !(WriteAccess) && !(DeleteAccess))
3466 {
3467 /* Check if we need to update the structure */
3468 if (!Update) return;
3469
3470 /* Otherwise, clear data */
3471 ShareAccess->OpenCount = 0;
3472 ShareAccess->Readers = 0;
3473 ShareAccess->Writers = 0;
3474 ShareAccess->Deleters = 0;
3475 ShareAccess->SharedRead = 0;
3476 ShareAccess->SharedWrite = 0;
3477 ShareAccess->SharedDelete = 0;
3478 }
3479 else
3480 {
3481 /* Calculate shared access */
3482 SharedRead = (DesiredShareAccess & FILE_SHARE_READ) != 0;
3483 SharedWrite = (DesiredShareAccess & FILE_SHARE_WRITE) != 0;
3484 SharedDelete = (DesiredShareAccess & FILE_SHARE_DELETE) != 0;
3485
3486 /* Set it in the FO */
3487 FileObject->SharedRead = SharedRead;
3488 FileObject->SharedWrite = SharedWrite;
3489 FileObject->SharedDelete = SharedDelete;
3490
3491 /* Check if we need to update the structure */
3492 if (!Update) return;
3493
3494 /* Otherwise, set data */
3495 ShareAccess->OpenCount = 1;
3496 ShareAccess->Readers = ReadAccess;
3497 ShareAccess->Writers = WriteAccess;
3498 ShareAccess->Deleters = DeleteAccess;
3499 ShareAccess->SharedRead = SharedRead;
3500 ShareAccess->SharedWrite = SharedWrite;
3501 ShareAccess->SharedDelete = SharedDelete;
3502 }
3503 }
3504
3505 /*
3506 * @implemented
3507 */
3508 VOID
3509 NTAPI
3510 IoCancelFileOpen(IN PDEVICE_OBJECT DeviceObject,
3511 IN PFILE_OBJECT FileObject)
3512 {
3513 PIRP Irp;
3514 KEVENT Event;
3515 KIRQL OldIrql;
3516 NTSTATUS Status;
3517 PIO_STACK_LOCATION Stack;
3518
3519 /* Check if handles were already created for the
3520 * open file. If so, that's over.
3521 */
3522 if (FileObject->Flags & FO_HANDLE_CREATED)
3523 KeBugCheckEx(INVALID_CANCEL_OF_FILE_OPEN,
3524 (ULONG_PTR)FileObject,
3525 (ULONG_PTR)DeviceObject, 0, 0);
3526
3527 /* Reset the events */
3528 KeInitializeEvent(&Event, SynchronizationEvent, FALSE);
3529 KeClearEvent(&FileObject->Event);
3530
3531 /* Allocate the IRP we'll use */
3532 Irp = IopAllocateIrpMustSucceed(DeviceObject->StackSize);
3533 /* Properly set it */
3534 Irp->Tail.Overlay.Thread = PsGetCurrentThread();
3535 Irp->UserEvent = &Event;
3536 Irp->UserIosb = &Irp->IoStatus;
3537 Irp->Overlay.AsynchronousParameters.UserApcRoutine = NULL;
3538 Irp->Tail.Overlay.OriginalFileObject = FileObject;
3539 Irp->RequestorMode = KernelMode;
3540 Irp->Flags = IRP_CLOSE_OPERATION | IRP_SYNCHRONOUS_API;
3541
3542 Stack = IoGetNextIrpStackLocation(Irp);
3543 Stack->MajorFunction = IRP_MJ_CLEANUP;
3544 Stack->FileObject = FileObject;
3545
3546 /* Put on top of IRPs list of the thread */
3547 IopQueueIrpToThread(Irp);
3548
3549 /* Call the driver */
3550 Status = IoCallDriver(DeviceObject, Irp);
3551 if (Status == STATUS_PENDING)
3552 {
3553 KeWaitForSingleObject(&Event, UserRequest,
3554 KernelMode, FALSE, NULL);
3555 }
3556
3557 /* Remove from IRPs list */
3558 KeRaiseIrql(APC_LEVEL, &OldIrql);
3559 IopUnQueueIrpFromThread(Irp);
3560 KeLowerIrql(OldIrql);
3561
3562 /* Free the IRP */
3563 IoFreeIrp(Irp);
3564
3565 /* Clear the event */
3566 KeClearEvent(&FileObject->Event);
3567 /* And finally, mark the open operation as canceled */
3568 FileObject->Flags |= FO_FILE_OPEN_CANCELLED;
3569 }
3570
3571 /*
3572 * @implemented
3573 */
3574 NTSTATUS
3575 NTAPI
3576 IoQueryFileDosDeviceName(IN PFILE_OBJECT FileObject,
3577 OUT POBJECT_NAME_INFORMATION *ObjectNameInformation)
3578 {
3579 NTSTATUS Status;
3580 ULONG Length, ReturnLength;
3581 POBJECT_NAME_INFORMATION LocalInfo;
3582
3583 /* Start with a buffer length of 200 */
3584 ReturnLength = 200;
3585 /*
3586 * We'll loop until query works.
3587 * We will use returned length for next loop
3588 * iteration, trying to have a big enough buffer.
3589 */
3590 for (Length = 200; ; Length = ReturnLength)
3591 {
3592 /* Allocate our work buffer */
3593 LocalInfo = ExAllocatePoolWithTag(PagedPool, Length, 'nDoI');
3594 if (LocalInfo == NULL)
3595 {
3596 return STATUS_INSUFFICIENT_RESOURCES;
3597 }
3598
3599 /* Query the DOS name */
3600 Status = IopQueryNameInternal(FileObject,
3601 TRUE,
3602 TRUE,
3603 LocalInfo,
3604 Length,
3605 &ReturnLength,
3606 KernelMode);
3607 /* If it succeed, nothing more to do */
3608 if (Status == STATUS_SUCCESS)
3609 {
3610 break;
3611 }
3612
3613 /* Otherwise, prepare for re-allocation */
3614 ExFreePoolWithTag(LocalInfo, 'nDoI');
3615
3616 /*
3617 * If we failed because of something else
3618 * than memory, simply stop and fail here
3619 */
3620 if (Status != STATUS_BUFFER_OVERFLOW)
3621 {
3622 return Status;
3623 }
3624 }
3625
3626 /* Success case here: return our buffer */
3627 *ObjectNameInformation = LocalInfo;
3628 return STATUS_SUCCESS;
3629 }
3630
3631 /*
3632 * @implemented
3633 */
3634 NTSTATUS
3635 NTAPI
3636 IoSetFileOrigin(IN PFILE_OBJECT FileObject,
3637 IN BOOLEAN Remote)
3638 {
3639 NTSTATUS Status = STATUS_SUCCESS;
3640 BOOLEAN FlagSet;
3641
3642 /* Get the flag status */
3643 FlagSet = FileObject->Flags & FO_REMOTE_ORIGIN ? TRUE : FALSE;
3644
3645 /* Don't set the flag if it was set already, and don't remove it if it wasn't set */
3646 if (Remote && !FlagSet)
3647 {
3648 /* Set the flag */
3649 FileObject->Flags |= FO_REMOTE_ORIGIN;
3650 }
3651 else if (!Remote && FlagSet)
3652 {
3653 /* Remove the flag */
3654 FileObject->Flags &= ~FO_REMOTE_ORIGIN;
3655 }
3656 else
3657 {
3658 /* Fail */
3659 Status = STATUS_INVALID_PARAMETER_MIX;
3660 }
3661
3662 /* Return status */
3663 return Status;
3664 }
3665
3666 /*
3667 * @implemented
3668 */
3669 NTSTATUS
3670 NTAPI
3671 NtCreateFile(PHANDLE FileHandle,
3672 ACCESS_MASK DesiredAccess,
3673 POBJECT_ATTRIBUTES ObjectAttributes,
3674 PIO_STATUS_BLOCK IoStatusBlock,
3675 PLARGE_INTEGER AllocateSize,
3676 ULONG FileAttributes,
3677 ULONG ShareAccess,
3678 ULONG CreateDisposition,
3679 ULONG CreateOptions,
3680 PVOID EaBuffer,
3681 ULONG EaLength)
3682 {
3683 /* Call the I/O Function */
3684 return IoCreateFile(FileHandle,
3685 DesiredAccess,
3686 ObjectAttributes,
3687 IoStatusBlock,
3688 AllocateSize,
3689 FileAttributes,
3690 ShareAccess,
3691 CreateDisposition,
3692 CreateOptions,
3693 EaBuffer,
3694 EaLength,
3695 CreateFileTypeNone,
3696 NULL,
3697 0);
3698 }
3699
3700 NTSTATUS
3701 NTAPI
3702 NtCreateMailslotFile(OUT PHANDLE FileHandle,
3703 IN ACCESS_MASK DesiredAccess,
3704 IN POBJECT_ATTRIBUTES ObjectAttributes,
3705 OUT PIO_STATUS_BLOCK IoStatusBlock,
3706 IN ULONG CreateOptions,
3707 IN ULONG MailslotQuota,
3708 IN ULONG MaxMessageSize,
3709 IN PLARGE_INTEGER TimeOut)
3710 {
3711 MAILSLOT_CREATE_PARAMETERS Buffer;
3712 PAGED_CODE();
3713
3714 /* Check for Timeout */
3715 if (TimeOut)
3716 {
3717 /* check if the call came from user mode */
3718 if (KeGetPreviousMode() != KernelMode)
3719 {
3720 /* Enter SEH for Probe */
3721 _SEH2_TRY
3722 {
3723 /* Probe the timeout */
3724 Buffer.ReadTimeout = ProbeForReadLargeInteger(TimeOut);
3725 }
3726 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
3727 {
3728 /* Return the exception code */
3729 _SEH2_YIELD(return _SEH2_GetExceptionCode());
3730 }
3731 _SEH2_END;
3732 }
3733 else
3734 {
3735 /* Otherwise, capture directly */
3736 Buffer.ReadTimeout = *TimeOut;
3737 }
3738
3739 /* Set the correct setting */
3740 Buffer.TimeoutSpecified = TRUE;
3741 }
3742 else
3743 {
3744 /* Tell the FSD we don't have a timeout */
3745 Buffer.TimeoutSpecified = FALSE;
3746 }
3747
3748 /* Set Settings */
3749 Buffer.MailslotQuota = MailslotQuota;
3750 Buffer.MaximumMessageSize = MaxMessageSize;
3751
3752 /* Call I/O */
3753 return IoCreateFile(FileHandle,
3754 DesiredAccess,
3755 ObjectAttributes,
3756 IoStatusBlock,
3757 NULL,
3758 0,
3759 FILE_SHARE_READ | FILE_SHARE_WRITE,
3760 FILE_CREATE,
3761 CreateOptions,
3762 NULL,
3763 0,
3764 CreateFileTypeMailslot,
3765 (PVOID)&Buffer,
3766 0);
3767 }
3768
3769 NTSTATUS
3770 NTAPI
3771 NtCreateNamedPipeFile(OUT PHANDLE FileHandle,
3772 IN ACCESS_MASK DesiredAccess,
3773 IN POBJECT_ATTRIBUTES ObjectAttributes,
3774 OUT PIO_STATUS_BLOCK IoStatusBlock,
3775 IN ULONG ShareAccess,
3776 IN ULONG CreateDisposition,
3777 IN ULONG CreateOptions,
3778 IN ULONG NamedPipeType,
3779 IN ULONG ReadMode,
3780 IN ULONG CompletionMode,
3781 IN ULONG MaximumInstances,
3782 IN ULONG InboundQuota,
3783 IN ULONG OutboundQuota,
3784 IN PLARGE_INTEGER DefaultTimeout)
3785 {
3786 NAMED_PIPE_CREATE_PARAMETERS Buffer;
3787 PAGED_CODE();
3788
3789 /* Check for Timeout */
3790 if (DefaultTimeout)
3791 {
3792 /* check if the call came from user mode */
3793 if (KeGetPreviousMode() != KernelMode)
3794 {
3795 /* Enter SEH for Probe */
3796 _SEH2_TRY
3797 {
3798 /* Probe the timeout */
3799 Buffer.DefaultTimeout =
3800 ProbeForReadLargeInteger(DefaultTimeout);
3801 }
3802 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
3803 {
3804 /* Return the exception code */
3805 _SEH2_YIELD(return _SEH2_GetExceptionCode());
3806 }
3807 _SEH2_END;
3808 }
3809 else
3810 {
3811 /* Otherwise, capture directly */
3812 Buffer.DefaultTimeout = *DefaultTimeout;
3813 }
3814
3815 /* Set the correct setting */
3816 Buffer.TimeoutSpecified = TRUE;
3817 }
3818 else
3819 {
3820 /* Tell the FSD we don't have a timeout */
3821 Buffer.TimeoutSpecified = FALSE;
3822 }
3823
3824 /* Set Settings */
3825 Buffer.NamedPipeType = NamedPipeType;
3826 Buffer.ReadMode = ReadMode;
3827 Buffer.CompletionMode = CompletionMode;
3828 Buffer.MaximumInstances = MaximumInstances;
3829 Buffer.InboundQuota = InboundQuota;
3830 Buffer.OutboundQuota = OutboundQuota;
3831
3832 /* Call I/O */
3833 return IoCreateFile(FileHandle,
3834 DesiredAccess,
3835 ObjectAttributes,
3836 IoStatusBlock,
3837 NULL,
3838 0,
3839 ShareAccess,
3840 CreateDisposition,
3841 CreateOptions,
3842 NULL,
3843 0,
3844 CreateFileTypeNamedPipe,
3845 (PVOID)&Buffer,
3846 0);
3847 }
3848
3849 NTSTATUS
3850 NTAPI
3851 NtFlushWriteBuffer(VOID)
3852 {
3853 PAGED_CODE();
3854
3855 /* Call the kernel */
3856 KeFlushWriteBuffer();
3857 return STATUS_SUCCESS;
3858 }
3859
3860 /*
3861 * @implemented
3862 */
3863 NTSTATUS
3864 NTAPI
3865 NtOpenFile(OUT PHANDLE FileHandle,
3866 IN ACCESS_MASK DesiredAccess,
3867 IN POBJECT_ATTRIBUTES ObjectAttributes,
3868 OUT PIO_STATUS_BLOCK IoStatusBlock,
3869 IN ULONG ShareAccess,
3870 IN ULONG OpenOptions)
3871 {
3872 /* Call the I/O Function */
3873 return IoCreateFile(FileHandle,
3874 DesiredAccess,
3875 ObjectAttributes,
3876 IoStatusBlock,
3877 NULL,
3878 0,
3879 ShareAccess,
3880 FILE_OPEN,
3881 OpenOptions,
3882 NULL,
3883 0,
3884 CreateFileTypeNone,
3885 NULL,
3886 0);
3887 }
3888
3889 NTSTATUS
3890 NTAPI
3891 NtQueryAttributesFile(IN POBJECT_ATTRIBUTES ObjectAttributes,
3892 OUT PFILE_BASIC_INFORMATION FileInformation)
3893 {
3894 /* Call the internal helper API */
3895 return IopQueryAttributesFile(ObjectAttributes,
3896 FileBasicInformation,
3897 sizeof(FILE_BASIC_INFORMATION),
3898 FileInformation);
3899 }
3900
3901 NTSTATUS
3902 NTAPI
3903 NtQueryFullAttributesFile(IN POBJECT_ATTRIBUTES ObjectAttributes,
3904 OUT PFILE_NETWORK_OPEN_INFORMATION FileInformation)
3905 {
3906 /* Call the internal helper API */
3907 return IopQueryAttributesFile(ObjectAttributes,
3908 FileNetworkOpenInformation,
3909 sizeof(FILE_NETWORK_OPEN_INFORMATION),
3910 FileInformation);
3911 }
3912
3913 /**
3914 * @name NtCancelIoFile
3915 *
3916 * Cancel all pending I/O operations in the current thread for specified
3917 * file object.
3918 *
3919 * @param FileHandle
3920 * Handle to file object to cancel requests for. No specific
3921 * access rights are needed.
3922 * @param IoStatusBlock
3923 * Pointer to status block which is filled with final completition
3924 * status on successful return.
3925 *
3926 * @return Status.
3927 *
3928 * @implemented
3929 */
3930 NTSTATUS
3931 NTAPI
3932 NtCancelIoFile(IN HANDLE FileHandle,
3933 OUT PIO_STATUS_BLOCK IoStatusBlock)
3934 {
3935 PFILE_OBJECT FileObject;
3936 PETHREAD Thread;
3937 PIRP Irp;
3938 KIRQL OldIrql;
3939 BOOLEAN OurIrpsInList = FALSE;
3940 LARGE_INTEGER Interval;
3941 KPROCESSOR_MODE PreviousMode = KeGetPreviousMode();
3942 NTSTATUS Status;
3943 PLIST_ENTRY ListHead, NextEntry;
3944 PAGED_CODE();
3945 IOTRACE(IO_API_DEBUG, "FileHandle: %p\n", FileHandle);
3946
3947 /* Check the previous mode */
3948 if (PreviousMode != KernelMode)
3949 {
3950 /* Enter SEH for probing */
3951 _SEH2_TRY
3952 {
3953 /* Probe the I/O Status Block */
3954 ProbeForWriteIoStatusBlock(IoStatusBlock);
3955 }
3956 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
3957 {
3958 /* Return the exception code */
3959 _SEH2_YIELD(return _SEH2_GetExceptionCode());
3960 }
3961 _SEH2_END;
3962 }
3963
3964 /* Reference the file object */
3965 Status = ObReferenceObjectByHandle(FileHandle,
3966 0,
3967 IoFileObjectType,
3968 PreviousMode,
3969 (PVOID*)&FileObject,
3970 NULL);
3971 if (!NT_SUCCESS(Status)) return Status;
3972
3973 /* IRP cancellations are synchronized at APC_LEVEL. */
3974 KeRaiseIrql(APC_LEVEL, &OldIrql);
3975
3976 /* Get the current thread */
3977 Thread = PsGetCurrentThread();
3978
3979 /* Update the operation counts */
3980 IopUpdateOperationCount(IopOtherTransfer);
3981
3982 /* Loop the list */
3983 ListHead = &Thread->IrpList;
3984 NextEntry = ListHead->Flink;
3985 while (ListHead != NextEntry)
3986 {
3987 /* Get the IRP and check if the File Object matches */
3988 Irp = CONTAINING_RECORD(NextEntry, IRP, ThreadListEntry);
3989 if (Irp->Tail.Overlay.OriginalFileObject == FileObject)
3990 {
3991 /* Cancel this IRP and keep looping */
3992 IoCancelIrp(Irp);
3993 OurIrpsInList = TRUE;
3994 }
3995
3996 /* Go to the next entry */
3997 NextEntry = NextEntry->Flink;
3998 }
3999
4000 /* Lower the IRQL */
4001 KeLowerIrql(OldIrql);
4002
4003 /* Check if we had found an IRP */
4004 if (OurIrpsInList)
4005 {
4006 /* Setup a 10ms wait */
4007 Interval.QuadPart = -100000;
4008
4009 /* Start looping */
4010 while (OurIrpsInList)
4011 {
4012 /* Do the wait */
4013 KeDelayExecutionThread(KernelMode, FALSE, &Interval);
4014 OurIrpsInList = FALSE;
4015
4016 /* Raise IRQL */
4017 KeRaiseIrql(APC_LEVEL, &OldIrql);
4018
4019 /* Now loop the list again */
4020 NextEntry = ListHead->Flink;
4021 while (NextEntry != ListHead)
4022 {
4023 /* Get the IRP and check if the File Object matches */
4024 Irp = CONTAINING_RECORD(NextEntry, IRP, ThreadListEntry);
4025 if (Irp->Tail.Overlay.OriginalFileObject == FileObject)
4026 {
4027 /* Keep looping */
4028 OurIrpsInList = TRUE;
4029 break;
4030 }
4031
4032 /* Go to the next entry */
4033 NextEntry = NextEntry->Flink;
4034 }
4035
4036 /* Lower the IRQL */
4037 KeLowerIrql(OldIrql);
4038 }
4039 }
4040
4041 /* Enter SEH for writing back the I/O Status */
4042 _SEH2_TRY
4043 {
4044 /* Write success */
4045 IoStatusBlock->Status = STATUS_SUCCESS;
4046 IoStatusBlock->Information = 0;
4047 }
4048 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
4049 {
4050 /* Ignore exception */
4051 }
4052 _SEH2_END;
4053
4054 /* Dereference the file object and return success */
4055 ObDereferenceObject(FileObject);
4056 return STATUS_SUCCESS;
4057 }
4058
4059 /*
4060 * @implemented
4061 */
4062 NTSTATUS
4063 NTAPI
4064 NtDeleteFile(IN POBJECT_ATTRIBUTES ObjectAttributes)
4065 {
4066 NTSTATUS Status;
4067 DUMMY_FILE_OBJECT LocalFileObject;
4068 HANDLE Handle;
4069 KPROCESSOR_MODE AccessMode = KeGetPreviousMode();
4070 OPEN_PACKET OpenPacket;
4071 PAGED_CODE();
4072 IOTRACE(IO_API_DEBUG, "FileMame: %wZ\n", ObjectAttributes->ObjectName);
4073
4074 /* Setup the Open Packet */
4075 RtlZeroMemory(&OpenPacket, sizeof(OPEN_PACKET));
4076 OpenPacket.Type = IO_TYPE_OPEN_PACKET;
4077 OpenPacket.Size = sizeof(OPEN_PACKET);
4078 OpenPacket.CreateOptions = FILE_DELETE_ON_CLOSE;
4079 OpenPacket.ShareAccess = FILE_SHARE_READ |
4080 FILE_SHARE_WRITE |
4081 FILE_SHARE_DELETE;
4082 OpenPacket.Disposition = FILE_OPEN;
4083 OpenPacket.DeleteOnly = TRUE;
4084 OpenPacket.LocalFileObject = &LocalFileObject;
4085
4086 /* Update the operation counts */
4087 IopUpdateOperationCount(IopOtherTransfer);
4088
4089 /*
4090 * Attempt opening the file. This will call the I/O Parse Routine for
4091 * the File Object (IopParseDevice) which will use the dummy file obejct
4092 * send the IRP to its device object. Note that we have two statuses
4093 * to worry about: the Object Manager's status (in Status) and the I/O
4094 * status, which is in the Open Packet's Final Status, and determined
4095 * by the Parse Check member.
4096 */
4097 Status = ObOpenObjectByName(ObjectAttributes,
4098 NULL,
4099 AccessMode,
4100 NULL,
4101 DELETE,
4102 &OpenPacket,
4103 &Handle);
4104 if (OpenPacket.ParseCheck == FALSE) return Status;
4105
4106 /* Retrn the Io status */
4107 return OpenPacket.FinalStatus;
4108 }
4109
4110 /* EOF */