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.

1211 lines
36 KiB

  1. /*++
  2. Copyright (c) 1991 Microsoft Corporation
  3. Module Name:
  4. rmmain.c
  5. Abstract:
  6. Security Reference Monitor - Init, Control and State Change
  7. Author:
  8. Scott Birrell (ScottBi) March 12, 1991
  9. Environment:
  10. Revision History:
  11. --*/
  12. #include "pch.h"
  13. #pragma hdrstop
  14. #ifdef ALLOC_PRAGMA
  15. #pragma alloc_text(INIT,SeRmInitPhase1)
  16. #pragma alloc_text(PAGE,SepRmCommandServerThread)
  17. #pragma alloc_text(PAGE,SepRmCommandServerThreadInit)
  18. #pragma alloc_text(PAGE,SepRmCallLsa)
  19. #pragma alloc_text(INIT,SepRmInitPhase0)
  20. #endif
  21. //
  22. // Reference Monitor Command Worker Table
  23. //
  24. //
  25. // Keep this in sync with RM_COMMAND_NUMBER in ntrmlsa.h
  26. //
  27. #ifdef ALLOC_DATA_PRAGMA
  28. #pragma const_seg("PAGECONST")
  29. #endif
  30. const SEP_RM_COMMAND_WORKER SepRmCommandDispatch[] = {
  31. NULL,
  32. SepRmSetAuditEventWrkr,
  33. SepRmCreateLogonSessionWrkr,
  34. SepRmDeleteLogonSessionWrkr
  35. };
  36. BOOLEAN
  37. SeRmInitPhase1(
  38. )
  39. /*++
  40. Routine Description:
  41. This function is called by Phase 1 System Initialization to initialize
  42. the Security Reference Monitor. Note that initialization of the
  43. Reference Monitor Global State has already been performed in Phase 0
  44. initialization to allow access validation routines to operate without
  45. having to check that Reference Monitor Initialization is complete.
  46. The steps listed below are performed in this routine. The remainder
  47. of Reference Monitor initialization requires the LSA subsystem to have run,
  48. so that initialization is performed in a separate thread (the RM Command
  49. Server Thread, see below), so that the present thread can create the
  50. Session Manager which execs the LSA.
  51. o Create the Reference Monitor Command LPC port. The LSA subsystem sends
  52. commands (e.g. turn on auditing) which change the Reference Monitor
  53. Global State.
  54. o Create an Event for use in synchronizing with the LSA subsystem. The
  55. LSA will signal the event when the portion of LSA initialization upon
  56. with the Reference Monitor depends is complete. The Reference Monitor
  57. uses another LPC port, called the LSA Command Port to send commands
  58. to the LSA, so the RM must know that this port has been created before
  59. trying to connect to it.
  60. o Create the Reference Monitor Command Server Thread. This thread is
  61. a permanent thread of the System Init process that fields the Reference
  62. Monitor State Change commands described above.
  63. Arguments:
  64. None.
  65. Return Value:
  66. BOOLEAN - TRUE if Rm Initialization (Phase 1) succeeded, else FALSE
  67. --*/
  68. {
  69. NTSTATUS Status;
  70. STRING RmCommandPortName;
  71. UNICODE_STRING UnicodeRmCommandPortName;
  72. OBJECT_ATTRIBUTES ObjectAttributes;
  73. STRING LsaInitEventName;
  74. UNICODE_STRING UnicodeLsaInitEventName;
  75. OBJECT_ATTRIBUTES LsaInitEventObjectAttributes;
  76. SECURITY_DESCRIPTOR LsaInitEventSecurityDescriptor;
  77. ULONG AclSize;
  78. PAGED_CODE();
  79. //
  80. // Create an LPC port called the Reference Monitor Command Port.
  81. // This will be used by the LSA to send commands to the Reference
  82. // Monitor to update its state data.
  83. //
  84. RtlInitString( &RmCommandPortName, "\\SeRmCommandPort" );
  85. Status = RtlAnsiStringToUnicodeString(
  86. &UnicodeRmCommandPortName,
  87. &RmCommandPortName,
  88. TRUE );
  89. ASSERT( NT_SUCCESS(Status) );
  90. InitializeObjectAttributes(
  91. &ObjectAttributes,
  92. &UnicodeRmCommandPortName,
  93. 0,
  94. NULL,
  95. NULL
  96. );
  97. Status = ZwCreatePort(
  98. &SepRmState.RmCommandServerPortHandle,
  99. &ObjectAttributes,
  100. sizeof(SEP_RM_CONNECT_INFO),
  101. sizeof(RM_COMMAND_MESSAGE),
  102. sizeof(RM_COMMAND_MESSAGE) * 32
  103. );
  104. RtlFreeUnicodeString( &UnicodeRmCommandPortName );
  105. if( !NT_SUCCESS(Status) ) {
  106. KdPrint(("Security: Rm Create Command Port failed 0x%lx\n", Status));
  107. return FALSE;
  108. }
  109. //
  110. // Prepare to create an event for synchronizing with the LSA.
  111. // First, build the Security Descriptor for the Init Event Object
  112. //
  113. Status = RtlCreateSecurityDescriptor(
  114. &LsaInitEventSecurityDescriptor,
  115. SECURITY_DESCRIPTOR_REVISION
  116. );
  117. if (!NT_SUCCESS(Status)) {
  118. KdPrint(("Security: Creating Lsa Init Event Desc failed 0x%lx\n",
  119. Status));
  120. return FALSE;
  121. }
  122. //
  123. // Allocate a temporary buffer from the paged pool. It is a fatal
  124. // system error if the allocation fails since security cannot be
  125. // enabled.
  126. //
  127. AclSize = sizeof(ACL) +
  128. sizeof(ACCESS_ALLOWED_ACE) +
  129. SeLengthSid(SeLocalSystemSid);
  130. LsaInitEventSecurityDescriptor.Dacl =
  131. ExAllocatePoolWithTag(PagedPool, AclSize, 'cAeS');
  132. if (LsaInitEventSecurityDescriptor.Dacl == NULL) {
  133. KdPrint(("Security LSA: Insufficient resources to initialize\n"));
  134. return FALSE;
  135. }
  136. //
  137. // Now create the Discretionary ACL within the Security Descriptor
  138. //
  139. Status = RtlCreateAcl(
  140. LsaInitEventSecurityDescriptor.Dacl,
  141. AclSize,
  142. ACL_REVISION2
  143. );
  144. if (!NT_SUCCESS(Status)) {
  145. KdPrint(("Security: Creating Lsa Init Event Dacl failed 0x%lx\n",
  146. Status));
  147. return FALSE;
  148. }
  149. //
  150. // Now add an ACE giving GENERIC_ALL access to the User ID
  151. //
  152. Status = RtlAddAccessAllowedAce(
  153. LsaInitEventSecurityDescriptor.Dacl,
  154. ACL_REVISION2,
  155. GENERIC_ALL,
  156. SeLocalSystemSid
  157. );
  158. if (!NT_SUCCESS(Status)) {
  159. KdPrint(("Security: Adding Lsa Init Event ACE failed 0x%lx\n",
  160. Status));
  161. return FALSE;
  162. }
  163. //
  164. // Set up the Object Attributes for the Lsa Initialization Event
  165. //
  166. RtlInitString( &LsaInitEventName, "\\SeLsaInitEvent" );
  167. Status = RtlAnsiStringToUnicodeString(
  168. &UnicodeLsaInitEventName,
  169. &LsaInitEventName,
  170. TRUE ); ASSERT( NT_SUCCESS(Status) );
  171. InitializeObjectAttributes(
  172. &LsaInitEventObjectAttributes,
  173. &UnicodeLsaInitEventName,
  174. 0,
  175. NULL,
  176. &LsaInitEventSecurityDescriptor
  177. );
  178. //
  179. // Create an event for use in synchronizing with the LSA. The LSA will
  180. // signal this event when LSA initialization has reached the point
  181. // where the LSA's Reference Monitor Server Port has been created.
  182. //
  183. Status = ZwCreateEvent(
  184. &(SepRmState.LsaInitEventHandle),
  185. EVENT_MODIFY_STATE,
  186. &LsaInitEventObjectAttributes,
  187. NotificationEvent,
  188. FALSE);
  189. RtlFreeUnicodeString( &UnicodeLsaInitEventName );
  190. if (!NT_SUCCESS(Status)) {
  191. KdPrint(("Security: LSA init event creation failed.0x%xl\n",
  192. Status));
  193. return FALSE;
  194. }
  195. //
  196. // Deallocate the pool memory used for the Init Event DACL
  197. //
  198. ExFreePool( LsaInitEventSecurityDescriptor.Dacl );
  199. //
  200. // Create a permanent thread of the Sysinit Process, called the
  201. // Reference Monitor Server Thread. This thread is dedicated to
  202. // receiving Reference Monitor commands and dispatching them.
  203. //
  204. Status = PsCreateSystemThread(
  205. &SepRmState.SepRmThreadHandle,
  206. THREAD_GET_CONTEXT |
  207. THREAD_SET_CONTEXT |
  208. THREAD_SET_INFORMATION,
  209. NULL,
  210. NULL,
  211. NULL,
  212. SepRmCommandServerThread,
  213. NULL
  214. );
  215. if (!NT_SUCCESS(Status)) {
  216. KdPrint(("Security: Rm Server Thread creation failed 0x%lx\n", Status));
  217. return FALSE;
  218. }
  219. //
  220. // Initialize data from the registry. This must go here because all other
  221. // Se initialization takes place before the registry is initialized.
  222. //
  223. SepAdtInitializeCrashOnFail();
  224. SepAdtInitializePrivilegeAuditing();
  225. SepAdtInitializeAuditingOptions();
  226. //
  227. // Reference Monitor initialization is successful if we get to here.
  228. //
  229. ZwClose( SepRmState.SepRmThreadHandle );
  230. SepRmState.SepRmThreadHandle = NULL;
  231. return TRUE;
  232. }
  233. VOID
  234. SepRmCommandServerThread(
  235. IN PVOID StartContext
  236. )
  237. /*++
  238. Routine Description:
  239. This function is executed indefinitely by a dedicated permanent thread
  240. of the Sysinit Process, called the Reference Monitor Server Thread.
  241. This thread updates Reference Monitor Global State Data by dispatching
  242. commands sent from the LSA through the the Reference Monitor LPC Command
  243. Port. The following steps are repeated indefinitely:
  244. o Initialize RM Command receive and reply buffer headers
  245. o Perform remaining Reference Monitor initialization involving LSA
  246. o Wait for RM command sent from LSA, send reply to previous command
  247. (if any)
  248. o Validate command
  249. o Dispatch to command worker routine to execute command.
  250. Arguments:
  251. None.
  252. Return Value:
  253. None.
  254. --*/
  255. {
  256. NTSTATUS Status;
  257. PRM_REPLY_MESSAGE Reply;
  258. RM_COMMAND_MESSAGE CommandMessage;
  259. RM_REPLY_MESSAGE ReplyMessage;
  260. PAGED_CODE();
  261. //
  262. // Perform the rest of the Reference Monitor initialization, involving
  263. // synchronization with the LSA or dependency on the LSA having run.
  264. //
  265. if (!SepRmCommandServerThreadInit()) {
  266. KdPrint(("Security: Terminating Rm Command Server Thread\n"));
  267. return;
  268. }
  269. Status = PoRequestShutdownEvent (NULL);
  270. if (!NT_SUCCESS (Status)) {
  271. ZwClose (SepRmState.RmCommandPortHandle);
  272. ZwClose (SepRmState.RmCommandServerPortHandle);
  273. ZwClose (SepRmState.LsaCommandPortHandle);
  274. ZwClose (SepLsaHandle);
  275. SepRmState.RmCommandPortHandle = NULL;
  276. SepRmState.RmCommandServerPortHandle = NULL;
  277. SepRmState.LsaCommandPortHandle = NULL;
  278. SepLsaHandle = NULL;
  279. return;
  280. }
  281. //
  282. // Initialize LPC port message header type and length fields for the
  283. // received command message.
  284. //
  285. CommandMessage.MessageHeader.u2.ZeroInit = 0;
  286. CommandMessage.MessageHeader.u1.s1.TotalLength =
  287. (CSHORT) sizeof(RM_COMMAND_MESSAGE);
  288. CommandMessage.MessageHeader.u1.s1.DataLength =
  289. CommandMessage.MessageHeader.u1.s1.TotalLength -
  290. (CSHORT) sizeof(PORT_MESSAGE);
  291. //
  292. // Initialize the LPC port message header type and data sizes for
  293. // for the reply message.
  294. //
  295. ReplyMessage.MessageHeader.u2.ZeroInit = 0;
  296. ReplyMessage.MessageHeader.u1.s1.TotalLength =
  297. (CSHORT) sizeof(RM_COMMAND_MESSAGE);
  298. ReplyMessage.MessageHeader.u1.s1.DataLength =
  299. ReplyMessage.MessageHeader.u1.s1.TotalLength -
  300. (CSHORT) sizeof(PORT_MESSAGE);
  301. //
  302. // First time through, there is no reply.
  303. //
  304. Reply = NULL;
  305. //
  306. // Now loop indefinitely, processing incoming Rm commands from the LSA.
  307. //
  308. for(;;) {
  309. //
  310. // Wait for Command, send reply to previous command (if any)
  311. //
  312. Status = ZwReplyWaitReceivePort(
  313. SepRmState.RmCommandPortHandle,
  314. NULL,
  315. (PPORT_MESSAGE) Reply,
  316. (PPORT_MESSAGE) &CommandMessage
  317. );
  318. if (!NT_SUCCESS(Status)) {
  319. //
  320. // malicious user apps can try to connect to this port. We will
  321. // fail later, but if their thread vanishes, we'll get a failure
  322. // here. Ignore it:
  323. //
  324. if ( Status == STATUS_UNSUCCESSFUL )
  325. {
  326. //
  327. // skip it:
  328. //
  329. Reply = NULL ;
  330. continue;
  331. }
  332. KdPrint(("Security: RM message receive from Lsa failed %lx\n",
  333. Status));
  334. }
  335. //
  336. // Now dispatch to a routine to handle the command. Allow
  337. // command errors to occur without bringing system down just now.
  338. //
  339. CommandMessage.MessageHeader.u2.s2.Type &= ~LPC_KERNELMODE_MESSAGE;
  340. if ( CommandMessage.MessageHeader.u2.s2.Type == LPC_REQUEST ) {
  341. if ( (CommandMessage.CommandNumber >= RmAuditSetCommand) &&
  342. (CommandMessage.CommandNumber <= RmDeleteLogonSession) ) {
  343. (*(SepRmCommandDispatch[CommandMessage.CommandNumber]))
  344. (&CommandMessage, &ReplyMessage);
  345. //
  346. // Initialize the client thread info and message id for the
  347. // reply message. First time through, the reply message structure
  348. // is not used.
  349. //
  350. ReplyMessage.MessageHeader.ClientId =
  351. CommandMessage.MessageHeader.ClientId;
  352. ReplyMessage.MessageHeader.MessageId =
  353. CommandMessage.MessageHeader.MessageId;
  354. Reply = &ReplyMessage;
  355. } else {
  356. ASSERT( (CommandMessage.CommandNumber >= RmAuditSetCommand) &&
  357. (CommandMessage.CommandNumber <= RmDeleteLogonSession) );
  358. Reply = NULL;
  359. }
  360. } else if (CommandMessage.MessageHeader.u2.s2.Type == LPC_PORT_CLOSED ) {
  361. KEVENT Event;
  362. BOOLEAN Wait;
  363. KeInitializeEvent (&Event, NotificationEvent, FALSE);
  364. SepLockLsaQueue();
  365. SepAdtLsaDeadEvent = &Event;
  366. Wait = !SepWorkListEmpty ();
  367. SepUnlockLsaQueue();
  368. if (Wait) {
  369. KeWaitForSingleObject (&Event,
  370. Executive,
  371. KernelMode,
  372. FALSE,
  373. NULL);
  374. }
  375. //
  376. // Our only client closed its handle. Tidy up and exit.
  377. //
  378. ZwClose (SepRmState.LsaCommandPortHandle);
  379. ZwClose (SepRmState.RmCommandPortHandle);
  380. ZwClose (SepRmState.RmCommandServerPortHandle);
  381. ZwClose (SepLsaHandle);
  382. SepRmState.LsaCommandPortHandle = NULL;
  383. SepRmState.RmCommandPortHandle = NULL;
  384. SepRmState.RmCommandServerPortHandle = NULL;
  385. SepLsaHandle = NULL;
  386. break;
  387. } else if (CommandMessage.MessageHeader.u2.s2.Type == LPC_CONNECTION_REQUEST) {
  388. HANDLE tmp;
  389. //
  390. // Reject extra connection attempts
  391. //
  392. Status = ZwAcceptConnectPort(&tmp,
  393. NULL,
  394. (PPORT_MESSAGE) &CommandMessage,
  395. FALSE,
  396. NULL,
  397. NULL);
  398. } else {
  399. Reply = NULL;
  400. }
  401. } // end_for
  402. UNREFERENCED_PARAMETER( StartContext );
  403. }
  404. BOOLEAN
  405. SepRmCommandServerThreadInit(
  406. VOID
  407. )
  408. /*++
  409. Routine Description:
  410. This function performs initialization of the Reference Monitor Server
  411. thread. The following steps are performed.
  412. o Wait on the LSA signalling the event. When the event is signalled,
  413. the LSA has already created the LSA Command Server LPC Port
  414. o Close the LSA Init Event Handle. The event is not used again.
  415. o Listen for the LSA to connect to the Port
  416. o Accept the connection.
  417. o Connect to the LSA Command Server LPC Port
  418. Arguments:
  419. None.
  420. Return Value:
  421. --*/
  422. {
  423. NTSTATUS Status;
  424. UNICODE_STRING LsaCommandPortName;
  425. PORT_MESSAGE ConnectionRequest;
  426. SECURITY_QUALITY_OF_SERVICE DynamicQos;
  427. OBJECT_ATTRIBUTES ObjectAttributes;
  428. PORT_VIEW ClientView;
  429. REMOTE_PORT_VIEW LsaClientView;
  430. BOOLEAN BooleanStatus = TRUE;
  431. PAGED_CODE();
  432. //
  433. // Save a pointer to our process so we can get back into this process
  434. // to send commands to the LSA (using a handle to an LPC port created
  435. // below).
  436. //
  437. SepRmLsaCallProcess = PsGetCurrentProcess();
  438. ObReferenceObject(SepRmLsaCallProcess);
  439. //
  440. // Wait on the LSA signalling the event. This means that the LSA
  441. // has created its command port, not that LSA initialization is
  442. // complete.
  443. //
  444. Status = ZwWaitForSingleObject(
  445. SepRmState.LsaInitEventHandle,
  446. FALSE,
  447. NULL);
  448. if ( !NT_SUCCESS(Status) ) {
  449. KdPrint(("Security Rm Init: Waiting for LSA Init Event failed 0x%lx\n", Status));
  450. goto RmCommandServerThreadInitError;
  451. }
  452. //
  453. // Close the LSA Init Event Handle. The event is not used again.
  454. //
  455. ZwClose(SepRmState.LsaInitEventHandle);
  456. //
  457. // Listen for a connection to be made by the LSA to the Reference Monitor
  458. // Command Port. This connection will be made by the LSA process.
  459. //
  460. ConnectionRequest.u1.s1.TotalLength = sizeof(ConnectionRequest);
  461. ConnectionRequest.u1.s1.DataLength = (CSHORT)0;
  462. Status = ZwListenPort(
  463. SepRmState.RmCommandServerPortHandle,
  464. &ConnectionRequest
  465. );
  466. if (!NT_SUCCESS(Status)) {
  467. KdPrint(("Security Rm Init: Listen to Command Port failed 0x%lx\n",
  468. Status));
  469. goto RmCommandServerThreadInitError;
  470. }
  471. //
  472. // Obtain a handle to the LSA process for use when auditing.
  473. //
  474. InitializeObjectAttributes( &ObjectAttributes, NULL, 0, NULL, NULL );
  475. Status = ZwOpenProcess(
  476. &SepLsaHandle,
  477. PROCESS_VM_OPERATION | PROCESS_VM_WRITE,
  478. &ObjectAttributes,
  479. &ConnectionRequest.ClientId
  480. );
  481. if (!NT_SUCCESS(Status)) {
  482. KdPrint(("Security Rm Init: Open Listen to Command Port failed 0x%lx\n",
  483. Status));
  484. goto RmCommandServerThreadInitError;
  485. }
  486. //
  487. // Accept the connection made by the LSA process.
  488. //
  489. LsaClientView.Length = sizeof(LsaClientView);
  490. Status = ZwAcceptConnectPort(
  491. &SepRmState.RmCommandPortHandle,
  492. NULL,
  493. &ConnectionRequest,
  494. TRUE,
  495. NULL,
  496. &LsaClientView
  497. );
  498. if (!NT_SUCCESS(Status)) {
  499. KdPrint(("Security Rm Init: Accept Connect to Command Port failed 0x%lx\n",
  500. Status));
  501. goto RmCommandServerThreadInitError;
  502. }
  503. //
  504. // Complete the connection.
  505. //
  506. Status = ZwCompleteConnectPort(SepRmState.RmCommandPortHandle);
  507. if (!NT_SUCCESS(Status)) {
  508. KdPrint(("Security Rm Init: Complete Connect to Command Port failed 0x%lx\n",
  509. Status));
  510. goto RmCommandServerThreadInitError;
  511. }
  512. //
  513. // Set up the security quality of service parameters to use over the
  514. // Lsa Command LPC port. Use the most efficient (least overhead) - which
  515. // is dynamic rather than static tracking.
  516. //
  517. DynamicQos.ImpersonationLevel = SecurityImpersonation;
  518. DynamicQos.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
  519. DynamicQos.EffectiveOnly = TRUE;
  520. //
  521. // Create the section to be used as unnamed shared memory for
  522. // communication between the RM and LSA.
  523. //
  524. SepRmState.LsaCommandPortSectionSize.LowPart = PAGE_SIZE;
  525. SepRmState.LsaCommandPortSectionSize.HighPart = 0;
  526. Status = ZwCreateSection(
  527. &SepRmState.LsaCommandPortSectionHandle,
  528. SECTION_ALL_ACCESS,
  529. NULL, // ObjectAttributes
  530. &SepRmState.LsaCommandPortSectionSize,
  531. PAGE_READWRITE,
  532. SEC_COMMIT,
  533. NULL // FileHandle
  534. );
  535. if (!NT_SUCCESS(Status)) {
  536. KdPrint(("Security Rm Init: Create Memory Section for LSA port failed: %X\n", Status));
  537. goto RmCommandServerThreadInitError;
  538. }
  539. //
  540. // Set up for a call to NtConnectPort and connect to the LSA port.
  541. // This setup includes a description of the port memory section so that
  542. // the LPC connection logic can make the section visible to both the
  543. // client and server processes.
  544. //
  545. ClientView.Length = sizeof(ClientView);
  546. ClientView.SectionHandle = SepRmState.LsaCommandPortSectionHandle;
  547. ClientView.SectionOffset = 0;
  548. ClientView.ViewSize = SepRmState.LsaCommandPortSectionSize.LowPart;
  549. ClientView.ViewBase = 0;
  550. ClientView.ViewRemoteBase = 0;
  551. //
  552. // Set up the security quality of service parameters to use over the
  553. // port. Use dynamic tracking so that XACTSRV will impersonate the
  554. // user that we are impersonating when we call NtRequestWaitReplyPort.
  555. // If we used static tracking, XACTSRV would impersonate the context
  556. // when the connection is made.
  557. //
  558. DynamicQos.ImpersonationLevel = SecurityImpersonation;
  559. DynamicQos.ContextTrackingMode = SECURITY_DYNAMIC_TRACKING;
  560. DynamicQos.EffectiveOnly = TRUE;
  561. //
  562. // Connect to the Lsa Command LPC Port. This port is used to send
  563. // commands from the RM to the LSA.
  564. //
  565. RtlInitUnicodeString( &LsaCommandPortName, L"\\SeLsaCommandPort" );
  566. Status = ZwConnectPort(
  567. &SepRmState.LsaCommandPortHandle,
  568. &LsaCommandPortName,
  569. &DynamicQos,
  570. &ClientView,
  571. NULL, // ServerView
  572. NULL, // MaxMessageLength
  573. NULL, // ConnectionInformation
  574. NULL // ConnectionInformationLength
  575. );
  576. if (!NT_SUCCESS(Status)) {
  577. KdPrint(("Security Rm Init: Connect to LSA Port failed 0x%lx\n", Status));
  578. goto RmCommandServerThreadInitError;
  579. }
  580. //
  581. // Store information about the section so that we can create pointers
  582. // meaningful to LSA.
  583. //
  584. SepRmState.RmViewPortMemory = ClientView.ViewBase;
  585. SepRmState.LsaCommandPortMemoryDelta =
  586. (LONG)((ULONG_PTR)ClientView.ViewRemoteBase - (ULONG_PTR) ClientView.ViewBase );
  587. SepRmState.LsaViewPortMemory = ClientView.ViewRemoteBase;
  588. RmCommandServerThreadInitFinish:
  589. //
  590. // Dont need this section handle any more, even if returning
  591. // success.
  592. //
  593. if ( SepRmState.LsaCommandPortSectionHandle != NULL ) {
  594. NtClose( SepRmState.LsaCommandPortSectionHandle );
  595. SepRmState.LsaCommandPortSectionHandle = NULL;
  596. }
  597. //
  598. // The Reference Monitor Thread has successfully initialized.
  599. //
  600. return BooleanStatus;
  601. RmCommandServerThreadInitError:
  602. if ( SepRmState.LsaCommandPortHandle != NULL ) {
  603. NtClose( SepRmState.LsaCommandPortHandle );
  604. SepRmState.LsaCommandPortHandle = NULL;
  605. }
  606. BooleanStatus = FALSE;
  607. goto RmCommandServerThreadInitFinish;
  608. }
  609. NTSTATUS
  610. SepRmCallLsa(
  611. PSEP_WORK_ITEM SepWorkItem
  612. )
  613. /*++
  614. Routine Description:
  615. This function sends a command to the LSA via the LSA Reference Monitor
  616. Server Command LPC Port. If the command has parameters, they will be
  617. copied directly into a message structure and sent via LPC, therefore,
  618. the supplied parameters may not contain any absolute pointers. A caller
  619. must remove pointers by "marshalling" them into the buffer CommandParams.
  620. This function will create a queue of requests. This is in order to allow
  621. greater throughput for the majority if its callers. If a thread enters
  622. this routine and finds the queue empty, it is the responsibility of that
  623. thread to service all requests that come in while it is working until the
  624. queue is empty again. Other threads that enter will simply hook their work
  625. item onto the queue and exit.
  626. To implement a new LSA command, do the following:
  627. ================================================
  628. (1) If the command takes no parameters, just call this routine directly
  629. and provide an LSA worker routine called Lsap<command>Wrkr. See
  630. file lsa\server\lsarm.c for examples
  631. (2) If the command takes parameters, provide a routine called
  632. SepRmSend<command>Command that takes the parameters in unmarshalled
  633. form and calls SepRmCallLsa() with the command id, marshalled
  634. parameters, length of marshalled parameters and pointer to
  635. optional reply message. The marshalled parameters are free format:
  636. the only restriction is that there must be no absolute address
  637. pointers. These parameters are all placed in the passed LsaWorkItem
  638. structure.
  639. (3) In file private\inc\ntrmlsa.h, append a command name to the
  640. enumerated type LSA_COMMAND_NUMBER defined in file
  641. private\inc\ntrmlsa.h. Change the #define for LsapMaximumCommand
  642. to reference the new command.
  643. (4) Add the Lsap<command>Wrkr to the command dispatch table structure
  644. LsapCommandDispatch[] in file lsarm.c.
  645. (5) Add function prototypes to lsap.h and sep.h.
  646. Arguments:
  647. LsaWorkItem - Supplies a pointer to an SE_LSA_WORK_ITEM containing the
  648. information to be passed to LSA. This structure will be freed
  649. asynchronously by some invocation of this routine, not necessarily
  650. in the current context.
  651. !THIS PARAMETER MUST BE ALLOCATED OUT OF NONPAGED POOL!
  652. Return Value:
  653. NTSTATUS - Result Code. This is either a result code returned from
  654. trying to send the command/receive the reply, or a status code
  655. from the command itself.
  656. --*/
  657. {
  658. NTSTATUS Status = STATUS_SUCCESS;
  659. LSA_COMMAND_MESSAGE CommandMessage;
  660. LSA_REPLY_MESSAGE ReplyMessage;
  661. PSEP_LSA_WORK_ITEM WorkQueueItem;
  662. ULONG LocalListLength = 0;
  663. SIZE_T RegionSize;
  664. PVOID CopiedCommandParams = NULL;
  665. PVOID LsaViewCopiedCommandParams = NULL;
  666. PAGED_CODE();
  667. UNREFERENCED_PARAMETER( SepWorkItem );
  668. #if 0
  669. DbgPrint("Entering SepRmCallLsa\n");
  670. #endif
  671. WorkQueueItem = SepWorkListHead();
  672. KeAttachProcess( &SepRmLsaCallProcess->Pcb );
  673. while ( WorkQueueItem ) {
  674. #if 0
  675. DbgPrint("Got a work item from head of queue, processing\n");
  676. #endif
  677. //
  678. // Construct a message for LPC. First, fill in the message header
  679. // fields for LPC, specifying the message type and data sizes for
  680. // the outgoing CommandMessage and the incoming ReplyMessage.
  681. //
  682. CommandMessage.MessageHeader.u2.ZeroInit = 0;
  683. CommandMessage.MessageHeader.u1.s1.TotalLength =
  684. ((CSHORT) RM_COMMAND_MESSAGE_HEADER_SIZE +
  685. (CSHORT) WorkQueueItem->CommandParamsLength);
  686. CommandMessage.MessageHeader.u1.s1.DataLength =
  687. CommandMessage.MessageHeader.u1.s1.TotalLength -
  688. (CSHORT) sizeof(PORT_MESSAGE);
  689. ReplyMessage.MessageHeader.u2.ZeroInit = 0;
  690. ReplyMessage.MessageHeader.u1.s1.DataLength = (CSHORT) WorkQueueItem->ReplyBufferLength;
  691. ReplyMessage.MessageHeader.u1.s1.TotalLength =
  692. ReplyMessage.MessageHeader.u1.s1.DataLength +
  693. (CSHORT) sizeof(PORT_MESSAGE);
  694. //
  695. // Next, fill in the header info needed by the LSA.
  696. //
  697. CommandMessage.CommandNumber = WorkQueueItem->CommandNumber;
  698. ReplyMessage.ReturnedStatus = STATUS_SUCCESS;
  699. //
  700. // Set up the Command Parameters either in the LPC Command Message
  701. // itself, in the preallocated Lsa shared memory block, or in a
  702. // specially allocated block. The parameters are either
  703. // immediate (i.e. in the WorkQueueItem itself, or are in a buffer
  704. // pointed to by the address in the WorkQueueItem.
  705. //
  706. switch (WorkQueueItem->CommandParamsMemoryType) {
  707. case SepRmImmediateMemory:
  708. //
  709. // The Command Parameters are in the CommandParams buffer
  710. // in the Work Queue Item. Just copy them to the corresponding
  711. // buffer in the CommandMessage buffer.
  712. //
  713. CommandMessage.CommandParamsMemoryType = SepRmImmediateMemory;
  714. RtlCopyMemory(
  715. CommandMessage.CommandParams,
  716. &WorkQueueItem->CommandParams,
  717. WorkQueueItem->CommandParamsLength
  718. );
  719. break;
  720. case SepRmPagedPoolMemory:
  721. case SepRmUnspecifiedMemory:
  722. //
  723. // The Command Parameters are contained in paged pool memory.
  724. // Since this memory is is not accessible by the LSA, we must
  725. // copy of them either to the LPC Command Message Block, or
  726. // into LSA shared memory.
  727. //
  728. if (WorkQueueItem->CommandParamsLength <= LSA_MAXIMUM_COMMAND_PARAM_SIZE) {
  729. //
  730. // Parameters will fit into the LPC Command Message block.
  731. //
  732. CopiedCommandParams = CommandMessage.CommandParams;
  733. RtlCopyMemory(
  734. CopiedCommandParams,
  735. WorkQueueItem->CommandParams.BaseAddress,
  736. WorkQueueItem->CommandParamsLength
  737. );
  738. CommandMessage.CommandParamsMemoryType = SepRmImmediateMemory;
  739. } else {
  740. //
  741. // Parameters too large for LPC Command Message block.
  742. // If possible, copy them to the preallocated Lsa Shared
  743. // Memory block. If they are too large to fit, copy them
  744. // to an individually allocated chunk of Shared Virtual
  745. // Memory.
  746. //
  747. if (WorkQueueItem->CommandParamsLength <= SEP_RM_LSA_SHARED_MEMORY_SIZE) {
  748. RtlCopyMemory(
  749. SepRmState.RmViewPortMemory,
  750. WorkQueueItem->CommandParams.BaseAddress,
  751. WorkQueueItem->CommandParamsLength
  752. );
  753. LsaViewCopiedCommandParams = SepRmState.LsaViewPortMemory;
  754. CommandMessage.CommandParamsMemoryType = SepRmLsaCommandPortSharedMemory;
  755. } else {
  756. Status = SepAdtCopyToLsaSharedMemory(
  757. SepLsaHandle,
  758. WorkQueueItem->CommandParams.BaseAddress,
  759. WorkQueueItem->CommandParamsLength,
  760. &LsaViewCopiedCommandParams
  761. );
  762. if (!NT_SUCCESS(Status)) {
  763. //
  764. // An error occurred, most likely in allocating
  765. // shared virtual memory. For now, just ignore
  766. // the error and discard the Audit Record. Later,
  767. // we may consider generating a warning record
  768. // indicating some records lost.
  769. //
  770. break;
  771. }
  772. CommandMessage.CommandParamsMemoryType = SepRmLsaCustomSharedMemory;
  773. }
  774. //
  775. // Buffer has been successfully copied to a shared Lsa
  776. // memory buffer. Place the address of the buffer valid in
  777. // the LSA's process context in the Command Message.
  778. //
  779. *((PVOID *) CommandMessage.CommandParams) =
  780. LsaViewCopiedCommandParams;
  781. CommandMessage.MessageHeader.u1.s1.TotalLength =
  782. ((CSHORT) RM_COMMAND_MESSAGE_HEADER_SIZE +
  783. (CSHORT) sizeof( LsaViewCopiedCommandParams ));
  784. CommandMessage.MessageHeader.u1.s1.DataLength =
  785. CommandMessage.MessageHeader.u1.s1.TotalLength -
  786. (CSHORT) sizeof(PORT_MESSAGE);
  787. }
  788. //
  789. // Free input command params buffer if Paged Pool.
  790. //
  791. if (WorkQueueItem->CommandParamsMemoryType == SepRmPagedPoolMemory) {
  792. ExFreePool( WorkQueueItem->CommandParams.BaseAddress );
  793. }
  794. break;
  795. default:
  796. Status = STATUS_INVALID_PARAMETER;
  797. break;
  798. }
  799. if (NT_SUCCESS(Status)) {
  800. //
  801. // Send Message to the LSA via the LSA Server Command LPC Port.
  802. // This must be done in the process in which the handle was created.
  803. //
  804. Status = ZwRequestWaitReplyPort(
  805. SepRmState.LsaCommandPortHandle,
  806. (PPORT_MESSAGE) &CommandMessage,
  807. (PPORT_MESSAGE) &ReplyMessage
  808. );
  809. //
  810. // If the command was successful, copy the data back to the output
  811. // buffer.
  812. //
  813. if (NT_SUCCESS(Status)) {
  814. //
  815. // Move output from command (if any) to buffer. Note that this
  816. // is done even if the command returns status, because some status
  817. // values are not errors.
  818. //
  819. if (ARGUMENT_PRESENT(WorkQueueItem->ReplyBuffer)) {
  820. RtlCopyMemory(
  821. WorkQueueItem->ReplyBuffer,
  822. ReplyMessage.ReplyBuffer,
  823. WorkQueueItem->ReplyBufferLength
  824. );
  825. }
  826. //
  827. // Return status from command.
  828. //
  829. Status = ReplyMessage.ReturnedStatus;
  830. if (!NT_SUCCESS(Status)) {
  831. KdPrint(("Security: Command sent from RM to LSA returned 0x%lx\n",
  832. Status));
  833. }
  834. } else {
  835. KdPrint(("Security: Sending Command RM to LSA failed 0x%lx\n", Status));
  836. }
  837. //
  838. // On return from the LPC call to the LSA, we expect the called
  839. // LSA worker routine to have copied the Command Parameters
  840. // buffer (if any). If a custom shared memory boffer was allocated,
  841. // free it now.
  842. //
  843. if (CommandMessage.CommandParamsMemoryType == SepRmLsaCustomSharedMemory) {
  844. RegionSize = 0;
  845. Status = ZwFreeVirtualMemory(
  846. SepLsaHandle,
  847. (PVOID *) &CommandMessage.CommandParams,
  848. &RegionSize,
  849. MEM_RELEASE
  850. );
  851. ASSERT(NT_SUCCESS(Status));
  852. }
  853. }
  854. //
  855. // Clean up. We must call the cleanup functions on its parameter
  856. // and then free the used WorkQueueItem itself.
  857. //
  858. if ( ARGUMENT_PRESENT( WorkQueueItem->CleanupFunction)) {
  859. (WorkQueueItem->CleanupFunction)(WorkQueueItem->CleanupParameter);
  860. }
  861. //
  862. // Determine if there is more work to do on this list
  863. //
  864. WorkQueueItem = SepDequeueWorkItem();
  865. #if 0
  866. if ( WorkQueueItem ) {
  867. DbgPrint("Got another item from list, going back\n");
  868. } else {
  869. DbgPrint("List is empty, leaving\n");
  870. }
  871. #endif
  872. }
  873. KeDetachProcess();
  874. if ( LocalListLength > SepLsaQueueLength ) {
  875. SepLsaQueueLength = LocalListLength;
  876. }
  877. return Status;
  878. }
  879. BOOLEAN
  880. SepRmInitPhase0(
  881. )
  882. /*++
  883. Routine Description:
  884. This function performs Reference Monitor Phase 0 initialization.
  885. This includes initializing the reference monitor database to a state
  886. which allows access validation routines to operate (always granting
  887. access) prior to the main init of the Reference Monitor in Phase 1
  888. initialization, without having to check if the RM is initialized.
  889. Arguments:
  890. None.
  891. Return Value:
  892. BOOLEAN - TRUE if successful, else FALSE
  893. --*/
  894. {
  895. BOOLEAN CompletionStatus;
  896. PAGED_CODE();
  897. CompletionStatus = SepRmDbInitialization();
  898. return CompletionStatus;
  899. }