SepInsertOrReferenceSharedSidEntries

NTSTATUS __stdcall SepInsertOrReferenceSharedSidEntries(
        _SID_AND_ATTRIBUTES *InputSids,
        _SID_AND_ATTRIBUTES *OutputSids,
        UINT64 SidArrayCount){
  _ETHREAD *CurrentThread; 
  NTSTATUS v4; 
  unsigned int v5; 
  __int64 v8; 
  signed __int64 v9; 
  signed __int64 v10; 
  _RTL_DYNAMIC_HASH_TABLE_ENTRY *SharedSidEntry; 
  _LIST_ENTRY *Blink; 
  VOID **PoolWithTag; 
  _RTL_DYNAMIC_HASH_TABLE_ENTRY *v14; 
  __int64 v15; 
  UINT64 v16; 
  _RTL_DYNAMIC_HASH_TABLE_ENTRY *v17; 
  __int64 v18; 
  _EX_PUSH_LOCK *v19; 
  unsigned int v21; 
  int v22; 

  CurrentThread = (_ETHREAD *)KeGetCurrentThread();
  v4 = 0;
  v5 = SidArrayCount;
  --CurrentThread->Tcb.KernelApcDisable;
  ExAcquirePushLockExclusiveEx(g_SepSidMapping, 0i64);
  v8 = 0i64;
  if( !v5 )
    goto LABEL_24;
  v9 = (char *)InputSids - (char *)OutputSids;
  v10 = (char *)OutputSids - (char *)InputSids;
  while( 1 )
  {
    *(unsigned int *)((char *)&InputSids->Attributes + v10) = *(unsigned int *)((char *)&InputSids->Attributes + v10 + v9);
    SharedSidEntry = SepFindSharedSidEntry(InputSids->Sid);
    if( SharedSidEntry )
    {
      if( _InterlockedIncrement64((volatile signed __int64 *)&SharedSidEntry[1]) <= 1 )
        __fastfail(0xEu);
      Blink = SharedSidEntry[1].Linkage.Blink;
      goto LABEL_12;
    }
    v21 = 8 * *((unsigned __int8 *)InputSids->Sid + 1) + 96;
    PoolWithTag = ExAllocatePoolWithTag(1ui64, v21, 1934845267i64);
    v14 = (_RTL_DYNAMIC_HASH_TABLE_ENTRY *)PoolWithTag;
    if( !PoolWithTag )
      break;
    PoolWithTag[4] = PoolWithTag + 5;
    PoolWithTag[3] = (VOID *)1;
    RtlCopySid(v21 - 40, PoolWithTag + 5, InputSids->Sid);
    v15 = (unsigned int)*((unsigned __int8 *)InputSids->Sid + 1) - 1;
    v16 = *((unsigned int *)InputSids->Sid + v15 + 2);
    if( !*((_DWORD *)InputSids->Sid + v15 + 2) )
      v16 = 1i64;
    if( !RtlInsertEntryHashTable((_RTL_DYNAMIC_HASH_TABLE *)g_SepSidMapping[1]._bf_0, v14, v16, 0i64) )
    {
      v4 = -1073741823;
      v22 = -1073741823;
      ExFreePoolWithTag(v14, 0x73536553u);
      goto LABEL_16;
    }
    Blink = v14[1].Linkage.Blink;
LABEL_12:
    *(void **)((char *)&InputSids->Sid + v10) = Blink;
    v8 = (unsigned int)(v8 + 1);
    ++InputSids;
    if( (unsigned int)v8 >= v5 )
    {
      v4 = 0;
      goto LABEL_24;
    }
  }
  v4 = -1073741801;
  v22 = -1073741801;
LABEL_16:
  if( (_DWORD)v8 )
  {
    do
    {
      v17 = SepFindSharedSidEntry(OutputSids->Sid);
      v18 = _InterlockedDecrement64((volatile signed __int64 *)&v17[1]);
      if( v18 <= 0 )
      {
        if( v18 )
          __fastfail(0xEu);
        if( RtlRemoveEntryHashTable((_RTL_DYNAMIC_HASH_TABLE *)g_SepSidMapping[1]._bf_0, v17, 0i64) )
          ExFreePoolWithTag(v17, 0);
      }
      ++OutputSids;
      --v8;
    }
    while( v8 );
    v4 = v22;
  }
LABEL_24:
  v19 = g_SepSidMapping;
  if( (_InterlockedExchangeAdd64(&g_SepSidMapping->_bf_0, 0xFFFFFFFFFFFFFFFFui64) & 6) == 2 )
    ExfTryToWakePushLock(v19);
  KeAbPostRelease(v19);
  KeLeaveCriticalRegion();
  return v4;
}

Referenced by:

SepSetTokenCapabilities