Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove potential memory leak in DokanCreateDiskDevice on allocation failure (resubmit) #108

Merged
merged 1 commit into from
Nov 12, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 42 additions & 27 deletions sys/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ with this program. If not, see <http://www.gnu.org/licenses/>.

static UNICODE_STRING sddl = RTL_CONSTANT_STRING(L"D:P(A;;GA;;;SY)(A;;GRGWGX;;;BA)(A;;GRGWGX;;;WD)(A;;GRGX;;;RC)");


NTSTATUS
DokanSendIoContlToMountManager(
__in PVOID InputBuffer,
Expand Down Expand Up @@ -378,6 +379,20 @@ FreeUnicodeString(
}


static VOID
FreeDcbNames(
__in PDokanDCB Dcb)
{
FreeUnicodeString(Dcb->SymbolicLinkName);
FreeUnicodeString(Dcb->DiskDeviceName);
FreeUnicodeString(Dcb->FileSystemDeviceName);

Dcb->SymbolicLinkName = NULL;
Dcb->DiskDeviceName = NULL;
Dcb->FileSystemDeviceName = NULL;
}


//#define DOKAN_NET_PROVIDER

NTSTATUS
Expand Down Expand Up @@ -429,7 +444,7 @@ DokanCreateDiskDevice(
RtlInitUnicodeString(&diskDeviceName, diskDeviceNameBuf);

//
// make a DeviceObject for Disk Device
// Create DeviceObject for the Disk Device
//
if (!isNetworkFileSystem) {
status = IoCreateDeviceSecure(
Expand All @@ -455,7 +470,10 @@ DokanCreateDiskDevice(


if (!NT_SUCCESS(status)) {
DDbgPrint(" IoCreateDevice (DISK_DEVICE) failed: 0x%x\n", status);
DDbgPrint(" %s failed: 0x%x\n", isNetworkFileSystem
? "IoCreateDevice(FILE_DEVICE_UNKNOWN)"
: "IoCreateDeviceSecure(FILE_DEVICE_DISK)",
status);
return status;
}
DDbgPrint("DokanDiskDevice: %wZ created\n", &diskDeviceName);
Expand Down Expand Up @@ -502,30 +520,27 @@ DokanCreateDiskDevice(
dcb->DiskDeviceName = AllocateUnicodeString(diskDeviceNameBuf);
dcb->FileSystemDeviceName = AllocateUnicodeString(fsDeviceNameBuf);

if (dcb->SymbolicLinkName == NULL) {
DDbgPrint(" Can't allocate memory for SymbolicLinkName");
ExDeleteResourceLite(&dcb->Resource);
IoDeleteDevice(diskDeviceObject);
return STATUS_INSUFFICIENT_RESOURCES;
}
if (dcb->SymbolicLinkName == NULL ||
dcb->DiskDeviceName == NULL ||
dcb->FileSystemDeviceName == NULL) {

if (dcb->DiskDeviceName == NULL) {
DDbgPrint(" Can't allocate memory for DiskDeviceName");
ExFreePool(dcb->SymbolicLinkName);
ExDeleteResourceLite(&dcb->Resource);
IoDeleteDevice(diskDeviceObject);
return STATUS_INSUFFICIENT_RESOURCES;
}
DDbgPrint(" Failed to allocate memory for device naming");

if (dcb->SymbolicLinkName != NULL)
ExFreePool(dcb->SymbolicLinkName);
if (dcb->DiskDeviceName != NULL)
ExFreePool(dcb->DiskDeviceName);
if (dcb->FileSystemDeviceName != NULL)
ExFreePool(dcb->FileSystemDeviceName);

if (dcb->FileSystemDeviceName == NULL) {
DDbgPrint(" Can't allocate memory for FileSystemDeviceName");
ExFreePool(dcb->SymbolicLinkName);
ExFreePool(dcb->DiskDeviceName);
ExDeleteResourceLite(&dcb->Resource);
IoDeleteDevice(diskDeviceObject);
return STATUS_INSUFFICIENT_RESOURCES;
}

//
// Create DeviceObject for the File System
//
status = IoCreateDeviceSecure(
DriverObject, // DriverObject
sizeof(DokanVCB), // DeviceExtensionSize
Expand All @@ -538,9 +553,14 @@ DokanCreateDiskDevice(
&fsDeviceObject); // DeviceObject

if (!NT_SUCCESS(status)) {
DDbgPrint(" IoCreateDevice (FILE_SYSTEM_DEVICE) failed: 0x%x\n", status);
DDbgPrint(" IoCreateDeviceSecure (%s) failed: 0x%x\n",
DeviceType == FILE_DEVICE_DISK_FILE_SYSTEM
? "FILE_DEVICE_DISK_FILE_SYSTEM"
: "FILE_DEVICE_NETWORK_FILE_SYSTEM",
status);
ExDeleteResourceLite(&dcb->Resource);
IoDeleteDevice(diskDeviceObject);
FreeDcbNames(dcb);
return status;
}
DDbgPrint("DokanFileSystemDevice: %wZ created\n", dcb->FileSystemDeviceName);
Expand Down Expand Up @@ -607,6 +627,7 @@ DokanCreateDiskDevice(
}
IoDeleteDevice(diskDeviceObject);
IoDeleteDevice(fsDeviceObject);
FreeDcbNames(dcb);
DDbgPrint(" IoCreateSymbolicLink returned 0x%x\n", status);
return status;
}
Expand Down Expand Up @@ -671,13 +692,7 @@ DokanDeleteDeviceObject(
DDbgPrint(" Delete Symbolic Name: %wZ\n", Dcb->SymbolicLinkName);
IoDeleteSymbolicLink(Dcb->SymbolicLinkName);

FreeUnicodeString(Dcb->SymbolicLinkName);
FreeUnicodeString(Dcb->DiskDeviceName);
FreeUnicodeString(Dcb->FileSystemDeviceName);

Dcb->SymbolicLinkName = NULL;
Dcb->DiskDeviceName = NULL;
Dcb->FileSystemDeviceName = NULL;
FreeDcbNames(Dcb);

if (Dcb->DeviceObject->Vpb) {
Dcb->DeviceObject->Vpb->DeviceObject = NULL;
Expand Down