/* The tokens and pipes the tests of the daemon's pipe make * (test/Test/DaemonPipe.hs): whether this process's token holds * Administrators enabled, a copy of it that holds the group for deny only * as a filtered administrator's token does, a copy of a token at an * integrity level, a pipe made as a token with the owner its descriptor * names, a pipe opened as a token, and whether a connection ends. The * test suite builds it on Windows alone, the one platform with these * tokens and pipes. Each call returns 0 or the Win32 error code of the * call that failed. */#include <stdlib.h>#include <windows.h>#include <sddl.h>/* What each direction of the pipes the tests make holds. */enum { NN_TEST_PIPE_BUFFER_BYTES = 4096 };/* Whether this process's token holds Administrators enabled, as * CheckTokenMembership reads a token, which a group held for deny only is * not: *held gets 1 or 0. */unsigned long nn_test_administrators_enabled(int *held) { BYTE administrators[SECURITY_MAX_SID_SIZE]; DWORD size = sizeof administrators; BOOL member = FALSE; *held = 0; if (!CreateWellKnownSid(WinBuiltinAdministratorsSid, NULL, administrators, &size)) { return GetLastError(); } if (!CheckTokenMembership(NULL, administrators, &member)) { return GetLastError(); } *held = member ? 1 : 0; return ERROR_SUCCESS;}/* A copy of this process's token, as a primary token for the caller to * release with CloseHandle, that holds Administrators for deny only and no * privilege, as a filtered administrator's token does. */unsigned long nn_test_filtered_token(void **token) { BYTE administrators[SECURITY_MAX_SID_SIZE]; DWORD size = sizeof administrators; SID_AND_ATTRIBUTES deny; HANDLE own = NULL; HANDLE filtered = NULL; DWORD error = ERROR_SUCCESS; *token = NULL; if (!CreateWellKnownSid(WinBuiltinAdministratorsSid, NULL, administrators, &size)) { return GetLastError(); } deny.Sid = administrators; deny.Attributes = 0; if (!OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE | TOKEN_QUERY | TOKEN_ASSIGN_PRIMARY, &own)) { return GetLastError(); } if (!CreateRestrictedToken(own, DISABLE_MAX_PRIVILEGE, 1, &deny, 0, NULL, 0, NULL, &filtered)) { error = GetLastError(); } else { *token = filtered; } CloseHandle(own); return error;}/* A copy of a token, a primary one for the caller to release with * CloseHandle, at the integrity level the RID names (SECURITY_MANDATORY_*_RID). * The base token is a primary token such as nn_test_filtered_token gives, or * NULL for this process's. */unsigned long nn_test_integrity_token(void *base, unsigned long rid, void **token) { SID_IDENTIFIER_AUTHORITY authority = SECURITY_MANDATORY_LABEL_AUTHORITY; TOKEN_MANDATORY_LABEL label; PSID level = NULL; HANDLE own = NULL; HANDLE copy = NULL; DWORD error = ERROR_SUCCESS; *token = NULL; if (base == NULL) { if (!OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE | TOKEN_QUERY, &own)) { return GetLastError(); } base = own; } if (!DuplicateTokenEx(base, TOKEN_ALL_ACCESS, NULL, SecurityImpersonation, TokenPrimary, ©)) { error = GetLastError(); } else if (!AllocateAndInitializeSid(&authority, 1, rid, 0, 0, 0, 0, 0, 0, 0, &level)) { error = GetLastError(); } else { label.Label.Sid = level; label.Label.Attributes = SE_GROUP_INTEGRITY; if (!SetTokenInformation(copy, TokenIntegrityLevel, &label, sizeof label + GetLengthSid(level))) { error = GetLastError(); } } if (error == ERROR_SUCCESS) { *token = copy; } else if (copy != NULL) { CloseHandle(copy); } if (level != NULL) { FreeSid(level); } if (own != NULL) { CloseHandle(own); } return error;}/* A copy of this process's token, a primary one for the caller to release * with CloseHandle, that holds the group the SID text names for deny only, * as a token shaped to hide a group holds it. */unsigned long nn_test_deny_group_token(const wchar_t *group, void **token) { SID_AND_ATTRIBUTES deny; PSID sid = NULL; HANDLE own = NULL; HANDLE shaped = NULL; DWORD error = ERROR_SUCCESS; *token = NULL; if (!ConvertStringSidToSidW(group, &sid)) { return GetLastError(); } deny.Sid = sid; deny.Attributes = 0; if (!OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE | TOKEN_QUERY | TOKEN_ASSIGN_PRIMARY, &own)) { error = GetLastError(); } else if (!CreateRestrictedToken(own, 0, 1, &deny, 0, NULL, 0, NULL, &shaped)) { error = GetLastError(); } else { *token = shaped; } if (own != NULL) { CloseHandle(own); } LocalFree(sid); return error;}/* Enable the restore privilege on the token, which lets its holder give * what it makes any owner. ERROR_NOT_ALL_ASSIGNED where the token does * not hold it. */static DWORD enable_restore(HANDLE token) { TOKEN_PRIVILEGES privileges; privileges.PrivilegeCount = 1; privileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; if (!LookupPrivilegeValueW(NULL, L"SeRestorePrivilege", &privileges.Privileges[0].Luid)) { return GetLastError(); } if (!AdjustTokenPrivileges(token, FALSE, &privileges, 0, NULL, NULL)) { return GetLastError(); } return GetLastError();}/* A copy of the token as an impersonation token, which becomes this * thread's until restore_self. Windows' guidance for a thread that cannot * stop impersonating is to end the process. */static DWORD impersonate(HANDLE token, HANDLE *copy) { if (!DuplicateTokenEx(token, TOKEN_ALL_ACCESS, NULL, SecurityImpersonation, TokenImpersonation, copy)) { return GetLastError(); } if (!SetThreadToken(NULL, *copy)) { DWORD error = GetLastError(); CloseHandle(*copy); *copy = NULL; return error; } return ERROR_SUCCESS;}static void restore_self(HANDLE copy) { if (!RevertToSelf()) { abort(); } CloseHandle(copy);}/* Make the pipe at name, with the security descriptor the SDDL string * describes, on this thread running as a copy of the token, which is a * primary token such as nn_test_filtered_token gives or NULL for this * process's; with restore nonzero the copy has the restore privilege * enabled. A NULL SDDL makes the pipe with no descriptor of its own, as * a further instance of a pipe is made, which Windows checks against the * first instance's descriptor. *pipe gets the instance for the caller to * release with CloseHandle. */unsigned long nn_test_make_pipe(void *token, int restore, const wchar_t *name, const wchar_t *sddl, void **pipe) { HANDLE own = NULL; HANDLE copy = NULL; PSECURITY_DESCRIPTOR descriptor = NULL; SECURITY_ATTRIBUTES attributes; HANDLE made; DWORD error = ERROR_SUCCESS; *pipe = NULL; if (token == NULL) { if (!OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE | TOKEN_QUERY, &own)) { return GetLastError(); } token = own; } if (sddl != NULL && !ConvertStringSecurityDescriptorToSecurityDescriptorW( sddl, SDDL_REVISION_1, &descriptor, NULL)) { error = GetLastError(); } if (error == ERROR_SUCCESS) { error = impersonate(token, ©); } if (error == ERROR_SUCCESS && restore) { error = enable_restore(copy); } if (error == ERROR_SUCCESS) { attributes.nLength = sizeof attributes; attributes.lpSecurityDescriptor = descriptor; attributes.bInheritHandle = FALSE; made = CreateNamedPipeW( name, PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED, PIPE_TYPE_BYTE | PIPE_WAIT | PIPE_REJECT_REMOTE_CLIENTS, PIPE_UNLIMITED_INSTANCES, NN_TEST_PIPE_BUFFER_BYTES, NN_TEST_PIPE_BUFFER_BYTES, 0, descriptor == NULL ? NULL : &attributes); if (made == INVALID_HANDLE_VALUE) { error = GetLastError(); } else { *pipe = made; } } if (copy != NULL) { restore_self(copy); } if (descriptor != NULL) { LocalFree(descriptor); } if (own != NULL) { CloseHandle(own); } return error;}/* Open the pipe at name for the access, on this thread running as a copy * of the token (a primary token, or NULL for this process's), as the * library's client opens it: letting the server identify the caller but * not act as it, and with effective_only nonzero seeing only the groups * the token has enabled. *pipe gets the connection for the caller to * release with CloseHandle, and is NULL where the open failed. */unsigned long nn_test_open_as(void *token, const wchar_t *name, unsigned long access, int effective_only, void **pipe) { HANDLE own = NULL; HANDLE copy = NULL; HANDLE opened; DWORD flags = SECURITY_SQOS_PRESENT | SECURITY_IDENTIFICATION; DWORD error = ERROR_SUCCESS; *pipe = NULL; if (effective_only) { flags |= SECURITY_EFFECTIVE_ONLY; } if (token == NULL) { if (!OpenProcessToken(GetCurrentProcess(), TOKEN_DUPLICATE | TOKEN_QUERY, &own)) { return GetLastError(); } token = own; } error = impersonate(token, ©); if (error == ERROR_SUCCESS) { opened = CreateFileW(name, access, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED | flags, NULL); if (opened == INVALID_HANDLE_VALUE) { error = GetLastError(); } else { *pipe = opened; } restore_self(copy); } if (own != NULL) { CloseHandle(own); } return error;}/* Whether the server of the connection ends it, which a read of a pipe the * server has closed does, within the milliseconds: *closed gets 1 where it * does and 0 where the read is still waiting then, or got data. The pipe * is one opened with FILE_FLAG_OVERLAPPED, as nn_test_open_as opens it. */unsigned long nn_test_wait_closed(void *pipe, unsigned long milliseconds, int *closed) { OVERLAPPED read; BYTE byte; DWORD transferred = 0; DWORD error = ERROR_SUCCESS; *closed = 0; ZeroMemory(&read, sizeof read); read.hEvent = CreateEventW(NULL, TRUE, FALSE, NULL); if (read.hEvent == NULL) { return GetLastError(); } if (ReadFile(pipe, &byte, 1, NULL, &read)) { CloseHandle(read.hEvent); return ERROR_SUCCESS; } error = GetLastError(); if (error == ERROR_IO_PENDING) { if (WaitForSingleObject(read.hEvent, milliseconds) == WAIT_TIMEOUT) { CancelIoEx(pipe, &read); } /* The read ends, completed or cancelled, before the OVERLAPPED goes. */ if (GetOverlappedResult(pipe, &read, &transferred, TRUE)) { error = ERROR_SUCCESS; } else { error = GetLastError(); } } if (error == ERROR_BROKEN_PIPE) { *closed = 1; error = ERROR_SUCCESS; } else if (error == ERROR_OPERATION_ABORTED) { error = ERROR_SUCCESS; } CloseHandle(read.hEvent); return error;}