Better NtClose

This commit is contained in:
momo5502
2024-12-22 16:33:50 +01:00
parent 5122b55661
commit b4e7606226
2 changed files with 39 additions and 35 deletions

View File

@@ -106,9 +106,15 @@ namespace handle_detail
};
}
struct generic_handle_store
{
virtual ~generic_handle_store() = default;
virtual bool erase(const handle h) = 0;
};
template <handle_types::type Type, typename T, uint32_t IndexShift = 0>
requires(utils::Serializable<T>)
class handle_store
class handle_store : public generic_handle_store
{
public:
using index_type = uint32_t;
@@ -208,7 +214,7 @@ public:
return this->erase(entry);
}
bool erase(const handle h)
bool erase(const handle h) override
{
return this->erase(h.value);
}
@@ -352,4 +358,4 @@ constexpr auto CURRENT_THREAD = make_handle(~1ULL);
constexpr auto CURRENT_PROCESS_TOKEN = make_handle(~3ULL);
constexpr auto CURRENT_THREAD_TOKEN = make_handle(~4ULL);
constexpr auto CURRENT_THREAD_EFFECTIVE_TOKEN = make_handle(~5ULL);
constexpr auto CURRENT_THREAD_EFFECTIVE_TOKEN = make_handle(~5ULL);

View File

@@ -348,6 +348,33 @@ namespace
return STATUS_SUCCESS;
}
generic_handle_store* get_handle_store(process_context& proc, const handle h)
{
switch (h.value.type)
{
case handle_types::thread:
return &proc.threads;
case handle_types::event:
return &proc.events;
case handle_types::file:
return &proc.files;
case handle_types::device:
return &proc.devices;
case handle_types::semaphore:
return &proc.semaphores;
case handle_types::registry:
return &proc.registry_keys;
case handle_types::mutant:
return &proc.mutants;
case handle_types::port:
return &proc.ports;
case handle_types::section:
return &proc.sections;
default:
return nullptr;
}
}
NTSTATUS handle_NtClose(const syscall_context& c, const handle h)
{
const auto value = h.value;
@@ -356,37 +383,8 @@ namespace
return STATUS_SUCCESS;
}
if (value.type == handle_types::thread && c.proc.threads.erase(h))
{
return STATUS_SUCCESS;
}
if (value.type == handle_types::event && c.proc.events.erase(h))
{
return STATUS_SUCCESS;
}
if (value.type == handle_types::file && c.proc.files.erase(h))
{
return STATUS_SUCCESS;
}
if (value.type == handle_types::device && c.proc.devices.erase(h))
{
return STATUS_SUCCESS;
}
if (value.type == handle_types::semaphore && c.proc.semaphores.erase(h))
{
return STATUS_SUCCESS;
}
if (value.type == handle_types::registry && c.proc.registry_keys.erase(h))
{
return STATUS_SUCCESS;
}
if (value.type == handle_types::mutant && c.proc.mutants.erase(h))
auto* handle_store = get_handle_store(c.proc, h);
if (handle_store && handle_store->erase(h))
{
return STATUS_SUCCESS;
}
@@ -419,7 +417,7 @@ namespace
if (previous_count)
{
previous_count.write(old_count);
previous_count.write(static_cast<LONG>(old_count));
}
return STATUS_SUCCESS;