Hi,
I want to access a user mode file handle from kernel, for reading an
existing file. For that I have created the user mode handle by calling
NtCreateFile. Then the handle is passed to the kernel through an
DeviceIoControl call. Then the handle is verified in Kernel using
“ObReferenceObjectByHandle”. But when I try to read from file using
zwReadFile I get an error - “Invalid handle”. Isn’ t this the right
approach?
I am adding my code here for your reference-
Usermode
UNICODE_STRING myFilePath;
OBJECT_ATTRIBUTES ObjectAttributes;
IO_STATUS_BLOCK IoStatusBlock;
NTSTATUS status;
HANDLE handle;
RtlInitUnicodeString(&myFilePath, L"\??\C:\myfile.txt");
InitializeObjectAttributes(&ObjectAttributes, &myFilePath,
OBJ_KERNEL_HANDLE | OBJ_FORCE_ACCESS_CHECK, NULL, NULL);
status = NtCreateFile(&handle, FILE_READ_DATA | SYNCHRONIZE,
&ObjectAttributes, &IoStatusBlock, NULL, FILE_ATTRIBUTE_NORMAL,
FILE_SHARE_READ, FILE_OPEN, FILE_RANDOM_ACCESS, NULL, 0);
Kernel
FILE_OBJECT fo;
OBJECT_HANDLE_INFORMATION ohi;
NTSTATUS stat=ObReferenceObjectByHandle(FileHandle /*handle received
through DeviceIoControl*/, 0L, *IoFileObjectType, UserMode, (PVOID)&fo,
&ohi);
//The above call succeeds
/*The following call is made from a WORKER THREAD*/
NTSTATUS stat = ZwReadFile(FileHandle, NULL, NULL, NULL, &IoStatusBlock,
sysAddr, readLength, &offset, NULL);
The above call fails with Invalid handle (Error code “0xc0000008”). Can you
please shed some light on this issue?
Thanks a lot,
Lloyd