IopAttachDeviceToDeviceStackSafe

_DEVICE_OBJECT *__stdcall IopAttachDeviceToDeviceStackSafe(
        _DEVICE_OBJECT *SourceDevice,
        _DEVICE_OBJECT *TargetDevice,
        _DEVICE_OBJECT **AttachedToDeviceObject){
  _DEVOBJ_EXTENSION *DeviceObjectExtension; 
  _DEVICE_OBJECT *v6; 
  _UNICODE_STRING *v7; 
  UINT8 v8; 
  __int64 v9; 
  __int64 v10; 
  _DEVICE_OBJECT *DeviceAttachmentBase; 
  _BYTE *v12; 
  _DEVICE_OBJECT *v13; 
  unsigned __int64 DeviceType; 
  __int64 v15; 
  unsigned __int8 v16; 
  PVOID *DeviceNode; 
  char v18; 
  unsigned int v19; 
  int v20; 
  char v22; 
  _DRIVER_OBJECT *DriverObject; 
  __int64 Buffer; 
  _DRIVER_OBJECT *v25; 
  ULONG *v26; 
  __int64 *v27; 
  __int16 v28; 
  NTSTATUS NameString; 
  VOID **PoolWithTag; 
  bool v31; 
  unsigned __int16 v32; 
  _DRIVER_OBJECT *v33; 
  int v34; 
  __int64 v35; 
  __int64 v36; 
  char v37; 
  PULONG ReturnLength; 
  __int16 v39; 
  __int16 v40; 
  _DEVICE_OBJECT *DiskDeviceObject; 
  __int64 v42[2]; 
  _DEVICE_OBJECT *v43; 
  _EVENT_DATA_DESCRIPTOR UserData; 
  __int64 v45; 
  int v46; 
  int v47; 
  _EVENT_DATA_DESCRIPTOR v48; 
  __int64 v49; 
  int v50; 
  int v51; 
  __int16 *v52; 
  int v53; 
  int v54; 
  __int64 v55; 
  int v56; 
  int v57; 
  ULONG Length[16]; 

  DeviceObjectExtension = SourceDevice->DeviceObjectExtension;
  v43 = TargetDevice;
  v6 = TargetDevice;
  v8 = KeAcquireQueuedSpinLock(10i64, (INT64)TargetDevice, (INT64)AttachedToDeviceObject);
  if( (MmVerifierData & 0x10) != 0 )
    IovAttachDeviceToDeviceStack(SourceDevice, v6, v7);
  LODWORD(v9) = IoGetAttachedDevice((INT64)v6);
  v10 = v9;
  DeviceAttachmentBase = IopGetDeviceAttachmentBase(v6);
  LOBYTE(v12) = 0;
  v13 = DeviceAttachmentBase;
  v37 = 0;
  DeviceType = DeviceAttachmentBase->DeviceType;
  if( (unsigned int)DeviceType <= 0x35 )
  {
    v15 = 0x20000100100108i64;
    if( _bittest64(&v15, DeviceType) )
    {
      if( (v13->Flags & 0x10000000) != 0 || *(&stru_140CF2E80 + 1659) )
      {
        v37 = 1;
        IopIsKnownGoodLegacyFsFilter(&SourceDevice->DriverObject->DriverName, v12);
        if( v22 )
        {
          LOBYTE(v12) = 0;
          v37 = 0;
        }
        else
        {
          ObfReferenceObjectWithTag(v13, 0x746C6644ui64);
          LOBYTE(v12) = 1;
        }
      }
    }
  }
  if( (*(_DWORD *)(v10 + 48) & 0x80u) != 0
    || (*(_DWORD *)(*(_QWORD *)(v10 + 312) + 32i64) & 0xF) != 0
    || (v16 = *(_BYTE *)(v10 + 76), v16 >= 0x7Du)
    || (_BYTE)v12 )
  {
    v10 = 0i64;
    if( AttachedToDeviceObject )
      *AttachedToDeviceObject = 0i64;
  }
  else
  {
    if( AttachedToDeviceObject )
    {
      *AttachedToDeviceObject = (_DEVICE_OBJECT *)v10;
      v16 = *(_BYTE *)(v10 + 76);
    }
    SourceDevice->StackSize = v16 + 1;
    SourceDevice->AlignmentRequirement = *(_DWORD *)(v10 + 152);
    SourceDevice->SectorSize = *(_WORD *)(v10 + 304);
    if( (*(_DWORD *)(*(_QWORD *)(v10 + 312) + 32i64) & 0x10) != 0 )
      SourceDevice->DeviceObjectExtension->ExtensionFlags |= 0x10u;
    if( (*(_DWORD *)(v10 + 48) & 0x8000000) != 0 )
      SourceDevice->Flags |= 0x8000000u;
    _InterlockedExchange64((volatile __int64 *)(v10 + 24), (__int64)SourceDevice);
    ++*(_WORD *)(v10 + 306);
    DeviceObjectExtension->AttachedTo = (_DEVICE_OBJECT *)v10;
    DeviceNode = (PVOID *)v13->DeviceObjectExtension->DeviceNode;
    if( DeviceNode && *((int *)DeviceNode + 99) < 0 )
      PnpRequestDeviceAction(DeviceNode[4], 8, 0, 1i64, 0i64, 0i64, 0i64);
  }
  KeReleaseQueuedSpinLock(LockQueueIoDatabaseLock, v8);
  v18 = v37;
  if( v37 )
  {
    if( *(&stru_140CF2E80 + 1659) )
    {
      if( EtwEventEnabled(*(&stru_140C452E0 + 327), &IoMgr_LegacyFsFilterBlockedByPolicy) )
      {
        DriverObject = SourceDevice->DriverObject;
        WORD2(ReturnLength) = DriverObject->DriverName.Length >> 1;
        UserData.Ptr = (unsigned __int64)&ReturnLength + 4;
        *(_QWORD *)&UserData.Size = 2i64;
        Buffer = (__int64)DriverObject->DriverName.Buffer;
        v46 = DriverObject->DriverName.Length;
        v45 = Buffer;
        v47 = 0;
        EtwWriteEx(
          *(&stru_140C452E0 + 327),
          &IoMgr_LegacyFsFilterBlockedByPolicy,
          0i64,
          0,
          (_LPCGUID)KeGetCurrentThread()[1].WaitBlock[1].WaitListEntry.Flink,
          0,
          2u,
          &UserData);
      }
    }
    else if( EtwEventEnabled(*(&stru_140C452E0 + 327), &IoMgr_LegacyFsFilterBlockedOnScm) )
    {
      v25 = SourceDevice->DriverObject;
      v26 = Length;
      v42[0] = 1835034i64;
      v27 = v42;
      v28 = v25->DriverName.Length >> 1;
      DiskDeviceObject = 0i64;
      v39 = v28;
      v42[1] = (__int64)L"(Unavailable)";
      LODWORD(ReturnLength) = 64;
      if( KeGetCurrentIrql() < 2u && IoGetDiskDeviceObject(v13, &DiskDeviceObject) >= 0 )
      {
        NameString = ObQueryNameString(
                       DiskDeviceObject,
                       (_OBJECT_NAME_INFORMATION *)Length,
                       (unsigned int)ReturnLength,
                       (UINT64 *)&ReturnLength);
        if( NameString == -1073741820 )
        {
          PoolWithTag = ExAllocatePoolWithTag(1ui64, (unsigned int)ReturnLength, 1850699593i64);
          v26 = (ULONG *)PoolWithTag;
          if( PoolWithTag )
            NameString = ObQueryNameString(
                           DiskDeviceObject,
                           (_OBJECT_NAME_INFORMATION *)PoolWithTag,
                           (unsigned int)ReturnLength,
                           (UINT64 *)&ReturnLength);
          else
            NameString = -1073741670;
        }
        ObfDereferenceObjectWithTag(DiskDeviceObject, 0x746C6644ui64);
        v31 = NameString < 0;
        v6 = v43;
        if( !v31 )
          v27 = (__int64 *)v26;
      }
      v32 = *(_WORD *)v27;
      v48.Reserved = 0;
      v40 = v32 >> 1;
      v48.Ptr = (unsigned __int64)&v39;
      v33 = SourceDevice->DriverObject;
      v48.Size = 2;
      v34 = v33->DriverName.Length;
      v35 = (__int64)v33->DriverName.Buffer;
      v51 = 0;
      v54 = 0;
      v50 = v34;
      v49 = v35;
      v52 = &v40;
      v53 = 2;
      v36 = v27[1];
      v56 = *(unsigned __int16 *)v27;
      v57 = 0;
      v55 = v36;
      EtwWriteEx(
        *(&stru_140C452E0 + 327),
        &IoMgr_LegacyFsFilterBlockedOnScm,
        0i64,
        0,
        (_LPCGUID)KeGetCurrentThread()[1].WaitBlock[1].WaitListEntry.Flink,
        0,
        4u,
        &v48);
      if( v26 && v26 != Length )
        ExFreePoolWithTag(v26, 0);
      v18 = v37;
    }
    if( v18 )
      ObfDereferenceObjectWithTag(v13, 0x746C6644ui64);
  }
  v19 = v6->DeviceType;
  if( v19 <= 0x14 )
  {
    v20 = 1048840;
    if( _bittest(&v20, v19) )
    {
      if( *(&stru_140CF2E80 + 599) )
        (*(void(__fastcall **)(_DEVICE_OBJECT *, _DEVICE_OBJECT *))(*(&stru_140CF2E80 + 599) + 40i64))(
          SourceDevice,
          v6);
    }
  }
  return(_DEVICE_OBJECT *)v10;
}

Referenced by:

IoAttachDeviceByPointer
IoAttachDeviceToDeviceStack
IoAttachDeviceToDeviceStackSafe