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

Add InitializeObjectAttributes function with example to ntddk.rs #253

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

0xflux
Copy link

@0xflux 0xflux commented Dec 10, 2024

Summary

This PR adds the InitializeObjectAttributes function to ntddk.rs. The function initialises the OBJECT_ATTRIBUTES structure, which is used by Windows kernel APIs for object management. The function ensures pointer safety and proper initialisation of fields within the structure, hopefully making it somewhat safe to use. I have included unsafe in the function signature to alert the caller to pointer dereferencing, though the unsafe keyword strictly isn't required due to the inner unsafe block.

I don't know whether you want this style of documentation in this crate; if you want it removing, please let me know! I'm new to contributing to this project, so I am open to feedback.

Changes Made

  • Function: Added InitializeObjectAttributes
    • Ensures safety by checking for null pointers before dereferencing.
    • Returns Ok(()) for successful initialisation or Err(()) if the pointer is invalid.
    • Modifies the OBJECT_ATTRIBUTES structure using an unsafe block.
  • Example:
    • Demonstrates initialising a UNICODE_STRING.
    • Includes attribute constants (OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE) for clarity.
    • Provides error handling for the result of the function.

Original C macro for reference

The original implementation in the WDK can be found below for reference to my PR:

#define InitializeObjectAttributes( p, n, a, r, s ) { \
    (p)->Length = sizeof( OBJECT_ATTRIBUTES );          \
    (p)->RootDirectory = r;                             \
    (p)->Attributes = a;                                \
    (p)->ObjectName = n;                                \
    (p)->SecurityDescriptor = s;                        \
    (p)->SecurityQualityOfService = NULL;               \
    }

This commit introduces the `InitializeObjectAttributes` function, which initializes the `OBJECT_ATTRIBUTES` structure used in Windows driver development. The function ensures safety by validating the provided pointer before modifying memory. Additionally, an example demonstrating the function's usage with a `UNICODE_STRING` and attribute constants is included in the documentation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant