From f99e919cb7bbe46e45d94ac76e6067d731564c8c Mon Sep 17 00:00:00 2001 From: Aiken Harris Date: Tue, 14 Jul 2026 23:04:48 +0200 Subject: [PATCH] Implement kernel handle encoding --- xtoskrnl/CMakeLists.txt | 1 + xtoskrnl/includes/ob.hh | 1 + xtoskrnl/includes/ob/handle.hh | 27 ++++++++++++ xtoskrnl/ob/handle.cc | 76 ++++++++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 xtoskrnl/includes/ob/handle.hh create mode 100644 xtoskrnl/ob/handle.cc diff --git a/xtoskrnl/CMakeLists.txt b/xtoskrnl/CMakeLists.txt index f542251..9efe19f 100644 --- a/xtoskrnl/CMakeLists.txt +++ b/xtoskrnl/CMakeLists.txt @@ -92,6 +92,7 @@ list(APPEND XTOSKRNL_SOURCE ${XTOSKRNL_SOURCE_DIR}/mm/quota.cc ${XTOSKRNL_SOURCE_DIR}/ob/data.cc ${XTOSKRNL_SOURCE_DIR}/ob/devmap.cc + ${XTOSKRNL_SOURCE_DIR}/ob/handle.cc ${XTOSKRNL_SOURCE_DIR}/ob/lifecycl.cc ${XTOSKRNL_SOURCE_DIR}/ob/obdir.cc ${XTOSKRNL_SOURCE_DIR}/ob/obmgr.cc diff --git a/xtoskrnl/includes/ob.hh b/xtoskrnl/includes/ob.hh index 1b3e199..cdd3d32 100644 --- a/xtoskrnl/includes/ob.hh +++ b/xtoskrnl/includes/ob.hh @@ -12,6 +12,7 @@ #include #include +#include #include #include #include diff --git a/xtoskrnl/includes/ob/handle.hh b/xtoskrnl/includes/ob/handle.hh new file mode 100644 index 0000000..d3148a8 --- /dev/null +++ b/xtoskrnl/includes/ob/handle.hh @@ -0,0 +1,27 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtoskrnl/includes/ob/handle.hh + * DESCRIPTION: Object Manager Handle Management + * DEVELOPERS: Aiken Harris + */ + +#ifndef __XTOSKRNL_OB_HANDLE_HH +#define __XTOSKRNL_OB_HANDLE_HH + +#include + + +/* Object Manager */ +namespace OB +{ + class Handle + { + public: + STATIC XTFASTCALL HANDLE DecodeKernelHandle(IN HANDLE Handle); + STATIC XTFASTCALL HANDLE EncodeKernelHandle(IN HANDLE Handle); + STATIC XTFASTCALL ULONG GetHandleAttributes(IN PHANDLE_TABLE_ENTRY HandleTableEntry); + }; +} + +#endif /* __XTOSKRNL_OB_HANDLE_HH */ diff --git a/xtoskrnl/ob/handle.cc b/xtoskrnl/ob/handle.cc new file mode 100644 index 0000000..74be1c1 --- /dev/null +++ b/xtoskrnl/ob/handle.cc @@ -0,0 +1,76 @@ +/** + * PROJECT: ExectOS + * COPYRIGHT: See COPYING.md in the top level directory + * FILE: xtoskrnl/ob/handle.cc + * DESCRIPTION: Object Manager Handle Management + * DEVELOPERS: Aiken Harris + */ + +#include + + +/** + * Encodes a standard handle into a kernel-space handle. + * + * @param Handle + * Supplies the raw standard handle to be encoded. + * + * @return This routine returns the encoded kernel handle. + * + * @since XT 1.0 + */ +XTFASTCALL +HANDLE +OB::Handle::DecodeKernelHandle(IN HANDLE Handle) +{ + /* Clear the kernel handle flag */ + return (HANDLE)(OBJECT_HANDLE_KERNEL_MASK ^ (ULONG_PTR)Handle); +} + +/** + * Decodes a kernel-space handle into a standard handle. + * + * @param Handle + * Supplies the encoded kernel handle to be decoded. + * + * @return This routine returns the decoded standard handle. + * + * @since XT 1.0 + */ +XTFASTCALL +HANDLE +OB::Handle::EncodeKernelHandle(IN HANDLE Handle) +{ + /* Set the kernel handle flag */ + return (HANDLE)(OBJECT_HANDLE_KERNEL_MASK | (ULONG_PTR)Handle); +} + +/** + * Retrieves and decodes the handle attributes from a given handle table entry. + * + * @param HandleTableEntry + * Supplies a pointer to the handle table entry to inspect. + * + * @return This routine returns a bitmask representing the active handle attributes. + * + * @since XT 1.0 + */ +XTFASTCALL +ULONG +OB::Handle::GetHandleAttributes(IN PHANDLE_TABLE_ENTRY HandleTableEntry) +{ + ULONG Attributes; + + /* Extract the base attributes */ + Attributes = HandleTableEntry->ObAttributes; + + /* Check if the protect-on-close access bit is set */ + if(HandleTableEntry->GrantedAccess & OBJECT_ACCESS_PROTECT_CLOSE_BIT) + { + /* Retain valid handle attributes and set the protect-close flag */ + return (Attributes & OBJECT_HANDLE_ATTRIBUTES) | OBJECT_PROTECT_CLOSE; + } + + /* Isolate and return the inherit and audit-on-close flags */ + return Attributes & (OBJECT_INHERIT | OBJECT_AUDIT_OBJECT_CLOSE); +}