Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

222 lines
5.5 KiB

  1. //____________________________________________________________________________
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1995 - 1996.
  5. //
  6. // File: shared.cxx
  7. //
  8. // Contents: This file contains a set of routines for the management of
  9. // shared memory.
  10. //
  11. // Functions: SCHEDAllocShared: Allocates a handle (in a given process)
  12. // to a copy of a memory block in this process.
  13. //
  14. // SCHEDFreeShared: Releases the handle (and the copy of the
  15. // memory block)
  16. //
  17. // SCHEDLockShared: Maps a handle (from a given process) into
  18. // a memory block in this process. Has the option of
  19. // transfering the handle to this process, thereby deleting
  20. // it from the given process
  21. //
  22. // SCHEDUnlockShared: Opposite of SCHEDLockShared, unmaps the
  23. // memory block
  24. //
  25. // History: 4/1/1996 RaviR Created (stole from shell\dll\shared.c)
  26. //
  27. //____________________________________________________________________________
  28. #include "..\pch\headers.hxx"
  29. #pragma hdrstop
  30. #include <Sddl.h>
  31. #include <StrSafe.h>
  32. HANDLE
  33. MapHandle(
  34. HANDLE hData,
  35. DWORD dwSource,
  36. DWORD dwDest,
  37. DWORD dwDesiredAccess,
  38. DWORD dwFlags)
  39. {
  40. HANDLE hSource = NULL;
  41. HANDLE hDest = NULL;
  42. HANDLE hNew = NULL;
  43. BOOL fOk;
  44. if (dwSource == GetCurrentProcessId())
  45. hSource = GetCurrentProcess();
  46. else
  47. hSource = OpenProcess( PROCESS_DUP_HANDLE, FALSE, dwSource);
  48. if (!hSource)
  49. goto DoExit;
  50. if (dwDest == GetCurrentProcessId())
  51. hDest = GetCurrentProcess();
  52. else
  53. hDest = OpenProcess( PROCESS_DUP_HANDLE, FALSE, dwDest);
  54. if (!hDest)
  55. goto DoExit;
  56. fOk = DuplicateHandle( hSource, hData,
  57. hDest, &hNew,
  58. dwDesiredAccess,
  59. FALSE, dwFlags | DUPLICATE_SAME_ACCESS);
  60. if (!fOk)
  61. hNew = (HANDLE)NULL;
  62. DoExit:
  63. if (hSource && dwSource != GetCurrentProcessId())
  64. CloseHandle(hSource);
  65. if (hDest && dwDest != GetCurrentProcessId())
  66. CloseHandle(hDest);
  67. return hNew;
  68. }
  69. HANDLE
  70. SCHEDAllocShared(
  71. LPCVOID lpvData,
  72. DWORD dwSize,
  73. DWORD dwDestinationProcessId)
  74. {
  75. HANDLE hData;
  76. SHMAPHEADER* lpmh;
  77. HANDLE hUsableData;
  78. // djinn up an appropriate security descriptor
  79. BYTE buf[512];
  80. PSECURITY_DESCRIPTOR pSD = NULL;
  81. // allow full control to admins, system and owner
  82. WCHAR sddl[] = L"D:(A;;FA;;;CO)(A;;FA;;;BA)(A;;FA;;;SY)";
  83. if (!ConvertStringSecurityDescriptorToSecurityDescriptorW(sddl, SDDL_REVISION_1, &pSD, NULL))
  84. return NULL;
  85. // * CURRENT USER *
  86. HANDLE hToken;
  87. OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken);
  88. BYTE sidBuf[512];
  89. SID_AND_ATTRIBUTES* pSidAndAttr = (SID_AND_ATTRIBUTES*) sidBuf;
  90. DWORD size = 512;
  91. GetTokenInformation(hToken, TokenUser, (LPVOID)pSidAndAttr, size, &size);
  92. if (!SetSecurityDescriptorOwner(pSD, pSidAndAttr->Sid, false))
  93. {
  94. LocalFree(pSD);
  95. return NULL;
  96. }
  97. SECURITY_ATTRIBUTES sa;
  98. sa.nLength = sizeof(SECURITY_ATTRIBUTES);
  99. sa.bInheritHandle = false;
  100. sa.lpSecurityDescriptor = pSD;
  101. //
  102. // Make a filemapping handle with this data in it.
  103. //
  104. hData = CreateFileMapping( INVALID_HANDLE_VALUE, &sa, PAGE_READWRITE,0,
  105. dwSize+sizeof(SHMAPHEADER),NULL);
  106. LocalFree(pSD);
  107. if (hData == NULL)
  108. {
  109. // DebugMsg...
  110. return NULL;
  111. }
  112. lpmh = (SHMAPHEADER *)MapViewOfFile(hData, FILE_MAP_READ | FILE_MAP_WRITE,
  113. 0, 0, 0);
  114. if (!lpmh)
  115. {
  116. // DebugMsg...
  117. CloseHandle(hData);
  118. return NULL;
  119. }
  120. lpmh->dwSize = dwSize;
  121. if (lpvData)
  122. memcpy((LPVOID)(lpmh+1),lpvData,dwSize);
  123. UnmapViewOfFile(lpmh);
  124. hUsableData = MapHandle(hData,
  125. GetCurrentProcessId(),
  126. dwDestinationProcessId,
  127. FILE_MAP_ALL_ACCESS,
  128. DUPLICATE_CLOSE_SOURCE);
  129. return hUsableData;
  130. }
  131. LPVOID
  132. SCHEDLockShared(
  133. HANDLE hData,
  134. DWORD dwSourceProcessId)
  135. {
  136. SHMAPHEADER* lpmh;
  137. HANDLE hUsableData;
  138. hUsableData = MapHandle(hData,dwSourceProcessId,GetCurrentProcessId(),FILE_MAP_ALL_ACCESS,0);
  139. //
  140. // Now map that new process specific handle and close it
  141. //
  142. lpmh = (SHMAPHEADER*)MapViewOfFile(hUsableData,
  143. FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
  144. CloseHandle(hUsableData);
  145. if (!lpmh)
  146. return NULL;
  147. return (LPVOID)(lpmh+1);
  148. }
  149. BOOL
  150. SCHEDUnlockShared(
  151. LPVOID lpvData)
  152. {
  153. SHMAPHEADER* lpmh = (SHMAPHEADER*)lpvData;
  154. //
  155. // Now just unmap the view of the file
  156. //
  157. return UnmapViewOfFile(lpmh-1);
  158. }
  159. BOOL
  160. SCHEDFreeShared(
  161. HANDLE hData,
  162. DWORD dwSourceProcessId)
  163. {
  164. HANDLE hUsableData;
  165. //
  166. // The below call closes the original handle in whatever process it
  167. // came from.
  168. //
  169. hUsableData = MapHandle(hData,dwSourceProcessId,
  170. GetCurrentProcessId(),
  171. FILE_MAP_ALL_ACCESS,DUPLICATE_CLOSE_SOURCE);
  172. //
  173. // Now free up the local handle
  174. //
  175. return CloseHandle(hUsableData);
  176. }