windowsregistrywdfumdf

Access denied when using WdfRegistryOpenKey function to create registry keys in umdf2


In my WDF driver, I want to save some custom data used for specific device to the registry. But I cannot use the WdfRegistryCreateKey() function provided by umdf2 to create a new key under "hardware_key\Device Parameters" on windows10 1909 platform. The error code is "Access Denied". And I have opened the parent key correctly in READ_KEY mask(if not READ_KEY mask, WdfDeviceOpenRegistryKey() will return STATUS_INVALID_PARAMETER indicate insufficient access rights). How to solve this problem?

Thanks in advance.

// create subkey function definition
NTSTATUS registry_create_key(WDFKEY parent_key, PUNICODE_STRING key_str,
    WDFKEY* key)
{
    NTSTATUS status;
    WDFKEY new_key;

    status = WdfRegistryCreateKey(parent_key, key_str, KEY_CREATE_SUB_KEY,
        REG_OPTION_NON_VOLATILE, NULL, WDF_NO_OBJECT_ATTRIBUTES,
        &new_key);
    if (!NT_SUCCESS(status)) {
        return status;
    }

    if (key)
        *key = new_key;
    else
        WdfRegistryClose(new_key);

    return status;
}

// open parent key, I have removed the return value judgment for brevity
status = WdfDeviceOpenRegistryKey(
        DeviceContext->Device,
        PLUGPLAY_REGKEY_DEVICE,
        KEY_READ,
        WDF_NO_OBJECT_ATTRIBUTES,
        &hkey);

    // The key name I want to create
    UNICODE_STRING  myKeyStr;
    RtlInitUnicodeString(
        &myKeyStr,
        L"myKeyStr"
    );

    // Call the subkey create function
    status = registry_create_key(hkey, &myKeyStr, &subkey);
    WdfRegistryClose(subkey);

Solution

  • You're opening the parent key for KEY_READ access then trying to create a subkey under it. You need to open the parent key with KEY_CREATE_SUB_KEY access.