Correct way to copy SID in WFP Callouts.

Hi all,

I’m working on a callout driver, and one of its features is to intercept network connection events. I have a callout at the FWPS_LAYER_ALE_FLOW_ESTABLISHED_V4 layer. This layer has a fixed data field FWPS_FIELD_ALE_FLOW_ESTABLISHED_V4_ALE_USER_ID, which contains a TOKEN_ACCESS_INFORMATION pointer that provides the SID of the user account that owns the connection. My goal is to copy this SID to a buffer and pass it to user-mode for further processing. My question is: what is the correct way to copy this SID to the buffer?

I was going to use standard Sid functions, but the documentation is quite clear:

  • The WFP engine calls the callout classify function at IRQL <= DISPATCH_LEVEL.
  • RtlCopySid(), RtlLengthSid(), which are needed to allocate the buffer and copy the sid, must be called at IRQL <= APC_LEVEL.

This means that we can only use the Sid functions to copy the user’s SID when the callout’s classify function is called at IRQL < DISPATCH_LEVEL, but this never actually happens. So we can’t use Sid functions at all, if I’m not missing something.

My other approach was to convert the SID to a Unicode String (RtlConvertSidToUnicodeString()) and pass that string to user mode. And this approach works. But as I know it’s not recommended to use unicode at the DISPATCH_LEVEL, because unicode charactes code pages memory may be paged and in this case the driver will crash.

So what is the correct way to copy user’s SID in this case?

Thank you in advance,
Vitaly

A self relative SID can be copied with a single CopyMemory. A non-self relative SID can be copied by walking the internal pointers and using CopyMemory

Hi, thank you for the answer, it was helpful!