12{
13 KAPC_STATE apcState;
14 PMDL mdl = NULL;
15 PVOID kernelAddress = NULL;
16 NTSTATUS status = STATUS_SUCCESS;
17
18
19 KeStackAttachProcess(targetProcess, &apcState);
20
21 __try {
22
23 mdl = IoAllocateMdl(TargetAddress, (ULONG)Size, FALSE, FALSE, NULL);
24 if (!mdl) {
25 status = STATUS_INSUFFICIENT_RESOURCES;
26 __leave;
27 }
28
29 __try {
30
31 MmProbeAndLockPages(mdl, KernelMode, IoWriteAccess);
32
33
34 kernelAddress = MmGetSystemAddressForMdlSafe(mdl, NormalPagePriority);
35 if (!kernelAddress) {
36 status = STATUS_INSUFFICIENT_RESOURCES;
37 __leave;
38 }
39
40
41 RtlCopyMemory(kernelAddress, DataToWrite, Size);
42 }
43 __except (EXCEPTION_EXECUTE_HANDLER) {
44 status = GetExceptionCode();
45 }
46 }
47 __finally {
48
49 if (kernelAddress) {
50 MmUnmapLockedPages(kernelAddress, mdl);
51 }
52 if (mdl) {
53 if (mdl->MdlFlags & MDL_PAGES_LOCKED) {
54 MmUnlockPages(mdl);
55 }
56 IoFreeMdl(mdl);
57 }
58
59
60 KeUnstackDetachProcess(&apcState);
61
62
63 ObDereferenceObject(targetProcess);
64 }
65
66 return status;
67}