Skip to content

Commit a9adb20

Browse files
committed
Driver: reorder code and add socket selection
1 parent 659a54b commit a9adb20

File tree

2 files changed

+59
-39
lines changed

2 files changed

+59
-39
lines changed

ScaphandreDrv/Driver.c

+54-39
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,13 @@
66
#pragma alloc_text (INIT, DriverEntry)
77
#endif
88

9-
NTSTATUS
10-
DriverEntry(
11-
_In_ PDRIVER_OBJECT DriverObject,
12-
_In_ PUNICODE_STRING RegistryPath
13-
)
9+
NTSTATUS DriverEntry(PDRIVER_OBJECT DriverObject,
10+
PUNICODE_STRING RegistryPath)
1411
{
15-
NTSTATUS status;
1612
PDEVICE_OBJECT device_object;
1713
UNICODE_STRING device_name;
1814
UNICODE_STRING sym_name;
15+
NTSTATUS status;
1916

2017
DbgPrint("Registry path address: %p\n", RegistryPath);
2118

@@ -55,14 +52,16 @@ NTSTATUS DispatchCreate(PDEVICE_OBJECT device, PIRP irp)
5552
memset(manufacturer, 0, sizeof(manufacturer));
5653
__cpuid(cpu_regs, 0);
5754
memcpy(manufacturer, &cpu_regs[1], sizeof(unsigned __int32));
58-
memcpy(manufacturer + sizeof(unsigned __int32), &cpu_regs[3], sizeof(unsigned __int32));
59-
memcpy(manufacturer + 2 * sizeof(unsigned __int32), &cpu_regs[2], sizeof(unsigned __int32));
55+
memcpy(manufacturer + sizeof(unsigned __int32), &cpu_regs[3],
56+
sizeof(unsigned __int32));
57+
memcpy(manufacturer + 2 * sizeof(unsigned __int32), &cpu_regs[2],
58+
sizeof(unsigned __int32));
6059

61-
if (strncmp(manufacturer, "GenuineIntel", sizeof(manufacturer) - 1) == 0)
60+
if (!strncmp(manufacturer, "GenuineIntel", sizeof(manufacturer) - 1))
6261
machine_type = E_MACHINE_INTEL;
63-
else if (strncmp(manufacturer, "AMDisbetter!", sizeof(manufacturer) - 1) == 0)
62+
else if (!strncmp(manufacturer, "AMDisbetter!", sizeof(manufacturer) - 1))
6463
machine_type = E_MACHINE_AMD;
65-
else if (strncmp(manufacturer, "AuthenticAMD", sizeof(manufacturer) - 1) == 0)
64+
else if (!strncmp(manufacturer, "AuthenticAMD", sizeof(manufacturer) - 1))
6665
machine_type = E_MACHINE_AMD;
6766
else
6867
machine_type = E_MACHINE_UNK;
@@ -96,44 +95,60 @@ NTSTATUS DispatchCleanup(PDEVICE_OBJECT device, PIRP irp)
9695

9796
NTSTATUS DispatchDeviceControl(PDEVICE_OBJECT device, PIRP irp)
9897
{
99-
NTSTATUS ntStatus;
100-
UINT32 msrRegister;
101-
ULONG inputBufferLength;
102-
ULONG outputBufferLength;
98+
GROUP_AFFINITY affinity, old;
99+
PIO_STACK_LOCATION stackLoc;
100+
PROCESSOR_NUMBER pnumber;
103101
ULONGLONG msrResult;
104-
PIO_STACK_LOCATION stackLocation;
102+
NTSTATUS ntStatus;
103+
struct data data;
104+
size_t inLength;
105105

106-
stackLocation = irp->Tail.Overlay.CurrentStackLocation;
107-
inputBufferLength = stackLocation->Parameters.DeviceIoControl.InputBufferLength;
108-
outputBufferLength = stackLocation->Parameters.DeviceIoControl.OutputBufferLength;
106+
stackLoc = irp->Tail.Overlay.CurrentStackLocation;
107+
inLength = stackLoc->Parameters.DeviceIoControl.InputBufferLength;
109108

110109
DbgPrint("Received event for driver %s... \n", device->DriverObject->DriverName);
111110

112-
/* METHOD_BUFFERED */
113-
if (inputBufferLength == sizeof(ULONGLONG))
114-
{
115-
/* MSR register codes provided by userland must not exceed 8 bytes */
116-
memcpy(&msrRegister, irp->AssociatedIrp.SystemBuffer, sizeof(ULONGLONG));
117-
if (validate_msr_lookup(msrRegister) != 0)
118-
{
119-
DbgPrint("Requested MSR register (%08x) access is not allowed!\n", msrRegister);
120-
ntStatus = STATUS_INVALID_DEVICE_REQUEST;
121-
}
122-
else
123-
{
124-
/* Call readmsr instruction */
125-
msrResult = __readmsr(msrRegister);
126-
memcpy(irp->AssociatedIrp.SystemBuffer, &msrResult, sizeof(ULONGLONG));
127-
ntStatus = STATUS_SUCCESS;
128-
irp->IoStatus.Information = sizeof(ULONGLONG);
129-
}
111+
if (inLength != sizeof(data)) {
112+
DbgPrint("Bad input length provided. Expected %zu bytes, got %zu.\n",
113+
sizeof(data), inLength);
114+
ntStatus = STATUS_INVALID_DEVICE_REQUEST;
115+
goto error;
130116
}
131-
else
117+
118+
/* Convert input data into structure */
119+
memcpy(&data, irp->AssociatedIrp.SystemBuffer, sizeof(data));
120+
if (validate_msr_lookup(data.msrRegister) != 0)
132121
{
133-
DbgPrint("Bad input length provided. Expected %u bytes, got %u.\n", sizeof(ULONGLONG), inputBufferLength);
122+
DbgPrint("Requested MSR register (%04x) access is not allowed!\n",
123+
data.msrRegister);
134124
ntStatus = STATUS_INVALID_DEVICE_REQUEST;
125+
goto error;
126+
}
127+
128+
/* Run code on the specified socket */
129+
if ((ntStatus = KeGetProcessorNumberFromIndex(data.cpuIndex, &pnumber))
130+
!= STATUS_SUCCESS) {
131+
DbgPrint("Failed to get processor info!\n");
132+
goto error;
135133
}
136134

135+
/* Set affinity */
136+
memset(&affinity, 0, sizeof(GROUP_AFFINITY));
137+
affinity.Group = pnumber.Group;
138+
KeSetSystemGroupAffinityThread(&affinity, &old);
139+
140+
/* Call readmsr instruction */
141+
msrResult = __readmsr(data.msrRegister);
142+
143+
/* Restore affinity */
144+
KeRevertToUserGroupAffinityThread(&old);
145+
146+
/* Save result */
147+
memcpy(irp->AssociatedIrp.SystemBuffer, &msrResult, sizeof(data));
148+
irp->IoStatus.Information = sizeof(data);
149+
ntStatus = STATUS_SUCCESS;
150+
151+
error:
137152
irp->IoStatus.Status = ntStatus;
138153
IofCompleteRequest(irp, IO_NO_INCREMENT);
139154

ScaphandreDrv/Driver.h

+5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ DRIVER_INITIALIZE DriverEntry;
2929

3030
EXTERN_C_END
3131

32+
struct data {
33+
UINT32 msrRegister;
34+
UINT32 cpuIndex;
35+
};
36+
3237
void DriverUnload(PDRIVER_OBJECT driver);
3338
NTSTATUS DispatchCreate(PDEVICE_OBJECT device, PIRP irp);
3439
NTSTATUS DispatchClose(PDEVICE_OBJECT device, PIRP irp);

0 commit comments

Comments
 (0)