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
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
//! The reader: parse an ext4 filesystem back into inodes, directories, and file
//! contents.
//!
//! It reads filesystems other tools wrote, not only the ones this crate writes. Both
//! block mappings are followed to any depth — the extent tree, and the direct/indirect
//! map ext2 and ext3 use for every file; any inode size is honored, down to the 128-byte
//! inode that has no extended area at all; and every checksum is verified against the
//! object's own bytes, so a field an image carries and this crate does not model does not
//! read as corruption. [`Reader::lookup`] resolves a path against the image's own root,
//! following symbolic links as it goes.
//!
//! # Robustness and strictness
//!
//! Two properties are kept apart. *Robustness* is always on: every on-disk field is
//! bounds-checked and every fallible step returns a [`ReadError`] rather than panicking
//! or reading out of range, on any input — including one built to break it.
//!
//! *Conformance strictness* is a policy: a threshold over the [`Severity`] of the
//! [`Anomaly`] a deviation carries. [`ReadPolicy::Strict`], the default, is fatal at any
//! anomaly a conformant ext4 would not carry, so a strict read either yields the
//! filesystem the image describes or names the deviation that stopped it.
//!
//! [`ReadPolicy::Lenient`] moves that threshold above every severity, so nothing is
//! fatal. A whole-image [`scan`](Reader::scan) walks the superblock, every group
//! descriptor, and every in-use inode and its extent tree, collecting each deviation
//! as an [`Anomaly`] into a [`ScanReport`] instead of stopping at the first — the
//! forensic counterpart to a strict read. The report projects to JSON, SARIF, or a
//! human table, and [`ScanReport::has_fatal`] applies a policy's threshold back to what
//! the scan found.
//!
//! The handle opens over any [`Read`] + [`Seek`] source at an arbitrary byte offset,
//! so it reads a filesystem embedded in a larger device or partition image as
//! readily as a bare one. That embedding is why the filesystem's own block count bounds
//! every reference before it is followed, separately from the source's length: the bytes
//! after the final block belong to whatever the image sits in, and a reference reaching
//! them is out of range however much source there is behind it.
use std::collections::HashSet;
use std::io::{Read, Seek, SeekFrom, Write};
use crate::acl::Acl;
use crate::csum::{Checksummer, Crc32c};
use crate::extent::{ExtentNode, MAX_EXTENT_DEPTH, parse_node, tail_offset};
use crate::feature::{FeatureSet, Incompat, LARGE_FILE_MIN_SIZE, Profile};
use crate::fidelity::Synthesis;
use crate::finding::{Family, Finding, Findings, Severity};
use crate::io::{offset_of, read_exact_at};
use crate::model::ROOT_INO;
use crate::ondisk::{
BG_BLOCK_UNINIT, BG_INODE_UNINIT, DIR_TAIL_LEN, DX_CHECKSUM_OFFSET, DX_ENTRY_LEN,
DX_NODE_COUNT_OFFSET, DX_ROOT_COUNT_OFFSET, DX_TAIL_LEN, DirEntry, EXTENT_ENTRY_SIZE,
EXTENT_TAIL_LEN, FileType, GOOD_OLD_FIRST_INODE, GOOD_OLD_INODE_SIZE, GroupDescriptor, Inode,
InodeFlags, ParseError, SuperBlock, XATTR_CHECKSUM_OFFSET, decode_device, dx_tail_offset,
get_u16, get_u32, orphan_entries_len, parse_block, parse_inline, put_u16, read_dx_entries,
read_dx_root_info, read_orphan_tail, superblock_checksum,
};
use crate::path::is_hostile_entry;
// The per-object checksum recipes, aliased because the local binding for a checksummer
// is itself called `csum`: `recipe::inode(csum, ..)` reads as what it is.
use crate::csum as recipe;
use crate::policy::{Limits, ReadPolicy};
use crate::source::Metadata;
use crate::tree::{Attributes, FsTree, NodeKind, TreeEntry, TreeError};
use crate::xattr::Xattr;
/// The `incompat` features whose on-disk layout this reader interprets. Every one is a
/// format this crate reads and, where relevant, writes: typed directory entries, extent
/// trees, 64-bit geometry, flex block groups, and a superblock-stored checksum seed. A
/// set `incompat` bit outside this mask means the reader cannot be certain it reads the
/// image correctly — an unknown extension, or `meta_bg`, whose distributed group
/// descriptors this reader's contiguous-table parsing does not follow. The `incompat`
/// word is the one an implementation must refuse when it does not recognize a bit, so
/// this mask is what a strict read enforces and a scan reports against.
const SUPPORTED_INCOMPAT: u32 = Incompat::FILETYPE.bits()
| Incompat::EXTENTS.bits()
| Incompat::SIXTY_FOUR_BIT.bits()
| Incompat::FLEX_BG.bits()
| Incompat::CSUM_SEED.bits();
/// The `incompat` bits set in `incompat` that this reader does not interpret: the word
/// with every [`SUPPORTED_INCOMPAT`] bit cleared. Zero means every feature the image
/// advertises is one the reader follows.
fn unsupported_incompat(incompat: Incompat) -> u32 {
incompat.bits() & !SUPPORTED_INCOMPAT
}
/// The `incompat` features ext4 defines that [`Incompat`] does not model, paired with
/// the names `dumpe2fs` and `tune2fs` print for them, so a report can be read straight
/// against those tools' output.
///
/// Naming a bit here is diagnosis, not support. What the reader will follow is
/// [`SUPPORTED_INCOMPAT`] and nothing else, and what a caller may ask a formatter for
/// is what [`Incompat`] models and nothing else — a name in this table reaches neither.
/// It exists so that an image the reader turns away is turned away by name.
const UNMODELLED_INCOMPAT: &[(&str, u32)] = &[
("compression", 0x0000_0001),
("needs_recovery", 0x0000_0004),
("journal_dev", 0x0000_0008),
("mmp", 0x0000_0100),
("ea_inode", 0x0000_0400),
("dirdata", 0x0000_1000),
("large_dir", 0x0000_4000),
("inline_data", 0x0000_8000),
("encrypt", 0x0001_0000),
("casefold", 0x0002_0000),
];
/// Describe the unsupported `incompat` bits for an anomaly's detail: the on-disk name
/// of each feature, in ascending bit order, and any bit ext4 itself does not define
/// gathered into one hexadecimal word. All of them are what the reader cannot vouch for
/// having read correctly.
fn describe_unsupported_incompat(bits: u32) -> String {
let mut parts: Vec<String> = Vec::new();
let mut undefined = 0u32;
for i in 0..u32::BITS {
let bit = 1u32 << i;
if bits & bit == 0 {
continue;
}
// A bit the feature word models carries its name there; the rest of ext4's
// vocabulary is named by the table above. Anything in neither belongs to no
// feature ext4 defines, so there is no name to give it.
if let Some(name) = Incompat::from_bits(bit).names().first() {
parts.push((*name).to_string());
} else if let Some((name, _)) = UNMODELLED_INCOMPAT.iter().find(|(_, b)| *b == bit) {
parts.push((*name).to_string());
} else {
undefined |= bit;
}
}
if undefined != 0 {
parts.push(format!("{undefined:#010x}"));
}
format!(
"unsupported incompatible feature(s) {}: the reader cannot be certain it \
interprets the on-disk format correctly",
parts.join(", ")
)
}
/// Direct block pointers in the classic map: `i_block[0..12]` name data blocks
/// outright, and the three words after them are one, two, and three levels of
/// indirection.
const DIRECT_BLOCKS: usize = 12;
/// Levels of indirection the classic block map reaches: single, double, and triple.
/// This bounds [`Reader::walk_indirect_window`]'s recursion by construction.
const INDIRECT_LEVELS: u32 = 3;
/// The most logical blocks one mapping window covers, which is what makes streaming a
/// file a fixed cost: the map for a window is eight bytes per block, so 4096 blocks is
/// 32 KiB of map covering 16 MiB of file at a 4 KiB block size. A file of any length is
/// read as a succession of these rather than as one map of everything its size claims.
const MAP_WINDOW_BLOCKS: usize = 4096;
/// The number of logical blocks a file can address: a logical block number is 32
/// bits wide in both the extent (`ee_block`) and classic maps, so a file spans at
/// most 2^32 blocks whatever its `i_size` claims. This is the ceiling a logical
/// offset is bounded by, distinct from the physical block count a block *number* is
/// bounded by — the distinction a sparse file turns on, where a high logical offset
/// is valid over a filesystem far smaller than the offset.
const MAX_LOGICAL_BLOCKS: u64 = 1 << 32;
use crate::policy::MAX_PATH;
/// The fewest bytes one directory record can occupy: the eight-byte header plus a name of
/// at least one byte, rounded up to the four-byte alignment every `rec_len` obeys.
///
/// This is [`min_rec_len(1)`](crate::ondisk::min_rec_len), which is where that rule is
/// spelled; the value appears again here because it divides a `u64` and the rule's
/// function is not `const`. A unit test holds the two equal, so a change to the record
/// header or to the alignment cannot move one without moving the other.
///
/// Every name a well-formed filesystem holds costs at least this much of its own blocks,
/// which is what turns the source's length into a bound on how many names it can
/// describe — and what makes that bound one no well-formed image reaches.
pub const MIN_DIRENT_LEN: u64 = 12;
/// Whether an inode is a directory (`S_IFDIR`).
fn is_dir(inode: &Inode) -> bool {
inode.mode & 0o170000 == 0o040000
}
/// Whether an inode is a symbolic link (`S_IFLNK`).
fn is_symlink(inode: &Inode) -> bool {
inode.mode & 0o170000 == 0o120000
}
/// Whether an inode is a regular file (`S_IFREG`). The `large_file` feature is scoped to
/// these alone, which is the same scope the kernel and `e2fsck` apply it in.
fn is_regular(inode: &Inode) -> bool {
inode.mode & 0o170000 == 0o100000
}
/// Whether a symlink stores its target inline in the inode's `i_block` area rather
/// than in a data block.
///
/// The distinction is block usage, not target length: a symlink that owns no data
/// block is a *fast* one, whatever its size field says. `i_blocks` counts 512-byte
/// sectors, and an external attribute block is charged to it as well, so that block —
/// one filesystem block's worth of sectors, present exactly when `i_file_acl` names
/// one — is discounted first. What remains is the storage the target itself occupies,
/// and a fast symlink occupies none.
///
/// Keying on the target length instead would read a short target held in a data block
/// straight out of the block area, returning a block pointer as a filename.
fn is_fast_symlink(inode: &Inode, block_size: usize) -> bool {
let ea_blocks = if inode.file_acl != 0 {
(block_size / 512) as u64
} else {
0
};
inode.blocks == ea_blocks
}
/// Whether an inode's `i_block` area maps data blocks at all.
///
/// Only a regular file, a directory, and a symlink whose target lives in a data block
/// have a mapping there. A *fast* symlink stores its target string in that area, and a
/// device node stores its major and minor numbers — so a reader that walks every
/// inode's `i_block` as a block map reads a filename, or a device number, as a list of
/// block numbers. An inode with no data (a FIFO, a socket, an unused reserved inode)
/// maps nothing.
fn maps_data(inode: &Inode, block_size: usize) -> bool {
match inode.mode & 0o170000 {
0o100000 | 0o040000 => true, // regular file, directory
0o120000 => !is_fast_symlink(inode, block_size), // a slow symlink only
_ => false,
}
}
/// The subsystem a deviation was found in.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub enum Category {
/// The primary or a backup superblock.
Superblock,
/// The group descriptor table.
GroupDescriptor,
/// A block or inode bitmap.
Bitmap,
/// An inode.
Inode,
/// An inode's extent tree.
ExtentTree,
/// A directory block or its hash-tree index.
Directory,
/// An extended-attribute block or inline set.
Xattr,
/// The journal.
Journal,
/// The orphan file, which records the inodes awaiting deletion.
Orphan,
}
impl Category {
/// The lowercase name of this subsystem, for a rendered report.
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Category::Superblock => "superblock",
Category::GroupDescriptor => "group descriptor",
Category::Bitmap => "bitmap",
Category::Inode => "inode",
Category::ExtentTree => "extent tree",
Category::Directory => "directory",
Category::Xattr => "xattr",
Category::Journal => "journal",
Category::Orphan => "orphan",
}
}
}
crate::naming::serialize_as_name!(Category);
/// Where in the image a deviation sits. Every field is optional: a deviation carries
/// only the coordinates that locate it.
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Location {
/// Block number, when the deviation is block-addressed.
pub block: Option<u64>,
/// Group number, when the deviation is group-addressed.
pub group: Option<u32>,
/// Inode number, when the deviation is inode-addressed.
pub inode: Option<u32>,
}
impl Location {
/// This location with any coordinate `other` carries that this one does not.
///
/// A deviation knows where it is in its own terms — an out-of-range block reference
/// knows its block — and the walk that met it knows the rest. Merging rather than
/// replacing is what keeps the more specific of the two.
#[must_use]
pub const fn or(self, other: Self) -> Self {
Self {
block: match self.block {
Some(b) => Some(b),
None => other.block,
},
group: match self.group {
Some(g) => Some(g),
None => other.group,
},
inode: match self.inode {
Some(i) => Some(i),
None => other.inode,
},
}
}
/// A location naming an inode and nothing else.
#[must_use]
pub const fn at_inode(inode: u32) -> Self {
Self {
block: None,
group: None,
inode: Some(inode),
}
}
/// A location naming a group and nothing else.
#[must_use]
pub const fn at_group(group: u32) -> Self {
Self {
block: None,
group: Some(group),
inode: None,
}
}
}
/// A typed deviation from what this crate would emit, carrying its severity, the
/// subsystem it was found in, where it sits, and a human description.
///
/// This is ext's structured value, and it stays ext's: the subsystem is a [`Category`]
/// rather than a word, and the place is a group, an inode, and a block rather than a byte
/// offset, because those are what a consumer reasoning about an ext4 image acts on.
///
/// Rendering goes through [`to_finding`](Self::to_finding), which projects into the
/// crate's [`Finding`] — the frame every family shares and every renderer consumes.
#[derive(Clone, PartialEq, Eq, Debug)]
#[non_exhaustive]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Anomaly {
/// How serious the deviation is.
pub severity: Severity,
/// The subsystem it was found in.
pub category: Category,
/// Where it sits in the image.
pub location: Location,
/// A human-readable description.
pub detail: String,
}
impl crate::Deviation for Anomaly {
fn severity(&self) -> Severity {
self.severity
}
/// The block size is the addressing unit an ext location is stated in.
fn to_finding(&self, unit: u32) -> Finding {
Anomaly::to_finding(self, unit)
}
}
impl Anomaly {
/// Project this anomaly into the crate's family-agnostic [`Finding`], resolving a
/// block-addressed location to the byte offset that block sits at under `block_size`.
///
/// The coordinates carry ext's own words for what they address, outermost first —
/// `group`, then `inode`, then `block` — which is the order a person reads a location
/// in. A group or an inode number is not converted to an offset, because where each
/// sits depends on the layout rather than on the number alone, so an anomaly located
/// only by one of those carries no offset.
#[must_use]
pub fn to_finding(&self, block_size: u32) -> Finding {
// Destructured exhaustively on purpose: a field added to `Anomaly` is a compile
// error here, which forces a decision about what the projection carries rather than
// letting a new fact about a finding go silently unreported.
let Self {
severity,
category,
location,
detail,
} = self;
let Location {
block,
group,
inode,
} = *location;
crate::finding::project(
*severity,
Family::Ext,
category.as_str(),
&[
("group", group.map(u64::from)),
("inode", inode.map(u64::from)),
("block", block),
],
// The block is what addresses bytes. A group or an inode number does not: where
// each sits depends on the layout rather than on the number alone.
block,
block_size,
detail,
)
}
}
/// Hold one checksummed object to the word it stores.
///
/// Every object under `metadata_csum` has its own recipe — which seed, which identity words
/// fold into it, which field participates as zero — and those stay where each object is
/// understood. What is the same for all of them is what happens once a value has been
/// recomputed: the two words are compared, and a disagreement names the object and its index
/// so a caller learns *which* group descriptor or *which* inode is self-inconsistent rather
/// than only that something is. Written once, so no object reports a mismatch differently
/// from the rest and none forgets to report one at all.
fn check_checksum(
object: &'static str,
index: u64,
stored: u32,
computed: u32,
) -> Result<(), ReadError> {
if stored == computed {
return Ok(());
}
Err(ReadError::ChecksumMismatch {
object,
index,
stored,
computed,
})
}
/// Project a [`ReadError`] to an [`Anomaly`] and stamp it with the inode it was found
/// under, keeping any more specific inode the error already carried.
fn anomaly_at_inode(err: &ReadError, ino: u32) -> Anomaly {
let mut a = err.anomaly();
a.location = a.location.or(Location::at_inode(ino));
a
}
/// Redirect an out-of-range *block* reference to the subsystem whose walk named it.
///
/// The bare projection of an `OutOfRange { what: "block" }` blames the superblock,
/// because a block number alone names no owning structure. A scanner that followed a
/// reference into that block does know the owner and threads it here, so a consumer
/// filtering by category sees the bad block filed under the extent tree, directory,
/// bitmap, or attribute block that pointed at it — not the one object the image
/// demonstrably got right. An error that carries its own subsystem (a parse or checksum
/// failure) keeps it, as does an out-of-range *inode* or *group*, which the inode and
/// group-descriptor subsystems already own.
fn redirect_block_ref(a: &mut Anomaly, err: &ReadError, category: Category) {
if matches!(err, ReadError::OutOfRange { what: "block", .. }) {
a.category = category;
}
}
/// Project an error found while walking `ino`'s block mapping — its extent tree, classic
/// block map, directory blocks, or external attribute block — filing an out-of-range
/// block against `category` and stamping the owning inode. Inode 0 is the null inode,
/// never a real one, so a fault with no known owner carries no inode stamp.
fn anomaly_in_mapping(err: &ReadError, ino: u32, category: Category) -> Anomaly {
let mut a = err.anomaly();
redirect_block_ref(&mut a, err, category);
if ino != 0 {
a.location = a.location.or(Location::at_inode(ino));
}
a
}
/// Project an error found while checking group `g`'s bitmaps, filing an out-of-range
/// bitmap block against `category` — the bitmap subsystem the descriptor named it for —
/// and stamping the owning group. A descriptor that cannot be reached at all is an
/// out-of-range *group*, which the group-descriptor subsystem keeps.
fn anomaly_in_bitmap(err: &ReadError, group: u32, category: Category) -> Anomaly {
let mut a = err.anomaly();
redirect_block_ref(&mut a, err, category);
a.location = a.location.or(Location::at_group(group));
a
}
/// The on-disk structure a [`ParseError`] is about.
fn parse_structure(err: &ParseError) -> &'static str {
match err {
ParseError::TooShort { structure, .. }
| ParseError::BadMagic { structure, .. }
| ParseError::InvalidField { structure, .. } => structure,
}
}
/// The [`Category`] an on-disk structure name belongs to, so a parse failure is filed
/// against the object that actually failed.
fn parse_category(structure: &str) -> Category {
match structure {
"Inode" => Category::Inode,
"GroupDescriptor" => Category::GroupDescriptor,
"ExtentHeader" | "ExtentNode" => Category::ExtentTree,
"DirEntry" | "DirEntryTail" => Category::Directory,
s if s.starts_with("Dx") => Category::Directory,
s if s.starts_with("Xattr") => Category::Xattr,
s if s.starts_with("JournalSuperblock") => Category::Journal,
"OrphanBlockTail" => Category::Orphan,
_ => Category::Superblock,
}
}
/// The read-only state one inode's extent scan threads through its recursion: which
/// inode it is under, the tree's checksum seed, whether checksums are on, and the
/// checksummer to recompute them with.
struct ExtentScanCtx<'a> {
ino: u32,
seed: u32,
has_csum: bool,
csum: &'a Crc32c,
}
/// Where an external extent node's checksum tail sits, taken from the node's own
/// declared capacity (`eh_max`) as the kernel does, not from the block size. The tail
/// follows the header and every entry slot the node declares room for, so a node that
/// does not fill its block is read where its tail actually is, matching a foreign tool
/// that wrote a short node. A capacity too large for the block is malformed; the
/// block-filling offset is used then, which stays in bounds and reads back as a
/// mismatch, so a crafted `eh_max` can neither move the tail out of the block nor pass
/// verification.
fn extent_tail_offset(node: &[u8], node_bytes: usize) -> usize {
if node.len() >= EXTENT_ENTRY_SIZE {
let eh_max = usize::from(get_u16(node, 4));
let tail = EXTENT_ENTRY_SIZE * (eh_max + 1);
if tail + EXTENT_TAIL_LEN <= node.len() {
return tail;
}
}
tail_offset(node_bytes)
}
/// A failure reading an image.
#[derive(Clone, PartialEq, Eq, Debug, thiserror::Error)]
#[non_exhaustive]
pub enum ReadError {
/// The underlying source could not be read or sought.
/// Carried as [`TreeError::Io`] describes, which is where the rule
/// this crate records an i/o failure by is written out: the kind beside the message, so a
/// caller tells a truncated image from an environment failure without matching on text.
#[error("i/o error: {message}")]
#[non_exhaustive]
Io {
/// How the underlying [`std::io::Error`] classified itself.
kind: std::io::ErrorKind,
/// The error rendered as text, for a message a person reads.
message: String,
},
/// A structure failed to parse.
#[error(transparent)]
Parse(#[from] ParseError),
/// A block or inode reference pointed outside the image.
#[error("reference to {what} {index} is out of range")]
#[non_exhaustive]
OutOfRange {
/// What was referenced (a block or an inode).
what: &'static str,
/// The offending index.
index: u64,
},
/// An inode number was zero or beyond the filesystem's inode count.
#[error("inode {inode} does not exist")]
#[non_exhaustive]
NoSuchInode {
/// The inode number that does not exist.
inode: u32,
},
/// An extent tree was deeper than the reader follows, or malformed.
#[error("extent tree is too deep or malformed")]
BadExtentTree,
/// A directory block or its hash-tree index was malformed: a zero-length record,
/// an entry running past the block, or a tree deeper than the reader follows.
#[error("directory structure is malformed")]
BadDirectory,
/// A directory entry names an inode number the filesystem does not have. Zero is the
/// unused slot and names nothing, so this is a *nonzero* number past
/// `s_inodes_count`: a reference to an inode that cannot exist, which is the
/// corruption `e2fsck` reports as "entry 'name' in / (2) has invalid inode #".
///
/// The entry's own name is not carried. It is bytes the image chose, and a fault
/// about a reference is located by the directory holding it rather than by a name a
/// consumer would render.
#[error("a directory entry names inode {inode}, which the filesystem does not have")]
#[non_exhaustive]
DirEntryNoSuchInode {
/// The inode number the entry names.
inode: u32,
},
/// A directory entry carries a name a real ext4 filesystem could not hold: an empty
/// one, or one containing a path separator or a NUL. The kernel's
/// `ext4_check_dir_entry` forbids the last two, so an image holding one was crafted or
/// corrupted. `.` and `..` are not among them — an ext4 directory holds both, and a
/// listing without them would not be the directory.
///
/// The name itself is not carried, for the same reason
/// [`DirEntryNoSuchInode`](Self::DirEntryNoSuchInode) does not carry one: it is bytes
/// the image chose, and rendering them is what the escaping rules exist to control.
/// The inode the entry names identifies it within its directory without echoing
/// anything the image wrote.
#[error("a directory entry naming inode {inode} has a name no directory can hold")]
#[non_exhaustive]
HostileName {
/// The inode number the offending entry names.
inode: u32,
},
/// The journal inode or its jbd2 superblock was malformed.
#[error("journal structure is malformed")]
BadJournal,
/// The orphan file was malformed: the feature is set but no inode holds the file, the
/// file claims more blocks than the filesystem has (it is a fixed array of entry
/// blocks, so it is never sparse), or one of its blocks does not end in the orphan
/// magic word.
#[error("orphan file structure is malformed")]
BadOrphanFile,
/// The superblock advertises an `incompat` feature this reader does not interpret —
/// an unknown extension, or `meta_bg`. Such a feature may change the on-disk format
/// in ways that make unaware access unsafe, so a strict read refuses it at open. The
/// anomaly this projects to names the features; the bare error carries the bits.
#[error(
"unsupported incompatible feature bits {bits:#010x}: the reader cannot be certain \
it interprets the on-disk format correctly"
)]
#[non_exhaustive]
UnsupportedIncompat {
/// The unsupported `incompat` bits: the feature word with every bit this reader
/// interprets cleared.
bits: u32,
},
/// An inode carries the extent-format flag, but the superblock does not enable the
/// `extent` feature, so the two disagree about how the inode maps its blocks. This is
/// the incoherence `e2fsck` reports as "inode is in extent format, but superblock is
/// missing EXTENTS feature".
#[error(
"inode {inode} is in extent format, but the superblock does not enable the \
extent feature"
)]
#[non_exhaustive]
ExtentFlagWithoutFeature {
/// The inode carrying the extent-format flag.
inode: u32,
},
/// An inode names an external extended-attribute block (`i_file_acl`), but the
/// superblock does not enable the `ext_attr` feature, so the feature word says the
/// filesystem holds no attributes while an inode points at a block of them. This is
/// the incoherence `e2fsck` reports as "i_file_acl for inode N is B, should be zero".
#[error(
"inode {inode} names attribute block {block}, but the superblock does not enable \
the ext_attr feature"
)]
#[non_exhaustive]
XattrBlockWithoutFeature {
/// The inode naming the block.
inode: u32,
/// The block it names.
block: u64,
},
/// A `system.posix_acl_*` attribute holds bytes that are not a stored ACL.
///
/// The attribute is read back in the form every boundary speaks, so a value that will
/// not parse has no form to be read back in. Reporting the stored bytes instead would
/// hand a consumer something it would install as an ACL and the kernel would refuse.
#[error(
"the {} attribute is not an ACL: {source}",
crate::escape::printable(.name)
)]
#[non_exhaustive]
MalformedAcl {
/// The attribute's name.
name: Vec<u8>,
/// The underlying ACL error.
#[source]
source: crate::acl::AclError,
},
/// A regular file is [`LARGE_FILE_MIN_SIZE`] or
/// larger, but the superblock does not enable the `large_file` feature that describes
/// such a file. This is the incoherence `e2fsck` reports as "filesystem contains large
/// files, but lacks LARGE_FILE flag in superblock". The bound is on regular files
/// alone: a directory of any size leaves the feature unneeded.
#[error(
"inode {inode} is a {size}-byte regular file, but the superblock does not enable \
the large_file feature"
)]
#[non_exhaustive]
LargeFileWithoutFeature {
/// The inode holding the file.
inode: u32,
/// Its size in bytes.
size: u64,
},
/// A directory carries the hash-index flag, but the superblock does not enable the
/// `dir_index` feature that permits one. This is the incoherence `e2fsck` reports as
/// "inode N has INDEX_FL flag set on filesystem without htree support".
#[error(
"inode {inode} is marked hash-indexed, but the superblock does not enable the \
dir_index feature"
)]
#[non_exhaustive]
IndexFlagWithoutFeature {
/// The directory inode carrying the hash-index flag.
inode: u32,
},
/// An inode that is not a directory carries the hash-index flag, which describes how a
/// directory's blocks are organized and means nothing on anything else. This is the
/// incoherence `e2fsck` reports as "inode N has INDEX_FL flag set but is not a
/// directory".
///
/// It is a separate fault from [`IndexFlagWithoutFeature`](Self::IndexFlagWithoutFeature),
/// which is a directory whose flag the superblock's feature words deny. An inode can
/// carry both faults, and each names a different thing to fix.
#[error("inode {inode} is marked hash-indexed, but it is not a directory")]
#[non_exhaustive]
IndexFlagOnNonDirectory {
/// The inode carrying the hash-index flag.
inode: u32,
},
/// A group descriptor places a bitmap or inode table outside the group it belongs to
/// on a filesystem without `flex_bg`, where each group's metadata must lie within it.
/// This is the corruption `e2fsck` reports as "block bitmap for group N is not in
/// group".
#[error("{what} for group {group} lies at block {block}, outside the group")]
#[non_exhaustive]
MetadataOutsideGroup {
/// The metadata that is out of place (`block bitmap`, `inode bitmap`, or `inode
/// table`).
what: &'static str,
/// The group whose descriptor named it.
group: u32,
/// The block it was placed at.
block: u64,
},
/// A path named no entry in the filesystem.
#[error("no such path: {}", crate::escape::printable(path))]
#[non_exhaustive]
NotFound {
/// The path that named no entry.
path: Vec<u8>,
},
/// A path used something that is not a directory as one.
#[error("not a directory: {}", crate::escape::printable(path))]
#[non_exhaustive]
NotADirectory {
/// The path whose component is not a directory.
path: Vec<u8>,
},
/// Resolving a path followed more symbolic links than the reader will, which a cycle
/// (`a -> b -> a`) and a chain long enough to be a denial of service both produce.
#[error(
"too many symbolic links resolving: {}",
crate::escape::printable(path)
)]
#[non_exhaustive]
SymlinkLoop {
/// The path whose resolution ran out of link budget.
path: Vec<u8>,
},
/// A walked path is longer than any consumer of one could use.
///
/// Nothing in the format bounds how deep a tree nests, and the walk's entry cap counts
/// *names* rather than the bytes their paths occupy — so a tree of directories each
/// holding one entry naming the next stays under every count while its paths grow by a
/// component each and cost the walk their sum.
///
/// The bound is `PATH_MAX`, which is the ceiling on what a path can be *used* for: a
/// consumer resolving one against a host gets `ENAMETOOLONG` past it, so a longer path
/// is not a path anything could act on.
#[error("a walked path is longer than the {limit} bytes a path may be")]
#[non_exhaustive]
PathTooLong {
/// The longest path a walk builds.
limit: usize,
},
/// A tree holds more names than a walk is bounded to gather.
///
/// The bound is the smaller of [`Limits::max_walk_entries`] and the number of names
/// the source has room to describe. A well-formed filesystem never reaches the second,
/// so this names either a caller's own cap or an image whose directories share blocks
/// to describe more names than they hold.
#[error("the tree holds more than {limit} names, the bound this walk is held to")]
#[non_exhaustive]
WalkTooLarge {
/// The bound that was reached.
limit: usize,
},
/// A whole-file read was asked for a file larger than [`Limits::max_file_bytes`], the
/// cap the caller set on it.
///
/// The bound is usually the caller's alone — no structural bound exists behind it for a
/// regular file, since a legitimate all-hole one and a crafted `i_size` are the same
/// shape from the outside. A symbolic link is the exception: no path is longer than
/// `PATH_MAX`, so [`read_symlink`](Reader::read_symlink) holds a target to that ceiling
/// whatever a caller asked for.
/// Reaching it is an error rather than a short buffer for the same reason
/// [`WalkTooLarge`](Self::WalkTooLarge) is: a caller extracting a file from a truncated
/// read would write an incomplete one and see success. To read part of a file
/// deliberately, use [`Reader::read_into`], which is bounded by the buffer given to it
/// and reports how much it filled.
#[error("the file is {size} bytes, more than the {limit}-byte cap this read is held to")]
#[non_exhaustive]
FileTooLarge {
/// The file's logical size, as its inode declares it.
size: u64,
/// The cap that was exceeded.
limit: u64,
},
/// A metadata object's stored checksum did not match its recomputed value.
#[error("{object} {index} checksum mismatch: stored {stored:#010x}, computed {computed:#010x}")]
#[non_exhaustive]
ChecksumMismatch {
/// The kind of object that failed (`superblock`, `group descriptor`, `inode`,
/// `extent node`, `block bitmap`, `inode bitmap`, `directory block`,
/// `xattr block`, or `orphan block`).
object: &'static str,
/// The object's index (group, inode, or block number; zero for the
/// superblock).
index: u64,
/// The checksum stored on disk.
stored: u32,
/// The checksum recomputed from the object's bytes.
computed: u32,
},
}
crate::io::io_error!(ReadError);
impl ReadError {
/// Classify this error as a typed [`Anomaly`]: its severity, subsystem, and
/// location. A strict read fails on the first such anomaly; this projection is
/// what a lenient read would collect instead.
#[must_use]
pub fn anomaly(&self) -> Anomaly {
let (severity, category, location) = match self {
// The source itself failed: nothing could be parsed, so it is structural.
ReadError::Io { .. } => (
Severity::Structural,
Category::Superblock,
Location::default(),
),
// A parse failure is reported against the object that failed to parse. The
// structure name each `ParseError` carries is what names it; defaulting to
// the superblock would file a malformed inode under the one object the
// image demonstrably got right.
ReadError::Parse(e) => (
Severity::Structural,
parse_category(parse_structure(e)),
Location::default(),
),
ReadError::OutOfRange { what, index } => {
let category = if *what == "inode" {
Category::Inode
} else if *what == "group" {
Category::GroupDescriptor
} else {
Category::Superblock
};
let location = match *what {
"block" => Location {
block: Some(*index),
..Location::default()
},
"inode" => Location {
inode: Some(*index as u32),
..Location::default()
},
"group" => Location {
group: Some(*index as u32),
..Location::default()
},
_ => Location::default(),
};
(Severity::Structural, category, location)
}
ReadError::NoSuchInode { inode: n } => (
Severity::Structural,
Category::Inode,
Location {
inode: Some(*n),
..Location::default()
},
),
ReadError::BadExtentTree => (
Severity::Structural,
Category::ExtentTree,
Location::default(),
),
ReadError::BadDirectory => (
Severity::Structural,
Category::Directory,
Location::default(),
),
// A reference to an inode that cannot exist is a fault in the directory that
// holds it, so the location is stamped with that directory by whichever walk
// reached the entry — not with the number the entry names, which locates
// nothing.
// A name no directory could hold is a fault in the directory holding it, located
// the same way and by the same callers as the reference fault above.
ReadError::DirEntryNoSuchInode { .. } | ReadError::HostileName { .. } => (
Severity::Structural,
Category::Directory,
Location::default(),
),
ReadError::BadJournal => (Severity::Structural, Category::Journal, Location::default()),
ReadError::BadOrphanFile => {
(Severity::Structural, Category::Orphan, Location::default())
}
// Path resolution failures are about the directory tree, and arise only from
// a lookup: a whole-image scan resolves no paths.
// A bound reached is a fact about the walk, not about the image's soundness
// — a caller's own cap reaches it too — so it is filed as a structural
// finding about the directory tree it stopped in.
ReadError::PathTooLong { .. } | ReadError::WalkTooLarge { .. } => (
Severity::Structural,
Category::Directory,
Location::default(),
),
// The same reasoning one object down: a cap the caller set is not a fault in
// the image, but the read it stopped could not be completed, so it is filed
// against the inode whose size exceeded it.
ReadError::FileTooLarge { .. } => {
(Severity::Structural, Category::Inode, Location::default())
}
ReadError::NotFound { .. }
| ReadError::NotADirectory { .. }
| ReadError::SymlinkLoop { .. } => (
Severity::Structural,
Category::Directory,
Location::default(),
),
ReadError::ChecksumMismatch { object, index, .. } => {
let (category, location) = match *object {
"superblock" => (Category::Superblock, Location::default()),
"group descriptor" => (
Category::GroupDescriptor,
Location {
group: Some(*index as u32),
..Location::default()
},
),
"inode" => (
Category::Inode,
Location {
inode: Some(*index as u32),
..Location::default()
},
),
"extent node" => (
Category::ExtentTree,
Location {
block: Some(*index),
..Location::default()
},
),
"block bitmap" | "inode bitmap" => (
Category::Bitmap,
Location {
group: Some(*index as u32),
..Location::default()
},
),
"directory block" => (
Category::Directory,
Location {
block: Some(*index),
..Location::default()
},
),
"xattr block" => (
Category::Xattr,
Location {
block: Some(*index),
..Location::default()
},
),
"orphan block" => (
Category::Orphan,
Location {
block: Some(*index),
..Location::default()
},
),
_ => (Category::Superblock, Location::default()),
};
(Severity::Integrity, category, location)
}
// An `incompat` feature the reader does not follow means it cannot vouch for
// the whole image, so it is structural and filed against the superblock that
// advertised it.
ReadError::UnsupportedIncompat { .. } => (
Severity::Structural,
Category::Superblock,
Location::default(),
),
ReadError::ExtentFlagWithoutFeature { inode } => (
Severity::Structural,
Category::Inode,
Location {
inode: Some(*inode),
..Location::default()
},
),
// A pointer at a block the feature word denies: the reader cannot be certain
// what that block holds, so it is structural, and it is the attribute
// subsystem the pointer claims to belong to.
ReadError::XattrBlockWithoutFeature { inode, block } => (
Severity::Structural,
Category::Xattr,
Location {
inode: Some(*inode),
block: Some(*block),
..Location::default()
},
),
// The attribute parsed; its value did not. Nothing downstream can follow it, so
// it is structural, in the subsystem whose name it was stored under. The inode
// is stamped by whichever walk reached the attribute, since an on-disk inode
// does not carry its own number.
ReadError::MalformedAcl { .. } => {
(Severity::Structural, Category::Xattr, Location::default())
}
// The size and the index are read correctly either way; what is missing is the
// feature word that should advertise them. Both are conformance deviations —
// valid ext4 as bytes, not as a self-consistent filesystem.
ReadError::LargeFileWithoutFeature { inode, .. } => (
Severity::Conformance,
Category::Inode,
Location {
inode: Some(*inode),
..Location::default()
},
),
ReadError::IndexFlagWithoutFeature { inode } => (
Severity::Conformance,
Category::Directory,
Location {
inode: Some(*inode),
..Location::default()
},
),
// The flag on a non-directory is a fact about that inode, not about any
// directory tree, so it is filed against the inode.
ReadError::IndexFlagOnNonDirectory { inode } => (
Severity::Conformance,
Category::Inode,
Location {
inode: Some(*inode),
..Location::default()
},
),
ReadError::MetadataOutsideGroup {
what, group, block, ..
} => {
// A bitmap out of place is the bitmap subsystem's; a misplaced inode table
// is the group descriptor's, which is what positions it.
let category = if what.ends_with("bitmap") {
Category::Bitmap
} else {
Category::GroupDescriptor
};
(
Severity::Structural,
category,
Location {
group: Some(*group),
block: Some(*block),
..Location::default()
},
)
}
};
// The unsupported-feature anomaly names the features it found; every other
// anomaly's detail is its own message.
let detail = match self {
ReadError::UnsupportedIncompat { bits } => describe_unsupported_incompat(*bits),
_ => self.to_string(),
};
Anomaly {
severity,
category,
location,
detail,
}
}
}
/// What a whole-image [`scan`](Reader::scan) found, in ext's own taxonomy.
///
/// This is the crate's [`ScanReport`](crate::ScanReport) over ext's [`Anomaly`]: an anomaly
/// names the subsystem as a [`Category`] value and its place as a [`Location`] of group,
/// inode, and block, which is what a consumer reasoning about an ext filesystem wants. The
/// addressing unit is the filesystem's block size, so a block-addressed anomaly projects to
/// the byte offset that block sits at.
pub type ScanReport = crate::ScanReport<Anomaly>;
/// One resolved directory entry: a name and the inode it points at.
#[derive(Clone, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub struct Entry {
/// Entry name.
pub name: Vec<u8>,
/// Inode the name points at.
pub inode: u32,
/// File type recorded in the entry.
pub file_type: FileType,
}
/// One name a [`walk`](Reader::walk) reached: its path, the inode number the name
/// points at, and that inode.
///
/// The number is what distinguishes a name from the file it names. Two paths sharing an
/// inode are two names for one file — a hard link — and nothing in the inode itself says
/// which other paths those are, so a consumer reconstructing the links (writing an
/// archive, counting the bytes a tree occupies) needs the number to tell one file with
/// two names from two files with identical contents.
#[derive(Clone, PartialEq, Eq, Debug)]
#[non_exhaustive]
pub struct WalkEntry {
/// Absolute path from the filesystem root, `/`-joined, always beginning with `/`.
pub path: Vec<u8>,
/// The inode number the name points at.
pub number: u32,
/// The inode itself.
pub inode: Inode,
}
/// A read-only handle over an ext4 image on any [`Read`] + [`Seek`] source.
///
/// The filesystem may sit at an arbitrary byte offset within the source — a partition
/// inside a whole-disk image — fixed at open time. Reads seek relative to that offset
/// and return owned buffers, so nothing is borrowed from the source between calls.
pub struct Reader<R> {
src: R,
base: u64,
/// The bytes the source holds from [`base`](Self::base) on, measured at open. Every
/// structural bound the reader applies is derived from it, so it is the filesystem's
/// share of the source rather than the whole of it.
source_len: u64,
sb: SuperBlock,
/// The superblock exactly as it sits on disk.
///
/// A checksum covers the bytes an object *has*, not the bytes this crate's model of
/// it can reproduce. The superblock carries fields no formatter is obliged to leave
/// zero and this crate does not model — the kernel's error record, the last-orphan
/// pointer, the mount options — and recomputing its checksum from a re-serialized
/// [`SuperBlock`] silently zeroes every one of them. That verifies only images this
/// crate wrote, and rejects healthy ones it did not. So the raw bytes are kept, and
/// the checksum is computed over them.
sb_raw: Vec<u8>,
feature: FeatureSet,
block_size: usize,
policy: ReadPolicy,
limits: Limits,
csum_seed: Option<u32>,
}
/// How a filesystem is opened: where it begins, how strictly it is read, what it may
/// allocate, and which checksum seed its metadata is verified against.
///
/// Every input to [`Reader::open_with`] is a field here rather than a parameter, so a
/// knob the reader grows arrives as a field a caller may ignore.
///
/// ```
/// # use ferrosys::ext::{OpenOptions, ReadPolicy, Reader};
/// # let image: Vec<u8> = Vec::new();
/// // A filesystem inside a partition, read leniently so a scan can describe what is
/// // wrong with it rather than the open refusing it.
/// let options = OpenOptions::new().base(1 << 20).policy(ReadPolicy::Lenient);
/// # let _ = options;
/// # let _ = image;
/// ```
#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
#[non_exhaustive]
pub struct OpenOptions {
/// Where the filesystem begins, how strictly it is read, and what one read may
/// allocate — the crate's [`OpenOptions`](crate::OpenOptions), which mean exactly what
/// they mean there.
///
/// Held rather than flattened, so an input every family takes is added in one place and
/// reaches every family at once. The three builders below set through it, so a caller
/// writes `OpenOptions::new().base(..)` either way.
pub common: crate::OpenOptions,
/// The `metadata_csum` seed to verify against, overriding the one the image implies.
///
/// A filesystem's checksums are computed from a seed: the value stored in the
/// superblock when `metadata_csum_seed` is set, and one derived from the UUID
/// otherwise. Naming a seed here is for the image whose stored seed and UUID
/// disagree — a UUID changed after the fact — where the checksums are valid against
/// a seed the image no longer implies. `None`, the default, uses the image's own.
pub csum_seed: Option<u32>,
}
impl OpenOptions {
/// Open at the start of the source, strictly, with the default limits and the
/// image's own checksum seed.
#[must_use]
pub const fn new() -> Self {
Self {
common: crate::OpenOptions::new(),
csum_seed: None,
}
}
/// Open a filesystem that begins `base` bytes into the source.
#[must_use]
pub const fn base(mut self, base: u64) -> Self {
self.common = self.common.base(base);
self
}
/// Read under `policy`.
#[must_use]
pub const fn policy(mut self, policy: ReadPolicy) -> Self {
self.common = self.common.policy(policy);
self
}
/// Cap what one read may allocate.
#[must_use]
pub const fn limits(mut self, limits: Limits) -> Self {
self.common = self.common.limits(limits);
self
}
/// Take the three shared inputs from `common`, leaving this family's own alone.
#[must_use]
pub const fn common(mut self, common: crate::OpenOptions) -> Self {
self.common = common;
self
}
/// Verify metadata checksums against `seed` rather than the one the image implies.
#[must_use]
pub const fn csum_seed(mut self, seed: u32) -> Self {
self.csum_seed = Some(seed);
self
}
}
impl<R: Read + Seek> Reader<R> {
/// Open a filesystem at the start of `src` under the default [`OpenOptions`].
///
/// # Errors
///
/// [`ReadError::Parse`] if the superblock's magic is wrong; [`ReadError::Io`] if
/// the source cannot be read; [`ReadError::UnsupportedIncompat`] if it advertises an
/// `incompat` feature this reader does not follow (the default policy is strict).
pub fn open(src: R) -> Result<Self, ReadError> {
Self::open_with(src, &OpenOptions::new())
}
/// Open a filesystem under `options`: where in `src` it begins, how strictly to read
/// it, what it may allocate, and which checksum seed to verify against.
///
/// # Errors
///
/// [`ReadError::Parse`] if the superblock's magic is wrong; [`ReadError::Io`] if
/// the source cannot be read or sought (a source too short to hold a superblock
/// among them); [`ReadError::UnsupportedIncompat`] if it advertises an `incompat`
/// feature this reader does not follow and the policy is [`ReadPolicy::Strict`]. A
/// lenient open accepts such an image so a [`scan`](Self::scan) can report it.
pub fn open_with(mut src: R, options: &OpenOptions) -> Result<Self, ReadError> {
let &OpenOptions {
common:
crate::OpenOptions {
base,
policy,
limits,
..
},
csum_seed,
} = options;
// The primary superblock is 1024 bytes into the filesystem. A `base` a caller
// pushed near the top of the 64-bit range cannot overflow into a small seek: the
// sum is checked, and an offset that leaves no room for a superblock is reported
// as out of range rather than wrapped.
let sb_offset = offset_of(base, 1, 1024).ok_or(ReadError::OutOfRange {
what: "bytes",
index: base,
})?;
let sb_bytes = read_exact_at(&mut src, sb_offset, SuperBlock::SIZE)?;
let mut sb = SuperBlock::read_from(&sb_bytes)?;
// A revision-0 filesystem predates the fields that describe an inode's size and
// the first inode a file may use: the words hold zero and the values are fixed
// by the revision — the 128-byte classic inode, and inode 11. Resolving them
// here is what lets the rest of the reader treat every revision alike, and it is
// the resolved value a caller sees. The raw bytes the checksum covers are kept
// separately, so this does not disturb it.
if sb.rev_level == 0 {
sb.inode_size = GOOD_OLD_INODE_SIZE as u16;
sb.first_ino = GOOD_OLD_FIRST_INODE;
}
// ext4 block sizes are 1024 << (0..=6) bytes; a larger shift value is a
// malformed superblock, rejected rather than shifted into overflow.
if sb.log_block_size > 6 {
return Err(ReadError::Parse(ParseError::InvalidField {
structure: "superblock",
field: "s_log_block_size",
value: u64::from(sb.log_block_size),
}));
}
let block_size = 1024usize << sb.log_block_size;
// The inode size is a power of two from 128 up to the block size; an odd value
// such as 33-63 would give a descriptor stride the on-disk format never uses and
// make every inode offset wrong, so it is rejected here rather than read into
// garbage.
if !sb.inode_size.is_power_of_two()
|| sb.inode_size < 128
|| usize::from(sb.inode_size) > block_size
{
return Err(ReadError::Parse(ParseError::InvalidField {
structure: "superblock",
field: "s_inode_size",
value: u64::from(sb.inode_size),
}));
}
let feature = FeatureSet {
compat: crate::feature::Compat::from_bits(sb.feature_compat),
incompat: Incompat::from_bits(sb.feature_incompat),
ro_compat: crate::feature::RoCompat::from_bits(sb.feature_ro_compat),
block_size: block_size as u32,
inode_size: sb.inode_size,
};
// The group-descriptor size is load-bearing only under `64bit`: it sets both the
// stride between descriptors and the width their checksum covers. Without the
// feature the reader uses the 32-byte classic form whatever the word holds, so
// the field is not consulted and not constrained — matching the kernel, which
// ignores it there. Under `64bit` it must satisfy the kernel's bounds: a power of
// two from `EXT4_MIN_DESC_SIZE_64BIT` (64) up to the block size. A 32-byte value
// would read a 64-bit table at half its stride and check it at the wrong width; a
// value past the block size would run each descriptor beyond its own block.
//
// This is also the bound [`desc_size`](Self::desc_size) rests on: it is what makes
// every width the reader hands the on-disk layer one that layer has a form for.
if feature.is_64bit()
&& (!sb.desc_size.is_power_of_two()
|| usize::from(sb.desc_size) < GroupDescriptor::SIZE_64
|| usize::from(sb.desc_size) > block_size)
{
return Err(ReadError::Parse(ParseError::InvalidField {
structure: "superblock",
field: "s_desc_size",
value: u64::from(sb.desc_size),
}));
}
// A group holds at least one block and at most what its one-block block bitmap
// indexes, and at least one inode and at most what its one-block inode bitmap does.
// Both fields divide, and both decide how much of the filesystem is examined at all:
// a zero `s_blocks_per_group` makes the group count zero, so every scan loop runs no
// times and reports a clean filesystem; a zero `s_inodes_per_group` lets the group
// loop run and examines no inode in any of them, while `inode_raw` answers
// `NoSuchInode` for every number — a filesystem with no readable inode at all,
// reported clean. `e2fsck` refuses both, and `is_clean()` is the verdict a forensic
// caller acts on, so they are refused here rather than scanned around.
let per_group_cap = 8 * block_size as u64;
if sb.blocks_per_group == 0 || u64::from(sb.blocks_per_group) > per_group_cap {
return Err(ReadError::Parse(ParseError::InvalidField {
structure: "superblock",
field: "s_blocks_per_group",
value: u64::from(sb.blocks_per_group),
}));
}
if sb.inodes_per_group == 0 || u64::from(sb.inodes_per_group) > per_group_cap {
return Err(ReadError::Parse(ParseError::InvalidField {
structure: "superblock",
field: "s_inodes_per_group",
value: u64::from(sb.inodes_per_group),
}));
}
// Two more fields feed the same group count, through the subtraction rather than
// the divisor: a block count that leaves nothing past the first data block makes
// the addressable span zero — the same every-loop-runs-no-times scan, reported
// clean, that the two per-group guards above exist to refuse. The first data block
// has exactly one value per block size — the block holding the superblock — and
// the family's own library refuses any other before its tools look further, which
// is the precedent both refusals follow. The block count is compared against it
// rather than against zero, because at a one-kibibyte block the canonical first
// data block is one, and a block count of one then scans exactly as little as a
// count of zero does.
if sb.first_data_block != u32::from(block_size == 1024) {
return Err(ReadError::Parse(ParseError::InvalidField {
structure: "superblock",
field: "s_first_data_block",
value: u64::from(sb.first_data_block),
}));
}
if sb.blocks_count <= u64::from(sb.first_data_block) {
return Err(ReadError::Parse(ParseError::InvalidField {
structure: "superblock",
field: "s_blocks_count",
value: sb.blocks_count,
}));
}
// The `incompat` word is the one an implementation must refuse when it carries a
// bit it does not recognize: those features change the on-disk format in ways that
// make unaware access unsafe. A strict read refuses such an image at open; a
// lenient read opens it so a [`scan`](Self::scan) reports the feature as an anomaly
// rather than the open failing. `unknown_bits` on the individual words is what a
// description still reports either way.
let unsupported = unsupported_incompat(feature.incompat);
if unsupported != 0 && policy.is_fatal(Severity::Structural) {
return Err(ReadError::UnsupportedIncompat { bits: unsupported });
}
// What the source has for this filesystem, which every structural bound is derived
// from. Measured here rather than at each use: a bound that governs how much a
// verifier examines must not depend on a seek that could fail mid-read, and the
// filesystem's length does not change under a reader.
let source_len = src.seek(SeekFrom::End(0))?.saturating_sub(base);
Ok(Self {
src,
base,
source_len,
sb,
sb_raw: sb_bytes,
feature,
block_size,
policy,
limits,
csum_seed,
})
}
/// The parsed superblock.
#[must_use]
pub fn superblock(&self) -> &SuperBlock {
&self.sb
}
/// The feature set the image advertises.
#[must_use]
pub fn feature(&self) -> FeatureSet {
self.feature
}
/// The ext filesystem family the image's feature words classify to
/// ([`Profile::of`]): its ext2, ext3, or ext4 label.
#[must_use]
pub fn profile(&self) -> Profile {
Profile::of(self.feature)
}
/// The conformance-strictness policy this handle reads under.
#[must_use]
pub fn policy(&self) -> ReadPolicy {
self.policy
}
/// Read `len` bytes at `offset` bytes into the filesystem into an owned buffer. A
/// read that runs off the end of the source is reported as an out-of-range
/// reference, not a raw i/o error, so callers can relabel it to the referent.
fn read_at(&mut self, offset: u64, len: usize) -> Result<Vec<u8>, ReadError> {
// A base-relative offset a malformed field pushed past the 64-bit range is an
// out-of-range reference, not an overflow.
let pos = offset_of(self.base, offset, 1).ok_or(ReadError::OutOfRange {
what: "bytes",
index: offset,
})?;
match read_exact_at(&mut self.src, pos, len) {
Ok(buf) => Ok(buf),
Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => Err(ReadError::OutOfRange {
what: "bytes",
index: offset,
}),
Err(e) => Err(ReadError::from(e)),
}
}
/// The bytes of block `block`, read into an owned buffer.
///
/// Every read of a numbered block goes through here, so this is where the
/// filesystem's own bound is applied to whatever named it — a group descriptor's
/// bitmap or inode table, an extent tree's index entry, a directory's mapping. Each
/// is a reference out of the image's own bytes, and each is checked against
/// [`check_block`](Self::check_block) before any i/o rather than against the source's
/// length alone.
fn block(&mut self, block: u64) -> Result<Vec<u8>, ReadError> {
self.check_block(block)?;
// Counted from the filesystem's own first byte; `read_at` is what places that in
// the source.
let start = offset_of(0, block, self.block_size as u64).ok_or(ReadError::OutOfRange {
what: "block",
index: block,
})?;
match self.read_at(start, self.block_size) {
Err(ReadError::OutOfRange { .. }) => Err(ReadError::OutOfRange {
what: "block",
index: block,
}),
other => other,
}
}
/// Read the descriptor for group `group` from the primary descriptor table.
///
/// # Errors
///
/// [`ReadError::Parse`] if the descriptor cannot be read; [`ReadError::OutOfRange`]
/// if the table does not reach it.
pub fn group_descriptor(&mut self, group: u32) -> Result<GroupDescriptor, ReadError> {
let raw = self.group_descriptor_raw(group)?;
Ok(GroupDescriptor::read_from(&raw, self.desc_size())?)
}
/// The stride of one group descriptor on disk.
///
/// `s_desc_size` describes the descriptor only under `64bit`; without that feature
/// the field means nothing and every descriptor is the 32-byte classic form,
/// whatever the word happens to hold. Honoring a stale 64 there would read every
/// descriptor at the wrong offset and check it at the wrong checksum width, so the
/// feature bit decides and the field is consulted only when it is meaningful.
///
/// What bounds the `64bit` arm is [`open_with`](Self::open_with), which refuses any
/// image whose `s_desc_size` is not a power of two from
/// [`GroupDescriptor::SIZE_64`] up to the block size: no opened reader reaches here
/// with a smaller one. The clamp restates that floor where the value is used, so the
/// width handed to the on-disk layer is one it has a form for however the field was
/// arrived at.
fn desc_size(&self) -> usize {
if self.feature.is_64bit() {
debug_assert!(
usize::from(self.sb.desc_size) >= GroupDescriptor::SIZE_64,
"open_with admitted a 64bit image with s_desc_size {}",
self.sb.desc_size
);
self.sb.desc_size.max(GroupDescriptor::SIZE_32 as u16) as usize
} else {
GroupDescriptor::SIZE_32
}
}
/// The descriptor for group `group` exactly as it sits on disk.
///
/// Its checksum covers these bytes, not a re-serialization of the parsed value, so
/// the verifier works from them.
fn group_descriptor_raw(&mut self, group: u32) -> Result<Vec<u8>, ReadError> {
let desc_size = self.desc_size();
let table = u64::from(self.sb.first_data_block) + 1;
// Both the table's place and the descriptor's place within it come from fields the
// image supplies, so both products are checked. Counted from the filesystem's own
// first byte; `read_at` is what places that in the source.
let off = offset_of(0, table, self.block_size as u64)
.and_then(|table| offset_of(table, u64::from(group), desc_size as u64))
.ok_or(ReadError::OutOfRange {
what: "group",
index: u64::from(group),
})?;
// The table's place is set by `s_first_data_block` and its extent by the group
// number, both of which the image supplies, so the descriptor's bytes are bounded
// by the filesystem exactly as a block reference is. A table a malformed
// superblock pushed past the final block would otherwise be read from whatever
// follows an embedded filesystem.
let last_block = (off + desc_size as u64 - 1) / self.block_size as u64;
if last_block >= self.sb.blocks_count {
return Err(ReadError::OutOfRange {
what: "group",
index: u64::from(group),
});
}
match self.read_at(off, desc_size) {
Err(ReadError::OutOfRange { .. }) => Err(ReadError::OutOfRange {
what: "group",
index: u64::from(group),
}),
other => other,
}
}
/// The number of block groups, derived from the superblock geometry.
///
/// It is the bound to iterate [`group_descriptor`](Self::group_descriptor) over,
/// and the checked arithmetic is why: the same derivation performed on a caller's
/// side would divide by a zero `s_blocks_per_group` on a hostile image. Opening
/// refuses every superblock whose geometry would make this zero, so on a reader
/// that exists it is at least one; the guards stay because this function cannot
/// see that refusal from here.
#[must_use]
pub fn group_count(&self) -> u32 {
let bpg = u64::from(self.sb.blocks_per_group);
if bpg == 0 {
return 0;
}
let addressable = self
.sb
.blocks_count
.saturating_sub(u64::from(self.sb.first_data_block));
addressable.div_ceil(bpg).min(u64::from(u32::MAX)) as u32
}
/// The bytes the source has for this filesystem: its length from
/// [`base`](OpenOptions::base) on. The forensic scan uses it to bound how far it will
/// look, so a superblock count field a bit-flip inflated cannot drive a loop past the
/// end of the source.
///
/// It is measured from the base and not from the start of the source, because every
/// bound built on it is a statement about the *filesystem*. A 16 MiB partition at the
/// end of a 2 TiB disk image is bounded by the 16 MiB that could hold it, not by
/// everything in front of it — which is the embedded case this reader is built for, and
/// the case a whole-source length would bound most loosely.
///
/// This is the source's apparent length, which is the only length a `Seek` source
/// offers: a sparse file reports the size it claims, not the blocks it occupies.
/// The bound is therefore a real limit on how far a walk reaches, and the cost of
/// reaching it scales with the size an image claims rather than with the bytes
/// stored for it.
///
/// It is measured once, at open. A source that cannot report its end fails the open
/// rather than reporting zero here: zero is not a conservative answer for a bound that
/// governs how much a verifier examines — it would make every loop built on it run no
/// times and report a filesystem with nothing wrong.
const fn source_len(&self) -> u64 {
self.source_len
}
/// The in-use inode numbers in group `g`, taken from its inode bitmap.
///
/// A set bit marks an in-use inode: the reserved inodes, and every inode a
/// directory operation has since allocated, wherever the allocator placed it. This
/// is what a live filesystem's inodes must be enumerated from — assuming they run
/// densely from one holds only for a freshly formatted image, and both misses an
/// inode a `mkdir` scattered into a later group and reads a never-initialized table
/// slot as though it held one.
///
/// A group the descriptor marks `BG_INODE_UNINIT` has no inode in use and yields
/// nothing. Bits past the group's inode count are padding, and a number past the
/// filesystem's inode count is not an inode; both are ignored. Inodes are numbered
/// from one, so group `g`'s first is `g * inodes_per_group + 1`. The scan reads at
/// most one block of bitmap, so a hostile `inodes_per_group` cannot drive the bit
/// loop past the bytes that exist.
///
/// # Errors
///
/// [`ReadError`] variants if the group descriptor or its inode bitmap cannot be
/// read.
fn group_in_use_inodes(&mut self, g: u32) -> Result<Vec<u32>, ReadError> {
let desc = self.group_descriptor(g)?;
if desc.flags & BG_INODE_UNINIT != 0 {
return Ok(Vec::new());
}
let ipg = self.sb.inodes_per_group;
let base = u64::from(g) * u64::from(ipg);
let bmp = self.block(desc.inode_bitmap)?;
let mut out = Vec::new();
for i in 0..ipg {
let byte = (i / 8) as usize;
if byte >= bmp.len() {
break;
}
if bmp[byte] & (1u8 << (i % 8)) == 0 {
continue;
}
let number = base + u64::from(i) + 1;
if number > u64::from(self.sb.inodes_count) {
break;
}
out.push(number as u32);
}
Ok(out)
}
/// The 16-bit crc32c group descriptor `group` should carry: the filesystem seed
/// folded with the group number, then the descriptor's own bytes with the checksum
/// field zeroed.
fn descriptor_checksum(&self, group: u32, raw: &[u8], csum: &Crc32c) -> u16 {
let mut buf = raw.to_vec();
// `bg_checksum` participates as zero; every other byte participates as it is,
// including any this crate does not model.
put_u16(&mut buf, GroupDescriptor::CHECKSUM_OFFSET, 0);
recipe::group_descriptor(csum, group, &buf)
}
/// The checksum inode `number` should carry, and the stored value to compare it
/// against: the filesystem seed folded with the inode number and generation, then
/// the inode's own bytes with its checksum fields zeroed.
///
/// The comparison is sixteen bits wide on an inode that carries no `i_checksum_hi`
/// — the kernel's `calculated &= 0xFFFF` — because there is no high half stored to
/// compare a full-width value against. `mke2fs` leaves the reserved inodes in
/// exactly that state, so on any filesystem it formatted, checking them at full
/// width rejects seven healthy inodes.
fn inode_checksum(&self, number: u32, raw: &[u8], csum: &Crc32c) -> (u32, u32) {
let inode_size = self.sb.inode_size;
let extra_isize = if usize::from(inode_size) > GOOD_OLD_INODE_SIZE {
get_u16(raw, 0x80)
} else {
0
};
let has_hi = Inode::has_checksum_hi(inode_size, extra_isize);
let mut buf = raw.to_vec();
put_u16(&mut buf, Inode::CHECKSUM_LO_OFFSET, 0);
let stored = {
let lo = u32::from(get_u16(raw, Inode::CHECKSUM_LO_OFFSET));
if has_hi {
// The high half participates as zero only when it is a checksum field
// at all. On an inode without one, those two bytes hold something else
// — the head of the inline attribute region, say — and zeroing them
// would checksum bytes the filesystem never had.
put_u16(&mut buf, Inode::CHECKSUM_HI_OFFSET, 0);
lo | (u32::from(get_u16(raw, Inode::CHECKSUM_HI_OFFSET)) << 16)
} else {
lo
}
};
let generation = get_u32(raw, 0x64);
let computed = recipe::inode(csum, number, generation, &buf, has_hi);
(stored, computed)
}
/// Verify the metadata checksums the image carries: the primary superblock, every
/// group descriptor and its block and inode bitmaps, and every in-use inode with its
/// extent tree, directory-block tails, and external attribute block, then the
/// journal superblock's structure.
///
/// Each object's crc32c is recomputed from the bytes on disk and compared to the
/// field the image stores, so corruption of the filesystem's own metadata is caught
/// wherever it lies rather than surfacing later as a wrong read.
///
/// This verifies *checksums*, and an image carries them only under `metadata_csum`.
/// On an ext2 or ext3 image — or any ext4 image without the feature — there is
/// nothing to verify and this is a no-op returning `Ok`: a clean result here means
/// "no stored checksum disagreed," not "the image is intact." It is not a
/// whole-image integrity gate. [`scan`](Self::scan) is that entry point — it reports
/// structural anomalies and the jbd2 journal superblock's soundness whether or not
/// the image checksums its metadata.
///
/// # Errors
///
/// [`ReadError::ChecksumMismatch`] on the first object whose stored checksum does
/// not match its recomputed value; other [`ReadError`] variants if an object
/// cannot be read.
pub fn verify_checksums(&mut self) -> Result<(), ReadError> {
if !self.feature.has_metadata_csum() {
return Ok(());
}
let csum = self.checksummer();
// Superblock.
let computed = superblock_checksum(&self.sb_raw);
check_checksum("superblock", 0, self.sb.checksum, computed)?;
// Every group descriptor, then its two bitmap checksums.
for g in 0..self.group_count() {
let raw = self.group_descriptor_raw(g)?;
let desc = GroupDescriptor::read_from(&raw, self.desc_size())?;
let computed = self.descriptor_checksum(g, &raw, &csum);
check_checksum(
"group descriptor",
u64::from(g),
u32::from(desc.checksum),
u32::from(computed),
)?;
self.verify_bitmap_checksums(g, &csum)?;
}
// Every in-use inode, taken from the inode bitmaps: its checksum, its extent
// tree, its directory tails, and its attribute block. Reading the bitmaps rather
// than assuming inodes run densely from one finds an inode a live filesystem
// scattered past that range and skips a never-initialized table slot, whose
// bytes are not an inode to checksum. The examined count is capped at the inodes
// the source can hold, so a hostile bitmap cannot drive an unbounded loop.
let max_inodes = self.source_len() / u64::from(self.sb.inode_size.max(128));
let mut examined = 0u64;
'inodes: for g in 0..self.group_count() {
for n in self.group_in_use_inodes(g)? {
if examined >= max_inodes {
break 'inodes;
}
examined += 1;
let raw = self.inode_raw(n)?;
let mut inode = Inode::read_from(&raw, self.sb.inode_size)?;
self.mask_ungated_halves(&mut inode);
let (stored, computed) = self.inode_checksum(n, &raw, &csum);
check_checksum("inode", u64::from(n), stored, computed)?;
self.verify_extent_nodes(n, &inode, &csum)?;
self.verify_directory_checksums(n, &inode, &csum)?;
self.verify_xattr_block_checksum(&inode, &csum)?;
}
}
// The journal is a structural object, not a checksummed one under the v2 log
// this crate reads; a malformed journal superblock surfaces as its own error.
self.journal_superblock()?;
self.verify_orphan_blocks(&csum)?;
Ok(())
}
/// The checksummer this image's metadata checksums are built on.
///
/// With `metadata_csum_seed` the seed is the one stored in the superblock, which is what the
/// filesystem's checksums were computed from even if its UUID has since been
/// changed. Without it, the seed is derived from the UUID, as ext4 does. A seed named
/// in [`OpenOptions::csum_seed`] overrides both, for the image whose stored seed and
/// UUID no longer agree with the checksums it carries.
fn checksummer(&self) -> Crc32c {
match self.csum_seed {
Some(seed) => Crc32c::with_seed(seed),
None if self.feature.has_csum_seed() => Crc32c::with_seed(self.sb.checksum_seed),
None => Crc32c::new(&self.sb.uuid),
}
}
/// Verify every block of the orphan file: that it ends in the orphan magic word and,
/// where the image carries checksums, that its checksum matches. The checksum covers
/// the block's entry array behind the file's identity and the block's own number, so
/// two blocks holding the same entries still carry different checksums.
///
/// Without `orphan_file` there is no such file and nothing to check.
fn verify_orphan_blocks(&mut self, csum: &Crc32c) -> Result<(), ReadError> {
let mut faults = Vec::new();
self.collect_orphan_faults(csum, &mut faults);
faults.into_iter().next().map_or(Ok(()), Err)
}
/// Collect the faults in every block of the orphan file, rather than stopping at the
/// first: a file with two bad blocks yields two. Each block must end in the orphan
/// magic word and, where the image carries checksums, match its checksum, which
/// covers the block's entry array behind the file's identity and the block's own
/// number so two blocks holding the same entries still checksum differently. Without
/// `orphan_file` there is no such file and nothing to check.
fn collect_orphan_faults(&mut self, csum: &Crc32c, faults: &mut Vec<ReadError>) {
if !self.feature.has_orphan_file() {
return;
}
let has_csum = self.feature.has_metadata_csum();
let ino = self.sb.orphan_file_inum;
if ino == 0 {
// The feature promises a file; a superblock naming no inode for it is
// malformed rather than an empty orphan list.
faults.push(ReadError::BadOrphanFile);
return;
}
let inode = match self.inode(ino) {
Ok(inode) => inode,
Err(e) => {
faults.push(e);
return;
}
};
let generation = inode.generation;
// The orphan file is a fixed array of entry blocks, never sparse, so it cannot
// span more blocks than the image holds. One that claims to is malformed — and
// saying so here is what keeps a claimed size from driving the map built below.
let bound = self.materialized_block_bound();
if inode.size.div_ceil(self.block_size as u64) > bound {
faults.push(ReadError::BadOrphanFile);
return;
}
let blocks = match self.data_blocks(&inode) {
Ok(blocks) => blocks,
Err(e) => {
faults.push(e);
return;
}
};
for phys in blocks {
// Every block of the orphan file is materialized: it is a fixed array of
// entry blocks, so a mapping that names none is not a sparse file.
if phys == 0 {
faults.push(ReadError::BadOrphanFile);
continue;
}
let block = match self.block(phys) {
Ok(block) => block,
Err(e) => {
faults.push(e);
continue;
}
};
let stored = match read_orphan_tail(&block) {
Ok(stored) => stored,
Err(_) => {
faults.push(ReadError::BadOrphanFile);
continue;
}
};
if !has_csum {
continue; // no checksums: the magic word is the whole of the format
}
let entries = &block[..orphan_entries_len(block.len())];
let c = recipe::orphan_block(csum, ino, generation, phys, entries);
if c != stored {
faults.push(ReadError::ChecksumMismatch {
object: "orphan block",
index: phys,
stored,
computed: c,
});
}
}
}
/// Verify the checksum tail of every external node in an inode's extent tree.
/// A tree that fits inline in the inode has no external node, so nothing to do:
/// the inode's own checksum already covers its root.
fn verify_extent_nodes(
&mut self,
ino: u32,
inode: &Inode,
csum: &Crc32c,
) -> Result<(), ReadError> {
if !inode.flags.contains(InodeFlags::EXTENTS) {
return Ok(());
}
// Every node in one inode's tree shares a seed.
let seed = recipe::inode_seed(csum, ino, inode.generation);
let mut visited = HashSet::new();
self.verify_extent_subtree(&inode.block, 0, seed, csum, &mut visited)
}
fn verify_extent_subtree(
&mut self,
node: &[u8],
depth: u16,
seed: u32,
csum: &Crc32c,
visited: &mut HashSet<u64>,
) -> Result<(), ReadError> {
if depth > MAX_EXTENT_DEPTH {
return Err(ReadError::BadExtentTree);
}
let ExtentNode::Index { entries, .. } = parse_node(node)? else {
return Ok(());
};
for idx in entries {
// A child reached twice is a cyclic or fan-out tree; bound the walk.
if !visited.insert(idx.leaf) {
return Err(ReadError::BadExtentTree);
}
let child = self.block(idx.leaf)?;
let tail = extent_tail_offset(&child, self.block_size);
let stored = u32::from_le_bytes(
child[tail..tail + EXTENT_TAIL_LEN]
.try_into()
.expect("tail slice is exactly EXTENT_TAIL_LEN bytes"),
);
let computed = csum.crc32c(seed, &child[..tail]);
check_checksum("extent node", idx.leaf, stored, computed)?;
self.verify_extent_subtree(&child, depth + 1, seed, csum, visited)?;
}
Ok(())
}
/// Collect the group-`g` metadata a non-`flex_bg` filesystem places outside the group
/// it belongs to. Without `flex_bg` each group's block bitmap, inode bitmap, and inode
/// table must lie within the group's own block range; `flex_bg` is the feature that
/// permits packing them into the flex head instead, so the check does nothing when it
/// is set. This is geometry, so it holds on the checksumless ext2 and ext3 images too,
/// and it is the placement rule `e2fsck` enforces as "bitmap for group N is not in
/// group". The values come from the descriptor already read; no block is fetched.
fn collect_metadata_placement_faults(
&self,
g: u32,
desc: &GroupDescriptor,
faults: &mut Vec<ReadError>,
) {
if self.feature.has_flex_bg() {
return;
}
let bpg = u64::from(self.sb.blocks_per_group);
if bpg == 0 {
return;
}
// The group spans `blocks_per_group` blocks from its first, and the last group is
// cut short at the end of the filesystem.
let start = u64::from(self.sb.first_data_block) + u64::from(g) * bpg;
let end = start.saturating_add(bpg).min(self.sb.blocks_count);
let in_group = |block: u64| block >= start && block < end;
for (what, block) in [
("block bitmap", desc.block_bitmap),
("inode bitmap", desc.inode_bitmap),
("inode table", desc.inode_table),
] {
if !in_group(block) {
faults.push(ReadError::MetadataOutsideGroup {
what,
group: g,
block,
});
}
}
}
/// Verify group `g`'s block-bitmap and inode-bitmap checksums against the values
/// recorded in its descriptor. Each is a crc32c of the used portion of the bitmap
/// block seeded from the filesystem seed, stored 32 bits wide under a 64-byte
/// descriptor and 16 bits wide under a 32-byte one. A bitmap the descriptor marks
/// uninitialized carries a zero checksum by construction and is not recomputed.
fn verify_bitmap_checksums(&mut self, g: u32, csum: &Crc32c) -> Result<(), ReadError> {
let mut faults = Vec::new();
self.collect_bitmap_faults(g, csum, &mut faults);
faults.into_iter().next().map_or(Ok(()), Err)
}
/// Collect both bitmap faults in group `g`, rather than stopping at the first: a
/// group whose block bitmap and inode bitmap both fail their checksum yields two
/// faults, not one. Each is a crc32c of the used portion of the bitmap block seeded
/// from the filesystem seed, stored 32 bits wide under a 64-byte descriptor and 16
/// wide under a 32-byte one. A bitmap the descriptor marks uninitialized carries a
/// zero checksum by construction and is not recomputed.
fn collect_bitmap_faults(&mut self, g: u32, csum: &Crc32c, faults: &mut Vec<ReadError>) {
let desc = match self.group_descriptor(g) {
Ok(desc) => desc,
Err(e) => {
faults.push(e);
return;
}
};
let wide = self.desc_size() >= GroupDescriptor::SIZE_64;
let fold = |c: u32| if wide { c } else { c & 0xffff };
if desc.flags & BG_BLOCK_UNINIT == 0 {
let len = recipe::block_bitmap_len(self.sb.blocks_per_group);
match self.block(desc.block_bitmap) {
Ok(bmp) => {
let computed = fold(recipe::bitmap(csum, &bmp[..len.min(bmp.len())]));
if computed != desc.block_bitmap_csum {
faults.push(ReadError::ChecksumMismatch {
object: "block bitmap",
index: u64::from(g),
stored: desc.block_bitmap_csum,
computed,
});
}
}
Err(e) => faults.push(e),
}
}
if desc.flags & BG_INODE_UNINIT == 0 {
let len = recipe::inode_bitmap_len(self.sb.inodes_per_group);
match self.block(desc.inode_bitmap) {
Ok(bmp) => {
let computed = fold(recipe::bitmap(csum, &bmp[..len.min(bmp.len())]));
if computed != desc.inode_bitmap_csum {
faults.push(ReadError::ChecksumMismatch {
object: "inode bitmap",
index: u64::from(g),
stored: desc.inode_bitmap_csum,
computed,
});
}
}
Err(e) => faults.push(e),
}
}
}
/// Verify the tail checksum of every block of directory inode `ino`. A block of
/// entries carries a twelve-byte tail whose crc32c covers the block up to it; a
/// hash-tree index block carries an eight-byte tail covering its in-use entries
/// followed by the tail with its checksum field zeroed. Both are seeded from the
/// filesystem seed folded with the directory's number and generation. Non-directory
/// inodes have nothing to check.
fn verify_directory_checksums(
&mut self,
ino: u32,
inode: &Inode,
csum: &Crc32c,
) -> Result<(), ReadError> {
let mut faults = Vec::new();
self.collect_directory_faults(ino, inode, csum, &mut faults);
faults.into_iter().next().map_or(Ok(()), Err)
}
/// Collect the tail-checksum faults in every block of directory inode `ino`, rather
/// than stopping at the first: a directory with two corrupt blocks yields two.
///
/// A directory's blocks fall into three roles, each with its own checksum tail: the
/// root (always logical block 0), the interior index nodes, and the leaves that hold
/// the names. The roles are found by following the index down from the root, not by
/// position, so a tree another tool grew leaves-first is read the same as one this
/// crate wrote. Non-directory inodes have nothing to check.
///
/// Each *physical* block is checked once, as [`scan_extent_node`](Self::scan_extent_node)
/// and [`scan_indirect`](Self::scan_indirect) also do: a block's tail checksum is a
/// property of that block and the directory that owns it, so a map naming one block
/// from many logical offsets — which only a crafted image does — would otherwise
/// report the same verdict once per offset.
fn collect_directory_faults(
&mut self,
ino: u32,
inode: &Inode,
csum: &Crc32c,
faults: &mut Vec<ReadError>,
) {
if inode.mode & 0o170000 != 0o040000 {
return;
}
let indexed = inode.flags.contains(InodeFlags::INDEX);
let base = recipe::inode_seed(csum, ino, inode.generation);
let blocks = match self.data_blocks(inode) {
Ok(blocks) => blocks,
Err(e) => {
faults.push(e);
return;
}
};
let (root_indexed, interior) = match self.directory_index_layout(&blocks, indexed) {
Ok(layout) => layout,
Err(e) => {
faults.push(e);
return;
}
};
let mut checked = HashSet::new();
for (logical, &phys) in blocks.iter().enumerate() {
// A directory has no holes: every logical block is materialized. A zero here
// is a mapping that names none, and there is no block to check.
if phys == 0 {
continue;
}
// One verdict per block, taken at the first logical offset naming it.
if !checked.insert(phys) {
continue;
}
let logical = logical as u64;
let block = match self.block(phys) {
Ok(block) => block,
Err(e) => {
faults.push(e);
continue;
}
};
let count_offset = if logical == 0 && root_indexed {
Some(DX_ROOT_COUNT_OFFSET)
} else if interior.contains(&logical) {
Some(DX_NODE_COUNT_OFFSET)
} else {
None
};
if let Err(e) = self.verify_one_dir_block(phys, &block, base, count_offset, csum) {
faults.push(e);
}
}
}
/// The index layout of a directory: whether its logical block 0 is a hash-tree
/// root, and which of its logical blocks are interior index nodes.
///
/// A hash tree's blocks fall into three roles — the root (always block 0), the
/// interior index nodes, and the leaves — and these are not positional. `mke2fs`
/// and the kernel grow a tree by filling leaves and allocate an interior node only
/// when the level above it overflows, so a foreign tree's interior nodes sit at
/// high logical blocks while its low blocks are leaves; this crate's own writer
/// places them right after the root instead. Following the root's index pointers
/// down its `indirect_levels` levels names the interior nodes wherever they sit, so
/// the two layouts read the same.
///
/// `blocks` is the directory's logical-to-physical map. A child naming a block
/// outside it, naming block 0, or already seen is ignored, so a malformed or cyclic
/// index cannot drive this past the directory's own blocks and each is read at most
/// once. A directory without the index flag, or one whose first block is a hole,
/// has no interior nodes and every block is treated as leaf entries.
fn directory_index_layout(
&mut self,
blocks: &[u64],
indexed: bool,
) -> Result<(bool, HashSet<u64>), ReadError> {
let mut interior = HashSet::new();
if !indexed {
return Ok((false, interior));
}
// Block 0 is the root. A directory flagged indexed whose first block is a hole
// has no readable root to follow.
let Some(root_phys) = blocks.first().copied().filter(|&b| b != 0) else {
return Ok((false, interior));
};
let root = self.block(root_phys)?;
let levels = match read_dx_root_info(&root) {
Ok((_, levels)) => levels,
// A root too short to hold its info is not a tree to walk; block 0 is still
// its root, with no interior nodes below it.
Err(_) => return Ok((true, interior)),
};
// Descend `levels` levels from the root, following each index block's child
// pointers. The blocks reached along the way are the interior nodes; the ones a
// further level down are the leaves, so they are not collected.
let mut frontier = vec![0u64];
for _ in 0..levels {
let mut next = Vec::new();
for &logical in &frontier {
let Some(phys) = blocks.get(logical as usize).copied().filter(|&b| b != 0) else {
continue;
};
let block = self.block(phys)?;
let count_offset = if logical == 0 {
DX_ROOT_COUNT_OFFSET
} else {
DX_NODE_COUNT_OFFSET
};
// A block whose count is malformed contributes no children; its bad
// tail is caught by the checksum pass that follows.
let Ok(entries) = read_dx_entries(&block, count_offset) else {
continue;
};
for entry in entries {
let child = u64::from(entry.block);
// Block 0 is the root, never a child; an out-of-range or repeated
// child is a malformed or cyclic pointer. Skipping all three bounds
// the walk and visits each block at most once.
if child != 0 && (child as usize) < blocks.len() && interior.insert(child) {
next.push(child);
}
}
}
frontier = next;
}
Ok((true, interior))
}
/// Verify one directory block's tail checksum. `count_offset` is `Some` for a
/// hash-tree index block (the offset its entry count sits at) and `None` for a block
/// of entries. `base` is the seed already folded with the directory's number and
/// generation.
fn verify_one_dir_block(
&self,
phys: u64,
block: &[u8],
base: u32,
count_offset: Option<usize>,
csum: &Crc32c,
) -> Result<(), ReadError> {
match count_offset {
None => {
if block.len() < DIR_TAIL_LEN + 4 {
return Ok(());
}
let covered = block.len() - DIR_TAIL_LEN;
let computed = csum.crc32c(base, &block[..covered]);
let stored = get_u32(block, block.len() - 4);
check_checksum("directory block", phys, stored, computed)?;
}
Some(count_offset) => {
let limit = usize::from(get_u16(block, count_offset));
let count = usize::from(get_u16(block, count_offset + 2));
let covered = count_offset + DX_ENTRY_LEN * count;
let tail = dx_tail_offset(count_offset, limit);
// The checksum sits in the last word of the `DX_TAIL_LEN`-byte tail, read
// at `tail + 4` as bytes [tail+4, tail+8), so the block must reach
// `tail + DX_TAIL_LEN`. A stored `limit` or `count` that puts either past
// the block is a fault, not a block to wave through: the kernel treats a
// tail it cannot place as a verification failure, and a clean answer here
// would leave every byte of the block unverified — the one corruption
// shape this pass would otherwise never speak to, since the fields that
// place the tail are inside what the checksum covers.
if covered > block.len() || tail + DX_TAIL_LEN > block.len() {
let (field, value) = if tail + DX_TAIL_LEN > block.len() {
("limit", limit)
} else {
("count", count)
};
return Err(ReadError::Parse(ParseError::InvalidField {
structure: "DxCountLimit",
field,
value: value as u64,
}));
}
// The tail is folded as its own bytes up to `dt_checksum`, then four
// zeros standing in for that field. Only the checksum word is treated as
// absent; `dt_reserved` before it is covered as it lies on disk, so an
// image that leaves anything there still verifies. That is why this is not
// one of `csum`'s shared recipes: the writer folds the tail it wrote, and
// one function could not be both without rejecting healthy foreign images.
let mut c = csum.crc32c(base, &block[..covered]);
c = csum.crc32c(c, &block[tail..tail + DX_CHECKSUM_OFFSET]);
c = csum.crc32c(c, &[0u8; DX_TAIL_LEN - DX_CHECKSUM_OFFSET]);
let stored = get_u32(block, tail + 4);
check_checksum("directory block", phys, stored, c)?;
}
}
Ok(())
}
/// Verify the `h_checksum` of an inode's external attribute block, if it has one.
/// The checksum is a crc32c over the whole block with its own field zeroed, seeded
/// from the filesystem seed folded with the block number as a 64-bit value. An inode
/// with no external attribute block has nothing to check; the inline attribute
/// region is covered by the inode's own checksum instead.
fn verify_xattr_block_checksum(
&mut self,
inode: &Inode,
csum: &Crc32c,
) -> Result<(), ReadError> {
let phys = inode.file_acl;
if phys == 0 {
return Ok(());
}
let mut block = self.block(phys)?;
if block.len() < 20 {
return Ok(());
}
let stored = get_u32(&block, XATTR_CHECKSUM_OFFSET);
crate::ondisk::put_u32(&mut block, XATTR_CHECKSUM_OFFSET, 0);
let c = recipe::xattr_block(csum, phys, &block);
check_checksum("xattr block", phys, stored, c)?;
Ok(())
}
/// Scan the whole image and collect every deviation as an [`Anomaly`], reporting
/// rather than rejecting: the lenient counterpart to the fail-fast accessors.
///
/// The scan walks the superblock, every group descriptor and its bitmap checksums,
/// and every in-use inode with its extent tree, directory-block tails, and external
/// attribute block, plus the journal superblock — checking each metadata checksum
/// the image carries and the bounds of every reference it follows. It never stops at
/// the first finding and, like every read, never panics on malformed input. Apply a
/// [`ReadPolicy`] threshold to the result with [`ScanReport::has_fatal`].
///
/// Every allocation the scan makes is bounded by the bytes the source actually holds,
/// not by a count an image claims: the objects walked are capped at the groups and
/// inodes the source can physically hold, each metadata block is read once, and the
/// findings themselves stop at [`Limits::max_findings`] with
/// [`ScanReport::is_truncated`] recording that they did. That is what makes this the
/// path to point at an image that may have been built to be hostile.
#[must_use]
pub fn scan(&mut self) -> ScanReport {
let mut anomalies = Findings::new(self.limits.max_findings);
let has_csum = self.feature.has_metadata_csum();
let csum = self.checksummer();
// Bound the walk by the length the source reports, so a count field a bit-flip
// inflated cannot drive a loop past the end of it.
let source_len = self.source_len();
let block_size = self.block_size as u64;
let max_blocks = source_len / block_size;
// An `incompat` feature the reader does not follow taints the whole image: it may
// change the layout of the structures walked below, so it is reported first. A
// strict open would have refused it, so this is what a lenient scan surfaces in
// its place.
let unsupported = unsupported_incompat(self.feature.incompat);
if unsupported != 0 {
anomalies.push(ReadError::UnsupportedIncompat { bits: unsupported }.anomaly());
}
// Superblock checksum.
if has_csum {
let computed = superblock_checksum(&self.sb_raw);
if computed != self.sb.checksum {
anomalies.push(
ReadError::ChecksumMismatch {
object: "superblock",
index: 0,
stored: self.sb.checksum,
computed,
}
.anomaly(),
);
}
}
// Every group descriptor: that it reads, and its checksum. The group count is
// capped at the groups the source can physically hold.
let bpg = u64::from(self.sb.blocks_per_group.max(1));
let group_bound = (max_blocks / bpg)
.saturating_add(2)
.min(u64::from(u32::MAX)) as u32;
let desc_size = self.desc_size();
for g in 0..self.group_count().min(group_bound) {
if anomalies.is_full() {
break;
}
let raw = match self.group_descriptor_raw(g) {
Err(e) => {
anomalies.push(e.anomaly());
continue;
}
Ok(raw) => raw,
};
match GroupDescriptor::read_from(&raw, desc_size) {
Err(e) => anomalies.push(ReadError::Parse(e).anomaly()),
Ok(desc) => {
// Placement is geometry, not checksums: it is checked on every image,
// including the checksumless ext2 and ext3 ones whose every group must
// hold its own metadata.
let mut placement = Vec::new();
self.collect_metadata_placement_faults(g, &desc, &mut placement);
for e in &placement {
anomalies.push(e.anomaly());
}
if has_csum {
let computed = self.descriptor_checksum(g, &raw, &csum);
if computed != desc.checksum {
anomalies.push(
ReadError::ChecksumMismatch {
object: "group descriptor",
index: u64::from(g),
stored: u32::from(desc.checksum),
computed: u32::from(computed),
}
.anomaly(),
);
}
let mut faults = Vec::new();
self.collect_bitmap_faults(g, &csum, &mut faults);
for e in &faults {
anomalies.push(anomaly_in_bitmap(e, g, Category::Bitmap));
}
}
}
}
}
// Every in-use inode, taken from the inode bitmaps: that it reads, its checksum,
// and its block mapping. Reading the bitmaps rather than assuming inodes run
// densely from one finds an inode a live filesystem scattered past that range
// and skips a never-initialized table slot, which is not an inode to read. The
// examined count is capped at the inodes the source can hold.
let max_inodes = source_len / u64::from(self.sb.inode_size.max(128));
// One visited set for the whole scan: a metadata block belongs to a single
// structure, so an external extent or indirect node read once is never read
// again, and a crafted or cyclic tree cannot fan the walk out without bound.
let mut visited = HashSet::new();
let inode_size = self.sb.inode_size;
let mut examined = 0u64;
// The same physical bound the descriptor loop uses. A group past it has no
// bitmap block inside the source, so every iteration could only record that the
// bitmap is unreadable — and a superblock claiming `u32::MAX` groups would turn
// that into billions of anomalies, which is an allocation driven by a claimed
// count rather than by the bytes that exist. The `examined` cap below cannot
// stand in for this one: it is only reached once a group yields an inode, and a
// group whose bitmap does not read yields none.
'inodes: for g in 0..self.group_count().min(group_bound) {
if examined >= max_inodes {
break;
}
// A report already at its cap collects nothing more, so the walk stops here
// rather than reading every remaining group's bitmap for findings it would
// discard.
if anomalies.is_full() {
break;
}
let in_use = match self.group_in_use_inodes(g) {
Ok(v) => v,
Err(e) => {
anomalies.push(anomaly_in_bitmap(&e, g, Category::Bitmap));
continue;
}
};
for n in in_use {
if examined >= max_inodes {
break 'inodes;
}
// The walk stops at the cap between inodes rather than mid-inode, so a
// report holds either the whole of what an inode had to say or none of it.
// Within one inode the accumulator is what bounds the collection: it drops
// what would exceed the cap, so the memory a scan holds never runs past the
// report it will produce.
if anomalies.is_full() {
break 'inodes;
}
examined += 1;
let raw = match self.inode_raw(n) {
Err(e) => {
anomalies.push(anomaly_at_inode(&e, n));
continue;
}
Ok(raw) => raw,
};
match Inode::read_from(&raw, inode_size) {
Err(e) => anomalies.push(anomaly_at_inode(&ReadError::Parse(e), n)),
Ok(mut inode) => {
self.mask_ungated_halves(&mut inode);
if has_csum {
let (stored, computed) = self.inode_checksum(n, &raw, &csum);
if computed != stored {
anomalies.push(
ReadError::ChecksumMismatch {
object: "inode",
index: u64::from(n),
stored,
computed,
}
.anomaly(),
);
}
}
// What the inode's own bytes say against what the superblock's
// feature words promise. Checked on every image, checksums or not:
// it is a disagreement between two fields, not a corruption a
// checksum would catch.
self.scan_feature_coherence(n, &inode, &mut anomalies);
self.scan_acl_values(n, &inode, &mut anomalies);
self.scan_inode_map(
n,
&inode,
has_csum,
&csum,
&mut visited,
&mut anomalies,
);
// A directory entry whose name a real filesystem could not hold, or
// whose inode the filesystem does not have, is a structural fault
// whether or not the image carries checksums, so both are checked
// outside the `has_csum` block below.
self.scan_dirents(n, &inode, &mut anomalies);
if has_csum {
let mut faults = Vec::new();
self.collect_directory_faults(n, &inode, &csum, &mut faults);
for e in &faults {
anomalies.push(anomaly_in_mapping(e, n, Category::Directory));
}
if let Err(e) = self.verify_xattr_block_checksum(&inode, &csum) {
anomalies.push(anomaly_in_mapping(&e, n, Category::Xattr));
}
}
}
}
}
}
// The journal superblock is structural: a malformed one is a Journal anomaly.
if let Err(e) = self.journal_superblock() {
anomalies.push(e.anomaly());
}
// The orphan file: its blocks' magic words always, and their checksums when the
// image carries them. Its length is fixed by the file itself rather than by any
// per-inode claim, so it is walked whatever the inode loop found.
let mut orphan_faults = Vec::new();
self.collect_orphan_faults(&csum, &mut orphan_faults);
let orphan_ino = self.sb.orphan_file_inum;
for e in &orphan_faults {
anomalies.push(anomaly_in_mapping(e, orphan_ino, Category::Orphan));
}
anomalies.into_report(self.block_size as u32)
}
/// Collect the anomalies in one inode's block mapping, whichever kind it carries.
///
/// For an extent tree: external-node checksum mismatches, unreadable or
/// unparseable nodes, index pointers out of range, a tree deeper than the reader
/// follows, and leaf runs that fall outside the filesystem. An inode whose mapping
/// fits inline has no external node to check.
///
/// For a classic block map: that every pointer names a block the filesystem has.
/// An indirect block carries no checksum — the classic map predates them — so
/// there is nothing to recompute, but the map is still walked. Skipping it would
/// let a scan report a clean bill of health for a mapping it never looked at, which
/// on ext2 and ext3, where *every* inode is mapped this way, means reporting a
/// clean bill of health for the whole filesystem.
fn scan_inode_map(
&mut self,
ino: u32,
inode: &Inode,
has_csum: bool,
csum: &Crc32c,
visited: &mut HashSet<u64>,
out: &mut Findings<Anomaly>,
) {
if !inode.flags.contains(InodeFlags::EXTENTS) {
// A fast symlink's target and a device node's major and minor share the
// i_block area a classic map's pointers would occupy; only an inode that
// maps data has pointers there to validate. Walking the others would read a
// target string or a device number as block numbers.
if !maps_data(inode, self.block_size) {
return;
}
if let Err(e) = self.scan_block_map(inode, visited) {
// A classic block map is no subsystem of its own: a pointer that falls
// outside the filesystem belongs to the inode that holds the map, or the
// directory the map is for.
let owner = if is_dir(inode) {
Category::Directory
} else {
Category::Inode
};
out.push(anomaly_in_mapping(&e, ino, owner));
}
return;
}
// The inode is extent-mapped. Whether the feature word agrees is reported by
// `scan_feature_coherence`, which holds every such rule; the tree itself is
// scanned here whatever that word says.
//
// Every node in one inode's tree shares a seed.
let ctx = ExtentScanCtx {
ino,
seed: recipe::inode_seed(csum, ino, inode.generation),
has_csum,
csum,
};
self.scan_extent_node(&ctx, &inode.block, 0, visited, out);
}
/// Report every disagreement between one inode's own bytes and the feature words the
/// superblock advertises.
///
/// A feature word is a promise about what the filesystem's structures look like, so an
/// inode that carries a structure the word denies makes the image self-contradictory:
/// a reader cannot tell which of the two to believe, and a kernel that trusts the word
/// reads the inode wrongly. Each rule here is one `e2fsck` enforces, and each has a
/// counterpart on the writing side, where the formatter refuses to emit the pair —
/// these are what catch the same incoherence in an image this crate did not write.
///
/// The mapping flag and the attribute-block pointer are structural: they decide how
/// the reader interprets bytes, so a disagreement leaves it unable to vouch for what
/// it read. The size and the hash-index flag are conformance deviations, read
/// correctly either way — for the size and for a directory's flag, what is missing is
/// the word that should advertise it; for the flag on anything but a directory, the
/// flag itself describes an organization that file does not have.
fn scan_feature_coherence(&self, ino: u32, inode: &Inode, out: &mut Findings<Anomaly>) {
if inode.flags.contains(InodeFlags::EXTENTS) && !self.feature.has_extents() {
out.push(ReadError::ExtentFlagWithoutFeature { inode: ino }.anomaly());
}
if inode.file_acl != 0 && !self.feature.has_ext_attr() {
out.push(
ReadError::XattrBlockWithoutFeature {
inode: ino,
block: inode.file_acl,
}
.anomaly(),
);
}
if is_regular(inode) && inode.size >= LARGE_FILE_MIN_SIZE && !self.feature.has_large_file()
{
out.push(
ReadError::LargeFileWithoutFeature {
inode: ino,
size: inode.size,
}
.anomaly(),
);
}
// The hash-index flag is two rules, not one, and each names a different thing to
// fix: a directory whose flag the feature words deny, and an inode carrying the
// flag that is not a directory at all. An inode can be both. Reporting the first
// for the second would describe a regular file as a hash-indexed directory.
if inode.flags.contains(InodeFlags::INDEX) {
if is_dir(inode) {
if !self.feature.has_dir_index() {
out.push(ReadError::IndexFlagWithoutFeature { inode: ino }.anomaly());
}
} else {
out.push(ReadError::IndexFlagOnNonDirectory { inode: ino }.anomaly());
}
}
}
/// Flag inode `ino` when a `system.posix_acl_*` attribute holds bytes that are not a
/// stored ACL.
///
/// Checked on every image rather than only a checksummed one: the bytes are exactly as
/// they were written, so no checksum disagrees with them. What disagrees is the name
/// and the value — an attribute the kernel will read as an ACL and refuse — and a read
/// of that inode's attributes fails, so this is what says which inode holds it.
///
/// Only the malformed-ACL failure is reported. Every other way reading the attributes
/// can fail is the attribute region's own, and the scan reports those where it walks
/// the region.
fn scan_acl_values(&mut self, ino: u32, inode: &Inode, out: &mut Findings<Anomaly>) {
if let Err(e @ ReadError::MalformedAcl { .. }) = self.xattrs(inode) {
out.push(anomaly_at_inode(&e, ino));
}
}
/// Flag directory `ino` for the faults its entry records carry: a name a real ext4
/// filesystem could not hold, and a reference to an inode the filesystem does not
/// have. A non-directory has no entries.
///
/// Both are structural, and both are checked on every image rather than only a
/// checksummed one — the records are exactly as they were written, so no checksum
/// disagrees with them, and an image crafted to carry either recomputes its checksums
/// over it.
///
/// A hostile name carries a path separator or a NUL (see [`is_hostile_entry`]) and is
/// impossible on a kernel-checked filesystem. A reference past `s_inodes_count` names
/// an inode that cannot exist, which is what `e2fsck` reports as an entry with an
/// invalid inode number. [`read_dir`](Self::read_dir) refuses both, and reporting them
/// here is what finds them across the whole image without resolving anything — a
/// directory nothing ever lists is one no read would reach.
///
/// The blocks are walked here rather than through [`read_dir`](Self::read_dir), which
/// is strict: it abandons a whole directory at its first bad record, and a directory
/// carrying either fault is exactly the one likely to carry a malformed record too —
/// so reading through it would skip the check on the directories that most need it. A
/// record this cannot parse ends that *block*, and the walk goes on to the next.
///
/// Each physical block is read once, and each kind of fault is reported once for the
/// whole directory, so a crafted map cannot make one inode's finding into thousands.
/// The walk carries on past a fault it has reported, since the records after it may
/// hold the other kind, and stops once both have been found.
fn scan_dirents(&mut self, ino: u32, inode: &Inode, out: &mut Findings<Anomaly>) {
if !is_dir(inode) {
return;
}
// An unreadable mapping is already faulted by the block-map and checksum checks,
// so it yields nothing here rather than a second, redundant fault.
let Ok(blocks) = self.data_blocks(inode) else {
return;
};
let mut read = HashSet::new();
let (mut bad_name, mut bad_reference) = (false, false);
for phys in blocks {
if phys == 0 || !read.insert(phys) {
continue;
}
let Ok(block) = self.block(phys) else {
continue;
};
let mut off = 0;
while off < block.len() {
let Ok((entry, rec_len)) = DirEntry::read_from(&block[off..], self.block_size)
else {
break; // this block is malformed from here on; the next one may not be
};
// The same guard `read_dir` carries, for the same reason: `read_from`
// returns nothing shorter than an eight-byte header, and a scan that could
// stop advancing would never finish the image.
if rec_len == 0 {
break;
}
// A zero inode is an unused slot: it holds neither a reference nor a name
// the directory carries.
if entry.inode != 0 {
if !bad_name && is_hostile_entry(&entry.name) {
out.push(anomaly_at_inode(
&ReadError::HostileName { inode: entry.inode },
ino,
));
bad_name = true;
}
if !bad_reference && let Err(e) = self.check_dir_entry_inode(entry.inode) {
out.push(anomaly_at_inode(&e, ino));
bad_reference = true;
}
if bad_name && bad_reference {
return;
}
}
off += rec_len;
}
}
}
fn scan_extent_node(
&mut self,
ctx: &ExtentScanCtx,
node: &[u8],
depth: u16,
visited: &mut HashSet<u64>,
out: &mut Findings<Anomaly>,
) {
if depth > MAX_EXTENT_DEPTH {
out.push(anomaly_at_inode(&ReadError::BadExtentTree, ctx.ino));
return;
}
let parsed = match parse_node(node) {
Ok(n) => n,
Err(e) => {
out.push(anomaly_at_inode(&ReadError::Parse(e), ctx.ino));
return;
}
};
match parsed {
ExtentNode::Leaves(leaves) => {
for leaf in leaves {
let end = leaf.start.saturating_add(u64::from(leaf.len));
if leaf.start >= self.sb.blocks_count || end > self.sb.blocks_count {
out.push(anomaly_in_mapping(
&ReadError::OutOfRange {
what: "block",
index: leaf.start,
},
ctx.ino,
Category::ExtentTree,
));
}
}
}
ExtentNode::Index { entries, .. } => {
for idx in entries {
// Read each external node once: a repeated or cyclic child pointer
// is skipped, so a crafted tree cannot fan the walk out unbounded.
if !visited.insert(idx.leaf) {
continue;
}
let child = match self.block(idx.leaf) {
Ok(c) => c,
Err(e) => {
out.push(anomaly_in_mapping(&e, ctx.ino, Category::ExtentTree));
continue;
}
};
if ctx.has_csum {
let tail = extent_tail_offset(&child, self.block_size);
let stored = u32::from_le_bytes(
child[tail..tail + EXTENT_TAIL_LEN]
.try_into()
.expect("tail slice is exactly EXTENT_TAIL_LEN bytes"),
);
let computed = ctx.csum.crc32c(ctx.seed, &child[..tail]);
if stored != computed {
out.push(anomaly_at_inode(
&ReadError::ChecksumMismatch {
object: "extent node",
index: idx.leaf,
stored,
computed,
},
ctx.ino,
));
}
}
self.scan_extent_node(ctx, &child, depth + 1, visited, out);
}
}
}
}
/// Read inode `number`.
///
/// # Errors
///
/// [`ReadError::NoSuchInode`] if the number is zero or past the inode count;
/// [`ReadError`] variants if its bytes cannot be located or parsed.
pub fn inode(&mut self, number: u32) -> Result<Inode, ReadError> {
let raw = self.inode_raw(number)?;
let mut inode = Inode::read_from(&raw, self.sb.inode_size)?;
self.mask_ungated_halves(&mut inode);
Ok(inode)
}
/// Drop the high half of the two inode fields whose upper bytes are another field
/// entirely when the feature that defines them is clear, and fold the block count's
/// per-inode unit into the one every consumer here counts in.
///
/// The byte layer joins both halves unconditionally, because whether the upper bytes are
/// a high half at all is a question about the *feature words*, which the byte layer does
/// not have. Here it does, so this is where the gate belongs — and it is the same rule
/// the kernel applies, in `ext4_inode_blocks` and `ext4_iget` respectively.
///
/// Without `huge_file`, offsets 0x74 and 0x75 are ext2's `l_i_frag` and `l_i_fsize`;
/// without `64bit`, 0x76 is `i_pad1`. Nothing a Linux filesystem writes leaves them
/// non-zero, which is exactly why an image that does is worth being right about:
/// `e2fsck` carries a problem code for each, so images with bytes there occur. Read as
/// high halves they change what the inode *is* — a fast symlink classifies as slow and
/// its target is walked as a block map, or a slow symlink classifies as fast and block
/// pointers are returned as the target.
fn mask_ungated_halves(&self, inode: &mut Inode) {
if !self.feature.has_huge_file() {
inode.blocks &= u64::from(u32::MAX);
} else if inode.flags.contains(InodeFlags::HUGE_FILE) {
// Under the feature, the per-inode flag moves the field's unit from 512-byte
// sectors to filesystem blocks. Every consumer here counts sectors — the
// fast-symlink discount most of all, where a unit read wrong classifies the
// target as a block map — so the unit is folded away where the halves are,
// which is what the kernel's own accessor does.
inode.blocks = inode.blocks.saturating_mul(self.block_size as u64 / 512);
}
if !self.feature.is_64bit() {
inode.file_acl &= u64::from(u32::MAX);
}
}
/// Read inode `number` exactly as it sits on disk, at this filesystem's
/// `s_inode_size`.
///
/// Its checksum covers these bytes. An inode carries fields this crate does not
/// model — `l_i_version`, which the kernel bumps on every update, and `i_projid` —
/// so recomputing the checksum from a re-serialized [`Inode`] would zero them and
/// reject every inode of any filesystem that has been mounted.
fn inode_raw(&mut self, number: u32) -> Result<Vec<u8>, ReadError> {
if number == 0 || number > self.sb.inodes_count {
return Err(ReadError::NoSuchInode { inode: number });
}
let ipg = self.sb.inodes_per_group;
if ipg == 0 {
// A malformed superblock with no inodes per group has nowhere to place
// this inode; report it missing rather than dividing by zero.
return Err(ReadError::NoSuchInode { inode: number });
}
let group = (number - 1) / ipg;
let index = u64::from((number - 1) % ipg);
let desc = self.group_descriptor(group)?;
let isize = self.sb.inode_size as usize;
// The inode table block comes from the group descriptor, so a malformed one
// can push the byte offset past the 64-bit range: treat that as out of range
// rather than overflowing. `index * isize` is bounded by the group geometry.
let off = offset_of(
index * isize as u64,
desc.inode_table,
self.block_size as u64,
)
.ok_or(ReadError::OutOfRange {
what: "inode",
index: u64::from(number),
})?;
// The table is placed by a group descriptor, so these bytes are a reference like
// any other and must lie inside the filesystem. What is bounded is the inode's
// whole byte range, through the block its last byte falls in rather than the block
// the table begins in: a table starting inside the filesystem and running past its
// final block holds inodes on both sides of the boundary, and only the ones wholly
// inside it are this filesystem's. `read_at` bounds the same bytes by the source,
// which on an image embedded in a larger one is a different bound and not this one.
let last_block = off.saturating_add(isize as u64 - 1) / self.block_size as u64;
if last_block >= self.sb.blocks_count {
return Err(ReadError::OutOfRange {
what: "inode",
index: u64::from(number),
});
}
match self.read_at(off, isize) {
Err(ReadError::OutOfRange { .. }) => Err(ReadError::OutOfRange {
what: "inode",
index: u64::from(number),
}),
other => other,
}
}
/// Resolve an inode's extent tree to its leaves, following index nodes to any
/// depth.
///
/// # Errors
///
/// [`ReadError::BadExtentTree`] if the tree exceeds [`MAX_EXTENT_DEPTH`];
/// [`ReadError`] variants if a node cannot be read.
#[cfg(test)]
fn extent_leaves(
&mut self,
inode: &Inode,
) -> Result<Vec<crate::ondisk::ExtentLeaf>, ReadError> {
self.extent_leaves_in(inode, 0..u64::MAX)
}
/// The leaves whose logical runs meet `window`, following only the index entries whose
/// subtrees could hold one.
///
/// An index entry names the first logical block its subtree covers and the entries of a
/// node are sorted, so the next entry's first block is where this one's coverage ends —
/// which is enough to skip a whole subtree without reading a block of it. That is what
/// makes reading part of a file cost part of a tree: without it every window rebuilds
/// the entire tree, so a read through an 8 KiB buffer walks it once per two blocks, and
/// a fragmented file streamed to an archive costs a tree walk per window for as many
/// windows as it has.
///
/// # Errors
///
/// [`ReadError::BadExtentTree`] if the tree exceeds [`MAX_EXTENT_DEPTH`];
/// [`ReadError`] variants if a node cannot be read.
fn extent_leaves_in(
&mut self,
inode: &Inode,
window: std::ops::Range<u64>,
) -> Result<Vec<crate::ondisk::ExtentLeaf>, ReadError> {
let mut leaves = Vec::new();
// One visited set per tree: a well-formed extent tree references each external
// node once, so a repeated child pointer is a cycle or fan-out and bounds the
// walk to the blocks that actually exist rather than looping.
let mut visited = HashSet::new();
self.walk_extent_node(&inode.block, 0, &window, &mut visited, &mut leaves)?;
// Leaves come out in logical order because entries within a node are sorted
// and index nodes are visited in order.
Ok(leaves)
}
fn walk_extent_node(
&mut self,
node: &[u8],
depth: u16,
window: &std::ops::Range<u64>,
visited: &mut HashSet<u64>,
out: &mut Vec<crate::ondisk::ExtentLeaf>,
) -> Result<(), ReadError> {
if depth > MAX_EXTENT_DEPTH {
return Err(ReadError::BadExtentTree);
}
match parse_node(node)? {
ExtentNode::Leaves(leaves) => {
// Validate each leaf against the block count before it reaches a reader
// that would allocate for and read every block it names, the same bound
// `scan_extent_node` applies. A leaf past the filesystem is a reference
// out of range, not silently followed.
for leaf in &leaves {
let end = leaf.start.saturating_add(u64::from(leaf.len));
if leaf.start >= self.sb.blocks_count || end > self.sb.blocks_count {
return Err(ReadError::OutOfRange {
what: "block",
index: leaf.start,
});
}
}
// Every leaf in the node is validated, whatever the window: a reference
// out of range is refused wherever it sits, so which part of a file a
// caller read cannot decide whether its map is judged. Only the leaves the
// window meets are collected.
out.extend(leaves.into_iter().filter(|leaf| {
let lo = u64::from(leaf.block);
let hi = lo.saturating_add(u64::from(leaf.len));
hi > window.start && lo < window.end
}));
}
ExtentNode::Index { entries, .. } => {
for (at, idx) in entries.iter().enumerate() {
// Where this subtree's coverage ends: the next entry's first logical
// block, since the entries of a node are sorted and cover the logical
// range between them. The last entry runs to the end of the file, which
// nothing here bounds, so it is open.
let lo = u64::from(idx.block);
let hi = entries
.get(at + 1)
.map_or(u64::MAX, |next| u64::from(next.block));
// Every subtree is checked against `visited` whether or not it is
// descended into: a repeated pointer is a cyclic tree, and a window that
// skipped one would make the refusal depend on which part was read.
if !visited.insert(idx.leaf) {
return Err(ReadError::BadExtentTree);
}
if hi <= window.start || lo >= window.end {
continue;
}
let child = self.block(idx.leaf)?;
self.walk_extent_node(&child, depth + 1, window, visited, out)?;
}
}
}
Ok(())
}
/// Reject a reference naming a block the filesystem does not have.
///
/// This is the filesystem's own boundary, and it is a different bound from the
/// source's length: an image that occupies a partition or a region of a larger file
/// has bytes past its final block that a reference can still reach, and following one
/// would read whatever lies beyond the filesystem as its own metadata. The two are
/// kept together — this bound rejects the reference, and the source's length still
/// catches an image truncated short of the blocks it claims.
///
/// [`block`](Self::block) applies it to every block it reads. It is called directly
/// where a pointer is validated without being read: a forensic walk records that a
/// mapping names a block outside the filesystem rather than fetching what it names.
fn check_block(&self, block: u64) -> Result<(), ReadError> {
if block >= self.sb.blocks_count {
return Err(ReadError::OutOfRange {
what: "block",
index: block,
});
}
Ok(())
}
/// Reject a directory entry naming an inode the filesystem does not have.
///
/// An entry's inode number is a reference like a block pointer, and the superblock's
/// inode count is what bounds it. Inodes are numbered from one, and the zero an
/// unused slot carries names nothing at all, so it is not a reference and never
/// reaches here.
fn check_dir_entry_inode(&self, inode: u32) -> Result<(), ReadError> {
if inode > self.sb.inodes_count {
return Err(ReadError::DirEntryNoSuchInode { inode });
}
Ok(())
}
/// Map an inode's logical blocks to physical ones, in logical order, with a zero
/// standing for a block that reads as zeros without occupying any.
///
/// This is the one place that decides how an inode maps its blocks. An inode with
/// [`InodeFlags::EXTENTS`] roots an extent tree; every other inode uses the classic
/// direct/indirect map, which is how ext2 and ext3 map every file, and how ext4
/// still maps the resize inode. Reading the second as the first finds an extent
/// header where a block number lives and calls a healthy filesystem corrupt.
///
/// The map holds one entry per logical block the file's size covers, so it is
/// bounded by that size — not by the blocks physically present. A hole costs no
/// block on disk, so a sparse file's size can far exceed the filesystem it lives
/// on, and its logical offsets can too; bounding the map by the physical block
/// count would truncate such a file or reject a valid high-offset extent. The
/// bound is instead the 2^32-block logical ceiling ([`MAX_LOGICAL_BLOCKS`]), so a
/// self-consistent image claiming a large sparse file drives an allocation up to
/// that claimed size. Each physical block *number* the map records is still
/// checked against the filesystem's block count.
///
/// A *directory* is the exception, and is bounded by the blocks the image physically
/// holds. A directory is never sparse — the kernel materializes every logical block it
/// declares — so one claiming more blocks than the filesystem has is malformed
/// whatever its size field says, and reading it as though it might be sparse would let
/// a two-byte edit to `i_size` drive a map of billions of entries. That bound is
/// loss-free on any well-formed image, where a directory's blocks are blocks that
/// exist.
///
/// A hostile `i_size` and a genuine sparse file are the same shape — a small
/// physical footprint under a large logical size — so for a regular file this
/// materialization trusts the declared size, and a whole-file read through
/// [`read_data`](Self::read_data) allocates in proportion to it. The forensic
/// [`scan`](Self::scan) path never materializes a file's map at all, and bounds what
/// it collects, so it is the bounded way to inspect an untrusted image.
///
/// # Errors
///
/// [`ReadError::OutOfRange`] if the mapping names a block outside the filesystem;
/// [`ReadError::BadExtentTree`] if an extent tree is cyclic or too deep;
/// [`ReadError`] variants if a mapping block cannot be read.
fn data_blocks(&mut self, inode: &Inode) -> Result<Vec<u64>, ReadError> {
let want = self.logical_block_count(inode);
self.map_window(inode, 0, want)
}
/// The blocks the image can actually hold: the filesystem's own block count, bounded
/// by the blocks the source is long enough to carry.
///
/// This is the ceiling on any structure that is *not* sparse — a directory, the orphan
/// file — whose logical extent is therefore a physical one. Both terms matter: the
/// first is the filesystem's own account of itself, and the second is the only one a
/// crafted superblock cannot inflate.
fn materialized_block_bound(&mut self) -> u64 {
self.sb
.blocks_count
.min(self.source_len() / self.block_size as u64)
}
/// How many logical blocks an inode's size covers — the length of the map
/// [`data_blocks`](Self::data_blocks) builds for it.
///
/// The classic map is positional and carries no length of its own, so this is what
/// says where it ends; for an extent tree it is what extends a file whose final blocks
/// are a hole the tree does not name. It is a logical count, capped at the logical
/// ceiling and not at the physical block count — that is what keeps a sparse file
/// whole — except for a directory, which holds no holes and so cannot span more blocks
/// than the image has.
fn logical_block_count(&mut self, inode: &Inode) -> usize {
let want = usize::try_from(
inode
.size
.div_ceil(self.block_size as u64)
.min(MAX_LOGICAL_BLOCKS),
)
.unwrap_or(usize::MAX);
if is_dir(inode) {
let bound = self.materialized_block_bound();
return want.min(usize::try_from(bound).unwrap_or(usize::MAX));
}
want
}
/// The logical-to-physical map of `count` logical blocks beginning at logical block
/// `first`, with a zero standing for a block that reads as zeros without occupying
/// any. The returned map is exactly `count` long.
///
/// This is the one place that decides how an inode maps its blocks, and it is windowed
/// so that reading part of a file costs part of a map: only the mapping structures the
/// window touches are read, so a block far into a file is reached through the indirect
/// blocks above it rather than by materializing everything before it. A caller that
/// needs only the head of a file — the journal's superblock is its first block — asks
/// for a window of one.
///
/// Pointers outside the window are neither read nor validated: a read validates the
/// map it follows. The forensic [`scan`](Self::scan) is what validates a whole map.
fn map_window(
&mut self,
inode: &Inode,
first: usize,
count: usize,
) -> Result<Vec<u64>, ReadError> {
// Not every inode's `i_block` area holds block pointers. A *fast* symlink keeps
// its target string there, and a device node its major and minor; reading either
// as a mapping interprets a filename or a device number as block numbers.
if count == 0 || !maps_data(inode, self.block_size) {
return Ok(Vec::new());
}
// Zero-filled, so every hole the walk below leaves untouched already reads as one.
let mut map = vec![0u64; count];
if inode.flags.contains(InodeFlags::EXTENTS) {
self.extent_window(inode, first, &mut map)?;
} else {
self.block_map_window(inode, first, &mut map)?;
}
Ok(map)
}
/// Place the part of an inode's extent tree that falls in the window.
///
/// [`extent_leaves_in`](Self::extent_leaves_in) descends only the index entries whose
/// subtrees the window meets, and has already checked every leaf's physical run against
/// the block count — so placing a leaf here is a matter of its logical offset: a run is
/// clipped to the window and a run entirely outside it is skipped.
fn extent_window(
&mut self,
inode: &Inode,
first: usize,
map: &mut [u64],
) -> Result<(), ReadError> {
let end = first.saturating_add(map.len());
let window = first as u64..end as u64;
for leaf in self.extent_leaves_in(inode, window)? {
let lo = leaf.block as usize;
let hi = lo.saturating_add(leaf.len as usize);
// An uninitialized run occupies blocks but holds no data: it reads back as
// zeros, exactly as a hole does, so it is left as one.
if hi <= first || lo >= end || !leaf.initialized {
continue;
}
for logical in lo.max(first)..hi.min(end) {
map[logical - first] = leaf.start + (logical - lo) as u64;
}
}
Ok(())
}
/// Place the part of an inode's classic block map that falls in the window: twelve
/// direct pointers, then one, two, and three levels of indirection, each level
/// covering `per_block^level` logical blocks after the last.
///
/// A zero pointer is a hole, at every level — a hole in an indirect pointer means
/// every block that indirect block would have mapped is a hole too, which is how a
/// sparse ext2 file stores a gigabyte of nothing in one word. A level whose span lies
/// outside the window is skipped without reading its blocks at all.
fn block_map_window(
&mut self,
inode: &Inode,
first: usize,
map: &mut [u64],
) -> Result<(), ReadError> {
let end = first.saturating_add(map.len());
for i in first..DIRECT_BLOCKS.min(end) {
let ptr = u64::from(get_u32(&inode.block, i * 4));
if ptr != 0 {
self.check_block(ptr)?;
}
map[i - first] = ptr;
}
let per_block = self.block_size / 4;
let mut base = DIRECT_BLOCKS;
for level in 1..=INDIRECT_LEVELS {
if base >= end {
break;
}
let next = base.saturating_add(per_block.saturating_pow(level));
if next > first {
let slot = DIRECT_BLOCKS + level as usize - 1;
let root = u64::from(get_u32(&inode.block, slot * 4));
self.walk_indirect_window(root, level, base, first, map)?;
}
base = next;
}
Ok(())
}
/// Place the blocks an indirect block maps that fall in the window, `level` levels
/// deep, where the block's own span begins at logical block `base`.
///
/// The recursion is at most [`INDIRECT_LEVELS`] deep by construction, one block is read
/// per level, and a slot whose span misses the window is skipped without descending
/// into it — so a crafted map can neither exhaust the stack nor drive reads without
/// bound, and a window deep into a sparse file costs one read per level rather than a
/// walk of everything before it.
fn walk_indirect_window(
&mut self,
block: u64,
level: u32,
base: usize,
first: usize,
map: &mut [u64],
) -> Result<(), ReadError> {
// A hole this far up stands for every block below it, which the zero-filled map
// already records.
if block == 0 {
return Ok(());
}
let buf = self.block(block)?;
let per_block = self.block_size / 4;
// What one slot of this block covers: a slot of a single-indirect block is one
// logical block, and each further level multiplies by the pointers a block holds.
let span = per_block.saturating_pow(level - 1);
let end = first.saturating_add(map.len());
for i in 0..per_block {
let lo = base.saturating_add(i.saturating_mul(span));
if lo >= end {
break;
}
if lo.saturating_add(span) <= first {
continue;
}
let ptr = u64::from(get_u32(&buf, i * 4));
if level == 1 {
if ptr != 0 {
self.check_block(ptr)?;
}
map[lo - first] = ptr;
} else {
self.walk_indirect_window(ptr, level - 1, lo, first, map)?;
}
}
Ok(())
}
/// Validate every pointer in an inode's classic map without materializing it: each
/// direct, indirect, and data-block pointer must name a block the filesystem has.
///
/// This is the classic-map counterpart to
/// [`scan_extent_node`](Self::scan_extent_node): a forensic walk that checks the
/// structure a read would follow but allocates nothing per logical block, so a
/// hostile `i_size` cannot turn a scan into the runaway allocation materializing
/// the full hole-padded map would be. The walk reads each indirect block once — a
/// block reached twice is a cyclic or fan-out map and is not followed again — and
/// is [`INDIRECT_LEVELS`] deep by construction, so it is bounded by the blocks the
/// filesystem actually holds.
fn scan_block_map(
&mut self,
inode: &Inode,
visited: &mut HashSet<u64>,
) -> Result<(), ReadError> {
for i in 0..DIRECT_BLOCKS {
let ptr = u64::from(get_u32(&inode.block, i * 4));
if ptr != 0 {
self.check_block(ptr)?;
}
}
for level in 1..=INDIRECT_LEVELS {
let slot = DIRECT_BLOCKS + level as usize - 1;
let root = u64::from(get_u32(&inode.block, slot * 4));
self.scan_indirect(root, level, visited)?;
}
Ok(())
}
/// Validate the pointers an indirect block maps, `level` levels deep, reading each
/// indirect block once. A zero pointer is a hole and names no block.
fn scan_indirect(
&mut self,
block: u64,
level: u32,
visited: &mut HashSet<u64>,
) -> Result<(), ReadError> {
if block == 0 {
return Ok(());
}
// A repeated indirect block is a cycle or fan-out, not a map: validated once, it
// is not walked again, so a crafted map cannot fan the walk out without bound.
if !visited.insert(block) {
return Ok(());
}
// The pointer at this block is bounded by the read of it; the ones it holds are
// bounded below, where they are validated without being read.
let buf = self.block(block)?;
let per_block = self.block_size / 4;
for i in 0..per_block {
let ptr = u64::from(get_u32(&buf, i * 4));
if level == 1 {
if ptr != 0 {
self.check_block(ptr)?;
}
} else {
self.scan_indirect(ptr, level - 1, visited)?;
}
}
Ok(())
}
/// Read the full contents of a regular file or slow symlink, truncated to the
/// inode's size. Works from whichever mapping the inode carries.
///
/// The returned buffer is the inode's whole logical size, holes materialized as
/// zeros, so this allocates in proportion to `i_size`. On an untrusted image that
/// size is attacker-controlled and a genuine sparse file is indistinguishable in
/// shape from a crafted one, so a large declared size drives a large allocation
/// either way: this method trusts `i_size`. To inspect an image this crate did not
/// write without materializing its files, use [`scan`](Self::scan), which allocates
/// nothing per logical block.
///
/// [`Limits::max_file_bytes`] is what withdraws that trust: a file larger than the cap
/// is [`ReadError::FileTooLarge`] rather than a shortened buffer, so a truncated read
/// can never be mistaken for a whole one. To read part of a file deliberately, use
/// [`read_into`](Self::read_into), which is bounded by the buffer given to it and
/// reports how much of it was filled; to read a large file without holding it, use
/// [`read_data_to`](Self::read_data_to).
///
/// # Errors
///
/// [`ReadError::FileTooLarge`] if the file is larger than [`Limits::max_file_bytes`];
/// other [`ReadError`] variants if the mapping or a data block cannot be read.
pub fn read_data(&mut self, inode: &Inode) -> Result<Vec<u8>, ReadError> {
// There is no structural bound to apply here, and that is a fact about the
// format rather than a gap. A sparse file's holes cost no blocks, so a file whose
// logical size dwarfs the filesystem holding it is well-formed and must read back
// at its full size — which makes a legitimate all-hole file and a crafted `i_size`
// the same shape from the outside. So this trusts `i_size`, as its documentation
// says, and the only cap is the one a caller sets.
let want = self.whole_file_len(inode)?;
let len = usize::try_from(want).map_err(|_| ReadError::FileTooLarge {
size: want,
limit: usize::MAX as u64,
})?;
let mut data = vec![0u8; len];
// The read is bounded by the buffer, which is bounded by the cap checked above, so
// the mapping it builds is bounded with it: eight bytes per logical block of the
// window, never one per block the size claims.
let filled = self.read_into(inode, 0, &mut data)?;
data.truncate(filled);
Ok(data)
}
/// Stream the full contents of a regular file or slow symlink to `out`, returning the
/// bytes written.
///
/// This is [`read_data`](Self::read_data) without holding the file: the contents are
/// written out a window at a time, so memory is a fixed working set rather than the
/// file's size — which is what extracting a multi-gigabyte file out of an image needs.
/// Holes are written as zeros, exactly as they read.
///
/// The same [`Limits::max_file_bytes`] cap applies, since what it expresses is distrust
/// of the declared size rather than a memory bound.
///
/// # Errors
///
/// [`ReadError::FileTooLarge`] if the file is larger than [`Limits::max_file_bytes`];
/// [`ReadError::Io`] if `out` cannot be written; other [`ReadError`] variants if the
/// mapping or a data block cannot be read.
pub fn read_data_to(&mut self, inode: &Inode, mut out: impl Write) -> Result<u64, ReadError> {
let want = self.whole_file_len(inode)?;
let bs = self.block_size as u64;
let zeros = vec![0u8; self.block_size];
let mut written = 0u64;
// One window's map at a time, so the map is a fixed cost however large the file:
// `MAP_WINDOW_BLOCKS` entries of eight bytes each. The windows are walked in order,
// so each costs one pass over the inode's mapping structures — a few block reads
// for an extent tree, the indirect blocks the window covers for a classic map.
while written < want {
let first = usize::try_from(written / bs).unwrap_or(usize::MAX);
let remaining_blocks = (want - written).div_ceil(bs);
let count = usize::try_from(remaining_blocks.min(MAP_WINDOW_BLOCKS as u64))
.unwrap_or(MAP_WINDOW_BLOCKS);
for phys in self.map_window(inode, first, count)? {
let left = want - written;
let take = usize::try_from(left.min(bs)).unwrap_or(self.block_size);
if phys == 0 {
// A hole: it reads as zeros without occupying a block to read.
out.write_all(&zeros[..take])?;
} else {
let block = self.block(phys)?;
out.write_all(&block[..take])?;
}
written += take as u64;
if written >= want {
break;
}
}
}
Ok(written)
}
/// Read a file's bytes at `offset` into `buf`, returning how many were filled.
///
/// This is the ranged read the whole-file forms are built on: it fills at most
/// `buf.len()` bytes and stops at the file's logical size, so a short return means the
/// range reached the end of the file rather than that anything was dropped. An `offset`
/// at or past the size fills nothing. Holes are filled with zeros, exactly as they
/// read.
///
/// Everything it allocates is bounded by `buf`: the logical-to-physical map covers the
/// window the buffer spans and no more, so a hostile `i_size` costs the caller nothing
/// beyond the buffer they chose. [`Limits::max_file_bytes`] therefore does not apply
/// here — the buffer *is* the bound — and the mapping structures are followed only for
/// the window, so blocks outside it are neither read nor validated.
///
/// # Errors
///
/// [`ReadError`] variants if the mapping or a data block cannot be read.
pub fn read_into(
&mut self,
inode: &Inode,
offset: u64,
buf: &mut [u8],
) -> Result<usize, ReadError> {
let size = self.file_len(inode);
if offset >= size || buf.is_empty() {
return Ok(0);
}
let bs = self.block_size as u64;
let want = (size - offset).min(buf.len() as u64);
// The window is the logical blocks the range touches: the block holding `offset`
// through the one holding its last byte.
let first = usize::try_from(offset / bs).unwrap_or(usize::MAX);
let last = usize::try_from((offset + want - 1) / bs).unwrap_or(usize::MAX);
let count = last - first + 1;
// Where the range begins inside its first block, and so where each block's bytes
// land in the buffer.
let mut skip = usize::try_from(offset % bs).unwrap_or(0);
let mut filled = 0usize;
for phys in self.map_window(inode, first, count)? {
let take =
(self.block_size - skip).min(usize::try_from(want).unwrap_or(usize::MAX) - filled);
if phys == 0 {
// A hole reads as zeros; the buffer is filled rather than left as it was,
// so a caller reusing one buffer never sees a previous read's bytes.
buf[filled..filled + take].fill(0);
} else {
let block = self.block(phys)?;
buf[filled..filled + take].copy_from_slice(&block[skip..skip + take]);
}
filled += take;
skip = 0;
if filled as u64 >= want {
break;
}
}
Ok(filled)
}
/// A file's logical size as a whole-file read sees it: the length
/// [`file_len`](Self::file_len) reports, held to [`Limits::max_file_bytes`].
///
/// # Errors
///
/// [`ReadError::FileTooLarge`] if the size exceeds the cap.
fn whole_file_len(&mut self, inode: &Inode) -> Result<u64, ReadError> {
let size = self.file_len(inode);
if size > self.limits.max_file_bytes {
return Err(ReadError::FileTooLarge {
size,
limit: self.limits.max_file_bytes,
});
}
Ok(size)
}
/// The logical size of the data an inode maps.
///
/// Three things narrow the inode's own size field. An inode whose block area holds
/// something else — a fast symlink's target, a device's numbers — maps no data at all,
/// whatever the field claims. A size past the 2^32-block logical ceiling cannot be
/// mapped past it. And a directory holds no holes, so it cannot span more blocks than
/// the image has; both bounds come from [`logical_block_count`](Self::logical_block_count).
///
/// This is the length every read of an inode's data yields, so it is also the length
/// anything declaring that data — a tar member's header — must state, or the
/// declaration and the bytes disagree.
pub(crate) fn file_len(&mut self, inode: &Inode) -> u64 {
if !maps_data(inode, self.block_size) {
return 0;
}
let mapped =
(self.logical_block_count(inode) as u64).saturating_mul(self.block_size as u64);
inode.size.min(mapped)
}
/// The most names the source has room to describe: every name a well-formed
/// filesystem holds occupies a directory record of at least [`MIN_DIRENT_LEN`] bytes
/// in a block of its own filesystem, so the source's length divided by that is a
/// bound no well-formed image reaches and a crafted one cannot exceed.
fn max_names(&mut self) -> usize {
usize::try_from(self.source_len() / MIN_DIRENT_LEN).unwrap_or(usize::MAX)
}
/// Parse the jbd2 journal superblock, or `None` when the image carries no journal.
///
/// The journal lives in inode 8 (`s_journal_inum`); its first data block is the
/// journal superblock. This confirms the log is a well-formed, empty v2 journal.
///
/// # Errors
///
/// [`ReadError`] variants if the journal inode or its first block cannot be read,
/// or the block is not a valid journal superblock.
pub fn journal_superblock(
&mut self,
) -> Result<Option<crate::journal::JournalSuperblock>, ReadError> {
let Some(first) = self.journal_superblock_block()? else {
return Ok(None);
};
let block = self.block(first)?;
Ok(Some(crate::journal::JournalSuperblock::read_from(&block)?))
}
/// The block holding the journal superblock, or `None` when the image carries no
/// journal.
///
/// Separate from [`journal_superblock`](Self::journal_superblock) because a caller
/// writing the log's recorded identity back needs where it is, not only what it says.
pub(crate) fn journal_superblock_block(&mut self) -> Result<Option<u64>, ReadError> {
if !self.feature.has_journal() || self.sb.journal_inum == 0 {
return Ok(None);
}
let inode = self.inode(self.sb.journal_inum)?;
// On ext3 the journal is block-mapped, so this must not assume an extent tree.
// Only the log's first block is wanted, so only that much of the map is built: an
// inode whose size claims terabytes of journal costs one entry to read, not one
// per block it claims.
let want = self.logical_block_count(&inode).min(1);
let first = self
.map_window(&inode, 0, want)?
.first()
.copied()
.filter(|&b| b != 0)
.ok_or(ReadError::BadJournal)?;
Ok(Some(first))
}
/// Read a symlink's target, whether stored inline (fast) or in a data block (slow).
///
/// A target longer than `PATH_MAX` is refused rather than read. A symbolic link's
/// target has a structural ceiling that a regular file's contents do not — no path this
/// or any other kernel resolves is longer — so trusting `i_size` here is not the
/// defensible trade-off it is for a sparse file. Without the bound a slow symlink falls
/// through to a whole-file read and allocates whatever its size field claims, which at
/// the 32 bits a symlink's size is read at is four gibibytes per link.
///
/// # Errors
///
/// [`ReadError::FileTooLarge`] if the target's declared length is past `PATH_MAX`;
/// [`ReadError`] variants if a slow symlink's block cannot be read.
pub fn read_symlink(&mut self, inode: &Inode) -> Result<Vec<u8>, ReadError> {
// Whether the target is stored inline is a matter of the blocks the inode owns,
// not of its size: a symlink holding a data block keeps its target there however
// short it is. An inline target is bounded by the block area whatever the size
// field claims, so a size past the area's end yields the area, not a panic.
if is_fast_symlink(inode, self.block_size) {
let len = usize::try_from(inode.size)
.unwrap_or(usize::MAX)
.min(inode.block.len());
return Ok(inode.block[..len].to_vec());
}
if inode.size > MAX_PATH as u64 {
return Err(ReadError::FileTooLarge {
size: inode.size,
limit: MAX_PATH as u64,
});
}
self.read_data(inode)
}
/// Read an inode's extended attributes, both the inline set stored in the inode
/// and the set in its external attribute block, if any.
///
/// # Errors
///
/// [`ReadError`] variants if the attribute block or an entry cannot be read.
pub fn xattrs(&mut self, inode: &Inode) -> Result<Vec<Xattr>, ReadError> {
let mut out = parse_inline(&inode.inline_xattr)?;
if inode.file_acl != 0 {
// The read bounds the pointer by the filesystem's own block count, so a
// pointer past the filesystem but inside a larger disk image cannot read
// whatever sits beyond the partition back as this inode's attributes.
let block = self.block(inode.file_acl)?;
out.extend(parse_block(&block)?);
}
// An ACL leaves in the form every boundary speaks, which is the same widening the
// inode's own times get: what is stored is this format's packing of a value, and
// what a read reports is the value.
for x in &mut out {
if x.name != Acl::ACCESS_NAME && x.name != Acl::DEFAULT_NAME {
continue;
}
let acl =
crate::ondisk::decode_acl(&x.value).map_err(|source| ReadError::MalformedAcl {
name: x.name.clone(),
source,
})?;
x.value = acl.encode();
}
Ok(out)
}
/// Decode a device node's `(major, minor)` numbers from its block area. Only
/// meaningful for a character- or block-special inode.
#[must_use]
pub fn device(&self, inode: &Inode) -> (u32, u32) {
let b0 = u32::from_le_bytes([
inode.block[0],
inode.block[1],
inode.block[2],
inode.block[3],
]);
let b1 = u32::from_le_bytes([
inode.block[4],
inode.block[5],
inode.block[6],
inode.block[7],
]);
decode_device(b0, b1)
}
/// Read a directory's entries, skipping the unused slots and the checksum tail.
///
/// Every entry handed back names an inode the filesystem has. An entry naming one it
/// does not is refused where the entry is *read* rather than where a later resolution of
/// it fails, so a caller listing a directory and a caller walking the tree get the same
/// account of the same corruption. Whether the inode is *in use* is a separate question,
/// which the bitmaps answer and a [`scan`](Self::scan) asks.
///
/// What "refused" means is the policy's, and it is the same answer a name no directory
/// could hold gets: a strict read stops at [`ReadError::DirEntryNoSuchInode`], and a
/// lenient one leaves the entry out and lets the scan report it. Two structurally
/// equivalent faults in one record are not given opposite treatments, and a lenient read
/// keeps the good entries beside the bad one — which is what it exists for.
///
/// This is one of the reads [`Limits::max_walk_entries`] governs: a directory is a
/// structure read whole, so the cap on how many entries one read gathers applies here
/// and not only across a tree. Past it the answer is
/// [`ReadError::WalkTooLarge`] rather than a short listing.
///
/// A block the mapping names more than once is read once. Nothing in the format stops a
/// mapping pointing every one of its slots at a single block, and a directory of eight
/// gibibytes' apparent length can do it from ten kilobytes of real content — so without
/// this the entries of one block are yielded a million times over. The scan path already
/// judged such a mapping once per block; the read path now reads it once per block.
///
/// # Errors
///
/// [`ReadError::DirEntryNoSuchInode`] under a strict policy if an entry names an inode
/// past the filesystem's inode count; [`ReadError::WalkTooLarge`] if the directory holds
/// more
/// entries than the bounds allow; [`ReadError`] variants if the directory's blocks
/// cannot be read or parsed.
pub fn read_dir(&mut self, inode: &Inode) -> Result<Vec<Entry>, ReadError> {
let cap = self.limits.max_walk_entries.min(self.max_names());
let mut entries = Vec::new();
self.for_each_dir_entry(inode, |entry| {
if entries.len() >= cap {
return Err(ReadError::WalkTooLarge { limit: cap });
}
entries.push(entry);
Ok(std::ops::ControlFlow::Continue(()))
})?;
Ok(entries)
}
/// Walk every live entry of a directory, calling `visit` until it answers `Break`.
///
/// The one place directory bytes become names and references: `read_dir` collects
/// through it, and a path resolution stops through it — so resolving a component reads
/// a large directory only as far as the name it is after, rather than materializing
/// every entry to use one.
fn for_each_dir_entry<F>(&mut self, inode: &Inode, mut visit: F) -> Result<(), ReadError>
where
F: FnMut(Entry) -> Result<std::ops::ControlFlow<()>, ReadError>,
{
let mut seen_blocks = HashSet::new();
for phys in self.data_blocks(inode)? {
// A directory block a mapping does not name holds no entries, and one it names
// twice holds them once.
if phys == 0 || !seen_blocks.insert(phys) {
continue;
}
let block = self.block(phys)?;
let mut off = 0;
// Walk to the end of the block. Under `metadata_csum` the final twelve
// bytes are a checksum tail, which parses as a zero-inode slot and is
// skipped by the `inode != 0` test below; without the feature the kernel
// tiles real entries across the whole block, so a legitimate entry can begin
// in those last twelve bytes. Stopping short of the block's end would drop
// it.
while off < self.block_size {
let (entry, rec_len) = DirEntry::read_from(&block[off..], self.block_size)?;
// [`DirEntry::read_from`] returns no record shorter than the eight-byte
// header it parses, so `off` always advances and this cannot fire. It
// guards that invariant rather than describing a record an image holds: a
// zero-length one would turn the walk below into the one thing a reader of
// hostile bytes must never do.
if rec_len == 0 {
return Err(ReadError::BadDirectory);
}
if entry.inode != 0 {
// An entry naming an inode the filesystem does not have and an entry
// carrying a name no directory could hold are the same fault in two
// fields, and get the same treatment: a policy that refuses deviations
// refuses them, and one that does not leaves the entry out and lets the
// scan report it. Failing the whole listing instead discards the good
// entries beside the bad one — which is precisely what a lenient read
// exists to recover.
//
// Both checks sit here because this is where bytes become a name and a
// reference: one check covers listing, resolution, the walk, and every
// sink a walk feeds, and no later caller has to remember either.
let fault = if is_hostile_entry(&entry.name) {
Some(ReadError::HostileName { inode: entry.inode })
} else {
self.check_dir_entry_inode(entry.inode).err()
};
if let Some(e) = fault {
if self.policy.is_fatal(Severity::Structural) {
return Err(e);
}
off += rec_len;
continue;
}
let keep = visit(Entry {
name: entry.name,
inode: entry.inode,
file_type: entry.file_type,
})?;
if keep.is_break() {
return Ok(());
}
}
off += rec_len;
}
}
Ok(())
}
/// Resolve a path to its inode number and inode, following symbolic links.
///
/// The path is resolved against *this filesystem's* root, never the host's — an
/// absolute link target restarts at inode 2 of this image, and nothing here can name
/// a file outside it. A leading `/` is optional, `.` and empty components are
/// skipped, and `..` ascends to the directory the resolution descended from, staying
/// at the root where there is nothing to ascend to.
///
/// Symbolic links in the path are followed, including in the final component.
/// This is not a nicety on a Linux root filesystem: a merged-`/usr` distribution
/// makes `/lib`, `/bin`, and `/sbin` links into `/usr`, so `/lib/modules` — the
/// first place to look when a kernel boots without drivers — is unreachable without
/// following one.
///
/// Resolution follows at most [`MAX_SYMLINK_HOPS`](crate::MAX_SYMLINK_HOPS) links, so a cycle terminates.
///
/// # Errors
///
/// [`ReadError::NotFound`] if a component names no entry;
/// [`ReadError::NotADirectory`] if a non-final component is not a directory;
/// [`ReadError::SymlinkLoop`] if the path follows too many links;
/// [`ReadError`] variants if a directory or link along the way cannot be read.
pub fn lookup(&mut self, path: &[u8]) -> Result<(u32, Inode), ReadError> {
self.resolve(path, true)
}
/// Resolve a path to its inode number and inode, following symbolic links in every
/// component *except* the last — so a path naming a symlink yields the link itself,
/// not its target. Otherwise as [`lookup`](Self::lookup).
///
/// # Errors
///
/// As [`lookup`](Self::lookup).
pub fn lookup_no_follow(&mut self, path: &[u8]) -> Result<(u32, Inode), ReadError> {
self.resolve(path, false)
}
/// Walk `path` component by component from the root, expanding symbolic links.
///
/// `follow_final` decides whether a link in the last component is expanded; the
/// links before it always are, because a path cannot continue through a link
/// without going where it points.
fn resolve(&mut self, path: &[u8], follow_final: bool) -> Result<(u32, Inode), ReadError> {
crate::resolve::drive(self, path, follow_final)
}
/// Walk the whole tree, yielding one [`WalkEntry`] per name below the root.
///
/// Entries come out in depth-first order, each directory's names sorted. The walk
/// starts *below* the root: the root directory itself has no name and so no entry,
/// and every path begins with `/`. `.` and `..` are not yielded.
///
/// A directory inode is descended into only the first time it is reached, so a
/// directory cycle or a hard-linked directory bounds the walk rather than fanning
/// it out; the repeat still appears as an entry, it is simply not descended.
///
/// # Bounds
///
/// The walk is bounded by [`Limits::max_walk_entries`] and by the number of names the
/// source has room to hold, whichever is smaller. Reaching either is an error rather
/// than a short list: a walk that returned what it managed to gather would drop names
/// with nothing to say it had, and a caller extracting a tree would write an
/// incomplete one and see success.
///
/// The bound counts every name the walk has *found*, not only the ones it has yielded.
/// Each name reached may reveal a whole directory's worth more, so a bound on what is
/// yielded alone would leave what is held bounded by the product of the two.
///
/// The structural bound is what makes this safe to point at an image built to be
/// hostile: distinct directory inodes may map the *same* data blocks, so a small image
/// can describe an unbounded number of names, while a well-formed one spends at least
/// [`MIN_DIRENT_LEN`] bytes of its own blocks per name and so can never reach the
/// bound.
///
/// # Errors
///
/// [`ReadError::NotADirectory`] if the root inode is not a directory, [`ReadError`]
/// variants if any directory along the walk cannot be read, or
/// [`ReadError::WalkTooLarge`] if the tree holds more names than the bounds allow.
pub fn walk(&mut self) -> Result<Vec<WalkEntry>, ReadError> {
let mut out = Vec::new();
self.walk_with::<ReadError>(|_, entry| {
out.push(entry);
Ok(())
})?;
Ok(out)
}
/// Walk the whole tree, handing each [`WalkEntry`] to `visit` as it is reached rather
/// than gathering them all first.
///
/// The order, the bounds, and the cycle handling are [`walk`](Self::walk)'s exactly;
/// what differs is what the walk holds. `walk` accumulates every name, so it costs one
/// whole [`Inode`] and path per name in the tree, all of them live at once. This holds
/// only the *frontier* — the names reached and not yet visited — and holds each of those
/// as a path and an inode number rather than as an inode, reading the inode back when
/// the name comes up. That is what makes a whole tree processable without its size
/// setting the working set: writing an archive, counting a tree, building an index.
///
/// It is a frontier and not a fixed set. Depth-first over an explicit stack, the
/// frontier is every unvisited sibling along the path currently being walked, so a
/// directory holding a million names does put a million paths on it — bounded, but not
/// small. What it is *not* is the whole tree, and it costs no inode per pending name.
///
/// The names on the frontier count against the walk's bounds exactly as the names
/// already visited do, which is what keeps the bound a bound on an image built to be
/// hostile: without it, one bound's worth of visits could each discover a whole
/// directory's worth of names, and the frontier would be the product rather than the
/// sum.
///
/// `visit` receives the reader itself, so it may read each entry's contents,
/// attributes, or link target as it goes. It is called once per name, in walk order,
/// and an error it returns stops the walk and is returned unchanged — so a consumer's
/// own failure is not reported as a fault in the image.
///
/// The error type is the consumer's, and the walk's own [`ReadError`]s convert into it,
/// so a caller whose work has failures of its own — writing an archive, say — reports
/// them in its own vocabulary rather than forcing them through this one.
///
/// # Errors
///
/// Whatever `visit` returns, or the [`walk`](Self::walk) errors converted into it:
/// [`ReadError::NotADirectory`] if the root inode is not a directory, [`ReadError`]
/// variants if a directory cannot be read, or [`ReadError::WalkTooLarge`] if the tree
/// holds more names than the bounds allow.
pub fn walk_with<E: From<ReadError>>(
&mut self,
mut visit: impl FnMut(&mut Self, WalkEntry) -> Result<(), E>,
) -> Result<(), E> {
crate::walk::drive(self, |reader, entry| visit(reader, entry))
}
}
/// The ext family's half of the shared resolution: a directory listing to find a name in, and
/// an inode number to come back to a directory by.
impl<R: Read + Seek> crate::resolve::Resolve for Reader<R> {
/// An inode number, not an inode: the descent holds one per component, and an [`Inode`]
/// carries its inline attribute region — the same reason the walk's frontier holds a
/// number. The inode is read again on the way back up, which happens once per `..`.
type Ancestor = u32;
/// The number and the inode both, which is what a lookup hands back: a caller that has
/// resolved a path has the inode it named without reading it a second time.
type Node = (u32, Inode);
type Error = ReadError;
fn root_node(&mut self) -> Result<Self::Node, ReadError> {
Ok((ROOT_INO, self.inode(ROOT_INO)?))
}
fn ancestor_of(&self, node: &Self::Node) -> u32 {
node.0
}
fn node_at(&mut self, ancestor: u32) -> Result<Self::Node, ReadError> {
Ok((ancestor, self.inode(ancestor)?))
}
fn is_directory(&self, node: &Self::Node) -> bool {
is_dir(&node.1)
}
fn find_name(
&mut self,
dir: &Self::Node,
name: &[u8],
) -> Result<Option<Self::Node>, ReadError> {
// Streamed with an early stop rather than listed and searched: a resolution asks
// for one name, and a large directory read whole per component would cost every
// entry's allocation to use one of them.
let mut found = None;
self.for_each_dir_entry(&dir.1, |e| {
Ok(if e.name == name {
found = Some(e.inode);
std::ops::ControlFlow::Break(())
} else {
std::ops::ControlFlow::Continue(())
})
})?;
match found {
Some(ino) => Ok(Some((ino, self.inode(ino)?))),
None => Ok(None),
}
}
fn read_link(&mut self, node: &Self::Node, path: &[u8]) -> Result<Option<Vec<u8>>, ReadError> {
if !is_symlink(&node.1) {
return Ok(None);
}
// A target longer than a path can be is refused before it is read, so a crafted
// `i_size` cannot make one link allocate the whole image.
if node.1.size > MAX_PATH as u64 {
return Err(self.not_found(path));
}
Ok(Some(self.read_symlink(&node.1)?))
}
fn not_found(&self, path: &[u8]) -> ReadError {
ReadError::NotFound {
path: path.to_vec(),
}
}
fn not_a_directory(&self, _node: &Self::Node, path: &[u8]) -> ReadError {
ReadError::NotADirectory {
path: path.to_vec(),
}
}
fn too_many_links(&self, path: &[u8]) -> ReadError {
ReadError::SymlinkLoop {
path: path.to_vec(),
}
}
}
/// The ext family's half of the shared walk: an inode number on the frontier, an inode
/// number as the cycle key, and a directory listing as the children.
impl<R: Read + Seek> crate::walk::Walk for Reader<R> {
/// A path and an inode number, not an inode: an [`Inode`] carries its inline attribute
/// region, which is the inode size less 128 bytes, and holding one per pending name
/// would make the frontier cost what the whole tree does. The inode is read at the pop
/// instead, which is the same one read per name the listing would have done — moved,
/// not added.
type Pending = (Vec<u8>, u32);
type Entry = WalkEntry;
type Key = u32;
type Error = ReadError;
fn cap(&mut self) -> usize {
self.limits.max_walk_entries.min(self.max_names())
}
fn seed(&mut self) -> Result<crate::walk::Seed<Self>, ReadError> {
let root = self.inode(ROOT_INO)?;
// The root is descended into like any other directory, so it is checked to be one
// like any other — as every child below it is, and as each component `resolve`
// walks through is.
//
// What an unguarded root costs is not a wrong listing. A directory's logical length
// is bounded by the blocks it could materially hold; a regular file's is not, because
// it may legitimately be sparse. So inode 2 with a regular-file mode and a large
// `i_size` sends `read_dir` to build a block map at the full logical size before a
// single entry exists to read — tens of gigabytes from an image of a few kilobytes,
// reached by a plain `walk()` with no sink involved.
if !is_dir(&root) {
return Err(ReadError::NotADirectory {
path: b"/".to_vec(),
});
}
let children = self.walk_children(&root, &[])?;
Ok((children, vec![ROOT_INO]))
}
fn resolve(&mut self, (path, number): Self::Pending) -> Result<WalkEntry, ReadError> {
let inode = self.inode(number)?;
Ok(WalkEntry {
path,
number,
inode,
})
}
fn descend_key(&self, entry: &WalkEntry) -> Option<u32> {
is_dir(&entry.inode).then_some(entry.number)
}
fn children(&mut self, entry: &WalkEntry) -> Result<Vec<Self::Pending>, ReadError> {
self.walk_children(&entry.inode, &entry.path)
}
fn too_large(limit: usize) -> ReadError {
ReadError::WalkTooLarge { limit }
}
}
impl<R: Read + Seek> Reader<R> {
/// The names below directory `inode`, each as a path and an inode number, in reverse
/// name order so a stack pops them in name order. `.` and `..` are skipped.
///
/// The inode itself is deliberately not read here: the walk reads it when the name comes
/// off the stack, so a pending name costs its path and four bytes rather than a whole
/// [`Inode`].
fn walk_children(
&mut self,
inode: &Inode,
prefix: &[u8],
) -> Result<Vec<(Vec<u8>, u32)>, ReadError> {
let mut entries = self.read_dir(inode)?;
entries.sort_by(|a, b| a.name.cmp(&b.name));
let mut children = Vec::new();
for e in entries.iter().rev() {
// The two entries a directory holds for itself and its parent are not names
// below it, so no path is built from either. A name no directory could hold
// needs no test here: `read_dir` above either refused it or left it out, so
// one never reaches this loop.
if crate::path::is_dot_entry(&e.name) {
continue;
}
let path = crate::walk::child_path(prefix, &e.name)
.ok_or(ReadError::PathTooLong { limit: MAX_PATH })?;
children.push((path, e.inode));
}
Ok(children)
}
}
/// How a read failure classifies in the crate's family-agnostic frame.
///
/// The mapping is what the crate's shared surface can say about an ext failure: a source
/// that could not be read stays exactly what it was, a feature this reader does not follow
/// is unsupported, a caller's bound being reached is a limit, and everything else is the
/// image's own bytes being wrong.
impl From<ReadError> for TreeError {
fn from(err: ReadError) -> Self {
match err {
ReadError::Io { kind, message } => TreeError::Io { kind, message },
ReadError::UnsupportedIncompat { .. } => TreeError::Unsupported {
family: Family::Ext,
detail: err.to_string(),
},
ReadError::FileTooLarge { .. }
| ReadError::PathTooLong { .. }
| ReadError::WalkTooLarge { .. } => TreeError::LimitExceeded {
family: Family::Ext,
detail: err.to_string(),
},
other => TreeError::Malformed {
family: Family::Ext,
detail: other.to_string(),
},
}
}
}
/// The ext family's implementation of the crate's extraction surface.
///
/// The node handle is the [`Inode`] itself, which the walk already read, so a sink that
/// stats and then reads a file costs no second lookup of it.
impl<R: Read + Seek> FsTree for Reader<R> {
type Node = Inode;
fn family(&self) -> Family {
Family::Ext
}
fn max_file_bytes(&self) -> u64 {
self.limits.max_file_bytes
}
fn walk_tree<E, F>(&mut self, mut visit: F) -> Result<(), E>
where
E: From<TreeError>,
F: FnMut(&mut Self, TreeEntry<Inode>) -> Result<(), E>,
{
// The root has no name, so `walk_with` does not reach it. Yielding it first under
// the empty path is what lets a sink apply the root's own metadata without a second
// way of asking for it.
//
// It is classified the same way every other node is rather than assumed to be a
// directory: the root of a sound filesystem is one, and an image that says
// otherwise is what a sink has to be told about rather than shown a kind the bytes
// do not carry.
let root = self
.inode(ROOT_INO)
.map_err(|e| E::from(TreeError::from(e)))?;
let root_entry = WalkEntry {
path: Vec::new(),
number: ROOT_INO,
inode: root,
};
let root_entry = tree_entry(self, root_entry).map_err(E::from)?;
visit(self, root_entry)?;
// `walk_with` reports in the caller's error type and needs it to carry a
// `ReadError`; the visitor here carries a `TreeError` instead. This holds both for
// the length of the walk and unwraps them at the end, so a failure of the
// filesystem and a failure of the sink each reach the caller as itself.
let outcome = self.walk_with::<WalkFail<E>>(|reader, entry| {
let node = tree_entry(reader, entry).map_err(WalkFail::Tree)?;
visit(reader, node).map_err(WalkFail::Visitor)
});
match outcome {
Ok(()) => Ok(()),
Err(WalkFail::Read(e)) => Err(E::from(TreeError::from(e))),
Err(WalkFail::Tree(e)) => Err(E::from(e)),
Err(WalkFail::Visitor(e)) => Err(e),
}
}
fn stat(&mut self, node: &Inode, _synthesis: &Synthesis) -> Result<Attributes, TreeError> {
// ext4 records every property the shared frame names, so nothing is invented and
// the synthesis inputs go unused. A caller's values are not applied over what the
// image holds: what is stored is what is reported.
let xattrs = self.xattrs(node)?;
Ok(Attributes::read(
Metadata {
mode: node.mode & 0o7777,
uid: node.uid,
gid: node.gid,
atime: node.atime,
ctime: node.ctime,
mtime: node.mtime,
},
xattrs,
))
}
fn read_bytes(
&mut self,
node: &Inode,
offset: u64,
buf: &mut [u8],
) -> Result<usize, TreeError> {
Ok(Reader::read_into(self, node, offset, buf)?)
}
fn link_target(&mut self, node: &Inode) -> Result<Vec<u8>, TreeError> {
if node.mode & 0o170000 != 0o120000 {
return Err(TreeError::Malformed {
family: Family::Ext,
detail: "the node is not a symbolic link".to_string(),
});
}
Ok(self.read_symlink(node)?)
}
}
/// The shared walk failure over this family's read error.
type WalkFail<E> = crate::tree::WalkFail<ReadError, E>;
/// What makes `?` on a [`ReadError`] work inside a walk through the shared surface. Written
/// per family because a blanket implementation would collide with the reflexive one.
impl<E> From<ReadError> for WalkFail<E> {
fn from(err: ReadError) -> Self {
WalkFail::Read(err)
}
}
/// Project one walk entry into the shared frame: what the node is, and whether the image
/// says more than one name reaches it.
///
/// The type nibble of a mode names one of seven things, and an inode carrying any other
/// value is malformed — so it is refused here rather than resolved to whichever kind is
/// nearest. Guessing would be the shape of bug that turns an unreadable inode into a socket
/// a sink then goes and creates: a lenient open reaches this with whatever the image said,
/// so the nibble is the image's claim rather than a fact.
fn tree_entry<R: Read + Seek>(
reader: &mut Reader<R>,
entry: WalkEntry,
) -> Result<TreeEntry<Inode>, TreeError> {
let WalkEntry {
path,
number,
inode,
..
} = entry;
let kind = match inode.mode & 0o170000 {
0o040000 => NodeKind::Directory,
0o100000 => NodeKind::File {
size: reader.file_len(&inode),
},
0o120000 => NodeKind::Symlink,
0o020000 => {
let (major, minor) = reader.device(&inode);
NodeKind::CharDevice { major, minor }
}
0o060000 => {
let (major, minor) = reader.device(&inode);
NodeKind::BlockDevice { major, minor }
}
0o010000 => NodeKind::Fifo,
0o140000 => NodeKind::Socket,
other => {
return Err(TreeError::Malformed {
family: Family::Ext,
detail: format!(
"inode {number} records file type {other:#o}, which names no file type"
),
});
}
};
// A directory has more than one link by construction — its own name and its `.` — so
// its link count says nothing, and it is never a second name for anything. Only an
// inode the image says has more than one name carries an identity, which keeps a sink's
// table down to the tree's hard links rather than a path per file in it.
let shared = (!matches!(kind, NodeKind::Directory) && inode.links_count > 1)
.then_some(u64::from(number));
let mut entry = TreeEntry::new(path, kind, inode);
entry.shared = shared;
Ok(entry)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::geometry::GrowReservation;
use crate::materialize::{FormatOptions, format};
use crate::source::{Metadata, TreeBuilder};
use crate::time::Timestamp;
const MIB: u64 = 1024 * 1024;
fn opts() -> FormatOptions {
let mut o = FormatOptions::new([1u8; 16], Timestamp::from_secs(1_700_000_000), [0u8; 16]);
o.grow = GrowReservation::UpTo(32 * 1024 * MIB);
o
}
#[test]
fn an_io_failure_keeps_the_kind_it_was_classified_as() {
// A truncated image and an unreadable one are different outcomes, and a caller
// that only sees the message has to match on text to tell them apart. The kind
// travels alongside so it does not have to.
let truncated = ReadError::from(std::io::Error::new(
std::io::ErrorKind::UnexpectedEof,
"image ends mid-superblock",
));
let ReadError::Io { kind, message } = &truncated else {
panic!("expected an i/o error, got {truncated:?}");
};
assert_eq!(*kind, std::io::ErrorKind::UnexpectedEof);
assert!(message.contains("image ends mid-superblock"), "{message}");
// And the text a caller logs still carries the message, so the richer payload did
// not come at the cost of the rendering.
assert!(
truncated.to_string().contains("image ends mid-superblock"),
"{truncated}"
);
let denied = ReadError::from(std::io::Error::from(std::io::ErrorKind::PermissionDenied));
assert!(matches!(
denied,
ReadError::Io {
kind: std::io::ErrorKind::PermissionDenied,
..
}
));
}
#[test]
fn a_size_past_the_logical_ceiling_is_not_a_length_a_read_yields() {
// A file's size field is the image's own claim; the length a read yields is what
// the map reaches, and no map reaches past 2^32 logical blocks. Anything that
// declares an inode's data ahead of streaming it — a tar member's header — states
// the second, or it promises bytes the body will never carry.
let time = Timestamp::from_secs(1_700_000_000);
let source = TreeBuilder::new().file(
b"/hello".to_vec(),
b"hi\n".to_vec(),
Metadata::new(0o644, time),
);
let image = format(source, 64 * MIB, opts()).unwrap();
let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
let (_, mut inode) = r.lookup(b"/hello").unwrap();
assert_eq!(r.file_len(&inode), 3);
// 16 TiB at a 4 KiB block is the whole logical reach of a map; past it the size
// field is the only thing saying the bytes are there.
let ceiling = MAX_LOGICAL_BLOCKS * r.block_size as u64;
for claimed in [ceiling + 1, ceiling * 2, u64::MAX] {
inode.size = claimed;
assert_eq!(r.file_len(&inode), ceiling, "claimed {claimed}");
}
// Below the ceiling the two agree, so nothing an ordinary image holds is narrowed.
inode.size = ceiling;
assert_eq!(r.file_len(&inode), ceiling);
}
/// The walk as a path-to-inode map, for the tests that ask what is at a path rather
/// than which paths share an inode.
fn walk_tree<R: Read + Seek>(r: &mut Reader<R>) -> std::collections::BTreeMap<Vec<u8>, Inode> {
r.walk()
.unwrap()
.into_iter()
.map(|e| (e.path, e.inode))
.collect()
}
#[test]
fn reads_back_an_empty_filesystem() {
let image = format(TreeBuilder::new(), 64 * MIB, opts()).unwrap();
let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
assert_eq!(r.superblock().inodes_count, 16384);
// Root has only lost+found.
let root = r.inode(2).unwrap();
let entries = r.read_dir(&root).unwrap();
let names: Vec<_> = entries.iter().map(|e| e.name.clone()).collect();
assert!(names.contains(&b".".to_vec()));
assert!(names.contains(&b"..".to_vec()));
assert!(names.contains(&b"lost+found".to_vec()));
}
#[test]
fn default_image_carries_a_readable_empty_journal() {
// The default profile has a journal: inode 8 is a regular extent file, the
// superblock points at it and backs up its block map, and its first block is a
// well-formed, empty v2 jbd2 superblock. A 512 MiB image sizes to 4096 blocks.
let image = format(TreeBuilder::new(), 512 * MIB, opts()).unwrap();
let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
assert!(r.feature().has_journal());
assert_eq!(r.superblock().journal_inum, 8);
assert_eq!(r.superblock().jnl_backup_type, 1);
let jsb = r.journal_superblock().unwrap().expect("has a journal");
assert_eq!(jsb.block_size, 4096);
assert_eq!(jsb.max_len, 4096);
assert_eq!(jsb.first, 1);
assert_eq!(jsb.sequence, 1);
assert_eq!(jsb.start, 0);
assert_eq!(jsb.nr_users, 1);
assert_eq!(jsb.uuid, [1u8; 16]);
// The journal inode is a regular extent file of the journal's size, and the
// superblock's s_jnl_blocks backup mirrors its i_block map.
let inode = r.inode(8).unwrap();
assert_eq!(inode.mode, 0o100600);
assert_eq!(inode.links_count, 1);
assert!(inode.flags.contains(InodeFlags::EXTENTS));
assert_eq!(inode.size, 4096 * 4096);
for (i, word) in inode.block.as_chunks::<4>().0.iter().enumerate() {
let w = u32::from_le_bytes(*word);
assert_eq!(r.superblock().jnl_blocks[i], w, "jnl_blocks[{i}]");
}
assert_eq!(r.superblock().jnl_blocks[16], inode.size as u32);
}
#[test]
fn no_journal_feature_leaves_inode_eight_empty() {
// Clearing has_journal reverts inode 8 to an empty reserved inode with no
// superblock journal pointer — the reader reports no journal. The orphan file's
// entries are journalled, so it goes with the journal.
let mut o = opts();
o.feature.compat = crate::feature::Compat::from_bits(
o.feature.compat.bits()
& !(crate::feature::Compat::HAS_JOURNAL.bits()
| crate::feature::Compat::ORPHAN_FILE.bits()),
);
let image = format(TreeBuilder::new(), 64 * MIB, o).unwrap();
let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
assert!(!r.feature().has_journal());
assert_eq!(r.superblock().journal_inum, 0);
assert_eq!(r.superblock().jnl_backup_type, 0);
assert!(r.journal_superblock().unwrap().is_none());
// Inode 8 is an empty reserved inode (it still carries a metadata checksum, so
// compare the content fields rather than the whole struct).
let inode = r.inode(8).unwrap();
assert_eq!(inode.mode, 0);
assert_eq!(inode.links_count, 0);
assert_eq!(inode.size, 0);
assert_eq!(inode.blocks, 0);
}
#[test]
fn round_trips_a_populated_tree() {
let time = Timestamp::from_secs(1_700_000_000);
let m = |mode| Metadata::new(mode, time);
let src = TreeBuilder::new()
.directory(b"/etc".to_vec(), m(0o755))
.file(b"/etc/hostname".to_vec(), b"ferrosys\n".to_vec(), m(0o644))
.symlink(
b"/etc/mtab".to_vec(),
b"/proc/self/mounts".to_vec(),
m(0o777),
)
.file(
b"/big".to_vec(),
vec![0x42; 20_000],
m(0o600).owned_by(7, 9),
);
let image = format(src, 64 * MIB, opts()).unwrap();
let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
let tree = walk_tree(&mut r);
// File contents survive.
let hostname = &tree[&b"/etc/hostname".to_vec()];
assert_eq!(r.read_data(hostname).unwrap(), b"ferrosys\n");
assert_eq!(hostname.mode, 0o100644);
// A large file spanning several blocks reads back byte-for-byte.
let big = &tree[&b"/big".to_vec()];
assert_eq!(r.read_data(big).unwrap(), vec![0x42; 20_000]);
assert_eq!(big.uid, 7);
assert_eq!(big.gid, 9);
// A fast symlink's target reads back.
let mtab = &tree[&b"/etc/mtab".to_vec()];
assert_eq!(r.read_symlink(mtab).unwrap(), b"/proc/self/mounts");
assert_eq!(mtab.mode & 0o170000, 0o120000);
}
#[test]
fn round_trips_hardlinks_as_a_shared_inode() {
let time = Timestamp::from_secs(1_700_000_000);
let m = |mode| Metadata::new(mode, time);
let src = TreeBuilder::new()
.file(b"/a".to_vec(), b"shared".to_vec(), m(0o644))
.hardlink(b"/b".to_vec(), b"/a".to_vec(), m(0o644));
let image = format(src, 64 * MIB, opts()).unwrap();
let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
let root = r.inode(2).unwrap();
let entries = r.read_dir(&root).unwrap();
let a = entries.iter().find(|e| e.name == b"a").unwrap().inode;
let b = entries.iter().find(|e| e.name == b"b").unwrap().inode;
assert_eq!(a, b, "hard links share one inode");
assert_eq!(r.inode(a).unwrap().links_count, 2);
}
#[test]
fn round_trips_a_slow_symlink() {
let time = Timestamp::from_secs(1_700_000_000);
let target = vec![b'p'; 200];
let src = TreeBuilder::new().symlink(
b"/link".to_vec(),
target.clone(),
Metadata::new(0o777, time),
);
let image = format(src, 64 * MIB, opts()).unwrap();
let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
let root = r.inode(2).unwrap();
let link_ino = r
.read_dir(&root)
.unwrap()
.iter()
.find(|e| e.name == b"link")
.unwrap()
.inode;
let link = r.inode(link_ino).unwrap();
assert_eq!(r.read_symlink(&link).unwrap(), target);
}
/// The byte offset of inode `number` within `bytes`, for the tests that reach past
/// the accessors to corrupt one field. Group 0's table holds the low inode numbers
/// every tree here uses.
fn inode_offset(bytes: &[u8], number: u32) -> usize {
let mut r = Reader::open(std::io::Cursor::new(bytes)).unwrap();
let inode_size = r.superblock().inode_size as usize;
let table = r.group_descriptor(0).unwrap().inode_table as usize;
table * 4096 + (number as usize - 1) * inode_size
}
#[test]
fn the_bytes_above_i_blocks_are_read_as_a_high_half_only_where_the_feature_says_so() {
// Without `huge_file` the two bytes at 0x74 are ext2's `l_i_frag` and `l_i_fsize`,
// and without `64bit` the two at 0x76 are `i_pad1`. Read as high halves they change
// what an inode *is*: `is_fast_symlink` keys on the block count matching the
// attribute blocks, so a fast symlink whose fragment bytes are non-zero classifies
// as slow and its inline target is walked as a block map.
//
// No Linux-written image leaves those bytes set — which is why the differential
// gate, built from `mke2fs` output, cannot see this, and why `e2fsck` carries a
// problem code for each of them: images that do occur.
let time = Timestamp::from_secs(1_700_000_000);
let src = TreeBuilder::new().symlink(
b"/short".to_vec(),
b"/etc/passwd".to_vec(),
Metadata::new(0o777, time),
);
let mut options = opts_no_csum();
options.feature = FeatureSet::EXT2;
let mut bytes = format(src, 16 * MIB, options).unwrap().into_bytes();
let (number, target) = {
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let (number, inode) = r.lookup_no_follow(b"/short").expect("the link is there");
(number, r.read_symlink(&inode).expect("read the target"))
};
assert_eq!(target, b"/etc/passwd", "the fixture is a fast symlink");
// The ext2 fragment fields, and the padding word beside them, set to what a
// filesystem another system wrote could leave there.
let at = inode_offset(&bytes, number);
crate::ondisk::put_u16(&mut bytes[at..], 0x74, 0xBEEF);
crate::ondisk::put_u16(&mut bytes[at..], 0x76, 0xFACE);
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let (_, inode) = r
.lookup_no_follow(b"/short")
.expect("the link is still there");
assert_eq!(
inode.blocks, 0,
"without huge_file the block count is i_blocks_lo alone"
);
assert_eq!(
inode.file_acl, 0,
"without 64bit the attribute block is i_file_acl alone"
);
assert_eq!(
r.read_symlink(&inode).expect("read the target"),
b"/etc/passwd",
"the link still reads as the fast symlink it is"
);
}
#[test]
fn a_directory_entry_with_no_name_is_flagged_and_never_walked() {
// `DirEntry::read_from` accepts a zero `name_len` beside a non-zero inode, so a
// crafted directory holds an entry with no name at all. The path a walk would build
// from it is the *directory's own* path with a trailing separator — so two such
// siblings produce two entries at identical paths, contradicting what a walk
// promises about them, and an archive writer renders one as a member ending in `/`,
// which every tar reader takes for a directory and merges with the real entry of
// that name.
//
// An empty name is not a name. The sink one module away already said so; the reader
// now agrees, in both families.
let time = Timestamp::from_secs(1_700_000_000);
let src = TreeBuilder::new()
.file(b"/f".to_vec(), b"x".to_vec(), Metadata::new(0o644, time))
.file(b"/g".to_vec(), b"y".to_vec(), Metadata::new(0o644, time));
let mut bytes = format(src, 16 * MIB, opts_no_csum()).unwrap().into_bytes();
let block = {
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let root = r.inode(ROOT_INO).unwrap();
usize::try_from(r.extent_leaves(&root).unwrap()[0].start).unwrap() * 4096
};
// The record for `f`, with its name length struck out. The inode stays, so the
// record is live and the name is nothing.
let mut off = block;
loop {
let (entry, rec_len) = DirEntry::read_from(&bytes[off..block + 4096], 4096).unwrap();
if entry.name == b"f" {
break;
}
off += rec_len;
}
bytes[off + 6] = 0;
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
// A strict read refuses rather than walking past it: an omission a caller is not
// told about is a tree that looks complete and is not.
assert!(matches!(r.walk(), Err(ReadError::HostileName { .. })));
// A lenient read is the one that recovers, and no path it builds is the directory's
// own with a trailing separator.
let mut lenient = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.unwrap();
let walked = lenient
.walk()
.expect("a lenient walk hands back what it can");
for entry in &walked {
assert!(
!entry.path.is_empty() && !entry.path.ends_with(b"/"),
"the walk built {:?} from a nameless entry",
String::from_utf8_lossy(&entry.path)
);
}
assert!(
walked.iter().any(|e| e.path == b"/g"),
"the entry beside it survives: {:?}",
walked.iter().map(|e| &e.path).collect::<Vec<_>>()
);
// And the scan says the directory holds a name no directory could.
let report = r.scan();
assert!(
report.anomalies().iter().any(|a| {
a.location.inode == Some(ROOT_INO)
&& a.category == Category::Directory
&& a.severity == Severity::Structural
}),
"the scan reports the nameless entry: {:#?}",
report.anomalies()
);
assert!(!report.is_clean());
}
#[test]
fn a_root_that_is_not_a_directory_is_refused_before_it_is_descended_into() {
// Every descent below the root is guarded by `is_dir`, and so is each component
// `resolve` walks through. The root was the one that was not, and it is the one an
// image controls with a single mode word.
//
// The cost is not a wrong listing. A directory's logical length is bounded by what it
// could materially hold; a regular file's is not, because a sparse file legitimately
// claims far more than it stores. So a root inode with a regular-file mode and a
// terabyte of claimed size sends the walk to build a block map at the full logical
// size — tens of gigabytes of allocation from an image of a few kilobytes — before
// there is a single entry to read. A test that reaches the old code path does not
// fail; it takes the machine down, which is the point.
let time = Timestamp::from_secs(1_700_000_000);
let src =
TreeBuilder::new().file(b"/f".to_vec(), b"x".to_vec(), Metadata::new(0o644, time));
let mut bytes = format(src, 16 * MIB, opts_no_csum()).unwrap().into_bytes();
let at = inode_offset(&bytes, ROOT_INO);
// `i_mode` opens the inode and `i_size_lo` is four bytes in. Regular file, 1 TiB.
crate::ondisk::put_u16(&mut bytes[at..], 0, 0o100_755);
crate::ondisk::put_u32(&mut bytes[at..], 4, 0xFFFF_FFFF);
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
assert!(
matches!(r.walk(), Err(ReadError::NotADirectory { ref path }) if path == b"/"),
"a root that is not a directory is refused by name"
);
// And through the caller that takes a closure, which is the path every sink uses.
let walked: Result<(), ReadError> = r.walk_with(|_, _| Ok(()));
assert!(matches!(walked, Err(ReadError::NotADirectory { .. })));
}
#[test]
fn a_symlink_owning_a_data_block_reads_its_target_from_the_block() {
// Whether a target is stored inline is a matter of the blocks the inode owns,
// not of its size: a symlink holding a data block keeps its target there however
// short its size field says it is. Splitting on the length instead returns the
// extent header that sits in the block area where the target is not — a silent
// wrong read of a filesystem an older kernel wrote.
let time = Timestamp::from_secs(1_700_000_000);
let target = vec![b'p'; 200];
let src = TreeBuilder::new().symlink(
b"/link".to_vec(),
target.clone(),
Metadata::new(0o777, time),
);
let mut bytes = format(src, 64 * MIB, opts()).unwrap().into_bytes();
let ino = {
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
r.lookup_no_follow(b"/link").expect("lookup /link").0
};
let off = inode_offset(&bytes, ino);
// i_size_lo sits at inode offset 0x04. Shortened to a length the inline area
// could have held, while the target stays in the data block the inode owns.
crate::ondisk::put_u32(&mut bytes[off..], 0x04, 40);
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let link = r.inode(ino).unwrap();
assert!(link.blocks > 0, "the symlink owns a data block");
assert_eq!(
r.read_symlink(&link).unwrap(),
target[..40].to_vec(),
"the target came from the block the inode owns, not from its block area"
);
}
#[test]
fn a_fast_symlink_carrying_an_attribute_block_still_reads_inline() {
// An external attribute block is charged to `i_blocks`, so a fast symlink that
// carries one owns a block without keeping its target in one. Discounting that
// block before asking whether any storage remains is what keeps the target
// readable; counting it reads the inline target as a block map instead.
let time = Timestamp::from_secs(1_700_000_000);
let target = b"/proc/self/mounts".to_vec();
let src = TreeBuilder::new()
.symlink(
b"/mtab".to_vec(),
target.clone(),
Metadata::new(0o777, time),
)
.xattr(b"user.blob".to_vec(), vec![0xAB; 300]);
let image = format(src, 64 * MIB, opts()).unwrap();
let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
let (_, link) = r.lookup_no_follow(b"/mtab").expect("lookup /mtab");
assert_ne!(link.file_acl, 0, "the attribute set spilled to a block");
assert_ne!(link.blocks, 0, "which is charged to i_blocks");
assert_eq!(r.read_symlink(&link).unwrap(), target);
}
#[test]
fn a_fast_symlink_claiming_more_than_its_block_area_yields_the_area() {
// A size field larger than the area the target lives in bounds the read to the
// area rather than indexing past it or walking a block map the inode has no
// blocks for.
let time = Timestamp::from_secs(1_700_000_000);
let src = TreeBuilder::new().symlink(
b"/mtab".to_vec(),
b"/proc/self/mounts".to_vec(),
Metadata::new(0o777, time),
);
let mut bytes = format(src, 64 * MIB, opts()).unwrap().into_bytes();
let ino = {
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
r.lookup_no_follow(b"/mtab").expect("lookup /mtab").0
};
let off = inode_offset(&bytes, ino);
crate::ondisk::put_u32(&mut bytes[off..], 0x04, 100);
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let link = r.inode(ino).unwrap();
assert_eq!(
r.read_symlink(&link).unwrap().len(),
Inode::BLOCK_BYTES,
"the read is bounded by the block area the target lives in"
);
}
#[test]
fn a_stored_acl_that_is_not_an_acl_is_refused_and_reported() {
// The attribute parses; its value does not. Handing the stored bytes back would
// give a consumer something it installs as an ACL and the kernel refuses, so the
// read fails — and the scan is what says which inode holds it.
let time = Timestamp::from_secs(1_700_000_000);
let acl = crate::acl::Acl::new(vec![
crate::acl::AclEntry {
who: crate::acl::AclQualifier::UserObj,
perm: crate::acl::Acl::READ | crate::acl::Acl::WRITE,
},
crate::acl::AclEntry {
who: crate::acl::AclQualifier::GroupObj,
perm: crate::acl::Acl::READ,
},
crate::acl::AclEntry {
who: crate::acl::AclQualifier::Other,
perm: crate::acl::Acl::READ,
},
])
.expect("a well-formed ACL");
let src = TreeBuilder::new()
.file(b"/f".to_vec(), b"x".to_vec(), Metadata::new(0o644, time))
.xattr(crate::acl::Acl::ACCESS_NAME.to_vec(), acl.encode());
let mut bytes = format(src, 64 * MIB, opts()).unwrap().into_bytes();
// Find the stored form in the image and give it a version no ACL has. The pattern
// is the whole encoded value, so it names the one place the ACL lives.
let stored = crate::ondisk::encode_acl(&acl);
let at = bytes
.windows(stored.len())
.position(|w| w == stored.as_slice())
.expect("the stored ACL is in the image");
bytes[at] = 0x07;
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let (ino, _) = r.lookup(b"/f").expect("lookup /f");
let inode = r.inode(ino).expect("the inode still reads");
assert!(
matches!(r.xattrs(&inode), Err(ReadError::MalformedAcl { .. })),
"a value that is not an ACL has no form to be read back in"
);
let report = r.scan();
let found = report
.anomalies()
.iter()
.find(|a| a.category == Category::Xattr && a.location.inode == Some(ino))
.expect("the scan names the inode holding it");
assert_eq!(found.severity, Severity::Structural);
}
#[test]
fn an_attribute_block_pointer_past_the_filesystem_is_refused() {
// A filesystem embedded in a larger disk image has bytes beyond its own block
// count that a pointer can still reach. `i_file_acl` is checked against the
// filesystem's block count exactly as a data-block pointer is, so it cannot read
// a neighbouring partition's bytes back as this inode's attributes.
let time = Timestamp::from_secs(1_700_000_000);
let src = TreeBuilder::new()
.file(b"/f".to_vec(), b"x".to_vec(), Metadata::new(0o644, time))
.xattr(b"user.blob".to_vec(), vec![0xAB; 300]);
let mut bytes = format(src, 64 * MIB, opts()).unwrap().into_bytes();
let (ino, blocks_count) = {
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
(
r.lookup(b"/f").expect("lookup /f").0,
r.superblock().blocks_count,
)
};
// The neighbour: one block past the filesystem, holding a well-formed attribute
// block. Parsing it in isolation proves it is readable, so the bounds check is
// the only thing that can stop the read below.
let planted = crate::xattr::Xattr {
name: b"user.neighbour".to_vec(),
value: b"from the next partition".to_vec(),
};
let neighbour = crate::ondisk::encode_block(std::slice::from_ref(&planted), 4096, false);
assert_eq!(
crate::ondisk::parse_block(&neighbour).unwrap(),
vec![planted],
"the planted block is a readable attribute block"
);
let at = usize::try_from(blocks_count).unwrap() * 4096;
bytes.resize(at + 4096, 0);
bytes[at..at + 4096].copy_from_slice(&neighbour);
// i_file_acl_lo sits at inode offset 0x68.
let off = inode_offset(&bytes, ino);
crate::ondisk::put_u32(
&mut bytes[off..],
0x68,
u32::try_from(blocks_count).unwrap(),
);
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let inode = r.inode(ino).unwrap();
assert_eq!(inode.file_acl, blocks_count);
assert!(
matches!(
r.xattrs(&inode),
Err(ReadError::OutOfRange { what: "block", index }) if index == blocks_count
),
"an attribute block outside the filesystem was read: {:?}",
r.xattrs(&inode)
);
}
/// Plant `block` one block past the end of `bytes`, which the caller has arranged to
/// be exactly the filesystem's own length, and return the block number it occupies.
///
/// This is the neighbour every boundary test needs: bytes that sit inside the source
/// and outside the filesystem, so a reference reaching them is stopped by the
/// filesystem's bound rather than by the source running out. Each such test plants
/// something a reader would happily accept if it got there, so what is being asserted
/// is the bound and not the plant.
fn plant_beyond(bytes: &mut Vec<u8>, block: &[u8]) -> u64 {
let at = bytes.len();
assert_eq!(at % 4096, 0, "the image ends on a block boundary");
bytes.extend_from_slice(block);
(at / 4096) as u64
}
#[test]
fn a_superblock_whose_geometry_would_scan_nothing_is_refused_at_open() {
// `group_count` is the addressable span over `s_blocks_per_group`, rounded up,
// and every scan loop runs once per group — so a field that makes the count zero
// makes every verdict "clean" without a byte being examined. The divisor fields
// have been refused at open for exactly that reason; these are the two fields of
// the subtraction, which reach the same zero through the other side. Lenient,
// because the forensic caller a scan verdict serves opens leniently.
let image = format(TreeBuilder::new(), 64 * MIB, opts_no_csum())
.unwrap()
.into_bytes();
// Offsets into the primary superblock at byte 1024: `s_blocks_count_lo` at 0x04,
// `s_first_data_block` at 0x14.
for (field, offset, value) in [
("s_blocks_count", 0x04usize, 0u32),
("s_first_data_block", 0x14, 0xffff_ffff),
] {
let mut mangled = image.clone();
crate::ondisk::put_u32(&mut mangled[1024..], offset, value);
let refused = Reader::open_with(
std::io::Cursor::new(&mangled),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.map(|_| ());
assert!(
matches!(
&refused,
Err(ReadError::Parse(ParseError::InvalidField { field: f, .. }))
if *f == field
),
"{field}: {refused:?}"
);
}
// The subtraction's other zero: at a one-kibibyte block the canonical first data
// block is one, so a block count of one passes a zero check and a first-data-block
// check while the addressable span is still nothing. The refusal has to compare
// the two fields, not each against a constant.
let mut small = opts_no_csum();
small.feature = small.feature.with_block_size(1024);
let image = format(TreeBuilder::new(), 16 * MIB, small)
.unwrap()
.into_bytes();
let mut mangled = image.clone();
crate::ondisk::put_u32(&mut mangled[1024..], 0x04, 1);
let refused = Reader::open_with(
std::io::Cursor::new(&mangled),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.map(|_| ());
assert!(
matches!(
&refused,
Err(ReadError::Parse(ParseError::InvalidField {
field: "s_blocks_count",
value: 1,
..
}))
),
"one block at a one-kibibyte block size scans nothing: {refused:?}"
);
}
#[test]
fn an_inode_table_pointed_past_the_filesystem_is_refused() {
// A group descriptor is what says where its group's inodes live, so a descriptor
// pointing past the filesystem's final block would have the reader take an inode —
// the root among them — out of the bytes that follow an embedded image. Every
// property of that inode would then be the surrounding disk's to choose: its mode,
// its mapping, its inline symlink target.
let mut bytes = format(TreeBuilder::new(), 64 * MIB, opts_no_csum())
.unwrap()
.into_bytes();
let (table, blocks_count, inode_size) = {
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
assert!(r.scan().is_clean(), "the image is clean before the edit");
(
usize::try_from(r.group_descriptor(0).unwrap().inode_table).unwrap(),
r.superblock().blocks_count,
usize::from(r.superblock().inode_size),
)
};
assert_eq!(bytes.len() as u64, blocks_count * 4096);
// The neighbour: a copy of the real table's first block, with the root inode turned
// into a symlink. An unbounded read reports that inode as the root, so the mode is
// what makes the escape visible from outside.
let mut planted = bytes[table * 4096..table * 4096 + 4096].to_vec();
// The root is inode 2, the table's second slot, and `i_mode` opens an inode record.
put_u16(&mut planted, inode_size, 0o120777);
let neighbour = plant_beyond(&mut bytes, &planted);
assert_eq!(neighbour, blocks_count);
// Group 0's descriptor opens the block after the superblock's, and
// `bg_inode_table_lo` sits at descriptor offset 0x08.
crate::ondisk::put_u32(&mut bytes[4096..], 0x08, u32::try_from(neighbour).unwrap());
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
assert!(
matches!(
r.inode(2),
Err(ReadError::OutOfRange {
what: "inode",
index: 2
})
),
"the root inode was read from past the filesystem: {:?}",
r.inode(2)
);
let report = r.scan();
assert!(
report
.anomalies()
.iter()
.any(|a| { a.category == Category::Inode && a.severity == Severity::Structural }),
"the scan reports the unreachable inode table: {report:?}"
);
}
#[test]
fn an_inode_at_the_end_of_a_table_that_runs_past_the_filesystem_is_refused() {
// The bound is on the inode's own bytes, not on the block its table starts in: a
// table beginning inside the filesystem and running past its final block holds
// inodes on both sides of the boundary, and only the ones wholly inside it are
// this filesystem's.
let mut bytes = format(TreeBuilder::new(), 64 * MIB, opts_no_csum())
.unwrap()
.into_bytes();
let (blocks_count, inode_size, ipg) = {
let r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
(
r.superblock().blocks_count,
u64::from(r.superblock().inode_size),
u64::from(r.superblock().inodes_per_group),
)
};
// A table beginning in the filesystem's final block: its first inodes sit inside
// and every one after them does not.
let table = blocks_count - 1;
crate::ondisk::put_u32(&mut bytes[4096..], 0x08, u32::try_from(table).unwrap());
// A neighbour holding a copy of the group's inodes, so the reads below are stopped
// by the boundary rather than by the source ending.
plant_beyond(&mut bytes, &vec![0u8; 4096]);
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let per_block = 4096 / inode_size;
// The last inode of the final block reads; the first past it does not.
assert!(r.inode(u32::try_from(per_block).unwrap()).is_ok());
let outside = u32::try_from(per_block + 1).unwrap();
assert!(outside <= u32::try_from(ipg).unwrap());
assert!(
matches!(
r.inode(outside),
Err(ReadError::OutOfRange { what: "inode", .. })
),
"an inode past the filesystem's final block was read: {:?}",
r.inode(outside)
);
}
#[test]
fn an_inode_bitmap_pointed_past_the_filesystem_is_refused() {
// Which inodes a group holds is read from its bitmap, and `flex_bg` permits that
// bitmap anywhere — so the placement rule that would catch this on an ext2 image
// says nothing here, and the filesystem's own bound is the whole of what stands
// between a descriptor and the bytes after the image.
let mut bytes = format(TreeBuilder::new(), 64 * MIB, opts_no_csum())
.unwrap()
.into_bytes();
let blocks_count = Reader::open(std::io::Cursor::new(&bytes))
.unwrap()
.superblock()
.blocks_count;
// The neighbour: a bitmap with every bit set, which an unbounded read would
// enumerate as a group full of inodes that are not there.
let neighbour = plant_beyond(&mut bytes, &vec![0xffu8; 4096]);
// `bg_inode_bitmap_lo` is at descriptor offset 0x04.
crate::ondisk::put_u32(&mut bytes[4096..], 0x04, u32::try_from(neighbour).unwrap());
let mut r = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.unwrap();
let report = r.scan();
let found = report
.anomalies()
.iter()
.find(|a| a.category == Category::Bitmap && a.location.group == Some(0))
.unwrap_or_else(|| panic!("the scan reports the unreachable bitmap: {report:?}"));
assert_eq!(found.severity, Severity::Structural);
assert_eq!(
found.location.block,
Some(blocks_count),
"the anomaly names the block the descriptor pointed at"
);
}
#[test]
fn an_extent_index_pointed_past_the_filesystem_is_refused() {
// An index entry names the block holding a child node, and a tree is followed by
// reading what it names. A child past the filesystem would have the mapping of a
// file — which blocks it owns, and so which bytes it reads back — supplied from
// outside the image.
let time = Timestamp::from_secs(1_700_000_000);
let src =
TreeBuilder::new().file(b"/f".to_vec(), b"x".to_vec(), Metadata::new(0o644, time));
let mut bytes = format(src, 64 * MIB, opts_no_csum()).unwrap().into_bytes();
let (ino, blocks_count) = {
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
assert!(r.scan().is_clean(), "the image is clean before the edit");
(
r.lookup(b"/f").expect("lookup /f").0,
r.superblock().blocks_count,
)
};
// The neighbour: a well-formed leaf node mapping a block of the filesystem. It
// parses on its own, so nothing but the bound stops the walk from following it.
let mut planted = vec![0u8; 4096];
crate::extent::write_node(
&ExtentNode::Leaves(vec![crate::ondisk::ExtentLeaf {
block: 0,
len: 1,
start: 100,
initialized: true,
}]),
crate::extent::node_capacity(4096),
&mut planted,
)
.expect("the planted node is well formed");
assert!(parse_node(&planted).is_ok());
let neighbour = plant_beyond(&mut bytes, &planted);
// Turn the file's inline root into a one-entry index node pointing at the
// neighbour. `i_block` opens at inode offset 0x28, the header's `eh_depth` is six
// bytes into it, and the entry that follows becomes an index entry whole — a leaf
// and an index lay their twelve bytes out differently, so it is written as one.
let off = inode_offset(&bytes, ino) + 0x28;
put_u16(&mut bytes[off..], 6, 1);
let entry = crate::ondisk::ExtentIdx {
block: 0,
leaf: neighbour,
}
.to_bytes();
bytes[off + EXTENT_ENTRY_SIZE..off + 2 * EXTENT_ENTRY_SIZE].copy_from_slice(&entry);
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let inode = r.inode(ino).unwrap();
assert!(
matches!(
r.read_data(&inode),
Err(ReadError::OutOfRange { what: "block", index }) if index == blocks_count
),
"an extent node outside the filesystem was followed: {:?}",
r.read_data(&inode)
);
let report = r.scan();
let found = report
.anomalies()
.iter()
.find(|a| a.category == Category::ExtentTree && a.location.inode == Some(ino))
.unwrap_or_else(|| panic!("the scan reports the unreachable node: {report:?}"));
assert_eq!(found.severity, Severity::Structural);
}
#[test]
fn an_extent_header_whose_counts_disagree_is_reported() {
// `eh_max` is the node's own account of what it can hold, and a walk that reads
// only `eh_entries` never consults it. A header the kernel and `e2fsck` both call
// corrupt would then be followed as a mapping and pass a scan as clean, which is
// an integrity verdict on a structure no ext4 driver would accept.
let time = Timestamp::from_secs(1_700_000_000);
let src =
TreeBuilder::new().file(b"/f".to_vec(), b"x".to_vec(), Metadata::new(0o644, time));
let clean = format(src, 64 * MIB, opts_no_csum()).unwrap().into_bytes();
let ino = {
let mut r = Reader::open(std::io::Cursor::new(&clean)).unwrap();
assert!(r.scan().is_clean(), "the image is clean before the edit");
r.lookup(b"/f").expect("lookup /f").0
};
let root = inode_offset(&clean, ino) + 0x28;
// `i_block` opens with the header: `eh_entries` two bytes in, `eh_max` four.
for (field, at, value) in [
("eh_max", 4u16, 0u16),
("eh_max", 4, 5), // one past the four the sixty-byte inline area holds
("eh_entries", 2, 2), // two entries in a node declaring room for one
] {
let mut bytes = clean.clone();
if field == "eh_entries" {
put_u16(&mut bytes[root..], 4, 1);
}
put_u16(&mut bytes[root..], usize::from(at), value);
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let inode = r.inode(ino).unwrap();
assert!(
matches!(
r.read_data(&inode),
Err(ReadError::Parse(ParseError::InvalidField {
structure: "ExtentHeader",
field: f,
..
})) if f == field
),
"a {field} of {value} was followed as a mapping: {:?}",
r.read_data(&inode)
);
let report = r.scan();
let found = report
.anomalies()
.iter()
.find(|a| a.category == Category::ExtentTree && a.location.inode == Some(ino))
.unwrap_or_else(|| panic!("a {field} of {value} is reported: {report:?}"));
assert_eq!(found.severity, Severity::Structural);
assert!(!report.is_clean());
}
}
#[test]
fn a_directory_entry_naming_an_inode_that_does_not_exist_is_refused() {
// An entry's inode number is a reference, and the superblock's inode count bounds
// it. Handing back an entry naming an inode the filesystem does not have leaves
// the corruption to be discovered by whoever resolves it, and leaves a scan of the
// whole image reporting nothing at all — the structural fault `e2fsck` names as an
// entry with an invalid inode number.
let time = Timestamp::from_secs(1_700_000_000);
let src =
TreeBuilder::new().file(b"/f".to_vec(), b"x".to_vec(), Metadata::new(0o644, time));
let mut bytes = format(src, 64 * MIB, opts_no_csum()).unwrap().into_bytes();
let (block, inodes_count) = {
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
assert!(r.scan().is_clean(), "the image is clean before the edit");
let root = r.inode(ROOT_INO).unwrap();
let block = r.extent_leaves(&root).unwrap()[0].start;
(
usize::try_from(block).unwrap() * 4096,
r.superblock().inodes_count,
)
};
// The root's entry for `f`, made to name one inode past the last the filesystem
// has. A directory record opens with its inode number.
let mut off = block;
loop {
let (entry, rec_len) = DirEntry::read_from(&bytes[off..block + 4096], 4096).unwrap();
if entry.name == b"f" {
break;
}
off += rec_len;
}
let missing = inodes_count + 1;
crate::ondisk::put_u32(&mut bytes[off..], 0, missing);
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let root = r.inode(ROOT_INO).unwrap();
assert_eq!(
r.read_dir(&root).err(),
Some(ReadError::DirEntryNoSuchInode { inode: missing }),
"a directory listing handed back an entry naming inode {missing}"
);
// The walk reaches the same refusal through the same read, so no path is built
// from an entry that resolves to nothing.
assert!(r.walk().is_err());
// A lenient read is the one that recovers, and it keeps what it can: the bad entry
// is left out and the good ones beside it come back. Discarding the whole listing
// over one reference would cost exactly what a forensic read is for — and it would
// treat a bad reference and a name no directory can hold, two faults of the same
// shape in the same record, in opposite ways.
let mut lenient = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.unwrap();
let root = lenient.inode(ROOT_INO).unwrap();
let names: Vec<Vec<u8>> = lenient
.read_dir(&root)
.expect("a lenient read hands back what it can")
.into_iter()
.map(|e| e.name)
.collect();
assert!(
names.iter().any(|n| n == b"."),
"the good entries survive: {names:?}"
);
assert!(
!names.iter().any(|n| n == b"f"),
"and the one naming nothing does not: {names:?}"
);
let report = r.scan();
let found = report
.anomalies()
.iter()
.find(|a| a.category == Category::Directory && a.location.inode == Some(ROOT_INO))
.unwrap_or_else(|| panic!("the scan reports the bad reference: {report:?}"));
assert_eq!(found.severity, Severity::Structural);
assert!(
found.detail.contains(&missing.to_string()),
"the finding names the inode the entry pointed at: {found:?}"
);
}
#[test]
fn a_directory_entry_carrying_a_name_no_directory_can_hold_is_refused() {
// The sibling of the test above, for the other field of the same record. A name
// holding a path separator traverses out of its directory and a NUL truncates the
// path a consumer builds; the kernel forbids both, so an image carrying one was
// crafted. Leaving the entry out of the walk and saying nothing means an extraction
// writes a tree missing files and reports success, which is the failure a reader of
// hostile bytes must not have.
let time = Timestamp::from_secs(1_700_000_000);
let src =
TreeBuilder::new().file(b"/f".to_vec(), b"x".to_vec(), Metadata::new(0o644, time));
let mut bytes = format(src, 64 * MIB, opts_no_csum()).unwrap().into_bytes();
let block = {
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
assert!(r.scan().is_clean(), "the image is clean before the edit");
let root = r.inode(ROOT_INO).unwrap();
usize::try_from(r.extent_leaves(&root).unwrap()[0].start).unwrap() * 4096
};
// The root's entry for `f`, its one-byte name overwritten with a path separator.
// The record keeps its length and its reference, so the only thing wrong with the
// directory is the name — which is what makes this a test of the name check and not
// of the parser.
let mut off = block;
loop {
let (entry, rec_len) = DirEntry::read_from(&bytes[off..block + 4096], 4096).unwrap();
if entry.name == b"f" {
break;
}
off += rec_len;
}
let (entry, _) = DirEntry::read_from(&bytes[off..block + 4096], 4096).unwrap();
let named = entry.inode;
bytes[off + 8] = b'/';
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let root = r.inode(ROOT_INO).unwrap();
assert_eq!(
r.read_dir(&root).err(),
Some(ReadError::HostileName { inode: named }),
"a directory listing handed back a name no directory can hold"
);
// The walk reaches the refusal through the same read, so no sink is ever offered a
// path built from such a name — and no walk quietly omits the entry instead.
assert!(r.walk().is_err());
let mut lenient = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.unwrap();
let root = lenient.inode(ROOT_INO).unwrap();
let names: Vec<Vec<u8>> = lenient
.read_dir(&root)
.expect("a lenient read hands back what it can")
.into_iter()
.map(|e| e.name)
.collect();
assert!(
names.iter().any(|n| n == b"."),
"the good entries survive: {names:?}"
);
assert!(
!names.iter().any(|n| n.contains(&b'/')),
"and the one no directory can hold does not: {names:?}"
);
let report = r.scan();
let found = report
.anomalies()
.iter()
.find(|a| a.category == Category::Directory && a.location.inode == Some(ROOT_INO))
.unwrap_or_else(|| panic!("the scan reports the name: {report:?}"));
assert_eq!(found.severity, Severity::Structural);
assert!(!report.is_clean());
// The detail says what is wrong without echoing the bytes that are wrong.
assert!(
!found.detail.contains('/'),
"the finding does not render the name: {found:?}"
);
}
#[test]
fn the_descriptor_stride_ignores_s_desc_size_without_64bit() {
// `s_desc_size` describes the group descriptor only under `64bit`. Without that
// feature every descriptor is the 32-byte classic form whatever the word holds,
// so the same filesystem must read identically however that word is set.
// Honoring a stale 64 strides past every descriptor after the first, reading
// group 1's from where group 2's would begin.
let mut o = opts();
o.feature.incompat = o.feature.incompat.without(Incompat::SIXTY_FOUR_BIT);
o.grow = GrowReservation::None;
let bytes = format(TreeBuilder::new(), 256 * MIB, o)
.unwrap()
.into_bytes();
let pristine = {
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
assert_eq!(r.desc_size(), GroupDescriptor::SIZE_32);
assert!(
r.group_count() > 1,
"the image must span more than one group for the stride to show"
);
(0..r.group_count())
.map(|g| r.group_descriptor(g).unwrap())
.collect::<Vec<_>>()
};
// s_desc_size sits at superblock offset 0xfe.
let mut corrupt = bytes.clone();
put_u16(&mut corrupt[1024..], 0xfe, GroupDescriptor::SIZE_64 as u16);
let mut r = Reader::open(std::io::Cursor::new(&corrupt)).unwrap();
assert_eq!(r.desc_size(), GroupDescriptor::SIZE_32);
let after: Vec<_> = (0..r.group_count())
.map(|g| r.group_descriptor(g).unwrap())
.collect();
assert_eq!(
after, pristine,
"a meaningless s_desc_size moved the descriptors"
);
// And under `64bit` the field is what decides: the default profile sets both.
let wide = format(TreeBuilder::new(), 64 * MIB, opts()).unwrap();
let r = Reader::open(std::io::Cursor::new(wide.as_bytes())).unwrap();
assert!(r.feature().is_64bit());
assert_eq!(r.desc_size(), GroupDescriptor::SIZE_64);
}
#[test]
fn a_64bit_superblock_rejects_an_out_of_bounds_desc_size() {
// Under `64bit` s_desc_size sets the descriptor stride and the width its checksum
// covers, so the reader holds it to the kernel's bounds: a power of two from
// `EXT4_MIN_DESC_SIZE_64BIT` (64) up to the block size. A 32-byte value would read
// a 64-bit table at half its stride; a value past the block size would run each
// descriptor beyond its block. Both are refused at open. The default profile is
// `64bit`, and open validates structure before any checksum, so the corruption
// surfaces as the field error, not a checksum mismatch.
let image = format(TreeBuilder::new(), 64 * MIB, opts()).unwrap();
// s_desc_size sits at superblock offset 0xfe; the superblock starts at byte 1024.
for bad in [32u16, 8192] {
let mut corrupt = image.as_bytes().to_vec();
put_u16(&mut corrupt[1024..], 0xfe, bad);
assert!(
matches!(
Reader::open(std::io::Cursor::new(&corrupt)),
Err(ReadError::Parse(ParseError::InvalidField {
field: "s_desc_size",
..
}))
),
"a 64bit image with s_desc_size={bad} must be refused at open"
);
}
// The valid 64-byte value the default writes still opens.
assert!(Reader::open(std::io::Cursor::new(image.as_bytes())).is_ok());
}
#[test]
fn a_corrupted_index_tail_reserved_word_is_caught() {
// The index checksum covers `dt_reserved` as it lies on disk and stands in four
// zero bytes only for `dt_checksum` itself. Folding the whole tail as zeros
// leaves the reserved word uncovered, so a bit flip there passes verification
// unseen — the one case where the two derivations differ.
let time = Timestamp::from_secs(1_700_000_000);
let mut o = opts();
o.feature.block_size = 1024;
o.grow = GrowReservation::None;
let mut src = TreeBuilder::new().directory(b"/d".to_vec(), Metadata::new(0o755, time));
for i in 0..64u32 {
let mut name = format!("/d/entry-{i:04}").into_bytes();
name.resize(b"/d/".len() + 200, b'x');
src = src.file(name, Vec::new(), Metadata::new(0o644, time));
}
let mut bytes = format(src, 64 * MIB, o).unwrap().into_bytes();
let (root_block, tail) = {
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let (_, dir) = r.lookup(b"/d").expect("the directory is present");
assert!(
dir.flags.contains(InodeFlags::INDEX),
"the directory must be hash-indexed for it to have an index tail"
);
r.verify_checksums()
.expect("it verifies clean to begin with");
let block = r.data_blocks(&dir).unwrap()[0];
let raw = r.block(block).unwrap();
let limit = usize::from(get_u16(&raw, DX_ROOT_COUNT_OFFSET));
(block, dx_tail_offset(DX_ROOT_COUNT_OFFSET, limit))
};
// dt_reserved opens the tail; dt_checksum follows it.
let at = usize::try_from(root_block).unwrap() * 1024 + tail;
assert_eq!(bytes[at..at + 4], [0; 4], "the writer leaves it zero");
bytes[at] = 0xff;
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
assert!(
matches!(
r.verify_checksums(),
Err(ReadError::ChecksumMismatch {
object: "directory block",
..
})
),
"a flipped dt_reserved went unnoticed: {:?}",
r.verify_checksums()
);
}
#[test]
fn unsupported_features_are_reported_by_name() {
// A report that renders a real feature as a bare hexadecimal bit tells a reader
// nothing to act on. Every bit ext4 defines is named — whether or not this crate
// models it — and only a bit no ext4 feature claims stays hexadecimal.
let d = describe_unsupported_incompat(
Incompat::META_BG.bits() | 0x0000_4000 | 0x0000_8000 | 0x8000_0000,
);
for name in ["meta_bg", "large_dir", "inline_data", "0x80000000"] {
assert!(d.contains(name), "{name} is missing from {d:?}");
}
let order: Vec<usize> = ["meta_bg", "large_dir", "inline_data", "0x80000000"]
.iter()
.map(|n| d.find(n).unwrap())
.collect();
assert!(
order.windows(2).all(|w| w[0] < w[1]),
"features are listed in ascending bit order: {d:?}"
);
}
#[test]
fn every_incompat_bit_is_named_by_exactly_one_table() {
// Two tables name ext4's `incompat` vocabulary between them: the feature word
// for what this crate models, and the diagnostic table for the rest. A bit in
// both would be named twice in one report.
for (name, bit) in UNMODELLED_INCOMPAT {
assert_eq!(bit.count_ones(), 1, "{name} names exactly one bit");
assert!(
Incompat::from_bits(*bit).names().is_empty(),
"{name} ({bit:#x}) is named by the feature word too"
);
assert_eq!(
bit & SUPPORTED_INCOMPAT,
0,
"{name} ({bit:#x}) is a feature the reader claims to follow"
);
}
}
#[test]
fn round_trips_inline_and_block_xattrs() {
let time = Timestamp::from_secs(1_700_000_000);
let cap = vec![
0x01, 0x00, 0x00, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
];
let src = TreeBuilder::new()
.directory(b"/bin".to_vec(), Metadata::new(0o755, time))
// A small attribute set stays inline in the inode.
.file(
b"/bin/ping".to_vec(),
b"elf".to_vec(),
Metadata::new(0o755, time),
)
.xattr(b"security.capability".to_vec(), cap.clone())
.xattr(b"user.note".to_vec(), b"hi".to_vec())
// A large value overflows to an external xattr block.
.file(b"/big".to_vec(), b"x".to_vec(), Metadata::new(0o644, time))
.xattr(b"user.blob".to_vec(), vec![0xAB; 300]);
let image = format(src, 64 * MIB, opts()).unwrap();
let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
let tree = walk_tree(&mut r);
let ping = &tree[&b"/bin/ping".to_vec()];
assert_eq!(ping.file_acl, 0, "small set stays inline, no acl block");
let mut got = r.xattrs(ping).unwrap();
got.sort_by(|a, b| a.name.cmp(&b.name));
assert_eq!(got.len(), 2);
assert_eq!(got[0].name, b"security.capability");
assert_eq!(got[0].value, cap);
assert_eq!(got[1].name, b"user.note");
let big = &tree[&b"/big".to_vec()];
assert_ne!(big.file_acl, 0, "large value spilled to a block");
let blob = r.xattrs(big).unwrap();
assert_eq!(blob.len(), 1);
assert_eq!(blob[0].name, b"user.blob");
assert_eq!(blob[0].value, vec![0xAB; 300]);
}
#[test]
fn a_xattr_set_splits_between_the_inode_and_its_block() {
// `user.huge` alone fills a whole 4096-byte attribute block, so the set only
// exists on disk because `user.tiny` stays inline: one inode carrying both
// regions at once, the state a kernel-written inode holds such a set in.
let time = Timestamp::from_secs(1_700_000_000);
let huge = vec![0xAA; 4040];
let tiny = vec![0xBB; 60];
let src = TreeBuilder::new()
.file(b"/f".to_vec(), b"x".to_vec(), Metadata::new(0o644, time))
.xattr(b"user.huge".to_vec(), huge.clone())
.xattr(b"user.tiny".to_vec(), tiny.clone());
let image = format(src, 64 * MIB, opts()).unwrap();
let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
let tree = walk_tree(&mut r);
let f = &tree[&b"/f".to_vec()];
assert_ne!(f.file_acl, 0, "the huge value spilled to a block");
let inline = parse_inline(&f.inline_xattr).unwrap();
assert_eq!(inline.len(), 1, "the tiny value stayed inline");
assert_eq!(inline[0].name, b"user.tiny");
let mut got = r.xattrs(f).unwrap();
got.sort_by(|a, b| a.name.cmp(&b.name));
assert_eq!(got.len(), 2, "both regions read back as one set");
assert_eq!(got[0].name, b"user.huge");
assert_eq!(got[0].value, huge);
assert_eq!(got[1].name, b"user.tiny");
assert_eq!(got[1].value, tiny);
}
#[test]
fn round_trips_device_fifo_and_socket_nodes() {
let time = Timestamp::from_secs(1_700_000_000);
let m = |mode| Metadata::new(mode, time);
let src = TreeBuilder::new()
.directory(b"/dev".to_vec(), m(0o755))
.char_device(b"/dev/null".to_vec(), 1, 3, m(0o666))
.block_device(b"/dev/sda".to_vec(), 8, 0, m(0o660))
.fifo(b"/dev/initctl".to_vec(), m(0o600))
.socket(b"/dev/log".to_vec(), m(0o666));
let image = format(src, 64 * MIB, opts()).unwrap();
let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
let tree = walk_tree(&mut r);
let null = &tree[&b"/dev/null".to_vec()];
assert_eq!(null.mode & 0o170000, 0o020000, "char device");
assert_eq!(null.mode & 0o7777, 0o666);
assert_eq!(r.device(null), (1, 3));
assert_eq!(null.blocks, 0, "no data blocks");
let sda = &tree[&b"/dev/sda".to_vec()];
assert_eq!(sda.mode & 0o170000, 0o060000, "block device");
assert_eq!(r.device(sda), (8, 0));
assert_eq!(
tree[&b"/dev/initctl".to_vec()].mode & 0o170000,
0o010000,
"fifo"
);
assert_eq!(
tree[&b"/dev/log".to_vec()].mode & 0o170000,
0o140000,
"socket"
);
}
#[test]
fn round_trips_distinct_and_fixed_timestamps() {
let m = Metadata::new(0o644, Timestamp::from_secs(500)).with_times(
Timestamp::from_secs(100),
Timestamp::from_secs(200),
Timestamp::from_secs(300),
);
let src = TreeBuilder::new().file(b"/f".to_vec(), b"x".to_vec(), m);
let image = format(src, 64 * MIB, opts()).unwrap();
let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
let root = r.inode(2).unwrap();
let f_ino = r
.read_dir(&root)
.unwrap()
.iter()
.find(|e| e.name == b"f")
.unwrap()
.inode;
let f = r.inode(f_ino).unwrap();
assert_eq!(f.atime, Timestamp::from_secs(100));
assert_eq!(f.ctime, Timestamp::from_secs(200));
assert_eq!(f.mtime, Timestamp::from_secs(300));
assert_eq!(
f.crtime,
Timestamp::from_secs(300),
"crtime derives from mtime"
);
// The fixed-time knob overrides every entry time.
let mut fixed = opts();
fixed.fixed_time = Some(Timestamp::from_secs(42));
let src = TreeBuilder::new().file(b"/f".to_vec(), b"x".to_vec(), m);
let image = format(src, 64 * MIB, fixed).unwrap();
let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
let root = r.inode(2).unwrap();
let f_ino = r
.read_dir(&root)
.unwrap()
.iter()
.find(|e| e.name == b"f")
.unwrap()
.inode;
let f = r.inode(f_ino).unwrap();
assert_eq!(f.atime, Timestamp::from_secs(42));
assert_eq!(f.mtime, Timestamp::from_secs(42));
assert_eq!(f.crtime, Timestamp::from_secs(42));
}
#[test]
fn the_fixed_time_clamp_reaches_the_reserved_structural_inodes() {
// The clamp forces every inode's four times, including the reserved structural
// inodes the materializer builds rather than the model — resize (7), journal
// (8), and orphan (12). Unset, those inodes take the format time; set, they take
// the clamp. (Teeth: before the fix they took the format time even when clamped.)
let format_time = Timestamp::from_secs(1_700_000_000);
let clamp = Timestamp::from_secs(42);
assert_ne!(
clamp, format_time,
"the clamp must differ from the format time"
);
let mut fixed = opts();
fixed.fixed_time = Some(clamp);
let image = format(TreeBuilder::new(), 64 * MIB, fixed).unwrap();
let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
for ino in [7u32, 8, 12] {
let s = r.inode(ino).unwrap();
assert_eq!(
(s.atime, s.ctime, s.mtime, s.crtime),
(clamp, clamp, clamp, clamp),
"inode {ino}'s times honor the clamp, not the format time"
);
}
// Unset, the same structural inodes carry the format time.
let image = format(TreeBuilder::new(), 64 * MIB, opts()).unwrap();
let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
for ino in [7u32, 8, 12] {
assert_eq!(
r.inode(ino).unwrap().mtime,
format_time,
"inode {ino} takes the format time when the clamp is unset"
);
}
}
#[test]
fn verifies_metadata_checksums_and_detects_corruption() {
let time = Timestamp::from_secs(1_700_000_000);
let m = |mode| Metadata::new(mode, time);
let src = TreeBuilder::new()
.directory(b"/etc".to_vec(), m(0o755))
.file(b"/etc/hostname".to_vec(), b"ferrosys\n".to_vec(), m(0o644))
.file(b"/big".to_vec(), vec![0x42; 20_000], m(0o600));
// A multi-group image so descriptors and backups are exercised.
let bytes = format(src, 512 * MIB, opts()).unwrap().into_bytes();
// A well-formed image passes its own checksum oracle.
Reader::open(std::io::Cursor::new(&bytes))
.unwrap()
.verify_checksums()
.unwrap();
// Corrupting a superblock field the checksum covers is caught.
let mut corrupt = bytes.clone();
corrupt[1024 + 0x30] ^= 0xff; // s_wtime
assert!(matches!(
Reader::open(std::io::Cursor::new(&corrupt))
.unwrap()
.verify_checksums(),
Err(ReadError::ChecksumMismatch {
object: "superblock",
..
})
));
// Corrupting the root inode is caught, located by number.
let root_off = {
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
r.group_descriptor(0).unwrap().inode_table as usize * 4096 + 256
};
let mut corrupt = bytes.clone();
corrupt[root_off] ^= 0xff; // a byte of root inode's mode
assert!(matches!(
Reader::open(std::io::Cursor::new(&corrupt))
.unwrap()
.verify_checksums(),
Err(ReadError::ChecksumMismatch {
object: "inode",
index: 2,
..
})
));
// Corrupting a group descriptor is caught and located by group. Group 1's
// free-block count is a field the descriptor checksum covers and nothing else
// reads before the descriptors are verified, so the descriptor's own oracle is
// what fires. The descriptor table follows the superblock's block at 4 KiB
// blocks, and `desc_size` is 64 bytes under `64bit`.
let desc_size = {
let r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
r.superblock().desc_size as usize
};
assert_eq!(desc_size, 64, "the default feature set sets 64bit");
let mut corrupt = bytes.clone();
corrupt[4096 + desc_size + 0x0c] ^= 0xff; // group 1's bg_free_blocks_count_lo
assert!(matches!(
Reader::open(std::io::Cursor::new(&corrupt))
.unwrap()
.verify_checksums(),
Err(ReadError::ChecksumMismatch {
object: "group descriptor",
index: 1,
..
})
));
}
#[test]
fn verifies_bitmap_directory_and_xattr_checksums() {
// Beyond the superblock, descriptors, and inodes, the oracle covers bitmaps,
// directory-block tails, and the external attribute block. Each category must
// stay clean on a well-formed image and fire when its own bytes are corrupted.
let time = Timestamp::from_secs(1_700_000_000);
let m = |mode| Metadata::new(mode, time);
let src = TreeBuilder::new()
.directory(b"/etc".to_vec(), m(0o755))
.file(b"/etc/hostname".to_vec(), b"ferrosys\n".to_vec(), m(0o644))
// A 400-byte value cannot fit inline, so it spills to an external block.
.file(b"/etc/big".to_vec(), b"x".to_vec(), m(0o644))
.xattr(b"user.big".to_vec(), vec![0xcd; 400]);
let bytes = format(src, 64 * MIB, opts()).unwrap().into_bytes();
// Clean: neither the strict oracle nor the lenient scan finds anything.
{
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
r.verify_checksums()
.expect("a well-formed image verifies clean");
let report = r.scan();
assert!(
report.anomalies().is_empty(),
"a clean image scans clean, got {:?}",
report.anomalies()
);
}
// Locate group 0's block bitmap, the root's first directory block, and the
// external attribute block of /etc/big.
let (bitmap_block, dir_block, xattr_block) = {
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let bitmap_block = r.group_descriptor(0).unwrap().block_bitmap;
let root = r.inode(2).unwrap();
let dir_block = r.extent_leaves(&root).unwrap()[0].start;
// /etc/big is the file carrying the external attribute block.
let mut xattr_block = 0u64;
for e in r.walk().unwrap() {
if e.path == b"/etc/big" {
xattr_block = e.inode.file_acl;
}
}
assert_ne!(xattr_block, 0, "the file has an external attribute block");
(bitmap_block, dir_block, xattr_block)
};
let bs = 4096u64;
let corrupt_at = |block: u64, within: u64| {
let mut c = bytes.clone();
c[(block * bs + within) as usize] ^= 0xff;
c
};
// A block-bitmap byte the descriptor's checksum covers.
let c = corrupt_at(bitmap_block, 0);
let mut r = Reader::open(std::io::Cursor::new(&c)).unwrap();
assert!(matches!(
r.verify_checksums(),
Err(ReadError::ChecksumMismatch {
object: "block bitmap",
..
})
));
assert!(
r.scan()
.anomalies()
.iter()
.any(|a| a.category == Category::Bitmap),
"scan reports a Bitmap anomaly"
);
// A directory-block byte inside the region the tail checksum covers.
let c = corrupt_at(dir_block, 40);
let mut r = Reader::open(std::io::Cursor::new(&c)).unwrap();
assert!(matches!(
r.verify_checksums(),
Err(ReadError::ChecksumMismatch {
object: "directory block",
..
})
));
assert!(
r.scan()
.anomalies()
.iter()
.any(|a| a.category == Category::Directory),
"scan reports a Directory anomaly"
);
// A byte of the external attribute block, which its h_checksum covers.
let c = corrupt_at(xattr_block, 32);
let mut r = Reader::open(std::io::Cursor::new(&c)).unwrap();
assert!(matches!(
r.verify_checksums(),
Err(ReadError::ChecksumMismatch {
object: "xattr block",
..
})
));
assert!(
r.scan()
.anomalies()
.iter()
.any(|a| a.category == Category::Xattr),
"scan reports an Xattr anomaly"
);
}
#[test]
fn verifies_a_two_level_htree_with_interior_index_nodes() {
// A hash tree deep enough to need interior index nodes has three block roles —
// root, interior node, and leaf — each with a differently shaped checksum tail.
// The verifier tells them apart by following the index down from the root, so a
// whole two-level tree must verify with no false mismatch. A 1024-byte block
// indexes at most 123 leaves at one level, so a directory spanning more than
// that many blocks is provably two-level.
let time = Timestamp::from_secs(1_700_000_000);
let mut o = opts();
o.feature.block_size = 1024;
o.grow = GrowReservation::None;
// 800 entries with 200-byte names pack four to a leaf, filling roughly 200 leaf
// blocks — past what a single 1024-byte root indexes, so the writer grows an
// interior level between the root and the leaves.
let mut src = TreeBuilder::new().directory(b"/bigdir".to_vec(), Metadata::new(0o755, time));
for i in 0..800u32 {
let mut name = format!("/bigdir/entry-{i:04}").into_bytes();
name.resize(b"/bigdir/".len() + 200, b'x');
src = src.file(name, Vec::new(), Metadata::new(0o644, time));
}
let bytes = format(src, 64 * MIB, o).unwrap().into_bytes();
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let (_, dir) = r.lookup(b"/bigdir").expect("the directory is present");
assert!(
dir.flags.contains(InodeFlags::INDEX),
"an 800-entry directory must be hash-indexed"
);
let block_count = dir.size / 1024;
assert!(
block_count > 124,
"a two-level tree spans more than a one-level root's 123 leaves and its \
root, but this directory is {block_count} blocks — the interior level \
this test exists to exercise was never grown"
);
// The whole tree verifies: the root, both interior nodes, and every leaf.
r.verify_checksums()
.expect("a well-formed two-level htree verifies clean");
assert!(
r.scan().anomalies().is_empty(),
"a clean two-level htree scans clean, got {:?}",
r.scan().anomalies()
);
// And every name survives the index the checksum pass just walked past.
let names: std::collections::BTreeSet<Vec<u8>> = r
.read_dir(&dir)
.expect("read_dir")
.into_iter()
.filter(|e| e.name != b"." && e.name != b"..")
.map(|e| e.name)
.collect();
assert_eq!(names.len(), 800, "every name survives the two-level index");
}
#[test]
fn an_index_block_whose_limit_cannot_place_its_tail_is_a_fault_not_a_clean_block() {
// The fields that place an index block's checksum tail are inside what the
// checksum covers, so a crafted `limit` that pushes the tail past the block is
// the one corruption shape that could turn the whole block invisible to
// verification: with the tail unplaceable there is nothing to compare, and a
// clean answer would bless every byte of the block unread. The kernel fails
// verification on exactly this shape, and so must this pass.
let time = Timestamp::from_secs(1_700_000_000);
let mut src = TreeBuilder::new().directory(b"/bigdir".to_vec(), Metadata::new(0o755, time));
for i in 0..200u32 {
let mut name = format!("/bigdir/entry-{i:04}").into_bytes();
name.resize(b"/bigdir/".len() + 100, b'x');
src = src.file(name, Vec::new(), Metadata::new(0o644, time));
}
let mut bytes = format(src, 64 * MIB, opts()).unwrap().into_bytes();
let root_off = {
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let (_, dir) = r.lookup(b"/bigdir").expect("the directory is present");
assert!(
dir.flags.contains(InodeFlags::INDEX),
"a 200-entry directory must be hash-indexed"
);
r.verify_checksums().expect("clean before the edit");
let bs = 1024usize << r.superblock().log_block_size;
usize::try_from(r.extent_leaves(&dir).unwrap()[0].start).unwrap() * bs
};
// The root's stored capacity. 0xffff places the tail far past the block, so the
// verifier that trusted it would find nothing to compare and wave the block
// through — with every other byte of the block free to differ.
put_u16(&mut bytes[root_off..], DX_ROOT_COUNT_OFFSET, 0xffff);
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let err = r
.verify_checksums()
.expect_err("an unplaceable tail is a fault");
assert!(
matches!(
err,
ReadError::Parse(ParseError::InvalidField {
structure: "DxCountLimit",
field: "limit",
..
})
),
"{err:?}"
);
}
#[test]
fn the_orphan_files_blocks_carry_their_magic_and_checksum() {
// The orphan file is where the kernel records an inode it is deleting, so its
// blocks are metadata: each closes with the orphan magic word and a checksum over
// its entries. Both must hold on a fresh image, and corrupting either must be
// seen — a wrong tail here would surface as corruption at the first deletion.
let bytes = format(TreeBuilder::new(), 64 * MIB, opts())
.unwrap()
.into_bytes();
let (first_block, blocks) = {
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let ino = r.superblock().orphan_file_inum;
assert_eq!(ino, 12, "the orphan file takes the inode past /lost+found");
let inode = r.inode(ino).unwrap();
assert_eq!(inode.links_count, 1);
assert_eq!(
inode.size,
32 * 4096,
"the size heuristic's floor at 64 MiB"
);
let leaves = r.extent_leaves(&inode).unwrap();
(leaves[0].start, u64::from(leaves[0].len))
};
assert_eq!(blocks, 32, "the file maps as one contiguous run");
// Clean: the tails verify, strictly and in the lenient scan.
{
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
r.verify_checksums().expect("the orphan tails verify");
assert!(r.scan().anomalies().is_empty());
}
let bs = 4096u64;
let corrupt_at = |offset: u64| {
let mut c = bytes.clone();
c[offset as usize] ^= 0xff;
c
};
// The checksum of the last block: a per-block field, so corrupting the final one
// proves every block is checked, not just the first.
let last = first_block + blocks - 1;
let c = corrupt_at((last + 1) * bs - 4);
let mut r = Reader::open(std::io::Cursor::new(&c)).unwrap();
assert!(matches!(
r.verify_checksums(),
Err(ReadError::ChecksumMismatch {
object: "orphan block",
..
})
));
assert!(
r.scan()
.anomalies()
.iter()
.any(|a| a.category == Category::Orphan),
"scan reports an Orphan anomaly"
);
// The magic word: a block that has lost it is not an orphan block at all.
let c = corrupt_at((first_block + 1) * bs - 8);
let mut r = Reader::open(std::io::Cursor::new(&c)).unwrap();
assert!(matches!(
r.verify_checksums(),
Err(ReadError::BadOrphanFile)
));
// A superblock claiming the feature but naming no inode for the file is
// malformed, not an empty orphan list: `s_orphan_file_inum` is at 0x280.
let mut c = bytes.clone();
c[1024 + 0x280..1024 + 0x284].copy_from_slice(&0u32.to_le_bytes());
let mut r = Reader::open(std::io::Cursor::new(&c)).unwrap();
assert!(
r.scan()
.anomalies()
.iter()
.any(|a| a.category == Category::Orphan),
"a feature with no file behind it is an Orphan anomaly"
);
}
#[test]
fn a_scan_is_bounded_by_the_bytes_that_exist_not_the_claimed_block_count() {
// A superblock claiming `2^64 - 1` blocks implies more block groups than a
// `u32` holds. Every group past the ones the source physically covers has no
// bitmap block to read, so each would record one "unreadable bitmap" anomaly —
// billions of them, an allocation driven by a count a bit-flip can set rather
// than by the bytes that exist. Both of the scan's group loops are capped at
// the groups the source can hold, so the work and the report stay proportional
// to the image.
let bytes = format(TreeBuilder::new(), 64 * MIB, opts())
.unwrap()
.into_bytes();
let mut corrupt = bytes.clone();
// s_blocks_count_lo is at superblock offset 0x04 and _hi at 0x150.
crate::ondisk::put_u32(&mut corrupt[1024 + 0x04..], 0, 0xffff_ffff);
crate::ondisk::put_u32(&mut corrupt[1024 + 0x150..], 0, 0xffff_ffff);
let mut r = Reader::open_with(
std::io::Cursor::new(&corrupt),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.unwrap();
let report = r.scan();
// The image spans two groups' worth of descriptors at most, so the report is a
// handful of anomalies. The bound is loose on purpose: what it rules out is a
// report whose length tracks the claimed group count.
assert!(
report.anomalies().len() < 64,
"the scan reported {} anomalies for a 64 MiB image — it is walking the \
claimed group count, not the source",
report.anomalies().len()
);
// A regression surfaces as this test never returning rather than as a failed
// assertion: the loop it guards runs four billion times and grows the report as
// it goes. That is the shape of the fault, so it is the shape of the failure.
}
#[test]
fn verify_checksums_ignores_a_hostile_free_inode_count() {
// In-use inodes are enumerated from the bitmaps, not from
// `inodes_count - free_inodes_count`, so a free count set above the inode count
// — a value that would once have underflowed a subtraction — neither panics nor
// steers the enumeration.
let bytes = format(TreeBuilder::new(), 64 * MIB, opts())
.unwrap()
.into_bytes();
let mut corrupt = bytes.clone();
// s_free_inodes_count is at superblock offset 0x10; set it above s_inodes_count.
crate::ondisk::put_u32(&mut corrupt[1024 + 0x10..], 0, 0xffff_ffff);
let mut r = Reader::open(std::io::Cursor::new(&corrupt)).unwrap();
// It returns (an error or Ok), never panics.
let _ = r.verify_checksums();
let _ = r.scan();
}
#[test]
fn open_rejects_a_bad_superblock() {
let mut bytes = vec![0u8; 4096 * 16];
// No magic anywhere.
assert!(matches!(
Reader::open(std::io::Cursor::new(&bytes)),
Err(ReadError::Parse(ParseError::BadMagic { .. }))
));
// Truncated image.
bytes.truncate(512);
assert!(Reader::open(std::io::Cursor::new(&bytes)).is_err());
}
#[test]
fn multi_group_image_round_trips() {
let time = Timestamp::from_secs(1_700_000_000);
let src = TreeBuilder::new().file(
b"/f".to_vec(),
b"multi-group".to_vec(),
Metadata::new(0o644, time),
);
let image = format(src, 512 * MIB, opts()).unwrap();
let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
assert_eq!(r.superblock().blocks_count, 131072);
// Descriptors for the backup groups read back with the flex placements.
let g3 = r.group_descriptor(3).unwrap();
assert_eq!(g3.block_bitmap, 8);
assert_eq!(g3.inode_table, 1549);
let root = r.inode(2).unwrap();
let f = r
.read_dir(&root)
.unwrap()
.iter()
.find(|e| e.name == b"f")
.unwrap()
.inode;
let f_inode = r.inode(f).unwrap();
assert_eq!(r.read_data(&f_inode).unwrap(), b"multi-group");
}
#[test]
fn opens_a_filesystem_at_an_arbitrary_offset() {
// A filesystem embedded past a leading gap — a partition inside a whole-disk
// image — reads back identically to a bare one.
let time = Timestamp::from_secs(1);
let src = TreeBuilder::new().file(
b"/hi".to_vec(),
b"there".to_vec(),
Metadata::new(0o644, time),
);
let image = format(src, 64 * MIB, opts()).unwrap();
let base = 1_048_576u64; // a 1 MiB partition gap ahead of the filesystem
let mut padded = vec![0x9au8; base as usize];
padded.extend_from_slice(image.as_bytes());
let mut r = Reader::open_with(
std::io::Cursor::new(&padded),
&OpenOptions::new().base(base),
)
.unwrap();
assert_eq!(r.policy(), ReadPolicy::Strict);
let root = r.inode(2).unwrap();
let names: Vec<_> = r
.read_dir(&root)
.unwrap()
.into_iter()
.map(|e| e.name)
.collect();
assert!(names.contains(&b"hi".to_vec()));
let hi = r
.walk()
.unwrap()
.into_iter()
.find(|e| e.path == b"/hi")
.unwrap()
.inode;
assert_eq!(r.read_data(&hi).unwrap(), b"there");
// Opening the same buffer at offset zero lands in the gap and is rejected.
assert!(Reader::open(std::io::Cursor::new(&padded)).is_err());
}
#[test]
fn open_with_refuses_a_base_that_overflows_the_superblock_position() {
// A `base` near the top of the range leaves no room for the superblock 1024 bytes
// in. The sum is refused as out of range rather than wrapping to a small seek that
// would read a non-superblock and mislabel the image.
let src = std::io::Cursor::new(vec![0u8; 4096]);
// `Reader` is not `Debug`, so the `Result` is matched rather than unwrapped.
assert!(matches!(
Reader::open_with(src, &OpenOptions::new().base(u64::MAX).policy(ReadPolicy::Lenient)),
Err(ReadError::OutOfRange {
what: "bytes",
index
}) if index == u64::MAX
));
}
#[test]
fn errors_classify_as_typed_anomalies() {
// A missing inode cannot be parsed further: structural, located by number.
let a = ReadError::NoSuchInode { inode: 7 }.anomaly();
assert_eq!(a.severity, Severity::Structural);
assert_eq!(a.category, Category::Inode);
assert_eq!(a.location.inode, Some(7));
// A checksum mismatch parses but is self-inconsistent: integrity.
let cm = ReadError::ChecksumMismatch {
object: "inode",
index: 2,
stored: 1,
computed: 2,
}
.anomaly();
assert_eq!(cm.severity, Severity::Integrity);
assert_eq!(cm.category, Category::Inode);
assert_eq!(cm.location.inode, Some(2));
// An out-of-range block reference is structural, located by block. With no
// call-site context an ownerless block reference names no subsystem, so it falls
// to the structural catch-all; the scan supplies the real owner — the extent
// tree or the inode — when it walks a mapping (see
// scan_files_an_out_of_range_extent_against_the_extent_tree).
let oor = ReadError::OutOfRange {
what: "block",
index: 99,
}
.anomaly();
assert_eq!(oor.severity, Severity::Structural);
assert_eq!(oor.category, Category::Superblock);
assert_eq!(oor.location.block, Some(99));
// The strict policy is fatal at conformance and above, and is the default.
assert!(ReadPolicy::Strict.is_fatal(Severity::Structural));
assert!(ReadPolicy::Strict.is_fatal(Severity::Integrity));
assert!(ReadPolicy::Strict.is_fatal(Severity::Conformance));
assert!(!ReadPolicy::Strict.is_fatal(Severity::Cosmetic));
assert_eq!(ReadPolicy::default(), ReadPolicy::Strict);
}
#[test]
fn lenient_policy_is_fatal_at_nothing() {
for sev in [
Severity::Cosmetic,
Severity::Conformance,
Severity::Integrity,
Severity::Structural,
] {
assert!(!ReadPolicy::Lenient.is_fatal(sev), "{sev:?} under lenient");
}
// Strict still draws the line at conformance-and-above, and is the default.
assert!(ReadPolicy::Strict.is_fatal(Severity::Structural));
assert!(!ReadPolicy::Strict.is_fatal(Severity::Cosmetic));
assert_eq!(ReadPolicy::default(), ReadPolicy::Strict);
}
#[test]
fn scans_a_clean_multigroup_image_as_clean() {
// A populated, multi-group image scans with no anomaly, so no policy finds it
// fatal. This exercises the whole walk — superblock, every descriptor, every
// in-use inode and its extent tree, including the journal inode.
let time = Timestamp::from_secs(1_700_000_000);
let m = |mode| Metadata::new(mode, time);
let src = TreeBuilder::new()
.directory(b"/etc".to_vec(), m(0o755))
.file(b"/etc/hostname".to_vec(), b"ferrosys\n".to_vec(), m(0o644))
.file(b"/big".to_vec(), vec![0x42; 200_000], m(0o600));
let image = format(src, 512 * MIB, opts()).unwrap();
let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
let report = r.scan();
assert!(
report.is_clean(),
"unexpected anomalies: {:?}",
report.anomalies()
);
assert_eq!(report.worst_severity(), None);
assert!(!report.has_fatal(ReadPolicy::Strict));
assert!(!report.has_fatal(ReadPolicy::Lenient));
assert_eq!(report.to_report().to_table(), "no findings\n");
assert!(!report.is_truncated());
assert_eq!(
report.to_report().to_json(),
"{\"schema\":2,\"clean\":true,\"count\":0,\"truncated\":false,\"findings\":[]}"
);
}
/// A feature profile with the metadata checksums turned off, so a test that edits a
/// superblock or descriptor field is not also reporting the checksum it invalidated.
/// The seed serves the checksums, so it comes off with them.
fn opts_no_csum() -> FormatOptions {
let mut o = opts();
o.feature = FeatureSet::DEFAULT
.with_feature("metadata_csum", false)
.expect("a known name")
.with_feature("metadata_csum_seed", false)
.expect("a known name");
assert_eq!(o.feature.validate(), Ok(()));
o
}
#[test]
fn refuses_an_unsupported_incompat_feature_and_reports_it() {
// The `incompat` word is the one an implementation must refuse when it does not
// recognize a bit: an unknown extension, or `meta_bg`, whose distributed group
// descriptors this reader does not follow. A strict open refuses; a lenient scan
// reports it as a structural anomaly against the superblock.
let clean = format(TreeBuilder::new(), 64 * MIB, opts())
.unwrap()
.into_bytes();
// The unmodified default profile is entirely supported, so a strict open accepts
// it — the refusal below fires on the injected bit, not the profile at large.
assert!(Reader::open(std::io::Cursor::new(&clean)).is_ok());
// The `incompat` word is a little-endian u32 at superblock offset 0x60, and the
// superblock begins 1024 bytes into the image.
let incompat = 1024 + 0x60;
for (name, byte, mask, want) in [
("meta_bg", 0, 0x10u8, 0x10u32),
("unknown high bit", 3, 0x80u8, 0x8000_0000u32),
] {
let mut bytes = clean.clone();
bytes[incompat + byte] |= mask;
assert_eq!(
Reader::open(std::io::Cursor::new(&bytes)).err(),
Some(ReadError::UnsupportedIncompat { bits: want }),
"a strict open refuses {name}",
);
let mut r = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.unwrap_or_else(|_| panic!("a lenient open accepts {name}"));
let report = r.scan();
let found = report
.anomalies()
.iter()
.find(|a| a.category == Category::Superblock && a.severity == Severity::Structural);
let found = found.unwrap_or_else(|| {
panic!("scan reports {name} as a structural superblock anomaly: {report:?}")
});
assert!(
found.detail.contains(name) || found.detail.contains("0x80000000"),
"the anomaly names the feature it found: {}",
found.detail
);
assert!(report.has_fatal(ReadPolicy::Strict));
}
}
#[test]
fn flags_an_extent_flagged_inode_without_the_extent_feature() {
// An inode carrying the extent-format flag on a filesystem whose superblock does
// not enable the extent feature is the incoherence `e2fsck` faults. The mapping
// mode was chosen by the inode flag alone, so the disagreement went unreported;
// it is a structural anomaly now. Checksums are off so the edit does not also
// trip the superblock's own checksum.
let src = TreeBuilder::new().file(
b"/hello".to_vec(),
b"world\n".to_vec(),
Metadata::new(0o644, Timestamp::from_secs(1_700_000_000)),
);
let mut bytes = format(src, 64 * MIB, opts_no_csum()).unwrap().into_bytes();
// Clear the extent bit (0x40) in the `incompat` word while the inodes keep their
// extent-format flag, so the two now disagree.
bytes[1024 + 0x60] &= !0x40;
let mut r = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.unwrap();
assert!(!r.feature().has_extents());
let report = r.scan();
let flagged: Vec<u32> = report
.anomalies()
.iter()
.filter(|a| {
a.category == Category::Inode
&& a.severity == Severity::Structural
&& a.detail.contains("extent format")
})
.filter_map(|a| a.location.inode)
.collect();
// The root directory and the file are both extent-mapped, so both are flagged;
// the block-mapped resize inode is not.
assert!(
flagged.contains(&ROOT_INO),
"the root inode is flagged: {report:?}"
);
assert!(
flagged.len() >= 2,
"every extent-mapped inode is flagged: {flagged:?}"
);
assert!(report.has_fatal(ReadPolicy::Strict));
}
/// Overwrite inode `number` with an extent-mapped inode of `mode` whose inline tree
/// names `runs` runs of `len` blocks, every one of them mapping the same physical
/// blocks from `start`, under a declared size of `size` bytes.
///
/// This is the shape a crafted image takes to make one inode's mapping claim far more
/// than the image holds: 256 bytes of inode naming the same blocks over and over, at
/// logical offsets a size field says are all in use. Nothing is added to the image —
/// the blocks it names are blocks that were already there.
fn craft_extent_inode(
bytes: &mut [u8],
number: u32,
mode: u16,
runs: u32,
len: u16,
start: u64,
size: u64,
) {
use crate::ondisk::{EXTENT_ENTRY_SIZE, ExtentHeader, ExtentLeaf};
let mut inode = Inode::empty(256);
inode.mode = mode;
inode.links_count = 1;
inode.flags = InodeFlags::EXTENTS;
inode.size = size;
let header = ExtentHeader {
entries: runs as u16,
max: runs as u16,
depth: 0,
generation: 0,
};
inode.block[..EXTENT_ENTRY_SIZE].copy_from_slice(&header.to_bytes());
for r in 0..runs {
let leaf = ExtentLeaf {
block: r * u32::from(len),
len,
start,
initialized: true,
};
let off = EXTENT_ENTRY_SIZE * (1 + r as usize);
inode.block[off..off + EXTENT_ENTRY_SIZE]
.copy_from_slice(&leaf.to_bytes().expect("a representable run"));
}
let at = inode_offset(bytes, number);
inode
.write_to(&mut bytes[at..at + 256], 256)
.expect("write the crafted inode");
}
#[test]
fn a_directory_mapping_one_block_many_times_is_judged_once_per_block() {
// A directory's logical-to-physical map may name one block from many logical
// offsets; only a crafted image does, and a scan that judged each offset
// separately would turn 256 bytes of inode into thousands of findings, each
// carrying an owned description. The verdict belongs to the block, so it is
// reached once per block however many offsets name it.
//
// The declared size is the largest a directory's size field holds — a million
// blocks, sixty-four times what the image has — so the map is bounded by the
// blocks that exist rather than by that claim: a directory has no holes.
const RUNS: u32 = 4;
const LEN: u16 = 1000;
let image = format(TreeBuilder::new(), 64 * MIB, opts()).unwrap();
let mut bytes = image.into_bytes();
// Inode 3 is a reserved inode the bitmap already marks in use, so the scan reaches
// it without the bitmap having to be crafted too.
craft_extent_inode(
&mut bytes,
3,
0o040755,
RUNS,
LEN,
2000,
u64::from(u32::MAX),
);
let mut r = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.unwrap();
let report = r.scan();
// The blocks it names hold no directory tail, so each is one finding — and there
// are as many findings as there are blocks, not as many as there are offsets.
let per_block = report
.anomalies()
.iter()
.filter(|a| a.category == Category::Directory && a.location.inode == Some(3))
.count();
assert_eq!(
per_block,
LEN as usize,
"one finding per block named, not per logical offset ({} offsets name {LEN} blocks)",
RUNS as usize * LEN as usize
);
assert!(!report.is_truncated(), "this image is fully scanned");
}
#[test]
fn a_report_stops_at_its_cap_and_says_so() {
// How many findings an image yields is the image's own claim, so a report bounds
// what it holds: a handful of crafted inodes can name every block in the image,
// and each block that fails its tail is a finding. The scan stops at the cap and
// records that it did, rather than growing a report in proportion to what a
// crafted image asks for.
let image = format(TreeBuilder::new(), 64 * MIB, opts()).unwrap();
let mut bytes = image.into_bytes();
// The reserved inodes the bitmap already marks in use. Three of them, each naming
// four thousand of the image's blocks, is more than the cap holds.
for number in [3u32, 4, 5] {
craft_extent_inode(
&mut bytes,
number,
0o040755,
4,
4000,
2000,
u64::from(u32::MAX),
);
}
let mut r = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.unwrap();
let report = r.scan();
assert!(
report.is_truncated(),
"a report past the cap says it stopped short"
);
assert_eq!(
report.anomalies().len(),
crate::FindingReport::MAX_FINDINGS,
"a truncated report holds exactly the cap"
);
// The truncation reaches every projection: a consumer of any of them must be able
// to tell a complete report from one that stopped.
let projected = report.to_report();
assert!(projected.to_json().contains("\"truncated\":true"));
assert!(projected.to_table().contains("report truncated at 10000"));
assert!(
projected
.to_sarif(None)
.contains("\"toolExecutionNotifications\""),
"SARIF records the short run as a notification about the invocation"
);
// The findings still stand: a truncated report is a floor, not a blank verdict.
assert!(report.has_fatal(ReadPolicy::Strict));
// And the cap is a knob, not a constant: a caller reading with a gigabyte to
// spare tightens it and gets a report that says it stopped that much sooner —
// and says so naming the bound that applied. A notice quoting the default
// constant would tell a caller who asked for seven that ten thousand were found.
let mut tight = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new()
.policy(ReadPolicy::Lenient)
.limits(Limits::new().max_findings(7)),
)
.unwrap();
let short = tight.scan();
assert!(short.is_truncated());
assert_eq!(short.anomalies().len(), 7);
let notice = "report truncated at 7 findings; the rest of the image was not scanned";
let short = short.to_report();
assert!(short.to_table().contains(notice), "{}", short.to_table());
assert!(short.to_sarif(None).contains(notice));
// A cap of zero stops the scan before it reads a group. The report is empty, and
// it is *not* clean: an absence of findings from a scan that never looked is not
// a verdict about the image.
let mut none = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new()
.policy(ReadPolicy::Lenient)
.limits(Limits::new().max_findings(0)),
)
.unwrap();
let empty = none.scan();
assert!(empty.is_truncated());
assert!(empty.anomalies().is_empty());
assert!(
!empty.is_clean(),
"a truncated report claimed the image was clean"
);
let empty = empty.to_report();
assert!(empty.to_json().contains("\"clean\":false"));
assert!(empty.to_table().contains("report truncated at 0 findings"));
}
#[test]
fn a_walk_is_bounded_by_the_names_the_source_has_room_for() {
// Distinct directory inodes may map the *same* data blocks, so a crafted image can
// describe an unbounded number of names from a handful of blocks. Every name a
// well-formed filesystem holds spends at least a dirent's worth of its own blocks,
// so the source's length bounds the walk without ever reaching a real image.
let source = TreeBuilder::new()
.directory(
b"/a".to_vec(),
Metadata::new(0o755, Timestamp::from_secs(0)),
)
.file(
b"/a/f".to_vec(),
b"x".to_vec(),
Metadata::new(0o644, Timestamp::from_secs(0)),
);
let image = format(source, 64 * MIB, opts()).unwrap();
let bytes = image.into_bytes();
// At the default limits every name comes back: the structural bound on a 64 MiB
// image is millions of names, and this tree has three.
let mut r = Reader::open_with(std::io::Cursor::new(&bytes), &OpenOptions::new()).unwrap();
let full = r.walk().unwrap();
assert!(full.len() >= 3, "{full:?}");
// A caller-set cap refuses rather than truncating: a short list with nothing to
// say it is short is a silent loss, and a walk is what a caller extracts a tree
// from.
let mut capped = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().limits(Limits::new().max_walk_entries(2)),
)
.unwrap();
assert!(matches!(
capped.walk(),
Err(ReadError::WalkTooLarge { limit: 2 })
));
}
#[test]
fn the_names_a_walk_has_found_count_against_its_bound_too() {
// The bound governs the frontier, not only what has been handed to the visitor. It
// has to: each visit may discover a whole directory's worth of names, so a bound on
// visits alone would leave the frontier bounded by the product rather than by the
// bound — and distinct directory inodes may map the same blocks, so a small image
// can present many such directories.
let time = Timestamp::from_secs(0);
let mut source = TreeBuilder::new();
for n in 0..8u8 {
source = source.file(
format!("/f{n}").into_bytes(),
b"x".to_vec(),
Metadata::new(0o644, time),
);
}
let bytes = format(source, 64 * MIB, opts()).unwrap().into_bytes();
let mut capped = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().limits(Limits::new().max_walk_entries(3)),
)
.unwrap();
let mut visited = 0usize;
let stopped = capped.walk_with::<ReadError>(|_, _| {
visited += 1;
Ok(())
});
assert!(matches!(stopped, Err(ReadError::WalkTooLarge { limit: 3 })));
// And it is refused where the frontier passes the bound, not after a bound's worth
// of names has been visited off the front of it.
assert_eq!(visited, 0, "the walk stopped before it spent the memory");
}
#[test]
fn the_bounds_are_measured_from_the_filesystem_and_not_from_the_source() {
// Every structural bound is derived from this: the blocks a scan walks, the inodes
// it verifies, the names a walk may find. All three are statements about the
// filesystem, which begins at the offset the image was opened at — so a small
// partition inside a large disk image must be bounded by the partition.
const BASE: usize = 1 << 20;
const TRAILING: usize = 3 << 20;
let source = TreeBuilder::new().file(
b"/f".to_vec(),
b"x".to_vec(),
Metadata::new(0o644, Timestamp::from_secs(0)),
);
let image = format(source, 8 * MIB, opts()).unwrap().into_bytes();
let mut disk = vec![0u8; BASE];
disk.extend_from_slice(&image);
disk.extend_from_slice(&vec![0u8; TRAILING]);
let mut r = Reader::open_with(
std::io::Cursor::new(&disk),
&OpenOptions::new().base(BASE as u64),
)
.unwrap();
assert_eq!(
r.source_len(),
(image.len() + TRAILING) as u64,
"what is in front of the filesystem is not the filesystem's to be bounded by"
);
// The filesystem itself still reads whole: the bound is a ceiling, and tightening
// it must not refuse an image that fits under it.
assert!(r.walk().unwrap().iter().any(|e| e.path == b"/f"));
}
#[test]
fn a_window_takes_only_the_extent_leaves_it_falls_in() {
// `map_window` says only the mapping structures the window touches are read, and the
// extent side read the entire tree for every window: a fresh walk, a fresh `Vec`, and
// a fresh `HashSet` per call, with no `first`/`len` passed to the descent at all.
// The tar path streams through an 8 KiB window, so that was a whole tree walk per two
// blocks.
//
// The descent is now bounded by the window at both ends: an index entry whose
// subtree lies outside it is skipped without its child block being read, and a leaf
// outside it is not collected. Only the second is observable on a tree small enough
// for this harness to build — a multi-level tree needs a file the allocator will not
// lay out contiguously, which nothing here can ask for — so that is what is asserted,
// and it is the same bound applied at the level above.
let time = Timestamp::from_secs(1_700_000_000);
let src = TreeBuilder::new().file(
b"/f".to_vec(),
vec![b'x'; 64 * 4096],
Metadata::new(0o644, time),
);
let bytes = format(src, 16 * MIB, opts()).unwrap().into_bytes();
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let (_, inode) = r.lookup(b"/f").unwrap();
assert!(
!r.extent_leaves_in(&inode, 0..64).unwrap().is_empty(),
"the file's own blocks are in the window that covers them"
);
assert!(
r.extent_leaves_in(&inode, 1_000..1_064).unwrap().is_empty(),
"a window past everything the file maps takes no leaf"
);
// And the whole-tree form is still the whole tree, which is what the scan reads.
assert_eq!(
r.extent_leaves(&inode).unwrap(),
r.extent_leaves_in(&inode, 0..u64::MAX).unwrap()
);
}
#[test]
fn a_directory_answers_to_the_walk_cap_and_reads_each_of_its_blocks_once() {
// `Limits::max_walk_entries` says it governs a single directory as well as a tree,
// and it did not: a whole listing was gathered before a tree walk consulted the cap
// once. A mapping may point every one of its slots at one block — nothing in the
// format stops it, and a directory of eight gibibytes' apparent length can do it
// from ten kilobytes of real content — so a listing that read each named block
// yielded that block's entries a million times over before any bound applied.
let time = Timestamp::from_secs(1_700_000_000);
let mut src = TreeBuilder::new().directory(b"/d".to_vec(), Metadata::new(0o755, time));
for i in 0..64 {
src = src.file(
format!("/d/f{i:03}").into_bytes(),
b"x".to_vec(),
Metadata::new(0o644, time),
);
}
let bytes = format(src, 16 * MIB, opts()).unwrap().into_bytes();
let mut r = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().limits(Limits::new().max_walk_entries(8)),
)
.unwrap();
let (_, dir) = r.lookup(b"/d").unwrap();
assert!(
matches!(r.read_dir(&dir), Err(ReadError::WalkTooLarge { limit: 8 })),
"one directory answers to the cap where it is read"
);
// And the same directory under no cap lists each of its names once, which is what
// reading a repeated block twice would break.
let mut r = Reader::open_with(std::io::Cursor::new(&bytes), &OpenOptions::new()).unwrap();
let (_, dir) = r.lookup(b"/d").unwrap();
let names: Vec<Vec<u8>> = r
.read_dir(&dir)
.unwrap()
.into_iter()
.map(|e| e.name)
.collect();
let unique: HashSet<&Vec<u8>> = names.iter().collect();
assert_eq!(names.len(), unique.len(), "a name is listed once");
}
#[test]
fn a_walk_refuses_a_path_longer_than_a_path_may_be() {
// Nothing in the format bounds how deep a tree nests, and the walk's entry cap counts
// names rather than the bytes their paths occupy — so a tree of directories each
// holding one entry naming the next stays under every count while its paths grow by
// a component each and cost the walk their sum.
//
// A path past `PATH_MAX` is not a path anything could act on: a consumer resolving
// one against a host gets `ENAMETOOLONG`. So the walk stops there and says so.
let time = Timestamp::from_secs(1_700_000_000);
let mut src = TreeBuilder::new();
let mut path = String::new();
// Each component is 40 bytes, so a hundred and twenty of them run past 4096.
for i in 0..120 {
path.push('/');
path.push_str(&format!("{i:03}-a-component-of-substantial-length"));
src = src.directory(path.clone().into_bytes(), Metadata::new(0o755, time));
}
assert!(
path.len() > 4096,
"the fixture reaches past a path's length"
);
let bytes = format(src, 32 * MIB, opts()).unwrap().into_bytes();
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
assert!(
matches!(r.walk(), Err(ReadError::PathTooLong { limit: 4096 })),
"a walk that would build a path no consumer could use stops instead"
);
}
#[test]
fn a_symlink_target_is_bounded_by_what_a_path_can_be() {
// A slow symlink falls through to a whole-file read, which trusts `i_size` — so a
// crafted size made one link allocate whatever it claimed, and a symlink's size is
// read at 32 bits, which is four gibibytes per link. `resolve` already applied the
// right ceiling in the one place it looked; every other caller of `read_symlink`
// did not, and `FsTree::link_target` is on that list, so every sink was too.
//
// A target has a structural ceiling a regular file's contents do not: no path this
// or any other kernel resolves is longer than `PATH_MAX`.
let time = Timestamp::from_secs(1_700_000_000);
// Long enough to need a block of its own, which is what makes it a slow symlink.
let src = TreeBuilder::new().symlink(
b"/link".to_vec(),
vec![b'a'; 200],
Metadata::new(0o777, time),
);
let mut bytes = format(src, 16 * MIB, opts_no_csum()).unwrap().into_bytes();
let number = {
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let (number, inode) = r.lookup_no_follow(b"/link").unwrap();
assert_eq!(r.read_symlink(&inode).unwrap().len(), 200);
number
};
// `i_size` at 400 MiB, which is what the read would have allocated.
let at = inode_offset(&bytes, number);
crate::ondisk::put_u32(&mut bytes[at..], 4, 400 << 20);
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let (_, inode) = r.lookup_no_follow(b"/link").unwrap();
assert!(
matches!(
r.read_symlink(&inode),
Err(ReadError::FileTooLarge { limit: 4096, .. })
),
"a target longer than a path can be is refused before it is read"
);
}
#[test]
fn a_file_read_is_capped_only_where_a_caller_asks() {
// `read_data` trusts `i_size`, because a sparse file's logical size legitimately
// exceeds the blocks behind it. A caller reading an image it does not trust caps
// it explicitly.
let source = TreeBuilder::new().file(
b"/f".to_vec(),
vec![7u8; 8192],
Metadata::new(0o644, Timestamp::from_secs(0)),
);
let image = format(source, 64 * MIB, opts()).unwrap();
let bytes = image.into_bytes();
let mut r = Reader::open_with(std::io::Cursor::new(&bytes), &OpenOptions::new()).unwrap();
let (_, inode) = r.lookup(b"/f").unwrap();
assert_eq!(r.read_data(&inode).unwrap().len(), 8192);
// Over the cap the read is refused, and the error carries both numbers a caller
// acts on. A hundred bytes of an eight-kilobyte file returned as `Ok` would be
// indistinguishable from a hundred-byte file — the silent loss the walk's own
// bound exists to avoid, one object down.
let mut capped = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().limits(Limits::new().max_file_bytes(100)),
)
.unwrap();
let (_, inode) = capped.lookup(b"/f").unwrap();
assert!(matches!(
capped.read_data(&inode),
Err(ReadError::FileTooLarge {
size: 8192,
limit: 100
})
));
// Streaming the same file is bounded the same way: the cap expresses distrust of
// the declared size, not a memory bound, so it is not escaped by not holding the
// bytes.
let mut sink = Vec::new();
assert!(matches!(
capped.read_data_to(&inode, &mut sink),
Err(ReadError::FileTooLarge { .. })
));
assert!(sink.is_empty(), "nothing is written before the refusal");
// A ranged read is bounded by the buffer the caller supplies, so the cap does not
// apply to it: this is the way to read part of a file deliberately, and the count
// it returns is what makes the partial read representable.
let mut buf = [0u8; 100];
assert_eq!(capped.read_into(&inode, 0, &mut buf).unwrap(), 100);
assert_eq!(buf, [7u8; 100]);
}
#[test]
fn a_capped_read_never_maps_the_claim_behind_it() {
// The cap is asked for by a caller reading an image it does not trust, so the
// image it has to hold against is the crafted one. A size field claiming sixteen
// terabytes on a 64 MiB image implies a map of 2^32 logical blocks — eight bytes
// each, thirty-four gigabytes — so a cap that let the read begin and truncated
// afterwards would allocate that before returning anything.
//
// What proves nothing was mapped is that this completes: the size is checked
// before a block of map exists.
const CLAIM: u64 = 1 << 44; // sixteen terabytes, on a 64 MiB image
let image = format(TreeBuilder::new(), 64 * MIB, opts()).unwrap();
let mut bytes = image.into_bytes();
craft_extent_inode(&mut bytes, 12, 0o100600, 1, 4, 2000, CLAIM);
for cap in [0u64, 10, 8192] {
let mut r = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new()
.policy(ReadPolicy::Lenient)
.limits(Limits::new().max_file_bytes(cap)),
)
.unwrap();
let inode = r.inode(12).unwrap();
assert!(
matches!(
r.read_data(&inode),
Err(ReadError::FileTooLarge { size: CLAIM, .. })
),
"a {CLAIM}-byte claim must be refused under a {cap}-byte cap"
);
}
// And a ranged read of the same inode reads the window it was asked for without
// mapping the claim: the crafted extent covers logical block 0, so the first
// block's bytes are readable while everything the claim adds past it is not
// materialized at all.
let mut r = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.unwrap();
let inode = r.inode(12).unwrap();
let mut buf = [0u8; 4096];
assert_eq!(r.read_into(&inode, 0, &mut buf).unwrap(), 4096);
}
#[test]
fn a_ranged_read_reads_the_window_it_is_given() {
// The primitive the whole-file forms are built on, over both mappings: a window
// anywhere in the file, holes filled as zeros, short only at the end of the file.
// `-O ^extent` is the classic direct/indirect map, where a window deep into the
// file is reached through the indirect blocks above it.
for label in ["extent", "block-mapped"] {
let mut o = opts();
if label == "block-mapped" {
o.feature = FeatureSet::EXT2;
}
// Long enough to reach single indirection under the classic map (12 direct
// blocks = 48 KiB at a 4 KiB block), with a recognizable byte per block.
let mut contents = Vec::new();
for block in 0..20u8 {
contents.extend(std::iter::repeat_n(block, 4096));
}
let source = TreeBuilder::new().file(
b"/f".to_vec(),
contents.clone(),
Metadata::new(0o644, Timestamp::from_secs(0)),
);
let image = format(source, 64 * MIB, o).unwrap();
let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
let (_, inode) = r.lookup(b"/f").unwrap();
// A whole-file read still returns the whole file, however it is now built.
assert_eq!(
r.read_data(&inode).unwrap(),
contents,
"{label}: whole file"
);
// A window crossing a block boundary, and one past the direct blocks.
for (offset, len) in [
(0usize, 10usize),
(4090, 12),
(65536, 8192),
(4096 * 19, 4096),
] {
let mut buf = vec![0u8; len];
let filled = r.read_into(&inode, offset as u64, &mut buf).unwrap();
assert_eq!(filled, len, "{label}: {len} bytes at {offset}");
assert_eq!(&buf[..filled], &contents[offset..offset + len]);
}
// A read at the end is short rather than an error, and one past the end reads
// nothing: that is how a caller knows it reached the end rather than a bound.
let mut buf = vec![0u8; 4096];
let filled = r
.read_into(&inode, contents.len() as u64 - 10, &mut buf)
.unwrap();
assert_eq!(filled, 10, "{label}: a read at the end is short");
assert_eq!(
r.read_into(&inode, contents.len() as u64, &mut buf)
.unwrap(),
0,
"{label}: a read past the end fills nothing"
);
// Streaming yields the same bytes as holding them, which is the property that
// lets a caller choose by memory rather than by fidelity.
let mut sink = Vec::new();
assert_eq!(
r.read_data_to(&inode, &mut sink).unwrap(),
contents.len() as u64
);
assert_eq!(sink, contents, "{label}: streamed bytes");
}
}
#[test]
fn a_ranged_read_of_a_sparse_file_reads_holes_as_zeros() {
// A hole reads as zeros without occupying a block, at any offset and at any level
// of the classic map — and a windowed read must fill the caller's buffer with them
// rather than leave whatever it held. The hole here is the whole file: an inode
// whose size claims blocks its mapping never names.
let image = format(TreeBuilder::new(), 64 * MIB, opts()).unwrap();
let mut bytes = image.into_bytes();
// A size of four blocks over a one-block extent at logical block 0: blocks 1..4
// are a hole the tree does not name.
craft_extent_inode(&mut bytes, 12, 0o100600, 1, 4, 2000, 4 * 4096);
let mut r = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.unwrap();
let inode = r.inode(12).unwrap();
let mut buf = vec![0xffu8; 8192];
let filled = r.read_into(&inode, 4096, &mut buf).unwrap();
assert_eq!(filled, 8192);
assert!(
buf.iter().all(|&b| b == 0),
"a hole reads as zeros, overwriting the buffer's previous contents"
);
}
#[test]
fn a_walk_with_visits_every_name_without_gathering_them() {
// The callback form and the gathering form describe the same tree in the same
// order; what differs is only whether the entries are held. Comparing the two is
// what keeps them from drifting.
let time = Timestamp::from_secs(0);
let source = TreeBuilder::new()
.directory(b"/etc".to_vec(), Metadata::new(0o755, time))
.file(
b"/etc/hostname".to_vec(),
b"host\n".to_vec(),
Metadata::new(0o644, time),
)
.directory(b"/usr".to_vec(), Metadata::new(0o755, time))
.file(
b"/usr/x".to_vec(),
b"x".to_vec(),
Metadata::new(0o644, time),
);
let image = format(source, 64 * MIB, opts()).unwrap();
let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
let gathered: Vec<Vec<u8>> = r.walk().unwrap().into_iter().map(|e| e.path).collect();
let mut streamed = Vec::new();
// The reader is handed to the callback, so an entry's contents are readable as it
// is reached — which is what makes a fixed-memory pass over a whole tree possible.
let mut bytes_seen = 0u64;
r.walk_with::<ReadError>(|reader, entry| {
if entry.inode.mode & 0o170000 == 0o100000 {
bytes_seen += reader.read_data(&entry.inode)?.len() as u64;
}
streamed.push(entry.path);
Ok(())
})
.unwrap();
assert_eq!(streamed, gathered);
assert_eq!(bytes_seen, 6, "hostname's five bytes and x's one");
// An error the callback returns stops the walk and comes back unchanged, so a
// consumer's own failure is never reported as a fault in the image.
let mut count = 0usize;
let stopped = r.walk_with::<ReadError>(|_, _| {
count += 1;
if count == 2 {
return Err(ReadError::BadJournal);
}
Ok(())
});
assert!(matches!(stopped, Err(ReadError::BadJournal)));
assert_eq!(count, 2, "the walk stops at the failing entry");
// The entry bound applies to the callback form too, so a hostile image cannot
// drive an unbounded number of calls.
let mut capped = Reader::open_with(
std::io::Cursor::new(image.as_bytes()),
&OpenOptions::new().limits(Limits::new().max_walk_entries(2)),
)
.unwrap();
assert!(matches!(
capped.walk_with::<ReadError>(|_, _| Ok(())),
Err(ReadError::WalkTooLarge { limit: 2 })
));
}
#[test]
fn a_named_seed_overrides_the_one_the_image_implies() {
// An image whose UUID was changed after it was written carries checksums computed
// from the old seed. Naming that seed is what lets it be verified at all; naming
// the wrong one makes every checksum fail, which is what proves the override is
// the value actually used.
let image = format(TreeBuilder::new(), 64 * MIB, opts()).unwrap();
let bytes = image.into_bytes();
let mut own = Reader::open_with(std::io::Cursor::new(&bytes), &OpenOptions::new()).unwrap();
own.verify_checksums()
.expect("the image verifies against its own seed");
let mut wrong = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().csum_seed(0xdead_beef),
)
.unwrap();
assert!(
matches!(
wrong.verify_checksums(),
Err(ReadError::ChecksumMismatch { .. })
),
"a seed the image's checksums were not computed from must not verify"
);
}
#[test]
fn the_journal_and_orphan_file_are_read_within_the_image_they_claim_to_be_in() {
// Two structures a scan reaches by name out of the superblock rather than through
// the inode walk: the journal, of which it reads the first block, and the orphan
// file, of which it reads every one. Both are regular files, whose size fields
// reach past thirty-two bits — so a crafted size claims terabytes of blocks, and a
// reader that materialized a map from that claim would allocate one entry per
// block claimed before looking at anything.
//
// The journal's map is built to the one block wanted; the orphan file's claim is
// measured against the blocks the image has, and a file that cannot fit is
// reported malformed. Neither costs memory in proportion to the claim, which is
// what this test is: it completes.
const CLAIM: u64 = 1 << 44; // sixteen terabytes, on a 64 MiB image
let image = format(TreeBuilder::new(), 64 * MIB, opts()).unwrap();
let mut bytes = image.into_bytes();
craft_extent_inode(&mut bytes, 8, 0o100600, 1, 4, 2000, CLAIM);
craft_extent_inode(&mut bytes, 12, 0o100600, 1, 4, 2000, CLAIM);
let mut r = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.unwrap();
assert_eq!(r.superblock().journal_inum, 8);
assert_eq!(r.superblock().orphan_file_inum, 12);
let report = r.scan();
// The orphan file is reported malformed rather than walked.
assert!(
report
.anomalies()
.iter()
.any(|a| a.category == Category::Orphan),
"the orphan file's claim is faulted: {}",
report.to_report().to_table()
);
// The journal's first block is read, and it holds no journal superblock.
assert!(
report
.anomalies()
.iter()
.any(|a| a.category == Category::Journal),
"the journal's first block is judged: {}",
report.to_report().to_table()
);
// Reading the journal directly is likewise bounded, and reaches the same verdict.
assert!(matches!(
r.journal_superblock(),
Err(ReadError::Parse(_) | ReadError::BadJournal)
));
}
#[test]
fn flags_an_attribute_block_without_the_ext_attr_feature() {
// `ext_attr` is what says a filesystem holds attributes at all. An inode naming an
// attribute block on a superblock without it is the incoherence `e2fsck` reports
// as "i_file_acl for inode N is B, should be zero": the feature word denies the
// block the pointer claims, so the reader cannot vouch for what that block holds.
let time = Timestamp::from_secs(1_700_000_000);
// A value too large for the inline region spills to a block, which is what puts a
// block number in `i_file_acl`.
let src = TreeBuilder::new()
.file(b"/f".to_vec(), b"data".to_vec(), Metadata::new(0o644, time))
.xattr(b"user.big".to_vec(), vec![0xAB; 2000]);
let mut bytes = format(src, 64 * MIB, opts_no_csum()).unwrap().into_bytes();
{
let mut r = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.unwrap();
assert!(r.feature().has_ext_attr());
assert!(
r.scan().is_clean(),
"the image is coherent while the feature stands"
);
}
// Clear `ext_attr` (compat 0x08) while the inode keeps its attribute block.
bytes[1024 + 0x5c] &= !0x08;
let mut r = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.unwrap();
assert!(!r.feature().has_ext_attr());
let report = r.scan();
let found = report
.anomalies()
.iter()
.find(|a| a.category == Category::Xattr && a.detail.contains("attribute block"))
.unwrap_or_else(|| panic!("the orphaned attribute block is flagged: {report:?}"));
assert_eq!(found.severity, Severity::Structural);
// The anomaly locates both ends of the disagreement: the inode and the block.
assert!(found.location.inode.is_some() && found.location.block.is_some());
assert!(report.has_fatal(ReadPolicy::Strict));
}
#[test]
fn flags_a_large_regular_file_without_the_large_file_feature() {
// A regular file of 2 GiB or more is what `large_file` describes, and the resize
// inode at a 4096-byte block is such a file — 4 GiB of declared classic-map reach.
// Clearing the feature is the incoherence `e2fsck` reports as "filesystem contains
// large files, but lacks LARGE_FILE flag in superblock".
let mut bytes = format(TreeBuilder::new(), 64 * MIB, opts_no_csum())
.unwrap()
.into_bytes();
{
let mut r = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.unwrap();
assert!(r.feature().has_large_file());
assert!(r.scan().is_clean());
}
// Clear `large_file` (ro_compat 0x02, at superblock offset 0x64).
bytes[1024 + 0x64] &= !0x02;
let mut r = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.unwrap();
assert!(!r.feature().has_large_file());
let report = r.scan();
let found = report
.anomalies()
.iter()
.find(|a| a.detail.contains("regular file"))
.unwrap_or_else(|| panic!("the large file is flagged: {report:?}"));
assert_eq!(found.severity, Severity::Conformance);
assert_eq!(found.category, Category::Inode);
assert_eq!(
found.location.inode,
Some(7),
"the resize inode is the file that reaches the bound"
);
// The directories the image holds are far smaller than the bound, and the bound is
// on regular files regardless — so exactly one inode is named.
assert_eq!(
report
.anomalies()
.iter()
.filter(|a| a.detail.contains("regular file"))
.count(),
1
);
assert!(report.has_fatal(ReadPolicy::Strict));
}
#[test]
fn flags_an_indexed_directory_without_the_dir_index_feature() {
// A directory carrying the hash-index flag on a superblock without `dir_index` is
// the incoherence `e2fsck` reports as "inode N has INDEX_FL flag set on filesystem
// without htree support". The directory still reads linearly, so it is a
// conformance deviation rather than a structural one.
let time = Timestamp::from_secs(1_700_000_000);
let mut src = TreeBuilder::new().directory(b"/d".to_vec(), Metadata::new(0o755, time));
for i in 0..600u32 {
let name = format!("/d/entry-{i:04}-padded-out-to-force-an-index").into_bytes();
src = src.file(name, Vec::new(), Metadata::new(0o644, time));
}
let mut bytes = format(src, 64 * MIB, opts_no_csum()).unwrap().into_bytes();
{
let mut r = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.unwrap();
assert!(r.feature().has_dir_index());
let (_, dir) = r.lookup(b"/d").expect("the directory");
assert!(
dir.flags.contains(InodeFlags::INDEX),
"the directory is large enough to be indexed"
);
assert!(r.scan().is_clean());
}
// Clear `dir_index` (compat 0x20) while the directory keeps its index flag.
bytes[1024 + 0x5c] &= !0x20;
let mut r = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.unwrap();
assert!(!r.feature().has_dir_index());
let report = r.scan();
let found = report
.anomalies()
.iter()
.find(|a| a.detail.contains("hash-indexed"))
.unwrap_or_else(|| panic!("the unadvertised index is flagged: {report:?}"));
assert_eq!(found.severity, Severity::Conformance);
assert_eq!(found.category, Category::Directory);
assert!(found.location.inode.is_some());
assert!(report.has_fatal(ReadPolicy::Strict));
}
#[test]
fn flags_the_hash_index_flag_on_something_that_is_not_a_directory() {
// The flag describes how a *directory's* blocks are organized, so on a regular file
// it means nothing — the incoherence `e2fsck` reports as "inode N has INDEX_FL flag
// set but is not a directory". It is a different fault from a directory whose flag
// the feature words deny, and reporting that one here would describe a regular file
// as a hash-indexed directory.
let time = Timestamp::from_secs(1_700_000_000);
let src =
TreeBuilder::new().file(b"/f".to_vec(), b"data".to_vec(), Metadata::new(0o644, time));
let mut bytes = format(src, 64 * MIB, opts_no_csum()).unwrap().into_bytes();
let (number, _) = {
let mut r = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.unwrap();
assert!(r.feature().has_dir_index());
assert!(r.scan().is_clean());
r.lookup(b"/f").expect("the file")
};
// Set INDEX_FL (0x1000) in the file's `i_flags`, at offset 0x20 of the inode.
let at = inode_offset(&bytes, number) + 0x20;
bytes[at + 1] |= 0x10;
let mut r = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.unwrap();
let file = r.inode(number).unwrap();
assert!(file.flags.contains(InodeFlags::INDEX));
let report = r.scan();
let found = report
.anomalies()
.iter()
.find(|a| a.detail.contains("not a directory"))
.unwrap_or_else(|| panic!("the flag on a non-directory is flagged: {report:?}"));
assert_eq!(found.severity, Severity::Conformance);
assert_eq!(found.category, Category::Inode);
assert_eq!(found.location.inode, Some(number));
// And not the directory rule: the feature stands, and this is not a directory.
assert!(
!report
.anomalies()
.iter()
.any(|a| a.detail.contains("does not enable the dir_index feature")),
"a regular file was described as a hash-indexed directory: {report:?}"
);
// Clearing `dir_index` (compat 0x20) does not add the directory rule either: that
// rule is about a directory, and the two faults stay distinct.
bytes[1024 + 0x5c] &= !0x20;
let mut r = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.unwrap();
let report = r.scan();
assert!(
!report
.anomalies()
.iter()
.any(|a| a.detail.contains("does not enable the dir_index feature")),
"{report:?}"
);
assert!(
report
.anomalies()
.iter()
.any(|a| a.detail.contains("not a directory"))
);
}
#[test]
fn the_dirent_bound_is_the_shortest_record_the_format_encodes() {
// `MIN_DIRENT_LEN` and `min_rec_len` spell the same rule — an eight-byte header
// plus a name, rounded up to four — in two places, because the bound divides a
// `u64` and the rule's function is not `const`. Holding them equal is what keeps a
// change to the header or the alignment from moving one and not the other.
assert_eq!(MIN_DIRENT_LEN, u64::from(crate::ondisk::min_rec_len(1)));
// And it really is the floor: a zero-length name is not a record a filesystem
// holds, and every longer name costs more.
assert!(MIN_DIRENT_LEN <= u64::from(crate::ondisk::min_rec_len(255)));
}
#[test]
fn flags_metadata_outside_its_group_without_flex_bg() {
// With `flex_bg` off, each group's bitmaps and inode table must lie within the
// group. This image is physically a flex one — group 1's metadata is packed into
// group 0 — so clearing the `flex_bg` bit makes that packing illegal, exactly the
// corruption `e2fsck` reports as "bitmap for group 1 is not in group". Two groups
// at 4 KiB blocks, checksums off so the edit stands alone.
let image = format(TreeBuilder::new(), 256 * MIB, opts_no_csum()).unwrap();
let mut bytes = image.into_bytes();
// Flex layout packs the later groups' metadata into group 0, so with flex_bg on
// the image scans clean: the placement rule does not apply.
{
let mut r = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.unwrap();
assert!(r.feature().has_flex_bg());
assert!(
r.scan().is_clean(),
"a flex image places metadata legally: {:?}",
r.scan().anomalies()
);
}
// Clear the flex_bg bit (0x200, bit 9) in the `incompat` word.
bytes[1024 + 0x60 + 1] &= !0x02;
let mut r = Reader::open_with(
std::io::Cursor::new(&bytes),
&OpenOptions::new().policy(ReadPolicy::Lenient),
)
.unwrap();
assert!(!r.feature().has_flex_bg());
let report = r.scan();
let outside: Vec<&Anomaly> = report
.anomalies()
.iter()
.filter(|a| a.detail.contains("outside the group"))
.collect();
assert!(
!outside.is_empty(),
"the packed metadata is now out of place: {report:?}"
);
// Group 1's block bitmap sits in group 0, so it is named against group 1 and is a
// structural bitmap anomaly.
assert!(
outside.iter().any(|a| {
a.location.group == Some(1)
&& a.category == Category::Bitmap
&& a.severity == Severity::Structural
}),
"group 1's bitmap is flagged as outside its group: {report:?}"
);
assert!(report.has_fatal(ReadPolicy::Strict));
}
#[test]
fn scan_collects_multiple_anomalies_and_renders_a_table() {
let time = Timestamp::from_secs(1_700_000_000);
let src = TreeBuilder::new().file(
b"/hostname".to_vec(),
b"ferrosys\n".to_vec(),
Metadata::new(0o644, time),
);
let bytes = format(src, 64 * MIB, opts()).unwrap().into_bytes();
// Corrupt two independent checksum-covered regions: a superblock field and a
// byte of the root inode. A fail-fast read stops at the first; the scan
// reports both.
let root_off = {
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
r.group_descriptor(0).unwrap().inode_table as usize * 4096 + 256
};
let mut corrupt = bytes.clone();
corrupt[1024 + 0x30] ^= 0xff; // s_wtime, covered by the superblock checksum
corrupt[root_off] ^= 0xff; // a byte of the root inode
let mut r = Reader::open(std::io::Cursor::new(&corrupt)).unwrap();
let report = r.scan();
// Both corruptions are reported, each an integrity anomaly located to its
// object; the scan collected rather than stopping at the first.
assert!(
report
.anomalies()
.iter()
.any(|a| a.category == Category::Superblock && a.severity == Severity::Integrity)
);
assert!(
report
.anomalies()
.iter()
.any(|a| a.category == Category::Inode
&& a.severity == Severity::Integrity
&& a.location.inode == Some(2))
);
assert!(report.anomalies().len() >= 2);
assert_eq!(report.worst_severity(), Some(Severity::Integrity));
// The scan rejects nothing; a strict policy applied to the report would.
assert!(report.has_fatal(ReadPolicy::Strict));
assert!(!report.has_fatal(ReadPolicy::Lenient));
// The table has a header and one row per anomaly, carrying the located inode.
let table = report.to_report().to_table();
let mut lines = table.lines();
let header = lines.next().unwrap();
assert!(header.starts_with("SEVERITY"));
assert!(
header.contains("CATEGORY") && header.contains("LOCATION") && header.contains("DETAIL")
);
assert_eq!(lines.count(), report.anomalies().len());
assert!(table.contains("integrity"));
assert!(table.contains("inode 2"));
}
#[test]
fn scan_files_an_out_of_range_extent_against_the_extent_tree() {
// An extent leaf that names a block outside the filesystem is the canonical
// extent corruption. The anomaly must be filed against the extent tree that
// named the block, located to the inode and the bad block — not the superblock,
// which is intact.
let time = Timestamp::from_secs(1_700_000_000);
let src =
TreeBuilder::new().file(b"/f".to_vec(), vec![0x42; 4096], Metadata::new(0o644, time));
let bytes = format(src, 64 * MIB, opts()).unwrap().into_bytes();
let (ino, inode_off) = {
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let (n, _) = r.lookup(b"/f").expect("lookup /f");
let isize = r.superblock().inode_size as usize;
let table = r.group_descriptor(0).unwrap().inode_table as usize;
(n, table * 4096 + (n as usize - 1) * isize)
};
// ee_start_lo sits at inode offset 0x3c: i_block (0x28), past the twelve-byte
// extent header and the entry's first eight bytes.
let bogus = 0x00ff_ffffu32; // far past a 64 MiB, 4 KiB-block filesystem
let mut corrupt = bytes.clone();
corrupt[inode_off + 0x3c..inode_off + 0x40].copy_from_slice(&bogus.to_le_bytes());
let mut r = Reader::open(std::io::Cursor::new(&corrupt)).unwrap();
let report = r.scan();
let extent = report
.anomalies()
.iter()
.find(|a| a.category == Category::ExtentTree)
.expect("an out-of-range extent is filed against the extent tree");
assert_eq!(extent.location.inode, Some(ino));
assert_eq!(extent.location.block, Some(u64::from(bogus)));
assert_eq!(extent.severity, Severity::Structural);
// The stray block is not blamed on the superblock, which the scan finds intact.
assert!(
!report
.anomalies()
.iter()
.any(|a| a.category == Category::Superblock),
"the out-of-range block was mis-filed against the superblock"
);
}
#[test]
fn scan_files_an_out_of_range_bitmap_block_against_the_bitmap() {
// A group descriptor naming a block bitmap outside the filesystem is a bitmap
// fault, not a superblock one: the out-of-range block reference is filed against
// the bitmap subsystem the descriptor named it for, located to the group and the
// stray block. Reverting the scan site to the bare projection files it under the
// superblock, which is intact.
let bytes = format(TreeBuilder::new(), 64 * MIB, opts())
.unwrap()
.into_bytes();
// Group 0's descriptor sits at the start of the descriptor table (block 1 for a
// 4 KiB-block filesystem with first_data_block 0); its bg_block_bitmap_lo is the
// descriptor's first field.
let desc_off = 4096;
let bogus = 0x00ff_ffffu32; // far past a 64 MiB, 4 KiB-block filesystem
let mut corrupt = bytes.clone();
corrupt[desc_off..desc_off + 4].copy_from_slice(&bogus.to_le_bytes());
let mut r = Reader::open(std::io::Cursor::new(&corrupt)).unwrap();
let report = r.scan();
let bitmap = report
.anomalies()
.iter()
.find(|a| a.category == Category::Bitmap && a.location.block == Some(u64::from(bogus)))
.expect("an out-of-range bitmap block is filed against the bitmap");
assert_eq!(bitmap.location.group, Some(0));
assert_eq!(bitmap.severity, Severity::Structural);
// The stray block is not blamed on the superblock. The corrupt descriptor still
// fails its own checksum — that is a group-descriptor anomaly, correctly typed.
assert!(
!report
.anomalies()
.iter()
.any(|a| a.category == Category::Superblock),
"the out-of-range bitmap block was mis-filed against the superblock"
);
}
#[test]
fn scan_files_an_out_of_range_xattr_block_against_the_attribute_block() {
// An inode whose external-attribute pointer names a block outside the filesystem
// is an attribute-block fault, not a superblock one. Reverting the scan site to
// the bare projection files it under the superblock, which is intact.
let time = Timestamp::from_secs(1_700_000_000);
let src = TreeBuilder::new().file(b"/f".to_vec(), Vec::new(), Metadata::new(0o644, time));
let bytes = format(src, 64 * MIB, opts()).unwrap().into_bytes();
let (ino, inode_off) = {
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let (n, _) = r.lookup(b"/f").expect("lookup /f");
let isize = r.superblock().inode_size as usize;
let table = r.group_descriptor(0).unwrap().inode_table as usize;
(n, table * 4096 + (n as usize - 1) * isize)
};
// i_file_acl_lo sits at inode offset 0x68; the high half (0x76) stays zero, so the
// pointer is the low word alone.
let bogus = 0x00ff_ffffu32;
let mut corrupt = bytes.clone();
corrupt[inode_off + 0x68..inode_off + 0x6c].copy_from_slice(&bogus.to_le_bytes());
let mut r = Reader::open(std::io::Cursor::new(&corrupt)).unwrap();
let report = r.scan();
let xattr = report
.anomalies()
.iter()
.find(|a| a.category == Category::Xattr && a.location.block == Some(u64::from(bogus)))
.expect("an out-of-range xattr block is filed against the attribute block");
assert_eq!(xattr.location.inode, Some(ino));
assert!(
!report
.anomalies()
.iter()
.any(|a| a.category == Category::Superblock),
"the out-of-range xattr block was mis-filed against the superblock"
);
}
#[test]
fn scan_collects_both_bitmap_faults_in_one_group() {
// A group whose block bitmap and inode bitmap both fail their checksum yields two
// anomalies, not one: scan collects every fault within an object rather than
// stopping at the first.
let bytes = format(TreeBuilder::new(), 64 * MIB, opts())
.unwrap()
.into_bytes();
let (block_bmp, inode_bmp) = {
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let d = r.group_descriptor(0).unwrap();
(d.block_bitmap, d.inode_bitmap)
};
// A byte of each bitmap, inside the region the descriptor's checksum covers.
let mut corrupt = bytes.clone();
corrupt[block_bmp as usize * 4096] ^= 0xff;
corrupt[inode_bmp as usize * 4096] ^= 0xff;
let mut r = Reader::open(std::io::Cursor::new(&corrupt)).unwrap();
let report = r.scan();
let bitmap_faults = report
.anomalies()
.iter()
.filter(|a| a.category == Category::Bitmap)
.count();
assert_eq!(
bitmap_faults,
2,
"both bitmaps of the group fault, so both are reported, got {:?}",
report.anomalies()
);
}
#[test]
fn scan_collects_multiple_faults_in_one_directory() {
// A directory with two corrupt blocks yields two anomalies, not one: scan
// collects every fault within the directory rather than stopping at the first.
let time = Timestamp::from_secs(1_700_000_000);
let mut src = TreeBuilder::new().directory(b"/d".to_vec(), Metadata::new(0o755, time));
// Enough entries that the directory spans several blocks, so two hold names.
for i in 0..300u32 {
src = src.file(
format!("/d/file-{i:04}").into_bytes(),
Vec::new(),
Metadata::new(0o644, time),
);
}
let bytes = format(src, 64 * MIB, opts()).unwrap().into_bytes();
// The directory's first physical block (its blocks are one contiguous run) and
// how many it spans. Logical block 0 is the index root; 1 and 2 hold entries.
let (first, nblocks) = {
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let (_, dir) = r.lookup(b"/d").unwrap();
let leaves = r.extent_leaves(&dir).unwrap();
(leaves[0].start, dir.size / 4096)
};
assert!(nblocks >= 3, "the directory must span several blocks");
// A byte inside two entry blocks, within the region each tail checksum covers.
let mut corrupt = bytes.clone();
corrupt[(first + 1) as usize * 4096 + 40] ^= 0xff;
corrupt[(first + 2) as usize * 4096 + 40] ^= 0xff;
let mut r = Reader::open(std::io::Cursor::new(&corrupt)).unwrap();
let report = r.scan();
let dir_faults = report
.anomalies()
.iter()
.filter(|a| a.category == Category::Directory)
.count();
assert_eq!(
dir_faults,
2,
"both corrupt directory blocks are reported, got {:?}",
report.anomalies()
);
}
#[test]
fn extent_tail_offset_follows_the_declared_capacity() {
use crate::extent::node_capacity;
const BS: usize = 4096;
// A node declaring capacity `eh_max`, its magic and other fields irrelevant to
// where the tail sits.
let node = |eh_max: usize| {
let mut n = vec![0u8; BS];
put_u16(&mut n, 4, eh_max as u16); // eh_max at offset 4
n
};
// A maximal node: the tail sits at the block-filling offset, exactly where the
// old block-size derivation put it, so real images are unaffected.
assert_eq!(
extent_tail_offset(&node(node_capacity(BS)), BS),
tail_offset(BS)
);
// A node one entry short of full: the tail moves back one entry slot, matching a
// foreign tool that wrote a node that does not fill its block.
assert_eq!(
extent_tail_offset(&node(node_capacity(BS) - 1), BS),
tail_offset(BS) - EXTENT_ENTRY_SIZE
);
// A capacity too large for the block is malformed: fall back to the block-filling
// offset, which stays in bounds rather than indexing past the node.
assert_eq!(extent_tail_offset(&node(0xffff), BS), tail_offset(BS));
}
#[test]
fn an_anomaly_projects_into_the_shared_finding_frame() {
// ext keeps its typed taxonomy; what crosses into the shared frame is the severity,
// the family, ext's own subsystem word, the coordinates under ext's own names, and
// — where the anomaly names a block — the byte offset that block sits at.
let a = Anomaly {
severity: Severity::Structural,
category: Category::ExtentTree,
location: Location {
block: Some(99),
group: None,
inode: Some(12),
},
detail: "bad node".to_string(),
};
let f = a.to_finding(4096);
assert_eq!(f.severity, Severity::Structural);
assert_eq!(f.family, Family::Ext);
assert_eq!(f.category, "extent tree");
assert_eq!(f.offset, Some(99 * 4096));
assert_eq!(
f.coordinates,
vec![
(std::borrow::Cow::Borrowed("inode"), 12),
(std::borrow::Cow::Borrowed("block"), 99),
]
);
// An anomaly located only by a group or an inode number has no offset: where each
// sits depends on the layout, not on the number.
let no_block = Anomaly {
severity: Severity::Cosmetic,
category: Category::Journal,
location: Location::default(),
detail: "x".to_string(),
};
let f = no_block.to_finding(4096);
assert_eq!(f.offset, None);
assert!(f.coordinates.is_empty());
}
#[test]
fn a_block_number_no_image_could_hold_names_no_offset() {
// A block number out of an untrusted image is whatever the bytes said, so the
// conversion to an offset is checked: a product that does not fit is a block that
// is not in the image, and the finding says where it is by number instead.
let a = Anomaly {
severity: Severity::Structural,
category: Category::Directory,
location: Location {
block: Some(u64::MAX),
..Location::default()
},
detail: "out of range".to_string(),
};
let f = a.to_finding(4096);
assert_eq!(f.offset, None);
assert_eq!(
f.coordinates,
vec![(std::borrow::Cow::Borrowed("block"), u64::MAX)]
);
}
/// A report of two anomalies at a 4 KiB block, for the golden document tests.
fn golden_report() -> ScanReport {
ScanReport::new(
vec![
Anomaly {
severity: Severity::Integrity,
category: Category::Inode,
location: Location {
block: Some(40),
group: Some(3),
inode: Some(12),
},
detail: "checksum \"bad\"".to_string(),
},
Anomaly {
severity: Severity::Cosmetic,
category: Category::Journal,
location: Location::default(),
detail: "note".to_string(),
},
],
false,
crate::FindingReport::MAX_FINDINGS,
4096,
)
}
#[test]
fn the_findings_document_is_a_versioned_golden_shape() {
// The emitted JSON is a contract no Rust signature describes: a downstream parser
// depends on it, and `cargo semver-checks` sees nothing when it changes. So the
// whole document is pinned here, byte for byte, as an ext scan renders it — which
// is the projection and the family's own words together, which is what a consumer
// actually reads. A diff in this assertion is a change every consumer's parser
// sees — which is exactly when `schema` must go up and the change must be
// deliberate.
assert_eq!(
golden_report().to_report().to_json(),
"{\"schema\":2,\"clean\":false,\"count\":2,\"truncated\":false,\"findings\":[\
{\"severity\":\"integrity\",\"family\":\"ext\",\"category\":\"inode\",\
\"offset\":163840,\"location\":{\"group\":3,\"inode\":12,\"block\":40},\
\"detail\":\"checksum \\\"bad\\\"\"},\
{\"severity\":\"cosmetic\",\"family\":\"ext\",\"category\":\"journal\",\
\"location\":{},\"detail\":\"note\"}]}"
);
assert_eq!(
golden_report().to_report().to_table(),
"SEVERITY CATEGORY LOCATION DETAIL\n\
integrity inode group 3 inode 12 block 40 checksum \"bad\"\n\
cosmetic journal - note\n"
);
// A clean report is its own shape, and it carries the version too: a consumer that
// only ever sees sound images must still be able to read the field.
let clean = ScanReport::default().to_report();
assert_eq!(
clean.to_json(),
"{\"schema\":2,\"clean\":true,\"count\":0,\"truncated\":false,\"findings\":[]}"
);
assert_eq!(clean.to_table(), "no findings\n");
assert_eq!(crate::finding::FINDINGS_SCHEMA_VERSION, 2);
}
#[test]
fn the_findings_document_projects_to_sarif() {
// A clean report is a well-formed, empty SARIF log: no rules, no results.
assert_eq!(
ScanReport::default().to_report().to_sarif(None),
"{\"$schema\":\"https://json.schemastore.org/sarif-2.1.0.json\",\
\"version\":\"2.1.0\",\"runs\":[{\"tool\":{\"driver\":{\"name\":\"ferrosys\",\
\"rules\":[]}},\"results\":[]}]}"
);
// Two anomalies, two categories: one rule apiece, named for the family as well as
// the subsystem; severity maps to the SARIF level, the coordinates become a logical
// location, the byte offset an address, the image URI a physical location, and the
// typed value rides in `properties`. The detail escapes like any JSON string.
let expected = String::from(
"{\"$schema\":\"https://json.schemastore.org/sarif-2.1.0.json\",\
\"version\":\"2.1.0\",\"runs\":[{\"tool\":{\"driver\":{\"name\":\"ferrosys\",\"rules\":[",
) + "{\"id\":\"ext/inode\",\"name\":\"ext/inode\",\"shortDescription\":{\"text\":\"ext inode finding\"}},"
+ "{\"id\":\"ext/journal\",\"name\":\"ext/journal\",\"shortDescription\":{\"text\":\"ext journal finding\"}}"
+ "]}},\"results\":["
+ "{\"ruleId\":\"ext/inode\",\"level\":\"error\",\"message\":{\"text\":\"checksum \\\"bad\\\"\"},"
+ "\"locations\":[{\"physicalLocation\":{\"artifactLocation\":{\"uri\":\"disk.img\"},"
+ "\"address\":{\"absoluteAddress\":163840}},"
+ "\"logicalLocations\":[{\"fullyQualifiedName\":\"group 3 inode 12 block 40\"}]}],"
+ "\"properties\":{\"severity\":\"integrity\",\"family\":\"ext\",\"category\":\"inode\","
+ "\"offset\":163840,\"group\":3,\"inode\":12,\"block\":40}},"
+ "{\"ruleId\":\"ext/journal\",\"level\":\"note\",\"message\":{\"text\":\"note\"},"
+ "\"locations\":[{\"physicalLocation\":{\"artifactLocation\":{\"uri\":\"disk.img\"}}}],"
+ "\"properties\":{\"severity\":\"cosmetic\",\"family\":\"ext\",\"category\":\"journal\"}}"
+ "]}]}";
assert_eq!(
golden_report().to_report().to_sarif(Some("disk.img")),
expected
);
// Without an artifact URI and with no coordinates, a result carries no `locations`.
let bare = ScanReport::new(
vec![Anomaly {
severity: Severity::Conformance,
category: Category::Superblock,
location: Location::default(),
detail: "x".to_string(),
}],
false,
crate::FindingReport::MAX_FINDINGS,
4096,
);
assert!(bare.to_report().to_sarif(None).contains(
"\"ruleId\":\"ext/superblock\",\"level\":\"warning\",\"message\":{\"text\":\"x\"},\
\"properties\":{\"severity\":\"conformance\",\"family\":\"ext\",\"category\":\"superblock\"}"
));
}
#[test]
fn reader_never_panics_on_mangled_images() {
// The never-panic contract: opening and every read path return errors on
// malformed bytes, never crash. A deterministic smoke test over degenerate
// geometry, truncations, and bit-flips of a valid image; the cargo-fuzz
// target in fuzz/ is the exhaustive version.
let time = Timestamp::from_secs(1_700_000_000);
let m = |mode| Metadata::new(mode, time);
let src = TreeBuilder::new()
.directory(b"/etc".to_vec(), m(0o755))
.file(b"/etc/hostname".to_vec(), b"ferrosys\n".to_vec(), m(0o644))
.file(b"/big".to_vec(), vec![0x42; 40_000], m(0o600));
let image = format(src, 64 * MIB, opts()).unwrap().into_bytes();
fn drive(bytes: &[u8]) {
if let Ok(mut r) = Reader::open(std::io::Cursor::new(bytes)) {
let _ = r.walk();
let _ = r.verify_checksums();
let _ = r.scan();
}
if let Ok(mut r) = Reader::open_with(
std::io::Cursor::new(bytes),
&OpenOptions::new().base(512).policy(ReadPolicy::Lenient),
) {
let _ = r.scan();
}
}
// The pristine image drives clean.
drive(&image);
// Degenerate geometry that would divide by zero or underflow if unguarded:
// an enormous inode count, a first data block past the end, and zeroed
// blocks-per-group and inodes-per-group. Offsets are into the primary
// superblock at byte 1024.
for (off, fill) in [
(0x00usize, 0xffu8), // s_inodes_count enormous
(0x14, 0xff), // s_first_data_block past the end
(0x20, 0x00), // s_blocks_per_group = 0
(0x28, 0x00), // s_inodes_per_group = 0
] {
let mut mangled = image.clone();
mangled[1024 + off..1024 + off + 4].fill(fill);
drive(&mangled);
}
// Truncations at assorted lengths.
for len in [0usize, 1, 512, 1023, 1024, 1025, 2048, 4096, 8192, 65_536] {
drive(&image[..len.min(image.len())]);
}
// Deterministic single-byte flips across the metadata region, one image
// reused (flip, drive, restore) so the sweep stays cheap.
let mut flip = image.clone();
let span = flip.len().min(64 * 1024);
let mut i = 0usize;
while i < span {
let orig = flip[i];
flip[i] ^= 0xff;
drive(&flip);
flip[i] = orig;
i += 251; // a prime stride so flips land on varied field offsets
}
// A few fixed non-image patterns.
drive(&vec![0x00u8; 4096 * 8]);
drive(&vec![0xffu8; 4096 * 8]);
let ramp: Vec<u8> = (0..8192u32).map(|k| (k % 256) as u8).collect();
drive(&ramp);
}
#[test]
fn walk_bounds_a_directory_cycle() {
// Patch the "b" entry in /a's directory block to point back at the root inode,
// making the tree cyclic. read_dir does not verify directory-block checksums,
// so the walk follows the patched entry; the visited-inode bound descends each
// directory once and returns, where an unbounded walk would recurse to the
// depth cap and error.
let time = Timestamp::from_secs(1_700_000_000);
let m = |mode| Metadata::new(mode, time);
let src = TreeBuilder::new()
.directory(b"/a".to_vec(), m(0o755))
.directory(b"/a/b".to_vec(), m(0o755));
let mut bytes = format(src, 64 * MIB, opts()).unwrap().into_bytes();
// The physical byte offset of /a's single directory block.
let (block_off, block_size) = {
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let root = r.inode(2).unwrap();
let a_ino = r
.read_dir(&root)
.unwrap()
.into_iter()
.find(|e| e.name == b"a")
.unwrap()
.inode;
let a_inode = r.inode(a_ino).unwrap();
let bs = 1024usize << r.superblock().log_block_size;
let start = r.extent_leaves(&a_inode).unwrap()[0].start;
(start as usize * bs, bs)
};
// Repoint the "b" directory entry at the root inode (2).
let mut off = block_off;
let end = block_off + block_size;
loop {
let rec_len = u16::from_le_bytes([bytes[off + 4], bytes[off + 5]]) as usize;
assert!(
rec_len >= 8 && off + rec_len <= end,
"walked a valid dir block"
);
let name_len = bytes[off + 6] as usize;
if name_len == 1 && bytes[off + 8] == b'b' {
bytes[off..off + 4].copy_from_slice(&2u32.to_le_bytes());
break;
}
off += rec_len;
}
// The walk terminates (an unbounded one would return the depth-cap error) and
// yields each directory once; /a/b now resolves to the root and is not
// descended again.
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let tree = r.walk().unwrap();
let paths: Vec<&[u8]> = tree.iter().map(|e| e.path.as_slice()).collect();
assert!(paths.contains(&b"/a".as_slice()));
assert!(paths.contains(&b"/a/b".as_slice()));
assert_eq!(
paths.iter().filter(|p| **p == b"/a").count(),
1,
"the cycle did not multiply entries"
);
}
#[test]
fn the_walk_names_the_inode_each_path_points_at() {
// Two names for one file and two files with identical contents are different
// filesystems, and only the inode number tells them apart: the inodes themselves
// are byte-identical in the second case. A consumer reconstructing hard links
// works from the number, so the walk yields it.
let time = Timestamp::from_secs(1_700_000_000);
let m = Metadata::new(0o644, time);
let src = TreeBuilder::new()
.file(b"/original".to_vec(), b"shared".to_vec(), m)
.hardlink(b"/link".to_vec(), b"/original".to_vec(), m)
.file(b"/twin".to_vec(), b"shared".to_vec(), m);
let image = format(src, 64 * MIB, opts()).unwrap();
let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
let by_path: std::collections::BTreeMap<Vec<u8>, u32> = r
.walk()
.unwrap()
.into_iter()
.map(|e| (e.path, e.number))
.collect();
assert_eq!(
by_path[&b"/link".to_vec()],
by_path[&b"/original".to_vec()],
"a hard link is the same inode under a second name"
);
assert_ne!(
by_path[&b"/twin".to_vec()],
by_path[&b"/original".to_vec()],
"identical contents are not the same file"
);
// The entry's inode is the one its number names, so a consumer need not fetch it
// again to act on what the walk handed back.
let entry = r
.walk()
.unwrap()
.into_iter()
.find(|e| e.path == b"/original")
.expect("the file is in the tree");
assert_eq!(r.inode(entry.number).unwrap(), entry.inode);
assert_eq!(entry.inode.links_count, 2, "the link is counted");
}
#[test]
fn walk_descends_a_tree_nested_past_any_recursion_cap() {
// A legitimate acyclic tree can nest arbitrarily deep, and the walk must reach
// the bottom of it. The descent is an explicit stack, not recursion, so depth is
// bounded by the image rather than a fixed frame count.
let time = Timestamp::from_secs(1_700_000_000);
let depth = 200usize;
let mut src = TreeBuilder::new();
let mut path = Vec::new();
for i in 0..depth {
path.extend_from_slice(format!("/d{i:03}").as_bytes());
src = src.directory(path.clone(), Metadata::new(0o755, time));
}
// A file at the very bottom, so the deepest level carries a name to find.
let mut leaf = path.clone();
leaf.extend_from_slice(b"/leaf");
src = src.file(leaf.clone(), b"deep\n".to_vec(), Metadata::new(0o644, time));
let bytes = format(src, 64 * MIB, opts()).unwrap().into_bytes();
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let paths: Vec<Vec<u8>> = r
.walk()
.expect("a deep tree walks in full")
.into_iter()
.map(|e| e.path)
.collect();
assert!(
paths.contains(&leaf),
"the entry {depth} levels down was not reached"
);
// The whole chain is present exactly once: the `depth` nested directories plus
// the leaf, counted apart from the other names an image carries (`/lost+found`).
let in_chain = paths.iter().filter(|p| p.starts_with(b"/d000")).count();
assert_eq!(
in_chain,
depth + 1,
"every directory in the chain and the leaf appears exactly once"
);
// The chain is present at every level, in depth order.
for i in 0..depth {
let mut prefix = Vec::new();
for j in 0..=i {
prefix.extend_from_slice(format!("/d{j:03}").as_bytes());
}
assert!(
paths.contains(&prefix),
"level {i} is missing from the walk"
);
}
}
/// A merged-`/usr` tree, as every current Linux distribution ships: the directories
/// live under `/usr` and the classic paths are symbolic links into it.
fn merged_usr() -> TreeBuilder {
let t = Timestamp::from_secs(1_700_000_000);
let m = |mode| Metadata::new(mode, t);
TreeBuilder::new()
.directory(b"/usr".to_vec(), m(0o755))
.directory(b"/usr/lib".to_vec(), m(0o755))
.directory(b"/usr/lib/modules".to_vec(), m(0o755))
.file(
b"/usr/lib/modules/vmlinuz".to_vec(),
b"not really a kernel".to_vec(),
m(0o644),
)
.directory(b"/etc".to_vec(), m(0o755))
.file(
b"/etc/fstab".to_vec(),
b"/ ext4 defaults\n".to_vec(),
m(0o644),
)
.symlink(b"/lib".to_vec(), b"usr/lib".to_vec(), m(0o777))
.symlink(
b"/absolute".to_vec(),
b"/usr/lib/modules".to_vec(),
m(0o777),
)
// The other shape a distribution's tree has: a relative target that ascends out
// of the directory holding the link before it descends again.
.symlink(b"/usr/lib64".to_vec(), b"../lib".to_vec(), m(0o777))
// A cycle, and a link to nowhere.
.symlink(b"/loop_a".to_vec(), b"loop_b".to_vec(), m(0o777))
.symlink(b"/loop_b".to_vec(), b"loop_a".to_vec(), m(0o777))
.symlink(b"/dangling".to_vec(), b"nowhere".to_vec(), m(0o777))
}
#[test]
fn lookup_follows_a_symlink_into_merged_usr() {
let image = format(merged_usr(), 64 * MIB, opts()).unwrap();
let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
// The path that matters: /lib/modules exists only through the link.
let (through_link, _) = r.lookup(b"/lib/modules").expect("/lib/modules resolves");
let (direct, _) = r.lookup(b"/usr/lib/modules").expect("/usr/lib/modules");
assert_eq!(through_link, direct, "the link reaches the same inode");
// And the file under it reads.
let (_, inode) = r.lookup(b"/lib/modules/vmlinuz").unwrap();
assert_eq!(r.read_data(&inode).unwrap(), b"not really a kernel");
// An absolute target restarts at this filesystem's root, not the host's.
let (abs, _) = r.lookup(b"/absolute").expect("/absolute resolves");
assert_eq!(abs, direct);
// A leading slash is optional, and `.` and empty components are skipped.
assert_eq!(r.lookup(b"lib/modules").unwrap().0, direct);
assert_eq!(r.lookup(b"//lib/./modules/").unwrap().0, direct);
// The root is the root.
assert_eq!(r.lookup(b"/").unwrap().0, 2);
}
#[test]
fn a_parent_component_ascends_whether_a_caller_wrote_it_or_a_link_stored_it() {
let image = format(merged_usr(), 64 * MIB, opts()).unwrap();
let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
let (direct, _) = r.lookup(b"/usr/lib/modules").expect("/usr/lib/modules");
for path in [
// Written by the caller.
&b"/usr/lib/../lib/modules"[..],
b"/etc/../usr/lib/modules",
// Stored in the image, which is the case a distribution's `/usr/lib64` is: the
// target ascends out of `/usr` and comes back down into it.
b"/usr/lib64/modules",
// And an ascent out of an expansion rather than out of what the caller wrote —
// the link is reached through another link.
b"/lib/modules/../modules",
] {
assert_eq!(
r.lookup(path).map(|(number, _)| number),
Ok(direct),
"{}",
String::from_utf8_lossy(path)
);
}
// At the root there is nothing to ascend to, so a run of them stays there rather
// than naming anything outside the image — however many a caller writes, and
// whether the resolution started there or climbed back to it.
for path in [&b"/.."[..], b"/../..", b"..", b"/usr/lib/../../../.."] {
assert_eq!(
r.lookup(path).map(|(number, _)| number),
Ok(ROOT_INO),
"{}",
String::from_utf8_lossy(path)
);
}
assert_eq!(r.lookup(b"/../etc/fstab").map(|(n, _)| n), {
r.lookup(b"/etc/fstab").map(|(n, _)| n)
});
// An ascent is a component like any other, so one below a file is the same refusal
// any other component below one gets.
assert!(matches!(
r.lookup(b"/etc/fstab/.."),
Err(ReadError::NotADirectory { .. })
));
}
#[test]
fn lookup_no_follow_stops_at_the_link() {
let image = format(merged_usr(), 64 * MIB, opts()).unwrap();
let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
let (_, link) = r.lookup_no_follow(b"/lib").expect("/lib as a link");
assert_eq!(link.mode & 0o170000, 0o120000, "it is a symlink");
assert_eq!(r.read_symlink(&link).unwrap(), b"usr/lib");
// Following, the same path is the directory it points at.
let (_, dir) = r.lookup(b"/lib").expect("/lib followed");
assert_eq!(dir.mode & 0o170000, 0o040000, "it is a directory");
// A link in a non-final component is followed either way — a path cannot
// continue through a link without going where it points.
assert_eq!(
r.lookup_no_follow(b"/lib/modules").unwrap().0,
r.lookup(b"/usr/lib/modules").unwrap().0
);
}
#[test]
fn lookup_bounds_a_symlink_cycle_and_names_what_is_missing() {
let image = format(merged_usr(), 64 * MIB, opts()).unwrap();
let mut r = Reader::open(std::io::Cursor::new(image.as_bytes())).unwrap();
// A cycle terminates as a loop rather than running forever.
assert!(matches!(
r.lookup(b"/loop_a"),
Err(ReadError::SymlinkLoop { .. })
));
// A link to nothing, and a name that is not there, are both "not found".
assert!(matches!(
r.lookup(b"/dangling"),
Err(ReadError::NotFound { .. })
));
assert!(matches!(
r.lookup(b"/nope"),
Err(ReadError::NotFound { .. })
));
// Walking *through* a non-directory is not the same as not finding it.
assert!(matches!(
r.lookup(b"/etc/fstab/deeper"),
Err(ReadError::NotADirectory { .. })
));
// The link itself is still reachable without following it.
assert!(r.lookup_no_follow(b"/loop_a").is_ok());
}
#[test]
fn a_reserved_inode_without_an_extra_area_verifies_on_its_low_half_alone() {
// `mke2fs` leaves the reserved inodes at `i_extra_isize = 0`, so they store only
// `l_i_checksum_lo`. Reproduce that here — set inode 1's extra area to zero and
// recompute its checksum the way the kernel does — and the verifier must accept
// it. Comparing a full 32-bit computed value against a stored half rejects seven
// healthy inodes on every filesystem `mke2fs` ever wrote.
let image = format(TreeBuilder::new(), 64 * MIB, opts()).unwrap();
let mut bytes = image.into_bytes();
let (table, inode_size, csum) = {
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let table = r.group_descriptor(0).unwrap().inode_table as usize;
let csum = r.checksummer();
(table, r.superblock().inode_size as usize, csum)
};
let off = table * 4096; // inode 1 is the first entry
// Zero the extra area, so the inode declares none and carries no high half.
bytes[off + 0x80..off + inode_size].fill(0);
put_u16(&mut bytes[off..], Inode::CHECKSUM_LO_OFFSET, 0);
// Stamped through the same recipe the writer and the reader use, so what this
// fixture asserts is the reader's handling of a missing high half rather than this
// test's own transcription of the fold.
let c = recipe::inode(&csum, 1, 0, &bytes[off..off + inode_size], false);
put_u16(&mut bytes[off..], Inode::CHECKSUM_LO_OFFSET, c as u16);
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
assert_eq!(
r.inode(1).unwrap().extra_isize,
0,
"the inode declares no extra area"
);
let report = r.scan();
assert!(
report.is_clean(),
"an inode storing only its low checksum half was rejected: {:?}",
report.anomalies()
);
r.verify_checksums()
.expect("and the strict verifier accepts it too");
}
#[test]
fn a_field_this_crate_does_not_model_does_not_break_the_inode_checksum() {
// The kernel writes `l_i_version` (0x24) on every inode update, and this crate
// does not model it. A checksum recomputed from a re-serialized `Inode` would
// zero it and reject every inode of any filesystem that has ever been mounted.
// Verifying against the inode's own bytes is what makes that a non-event.
let image = format(TreeBuilder::new(), 64 * MIB, opts()).unwrap();
let mut bytes = image.into_bytes();
let (table, inode_size, csum) = {
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let table = r.group_descriptor(0).unwrap().inode_table as usize;
let csum = r.checksummer();
(table, r.superblock().inode_size as usize, csum)
};
// Inode 2, the root.
let off = table * 4096 + inode_size;
crate::ondisk::put_u32(&mut bytes[off..], 0x24, 0xdead_beef); // l_i_version
put_u16(&mut bytes[off..], Inode::CHECKSUM_LO_OFFSET, 0);
put_u16(&mut bytes[off..], Inode::CHECKSUM_HI_OFFSET, 0);
let c = recipe::inode(&csum, 2, 0, &bytes[off..off + inode_size], true);
put_u16(&mut bytes[off..], Inode::CHECKSUM_LO_OFFSET, c as u16);
put_u16(
&mut bytes[off..],
Inode::CHECKSUM_HI_OFFSET,
(c >> 16) as u16,
);
let mut r = Reader::open(std::io::Cursor::new(&bytes)).unwrap();
let report = r.scan();
assert!(
report.is_clean(),
"an inode carrying a field this crate does not model was rejected: {:?}",
report.anomalies()
);
}
}