PipChangeDeviceObjectFromRegistryProperties

NTSTATUS __stdcall PipChangeDeviceObjectFromRegistryProperties(
        _DEVICE_OBJECT *PhysicalDeviceObject,
        PVOID DeviceInstanceKey,
        PWCHAR ClassGuid,
        PVOID InstallerClassKey,
        UINT8 UsePdoCharacteristics){
  _DEVOBJ_EXTENSION *DeviceObjectExtension; 
  void *DeviceNode; 
  unsigned int ExtensionFlags; 
  void *v12; 
  WCHAR *v13; 
  int v14; 
  UINT8 RegistryDwordWithFallback; 
  WCHAR *v16; 
  UINT8 v17; 
  WCHAR *v18; 
  _DEVICE_OBJECT *AttachedDevice; 
  int v20; 
  unsigned int v21; 
  VOID *v22; 
  unsigned int v23; 
  VOID *RegistrySecurityWithFallback; 
  UINT8 *v25; 
  VOID *v26; 
  char v27; 
  _DEVICE_OBJECT *i; 
  int OwnerSecurityDescriptor; 
  char v30; 
  _DEVICE_OBJECT *v31; 
  int v33; 
  unsigned int v34; 
  unsigned __int8 SaclPresent; 
  UINT8 OwnerDefaulted; 
  char v37; 
  char v38; 
  UINT8 v39; 
  UINT64 DeviceType; 
  UINT64 Value; 
  PVOID Owner; 
  UINT64 SecurityInformation; 
  PACL Sacl; 
  _ACL *AllocatedSacl; 
  unsigned __int64 v46; 
  _ACL *AllocatedAcl[2]; 
  __int128 v48; 
  __int64 v49; 

  v46 = (unsigned __int64)InstallerClassKey;
  Sacl = (PACL)ClassGuid;
  SaclPresent = 0;
  DeviceObjectExtension = PhysicalDeviceObject->DeviceObjectExtension;
  Owner = 0i64;
  if( PhysicalDeviceObject )
    DeviceNode = DeviceObjectExtension->DeviceNode;
  else
    DeviceNode = 0i64;
  ExtensionFlags = DeviceObjectExtension->ExtensionFlags;
  v12 = 0i64;
  v13 = (WCHAR *)*((_QWORD *)DeviceNode + 6);
  v38 = 0;
  AllocatedSacl = 0i64;
  SecurityInformation = 0i64;
  v14 = ExtensionFlags & 0x800;
  LODWORD(Value) = 0;
  DeviceType = 0i64;
  RegistryDwordWithFallback = PipGetRegistryDwordWithFallback(
                                CM_REG_PROP_DEVTYPE,
                                v13,
                                DeviceInstanceKey,
                                ClassGuid,
                                InstallerClassKey,
                                (UINT64 *)((char *)&DeviceType + 4));
  v16 = (WCHAR *)*((_QWORD *)DeviceNode + 6);
  v37 = RegistryDwordWithFallback;
  v17 = PipGetRegistryDwordWithFallback(
          CM_REG_PROP_EXCLUSIVE,
          v16,
          DeviceInstanceKey,
          ClassGuid,
          InstallerClassKey,
          &Value);
  v18 = (WCHAR *)*((_QWORD *)DeviceNode + 6);
  v39 = v17;
  PipGetRegistryDwordWithFallback(
    CM_REG_PROP_CHARACTERISTICS,
    v18,
    DeviceInstanceKey,
    ClassGuid,
    InstallerClassKey,
    &DeviceType);
  if( UsePdoCharacteristics || (AttachedDevice = PhysicalDeviceObject->AttachedDevice) == 0i64 )
    AttachedDevice = PhysicalDeviceObject;
  v20 = 0;
  while( AttachedDevice )
  {
    v20 |= AttachedDevice->Characteristics;
    AttachedDevice = AttachedDevice->AttachedDevice;
  }
  v21 = 0;
  v22 = (VOID *)*((_QWORD *)DeviceNode + 6);
  v23 = (DeviceType | v20) & 0x5010F;
  LODWORD(DeviceType) = 0;
  RegistrySecurityWithFallback = PipGetRegistrySecurityWithFallback(
                                   (WCHAR *)(v46 & -(__int64)(v14 != 0)),
                                   v22,
                                   (WCHAR *)DeviceInstanceKey,
                                   (VOID *)((unsigned __int64)Sacl & -(__int64)(v14 != 0)));
  v26 = RegistrySecurityWithFallback;
  if( RegistrySecurityWithFallback )
  {
    OwnerSecurityDescriptor = RtlGetOwnerSecurityDescriptor(RegistrySecurityWithFallback, &Owner, &OwnerDefaulted);
    if( OwnerSecurityDescriptor < 0 )
      goto LABEL_22;
    v33 = 0;
    if( Owner )
    {
      v21 = 1;
      v33 = 1;
    }
    OwnerSecurityDescriptor = RtlGetGroupSecurityDescriptor(v26, &Owner, &OwnerDefaulted);
    if( OwnerSecurityDescriptor < 0 )
      goto LABEL_22;
    if( Owner )
    {
      v21 = v33 | 2;
      v33 |= 2u;
    }
    OwnerSecurityDescriptor = RtlGetSaclSecurityDescriptor(v26, &SaclPresent, &Sacl, &OwnerDefaulted);
    if( OwnerSecurityDescriptor < 0 )
      goto LABEL_22;
    if( SaclPresent )
    {
      v21 = v33 | 8;
      v33 |= 8u;
    }
    OwnerSecurityDescriptor = RtlGetDaclSecurityDescriptor(v26, &SaclPresent, &Sacl, &OwnerDefaulted);
    if( OwnerSecurityDescriptor < 0 )
      goto LABEL_22;
    v34 = HIDWORD(DeviceType);
    if( SaclPresent )
      v21 = v33 | 4;
    v27 = v37;
    goto LABEL_41;
  }
  v27 = v37;
  if( v37 )
  {
    v34 = HIDWORD(DeviceType);
    v49 = 0i64;
    *(_OWORD *)AllocatedAcl = 0i64;
    v48 = 0i64;
    v26 = IopCreateDefaultDeviceSecurityDescriptor(
            HIDWORD(DeviceType),
            v23,
            v25,
            AllocatedAcl,
            &AllocatedSacl,
            &SecurityInformation,
            (UINT64)&DeviceType);
    if( !v26 )
    {
      v12 = (void *)SecurityInformation;
      OwnerSecurityDescriptor = -1073741823;
      goto LABEL_23;
    }
    v21 = DeviceType;
    v38 = 1;
LABEL_41:
    if( v27 )
      PhysicalDeviceObject->DeviceType = v34;
  }
  if( v39 && (_DWORD)Value )
    PhysicalDeviceObject->Flags |= 8u;
  PhysicalDeviceObject->Characteristics = v23 | PhysicalDeviceObject->Characteristics & 0xFFFAFEF0;
  for( i = PhysicalDeviceObject->AttachedDevice; i; i = i->AttachedDevice )
    i->Characteristics |= v23;
  OwnerSecurityDescriptor = 0;
  if( v26 )
    OwnerSecurityDescriptor = ObSetSecurityObjectByPointer(PhysicalDeviceObject, v21, v26);
  v30 = v38;
  v31 = PhysicalDeviceObject;
  do
  {
    if( (v31->DriverObject->Flags & 0x100) != 0 )
      *((_DWORD *)PhysicalDeviceObject->DeviceObjectExtension->DeviceNode + 99) |= 0xC00000u;
    if( v30 && v31->SecurityDescriptor )
      v31->DeviceObjectExtension->ExtensionFlags |= 0x800u;
    v31 = v31->AttachedDevice;
  }
  while( v31 );
  v12 = (void *)SecurityInformation;
LABEL_22:
  if( v26 && !v38 )
    ExFreePoolWithTag(v26, 0);
LABEL_23:
  if( AllocatedSacl )
    ExFreePoolWithTag(AllocatedSacl, 0);
  if( v12 )
    ExFreePoolWithTag(v12, 0);
  return OwnerSecurityDescriptor;
}

Referenced by:

PipCallDriverAddDevice