3333#define IsLowV86Mem (_Seg , _Off ) ((((_Seg) << 4) + (_Off)) < (0xa0000))
3434
3535/* Those two functions below are there so that CSRSS can't access low mem.
36- * Expecially , MAKE IT CRASH ON NULL ACCESS */
36+ * Especially , MAKE IT CRASH ON NULL ACCESS */
3737static
3838VOID
3939ProtectLowV86Mem (VOID )
@@ -44,7 +44,7 @@ ProtectLowV86Mem(VOID)
4444 NTSTATUS Status ;
4545 SIZE_T ViewSize = 0xa0000 - PAGE_SIZE ;
4646
47- /* We should only do that for CSRSS. */
47+ /* We should only do that for CSRSS */
4848 ASSERT (PsGetCurrentProcess () == (PEPROCESS )CsrProcess );
4949
5050 /* Commit (again) the pages, but with PAGE_NOACCESS protection */
@@ -96,6 +96,9 @@ IntInitializeVideoAddressSpace(VOID)
9696 CHAR IVTAndBda [1024 + 256 ];
9797#endif // _M_IX86
9898
99+ /* We should only do that for CSRSS */
100+ ASSERT (PsGetCurrentProcess () == (PEPROCESS )CsrProcess );
101+
99102 /* Free the 1MB pre-reserved region. In reality, ReactOS should simply support us mapping the view into the reserved area, but it doesn't. */
100103 BaseAddress = 0 ;
101104 ViewSize = 1024 * 1024 ;
@@ -216,13 +219,15 @@ IntInt10AllocateBuffer(
216219 NTSTATUS Status ;
217220#ifdef _M_IX86
218221 PVOID MemoryAddress ;
219- PKPROCESS CallingProcess = ( PKPROCESS ) PsGetCurrentProcess () ;
222+ PKPROCESS CallingProcess ;
220223 KAPC_STATE ApcState ;
221224 SIZE_T Size ;
222225
223226 TRACE_ (VIDEOPRT , "IntInt10AllocateBuffer\n" );
224227
225- IntAttachToCSRSS (& CallingProcess , & ApcState );
228+ /* Perform the call in the CSRSS context */
229+ if (!IntAttachToCSRSS (& CallingProcess , & ApcState ))
230+ return ERROR_INVALID_PARAMETER ;
226231
227232 Size = * Length ;
228233 MemoryAddress = (PVOID )0x20000 ;
@@ -236,7 +241,7 @@ IntInt10AllocateBuffer(
236241 if (!NT_SUCCESS (Status ))
237242 {
238243 WARN_ (VIDEOPRT , "- ZwAllocateVirtualMemory failed\n" );
239- IntDetachFromCSRSS (& CallingProcess , & ApcState );
244+ IntDetachFromCSRSS (CallingProcess , & ApcState );
240245 return ERROR_NOT_ENOUGH_MEMORY ;
241246 }
242247
@@ -247,20 +252,20 @@ IntInt10AllocateBuffer(
247252 & Size ,
248253 MEM_RELEASE );
249254 WARN_ (VIDEOPRT , "- Unacceptable memory allocated\n" );
250- IntDetachFromCSRSS (& CallingProcess , & ApcState );
255+ IntDetachFromCSRSS (CallingProcess , & ApcState );
251256 return ERROR_NOT_ENOUGH_MEMORY ;
252257 }
253258
259+ IntDetachFromCSRSS (CallingProcess , & ApcState );
260+
254261 * Length = (ULONG )Size ;
255262 * Seg = (USHORT )((ULONG_PTR )MemoryAddress >> 4 );
256263 * Off = (USHORT )((ULONG_PTR )MemoryAddress & 0xF );
257264
258- INFO_ (VIDEOPRT , "- Segment: %x\n" , ( ULONG_PTR ) MemoryAddress >> 4 );
259- INFO_ (VIDEOPRT , "- Offset: %x\n" , ( ULONG_PTR ) MemoryAddress & 0xF );
265+ INFO_ (VIDEOPRT , "- Segment: %x\n" , * Seg );
266+ INFO_ (VIDEOPRT , "- Offset: %x\n" , * Off );
260267 INFO_ (VIDEOPRT , "- Length: %x\n" , * Length );
261268
262- IntDetachFromCSRSS (& CallingProcess , & ApcState );
263-
264269 return NO_ERROR ;
265270#else
266271 Status = x86BiosAllocateBuffer (Length , Seg , Off );
@@ -278,21 +283,24 @@ IntInt10FreeBuffer(
278283 NTSTATUS Status ;
279284#ifdef _M_IX86
280285 PVOID MemoryAddress = (PVOID )((ULONG_PTR )(Seg << 4 ) | Off );
281- PKPROCESS CallingProcess = ( PKPROCESS ) PsGetCurrentProcess () ;
286+ PKPROCESS CallingProcess ;
282287 KAPC_STATE ApcState ;
283288 SIZE_T Size = 0 ;
284289
285290 TRACE_ (VIDEOPRT , "IntInt10FreeBuffer\n" );
286291 INFO_ (VIDEOPRT , "- Segment: %x\n" , Seg );
287292 INFO_ (VIDEOPRT , "- Offset: %x\n" , Off );
288293
289- IntAttachToCSRSS (& CallingProcess , & ApcState );
294+ /* Perform the call in the CSRSS context */
295+ if (!IntAttachToCSRSS (& CallingProcess , & ApcState ))
296+ return ERROR_INVALID_PARAMETER ;
297+
290298 Status = ZwFreeVirtualMemory (NtCurrentProcess (),
291299 & MemoryAddress ,
292300 & Size ,
293301 MEM_RELEASE );
294302
295- IntDetachFromCSRSS (& CallingProcess , & ApcState );
303+ IntDetachFromCSRSS (CallingProcess , & ApcState );
296304
297305 return Status ;
298306#else
@@ -311,7 +319,7 @@ IntInt10ReadMemory(
311319 IN ULONG Length )
312320{
313321#ifdef _M_IX86
314- PKPROCESS CallingProcess = ( PKPROCESS ) PsGetCurrentProcess () ;
322+ PKPROCESS CallingProcess ;
315323 KAPC_STATE ApcState ;
316324
317325 TRACE_ (VIDEOPRT , "IntInt10ReadMemory\n" );
@@ -320,15 +328,17 @@ IntInt10ReadMemory(
320328 INFO_ (VIDEOPRT , "- Buffer: %x\n" , Buffer );
321329 INFO_ (VIDEOPRT , "- Length: %x\n" , Length );
322330
323- IntAttachToCSRSS (& CallingProcess , & ApcState );
331+ /* Perform the call in the CSRSS context */
332+ if (!IntAttachToCSRSS (& CallingProcess , & ApcState ))
333+ return ERROR_INVALID_PARAMETER ;
324334
325335 if (IsLowV86Mem (Seg , Off ))
326336 UnprotectLowV86Mem ();
327337 RtlCopyMemory (Buffer , (PVOID )((ULONG_PTR )(Seg << 4 ) | Off ), Length );
328338 if (IsLowV86Mem (Seg , Off ))
329339 ProtectLowV86Mem ();
330340
331- IntDetachFromCSRSS (& CallingProcess , & ApcState );
341+ IntDetachFromCSRSS (CallingProcess , & ApcState );
332342
333343 return NO_ERROR ;
334344#else
@@ -349,7 +359,7 @@ IntInt10WriteMemory(
349359 IN ULONG Length )
350360{
351361#ifdef _M_IX86
352- PKPROCESS CallingProcess = ( PKPROCESS ) PsGetCurrentProcess () ;
362+ PKPROCESS CallingProcess ;
353363 KAPC_STATE ApcState ;
354364
355365 TRACE_ (VIDEOPRT , "IntInt10WriteMemory\n" );
@@ -358,13 +368,17 @@ IntInt10WriteMemory(
358368 INFO_ (VIDEOPRT , "- Buffer: %x\n" , Buffer );
359369 INFO_ (VIDEOPRT , "- Length: %x\n" , Length );
360370
361- IntAttachToCSRSS (& CallingProcess , & ApcState );
371+ /* Perform the call in the CSRSS context */
372+ if (!IntAttachToCSRSS (& CallingProcess , & ApcState ))
373+ return ERROR_INVALID_PARAMETER ;
374+
362375 if (IsLowV86Mem (Seg , Off ))
363376 UnprotectLowV86Mem ();
364377 RtlCopyMemory ((PVOID )((ULONG_PTR )(Seg << 4 ) | Off ), Buffer , Length );
365378 if (IsLowV86Mem (Seg , Off ))
366379 ProtectLowV86Mem ();
367- IntDetachFromCSRSS (& CallingProcess , & ApcState );
380+
381+ IntDetachFromCSRSS (CallingProcess , & ApcState );
368382
369383 return NO_ERROR ;
370384#else
@@ -387,16 +401,11 @@ IntInt10CallBios(
387401 CONTEXT BiosContext ;
388402#endif
389403 NTSTATUS Status ;
390- PKPROCESS CallingProcess = ( PKPROCESS ) PsGetCurrentProcess () ;
404+ PKPROCESS CallingProcess ;
391405 KAPC_STATE ApcState ;
392406
393- /* Attach to CSRSS */
394- IntAttachToCSRSS (& CallingProcess , & ApcState );
395-
396- /* Clear the context */
407+ /* Clear the context and fill out the BIOS arguments */
397408 RtlZeroMemory (& BiosContext , sizeof (BiosContext ));
398-
399- /* Fill out the bios arguments */
400409 BiosContext .Eax = BiosArguments -> Eax ;
401410 BiosContext .Ebx = BiosArguments -> Ebx ;
402411 BiosContext .Ecx = BiosArguments -> Ecx ;
@@ -407,6 +416,10 @@ IntInt10CallBios(
407416 BiosContext .SegDs = BiosArguments -> SegDs ;
408417 BiosContext .SegEs = BiosArguments -> SegEs ;
409418
419+ /* Perform the call in the CSRSS context */
420+ if (!IntAttachToCSRSS (& CallingProcess , & ApcState ))
421+ return ERROR_INVALID_PARAMETER ;
422+
410423 /* Do the ROM BIOS call */
411424 (void )KeWaitForMutexObject (& VideoPortInt10Mutex ,
412425 Executive ,
@@ -425,6 +438,8 @@ IntInt10CallBios(
425438
426439 KeReleaseMutex (& VideoPortInt10Mutex , FALSE);
427440
441+ IntDetachFromCSRSS (CallingProcess , & ApcState );
442+
428443 /* Return the arguments */
429444 BiosArguments -> Eax = BiosContext .Eax ;
430445 BiosArguments -> Ebx = BiosContext .Ebx ;
@@ -436,15 +451,7 @@ IntInt10CallBios(
436451 BiosArguments -> SegDs = (USHORT )BiosContext .SegDs ;
437452 BiosArguments -> SegEs = (USHORT )BiosContext .SegEs ;
438453
439- /* Detach and return status */
440- IntDetachFromCSRSS (& CallingProcess , & ApcState );
441-
442- if (NT_SUCCESS (Status ))
443- {
444- return NO_ERROR ;
445- }
446-
447- return ERROR_INVALID_PARAMETER ;
454+ return NT_SUCCESS (Status ) ? NO_ERROR : ERROR_INVALID_PARAMETER ;
448455}
449456
450457/* PUBLIC FUNCTIONS ***********************************************************/
0 commit comments