22#include " ObjectAttributes.h"
33#include " ScopeExit.h"
44#include " VariableSizeStruct.h"
5+ #include " IWinApi.h"
56
67namespace kf
78{
@@ -69,7 +70,7 @@ namespace kf
6970 class FltCommunicationPort
7071 {
7172 public:
72- FltCommunicationPort () : m_filter(), m_port()
73+ FltCommunicationPort (IWinApi& api ) : m_api(api), m_filter(), m_port()
7374 {
7475 }
7576
@@ -85,26 +86,26 @@ namespace kf
8586 m_filter = filter;
8687
8788 PSECURITY_DESCRIPTOR securityDescriptor = nullptr ;
88- NTSTATUS status = :: FltBuildDefaultSecurityDescriptor (&securityDescriptor, FLT_PORT_ALL_ACCESS);
89+ NTSTATUS status = m_api. FltBuildDefaultSecurityDescriptor (&securityDescriptor, FLT_PORT_ALL_ACCESS);
8990
9091 if (!NT_SUCCESS (status))
9192 {
9293 return status;
9394 }
9495
95- SCOPE_EXIT{ :: FltFreeSecurityDescriptor (securityDescriptor); };
96+ SCOPE_EXIT{ m_api. FltFreeSecurityDescriptor (securityDescriptor); };
9697
9798 VariableSizeStruct<SYSTEM_MANDATORY_LABEL_ACE, PagedPool> lowIntegrityAce;
9899 VariableSizeStruct<ACL, PagedPool> sacl;
99100 if (allowNonAdmins)
100101 {
101- status = RtlSetDaclSecurityDescriptor (securityDescriptor, true , nullptr , false );
102+ status = m_api. RtlSetDaclSecurityDescriptor (securityDescriptor, true , nullptr , false );
102103 if (!NT_SUCCESS (status))
103104 {
104105 return status;
105106 }
106107
107- const auto lowMandatorySidLength = RtlLengthSid (SeExports->SeLowMandatorySid );
108+ const auto lowMandatorySidLength = m_api. RtlLengthSid (SeExports->SeLowMandatorySid );
108109 status = lowIntegrityAce.emplace (FIELD_OFFSET (SYSTEM_MANDATORY_LABEL_ACE, SidStart) + lowMandatorySidLength);
109110 if (!NT_SUCCESS (status))
110111 {
@@ -114,7 +115,7 @@ namespace kf
114115 lowIntegrityAce->Header .AceType = SYSTEM_MANDATORY_LABEL_ACE_TYPE;
115116 lowIntegrityAce->Header .AceSize = static_cast <USHORT>(FIELD_OFFSET (SYSTEM_MANDATORY_LABEL_ACE, SidStart) + lowMandatorySidLength);
116117 lowIntegrityAce->Mask = 0 ;
117- status = RtlCopySid (lowMandatorySidLength, &lowIntegrityAce->SidStart , SeExports->SeLowMandatorySid );
118+ status = m_api. RtlCopySid (lowMandatorySidLength, &lowIntegrityAce->SidStart , SeExports->SeLowMandatorySid );
118119 if (!NT_SUCCESS (status))
119120 {
120121 return status;
@@ -126,19 +127,19 @@ namespace kf
126127 {
127128 return status;
128129 }
129- status = RtlCreateAcl (sacl.get (), saclSize, ACL_REVISION);
130+ status = m_api. RtlCreateAcl (sacl.get (), saclSize, ACL_REVISION);
130131 if (!NT_SUCCESS (status))
131132 {
132133 return status;
133134 }
134135
135- status = RtlAddAce (sacl.get (), ACL_REVISION, 0 , static_cast <PVOID>(lowIntegrityAce.get ()), lowIntegrityAce->Header .AceSize );
136+ status = m_api. RtlAddAce (sacl.get (), ACL_REVISION, 0 , static_cast <PVOID>(lowIntegrityAce.get ()), lowIntegrityAce->Header .AceSize );
136137 if (!NT_SUCCESS (status))
137138 {
138139 return status;
139140 }
140141
141- status = RtlSetSaclSecurityDescriptor (securityDescriptor, true , sacl.get (), false );
142+ status = m_api. RtlSetSaclSecurityDescriptor (securityDescriptor, true , sacl.get (), false );
142143 if (!NT_SUCCESS (status))
143144 {
144145 return status;
@@ -147,14 +148,14 @@ namespace kf
147148
148149 ObjectAttributes oa (&name, securityDescriptor);
149150
150- return :: FltCreateCommunicationPort (filter, &m_port, &oa, this , connectNotify, disconnectNotify, messageNotify, maxConnections);
151+ return m_api. FltCreateCommunicationPort (filter, &m_port, &oa, this , connectNotify, disconnectNotify, messageNotify, maxConnections);
151152 }
152153
153154 void close ()
154155 {
155156 if (m_port)
156157 {
157- :: FltCloseCommunicationPort (m_port);
158+ m_api. FltCloseCommunicationPort (m_port);
158159 m_port = nullptr ;
159160 }
160161
@@ -175,7 +176,7 @@ namespace kf
175176 {
176177 ASSERT (serverPortCookie);
177178 auto self = static_cast <FltCommunicationPort*>(serverPortCookie);
178- return Handler::onConnect (self->m_filter , clientPort, connectionContext, connectionContextLength, reinterpret_cast <Handler**>(connectionCookie));
179+ return Handler::onConnect (self->m_filter , clientPort, connectionContext, connectionContextLength, reinterpret_cast <Handler**>(connectionCookie), self-> m_api );
179180 }
180181
181182 static VOID FLTAPI disconnectNotify (
@@ -214,15 +215,15 @@ namespace kf
214215 {
215216 if (inputBufferLength)
216217 {
217- inputMdl = IoAllocateMdl (inputBuffer, inputBufferLength, false , false , nullptr );
218+ inputMdl = handler-> m_api . IoAllocateMdl (inputBuffer, inputBufferLength, false , false , nullptr );
218219 if (!inputMdl)
219220 {
220221 return STATUS_INSUFFICIENT_RESOURCES;
221222 }
222223
223- MmProbeAndLockPages (inputMdl, KernelMode, IoReadAccess);
224+ handler-> m_api . MmProbeAndLockPages (inputMdl, KernelMode, IoReadAccess);
224225
225- inputBuffer = MmGetSystemAddressForMdlSafe (inputMdl, NormalPagePriority | MdlMappingNoExecute | MdlMappingNoWrite);
226+ inputBuffer = handler-> m_api . MmGetSystemAddressForMdlSafe (inputMdl, NormalPagePriority | MdlMappingNoExecute | MdlMappingNoWrite);
226227 if (!inputBuffer)
227228 {
228229 return STATUS_INSUFFICIENT_RESOURCES;
@@ -231,15 +232,15 @@ namespace kf
231232
232233 if (outputBufferLength)
233234 {
234- outputMdl = IoAllocateMdl (outputBuffer, outputBufferLength, false , false , nullptr );
235+ outputMdl = handler-> m_api . IoAllocateMdl (outputBuffer, outputBufferLength, false , false , nullptr );
235236 if (!outputMdl)
236237 {
237238 return STATUS_INSUFFICIENT_RESOURCES;
238239 }
239240
240- MmProbeAndLockPages (outputMdl, KernelMode, IoWriteAccess);
241+ handler-> m_api . MmProbeAndLockPages (outputMdl, KernelMode, IoWriteAccess);
241242
242- outputBuffer = MmGetSystemAddressForMdlSafe (outputMdl, NormalPagePriority | MdlMappingNoExecute);
243+ outputBuffer = handler-> m_api . MmGetSystemAddressForMdlSafe (outputMdl, NormalPagePriority | MdlMappingNoExecute);
243244 if (!outputBuffer)
244245 {
245246 return STATUS_INSUFFICIENT_RESOURCES;
@@ -258,16 +259,16 @@ namespace kf
258259 // Cleanup
259260 //
260261
261- auto freeMdl = [](PMDL& mdl)
262+ auto freeMdl = [&handler ](PMDL& mdl)
262263 {
263264 if (mdl)
264265 {
265266 if (FlagOn (mdl->MdlFlags , MDL_PAGES_LOCKED))
266267 {
267- MmUnlockPages (mdl);
268+ handler-> m_api . MmUnlockPages (mdl);
268269 }
269270
270- IoFreeMdl (mdl);
271+ handler-> m_api . IoFreeMdl (mdl);
271272 mdl = nullptr ;
272273 }
273274 };
@@ -281,5 +282,6 @@ namespace kf
281282 private:
282283 PFLT_FILTER m_filter;
283284 PFLT_PORT m_port;
285+ IWinApi& m_api;
284286 };
285287} // namespace
0 commit comments