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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
|
/*++
Copyright (c) 2008 - 2009 Microsoft Corporation
Module Name:
ncfileinfo.c
Abstract:
Contains routines to process user-initiated query file and set file
information requests.
Environment:
Kernel mode
--*/
#include "nc.h"
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGE, NcPreQueryAlternateName)
#pragma alloc_text(PAGE, NcPostQueryHardLinks)
#pragma alloc_text(PAGE, NcPostQueryName)
#pragma alloc_text(PAGE, NcPreSetDisposition)
#pragma alloc_text(PAGE, NcPreSetLinkInformation)
#pragma alloc_text(PAGE, NcPreSetShortName)
#pragma alloc_text(PAGE, NcPreRename)
#endif
FLT_POSTOP_CALLBACK_STATUS
NcPostQueryName (
_Inout_ PFLT_CALLBACK_DATA Data,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_opt_ PVOID CompletionContext,
_In_ FLT_POST_OPERATION_FLAGS Flags
)
/*++
Routine Description:
This routine is called when the user wants to request a name for a
previously opened handle. Since we munged the name to be to the real
mapping in pre-create, we must munge it back to the user visible view
in response to name requests, even by opened name.
Note that this function processes three information classes:
FileNameInformation
FileNormalizedNameInformation
FileAllInformation
Arguments:
Data - Pointer to the filter CallbackData that is passed to us.
FltObjects - Pointer to the FLT_RELATED_OBJECTS data structure
containing opaque handles to this filter, instance, its
associated volume and file object.
CompletionContext - The context for the completion routine for
this operation. We set this to the handle context for
directory change notifications.
Return Value:
The return value is the Status of the operation.
--*/
{
NTSTATUS Status = STATUS_SUCCESS;
NC_PATH_OVERLAP Overlap;
PNC_INSTANCE_CONTEXT InstanceContext = NULL;
FILE_INFORMATION_CLASS InfoClass = Data->Iopb->Parameters.QueryFileInformation.FileInformationClass;
PVOID UserBuffer = Data->Iopb->Parameters.QueryFileInformation.InfoBuffer;
ULONG UserBufferLength = Data->Iopb->Parameters.QueryFileInformation.Length;
ULONG SizeActuallyReturned = (ULONG)Data->IoStatus.Information;
ULONG LengthNeeded = 0;
ULONG UserStructureSize = 0;
ULONG RequiredNameSize = 0;
ULONG NameLengthAvailable = 0;
PFILE_NAME_INFORMATION NameInfo;
BOOLEAN IgnoreCase = !BooleanFlagOn( FltObjects->FileObject->Flags,
FO_OPENED_CASE_SENSITIVE );
UNICODE_STRING Remainder;
UNICODE_STRING RemainderCopy = EMPTY_UNICODE_STRING;
UNICODE_STRING ReturnedName;
UNREFERENCED_PARAMETER( CompletionContext );
UNREFERENCED_PARAMETER( Flags );
PAGED_CODE();
FLT_ASSERT( IoGetTopLevelIrp() == NULL );
//
// If the operation failed already, we have no processing to do unless the
// failure is a buffer overflow.
//
if (!NT_SUCCESS( Data->IoStatus.Status ) &&
(Data->IoStatus.Status != STATUS_BUFFER_OVERFLOW)) {
Status = Data->IoStatus.Status;
goto NcPostQueryNameInformationCleanup;
}
//
// Get our instance context.
//
Status = FltGetInstanceContext( FltObjects->Instance,
&InstanceContext);
if (!NT_SUCCESS( Status )) {
goto NcPostQueryNameInformationCleanup;
}
//
// Find the name in the user buffer.
//
if (InfoClass == FileAllInformation) {
NameInfo = & ((PFILE_ALL_INFORMATION) UserBuffer)->NameInformation;
} else {
FLT_ASSERT( InfoClass == FileNameInformation ||
InfoClass == FileNormalizedNameInformation );
NameInfo = UserBuffer;
}
//
// If the name is too long for a UNICODE_STRING, we can't process it.
// This should never really happen, since UNICODE_STRINGs are used
// all across the NT IO model.
//
if (NameInfo->FileNameLength >= MAXUSHORT) {
Status = STATUS_OBJECT_PATH_INVALID;
goto NcPostQueryNameInformationCleanup;
}
//
// Now that we have an instance context and NameInfo buffer, see if the file
// system failed with a buffer overflow.
//
if (Data->IoStatus.Status == STATUS_BUFFER_OVERFLOW) {
//
// We need to bias the FileNameLength field by the difference between
// the real and user mapping lengths if the user mapping is longer. This
// is so that if the caller re-issues the name query we won't fail with
// a buffer overflow in the filter even if the file system succeeded.
//
if (InstanceContext->Mapping.UserMapping.LongNamePath.VolumelessName.Length >
InstanceContext->Mapping.RealMapping.LongNamePath.VolumelessName.Length) {
NameInfo->FileNameLength += InstanceContext->Mapping.UserMapping.LongNamePath.VolumelessName.Length -
InstanceContext->Mapping.RealMapping.LongNamePath.VolumelessName.Length;
}
Status = Data->IoStatus.Status;
LengthNeeded = SizeActuallyReturned;
goto NcPostQueryNameInformationCleanup;
}
ReturnedName.Buffer = NameInfo->FileName;
ReturnedName.MaximumLength =
ReturnedName.Length = (USHORT)NameInfo->FileNameLength;
//
// Check if the name being returned is within the real mapping.
// If not, we have no translation to perform.
//
NcComparePath( &ReturnedName,
&InstanceContext->Mapping.RealMapping,
&Remainder,
IgnoreCase,
FALSE,
&Overlap );
if (!Overlap.InMapping && !Overlap.Match) {
Status = Data->IoStatus.Status;
LengthNeeded = SizeActuallyReturned;
goto NcPostQueryNameInformationCleanup;
}
//
// Make sure that the user buffer is long enough.
//
UserStructureSize = FIELD_OFFSET( FILE_NAME_INFORMATION, FileName );
if (InfoClass == FileAllInformation) {
UserStructureSize += FIELD_OFFSET( FILE_ALL_INFORMATION, NameInformation );
}
RequiredNameSize = InstanceContext->Mapping.UserMapping.LongNamePath.VolumelessName.Length;
//
// Add back the trailing portion of the name. Note that Remainder
// is only defined if InMapping is TRUE.
//
if (Overlap.InMapping && !Overlap.Match) {
RequiredNameSize += Remainder.Length + sizeof(WCHAR);
}
LengthNeeded = UserStructureSize + RequiredNameSize;
//
// Whether the user has provided enough buffer or not, the
// FILE_NAME_INFORMATION.FileNameLength field has to contain the total length
// of the name we want to return.
//
NameInfo->FileNameLength = RequiredNameSize;
//
// If the user's buffer is not big enough to handle the name we need to return,
// we will copy in as much as we can and return STATUS_BUFFER_OVERFLOW. The
// user expects that the needed name length will be reported in the
// FILE_NAME_INFORMATION.FileNameLength field.
//
if (UserBufferLength < LengthNeeded) {
NameLengthAvailable = UserBufferLength - UserStructureSize;
//
// Truncate the LengthNeeded value since it will be returned in the
// IoStatus block to tell I/O Manager how much to copy back to the
// user's buffer.
//
LengthNeeded = UserBufferLength;
Status = STATUS_BUFFER_OVERFLOW;
//
// We have enough space. Let's assume we'll succeed to copy the name.
//
} else {
NameLengthAvailable = NameInfo->FileNameLength;
Status = STATUS_SUCCESS;
}
if (Overlap.InMapping && !Overlap.Match) {
//
// Copy the remainder of the returned name from the user. This is
// done so that we can rewrite the user's buffer. Note that if we
// are not a match, we expect some remainder.
//
FLT_ASSERT( Remainder.Length > 0 );
RemainderCopy.Buffer = ExAllocatePoolZero( PagedPool,
Remainder.Length,
NC_TAG );
if (RemainderCopy.Buffer == NULL) {
Status = STATUS_INSUFFICIENT_RESOURCES;
goto NcPostQueryNameInformationCleanup;
}
RtlCopyMemory( RemainderCopy.Buffer,
Remainder.Buffer,
Remainder.Length );
RemainderCopy.MaximumLength =
RemainderCopy.Length = Remainder.Length;
}
//
// Firstly, copy back the name to our mapping.
//
RtlCopyMemory( &NameInfo->FileName,
InstanceContext->Mapping.UserMapping.LongNamePath.VolumelessName.Buffer,
min(InstanceContext->Mapping.UserMapping.LongNamePath.VolumelessName.Length,
NameLengthAvailable) );
if (NameLengthAvailable > InstanceContext->Mapping.UserMapping.LongNamePath.VolumelessName.Length) {
NameLengthAvailable -= InstanceContext->Mapping.UserMapping.LongNamePath.VolumelessName.Length;
} else {
NameLengthAvailable = 0;
}
//
// If the object being queried is within the mapping, copy back the
// remainder of that name.
//
if ((NameLengthAvailable > 0) &&
Overlap.InMapping && !Overlap.Match) {
NameInfo->FileName[InstanceContext->Mapping.UserMapping.LongNamePath.VolumelessName.Length / sizeof(WCHAR)] = '\\';
RtlCopyMemory( Add2Ptr( &NameInfo->FileName,
InstanceContext->Mapping.UserMapping.LongNamePath.VolumelessName.Length + sizeof(WCHAR) ),
RemainderCopy.Buffer,
min(RemainderCopy.Length, NameLengthAvailable) );
}
//
// We have finished the query, complete operation.
//
NcPostQueryNameInformationCleanup:
Data->IoStatus.Status = Status;
//
// Note that STATUS_BUFFER_OVERFLOW is not a success code, but for name queries
// it indicates that the caller needs to allocate a bigger buffer. The needed
// size for the name is stored in the FILE_NAME_INFORMATION.FileNameLength field.
// Therefore the IoStatus.Information field must not be 0 for a buffer overflow,
// it must contain the size of the data that was actually copied.
//
if (NT_SUCCESS( Status ) ||
(Status == STATUS_BUFFER_OVERFLOW)) {
Data->IoStatus.Information = LengthNeeded;
} else {
Data->IoStatus.Information = 0;
}
if (RemainderCopy.Buffer != NULL) {
ExFreePoolWithTag( RemainderCopy.Buffer, NC_TAG );
}
if (InstanceContext != NULL) {
FltReleaseContext( InstanceContext );
}
return FLT_POSTOP_FINISHED_PROCESSING;
}
FLT_PREOP_CALLBACK_STATUS
NcPreQueryAlternateName (
_Inout_ PFLT_CALLBACK_DATA Data,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Flt_CompletionContext_Outptr_ PVOID *CompletionContext
)
/*++
Routine Description:
This routine is called when the user wants to find the alternate name
for a previously opened handle. An alternate name means the short
half of a long/short name pair.
Arguments:
Data - Pointer to the filter CallbackData that is passed to us.
FltObjects - Pointer to the FLT_RELATED_OBJECTS data structure
containing opaque handles to this filter, instance, its
associated volume and file object.
CompletionContext - The context for the completion routine for
this operation. We set this to the handle context for
directory change notifications.
Return Value:
The return value is the Status of the operation.
--*/
{
NTSTATUS Status;
FLT_PREOP_CALLBACK_STATUS ReturnValue;
NC_PATH_OVERLAP Overlap;
PFLT_FILE_NAME_INFORMATION FileInfo = NULL;
PNC_INSTANCE_CONTEXT InstanceContext = NULL;
PVOID UserBuffer = Data->Iopb->Parameters.QueryFileInformation.InfoBuffer;
ULONG UserBufferLength = Data->Iopb->Parameters.QueryFileInformation.Length;
ULONG LengthNeeded = 0;
PFILE_NAME_INFORMATION NameInfo;
BOOLEAN IgnoreCase = !BooleanFlagOn( FltObjects->FileObject->Flags,
FO_OPENED_CASE_SENSITIVE );
PUNICODE_STRING FinalComponentToReturn;
UNREFERENCED_PARAMETER( CompletionContext );
PAGED_CODE();
FLT_ASSERT( IoGetTopLevelIrp() == NULL );
//
// Get our instance context.
//
Status = FltGetInstanceContext( FltObjects->Instance,
&InstanceContext);
if (!NT_SUCCESS( Status )) {
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreQueryAlternateNameInformationCleanup;
}
//
// Get the file's name.
//
Status = NcGetFileNameInformation( Data,
NULL,
NULL,
FLT_FILE_NAME_OPENED |
FLT_FILE_NAME_QUERY_DEFAULT |
FLT_FILE_NAME_REQUEST_FROM_CURRENT_PROVIDER,
&FileInfo );
if (!NT_SUCCESS( Status )) {
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreQueryAlternateNameInformationCleanup;
}
Status = FltParseFileNameInformation( FileInfo );
if (!NT_SUCCESS( Status )) {
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreQueryAlternateNameInformationCleanup;
}
//
// We only need to handle the case where a shortname is being
// generated on the mapping itself. These names are final
// component path only, so any files within the mapping will
// still be correct even if we don't munge them.
//
NcComparePath( &FileInfo->Name,
&InstanceContext->Mapping.UserMapping,
NULL,
IgnoreCase,
TRUE,
&Overlap );
if (!Overlap.Match) {
NcComparePath( &FileInfo->Name,
&InstanceContext->Mapping.RealMapping,
NULL,
IgnoreCase,
TRUE,
&Overlap );
FLT_ASSERT( !Overlap.Match );
if (!Overlap.Match) {
//
// This file is not the mapping, so we can just let this
// request go down normally.
//
ReturnValue = FLT_PREOP_SUCCESS_NO_CALLBACK;
goto NcPreQueryAlternateNameInformationCleanup;
}
}
//
// Return the short name.
//
// Note that this behavior differs from the filesystem in two respects:
//
// 1. We are not attempting to detect (and fail for) an open-by-ID
// handle. Since these are link agnostic, returning data is
// meaningless.
//
// 2. We may support having multiple alternate names for multiple
// links on the file, if the mapping was created as a file then
// a hardlink was created with a mapping name. This file thus
// contains two shortnames, which NTFS does not support. In
// theory APIs should be clean to this (and a future filesystem
// may wish to support it.)
//
FinalComponentToReturn = &InstanceContext->Mapping.UserMapping.ShortNamePath.FinalComponentName;
//
// Make sure that the user buffer is long enough.
//
LengthNeeded = FIELD_OFFSET( FILE_NAME_INFORMATION, FileName );
LengthNeeded += FinalComponentToReturn->Length;
if (UserBufferLength < LengthNeeded) {
Status = STATUS_BUFFER_OVERFLOW;
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreQueryAlternateNameInformationCleanup;
}
NameInfo = UserBuffer;
//
// Copy back the name to our mapping.
//
RtlCopyMemory( &NameInfo->FileName,
FinalComponentToReturn->Buffer,
FinalComponentToReturn->Length );
NameInfo->FileNameLength = FinalComponentToReturn->Length;
//
// We have finished the query, complete operation.
//
Status = STATUS_SUCCESS;
ReturnValue = FLT_PREOP_COMPLETE;
NcPreQueryAlternateNameInformationCleanup:
if (ReturnValue == FLT_PREOP_COMPLETE) {
Data->IoStatus.Status = Status;
//
// Note that STATUS_BUFFER_OVERFLOW is not a success code, and
// will result in zero bytes being copied back to the caller.
//
if (NT_SUCCESS( Status )) {
Data->IoStatus.Information = LengthNeeded;
} else {
Data->IoStatus.Information = 0;
}
}
if (FileInfo != NULL) {
FltReleaseFileNameInformation( FileInfo );
}
if (InstanceContext != NULL) {
FltReleaseContext( InstanceContext );
}
return ReturnValue;
}
FLT_POSTOP_CALLBACK_STATUS
NcPostQueryHardLinks (
_Inout_ PFLT_CALLBACK_DATA Data,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_opt_ PVOID CompletionContext,
_In_ FLT_POST_OPERATION_FLAGS Flags
)
/*++
Routine Description:
This routine is called when the user wants to enumerate all hard links
for a previously opened handle.
Arguments:
Data - Pointer to the filter CallbackData that is passed to us.
FltObjects - Pointer to the FLT_RELATED_OBJECTS data structure
containing opaque handles to this filter, instance, its
associated volume and file object.
CompletionContext - The context for the completion routine for
this operation. We set this to the handle context for
directory change notifications.
Return Value:
The return value is the Status of the operation.
--*/
{
NTSTATUS Status;
PNC_INSTANCE_CONTEXT InstanceContext = NULL;
BOOLEAN IgnoreCase = !BooleanFlagOn( FltObjects->FileObject->Flags,
FO_OPENED_CASE_SENSITIVE );
//
// Pointer to the buffer returned to us, which we will also return to
// our caller; length of the buffer; size of the buffer filled in
// by the filesystem; size of the buffer that we filled in
//
PFILE_LINKS_INFORMATION UserBuffer = Data->Iopb->Parameters.QueryFileInformation.InfoBuffer;
ULONG UserBufferLength = Data->Iopb->Parameters.QueryFileInformation.Length;
ULONG SizeActuallyReturned = (ULONG)Data->IoStatus.Information;
ULONG BytesWritten = 0;
//
// Length of the current entry that we're processing; a UNICODE_STRING
// for the final component of the name that we're processing; and
// a flag indicating whether this entry is being modified
//
ULONG EntrySize;
UNICODE_STRING EntryName;
BOOLEAN MungeEntry;
//
// Copy of the buffer returned from the filesystem, our iterators as
// we process this buffer, and a pointer to the previous destination
// entry (if one exists) so we can zero the offset to next entry
// field on completion
//
PFILE_LINKS_INFORMATION OriginalBuffer = NULL;
PFILE_LINK_ENTRY_INFORMATION SourceEntry;
PFILE_LINK_ENTRY_INFORMATION DestEntry;
PFILE_LINK_ENTRY_INFORMATION PrevDestEntry = NULL;
//
// Variables that we use to obtain IDs to the mapping parents.
//
OBJECT_ATTRIBUTES MappingParentAttributes;
HANDLE MappingParentHandle = NULL;
PFILE_OBJECT MappingParentFileObject = NULL;
IO_STATUS_BLOCK MappingParentStatusBlock;
//
// File IDs for the mapping parents
//
LONGLONG RealMappingParentId;
LONGLONG UserMappingParentId;
UNREFERENCED_PARAMETER( CompletionContext );
UNREFERENCED_PARAMETER( Flags );
PAGED_CODE();
FLT_ASSERT( IoGetTopLevelIrp() == NULL );
//
// If the buffer was invalid, or if the call failed, leave now. Note
// that this may be STATUS_BUFFER_OVERFLOW and the number of bytes the
// caller needs will be reported inaccurately. To handle this we'd
// really need to issue our own call to get the full buffer, then
// transform it to find the "correct" length the user will need.
//
// Rather than do this, we return the caller a value for bytes required
// which may not be accurate. When they call us again, we will
// have data to transform, and can then fail the call again specifying
// a new value for bytes required that is accurate.
//
if (SizeActuallyReturned <= (ULONG)FIELD_OFFSET( FILE_LINKS_INFORMATION, Entry ) ||
!NT_SUCCESS( Data->IoStatus.Status )) {
BytesWritten = SizeActuallyReturned;
Status = Data->IoStatus.Status;
goto NcPostQueryHardLinkInformationCleanup;
}
//
// Get our instance context.
//
Status = FltGetInstanceContext( FltObjects->Instance,
&InstanceContext);
if (!NT_SUCCESS( Status )) {
goto NcPostQueryHardLinkInformationCleanup;
}
//
// Open the mapping parents and query IDs.
//
InitializeObjectAttributes( &MappingParentAttributes,
&InstanceContext->Mapping.RealMapping.LongNamePath.ParentPath,
OBJ_KERNEL_HANDLE | (IgnoreCase?OBJ_CASE_INSENSITIVE:0),
NULL,
NULL);
Status = NcCreateFileHelper( NcGlobalData.FilterHandle, // Filter
Data->Iopb->TargetInstance, // Instance
&MappingParentHandle, // Returned Handle
&MappingParentFileObject, // Returned FileObject
FILE_READ_ATTRIBUTES|FILE_TRAVERSE, // Desired Access
&MappingParentAttributes, // object attributes
&MappingParentStatusBlock, // Returned IOStatusBlock
0, // Allocation Size
FILE_ATTRIBUTE_NORMAL, // File Attributes
0, // Share Access
FILE_OPEN, // Create Disposition
FILE_DIRECTORY_FILE, // Create Options
NULL, // Ea Buffer
0, // EA Length
IO_IGNORE_SHARE_ACCESS_CHECK, // Flags
Data->Iopb->TargetFileObject ); // Transaction info.
if (!NT_SUCCESS( Status )) {
FLT_ASSERT( Status != STATUS_OBJECT_PATH_NOT_FOUND &&
Status != STATUS_OBJECT_NAME_NOT_FOUND );
goto NcPostQueryHardLinkInformationCleanup;
}
Status = FltQueryInformationFile( Data->Iopb->TargetInstance,
MappingParentFileObject,
&RealMappingParentId,
sizeof(RealMappingParentId),
FileInternalInformation,
NULL );
if (!NT_SUCCESS( Status )) {
goto NcPostQueryHardLinkInformationCleanup;
}
FltClose( MappingParentHandle );
ObDereferenceObject( MappingParentFileObject );
MappingParentHandle = NULL;
MappingParentFileObject = NULL;
InitializeObjectAttributes( &MappingParentAttributes,
&InstanceContext->Mapping.UserMapping.LongNamePath.ParentPath,
OBJ_KERNEL_HANDLE | (IgnoreCase?OBJ_CASE_INSENSITIVE:0),
NULL,
NULL);
Status = NcCreateFileHelper( NcGlobalData.FilterHandle, // Filter
Data->Iopb->TargetInstance, // Instance
&MappingParentHandle, // Returned Handle
&MappingParentFileObject, // Returned FileObject
FILE_READ_ATTRIBUTES|FILE_TRAVERSE, // Desired Access
&MappingParentAttributes, // object attributes
&MappingParentStatusBlock, // Returned IOStatusBlock
0, // Allocation Size
FILE_ATTRIBUTE_NORMAL, // File Attributes
0, // Share Access
FILE_OPEN, // Create Disposition
FILE_DIRECTORY_FILE, // Create Options
NULL, // Ea Buffer
0, // EA Length
IO_IGNORE_SHARE_ACCESS_CHECK, // Flags
Data->Iopb->TargetFileObject ); // Transaction info.
if (!NT_SUCCESS( Status )) {
FLT_ASSERT( Status != STATUS_OBJECT_PATH_NOT_FOUND &&
Status != STATUS_OBJECT_NAME_NOT_FOUND );
goto NcPostQueryHardLinkInformationCleanup;
}
Status = FltQueryInformationFile( Data->Iopb->TargetInstance,
MappingParentFileObject,
&UserMappingParentId,
sizeof(UserMappingParentId),
FileInternalInformation,
NULL );
if (!NT_SUCCESS( Status )) {
goto NcPostQueryHardLinkInformationCleanup;
}
FltClose( MappingParentHandle );
ObDereferenceObject( MappingParentFileObject );
MappingParentHandle = NULL;
MappingParentFileObject = NULL;
//
// Take a copy of the results of the call from the filesystem.
//
OriginalBuffer = ExAllocatePoolZero( PagedPool,
SizeActuallyReturned,
NC_TAG );
if (OriginalBuffer == NULL) {
Status = STATUS_INSUFFICIENT_RESOURCES;
goto NcPostQueryHardLinkInformationCleanup;
}
RtlCopyMemory( OriginalBuffer, UserBuffer, SizeActuallyReturned );
//
// Set up our iterators to walk through the returned links.
//
DestEntry = &UserBuffer->Entry;
if (UserBuffer->EntriesReturned >= 1) {
SourceEntry = &OriginalBuffer->Entry;
} else {
SourceEntry = NULL;
}
UserBuffer->EntriesReturned = 0;
BytesWritten = FIELD_OFFSET( FILE_LINKS_INFORMATION, Entry );
UserBuffer->BytesNeeded = BytesWritten;
while( SourceEntry ) {
//
// Assume we don't need to munge the link. If the parent IDs,
// final component lengths and final component strings correspond
// to the real mapping, we will need to transform it.
//
MungeEntry = FALSE;
if (SourceEntry->ParentFileId == RealMappingParentId) {
if (SourceEntry->FileNameLength * sizeof(WCHAR) >= MAXUSHORT) {
Status = STATUS_OBJECT_PATH_INVALID;
goto NcPostQueryHardLinkInformationCleanup;
}
EntryName.Buffer = SourceEntry->FileName;
EntryName.Length = EntryName.MaximumLength = (USHORT)SourceEntry->FileNameLength * sizeof(WCHAR);
if (EntryName.Length == InstanceContext->Mapping.RealMapping.LongNamePath.FinalComponentName.Length &&
RtlCompareUnicodeString( &EntryName, &InstanceContext->Mapping.RealMapping.LongNamePath.FinalComponentName, IgnoreCase ) == 0) {
MungeEntry = TRUE;
}
//
// TODO: Preserve shortness in output
//
if (EntryName.Length == InstanceContext->Mapping.RealMapping.ShortNamePath.FinalComponentName.Length &&
RtlCompareUnicodeString( &EntryName, &InstanceContext->Mapping.RealMapping.ShortNamePath.FinalComponentName, IgnoreCase ) == 0) {
MungeEntry = TRUE;
}
}
//
// Calculate the length of the entry that we want to write.
//
if (MungeEntry) {
EntrySize = FIELD_OFFSET( FILE_LINK_ENTRY_INFORMATION, FileName ) +
InstanceContext->Mapping.UserMapping.LongNamePath.FinalComponentName.Length;
EntrySize = AlignToSize( EntrySize, 8 );
} else {
EntrySize = FIELD_OFFSET( FILE_LINK_ENTRY_INFORMATION, FileName ) +
SourceEntry->FileNameLength * sizeof(WCHAR);
EntrySize = AlignToSize( EntrySize, 8 );
}
//
// Record how much space the caller would need to return all entries.
//
UserBuffer->BytesNeeded += EntrySize;
//
// If we have space, copy this entry into the user's buffer and
// advance our destination iterator.
//
if (BytesWritten + EntrySize <= UserBufferLength) {
if (MungeEntry) {
DestEntry->NextEntryOffset = EntrySize;
DestEntry->ParentFileId = UserMappingParentId;
DestEntry->FileNameLength = InstanceContext->Mapping.UserMapping.LongNamePath.FinalComponentName.Length / sizeof(WCHAR);
RtlCopyMemory( DestEntry->FileName,
InstanceContext->Mapping.UserMapping.LongNamePath.FinalComponentName.Buffer,
InstanceContext->Mapping.UserMapping.LongNamePath.FinalComponentName.Length );
} else {
RtlCopyMemory( DestEntry, SourceEntry, EntrySize );
}
PrevDestEntry = DestEntry;
DestEntry = Add2Ptr( DestEntry, EntrySize );
UserBuffer->EntriesReturned++;
BytesWritten += EntrySize;
}
//
// If we still have links that we have not yet consumed, move
// to those.
//
if (SourceEntry->NextEntryOffset != 0) {
SourceEntry = Add2Ptr( SourceEntry, SourceEntry->NextEntryOffset );
} else {
SourceEntry = NULL;
}
}
//
// If we already copied one or more links, make sure our list is
// correctly terminated.
//
if (PrevDestEntry != NULL) {
PrevDestEntry->NextEntryOffset = 0;
}
//
// If we copied all results, return STATUS_SUCCESS. If we saw entries
// that we did not copy, return STATUS_BUFFER_OVERFLOW.
//
if (BytesWritten == UserBuffer->BytesNeeded) {
Status = STATUS_SUCCESS;
} else {
Status = STATUS_BUFFER_OVERFLOW;
}
NcPostQueryHardLinkInformationCleanup:
Data->IoStatus.Status = Status;
if (NT_SUCCESS( Status ) || Status == STATUS_BUFFER_OVERFLOW) {
Data->IoStatus.Information = BytesWritten;
} else {
Data->IoStatus.Information = 0;
}
if (MappingParentHandle != NULL) {
FltClose( MappingParentHandle );
MappingParentHandle = NULL;
}
if (MappingParentFileObject != NULL) {
ObDereferenceObject( MappingParentFileObject );
MappingParentFileObject = NULL;
}
if (OriginalBuffer != NULL) {
ExFreePoolWithTag( OriginalBuffer, NC_TAG );
}
if (InstanceContext != NULL) {
FltReleaseContext( InstanceContext );
}
return FLT_POSTOP_FINISHED_PROCESSING;
}
FLT_PREOP_CALLBACK_STATUS
NcPreSetShortName (
_Inout_ PFLT_CALLBACK_DATA Data,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Flt_CompletionContext_Outptr_ PVOID *CompletionContext
)
/*++
Routine Description:
This routine is called when the user wants to change the short name
for a previously opened handle.
Arguments:
Data - Pointer to the filter CallbackData that is passed to us.
FltObjects - Pointer to the FLT_RELATED_OBJECTS data structure
containing opaque handles to this filter, instance, its
associated volume and file object.
CompletionContext - The context for the completion routine for
this operation. We set this to the handle context for
directory change notifications.
Return Value:
The return value is the Status of the operation.
--*/
{
FLT_PREOP_CALLBACK_STATUS ReturnValue;
NTSTATUS Status;
PFILE_NAME_INFORMATION NameInfo =
Data->Iopb->Parameters.SetFileInformation.InfoBuffer;
PFLT_FILE_NAME_INFORMATION FileInfo = NULL;
PNC_INSTANCE_CONTEXT InstanceContext = NULL;
NC_PATH_OVERLAP RealOverlap;
NC_PATH_OVERLAP UserOverlap;
BOOLEAN IgnoreCase = !BooleanFlagOn( FltObjects->FileObject->Flags,
FO_OPENED_CASE_SENSITIVE );
PAGED_CODE();
FLT_ASSERT( IoGetTopLevelIrp() == NULL );
UNREFERENCED_PARAMETER( CompletionContext );
Status = FltGetInstanceContext( FltObjects->Instance,
&InstanceContext);
if (!NT_SUCCESS( Status )) {
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreSetShortNameCleanup;
}
//
// Let's skip doing any of this work for file systems that we know don't
// have short names.
//
if ((InstanceContext->VolumeFilesystemType == FLT_FSTYPE_EXFAT) ||
(InstanceContext->VolumeFilesystemType == FLT_FSTYPE_REFS)) {
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreSetShortNameCleanup;
}
//
// Get the file's name.
//
Status = NcGetFileNameInformation( Data,
NULL,
NULL,
FLT_FILE_NAME_OPENED |
FLT_FILE_NAME_QUERY_DEFAULT |
FLT_FILE_NAME_REQUEST_FROM_CURRENT_PROVIDER,
&FileInfo );
if (!NT_SUCCESS( Status )) {
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreSetShortNameCleanup;
}
Status = FltParseFileNameInformation( FileInfo );
if (!NT_SUCCESS( Status )) {
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreSetShortNameCleanup;
}
//
// Calculate Overlap and Remainder
//
NcComparePath( &FileInfo->Name,
&InstanceContext->Mapping.UserMapping,
NULL,
IgnoreCase,
TRUE,
&UserOverlap );
NcComparePath( &FileInfo->Name,
&InstanceContext->Mapping.RealMapping,
NULL,
IgnoreCase,
TRUE,
&RealOverlap );
//
// Currently the file names that we use are read only, so changing a
// shortname on the mapping or any of its ancestors cannot be
// supported.
//
// TODO: Should we support this?
//
if (RealOverlap.Match ||
UserOverlap.Match ||
RealOverlap.Ancestor ||
UserOverlap.Ancestor ||
NameInfo->FileNameLength > MAXUSHORT) {
Status = STATUS_ACCESS_DENIED;
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreSetShortNameCleanup;
}
//
// If the user is attempting to set a name which is used by the real
// file, we let the request go to the file system (which will fail
// it.) For names used by the user mapping, we need to detect and
// fail those.
//
if (UserOverlap.Peer) {
UNICODE_STRING TargetComponent;
TargetComponent.Buffer = NameInfo->FileName;
TargetComponent.Length = TargetComponent.MaximumLength = (USHORT)NameInfo->FileNameLength;
if( RtlCompareUnicodeString( &TargetComponent,
&InstanceContext->Mapping.UserMapping.LongNamePath.FinalComponentName,
IgnoreCase) == 0 ||
RtlCompareUnicodeString( &TargetComponent,
&InstanceContext->Mapping.UserMapping.ShortNamePath.FinalComponentName,
IgnoreCase) == 0 ) {
Status = STATUS_ACCESS_DENIED;
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreSetShortNameCleanup;
}
}
//
// If the user is not setting a short name on our mapping, an ancestor
// of our mapping or targetting our mapping, let the request go to the
// file system.
//
Status = STATUS_SUCCESS;
ReturnValue = FLT_PREOP_SUCCESS_NO_CALLBACK;
NcPreSetShortNameCleanup:
if (ReturnValue == FLT_PREOP_COMPLETE) {
Data->IoStatus.Status = Status;
}
if (FileInfo != NULL) {
FltReleaseFileNameInformation( FileInfo );
}
if (InstanceContext != NULL) {
FltReleaseContext( InstanceContext );
}
return ReturnValue;
}
FLT_PREOP_CALLBACK_STATUS
NcPreSetDisposition (
_Inout_ PFLT_CALLBACK_DATA Data,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Flt_CompletionContext_Outptr_ PVOID *CompletionContext
)
/*++
Routine Description:
Fltmgr callback which manages setting the delete disposition on a file.
We must disallow setting the delete disposition on an ancestor of either
mapping because otherwise we would have to maintain the mapping's
short/long name pairings.
Arguments:
Data - Pointer to the filter CallbackData that is passed to us.
FltObjects - Pointer to the FLT_RELATED_OBJECTS data structure containing
opaque handles to this filter, instance, its associated volume and
file object.
CompletionContext - The context for the completion routine for this
operation.
Return Value:
The return value is the Status of the operation.
--*/
{
NTSTATUS Status;
FLT_PREOP_CALLBACK_STATUS ReturnValue;
PFLT_FILE_NAME_INFORMATION FileInfo = NULL;
PNC_INSTANCE_CONTEXT InstanceContext = NULL;
PNC_MAPPING Mapping;
NC_PATH_OVERLAP RealOverlap;
NC_PATH_OVERLAP UserOverlap;
FILE_INFORMATION_CLASS fileInformationClass;
BOOLEAN IsDeleteFile;
BOOLEAN IgnoreCase = !BooleanFlagOn( FltObjects->FileObject->Flags,
FO_OPENED_CASE_SENSITIVE );
PAGED_CODE();
UNREFERENCED_PARAMETER( CompletionContext );
FLT_ASSERT( IoGetTopLevelIrp() == NULL );
fileInformationClass = Data->Iopb->Parameters.SetFileInformation.FileInformationClass;
//
// See if they are setting the delete disposition to false.
// If they are we can passthrough. We don't care if people
// want to mark the mapping as "don't delete".
//
IsDeleteFile = (fileInformationClass == FileDispositionInformationEx) ?
BooleanFlagOn( ((PFILE_DISPOSITION_INFORMATION_EX)Data->Iopb->Parameters.SetFileInformation.InfoBuffer)->Flags, FILE_DISPOSITION_DELETE ) :
((PFILE_DISPOSITION_INFORMATION)Data->Iopb->Parameters.SetFileInformation.InfoBuffer)->DeleteFile;
if (IsDeleteFile == FALSE) {
Status = STATUS_SUCCESS;
goto NcPreSetDispositionCleanup;
}
//
// The user is trying to delete a file.
// We have to make sure that the file is not an ancestor of either mapping.
//
//
// Get the file's name.
//
Status = NcGetFileNameInformation( Data,
NULL,
NULL,
FLT_FILE_NAME_OPENED |
FLT_FILE_NAME_QUERY_DEFAULT |
FLT_FILE_NAME_REQUEST_FROM_CURRENT_PROVIDER,
&FileInfo);
if (!NT_SUCCESS( Status )) {
goto NcPreSetDispositionCleanup;
}
Status = FltParseFileNameInformation( FileInfo );
if (!NT_SUCCESS( Status )) {
goto NcPreSetDispositionCleanup;
}
//
// Get the mapping
//
Status = FltGetInstanceContext( FltObjects->Instance,
&InstanceContext );
if (!NT_SUCCESS( Status )) {
goto NcPreSetDispositionCleanup;
}
Mapping = &InstanceContext->Mapping;
//
// Check to see of this will delete an ancestor of the real mapping.
//
NcComparePath( &FileInfo->Name,
&Mapping->RealMapping,
NULL,
IgnoreCase,
TRUE,
&RealOverlap );
if (RealOverlap.Ancestor) {
//
// The file is an ancestor of the real mapping, so disallow setting
// disposition.
//
Status = STATUS_ACCESS_DENIED;
goto NcPreSetDispositionCleanup;
}
//
// Check the user mapping.
//
NcComparePath( &FileInfo->Name,
&Mapping->UserMapping,
NULL,
IgnoreCase,
TRUE,
&UserOverlap );
if (UserOverlap.Ancestor) {
//
// The file is an ancestor of the user mapping, so disallow setting
// disposition.
//
Status = STATUS_ACCESS_DENIED;
goto NcPreSetDispositionCleanup;
}
//
// The file is ok to mark for delete.
//
Status = STATUS_SUCCESS;
goto NcPreSetDispositionCleanup;
NcPreSetDispositionCleanup:
if (!NT_SUCCESS( Status )) {
ReturnValue = FLT_PREOP_COMPLETE;
Data->IoStatus.Status = Status;
} else {
ReturnValue = FLT_PREOP_SUCCESS_NO_CALLBACK;
}
if (FileInfo) {
FltReleaseFileNameInformation( FileInfo );
}
if (InstanceContext) {
FltReleaseContext( InstanceContext );
}
return ReturnValue;
}
FLT_PREOP_CALLBACK_STATUS
NcPreSetLinkInformation (
_Inout_ PFLT_CALLBACK_DATA Data,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Flt_CompletionContext_Outptr_ PVOID *CompletionContext
)
/*++
Routine Description:
Fltmgr callback which manages link creation on a file.
We need to make sure that new links down the user mapping
are redirected to the real mapping.
Arguments:
Data - Pointer to the filter CallbackData that is passed to us.
FltObjects - Pointer to the FLT_RELATED_OBJECTS data structure containing
opaque handles to this filter, instance, its associated volume and
file object.
CompletionContext - The context for the completion routine for this
operation.
Return Value:
The return value is the Status of the operation.
--*/
{
FLT_PREOP_CALLBACK_STATUS ReturnValue;
NTSTATUS Status;
PFILE_LINK_INFORMATION LinkInfo =
Data->Iopb->Parameters.SetFileInformation.InfoBuffer;
PFILE_LINK_INFORMATION MungedLinkInfo = NULL;
ULONG MungedLinkInfoSize;
PFLT_FILE_NAME_INFORMATION FileInfo = NULL;
PNC_INSTANCE_CONTEXT InstanceContext = NULL;
NC_PATH_OVERLAP RealOverlap;
NC_PATH_OVERLAP UserOverlap;
UNICODE_STRING UserRemainder;
UNICODE_STRING MungedName = EMPTY_UNICODE_STRING;
BOOLEAN IgnoreCase = !BooleanFlagOn( FltObjects->FileObject->Flags,
FO_OPENED_CASE_SENSITIVE );
PAGED_CODE();
FLT_ASSERT( IoGetTopLevelIrp() == NULL );
UNREFERENCED_PARAMETER( CompletionContext );
Status = FltGetInstanceContext( FltObjects->Instance,
&InstanceContext);
if (!NT_SUCCESS( Status )) {
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreSetLinkInformationCleanup;
}
Status = FltGetDestinationFileNameInformation( FltObjects->Instance,
FltObjects->FileObject,
LinkInfo->RootDirectory,
LinkInfo->FileName,
LinkInfo->FileNameLength,
FLT_FILE_NAME_OPENED |
FLT_FILE_NAME_QUERY_DEFAULT |
FLT_FILE_NAME_REQUEST_FROM_CURRENT_PROVIDER,
&FileInfo);
if (!NT_SUCCESS( Status )) {
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreSetLinkInformationCleanup;
}
Status = FltParseFileNameInformation( FileInfo );
if (!NT_SUCCESS( Status )) {
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreSetLinkInformationCleanup;
}
//
// Calculate Overlap and Remainder
//
NcComparePath( &FileInfo->Name,
&InstanceContext->Mapping.UserMapping,
&UserRemainder,
IgnoreCase,
TRUE,
&UserOverlap );
NcComparePath( &FileInfo->Name,
&InstanceContext->Mapping.RealMapping,
NULL,
IgnoreCase,
TRUE,
&RealOverlap );
//
// We cannot allow the user to link inside the real mapping, since it
// is hidden.
//
if (RealOverlap.Match) {
Status = STATUS_ACCESS_DENIED;
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreSetLinkInformationCleanup;
} else if (RealOverlap.InMapping) {
//
// We should never get here. Getting here requires an
// OPEN_TARGET_DIRECTORY open which should already have failed.
//
FLT_ASSERT( !RealOverlap.InMapping );
Status = STATUS_OBJECT_PATH_NOT_FOUND;
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreSetLinkInformationCleanup;
} else if ((RealOverlap.Ancestor || UserOverlap.Ancestor) &&
LinkInfo->ReplaceIfExists) {
//
// The user is attempting to overwrite a parent of the mapping.
// Fail this operation.
//
Status = STATUS_ACCESS_DENIED;
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreSetLinkInformationCleanup;
}
//
// If the destination path is outside the mapping then we can pass it
// through without a problem.
//
if (!UserOverlap.InMapping) {
//
// The destination outside the mapping. We can ignore this IO.
//
Status = STATUS_SUCCESS;
ReturnValue = FLT_PREOP_SUCCESS_NO_CALLBACK;
goto NcPreSetLinkInformationCleanup;
}
//
// The destination is inside the mapping. This means we have to issue
// our own request and forward the results to the user.
//
//
// We need to build a new path to link on.
//
Status = NcConstructPath( &InstanceContext->Mapping.RealMapping,
&UserRemainder,
TRUE,
&MungedName );
if (!NT_SUCCESS( Status )) {
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreSetLinkInformationCleanup;
}
//
// Create our own link structure.
//
MungedLinkInfoSize = sizeof(FILE_LINK_INFORMATION) + MungedName.Length - sizeof(WCHAR);
MungedLinkInfo = ExAllocatePoolZero( PagedPool,
MungedLinkInfoSize,
NC_SET_LINK_BUFFER_TAG );
if (MungedLinkInfo == NULL) {
Status = STATUS_INSUFFICIENT_RESOURCES;
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreSetLinkInformationCleanup;
}
MungedLinkInfo->ReplaceIfExists = LinkInfo->ReplaceIfExists;
MungedLinkInfo->RootDirectory = NULL;
MungedLinkInfo->FileNameLength = MungedName.Length;
RtlCopyMemory( &MungedLinkInfo->FileName, MungedName.Buffer, MungedName.Length );
//
// Issue our own request.
//
Status = FltSetInformationFile( FltObjects->Instance,
FltObjects->FileObject,
MungedLinkInfo,
MungedLinkInfoSize,
FileLinkInformation );
//
// Because we issued the IO, we will pass complete this ourselves.
//
ReturnValue = FLT_PREOP_COMPLETE;
NcPreSetLinkInformationCleanup:
if (ReturnValue == FLT_PREOP_COMPLETE) {
Data->IoStatus.Status = Status;
}
if (MungedLinkInfo != NULL) {
ExFreePoolWithTag( MungedLinkInfo, NC_SET_LINK_BUFFER_TAG );
}
if (FileInfo != NULL) {
FltReleaseFileNameInformation( FileInfo );
}
if (InstanceContext != NULL) {
FltReleaseContext( InstanceContext );
}
if (MungedName.Buffer != NULL) {
ExFreePoolWithTag( MungedName.Buffer, NC_TAG );
}
return ReturnValue;
}
FLT_PREOP_CALLBACK_STATUS
NcPreRename(
_Inout_ PFLT_CALLBACK_DATA Data,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Flt_CompletionContext_Outptr_ PVOID *CompletionContext
)
/*++
Routine Description:
Fltmgr callback which manages renaming files. We must disallow renaming
on an ancestor of either mapping because otherwise we would have to
maintain the mapping's short/long name pairings.
Arguments:
Data - Pointer to the filter CallbackData that is passed to us.
FltObjects - Pointer to the FLT_RELATED_OBJECTS data structure containing
opaque handles to this filter, instance, its associated volume and
file object.
CompletionContext - The context for the completion routine for this
operation.
Return Value:
The return value is the Status of the operation.
--*/
{
//
// Return Values
//
NTSTATUS Status;
FLT_PREOP_CALLBACK_STATUS ReturnValue;
//
// Contexts
//
PNC_INSTANCE_CONTEXT InstanceContext = NULL;
//
// Data
//
PFILE_RENAME_INFORMATION RenameInfo =
Data->Iopb->Parameters.SetFileInformation.InfoBuffer;
//
// FileInformation
//
PFLT_FILE_NAME_INFORMATION TargetInfo = NULL;
PFLT_FILE_NAME_INFORMATION SrcInfo = NULL;
//
// Target Real Overlap
//
NC_PATH_OVERLAP TargetRealOverlap;
UNICODE_STRING TargetRealRemainder;
//
// Target User Overlap
//
NC_PATH_OVERLAP TargetUserOverlap;
UNICODE_STRING TargetUserRemainder;
//
// Src Real Overlap
//
NC_PATH_OVERLAP SrcRealOverlap;
NC_PATH_OVERLAP SrcUserOverlap;
//
// Munge Data
//
UNICODE_STRING MungedTargetName = EMPTY_UNICODE_STRING;
PFILE_RENAME_INFORMATION MungedRenameInfo = NULL;
ULONG MungedRenameLength;
BOOLEAN IgnoreCase = !BooleanFlagOn( FltObjects->FileObject->Flags,
FO_OPENED_CASE_SENSITIVE );
FILE_INFORMATION_CLASS fileInformationClass;
BOOLEAN ReplaceIfExists;
fileInformationClass = Data->Iopb->Parameters.SetFileInformation.FileInformationClass;
ReplaceIfExists = (fileInformationClass == FileRenameInformationEx) ?
BooleanFlagOn( RenameInfo->Flags, FILE_RENAME_REPLACE_IF_EXISTS ) :
RenameInfo->ReplaceIfExists;
PAGED_CODE();
UNREFERENCED_PARAMETER( CompletionContext );
FLT_ASSERT( IoGetTopLevelIrp() == NULL );
FLT_ASSERT( (fileInformationClass == FileRenameInformation) ||
(fileInformationClass == FileRenameInformationEx) );
//
// Get Instance Context
//
Status = FltGetInstanceContext( FltObjects->Instance,
&InstanceContext );
if (!NT_SUCCESS( Status )) {
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreRenameCleanup;
}
//
// Find out the src file's name.
//
Status = NcGetFileNameInformation( Data,
NULL,
NULL,
FLT_FILE_NAME_OPENED |
FLT_FILE_NAME_QUERY_DEFAULT |
FLT_FILE_NAME_REQUEST_FROM_CURRENT_PROVIDER,
&SrcInfo);
if (!NT_SUCCESS( Status )) {
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreRenameCleanup;
}
Status = FltParseFileNameInformation( SrcInfo );
if (!NT_SUCCESS( Status )) {
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreRenameCleanup;
}
//
// Find the src's overlap with the real and user mappings.
//
NcComparePath( &SrcInfo->Name,
&InstanceContext->Mapping.RealMapping,
NULL,
IgnoreCase,
TRUE,
&SrcRealOverlap );
NcComparePath( &SrcInfo->Name,
&InstanceContext->Mapping.UserMapping,
NULL,
IgnoreCase,
TRUE,
&SrcUserOverlap );
//
// If the src is an ancestor of either the user or real mappings we can
// fail the request.
//
if (SrcUserOverlap.Ancestor || SrcRealOverlap.Ancestor) {
ReturnValue = FLT_PREOP_COMPLETE;
Status = STATUS_ACCESS_DENIED;
goto NcPreRenameCleanup;
}
//
// Find out the target file's name.
//
Status = FltGetDestinationFileNameInformation( FltObjects->Instance,
FltObjects->FileObject,
RenameInfo->RootDirectory,
RenameInfo->FileName,
RenameInfo->FileNameLength,
FLT_FILE_NAME_OPENED |
FLT_FILE_NAME_QUERY_DEFAULT |
FLT_FILE_NAME_REQUEST_FROM_CURRENT_PROVIDER,
&TargetInfo);
if (!NT_SUCCESS( Status )) {
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreRenameCleanup;
}
Status = FltParseFileNameInformation( TargetInfo );
if( !NT_SUCCESS( Status ) ) {
FLT_ASSERT( NT_SUCCESS( Status ) );
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreRenameCleanup;
}
//
// Find the target's overlap with the real and user mappings.
//
NcComparePath( &TargetInfo->Name,
&InstanceContext->Mapping.RealMapping,
&TargetRealRemainder,
IgnoreCase,
TRUE,
&TargetRealOverlap );
NcComparePath( &TargetInfo->Name,
&InstanceContext->Mapping.UserMapping,
&TargetUserRemainder,
IgnoreCase,
TRUE,
&TargetUserOverlap );
//
// If the target is in the real mapping, then disallow the rename. If
// the target is to an ancestor of the mappings, this could change IDs
// and is therefore also disallowed.
//
if (TargetRealOverlap.InMapping) {
Status = STATUS_ACCESS_DENIED;
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreRenameCleanup;
} else if ((TargetRealOverlap.Ancestor || TargetUserOverlap.Ancestor) &&
ReplaceIfExists) {
Status = STATUS_ACCESS_DENIED;
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreRenameCleanup;
}
//
// If the target is in the user mapping, then we need to munge the
// name and send the request down. If this is a stream rename we
// do not perform the mapping since only the stream name is changing.
//
if (TargetUserOverlap.InMapping &&
(RenameInfo->FileName[0] != ':')) {
Status = NcConstructPath( &InstanceContext->Mapping.RealMapping,
&TargetUserRemainder,
TRUE,
&MungedTargetName );
if (!NT_SUCCESS( Status )) {
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreRenameCleanup;
}
//
// Because the target is in the user mapping, we have to issue
// our own rename down the real mapping.
//
//
// Allocate rename information structure.
//
MungedRenameLength = sizeof(FILE_RENAME_INFORMATION) -
sizeof(WCHAR) +
MungedTargetName.Length;
MungedRenameInfo = ExAllocatePoolZero( PagedPool,
MungedRenameLength,
NC_RENAME_BUFFER_TAG );
if (MungedRenameInfo == NULL) {
Status = STATUS_INSUFFICIENT_RESOURCES;
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreRenameCleanup;
}
//
// Copy user rename parameters.
//
MungedRenameInfo->Flags = RenameInfo->Flags;
MungedRenameInfo->RootDirectory = NULL;
MungedRenameInfo->FileNameLength = MungedTargetName.Length;
RtlCopyMemory( &MungedRenameInfo->FileName,
MungedTargetName.Buffer,
MungedTargetName.Length );
//
// Send the request. Note that we cannot just place the new buffer
// in the CallbackData; filesystems use the name from a previous
// OPEN_TARGET_DIRECTORY open, so changing the buffer here would
// result in unexpected (and undefined!) behavior.
//
Status = FltSetInformationFile( FltObjects->Instance,
FltObjects->FileObject,
MungedRenameInfo,
MungedRenameLength,
fileInformationClass );
//
// Complete the IO.
//
ReturnValue = FLT_PREOP_COMPLETE;
goto NcPreRenameCleanup;
} else {
//
// The target was outside the mapping. The rename does not have
// to be munged. Pass through.
//
ReturnValue = FLT_PREOP_SUCCESS_NO_CALLBACK;
goto NcPreRenameCleanup;
}
NcPreRenameCleanup:
if (ReturnValue == FLT_PREOP_COMPLETE) {
Data->IoStatus.Status = Status;
}
if (InstanceContext != NULL) {
FltReleaseContext( InstanceContext );
}
if (TargetInfo != NULL) {
FltReleaseFileNameInformation( TargetInfo );
}
if (SrcInfo != NULL) {
FltReleaseFileNameInformation( SrcInfo );
}
if (MungedTargetName.Buffer != NULL) {
ExFreePoolWithTag( MungedTargetName.Buffer, NC_GENERATE_NAME_TAG );
}
if (MungedRenameInfo != NULL) {
ExFreePoolWithTag( MungedRenameInfo, NC_RENAME_BUFFER_TAG );
}
return ReturnValue;
}
|