Source code of Windows XP (NT5)
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.

451 lines
10 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. VirtualAllocEx( GetCurrentProcess(),
  128. SharedSection->Base,
  129. InitialSize,
  130. MEM_COMMIT,
  131. PAGE_READWRITE );
  132. HeapFlags &= LSA_SHARED_HEAP_FLAGS ;
  133. SharedSection->Heap = RtlCreateHeap( HeapFlags,
  134. SharedSection->Base,
  135. MaxSize,
  136. InitialSize,
  137. NULL,
  138. NULL );
  139. if ( !SharedSection->Heap )
  140. {
  141. UnmapViewOfFile( SharedSection->Base );
  142. CloseHandle( SharedSection->Section );
  143. LsapFreeLsaHeap( SharedSection );
  144. return NULL ;
  145. }
  146. return SharedSection ;
  147. }
  148. //+---------------------------------------------------------------------------
  149. //
  150. // Function: LsapDeleteSharedSection
  151. //
  152. // Synopsis: Internal worker that deletes a shared section
  153. //
  154. // Arguments: [SharedSection] -- section to delete
  155. //
  156. // History: 1-29-97 RichardW Created
  157. //
  158. // Notes:
  159. //
  160. //----------------------------------------------------------------------------
  161. BOOL
  162. LsapDeleteSharedSection(
  163. PLSAP_SHARED_SECTION SharedSection
  164. )
  165. {
  166. RtlDestroyHeap( SharedSection->Heap );
  167. NtUnmapViewOfSection( SharedSection->Session->hProcess,
  168. SharedSection->Base );
  169. NtUnmapViewOfSection( NtCurrentProcess(),
  170. SharedSection->Base );
  171. CloseHandle( SharedSection->Section );
  172. LsapFreeLsaHeap( SharedSection );
  173. return TRUE;
  174. }
  175. HRESULT
  176. LsapSharedSectionRundown(
  177. PSession Session,
  178. PVOID Ignored
  179. )
  180. {
  181. PLIST_ENTRY List ;
  182. PLSAP_SHARED_SECTION Section ;
  183. while ( !IsListEmpty( &Session->SectionList ) )
  184. {
  185. List = RemoveHeadList( &Session->SectionList );
  186. Section = (PLSAP_SHARED_SECTION) List ;
  187. LsapDeleteSharedSection( Section );
  188. }
  189. return SEC_E_OK ;
  190. }
  191. //////////////////////////////////////////////////////////////////////////////
  192. //
  193. // Functions exported to security packages for shared memory
  194. //
  195. //////////////////////////////////////////////////////////////////////////////
  196. //+---------------------------------------------------------------------------
  197. //
  198. // Function: LsaCreateSharedMemory
  199. //
  200. // Synopsis: "Public" function to create the shared memory segment
  201. //
  202. // Arguments: [MaxSize] --
  203. // [InitialSize] --
  204. //
  205. // History: 1-29-97 RichardW Created
  206. //
  207. // Notes:
  208. //
  209. //----------------------------------------------------------------------------
  210. PVOID
  211. NTAPI
  212. LsaCreateSharedMemory(
  213. ULONG InitialSize,
  214. ULONG MaxSize
  215. )
  216. {
  217. PSession pSession ;
  218. PLSAP_SHARED_SECTION SharedSection ;
  219. pSession = GetCurrentSession();
  220. //
  221. // Validate that we are running in a real call with a real client
  222. //
  223. if (pSession->dwProcessID == GetCurrentProcessId())
  224. {
  225. return NULL ;
  226. }
  227. //
  228. // Try to create the shared section
  229. //
  230. SharedSection = LsapCreateSharedSection( pSession,
  231. LSA_DEFAULT_HEAP_FLAGS,
  232. InitialSize,
  233. MaxSize
  234. );
  235. if ( SharedSection )
  236. {
  237. SharedSection->Session = pSession ;
  238. LockSession( pSession );
  239. if ( IsListEmpty( &pSession->SectionList ) )
  240. {
  241. AddRundown( pSession, LsapSharedSectionRundown, NULL );
  242. }
  243. InsertTailList( &pSession->SectionList,
  244. &SharedSection->List );
  245. UnlockSession( pSession );
  246. }
  247. return SharedSection ;
  248. }
  249. //+---------------------------------------------------------------------------
  250. //
  251. // Function: LsaAllocateSharedMemory
  252. //
  253. // Synopsis: Allocates memory out of a shared section.
  254. //
  255. // Arguments: [Shared] -- Handle to shared section
  256. // [Size] -- Size of allocation.
  257. //
  258. // History: 1-29-97 RichardW Created
  259. //
  260. // Notes:
  261. //
  262. //----------------------------------------------------------------------------
  263. PVOID
  264. NTAPI
  265. LsaAllocateSharedMemory(
  266. PVOID Shared,
  267. ULONG Size
  268. )
  269. {
  270. PLSAP_SHARED_SECTION SharedSection ;
  271. PVOID Memory ;
  272. SharedSection = (PLSAP_SHARED_SECTION) Shared ;
  273. __try
  274. {
  275. Memory = RtlAllocateHeap( SharedSection->Heap, 0, Size );
  276. }
  277. __except (EXCEPTION_EXECUTE_HANDLER)
  278. {
  279. Memory = NULL ;
  280. }
  281. return Memory ;
  282. }
  283. //+---------------------------------------------------------------------------
  284. //
  285. // Function: LsaFreeSharedMemory
  286. //
  287. // Synopsis: Frees memory allocated out of a section
  288. //
  289. // Arguments: [Shared] --
  290. // [Memory] --
  291. //
  292. // History: 1-29-97 RichardW Created
  293. //
  294. // Notes:
  295. //
  296. //----------------------------------------------------------------------------
  297. VOID
  298. NTAPI
  299. LsaFreeSharedMemory(
  300. PVOID Shared,
  301. PVOID Memory
  302. )
  303. {
  304. PLSAP_SHARED_SECTION SharedSection ;
  305. SharedSection = (PLSAP_SHARED_SECTION) Shared ;
  306. __try
  307. {
  308. HeapFree( SharedSection->Heap, 0, Memory );
  309. }
  310. __except (EXCEPTION_EXECUTE_HANDLER)
  311. {
  312. NOTHING ;
  313. }
  314. }
  315. //+---------------------------------------------------------------------------
  316. //
  317. // Function: LsaDeleteSharedMemory
  318. //
  319. // Synopsis: Public wrapper to delete the shared section
  320. //
  321. // Arguments: [Shared] -- handle to shared section
  322. //
  323. // History: 1-29-97 RichardW Created
  324. //
  325. // Notes:
  326. //
  327. //----------------------------------------------------------------------------
  328. BOOLEAN
  329. NTAPI
  330. LsaDeleteSharedMemory(
  331. PVOID Shared
  332. )
  333. {
  334. PLSAP_SHARED_SECTION SharedSection ;
  335. PSession pSession ;
  336. BOOL Success ;
  337. pSession = GetCurrentSession();
  338. SharedSection = (PLSAP_SHARED_SECTION) Shared ;
  339. if ( pSession != SharedSection->Session )
  340. {
  341. DebugLog(( DEB_WARN, "Package tried to delete shared memory for a different process\n" ));
  342. return FALSE ;
  343. }
  344. Success = FALSE ;
  345. __try
  346. {
  347. LockSession( pSession );
  348. RemoveEntryList( &SharedSection->List );
  349. Success = LsapDeleteSharedSection( SharedSection );
  350. if ( IsListEmpty( &pSession->SectionList ) )
  351. {
  352. DelRundown( pSession, LsapSharedSectionRundown );
  353. }
  354. }
  355. __finally
  356. {
  357. UnlockSession( pSession );
  358. }
  359. return ( Success != 0 ) ;
  360. }