xpcom/tests/windows/TestNTFSPermissions.cpp

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 * Test for NTFS File Permissions being correctly changed to match the new
michael@0 8 * directory upon moving a file. (Bug 224692.)
michael@0 9 */
michael@0 10
michael@0 11 #include "../TestHarness.h"
michael@0 12 #include "nsEmbedString.h"
michael@0 13 #include "nsIFile.h"
michael@0 14 #include <windows.h>
michael@0 15 #include <aclapi.h>
michael@0 16
michael@0 17 #define BUFFSIZE 512
michael@0 18
michael@0 19
michael@0 20
michael@0 21 nsresult TestPermissions()
michael@0 22 {
michael@0 23
michael@0 24 nsresult rv; // Return value
michael@0 25
michael@0 26 // File variables
michael@0 27 HANDLE tempFileHandle;
michael@0 28 nsCOMPtr<nsIFile> tempFile;
michael@0 29 nsCOMPtr<nsIFile> tempDirectory1;
michael@0 30 nsCOMPtr<nsIFile> tempDirectory2;
michael@0 31 WCHAR filePath[MAX_PATH];
michael@0 32 WCHAR dir1Path[MAX_PATH];
michael@0 33 WCHAR dir2Path[MAX_PATH];
michael@0 34
michael@0 35 // Security variables
michael@0 36 DWORD result;
michael@0 37 PSID everyoneSID = nullptr, adminSID = nullptr;
michael@0 38 PACL dirACL = nullptr, fileACL = nullptr;
michael@0 39 PSECURITY_DESCRIPTOR dirSD = nullptr, fileSD = nullptr;
michael@0 40 EXPLICIT_ACCESS ea[2];
michael@0 41 SID_IDENTIFIER_AUTHORITY SIDAuthWorld =
michael@0 42 SECURITY_WORLD_SID_AUTHORITY;
michael@0 43 SID_IDENTIFIER_AUTHORITY SIDAuthNT = SECURITY_NT_AUTHORITY;
michael@0 44 SECURITY_ATTRIBUTES sa;
michael@0 45 TRUSTEE everyoneTrustee;
michael@0 46 ACCESS_MASK everyoneRights;
michael@0 47
michael@0 48 // Create a well-known SID for the Everyone group.
michael@0 49 if(!AllocateAndInitializeSid(&SIDAuthWorld, 1,
michael@0 50 SECURITY_WORLD_RID,
michael@0 51 0, 0, 0, 0, 0, 0, 0,
michael@0 52 &everyoneSID))
michael@0 53 {
michael@0 54 fail("NTFS Permissions: AllocateAndInitializeSid Error");
michael@0 55 return NS_ERROR_FAILURE;
michael@0 56 }
michael@0 57
michael@0 58 // Create a SID for the Administrators group.
michael@0 59 if(! AllocateAndInitializeSid(&SIDAuthNT, 2,
michael@0 60 SECURITY_BUILTIN_DOMAIN_RID,
michael@0 61 DOMAIN_ALIAS_RID_ADMINS,
michael@0 62 0, 0, 0, 0, 0, 0,
michael@0 63 &adminSID))
michael@0 64 {
michael@0 65 fail("NTFS Permissions: AllocateAndInitializeSid Error");
michael@0 66 return NS_ERROR_FAILURE;
michael@0 67 }
michael@0 68
michael@0 69 // Initialize an EXPLICIT_ACCESS structure for an ACE.
michael@0 70 // The ACE will allow Everyone read access to the directory.
michael@0 71 ZeroMemory(&ea, 2 * sizeof(EXPLICIT_ACCESS));
michael@0 72 ea[0].grfAccessPermissions = GENERIC_READ;
michael@0 73 ea[0].grfAccessMode = SET_ACCESS;
michael@0 74 ea[0].grfInheritance= SUB_CONTAINERS_AND_OBJECTS_INHERIT;
michael@0 75 ea[0].Trustee.TrusteeForm = TRUSTEE_IS_SID;
michael@0 76 ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP;
michael@0 77 ea[0].Trustee.ptstrName = (LPTSTR) everyoneSID;
michael@0 78
michael@0 79 // Initialize an EXPLICIT_ACCESS structure for an ACE.
michael@0 80 // The ACE will allow the Administrators group full access
michael@0 81 ea[1].grfAccessPermissions = GENERIC_ALL | STANDARD_RIGHTS_ALL;
michael@0 82 ea[1].grfAccessMode = SET_ACCESS;
michael@0 83 ea[1].grfInheritance= SUB_CONTAINERS_AND_OBJECTS_INHERIT;
michael@0 84 ea[1].Trustee.TrusteeForm = TRUSTEE_IS_SID;
michael@0 85 ea[1].Trustee.TrusteeType = TRUSTEE_IS_GROUP;
michael@0 86 ea[1].Trustee.ptstrName = (LPTSTR) adminSID;
michael@0 87
michael@0 88 // Create a new ACL that contains the new ACEs.
michael@0 89 result = SetEntriesInAcl(2, ea, nullptr, &dirACL);
michael@0 90 if (ERROR_SUCCESS != result)
michael@0 91 {
michael@0 92 fail("NTFS Permissions: SetEntriesInAcl Error");
michael@0 93 return NS_ERROR_FAILURE;
michael@0 94 }
michael@0 95
michael@0 96 // Initialize a security descriptor.
michael@0 97 dirSD = (PSECURITY_DESCRIPTOR) LocalAlloc(LPTR,
michael@0 98 SECURITY_DESCRIPTOR_MIN_LENGTH);
michael@0 99 if (nullptr == dirSD)
michael@0 100 {
michael@0 101 fail("NTFS Permissions: LocalAlloc Error");
michael@0 102 return NS_ERROR_FAILURE;
michael@0 103 }
michael@0 104
michael@0 105 if (!InitializeSecurityDescriptor(dirSD,
michael@0 106 SECURITY_DESCRIPTOR_REVISION))
michael@0 107 {
michael@0 108 fail("NTFS Permissions: InitializeSecurityDescriptor Error");
michael@0 109 return NS_ERROR_FAILURE;
michael@0 110 }
michael@0 111
michael@0 112 // Add the ACL to the security descriptor.
michael@0 113 if (!SetSecurityDescriptorDacl(dirSD, true, dirACL, false))
michael@0 114 {
michael@0 115 fail("NTFS Permissions: SetSecurityDescriptorDacl Error");
michael@0 116 return NS_ERROR_FAILURE;
michael@0 117 }
michael@0 118
michael@0 119 // Initialize a security attributes structure.
michael@0 120 sa.nLength = sizeof (SECURITY_ATTRIBUTES);
michael@0 121 sa.lpSecurityDescriptor = dirSD;
michael@0 122 sa.bInheritHandle = false;
michael@0 123
michael@0 124 // Create and open first temporary directory
michael@0 125 if(!CreateDirectoryW(L".\\NTFSPERMTEMP1", &sa))
michael@0 126 {
michael@0 127 fail("NTFS Permissions: Creating Temporary Directory");
michael@0 128 return NS_ERROR_FAILURE;
michael@0 129 }
michael@0 130
michael@0 131 GetFullPathNameW((LPCWSTR)L".\\NTFSPERMTEMP1", MAX_PATH, dir1Path,
michael@0 132 nullptr);
michael@0 133
michael@0 134 rv = NS_NewLocalFile(nsEmbedString(dir1Path), false,
michael@0 135 getter_AddRefs(tempDirectory1));
michael@0 136 if (NS_FAILED(rv))
michael@0 137 {
michael@0 138 fail("NTFS Permissions: Opening Temporary Directory 1");
michael@0 139 return rv;
michael@0 140 }
michael@0 141
michael@0 142
michael@0 143 // Create and open temporary file
michael@0 144 tempFileHandle = CreateFileW(L".\\NTFSPERMTEMP1\\NTFSPerm.tmp",
michael@0 145 GENERIC_READ | GENERIC_WRITE,
michael@0 146 0,
michael@0 147 nullptr, //default security
michael@0 148 CREATE_ALWAYS,
michael@0 149 FILE_ATTRIBUTE_NORMAL,
michael@0 150 nullptr);
michael@0 151
michael@0 152 if(tempFileHandle == INVALID_HANDLE_VALUE)
michael@0 153 {
michael@0 154 fail("NTFS Permissions: Creating Temporary File");
michael@0 155 return NS_ERROR_FAILURE;
michael@0 156 }
michael@0 157
michael@0 158 CloseHandle(tempFileHandle);
michael@0 159
michael@0 160 GetFullPathNameW((LPCWSTR)L".\\NTFSPERMTEMP1\\NTFSPerm.tmp",
michael@0 161 MAX_PATH, filePath, nullptr);
michael@0 162
michael@0 163 rv = NS_NewLocalFile(nsEmbedString(filePath), false,
michael@0 164 getter_AddRefs(tempFile));
michael@0 165 if (NS_FAILED(rv))
michael@0 166 {
michael@0 167 fail("NTFS Permissions: Opening Temporary File");
michael@0 168 return rv;
michael@0 169 }
michael@0 170
michael@0 171 // Update Everyone Explict_Acess to full access.
michael@0 172 ea[0].grfAccessPermissions = GENERIC_ALL | STANDARD_RIGHTS_ALL;
michael@0 173
michael@0 174 // Update the ACL to contain the new ACEs.
michael@0 175 result = SetEntriesInAcl(2, ea, nullptr, &dirACL);
michael@0 176 if (ERROR_SUCCESS != result)
michael@0 177 {
michael@0 178 fail("NTFS Permissions: SetEntriesInAcl 2 Error");
michael@0 179 return NS_ERROR_FAILURE;
michael@0 180 }
michael@0 181
michael@0 182 // Add the new ACL to the security descriptor.
michael@0 183 if (!SetSecurityDescriptorDacl(dirSD, true, dirACL, false))
michael@0 184 {
michael@0 185 fail("NTFS Permissions: SetSecurityDescriptorDacl 2 Error");
michael@0 186 return NS_ERROR_FAILURE;
michael@0 187 }
michael@0 188
michael@0 189 // Create and open second temporary directory
michael@0 190 if(!CreateDirectoryW(L".\\NTFSPERMTEMP2", &sa))
michael@0 191 {
michael@0 192 fail("NTFS Permissions: Creating Temporary Directory 2");
michael@0 193 return NS_ERROR_FAILURE;
michael@0 194 }
michael@0 195
michael@0 196 GetFullPathNameW((LPCWSTR)L".\\NTFSPERMTEMP2", MAX_PATH, dir2Path,
michael@0 197 nullptr);
michael@0 198
michael@0 199 rv = NS_NewLocalFile(nsEmbedString(dir2Path), false,
michael@0 200 getter_AddRefs(tempDirectory2));
michael@0 201 if (NS_FAILED(rv))
michael@0 202 {
michael@0 203 fail("NTFS Permissions: Opening Temporary Directory 2");
michael@0 204 return rv;
michael@0 205 }
michael@0 206
michael@0 207 // Move the file.
michael@0 208 rv = tempFile->MoveTo(tempDirectory2, EmptyString());
michael@0 209
michael@0 210 if (NS_FAILED(rv))
michael@0 211 {
michael@0 212 fail("NTFS Permissions: Moving");
michael@0 213 return rv;
michael@0 214 }
michael@0 215
michael@0 216 // Access the ACL of the file
michael@0 217 result = GetNamedSecurityInfoW(L".\\NTFSPERMTEMP2\\NTFSPerm.tmp",
michael@0 218 SE_FILE_OBJECT,
michael@0 219 DACL_SECURITY_INFORMATION |
michael@0 220 UNPROTECTED_DACL_SECURITY_INFORMATION,
michael@0 221 nullptr, nullptr, &fileACL, nullptr,
michael@0 222 &fileSD);
michael@0 223 if (ERROR_SUCCESS != result)
michael@0 224 {
michael@0 225 fail("NTFS Permissions: GetNamedSecurityDescriptor Error");
michael@0 226 return NS_ERROR_FAILURE;
michael@0 227 }
michael@0 228
michael@0 229 // Build a trustee representing "Everyone"
michael@0 230 BuildTrusteeWithSid(&everyoneTrustee, everyoneSID);
michael@0 231
michael@0 232 // Get Everyone's effective rights.
michael@0 233 result = GetEffectiveRightsFromAcl(fileACL, &everyoneTrustee,
michael@0 234 &everyoneRights);
michael@0 235 if (ERROR_SUCCESS != result)
michael@0 236 {
michael@0 237 fail("NTFS Permissions: GetEffectiveRightsFromAcl Error");
michael@0 238 return NS_ERROR_FAILURE;
michael@0 239 }
michael@0 240
michael@0 241 // Check for delete access, which we won't have unless permissions have
michael@0 242 // updated
michael@0 243 if((everyoneRights & DELETE) == (DELETE))
michael@0 244 {
michael@0 245 passed("NTFS Permissions Test");
michael@0 246 rv = NS_OK;
michael@0 247 }
michael@0 248 else
michael@0 249 {
michael@0 250 fail("NTFS Permissions: Access check.");
michael@0 251 rv = NS_ERROR_FAILURE;
michael@0 252 }
michael@0 253
michael@0 254 // Cleanup
michael@0 255 if (everyoneSID)
michael@0 256 FreeSid(everyoneSID);
michael@0 257 if (adminSID)
michael@0 258 FreeSid(adminSID);
michael@0 259 if (dirACL)
michael@0 260 LocalFree(dirACL);
michael@0 261 if (dirSD)
michael@0 262 LocalFree(dirSD);
michael@0 263 if(fileACL)
michael@0 264 LocalFree(fileACL);
michael@0 265
michael@0 266 tempDirectory1->Remove(true);
michael@0 267 tempDirectory2->Remove(true);
michael@0 268
michael@0 269 return rv;
michael@0 270 }
michael@0 271
michael@0 272 int main(int argc, char** argv)
michael@0 273 {
michael@0 274 ScopedXPCOM xpcom("NTFSPermissionsTests"); // name for tests being run
michael@0 275 if (xpcom.failed())
michael@0 276 return 1;
michael@0 277
michael@0 278 int rv = 0;
michael@0 279
michael@0 280 if(NS_FAILED(TestPermissions()))
michael@0 281 rv = 1;
michael@0 282
michael@0 283 return rv;
michael@0 284
michael@0 285 }
michael@0 286

mercurial