1

So I have a user mode process which will send 'requests' to a kernel driver by setting a variable in the usermode processes's memory. The kernel driver sets the handle once it sees that the pid is not null.

long SendHandleByProcessIDRequest(int pid)
{
    _KeHandleByProcessIDRequest._pid = pid;
    while (_KeHandleByProcessIDRequest._Handle == 0) {}; // wait until the kernel driver has set the handle output
    long handle = _KeHandleByProcessIDRequest._Handle;

    // Request is complete
    _KeHandleByProcessIDRequest._pid = NULL;
    _KeHandleByProcessIDRequest._Handle = NULL;

    return handle;
}

The issue is that the while loop will never stop, even when I know the kernel driver is setting the _handle correctly except for when I put

std::cout << _KeHandleByProcessIDRequest._Handle << std::endl;

inside of the while loop. I assume this is because the program assumes the value is the same because it hasn't been modified in this program since the last check so it thinks the handle is the same value over and over and the cout call is 'updating' the value.

Thanks in advance

Cow Nation
  • 71
  • 9
  • 1
    In commonly used c++ where volatile isn't deprecated, you can use it on a variable declaration to tell the compiler that a variable needs actual read on each access. (e.g. `volatile HANDLE _Handle;` in your KHBPIDR struct definition.) – Alceste_ Oct 29 '19 at 05:47
  • Try adding an ``__mm_pause`` in the infinite loop. A better alternative would be for the kernel driver to trigger an event and have the user program wait for it. That would be a proper wait that also lets the OS thread scheduler know what your intentions are. – Pickle Rick Oct 29 '19 at 05:51
  • On a completely different note, you might want to have a look at https://stackoverflow.com/a/228797/8155816 to enhance your naming conventions. – Alceste_ Oct 29 '19 at 05:51
  • The volatile keyword worked, thanks a lot! – Cow Nation Oct 29 '19 at 06:15

0 Answers0