Drivers and user-mode components access most system-defined objects through handles. Handles are represented by the HANDLE opaque data type. (Note that handles are not used for accessing device objects or driver objects.)
For most object types, the kernel-mode routine that creates or opens the object provides a handle to the caller. The caller then uses that handle in subsequent operations on the object.
Here is a list of object types that drivers typically use, and the routines that provide handles to objects of that type.
| Object Type | Corresponding Create/Open Routine |
|---|---|
| file | IoCreateFile, ZwCreateFile, ZwOpenFile |
| registry keys | IoOpenDeviceInterfaceRegistryKey, IoOpenDeviceRegistryKey, ZwCreateKey, ZwOpenKey |
| threads | PsCreateSystemThread |
| events | IoCreateSynchronizationEvent, IoCreateNotificationEvent |
| symbolic links | ZwOpenSymbolicLinkObject |
| directory objects | ZwCreateDirectoryObject |
| section objects | ZwOpenSection |
When the driver no longer requires access to the object, it calls the ZwClose routine to close the handle. This works for all of the object types listed in the table above.
Most of the routines that provide handles take an OBJECT_ATTRIBUTES structure as a parameter. This structure can be used to specify attributes for the handle.
Drivers can specify the following handle attributes:
The handle can only be accessed from kernel mode.
Any children of the current process receive a copy of the handle when they are created.
This attribute specifies that the system performs all access checks on the handle. By default, the system bypasses all access checks on handles created in kernel mode.
Use the InitializeObjectAttributes routine to set these attributes in an OBJECT_ATTRIBUTES structure.
Whenever a driver creates an object handle for its private use, the driver must specify the OBJ_KERNEL_HANDLE attribute. This ensures that the handle is inaccessible to user-mode applications.
A driver that shares object handles between kernel mode and user mode must be carefully written to avoid accidentally creating security holes. Here are some guidelines: