Hello there,
Hope someone can figure out what I missed on the following code I created to rename a file.
The status of ZwSetInformationFile returns Success but after reaching at the end of my routine, bluescreen occurs and I can’t figure out what I missed.
Here’s the code I created to rename a file:
NTSTATUS
RenameFile(
IN UNICODE_STRING OldFileName,
IN UNICODE_STRING NewFileName
)
{
NTSTATUS Status = STATUS_SUCCESS;
HANDLE FileHandle;
HANDLE RootDirHandle;
OBJECT_ATTRIBUTES FileObjectAttributes;
OBJECT_ATTRIBUTES RootDirObjectAttributes;
IO_STATUS_BLOCK IoStatusBlock;
UNICODE_STRING uniFullFileName;
UNICODE_STRING uniFullNewFileName;
UNICODE_STRING uniDiskDrive;
FILE_RENAME_INFORMATION frInfo;
RtlInitUnicodeString( &uniDiskDrive, L"\??\C:\" );
uniFullFileName.Length = ( wcslen(uniDiskDrive.Buffer) + wcslen(OldFileName.Buffer) ) * sizeof(WCHAR);
uniFullFileName.MaximumLength = uniFullFileName.Length + 2;
uniFullFileName.Buffer = ExAllocatePoolWithTag(
NonPagedPool,
uniFullFileName.MaximumLength,
TAG_GENERAL
);
if( uniFullFileName.Buffer == NULL ) {
KdPrint( (“%s: Can’t allocate memory for a buffer.\n”, NAMEBASE));
Status = STATUS_INSUFFICIENT_RESOURCES;
goto Exit;
}
RtlCopyUnicodeString( &uniFullFileName, &uniDiskDrive );
Status = RtlAppendUnicodeStringToString(
&uniFullFileName,
&OldFileName
);
if( !NT_SUCCESS( Status ) ) {
KdPrint( (“%s: RtlAppendUnicodeStringToString Fails.\n”, NAMEBASE) );
goto Exit;
} //endif
InitializeObjectAttributes(
&FileObjectAttributes,
&uniFullFileName,
OBJ_CASE_INSENSITIVE,
NULL,
NULL
);
Status = ZwCreateFile(
&FileHandle, // returned file handle
FILE_ANY_ACCESS, // desired access
&FileObjectAttributes, // ptr to object attributes
&IoStatusBlock, // ptr to I/O status block
NULL, // allocation size
FILE_ATTRIBUTE_NORMAL, // file attributes
0, // share access
FILE_OPEN, // create disposition
FILE_SYNCHRONOUS_IO_NONALERT, // create options
NULL, // ptr to extended attributes
0);
if( !NT_SUCCESS( Status ) ) {
KdPrint( (“%s: ZwCreateFile Fails on RenameFile.\n”, NAMEBASE) );
KdPrint( (“%s: Possibly the file doesn’t exist.\n”, NAMEBASE) );
goto Exit;
} //endif
//
// Open the Directory
//
InitializeObjectAttributes(
&RootDirObjectAttributes,
&uniRamdiskDrive,
OBJ_CASE_INSENSITIVE,
NULL,
NULL
);
Status = ZwCreateFile(
&RootDirHandle, // returned file handle
FILE_ANY_ACCESS, // desired access
&RootDirObjectAttributes, // ptr to object attributes
&IoStatusBlock, // ptr to I/O status block
NULL, // allocation size
FILE_ATTRIBUTE_NORMAL, // file attributes
0, // share access
FILE_OPEN, // create disposition
FILE_SYNCHRONOUS_IO_NONALERT, // create options
NULL, // ptr to extended attributes
0);
if( !NT_SUCCESS( Status ) ) {
KdPrint( (“%s: ZwCreateFile Fails on RenameFile.\n”, NAMEBASE) );
KdPrint( (“%s: Possibly the file doesn’t exist.\n”, NAMEBASE) );
goto Exit;
} //endif
uniFullNewFileName.Length = ( wcslen(uniRamdiskDrive.Buffer) + wcslen(NewFileName.Buffer) ) * sizeof(WCHAR);
uniFullNewFileName.MaximumLength = uniFullNewFileName.Length + 2;
uniFullNewFileName.Buffer = ExAllocatePoolWithTag(
NonPagedPool,
uniFullNewFileName.MaximumLength,
TAG_GENERAL
);
frInfo.ReplaceIfExists = FALSE;
frInfo.RootDirectory = RootDirHandle;
frInfo.FileNameLength = NewFileName.Length;
//frInfo.FileName = *NewFileName.Buffer;
memcpy( frInfo.FileName, NewFileName.Buffer, frInfo.FileNameLength + 2 );
Status = ZwSetInformationFile(
FileHandle,
&IoStatusBlock,
&frInfo,
sizeof(FILE_RENAME_INFORMATION) + NewFileName.Length + 2,
FileRenameInformation
);
if( !NT_SUCCESS( Status ) ) {
KdPrint( (“%s: ZwSetInformationFile Fails on RamdiskRenameFile.\n”, NAMEBASE) );
goto Exit;
} //endif
Exit:
return Status;
}
I tried setting frInfo.RootDirectory = NULL but same result. Also I did to assign the new filename with the complete path but still my driver causes BSOD error. Hope someone here can figure out what’s wrong with my implementation.
Thanks.