Repository navigation
Expand file tree
/
Copy pathPrivateNamespace.cpp
More file actions
127 lines (116 loc) · 3.6 KB
/
Copy pathPrivateNamespace.cpp
File metadata and controls
127 lines (116 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include "stdafx.h"
#include "PrivateNamespace.h"
#include <sddl.h>
#pragma comment(lib, "advapi32.lib")
namespace
{
PVOID BuildBoundaryDescriptor(LPCTSTR descriptorName)
{
PVOID boundaryDescriptor = CreateBoundaryDescriptor(descriptorName, 0);
if (!boundaryDescriptor)
{
return NULL;
}
SID_IDENTIFIER_AUTHORITY worldAuthority = SECURITY_WORLD_SID_AUTHORITY;
PSID worldSid = NULL;
if (!AllocateAndInitializeSid(&worldAuthority, 1, SECURITY_WORLD_RID,
0, 0, 0, 0, 0, 0, 0, &worldSid)
|| !AddSIDToBoundaryDescriptor(&boundaryDescriptor, worldSid))
{
if (worldSid) FreeSid(worldSid);
DeleteBoundaryDescriptor(boundaryDescriptor);
return NULL;
}
FreeSid(worldSid);
SID_IDENTIFIER_AUTHORITY mandatoryAuthority =
SECURITY_MANDATORY_LABEL_AUTHORITY;
PSID mediumIntegritySid = NULL;
if (!AllocateAndInitializeSid(&mandatoryAuthority, 1,
SECURITY_MANDATORY_MEDIUM_RID, 0, 0, 0, 0, 0, 0, 0,
&mediumIntegritySid)
|| !AddIntegrityLabelToBoundaryDescriptor(&boundaryDescriptor,
mediumIntegritySid))
{
if (mediumIntegritySid) FreeSid(mediumIntegritySid);
DeleteBoundaryDescriptor(boundaryDescriptor);
return NULL;
}
FreeSid(mediumIntegritySid);
return boundaryDescriptor;
}
BOOL MakeNamespaceName(LPTSTR buffer, size_t bufferLength,
DWORD ownerProcessId)
{
return _sntprintf_s(buffer, bufferLength, _TRUNCATE,
_T("MenuToolsSession%u"), ownerProcessId) >= 0;
}
}
HANDLE PrivateNamespace::Create(DWORD ownerProcessId)
{
TCHAR namespaceName[64];
if (!MakeNamespaceName(namespaceName, ARRAYSIZE(namespaceName),
ownerProcessId))
{
return NULL;
}
PVOID boundaryDescriptor = BuildBoundaryDescriptor(namespaceName);
if (!boundaryDescriptor)
{
return NULL;
}
PSECURITY_DESCRIPTOR securityDescriptor = NULL;
SECURITY_ATTRIBUTES securityAttributes = { sizeof(SECURITY_ATTRIBUTES) };
BOOL converted = ConvertStringSecurityDescriptorToSecurityDescriptor(
_T("D:P(A;;GA;;;OW)(A;;0x0000000F;;;WD)"),
SDDL_REVISION_1, &securityDescriptor, NULL);
securityAttributes.lpSecurityDescriptor = securityDescriptor;
HANDLE privateNamespace = converted
? CreatePrivateNamespace(&securityAttributes, boundaryDescriptor,
namespaceName) : NULL;
if (securityDescriptor) LocalFree(securityDescriptor);
DeleteBoundaryDescriptor(boundaryDescriptor);
return privateNamespace;
}
HANDLE PrivateNamespace::Open(DWORD ownerProcessId)
{
TCHAR namespaceName[64];
if (!MakeNamespaceName(namespaceName, ARRAYSIZE(namespaceName),
ownerProcessId))
{
return NULL;
}
PVOID boundaryDescriptor = BuildBoundaryDescriptor(namespaceName);
if (!boundaryDescriptor)
{
return NULL;
}
HANDLE privateNamespace = OpenPrivateNamespace(boundaryDescriptor,
namespaceName);
DeleteBoundaryDescriptor(boundaryDescriptor);
return privateNamespace;
}
BOOL PrivateNamespace::MakeObjectName(LPTSTR buffer, size_t bufferLength,
DWORD ownerProcessId, LPCTSTR objectName)
{
TCHAR namespaceName[64];
return MakeNamespaceName(namespaceName, ARRAYSIZE(namespaceName),
ownerProcessId)
&& _sntprintf_s(buffer, bufferLength, _TRUNCATE, _T("%s\\%s"),
namespaceName, objectName) >= 0;
}
BOOL PrivateNamespace::CreateSharedEventSecurityAttributes(
SECURITY_ATTRIBUTES* securityAttributes,
PSECURITY_DESCRIPTOR* securityDescriptor)
{
*securityDescriptor = NULL;
if (!ConvertStringSecurityDescriptorToSecurityDescriptor(
_T("D:P(A;;GA;;;OW)(A;;0x00100002;;;WD)"),
SDDL_REVISION_1, securityDescriptor, NULL))
{
return FALSE;
}
securityAttributes->nLength = sizeof(SECURITY_ATTRIBUTES);
securityAttributes->lpSecurityDescriptor = *securityDescriptor;
securityAttributes->bInheritHandle = FALSE;
return TRUE;
}