-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrules.c
More file actions
228 lines (199 loc) · 7.02 KB
/
Copy pathrules.c
File metadata and controls
228 lines (199 loc) · 7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (c) 2026 Lafnaps
/*++
rules.c — loading and validating the rule table from the registry.
Everything here runs at PASSIVE_LEVEL during driver start. The data comes from
outside (the registry), so it is validated pedantically: the size, the count and
the indices in the blob cannot be trusted.
--*/
#include "kbdralt.h"
#ifdef ALLOC_PRAGMA
// Rules are only loaded at startup, at PASSIVE_LEVEL, so there is no reason to keep
// this code resident. Without this line the analyzer reports C28172.
#pragma alloc_text (PAGE, KbdRAltLoadRules)
#endif
PKBDRALT_RULETABLE g_Rules = NULL;
#define KBDRALT_POOL_TAG 'tlRK' // 'KRlt'
//
// The built-in rule: right Alt (E0 38) -> LAlt+LShift chord.
// Used when the registry holds nothing or the blob fails validation.
//
static VOID
KbdRAltFillDefault(
_Out_ PKBDRALT_RULETABLE table
)
{
RtlZeroMemory(table, sizeof(*table));
table->Count = 1;
table->Rule[0].InScan = SCAN_ALT;
table->Rule[0].InExtended = 1;
table->Rule[0].Mode = KbdRAltModeChord;
table->Rule[0].OutCount = 2;
table->Rule[0].Out[0].Scan = SCAN_ALT; // LAlt
table->Rule[0].Out[0].Extended = 0;
table->Rule[0].Out[1].Scan = SCAN_SHIFT; // LShift
table->Rule[0].Out[1].Extended = 0;
}
//
// Validate a single rule. Returns FALSE if the rule must not be executed.
//
static BOOLEAN
KbdRAltRuleIsSane(
_In_ const KBDRALT_RULE *r
)
{
ULONG i;
if (r->Mode >= KbdRAltModeMax) {
return FALSE;
}
// The reserved field must be zero. That lets a future version give it meaning
// (per-device scoping, for instance) and be sure old blobs are not misread.
if (r->Reserved != 0) {
return FALSE;
}
// The extended flag must be exactly 0 or 1: the comparison in FindRule is exact,
// so a value of 2 would silently make the rule unreachable.
if (r->InExtended > 1) {
return FALSE;
}
if (r->OutCount == 0 || r->OutCount > KBDRALT_MAX_OUT_PER_RULE) {
return FALSE;
}
// A key-for-key substitution must produce exactly one key, otherwise it is
// ambiguous what to emit on make and what on break.
if (r->Mode == KbdRAltModeRemap && r->OutCount != 1) {
return FALSE;
}
// A set-1 scan code is one byte. Zero is not valid on either side.
if (r->InScan == 0 || r->InScan > 0xFF) {
return FALSE;
}
for (i = 0; i < r->OutCount; i++) {
if (r->Out[i].Scan == 0 || r->Out[i].Scan > 0xFF) {
return FALSE;
}
if (r->Out[i].Extended > 1) {
return FALSE;
}
}
return TRUE;
}
//
// Two rules for the same input key is almost certainly a typo in the configuration.
// The first one would win while the author expected the second, and the cause would
// take a long time to find. Reject the whole table instead.
//
static BOOLEAN
KbdRAltHasDuplicateInputs(
_In_reads_(count) const KBDRALT_RULE *rules,
_In_ ULONG count
)
{
ULONG i, j;
for (i = 0; i < count; i++) {
for (j = i + 1; j < count; j++) {
if (rules[i].InScan == rules[j].InScan &&
rules[i].InExtended == rules[j].InExtended) {
return TRUE;
}
}
}
return FALSE;
}
//
// Read the blob from Parameters\Rules and turn it into a working table.
// On any failure the built-in rule is kept, so the driver is never left idle.
//
_IRQL_requires_max_(PASSIVE_LEVEL)
VOID
KbdRAltLoadRules(
_In_ WDFDRIVER Driver
)
{
NTSTATUS status;
WDFKEY key = NULL;
WDFMEMORY mem = NULL;
PKBDRALT_RULETABLE table;
DECLARE_CONST_UNICODE_STRING(valueName, L"Rules");
PAGED_CODE();
table = (PKBDRALT_RULETABLE)ExAllocatePool2(POOL_FLAG_NON_PAGED,
sizeof(KBDRALT_RULETABLE),
KBDRALT_POOL_TAG);
if (table == NULL) {
return; // g_Rules keeps its previous value; NULL on first start, see below
}
KbdRAltFillDefault(table);
status = WdfDriverOpenParametersRegistryKey(Driver, KEY_READ,
WDF_NO_OBJECT_ATTRIBUTES, &key);
if (NT_SUCCESS(status)) {
WDF_OBJECT_ATTRIBUTES memAttr;
WDF_OBJECT_ATTRIBUTES_INIT(&memAttr);
memAttr.ParentObject = key;
status = WdfRegistryQueryMemory(key, &valueName, NonPagedPoolNx,
&memAttr, &mem, NULL);
if (NT_SUCCESS(status)) {
size_t blobSize = 0;
PKBDRALT_RULES blob = (PKBDRALT_RULES)WdfMemoryGetBuffer(mem, &blobSize);
// The header itself must fit inside the blob.
if (blob != NULL &&
blobSize >= FIELD_OFFSET(KBDRALT_RULES, Rule) &&
blob->Version == KBDRALT_RULES_VERSION &&
blob->Count > 0 &&
blob->Count <= KBDRALT_MAX_RULES) {
size_t need = FIELD_OFFSET(KBDRALT_RULES, Rule) +
(size_t)blob->Count * sizeof(KBDRALT_RULE);
if (blobSize >= need) {
ULONG i;
ULONG good = 0;
BOOLEAN allSane = TRUE;
for (i = 0; i < blob->Count; i++) {
if (!KbdRAltRuleIsSane(&blob->Rule[i])) {
allSane = FALSE;
break;
}
}
if (allSane &&
KbdRAltHasDuplicateInputs(blob->Rule, blob->Count)) {
allSane = FALSE;
}
// Accept the table whole or not at all: a partially applied rule
// set is the worst outcome, because the user believes it is
// configured while only half of it works.
if (allSane) {
RtlZeroMemory(table, sizeof(*table));
for (i = 0; i < blob->Count; i++) {
table->Rule[good++] = blob->Rule[i];
}
table->Count = good;
}
}
}
}
WdfRegistryClose(key);
}
{
PKBDRALT_RULETABLE old =
(PKBDRALT_RULETABLE)InterlockedExchangePointer((PVOID volatile *)&g_Rules,
table);
if (old != NULL) {
// Nothing can be reading the old table concurrently at driver start:
// no devices exist yet and ServiceCallback is not connected.
ExFreePoolWithTag(old, KBDRALT_POOL_TAG);
}
}
}
//
// Released when the driver unloads.
//
VOID
KbdRAltEvtDriverCleanup(
_In_ WDFOBJECT Driver
)
{
PKBDRALT_RULETABLE old;
UNREFERENCED_PARAMETER(Driver);
old = (PKBDRALT_RULETABLE)InterlockedExchangePointer((PVOID volatile *)&g_Rules, NULL);
if (old != NULL) {
ExFreePoolWithTag(old, KBDRALT_POOL_TAG);
}
}