Bug 14276

Summary: Проблема "Some or all identity references could not be translated"
Product: WINE@Etersoft Reporter: Алексей Герасимов <alexger97>
Component: dotNET; .NET; monoAssignee: Алексей Герасимов <alexger97>
Status: CLOSED LATER QA Contact: Vitaly Lipatov <lav>
Severity: minor    
Priority: P4 CC: lav, mx
Version: unspecified   
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Whiteboard:
Заявки RT: Связано с:
Дата напоминания:
Bug Depends on: 14293    
Bug Blocks: 14274, 16605    

Description Алексей Герасимов 2019-11-05 19:05:35 MSK
Ошибка, возникающая при работе в пропатченной версии wine Виталия. Связана, по моим догадкам, с получением WindowsIdentity (токена текущего потока / процесса).

Необходимо детальнее понять причину проблемы.
Comment 1 Алексей Герасимов 2019-11-05 19:08:24 MSK
 Запуск через wwconsole IdentityReferenceTest.exe на московском стенде в Wine 

SecurityIdentifier1
Some or all identity references could not be translated.   at System.Security.Pr
incipal.SecurityIdentifier.Translate(IdentityReferenceCollection sourceSids, Typ
e targetType, Boolean forceSuccess)
   at System.Security.Principal.SecurityIdentifier.Translate(Type targetType)
   at System.Security.Principal.WindowsIdentity.GetName()
   at System.Security.Principal.WindowsIdentity.get_Name()
   at IdentityReferenceTest.Test.MakeTest1() in C:\Users\alexg\source\repos\Iden
tityReferenceTest\IdentityReferenceTest\Test.cs:line 28
   at IdentityReferenceTest.Program.Main(String[] args) in C:\Users\alexg\source
\repos\IdentityReferenceTest\IdentityReferenceTest\Program.cs:line 


Здесь мы сталкиваемся уже с  ошибкой, связанной с получением, как мне представляется, Windows Identity. 

Возможно, дело в не совсем корректной сборке wine. 

Проблема возникла при тестировании https://bugs.etersoft.ru/show_bug.cgi?id=14274
Comment 2 Vitaly Lipatov 2019-11-15 01:32:01 MSK
Похоже, что там SID транслируется в бинарную форму средствами WinAPI, а потом собирается обратно из байтов кодом на C# и происходит сравнение. Причём результат не совпадает (в триаде, соответствующей домену, начинают совпадать значения).

Конвертация в байты:
internal static int CreateSidFromString(string stringSid, out byte[] resultSid)
{
    IntPtr zero = IntPtr.Zero;
    int lastWin32Error;
    try
    {
        if (1 != Win32Native.ConvertStringSidToSid(stringSid, out zero))
        {
            lastWin32Error = Marshal.GetLastWin32Error();
            goto IL_2D;
        }
        resultSid = Win32.ConvertIntPtrSidToByteArraySid(zero);
...
}

При обратном преобразовании получаем
AccountDomainSid	{S-1-5-21-985848698-2147483647-2147483647}

При том что он должен остаться
S-1-5-21-985848698-3514887597-2876320117
Comment 3 Vitaly Lipatov 2019-11-15 02:05:32 MSK
(Ответ Vitaly Lipatov на комментарий #2)
...
>         if (1 != Win32Native.ConvertStringSidToSid(stringSid, out zero))
>         {
>             lastWin32Error = Marshal.GetLastWin32Error();
>             goto IL_2D;
>         }
>         resultSid = Win32.ConvertIntPtrSidToByteArraySid(zero);

В общем, сумасшедшая функция
ParseStringSidToSid

Работа с которой выглядит как-то так:
0096:trace:advapi:ConvertStringSecurityDescriptorToSecurityDescriptorW L"D:P(A;;GRGW;;;BA)(A;;GRGW;;;S-1-5-21-985848698-3514887597-2876320117-1644)S:(ML;;NWNR;;;S-1-16-12288)", 1, 0x346f7e0, 0x346f7c8
0096:trace:advapi:ParseStringAclToAcl L"P(A;;GRGW;;;BA)(A;;GRGW;;;S-1-5-21-985848698-3514887597-2876320117-1644)"
0096:trace:advapi:ParseStringSidToSid L"BA)(A;;GRGW;;;S-1-5-21-985848698-3514887597-2876320117-1644)", (nil), 0x346f6ec
0096:trace:advapi:ParseStringSidToSid only size requested, returning TRUE with 16
0096:trace:advapi:ParseStringSidToSid L"S-1-5-21-985848698-3514887597-2876320117-1644)", (nil), 0x346f6ec
0096:trace:advapi:ParseStringSidToSid only size requested, returning TRUE with 28
0096:trace:advapi:ParseStringAclToAcl L"(ML;;NWNR;;;S-1-16-12288)"
0096:trace:advapi:ParseStringSidToSid L"S-1-16-12288)", (nil), 0x346f6ec
0096:trace:advapi:ParseStringSidToSid only size requested, returning TRUE with 12
0096:trace:advapi:ParseStringAclToAcl L"P(A;;GRGW;;;BA)(A;;GRGW;;;S-1-5-21-985848698-3514887597-2876320117-1644)"
0096:trace:advapi:ParseStringSidToSid L"BA)(A;;GRGW;;;S-1-5-21-985848698-3514887597-2876320117-1644)", 0x14d72c, 0x346f6ec
0096:fixme:advapi:ParseStringSidToSid WellKnownSids[27] L"BA)(A;;GRGW;;;S-1-5-21-985848698-3514887597-2876320117-1644)"


И в которой умные люди использовали atoiW для конвертации числа.
Comment 4 Vitaly Lipatov 2019-11-15 02:39:18 MSK
Накопал, что OpenThreadToken возвращает hr
0x800703F0	-2147023888	ERROR NO TOKEN	An attempt was made to reference a token that does not exist

Но потом OpenProcessToken нормально возвращает токен 0x134.

Получив его, почему-то бодро делаем new WindowsIdentity()

В котором меня сильно смущают
IsGuest>{System.Security.SecurityException: Нет маркера олицетворения
  at System.Security.Principal.WindowsIdentity.CheckNtTokenForSid(SecurityIdentifier sid)
   at System.Security.Principal.WindowsIdentity.get_IsGuest()

Name<-->{System.Security.Principal.IdentityNotMappedException: Some or all identity references could not be translated.^M
   at System.Security.Principal.SecurityIdentifier.Translate(IdentityReferenceCollection sourceSids, Type targetType, Boolean forceSuccess)^M
   at System.Security.Principal.SecurityIdentifier.Translate(Type targetType)^M
   at System.Security.Principal.WindowsIdentity.GetName()^M
   at System.Security.Principal.WindowsIdentity.get_Name()}<--->string {System.Security.Principal.IdentityNotMappedException}^

Вызывается там примерно
if (!Interop.Advapi32.DuplicateTokenEx(
...

// CheckTokenMembership will check if the SID is both present and enabled in the access token.
if (!Interop.Advapi32.CheckTokenMembership((til != TokenImpersonationLevel.None ? _safeTokenHandle : token),
                                                      sid.BinaryForm,
                                                      ref isMember))
                    throw new SecurityException(new Win32Exception().Message);
...

https://github.com/dotnet/corefx/blob/3b6ec2ba944fb72f921f95b99b6ebf2e7bb3dc5a/src/System.Security.Principal.Windows/src/System/Security/Principal/WindowsIdentity.cs
Comment 5 Vitaly Lipatov 2019-11-15 03:25:21 MSK
После хака в LsaQueryInformationPolicy
https://bugs.etersoft.ru/show_bug.cgi?id=14292

и с всегда заTRUEнным IsWellKnownSid

стал получать
00f8:err:ole:COMPOBJ_DllList_Add couldn't load in-process dll L"adsnt.dll"
00f8:err:ole:CoGetClassObject no class object {250e91a0-0367-11cf-abc4-02608c9e7553} could be created for context 0x1
00f8:fixme:ntdll:EtwEventRegister ({8e9f5090-2d75-4d03-8a81-e5afbf85daf1}, 0x45504b6, (nil), 0x12dd79c) stub.
\u0414\u043e\u0441\u0442\u0443\u043f \u0437\u0430\u043f\u0440\u0435\u0449\u0451\u043d.
   at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   at System.DirectoryServices.DirectoryEntry.Bind()
   at System.DirectoryServices.DirectoryEntry.RefreshCache()

Но пришлось не трогать IsWellKnownSid и тогда я поставил в нём
int *a = 4; *a = 4, получив цепочку вызовов:

Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at Microsoft.Win32.Win32Native.LsaLookupSids(SafeLsaPolicyHandle handle, Int32 count, IntPtr[] sids, SafeLsaMemoryHandle& referencedDomains, SafeLsaMemoryHandle& names)
   at System.Security.Principal.SecurityIdentifier.TranslateToNTAccounts(IdentityReferenceCollection sourceSids, Boolean& someFailed)
   at System.Security.Principal.SecurityIdentifier.Translate(IdentityReferenceCollection sourceSids, Type targetType, Boolean& someFailed)
   at System.Security.Principal.SecurityIdentifier.Translate(IdentityReferenceCollection sourceSids, Type targetType, Boolean forceSuccess)
   at System.Security.Principal.SecurityIdentifier.Translate(Type targetType)
   at System.Security.Principal.WindowsIdentity.GetName()
   at System.Security.Principal.WindowsIdentity.get_Name()
Comment 6 Vitaly Lipatov 2019-11-15 04:16:39 MSK
Надо сказать, что LsaLookupSids выводит
005f:trace:advapi:LsaLookupSids mapped 1 out of 1

а заканчивается так:
    TRACE("mapped %u out of %u\n", mapped, Count);

    if (mapped == Count) return STATUS_SUCCESS;
    if (mapped) return STATUS_SOME_NOT_MAPPED;
    return STATUS_NONE_MAPPED;
}

В итоге ничего нового, ошибка формируется в System.Security.Principal.SecurityIdentifier.TranslateToNTAccounts

Где вызывается LsaLookupSids, который вызывает LookupAccountNameW, в которой у нас вообще написано, что она только для локальных компьютеров. В итоге для транслированного имени остаётся флаг SidTypeUnknown (8) вместо SidTypeDomain (3) и получаем то, что получаем.

https://github.com/dotnet/corefx/blob/2133679ff62222ba2734232ab8fa4c65765e82a2/src/System.Security.Principal.Windows/src/System/Security/Principal/SID.cs
Comment 7 Михаил Михайлов 2025-10-15 18:10:59 MSK
Проект заморожен. Ставлю LATER
Comment 8 Михаил Михайлов 2025-10-15 18:11:08 MSK
Бага закрыта