My Project
Loading...
Searching...
No Matches
attach.c
Go to the documentation of this file.
1/*++
2
3Module Name:
4
5 attach.c
6
7Purpose:
8
9 This module contains the implementation of process attaching.
10
11Author:
12
13 slep (Matanel) 2025.
14
15Revision History:
16
17--*/
18
19#include "../../includes/me.h"
20#include "../../includes/ps.h"
21
22void
24 IN PIPROCESS Process,
25 OUT PAPC_STATE ApcState
26)
27
28/*++
29
30 Routine description:
31
32 Attach to a process address space, this routine should be managed carefully, and have simple code between the attaching and detaching.
33
34
35 Arguments:
36
37 [IN] PIPROCESS Process - Pointer to process to attach to (IPROCESS)
38 [OUT] PAPC_STATE - Pointer to store the state in resident memory.
39
40 Return Values:
41
42 None.
43
44 Notes:
45
46 DPCs CANNOT attach to a different process.
47
48--*/
49
50{
51 if (MeIsExecutingDpc()) {
52 // CANNOT Attach to a process while executing a DPC.
55 (void*)Process,
56 (void*)(uintptr_t)RETADDR(0),
57 (void*)MeIsExecutingDpc(),
58 NULL
59 );
60 }
61
62 PITHREAD CurrentThread = MeGetCurrentThread();
63 if (unlikely(!CurrentThread)) return;
64
65 // Save the process we were running on.
66 ApcState->SavedApcProcess = CurrentThread->ApcState.SavedApcProcess;
67 ApcState->SavedCr3 = __read_cr3();
68 ApcState->AttachedToProcess = true;
69
70 // Raise to SYNCH and lock scheduler.
71 // TODO SYNCH
73
74 // Switch identity to new process.
75 CurrentThread->ApcState.SavedApcProcess = PsGetEProcessFromIProcess(Process);
76 CurrentThread->ApcState.AttachedToProcess = true;
77
78 // Switch CR3s.
79 uint64_t TargetCr3 = Process->PageDirectoryPhysical;
80 if (ApcState->SavedCr3 != TargetCr3) {
81 __write_cr3(TargetCr3);
82 }
83}
84
85void
87 IN PAPC_STATE ApcState
88)
89
90/*++
91
92 Routine description:
93
94 Detach from a process address space.
95
96 Arguments:
97
98 [IN] PAPC_STATE ApcState - The APC_STATE stored by MeAttachProcess.
99
100 Return Values:
101
102 None.
103
104--*/
105
106{
107 PITHREAD CurrentThread = MeGetCurrentThread();
108 if (unlikely(!CurrentThread)) return;
109 if (!ApcState->AttachedToProcess) return;
110
111 // Restore original CR3.
112 uint64_t CurrentCr3 = __read_cr3();
113 if (CurrentCr3 != ApcState->SavedCr3) {
114 __write_cr3(ApcState->SavedCr3);
115 }
116
117 // Restore thread's identity to original process.
118 CurrentThread->ApcState.SavedApcProcess = ApcState->SavedApcProcess;
119 CurrentThread->ApcState.AttachedToProcess = ApcState->AttachedToProcess;
120
121 // Restore scheduler lock / IRQL.
122 // TODO SYNCH LEVEL
124
125 // Clear attached state.
126 ApcState->AttachedToProcess = false;
127}
#define IN
Definition annotations.h:7
#define OUT
Definition annotations.h:8
void MeDetachProcess(IN PAPC_STATE ApcState)
Definition attach.c:86
void MeAttachProcess(IN PIPROCESS Process, OUT PAPC_STATE ApcState)
Definition attach.c:23
NORETURN void MeBugCheckEx(IN enum _BUGCHECK_CODES BugCheckCode, IN void *BugCheckParameter1, IN void *BugCheckParameter2, IN void *BugCheckParameter3, IN void *BugCheckParameter4)
Definition bugcheck.c:305
IPROCESS * PIPROCESS
Definition core.h:38
ITHREAD * PITHREAD
Definition core.h:34
FORCEINLINE void __write_cr3(uint64_t val)
Definition intrin.h:83
FORCEINLINE uint64_t __read_cr3(void)
Definition intrin.h:78
#define RETADDR(level)
Definition macros.h:38
#define unlikely(x)
Definition macros.h:47
@ INVALID_PROCESS_ATTACH_ATTEMPT
Definition me.h:134
FORCEINLINE PITHREAD MeGetCurrentThread(void)
Definition me.h:431
FORCEINLINE bool MeIsExecutingDpc(void)
Definition me.h:453
FORCEINLINE void MeReleaseSchedulerLock(void)
Definition me.h:380
struct _APC_STATE * PAPC_STATE
FORCEINLINE void MeAcquireSchedulerLock(void)
Definition me.h:365
FORCEINLINE PEPROCESS PsGetEProcessFromIProcess(IN PIPROCESS IProcess)
Definition ps.h:249
bool AttachedToProcess
Definition me.h:250
PEPROCESS SavedApcProcess
Definition me.h:249
struct _APC_STATE ApcState
Definition me.h:270