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
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
|
/*++
Copyright (c) 1999 - 2002 Microsoft Corporation
Module Name:
SimRep.c
Abstract:
The Simulate Reparse Sample demonstrates how to return STATUS_REPARSE
on precreates. This allows the filter to redirect opens down one path
to another path. The Precreate path is complicated by network query opens
which come down as Fast IO. Fast IO cannot be redirected with Status Reparse
because reparse only works on IRP based IO.
Simulating reparse points requires that the filter replace the name in the
file object. This will cause Driver Verifier to complain that the filter is
leaking pool and will prevent it from being unloaded. To solve this issue
SimRep attempts to use a Windows 7 Function called IoReplaceFileObjectName
which will allow IO Mgr to replace the name for us with the correct pool tag.
However, on downlevel OS Versions SimRep will go ahead and replace the name
itself.
It is important to note that SimRep only demonstrates how to return
STATUS_REPARSE, not how to deal with file names on NT. SimRep uses two strings
to act as a mapping. When the file open name starts with the "old name mapping"
string the filter replaces it with the "new name mapping" string. This does not
take short names into account.
SimRep can also be configured to redirect renames and creation of hardlinks.
This functionality is demonstrated in the code and can be turned on with a
registry key value indicated in the inf file. To correctly handle rename and
set link operations:
1. SimRep has to reparse opens with the SL_OPEN_TARGET_DIRECTORY flag set in
the pre-create, since this is the create that IoManager uses to open the
target of the rename.
2. SimRep implements a "pass-through" name provider. It needs to do this so
that the creates issued to resolve normalized name queries will be seen by
SimRep and it can redirect them correctly, so as to provide consistent names
to other filters.
3. SimRep has to monitor IRP_MJ_SET_INFORMATION for rename and set link
operations and re-issue them for the correct destination so that filters
below SimRep are made aware of this redirection.
Note that SimRep simply redirects creates (and optionally renames and set
hardlink) operations. It makes no attempt to virtualize the namespace for
filters above SimRep. So the layers above SimRep will be aware of the
redirection if they query the name of the file once the create, rename or set
hardlink operation is complete.
Environment:
Kernel mode
--*/
//
// Enabled warnings
//
#pragma warning(error:4100) // Enable-Unreferenced formal parameter
#pragma warning(error:4101) // Enable-Unreferenced local variable
#pragma warning(error:4061) // Enable-missing enumeration in switch statement
#pragma warning(error:4505) // Enable-identify dead functions
//
// Includes
//
//
// This sample contains OS version specific code. If compiled for VISTA it
// will not run properly on older versions of Windows.
//
#define SIMREP_VISTA (NTDDI_VERSION >= NTDDI_VISTA)
#include <fltKernel.h>
//
// Memory Pool Tags
//
#define SIMREP_STRING_TAG 'tSpR'
#define SIMREP_REG_TAG 'eRpR'
//
// Constants
//
#define REPLACE_ROUTINE_NAME_STRING L"IoReplaceFileObjectName"
#define REPLACE_QUERY_DIRECTORY_FILE_ROUTINE_NAME_STRING "FltQueryDirectoryFile"
//
// Context sample filter global data structures.
//
typedef struct _MAPPING_ENTRY {
//
// Path underwhich we want to reparse.
//
UNICODE_STRING OldName;
//
// Path to reparse to.
//
UNICODE_STRING NewName;
} MAPPING_ENTRY, *PMAPPING_ENTRY;
//
// Starting with windows 7, the IO Manager provides IoReplaceFileObjectName,
// but old versions of Windows will not have this function. Rather than just
// writing our own function, and forfeiting future windows functionality, we can
// use MmGetRoutineAddr, which will allow us to dynamically import IoReplaceFileObjectName
// if it exists. If not it allows us to implement the function ourselves.
//
typedef
NTSTATUS
(* PReplaceFileObjectName ) (
_In_ PFILE_OBJECT FileObject,
_In_reads_bytes_(FileNameLength) PWSTR NewFileName,
_In_ USHORT FileNameLength
);
typedef
NTSTATUS
(FLTAPI *PFltQueryDirectoryFile)(
_In_ PFLT_INSTANCE Instance,
_In_ PFILE_OBJECT FileObject,
_In_reads_bytes_(Length) PVOID FileInformationBuffer,
_In_ ULONG Length,
_In_ FILE_INFORMATION_CLASS FileInformationClass,
_In_ BOOLEAN ReturnSingleEntry,
_In_opt_ PUNICODE_STRING FileName,
_In_ BOOLEAN RestartScan,
_Out_opt_ PULONG LengthReturned
);
typedef struct _SIMREP_GLOBAL_DATA {
//
// Handle to minifilter returned from FltRegisterFilter()
//
PFLT_FILTER Filter;
//
// Structure to hold mapping information.
//
MAPPING_ENTRY Mapping;
//
// Pointer to the function we will use to
// replace file names.
//
PReplaceFileObjectName ReplaceFileNameFunction;
//
// Pointer to the function we will use to
// query directory file.
//
PFltQueryDirectoryFile QueryDirectoryFileFunction;
//
// Flag to control if the filter remaps renames
//
BOOLEAN RemapRenamesAndLinks;
#if DBG
//
// Field to control nature of debug output
//
ULONG DebugLevel;
#endif
} SIMREP_GLOBAL_DATA, *PSIMREP_GLOBAL_DATA;
//
// Debug helper functions
//
#if DBG
#define DEBUG_TRACE_ERROR 0x00000001 // Errors - whenever we return a failure code
#define DEBUG_TRACE_LOAD_UNLOAD 0x00000002 // Loading/unloading of the filter
#define DEBUG_TRACE_INSTANCES 0x00000004 // Attach / detach of instances
#define DEBUG_TRACE_REPARSE_OPERATIONS 0x00000008 // Operations that are performed to determine if we should return STATUS_REPARSE
#define DEBUG_TRACE_REPARSED_OPERATIONS 0x00000010 // Operations that return STATUS_REPARSE
#define DEBUG_TRACE_REPARSED_REISSUE 0X00000020 // Operations that need to be reissued with an IRP.
#define DEBUG_TRACE_NAME_OPERATIONS 0x00000040 // Operations involving name provider callbacks
#define DEBUG_TRACE_RENAME_REDIRECTION_OPERATIONS 0x00000080 // Operations involving rename or hardlink redirection
#define DEBUG_TRACE_ALL_IO 0x00000100 // All IO operations tracked by this filter
#define DEBUG_TRACE_ALL 0xFFFFFFFF // All flags
#define DebugTrace(Level, Data) \
if ((Level) & Globals.DebugLevel) { \
DbgPrint Data; \
}
#else
#define DebugTrace(Level, Data) {NOTHING;}
#endif
//
// Function that handle driver load/unload and instance setup/cleanup
//
DRIVER_INITIALIZE DriverEntry;
NTSTATUS
DriverEntry (
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING RegistryPath
);
typedef
NTSTATUS
(*PFN_IoOpenDriverRegistryKey) (
PDRIVER_OBJECT DriverObject,
DRIVER_REGKEY_TYPE RegKeyType,
ACCESS_MASK DesiredAccess,
ULONG Flags,
PHANDLE DriverRegKey
);
PFN_IoOpenDriverRegistryKey
SimRepGetIoOpenDriverRegistryKey (
VOID
);
NTSTATUS
SimRepOpenServiceParametersKey (
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING ServiceRegistryPath,
_Out_ PHANDLE ServiceParametersKey
);
NTSTATUS
SimRepSetConfiguration(
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING RegistryPath
);
VOID SimRepFreeGlobals(
);
NTSTATUS
SimRepUnload (
FLT_FILTER_UNLOAD_FLAGS Flags
);
NTSTATUS
SimRepInstanceSetup (
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_ FLT_INSTANCE_SETUP_FLAGS Flags,
_In_ DEVICE_TYPE VolumeDeviceType,
_In_ FLT_FILESYSTEM_TYPE VolumeFilesystemType
);
NTSTATUS
SimRepInstanceQueryTeardown (
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_ FLT_INSTANCE_QUERY_TEARDOWN_FLAGS Flags
);
//
// Functions that track operations on the volume
//
FLT_PREOP_CALLBACK_STATUS
SimRepPreCreate (
_Inout_ PFLT_CALLBACK_DATA Cbd,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Flt_CompletionContext_Outptr_ PVOID *CompletionContext
);
FLT_PREOP_CALLBACK_STATUS
SimRepPreNetworkQueryOpen (
_Inout_ PFLT_CALLBACK_DATA Cbd,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Flt_CompletionContext_Outptr_ PVOID *CompletionContext
);
//
// Functions to support rename and hard link creation remapping
//
FLT_PREOP_CALLBACK_STATUS
SimRepPreSetInformation (
_Inout_ PFLT_CALLBACK_DATA Cbd,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Flt_CompletionContext_Outptr_ PVOID *CompletionContext
);
//
// Functions that provide string allocation support
//
_When_(return==0, _Post_satisfies_(String->Buffer != NULL))
NTSTATUS
SimRepAllocateUnicodeString (
_Inout_ PUNICODE_STRING String
);
VOID
SimRepFreeUnicodeString (
_Inout_ PUNICODE_STRING String
);
NTSTATUS
SimRepReplaceFileObjectName (
_In_ PFILE_OBJECT FileObject,
_In_reads_bytes_(FileNameLength) PWSTR NewFileName,
_In_ USHORT FileNameLength
);
BOOLEAN
SimRepCompareMapping(
_In_ PFLT_FILE_NAME_INFORMATION NameInfo,
_In_ PUNICODE_STRING MappingPath,
_In_ BOOLEAN IgnoreCase,
_Out_opt_ PBOOLEAN ExactMatch
);
NTSTATUS
SimRepMungeName(
_In_ PFLT_FILE_NAME_INFORMATION NameInfo,
_In_ PUNICODE_STRING SubPath,
_In_ PUNICODE_STRING NewSubPath,
_In_ BOOLEAN IgnoreCase,
_In_ BOOLEAN ExactMatch,
_Out_ PUNICODE_STRING MungedPath
);
//
// Functions that implement a pass through name provider
//
NTSTATUS
SimRepGenerateFileName (
_In_ PFLT_INSTANCE Instance,
_In_ PFILE_OBJECT FileObject,
_When_(FileObject->FsContext != NULL, _In_opt_)
_When_(FileObject->FsContext == NULL, _In_)
PFLT_CALLBACK_DATA Cbd,
_In_ FLT_FILE_NAME_OPTIONS NameOptions,
_Out_ PBOOLEAN CacheFileNameInformation,
_Inout_ PFLT_NAME_CONTROL FileName
);
NTSTATUS
SimRepNormalizeNameComponent (
_In_ PFLT_INSTANCE Instance,
_In_ PCUNICODE_STRING ParentDirectory,
_In_ USHORT DeviceNameLength,
_In_ PCUNICODE_STRING Component,
_Out_writes_bytes_(ExpandComponentNameLength) PFILE_NAMES_INFORMATION ExpandComponentName,
_In_ ULONG ExpandComponentNameLength,
_In_ FLT_NORMALIZE_NAME_FLAGS Flags,
_Inout_ PVOID *NormalizationContext
);
#if SIMREP_VISTA
NTSTATUS
SimRepNormalizeNameComponentEx (
_In_ PFLT_INSTANCE Instance,
_In_ PFILE_OBJECT FileObject,
_In_ PCUNICODE_STRING ParentDirectory,
_In_ USHORT DeviceNameLength,
_In_ PCUNICODE_STRING Component,
_Out_writes_bytes_(ExpandComponentNameLength) PFILE_NAMES_INFORMATION ExpandComponentName,
_In_ ULONG ExpandComponentNameLength,
_In_ FLT_NORMALIZE_NAME_FLAGS Flags,
_Inout_ PVOID *NormalizationContext
);
#endif
NTSTATUS
SimRepQueryDirectoryFile (
_In_ PFLT_INSTANCE Instance,
_In_ PFILE_OBJECT FileObject,
_Out_writes_bytes_(Length) PVOID FileInformationBuffer,
_In_ ULONG Length,
_In_ FILE_INFORMATION_CLASS FileInformationClass,
_In_ BOOLEAN ReturnSingleEntry,
_In_opt_ PUNICODE_STRING FileName,
_In_ BOOLEAN RestartScan,
_Out_opt_ PULONG LengthReturned
);
//
// Filter callback routines
//
FLT_OPERATION_REGISTRATION Callbacks[] = {
{ IRP_MJ_CREATE,
FLTFL_OPERATION_REGISTRATION_SKIP_PAGING_IO,
SimRepPreCreate,
NULL },
{ IRP_MJ_NETWORK_QUERY_OPEN,
FLTFL_OPERATION_REGISTRATION_SKIP_PAGING_IO,
SimRepPreNetworkQueryOpen,
NULL },
{ IRP_MJ_OPERATION_END }
};
//
// Filter registration data structure
//
FLT_REGISTRATION FilterRegistration = {
sizeof( FLT_REGISTRATION ), // Size
FLT_REGISTRATION_VERSION, // Version
0, // Flags
NULL, // Context
Callbacks, // Operation callbacks
SimRepUnload, // Filters unload routine
SimRepInstanceSetup, // InstanceSetup routine
SimRepInstanceQueryTeardown, // InstanceQueryTeardown routine
NULL, // InstanceTeardownStart routine
NULL, // InstanceTeardownComplete routine
NULL, // Filename generation support callback
NULL, // Filename normalization support callback
NULL, // Normalize name component cleanup callback
#if SIMREP_VISTA
NULL, // Transaction notification callback
NULL // Filename normalization support callback
#endif // SIMREP_VISTA
};
//
// Filter callback routines with rename handling
//
FLT_OPERATION_REGISTRATION CallbacksWithRename[] = {
{ IRP_MJ_CREATE,
FLTFL_OPERATION_REGISTRATION_SKIP_PAGING_IO,
SimRepPreCreate,
NULL },
{ IRP_MJ_NETWORK_QUERY_OPEN,
FLTFL_OPERATION_REGISTRATION_SKIP_PAGING_IO,
SimRepPreNetworkQueryOpen,
NULL },
{ IRP_MJ_SET_INFORMATION,
FLTFL_OPERATION_REGISTRATION_SKIP_PAGING_IO,
SimRepPreSetInformation,
NULL },
{ IRP_MJ_OPERATION_END }
};
//
// Filter registration data structure with renames
// Filter registers as a name provider and for SetInformation
//
FLT_REGISTRATION FilterRegistrationWithRename = {
sizeof( FLT_REGISTRATION ), // Size
FLT_REGISTRATION_VERSION, // Version
0, // Flags
NULL, // Context
CallbacksWithRename, // Operation callbacks
SimRepUnload, // Filters unload routine
SimRepInstanceSetup, // InstanceSetup routine
SimRepInstanceQueryTeardown, // InstanceQueryTeardown routine
NULL, // InstanceTeardownStart routine
NULL, // InstanceTeardownComplete routine
SimRepGenerateFileName, // Filename generation support callback
SimRepNormalizeNameComponent, // Filename normalization support callback
NULL, // Normalize name component cleanup callback
#if SIMREP_VISTA
NULL, // Transaction notification callback
SimRepNormalizeNameComponentEx // Filename normalization support callback
#endif // SIMREP_VISTA
};
//
// Global variables
//
SIMREP_GLOBAL_DATA Globals;
//
// Assign text sections for each routine.
//
#ifdef ALLOC_PRAGMA
#pragma alloc_text(INIT, DriverEntry)
#pragma alloc_text(INIT, SimRepGetIoOpenDriverRegistryKey)
#pragma alloc_text(INIT, SimRepOpenServiceParametersKey)
#pragma alloc_text(INIT, SimRepSetConfiguration)
#pragma alloc_text(PAGE, SimRepUnload)
#pragma alloc_text(PAGE, SimRepInstanceSetup)
#pragma alloc_text(PAGE, SimRepInstanceQueryTeardown)
#pragma alloc_text(PAGE, SimRepAllocateUnicodeString)
#pragma alloc_text(PAGE, SimRepFreeUnicodeString)
#pragma alloc_text(PAGE, SimRepReplaceFileObjectName)
#pragma alloc_text(PAGE, SimRepCompareMapping)
#pragma alloc_text(PAGE, SimRepMungeName)
#pragma alloc_text(PAGE, SimRepPreCreate)
#pragma alloc_text(PAGE, SimRepPreNetworkQueryOpen)
#pragma alloc_text(PAGE, SimRepPreSetInformation)
#pragma alloc_text(PAGE, SimRepFreeGlobals)
#pragma alloc_text(PAGE, SimRepGenerateFileName)
#pragma alloc_text(PAGE, SimRepNormalizeNameComponent)
#if SIMREP_VISTA
#pragma alloc_text(PAGE, SimRepNormalizeNameComponentEx)
#endif
#pragma alloc_text(PAGE, SimRepQueryDirectoryFile)
#endif
//
// Filter driver initialization and unload routines
//
#pragma warning(push)
#pragma warning(disable:4152) // nonstandard extension, function/data pointer conversion in expression
NTSTATUS
DriverEntry (
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING RegistryPath
)
/*++
Routine Description:
This is the initialization routine for this filter driver. It registers
itself with the filter manager and initializes all its global data structures.
Arguments:
DriverObject - Pointer to driver object created by the system to
represent this driver.
RegistryPath - Unicode string identifying where the parameters for this
driver are located in the registry.
Return Value:
Returns STATUS_SUCCESS.
--*/
{
NTSTATUS status;
UNICODE_STRING replaceRoutineName;
PFLT_REGISTRATION Registration;
//
// Default to NonPagedPoolNx for non paged pool allocations where supported.
//
ExInitializeDriverRuntime( DrvRtPoolNxOptIn );
//
// Set default global configuration
//
#if DBG
Globals.DebugLevel = DEBUG_TRACE_ALL;
#endif
Globals.RemapRenamesAndLinks = FALSE;
RtlInitUnicodeString( &Globals.Mapping.NewName, NULL );
RtlInitUnicodeString( &Globals.Mapping.OldName, NULL );
//
// Import function to replace file names.
//
RtlInitUnicodeString( &replaceRoutineName, REPLACE_ROUTINE_NAME_STRING );
Globals.ReplaceFileNameFunction = MmGetSystemRoutineAddress( &replaceRoutineName );
if (Globals.ReplaceFileNameFunction == NULL) {
Globals.ReplaceFileNameFunction = SimRepReplaceFileObjectName;
}
//
// If available (Windows Vista or later), use the FltQueryDirectoryFile API.
//
Globals.QueryDirectoryFileFunction = FltGetRoutineAddress( REPLACE_QUERY_DIRECTORY_FILE_ROUTINE_NAME_STRING );
//
// Set the filter configuration based on registry keys
//
status = SimRepSetConfiguration( DriverObject, RegistryPath );
DebugTrace( DEBUG_TRACE_LOAD_UNLOAD,
("[SimRep]: Driver being loaded\n") );
if (!NT_SUCCESS( status )) {
goto DriverEntryCleanup;
}
//
// Register with the filter manager. If the filter is not
// configured to remap renames and hardlink creation do not
// register name provider or SetInformation callbacks.
//
Registration = (Globals.RemapRenamesAndLinks == FALSE) ?
&FilterRegistration : &FilterRegistrationWithRename;
status = FltRegisterFilter( DriverObject,
Registration,
&Globals.Filter );
if (!NT_SUCCESS( status )) {
goto DriverEntryCleanup;
}
//
// Start filtering I/O
//
status = FltStartFiltering( Globals.Filter );
if (!NT_SUCCESS( status )) {
FltUnregisterFilter( Globals.Filter );
}
DriverEntryCleanup:
DebugTrace( DEBUG_TRACE_LOAD_UNLOAD,
("[SimRep]: Driver loaded complete (Status = 0x%08X)\n",
status) );
if (!NT_SUCCESS( status )) {
SimRepFreeGlobals();
}
return status;
}
#pragma warning(pop)
PFN_IoOpenDriverRegistryKey
SimRepGetIoOpenDriverRegistryKey (
VOID
)
{
static PFN_IoOpenDriverRegistryKey pIoOpenDriverRegistryKey = NULL;
UNICODE_STRING FunctionName = {0};
if (pIoOpenDriverRegistryKey == NULL) {
RtlInitUnicodeString(&FunctionName, L"IoOpenDriverRegistryKey");
pIoOpenDriverRegistryKey = (PFN_IoOpenDriverRegistryKey)MmGetSystemRoutineAddress(&FunctionName);
}
return pIoOpenDriverRegistryKey;
}
NTSTATUS
SimRepOpenServiceParametersKey (
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING ServiceRegistryPath,
_Out_ PHANDLE ServiceParametersKey
)
/*++
Routine Description:
This routine opens the service parameters key, using the isolation-compliant
APIs when possible.
Arguments:
DriverObject - Pointer to driver object created by the system to
represent this driver.
RegistryPath - The path key passed to the driver during DriverEntry.
ServiceParametersKey - Returns a handle to the service parameters subkey.
Return Value:
STATUS_SUCCESS if the function completes successfully. Otherwise a valid
NTSTATUS code is returned.
--*/
{
NTSTATUS status;
PFN_IoOpenDriverRegistryKey pIoOpenDriverRegistryKey;
UNICODE_STRING Subkey;
HANDLE ParametersKey = NULL;
HANDLE ServiceRegKey = NULL;
OBJECT_ATTRIBUTES Attributes;
//
// Open the parameters key to read values from the INF, using the API to
// open the key if possible
//
pIoOpenDriverRegistryKey = SimRepGetIoOpenDriverRegistryKey();
if (pIoOpenDriverRegistryKey != NULL) {
//
// Open the parameters key using the API
//
status = pIoOpenDriverRegistryKey( DriverObject,
DriverRegKeyParameters,
KEY_READ,
0,
&ParametersKey );
if (!NT_SUCCESS( status )) {
goto SimRepOpenServiceParametersKeyCleanup;
}
} else {
//
// Open specified service root key
//
InitializeObjectAttributes( &Attributes,
ServiceRegistryPath,
OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
NULL,
NULL );
status = ZwOpenKey( &ServiceRegKey,
KEY_READ,
&Attributes );
if (!NT_SUCCESS( status )) {
goto SimRepOpenServiceParametersKeyCleanup;
}
//
// Open the parameters key relative to service key path
//
RtlInitUnicodeString( &Subkey, L"Parameters" );
InitializeObjectAttributes( &Attributes,
&Subkey,
OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,
ServiceRegKey,
NULL );
status = ZwOpenKey( &ParametersKey,
KEY_READ,
&Attributes );
if (!NT_SUCCESS( status )) {
goto SimRepOpenServiceParametersKeyCleanup;
}
}
//
// Return value to caller
//
*ServiceParametersKey = ParametersKey;
SimRepOpenServiceParametersKeyCleanup:
if (ServiceRegKey != NULL) {
ZwClose( ServiceRegKey );
}
return status;
}
NTSTATUS
SimRepSetConfiguration(
_In_ PDRIVER_OBJECT DriverObject,
_In_ PUNICODE_STRING RegistryPath
)
/*++
Routine Descrition:
This routine sets the filter configuration based on registry values.
Arguments:
DriverObject - Pointer to driver object created by the system to
represent this driver.
RegistryPath - The path key passed to the driver during DriverEntry.
Return Value:
Returns the status of this operation.
--*/
{
NTSTATUS status;
HANDLE driverRegKey = NULL;
UNICODE_STRING valueName;
UCHAR buffer[sizeof(KEY_VALUE_PARTIAL_INFORMATION) + sizeof(ULONG)];
PKEY_VALUE_PARTIAL_INFORMATION value = (PKEY_VALUE_PARTIAL_INFORMATION)buffer;
ULONG valueLength = sizeof(buffer);
ULONG resultLength;
PKEY_VALUE_PARTIAL_INFORMATION mappingValue = NULL;
ULONG mappingValueLength = 0;
WCHAR oldMappingTail;
WCHAR newMappingTail;
PAGED_CODE();
//
// Open service parameters key to query values from
//
status = SimRepOpenServiceParametersKey( DriverObject,
RegistryPath,
&driverRegKey );
if (!NT_SUCCESS( status )) {
driverRegKey = NULL;
goto SimRepSetConfigurationCleanup;
}
#if DBG
//
// Query the debug level
//
RtlInitUnicodeString( &valueName, L"DebugLevel" );
status = ZwQueryValueKey( driverRegKey,
&valueName,
KeyValuePartialInformation,
value,
valueLength,
&resultLength );
if (NT_SUCCESS( status )) {
Globals.DebugLevel = *(PULONG)value->Data;
}
#endif
//
// Query the remap rename flag
//
RtlInitUnicodeString( &valueName, L"RemapRenamesAndLinks" );
status = ZwQueryValueKey( driverRegKey,
&valueName,
KeyValuePartialInformation,
value,
valueLength,
&resultLength );
if (NT_SUCCESS( status )) {
Globals.RemapRenamesAndLinks = *(PULONG)value->Data > 0 ? TRUE : FALSE;
}
//
// Query the length of the old mapping.
//
RtlInitUnicodeString( &valueName, L"OldMapping" );
status = ZwQueryValueKey( driverRegKey,
&valueName,
KeyValuePartialInformation,
NULL,
0,
&mappingValueLength );
if (status!=STATUS_BUFFER_TOO_SMALL && status!=STATUS_BUFFER_OVERFLOW) {
status = STATUS_INVALID_PARAMETER;
goto SimRepSetConfigurationCleanup;
}
//
// Extract the old mapping string.
//
mappingValue = ExAllocatePoolZero( PagedPool,
mappingValueLength,
SIMREP_REG_TAG );
if (mappingValue == NULL) {
status = STATUS_INSUFFICIENT_RESOURCES;
goto SimRepSetConfigurationCleanup;
}
status = ZwQueryValueKey( driverRegKey,
&valueName,
KeyValuePartialInformation,
mappingValue,
mappingValueLength,
&resultLength );
if (!NT_SUCCESS( status )) {
goto SimRepSetConfigurationCleanup;
}
if (mappingValue->Type != REG_SZ) {
status = STATUS_INVALID_PARAMETER;
goto SimRepSetConfigurationCleanup;
}
Globals.Mapping.OldName.MaximumLength = (USHORT)mappingValue->DataLength;
status = SimRepAllocateUnicodeString( &Globals.Mapping.OldName );
if (!NT_SUCCESS( status )) {
goto SimRepSetConfigurationCleanup;
}
//
// The length which we receive from ZwQueryValueKey contains size for
// the NULL termination as well. Since we are dealing with unicode
// string we'll chop off the null termination in the length.
//
Globals.Mapping.OldName.Length = (USHORT)mappingValue->DataLength - sizeof( UNICODE_NULL );
RtlCopyMemory(Globals.Mapping.OldName.Buffer,
mappingValue->Data,
Globals.Mapping.OldName.Length);
//
// Query the length of the new mapping.
//
RtlInitUnicodeString( &valueName, L"NewMapping" );
status = ZwQueryValueKey( driverRegKey,
&valueName,
KeyValuePartialInformation,
mappingValue,
mappingValueLength,
&mappingValueLength );
if (!NT_SUCCESS( status )) {
if (status!=STATUS_BUFFER_TOO_SMALL && status!=STATUS_BUFFER_OVERFLOW) {
goto SimRepSetConfigurationCleanup;
}
ExFreePoolWithTag( mappingValue, SIMREP_REG_TAG );
mappingValue = ExAllocatePoolZero( PagedPool,
mappingValueLength,
SIMREP_REG_TAG );
if (mappingValue == NULL) {
status = STATUS_INSUFFICIENT_RESOURCES;
goto SimRepSetConfigurationCleanup;
}
}
//
// Extract the new mapping string.
//
status = ZwQueryValueKey( driverRegKey,
&valueName,
KeyValuePartialInformation,
mappingValue,
mappingValueLength,
&mappingValueLength );
if (!NT_SUCCESS( status )) {
goto SimRepSetConfigurationCleanup;
}
if (mappingValue->Type != REG_SZ) {
status = STATUS_INVALID_PARAMETER;
goto SimRepSetConfigurationCleanup;
}
Globals.Mapping.NewName.MaximumLength = (USHORT) mappingValue->DataLength;
status = SimRepAllocateUnicodeString( &Globals.Mapping.NewName );
if (!NT_SUCCESS( status )) {
goto SimRepSetConfigurationCleanup;
}
//
// The length which we receive from ZwQueryValueKey contains size for
// the NULL termination as well. Since we are dealing with unicode
// string we'll chop off the null termination in the length.
//
Globals.Mapping.NewName.Length = (USHORT)mappingValue->DataLength - sizeof( UNICODE_NULL );
RtlCopyMemory(Globals.Mapping.NewName.Buffer,
mappingValue->Data,
Globals.Mapping.NewName.Length);
//
// Ensure the old and new mapping are consistent in specifying either files or directories
// as determined by the presence of a trailing backslash
//
oldMappingTail = (WCHAR)Globals.Mapping.OldName.Buffer[Globals.Mapping.OldName.Length / sizeof( WCHAR ) - 1];
newMappingTail = (WCHAR)Globals.Mapping.NewName.Buffer[Globals.Mapping.NewName.Length / sizeof( WCHAR ) - 1];
if ((oldMappingTail != newMappingTail) &&
((oldMappingTail == OBJ_NAME_PATH_SEPARATOR) ||
(newMappingTail == OBJ_NAME_PATH_SEPARATOR))) {
status = STATUS_INVALID_PARAMETER;
goto SimRepSetConfigurationCleanup;
}
SimRepSetConfigurationCleanup:
if (mappingValue != NULL) {
ExFreePoolWithTag( mappingValue, SIMREP_REG_TAG );
mappingValue = NULL;
}
if (driverRegKey != NULL) {
ZwClose( driverRegKey );
}
if (!NT_SUCCESS( status )) {
SimRepFreeUnicodeString( &Globals.Mapping.NewName );
SimRepFreeUnicodeString( &Globals.Mapping.OldName );
}
return status;
}
VOID SimRepFreeGlobals(
)
/*++
Routine Descrition:
This routine cleans up the global structure on both
teardown and initialization failure.
Arguments:
Return Value:
None.
--*/
{
PAGED_CODE();
SimRepFreeUnicodeString( &Globals.Mapping.NewName );
SimRepFreeUnicodeString( &Globals.Mapping.OldName );
}
NTSTATUS
SimRepUnload (
FLT_FILTER_UNLOAD_FLAGS Flags
)
/*++
Routine Description:
This is the unload routine for this filter driver. This is called
when the minifilter is about to be unloaded. SimRep can unload
easily because it does not own any IOs. When the filter is unloaded
existing reparsed creates will continue to work, but new creates will
not be reparsed. This is fine from the filter's perspective, but could
result in unexpected bahavior for apps.
Arguments:
Flags - Indicating if this is a mandatory unload.
Return Value:
Returns the final status of this operation.
--*/
{
UNREFERENCED_PARAMETER( Flags );
PAGED_CODE();
DebugTrace( DEBUG_TRACE_LOAD_UNLOAD,
("[SimRep]: Unloading driver\n") );
FltUnregisterFilter( Globals.Filter );
SimRepFreeGlobals();
return STATUS_SUCCESS;
}
//
// Instance setup/teardown routines.
//
NTSTATUS
SimRepInstanceSetup (
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_ FLT_INSTANCE_SETUP_FLAGS Flags,
_In_ DEVICE_TYPE VolumeDeviceType,
_In_ FLT_FILESYSTEM_TYPE VolumeFilesystemType
)
/*++
Routine Description:
This routine is called whenever a new instance is created on a volume. This
gives us a chance to decide if we need to attach to this volume or not.
SimRep does not attach on automatic attachment, but will attach when asked
manually.
Arguments:
FltObjects - Pointer to the FLT_RELATED_OBJECTS data structure containing
opaque handles to this filter, instance and its associated volume.
Flags - Flags describing the reason for this attach request.
Return Value:
STATUS_SUCCESS - attach
STATUS_FLT_DO_NOT_ATTACH - do not attach
--*/
{
UNREFERENCED_PARAMETER( FltObjects );
UNREFERENCED_PARAMETER( Flags );
UNREFERENCED_PARAMETER( VolumeDeviceType );
UNREFERENCED_PARAMETER( VolumeFilesystemType );
PAGED_CODE();
if ( FlagOn( Flags, FLTFL_INSTANCE_SETUP_AUTOMATIC_ATTACHMENT ) ) {
//
// Do not automatically attach to a volume.
//
DebugTrace( DEBUG_TRACE_INSTANCES,
("[Simrep]: Instance setup skipped (Volume = %p, Instance = %p)\n",
FltObjects->Volume,
FltObjects->Instance) );
return STATUS_FLT_DO_NOT_ATTACH;
}
//
// Attach on manual attachment.
//
DebugTrace( DEBUG_TRACE_INSTANCES,
("[SimRep]: Instance setup started (Volume = %p, Instance = %p)\n",
FltObjects->Volume,
FltObjects->Instance) );
return STATUS_SUCCESS;
}
NTSTATUS
SimRepInstanceQueryTeardown (
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_In_ FLT_INSTANCE_QUERY_TEARDOWN_FLAGS Flags
)
/*++
Routine Description:
This is called when an instance is being manually deleted by a
call to FltDetachVolume or FilterDetach thereby giving us a
chance to fail that detach request. SimRep only implements it
because otherwise calls to FltDetachVolume or FilterDetach would
fail to detach.
Arguments:
FltObjects - Pointer to the FLT_RELATED_OBJECTS data structure containing
opaque handles to this filter, instance and its associated volume.
Flags - Indicating where this detach request came from.
Return Value:
Returns the status of this operation.
--*/
{
UNREFERENCED_PARAMETER( FltObjects );
UNREFERENCED_PARAMETER( Flags );
PAGED_CODE();
DebugTrace( DEBUG_TRACE_INSTANCES,
("[SimRep]: Instance query teadown ended (Instance = %p)\n",
FltObjects->Instance) );
return STATUS_SUCCESS;
}
FLT_PREOP_CALLBACK_STATUS
SimRepPreNetworkQueryOpen (
_Inout_ PFLT_CALLBACK_DATA Cbd,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Flt_CompletionContext_Outptr_ PVOID *CompletionContext
)
/*++
Routine Description:
Because network query opens are FastIo operations, they cannot be reparsed.
This means network query opens which need to be redirected must be failed
with FLT_PREOP_DISALLOW_FASTIO. This will cause the Io Manager to reissue
the open as a regular IRP based open. To prevent performance regression,
only fail network query opens which need to be reparsed.
This is pageable because it can not be called on the paging path
Arguments:
Cbd - 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.
--*/
{
PFLT_FILE_NAME_INFORMATION nameInfo = NULL;
NTSTATUS status;
FLT_PREOP_CALLBACK_STATUS callbackStatus;
BOOLEAN match;
PIO_STACK_LOCATION irpSp;
UNREFERENCED_PARAMETER( FltObjects );
UNREFERENCED_PARAMETER( CompletionContext );
PAGED_CODE();
DebugTrace( DEBUG_TRACE_ALL_IO,
("[SimRep]: SimRepPreNetworkQueryOpen -> Enter (Cbd = %p, FileObject = %p)\n",
Cbd,
FltObjects->FileObject) );
//
// Initialize defaults
//
status = STATUS_SUCCESS;
callbackStatus = FLT_PREOP_SUCCESS_NO_CALLBACK; // pass through - default is no post op callback
//
// We only registered for this IRP, so thats all we better get!
//
NT_ASSERT( Cbd->Iopb->MajorFunction == IRP_MJ_NETWORK_QUERY_OPEN );
NT_ASSERT( FLT_IS_FASTIO_OPERATION( Cbd ) );
irpSp = IoGetCurrentIrpStackLocation(Cbd->Iopb->Parameters.NetworkQueryOpen.Irp);
//
// Check if this is a paging file as we don't want to redirect
// the location of the paging file.
//
if (FlagOn( irpSp->Flags, SL_OPEN_PAGING_FILE )) {
DebugTrace( DEBUG_TRACE_ALL_IO,
("[SimRep]: SimRepPreNetworkQueryOpen -> Ignoring paging file open (Cbd = %p, FileObject = %p)\n",
Cbd,
FltObjects->FileObject) );
goto SimRepPreNetworkQueryOpenCleanup;
}
//
// We are not allowing volume opens to be reparsed in the sample.
//
if (FlagOn( Cbd->Iopb->TargetFileObject->Flags, FO_VOLUME_OPEN )) {
DebugTrace( DEBUG_TRACE_ALL_IO,
("[SimRep]: SimRepPreNetworkQueryOpen -> Ignoring volume open (Cbd = %p, FileObject = %p)\n",
Cbd,
FltObjects->FileObject) );
goto SimRepPreNetworkQueryOpenCleanup;
}
//
// Don't reparse an open by ID because it is not possible to determine create path intent.
//
if (FlagOn( irpSp->Parameters.Create.Options, FILE_OPEN_BY_FILE_ID )) {
goto SimRepPreNetworkQueryOpenCleanup;
}
//
// A rename should never come on the fast IO path
//
NT_ASSERT( irpSp->Flags != SL_OPEN_TARGET_DIRECTORY );
status = FltGetFileNameInformation( Cbd,
FLT_FILE_NAME_OPENED |
FLT_FILE_NAME_QUERY_DEFAULT,
&nameInfo );
if (!NT_SUCCESS( status )) {
DebugTrace( DEBUG_TRACE_REPARSE_OPERATIONS | DEBUG_TRACE_ERROR,
("[SimRep]: SimRepPreNetworkQueryOpen -> Failed to get name information (Cbd = %p, FileObject = %p)\n",
Cbd,
FltObjects->FileObject) );
goto SimRepPreNetworkQueryOpenCleanup;
}
DebugTrace( DEBUG_TRACE_REPARSE_OPERATIONS,
("[SimRep]: SimRepPreNetworkQueryOpen -> Processing create for file %wZ (Cbd = %p, FileObject = %p)\n",
&nameInfo->Name,
Cbd,
FltObjects->FileObject) );
//
// Parse the filename information
//
status = FltParseFileNameInformation( nameInfo );
if (!NT_SUCCESS( status )) {
DebugTrace( DEBUG_TRACE_REPARSE_OPERATIONS | DEBUG_TRACE_ERROR,
("[SimRep]: SimRepPreNetworkQueryOpen -> Failed to parse name information for file %wZ (Cbd = %p, FileObject = %p)\n",
&nameInfo->Name,
Cbd,
FltObjects->FileObject) );
goto SimRepPreNetworkQueryOpenCleanup;
}
//
// Determine if this query involes a path that matches the remapping path.
// Note: if the create is case sensitive this comparison must be as well.
//
match = SimRepCompareMapping( nameInfo,
&Globals.Mapping.OldName,
!FlagOn( irpSp->Flags, SL_CASE_SENSITIVE ),
NULL );
if (match) {
DebugTrace( DEBUG_TRACE_REPARSE_OPERATIONS,
("[SimRep]: SimRepPreNetworkQueryOpen -> File name %wZ matches mapping. (Cbd = %p, FileObject = %p)\n"
"\tMapping.OldFileName = %wZ\n"
"\tMapping.NewFileName = %wZ\n",
&nameInfo->Name,
Cbd,
FltObjects->FileObject,
Globals.Mapping.OldName,
Globals.Mapping.NewName) );
//
// Because the file matched the mapping, we need to redirect this open with a new name.
//
//
// We can't return STATUS_REPARSE because it is FastIO. Return
// FLT_PREOP_DISALLOW_FASTIO, so it will be reissued down the slow path.
//
DebugTrace(DEBUG_TRACE_REPARSED_REISSUE,
("[SimRep]: Disallow fast IO that is to a mapped path! %wZ\n",
&nameInfo->Name) );
callbackStatus = FLT_PREOP_DISALLOW_FASTIO;
}
SimRepPreNetworkQueryOpenCleanup:
//
// Release the references we have acquired
//
if (nameInfo != NULL) {
FltReleaseFileNameInformation( nameInfo );
}
if (!NT_SUCCESS( status )) {
//
// An error occurred, fail the query
//
DebugTrace( DEBUG_TRACE_ERROR,
("[SimRep]: SimRepPreNetworkQueryOpen -> Failed with status 0x%x \n",
status) );
Cbd->IoStatus.Status = status;
callbackStatus = FLT_PREOP_COMPLETE;
}
DebugTrace( DEBUG_TRACE_ALL_IO,
("[SimRep]: SimRepPreNetworkQueryOpen -> Exit (Cbd = %p, FileObject = %p)\n",
Cbd,
FltObjects->FileObject) );
return callbackStatus;
}
FLT_PREOP_CALLBACK_STATUS
SimRepPreCreate (
_Inout_ PFLT_CALLBACK_DATA Cbd,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Flt_CompletionContext_Outptr_ PVOID *CompletionContext
)
/*++
Routine Description:
This routine does the work for SimRep sample. SimRepPreCreate is called in
the pre-operation path for IRP_MJ_CREATE and IRP_MJ_NETWORK_QUERY_OPEN.
The function queries the requested file name for the create and compares
it to the mapping path. If the file is down the "old mapping path", the
filter checks to see if the request is fast io based. If it is we cannot
reparse the create because fast io does not support STATUS_REPARSE.
Instead we return FLT_PREOP_DISALLOW_FASTIO to force the io to be reissued
on the IRP path. If the create is IRP based, then we replace the file
object's file name field with a new path based on the "new mapping path".
This is pageable because it could not be called on the paging path
Arguments:
Cbd - 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.
--*/
{
PFLT_FILE_NAME_INFORMATION nameInfo = NULL;
NTSTATUS status;
FLT_PREOP_CALLBACK_STATUS callbackStatus;
UNICODE_STRING newFileName;
UNREFERENCED_PARAMETER( FltObjects );
UNREFERENCED_PARAMETER( CompletionContext );
PAGED_CODE();
DebugTrace( DEBUG_TRACE_ALL_IO,
("[SimRep]: SimRepPreCreate -> Enter (Cbd = %p, FileObject = %p)\n",
Cbd,
FltObjects->FileObject) );
//
// Initialize defaults
//
status = STATUS_SUCCESS;
callbackStatus = FLT_PREOP_SUCCESS_NO_CALLBACK; // pass through - default is no post op callback
RtlInitUnicodeString( &newFileName, NULL );
//
// We only registered for this irp, so thats all we better get!
//
NT_ASSERT( Cbd->Iopb->MajorFunction == IRP_MJ_CREATE );
//
// Check if this is a paging file as we don't want to redirect
// the location of the paging file.
//
if (FlagOn( Cbd->Iopb->OperationFlags, SL_OPEN_PAGING_FILE )) {
DebugTrace( DEBUG_TRACE_ALL_IO,
("[SimRep]: SimRepPreCreate -> Ignoring paging file open (Cbd = %p, FileObject = %p)\n",
Cbd,
FltObjects->FileObject) );
goto SimRepPreCreateCleanup;
}
//
// We are not allowing volume opens to be reparsed in the sample.
//
if (FlagOn( Cbd->Iopb->TargetFileObject->Flags, FO_VOLUME_OPEN )) {
DebugTrace( DEBUG_TRACE_ALL_IO,
("[SimRep]: SimRepPreCreate -> Ignoring volume open (Cbd = %p, FileObject = %p)\n",
Cbd,
FltObjects->FileObject) );
goto SimRepPreCreateCleanup;
}
//
// SimRep does not honor the FILE_OPEN_REPARSE_POINT create option. For a
// symbolic the caller would pass this flag, for example, in order to open
// the link for deletion. There is no concept of deleting the mapping for
// this filter so it is not clear what the purpose of honoring this flag
// would be.
//
//
// Don't reparse an open by ID because it is not possible to determine create path intent.
//
if (FlagOn( Cbd->Iopb->Parameters.Create.Options, FILE_OPEN_BY_FILE_ID )) {
goto SimRepPreCreateCleanup;
}
if (FlagOn( Cbd->Iopb->OperationFlags, SL_OPEN_TARGET_DIRECTORY ) &&
!Globals.RemapRenamesAndLinks) {
//
// This is a prelude to a rename or hard link creation but the filter
// is NOT configured to filter these operations. To perform the operation
// successfully and in a consistent manner this create must not trigger
// a reparse. Pass through the create without attempting any redirection.
//
goto SimRepPreCreateCleanup;
}
//
// Get the name information.
//
if (FlagOn( Cbd->Iopb->OperationFlags, SL_OPEN_TARGET_DIRECTORY )) {
//
// The SL_OPEN_TARGET_DIRECTORY flag indicates the caller is attempting
// to open the target of a rename or hard link creation operation. We
// must clear this flag when asking fltmgr for the name or the result
// will not include the final component. We need the full path in order
// to compare the name to our mapping.
//
ClearFlag( Cbd->Iopb->OperationFlags, SL_OPEN_TARGET_DIRECTORY );
DebugTrace( DEBUG_TRACE_RENAME_REDIRECTION_OPERATIONS,
("[SimRep]: SimRepPreCreate -> Clearing SL_OPEN_TARGET_DIRECTORY for %wZ (Cbd = %p, FileObject = %p)\n",
&nameInfo->Name,
Cbd,
FltObjects->FileObject) );
//
// Get the filename as it appears below this filter. Note that we use
// FLT_FILE_NAME_QUERY_FILESYSTEM_ONLY when querying the filename
// so that the filename as it appears below this filter does not end up
// in filter manager's name cache.
//
status = FltGetFileNameInformation( Cbd,
FLT_FILE_NAME_OPENED | FLT_FILE_NAME_QUERY_FILESYSTEM_ONLY,
&nameInfo );
//
// Restore the SL_OPEN_TARGET_DIRECTORY flag so the create will proceed
// for the target. The file systems depend on this flag being set in
// the target create in order for the subsequent SET_INFORMATION
// operation to proceed correctly.
//
SetFlag( Cbd->Iopb->OperationFlags, SL_OPEN_TARGET_DIRECTORY );
} else {
//
// Note that we use FLT_FILE_NAME_QUERY_DEFAULT when querying the
// filename. In the precreate the filename should not be in filter
// manager's name cache so there is no point looking there.
//
status = FltGetFileNameInformation( Cbd,
FLT_FILE_NAME_OPENED |
FLT_FILE_NAME_QUERY_DEFAULT,
&nameInfo );
}
if (!NT_SUCCESS( status )) {
DebugTrace( DEBUG_TRACE_REPARSE_OPERATIONS | DEBUG_TRACE_ERROR,
("[SimRep]: SimRepPreCreate -> Failed to get name information (Cbd = %p, FileObject = %p)\n",
Cbd,
FltObjects->FileObject) );
goto SimRepPreCreateCleanup;
}
DebugTrace( DEBUG_TRACE_REPARSE_OPERATIONS,
("[SimRep]: SimRepPreCreate -> Processing create for file %wZ (Cbd = %p, FileObject = %p)\n",
&nameInfo->Name,
Cbd,
FltObjects->FileObject) );
//
// Parse the filename information
//
status = FltParseFileNameInformation( nameInfo );
if (!NT_SUCCESS( status )) {
DebugTrace( DEBUG_TRACE_REPARSE_OPERATIONS | DEBUG_TRACE_ERROR,
("[SimRep]: SimRepPreCreate -> Failed to parse name information for file %wZ (Cbd = %p, FileObject = %p)\n",
&nameInfo->Name,
Cbd,
FltObjects->FileObject) );
goto SimRepPreCreateCleanup;
}
//
// Munge the path from the old mapping to new mapping if the query overlaps
// the mapping path. Note: if the create is case sensitive this comparison
// must be as well.
//
status = SimRepMungeName( nameInfo,
&Globals.Mapping.OldName,
&Globals.Mapping.NewName,
!FlagOn( Cbd->Iopb->OperationFlags, SL_CASE_SENSITIVE ),
FALSE,
&newFileName);
if (!NT_SUCCESS( status )) {
if (status == STATUS_NOT_FOUND) {
status = STATUS_SUCCESS;
}
goto SimRepPreCreateCleanup;
}
DebugTrace( DEBUG_TRACE_REPARSE_OPERATIONS,
("[SimRep]: SimRepPreCreate -> File name %wZ matches mapping. (Cbd = %p, FileObject = %p)\n"
"\tMapping.OldFileName = %wZ\n"
"\tMapping.NewFileName = %wZ\n",
&nameInfo->Name,
Cbd,
FltObjects->FileObject,
Globals.Mapping.OldName,
Globals.Mapping.NewName) );
//
// Switch names
//
status = Globals.ReplaceFileNameFunction( Cbd->Iopb->TargetFileObject,
newFileName.Buffer,
newFileName.Length );
if ( !NT_SUCCESS( status )) {
DebugTrace( DEBUG_TRACE_REPARSE_OPERATIONS | DEBUG_TRACE_ERROR,
("[SimRep]: SimRepPreCreate -> Failed to allocate string for file %wZ (Cbd = %p, FileObject = %p)\n",
&nameInfo->Name,
Cbd,
FltObjects->FileObject ));
goto SimRepPreCreateCleanup;
}
//
// Set the status to STATUS_REPARSE
//
status = STATUS_REPARSE;
DebugTrace( DEBUG_TRACE_REPARSE_OPERATIONS | DEBUG_TRACE_REPARSED_OPERATIONS,
("[SimRep]: SimRepPreCreate -> Returning STATUS_REPARSE for file %wZ. (Cbd = %p, FileObject = %p)\n"
"\tNewName = %wZ\n",
&nameInfo->Name,
Cbd,
FltObjects->FileObject,
&newFileName) );
SimRepPreCreateCleanup:
//
// Release the references we have acquired
//
SimRepFreeUnicodeString( &newFileName );
if (nameInfo != NULL) {
FltReleaseFileNameInformation( nameInfo );
}
if (status == STATUS_REPARSE) {
//
// Reparse the open
//
Cbd->IoStatus.Status = STATUS_REPARSE;
Cbd->IoStatus.Information = IO_REPARSE;
callbackStatus = FLT_PREOP_COMPLETE;
} else if (!NT_SUCCESS( status )) {
//
// An error occurred, fail the open
//
DebugTrace( DEBUG_TRACE_ERROR,
("[SimRep]: SimRepPreCreate -> Failed with status 0x%x \n",
status) );
Cbd->IoStatus.Status = status;
callbackStatus = FLT_PREOP_COMPLETE;
}
DebugTrace( DEBUG_TRACE_ALL_IO,
("[SimRep]: SimRepPreCreate -> Exit (Cbd = %p, FileObject = %p)\n",
Cbd,
FltObjects->FileObject) );
return callbackStatus;
}
FLT_PREOP_CALLBACK_STATUS
SimRepPreSetInformation (
_Inout_ PFLT_CALLBACK_DATA Cbd,
_In_ PCFLT_RELATED_OBJECTS FltObjects,
_Flt_CompletionContext_Outptr_ PVOID *CompletionContext
)
/*++
Routine Description:
Pre callback for handling SetInformation.
Arguments:
Cdb - 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 returnStatus = FLT_PREOP_SUCCESS_NO_CALLBACK;
NTSTATUS status = STATUS_SUCCESS;
PVOID buffer = NULL;
ULONG bufferLength = 0;
FILE_INFORMATION_CLASS fileInfoClass;
PFILE_RENAME_INFORMATION renameInfo = NULL;
PFILE_RENAME_INFORMATION newRenameInfo = NULL;
PFILE_LINK_INFORMATION linkInfo = NULL;
PFILE_LINK_INFORMATION newLinkInfo = NULL;
PFLT_FILE_NAME_INFORMATION nameInfo = NULL;
UNICODE_STRING newFileName;
struct {
HANDLE RootDirectory;
ULONG FileNameLength;
PWSTR FileName;
} setInfo;
PAGED_CODE();
UNREFERENCED_PARAMETER( CompletionContext );
RtlInitUnicodeString(&newFileName, NULL);
NT_ASSERT( Globals.RemapRenamesAndLinks );
fileInfoClass = Cbd->Iopb->Parameters.SetFileInformation.FileInformationClass;
#pragma warning( push )
#pragma warning( disable:4061 )
switch (fileInfoClass) {
case FileRenameInformation:
case FileRenameInformationEx:
//
// Note: We should never see a rename of the mapping path \x\y itself
// because the name would have been reparsed to the new mapping \a\b.
// This is different than the behavior of normal reparse points where
// the same operation would reassign the reparse point.
//
renameInfo = Cbd->Iopb->Parameters.SetFileInformation.InfoBuffer;
//
// Accessing ReplaceIfExists -
// Using FileRenameInformation - renameInfo->ReplaceIfExists
// Using FileRenameInformationEx - FlagOn( renameInfo->Flags, FILE_RENAME_REPLACE_IF_EXISTS )
//
setInfo.RootDirectory = renameInfo->RootDirectory;
setInfo.FileNameLength = renameInfo->FileNameLength;
setInfo.FileName = renameInfo->FileName;
break;
case FileLinkInformation:
linkInfo = Cbd->Iopb->Parameters.SetFileInformation.InfoBuffer;
// Accessing ReplaceIfExists - linkInfo->ReplaceIfExists
setInfo.RootDirectory = linkInfo->RootDirectory;
setInfo.FileNameLength = linkInfo->FileNameLength;
setInfo.FileName = linkInfo->FileName;
break;
case FileDirectoryInformation: // 1
case FileFullDirectoryInformation: // 2
case FileBothDirectoryInformation: // 3
case FileBasicInformation: // 4 wdm
case FileStandardInformation: // 5 wdm
case FileInternalInformation: // 6
case FileEaInformation: // 7
case FileAccessInformation: // 8
case FileNameInformation: // 9
case FileNamesInformation: // 12
case FileDispositionInformation: // 13
case FilePositionInformation: // 14 wdm
case FileFullEaInformation: // 15
case FileModeInformation: // 16
case FileAlignmentInformation: // 17
case FileAllInformation: // 18
case FileAllocationInformation: // 19
case FileEndOfFileInformation: // 20 wdm
case FileAlternateNameInformation: // 21
case FileStreamInformation: // 22
case FilePipeInformation: // 23
case FilePipeLocalInformation: // 24
case FilePipeRemoteInformation: // 25
case FileMailslotQueryInformation: // 26
case FileMailslotSetInformation: // 27
case FileCompressionInformation: // 28
case FileObjectIdInformation: // 29
case FileCompletionInformation: // 30
case FileMoveClusterInformation: // 31
case FileQuotaInformation: // 32
case FileReparsePointInformation: // 33
case FileNetworkOpenInformation: // 34
case FileAttributeTagInformation: // 35
case FileTrackingInformation: // 36
case FileIdBothDirectoryInformation: // 37
case FileIdFullDirectoryInformation: // 38
case FileValidDataLengthInformation: // 39
case FileShortNameInformation: // 40
case FileDispositionInformationEx: // 64
goto SimRepPreSetInformationCleanup;
default:
//
// It is risky to pass through information classes that we don't
// know about. Try to catch new or invalid classes in testing.
//
NT_ASSERTMSG("SimRep passing through unknown information class\n", FALSE);
goto SimRepPreSetInformationCleanup;
}
#pragma warning( pop )
//
// When this filter is configured to remap renames and hardlinks we need
// to ensure other filters see a consistent destination for the
// operation. The FileName buffer will not match the actual rename path
// when a reparse is involved and if lower filters pass it to
// FltGetDestinationFileNameInformation they will get back the wrong
// destination. To fix this we'll need to munge the FileName buffer
// explicitly.
//
// The reason FltGetDestinationFileNameInformation gives the correct
// destination here is because we send it to ourselves (the current
// provider) and, as a name provider, our filter will get the creates
// issued for the parent directory name normalization and perform the
// reparse.
//
status = FltGetDestinationFileNameInformation( FltObjects->Instance,
FltObjects->FileObject,
setInfo.RootDirectory,
setInfo.FileName,
setInfo.FileNameLength,
FLT_FILE_NAME_REQUEST_FROM_CURRENT_PROVIDER | FLT_FILE_NAME_OPENED | FLT_FILE_NAME_QUERY_DEFAULT,
&nameInfo );
if (!NT_SUCCESS( status )) {
DebugTrace( DEBUG_TRACE_RENAME_REDIRECTION_OPERATIONS | DEBUG_TRACE_ERROR,
("[SimRep]: SimRepPreSetInformation -> Failed to get destination filename information (Cbd = %p, FileObject = %p)\n",
Cbd,
FltObjects->FileObject) );
goto SimRepPreSetInformationCleanup;
}
status = FltParseFileNameInformation( nameInfo );
if (!NT_SUCCESS( status )) {
goto SimRepPreSetInformationCleanup;
}
//
// Stream operations are already consistent regardless of whether the file
// is redirected so there is nothing to do.
//
if (nameInfo->Stream.Length != 0) {
goto SimRepPreSetInformationCleanup;
}
//
// If the operation destion overlaps the new mapping get a new filename
// string to send in the request.
//
status = SimRepMungeName( nameInfo,
&Globals.Mapping.NewName,
&Globals.Mapping.NewName,
!FlagOn( FltObjects->FileObject->Flags, FO_OPENED_CASE_SENSITIVE ),
FALSE,
&newFileName );
if (status == STATUS_NOT_FOUND) {
//
// If the operation destination overlaps the old mapping exactly, get
// a new filename string munged with the new mapping to send in the
// request. This is a special case where our name provider will not
// perform the reparse during name resolution because the parent
// directories don't overlap the mapping.
//
status = SimRepMungeName( nameInfo,
&Globals.Mapping.OldName,
&Globals.Mapping.NewName,
!FlagOn( FltObjects->FileObject->Flags, FO_OPENED_CASE_SENSITIVE ),
TRUE,
&newFileName );
}
if (!NT_SUCCESS( status )) {
//
// The rename doesn't overlap the mapping at all. No need to munge
//
if (status == STATUS_NOT_FOUND) {
status = STATUS_SUCCESS;
}
goto SimRepPreSetInformationCleanup;
}
//
// Explicitly set the munged the name in the set information structure so
// lower filters who see this operation will see the correct
// destination from FLT_GET_DESTINATION_FILE_NAME_INFORMATION.
//
if ((fileInfoClass == FileRenameInformation) ||
(fileInfoClass == FileRenameInformationEx)) {
bufferLength = FIELD_OFFSET( FILE_RENAME_INFORMATION, FileName ) + newFileName.Length;
buffer = ExAllocatePoolZero( PagedPool, bufferLength, SIMREP_STRING_TAG );
if (buffer == NULL) {
status = STATUS_INSUFFICIENT_RESOURCES;
goto SimRepPreSetInformationCleanup;
}
newRenameInfo = (PFILE_RENAME_INFORMATION)buffer;
newRenameInfo->Flags = renameInfo->Flags;
newRenameInfo->RootDirectory = NULL;
newRenameInfo->FileNameLength = newFileName.Length;
RtlCopyMemory( &newRenameInfo->FileName, newFileName.Buffer, newFileName.Length );
} else if (fileInfoClass == FileLinkInformation) {
bufferLength = FIELD_OFFSET( FILE_LINK_INFORMATION, FileName ) + newFileName.Length;
buffer = ExAllocatePoolZero( PagedPool, bufferLength, SIMREP_STRING_TAG );
if (buffer == NULL) {
status = STATUS_INSUFFICIENT_RESOURCES;
goto SimRepPreSetInformationCleanup;
}
newLinkInfo = (PFILE_LINK_INFORMATION)buffer;
newLinkInfo->ReplaceIfExists = linkInfo->ReplaceIfExists;
newLinkInfo->RootDirectory = NULL;
newLinkInfo->FileNameLength = newFileName.Length;
RtlCopyMemory( &newLinkInfo->FileName, newFileName.Buffer, newFileName.Length );
}
status = FltSetInformationFile( FltObjects->Instance,
FltObjects->FileObject,
buffer,
bufferLength,
fileInfoClass );
if (!NT_SUCCESS( status )) {
DebugTrace( DEBUG_TRACE_RENAME_REDIRECTION_OPERATIONS | DEBUG_TRACE_ERROR,
("[SimRep]: SimRepPreSetInformation -> Failed sending FltSetInformationFile (Cbd = %p, FileObject = %p)\n",
Cbd,
FltObjects->FileObject) );
goto SimRepPreSetInformationCleanup;
}
Cbd->IoStatus.Status = status;
returnStatus = FLT_PREOP_COMPLETE;
SimRepPreSetInformationCleanup:
if (nameInfo) {
FltReleaseFileNameInformation( nameInfo );
}
if (buffer) {
ExFreePoolWithTag( buffer, SIMREP_STRING_TAG );
}
SimRepFreeUnicodeString( &newFileName );
if (!NT_SUCCESS( status )) {
DebugTrace( DEBUG_TRACE_ERROR,
("[SimRep]: SimRepSetInformation -> Failed with status 0x%x \n",
status) );
Cbd->IoStatus.Status = status;
returnStatus = FLT_PREOP_COMPLETE;
}
return returnStatus;
}
//
// Support Routines
//
_When_(return==0, _Post_satisfies_(String->Buffer != NULL))
NTSTATUS
SimRepAllocateUnicodeString (
_Inout_ PUNICODE_STRING String
)
/*++
Routine Description:
This routine allocates a unicode string
Arguments:
Size - the size in bytes needed for the string buffer
String - supplies the size of the string to be allocated in the MaximumLength field
return the unicode string
Return Value:
STATUS_SUCCESS - success
STATUS_INSUFFICIENT_RESOURCES - failure
--*/
{
PAGED_CODE();
String->Buffer = ExAllocatePoolZero( NonPagedPool,
String->MaximumLength,
SIMREP_STRING_TAG );
if (String->Buffer == NULL) {
DebugTrace( DEBUG_TRACE_ERROR,
("[SimRep]: Failed to allocate unicode string of size 0x%x\n",
String->MaximumLength) );
return STATUS_INSUFFICIENT_RESOURCES;
}
String->Length = 0;
return STATUS_SUCCESS;
}
VOID
SimRepFreeUnicodeString (
_Inout_ PUNICODE_STRING String
)
/*++
Routine Description:
This routine frees a unicode string
Arguments:
String - supplies the string to be freed
Return Value:
None
--*/
{
PAGED_CODE();
if (String->Buffer) {
ExFreePoolWithTag( String->Buffer,
SIMREP_STRING_TAG );
String->Buffer = NULL;
}
String->Length = String->MaximumLength = 0;
String->Buffer = NULL;
}
NTSTATUS
SimRepReplaceFileObjectName (
_In_ PFILE_OBJECT FileObject,
_In_reads_bytes_(FileNameLength) PWSTR NewFileName,
_In_ USHORT FileNameLength
)
/*++
Routine Description:
This routine is used to replace a file object's name
with a provided name. This should only be called if
IoReplaceFileObjectName is not on the system.
If this function is used and verifier is enabled
the filter will fail to unload due to a false
positive on the leaked pool test.
Arguments:
FileObject - Pointer to file object whose name is to be replaced.
NewFileName - Pointer to buffer containing the new name.
FileNameLength - Length of the new name in bytes.
Return Value:
STATUS_INSUFFICIENT_RESOURCES - No memory to allocate the new buffer.
STATUS_SUCCESS otherwise.
--*/
{
PWSTR buffer;
PUNICODE_STRING fileName;
USHORT newMaxLength;
PAGED_CODE();
fileName = &FileObject->FileName;
//
// If the new name fits inside the current buffer we simply copy it over
// instead of allocating a new buffer (and keep the MaximumLength value
// the same).
//
if (FileNameLength <= fileName->MaximumLength) {
RtlZeroMemory(fileName->Buffer, fileName->MaximumLength);
goto CopyAndReturn;
}
//
// Use an optimal buffer size
//
newMaxLength = FileNameLength;
buffer = ExAllocatePoolZero( PagedPool,
newMaxLength,
SIMREP_STRING_TAG );
if (!buffer) {
return STATUS_INSUFFICIENT_RESOURCES;
}
if (fileName->Buffer != NULL) {
ExFreePool(fileName->Buffer);
}
fileName->Buffer = buffer;
fileName->MaximumLength = newMaxLength;
CopyAndReturn:
fileName->Length = FileNameLength;
RtlCopyMemory(fileName->Buffer, NewFileName, FileNameLength);
return STATUS_SUCCESS;
}
NTSTATUS
SimRepMungeName(
_In_ PFLT_FILE_NAME_INFORMATION NameInfo,
_In_ PUNICODE_STRING SubPath,
_In_ PUNICODE_STRING NewSubPath,
_In_ BOOLEAN IgnoreCase,
_In_ BOOLEAN ExactMatch,
_Out_ PUNICODE_STRING MungedPath
)
/*++
Routine Description:
This routine will create a new path by munginging a new subpath
over and existing subpath.
Arguments:
NameInfo - Pointer to the name information for the file.
SubPath - The path to munge.
IgnoreCase - If TRUE do a case insenstive comparison.
ExactMatch - If TRUE only proceed if the whole path will be replaced
MungedPath - A unicode string to received the munged path created. The
buffer of the string will be allocated in this function.
Return Value:
STATUS_SUCCESS - the path was successfully munged
STATUS_NOT_FOUND - the SubPath was not found or is not an exact match
An appropriate NTSTATUS error otherwise.
--*/
{
NTSTATUS status = STATUS_NOT_FOUND;
BOOLEAN match;
BOOLEAN exactMatch;
USHORT length;
PAGED_CODE();
match = SimRepCompareMapping( NameInfo, SubPath, IgnoreCase, &exactMatch );
if (match) {
if (ExactMatch && !exactMatch) {
goto SimRepMungeNameCleanup;
}
NT_ASSERT( NameInfo->Name.Length >= SubPath->Length );
length = NameInfo->Name.Length - SubPath->Length + NewSubPath->Length;
RtlInitUnicodeString( MungedPath, NULL );
MungedPath->MaximumLength = (USHORT)length;
status = SimRepAllocateUnicodeString( MungedPath );
if (!NT_SUCCESS( status )) {
goto SimRepMungeNameCleanup;
}
//
// Copy the volume portion of the name (part of the name preceding the matching part)
//
RtlCopyUnicodeString( MungedPath, &NameInfo->Volume );
//
// Copy the new file name in place of the matching part of the name
//
status = RtlAppendUnicodeStringToString( MungedPath, NewSubPath );
NT_ASSERT( NT_SUCCESS( status ) );
//
// Copy the portion of the name following the matching part of the name
//
RtlCopyMemory( Add2Ptr( MungedPath->Buffer, NameInfo->Volume.Length + NewSubPath->Length ),
Add2Ptr( NameInfo->Name.Buffer, NameInfo->Volume.Length + SubPath->Length ),
NameInfo->Name.Length - NameInfo->Volume.Length - SubPath->Length );
//
// Compute the final length of the new name
//
MungedPath->Length = length;
}
SimRepMungeNameCleanup:
return status;
}
BOOLEAN
SimRepCompareMapping(
_In_ PFLT_FILE_NAME_INFORMATION NameInfo,
_In_ PUNICODE_STRING MappingPath,
_In_ BOOLEAN IgnoreCase,
_Out_opt_ PBOOLEAN ExactMatch
)
/*++
Routine Description:
This routine will compare the file specified by the
name information structure to the given mapping path
to determine if the file is the mapping path itself
or a child of the mapping path.
Arguments:
NameInfo - Pointer to the name information for the file.
MappingPath - The mapping path to compare against.
IgnoreCase - If TRUE do a case insenstive comparison.
ExactMatch - If supplied receives TRUE if the name exactly
matches the mapping path.
Return Value:
TRUE - the file matches the mapping path
FALSE - the file is not in the mapping path
--*/
{
UNICODE_STRING fileName;
BOOLEAN match;
BOOLEAN exactMatch;
PAGED_CODE();
//
// The NameInfo parameter is assumed to have been parsed
//
NT_ASSERT (FlagOn(NameInfo->NamesParsed, FLTFL_FILE_NAME_PARSED_FINAL_COMPONENT) &&
FlagOn(NameInfo->NamesParsed, FLTFL_FILE_NAME_PARSED_EXTENSION) &&
FlagOn(NameInfo->NamesParsed, FLTFL_FILE_NAME_PARSED_STREAM) &&
FlagOn(NameInfo->NamesParsed, FLTFL_FILE_NAME_PARSED_PARENT_DIR));
//
// Point filename to the name of the file, excluding the name of the volume
//
NT_ASSERT( NameInfo->Name.Buffer == NameInfo->Volume.Buffer );
NT_ASSERT( NameInfo->Name.Length >= NameInfo->Volume.Length);
match = FALSE;
exactMatch = FALSE;
fileName.Buffer = Add2Ptr( NameInfo->Name.Buffer, NameInfo->Volume.Length );
fileName.MaximumLength = NameInfo->Name.Length - NameInfo->Volume.Length;
fileName.Length = fileName.MaximumLength;
//
// Check if the filename matches this mapping entry (is the mapping
// entry itself or some child directory of the mapping entry)
//
if (RtlPrefixUnicodeString( MappingPath, &fileName, IgnoreCase )) {
if (fileName.Length == MappingPath->Length) {
//
// This path is the mapping itself
//
match = TRUE;
exactMatch = TRUE;
} else if (fileName.Buffer[(MappingPath->Length/sizeof( WCHAR ))] == OBJ_NAME_PATH_SEPARATOR) {
//
// This path is a child of the mapping
//
match = TRUE;
}
//
// No match here means the path simply overlaps the mapping like
// \a\b\c overlaps \a\b\cd.txt
//
}
if (ARGUMENT_PRESENT( ExactMatch )) {
*ExactMatch = exactMatch;
}
return match;
}
//
// In order to remap renames and hard links correctly SimRep needs
// to be called as part of name resolution. To achieve this SimRep
// must be a name provider, albeit a simple "pass through" provider.
// SimRep is is only demonstrating how to simulate reparse points,
// not how to virtualize a namespace. Hence the name provider does
// not munge the names but simply passes the name queries through.
//
NTSTATUS
SimRepGenerateFileName (
_In_ PFLT_INSTANCE Instance,
_In_ PFILE_OBJECT FileObject,
_When_(FileObject->FsContext != NULL, _In_opt_)
_When_(FileObject->FsContext == NULL, _In_)
PFLT_CALLBACK_DATA Cbd,
_In_ FLT_FILE_NAME_OPTIONS NameOptions,
_Out_ PBOOLEAN CacheFileNameInformation,
_Inout_ PFLT_NAME_CONTROL FileName
)
/*++
Routine Description:
This routine generates a file name of the type specified in NameFormat
for the specified file object.
Arguments:
Instance - Opaque instance pointer for the minifilter driver instance that
this callback routine is registered for.
FileObject - The fileobject for which the name is being requested.
Cbd - If non-NULL, the CallbackData structure defining the operation
we are in the midst of processing when this name is queried.
NameOptions - value that specifies the name format, query method, and flags
for this file name information query
CacheFileNameInformation - A pointer to a Boolean value specifying whether
this name can be cached.
FileName - A pointer to a filter manager-allocated FLT_NAME_CONTROL
structure to receive the file name on output
Return Value:
Returns STATUS_SUCCESS if a name could be returned, or the appropriate
error otherwise.
--*/
{
PFLT_FILE_NAME_INFORMATION userFileNameInfo = NULL;
PUNICODE_STRING userFileName;
NTSTATUS status = STATUS_SUCCESS;
PAGED_CODE();
//
// Clear FLT_FILE_NAME_REQUEST_FROM_CURRENT_PROVIDER from the name options
// We pass the same name options when we issue a name query to satisfy this
// name query. We want that name query to be targeted below simrep.sys and
// not recurse into simrep.sys
//
ClearFlag( NameOptions, FLT_FILE_NAME_REQUEST_FROM_CURRENT_PROVIDER );
if (FileObject->FsContext == NULL) {
//
// This file object has not yet been opened. We will query the filter
// manager for the name and return that name. We must use the original
// NameOptions we received in the query. If we were to swallow flags
// such as FLT_FILE_NAME_QUERY_FILESYSTEM_ONLY or
// FLT_FILE_NAME_DO_NOT_CACHE we could corrupt the name cache.
//
status = FltGetFileNameInformation( Cbd,
NameOptions,
&userFileNameInfo );
if (!NT_SUCCESS( status )) {
goto SimRepGenerateFileNameCleanup;
}
userFileName = &userFileNameInfo->Name;
} else {
//
// The file has been opened. If the call is not in the context of an IO
// operation (we don't have a callback data), we have to get the
// filename with FltGetFilenameInformationUnsafe using the fileobject.
// Note, the only way we won't have a callback is if someone called
// FltGetFileNameInformationUnsafe already.
//
if (ARGUMENT_PRESENT( Cbd )) {
status = FltGetFileNameInformation( Cbd,
NameOptions,
&userFileNameInfo );
} else {
status = FltGetFileNameInformationUnsafe( FileObject,
Instance,
NameOptions,
&userFileNameInfo );
}
if (!NT_SUCCESS( status )) {
goto SimRepGenerateFileNameCleanup;
}
userFileName = &userFileNameInfo->Name;
}
status = FltCheckAndGrowNameControl( FileName,
userFileName->Length );
if (!NT_SUCCESS( status )) {
goto SimRepGenerateFileNameCleanup;
}
RtlCopyUnicodeString( &FileName->Name, userFileName );
//
// If the file object is unopened then the name of the stream represented by
// the file object may change from pre-create to post-create.
// For example, the name being opened could actually be a symbolic link
//
*CacheFileNameInformation = (FileObject->FsContext != NULL);
SimRepGenerateFileNameCleanup:
if (userFileNameInfo != NULL) {
FltReleaseFileNameInformation( userFileNameInfo );
}
if (!NT_SUCCESS( status )) {
DebugTrace( DEBUG_TRACE_NAME_OPERATIONS | DEBUG_TRACE_ERROR,
("SimRepGenerateFileName: failed %x\n",
status) );
}
return status;
}
NTSTATUS
SimRepNormalizeNameComponent (
_In_ PFLT_INSTANCE Instance,
_In_ PCUNICODE_STRING ParentDirectory,
_In_ USHORT DeviceNameLength,
_In_ PCUNICODE_STRING Component,
_Out_writes_bytes_(ExpandComponentNameLength) PFILE_NAMES_INFORMATION ExpandComponentName,
_In_ ULONG ExpandComponentNameLength,
_In_ FLT_NORMALIZE_NAME_FLAGS Flags,
_Inout_ PVOID *NormalizationContext
)
/*++
Routine Description:
This routine normalizes, converts to a long name if needed, a name component.
Arguments:
Instance - Opaque instance pointer for the minifilter driver instance that
this callback routine is registered for.
ParentDirectory - Pointer to a UNICODE_STRING structure that contains the
name of the parent directory for this name component.
VolumeNameLength - Length, in bytes, of the parent directory name that is
stored in the structure that the ParentDirectory parameter points to.
Component - Pointer to a UNICODE_STRING structure that contains the name
component to be expanded.
ExpandComponentName - Pointer to a FILE_NAMES_INFORMATION structure that
receives the expanded (normalized) file name information for the name component.
ExpandComponentNameLength - Length, in bytes, of the buffer that the
ExpandComponentName parameter points to.
Flags - Name normalization flags.
NormalizationContext - Pointer to minifilter driver-provided context
information to be passed in any subsequent calls to this callback routine
that are made to normalize the remaining components in the same file name
path.
Return Value:
Returns STATUS_SUCCESS if a name could be returned, or the appropriate
error otherwise.
--*/
{
NTSTATUS status;
HANDLE directoryHandle = NULL;
PFILE_OBJECT directoryFileObject = NULL;
OBJECT_ATTRIBUTES objAttributes;
IO_STATUS_BLOCK ioStatusBlock;
BOOLEAN ignoreCase = !BooleanFlagOn( Flags,
FLTFL_NORMALIZE_NAME_CASE_SENSITIVE );
UNREFERENCED_PARAMETER( NormalizationContext );
UNREFERENCED_PARAMETER( DeviceNameLength );
PAGED_CODE();
//
// Validate the buffer is big enough
//
if (ExpandComponentNameLength < sizeof(FILE_NAMES_INFORMATION)) {
return STATUS_INVALID_PARAMETER;
}
InitializeObjectAttributes( &objAttributes,
(PUNICODE_STRING)ParentDirectory,
OBJ_KERNEL_HANDLE
| (ignoreCase ? OBJ_CASE_INSENSITIVE : 0),
NULL,
NULL );
status = FltCreateFile( Globals.Filter,
Instance,
&directoryHandle,
FILE_LIST_DIRECTORY | SYNCHRONIZE, // DesiredAccess
&objAttributes,
&ioStatusBlock,
NULL, // AllocationSize
FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_NORMAL, // FileAttributes
FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE, // ShareAccess
FILE_OPEN, // CreateDisposition
FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
| FILE_OPEN_FOR_BACKUP_INTENT, // CreateOptions
NULL, // EaBuffer
0, // EaLength
IO_IGNORE_SHARE_ACCESS_CHECK ); // Flags
if (!NT_SUCCESS( status )) {
goto SimRepNormalizeNameComponentCleanup;
}
status = ObReferenceObjectByHandle( directoryHandle,
FILE_LIST_DIRECTORY | SYNCHRONIZE, // DesiredAccess
*IoFileObjectType,
KernelMode,
&directoryFileObject,
NULL );
if (!NT_SUCCESS( status )) {
goto SimRepNormalizeNameComponentCleanup;
}
//
// Query the file entry to get the long name
//
status = SimRepQueryDirectoryFile( Instance,
directoryFileObject,
ExpandComponentName,
ExpandComponentNameLength,
FileNamesInformation,
TRUE, /* ReturnSingleEntry */
(PUNICODE_STRING)Component,
TRUE, /* restartScan */
NULL );
SimRepNormalizeNameComponentCleanup:
if (NULL != directoryHandle) {
FltClose( directoryHandle );
}
if (NULL != directoryFileObject) {
ObDereferenceObject( directoryFileObject );
}
return status;
}
#if SIMREP_VISTA
NTSTATUS
SimRepNormalizeNameComponentEx (
_In_ PFLT_INSTANCE Instance,
_In_ PFILE_OBJECT FileObject,
_In_ PCUNICODE_STRING ParentDirectory,
_In_ USHORT DeviceNameLength,
_In_ PCUNICODE_STRING Component,
_Out_writes_bytes_(ExpandComponentNameLength) PFILE_NAMES_INFORMATION ExpandComponentName,
_In_ ULONG ExpandComponentNameLength,
_In_ FLT_NORMALIZE_NAME_FLAGS Flags,
_Inout_ PVOID *NormalizationContext
)
/*++
Routine Description:
This routine normalizes, converts to a long name if needed, a name component.
Arguments:
Instance - Opaque instance pointer for the minifilter driver instance that
this callback routine is registered for.
FileObject - Pointer to the file object for the file whose name is being
requested or the file that is the target of the IRP_MJ_SET_INFORMATION
operation if the FLTFL_NORMALIZE_NAME_DESTINATION_FILE_NAME flag is set.
ee the Flags parameter below for more information.
ParentDirectory - Pointer to a UNICODE_STRING structure that contains the
name of the parent directory for this name component.
VolumeNameLength - Length, in bytes, of the parent directory name that is
stored in the structure that the ParentDirectory parameter points to.
Component - Pointer to a UNICODE_STRING structure that contains the name
component to be expanded.
ExpandComponentName - Pointer to a FILE_NAMES_INFORMATION structure that
receives the expanded (normalized) file name information for the name component.
ExpandComponentNameLength - Length, in bytes, of the buffer that the
ExpandComponentName parameter points to.
Flags - Name normalization flags.
NormalizationContext - Pointer to minifilter driver-provided context
information to be passed in any subsequent calls to this callback routine
that are made to normalize the remaining components in the same file name
path.
Return Value:
Returns STATUS_SUCCESS if a name could be returned, or the appropriate
error otherwise.
--*/
{
NTSTATUS status;
HANDLE directoryHandle = NULL;
PFILE_OBJECT directoryFileObject = NULL;
OBJECT_ATTRIBUTES objAttributes;
IO_STATUS_BLOCK ioStatusBlock;
BOOLEAN ignoreCase = !BooleanFlagOn( Flags,
FLTFL_NORMALIZE_NAME_CASE_SENSITIVE );
IO_DRIVER_CREATE_CONTEXT createContext;
TXN_PARAMETER_BLOCK txnBlock;
PTXN_PARAMETER_BLOCK originalTxnBlock;
UNREFERENCED_PARAMETER( NormalizationContext );
UNREFERENCED_PARAMETER( DeviceNameLength );
PAGED_CODE();
//
// Validate the buffer is big enough
//
if (ExpandComponentNameLength < sizeof(FILE_NAMES_INFORMATION)) {
return STATUS_INVALID_PARAMETER;
}
InitializeObjectAttributes( &objAttributes,
(PUNICODE_STRING)ParentDirectory,
OBJ_KERNEL_HANDLE
| (ignoreCase ? OBJ_CASE_INSENSITIVE : 0),
NULL,
NULL );
ASSERT( ARGUMENT_PRESENT( FileObject ) );
//
// On Vista and beyond, we need to query the normalized name in the context
// of the same transaction as the name query
//
IoInitializeDriverCreateContext( &createContext );
originalTxnBlock = IoGetTransactionParameterBlock( FileObject );
if (originalTxnBlock != NULL) {
//
// Do not propagate the miniversion for the parent open
// as directories don't have a miniversion.
//
txnBlock.Length = sizeof( txnBlock );
txnBlock.TransactionObject = originalTxnBlock->TransactionObject;
txnBlock.TxFsContext = TXF_MINIVERSION_DEFAULT_VIEW;
createContext.TxnParameters = &txnBlock;
}
status = FltCreateFileEx2( Globals.Filter,
Instance,
&directoryHandle,
&directoryFileObject,
FILE_LIST_DIRECTORY | SYNCHRONIZE, // DesiredAccess
&objAttributes,
&ioStatusBlock,
NULL, // AllocationSize
FILE_ATTRIBUTE_DIRECTORY
| FILE_ATTRIBUTE_NORMAL, // FileAttributes
FILE_SHARE_READ
| FILE_SHARE_WRITE
| FILE_SHARE_DELETE, // ShareAccess
FILE_OPEN, // CreateDisposition
FILE_DIRECTORY_FILE
| FILE_SYNCHRONOUS_IO_NONALERT
| FILE_OPEN_FOR_BACKUP_INTENT, // CreateOptions
NULL, // EaBuffer
0, // EaLength
IO_IGNORE_SHARE_ACCESS_CHECK, // Flags
&createContext );
if (!NT_SUCCESS( status )) {
goto SimRepNormalizeNameComponentExCleanup;
}
//
// Query the file entry to get the long name
//
status = SimRepQueryDirectoryFile( Instance,
directoryFileObject,
ExpandComponentName,
ExpandComponentNameLength,
FileNamesInformation,
TRUE, /* ReturnSingleEntry */
(PUNICODE_STRING)Component,
TRUE, /* restartScan */
NULL );
SimRepNormalizeNameComponentExCleanup:
if (NULL != directoryHandle) {
FltClose( directoryHandle );
}
if (NULL != directoryFileObject) {
ObDereferenceObject( directoryFileObject );
}
return status;
}
#endif
NTSTATUS
SimRepQueryDirectoryFile (
_In_ PFLT_INSTANCE Instance,
_In_ PFILE_OBJECT FileObject,
_Out_writes_bytes_(Length) PVOID FileInformationBuffer,
_In_ ULONG Length,
_In_ FILE_INFORMATION_CLASS FileInformationClass,
_In_ BOOLEAN ReturnSingleEntry,
_In_opt_ PUNICODE_STRING FileName,
_In_ BOOLEAN RestartScan,
_Out_opt_ PULONG LengthReturned
)
/*++
Routine Description:
This function is like ZwQueryDirectoryFile for filters
Arguments:
Instance - Supplies the Instance initiating this IO.
FileObject - Supplies the file object about which the requested
information should be changed.
FileInformation - Supplies a buffer containing the information which should
be changed on the file.
Length - Supplies the length, in bytes, of the FileInformation buffer.
FileInformationClass - Specifies the type of information which should be
changed about the file.
ReturnSingleEntry - If this parameter is TRUE, SimRepQueryDirectoryFile
returns only the first entry that is found.
FileName - An optional pointer to a caller-allocated Unicode string
containing the name of a file (or multiple files, if wildcards are used)
within the directory specified by FileHandle. This parameter is optional
and can be NULL.
RestartScan - Set to TRUE if the scan is to start at the first entry in
the directory. Set to FALSE if resuming the scan from a previous call.
Return Value:
The status returned is the final completion status of the operation.
--*/
{
PFLT_CALLBACK_DATA data;
NTSTATUS status;
PAGED_CODE();
if (Globals.QueryDirectoryFileFunction != NULL) {
return Globals.QueryDirectoryFileFunction( Instance,
FileObject,
FileInformationBuffer,
Length,
FileInformationClass,
ReturnSingleEntry,
FileName,
RestartScan,
LengthReturned );
}
//
// Customized FltQueryDirectoryFile if it is not exported from FltMgr.
//
status = FltAllocateCallbackData( Instance, FileObject, &data );
if (!NT_SUCCESS( status )) {
return status;
}
data->Iopb->MajorFunction = IRP_MJ_DIRECTORY_CONTROL;
data->Iopb->MinorFunction = IRP_MN_QUERY_DIRECTORY;
data->Iopb->Parameters.DirectoryControl.QueryDirectory.Length = Length;
data->Iopb->Parameters.DirectoryControl.QueryDirectory.FileName = FileName;
data->Iopb->Parameters.DirectoryControl.QueryDirectory.FileInformationClass = FileInformationClass;
data->Iopb->Parameters.DirectoryControl.QueryDirectory.FileIndex = 0;
data->Iopb->Parameters.DirectoryControl.QueryDirectory.DirectoryBuffer = FileInformationBuffer;
data->Iopb->Parameters.DirectoryControl.QueryDirectory.MdlAddress = NULL;
if (RestartScan) {
data->Iopb->OperationFlags |= SL_RESTART_SCAN;
}
if (ReturnSingleEntry) {
data->Iopb->OperationFlags |= SL_RETURN_SINGLE_ENTRY;
}
//
// Perform a synchronous operation.
//
FltPerformSynchronousIo( data );
status = data->IoStatus.Status;
if (ARGUMENT_PRESENT(LengthReturned) &&
NT_SUCCESS( status )) {
*LengthReturned = (ULONG) data->IoStatus.Information;
}
FltFreeCallbackData( data );
return status;
}
|