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.

433 lines
11 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1997.
  5. //
  6. // File: shmem.cxx
  7. //
  8. // Contents:
  9. //
  10. // History:
  11. // RichardW 12/17/1996 Created
  12. //
  13. //----------------------------------------------------------------------------
  14. #include <lsapch.hxx>
  15. #define LSA_SHARED_HEAP_FLAGS (HEAP_NO_SERIALIZE | \
  16. HEAP_GENERATE_EXCEPTIONS | \
  17. HEAP_ZERO_MEMORY | \
  18. HEAP_REALLOC_IN_PLACE_ONLY | \
  19. HEAP_TAIL_CHECKING_ENABLED | \
  20. HEAP_FREE_CHECKING_ENABLED | \
  21. HEAP_DISABLE_COALESCE_ON_FREE | \
  22. HEAP_CREATE_ALIGN_16 | \
  23. HEAP_CREATE_ENABLE_TRACING )
  24. #if DBG
  25. #define LSA_DEFAULT_HEAP_FLAGS (HEAP_ZERO_MEMORY | \
  26. HEAP_TAIL_CHECKING_ENABLED | \
  27. HEAP_FREE_CHECKING_ENABLED )
  28. #else
  29. #define LSA_DEFAULT_HEAP_FLAGS (HEAP_ZERO_MEMORY)
  30. #endif
  31. //+---------------------------------------------------------------------------
  32. //
  33. // Function: LsapCreateSharedSection
  34. //
  35. // Synopsis: Internal worker that creates a section mapped into the process
  36. // indicated by pSession
  37. //
  38. // Effects:
  39. //
  40. // Arguments: [pSession] -- Session where section should be mapped
  41. // [HeapFlags] -- Heap flags for section
  42. // [InitialSize] -- Initial size
  43. // [MaxSize] -- Maximum size
  44. //
  45. // History: 1-29-97 RichardW Created
  46. //
  47. // Notes:
  48. //
  49. //----------------------------------------------------------------------------
  50. PLSAP_SHARED_SECTION
  51. LsapCreateSharedSection(
  52. PSession pSession,
  53. ULONG HeapFlags,
  54. ULONG InitialSize,
  55. ULONG MaxSize
  56. )
  57. {
  58. HANDLE Section ;
  59. PLSAP_SHARED_SECTION SharedSection ;
  60. LARGE_INTEGER SectionOffset ;
  61. NTSTATUS Status ;
  62. PVOID ClientBase ;
  63. SIZE_T ViewSize ;
  64. //
  65. // Create the section. The security descriptor is defaulted to
  66. // local system for now, although this will fixed.
  67. //
  68. Section = CreateFileMapping( INVALID_HANDLE_VALUE,
  69. NULL,
  70. PAGE_READWRITE |
  71. SEC_RESERVE,
  72. 0,
  73. MaxSize,
  74. NULL );
  75. if ( Section == NULL )
  76. {
  77. return NULL ;
  78. }
  79. SharedSection = (PLSAP_SHARED_SECTION ) LsapAllocateLsaHeap( sizeof( LSAP_SHARED_SECTION ) );
  80. if ( !SharedSection )
  81. {
  82. CloseHandle( Section );
  83. return NULL ;
  84. }
  85. SharedSection->Section = Section ;
  86. //
  87. // Map the shared section into our address space:
  88. //
  89. SharedSection->Base = MapViewOfFileEx( Section,
  90. FILE_MAP_READ | FILE_MAP_WRITE,
  91. 0,
  92. 0,
  93. 0,
  94. NULL );
  95. if ( !SharedSection->Base )
  96. {
  97. CloseHandle( Section );
  98. LsapFreeLsaHeap( SharedSection );
  99. return NULL ;
  100. }
  101. //
  102. // Map it into the client's address space
  103. //
  104. SectionOffset.QuadPart = 0;
  105. ClientBase = SharedSection->Base ;
  106. ViewSize = 0 ;
  107. Status = NtMapViewOfSection( Section,
  108. pSession->hProcess,
  109. &ClientBase,
  110. 0L,
  111. 0L,
  112. NULL,
  113. &ViewSize,
  114. ViewUnmap,
  115. 0L,
  116. PAGE_READWRITE );
  117. if ( !NT_SUCCESS( Status ) )
  118. {
  119. UnmapViewOfFile( SharedSection->Base );
  120. CloseHandle( SharedSection->Section );
  121. LsapFreeLsaHeap( SharedSection );
  122. return NULL ;
  123. }
  124. //
  125. // Okay, now commit the initial size, and start the heap on it.
  126. //
  127. if ( NULL == VirtualAllocEx(
  128. GetCurrentProcess(),
  129. SharedSection->Base,
  130. InitialSize,
  131. MEM_COMMIT,
  132. PAGE_READWRITE ))
  133. {
  134. UnmapViewOfFile( SharedSection->Base );
  135. CloseHandle( SharedSection->Section );
  136. LsapFreeLsaHeap( SharedSection );
  137. return NULL;
  138. }
  139. HeapFlags &= LSA_SHARED_HEAP_FLAGS ;
  140. SharedSection->Heap = RtlCreateHeap( HeapFlags,
  141. SharedSection->Base,
  142. MaxSize,
  143. InitialSize,
  144. NULL,
  145. NULL );
  146. if ( !SharedSection->Heap )
  147. {
  148. UnmapViewOfFile( SharedSection->Base );
  149. CloseHandle( SharedSection->Section );
  150. LsapFreeLsaHeap( SharedSection );
  151. return NULL ;
  152. }
  153. return SharedSection ;
  154. }
  155. //+---------------------------------------------------------------------------
  156. //
  157. // Function: LsapDeleteSharedSection
  158. //
  159. // Synopsis: Internal worker that deletes a shared section
  160. //
  161. // Arguments: [SharedSection] -- section to delete
  162. //
  163. // History: 1-29-97 RichardW Created
  164. //
  165. // Notes:
  166. //
  167. //----------------------------------------------------------------------------
  168. BOOL
  169. LsapDeleteSharedSection(
  170. PLSAP_SHARED_SECTION SharedSection
  171. )
  172. {
  173. RtlDestroyHeap( SharedSection->Heap );
  174. NtUnmapViewOfSection( SharedSection->Session->hProcess,
  175. SharedSection->Base );
  176. NtUnmapViewOfSection( NtCurrentProcess(),
  177. SharedSection->Base );
  178. CloseHandle( SharedSection->Section );
  179. LsapFreeLsaHeap( SharedSection );
  180. return TRUE;
  181. }
  182. HRESULT
  183. LsapSharedSectionRundown(
  184. PSession Session,
  185. PVOID Ignored
  186. )
  187. {
  188. PLIST_ENTRY List ;
  189. PLSAP_SHARED_SECTION Section ;
  190. while ( !IsListEmpty( &Session->SectionList ) )
  191. {
  192. List = RemoveHeadList( &Session->SectionList );
  193. Section = (PLSAP_SHARED_SECTION) List ;
  194. LsapDeleteSharedSection( Section );
  195. }
  196. return SEC_E_OK ;
  197. }
  198. //////////////////////////////////////////////////////////////////////////////
  199. //
  200. // Functions exported to security packages for shared memory
  201. //
  202. //////////////////////////////////////////////////////////////////////////////
  203. //+---------------------------------------------------------------------------
  204. //
  205. // Function: LsaCreateSharedMemory
  206. //
  207. // Synopsis: "Public" function to create the shared memory segment
  208. //
  209. // Arguments: [MaxSize] --
  210. // [InitialSize] --
  211. //
  212. // History: 1-29-97 RichardW Created
  213. //
  214. // Notes:
  215. //
  216. //----------------------------------------------------------------------------
  217. PVOID
  218. NTAPI
  219. LsaCreateSharedMemory(
  220. ULONG InitialSize,
  221. ULONG MaxSize
  222. )
  223. {
  224. PSession pSession ;
  225. PLSAP_SHARED_SECTION SharedSection ;
  226. pSession = GetCurrentSession();
  227. //
  228. // Validate that we are running in a real call with a real client
  229. //
  230. if (pSession->dwProcessID == GetCurrentProcessId())
  231. {
  232. return NULL ;
  233. }
  234. //
  235. // Try to create the shared section
  236. //
  237. SharedSection = LsapCreateSharedSection(
  238. pSession,
  239. LSA_DEFAULT_HEAP_FLAGS,
  240. InitialSize,
  241. MaxSize
  242. );
  243. if ( SharedSection )
  244. {
  245. SharedSection->Session = pSession ;
  246. LockSession( pSession );
  247. if ( IsListEmpty( &pSession->SectionList ) )
  248. {
  249. if ( FALSE == AddRundown( pSession, LsapSharedSectionRundown, NULL ))
  250. {
  251. UnlockSession( pSession );
  252. LsapDeleteSharedSection( SharedSection );
  253. SharedSection = NULL;
  254. }
  255. }
  256. }
  257. if ( SharedSection )
  258. {
  259. InsertTailList( &pSession->SectionList,
  260. &SharedSection->List );
  261. UnlockSession( pSession );
  262. }
  263. return SharedSection ;
  264. }
  265. //+---------------------------------------------------------------------------
  266. //
  267. // Function: LsaAllocateSharedMemory
  268. //
  269. // Synopsis: Allocates memory out of a shared section.
  270. //
  271. // Arguments: [Shared] -- Handle to shared section
  272. // [Size] -- Size of allocation.
  273. //
  274. // History: 1-29-97 RichardW Created
  275. //
  276. // Notes:
  277. //
  278. //----------------------------------------------------------------------------
  279. PVOID
  280. NTAPI
  281. LsaAllocateSharedMemory(
  282. PVOID Shared,
  283. ULONG Size
  284. )
  285. {
  286. PLSAP_SHARED_SECTION SharedSection = (PLSAP_SHARED_SECTION) Shared ;
  287. return RtlAllocateHeap( SharedSection->Heap, 0, Size );
  288. }
  289. //+---------------------------------------------------------------------------
  290. //
  291. // Function: LsaFreeSharedMemory
  292. //
  293. // Synopsis: Frees memory allocated out of a section
  294. //
  295. // Arguments: [Shared] --
  296. // [Memory] --
  297. //
  298. // History: 1-29-97 RichardW Created
  299. //
  300. // Notes:
  301. //
  302. //----------------------------------------------------------------------------
  303. VOID
  304. NTAPI
  305. LsaFreeSharedMemory(
  306. PVOID Shared,
  307. PVOID Memory
  308. )
  309. {
  310. PLSAP_SHARED_SECTION SharedSection = (PLSAP_SHARED_SECTION) Shared ;
  311. HeapFree( SharedSection->Heap, 0, Memory );
  312. }
  313. //+---------------------------------------------------------------------------
  314. //
  315. // Function: LsaDeleteSharedMemory
  316. //
  317. // Synopsis: Public wrapper to delete the shared section
  318. //
  319. // Arguments: [Shared] -- handle to shared section
  320. //
  321. // History: 1-29-97 RichardW Created
  322. //
  323. // Notes:
  324. //
  325. //----------------------------------------------------------------------------
  326. BOOLEAN
  327. NTAPI
  328. LsaDeleteSharedMemory(
  329. PVOID Shared
  330. )
  331. {
  332. PLSAP_SHARED_SECTION SharedSection ;
  333. PSession pSession ;
  334. BOOL Success ;
  335. pSession = GetCurrentSession();
  336. SharedSection = (PLSAP_SHARED_SECTION) Shared ;
  337. if ( pSession != SharedSection->Session )
  338. {
  339. DebugLog(( DEB_WARN, "Package tried to delete shared memory for a different process\n" ));
  340. return FALSE ;
  341. }
  342. LockSession( pSession );
  343. RemoveEntryList( &SharedSection->List );
  344. Success = LsapDeleteSharedSection( SharedSection );
  345. if ( IsListEmpty( &pSession->SectionList ) )
  346. {
  347. DelRundown( pSession, LsapSharedSectionRundown );
  348. }
  349. UnlockSession( pSession );
  350. return ( Success != 0 ) ;
  351. }