summaryrefslogtreecommitdiffstats
path: root/sys/security/mac/mac_syscalls.c
blob: 6d3f124bf19ea990a006a4a2b3ec7e1c8111a9f8 (plain)
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
/*-
 * Copyright (c) 1999, 2000, 2001, 2002 Robert N. M. Watson
 * Copyright (c) 2001 Ilmar S. Habibulin
 * Copyright (c) 2001, 2002 Networks Associates Technology, Inc.
 * All rights reserved.
 *
 * This software was developed by Robert Watson and Ilmar Habibulin for the
 * TrustedBSD Project.
 *
 * This software was developed for the FreeBSD Project in part by NAI Labs,
 * the Security Research Division of Network Associates, Inc. under
 * DARPA/SPAWAR contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA
 * CHATS research program.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The names of the authors may not be used to endorse or promote
 *    products derived from this software without specific prior written
 *    permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */
/*
 * Developed by the TrustedBSD Project.
 *
 * Framework for extensible kernel access control.  Kernel and userland
 * interface to the framework, policy registration and composition.
 */

#include "opt_mac.h"
#include <sys/param.h>
#include <sys/extattr.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/sx.h>
#include <sys/mac.h>
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/sysproto.h>
#include <sys/sysent.h>
#include <sys/vnode.h>
#include <sys/mount.h>
#include <sys/file.h>
#include <sys/namei.h>
#include <sys/socket.h>
#include <sys/pipe.h>
#include <sys/socketvar.h>
#include <sys/sx.h>
#include <sys/sysctl.h>

#include <vm/vm.h>
#include <vm/pmap.h>
#include <vm/vm_map.h>
#include <vm/vm_object.h>

#include <sys/mac_policy.h>

#include <fs/devfs/devfs.h>

#include <net/bpf.h>
#include <net/bpfdesc.h>
#include <net/if.h>
#include <net/if_var.h>

#include <netinet/in.h>
#include <netinet/ip_var.h>

#ifdef MAC

SYSCTL_DECL(_security);

SYSCTL_NODE(_security, OID_AUTO, mac, CTLFLAG_RW, 0,
    "TrustedBSD MAC policy controls");
SYSCTL_NODE(_security_mac, OID_AUTO, debug, CTLFLAG_RW, 0,
    "TrustedBSD MAC debug info");

static int	mac_debug_label_fallback = 0;
SYSCTL_INT(_security_mac_debug, OID_AUTO, label_fallback, CTLFLAG_RW,
    &mac_debug_label_fallback, 0, "Filesystems should fall back to fs label"
    "when label is corrupted.");
TUNABLE_INT("security.mac.debug_label_fallback",
    &mac_debug_label_fallback);

#ifndef MAC_MAX_POLICIES
#define	MAC_MAX_POLICIES	8
#endif
#if MAC_MAX_POLICIES > 32
#error "MAC_MAX_POLICIES too large"
#endif
static unsigned int mac_max_policies = MAC_MAX_POLICIES;
static unsigned int mac_policy_offsets_free = (1 << MAC_MAX_POLICIES) - 1;
SYSCTL_UINT(_security_mac, OID_AUTO, max_policies, CTLFLAG_RD,
    &mac_max_policies, 0, "");

static int	mac_late = 0;

static int	mac_enforce_fs = 1;
SYSCTL_INT(_security_mac, OID_AUTO, enforce_fs, CTLFLAG_RW,
    &mac_enforce_fs, 0, "Enforce MAC policy on file system objects");
TUNABLE_INT("security.mac.enforce_fs", &mac_enforce_fs);

static int	mac_enforce_network = 1;
SYSCTL_INT(_security_mac, OID_AUTO, enforce_network, CTLFLAG_RW,
    &mac_enforce_network, 0, "Enforce MAC policy on network packets");
TUNABLE_INT("security.mac.enforce_network", &mac_enforce_network);

static int	mac_enforce_process = 1;
SYSCTL_INT(_security_mac, OID_AUTO, enforce_process, CTLFLAG_RW,
    &mac_enforce_process, 0, "Enforce MAC policy on inter-process operations");
TUNABLE_INT("security.mac.enforce_process", &mac_enforce_process);

static int	mac_enforce_socket = 1;
SYSCTL_INT(_security_mac, OID_AUTO, enforce_socket, CTLFLAG_RW,
    &mac_enforce_socket, 0, "Enforce MAC policy on socket operations");
TUNABLE_INT("security.mac.enforce_socket", &mac_enforce_socket);

static int	mac_enforce_pipe = 1;
SYSCTL_INT(_security_mac, OID_AUTO, enforce_pipe, CTLFLAG_RW,
    &mac_enforce_pipe, 0, "Enforce MAC policy on pipe operations");

static int	mac_label_size = sizeof(struct mac);
SYSCTL_INT(_security_mac, OID_AUTO, label_size, CTLFLAG_RD,
    &mac_label_size, 0, "Pre-compiled MAC label size");

static int	mac_cache_fslabel_in_vnode = 1;
SYSCTL_INT(_security_mac, OID_AUTO, cache_fslabel_in_vnode, CTLFLAG_RW,
    &mac_cache_fslabel_in_vnode, 0, "Cache mount fslabel in vnode");
TUNABLE_INT("security.mac.cache_fslabel_in_vnode",
    &mac_cache_fslabel_in_vnode);

static int	mac_vnode_label_cache_hits = 0;
SYSCTL_INT(_security_mac, OID_AUTO, vnode_label_cache_hits, CTLFLAG_RD,
    &mac_vnode_label_cache_hits, 0, "Cache hits on vnode labels");
static int	mac_vnode_label_cache_misses = 0;
SYSCTL_INT(_security_mac, OID_AUTO, vnode_label_cache_misses, CTLFLAG_RD,
    &mac_vnode_label_cache_misses, 0, "Cache misses on vnode labels");
static int	mac_mmap_revocation_via_cow = 1;
SYSCTL_INT(_security_mac, OID_AUTO, mmap_revocation_via_cow, CTLFLAG_RW,
    &mac_mmap_revocation_via_cow, 0, "Revoke mmap access to files via "
    "copy-on-write semantics, or by removing all write access");

static unsigned int nmacmbufs, nmaccreds, nmacifnets, nmacbpfdescs,
    nmacsockets, nmacmounts, nmactemp, nmacvnodes, nmacdevfsdirents,
    nmacipqs, nmacpipes;
SYSCTL_UINT(_security_mac_debug, OID_AUTO, mbufs, CTLFLAG_RD,
    &nmacmbufs, 0, "number of mbufs in use");
SYSCTL_UINT(_security_mac_debug, OID_AUTO, creds, CTLFLAG_RD,
    &nmaccreds, 0, "number of ucreds in use");
SYSCTL_UINT(_security_mac_debug, OID_AUTO, ifnets, CTLFLAG_RD,
    &nmacifnets, 0, "number of ifnets in use");
SYSCTL_UINT(_security_mac_debug, OID_AUTO, ipqs, CTLFLAG_RD,
    &nmacipqs, 0, "number of ipqs in use");
SYSCTL_UINT(_security_mac_debug, OID_AUTO, bpfdescs, CTLFLAG_RD,
    &nmacbpfdescs, 0, "number of bpfdescs in use");
SYSCTL_UINT(_security_mac_debug, OID_AUTO, sockets, CTLFLAG_RD,
    &nmacsockets, 0, "number of sockets in use");
SYSCTL_UINT(_security_mac_debug, OID_AUTO, pipes, CTLFLAG_RD,
    &nmacpipes, 0, "number of pipes in use");
SYSCTL_UINT(_security_mac_debug, OID_AUTO, mounts, CTLFLAG_RD,
    &nmacmounts, 0, "number of mounts in use");
SYSCTL_UINT(_security_mac_debug, OID_AUTO, temp, CTLFLAG_RD,
    &nmactemp, 0, "number of temporary labels in use");
SYSCTL_UINT(_security_mac_debug, OID_AUTO, vnodes, CTLFLAG_RD,
    &nmacvnodes, 0, "number of vnodes in use");
SYSCTL_UINT(_security_mac_debug, OID_AUTO, devfsdirents, CTLFLAG_RD,
    &nmacdevfsdirents, 0, "number of devfs dirents inuse");

static int	error_select(int error1, int error2);
static int	mac_externalize(struct label *label, struct mac *mac);
static int	mac_policy_register(struct mac_policy_conf *mpc);
static int	mac_policy_unregister(struct mac_policy_conf *mpc);

static int	mac_stdcreatevnode_ea(struct vnode *vp);
static void	mac_cred_mmapped_drop_perms(struct thread *td,
		    struct ucred *cred);
static void	mac_cred_mmapped_drop_perms_recurse(struct thread *td,
		    struct ucred *cred, struct vm_map *map);

MALLOC_DEFINE(M_MACOPVEC, "macopvec", "MAC policy operation vector");
MALLOC_DEFINE(M_MACPIPELABEL, "macpipelabel", "MAC labels for pipes");

/*
 * mac_policy_list_lock protects the consistency of 'mac_policy_list',
 * the linked list of attached policy modules.  Read-only consumers of
 * the list must acquire a shared lock for the duration of their use;
 * writers must acquire an exclusive lock.  Note that for compound
 * operations, locks should be held for the entire compound operation,
 * and that this is not yet done for relabel requests.
 */
static struct mtx mac_policy_list_lock;
static LIST_HEAD(, mac_policy_conf) mac_policy_list;
static int mac_policy_list_busy;
#define	MAC_POLICY_LIST_LOCKINIT()	mtx_init(&mac_policy_list_lock,	\
	"mac_policy_list_lock", NULL, MTX_DEF);
#define	MAC_POLICY_LIST_LOCK()	mtx_lock(&mac_policy_list_lock);
#define	MAC_POLICY_LIST_UNLOCK()	mtx_unlock(&mac_policy_list_lock);

#define	MAC_POLICY_LIST_BUSY() do {					\
	MAC_POLICY_LIST_LOCK();						\
	mac_policy_list_busy++;						\
	MAC_POLICY_LIST_UNLOCK();					\
} while (0)

#define	MAC_POLICY_LIST_UNBUSY() do {					\
	MAC_POLICY_LIST_LOCK();						\
	mac_policy_list_busy--;						\
	if (mac_policy_list_busy < 0)					\
		panic("Extra mac_policy_list_busy--");			\
	MAC_POLICY_LIST_UNLOCK();					\
} while (0)

/*
 * MAC_CHECK performs the designated check by walking the policy
 * module list and checking with each as to how it feels about the
 * request.  Note that it returns its value via 'error' in the scope
 * of the caller.
 */
#define	MAC_CHECK(check, args...) do {					\
	struct mac_policy_conf *mpc;					\
									\
	error = 0;							\
	MAC_POLICY_LIST_BUSY();						\
	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {			\
		if (mpc->mpc_ops->mpo_ ## check != NULL)		\
			error = error_select(				\
			    mpc->mpc_ops->mpo_ ## check (args),		\
			    error);					\
	}								\
	MAC_POLICY_LIST_UNBUSY();					\
} while (0)

/*
 * MAC_BOOLEAN performs the designated boolean composition by walking
 * the module list, invoking each instance of the operation, and
 * combining the results using the passed C operator.  Note that it
 * returns its value via 'result' in the scope of the caller, which
 * should be initialized by the caller in a meaningful way to get
 * a meaningful result.
 */
#define	MAC_BOOLEAN(operation, composition, args...) do {		\
	struct mac_policy_conf *mpc;					\
									\
	MAC_POLICY_LIST_BUSY();						\
	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {			\
		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
			result = result composition			\
			    mpc->mpc_ops->mpo_ ## operation (args);	\
	}								\
	MAC_POLICY_LIST_UNBUSY();					\
} while (0)

/*
 * MAC_PERFORM performs the designated operation by walking the policy
 * module list and invoking that operation for each policy.
 */
#define	MAC_PERFORM(operation, args...) do {				\
	struct mac_policy_conf *mpc;					\
									\
	MAC_POLICY_LIST_BUSY();						\
	LIST_FOREACH(mpc, &mac_policy_list, mpc_list) {			\
		if (mpc->mpc_ops->mpo_ ## operation != NULL)		\
			mpc->mpc_ops->mpo_ ## operation (args);		\
	}								\
	MAC_POLICY_LIST_UNBUSY();					\
} while (0)

/*
 * Initialize the MAC subsystem, including appropriate SMP locks.
 */
static void
mac_init(void)
{

	LIST_INIT(&mac_policy_list);
	MAC_POLICY_LIST_LOCKINIT();
}

/*
 * For the purposes of modules that want to know if they were loaded
 * "early", set the mac_late flag once we've processed modules either
 * linked into the kernel, or loaded before the kernel startup.
 */
static void
mac_late_init(void)
{

	mac_late = 1;
}

/*
 * Allow MAC policy modules to register during boot, etc.
 */
int
mac_policy_modevent(module_t mod, int type, void *data)
{
	struct mac_policy_conf *mpc;
	int error;

	error = 0;
	mpc = (struct mac_policy_conf *) data;

	switch (type) {
	case MOD_LOAD:
		if (mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_NOTLATE &&
		    mac_late) {
			printf("mac_policy_modevent: can't load %s policy "
			    "after booting\n", mpc->mpc_name);
			error = EBUSY;
			break;
		}
		error = mac_policy_register(mpc);
		break;
	case MOD_UNLOAD:
		/* Don't unregister the module if it was never registered. */
		if ((mpc->mpc_runtime_flags & MPC_RUNTIME_FLAG_REGISTERED)
		    != 0)
			error = mac_policy_unregister(mpc);
		else
			error = 0;
		break;
	default:
		break;
	}

	return (error);
}

static int
mac_policy_register(struct mac_policy_conf *mpc)
{
	struct mac_policy_conf *tmpc;
	struct mac_policy_ops *ops;
	struct mac_policy_op_entry *mpe;
	int slot;

	MALLOC(mpc->mpc_ops, struct mac_policy_ops *, sizeof(*ops), M_MACOPVEC,
	    M_WAITOK | M_ZERO);
	for (mpe = mpc->mpc_entries; mpe->mpe_constant != MAC_OP_LAST; mpe++) {
		switch (mpe->mpe_constant) {
		case MAC_OP_LAST:
			/*
			 * Doesn't actually happen, but this allows checking
			 * that all enumerated values are handled.
			 */
			break;
		case MAC_DESTROY:
			mpc->mpc_ops->mpo_destroy =
			    mpe->mpe_function;
			break;
		case MAC_INIT:
			mpc->mpc_ops->mpo_init =
			    mpe->mpe_function;
			break;
		case MAC_INIT_BPFDESC:
			mpc->mpc_ops->mpo_init_bpfdesc =
			    mpe->mpe_function;
			break;
		case MAC_INIT_CRED:
			mpc->mpc_ops->mpo_init_cred =
			    mpe->mpe_function;
			break;
		case MAC_INIT_DEVFSDIRENT:
			mpc->mpc_ops->mpo_init_devfsdirent =
			    mpe->mpe_function;
			break;
		case MAC_INIT_IFNET:
			mpc->mpc_ops->mpo_init_ifnet =
			    mpe->mpe_function;
			break;
		case MAC_INIT_IPQ:
			mpc->mpc_ops->mpo_init_ipq =
			    mpe->mpe_function;
			break;
		case MAC_INIT_MBUF:
			mpc->mpc_ops->mpo_init_mbuf =
			    mpe->mpe_function;
			break;
		case MAC_INIT_MOUNT:
			mpc->mpc_ops->mpo_init_mount =
			    mpe->mpe_function;
			break;
		case MAC_INIT_PIPE:
			mpc->mpc_ops->mpo_init_pipe =
			    mpe->mpe_function;
			break;
		case MAC_INIT_SOCKET:
			mpc->mpc_ops->mpo_init_socket =
			    mpe->mpe_function;
			break;
		case MAC_INIT_TEMP:
			mpc->mpc_ops->mpo_init_temp =
			    mpe->mpe_function;
			break;
		case MAC_INIT_VNODE:
			mpc->mpc_ops->mpo_init_vnode =
			    mpe->mpe_function;
			break;
		case MAC_DESTROY_BPFDESC:
			mpc->mpc_ops->mpo_destroy_bpfdesc =
			    mpe->mpe_function;
			break;
		case MAC_DESTROY_CRED:
			mpc->mpc_ops->mpo_destroy_cred =
			    mpe->mpe_function;
			break;
		case MAC_DESTROY_DEVFSDIRENT:
			mpc->mpc_ops->mpo_destroy_devfsdirent =
			    mpe->mpe_function;
			break;
		case MAC_DESTROY_IFNET:
			mpc->mpc_ops->mpo_destroy_ifnet =
			    mpe->mpe_function;
			break;
		case MAC_DESTROY_IPQ:
			mpc->mpc_ops->mpo_destroy_ipq =
			    mpe->mpe_function;
			break;
		case MAC_DESTROY_MBUF:
			mpc->mpc_ops->mpo_destroy_mbuf =
			    mpe->mpe_function;
			break;
		case MAC_DESTROY_MOUNT:
			mpc->mpc_ops->mpo_destroy_mount =
			    mpe->mpe_function;
			break;
		case MAC_DESTROY_PIPE:
			mpc->mpc_ops->mpo_destroy_pipe =
			    mpe->mpe_function;
			break;
		case MAC_DESTROY_SOCKET:
			mpc->mpc_ops->mpo_destroy_socket =
			    mpe->mpe_function;
			break;
		case MAC_DESTROY_TEMP:
			mpc->mpc_ops->mpo_destroy_temp =
			    mpe->mpe_function;
			break;
		case MAC_DESTROY_VNODE:
			mpc->mpc_ops->mpo_destroy_vnode =
			    mpe->mpe_function;
			break;
		case MAC_EXTERNALIZE:
			mpc->mpc_ops->mpo_externalize =
			    mpe->mpe_function;
			break;
		case MAC_INTERNALIZE:
			mpc->mpc_ops->mpo_internalize =
			    mpe->mpe_function;
			break;
		case MAC_CREATE_DEVFS_DEVICE:
			mpc->mpc_ops->mpo_create_devfs_device =
			    mpe->mpe_function;
			break;
		case MAC_CREATE_DEVFS_DIRECTORY:
			mpc->mpc_ops->mpo_create_devfs_directory =
			    mpe->mpe_function;
			break;
		case MAC_CREATE_DEVFS_VNODE:
			mpc->mpc_ops->mpo_create_devfs_vnode =
			    mpe->mpe_function;
			break;
		case MAC_STDCREATEVNODE_EA:
			mpc->mpc_ops->mpo_stdcreatevnode_ea =
			    mpe->mpe_function;
			break;
		case MAC_CREATE_VNODE:
			mpc->mpc_ops->mpo_create_vnode =
			    mpe->mpe_function;
			break;
		case MAC_CREATE_MOUNT:
			mpc->mpc_ops->mpo_create_mount =
			    mpe->mpe_function;
			break;
		case MAC_CREATE_ROOT_MOUNT:
			mpc->mpc_ops->mpo_create_root_mount =
			    mpe->mpe_function;
			break;
		case MAC_RELABEL_VNODE:
			mpc->mpc_ops->mpo_relabel_vnode =
			    mpe->mpe_function;
			break;
		case MAC_UPDATE_DEVFSDIRENT:
			mpc->mpc_ops->mpo_update_devfsdirent =
			    mpe->mpe_function;
			break;
		case MAC_UPDATE_PROCFSVNODE:
			mpc->mpc_ops->mpo_update_procfsvnode =
			    mpe->mpe_function;
			break;
		case MAC_UPDATE_VNODE_FROM_EXTATTR:
			mpc->mpc_ops->mpo_update_vnode_from_extattr =
			    mpe->mpe_function;
			break;
		case MAC_UPDATE_VNODE_FROM_EXTERNALIZED:
			mpc->mpc_ops->mpo_update_vnode_from_externalized =
			    mpe->mpe_function;
			break;
		case MAC_UPDATE_VNODE_FROM_MOUNT:
			mpc->mpc_ops->mpo_update_vnode_from_mount =
			    mpe->mpe_function;
			break;
		case MAC_CREATE_MBUF_FROM_SOCKET:
			mpc->mpc_ops->mpo_create_mbuf_from_socket =
			    mpe->mpe_function;
			break;
		case MAC_CREATE_PIPE:
			mpc->mpc_ops->mpo_create_pipe =
			    mpe->mpe_function;
			break;
		case MAC_CREATE_SOCKET:
			mpc->mpc_ops->mpo_create_socket =
			    mpe->mpe_function;
			break;
		case MAC_CREATE_SOCKET_FROM_SOCKET:
			mpc->mpc_ops->mpo_create_socket_from_socket =
			    mpe->mpe_function;
			break;
		case MAC_RELABEL_PIPE:
			mpc->mpc_ops->mpo_relabel_pipe =
			    mpe->mpe_function;
			break;
		case MAC_RELABEL_SOCKET:
			mpc->mpc_ops->mpo_relabel_socket =
			    mpe->mpe_function;
			break;
		case MAC_SET_SOCKET_PEER_FROM_MBUF:
			mpc->mpc_ops->mpo_set_socket_peer_from_mbuf =
			    mpe->mpe_function;
			break;
		case MAC_SET_SOCKET_PEER_FROM_SOCKET:
			mpc->mpc_ops->mpo_set_socket_peer_from_socket =
			    mpe->mpe_function;
			break;
		case MAC_CREATE_BPFDESC:
			mpc->mpc_ops->mpo_create_bpfdesc =
			    mpe->mpe_function;
			break;
		case MAC_CREATE_DATAGRAM_FROM_IPQ:
			mpc->mpc_ops->mpo_create_datagram_from_ipq =
			    mpe->mpe_function;
			break;
		case MAC_CREATE_FRAGMENT:
			mpc->mpc_ops->mpo_create_fragment =
			    mpe->mpe_function;
			break;
		case MAC_CREATE_IFNET:
			mpc->mpc_ops->mpo_create_ifnet =
			    mpe->mpe_function;
			break;
		case MAC_CREATE_IPQ:
			mpc->mpc_ops->mpo_create_ipq =
			    mpe->mpe_function;
			break;
		case MAC_CREATE_MBUF_FROM_MBUF:
			mpc->mpc_ops->mpo_create_mbuf_from_mbuf =
			    mpe->mpe_function;
			break;
		case MAC_CREATE_MBUF_LINKLAYER:
			mpc->mpc_ops->mpo_create_mbuf_linklayer =
			    mpe->mpe_function;
			break;
		case MAC_CREATE_MBUF_FROM_BPFDESC:
			mpc->mpc_ops->mpo_create_mbuf_from_bpfdesc =
			    mpe->mpe_function;
			break;
		case MAC_CREATE_MBUF_FROM_IFNET:
			mpc->mpc_ops->mpo_create_mbuf_from_ifnet =
			    mpe->mpe_function;
			break;
		case MAC_CREATE_MBUF_MULTICAST_ENCAP:
			mpc->mpc_ops->mpo_create_mbuf_multicast_encap =
			    mpe->mpe_function;
			break;
		case MAC_CREATE_MBUF_NETLAYER:
			mpc->mpc_ops->mpo_create_mbuf_netlayer =
			    mpe->mpe_function;
			break;
		case MAC_FRAGMENT_MATCH:
			mpc->mpc_ops->mpo_fragment_match =
			    mpe->mpe_function;
			break;
		case MAC_RELABEL_IFNET:
			mpc->mpc_ops->mpo_relabel_ifnet =
			    mpe->mpe_function;
			break;
		case MAC_UPDATE_IPQ:
			mpc->mpc_ops->mpo_update_ipq =
			    mpe->mpe_function;
			break;
		case MAC_CREATE_CRED:
			mpc->mpc_ops->mpo_create_cred =
			    mpe->mpe_function;
			break;
		case MAC_EXECVE_TRANSITION:
			mpc->mpc_ops->mpo_execve_transition =
			    mpe->mpe_function;
			break;
		case MAC_EXECVE_WILL_TRANSITION:
			mpc->mpc_ops->mpo_execve_will_transition =
			    mpe->mpe_function;
			break;
		case MAC_CREATE_PROC0:
			mpc->mpc_ops->mpo_create_proc0 = mpe->mpe_function;
			break;
		case MAC_CREATE_PROC1:
			mpc->mpc_ops->mpo_create_proc1 = mpe->mpe_function;
			break;
		case MAC_RELABEL_CRED:
			mpc->mpc_ops->mpo_relabel_cred =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_BPFDESC_RECEIVE:
			mpc->mpc_ops->mpo_check_bpfdesc_receive =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_CRED_RELABEL:
			mpc->mpc_ops->mpo_check_cred_relabel =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_CRED_VISIBLE:
			mpc->mpc_ops->mpo_check_cred_visible =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_IFNET_RELABEL:
			mpc->mpc_ops->mpo_check_ifnet_relabel =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_IFNET_TRANSMIT:
			mpc->mpc_ops->mpo_check_ifnet_transmit =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_MOUNT_STAT:
			mpc->mpc_ops->mpo_check_mount_stat =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_PIPE_IOCTL:
			mpc->mpc_ops->mpo_check_pipe_ioctl =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_PIPE_OP:
			mpc->mpc_ops->mpo_check_pipe_op =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_PIPE_RELABEL:
			mpc->mpc_ops->mpo_check_pipe_relabel =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_PROC_DEBUG:
			mpc->mpc_ops->mpo_check_proc_debug =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_PROC_SCHED:
			mpc->mpc_ops->mpo_check_proc_sched =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_PROC_SIGNAL:
			mpc->mpc_ops->mpo_check_proc_signal =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_SOCKET_BIND:
			mpc->mpc_ops->mpo_check_socket_bind =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_SOCKET_CONNECT:
			mpc->mpc_ops->mpo_check_socket_connect =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_SOCKET_LISTEN:
			mpc->mpc_ops->mpo_check_socket_listen =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_SOCKET_RECEIVE:
			mpc->mpc_ops->mpo_check_socket_receive =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_SOCKET_RELABEL:
			mpc->mpc_ops->mpo_check_socket_relabel =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_SOCKET_VISIBLE:
			mpc->mpc_ops->mpo_check_socket_visible =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_ACCESS:
			mpc->mpc_ops->mpo_check_vnode_access =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_CHDIR:
			mpc->mpc_ops->mpo_check_vnode_chdir =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_CHROOT:
			mpc->mpc_ops->mpo_check_vnode_chroot =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_CREATE:
			mpc->mpc_ops->mpo_check_vnode_create =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_DELETE:
			mpc->mpc_ops->mpo_check_vnode_delete =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_DELETEACL:
			mpc->mpc_ops->mpo_check_vnode_deleteacl =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_EXEC:
			mpc->mpc_ops->mpo_check_vnode_exec =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_GETACL:
			mpc->mpc_ops->mpo_check_vnode_getacl =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_GETEXTATTR:
			mpc->mpc_ops->mpo_check_vnode_getextattr =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_LOOKUP:
			mpc->mpc_ops->mpo_check_vnode_lookup =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_MMAP_PERMS:
			mpc->mpc_ops->mpo_check_vnode_mmap_perms =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_OP:
			mpc->mpc_ops->mpo_check_vnode_op =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_OPEN:
			mpc->mpc_ops->mpo_check_vnode_open =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_READDIR:
			mpc->mpc_ops->mpo_check_vnode_readdir =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_READLINK:
			mpc->mpc_ops->mpo_check_vnode_readlink =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_RELABEL:
			mpc->mpc_ops->mpo_check_vnode_relabel =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_RENAME_FROM:
			mpc->mpc_ops->mpo_check_vnode_rename_from =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_RENAME_TO:
			mpc->mpc_ops->mpo_check_vnode_rename_to =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_REVOKE:
			mpc->mpc_ops->mpo_check_vnode_revoke =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_SETACL:
			mpc->mpc_ops->mpo_check_vnode_setacl =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_SETEXTATTR:
			mpc->mpc_ops->mpo_check_vnode_setextattr =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_SETFLAGS:
			mpc->mpc_ops->mpo_check_vnode_setflags =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_SETMODE:
			mpc->mpc_ops->mpo_check_vnode_setmode =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_SETOWNER:
			mpc->mpc_ops->mpo_check_vnode_setowner =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_SETUTIMES:
			mpc->mpc_ops->mpo_check_vnode_setutimes =
			    mpe->mpe_function;
			break;
		case MAC_CHECK_VNODE_STAT:
			mpc->mpc_ops->mpo_check_vnode_stat =
			    mpe->mpe_function;
			break;
/*
		default:
			printf("MAC policy `%s': unknown operation %d\n",
			    mpc->mpc_name, mpe->mpe_constant);
			return (EINVAL);
*/
		}
	}
	MAC_POLICY_LIST_LOCK();
	if (mac_policy_list_busy > 0) {
		MAC_POLICY_LIST_UNLOCK();
		FREE(mpc->mpc_ops, M_MACOPVEC);
		mpc->mpc_ops = NULL;
		return (EBUSY);
	}
	LIST_FOREACH(tmpc, &mac_policy_list, mpc_list) {
		if (strcmp(tmpc->mpc_name, mpc->mpc_name) == 0) {
			MAC_POLICY_LIST_UNLOCK();
			FREE(mpc->mpc_ops, M_MACOPVEC);
			mpc->mpc_ops = NULL;
			return (EEXIST);
		}
	}
	if (mpc->mpc_field_off != NULL) {
		slot = ffs(mac_policy_offsets_free);
		if (slot == 0) {
			MAC_POLICY_LIST_UNLOCK();
			FREE(mpc->mpc_ops, M_MACOPVEC);
			mpc->mpc_ops = NULL;
			return (ENOMEM);
		}
		slot--;
		mac_policy_offsets_free &= ~(1 << slot);
		*mpc->mpc_field_off = slot;
	}
	mpc->mpc_runtime_flags |= MPC_RUNTIME_FLAG_REGISTERED;
	LIST_INSERT_HEAD(&mac_policy_list, mpc, mpc_list);

	/* Per-policy initialization. */
	if (mpc->mpc_ops->mpo_init != NULL)
		(*(mpc->mpc_ops->mpo_init))(mpc);
	MAC_POLICY_LIST_UNLOCK();

	printf("Security policy loaded: %s (%s)\n", mpc->mpc_fullname,
	    mpc->mpc_name);

	return (0);
}

static int
mac_policy_unregister(struct mac_policy_conf *mpc)
{

#if 0
	/*
	 * Don't allow unloading modules with private data.
	 */
	if (mpc->mpc_field_off != NULL)
		return (EBUSY);
#endif
	if ((mpc->mpc_loadtime_flags & MPC_LOADTIME_FLAG_UNLOADOK) == 0)
		return (EBUSY);
	MAC_POLICY_LIST_LOCK();
	if (mac_policy_list_busy > 0) {
		MAC_POLICY_LIST_UNLOCK();
		return (EBUSY);
	}
	if (mpc->mpc_ops->mpo_destroy != NULL)
		(*(mpc->mpc_ops->mpo_destroy))(mpc);

	LIST_REMOVE(mpc, mpc_list);
	MAC_POLICY_LIST_UNLOCK();

	FREE(mpc->mpc_ops, M_MACOPVEC);
	mpc->mpc_ops = NULL;

	printf("Security policy unload: %s (%s)\n", mpc->mpc_fullname,
	    mpc->mpc_name);

	return (0);
}

/*
 * Define an error value precedence, and given two arguments, selects the
 * value with the higher precedence.
 */
static int
error_select(int error1, int error2)
{

	/* Certain decision-making errors take top priority. */
	if (error1 == EDEADLK || error2 == EDEADLK)
		return (EDEADLK);

	/* Invalid arguments should be reported where possible. */
	if (error1 == EINVAL || error2 == EINVAL)
		return (EINVAL);

	/* Precedence goes to "visibility", with both process and file. */
	if (error1 == ESRCH || error2 == ESRCH)
		return (ESRCH);

	if (error1 == ENOENT || error2 == ENOENT)
		return (ENOENT);

	/* Precedence goes to DAC/MAC protections. */
	if (error1 == EACCES || error2 == EACCES)
		return (EACCES);

	/* Precedence goes to privilege. */
	if (error1 == EPERM || error2 == EPERM)
		return (EPERM);

	/* Precedence goes to error over success; otherwise, arbitrary. */
	if (error1 != 0)
		return (error1);
	return (error2);
}

void
mac_update_devfsdirent(struct devfs_dirent *de, struct vnode *vp)
{

	MAC_PERFORM(update_devfsdirent, de, &de->de_label, vp, &vp->v_label);
}

void
mac_update_procfsvnode(struct vnode *vp, struct ucred *cred)
{

	MAC_PERFORM(update_procfsvnode, vp, &vp->v_label, cred);
}

/*
 * Support callout for policies that manage their own externalization
 * using extended attributes.
 */
static int
mac_update_vnode_from_extattr(struct vnode *vp, struct mount *mp)
{
	int error;

	MAC_CHECK(update_vnode_from_extattr, vp, &vp->v_label, mp,
	    &mp->mnt_fslabel);

	return (error);
}

/*
 * Given an externalized mac label, internalize it and stamp it on a
 * vnode.
 */
static int
mac_update_vnode_from_externalized(struct vnode *vp, struct mac *extmac)
{
	int error;

	MAC_CHECK(update_vnode_from_externalized, vp, &vp->v_label, extmac);

	return (error);
}

/*
 * Call out to individual policies to update the label in a vnode from
 * the mountpoint.
 */
void
mac_update_vnode_from_mount(struct vnode *vp, struct mount *mp)
{

	MAC_PERFORM(update_vnode_from_mount, vp, &vp->v_label, mp,
	    &mp->mnt_fslabel);

	if (mac_cache_fslabel_in_vnode)
		vp->v_flag |= VCACHEDLABEL;
}

/*
 * Implementation of VOP_REFRESHLABEL() that relies on extended attributes
 * to store label data.  Can be referenced by filesystems supporting
 * extended attributes.
 */
int
vop_stdrefreshlabel_ea(struct vop_refreshlabel_args *ap)
{
	struct vnode *vp = ap->a_vp;
	struct mac extmac;
	int buflen, error;

	ASSERT_VOP_LOCKED(vp, "vop_stdrefreshlabel_ea");

	/*
	 * Call out to external policies first.  Order doesn't really
	 * matter, as long as failure of one assures failure of all.
	 */
	error = mac_update_vnode_from_extattr(vp, vp->v_mount);
	if (error)
		return (error);

	buflen = sizeof(extmac);
	error = vn_extattr_get(vp, IO_NODELOCKED,
	    FREEBSD_MAC_EXTATTR_NAMESPACE, FREEBSD_MAC_EXTATTR_NAME, &buflen,
	    (char *)&extmac, curthread);
	switch (error) {
	case 0:
		/* Got it */
		break;

	case ENOATTR:
		/*
		 * Use the label from the mount point.
		 */
		mac_update_vnode_from_mount(vp, vp->v_mount);
		return (0);

	case EOPNOTSUPP:
	default:
		/* Fail horribly. */
		return (error);
	}

	if (buflen != sizeof(extmac))
		error = EPERM;		/* Fail very closed. */
	if (error == 0)
		error = mac_update_vnode_from_externalized(vp, &extmac);
	if (error == 0)
		vp->v_flag |= VCACHEDLABEL;
	else {
		struct vattr va;

		printf("Corrupted label on %s",
		    vp->v_mount->mnt_stat.f_mntonname);
		if (VOP_GETATTR(vp, &va, curthread->td_ucred, curthread) == 0)
			printf(" inum %ld", va.va_fileid);
		if (mac_debug_label_fallback) {
			printf(", falling back.\n");
			mac_update_vnode_from_mount(vp, vp->v_mount);
			error = 0;
		} else {
			printf(".\n");
			error = EPERM;
		}
	}

	return (error);
}

/*
 * Make sure the vnode label is up-to-date.  If EOPNOTSUPP, then we handle
 * the labeling activity outselves.  Filesystems should be careful not
 * to change their minds regarding whether they support vop_refreshlabel()
 * for a vnode or not.  Don't cache the vnode here, allow the file
 * system code to determine if it's safe to cache.  If we update from
 * the mount, don't cache since a change to the mount label should affect
 * all vnodes.
 */
static int
vn_refreshlabel(struct vnode *vp, struct ucred *cred)
{
	int error;

	ASSERT_VOP_LOCKED(vp, "vn_refreshlabel");

	if (vp->v_mount == NULL) {
/*
		Eventually, we probably want to special-case refreshing
		of deadfs vnodes, and if there's a lock-free race somewhere,
		that case might be handled here.

		mac_update_vnode_deadfs(vp);
		return (0);
 */
		/* printf("vn_refreshlabel: null v_mount\n"); */
		if (vp->v_tag != VT_NON)
			printf(
			    "vn_refreshlabel: null v_mount with non-VT_NON\n");
		return (EBADF);
	}

	if (vp->v_flag & VCACHEDLABEL) {
		mac_vnode_label_cache_hits++;
		return (0);
	} else
		mac_vnode_label_cache_misses++;

	if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0) {
		mac_update_vnode_from_mount(vp, vp->v_mount);
		return (0);
	}

	error = VOP_REFRESHLABEL(vp, cred, curthread);
	switch (error) {
	case EOPNOTSUPP:
		/*
		 * If labels are not supported on this vnode, fall back to
		 * the label in the mount and propagate it to the vnode.
		 * There should probably be some sort of policy/flag/decision
		 * about doing this.
		 */
		mac_update_vnode_from_mount(vp, vp->v_mount);
		error = 0;
	default:
		return (error);
	}
}

/*
 * Helper function for file systems using the vop_std*_ea() calls.  This
 * function must be called after EA service is available for the vnode,
 * but before it's hooked up to the namespace so that the node persists
 * if there's a crash, or before it can be accessed.  On successful
 * commit of the label to disk (etc), do cache the label.
 */
int
vop_stdcreatevnode_ea(struct vnode *dvp, struct vnode *tvp, struct ucred *cred)
{
	struct mac extmac;
	int error;

	if ((dvp->v_mount->mnt_flag & MNT_MULTILABEL) == 0) {
		mac_update_vnode_from_mount(tvp, tvp->v_mount);
	} else {
		error = vn_refreshlabel(dvp, cred);
		if (error)
			return (error);

		/*
		 * Stick the label in the vnode.  Then try to write to
		 * disk.  If we fail, return a failure to abort the
		 * create operation.  Really, this failure shouldn't
		 * happen except in fairly unusual circumstances (out
		 * of disk, etc).
		 */
		mac_create_vnode(cred, dvp, tvp);

		error = mac_stdcreatevnode_ea(tvp);
		if (error)
			return (error);

		/*
		 * XXX: Eventually this will go away and all policies will
		 * directly manage their extended attributes.
		 */
		error = mac_externalize(&tvp->v_label, &extmac);
		if (error)
			return (error);

		error = vn_extattr_set(tvp, IO_NODELOCKED,
		    FREEBSD_MAC_EXTATTR_NAMESPACE, FREEBSD_MAC_EXTATTR_NAME,
		    sizeof(extmac), (char *)&extmac, curthread);
		if (error == 0)
			tvp->v_flag |= VCACHEDLABEL;
		else {
#if 0
			/*
			 * In theory, we could have fall-back behavior here.
			 * It would probably be incorrect.
			 */
#endif
			return (error);
		}
	}

	return (0);
}

void
mac_execve_transition(struct ucred *old, struct ucred *new, struct vnode *vp)
{
	int error;

	ASSERT_VOP_LOCKED(vp, "mac_execve_transition");

	error = vn_refreshlabel(vp, old);
	if (error) {
		printf("mac_execve_transition: vn_refreshlabel returned %d\n",
		    error);
		printf("mac_execve_transition: using old vnode label\n");
	}

	MAC_PERFORM(execve_transition, old, new, vp, &vp->v_label);
}

int
mac_execve_will_transition(struct ucred *old, struct vnode *vp)
{
	int error, result;

	error = vn_refreshlabel(vp, old);
	if (error)
		return (error);

	result = 0;
	MAC_BOOLEAN(execve_will_transition, ||, old, vp, &vp->v_label);

	return (result);
}

static void
mac_init_label(struct label *label)
{

	bzero(label, sizeof(*label));
	label->l_flags = MAC_FLAG_INITIALIZED;
}

static void
mac_init_structmac(struct mac *mac)
{

	bzero(mac, sizeof(*mac));
	mac->m_macflags = MAC_FLAG_INITIALIZED;
}

static void
mac_destroy_label(struct label *label)
{

	KASSERT(label->l_flags & MAC_FLAG_INITIALIZED,
	    ("destroying uninitialized label"));

	bzero(label, sizeof(*label));
	/* implicit: label->l_flags &= ~MAC_FLAG_INITIALIZED; */
}

int
mac_init_mbuf(struct mbuf *m, int how)
{
	KASSERT(m->m_flags & M_PKTHDR, ("mac_init_mbuf on non-header mbuf"));

	/* "how" is one of M_(TRY|DONT)WAIT */
	mac_init_label(&m->m_pkthdr.label);
	MAC_PERFORM(init_mbuf, m, how, &m->m_pkthdr.label);
	atomic_add_int(&nmacmbufs, 1);
	return (0);
}

void
mac_destroy_mbuf(struct mbuf *m)
{

	MAC_PERFORM(destroy_mbuf, m, &m->m_pkthdr.label);
	mac_destroy_label(&m->m_pkthdr.label);
	atomic_subtract_int(&nmacmbufs, 1);
}

void
mac_init_cred(struct ucred *cr)
{

	mac_init_label(&cr->cr_label);
	MAC_PERFORM(init_cred, cr, &cr->cr_label);
	atomic_add_int(&nmaccreds, 1);
}

void
mac_destroy_cred(struct ucred *cr)
{

	MAC_PERFORM(destroy_cred, cr, &cr->cr_label);
	mac_destroy_label(&cr->cr_label);
	atomic_subtract_int(&nmaccreds, 1);
}

void
mac_init_ifnet(struct ifnet *ifp)
{

	mac_init_label(&ifp->if_label);
	MAC_PERFORM(init_ifnet, ifp, &ifp->if_label);
	atomic_add_int(&nmacifnets, 1);
}

void
mac_destroy_ifnet(struct ifnet *ifp)
{

	MAC_PERFORM(destroy_ifnet, ifp, &ifp->if_label);
	mac_destroy_label(&ifp->if_label);
	atomic_subtract_int(&nmacifnets, 1);
}

void
mac_init_ipq(struct ipq *ipq)
{

	mac_init_label(&ipq->ipq_label);
	MAC_PERFORM(init_ipq, ipq, &ipq->ipq_label);
	atomic_add_int(&nmacipqs, 1);
}

void
mac_destroy_ipq(struct ipq *ipq)
{

	MAC_PERFORM(destroy_ipq, ipq, &ipq->ipq_label);
	mac_destroy_label(&ipq->ipq_label);
	atomic_subtract_int(&nmacipqs, 1);
}

void
mac_init_socket(struct socket *socket)
{

	mac_init_label(&socket->so_label);
	mac_init_label(&socket->so_peerlabel);
	MAC_PERFORM(init_socket, socket, &socket->so_label,
	    &socket->so_peerlabel);
	atomic_add_int(&nmacsockets, 1);
}

void
mac_destroy_socket(struct socket *socket)
{

	MAC_PERFORM(destroy_socket, socket, &socket->so_label,
	    &socket->so_peerlabel);
	mac_destroy_label(&socket->so_label);
	mac_destroy_label(&socket->so_peerlabel);
	atomic_subtract_int(&nmacsockets, 1);
}

void
mac_init_pipe(struct pipe *pipe)
{
	struct label *label;

	label = malloc(sizeof(struct label), M_MACPIPELABEL, M_ZERO|M_WAITOK);
	mac_init_label(label);
	pipe->pipe_label = label;
	pipe->pipe_peer->pipe_label = label;
	MAC_PERFORM(init_pipe, pipe, pipe->pipe_label);
	atomic_add_int(&nmacpipes, 1);
}

void
mac_destroy_pipe(struct pipe *pipe)
{

	MAC_PERFORM(destroy_pipe, pipe, pipe->pipe_label);
	mac_destroy_label(pipe->pipe_label);
	free(pipe->pipe_label, M_MACPIPELABEL);
	atomic_subtract_int(&nmacpipes, 1);
}

void
mac_init_bpfdesc(struct bpf_d *bpf_d)
{

	mac_init_label(&bpf_d->bd_label);
	MAC_PERFORM(init_bpfdesc, bpf_d, &bpf_d->bd_label);
	atomic_add_int(&nmacbpfdescs, 1);
}

void
mac_destroy_bpfdesc(struct bpf_d *bpf_d)
{

	MAC_PERFORM(destroy_bpfdesc, bpf_d, &bpf_d->bd_label);
	mac_destroy_label(&bpf_d->bd_label);
	atomic_subtract_int(&nmacbpfdescs, 1);
}

void
mac_init_mount(struct mount *mp)
{

	mac_init_label(&mp->mnt_mntlabel);
	mac_init_label(&mp->mnt_fslabel);
	MAC_PERFORM(init_mount, mp, &mp->mnt_mntlabel, &mp->mnt_fslabel);
	atomic_add_int(&nmacmounts, 1);
}

void
mac_destroy_mount(struct mount *mp)
{

	MAC_PERFORM(destroy_mount, mp, &mp->mnt_mntlabel, &mp->mnt_fslabel);
	mac_destroy_label(&mp->mnt_fslabel);
	mac_destroy_label(&mp->mnt_mntlabel);
	atomic_subtract_int(&nmacmounts, 1);
}

static void
mac_init_temp(struct label *label)
{

	mac_init_label(label);
	MAC_PERFORM(init_temp, label);
	atomic_add_int(&nmactemp, 1);
}

static void
mac_destroy_temp(struct label *label)
{

	MAC_PERFORM(destroy_temp, label);
	mac_destroy_label(label);
	atomic_subtract_int(&nmactemp, 1);
}

void
mac_init_vnode(struct vnode *vp)
{

	mac_init_label(&vp->v_label);
	MAC_PERFORM(init_vnode, vp, &vp->v_label);
	atomic_add_int(&nmacvnodes, 1);
}

void
mac_destroy_vnode(struct vnode *vp)
{

	MAC_PERFORM(destroy_vnode, vp, &vp->v_label);
	mac_destroy_label(&vp->v_label);
	atomic_subtract_int(&nmacvnodes, 1);
}

void
mac_init_devfsdirent(struct devfs_dirent *de)
{

	mac_init_label(&de->de_label);
	MAC_PERFORM(init_devfsdirent, de, &de->de_label);
	atomic_add_int(&nmacdevfsdirents, 1);
}

void
mac_destroy_devfsdirent(struct devfs_dirent *de)
{

	MAC_PERFORM(destroy_devfsdirent, de, &de->de_label);
	mac_destroy_label(&de->de_label);
	atomic_subtract_int(&nmacdevfsdirents, 1);
}

static int
mac_externalize(struct label *label, struct mac *mac)
{
	int error;

	mac_init_structmac(mac);
	MAC_CHECK(externalize, label, mac);

	return (error);
}

static int
mac_internalize(struct label *label, struct mac *mac)
{
	int error;

	mac_init_temp(label);
	MAC_CHECK(internalize, label, mac);
	if (error)
		mac_destroy_temp(label);

	return (error);
}

/*
 * Initialize MAC label for the first kernel process, from which other
 * kernel processes and threads are spawned.
 */
void
mac_create_proc0(struct ucred *cred)
{

	MAC_PERFORM(create_proc0, cred);
}

/*
 * Initialize MAC label for the first userland process, from which other
 * userland processes and threads are spawned.
 */
void
mac_create_proc1(struct ucred *cred)
{

	MAC_PERFORM(create_proc1, cred);
}

/*
 * When a new process is created, its label must be initialized.  Generally,
 * this involves inheritence from the parent process, modulo possible
 * deltas.  This function allows that processing to take place.
 */
void
mac_create_cred(struct ucred *parent_cred, struct ucred *child_cred)
{

	MAC_PERFORM(create_cred, parent_cred, child_cred);
}

int
mac_check_vnode_access(struct ucred *cred, struct vnode *vp, int flags)
{
	int error;

	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_access");

	if (!mac_enforce_fs)
		return (0);

	error = vn_refreshlabel(vp, cred);
	if (error)
		return (error);

	MAC_CHECK(check_vnode_access, cred, vp, &vp->v_label, flags);
	return (error);
}

int
mac_check_vnode_chdir(struct ucred *cred, struct vnode *dvp)
{
	int error;

	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chdir");

	if (!mac_enforce_fs)
		return (0);

	error = vn_refreshlabel(dvp, cred);
	if (error)
		return (error);

	MAC_CHECK(check_vnode_chdir, cred, dvp, &dvp->v_label);
	return (error);
}

int
mac_check_vnode_chroot(struct ucred *cred, struct vnode *dvp)
{
	int error;

	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_chroot");

	if (!mac_enforce_fs)
		return (0);

	error = vn_refreshlabel(dvp, cred);
	if (error)
		return (error);

	MAC_CHECK(check_vnode_chroot, cred, dvp, &dvp->v_label);
	return (error);
}

int
mac_check_vnode_create(struct ucred *cred, struct vnode *dvp,
    struct componentname *cnp, struct vattr *vap)
{
	int error;

	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_create");

	if (!mac_enforce_fs)
		return (0);

	error = vn_refreshlabel(dvp, cred);
	if (error)
		return (error);

	MAC_CHECK(check_vnode_create, cred, dvp, &dvp->v_label, cnp, vap);
	return (error);
}

int
mac_check_vnode_delete(struct ucred *cred, struct vnode *dvp, struct vnode *vp,
    struct componentname *cnp)
{
	int error;

	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_delete");
	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_delete");

	if (!mac_enforce_fs)
		return (0);

	error = vn_refreshlabel(dvp, cred);
	if (error)
		return (error);
	error = vn_refreshlabel(vp, cred);
	if (error)
		return (error);

	MAC_CHECK(check_vnode_delete, cred, dvp, &dvp->v_label, vp,
	    &vp->v_label, cnp);
	return (error);
}

int
mac_check_vnode_deleteacl(struct ucred *cred, struct vnode *vp,
    acl_type_t type)
{
	int error;

	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_deleteacl");

	if (!mac_enforce_fs)
		return (0);

	error = vn_refreshlabel(vp, cred);
	if (error)
		return (error);

	MAC_CHECK(check_vnode_deleteacl, cred, vp, &vp->v_label, type);
	return (error);
}

int
mac_check_vnode_exec(struct ucred *cred, struct vnode *vp)
{
	int error;

	if (!mac_enforce_process && !mac_enforce_fs)
		return (0);

	error = vn_refreshlabel(vp, cred);
	if (error)
		return (error);
	MAC_CHECK(check_vnode_exec, cred, vp, &vp->v_label);

	return (error);
}

int
mac_check_vnode_getacl(struct ucred *cred, struct vnode *vp, acl_type_t type)
{
	int error;

	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getacl");

	if (!mac_enforce_fs)
		return (0);

	error = vn_refreshlabel(vp, cred);
	if (error)
		return (error);

	MAC_CHECK(check_vnode_getacl, cred, vp, &vp->v_label, type);
	return (error);
}

int
mac_check_vnode_getextattr(struct ucred *cred, struct vnode *vp,
    int attrnamespace, const char *name, struct uio *uio)
{
	int error;

	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_getextattr");

	if (!mac_enforce_fs)
		return (0);

	error = vn_refreshlabel(vp, cred);
	if (error)
		return (error);

	MAC_CHECK(check_vnode_getextattr, cred, vp, &vp->v_label,
	    attrnamespace, name, uio);
	return (error);
}

int
mac_check_vnode_lookup(struct ucred *cred, struct vnode *dvp,
    struct componentname *cnp)
{
	int error;

	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_lookup");

	if (!mac_enforce_fs)
		return (0);

	error = vn_refreshlabel(dvp, cred);
	if (error)
		return (error);

	MAC_CHECK(check_vnode_lookup, cred, dvp, &dvp->v_label, cnp);
	return (error);
}

vm_prot_t
mac_check_vnode_mmap_prot(struct ucred *cred, struct vnode *vp, int newmapping)
{
	vm_prot_t result = VM_PROT_ALL;

	/*
	 * This should be some sort of MAC_BITWISE, maybe :)
	 */
	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_mmap_perms");
	MAC_BOOLEAN(check_vnode_mmap_perms, &, cred, vp, &vp->v_label,
	    newmapping);
	return (result);
}

int
mac_check_vnode_op(struct ucred *cred, struct vnode *vp, int op)
{
	int error;

	if (!mac_enforce_fs)
		return (0);

	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_op");

	error = vn_refreshlabel(vp, cred);
	if (error)
		return (error);

	MAC_CHECK(check_vnode_op, cred, vp, &vp->v_label, op);

	return (error);
}

int
mac_check_vnode_open(struct ucred *cred, struct vnode *vp, mode_t acc_mode)
{
	int error;

	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_open");

	if (!mac_enforce_fs)
		return (0);

	error = vn_refreshlabel(vp, cred);
	if (error)
		return (error);

	MAC_CHECK(check_vnode_open, cred, vp, &vp->v_label, acc_mode);
	return (error);
}

int
mac_check_vnode_readdir(struct ucred *cred, struct vnode *dvp)
{
	int error;

	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_readdir");

	if (!mac_enforce_fs)
		return (0);

	error = vn_refreshlabel(dvp, cred);
	if (error)
		return (error);

	MAC_CHECK(check_vnode_readdir, cred, dvp, &dvp->v_label);
	return (error);
}

int
mac_check_vnode_readlink(struct ucred *cred, struct vnode *vp)
{
	int error;

	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_readlink");

	if (!mac_enforce_fs)
		return (0);

	error = vn_refreshlabel(vp, cred);
	if (error)
		return (error);

	MAC_CHECK(check_vnode_readlink, cred, vp, &vp->v_label);
	return (error);
}

static int
mac_check_vnode_relabel(struct ucred *cred, struct vnode *vp,
    struct label *newlabel)
{
	int error;

	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_relabel");

	error = vn_refreshlabel(vp, cred);
	if (error)
		return (error);

	MAC_CHECK(check_vnode_relabel, cred, vp, &vp->v_label, newlabel);

	return (error);
}

int
mac_check_vnode_rename_from(struct ucred *cred, struct vnode *dvp,
    struct vnode *vp, struct componentname *cnp)
{
	int error;

	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_from");
	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_from");

	if (!mac_enforce_fs)
		return (0);

	error = vn_refreshlabel(dvp, cred);
	if (error)
		return (error);
	error = vn_refreshlabel(vp, cred);
	if (error)
		return (error);

	MAC_CHECK(check_vnode_rename_from, cred, dvp, &dvp->v_label, vp,
	    &vp->v_label, cnp);
	return (error);
}

int
mac_check_vnode_rename_to(struct ucred *cred, struct vnode *dvp,
    struct vnode *vp, int samedir, struct componentname *cnp)
{
	int error;

	ASSERT_VOP_LOCKED(dvp, "mac_check_vnode_rename_to");
	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_rename_to");

	if (!mac_enforce_fs)
		return (0);

	error = vn_refreshlabel(dvp, cred);
	if (error)
		return (error);
	if (vp != NULL) {
		error = vn_refreshlabel(vp, cred);
		if (error)
			return (error);
	}
	MAC_CHECK(check_vnode_rename_to, cred, dvp, &dvp->v_label, vp,
	    vp != NULL ? &vp->v_label : NULL, samedir, cnp);
	return (error);
}

int
mac_check_vnode_revoke(struct ucred *cred, struct vnode *vp)
{
	int error;

	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_revoke");

	if (!mac_enforce_fs)
		return (0);

	error = vn_refreshlabel(vp, cred);
	if (error)
		return (error);

	MAC_CHECK(check_vnode_revoke, cred, vp, &vp->v_label);
	return (error);
}

int
mac_check_vnode_setacl(struct ucred *cred, struct vnode *vp, acl_type_t type,
    struct acl *acl)
{
	int error;

	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setacl");

	if (!mac_enforce_fs)
		return (0);

	error = vn_refreshlabel(vp, cred);
	if (error)
		return (error);

	MAC_CHECK(check_vnode_setacl, cred, vp, &vp->v_label, type, acl);
	return (error);
}

int
mac_check_vnode_setextattr(struct ucred *cred, struct vnode *vp,
    int attrnamespace, const char *name, struct uio *uio)
{
	int error;

	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setextattr");

	if (!mac_enforce_fs)
		return (0);

	error = vn_refreshlabel(vp, cred);
	if (error)
		return (error);

	MAC_CHECK(check_vnode_setextattr, cred, vp, &vp->v_label,
	    attrnamespace, name, uio);
	return (error);
}

int
mac_check_vnode_setflags(struct ucred *cred, struct vnode *vp, u_long flags)
{
	int error;

	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setflags");

	if (!mac_enforce_fs)
		return (0);

	error = vn_refreshlabel(vp, cred);
	if (error)
		return (error);

	MAC_CHECK(check_vnode_setflags, cred, vp, &vp->v_label, flags);
	return (error);
}

int
mac_check_vnode_setmode(struct ucred *cred, struct vnode *vp, mode_t mode)
{
	int error;

	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setmode");

	if (!mac_enforce_fs)
		return (0);

	error = vn_refreshlabel(vp, cred);
	if (error)
		return (error);

	MAC_CHECK(check_vnode_setmode, cred, vp, &vp->v_label, mode);
	return (error);
}

int
mac_check_vnode_setowner(struct ucred *cred, struct vnode *vp, uid_t uid,
    gid_t gid)
{
	int error;

	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setowner");

	if (!mac_enforce_fs)
		return (0);

	error = vn_refreshlabel(vp, cred);
	if (error)
		return (error);

	MAC_CHECK(check_vnode_setowner, cred, vp, &vp->v_label, uid, gid);
	return (error);
}

int
mac_check_vnode_setutimes(struct ucred *cred, struct vnode *vp,
    struct timespec atime, struct timespec mtime)
{
	int error;

	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_setutimes");

	if (!mac_enforce_fs)
		return (0);

	error = vn_refreshlabel(vp, cred);
	if (error)
		return (error);

	MAC_CHECK(check_vnode_setutimes, cred, vp, &vp->v_label, atime,
	    mtime);
	return (error);
}

int
mac_check_vnode_stat(struct ucred *cred, struct vnode *vp)
{
	int error;

	ASSERT_VOP_LOCKED(vp, "mac_check_vnode_stat");

	if (!mac_enforce_fs)
		return (0);

	error = vn_refreshlabel(vp, cred);
	if (error)
		return (error);

	MAC_CHECK(check_vnode_stat, cred, vp, &vp->v_label);
	return (error);
}

/*
 * When relabeling a process, call out to the policies for the maximum
 * permission allowed for each object type we know about in its
 * memory space, and revoke access (in the least surprising ways we
 * know) when necessary.  The process lock is not held here.
 */
static void
mac_cred_mmapped_drop_perms(struct thread *td, struct ucred *cred)
{

	/* XXX freeze all other threads */
	mtx_lock(&Giant);
	mac_cred_mmapped_drop_perms_recurse(td, cred,
	    &td->td_proc->p_vmspace->vm_map);
	mtx_unlock(&Giant);
	/* XXX allow other threads to continue */
}

static __inline const char *
prot2str(vm_prot_t prot)
{

	switch (prot & VM_PROT_ALL) {
	case VM_PROT_READ:
		return ("r--");
	case VM_PROT_READ | VM_PROT_WRITE:
		return ("rw-");
	case VM_PROT_READ | VM_PROT_EXECUTE:
		return ("r-x");
	case VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE:
		return ("rwx");
	case VM_PROT_WRITE:
		return ("-w-");
	case VM_PROT_EXECUTE:
		return ("--x");
	case VM_PROT_WRITE | VM_PROT_EXECUTE:
		return ("-wx");
	default:
		return ("---");
	}
}

static void
mac_cred_mmapped_drop_perms_recurse(struct thread *td, struct ucred *cred,
    struct vm_map *map)
{
	struct vm_map_entry *vme;
	vm_prot_t result, revokeperms;
	vm_object_t object;
	vm_ooffset_t offset;
	struct vnode *vp;

	vm_map_lock_read(map);
	for (vme = map->header.next; vme != &map->header; vme = vme->next) {
		if (vme->eflags & MAP_ENTRY_IS_SUB_MAP) {
			mac_cred_mmapped_drop_perms_recurse(td, cred,
			    vme->object.sub_map);
			continue;
		}
		/*
		 * Skip over entries that obviously are not shared.
		 */
		if (vme->eflags & (MAP_ENTRY_COW | MAP_ENTRY_NOSYNC) ||
		    !vme->max_protection)
			continue;
		/*
		 * Drill down to the deepest backing object.
		 */
		offset = vme->offset;
		object = vme->object.vm_object;
		if (object == NULL)
			continue;
		while (object->backing_object != NULL) {
			object = object->backing_object;
			offset += object->backing_object_offset;
		}
		/*
		 * At the moment, vm_maps and objects aren't considered
		 * by the MAC system, so only things with backing by a
		 * normal object (read: vnodes) are checked.
		 */
		if (object->type != OBJT_VNODE)
			continue;
		vp = (struct vnode *)object->handle;
		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
		result = mac_check_vnode_mmap_prot(cred, vp, 0);
		VOP_UNLOCK(vp, 0, td);
		/*
		 * Find out what maximum protection we may be allowing
		 * now but a policy needs to get removed.
		 */
		revokeperms = vme->max_protection & ~result;
		if (!revokeperms)
			continue;
		printf("pid %d: revoking %s perms from %#lx:%d "
		    "(max %s/cur %s)\n", td->td_proc->p_pid,
		    prot2str(revokeperms), vme->start, vme->end - vme->start,
		    prot2str(vme->max_protection), prot2str(vme->protection));
		vm_map_lock_upgrade(map);
		/*
		 * This is the really simple case: if a map has more
		 * max_protection than is allowed, but it's not being
		 * actually used (that is, the current protection is
		 * still allowed), we can just wipe it out and do
		 * nothing more.
		 */
		if ((vme->protection & revokeperms) == 0) {
			vme->max_protection -= revokeperms;
		} else {
			if (revokeperms & VM_PROT_WRITE) {
				/*
				 * In the more complicated case, flush out all
				 * pending changes to the object then turn it
				 * copy-on-write.
				 */
				vm_object_reference(object);
				vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
				vm_object_page_clean(object,
				    OFF_TO_IDX(offset),
				    OFF_TO_IDX(offset + vme->end - vme->start +
					PAGE_MASK),
				    OBJPC_SYNC);
				VOP_UNLOCK(vp, 0, td);
				vm_object_deallocate(object);
				/*
				 * Why bother if there's no read permissions
				 * anymore?  For the rest, we need to leave
				 * the write permissions on for COW, or
				 * remove them entirely if configured to.
				 */
				if (!mac_mmap_revocation_via_cow) {
					vme->max_protection &= ~VM_PROT_WRITE;
					vme->protection &= ~VM_PROT_WRITE;
				} if ((revokeperms & VM_PROT_READ) == 0)
					vme->eflags |= MAP_ENTRY_COW |
					    MAP_ENTRY_NEEDS_COPY;
			}
			if (revokeperms & VM_PROT_EXECUTE) {
				vme->max_protection &= ~VM_PROT_EXECUTE;
				vme->protection &= ~VM_PROT_EXECUTE;
			}
			if (revokeperms & VM_PROT_READ) {
				vme->max_protection = 0;
				vme->protection = 0;
			}
			pmap_protect(map->pmap, vme->start, vme->end,
			    vme->protection & ~revokeperms);
			vm_map_simplify_entry(map, vme);
		}
		vm_map_lock_downgrade(map);
	}
	vm_map_unlock_read(map);
}

/*
 * When the subject's label changes, it may require revocation of privilege
 * to mapped objects.  This can't be done on-the-fly later with a unified
 * buffer cache.
 */
static void
mac_relabel_cred(struct ucred *cred, struct label *newlabel)
{

	MAC_PERFORM(relabel_cred, cred, newlabel);
	mac_cred_mmapped_drop_perms(curthread, cred);
}

void
mac_relabel_vnode(struct ucred *cred, struct vnode *vp, struct label *newlabel)
{

	MAC_PERFORM(relabel_vnode, cred, vp, &vp->v_label, newlabel);
}

void
mac_create_ifnet(struct ifnet *ifnet)
{

	MAC_PERFORM(create_ifnet, ifnet, &ifnet->if_label);
}

void
mac_create_bpfdesc(struct ucred *cred, struct bpf_d *bpf_d)
{

	MAC_PERFORM(create_bpfdesc, cred, bpf_d, &bpf_d->bd_label);
}

void
mac_create_socket(struct ucred *cred, struct socket *socket)
{

	MAC_PERFORM(create_socket, cred, socket, &socket->so_label);
}

void
mac_create_pipe(struct ucred *cred, struct pipe *pipe)
{

	MAC_PERFORM(create_pipe, cred, pipe, pipe->pipe_label);
}

void
mac_create_socket_from_socket(struct socket *oldsocket,
    struct socket *newsocket)
{

	MAC_PERFORM(create_socket_from_socket, oldsocket, &oldsocket->so_label,
	    newsocket, &newsocket->so_label);
}

static void
mac_relabel_socket(struct ucred *cred, struct socket *socket,
    struct label *newlabel)
{

	MAC_PERFORM(relabel_socket, cred, socket, &socket->so_label, newlabel);
}

static void
mac_relabel_pipe(struct ucred *cred, struct pipe *pipe, struct label *newlabel)
{

	MAC_PERFORM(relabel_pipe, cred, pipe, pipe->pipe_label, newlabel);
}

void
mac_set_socket_peer_from_mbuf(struct mbuf *mbuf, struct socket *socket)
{

	MAC_PERFORM(set_socket_peer_from_mbuf, mbuf, &mbuf->m_pkthdr.label,
	    socket, &socket->so_peerlabel);
}

void
mac_set_socket_peer_from_socket(struct socket *oldsocket,
    struct socket *newsocket)
{

	MAC_PERFORM(set_socket_peer_from_socket, oldsocket,
	    &oldsocket->so_label, newsocket, &newsocket->so_peerlabel);
}

void
mac_create_datagram_from_ipq(struct ipq *ipq, struct mbuf *datagram)
{

	MAC_PERFORM(create_datagram_from_ipq, ipq, &ipq->ipq_label,
	    datagram, &datagram->m_pkthdr.label);
}

void
mac_create_fragment(struct mbuf *datagram, struct mbuf *fragment)
{

	MAC_PERFORM(create_fragment, datagram, &datagram->m_pkthdr.label,
	    fragment, &fragment->m_pkthdr.label);
}

void
mac_create_ipq(struct mbuf *fragment, struct ipq *ipq)
{

	MAC_PERFORM(create_ipq, fragment, &fragment->m_pkthdr.label, ipq,
	    &ipq->ipq_label);
}

void
mac_create_mbuf_from_mbuf(struct mbuf *oldmbuf, struct mbuf *newmbuf)
{

	MAC_PERFORM(create_mbuf_from_mbuf, oldmbuf, &oldmbuf->m_pkthdr.label,
	    newmbuf, &newmbuf->m_pkthdr.label);
}

void
mac_create_mbuf_from_bpfdesc(struct bpf_d *bpf_d, struct mbuf *mbuf)
{

	MAC_PERFORM(create_mbuf_from_bpfdesc, bpf_d, &bpf_d->bd_label, mbuf,
	    &mbuf->m_pkthdr.label);
}

void
mac_create_mbuf_linklayer(struct ifnet *ifnet, struct mbuf *mbuf)
{

	MAC_PERFORM(create_mbuf_linklayer, ifnet, &ifnet->if_label, mbuf,
	    &mbuf->m_pkthdr.label);
}

void
mac_create_mbuf_from_ifnet(struct ifnet *ifnet, struct mbuf *mbuf)
{

	MAC_PERFORM(create_mbuf_from_ifnet, ifnet, &ifnet->if_label, mbuf,
	    &mbuf->m_pkthdr.label);
}

void
mac_create_mbuf_multicast_encap(struct mbuf *oldmbuf, struct ifnet *ifnet,
    struct mbuf *newmbuf)
{

	MAC_PERFORM(create_mbuf_multicast_encap, oldmbuf,
	    &oldmbuf->m_pkthdr.label, ifnet, &ifnet->if_label, newmbuf,
	    &newmbuf->m_pkthdr.label);
}

void
mac_create_mbuf_netlayer(struct mbuf *oldmbuf, struct mbuf *newmbuf)
{

	MAC_PERFORM(create_mbuf_netlayer, oldmbuf, &oldmbuf->m_pkthdr.label,
	    newmbuf, &newmbuf->m_pkthdr.label);
}

int
mac_fragment_match(struct mbuf *fragment, struct ipq *ipq)
{
	int result;

	result = 1;
	MAC_BOOLEAN(fragment_match, &&, fragment, &fragment->m_pkthdr.label,
	    ipq, &ipq->ipq_label);

	return (result);
}

void
mac_update_ipq(struct mbuf *fragment, struct ipq *ipq)
{

	MAC_PERFORM(update_ipq, fragment, &fragment->m_pkthdr.label, ipq,
	    &ipq->ipq_label);
}

void
mac_create_mbuf_from_socket(struct socket *socket, struct mbuf *mbuf)
{

	MAC_PERFORM(create_mbuf_from_socket, socket, &socket->so_label, mbuf,
	    &mbuf->m_pkthdr.label);
}

void
mac_create_mount(struct ucred *cred, struct mount *mp)
{

	MAC_PERFORM(create_mount, cred, mp, &mp->mnt_mntlabel,
	    &mp->mnt_fslabel);
}

void
mac_create_root_mount(struct ucred *cred, struct mount *mp)
{

	MAC_PERFORM(create_root_mount, cred, mp, &mp->mnt_mntlabel,
	    &mp->mnt_fslabel);
}

int
mac_check_bpfdesc_receive(struct bpf_d *bpf_d, struct ifnet *ifnet)
{
	int error;

	if (!mac_enforce_network)
		return (0);

	MAC_CHECK(check_bpfdesc_receive, bpf_d, &bpf_d->bd_label, ifnet,
	    &ifnet->if_label);

	return (error);
}

static int
mac_check_cred_relabel(struct ucred *cred, struct label *newlabel)
{
	int error;

	MAC_CHECK(check_cred_relabel, cred, newlabel);

	return (error);
}

int
mac_check_cred_visible(struct ucred *u1, struct ucred *u2)
{
	int error;

	if (!mac_enforce_process)
		return (0);

	MAC_CHECK(check_cred_visible, u1, u2);

	return (error);
}

int
mac_check_ifnet_transmit(struct ifnet *ifnet, struct mbuf *mbuf)
{
	int error;

	if (!mac_enforce_network)
		return (0);

	KASSERT(mbuf->m_flags & M_PKTHDR, ("packet has no pkthdr"));
	if (!(mbuf->m_pkthdr.label.l_flags & MAC_FLAG_INITIALIZED))
		printf("%s%d: not initialized\n", ifnet->if_name,
		    ifnet->if_unit);

	MAC_CHECK(check_ifnet_transmit, ifnet, &ifnet->if_label, mbuf,
	    &mbuf->m_pkthdr.label);

	return (error);
}

int
mac_check_mount_stat(struct ucred *cred, struct mount *mount)
{
	int error;

	if (!mac_enforce_fs)
		return (0);

	MAC_CHECK(check_mount_stat, cred, mount, &mount->mnt_mntlabel);

	return (error);
}

int
mac_check_pipe_ioctl(struct ucred *cred, struct pipe *pipe, unsigned long cmd,
    void *data)
{
	int error;

	MAC_CHECK(check_pipe_ioctl, cred, pipe, pipe->pipe_label, cmd, data);

	return (error);
}

int
mac_check_pipe_op(struct ucred *cred, struct pipe *pipe, int op)
{
	int error;

	MAC_CHECK(check_pipe_op, cred, pipe, pipe->pipe_label, op);

	return (error);
}

static int
mac_check_pipe_relabel(struct ucred *cred, struct pipe *pipe,
    struct label *newlabel)
{
	int error;

	MAC_CHECK(check_pipe_relabel, cred, pipe, pipe->pipe_label, newlabel);

	return (error);
}

int
mac_check_proc_debug(struct ucred *cred, struct proc *proc)
{
	int error;

	if (!mac_enforce_process)
		return (0);

	MAC_CHECK(check_proc_debug, cred, proc);

	return (error);
}

int
mac_check_proc_sched(struct ucred *cred, struct proc *proc)
{
	int error;

	if (!mac_enforce_process)
		return (0);

	MAC_CHECK(check_proc_sched, cred, proc);

	return (error);
}

int
mac_check_proc_signal(struct ucred *cred, struct proc *proc, int signum)
{
	int error;

	if (!mac_enforce_process)
		return (0);

	MAC_CHECK(check_proc_signal, cred, proc, signum);

	return (error);
}

int
mac_check_socket_bind(struct ucred *ucred, struct socket *socket,
    struct sockaddr *sockaddr)
{
	int error;

	if (!mac_enforce_socket)
		return (0);

	MAC_CHECK(check_socket_bind, ucred, socket, &socket->so_label,
	    sockaddr);

	return (error);
}

int
mac_check_socket_connect(struct ucred *cred, struct socket *socket,
    struct sockaddr *sockaddr)
{
	int error;

	if (!mac_enforce_socket)
		return (0);

	MAC_CHECK(check_socket_connect, cred, socket, &socket->so_label,
	    sockaddr);

	return (error);
}

int
mac_check_socket_listen(struct ucred *cred, struct socket *socket)
{
	int error;

	if (!mac_enforce_socket)
		return (0);

	MAC_CHECK(check_socket_listen, cred, socket, &socket->so_label);
	return (error);
}

int
mac_check_socket_receive(struct socket *socket, struct mbuf *mbuf)
{
	int error;

	if (!mac_enforce_socket)
		return (0);

	MAC_CHECK(check_socket_receive, socket, &socket->so_label, mbuf,
	    &mbuf->m_pkthdr.label);

	return (error);
}

static int
mac_check_socket_relabel(struct ucred *cred, struct socket *socket,
    struct label *newlabel)
{
	int error;

	MAC_CHECK(check_socket_relabel, cred, socket, &socket->so_label,
	    newlabel);

	return (error);
}

int
mac_check_socket_visible(struct ucred *cred, struct socket *socket)
{
	int error;

	if (!mac_enforce_socket)
		return (0);
                
	MAC_CHECK(check_socket_visible, cred, socket, &socket->so_label);
                            
	return (error);
}

int
mac_ioctl_ifnet_get(struct ucred *cred, struct ifreq *ifr,
    struct ifnet *ifnet)
{
	struct mac label;
	int error;

	error = mac_externalize(&ifnet->if_label, &label);
	if (error)
		return (error);

	return (copyout(&label, ifr->ifr_ifru.ifru_data, sizeof(label)));
}

int
mac_ioctl_ifnet_set(struct ucred *cred, struct ifreq *ifr,
    struct ifnet *ifnet)
{
	struct mac newlabel;
	struct label intlabel;
	int error;

	error = copyin(ifr->ifr_ifru.ifru_data, &newlabel, sizeof(newlabel));
	if (error)
		return (error);

	error = mac_internalize(&intlabel, &newlabel);
	if (error)
		return (error);

	/*
	 * XXX: Note that this is a redundant privilege check, since
	 * policies impose this check themselves if required by the
	 * policy.  Eventually, this should go away.
	 */
	error = suser_cred(cred, 0);
	if (error)
		goto out;

	MAC_CHECK(check_ifnet_relabel, cred, ifnet, &ifnet->if_label,
	    &intlabel);
	if (error)
		goto out;

	MAC_PERFORM(relabel_ifnet, cred, ifnet, &ifnet->if_label, &intlabel);

out:
	mac_destroy_temp(&intlabel);
	return (error);
}

void
mac_create_devfs_vnode(struct devfs_dirent *de, struct vnode *vp)
{

	MAC_PERFORM(create_devfs_vnode, de, &de->de_label, vp, &vp->v_label);
}

void
mac_create_devfs_device(dev_t dev, struct devfs_dirent *de)
{

	MAC_PERFORM(create_devfs_device, dev, de, &de->de_label);
}

static int
mac_stdcreatevnode_ea(struct vnode *vp)
{
	int error;

	MAC_CHECK(stdcreatevnode_ea, vp, &vp->v_label);

	return (error);
}

void
mac_create_devfs_directory(char *dirname, int dirnamelen,
    struct devfs_dirent *de)
{

	MAC_PERFORM(create_devfs_directory, dirname, dirnamelen, de,
	    &de->de_label);
}

/*
 * When a new vnode is created, this call will initialize its label.
 */
void
mac_create_vnode(struct ucred *cred, struct vnode *parent,
    struct vnode *child)
{
	int error;

	ASSERT_VOP_LOCKED(parent, "mac_create_vnode");
	ASSERT_VOP_LOCKED(child, "mac_create_vnode");

	error = vn_refreshlabel(parent, cred);
	if (error) {
		printf("mac_create_vnode: vn_refreshlabel returned %d\n",
		    error);
		printf("mac_create_vnode: using old vnode label\n");
	}

	MAC_PERFORM(create_vnode, cred, parent, &parent->v_label, child,
	    &child->v_label);
}

int
mac_setsockopt_label_set(struct ucred *cred, struct socket *so,
    struct mac *extmac)
{
	struct label intlabel;
	int error;

	error = mac_internalize(&intlabel, extmac);
	if (error)
		return (error);

	mac_check_socket_relabel(cred, so, &intlabel);
	if (error) {
		mac_destroy_temp(&intlabel);
		return (error);
	}

	mac_relabel_socket(cred, so, &intlabel);

	mac_destroy_temp(&intlabel);
	return (0);
}

int
mac_pipe_label_set(struct ucred *cred, struct pipe *pipe, struct label *label)
{
	int error;

	error = mac_check_pipe_relabel(cred, pipe, label);
	if (error)
		return (error);

	mac_relabel_pipe(cred, pipe, label);

	return (0);
}

int
mac_getsockopt_label_get(struct ucred *cred, struct socket *so,
    struct mac *extmac)
{

	return (mac_externalize(&so->so_label, extmac));
}

int
mac_getsockopt_peerlabel_get(struct ucred *cred, struct socket *so,
    struct mac *extmac)
{

	return (mac_externalize(&so->so_peerlabel, extmac));
}

/*
 * Implementation of VOP_SETLABEL() that relies on extended attributes
 * to store label data.  Can be referenced by filesystems supporting
 * extended attributes.
 */
int
vop_stdsetlabel_ea(struct vop_setlabel_args *ap)
{
	struct vnode *vp = ap->a_vp;
	struct label *intlabel = ap->a_label;
	struct mac extmac;
	int error;

	ASSERT_VOP_LOCKED(vp, "vop_stdsetlabel_ea");

	/*
	 * XXX: Eventually call out to EA check/set calls here.
	 * Be particularly careful to avoid race conditions,
	 * consistency problems, and stability problems when
	 * dealing with multiple EAs.  In particular, we require
	 * the ability to write multiple EAs on the same file in
	 * a single transaction, which the current EA interface
	 * does not provide.
	 */

	error = mac_externalize(intlabel, &extmac);
	if (error)
		return (error);

	error = vn_extattr_set(vp, IO_NODELOCKED,
	    FREEBSD_MAC_EXTATTR_NAMESPACE, FREEBSD_MAC_EXTATTR_NAME,
	    sizeof(extmac), (char *)&extmac, curthread);
	if (error)
		return (error);

	mac_relabel_vnode(ap->a_cred, vp, intlabel);

	vp->v_flag |= VCACHEDLABEL;

	return (0);
}

static int
vn_setlabel(struct vnode *vp, struct label *intlabel, struct ucred *cred)
{
	int error;

	if (vp->v_mount == NULL) {
		/* printf("vn_setlabel: null v_mount\n"); */
		if (vp->v_tag != VT_NON)
			printf("vn_setlabel: null v_mount with non-VT_NON\n");
		return (EBADF);
	}

	if ((vp->v_mount->mnt_flag & MNT_MULTILABEL) == 0)
		return (EOPNOTSUPP);

	/*
	 * Multi-phase commit.  First check the policies to confirm the
	 * change is OK.  Then commit via the filesystem.  Finally,
	 * update the actual vnode label.  Question: maybe the filesystem
	 * should update the vnode at the end as part of VOP_SETLABEL()?
	 */
	error = mac_check_vnode_relabel(cred, vp, intlabel);
	if (error)
		return (error);

	/*
	 * VADMIN provides the opportunity for the filesystem to make
	 * decisions about who is and is not able to modify labels
	 * and protections on files.  This might not be right.  We can't
	 * assume VOP_SETLABEL() will do it, because we might implement
	 * that as part of vop_stdsetlabel_ea().
	 */
	error = VOP_ACCESS(vp, VADMIN, cred, curthread);
	if (error)
		return (error);

	error = VOP_SETLABEL(vp, intlabel, cred, curthread);
	if (error)
		return (error);

	return (0);
}

/*
 * MPSAFE
 */
int
__mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
{
	struct mac extmac;
	int error;

	error = mac_externalize(&td->td_ucred->cr_label, &extmac);
	if (error == 0)
		error = copyout(&extmac, SCARG(uap, mac_p), sizeof(extmac));

	return (error);
}

/*
 * MPSAFE
 *
 * XXX: Needs to be re-written for proc locking.
 */
int
__mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
{
	struct ucred *newcred, *oldcred;
	struct proc *p;
	struct mac extmac;
	struct label intlabel;
	int error;

	error = copyin(SCARG(uap, mac_p), &extmac, sizeof(extmac));
	if (error)
		return (error);

	error = mac_internalize(&intlabel, &extmac);
	if (error)
		return (error);

	newcred = crget();

	p = td->td_proc;
	PROC_LOCK(p);
	oldcred = p->p_ucred;

	error = mac_check_cred_relabel(oldcred, &intlabel);
	if (error) {
		PROC_UNLOCK(p);
		mac_destroy_temp(&intlabel);
		crfree(newcred);
		return (error);
	}

	setsugid(p);
	crcopy(newcred, oldcred);
	PROC_UNLOCK(p);
	mac_relabel_cred(newcred, &intlabel);

	PROC_LOCK(p);
	p->p_ucred = newcred;
	PROC_UNLOCK(p);
	crfree(oldcred);
	mac_destroy_temp(&intlabel);
	return (0);
}

/*
 * MPSAFE
 */
int
__mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
{
	struct file *fp;
	struct mac extmac;
	struct vnode *vp;
	struct pipe *pipe;
	int error;

	mtx_lock(&Giant);

	error = fget(td, SCARG(uap, fd), &fp);
	if (error)
		goto out;

	switch (fp->f_type) {
	case DTYPE_FIFO:
	case DTYPE_VNODE:
		vp = (struct vnode *)fp->f_data;

		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
		error = vn_refreshlabel(vp, td->td_ucred);
		if (error == 0)
			error = mac_externalize(&vp->v_label, &extmac);
		VOP_UNLOCK(vp, 0, td);
		break;
	case DTYPE_PIPE:
		pipe = (struct pipe *)fp->f_data;
		error = mac_externalize(pipe->pipe_label, &extmac);
		break;
	default:
		error = EINVAL;
	}

	if (error == 0)
		error = copyout(&extmac, SCARG(uap, mac_p), sizeof(extmac));

	fdrop(fp, td);

out:
	mtx_unlock(&Giant);
	return (error);
}

/*
 * MPSAFE
 */
int
__mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
{
	struct nameidata nd;
	struct mac extmac;
	int error;

	mtx_lock(&Giant);
	NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_USERSPACE,
	    SCARG(uap, path_p), td);
	error = namei(&nd);
	if (error)
		goto out;

	error = vn_refreshlabel(nd.ni_vp, td->td_ucred);
	if (error == 0)
		error = mac_externalize(&nd.ni_vp->v_label, &extmac);
	NDFREE(&nd, 0);
	if (error)
		goto out;

	error = copyout(&extmac, SCARG(uap, mac_p), sizeof(extmac));

out:
	mtx_unlock(&Giant);
	return (error);
}

/*
 * MPSAFE
 */
int
__mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
{
	struct file *fp;
	struct mac extmac;
	struct label intlabel;
	struct mount *mp;
	struct vnode *vp;
	struct pipe *pipe;
	int error;

	mtx_lock(&Giant);
	error = fget(td, SCARG(uap, fd), &fp);
	if (error)
		goto out1;

	error = copyin(SCARG(uap, mac_p), &extmac, sizeof(extmac));
	if (error)
		goto out2;

	error = mac_internalize(&intlabel, &extmac);
	if (error)
		goto out2;

	switch (fp->f_type) {
	case DTYPE_FIFO:
	case DTYPE_VNODE:
		vp = (struct vnode *)fp->f_data;
		error = vn_start_write(vp, &mp, V_WAIT | PCATCH);
		if (error != 0)
			break;

		vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td);
		error = vn_setlabel(vp, &intlabel, td->td_ucred);
		VOP_UNLOCK(vp, 0, td);
		vn_finished_write(mp);
		mac_destroy_temp(&intlabel);
		break;
	case DTYPE_PIPE:
		pipe = (struct pipe *)fp->f_data;
		error = mac_pipe_label_set(td->td_ucred, pipe, &intlabel);
		break;
	default:
		error = EINVAL;
	}

out2:
	fdrop(fp, td);
out1:
	mtx_unlock(&Giant);
	return (error);
}

/*
 * MPSAFE
 */
int
__mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
{
	struct nameidata nd;
	struct mac extmac;
	struct label intlabel;
	struct mount *mp;
	int error;

	mtx_lock(&Giant);

	error = copyin(SCARG(uap, mac_p), &extmac, sizeof(extmac));
	if (error)
		goto out;

	error = mac_internalize(&intlabel, &extmac);
	if (error)
		goto out;

	NDINIT(&nd, LOOKUP, LOCKLEAF | FOLLOW, UIO_USERSPACE,
	    SCARG(uap, path_p), td);
	error = namei(&nd);
	if (error)
		goto out2;
	error = vn_start_write(nd.ni_vp, &mp, V_WAIT | PCATCH);
	if (error)
		goto out2;

	error = vn_setlabel(nd.ni_vp, &intlabel, td->td_ucred);

	vn_finished_write(mp);
out2:
	mac_destroy_temp(&intlabel);
	NDFREE(&nd, 0);
out:
	mtx_unlock(&Giant);
	return (error);
}

SYSINIT(mac, SI_SUB_MAC, SI_ORDER_FIRST, mac_init, NULL);
SYSINIT(mac_late, SI_SUB_MAC_LATE, SI_ORDER_FIRST, mac_late_init, NULL);

#else /* !MAC */

int
__mac_get_proc(struct thread *td, struct __mac_get_proc_args *uap)
{

	return (ENOSYS);
}

int
__mac_set_proc(struct thread *td, struct __mac_set_proc_args *uap)
{

	return (ENOSYS);
}

int
__mac_get_fd(struct thread *td, struct __mac_get_fd_args *uap)
{

	return (ENOSYS);
}

int
__mac_get_file(struct thread *td, struct __mac_get_file_args *uap)
{

	return (ENOSYS);
}

int
__mac_set_fd(struct thread *td, struct __mac_set_fd_args *uap)
{

	return (ENOSYS);
}

int
__mac_set_file(struct thread *td, struct __mac_set_file_args *uap)
{

	return (ENOSYS);
}

#endif /* !MAC */
OpenPOWER on IntegriCloud