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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
|
/*++
Copyright (c) 1989-1997 Microsoft Corporation
Module Name:
writprop.h
Abstract:
This module contains the write user FsCtl for the Ntfs Property support.
--*/
#include <viewprop.h> // needs propset.h and ntfsprop.h
#define Dbg DEBUG_TRACE_PROP_FSCTL
VOID
WritePropertyData (
IN PPROPERTY_CONTEXT Context,
IN ULONG InBufferLength,
IN PVOID InBuffer,
OUT PULONG OutBufferLength,
OUT PVOID OutBuffer
)
/*++
Routine Description:
This routine performs property write functions.
We verify the header format and perform the write operation.
Arguments:
Context - Property Context for the call
InBufferLength - Length of the command buffer
InBuffer - pointer to the unverified user command buffer. All access
to this needs to be wrapped in try/finally.
OutBufferLength - pointer to ULONG length of output buffer. This value
is set on return to indicate the total size of data within the output
buffer.
OutBuffer - pointer to the unverified user command buffer. All access
to this needs to be wrapped in try/finally
Return Value:
Nothing
--*/
{
PVOID InBufferEnd = Add2Ptr( InBuffer, InBufferLength );
PPROPERTY_INFO Info = NULL;
try {
PPROPERTY_WRITE_CONTROL Control = (PPROPERTY_WRITE_CONTROL) InBuffer;
PVOID NextOutput = OutBuffer;
ULONG TotalLength = 0;
ULONG Count;
ULONG i;
ULONG Offset;
KPROCESSOR_MODE requestorMode;
//
// Map attribute for full size
//
MapPropertyContext( Context );
//
// Verify the property set has valid contents.
//
CheckPropertySet( Context );
//
// Simple sanity check of input buffer
//
if (
//
// Long alignment of all buffers
//
InBuffer != (PVOID)LongAlign( InBuffer ) ||
OutBuffer != (PVOID)LongAlign( OutBuffer ) ||
//
// Room for control at least
//
InBufferEnd < (PVOID)(Control + 1)
) {
ExRaiseStatus( STATUS_INVALID_USER_BUFFER );
}
requestorMode = KeGetPreviousMode( );
if (requestorMode != KernelMode) {
//
// Verify that the input buffer is readable
//
ProbeForRead( InBuffer, InBufferLength, sizeof( ULONG ));
}
if (Control->Op == PWC_WRITE_PROP) {
PPROPERTY_SPECIFICATIONS InSpecs;
PPROPERTY_VALUES Values;
PROPID NextId = Control->NextPropertyId;
//
// The input buffer is:
// PROPERTY_WRITE_CONTROL
// PROPERTY_SPECIFICATIONS
// PROPERTY_VALUES
//
// The output buffer will be:
// PROPERTY_IDS
// INDIRECT_PROPERTIES
//
if (requestorMode != KernelMode) {
//
// Verify that the output buffer is readable
//
ProbeForWrite( OutBuffer, *OutBufferLength, sizeof( ULONG ));
}
//
// Build value headers array from specifiers. Calculate size of
// property Ids and indirect output.
//
InSpecs = (PPROPERTY_SPECIFICATIONS) (Control + 1);
if (InBufferEnd < (PVOID)(InSpecs + 1) ||
InBufferEnd < Add2Ptr( InSpecs, PROPERTY_SPECIFICATIONS_SIZE( InSpecs->Count ))) {
ExRaiseStatus( STATUS_INVALID_USER_BUFFER );
}
Values = (PPROPERTY_VALUES) Add2Ptr( InSpecs, LongAlign( InSpecs->Length ));
if (InBufferEnd < (PVOID)(Values + 1) ||
InBufferEnd < Add2Ptr( Values, Values->Length ) ||
InSpecs->Count != Values->Count) {
ExRaiseStatus( STATUS_INVALID_USER_BUFFER );
}
Info = BuildPropertyInfoFromPropSpec( Context,
InSpecs,
Values, // End of InSpecs
NextId );
//
// Check for enough room on output
//
TotalLength = Info->TotalIdsSize + Info->TotalIndirectSize;
if (TotalLength > *OutBufferLength) {
PULONG ReturnLength = OutBuffer;
*ReturnLength = TotalLength;
*OutBufferLength = sizeof( ULONG );
ExRaiseStatus( STATUS_BUFFER_OVERFLOW );
}
//
// Build property Ids
//
NextOutput = BuildPropertyIds( Info, OutBuffer );
//
// BUGBUG - build indirect properties
//
//
// Walk through the headers array and set the new values, saving away
// the new offsets
//
for (i = 0; i < Info->Count; i++) {
ULONG SerializedValueLength;
SERIALIZEDPROPERTYVALUE *SerializedValue;
USHORT NameLength;
PWCHAR Name;
ULONG Length;
PPROPERTY_HEAP_ENTRY HeapEntry = PROPERTY_INFO_HEAP_ENTRY( Info, i );
//
// The new values are found in the PROPERTY_VALUES input
//
SerializedValueLength = PROPERTY_VALUE_LENGTH( Values, i );
SerializedValue = PROPERTY_VALUE( Values, i );
//
// BUGBUG - handle duplicate sets
//
//
// If we've previously found a header, then we are replacing one that
// exists previously.
//
if (HeapEntry != EMPTY_PROPERTY) {
//
// if this property was named just by an Id, then use the name
// from the heap entry
//
if (InSpecs->Specifiers[i].Variant == PRSPEC_PROPID) {
NameLength = HeapEntry->PropertyNameLength;
Name = &HeapEntry->PropertyName[0];
//
// Use the name from the property specification
//
} else {
NameLength = PROPERTY_SPECIFIER_NAME_LENGTH( InSpecs, i );
Name = PROPERTY_SPECIFIER_NAME( InSpecs, i );
}
Length = PROPERTY_HEAP_ENTRY_SIZE( NameLength,
SerializedValueLength );
//
// If the new length matches that of the original header, then
// we can adjust set the values in place
//
if (Length == HeapEntry->PropertyValueLength) {
SetValueInHeap( Context, HeapEntry,
PROPERTY_INFO_ID( Info, i),
NameLength, Name,
SerializedValueLength, SerializedValue );
//
// The lengths don't match. Since we may be using the name
// in-place we have to add before deleting. Also, the property
// Id needs to be accessed before deleting.
//
} else {
Offset =
AddValueToHeap( Context, PROPERTY_INFO_ID( Info, i),
Length,
NameLength, Name,
SerializedValueLength, SerializedValue );
ChangeTable( Context, PROPERTY_INFO_ID( Info, i), Offset );
DeleteFromHeap( Context, HeapEntry );
//
// update header
//
Info->Entries[i].Heap =
GET_HEAP_ENTRY( Context->HeapHeader, Offset );
}
//
// We are adding a new property.
//
} else {
//
// If the property was named by an Id, then there is no name to
// create
//
if (InSpecs->Specifiers[i].Variant == PRSPEC_PROPID) {
NameLength = 0;
Name = NULL;
//
// Otherwise, use the specified name and generate an Id
//
} else {
NameLength = PROPERTY_SPECIFIER_NAME_LENGTH( InSpecs, i );
Name = PROPERTY_SPECIFIER_NAME( InSpecs, i );
}
//
// Add the new value to the heap and table
//
Length = PROPERTY_HEAP_ENTRY_SIZE( NameLength,
SerializedValueLength );
Offset =
AddValueToHeap( Context, PROPERTY_INFO_ID( Info, i ), Length,
NameLength, Name,
SerializedValueLength, SerializedValue );
ChangeTable( Context, PROPERTY_INFO_ID( Info, i ), Offset );
//
// Set new header value
//
Info->Entries[i].Heap = GET_HEAP_ENTRY( Context->HeapHeader, Offset );
}
}
// PROPASSERT( PtrOffset( OutBuffer, NextOutput ) == TotalLength );
} else if (Control->Op == PWC_DELETE_PROP) {
PPROPERTY_SPECIFICATIONS InSpecs;
//
// The input buffer is:
// PROPERTY_WRITE_CONTROL
// PROPERTY_SPECIFICATIONS
//
// The output buffer will be NULL
//
//
// Build value headers array from specifiers. Calculate size of
// property Ids and indirect output.
//
InSpecs = (PPROPERTY_SPECIFICATIONS) (Control + 1);
if (InBufferEnd < (PVOID)(InSpecs + 1) ||
InBufferEnd < Add2Ptr( InSpecs, PROPERTY_SPECIFICATIONS_SIZE( InSpecs->Count ))) {
ExRaiseStatus( STATUS_INVALID_USER_BUFFER );
}
Info = BuildPropertyInfoFromPropSpec( Context,
InSpecs,
InBufferEnd,
PID_ILLEGAL );
//
// No output is necessary
//
TotalLength = 0;
//
// Walk through the headers array and delete the values
//
for (i = 0; i < Info->Count; i++) {
PPROPERTY_HEAP_ENTRY HeapEntry = PROPERTY_INFO_HEAP_ENTRY( Info, i );
//
// If we found a heap entry then delete it from the heap and from
// the IdTable
//
if (HeapEntry != EMPTY_PROPERTY) {
ChangeTable( Context, PROPERTY_INFO_ID( Info, i), 0 );
DeleteFromHeap( Context, HeapEntry );
//
// update header
//
Info->Entries[i].Heap = EMPTY_PROPERTY;
}
}
// PROPASSERT( PtrOffset( OutBuffer, NextOutput ) == TotalLength );
} else if (Control->Op == PWC_WRITE_NAME) {
PPROPERTY_IDS Ids;
PPROPERTY_NAMES Names;
//
// The input buffer is:
// PROPERTY_WRITE_CONTROL
// PROPERTY_IDS
// PROPERTY_NAMES
//
// The output buffer will be NULL
//
//
// Build value headers array from Ids.
//
Ids = (PPROPERTY_IDS) (Control + 1);
if (InBufferEnd < (PVOID)(Ids + 1) ||
InBufferEnd < Add2Ptr( Ids, PROPERTY_IDS_SIZE( Ids->Count ))) {
ExRaiseStatus( STATUS_INVALID_USER_BUFFER );
}
Names = (PPROPERTY_NAMES) Add2Ptr( Ids, PROPERTY_IDS_SIZE( Ids->Count ));
if (InBufferEnd < (PVOID)(Names + 1) ||
InBufferEnd < Add2Ptr( Names, Names->Length ) ||
Ids->Count != Names->Count) {
ExRaiseStatus( STATUS_INVALID_USER_BUFFER );
}
Info = BuildPropertyInfoFromIds( Context, Ids );
//
// No output is necessary
//
TotalLength = 0;
//
// Walk through the headers array and set the new names, saving away
// the new offsets
//
for (i = 0; i < Info->Count; i++) {
ULONG SerializedValueLength;
SERIALIZEDPROPERTYVALUE *SerializedValue;
USHORT NameLength;
PWCHAR Name;
ULONG Length;
PPROPERTY_HEAP_ENTRY HeapEntry =
PROPERTY_INFO_HEAP_ENTRY( Info, i );
//
// The old values are found in the heap entry itself
//
SerializedValueLength =
PROPERTY_HEAP_ENTRY_VALUE_LENGTH( HeapEntry );
SerializedValue = PROPERTY_HEAP_ENTRY_VALUE( HeapEntry );
//
// The new name is found in the input
//
NameLength = (USHORT) PROPERTY_NAME_LENGTH( Names, i );
Name = PROPERTY_NAME( Names, i );
//
// Get new length of heap entry
//
Length = PROPERTY_HEAP_ENTRY_SIZE( NameLength,
SerializedValueLength );
//
// BUGBUG - handle duplicate sets
//
//
// If the new length matches that of the original header, then
// we can adjust set the values in place. We do this only if the
// property is not the EMPTY_PROPERTY (i.e., no one has set
// a value). If someone does specify a property that does not yet
// exist, one is created with the empty value.
//
if (HeapEntry != EMPTY_PROPERTY &&
Length == HeapEntry->PropertyValueLength) {
SetValueInHeap( Context, HeapEntry,
PROPERTY_INFO_ID( Info, i),
NameLength, Name,
SerializedValueLength, SerializedValue );
//
// The lengths don't match. Since we may be using the name
// in-place we have to add before deleting.
//
} else {
Offset =
AddValueToHeap( Context, PROPERTY_INFO_ID( Info, i),
Length,
NameLength, Name,
SerializedValueLength, SerializedValue );
ChangeTable( Context, PROPERTY_INFO_ID( Info, i), Offset );
if (HeapEntry != EMPTY_PROPERTY) {
DeleteFromHeap( Context, HeapEntry );
}
//
// update header
//
Info->Entries[i].Heap =
GET_HEAP_ENTRY( Context->HeapHeader, Offset );
}
}
// PROPASSERT( PtrOffset( OutBuffer, NextOutput ) == TotalLength );
} else if (Control->Op == PWC_DELETE_NAME) {
PPROPERTY_IDS Ids;
//
// The input buffer is:
// PROPERTY_WRITE_CONTROL
// PROPERTY_IDS
//
// The output buffer will be NULL
//
//
// Build value headers array from Ids.
//
Ids = (PPROPERTY_IDS) (Control + 1);
if (InBufferEnd < (PVOID)(Ids + 1) ||
InBufferEnd < Add2Ptr( Ids, PROPERTY_IDS_SIZE( Ids->Count ))) {
ExRaiseStatus( STATUS_INVALID_USER_BUFFER );
}
Info = BuildPropertyInfoFromIds( Context, Ids );
//
// No output is necessary
//
TotalLength = 0;
//
// Walk through the headers array and delete the names
//
for (i = 0; i < Info->Count; i++) {
ULONG SerializedValueLength;
SERIALIZEDPROPERTYVALUE *SerializedValue;
ULONG Length;
PPROPERTY_HEAP_ENTRY HeapEntry = PROPERTY_INFO_HEAP_ENTRY( Info, i );
//
// The old values are found in the heap entry itself
//
SerializedValueLength = PROPERTY_HEAP_ENTRY_VALUE_LENGTH( HeapEntry );
SerializedValue = PROPERTY_HEAP_ENTRY_VALUE( HeapEntry );
//
// Get new length of heap entry
//
Length = PROPERTY_HEAP_ENTRY_SIZE( 0, SerializedValueLength );
//
// If the new length matches that of the original header, then
// we are deleting a name that isn't there. This is a NOP.
//
if (Length == HeapEntry->PropertyValueLength) {
NOTHING;
//
// The lengths don't match.
//
} else {
Offset =
AddValueToHeap( Context, PROPERTY_INFO_ID( Info, i),
Length,
0, NULL,
SerializedValueLength, SerializedValue );
ChangeTable( Context, PROPERTY_INFO_ID( Info, i), Offset );
if (HeapEntry != EMPTY_PROPERTY) {
DeleteFromHeap( Context, HeapEntry );
}
//
// update header
//
Info->Entries[i].Heap =
GET_HEAP_ENTRY( Context->HeapHeader, Offset );
}
}
// PROPASSERT( PtrOffset( OutBuffer, NextOutput ) == TotalLength );
} else if (Control->Op == PWC_WRITE_ALL) {
PPROPERTY_IDS Ids;
PPROPERTY_NAMES Names;
PPROPERTY_VALUES Values;
//
// The input buffer is:
// PROPERTY_WRITE_CONTROL
// PROPERTY_IDS
// PROPERTY_NAMES
// PROPERTY_VALUES
//
// The output buffer will be NULL
//
//
// Get and validate pointers to data blocks. We still need to validate
// pointers and offsets as we read the data out.
//
Ids = (PPROPERTY_IDS) (Control + 1);
if (InBufferEnd < (PVOID)(Ids + 1) ||
InBufferEnd < Add2Ptr( Ids, PROPERTY_IDS_SIZE( Ids->Count ))) {
ExRaiseStatus( STATUS_INVALID_USER_BUFFER );
}
Names = (PPROPERTY_NAMES) Add2Ptr( Ids, PROPERTY_IDS_SIZE( Ids->Count ));
if (InBufferEnd < (PVOID)(Names + 1) ||
InBufferEnd < Add2Ptr( Names, Names->Length ) ||
Ids->Count != Names->Count) {
ExRaiseStatus( STATUS_INVALID_USER_BUFFER );
}
Values = (PPROPERTY_VALUES) LongAlign( Add2Ptr( Names, Names->Length ));
if (InBufferEnd < (PVOID)(Values + 1) ||
InBufferEnd < Add2Ptr( Values, Values->Length ) ||
Ids->Count != Values->Count) {
ExRaiseStatus( STATUS_INVALID_USER_BUFFER );
}
//
// Initialize the property set
//
InitializePropertyData( Context );
SetPropertyContextPointersFromMap( Context );
for (i = 0; i < Ids->Count; i++) {
ULONG SerializedValueLength;
SERIALIZEDPROPERTYVALUE *SerializedValue;
USHORT NameLength;
PWCHAR Name;
ULONG Length;
//
// The values are found in the PROPERTY_VALUES input
//
SerializedValueLength = PROPERTY_VALUE_LENGTH( Values, i );
SerializedValue = PROPERTY_VALUE( Values, i );
//
// The name is found in the PROPERTY_NAMES input
//
NameLength = (USHORT) PROPERTY_NAME_LENGTH( Names, i);
Name = PROPERTY_NAME( Names, i);
Length = PROPERTY_HEAP_ENTRY_SIZE( NameLength,
SerializedValueLength );
//
// BUGBUG - handle duplicate sets
//
//
// add id name and value
//
Offset =
AddValueToHeap( Context, PROPERTY_ID( Ids, i ), Length,
NameLength, Name,
SerializedValueLength, SerializedValue );
ChangeTable( Context, PROPERTY_ID( Ids, i ), Offset );
}
//
// No output
//
TotalLength = 0;
} else {
ExRaiseStatus( STATUS_INVALID_USER_BUFFER );
}
*OutBufferLength = TotalLength;
} finally {
if (Info != NULL) {
NtfsFreePool( Info );
}
}
}
|