A estrutura SECURITY_ATTRIBUTES e CreateNamedPipe ()

Meu cenário é o seguinte: o processo que cria o objeto de pipe nomeado comCreateNamedPipe() possui privilégios de administrador, mas o processo do cliente "conectando-se" a ele comCreateFile() não. Passando umNULL como o último argumento paraCreateNamedPipe() parece padrão para direitos de acesso somente de administrador.

Como um hack, tentei fazer um servidorImpersonateLoggedOnUser()/RevertToSelf() método para a duração do código relacionado ao canal, mas falha. Parece-me que a melhor coisa a fazer aqui é realmente definir umSECURITY_ATTRIBUTES struct para o último parâmetro deCreateNamedPipe(), mas estou tendo problemas para descobrir como fazer isso.

oExemplo MSDN tem um exemplo referente à manipulação de chave de registro, mas não tenho o conhecimento necessário para adaptá-lo aos meus propósitos.

Isto é o que eu tentei:

if (!AllocateAndInitializeSid(&SIDAuthWorld, 1,
    SECURITY_WORLD_RID,
    0, 0, 0, 0, 0, 0, 0,
    &pEveryoneSID))
{
    _tprintf(_T("AllocateAndInitializeSid Error %u\n"), GetLastError());
    ret_val = 0;
    goto Cleanup;
}

// Initialize an EXPLICIT_ACCESS structure for an ACE.
// The ACE will allow Everyone read access to the key.
ZeroMemory(&ea, 2 * sizeof(EXPLICIT_ACCESS));
ea[0].grfAccessPermissions = STANDARD_RIGHTS_ALL;
ea[0].grfAccessMode = SET_ACCESS;
ea[0].grfInheritance = NO_INHERITANCE;
ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
ea[0].Trustee.ptstrName = (LPTSTR)pEveryoneSID;

// there's another ACE for administrators in between, but is of no relevance here

dwRes = SetEntriesInAcl(2, ea, NULL, &pACL);

// Initialize a security descriptor.  
pSD = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR,
    SECURITY_DESCRIPTOR_MIN_LENGTH);
if (NULL == pSD)
{
    _tprintf(_T("LocalAlloc Error %u\n"), GetLastError());
    ret_val = 0;
    goto Cleanup;
}

if (!InitializeSecurityDescriptor(pSD,
    SECURITY_DESCRIPTOR_REVISION))
{
    _tprintf(_T("InitializeSecurityDescriptor Error %u\n"),
        GetLastError());
    ret_val = 0;
    goto Cleanup;
}

// Add the ACL to the security descriptor. 
if (!SetSecurityDescriptorDacl(pSD,
    TRUE,     // bDaclPresent flag   
    pACL,
    FALSE))   // not a default DACL 
{
    _tprintf(_T("SetSecurityDescriptorDacl Error %u\n"),
        GetLastError());
    ret_val = 0;
    goto Cleanup;
}

    // Initialize a security attributes structure.
*sa = new SECURITY_ATTRIBUTES;

(*sa)->nLength = sizeof(SECURITY_ATTRIBUTES);
(*sa)->lpSecurityDescriptor = pSD;
(*sa)->bInheritHandle = FALSE;

O resultado é que o lado do cliente recebe o erro0x5 (acesso negado) emCreateFile(). O que há de errado aqui?

questionAnswers(3)

yourAnswerToTheQuestion