Hack Forums
[C] Driver crashes on receiving IRP from user-mode program - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] Driver crashes on receiving IRP from user-mode program (/showthread.php?tid=6120803)



Driver crashes on receiving IRP from user-mode program - HardStoN - Nov 10, 2020

Hi,

Thats my IOCTL handle function in the driver
Code
NTSTATUS IOCTL(PDEVICE_OBJECT DeviceObject, PIRP Irp) {

    NTSTATUS Status = STATUS_UNSUCCESSFUL;
    PIO_STACK_LOCATION stck = IoGetCurrentIrpStackLocation(Irp);
    ULONG ctlCode = stck->Parameters.DeviceIoControl.IoControlCode;
    ULONG ByteIO = 0;

    if (ctlCode == IOCTL_TEST_CODE) {
        PULONG ptr_output = (PULONG)Irp->AssociatedIrp.SystemBuffer;
        *ptr_output = 852;

        DbgPrint("I got the code!");
        Status = STATUS_SUCCESS;
        ByteIO = sizeof(*ptr_output);
    }
    else
    {
        ByteIO = 0;
        DbgPrint("Unknown code has been fetched!");
    }

    Irp->IoStatus.Status = Status;
    Irp->IoStatus.Information = ByteIO;
    IoCompleteRequest(Irp, IO_NO_INCREMENT);

    return Status;
}

And that's my controller
Code
#include <iostream>
#include <Windows.h>

using namespace std;

#define IOCTL_TEST_CODE CTL_CODE(FILE_DEVICE_UNKNOWN, 0xdeadbeef, METHOD_OUT_DIRECT, FILE_ANY_ACCESS)

int main()
{
    HANDLE hDriver = CreateFileA("\\\\.\\permppdr", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
        0, OPEN_EXISTING, 0, 0);
    
    if (hDriver == INVALID_HANDLE_VALUE) {
        cout << "Invalid handle!" << endl;
        return 0;
    }

    DeviceIoControl(hDriver, IOCTL_TEST_CODE, 0, 0, 0, 0, 0, 0);
    CloseHandle(hDriver);
    return 0;
}

The driver loads and unloads successfully but the second i run the controller the driver crashes and i get a blue screen :)

Any ideas why?


RE: Driver crashes on receiving IRP from user-mode program - cghhhhasd - Nov 10, 2020

I don't write drivers, but take a look at this full IOCTL example code from Microsoft: https://github.com/microsoft/Windows-driver-samples/tree/master/general/ioctl/wdm


RE: Driver crashes on receiving IRP from user-mode program - HardStoN - Nov 10, 2020

yeah thank u either way i solved it :) there was a problem with my Irp variables and when i did
Code
IoCompleteRequest(Irp, IO_NO_INCREMENT);
it crashed but now its fixed