kernel
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#include "../../assert.h"
22
23void
25 IN PIPROCESS Process,
26 OUT PAPC_STATE ApcState
27)
28
29/*++
30
31 Routine description:
32
33 Attach to a process address space, this routine should be managed carefully, and have simple code between the attaching and detaching.
34
35
36 Arguments:
37
38 [IN] PIPROCESS Process - Pointer to process to attach to (IPROCESS)
39 [OUT] PAPC_STATE - Pointer to store the state in resident memory.
40
41 Return Values:
42
43 None.
44
45 Notes:
46
47 DPCs CANNOT attach to a different process.
48
49--*/
50
51{
52 if (MeIsExecutingDpc()) {
53 // CANNOT Attach to a process while executing a DPC.
56 (void*)Process,
57 (void*)(uintptr_t)RETADDR(0),
58 (void*)MeIsExecutingDpc(),
59 NULL
60 );
61 }
62
63 PITHREAD CurrentThread = MeGetCurrentThread();
64 if (unlikely(!CurrentThread)) return;
65
66 // Save the process we were running on.
67 ApcState->SavedApcProcess = CurrentThread->ApcState.SavedApcProcess;
68 ApcState->SavedCr3 = __read_cr3();
69 ApcState->AttachedToProcess = true;
70 ApcState->SavedThreadAttached = CurrentThread->ApcState.AttachedToProcess;
71
72 // Raise to SYNCH and lock scheduler.
73 // TODO SYNCH
75
76 // Switch identity to new process.
77 CurrentThread->ApcState.SavedApcProcess = PsGetEProcessFromIProcess(Process);
78 CurrentThread->ApcState.AttachedToProcess = true;
79
80 // Switch CR3s.
81 uint64_t TargetCr3 = Process->PageDirectoryPhysical;
82 assert(TargetCr3 != 0);
83
84 if (ApcState->SavedCr3 != TargetCr3) {
85 __write_cr3(TargetCr3);
86 }
87}
88
89void
91 IN PAPC_STATE ApcState
92)
93
94/*++
95
96 Routine description:
97
98 Detach from a process address space.
99
100 Arguments:
101
102 [IN] PAPC_STATE ApcState - The APC_STATE stored by MeAttachProcess.
103
104 Return Values:
105
106 None.
107
108--*/
109
110{
111 PITHREAD CurrentThread = MeGetCurrentThread();
112 if (unlikely(!CurrentThread)) return;
113 if (!ApcState->AttachedToProcess) return;
114
115 // Restore original CR3.
116 uint64_t CurrentCr3 = __read_cr3();
117 if (CurrentCr3 != ApcState->SavedCr3) {
118 __write_cr3(ApcState->SavedCr3);
119 }
120
121 // Restore thread's identity to original process.
122 CurrentThread->ApcState.SavedApcProcess = ApcState->SavedApcProcess;
123 CurrentThread->ApcState.AttachedToProcess = ApcState->SavedThreadAttached;
124
125 // Restore scheduler lock / IRQL.
126 // TODO SYNCH LEVEL
128
129 // Clear attached state.
130 ApcState->AttachedToProcess = false;
131}
#define IN
Definition annotations.h:8
#define OUT
Definition annotations.h:9
#define assert(...)
Definition assert.h:57
void MeDetachProcess(IN PAPC_STATE ApcState)
Definition attach.c:90
void MeAttachProcess(IN PIPROCESS Process, OUT PAPC_STATE ApcState)
Definition attach.c:24
NORETURN void MeBugCheckEx(IN enum _BUGCHECK_CODES BugCheckCode, IN void *BugCheckParameter1, IN void *BugCheckParameter2, IN void *BugCheckParameter3, IN void *BugCheckParameter4)
Definition bugcheck.c:245
IPROCESS * PIPROCESS
Definition core.h:40
ITHREAD * PITHREAD
Definition core.h:36
FORCEINLINE void __write_cr3(uint64_t val)
Definition intrin.h:98
FORCEINLINE uint64_t __read_cr3(void)
Definition intrin.h:93
#define RETADDR(level)
Definition macros.h:53
#define unlikely(x)
Definition macros.h:62
@ INVALID_PROCESS_ATTACH_ATTEMPT
Definition me.h:134
FORCEINLINE PITHREAD MeGetCurrentThread(void)
Definition me.h:444
FORCEINLINE bool MeIsExecutingDpc(void)
Definition me.h:466
FORCEINLINE void MeReleaseSchedulerLock(void)
Definition me.h:393
struct _APC_STATE * PAPC_STATE
FORCEINLINE void MeAcquireSchedulerLock(void)
Definition me.h:378
FORCEINLINE PEPROCESS PsGetEProcessFromIProcess(IN PIPROCESS IProcess)
Definition ps.h:325
bool AttachedToProcess
Definition me.h:255
PEPROCESS SavedApcProcess
Definition me.h:254
struct _APC_STATE ApcState
Definition me.h:275