Skip to content

Commit e05ca16

Browse files
author
Dmitry Fleytman
committed
ControlDevice: Split dynamic and persistent hide rules lists
Required to properly support removal of persistent hide rule Signed-off-by: Dmitry Fleytman <[email protected]>
1 parent cc1092c commit e05ca16

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

UsbDk/ControlDevice.cpp

+14-11
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ bool CUsbDkControlDevice::ShouldHide(const USB_DEVICE_DESCRIPTOR &DevDescriptor)
238238
{
239239
auto Hide = false;
240240

241-
const_cast<HideRulesSet*>(&m_HideRules)->ForEach([&DevDescriptor, &Hide](CUsbDkHideRule *Entry) -> bool
241+
const auto &HideVisitor = [&DevDescriptor, &Hide](CUsbDkHideRule *Entry) -> bool
242242
{
243243
if (Entry->Match(DevDescriptor))
244244
{
@@ -247,7 +247,10 @@ bool CUsbDkControlDevice::ShouldHide(const USB_DEVICE_DESCRIPTOR &DevDescriptor)
247247
}
248248

249249
return true;
250-
});
250+
};
251+
252+
const_cast<HideRulesSet*>(&m_HideRules)->ForEach(HideVisitor);
253+
const_cast<HideRulesSet*>(&m_PersistentHideRules)->ForEach(HideVisitor);
251254

252255
return Hide;
253256
}
@@ -424,7 +427,7 @@ NTSTATUS CUsbDkControlDevice::Register()
424427
if (NT_SUCCESS(status))
425428
{
426429
FinishInitializing();
427-
LoadPersistentHideRules();
430+
ReloadPersistentHideRules();
428431
}
429432

430433
return status;
@@ -637,7 +640,7 @@ NTSTATUS CUsbDkControlDevice::AddRedirect(const USB_DK_DEVICE_ID &DeviceId, PHAN
637640
return STATUS_SUCCESS;
638641
}
639642

640-
NTSTATUS CUsbDkControlDevice::AddHideRule(const USB_DK_HIDE_RULE &UsbDkRule)
643+
NTSTATUS CUsbDkControlDevice::AddHideRuleToSet(const USB_DK_HIDE_RULE &UsbDkRule, HideRulesSet &Set)
641644
{
642645
TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_CONTROLDEVICE, "%!FUNC! entry");
643646

@@ -656,14 +659,14 @@ NTSTATUS CUsbDkControlDevice::AddHideRule(const USB_DK_HIDE_RULE &UsbDkRule)
656659
return STATUS_INSUFFICIENT_RESOURCES;
657660
}
658661

659-
if(!m_HideRules.Add(NewRule))
662+
if(!Set.Add(NewRule))
660663
{
661664
TraceEvents(TRACE_LEVEL_ERROR, TRACE_CONTROLDEVICE, "%!FUNC! failed. Hide rule already present.");
662665
return STATUS_OBJECT_NAME_COLLISION;
663666
}
664667

665668
TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_CONTROLDEVICE, "%!FUNC! Current hide rules:");
666-
m_HideRules.Dump();
669+
Set.Dump();
667670

668671
NewRule.detach();
669672
return STATUS_SUCCESS;
@@ -672,9 +675,7 @@ NTSTATUS CUsbDkControlDevice::AddHideRule(const USB_DK_HIDE_RULE &UsbDkRule)
672675
void CUsbDkControlDevice::ClearHideRules()
673676
{
674677
m_HideRules.Clear();
675-
TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_CONTROLDEVICE, "%!FUNC! All hide rules dropped.");
676-
TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_CONTROLDEVICE, "%!FUNC! Reloading persistent hide rules.");
677-
LoadPersistentHideRules();
678+
TraceEvents(TRACE_LEVEL_INFORMATION, TRACE_CONTROLDEVICE, "%!FUNC! All dynamic hide rules dropped.");
678679
}
679680

680681
class CHideRulesRegKey final : public CRegKey
@@ -825,8 +826,10 @@ class CRegHideRule final : private CRegKey
825826
}
826827
};
827828

828-
NTSTATUS CUsbDkControlDevice::LoadPersistentHideRules()
829+
NTSTATUS CUsbDkControlDevice::ReloadPersistentHideRules()
829830
{
831+
m_PersistentHideRules.Clear();
832+
830833
CHideRulesRegKey RulesKey;
831834
auto status = RulesKey.Open();
832835
if (NT_SUCCESS(status))
@@ -839,7 +842,7 @@ NTSTATUS CUsbDkControlDevice::LoadPersistentHideRules()
839842
if (NT_SUCCESS(Rule.Open(RulesKey, *Name)) &&
840843
NT_SUCCESS(Rule.Read(ParsedRule)))
841844
{
842-
AddHideRule(ParsedRule);
845+
AddPersistentHideRule(ParsedRule);
843846
}
844847
});
845848
}

UsbDk/ControlDevice.h

+11-3
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,17 @@ class CUsbDkControlDevice : private CWdfControlDevice, public CAllocatable<NonPa
218218

219219
ULONG CountDevices();
220220
NTSTATUS RescanRegistry()
221-
{ return LoadPersistentHideRules(); }
221+
{ return ReloadPersistentHideRules(); }
222222

223223
bool EnumerateDevices(USB_DK_DEVICE_INFO *outBuff, size_t numberAllocatedDevices, size_t &numberExistingDevices);
224224
NTSTATUS ResetUsbDevice(const USB_DK_DEVICE_ID &DeviceId);
225225
NTSTATUS AddRedirect(const USB_DK_DEVICE_ID &DeviceId, PHANDLE ObjectHandle);
226-
NTSTATUS AddHideRule(const USB_DK_HIDE_RULE &UsbDkRule);
226+
227+
NTSTATUS AddHideRule(const USB_DK_HIDE_RULE &UsbDkRule)
228+
{ return AddHideRuleToSet(UsbDkRule, m_HideRules); }
229+
NTSTATUS AddPersistentHideRule(const USB_DK_HIDE_RULE &UsbDkRule)
230+
{ return AddHideRuleToSet(UsbDkRule, m_PersistentHideRules); }
231+
227232
void ClearHideRules();
228233

229234
NTSTATUS RemoveRedirect(const USB_DK_DEVICE_ID &DeviceId);
@@ -261,7 +266,7 @@ class CUsbDkControlDevice : private CWdfControlDevice, public CAllocatable<NonPa
261266
bool WaitForDetachment(const USB_DK_DEVICE_ID &ID);
262267

263268
private:
264-
NTSTATUS LoadPersistentHideRules();
269+
NTSTATUS ReloadPersistentHideRules();
265270

266271
CObjHolder<CUsbDkControlDeviceQueue> m_DeviceQueue;
267272
static CRefCountingHolder<CUsbDkControlDevice> *m_UsbDkControlDevice;
@@ -275,6 +280,9 @@ class CUsbDkControlDevice : private CWdfControlDevice, public CAllocatable<NonPa
275280

276281
typedef CWdmSet<CUsbDkHideRule, CLockedAccess, CNonCountingObject> HideRulesSet;
277282
HideRulesSet m_HideRules;
283+
HideRulesSet m_PersistentHideRules;
284+
285+
NTSTATUS AddHideRuleToSet(const USB_DK_HIDE_RULE &UsbDkRule, HideRulesSet &Set);
278286

279287
template <typename TPredicate, typename TFunctor>
280288
bool UsbDevicesForEachIf(TPredicate Predicate, TFunctor Functor)

0 commit comments

Comments
 (0)