2 * winsecur.c: implementation of winsecur.h.
10 #if !defined NO_SECURITY
12 #define WINSECUR_GLOBAL
15 /* Initialised once, then kept around to reuse forever */
16 static PSID worldsid, networksid, usersid;
21 static int attempted = FALSE;
22 static int successful;
23 static HMODULE advapi;
27 advapi = load_system32_dll("advapi32.dll");
28 successful = advapi &&
29 GET_WINDOWS_FUNCTION(advapi, GetSecurityInfo) &&
30 GET_WINDOWS_FUNCTION(advapi, OpenProcessToken) &&
31 GET_WINDOWS_FUNCTION(advapi, GetTokenInformation) &&
32 GET_WINDOWS_FUNCTION(advapi, InitializeSecurityDescriptor) &&
33 GET_WINDOWS_FUNCTION(advapi, SetSecurityDescriptorOwner) &&
34 GET_WINDOWS_FUNCTION(advapi, SetEntriesInAclA);
41 static int attempted = FALSE;
42 static int successful;
47 crypt = load_system32_dll("crypt32.dll");
49 GET_WINDOWS_FUNCTION(crypt, CryptProtectMemory);
54 PSID get_user_sid(void)
56 HANDLE proc = NULL, tok = NULL;
57 TOKEN_USER *user = NULL;
59 PSID sid = NULL, ret = NULL;
64 if ((proc = OpenProcess(MAXIMUM_ALLOWED, FALSE,
65 GetCurrentProcessId())) == NULL)
68 if (!p_OpenProcessToken(proc, TOKEN_QUERY, &tok))
71 if (!p_GetTokenInformation(tok, TokenUser, NULL, 0, &toklen) &&
72 GetLastError() != ERROR_INSUFFICIENT_BUFFER)
75 if ((user = (TOKEN_USER *)LocalAlloc(LPTR, toklen)) == NULL)
78 if (!p_GetTokenInformation(tok, TokenUser, user, toklen, &toklen))
81 sidlen = GetLengthSid(user->User.Sid);
83 sid = (PSID)smalloc(sidlen);
85 if (!CopySid(sidlen, sid, user->User.Sid))
88 /* Success. Move sid into the return value slot, and null it out
89 * to stop the cleanup code freeing it. */
106 int getsids(char *error)
108 SID_IDENTIFIER_AUTHORITY world_auth = SECURITY_WORLD_SID_AUTHORITY;
109 SID_IDENTIFIER_AUTHORITY nt_auth = SECURITY_NT_AUTHORITY;
115 if ((usersid = get_user_sid()) == NULL) {
116 error = dupprintf("unable to construct SID for current user: %s",
117 win_strerror(GetLastError()));
123 if (!AllocateAndInitializeSid(&world_auth, 1, SECURITY_WORLD_RID,
124 0, 0, 0, 0, 0, 0, 0, &worldsid)) {
125 error = dupprintf("unable to construct SID for world: %s",
126 win_strerror(GetLastError()));
132 if (!AllocateAndInitializeSid(&nt_auth, 1, SECURITY_NETWORK_RID,
133 0, 0, 0, 0, 0, 0, 0, &networksid)) {
134 error = dupprintf("unable to construct SID for "
135 "local same-user access only: %s",
136 win_strerror(GetLastError()));
152 int make_private_security_descriptor(DWORD permissions,
153 PSECURITY_DESCRIPTOR *psd,
157 SID_IDENTIFIER_AUTHORITY world_auth = SECURITY_WORLD_SID_AUTHORITY;
158 SID_IDENTIFIER_AUTHORITY nt_auth = SECURITY_NT_AUTHORITY;
159 EXPLICIT_ACCESS ea[3];
168 if (!getsids(*error))
171 memset(ea, 0, sizeof(ea));
172 ea[0].grfAccessPermissions = permissions;
173 ea[0].grfAccessMode = REVOKE_ACCESS;
174 ea[0].grfInheritance = NO_INHERITANCE;
175 ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
176 ea[0].Trustee.ptstrName = (LPTSTR)worldsid;
177 ea[1].grfAccessPermissions = permissions;
178 ea[1].grfAccessMode = GRANT_ACCESS;
179 ea[1].grfInheritance = NO_INHERITANCE;
180 ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
181 ea[1].Trustee.ptstrName = (LPTSTR)usersid;
182 ea[2].grfAccessPermissions = permissions;
183 ea[2].grfAccessMode = REVOKE_ACCESS;
184 ea[2].grfInheritance = NO_INHERITANCE;
185 ea[2].Trustee.TrusteeForm = TRUSTEE_IS_SID;
186 ea[2].Trustee.ptstrName = (LPTSTR)networksid;
188 acl_err = p_SetEntriesInAclA(3, ea, NULL, acl);
189 if (acl_err != ERROR_SUCCESS || *acl == NULL) {
190 *error = dupprintf("unable to construct ACL: %s",
191 win_strerror(acl_err));
195 *psd = (PSECURITY_DESCRIPTOR)
196 LocalAlloc(LPTR, SECURITY_DESCRIPTOR_MIN_LENGTH);
198 *error = dupprintf("unable to allocate security descriptor: %s",
199 win_strerror(GetLastError()));
203 if (!InitializeSecurityDescriptor(*psd, SECURITY_DESCRIPTOR_REVISION)) {
204 *error = dupprintf("unable to initialise security descriptor: %s",
205 win_strerror(GetLastError()));
209 if (!SetSecurityDescriptorOwner(*psd, usersid, FALSE)) {
210 *error = dupprintf("unable to set owner in security descriptor: %s",
211 win_strerror(GetLastError()));
215 if (!SetSecurityDescriptorDacl(*psd, TRUE, *acl, FALSE)) {
216 *error = dupprintf("unable to set DACL in security descriptor: %s",
217 win_strerror(GetLastError()));
240 int protectprocess(char *error)
242 SID_IDENTIFIER_AUTHORITY world_auth = SECURITY_WORLD_SID_AUTHORITY;
243 SID_IDENTIFIER_AUTHORITY nt_auth = SECURITY_NT_AUTHORITY;
244 EXPLICIT_ACCESS ea[2];
249 static const nastyace=WRITE_DAC | WRITE_OWNER |
250 PROCESS_CREATE_PROCESS | PROCESS_CREATE_THREAD |
251 PROCESS_DUP_HANDLE | PROCESS_QUERY_INFORMATION |
252 PROCESS_SET_QUOTA | PROCESS_SET_INFORMATION |
253 PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE |
254 PROCESS_SUSPEND_RESUME;
259 memset(ea, 0, sizeof(ea));
262 ea[0].grfAccessPermissions = nastyace;
263 ea[0].grfAccessMode = DENY_ACCESS;
264 ea[0].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
265 ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
266 ea[0].Trustee.ptstrName = (LPTSTR)worldsid;
269 ea[1].grfAccessPermissions = ~nastyace & 0x1fff;
270 ea[1].grfAccessMode = GRANT_ACCESS;
271 ea[1].grfInheritance = SUB_CONTAINERS_AND_OBJECTS_INHERIT;
272 ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
273 ea[1].Trustee.ptstrName = (LPTSTR)usersid;
275 acl_err = p_SetEntriesInAclA(2, ea, NULL, &acl);
277 if (acl_err != ERROR_SUCCESS || acl == NULL) {
278 error = dupprintf("unable to construct ACL: %s",
279 win_strerror(acl_err));
287 OWNER_SECURITY_INFORMATION | DACL_SECURITY_INFORMATION,
293 error=dupprintf("Unable to set process ACL: %s",
294 win_strerror(GetLastError()));
310 #endif /* !defined NO_SECURITY */