I want to delete files by sending IRP via a WDK driver. It works well for deleting all files no matter *.pdf or *.pptx, except some EXEs which require Administrator to run, such as EXE installer and setup.exe etc. I don't know why it hangs on IoCallDriver once I try to delete EXE installers. I have also tried to set a timeout for KeWaitForSingleObject but no luck, the driver keeps hanging on IoCallDriver.
Does Windows limit drivers to remove these Administrator authority EXEs? How to solve this? Thanks a lot.
NTSTATUS send_delete_file_irp(PFILE_OBJECT file_object) {
KEVENT event;
PDEVICE_OBJECT device_object = IoGetBaseFileSystemDeviceObject(file_object);
PIRP irp = IoAllocateIrp(device_object->StackSize, false);
// Set the complete routine that will free the IRP and signal the event
KeInitializeEvent(&event, SynchronizationEvent, false);
IoSetCompletionRoutine(
irp,
io_complete,
&event,
true,
true,
true);
FILE_DISPOSITION_INFORMATION file_disposition;
file_disposition.DeleteFile = true;
IO_STATUS_BLOCK io_status_block;
irp->AssociatedIrp.SystemBuffer = &file_disposition;
irp->UserEvent = &event;
irp->UserIosb = &io_status_block;
irp->Tail.Overlay.OriginalFileObject = file_object;
irp->Tail.Overlay.Thread = (PETHREAD)KeGetCurrentThread();
irp->RequestorMode = KernelMode;
IO_STACK_LOCATION* stack_location = IoGetNextIrpStackLocation(irp);
stack_location->MajorFunction = IRP_MJ_SET_INFORMATION;
stack_location->DeviceObject = device_object;
stack_location->FileObject = file_object;
stack_location->Parameters.SetFile.Length = sizeof(FILE_DISPOSITION_INFORMATION);
stack_location->Parameters.SetFile.FileInformationClass = FileDispositionInformation;
stack_location->Parameters.SetFile.FileObject = file_object;
IoCallDriver(device_object, irp);
KeWaitForSingleObject(&event, Executive, KernelMode, true, nullptr);
return STATUS_SUCCESS;
}