aboutsummaryrefslogtreecommitdiffstats
path: root/recipes-kernel/linux/linux-yocto-4.1/ccs-tools-yocto_security.patch
blob: 33a69a12fa2b73397b113564948fea14b9562079 (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
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
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
17001
17002
17003
17004
17005
17006
17007
17008
17009
17010
17011
17012
17013
17014
17015
17016
17017
17018
17019
17020
17021
17022
17023
17024
17025
17026
17027
17028
17029
17030
17031
17032
17033
17034
17035
17036
17037
17038
17039
17040
17041
17042
17043
17044
17045
17046
17047
17048
17049
17050
17051
17052
17053
17054
17055
17056
17057
17058
17059
17060
17061
17062
17063
17064
17065
17066
17067
17068
17069
17070
17071
17072
17073
17074
17075
17076
17077
17078
17079
17080
17081
17082
17083
17084
17085
17086
17087
17088
17089
17090
17091
17092
17093
17094
17095
17096
17097
17098
17099
17100
17101
17102
17103
17104
17105
17106
17107
17108
17109
17110
17111
17112
17113
17114
17115
17116
17117
17118
17119
17120
17121
17122
17123
17124
17125
17126
17127
17128
17129
17130
17131
17132
17133
17134
17135
17136
17137
17138
17139
17140
17141
17142
17143
17144
17145
17146
17147
17148
17149
17150
17151
17152
17153
17154
17155
17156
17157
17158
17159
17160
17161
17162
17163
17164
17165
17166
17167
17168
17169
17170
17171
17172
17173
17174
17175
17176
17177
17178
17179
17180
17181
17182
17183
17184
17185
17186
17187
17188
17189
17190
17191
17192
17193
17194
17195
17196
17197
17198
17199
17200
17201
17202
17203
17204
17205
17206
17207
17208
17209
17210
17211
17212
17213
17214
17215
17216
17217
17218
17219
17220
17221
17222
17223
17224
17225
17226
17227
17228
17229
17230
17231
17232
17233
17234
17235
17236
17237
17238
17239
17240
17241
17242
17243
17244
17245
17246
17247
17248
17249
17250
17251
17252
17253
17254
17255
17256
17257
17258
17259
17260
17261
17262
17263
17264
17265
17266
17267
17268
17269
17270
17271
17272
17273
17274
17275
17276
17277
17278
17279
17280
17281
17282
17283
17284
17285
17286
17287
17288
17289
17290
17291
17292
17293
17294
17295
17296
17297
17298
17299
17300
17301
17302
17303
17304
17305
17306
17307
17308
17309
17310
17311
17312
17313
17314
17315
17316
17317
17318
17319
17320
17321
17322
17323
17324
17325
17326
17327
17328
17329
17330
17331
17332
17333
17334
17335
17336
17337
17338
17339
17340
17341
17342
17343
17344
17345
17346
17347
17348
17349
17350
17351
17352
17353
17354
17355
17356
17357
17358
17359
17360
17361
17362
17363
17364
17365
17366
17367
17368
17369
17370
17371
17372
17373
17374
17375
17376
17377
17378
17379
17380
17381
17382
17383
17384
17385
17386
17387
17388
17389
17390
17391
17392
17393
17394
17395
17396
17397
17398
17399
17400
17401
17402
17403
17404
17405
17406
17407
17408
17409
17410
17411
17412
17413
17414
17415
17416
17417
17418
17419
17420
17421
17422
17423
17424
17425
17426
17427
17428
17429
17430
17431
17432
17433
17434
17435
17436
17437
17438
17439
17440
17441
17442
17443
17444
17445
17446
17447
17448
17449
17450
17451
17452
17453
17454
17455
17456
17457
17458
17459
17460
17461
17462
17463
17464
17465
17466
17467
17468
17469
17470
17471
17472
17473
17474
17475
17476
17477
17478
17479
17480
17481
17482
17483
17484
17485
17486
17487
17488
17489
17490
17491
17492
17493
17494
17495
17496
17497
17498
17499
17500
17501
17502
17503
17504
17505
17506
17507
17508
17509
17510
17511
17512
17513
17514
17515
17516
17517
17518
17519
17520
17521
17522
17523
17524
17525
17526
17527
17528
17529
17530
17531
17532
17533
17534
17535
17536
17537
17538
17539
17540
17541
17542
17543
17544
17545
17546
17547
17548
17549
17550
17551
17552
17553
17554
17555
17556
17557
17558
17559
17560
17561
17562
17563
17564
17565
17566
17567
17568
17569
17570
17571
17572
17573
17574
17575
17576
17577
17578
17579
17580
17581
17582
17583
17584
17585
17586
17587
17588
17589
17590
17591
17592
17593
17594
17595
17596
17597
17598
17599
17600
17601
17602
17603
17604
17605
17606
17607
17608
17609
17610
17611
17612
17613
17614
17615
17616
17617
17618
17619
17620
17621
17622
17623
17624
17625
17626
17627
17628
17629
17630
17631
17632
17633
17634
17635
17636
17637
17638
17639
17640
17641
17642
17643
17644
17645
17646
17647
17648
17649
17650
17651
17652
17653
17654
17655
17656
17657
17658
17659
17660
17661
17662
17663
17664
17665
17666
17667
17668
17669
17670
17671
17672
17673
17674
17675
17676
17677
17678
17679
17680
17681
17682
17683
17684
17685
17686
17687
17688
17689
17690
17691
17692
17693
17694
17695
17696
17697
17698
17699
17700
17701
17702
17703
17704
17705
17706
17707
17708
17709
17710
17711
17712
17713
17714
17715
17716
17717
17718
17719
17720
17721
17722
17723
17724
17725
17726
17727
17728
17729
17730
17731
17732
17733
17734
17735
17736
17737
17738
17739
17740
17741
17742
17743
17744
17745
17746
17747
17748
17749
17750
17751
17752
17753
17754
17755
17756
17757
17758
17759
17760
17761
17762
17763
17764
17765
17766
17767
17768
17769
17770
17771
17772
17773
17774
17775
17776
17777
17778
17779
17780
17781
17782
17783
17784
17785
17786
17787
17788
17789
17790
17791
17792
17793
17794
17795
17796
17797
17798
17799
17800
17801
17802
17803
17804
17805
17806
17807
17808
17809
17810
17811
17812
17813
17814
17815
17816
17817
17818
17819
17820
17821
17822
17823
17824
17825
17826
17827
17828
17829
17830
17831
17832
17833
17834
17835
17836
17837
17838
17839
17840
17841
17842
17843
17844
17845
17846
17847
17848
17849
17850
17851
17852
17853
17854
17855
17856
17857
17858
17859
17860
17861
17862
17863
17864
17865
17866
17867
17868
17869
17870
17871
17872
17873
17874
17875
17876
17877
17878
17879
17880
17881
17882
17883
17884
17885
17886
17887
17888
17889
17890
17891
17892
17893
17894
17895
17896
17897
17898
17899
17900
17901
17902
17903
17904
17905
17906
17907
17908
17909
17910
17911
17912
17913
17914
17915
17916
17917
17918
17919
17920
From d4a329ee417f1b75ba087828bb4b2f62c1ef57bb Mon Sep 17 00:00:00 2001
From: Auto Configured <auto.configured>
Date: Sun, 25 Oct 2015 12:19:45 -0700
Subject: [PATCH] Ci add ccsecuroty

Signed-off-by: Auto Configured <auto.configured>
---
 include/linux/ccsecurity.h           |  926 +++++
 include/linux/lsm2ccsecurity.h       |  181 +
 security/ccsecurity/Config.in        |   83 +
 security/ccsecurity/Kconfig          |  190 +
 security/ccsecurity/Makefile         |  122 +
 security/ccsecurity/gc.c             | 1036 ++++++
 security/ccsecurity/internal.h       | 2090 +++++++++++
 security/ccsecurity/load_policy.c    |  352 ++
 security/ccsecurity/lsm2ccsecurity.c |  192 +
 security/ccsecurity/memory.c         |  356 ++
 security/ccsecurity/permission.c     | 5025 ++++++++++++++++++++++++++
 security/ccsecurity/policy_io.c      | 6484 ++++++++++++++++++++++++++++++++++
 security/ccsecurity/realpath.c       |  767 ++++
 13 files changed, 17804 insertions(+)
 create mode 100644 include/linux/ccsecurity.h
 create mode 100644 include/linux/lsm2ccsecurity.h
 create mode 100644 security/ccsecurity/Config.in
 create mode 100644 security/ccsecurity/Kconfig
 create mode 100644 security/ccsecurity/Makefile
 create mode 100644 security/ccsecurity/gc.c
 create mode 100644 security/ccsecurity/internal.h
 create mode 100644 security/ccsecurity/load_policy.c
 create mode 100644 security/ccsecurity/lsm2ccsecurity.c
 create mode 100644 security/ccsecurity/memory.c
 create mode 100644 security/ccsecurity/permission.c
 create mode 100644 security/ccsecurity/policy_io.c
 create mode 100644 security/ccsecurity/realpath.c

diff --git a/include/linux/ccsecurity.h b/include/linux/ccsecurity.h
new file mode 100644
index 0000000..6c1ca2b
--- /dev/null
+++ b/include/linux/ccsecurity.h
@@ -0,0 +1,926 @@
+/*
+ * include/linux/ccsecurity.h
+ *
+ * Copyright (C) 2005-2012  NTT DATA CORPORATION
+ *
+ * Version: 1.8.4   2015/05/05
+ */
+
+#ifndef _LINUX_CCSECURITY_H
+#define _LINUX_CCSECURITY_H
+
+#include <linux/version.h>
+
+#ifndef __user
+#define __user
+#endif
+
+struct nameidata;
+struct path;
+struct dentry;
+struct vfsmount;
+struct linux_binprm;
+struct pt_regs;
+struct file;
+struct ctl_table;
+struct socket;
+struct sockaddr;
+struct sock;
+struct sk_buff;
+struct msghdr;
+struct pid_namespace;
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)
+int search_binary_handler(struct linux_binprm *bprm);
+#else
+int search_binary_handler(struct linux_binprm *bprm, struct pt_regs *regs);
+#endif
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 0, 0)
+#include <linux/lsm2ccsecurity.h>
+#endif
+
+#ifdef CONFIG_CCSECURITY
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) && LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0)
+/* Obtain prototype of __d_path(). */
+#include <linux/dcache.h>
+#endif
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
+/* Obtain definition of kuid_t and kgid_t. */
+#include <linux/uidgid.h>
+#endif
+
+/* For exporting variables and functions. */
+struct ccsecurity_exports {
+	void (*load_policy) (const char *filename);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) && defined(CONFIG_SECURITY)
+	void (*add_hooks) (void);
+#endif
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
+	char * (*d_absolute_path) (const struct path *, char *, int);
+#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)
+	typeof(__d_path) (*__d_path);
+#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
+	spinlock_t *vfsmount_lock;
+#endif
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
+	struct task_struct * (*find_task_by_vpid) (pid_t nr);
+	struct task_struct * (*find_task_by_pid_ns) (pid_t nr,
+						     struct pid_namespace *ns);
+#endif
+};
+
+/* For doing access control. */
+struct ccsecurity_operations {
+	void (*check_profile) (void);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
+	int (*chroot_permission) (struct path *path);
+	int (*pivot_root_permission) (struct path *old_path,
+				      struct path *new_path);
+	int (*mount_permission) (const char *dev_name, struct path *path,
+				 const char *type, unsigned long flags,
+				 void *data_page);
+#else
+	int (*chroot_permission) (struct nameidata *nd);
+	int (*pivot_root_permission) (struct nameidata *old_nd,
+				      struct nameidata *new_nd);
+	int (*mount_permission) (const char *dev_name, struct nameidata *nd,
+				 const char *type, unsigned long flags,
+				 void *data_page);
+#endif
+	int (*umount_permission) (struct vfsmount *mnt, int flags);
+	_Bool (*lport_reserved) (const u16 port);
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 32)
+	void (*save_open_mode) (int mode);
+	void (*clear_open_mode) (void);
+	int (*open_permission) (struct dentry *dentry, struct vfsmount *mnt,
+				const int flag);
+#else
+	int (*open_permission) (struct file *file);
+#endif
+	int (*ptrace_permission) (long request, long pid);
+	int (*ioctl_permission) (struct file *filp, unsigned int cmd,
+				 unsigned long arg);
+	int (*parse_table) (int __user *name, int nlen, void __user *oldval,
+			    void __user *newval, struct ctl_table *table);
+	_Bool (*capable) (const u8 operation);
+	int (*mknod_permission) (struct dentry *dentry, struct vfsmount *mnt,
+				 unsigned int mode, unsigned int dev);
+	int (*mkdir_permission) (struct dentry *dentry, struct vfsmount *mnt,
+				 unsigned int mode);
+	int (*rmdir_permission) (struct dentry *dentry, struct vfsmount *mnt);
+	int (*unlink_permission) (struct dentry *dentry, struct vfsmount *mnt);
+	int (*symlink_permission) (struct dentry *dentry, struct vfsmount *mnt,
+				   const char *from);
+	int (*truncate_permission) (struct dentry *dentry,
+				    struct vfsmount *mnt);
+	int (*rename_permission) (struct dentry *old_dentry,
+				  struct dentry *new_dentry,
+				  struct vfsmount *mnt);
+	int (*link_permission) (struct dentry *old_dentry,
+				struct dentry *new_dentry,
+				struct vfsmount *mnt);
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
+	int (*open_exec_permission) (struct dentry *dentry,
+				     struct vfsmount *mnt);
+	int (*uselib_permission) (struct dentry *dentry, struct vfsmount *mnt);
+#endif
+	int (*fcntl_permission) (struct file *file, unsigned int cmd,
+				 unsigned long arg);
+	int (*kill_permission) (pid_t pid, int sig);
+	int (*tgkill_permission) (pid_t tgid, pid_t pid, int sig);
+	int (*tkill_permission) (pid_t pid, int sig);
+	int (*socket_create_permission) (int family, int type, int protocol);
+	int (*socket_listen_permission) (struct socket *sock);
+	int (*socket_connect_permission) (struct socket *sock,
+					  struct sockaddr *addr, int addr_len);
+	int (*socket_bind_permission) (struct socket *sock,
+				       struct sockaddr *addr, int addr_len);
+	int (*socket_post_accept_permission) (struct socket *sock,
+					      struct socket *newsock);
+	int (*socket_sendmsg_permission) (struct socket *sock,
+					  struct msghdr *msg, int size);
+	int (*socket_post_recvmsg_permission) (struct sock *sk,
+					       struct sk_buff *skb, int flags);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
+	int (*chown_permission) (struct dentry *dentry, struct vfsmount *mnt,
+				 kuid_t user, kgid_t group);
+#else
+	int (*chown_permission) (struct dentry *dentry, struct vfsmount *mnt,
+				 uid_t user, gid_t group);
+#endif
+	int (*chmod_permission) (struct dentry *dentry, struct vfsmount *mnt,
+				 mode_t mode);
+	int (*getattr_permission) (struct vfsmount *mnt,
+				   struct dentry *dentry);
+	int (*sigqueue_permission) (pid_t pid, int sig);
+	int (*tgsigqueue_permission) (pid_t tgid, pid_t pid, int sig);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)
+	int (*search_binary_handler) (struct linux_binprm *bprm);
+#else
+	int (*search_binary_handler) (struct linux_binprm *bprm,
+				      struct pt_regs *regs);
+#endif
+#ifdef CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY
+	int (*alloc_task_security) (const struct task_struct *task);
+	void (*free_task_security) (const struct task_struct *task);
+#endif
+	_Bool disabled;
+};
+
+extern struct ccsecurity_operations ccsecurity_ops;
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
+
+static inline int ccs_chroot_permission(struct path *path)
+{
+	int (*func) (struct path *) = ccsecurity_ops.chroot_permission;
+	return func ? func(path) : 0;
+}
+
+static inline int ccs_pivot_root_permission(struct path *old_path,
+					    struct path *new_path)
+{
+	int (*func) (struct path *, struct path *)
+		= ccsecurity_ops.pivot_root_permission;
+	return func ? func(old_path, new_path) : 0;
+}
+
+static inline int ccs_mount_permission(const char *dev_name, struct path *path,
+				       const char *type, unsigned long flags,
+				       void *data_page)
+{
+	int (*func) (const char *, struct path *, const char *, unsigned long,
+		     void *) = ccsecurity_ops.mount_permission;
+	return func ? func(dev_name, path, type, flags, data_page) : 0;
+}
+
+#else
+
+static inline int ccs_chroot_permission(struct nameidata *nd)
+{
+	int (*func) (struct nameidata *) = ccsecurity_ops.chroot_permission;
+	return func ? func(nd) : 0;
+}
+
+static inline int ccs_pivot_root_permission(struct nameidata *old_nd,
+					    struct nameidata *new_nd)
+{
+	int (*func) (struct nameidata *, struct nameidata *)
+		= ccsecurity_ops.pivot_root_permission;
+	return func ? func(old_nd, new_nd) : 0;
+}
+
+static inline int ccs_mount_permission(const char *dev_name,
+				       struct nameidata *nd, const char *type,
+				       unsigned long flags, void *data_page)
+{
+	int (*func) (const char *, struct nameidata *, const char *,
+		     unsigned long, void *) = ccsecurity_ops.mount_permission;
+	return func ? func(dev_name, nd, type, flags, data_page) : 0;
+}
+
+#endif
+
+static inline int ccs_umount_permission(struct vfsmount *mnt, int flags)
+{
+	int (*func) (struct vfsmount *, int)
+		= ccsecurity_ops.umount_permission;
+	return func ? func(mnt, flags) : 0;
+}
+
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 32)
+
+static inline void ccs_save_open_mode(int mode)
+{
+	void (*func) (int) = ccsecurity_ops.save_open_mode;
+	if (func)
+		func(mode);
+}
+
+static inline void ccs_clear_open_mode(void)
+{
+	void (*func) (void) = ccsecurity_ops.clear_open_mode;
+	if (func)
+		func();
+}
+
+static inline int ccs_open_permission(struct dentry *dentry,
+				      struct vfsmount *mnt, const int flag)
+{
+	int (*func) (struct dentry *, struct vfsmount *, const int)
+		= ccsecurity_ops.open_permission;
+	return func ? func(dentry, mnt, flag) : 0;
+}
+
+#else
+
+static inline int ccs_open_permission(struct file *filp)
+{
+	int (*func) (struct file *) = ccsecurity_ops.open_permission;
+	return func ? func(filp) : 0;
+}
+
+#endif
+
+static inline int ccs_fcntl_permission(struct file *file, unsigned int cmd,
+				       unsigned long arg)
+{
+	int (*func) (struct file *, unsigned int, unsigned long)
+		= ccsecurity_ops.fcntl_permission;
+	return func ? func(file, cmd, arg) : 0;
+}
+
+static inline int ccs_ioctl_permission(struct file *filp, unsigned int cmd,
+				       unsigned long arg)
+{
+	int (*func) (struct file *, unsigned int, unsigned long)
+		= ccsecurity_ops.ioctl_permission;
+	return func ? func(filp, cmd, arg) : 0;
+}
+
+static inline int ccs_parse_table(int __user *name, int nlen,
+				  void __user *oldval, void __user *newval,
+				  struct ctl_table *table)
+{
+	int (*func) (int __user *, int, void __user *, void __user *,
+		     struct ctl_table *) = ccsecurity_ops.parse_table;
+	return func ? func(name, nlen, oldval, newval, table) : 0;
+}
+
+static inline int ccs_mknod_permission(struct dentry *dentry,
+				       struct vfsmount *mnt, unsigned int mode,
+				       unsigned int dev)
+{
+	int (*func) (struct dentry *, struct vfsmount *, unsigned int,
+		     unsigned int) = ccsecurity_ops.mknod_permission;
+	return func ? func(dentry, mnt, mode, dev) : 0;
+}
+
+static inline int ccs_mkdir_permission(struct dentry *dentry,
+				       struct vfsmount *mnt, unsigned int mode)
+{
+	int (*func) (struct dentry *, struct vfsmount *, unsigned int)
+		= ccsecurity_ops.mkdir_permission;
+	return func ? func(dentry, mnt, mode) : 0;
+}
+
+static inline int ccs_rmdir_permission(struct dentry *dentry,
+				       struct vfsmount *mnt)
+{
+	int (*func) (struct dentry *, struct vfsmount *)
+		= ccsecurity_ops.rmdir_permission;
+	return func ? func(dentry, mnt) : 0;
+}
+
+static inline int ccs_unlink_permission(struct dentry *dentry,
+					struct vfsmount *mnt)
+{
+	int (*func) (struct dentry *, struct vfsmount *)
+		= ccsecurity_ops.unlink_permission;
+	return func ? func(dentry, mnt) : 0;
+}
+
+static inline int ccs_symlink_permission(struct dentry *dentry,
+					 struct vfsmount *mnt,
+					 const char *from)
+{
+	int (*func) (struct dentry *, struct vfsmount *, const char *)
+		= ccsecurity_ops.symlink_permission;
+	return func ? func(dentry, mnt, from) : 0;
+}
+
+static inline int ccs_truncate_permission(struct dentry *dentry,
+					  struct vfsmount *mnt)
+{
+	int (*func) (struct dentry *, struct vfsmount *)
+		= ccsecurity_ops.truncate_permission;
+	return func ? func(dentry, mnt) : 0;
+}
+
+static inline int ccs_rename_permission(struct dentry *old_dentry,
+					struct dentry *new_dentry,
+					struct vfsmount *mnt)
+{
+	int (*func) (struct dentry *, struct dentry *, struct vfsmount *)
+		= ccsecurity_ops.rename_permission;
+	return func ? func(old_dentry, new_dentry, mnt) : 0;
+}
+
+static inline int ccs_link_permission(struct dentry *old_dentry,
+				      struct dentry *new_dentry,
+				      struct vfsmount *mnt)
+{
+	int (*func) (struct dentry *, struct dentry *, struct vfsmount *)
+		= ccsecurity_ops.link_permission;
+	return func ? func(old_dentry, new_dentry, mnt) : 0;
+}
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
+
+static inline int ccs_open_exec_permission(struct dentry *dentry,
+					   struct vfsmount *mnt)
+{
+	int (*func) (struct dentry *, struct vfsmount *)
+		= ccsecurity_ops.open_exec_permission;
+	return func ? func(dentry, mnt) : 0;
+}
+
+static inline int ccs_uselib_permission(struct dentry *dentry,
+					struct vfsmount *mnt)
+{
+	int (*func) (struct dentry *, struct vfsmount *)
+		= ccsecurity_ops.uselib_permission;
+	return func ? func(dentry, mnt) : 0;
+}
+
+#endif
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
+
+static inline int ccs_chown_permission(struct dentry *dentry,
+				       struct vfsmount *mnt, kuid_t user,
+				       kgid_t group)
+{
+	int (*func) (struct dentry *, struct vfsmount *, kuid_t, kgid_t)
+		= ccsecurity_ops.chown_permission;
+	return func ? func(dentry, mnt, user, group) : 0;
+}
+
+#else
+
+static inline int ccs_chown_permission(struct dentry *dentry,
+				       struct vfsmount *mnt, uid_t user,
+				       gid_t group)
+{
+	int (*func) (struct dentry *, struct vfsmount *, uid_t, gid_t)
+		= ccsecurity_ops.chown_permission;
+	return func ? func(dentry, mnt, user, group) : 0;
+}
+
+#endif
+
+static inline int ccs_chmod_permission(struct dentry *dentry,
+				       struct vfsmount *mnt, mode_t mode)
+{
+	int (*func) (struct dentry *, struct vfsmount *, mode_t)
+		= ccsecurity_ops.chmod_permission;
+	return func ? func(dentry, mnt, mode) : 0;
+}
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)
+
+static inline int ccs_search_binary_handler(struct linux_binprm *bprm)
+{
+	return ccsecurity_ops.search_binary_handler(bprm);
+}
+
+#else
+
+static inline int ccs_search_binary_handler(struct linux_binprm *bprm,
+					    struct pt_regs *regs)
+{
+	return ccsecurity_ops.search_binary_handler(bprm, regs);
+}
+
+#endif
+
+#else
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
+
+static inline int ccs_chroot_permission(struct path *path)
+{
+	return 0;
+}
+
+static inline int ccs_pivot_root_permission(struct path *old_path,
+					    struct path *new_path)
+{
+	return 0;
+}
+
+static inline int ccs_mount_permission(const char *dev_name, struct path *path,
+				       const char *type, unsigned long flags,
+				       void *data_page)
+{
+	return 0;
+}
+
+#else
+
+static inline int ccs_chroot_permission(struct nameidata *nd)
+{
+	return 0;
+}
+
+static inline int ccs_pivot_root_permission(struct nameidata *old_nd,
+					    struct nameidata *new_nd)
+{
+	return 0;
+}
+
+static inline int ccs_mount_permission(const char *dev_name,
+				       struct nameidata *nd, const char *type,
+				       unsigned long flags, void *data_page)
+{
+	return 0;
+}
+
+#endif
+
+static inline int ccs_umount_permission(struct vfsmount *mnt, int flags)
+{
+	return 0;
+}
+
+static inline void ccs_save_open_mode(int mode)
+{
+}
+
+static inline void ccs_clear_open_mode(void)
+{
+}
+
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 32)
+
+static inline int ccs_open_permission(struct dentry *dentry,
+				      struct vfsmount *mnt, const int flag)
+{
+	return 0;
+}
+
+#else
+
+static inline int ccs_open_permission(struct file *filp)
+{
+	return 0;
+}
+
+#endif
+
+static inline int ccs_ioctl_permission(struct file *filp, unsigned int cmd,
+				       unsigned long arg)
+{
+	return 0;
+}
+
+static inline int ccs_parse_table(int __user *name, int nlen,
+				  void __user *oldval, void __user *newval,
+				  struct ctl_table *table)
+{
+	return 0;
+}
+
+static inline int ccs_mknod_permission(struct dentry *dentry,
+				       struct vfsmount *mnt, unsigned int mode,
+				       unsigned int dev)
+{
+	return 0;
+}
+
+static inline int ccs_mkdir_permission(struct dentry *dentry,
+				       struct vfsmount *mnt, unsigned int mode)
+{
+	return 0;
+}
+
+static inline int ccs_rmdir_permission(struct dentry *dentry,
+				       struct vfsmount *mnt)
+{
+	return 0;
+}
+
+static inline int ccs_unlink_permission(struct dentry *dentry,
+					struct vfsmount *mnt)
+{
+	return 0;
+}
+
+static inline int ccs_symlink_permission(struct dentry *dentry,
+					 struct vfsmount *mnt,
+					 const char *from)
+{
+	return 0;
+}
+
+static inline int ccs_truncate_permission(struct dentry *dentry,
+					  struct vfsmount *mnt)
+{
+	return 0;
+}
+
+static inline int ccs_rename_permission(struct dentry *old_dentry,
+					struct dentry *new_dentry,
+					struct vfsmount *mnt)
+{
+	return 0;
+}
+
+static inline int ccs_link_permission(struct dentry *old_dentry,
+				      struct dentry *new_dentry,
+				      struct vfsmount *mnt)
+{
+	return 0;
+}
+
+static inline int ccs_open_exec_permission(struct dentry *dentry,
+					   struct vfsmount *mnt)
+{
+	return 0;
+}
+
+static inline int ccs_uselib_permission(struct dentry *dentry,
+					struct vfsmount *mnt)
+{
+	return 0;
+}
+
+static inline int ccs_fcntl_permission(struct file *file, unsigned int cmd,
+				       unsigned long arg)
+{
+	return 0;
+}
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
+
+static inline int ccs_chown_permission(struct dentry *dentry,
+				       struct vfsmount *mnt, kuid_t user,
+				       kgid_t group)
+{
+	return 0;
+}
+
+#else
+
+static inline int ccs_chown_permission(struct dentry *dentry,
+				       struct vfsmount *mnt, uid_t user,
+				       gid_t group)
+{
+	return 0;
+}
+
+#endif
+
+static inline int ccs_chmod_permission(struct dentry *dentry,
+				       struct vfsmount *mnt, mode_t mode)
+{
+	return 0;
+}
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)
+
+static inline int ccs_search_binary_handler(struct linux_binprm *bprm)
+{
+	return search_binary_handler(bprm);
+}
+
+#else
+
+static inline int ccs_search_binary_handler(struct linux_binprm *bprm,
+					    struct pt_regs *regs)
+{
+	return search_binary_handler(bprm, regs);
+}
+
+#endif
+
+#endif
+
+#ifdef CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY
+
+static inline int ccs_alloc_task_security(const struct task_struct *task)
+{
+	int (*func) (const struct task_struct *)
+		= ccsecurity_ops.alloc_task_security;
+	return func ? func(task) : 0;
+}
+
+static inline void ccs_free_task_security(const struct task_struct *task)
+{
+	void (*func) (const struct task_struct *)
+		= ccsecurity_ops.free_task_security;
+	if (func)
+		func(task);
+}
+
+#else
+
+static inline int ccs_alloc_task_security(const struct task_struct *task)
+{
+	return 0;
+}
+
+static inline void ccs_free_task_security(const struct task_struct *task)
+{
+}
+
+#endif
+
+#ifdef CONFIG_CCSECURITY_FILE_GETATTR
+
+static inline int ccs_getattr_permission(struct vfsmount *mnt,
+					 struct dentry *dentry)
+{
+	int (*func) (struct vfsmount *, struct dentry *)
+		= ccsecurity_ops.getattr_permission;
+	return func ? func(mnt, dentry) : 0;
+}
+
+#else
+
+static inline int ccs_getattr_permission(struct vfsmount *mnt,
+					 struct dentry *dentry)
+{
+	return 0;
+}
+
+#endif
+
+#ifdef CONFIG_CCSECURITY_NETWORK
+
+static inline int ccs_socket_listen_permission(struct socket *sock)
+{
+	int (*func) (struct socket *)
+		= ccsecurity_ops.socket_listen_permission;
+	return func ? func(sock) : 0;
+}
+
+static inline int ccs_socket_connect_permission(struct socket *sock,
+						struct sockaddr *addr,
+						int addr_len)
+{
+	int (*func) (struct socket *, struct sockaddr *, int)
+		= ccsecurity_ops.socket_connect_permission;
+	return func ? func(sock, addr, addr_len) : 0;
+}
+
+static inline int ccs_socket_bind_permission(struct socket *sock,
+					     struct sockaddr *addr,
+					     int addr_len)
+{
+	int (*func) (struct socket *, struct sockaddr *, int)
+		= ccsecurity_ops.socket_bind_permission;
+	return func ? func(sock, addr, addr_len) : 0;
+}
+
+static inline int ccs_socket_post_accept_permission(struct socket *sock,
+						    struct socket *newsock)
+{
+	int (*func) (struct socket *, struct socket *)
+		= ccsecurity_ops.socket_post_accept_permission;
+	return func ? func(sock, newsock) : 0;
+}
+
+static inline int ccs_socket_sendmsg_permission(struct socket *sock,
+						struct msghdr *msg,
+						int size)
+{
+	int (*func) (struct socket *, struct msghdr *, int)
+		= ccsecurity_ops.socket_sendmsg_permission;
+	return func ? func(sock, msg, size) : 0;
+}
+
+#else
+
+static inline int ccs_socket_listen_permission(struct socket *sock)
+{
+	return 0;
+}
+
+static inline int ccs_socket_connect_permission(struct socket *sock,
+						struct sockaddr *addr,
+						int addr_len)
+{
+	return 0;
+}
+
+static inline int ccs_socket_bind_permission(struct socket *sock,
+					     struct sockaddr *addr,
+					     int addr_len)
+{
+	return 0;
+}
+
+static inline int ccs_socket_post_accept_permission(struct socket *sock,
+						    struct socket *newsock)
+{
+	return 0;
+}
+
+static inline int ccs_socket_sendmsg_permission(struct socket *sock,
+						struct msghdr *msg,
+						int size)
+{
+	return 0;
+}
+
+#endif
+
+#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG
+
+static inline int ccs_socket_post_recvmsg_permission(struct sock *sk,
+						     struct sk_buff *skb,
+						     int flags)
+{
+	int (*func) (struct sock *, struct sk_buff *, int)
+		= ccsecurity_ops.socket_post_recvmsg_permission;
+	return func ? func(sk, skb, flags) : 0;
+}
+
+#else
+
+static inline int ccs_socket_post_recvmsg_permission(struct sock *sk,
+						     struct sk_buff *skb,
+						     int flags)
+{
+	return 0;
+}
+
+#endif
+
+#ifdef CONFIG_CCSECURITY_PORTRESERVE
+
+static inline _Bool ccs_lport_reserved(const u16 port)
+{
+	_Bool (*func) (const u16) = ccsecurity_ops.lport_reserved;
+	return func ? func(port) : 0;
+}
+
+#else
+
+static inline _Bool ccs_lport_reserved(const u16 port)
+{
+	return 0;
+}
+
+#endif
+
+#ifdef CONFIG_CCSECURITY_CAPABILITY
+
+static inline _Bool ccs_capable(const u8 operation)
+{
+	_Bool (*func) (const u8) = ccsecurity_ops.capable;
+	return func ? func(operation) : 1;
+}
+
+static inline int ccs_socket_create_permission(int family, int type,
+					       int protocol)
+{
+	int (*func) (int, int, int) = ccsecurity_ops.socket_create_permission;
+	return func ? func(family, type, protocol) : 0;
+}
+
+static inline int ccs_ptrace_permission(long request, long pid)
+{
+	int (*func) (long, long) = ccsecurity_ops.ptrace_permission;
+	return func ? func(request, pid) : 0;
+}
+
+#else
+
+static inline _Bool ccs_capable(const u8 operation)
+{
+	return 1;
+}
+
+static inline int ccs_socket_create_permission(int family, int type,
+					       int protocol)
+{
+	return 0;
+}
+
+static inline int ccs_ptrace_permission(long request, long pid)
+{
+	return 0;
+}
+
+#endif
+
+#ifdef CONFIG_CCSECURITY_IPC
+
+static inline int ccs_kill_permission(pid_t pid, int sig)
+{
+	int (*func) (pid_t, int) = ccsecurity_ops.kill_permission;
+	return func ? func(pid, sig) : 0;
+}
+
+static inline int ccs_tgkill_permission(pid_t tgid, pid_t pid, int sig)
+{
+	int (*func) (pid_t, pid_t, int) = ccsecurity_ops.tgkill_permission;
+	return func ? func(tgid, pid, sig) : 0;
+}
+
+static inline int ccs_tkill_permission(pid_t pid, int sig)
+{
+	int (*func) (pid_t, int) = ccsecurity_ops.tkill_permission;
+	return func ? func(pid, sig) : 0;
+}
+
+static inline int ccs_sigqueue_permission(pid_t pid, int sig)
+{
+	int (*func) (pid_t, int) = ccsecurity_ops.sigqueue_permission;
+	return func ? func(pid, sig) : 0;
+}
+
+static inline int ccs_tgsigqueue_permission(pid_t tgid, pid_t pid, int sig)
+{
+	int (*func) (pid_t, pid_t, int) = ccsecurity_ops.tgsigqueue_permission;
+	return func ? func(tgid, pid, sig) : 0;
+}
+
+#else
+
+static inline int ccs_kill_permission(pid_t pid, int sig)
+{
+	return 0;
+}
+
+static inline int ccs_tgkill_permission(pid_t tgid, pid_t pid, int sig)
+{
+	return 0;
+}
+
+static inline int ccs_tkill_permission(pid_t pid, int sig)
+{
+	return 0;
+}
+
+static inline int ccs_sigqueue_permission(pid_t pid, int sig)
+{
+	return 0;
+}
+
+static inline int ccs_tgsigqueue_permission(pid_t tgid, pid_t pid, int sig)
+{
+	return 0;
+}
+
+#endif
+
+/* Index numbers for Capability Controls. */
+enum ccs_capability_acl_index {
+	/* socket(PF_ROUTE, *, *)                                      */
+	CCS_USE_ROUTE_SOCKET,
+	/* socket(PF_PACKET, *, *)                                     */
+	CCS_USE_PACKET_SOCKET,
+	/* sys_reboot()                                                */
+	CCS_SYS_REBOOT,
+	/* sys_vhangup()                                               */
+	CCS_SYS_VHANGUP,
+	/* do_settimeofday(), sys_adjtimex()                           */
+	CCS_SYS_SETTIME,
+	/* sys_nice(), sys_setpriority()                               */
+	CCS_SYS_NICE,
+	/* sys_sethostname(), sys_setdomainname()                      */
+	CCS_SYS_SETHOSTNAME,
+	/* sys_create_module(), sys_init_module(), sys_delete_module() */
+	CCS_USE_KERNEL_MODULE,
+	/* sys_kexec_load()                                            */
+	CCS_SYS_KEXEC_LOAD,
+	/* sys_ptrace()                                                */
+	CCS_SYS_PTRACE,
+	CCS_MAX_CAPABILITY_INDEX
+};
+
+#endif
diff --git a/include/linux/lsm2ccsecurity.h b/include/linux/lsm2ccsecurity.h
new file mode 100644
index 0000000..ab4ea5c
--- /dev/null
+++ b/include/linux/lsm2ccsecurity.h
@@ -0,0 +1,181 @@
+/*
+ * include/linux/lsm2ccsecurity.h
+ *
+ * Copyright (C) 2005-2012  NTT DATA CORPORATION
+ *
+ * Version: 1.8.4   2015/07/11
+ */
+
+#ifndef _LINUX_LSM2CCSECURITY_H
+#define _LINUX_LSM2CCSECURITY_H
+
+#include <linux/version.h>
+#include <linux/uidgid.h>
+
+#ifdef CONFIG_CCSECURITY
+
+int ccs_settime(const struct timespec *ts, const struct timezone *tz);
+int ccs_sb_mount(const char *dev_name, struct path *path, const char *type,
+		 unsigned long flags, void *data);
+int ccs_sb_umount(struct vfsmount *mnt, int flags);
+int ccs_sb_pivotroot(struct path *old_path, struct path *new_path);
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 1, 0)
+int ccs_inode_getattr(struct vfsmount *mnt, struct dentry *dentry);
+#else
+int ccs_inode_getattr(const struct path *path);
+#endif
+int ccs_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
+int ccs_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg);
+int ccs_file_open(struct file *file, const struct cred *cred);
+int ccs_socket_create(int family, int type, int protocol, int kern);
+int ccs_socket_bind(struct socket *sock, struct sockaddr *address,
+		    int addrlen);
+int ccs_socket_connect(struct socket *sock, struct sockaddr *address,
+		       int addrlen);
+int ccs_socket_listen(struct socket *sock, int backlog);
+int ccs_socket_sendmsg(struct socket *sock, struct msghdr *msg, int size);
+int ccs_path_unlink(struct path *dir, struct dentry *dentry);
+int ccs_path_mkdir(struct path *dir, struct dentry *dentry, umode_t mode);
+int ccs_path_rmdir(struct path *dir, struct dentry *dentry);
+int ccs_path_mknod(struct path *dir, struct dentry *dentry, umode_t mode,
+		   unsigned int dev);
+int ccs_path_truncate(struct path *path);
+int ccs_path_symlink(struct path *dir, struct dentry *dentry,
+		     const char *old_name);
+int ccs_path_link(struct dentry *old_dentry, struct path *new_dir,
+		  struct dentry *new_dentry);
+int ccs_path_rename(struct path *old_dir, struct dentry *old_dentry,
+		    struct path *new_dir, struct dentry *new_dentry);
+int ccs_path_chmod(struct path *path, umode_t mode);
+int ccs_path_chown(struct path *path, kuid_t uid, kgid_t gid);
+int ccs_path_chroot(struct path *path);
+
+#else
+
+static inline int ccs_settime(const struct timespec *ts,
+			      const struct timezone *tz)
+{
+	return 0;
+}
+static inline int ccs_sb_mount(const char *dev_name, struct path *path,
+			       const char *type, unsigned long flags,
+			       void *data)
+{
+	return 0;
+}
+static inline int ccs_sb_umount(struct vfsmount *mnt, int flags)
+{
+	return 0;
+}
+static inline int ccs_sb_pivotroot(struct path *old_path,
+				   struct path *new_path)
+{
+	return 0;
+}
+#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 1, 0)
+static inline int ccs_inode_getattr(struct vfsmount *mnt,
+				    struct dentry *dentry)
+{
+	return 0;
+}
+#else
+static inline int ccs_inode_getattr(const struct path *path)
+{
+	return 0;
+}
+#endif
+static inline int ccs_file_ioctl(struct file *file, unsigned int cmd,
+				 unsigned long arg)
+{
+	return 0;
+}
+static inline int ccs_file_fcntl(struct file *file, unsigned int cmd,
+				 unsigned long arg)
+{
+	return 0;
+}
+static inline int ccs_file_open(struct file *file, const struct cred *cred)
+{
+	return 0;
+}
+static inline int ccs_socket_create(int family, int type, int protocol,
+				    int kern)
+{
+	return 0;
+}
+static inline int ccs_socket_bind(struct socket *sock,
+				  struct sockaddr *address, int addrlen)
+{
+	return 0;
+}
+static inline int ccs_socket_connect(struct socket *sock,
+				     struct sockaddr *address, int addrlen)
+{
+	return 0;
+}
+static inline int ccs_socket_listen(struct socket *sock, int backlog)
+{
+	return 0;
+}
+static inline int ccs_socket_sendmsg(struct socket *sock, struct msghdr *msg,
+				     int size)
+{
+	return 0;
+}
+static inline int ccs_path_unlink(struct path *dir, struct dentry *dentry)
+{
+	return 0;
+}
+static inline int ccs_path_mkdir(struct path *dir, struct dentry *dentry,
+				 umode_t mode)
+{
+	return 0;
+}
+static inline int ccs_path_rmdir(struct path *dir, struct dentry *dentry)
+{
+	return 0;
+}
+static inline int ccs_path_mknod(struct path *dir, struct dentry *dentry,
+				 umode_t mode, unsigned int dev)
+{
+	return 0;
+}
+static inline int ccs_path_truncate(struct path *path)
+{
+	return 0;
+}
+static inline int ccs_path_symlink(struct path *dir, struct dentry *dentry,
+				   const char *old_name)
+{
+	return 0;
+}
+static inline int ccs_path_link(struct dentry *old_dentry,
+				struct path *new_dir,
+				struct dentry *new_dentry)
+{
+	return 0;
+}
+static inline int ccs_path_rename(struct path *old_dir,
+				  struct dentry *old_dentry,
+				  struct path *new_dir,
+				  struct dentry *new_dentry)
+{
+	return 0;
+}
+static inline int ccs_path_chmod(struct path *path, umode_t mode)
+{
+	return 0;
+}
+static inline int ccs_path_chown(struct path *path, kuid_t uid, kgid_t gid)
+{
+	return 0;
+}
+static inline int ccs_path_chroot(struct path *path)
+{
+	return 0;
+}
+
+#endif /* defined(CONFIG_CCSECURITY) */
+
+#endif /* !defined(_LINUX_LSM2CCSECURITY_H) */
diff --git a/security/ccsecurity/Config.in b/security/ccsecurity/Config.in
new file mode 100644
index 0000000..da39bad6
--- /dev/null
+++ b/security/ccsecurity/Config.in
@@ -0,0 +1,83 @@
+#
+# Mandatory Access Control configuration
+#
+mainmenu_option next_comment
+comment 'Security options'
+
+[ -z "$CONFIG_CCSECURITY" ] && define_bool CONFIG_CCSECURITY y
+bool 'CCSecurity support' CONFIG_CCSECURITY
+
+if [ "$CONFIG_CCSECURITY" = "y" ]; then
+
+  [ -z "$CONFIG_CCSECURITY_LKM" ] && define_bool CONFIG_CCSECURITY_LKM n
+  bool 'Compile as loadable kernel module' CONFIG_CCSECURITY_LKM
+
+  [ -z "$CONFIG_CCSECURITY_DISABLE_BY_DEFAULT" ] && define_bool CONFIG_CCSECURITY_DISABLE_BY_DEFAULT n
+  bool 'Disable by default' CONFIG_CCSECURITY_DISABLE_BY_DEFAULT
+
+  [ -z "$CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY" ] && define_int CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY 2048
+  [ $CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY -lt 0 ] && define_int CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY 0
+  int  'Default maximal count for learning mode' CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY
+
+  [ -z "$CONFIG_CCSECURITY_MAX_AUDIT_LOG" ] && define_int CONFIG_CCSECURITY_MAX_AUDIT_LOG 1024
+  [ $CONFIG_CCSECURITY_MAX_AUDIT_LOG -lt 0 ] && define_int CONFIG_CCSECURITY_MAX_AUDIT_LOG 0
+  int  'Default maximal count for audit log' CONFIG_CCSECURITY_MAX_AUDIT_LOG
+
+  [ -z "$CONFIG_CCSECURITY_OMIT_USERSPACE_LOADER" ] && define_bool CONFIG_CCSECURITY_OMIT_USERSPACE_LOADER n
+  bool 'Activate without calling userspace policy loader.' CONFIG_CCSECURITY_OMIT_USERSPACE_LOADER
+
+  if [ "$CONFIG_CCSECURITY_OMIT_USERSPACE_LOADER" = "n" ]; then
+
+    define_string CONFIG_CCSECURITY_POLICY_LOADER "/sbin/ccs-init"
+    string 'Location of userspace policy loader' CONFIG_CCSECURITY_POLICY_LOADER "/sbin/ccs-init"
+
+    define_string CONFIG_CCSECURITY_ACTIVATION_TRIGGER "/sbin/init"
+    string 'Trigger for calling userspace policy loader' CONFIG_CCSECURITY_ACTIVATION_TRIGGER "/sbin/init"
+
+  fi
+
+  [ -z "$CONFIG_CCSECURITY_FILE_READDIR" ] && define_bool CONFIG_CCSECURITY_FILE_READDIR y
+  bool "Enable readdir operation restriction." CONFIG_CCSECURITY_FILE_READDIR
+
+  [ -z "$CONFIG_CCSECURITY_FILE_GETATTR" ] && define_bool CONFIG_CCSECURITY_FILE_GETATTR y
+  bool "Enable getattr operation restriction." CONFIG_CCSECURITY_FILE_GETATTR
+
+  if [ "$CONFIG_NET" = "y" ]; then
+
+     [ -z "$CONFIG_CCSECURITY_NETWORK" ] && define_bool CONFIG_CCSECURITY_NETWORK y
+     bool "Enable socket operation restriction." CONFIG_CCSECURITY_NETWORK
+
+     if [ "$CONFIG_CCSECURITY_NETWORK" = "y" ]; then
+
+	#[ -z "$CONFIG_CCSECURITY_NETWORK_RECVMSG" ] &&
+	define_bool CONFIG_CCSECURITY_NETWORK_RECVMSG y
+
+     fi
+
+  fi
+
+  [ -z "$CONFIG_CCSECURITY_CAPABILITY" ] && define_bool CONFIG_CCSECURITY_CAPABILITY y
+  bool "Enable non-POSIX capability operation restriction." CONFIG_CCSECURITY_CAPABILITY
+
+  [ -z "$CONFIG_CCSECURITY_IPC" ] && define_bool CONFIG_CCSECURITY_IPC y
+  bool "Enable IPC operation restriction." CONFIG_CCSECURITY_IPC
+
+  [ -z "$CONFIG_CCSECURITY_MISC" ] && define_bool CONFIG_CCSECURITY_MISC y
+  bool "Enable environment variable names restriction." CONFIG_CCSECURITY_MISC
+
+  [ -z "$CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER" ] && define_bool CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER y
+  bool "Enable execute handler functionality." CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER
+
+  [ -z "$CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION" ] && define_bool CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION y
+  bool "Enable domain transition without program execution request." CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION
+
+  if [ "$CONFIG_NET" = "y" ]; then
+
+     [ -z "$CONFIG_CCSECURITY_PORTRESERVE" ] && define_bool CONFIG_CCSECURITY_PORTRESERVE y
+     bool "Enable local port reserver." CONFIG_CCSECURITY_PORTRESERVE
+
+  fi
+
+fi
+
+endmenu
diff --git a/security/ccsecurity/Kconfig b/security/ccsecurity/Kconfig
new file mode 100644
index 0000000..8d7b200
--- /dev/null
+++ b/security/ccsecurity/Kconfig
@@ -0,0 +1,190 @@
+config CCSECURITY
+	bool "CCSecurity support"
+	default y
+	help
+	  Say Y here to support non-LSM version of TOMOYO Linux.
+	  http://tomoyo.osdn.jp/
+
+config CCSECURITY_LKM
+	bool "Compile as loadable kernel module"
+	default n
+	depends on CCSECURITY && MODULES
+	help
+	  This version of TOMOYO depends on patching the kernel source in order
+	  to insert some hooks which LSM does not provide. Therefore,
+	  recompiling the kernel is inevitable. But if you want to keep
+	  vmlinux's size as small as possible, you can compile most part of
+	  TOMOYO as a loadable kernel module by saying Y here.
+
+config CCSECURITY_DISABLE_BY_DEFAULT
+	bool "Disable by default"
+	default n
+	depends on CCSECURITY
+	help
+	  Say Y here if you want TOMOYO disabled by default.
+	  To enable TOMOYO, pass ccsecurity=on to kernel command line.
+	  To disable TOMOYO, pass ccsecurity=off to kernel command line.
+
+config CCSECURITY_USE_EXTERNAL_TASK_SECURITY
+	bool "Do not modify 'struct task_struct' in order to keep KABI"
+	default n
+	depends on CCSECURITY
+	help
+	  Say Y here if you want to keep KABI for prebuilt kernel modules
+	  unchanged. TOMOYO needs "struct ccs_domain_info *" and "u32" for each
+	  "struct task_struct". But embedding these variables into
+	  "struct task_struct" breaks KABI for prebuilt kernel modules (which
+	  means that you will need to rebuild prebuilt kernel modules).
+	  If you say Y here, these variables are managed outside
+	  "struct task_struct" rather than embedding into "struct task_struct",
+	  but accessing these variables becomes slower because lookup operation
+	  is performed every time the current thread needs to access them.
+
+config CCSECURITY_MAX_ACCEPT_ENTRY
+	int "Default maximal count for learning mode"
+	default 2048
+	range 0 2147483647
+	depends on CCSECURITY
+	help
+	  This is the default value for maximal ACL entries
+	  that are automatically appended into policy at "learning mode".
+	  Some programs access thousands of objects, so running
+	  such programs in "learning mode" dulls the system response
+	  and consumes much memory.
+	  This is the safeguard for such programs.
+
+config CCSECURITY_MAX_AUDIT_LOG
+	int "Default maximal count for audit log"
+	default 1024
+	range 0 2147483647
+	depends on CCSECURITY
+	help
+	  This is the default value for maximal entries for
+	  audit logs that the kernel can hold on memory.
+	  You can read the log via /proc/ccs/audit.
+	  If you don't need audit logs, you may set this value to 0.
+
+config CCSECURITY_OMIT_USERSPACE_LOADER
+	bool "Activate without calling userspace policy loader."
+	default n
+	depends on CCSECURITY
+	---help---
+	  Say Y here if you want to activate access control as soon as built-in
+	  policy was loaded. This option will be useful for systems where
+	  operations which can lead to the hijacking of the boot sequence are
+	  needed before loading the policy. For example, you can activate
+	  immediately after loading the fixed part of policy which will allow
+	  only operations needed for mounting a partition which contains the
+	  variant part of policy and verifying (e.g. running GPG check) and
+	  loading the variant part of policy. Since you can start using
+	  enforcing mode from the beginning, you can reduce the possibility of
+	  hijacking the boot sequence.
+
+	  If you say Y to both "Compile as loadable kernel module" option and
+	  "Activate without calling userspace policy loader." option, be sure
+	  to excplicitly load the kernel module from the userspace, for
+	  the kernel will not call /sbin/ccs-init when /sbin/init starts.
+
+config CCSECURITY_POLICY_LOADER
+	string "Location of userspace policy loader"
+	default "/sbin/ccs-init"
+	depends on CCSECURITY
+	depends on !CCSECURITY_OMIT_USERSPACE_LOADER
+	---help---
+	  This is the default pathname of policy loader which is called before
+	  activation. You can override this setting via CCS_loader= kernel
+	  command line option.
+
+config CCSECURITY_ACTIVATION_TRIGGER
+	string "Trigger for calling userspace policy loader"
+	default "/sbin/init"
+	depends on CCSECURITY
+	depends on !CCSECURITY_OMIT_USERSPACE_LOADER
+	---help---
+	  This is the default pathname of activation trigger.
+	  You can override this setting via CCS_trigger= kernel command line
+	  option. For example, if you pass init=/bin/systemd option, you may
+	  want to also pass CCS_trigger=/bin/systemd option.
+
+	  Say Y here if you want to enable only specific functionality in order
+	  to reduce object file size.
+
+config CCSECURITY_FILE_READDIR
+	bool "Enable readdir operation restriction."
+	default y
+	depends on CCSECURITY
+	---help---
+	  Say Y here if you want to enable analysis/restriction of opening
+	  directories for reading. Reading directory entries is a commonly
+	  requested operation and damage caused by not restricting it as MAC
+	  might be acceptable for you.
+
+config CCSECURITY_FILE_GETATTR
+	bool "Enable getattr operation restriction."
+	default y
+	depends on CCSECURITY
+	---help---
+	  Say Y here if you want to enable analysis/restriction of getting
+	  information of files. Getting file's information is a commonly
+	  requested operation and damage caused by not restricting it as MAC
+	  might be acceptable for you.
+
+config CCSECURITY_NETWORK
+	bool "Enable socket operation restriction."
+	default y
+	depends on NET
+	depends on CCSECURITY
+	---help---
+	  Say Y here if you want to enable analysis/restriction of INET and
+	  UNIX domain socket's operations.
+
+config CCSECURITY_CAPABILITY
+	bool "Enable non-POSIX capability operation restriction."
+	default y
+	depends on CCSECURITY
+	---help---
+	  Say Y here if you want to enable analysis/restriction of non-POSIX
+	  capabilities.
+
+config CCSECURITY_IPC
+	bool "Enable IPC operation restriction."
+	default y
+	depends on CCSECURITY
+	---help---
+	  Say Y here if you want to enable analysis/restriction of sending
+	  signals.
+
+config CCSECURITY_MISC
+	bool "Enable environment variable names restriction."
+	default y
+	depends on CCSECURITY
+	---help---
+	  Say Y here if you want to enable analysis/restriction of environment
+	  variable names passed upon program execution request.
+
+config CCSECURITY_TASK_EXECUTE_HANDLER
+	bool "Enable execute handler functionality."
+	default y
+	depends on CCSECURITY
+	---help---
+	  Say Y here if you want to enable execute handler functionality.
+
+config CCSECURITY_TASK_DOMAIN_TRANSITION
+	bool "Enable domain transition without program execution request."
+	default y
+	depends on CCSECURITY
+	---help---
+	  Say Y here if you want to enable domain transition without involving
+	  program execution request.
+
+config CCSECURITY_PORTRESERVE
+       bool "Enable local port reserver."
+       default y
+       depends on NET
+       depends on CCSECURITY
+       ---help---
+	 Say Y here if you want to implement
+	 /proc/sys/net/ipv4/ip_local_reserved_ports as a MAC policy.
+
+config CCSECURITY_NETWORK_RECVMSG
+	def_bool CCSECURITY_NETWORK
diff --git a/security/ccsecurity/Makefile b/security/ccsecurity/Makefile
new file mode 100644
index 0000000..79c3632
--- /dev/null
+++ b/security/ccsecurity/Makefile
@@ -0,0 +1,122 @@
+ccsecurity-objs := permission.o gc.o memory.o policy_io.o realpath.o
+
+ifeq ($(VERSION)$(PATCHLEVEL),24)
+
+ifdef CONFIG_CCSECURITY
+O_TARGET := ccsecurity.o
+ifdef CONFIG_CCSECURITY_LKM
+all_targets: load_policy.o
+obj-m := ccsecurity.o
+obj-y := $(ccsecurity-objs)
+else
+all_targets: ccsecurity.o
+obj-y := load_policy.o $(ccsecurity-objs)
+endif
+export-objs := load_policy.o
+endif
+include $(TOPDIR)/Rules.make
+
+policy/profile.conf:
+	@mkdir -p policy/
+	@echo Creating an empty policy/profile.conf
+	@touch $@
+
+policy/exception_policy.conf:
+	@mkdir -p policy/
+	@echo Creating a default policy/exception_policy.conf
+	@echo initialize_domain /sbin/modprobe from any >> $@
+	@echo initialize_domain /sbin/hotplug from any >> $@
+
+policy/domain_policy.conf:
+	@mkdir -p policy/
+	@echo Creating an empty policy/domain_policy.conf
+	@touch $@
+
+policy/manager.conf:
+	@mkdir -p policy/
+	@echo Creating an empty policy/manager.conf
+	@touch $@
+
+policy/stat.conf:
+	@mkdir -p policy/
+	@echo Creating an empty policy/stat.conf
+	@touch $@
+
+builtin-policy.h: policy/profile.conf policy/exception_policy.conf policy/domain_policy.conf policy/manager.conf policy/stat.conf
+	@echo Generating built-in policy for TOMOYO 1.8.x.
+	@echo "static char ccs_builtin_profile[] __initdata =" > $@.tmp
+	@sed -e 's/\\/\\\\/g' -e 's/\"/\\"/g' -e 's/\(.*\)/"\1\\n"/' < policy/profile.conf >> $@.tmp
+	@echo "\"\";" >> $@.tmp
+	@echo "static char ccs_builtin_exception_policy[] __initdata =" >> $@.tmp
+	@sed -e 's/\\/\\\\/g' -e 's/\"/\\"/g' -e 's/\(.*\)/"\1\\n"/' < policy/exception_policy.conf >> $@.tmp
+	@echo "\"\";" >> $@.tmp
+	@echo "static char ccs_builtin_domain_policy[] __initdata =" >> $@.tmp
+	@sed -e 's/\\/\\\\/g' -e 's/\"/\\"/g' -e 's/\(.*\)/"\1\\n"/' < policy/domain_policy.conf >> $@.tmp
+	@echo "\"\";" >> $@.tmp
+	@echo "static char ccs_builtin_manager[] __initdata =" >> $@.tmp
+	@sed -e 's/\\/\\\\/g' -e 's/\"/\\"/g' -e 's/\(.*\)/"\1\\n"/' < policy/manager.conf >> $@.tmp
+	@echo "\"\";" >> $@.tmp
+	@echo "static char ccs_builtin_stat[] __initdata =" >> $@.tmp
+	@sed -e 's/\\/\\\\/g' -e 's/\"/\\"/g' -e 's/\(.*\)/"\1\\n"/' < policy/stat.conf >> $@.tmp
+	@echo "\"\";" >> $@.tmp
+	@mv $@.tmp $@
+
+policy_io.o: builtin-policy.h
+
+else
+
+obj-y += load_policy.o
+ifdef CONFIG_CCSECURITY_LKM
+obj-m += ccsecurity.o
+else
+obj-y += ccsecurity.o
+endif
+
+$(obj)/policy/profile.conf:
+	@mkdir -p $(obj)/policy/
+	@echo Creating an empty policy/profile.conf
+	@touch $@
+
+$(obj)/policy/exception_policy.conf:
+	@mkdir -p $(obj)/policy/
+	@echo Creating a default policy/exception_policy.conf
+	@echo initialize_domain /sbin/modprobe from any >> $@
+	@echo initialize_domain /sbin/hotplug from any >> $@
+
+$(obj)/policy/domain_policy.conf:
+	@mkdir -p $(obj)/policy/
+	@echo Creating an empty policy/domain_policy.conf
+	@touch $@
+
+$(obj)/policy/manager.conf:
+	@mkdir -p $(obj)/policy/
+	@echo Creating an empty policy/manager.conf
+	@touch $@
+
+$(obj)/policy/stat.conf:
+	@mkdir -p $(obj)/policy/
+	@echo Creating an empty policy/stat.conf
+	@touch $@
+
+$(obj)/builtin-policy.h: $(obj)/policy/profile.conf $(obj)/policy/exception_policy.conf $(obj)/policy/domain_policy.conf $(obj)/policy/manager.conf $(obj)/policy/stat.conf
+	@echo Generating built-in policy for TOMOYO 1.8.x.
+	@echo "static char ccs_builtin_profile[] __initdata =" > $@.tmp
+	@sed -e 's/\\/\\\\/g' -e 's/\"/\\"/g' -e 's/\(.*\)/"\1\\n"/' < $(obj)/policy/profile.conf >> $@.tmp
+	@echo "\"\";" >> $@.tmp
+	@echo "static char ccs_builtin_exception_policy[] __initdata =" >> $@.tmp
+	@sed -e 's/\\/\\\\/g' -e 's/\"/\\"/g' -e 's/\(.*\)/"\1\\n"/' < $(obj)/policy/exception_policy.conf >> $@.tmp
+	@echo "\"\";" >> $@.tmp
+	@echo "static char ccs_builtin_domain_policy[] __initdata =" >> $@.tmp
+	@sed -e 's/\\/\\\\/g' -e 's/\"/\\"/g' -e 's/\(.*\)/"\1\\n"/' < $(obj)/policy/domain_policy.conf >> $@.tmp
+	@echo "\"\";" >> $@.tmp
+	@echo "static char ccs_builtin_manager[] __initdata =" >> $@.tmp
+	@sed -e 's/\\/\\\\/g' -e 's/\"/\\"/g' -e 's/\(.*\)/"\1\\n"/' < $(obj)/policy/manager.conf >> $@.tmp
+	@echo "\"\";" >> $@.tmp
+	@echo "static char ccs_builtin_stat[] __initdata =" >> $@.tmp
+	@sed -e 's/\\/\\\\/g' -e 's/\"/\\"/g' -e 's/\(.*\)/"\1\\n"/' < $(obj)/policy/stat.conf >> $@.tmp
+	@echo "\"\";" >> $@.tmp
+	@mv $@.tmp $@
+
+$(obj)/policy_io.o: $(obj)/builtin-policy.h
+
+endif
diff --git a/security/ccsecurity/gc.c b/security/ccsecurity/gc.c
new file mode 100644
index 0000000..0a578ab
--- /dev/null
+++ b/security/ccsecurity/gc.c
@@ -0,0 +1,1036 @@
+/*
+ * security/ccsecurity/gc.c
+ *
+ * Copyright (C) 2005-2012  NTT DATA CORPORATION
+ *
+ * Version: 1.8.4   2015/05/05
+ */
+
+#include "internal.h"
+
+/***** SECTION1: Constants definition *****/
+
+/* For compatibility with older kernels. */
+#ifndef for_each_process
+#define for_each_process for_each_task
+#endif
+
+/* The list for "struct ccs_io_buffer". */
+static LIST_HEAD(ccs_io_buffer_list);
+/* Lock for protecting ccs_io_buffer_list. */
+static DEFINE_SPINLOCK(ccs_io_buffer_list_lock);
+
+/***** SECTION2: Structure definition *****/
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
+
+/*
+ * Lock for syscall users.
+ *
+ * This lock is used for protecting single SRCU section for 2.6.18 and
+ * earlier kernels because they don't have SRCU support.
+ */
+struct ccs_lock_struct {
+	int counter_idx; /* Currently active index (0 or 1). */
+	int counter[2];  /* Current users. Protected by ccs_counter_lock. */
+};
+
+#endif
+
+/***** SECTION3: Prototype definition section *****/
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
+int ccs_lock(void);
+#endif
+void ccs_del_acl(struct list_head *element);
+void ccs_del_condition(struct list_head *element);
+void ccs_notify_gc(struct ccs_io_buffer *head, const bool is_register);
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
+void ccs_unlock(const int idx);
+#endif
+
+static bool ccs_domain_used_by_task(struct ccs_domain_info *domain);
+static bool ccs_name_used_by_io_buffer(const char *string, const size_t size);
+static bool ccs_struct_used_by_io_buffer(const struct list_head *element);
+static int ccs_gc_thread(void *unused);
+static void ccs_collect_acl(struct list_head *list);
+static void ccs_collect_entry(void);
+static void ccs_collect_member(const enum ccs_policy_id id,
+			       struct list_head *member_list);
+static void ccs_memory_free(const void *ptr, const enum ccs_policy_id type);
+static void ccs_put_name_union(struct ccs_name_union *ptr);
+static void ccs_put_number_union(struct ccs_number_union *ptr);
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
+static void ccs_synchronize_counter(void);
+#endif
+static void ccs_try_to_gc(const enum ccs_policy_id type,
+			  struct list_head *element);
+
+/***** SECTION4: Standalone functions section *****/
+
+/***** SECTION5: Variables definition section *****/
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
+
+/*
+ * Lock for syscall users.
+ *
+ * This lock is held for only protecting single SRCU section.
+ */
+struct srcu_struct ccs_ss;
+
+#else
+
+static struct ccs_lock_struct ccs_counter;
+/* Lock for protecting ccs_counter. */
+static DEFINE_SPINLOCK(ccs_counter_lock);
+
+#endif
+
+/***** SECTION6: Dependent functions section *****/
+
+/**
+ * ccs_memory_free - Free memory for elements.
+ *
+ * @ptr:  Pointer to allocated memory.
+ * @type: One of values in "enum ccs_policy_id".
+ *
+ * Returns nothing.
+ *
+ * Caller holds ccs_policy_lock mutex.
+ */
+static void ccs_memory_free(const void *ptr, const enum ccs_policy_id type)
+{
+	/* Size of an element. */
+	static const u8 e[CCS_MAX_POLICY] = {
+#ifdef CONFIG_CCSECURITY_PORTRESERVE
+		[CCS_ID_RESERVEDPORT] = sizeof(struct ccs_reserved),
+#endif
+		[CCS_ID_GROUP] = sizeof(struct ccs_group),
+#ifdef CONFIG_CCSECURITY_NETWORK
+		[CCS_ID_ADDRESS_GROUP] = sizeof(struct ccs_address_group),
+#endif
+		[CCS_ID_PATH_GROUP] = sizeof(struct ccs_path_group),
+		[CCS_ID_NUMBER_GROUP] = sizeof(struct ccs_number_group),
+		[CCS_ID_AGGREGATOR] = sizeof(struct ccs_aggregator),
+		[CCS_ID_TRANSITION_CONTROL]
+		= sizeof(struct ccs_transition_control),
+		[CCS_ID_MANAGER] = sizeof(struct ccs_manager),
+		/* [CCS_ID_CONDITION] = "struct ccs_condition"->size, */
+		/* [CCS_ID_NAME] = "struct ccs_name"->size, */
+		/* [CCS_ID_ACL] = a["struct ccs_acl_info"->type], */
+		[CCS_ID_DOMAIN] = sizeof(struct ccs_domain_info),
+	};
+	/* Size of a domain ACL element. */
+	static const u8 a[] = {
+		[CCS_TYPE_PATH_ACL] = sizeof(struct ccs_path_acl),
+		[CCS_TYPE_PATH2_ACL] = sizeof(struct ccs_path2_acl),
+		[CCS_TYPE_PATH_NUMBER_ACL]
+		= sizeof(struct ccs_path_number_acl),
+		[CCS_TYPE_MKDEV_ACL] = sizeof(struct ccs_mkdev_acl),
+		[CCS_TYPE_MOUNT_ACL] = sizeof(struct ccs_mount_acl),
+#ifdef CONFIG_CCSECURITY_NETWORK
+		[CCS_TYPE_INET_ACL] = sizeof(struct ccs_inet_acl),
+		[CCS_TYPE_UNIX_ACL] = sizeof(struct ccs_unix_acl),
+#endif
+#ifdef CONFIG_CCSECURITY_MISC
+		[CCS_TYPE_ENV_ACL] = sizeof(struct ccs_env_acl),
+#endif
+#ifdef CONFIG_CCSECURITY_CAPABILITY
+		[CCS_TYPE_CAPABILITY_ACL] = sizeof(struct ccs_capability_acl),
+#endif
+#ifdef CONFIG_CCSECURITY_IPC
+		[CCS_TYPE_SIGNAL_ACL] = sizeof(struct ccs_signal_acl),
+#endif
+#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER
+		[CCS_TYPE_AUTO_EXECUTE_HANDLER]
+		= sizeof(struct ccs_handler_acl),
+		[CCS_TYPE_DENIED_EXECUTE_HANDLER]
+		= sizeof(struct ccs_handler_acl),
+#endif
+#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION
+		[CCS_TYPE_AUTO_TASK_ACL] = sizeof(struct ccs_task_acl),
+		[CCS_TYPE_MANUAL_TASK_ACL] = sizeof(struct ccs_task_acl),
+#endif
+	};
+	size_t size;
+	if (type == CCS_ID_ACL)
+		size = a[container_of(ptr, typeof(struct ccs_acl_info),
+				      list)->type];
+	else if (type == CCS_ID_NAME)
+		size = container_of(ptr, typeof(struct ccs_name),
+				    head.list)->size;
+	else if (type == CCS_ID_CONDITION)
+		size = container_of(ptr, typeof(struct ccs_condition),
+				    head.list)->size;
+	else
+		size = e[type];
+	ccs_memory_used[CCS_MEMORY_POLICY] -= ccs_round2(size);
+	kfree(ptr);
+}
+
+/**
+ * ccs_put_name_union - Drop reference on "struct ccs_name_union".
+ *
+ * @ptr: Pointer to "struct ccs_name_union".
+ *
+ * Returns nothing.
+ */
+static void ccs_put_name_union(struct ccs_name_union *ptr)
+{
+	ccs_put_group(ptr->group);
+	ccs_put_name(ptr->filename);
+}
+
+/**
+ * ccs_put_number_union - Drop reference on "struct ccs_number_union".
+ *
+ * @ptr: Pointer to "struct ccs_number_union".
+ *
+ * Returns nothing.
+ */
+static void ccs_put_number_union(struct ccs_number_union *ptr)
+{
+	ccs_put_group(ptr->group);
+}
+
+/**
+ * ccs_struct_used_by_io_buffer - Check whether the list element is used by /proc/ccs/ users or not.
+ *
+ * @element: Pointer to "struct list_head".
+ *
+ * Returns true if @element is used by /proc/ccs/ users, false otherwise.
+ */
+static bool ccs_struct_used_by_io_buffer(const struct list_head *element)
+{
+	struct ccs_io_buffer *head;
+	bool in_use = false;
+	spin_lock(&ccs_io_buffer_list_lock);
+	list_for_each_entry(head, &ccs_io_buffer_list, list) {
+		head->users++;
+		spin_unlock(&ccs_io_buffer_list_lock);
+		mutex_lock(&head->io_sem);
+		if (head->r.domain == element || head->r.group == element ||
+		    head->r.acl == element || &head->w.domain->list == element)
+			in_use = true;
+		mutex_unlock(&head->io_sem);
+		spin_lock(&ccs_io_buffer_list_lock);
+		head->users--;
+		if (in_use)
+			break;
+	}
+	spin_unlock(&ccs_io_buffer_list_lock);
+	return in_use;
+}
+
+/**
+ * ccs_name_used_by_io_buffer - Check whether the string is used by /proc/ccs/ users or not.
+ *
+ * @string: String to check.
+ * @size:   Memory allocated for @string .
+ *
+ * Returns true if @string is used by /proc/ccs/ users, false otherwise.
+ */
+static bool ccs_name_used_by_io_buffer(const char *string, const size_t size)
+{
+	struct ccs_io_buffer *head;
+	bool in_use = false;
+	spin_lock(&ccs_io_buffer_list_lock);
+	list_for_each_entry(head, &ccs_io_buffer_list, list) {
+		int i;
+		head->users++;
+		spin_unlock(&ccs_io_buffer_list_lock);
+		mutex_lock(&head->io_sem);
+		for (i = 0; i < CCS_MAX_IO_READ_QUEUE; i++) {
+			const char *w = head->r.w[i];
+			if (w < string || w > string + size)
+				continue;
+			in_use = true;
+			break;
+		}
+		mutex_unlock(&head->io_sem);
+		spin_lock(&ccs_io_buffer_list_lock);
+		head->users--;
+		if (in_use)
+			break;
+	}
+	spin_unlock(&ccs_io_buffer_list_lock);
+	return in_use;
+}
+
+/**
+ * ccs_del_transition_control - Delete members in "struct ccs_transition_control".
+ *
+ * @element: Pointer to "struct list_head".
+ *
+ * Returns nothing.
+ */
+static inline void ccs_del_transition_control(struct list_head *element)
+{
+	struct ccs_transition_control *ptr =
+		container_of(element, typeof(*ptr), head.list);
+	ccs_put_name(ptr->domainname);
+	ccs_put_name(ptr->program);
+}
+
+/**
+ * ccs_del_aggregator - Delete members in "struct ccs_aggregator".
+ *
+ * @element: Pointer to "struct list_head".
+ *
+ * Returns nothing.
+ */
+static inline void ccs_del_aggregator(struct list_head *element)
+{
+	struct ccs_aggregator *ptr =
+		container_of(element, typeof(*ptr), head.list);
+	ccs_put_name(ptr->original_name);
+	ccs_put_name(ptr->aggregated_name);
+}
+
+/**
+ * ccs_del_manager - Delete members in "struct ccs_manager".
+ *
+ * @element: Pointer to "struct list_head".
+ *
+ * Returns nothing.
+ */
+static inline void ccs_del_manager(struct list_head *element)
+{
+	struct ccs_manager *ptr =
+		container_of(element, typeof(*ptr), head.list);
+	ccs_put_name(ptr->manager);
+}
+
+/**
+ * ccs_domain_used_by_task - Check whether the given pointer is referenced by a task.
+ *
+ * @domain: Pointer to "struct ccs_domain_info".
+ *
+ * Returns true if @domain is in use, false otherwise.
+ */
+static bool ccs_domain_used_by_task(struct ccs_domain_info *domain)
+{
+	bool in_use = false;
+	/*
+	 * Don't delete this domain if somebody is doing execve().
+	 *
+	 * Since ccs_finish_execve() first reverts ccs_domain_info and then
+	 * updates ccs_flags, we need smp_rmb() to make sure that GC first
+	 * checks ccs_flags and then checks ccs_domain_info.
+	 */
+#ifdef CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY
+	int idx;
+	rcu_read_lock();
+	for (idx = 0; idx < CCS_MAX_TASK_SECURITY_HASH; idx++) {
+		struct ccs_security *ptr;
+		struct list_head *list = &ccs_task_security_list[idx];
+		list_for_each_entry_rcu(ptr, list, list) {
+			if (!(ptr->ccs_flags & CCS_TASK_IS_IN_EXECVE)) {
+				smp_rmb(); /* Avoid out of order execution. */
+				if (ptr->ccs_domain_info != domain)
+					continue;
+			}
+			in_use = true;
+			goto out;
+		}
+	}
+out:
+	rcu_read_unlock();
+#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0)
+	struct task_struct *g;
+	struct task_struct *t;
+	ccs_tasklist_lock();
+	do_each_thread(g, t) {
+		if (!(t->ccs_flags & CCS_TASK_IS_IN_EXECVE)) {
+			smp_rmb(); /* Avoid out of order execution. */
+			if (t->ccs_domain_info != domain)
+				continue;
+		}
+		in_use = true;
+		goto out;
+	} while_each_thread(g, t);
+out:
+	ccs_tasklist_unlock();
+#else
+	struct task_struct *p;
+	ccs_tasklist_lock();
+	for_each_process(p) {
+		if (!(p->ccs_flags & CCS_TASK_IS_IN_EXECVE)) {
+			smp_rmb(); /* Avoid out of order execution. */
+			if (p->ccs_domain_info != domain)
+				continue;
+		}
+		in_use = true;
+		break;
+	}
+	ccs_tasklist_unlock();
+#endif
+	return in_use;
+}
+
+/**
+ * ccs_del_acl - Delete members in "struct ccs_acl_info".
+ *
+ * @element: Pointer to "struct list_head".
+ *
+ * Returns nothing.
+ */
+void ccs_del_acl(struct list_head *element)
+{
+	struct ccs_acl_info *acl = container_of(element, typeof(*acl), list);
+	ccs_put_condition(acl->cond);
+	switch (acl->type) {
+	case CCS_TYPE_PATH_ACL:
+		{
+			struct ccs_path_acl *entry =
+				container_of(acl, typeof(*entry), head);
+			ccs_put_name_union(&entry->name);
+		}
+		break;
+	case CCS_TYPE_PATH2_ACL:
+		{
+			struct ccs_path2_acl *entry =
+				container_of(acl, typeof(*entry), head);
+			ccs_put_name_union(&entry->name1);
+			ccs_put_name_union(&entry->name2);
+		}
+		break;
+	case CCS_TYPE_PATH_NUMBER_ACL:
+		{
+			struct ccs_path_number_acl *entry =
+				container_of(acl, typeof(*entry), head);
+			ccs_put_name_union(&entry->name);
+			ccs_put_number_union(&entry->number);
+		}
+		break;
+	case CCS_TYPE_MKDEV_ACL:
+		{
+			struct ccs_mkdev_acl *entry =
+				container_of(acl, typeof(*entry), head);
+			ccs_put_name_union(&entry->name);
+			ccs_put_number_union(&entry->mode);
+			ccs_put_number_union(&entry->major);
+			ccs_put_number_union(&entry->minor);
+		}
+		break;
+	case CCS_TYPE_MOUNT_ACL:
+		{
+			struct ccs_mount_acl *entry =
+				container_of(acl, typeof(*entry), head);
+			ccs_put_name_union(&entry->dev_name);
+			ccs_put_name_union(&entry->dir_name);
+			ccs_put_name_union(&entry->fs_type);
+			ccs_put_number_union(&entry->flags);
+		}
+		break;
+#ifdef CONFIG_CCSECURITY_NETWORK
+	case CCS_TYPE_INET_ACL:
+		{
+			struct ccs_inet_acl *entry =
+				container_of(acl, typeof(*entry), head);
+			ccs_put_group(entry->address.group);
+			ccs_put_number_union(&entry->port);
+		}
+		break;
+	case CCS_TYPE_UNIX_ACL:
+		{
+			struct ccs_unix_acl *entry =
+				container_of(acl, typeof(*entry), head);
+			ccs_put_name_union(&entry->name);
+		}
+		break;
+#endif
+#ifdef CONFIG_CCSECURITY_MISC
+	case CCS_TYPE_ENV_ACL:
+		{
+			struct ccs_env_acl *entry =
+				container_of(acl, typeof(*entry), head);
+			ccs_put_name(entry->env);
+		}
+		break;
+#endif
+#ifdef CONFIG_CCSECURITY_CAPABILITY
+	case CCS_TYPE_CAPABILITY_ACL:
+		{
+			/* Nothing to do. */
+		}
+		break;
+#endif
+#ifdef CONFIG_CCSECURITY_IPC
+	case CCS_TYPE_SIGNAL_ACL:
+		{
+			struct ccs_signal_acl *entry =
+				container_of(acl, typeof(*entry), head);
+			ccs_put_number_union(&entry->sig);
+			ccs_put_name(entry->domainname);
+		}
+		break;
+#endif
+#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER
+	case CCS_TYPE_AUTO_EXECUTE_HANDLER:
+	case CCS_TYPE_DENIED_EXECUTE_HANDLER:
+		{
+			struct ccs_handler_acl *entry =
+				container_of(acl, typeof(*entry), head);
+			ccs_put_name(entry->handler);
+		}
+		break;
+#endif
+#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION
+	case CCS_TYPE_AUTO_TASK_ACL:
+	case CCS_TYPE_MANUAL_TASK_ACL:
+		{
+			struct ccs_task_acl *entry =
+				container_of(acl, typeof(*entry), head);
+			ccs_put_name(entry->domainname);
+		}
+		break;
+#endif
+	}
+}
+
+/**
+ * ccs_del_domain - Delete members in "struct ccs_domain_info".
+ *
+ * @element: Pointer to "struct list_head".
+ *
+ * Returns nothing.
+ *
+ * Caller holds ccs_policy_lock mutex.
+ */
+static inline void ccs_del_domain(struct list_head *element)
+{
+	struct ccs_domain_info *domain =
+		container_of(element, typeof(*domain), list);
+	struct ccs_acl_info *acl;
+	struct ccs_acl_info *tmp;
+	/*
+	 * Since this domain is referenced from neither "struct ccs_io_buffer"
+	 * nor "struct task_struct", we can delete elements without checking
+	 * for is_deleted flag.
+	 */
+	list_for_each_entry_safe(acl, tmp, &domain->acl_info_list, list) {
+		ccs_del_acl(&acl->list);
+		ccs_memory_free(acl, CCS_ID_ACL);
+	}
+	ccs_put_name(domain->domainname);
+}
+
+/**
+ * ccs_del_path_group - Delete members in "struct ccs_path_group".
+ *
+ * @element: Pointer to "struct list_head".
+ *
+ * Returns nothing.
+ */
+static inline void ccs_del_path_group(struct list_head *element)
+{
+	struct ccs_path_group *member =
+		container_of(element, typeof(*member), head.list);
+	ccs_put_name(member->member_name);
+}
+
+/**
+ * ccs_del_group - Delete "struct ccs_group".
+ *
+ * @element: Pointer to "struct list_head".
+ *
+ * Returns nothing.
+ */
+static inline void ccs_del_group(struct list_head *element)
+{
+	struct ccs_group *group =
+		container_of(element, typeof(*group), head.list);
+	ccs_put_name(group->group_name);
+}
+
+/**
+ * ccs_del_address_group - Delete members in "struct ccs_address_group".
+ *
+ * @element: Pointer to "struct list_head".
+ *
+ * Returns nothing.
+ */
+static inline void ccs_del_address_group(struct list_head *element)
+{
+	/* Nothing to do. */
+}
+
+/**
+ * ccs_del_number_group - Delete members in "struct ccs_number_group".
+ *
+ * @element: Pointer to "struct list_head".
+ *
+ * Returns nothing.
+ */
+static inline void ccs_del_number_group(struct list_head *element)
+{
+	/* Nothing to do. */
+}
+
+/**
+ * ccs_del_reservedport - Delete members in "struct ccs_reserved".
+ *
+ * @element: Pointer to "struct list_head".
+ *
+ * Returns nothing.
+ */
+static inline void ccs_del_reservedport(struct list_head *element)
+{
+	/* Nothing to do. */
+}
+
+/**
+ * ccs_del_condition - Delete members in "struct ccs_condition".
+ *
+ * @element: Pointer to "struct list_head".
+ *
+ * Returns nothing.
+ */
+void ccs_del_condition(struct list_head *element)
+{
+	struct ccs_condition *cond = container_of(element, typeof(*cond),
+						  head.list);
+	const u16 condc = cond->condc;
+	const u16 numbers_count = cond->numbers_count;
+	const u16 names_count = cond->names_count;
+	const u16 argc = cond->argc;
+	const u16 envc = cond->envc;
+	unsigned int i;
+	const struct ccs_condition_element *condp
+		= (const struct ccs_condition_element *) (cond + 1);
+	struct ccs_number_union *numbers_p
+		= (struct ccs_number_union *) (condp + condc);
+	struct ccs_name_union *names_p
+		= (struct ccs_name_union *) (numbers_p + numbers_count);
+	const struct ccs_argv *argv
+		= (const struct ccs_argv *) (names_p + names_count);
+	const struct ccs_envp *envp
+		= (const struct ccs_envp *) (argv + argc);
+	for (i = 0; i < numbers_count; i++)
+		ccs_put_number_union(numbers_p++);
+	for (i = 0; i < names_count; i++)
+		ccs_put_name_union(names_p++);
+	for (i = 0; i < argc; argv++, i++)
+		ccs_put_name(argv->value);
+	for (i = 0; i < envc; envp++, i++) {
+		ccs_put_name(envp->name);
+		ccs_put_name(envp->value);
+	}
+	ccs_put_name(cond->transit);
+}
+
+/**
+ * ccs_del_name - Delete members in "struct ccs_name".
+ *
+ * @element: Pointer to "struct list_head".
+ *
+ * Returns nothing.
+ */
+static inline void ccs_del_name(struct list_head *element)
+{
+	/* Nothing to do. */
+}
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 19)
+
+/**
+ * ccs_lock - Alternative for srcu_read_lock().
+ *
+ * Returns index number which has to be passed to ccs_unlock().
+ */
+int ccs_lock(void)
+{
+	int idx;
+	spin_lock(&ccs_counter_lock);
+	idx = ccs_counter.counter_idx;
+	ccs_counter.counter[idx]++;
+	spin_unlock(&ccs_counter_lock);
+	return idx;
+}
+
+/**
+ * ccs_unlock - Alternative for srcu_read_unlock().
+ *
+ * @idx: Index number returned by ccs_lock().
+ *
+ * Returns nothing.
+ */
+void ccs_unlock(const int idx)
+{
+	spin_lock(&ccs_counter_lock);
+	ccs_counter.counter[idx]--;
+	spin_unlock(&ccs_counter_lock);
+}
+
+/**
+ * ccs_synchronize_counter - Alternative for synchronize_srcu().
+ *
+ * Returns nothing.
+ */
+static void ccs_synchronize_counter(void)
+{
+	int idx;
+	int v;
+	/*
+	 * Change currently active counter's index. Make it visible to other
+	 * threads by doing it with ccs_counter_lock held.
+	 * This function is called by garbage collector thread, and the garbage
+	 * collector thread is exclusive. Therefore, it is guaranteed that
+	 * SRCU grace period has expired when returning from this function.
+	 */
+	spin_lock(&ccs_counter_lock);
+	idx = ccs_counter.counter_idx;
+	ccs_counter.counter_idx ^= 1;
+	v = ccs_counter.counter[idx];
+	spin_unlock(&ccs_counter_lock);
+	/* Wait for previously active counter to become 0. */
+	while (v) {
+		ssleep(1);
+		spin_lock(&ccs_counter_lock);
+		v = ccs_counter.counter[idx];
+		spin_unlock(&ccs_counter_lock);
+	}
+}
+
+#endif
+
+/**
+ * ccs_try_to_gc - Try to kfree() an entry.
+ *
+ * @type:    One of values in "enum ccs_policy_id".
+ * @element: Pointer to "struct list_head".
+ *
+ * Returns nothing.
+ *
+ * Caller holds ccs_policy_lock mutex.
+ */
+static void ccs_try_to_gc(const enum ccs_policy_id type,
+			  struct list_head *element)
+{
+	/*
+	 * __list_del_entry() guarantees that the list element became no longer
+	 * reachable from the list which the element was originally on (e.g.
+	 * ccs_domain_list). Also, synchronize_srcu() guarantees that the list
+	 * element became no longer referenced by syscall users.
+	 */
+	__list_del_entry(element);
+	mutex_unlock(&ccs_policy_lock);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
+	synchronize_srcu(&ccs_ss);
+#else
+	ccs_synchronize_counter();
+#endif
+	/*
+	 * However, there are two users which may still be using the list
+	 * element. We need to defer until both users forget this element.
+	 *
+	 * Don't kfree() until "struct ccs_io_buffer"->r.{domain,group,acl} and
+	 * "struct ccs_io_buffer"->w.domain forget this element.
+	 */
+	if (ccs_struct_used_by_io_buffer(element))
+		goto reinject;
+	switch (type) {
+	case CCS_ID_TRANSITION_CONTROL:
+		ccs_del_transition_control(element);
+		break;
+	case CCS_ID_MANAGER:
+		ccs_del_manager(element);
+		break;
+	case CCS_ID_AGGREGATOR:
+		ccs_del_aggregator(element);
+		break;
+	case CCS_ID_GROUP:
+		ccs_del_group(element);
+		break;
+	case CCS_ID_PATH_GROUP:
+		ccs_del_path_group(element);
+		break;
+#ifdef CONFIG_CCSECURITY_NETWORK
+	case CCS_ID_ADDRESS_GROUP:
+		ccs_del_address_group(element);
+		break;
+#endif
+	case CCS_ID_NUMBER_GROUP:
+		ccs_del_number_group(element);
+		break;
+#ifdef CONFIG_CCSECURITY_PORTRESERVE
+	case CCS_ID_RESERVEDPORT:
+		ccs_del_reservedport(element);
+		break;
+#endif
+	case CCS_ID_CONDITION:
+		ccs_del_condition(element);
+		break;
+	case CCS_ID_NAME:
+		/*
+		 * Don't kfree() until all "struct ccs_io_buffer"->r.w[] forget
+		 * this element.
+		 */
+		if (ccs_name_used_by_io_buffer
+		    (container_of(element, typeof(struct ccs_name),
+				  head.list)->entry.name,
+		     container_of(element, typeof(struct ccs_name),
+				  head.list)->size))
+			goto reinject;
+		ccs_del_name(element);
+		break;
+	case CCS_ID_ACL:
+		ccs_del_acl(element);
+		break;
+	case CCS_ID_DOMAIN:
+		/*
+		 * Don't kfree() until all "struct task_struct" forget this
+		 * element.
+		 */
+		if (ccs_domain_used_by_task
+		    (container_of(element, typeof(struct ccs_domain_info),
+				  list)))
+			goto reinject;
+		break;
+	case CCS_MAX_POLICY:
+		break;
+	}
+	mutex_lock(&ccs_policy_lock);
+	if (type == CCS_ID_DOMAIN)
+		ccs_del_domain(element);
+	ccs_memory_free(element, type);
+	return;
+reinject:
+	/*
+	 * We can safely reinject this element here bacause
+	 * (1) Appending list elements and removing list elements are protected
+	 *     by ccs_policy_lock mutex.
+	 * (2) Only this function removes list elements and this function is
+	 *     exclusively executed by ccs_gc_mutex mutex.
+	 * are true.
+	 */
+	mutex_lock(&ccs_policy_lock);
+	list_add_rcu(element, element->prev);
+}
+
+/**
+ * ccs_collect_member - Delete elements with "struct ccs_acl_head".
+ *
+ * @id:          One of values in "enum ccs_policy_id".
+ * @member_list: Pointer to "struct list_head".
+ *
+ * Returns nothing.
+ *
+ * Caller holds ccs_policy_lock mutex.
+ */
+static void ccs_collect_member(const enum ccs_policy_id id,
+			       struct list_head *member_list)
+{
+	struct ccs_acl_head *member;
+	struct ccs_acl_head *tmp;
+	list_for_each_entry_safe(member, tmp, member_list, list) {
+		if (!member->is_deleted)
+			continue;
+		member->is_deleted = CCS_GC_IN_PROGRESS;
+		ccs_try_to_gc(id, &member->list);
+	}
+}
+
+/**
+ * ccs_collect_acl - Delete elements in "struct ccs_domain_info".
+ *
+ * @list: Pointer to "struct list_head".
+ *
+ * Returns nothing.
+ *
+ * Caller holds ccs_policy_lock mutex.
+ */
+static void ccs_collect_acl(struct list_head *list)
+{
+	struct ccs_acl_info *acl;
+	struct ccs_acl_info *tmp;
+	list_for_each_entry_safe(acl, tmp, list, list) {
+		if (!acl->is_deleted)
+			continue;
+		acl->is_deleted = CCS_GC_IN_PROGRESS;
+		ccs_try_to_gc(CCS_ID_ACL, &acl->list);
+	}
+}
+
+/**
+ * ccs_collect_entry - Try to kfree() deleted elements.
+ *
+ * Returns nothing.
+ */
+static void ccs_collect_entry(void)
+{
+	int i;
+	enum ccs_policy_id id;
+	struct ccs_policy_namespace *ns;
+	mutex_lock(&ccs_policy_lock);
+	{
+		struct ccs_domain_info *domain;
+		struct ccs_domain_info *tmp;
+		list_for_each_entry_safe(domain, tmp, &ccs_domain_list, list) {
+			ccs_collect_acl(&domain->acl_info_list);
+			if (!domain->is_deleted ||
+			    ccs_domain_used_by_task(domain))
+				continue;
+			ccs_try_to_gc(CCS_ID_DOMAIN, &domain->list);
+		}
+	}
+	list_for_each_entry(ns, &ccs_namespace_list, namespace_list) {
+		for (id = 0; id < CCS_MAX_POLICY; id++)
+			ccs_collect_member(id, &ns->policy_list[id]);
+		for (i = 0; i < CCS_MAX_ACL_GROUPS; i++)
+			ccs_collect_acl(&ns->acl_group[i]);
+	}
+	{
+		struct ccs_shared_acl_head *ptr;
+		struct ccs_shared_acl_head *tmp;
+		list_for_each_entry_safe(ptr, tmp, &ccs_condition_list, list) {
+			if (atomic_read(&ptr->users) > 0)
+				continue;
+			atomic_set(&ptr->users, CCS_GC_IN_PROGRESS);
+			ccs_try_to_gc(CCS_ID_CONDITION, &ptr->list);
+		}
+	}
+	list_for_each_entry(ns, &ccs_namespace_list, namespace_list) {
+		for (i = 0; i < CCS_MAX_GROUP; i++) {
+			struct list_head *list = &ns->group_list[i];
+			struct ccs_group *group;
+			struct ccs_group *tmp;
+			switch (i) {
+			case 0:
+				id = CCS_ID_PATH_GROUP;
+				break;
+			case 1:
+				id = CCS_ID_NUMBER_GROUP;
+				break;
+			default:
+#ifdef CONFIG_CCSECURITY_NETWORK
+				id = CCS_ID_ADDRESS_GROUP;
+#else
+				continue;
+#endif
+				break;
+			}
+			list_for_each_entry_safe(group, tmp, list, head.list) {
+				ccs_collect_member(id, &group->member_list);
+				if (!list_empty(&group->member_list) ||
+				    atomic_read(&group->head.users) > 0)
+					continue;
+				atomic_set(&group->head.users,
+					   CCS_GC_IN_PROGRESS);
+				ccs_try_to_gc(CCS_ID_GROUP, &group->head.list);
+			}
+		}
+	}
+	for (i = 0; i < CCS_MAX_HASH; i++) {
+		struct list_head *list = &ccs_name_list[i];
+		struct ccs_shared_acl_head *ptr;
+		struct ccs_shared_acl_head *tmp;
+		list_for_each_entry_safe(ptr, tmp, list, list) {
+			if (atomic_read(&ptr->users) > 0)
+				continue;
+			atomic_set(&ptr->users, CCS_GC_IN_PROGRESS);
+			ccs_try_to_gc(CCS_ID_NAME, &ptr->list);
+		}
+	}
+	mutex_unlock(&ccs_policy_lock);
+}
+
+/**
+ * ccs_gc_thread - Garbage collector thread function.
+ *
+ * @unused: Unused.
+ *
+ * Returns 0.
+ */
+static int ccs_gc_thread(void *unused)
+{
+	/* Garbage collector thread is exclusive. */
+	static DEFINE_MUTEX(ccs_gc_mutex);
+	if (!mutex_trylock(&ccs_gc_mutex))
+		goto out;
+#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 6)
+	/* daemonize() not needed. */
+#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
+	daemonize("GC for CCS");
+#else
+	daemonize();
+	reparent_to_init();
+#if defined(TASK_DEAD)
+	{
+		struct task_struct *task = current;
+		spin_lock_irq(&task->sighand->siglock);
+		siginitsetinv(&task->blocked, 0);
+		recalc_sigpending();
+		spin_unlock_irq(&task->sighand->siglock);
+	}
+#else
+	{
+		struct task_struct *task = current;
+		spin_lock_irq(&task->sigmask_lock);
+		siginitsetinv(&task->blocked, 0);
+		recalc_sigpending(task);
+		spin_unlock_irq(&task->sigmask_lock);
+	}
+#endif
+	snprintf(current->comm, sizeof(current->comm) - 1, "GC for CCS");
+#endif
+	ccs_collect_entry();
+	{
+		struct ccs_io_buffer *head;
+		struct ccs_io_buffer *tmp;
+		spin_lock(&ccs_io_buffer_list_lock);
+		list_for_each_entry_safe(head, tmp, &ccs_io_buffer_list,
+					 list) {
+			if (head->users)
+				continue;
+			list_del(&head->list);
+			kfree(head->read_buf);
+			kfree(head->write_buf);
+			kfree(head);
+		}
+		spin_unlock(&ccs_io_buffer_list_lock);
+	}
+	mutex_unlock(&ccs_gc_mutex);
+out:
+	/* This acts as do_exit(0). */
+	return 0;
+}
+
+/**
+ * ccs_notify_gc - Register/unregister /proc/ccs/ users.
+ *
+ * @head:        Pointer to "struct ccs_io_buffer".
+ * @is_register: True if register, false if unregister.
+ *
+ * Returns nothing.
+ */
+void ccs_notify_gc(struct ccs_io_buffer *head, const bool is_register)
+{
+	bool is_write = false;
+	spin_lock(&ccs_io_buffer_list_lock);
+	if (is_register) {
+		head->users = 1;
+		list_add(&head->list, &ccs_io_buffer_list);
+	} else {
+		is_write = head->write_buf != NULL;
+		if (!--head->users) {
+			list_del(&head->list);
+			kfree(head->read_buf);
+			kfree(head->write_buf);
+			kfree(head);
+		}
+	}
+	spin_unlock(&ccs_io_buffer_list_lock);
+	if (is_write) {
+#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 6)
+		struct task_struct *task = kthread_create(ccs_gc_thread, NULL,
+							  "GC for CCS");
+		if (!IS_ERR(task))
+			wake_up_process(task);
+#else
+		kernel_thread(ccs_gc_thread, NULL, 0);
+#endif
+	}
+}
diff --git a/security/ccsecurity/internal.h b/security/ccsecurity/internal.h
new file mode 100644
index 0000000..3f703f2
--- /dev/null
+++ b/security/ccsecurity/internal.h
@@ -0,0 +1,2090 @@
+/*
+ * security/ccsecurity/internal.h
+ *
+ * Copyright (C) 2005-2012  NTT DATA CORPORATION
+ *
+ * Version: 1.8.4   2015/05/05
+ */
+
+#ifndef _SECURITY_CCSECURITY_INTERNAL_H
+#define _SECURITY_CCSECURITY_INTERNAL_H
+
+#include <linux/version.h>
+#include <linux/types.h>
+#include <linux/kernel.h>
+#include <linux/string.h>
+#include <linux/mm.h>
+#include <linux/utime.h>
+#include <linux/file.h>
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 38)
+#include <linux/smp_lock.h>
+#endif
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/slab.h>
+#include <linux/highmem.h>
+#include <linux/poll.h>
+#include <linux/binfmts.h>
+#include <linux/delay.h>
+#include <linux/sched.h>
+#include <linux/dcache.h>
+#include <linux/mount.h>
+#include <linux/net.h>
+#include <linux/inet.h>
+#include <linux/in.h>
+#include <linux/in6.h>
+#include <linux/un.h>
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
+#include <linux/fs.h>
+#endif
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
+#include <linux/namei.h>
+#endif
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
+#include <linux/fs_struct.h>
+#endif
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
+#include <linux/namespace.h>
+#endif
+#include <linux/proc_fs.h>
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(RHEL_MAJOR)
+#include <linux/hash.h>
+#endif
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18) || (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33) && defined(CONFIG_SYSCTL_SYSCALL))
+#include <linux/sysctl.h>
+#endif
+#if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 6)
+#include <linux/kthread.h>
+#endif
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)
+#include <linux/magic.h>
+#endif
+#include <stdarg.h>
+#include <asm/uaccess.h>
+#include <net/sock.h>
+#include <net/af_unix.h>
+#include <net/ip.h>
+#include <net/ipv6.h>
+#include <net/udp.h>
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
+#define sk_family family
+#define sk_protocol protocol
+#define sk_type type
+#endif
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
+
+/* Structure for holding "struct vfsmount *" and "struct dentry *". */
+struct path {
+	struct vfsmount *mnt;
+	struct dentry *dentry;
+};
+
+#endif
+
+#ifndef __printf
+#define __printf(a,b) __attribute__((format(printf,a,b)))
+#endif
+#ifndef __packed
+#define __packed __attribute__((__packed__))
+#endif
+#ifndef bool
+#define bool _Bool
+#endif
+#ifndef false
+#define false 0
+#endif
+#ifndef true
+#define true 1
+#endif
+
+#ifndef __user
+#define __user
+#endif
+
+#ifndef current_uid
+#define current_uid()   (current->uid)
+#endif
+#ifndef current_gid
+#define current_gid()   (current->gid)
+#endif
+#ifndef current_euid
+#define current_euid()  (current->euid)
+#endif
+#ifndef current_egid
+#define current_egid()  (current->egid)
+#endif
+#ifndef current_suid
+#define current_suid()  (current->suid)
+#endif
+#ifndef current_sgid
+#define current_sgid()  (current->sgid)
+#endif
+#ifndef current_fsuid
+#define current_fsuid() (current->fsuid)
+#endif
+#ifndef current_fsgid
+#define current_fsgid() (current->fsgid)
+#endif
+
+#ifndef DEFINE_SPINLOCK
+#define DEFINE_SPINLOCK(x) spinlock_t x = SPIN_LOCK_UNLOCKED
+#endif
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16)
+#define mutex semaphore
+#define mutex_init(mutex) init_MUTEX(mutex)
+#define mutex_unlock(mutex) up(mutex)
+#define mutex_lock(mutex) down(mutex)
+#define mutex_lock_interruptible(mutex) down_interruptible(mutex)
+#define mutex_trylock(mutex) (!down_trylock(mutex))
+#define DEFINE_MUTEX(mutexname) DECLARE_MUTEX(mutexname)
+#endif
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 15)
+#define MS_UNBINDABLE	(1<<17)	/* change to unbindable */
+#define MS_PRIVATE	(1<<18)	/* change to private */
+#define MS_SLAVE	(1<<19)	/* change to slave */
+#define MS_SHARED	(1<<20)	/* change to shared */
+#endif
+
+#ifndef container_of
+#define container_of(ptr, type, member) ({				\
+			const typeof(((type *)0)->member) *__mptr = (ptr); \
+			(type *)((char *)__mptr - offsetof(type, member)); })
+#endif
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
+#define smp_read_barrier_depends smp_rmb
+#endif
+
+#ifndef ACCESS_ONCE
+#define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
+#endif
+
+#ifndef rcu_dereference
+#define rcu_dereference(p)     ({					\
+			typeof(p) _________p1 = ACCESS_ONCE(p);		\
+			smp_read_barrier_depends(); /* see RCU */	\
+			(_________p1);					\
+		})
+#endif
+
+#ifndef rcu_assign_pointer
+#define rcu_assign_pointer(p, v)			\
+	({						\
+		if (!__builtin_constant_p(v) ||		\
+		    ((v) != NULL))			\
+			smp_wmb(); /* see RCU */	\
+		(p) = (v);				\
+	})
+#endif
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 9, 0)
+#define f_vfsmnt f_path.mnt
+#endif
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 14)
+
+/**
+ * kzalloc() - Allocate memory. The memory is set to zero.
+ *
+ * @size:  Size to allocate.
+ * @flags: GFP flags.
+ *
+ * Returns pointer to allocated memory on success, NULL otherwise.
+ *
+ * This is for compatibility with older kernels.
+ *
+ * Since several distributions backported kzalloc(), I define it as a macro
+ * rather than an inlined function in order to avoid multiple definition error.
+ */
+#define kzalloc(size, flags) ({					\
+			void *ret = kmalloc((size), (flags));	\
+			if (ret)				\
+				memset(ret, 0, (size));		\
+			ret; })
+
+#endif
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
+
+/**
+ * path_put - Drop reference on "struct path".
+ *
+ * @path: Pointer to "struct path".
+ *
+ * Returns nothing.
+ *
+ * This is for compatibility with older kernels.
+ */
+static inline void path_put(struct path *path)
+{
+	dput(path->dentry);
+	mntput(path->mnt);
+}
+
+#endif
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
+
+/**
+ * __list_add_rcu - Insert a new entry between two known consecutive entries.
+ *
+ * @new:  Pointer to "struct list_head".
+ * @prev: Pointer to "struct list_head".
+ * @next: Pointer to "struct list_head".
+ *
+ * Returns nothing.
+ *
+ * This is for compatibility with older kernels.
+ */
+static inline void __list_add_rcu(struct list_head *new,
+				  struct list_head *prev,
+				  struct list_head *next)
+{
+	new->next = next;
+	new->prev = prev;
+	rcu_assign_pointer(prev->next, new);
+	next->prev = new;
+}
+
+/**
+ * list_add_tail_rcu - Add a new entry to rcu-protected list.
+ *
+ * @new:  Pointer to "struct list_head".
+ * @head: Pointer to "struct list_head".
+ *
+ * Returns nothing.
+ *
+ * This is for compatibility with older kernels.
+ */
+static inline void list_add_tail_rcu(struct list_head *new,
+				     struct list_head *head)
+{
+	__list_add_rcu(new, head->prev, head);
+}
+
+/**
+ * list_add_rcu - Add a new entry to rcu-protected list.
+ *
+ * @new:  Pointer to "struct list_head".
+ * @head: Pointer to "struct list_head".
+ *
+ * Returns nothing.
+ *
+ * This is for compatibility with older kernels.
+ */
+static inline void list_add_rcu(struct list_head *new, struct list_head *head)
+{
+	__list_add_rcu(new, head, head->next);
+}
+
+#endif
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 38)
+
+/**
+ * __list_del_entry - Deletes entry from list without re-initialization.
+ *
+ * @entry: Pointer to "struct list_head".
+ *
+ * Returns nothing.
+ *
+ * This is for compatibility with older kernels.
+ */
+static inline void __list_del_entry(struct list_head *entry)
+{
+	__list_del(entry->prev, entry->next);
+}
+
+#endif
+
+#ifndef list_for_each_entry_safe
+
+/**
+ * list_for_each_entry_safe - Iterate over list of given type safe against removal of list entry.
+ *
+ * @pos:    The "type *" to use as a loop cursor.
+ * @n:      Another "type *" to use as temporary storage.
+ * @head:   Pointer to "struct list_head".
+ * @member: The name of the list_struct within the struct.
+ *
+ * This is for compatibility with older kernels.
+ */
+#define list_for_each_entry_safe(pos, n, head, member)                  \
+	for (pos = list_entry((head)->next, typeof(*pos), member),      \
+		     n = list_entry(pos->member.next, typeof(*pos), member); \
+	     &pos->member != (head);					\
+	     pos = n, n = list_entry(n->member.next, typeof(*n), member))
+
+#endif
+
+#ifndef srcu_dereference
+
+/**
+ * srcu_dereference - Fetch SRCU-protected pointer with checking.
+ *
+ * @p:  The pointer to read, prior to dereferencing.
+ * @ss: Pointer to "struct srcu_struct".
+ *
+ * Returns @p.
+ *
+ * This is for compatibility with older kernels.
+ */
+#define srcu_dereference(p, ss) rcu_dereference(p)
+
+#endif
+
+#ifndef list_for_each_entry_srcu
+
+/**
+ * list_for_each_entry_srcu - Iterate over rcu list of given type.
+ *
+ * @pos:    The type * to use as a loop cursor.
+ * @head:   The head for your list.
+ * @member: The name of the list_struct within the struct.
+ * @ss:     Pointer to "struct srcu_struct".
+ *
+ * As of 2.6.36, this macro is not provided because only TOMOYO wants it.
+ */
+#define list_for_each_entry_srcu(pos, head, member, ss)		      \
+	for (pos = list_entry(srcu_dereference((head)->next, ss),     \
+			      typeof(*pos), member);		      \
+	     prefetch(pos->member.next), &pos->member != (head);      \
+	     pos = list_entry(srcu_dereference(pos->member.next, ss), \
+			      typeof(*pos), member))
+
+#endif
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 30) || (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) && LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 9))
+
+#if LINUX_VERSION_CODE == KERNEL_VERSION(2, 4, 21)
+#undef ssleep
+#endif
+
+#ifndef ssleep
+
+/**
+ * ssleep - Sleep for specified seconds.
+ *
+ * @secs: Seconds to sleep.
+ *
+ * Returns nothing.
+ *
+ * This is for compatibility with older kernels.
+ *
+ * Since several distributions backported ssleep(), I define it as a macro
+ * rather than an inlined function in order to avoid multiple definition error.
+ */
+#define ssleep(secs) {						\
+		set_current_state(TASK_UNINTERRUPTIBLE);	\
+		schedule_timeout((HZ * secs) + 1);		\
+	}
+
+#endif
+
+#endif
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 5, 0)
+
+/**
+ * from_kuid - Convert kuid_t to uid_t.
+ *
+ * @ns:  Unused.
+ * @uid: kuid_t value.
+ *
+ * Returns uid seen from init's user namespace.
+ */
+#define from_kuid(ns, uid) (uid)
+
+/**
+ * from_kgid - Convert kgid_t to gid_t.
+ *
+ * @ns:  Unused.
+ * @gid: kgid_t value.
+ *
+ * Returns gid seen from init's user namespace.
+ */
+#define from_kgid(ns, gid) (gid)
+
+/**
+ * uid_eq - Check whether the uids are equals or not.
+ *
+ * @left: Uid seen from current user namespace.
+ * @right: Uid seen from current user namespace.
+ *
+ * Returns true if uid is root in init's user namespace, false otherwise.
+ */
+#define uid_eq(left, right) ((left) == (right))
+#define GLOBAL_ROOT_UID 0
+
+#endif
+
+/*
+ * TOMOYO specific part start.
+ */
+
+#include <linux/ccsecurity.h>
+
+/* Enumeration definition for internal use. */
+
+/* Index numbers for Access Controls. */
+enum ccs_acl_entry_type_index {
+	CCS_TYPE_PATH_ACL,
+	CCS_TYPE_PATH2_ACL,
+	CCS_TYPE_PATH_NUMBER_ACL,
+	CCS_TYPE_MKDEV_ACL,
+	CCS_TYPE_MOUNT_ACL,
+#ifdef CONFIG_CCSECURITY_MISC
+	CCS_TYPE_ENV_ACL,
+#endif
+#ifdef CONFIG_CCSECURITY_CAPABILITY
+	CCS_TYPE_CAPABILITY_ACL,
+#endif
+#ifdef CONFIG_CCSECURITY_NETWORK
+	CCS_TYPE_INET_ACL,
+	CCS_TYPE_UNIX_ACL,
+#endif
+#ifdef CONFIG_CCSECURITY_IPC
+	CCS_TYPE_SIGNAL_ACL,
+#endif
+#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER
+	CCS_TYPE_AUTO_EXECUTE_HANDLER,
+	CCS_TYPE_DENIED_EXECUTE_HANDLER,
+#endif
+#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION
+	CCS_TYPE_AUTO_TASK_ACL,
+	CCS_TYPE_MANUAL_TASK_ACL,
+#endif
+};
+
+/* Index numbers for "struct ccs_condition". */
+enum ccs_conditions_index {
+	CCS_TASK_UID,             /* current_uid()   */
+	CCS_TASK_EUID,            /* current_euid()  */
+	CCS_TASK_SUID,            /* current_suid()  */
+	CCS_TASK_FSUID,           /* current_fsuid() */
+	CCS_TASK_GID,             /* current_gid()   */
+	CCS_TASK_EGID,            /* current_egid()  */
+	CCS_TASK_SGID,            /* current_sgid()  */
+	CCS_TASK_FSGID,           /* current_fsgid() */
+	CCS_TASK_PID,             /* sys_getpid()   */
+	CCS_TASK_PPID,            /* sys_getppid()  */
+	CCS_EXEC_ARGC,            /* "struct linux_binprm *"->argc */
+	CCS_EXEC_ENVC,            /* "struct linux_binprm *"->envc */
+	CCS_TYPE_IS_SOCKET,       /* S_IFSOCK */
+	CCS_TYPE_IS_SYMLINK,      /* S_IFLNK */
+	CCS_TYPE_IS_FILE,         /* S_IFREG */
+	CCS_TYPE_IS_BLOCK_DEV,    /* S_IFBLK */
+	CCS_TYPE_IS_DIRECTORY,    /* S_IFDIR */
+	CCS_TYPE_IS_CHAR_DEV,     /* S_IFCHR */
+	CCS_TYPE_IS_FIFO,         /* S_IFIFO */
+	CCS_MODE_SETUID,          /* S_ISUID */
+	CCS_MODE_SETGID,          /* S_ISGID */
+	CCS_MODE_STICKY,          /* S_ISVTX */
+	CCS_MODE_OWNER_READ,      /* S_IRUSR */
+	CCS_MODE_OWNER_WRITE,     /* S_IWUSR */
+	CCS_MODE_OWNER_EXECUTE,   /* S_IXUSR */
+	CCS_MODE_GROUP_READ,      /* S_IRGRP */
+	CCS_MODE_GROUP_WRITE,     /* S_IWGRP */
+	CCS_MODE_GROUP_EXECUTE,   /* S_IXGRP */
+	CCS_MODE_OTHERS_READ,     /* S_IROTH */
+	CCS_MODE_OTHERS_WRITE,    /* S_IWOTH */
+	CCS_MODE_OTHERS_EXECUTE,  /* S_IXOTH */
+	CCS_TASK_TYPE,            /* ((u8) task->ccs_flags) &
+				     CCS_TASK_IS_EXECUTE_HANDLER */
+	CCS_TASK_EXECUTE_HANDLER, /* CCS_TASK_IS_EXECUTE_HANDLER */
+	CCS_EXEC_REALPATH,
+	CCS_SYMLINK_TARGET,
+	CCS_PATH1_UID,
+	CCS_PATH1_GID,
+	CCS_PATH1_INO,
+	CCS_PATH1_MAJOR,
+	CCS_PATH1_MINOR,
+	CCS_PATH1_PERM,
+	CCS_PATH1_TYPE,
+	CCS_PATH1_DEV_MAJOR,
+	CCS_PATH1_DEV_MINOR,
+	CCS_PATH2_UID,
+	CCS_PATH2_GID,
+	CCS_PATH2_INO,
+	CCS_PATH2_MAJOR,
+	CCS_PATH2_MINOR,
+	CCS_PATH2_PERM,
+	CCS_PATH2_TYPE,
+	CCS_PATH2_DEV_MAJOR,
+	CCS_PATH2_DEV_MINOR,
+	CCS_PATH1_PARENT_UID,
+	CCS_PATH1_PARENT_GID,
+	CCS_PATH1_PARENT_INO,
+	CCS_PATH1_PARENT_PERM,
+	CCS_PATH2_PARENT_UID,
+	CCS_PATH2_PARENT_GID,
+	CCS_PATH2_PARENT_INO,
+	CCS_PATH2_PARENT_PERM,
+	CCS_MAX_CONDITION_KEYWORD,
+	CCS_NUMBER_UNION,
+	CCS_NAME_UNION,
+	CCS_ARGV_ENTRY,
+	CCS_ENVP_ENTRY,
+};
+
+/* Index numbers for domain's attributes. */
+enum ccs_domain_info_flags_index {
+	/* Quota warnning flag.   */
+	CCS_DIF_QUOTA_WARNED,
+	/*
+	 * This domain was unable to create a new domain at
+	 * ccs_find_next_domain() because the name of the domain to be created
+	 * was too long or it could not allocate memory.
+	 * More than one process continued execve() without domain transition.
+	 */
+	CCS_DIF_TRANSITION_FAILED,
+	CCS_MAX_DOMAIN_INFO_FLAGS
+};
+
+/* Index numbers for audit type. */
+enum ccs_grant_log {
+	/* Follow profile's configuration. */
+	CCS_GRANTLOG_AUTO,
+	/* Do not generate grant log. */
+	CCS_GRANTLOG_NO,
+	/* Generate grant_log. */
+	CCS_GRANTLOG_YES,
+};
+
+/* Index numbers for group entries. */
+enum ccs_group_id {
+	CCS_PATH_GROUP,
+	CCS_NUMBER_GROUP,
+#ifdef CONFIG_CCSECURITY_NETWORK
+	CCS_ADDRESS_GROUP,
+#endif
+	CCS_MAX_GROUP
+};
+
+/* Index numbers for category of functionality. */
+enum ccs_mac_category_index {
+	CCS_MAC_CATEGORY_FILE,
+#ifdef CONFIG_CCSECURITY_NETWORK
+	CCS_MAC_CATEGORY_NETWORK,
+#endif
+#ifdef CONFIG_CCSECURITY_MISC
+	CCS_MAC_CATEGORY_MISC,
+#endif
+#ifdef CONFIG_CCSECURITY_IPC
+	CCS_MAC_CATEGORY_IPC,
+#endif
+#ifdef CONFIG_CCSECURITY_CAPABILITY
+	CCS_MAC_CATEGORY_CAPABILITY,
+#endif
+	CCS_MAX_MAC_CATEGORY_INDEX
+};
+
+/* Index numbers for functionality. */
+enum ccs_mac_index {
+	CCS_MAC_FILE_EXECUTE,
+	CCS_MAC_FILE_OPEN,
+	CCS_MAC_FILE_CREATE,
+	CCS_MAC_FILE_UNLINK,
+#ifdef CONFIG_CCSECURITY_FILE_GETATTR
+	CCS_MAC_FILE_GETATTR,
+#endif
+	CCS_MAC_FILE_MKDIR,
+	CCS_MAC_FILE_RMDIR,
+	CCS_MAC_FILE_MKFIFO,
+	CCS_MAC_FILE_MKSOCK,
+	CCS_MAC_FILE_TRUNCATE,
+	CCS_MAC_FILE_SYMLINK,
+	CCS_MAC_FILE_MKBLOCK,
+	CCS_MAC_FILE_MKCHAR,
+	CCS_MAC_FILE_LINK,
+	CCS_MAC_FILE_RENAME,
+	CCS_MAC_FILE_CHMOD,
+	CCS_MAC_FILE_CHOWN,
+	CCS_MAC_FILE_CHGRP,
+	CCS_MAC_FILE_IOCTL,
+	CCS_MAC_FILE_CHROOT,
+	CCS_MAC_FILE_MOUNT,
+	CCS_MAC_FILE_UMOUNT,
+	CCS_MAC_FILE_PIVOT_ROOT,
+#ifdef CONFIG_CCSECURITY_NETWORK
+	CCS_MAC_NETWORK_INET_STREAM_BIND,
+	CCS_MAC_NETWORK_INET_STREAM_LISTEN,
+	CCS_MAC_NETWORK_INET_STREAM_CONNECT,
+	CCS_MAC_NETWORK_INET_STREAM_ACCEPT,
+	CCS_MAC_NETWORK_INET_DGRAM_BIND,
+	CCS_MAC_NETWORK_INET_DGRAM_SEND,
+#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG
+	CCS_MAC_NETWORK_INET_DGRAM_RECV,
+#endif
+	CCS_MAC_NETWORK_INET_RAW_BIND,
+	CCS_MAC_NETWORK_INET_RAW_SEND,
+#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG
+	CCS_MAC_NETWORK_INET_RAW_RECV,
+#endif
+	CCS_MAC_NETWORK_UNIX_STREAM_BIND,
+	CCS_MAC_NETWORK_UNIX_STREAM_LISTEN,
+	CCS_MAC_NETWORK_UNIX_STREAM_CONNECT,
+	CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT,
+	CCS_MAC_NETWORK_UNIX_DGRAM_BIND,
+	CCS_MAC_NETWORK_UNIX_DGRAM_SEND,
+#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG
+	CCS_MAC_NETWORK_UNIX_DGRAM_RECV,
+#endif
+	CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND,
+	CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN,
+	CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT,
+	CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT,
+#endif
+#ifdef CONFIG_CCSECURITY_MISC
+	CCS_MAC_ENVIRON,
+#endif
+#ifdef CONFIG_CCSECURITY_IPC
+	CCS_MAC_SIGNAL,
+#endif
+#ifdef CONFIG_CCSECURITY_CAPABILITY
+	CCS_MAC_CAPABILITY_USE_ROUTE_SOCKET,
+	CCS_MAC_CAPABILITY_USE_PACKET_SOCKET,
+	CCS_MAC_CAPABILITY_SYS_REBOOT,
+	CCS_MAC_CAPABILITY_SYS_VHANGUP,
+	CCS_MAC_CAPABILITY_SYS_SETTIME,
+	CCS_MAC_CAPABILITY_SYS_NICE,
+	CCS_MAC_CAPABILITY_SYS_SETHOSTNAME,
+	CCS_MAC_CAPABILITY_USE_KERNEL_MODULE,
+	CCS_MAC_CAPABILITY_SYS_KEXEC_LOAD,
+	CCS_MAC_CAPABILITY_SYS_PTRACE,
+#endif
+	CCS_MAX_MAC_INDEX
+};
+
+/* Index numbers for /proc/ccs/stat interface. */
+enum ccs_memory_stat_type {
+	CCS_MEMORY_POLICY,
+	CCS_MEMORY_AUDIT,
+	CCS_MEMORY_QUERY,
+	CCS_MAX_MEMORY_STAT
+};
+
+/* Index numbers for access controls with one pathname and three numbers. */
+enum ccs_mkdev_acl_index {
+	CCS_TYPE_MKBLOCK,
+	CCS_TYPE_MKCHAR,
+	CCS_MAX_MKDEV_OPERATION
+};
+
+/* Index numbers for operation mode. */
+enum ccs_mode_value {
+	CCS_CONFIG_DISABLED,
+	CCS_CONFIG_LEARNING,
+	CCS_CONFIG_PERMISSIVE,
+	CCS_CONFIG_ENFORCING,
+	CCS_CONFIG_MAX_MODE,
+	CCS_CONFIG_WANT_REJECT_LOG =  64,
+	CCS_CONFIG_WANT_GRANT_LOG  = 128,
+	CCS_CONFIG_USE_DEFAULT     = 255,
+};
+
+/* Index numbers for socket operations. */
+enum ccs_network_acl_index {
+	CCS_NETWORK_BIND,    /* bind() operation. */
+	CCS_NETWORK_LISTEN,  /* listen() operation. */
+	CCS_NETWORK_CONNECT, /* connect() operation. */
+	CCS_NETWORK_ACCEPT,  /* accept() operation. */
+	CCS_NETWORK_SEND,    /* send() operation. */
+#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG
+	CCS_NETWORK_RECV,    /* recv() operation. */
+#endif
+	CCS_MAX_NETWORK_OPERATION
+};
+
+/* Index numbers for access controls with two pathnames. */
+enum ccs_path2_acl_index {
+	CCS_TYPE_LINK,
+	CCS_TYPE_RENAME,
+	CCS_TYPE_PIVOT_ROOT,
+	CCS_MAX_PATH2_OPERATION
+};
+
+/* Index numbers for access controls with one pathname. */
+enum ccs_path_acl_index {
+	CCS_TYPE_EXECUTE,
+	CCS_TYPE_READ,
+	CCS_TYPE_WRITE,
+	CCS_TYPE_APPEND,
+	CCS_TYPE_UNLINK,
+#ifdef CONFIG_CCSECURITY_FILE_GETATTR
+	CCS_TYPE_GETATTR,
+#endif
+	CCS_TYPE_RMDIR,
+	CCS_TYPE_TRUNCATE,
+	CCS_TYPE_SYMLINK,
+	CCS_TYPE_CHROOT,
+	CCS_TYPE_UMOUNT,
+	CCS_MAX_PATH_OPERATION
+};
+
+/* Index numbers for access controls with one pathname and one number. */
+enum ccs_path_number_acl_index {
+	CCS_TYPE_CREATE,
+	CCS_TYPE_MKDIR,
+	CCS_TYPE_MKFIFO,
+	CCS_TYPE_MKSOCK,
+	CCS_TYPE_IOCTL,
+	CCS_TYPE_CHMOD,
+	CCS_TYPE_CHOWN,
+	CCS_TYPE_CHGRP,
+	CCS_MAX_PATH_NUMBER_OPERATION
+};
+
+/* Index numbers for stat(). */
+enum ccs_path_stat_index {
+	/* Do not change this order. */
+	CCS_PATH1,
+	CCS_PATH1_PARENT,
+	CCS_PATH2,
+	CCS_PATH2_PARENT,
+	CCS_MAX_PATH_STAT
+};
+
+/* Index numbers for entry type. */
+enum ccs_policy_id {
+#ifdef CONFIG_CCSECURITY_PORTRESERVE
+	CCS_ID_RESERVEDPORT,
+#endif
+	CCS_ID_GROUP,
+#ifdef CONFIG_CCSECURITY_NETWORK
+	CCS_ID_ADDRESS_GROUP,
+#endif
+	CCS_ID_PATH_GROUP,
+	CCS_ID_NUMBER_GROUP,
+	CCS_ID_AGGREGATOR,
+	CCS_ID_TRANSITION_CONTROL,
+	CCS_ID_MANAGER,
+	CCS_ID_CONDITION,
+	CCS_ID_NAME,
+	CCS_ID_ACL,
+	CCS_ID_DOMAIN,
+	CCS_MAX_POLICY
+};
+
+/* Index numbers for /proc/ccs/stat interface. */
+enum ccs_policy_stat_type {
+	/* Do not change this order. */
+	CCS_STAT_POLICY_UPDATES,
+	CCS_STAT_POLICY_LEARNING,   /* == CCS_CONFIG_LEARNING */
+	CCS_STAT_POLICY_PERMISSIVE, /* == CCS_CONFIG_PERMISSIVE */
+	CCS_STAT_POLICY_ENFORCING,  /* == CCS_CONFIG_ENFORCING */
+	CCS_MAX_POLICY_STAT
+};
+
+/* Index numbers for profile's PREFERENCE values. */
+enum ccs_pref_index {
+	CCS_PREF_MAX_AUDIT_LOG,
+	CCS_PREF_MAX_LEARNING_ENTRY,
+	CCS_PREF_ENFORCING_PENALTY,
+	CCS_MAX_PREF
+};
+
+/* Index numbers for /proc/ccs/ interfaces. */
+enum ccs_proc_interface_index {
+	CCS_DOMAIN_POLICY,
+	CCS_EXCEPTION_POLICY,
+	CCS_PROCESS_STATUS,
+	CCS_STAT,
+	CCS_AUDIT,
+	CCS_VERSION,
+	CCS_PROFILE,
+	CCS_QUERY,
+	CCS_MANAGER,
+#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER
+	CCS_EXECUTE_HANDLER,
+#endif
+};
+
+/* Index numbers for special mount operations. */
+enum ccs_special_mount {
+	CCS_MOUNT_BIND,            /* mount --bind /source /dest   */
+	CCS_MOUNT_MOVE,            /* mount --move /old /new       */
+	CCS_MOUNT_REMOUNT,         /* mount -o remount /dir        */
+	CCS_MOUNT_MAKE_UNBINDABLE, /* mount --make-unbindable /dir */
+	CCS_MOUNT_MAKE_PRIVATE,    /* mount --make-private /dir    */
+	CCS_MOUNT_MAKE_SLAVE,      /* mount --make-slave /dir      */
+	CCS_MOUNT_MAKE_SHARED,     /* mount --make-shared /dir     */
+	CCS_MAX_SPECIAL_MOUNT
+};
+
+/* Index numbers for domain transition control keywords. */
+enum ccs_transition_type {
+	/* Do not change this order, */
+	CCS_TRANSITION_CONTROL_NO_RESET,
+	CCS_TRANSITION_CONTROL_RESET,
+	CCS_TRANSITION_CONTROL_NO_INITIALIZE,
+	CCS_TRANSITION_CONTROL_INITIALIZE,
+	CCS_TRANSITION_CONTROL_NO_KEEP,
+	CCS_TRANSITION_CONTROL_KEEP,
+	CCS_MAX_TRANSITION_TYPE
+};
+
+/* Index numbers for type of numeric values. */
+enum ccs_value_type {
+	CCS_VALUE_TYPE_INVALID,
+	CCS_VALUE_TYPE_DECIMAL,
+	CCS_VALUE_TYPE_OCTAL,
+	CCS_VALUE_TYPE_HEXADECIMAL,
+};
+
+/* Constants definition for internal use. */
+
+/*
+ * TOMOYO uses this hash only when appending a string into the string table.
+ * Frequency of appending strings is very low. So we don't need large (e.g.
+ * 64k) hash size. 256 will be sufficient.
+ */
+#define CCS_HASH_BITS 8
+#define CCS_MAX_HASH (1u << CCS_HASH_BITS)
+
+/*
+ * TOMOYO checks only SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, SOCK_SEQPACKET.
+ * Therefore, we don't need SOCK_MAX.
+ */
+#define CCS_SOCK_MAX 6
+
+/* Size of temporary buffer for execve() operation. */
+#define CCS_EXEC_TMPSIZE     4096
+
+/* Garbage collector is trying to kfree() this element. */
+#define CCS_GC_IN_PROGRESS -1
+
+/* Profile number is an integer between 0 and 255. */
+#define CCS_MAX_PROFILES 256
+
+/* Group number is an integer between 0 and 255. */
+#define CCS_MAX_ACL_GROUPS 256
+
+/* Current thread is doing open(O_RDONLY | O_TRUNC) ? */
+#define CCS_OPEN_FOR_READ_TRUNCATE        1
+/* Current thread is doing open(3) ? */
+#define CCS_OPEN_FOR_IOCTL_ONLY           2
+/* Current thread is doing do_execve() ? */
+#define CCS_TASK_IS_IN_EXECVE             4
+/* Current thread is running as an execute handler program? */
+#define CCS_TASK_IS_EXECUTE_HANDLER       8
+/* Current thread is allowed to modify policy via /proc/ccs/ interface? */
+#define CCS_TASK_IS_MANAGER              16
+
+/*
+ * Retry this request. Returned by ccs_supervisor() if policy violation has
+ * occurred in enforcing mode and the userspace daemon decided to retry.
+ *
+ * We must choose a positive value in order to distinguish "granted" (which is
+ * 0) and "rejected" (which is a negative value) and "retry".
+ */
+#define CCS_RETRY_REQUEST 1
+
+/* Ignore gfp flags which are not supported. */
+#ifndef __GFP_HIGHIO
+#define __GFP_HIGHIO 0
+#endif
+#ifndef __GFP_NOWARN
+#define __GFP_NOWARN 0
+#endif
+#ifndef __GFP_NORETRY
+#define __GFP_NORETRY 0
+#endif
+#ifndef __GFP_NOMEMALLOC
+#define __GFP_NOMEMALLOC 0
+#endif
+
+/* The gfp flags used by TOMOYO. */
+#define CCS_GFP_FLAGS (__GFP_WAIT | __GFP_IO | __GFP_HIGHIO | __GFP_NOWARN | \
+		       __GFP_NORETRY | __GFP_NOMEMALLOC)
+
+/* Size of read buffer for /proc/ccs/ interface. */
+#define CCS_MAX_IO_READ_QUEUE 64
+
+/* Structure definition for internal use. */
+
+/* Common header for holding ACL entries. */
+struct ccs_acl_head {
+	struct list_head list;
+	s8 is_deleted; /* true or false or CCS_GC_IN_PROGRESS */
+} __packed;
+
+/* Common header for shared entries. */
+struct ccs_shared_acl_head {
+	struct list_head list;
+	atomic_t users;
+} __packed;
+
+/* Common header for individual entries. */
+struct ccs_acl_info {
+	struct list_head list;
+	struct ccs_condition *cond; /* Maybe NULL. */
+	s8 is_deleted; /* true or false or CCS_GC_IN_PROGRESS */
+	u8 type; /* One of values in "enum ccs_acl_entry_type_index". */
+	u16 perm;
+} __packed;
+
+/* Structure for holding a word. */
+struct ccs_name_union {
+	/* Either @filename or @group is NULL. */
+	const struct ccs_path_info *filename;
+	struct ccs_group *group;
+};
+
+/* Structure for holding a number. */
+struct ccs_number_union {
+	unsigned long values[2];
+	struct ccs_group *group; /* Maybe NULL. */
+	/* One of values in "enum ccs_value_type". */
+	u8 value_type[2];
+};
+
+/* Structure for holding an IP address. */
+struct ccs_ipaddr_union {
+	struct in6_addr ip[2]; /* Big endian. */
+	struct ccs_group *group; /* Pointer to address group. */
+	bool is_ipv6; /* Valid only if @group == NULL. */
+};
+
+/* Structure for "path_group"/"number_group"/"address_group" directive. */
+struct ccs_group {
+	struct ccs_shared_acl_head head;
+	/* Name of group (without leading '@'). */
+	const struct ccs_path_info *group_name;
+	/*
+	 * List of "struct ccs_path_group" or "struct ccs_number_group" or
+	 * "struct ccs_address_group".
+	 */
+	struct list_head member_list;
+};
+
+/* Structure for "path_group" directive. */
+struct ccs_path_group {
+	struct ccs_acl_head head;
+	const struct ccs_path_info *member_name;
+};
+
+/* Structure for "number_group" directive. */
+struct ccs_number_group {
+	struct ccs_acl_head head;
+	struct ccs_number_union number;
+};
+
+/* Structure for "address_group" directive. */
+struct ccs_address_group {
+	struct ccs_acl_head head;
+	/* Structure for holding an IP address. */
+	struct ccs_ipaddr_union address;
+};
+
+/* Subset of "struct stat". Used by conditional ACL and audit logs. */
+struct ccs_mini_stat {
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
+	kuid_t uid;
+	kgid_t gid;
+#else
+	uid_t uid;
+	gid_t gid;
+#endif
+	ino_t ino;
+	umode_t mode;
+	dev_t dev;
+	dev_t rdev;
+};
+
+/* Structure for dumping argv[] and envp[] of "struct linux_binprm". */
+struct ccs_page_dump {
+	struct page *page;    /* Previously dumped page. */
+	char *data;           /* Contents of "page". Size is PAGE_SIZE. */
+};
+
+/* Structure for attribute checks in addition to pathname checks. */
+struct ccs_obj_info {
+	/* True if ccs_get_attributes() was already called, false otherwise. */
+	bool validate_done;
+	/* True if @stat[] is valid. */
+	bool stat_valid[CCS_MAX_PATH_STAT];
+	/* First pathname. Initialized with { NULL, NULL } if no path. */
+	struct path path1;
+	/* Second pathname. Initialized with { NULL, NULL } if no path. */
+	struct path path2;
+	/*
+	 * Information on @path1, @path1's parent directory, @path2, @path2's
+	 * parent directory.
+	 */
+	struct ccs_mini_stat stat[CCS_MAX_PATH_STAT];
+	/*
+	 * Content of symbolic link to be created. NULL for operations other
+	 * than symlink().
+	 */
+	struct ccs_path_info *symlink_target;
+};
+
+/* Structure for entries which follows "struct ccs_condition". */
+struct ccs_condition_element {
+	/*
+	 * Left hand operand. A "struct ccs_argv" for CCS_ARGV_ENTRY, a
+	 * "struct ccs_envp" for CCS_ENVP_ENTRY is attached to the tail
+	 * of the array of this struct.
+	 */
+	u8 left;
+	/*
+	 * Right hand operand. A "struct ccs_number_union" for
+	 * CCS_NUMBER_UNION, a "struct ccs_name_union" for CCS_NAME_UNION is
+	 * attached to the tail of the array of this struct.
+	 */
+	u8 right;
+	/* Equation operator. True if equals or overlaps, false otherwise. */
+	bool equals;
+};
+
+/* Structure for optional arguments. */
+struct ccs_condition {
+	struct ccs_shared_acl_head head;
+	u32 size; /* Memory size allocated for this entry. */
+	u16 condc; /* Number of conditions in this struct. */
+	u16 numbers_count; /* Number of "struct ccs_number_union values". */
+	u16 names_count; /* Number of "struct ccs_name_union names". */
+	u16 argc; /* Number of "struct ccs_argv". */
+	u16 envc; /* Number of "struct ccs_envp". */
+	u8 grant_log; /* One of values in "enum ccs_grant_log". */
+	bool exec_transit; /* True if transit is for "file execute". */
+	const struct ccs_path_info *transit; /* Maybe NULL. */
+	/*
+	 * struct ccs_condition_element condition[condc];
+	 * struct ccs_number_union values[numbers_count];
+	 * struct ccs_name_union names[names_count];
+	 * struct ccs_argv argv[argc];
+	 * struct ccs_envp envp[envc];
+	 */
+};
+
+struct ccs_execve;
+struct ccs_policy_namespace;
+
+/* Structure for request info. */
+struct ccs_request_info {
+	/*
+	 * For holding parameters specific to operations which deal files.
+	 * NULL if not dealing files.
+	 */
+	struct ccs_obj_info *obj;
+	/*
+	 * For holding parameters specific to execve() request.
+	 * NULL if not dealing do_execve().
+	 */
+	struct ccs_execve *ee;
+	/*
+	 * For holding parameters.
+	 * Pointers in this union are not NULL except path->matched_path.
+	 */
+	union {
+		struct {
+			const struct ccs_path_info *filename;
+			/*
+			 * For using wildcards at ccs_find_next_domain().
+			 *
+			 * The matched_acl cannot be used because it may refer
+			 * a "struct ccs_path_acl" with ->is_group == true.
+			 * We want to use exact "struct ccs_path_info" rather
+			 * than "struct ccs_path_acl".
+			 */
+			const struct ccs_path_info *matched_path;
+			/* One of values in "enum ccs_path_acl_index". */
+			u8 operation;
+		} path;
+		struct {
+			const struct ccs_path_info *filename1;
+			const struct ccs_path_info *filename2;
+			/* One of values in "enum ccs_path2_acl_index". */
+			u8 operation;
+		} path2;
+		struct {
+			const struct ccs_path_info *filename;
+			unsigned int mode;
+			unsigned int major;
+			unsigned int minor;
+			/* One of values in "enum ccs_mkdev_acl_index". */
+			u8 operation;
+		} mkdev;
+		struct {
+			const struct ccs_path_info *filename;
+			unsigned long number;
+			/*
+			 * One of values in "enum ccs_path_number_acl_index".
+			 */
+			u8 operation;
+		} path_number;
+#ifdef CONFIG_CCSECURITY_NETWORK
+		struct {
+			const u32 *address; /* Big endian. */
+			u16 port; /* Host endian. */
+			/* One of values smaller than CCS_SOCK_MAX. */
+			u8 protocol;
+			/* One of values in "enum ccs_network_acl_index". */
+			u8 operation;
+			bool is_ipv6;
+		} inet_network;
+		struct {
+			const struct ccs_path_info *address;
+			/* One of values smaller than CCS_SOCK_MAX. */
+			u8 protocol;
+			/* One of values in "enum ccs_network_acl_index". */
+			u8 operation;
+		} unix_network;
+#endif
+#ifdef CONFIG_CCSECURITY_MISC
+		struct {
+			const struct ccs_path_info *name;
+		} environ;
+#endif
+#ifdef CONFIG_CCSECURITY_CAPABILITY
+		struct {
+			/* One of values in "enum ccs_capability_acl_index". */
+			u8 operation;
+		} capability;
+#endif
+#ifdef CONFIG_CCSECURITY_IPC
+		struct {
+			const char *dest_pattern;
+			int sig;
+		} signal;
+#endif
+		struct {
+			const struct ccs_path_info *type;
+			const struct ccs_path_info *dir;
+			const struct ccs_path_info *dev;
+			unsigned long flags;
+			int need_dev;
+		} mount;
+#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION
+		struct {
+			const struct ccs_path_info *domainname;
+		} task;
+#endif
+	} param;
+	/*
+	 * For updating current->ccs_domain_info at ccs_update_task_domain().
+	 * Initialized to NULL at ccs_init_request_info().
+	 * Matching "struct ccs_acl_info" is copied if access request was
+	 * granted. Re-initialized to NULL at ccs_update_task_domain().
+	 */
+	struct ccs_acl_info *matched_acl;
+	u8 param_type; /* One of values in "enum ccs_acl_entry_type_index". */
+	bool granted; /* True if granted, false otherwise. */
+	/* True if current thread should not be carried sleep penalty. */
+	bool dont_sleep_on_enforce_error;
+	/*
+	 * For counting number of retries made for this request.
+	 * This counter is incremented whenever ccs_supervisor() returned
+	 * CCS_RETRY_REQUEST.
+	 */
+	u8 retry;
+	/*
+	 * For holding profile number used for this request.
+	 * One of values between 0 and CCS_MAX_PROFILES - 1.
+	 */
+	u8 profile;
+	/*
+	 * For holding operation mode used for this request.
+	 * One of CCS_CONFIG_DISABLED, CCS_CONFIG_LEARNING,
+	 * CCS_CONFIG_PERMISSIVE, CCS_CONFIG_ENFORCING.
+	 */
+	u8 mode;
+	/*
+	 * For holding operation index used for this request.
+	 * Used by ccs_init_request_info() / ccs_get_mode() /
+	 * ccs_write_log(). One of values in "enum ccs_mac_index".
+	 */
+	u8 type;
+};
+
+/* Structure for holding a token. */
+struct ccs_path_info {
+	const char *name;
+	u32 hash;          /* = full_name_hash(name, strlen(name)) */
+	u16 total_len;     /* = strlen(name)                       */
+	u16 const_len;     /* = ccs_const_part_length(name)        */
+	bool is_dir;       /* = ccs_strendswith(name, "/")         */
+	bool is_patterned; /* = const_len < total_len              */
+};
+
+/* Structure for execve() operation. */
+struct ccs_execve {
+	struct ccs_request_info r;
+	struct ccs_obj_info obj;
+	struct linux_binprm *bprm;
+	struct ccs_domain_info *previous_domain;
+	const struct ccs_path_info *transition;
+	/* For execute_handler */
+	const struct ccs_path_info *handler;
+	char *handler_path; /* = kstrdup(handler->name, CCS_GFP_FLAGS) */
+	/* For dumping argv[] and envp[]. */
+	struct ccs_page_dump dump;
+	/* For temporary use. */
+	char *tmp; /* Size is CCS_EXEC_TMPSIZE bytes */
+};
+
+/* Structure for domain information. */
+struct ccs_domain_info {
+	struct list_head list;
+	struct list_head acl_info_list;
+	/* Name of this domain. Never NULL.          */
+	const struct ccs_path_info *domainname;
+	/* Namespace for this domain. Never NULL. */
+	struct ccs_policy_namespace *ns;
+	/* Group numbers to use. */
+	unsigned long group[CCS_MAX_ACL_GROUPS / BITS_PER_LONG];
+	u8 profile;        /* Profile number to use. */
+	bool is_deleted;   /* Delete flag.           */
+	bool flags[CCS_MAX_DOMAIN_INFO_FLAGS];
+};
+
+/*
+ * Structure for "reset_domain"/"no_reset_domain"/"initialize_domain"/
+ * "no_initialize_domain"/"keep_domain"/"no_keep_domain" keyword.
+ */
+struct ccs_transition_control {
+	struct ccs_acl_head head;
+	u8 type; /* One of values in "enum ccs_transition_type" */
+	bool is_last_name; /* True if the domainname is ccs_last_word(). */
+	const struct ccs_path_info *domainname; /* Maybe NULL */
+	const struct ccs_path_info *program;    /* Maybe NULL */
+};
+
+/* Structure for "aggregator" keyword. */
+struct ccs_aggregator {
+	struct ccs_acl_head head;
+	const struct ccs_path_info *original_name;
+	const struct ccs_path_info *aggregated_name;
+};
+
+/* Structure for "deny_autobind" keyword. */
+struct ccs_reserved {
+	struct ccs_acl_head head;
+	struct ccs_number_union port;
+};
+
+/* Structure for policy manager. */
+struct ccs_manager {
+	struct ccs_acl_head head;
+	/* A path to program or a domainname. */
+	const struct ccs_path_info *manager;
+};
+
+/* Structure for argv[]. */
+struct ccs_argv {
+	unsigned long index;
+	const struct ccs_path_info *value;
+	bool is_not;
+};
+
+/* Structure for envp[]. */
+struct ccs_envp {
+	const struct ccs_path_info *name;
+	const struct ccs_path_info *value;
+	bool is_not;
+};
+
+/*
+ * Structure for "task auto_execute_handler" and "task denied_execute_handler"
+ * directive.
+ *
+ * If "task auto_execute_handler" directive exists and the current process is
+ * not an execute handler, all execve() requests are replaced by execve()
+ * requests of a program specified by "task auto_execute_handler" directive.
+ * If the current process is an execute handler, "task auto_execute_handler"
+ * and "task denied_execute_handler" directives are ignored.
+ * The program specified by "task execute_handler" validates execve()
+ * parameters and executes the original execve() requests if appropriate.
+ *
+ * "task denied_execute_handler" directive is used only when execve() request
+ * was rejected in enforcing mode (i.e. CONFIG::file::execute={ mode=enforcing
+ * }). The program specified by "task denied_execute_handler" does whatever it
+ * wants to do (e.g. silently terminate, change firewall settings, redirect the
+ * user to honey pot etc.).
+ */
+struct ccs_handler_acl {
+	struct ccs_acl_info head;       /* type = CCS_TYPE_*_EXECUTE_HANDLER */
+	const struct ccs_path_info *handler; /* Pointer to single pathname.  */
+};
+
+/*
+ * Structure for "task auto_domain_transition" and
+ * "task manual_domain_transition" directive.
+ */
+struct ccs_task_acl {
+	struct ccs_acl_info head; /* type = CCS_TYPE_*_TASK_ACL */
+	/* Pointer to domainname. */
+	const struct ccs_path_info *domainname;
+};
+
+/*
+ * Structure for "file execute", "file read", "file write", "file append",
+ * "file unlink", "file getattr", "file rmdir", "file truncate",
+ * "file symlink", "file chroot" and "file unmount" directive.
+ */
+struct ccs_path_acl {
+	struct ccs_acl_info head; /* type = CCS_TYPE_PATH_ACL */
+	struct ccs_name_union name;
+};
+
+/*
+ * Structure for "file rename", "file link" and "file pivot_root" directive.
+ */
+struct ccs_path2_acl {
+	struct ccs_acl_info head; /* type = CCS_TYPE_PATH2_ACL */
+	struct ccs_name_union name1;
+	struct ccs_name_union name2;
+};
+
+/*
+ * Structure for "file create", "file mkdir", "file mkfifo", "file mksock",
+ * "file ioctl", "file chmod", "file chown" and "file chgrp" directive.
+ */
+struct ccs_path_number_acl {
+	struct ccs_acl_info head; /* type = CCS_TYPE_PATH_NUMBER_ACL */
+	struct ccs_name_union name;
+	struct ccs_number_union number;
+};
+
+/* Structure for "file mkblock" and "file mkchar" directive. */
+struct ccs_mkdev_acl {
+	struct ccs_acl_info head; /* type = CCS_TYPE_MKDEV_ACL */
+	struct ccs_name_union name;
+	struct ccs_number_union mode;
+	struct ccs_number_union major;
+	struct ccs_number_union minor;
+};
+
+/* Structure for "file mount" directive. */
+struct ccs_mount_acl {
+	struct ccs_acl_info head; /* type = CCS_TYPE_MOUNT_ACL */
+	struct ccs_name_union dev_name;
+	struct ccs_name_union dir_name;
+	struct ccs_name_union fs_type;
+	struct ccs_number_union flags;
+};
+
+/* Structure for "misc env" directive in domain policy. */
+struct ccs_env_acl {
+	struct ccs_acl_info head;        /* type = CCS_TYPE_ENV_ACL  */
+	const struct ccs_path_info *env; /* environment variable */
+};
+
+/* Structure for "capability" directive. */
+struct ccs_capability_acl {
+	struct ccs_acl_info head; /* type = CCS_TYPE_CAPABILITY_ACL */
+	u8 operation; /* One of values in "enum ccs_capability_acl_index". */
+};
+
+/* Structure for "ipc signal" directive. */
+struct ccs_signal_acl {
+	struct ccs_acl_info head; /* type = CCS_TYPE_SIGNAL_ACL */
+	struct ccs_number_union sig;
+	/* Pointer to destination pattern. */
+	const struct ccs_path_info *domainname;
+};
+
+/* Structure for "network inet" directive. */
+struct ccs_inet_acl {
+	struct ccs_acl_info head; /* type = CCS_TYPE_INET_ACL */
+	u8 protocol;
+	struct ccs_ipaddr_union address;
+	struct ccs_number_union port;
+};
+
+/* Structure for "network unix" directive. */
+struct ccs_unix_acl {
+	struct ccs_acl_info head; /* type = CCS_TYPE_UNIX_ACL */
+	u8 protocol;
+	struct ccs_name_union name;
+};
+
+/* Structure for holding string data. */
+struct ccs_name {
+	struct ccs_shared_acl_head head;
+	int size; /* Memory size allocated for this entry. */
+	struct ccs_path_info entry;
+};
+
+/* Structure for holding a line from /proc/ccs/ interface. */
+struct ccs_acl_param {
+	char *data; /* Unprocessed data. */
+	struct list_head *list; /* List to add or remove. */
+	struct ccs_policy_namespace *ns; /* Namespace to use. */
+	bool is_delete; /* True if it is a delete request. */
+	union ccs_acl_union {
+		struct ccs_acl_info acl_info;
+		struct ccs_handler_acl handler_acl;
+		struct ccs_task_acl task_acl;
+		struct ccs_path_acl path_acl;
+		struct ccs_path2_acl path2_acl;
+		struct ccs_path_number_acl path_number_acl;
+		struct ccs_mkdev_acl mkdev_acl;
+		struct ccs_mount_acl mount_acl;
+		struct ccs_env_acl env_acl;
+		struct ccs_capability_acl capability_acl;
+		struct ccs_signal_acl signal_acl;
+		struct ccs_inet_acl inet_acl;
+		struct ccs_unix_acl unix_acl;
+		/**/
+		struct ccs_acl_head acl_head;
+		struct ccs_transition_control transition_control;
+		struct ccs_aggregator aggregator;
+		struct ccs_reserved reserved;
+		struct ccs_manager manager;
+		struct ccs_path_group path_group;
+		struct ccs_number_group number_group;
+		struct ccs_address_group address_group;
+	} e;
+};
+
+/* Structure for reading/writing policy via /proc/ccs/ interfaces. */
+struct ccs_io_buffer {
+	/* Exclusive lock for this structure.   */
+	struct mutex io_sem;
+	char __user *read_user_buf;
+	size_t read_user_buf_avail;
+	struct {
+		struct list_head *ns;
+		struct list_head *domain;
+		struct list_head *group;
+		struct list_head *acl;
+		size_t avail;
+		unsigned int step;
+		unsigned int query_index;
+		u16 index;
+		u16 cond_index;
+		u8 acl_group_index;
+		u8 cond_step;
+		u8 bit;
+		u8 w_pos;
+		bool eof;
+		bool print_this_domain_only;
+		bool print_transition_related_only;
+		bool print_cond_part;
+		const char *w[CCS_MAX_IO_READ_QUEUE];
+	} r;
+	struct {
+		struct ccs_policy_namespace *ns;
+		struct ccs_domain_info *domain;
+		size_t avail;
+		bool is_delete;
+	} w;
+	/* Buffer for reading.                  */
+	char *read_buf;
+	/* Size of read buffer.                 */
+	size_t readbuf_size;
+	/* Buffer for writing.                  */
+	char *write_buf;
+	/* Size of write buffer.                */
+	size_t writebuf_size;
+	/* Type of interface. */
+	enum ccs_proc_interface_index type;
+	/* Users counter protected by ccs_io_buffer_list_lock. */
+	u8 users;
+	/* List for telling GC not to kfree() elements. */
+	struct list_head list;
+};
+
+/* Structure for /proc/ccs/profile interface. */
+struct ccs_profile {
+	const struct ccs_path_info *comment;
+	u8 default_config;
+	u8 config[CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX];
+	unsigned int pref[CCS_MAX_PREF];
+};
+
+/* Structure for representing YYYY/MM/DD hh/mm/ss. */
+struct ccs_time {
+	u16 year;
+	u8 month;
+	u8 day;
+	u8 hour;
+	u8 min;
+	u8 sec;
+};
+
+/* Structure for policy namespace. */
+struct ccs_policy_namespace {
+	/* Profile table. Memory is allocated as needed. */
+	struct ccs_profile *profile_ptr[CCS_MAX_PROFILES];
+	/* List of "struct ccs_group". */
+	struct list_head group_list[CCS_MAX_GROUP];
+	/* List of policy. */
+	struct list_head policy_list[CCS_MAX_POLICY];
+	/* The global ACL referred by "use_group" keyword. */
+	struct list_head acl_group[CCS_MAX_ACL_GROUPS];
+	/* List for connecting to ccs_namespace_list list. */
+	struct list_head namespace_list;
+	/* Profile version. Currently only 20150505 is supported. */
+	unsigned int profile_version;
+	/* Name of this namespace (e.g. "<kernel>", "</usr/sbin/httpd>" ). */
+	const char *name;
+};
+
+/* Prototype definition for "struct ccsecurity_operations". */
+
+void __init ccs_permission_init(void);
+void __init ccs_mm_init(void);
+
+/* Prototype definition for internal use. */
+
+bool ccs_dump_page(struct linux_binprm *bprm, unsigned long pos,
+		   struct ccs_page_dump *dump);
+bool ccs_memory_ok(const void *ptr, const unsigned int size);
+char *ccs_encode(const char *str);
+char *ccs_encode2(const char *str, int str_len);
+char *ccs_realpath(const struct path *path);
+const char *ccs_get_exe(void);
+const struct ccs_path_info *ccs_get_name(const char *name);
+int ccs_audit_log(struct ccs_request_info *r);
+int ccs_check_acl(struct ccs_request_info *r);
+int ccs_init_request_info(struct ccs_request_info *r, const u8 index);
+struct ccs_domain_info *ccs_assign_domain(const char *domainname,
+					  const bool transit);
+u8 ccs_get_config(const u8 profile, const u8 index);
+void *ccs_commit_ok(void *data, const unsigned int size);
+void ccs_del_acl(struct list_head *element);
+void ccs_del_condition(struct list_head *element);
+void ccs_fill_path_info(struct ccs_path_info *ptr);
+void ccs_get_attributes(struct ccs_obj_info *obj);
+void ccs_notify_gc(struct ccs_io_buffer *head, const bool is_register);
+void ccs_transition_failed(const char *domainname);
+void ccs_warn_oom(const char *function);
+void ccs_write_log(struct ccs_request_info *r, const char *fmt, ...)
+	__printf(2, 3);
+
+/* Variable definition for internal use. */
+
+extern bool ccs_policy_loaded;
+extern const char * const ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS];
+extern const u8 ccs_c2mac[CCS_MAX_CAPABILITY_INDEX];
+extern const u8 ccs_pn2mac[CCS_MAX_PATH_NUMBER_OPERATION];
+extern const u8 ccs_pnnn2mac[CCS_MAX_MKDEV_OPERATION];
+extern const u8 ccs_pp2mac[CCS_MAX_PATH2_OPERATION];
+extern struct ccs_domain_info ccs_kernel_domain;
+extern struct list_head ccs_condition_list;
+extern struct list_head ccs_domain_list;
+extern struct list_head ccs_name_list[CCS_MAX_HASH];
+extern struct list_head ccs_namespace_list;
+extern struct mutex ccs_policy_lock;
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
+extern struct srcu_struct ccs_ss;
+#endif
+extern unsigned int ccs_memory_quota[CCS_MAX_MEMORY_STAT];
+extern unsigned int ccs_memory_used[CCS_MAX_MEMORY_STAT];
+
+/* Inlined functions for internal use. */
+
+/**
+ * ccs_pathcmp - strcmp() for "struct ccs_path_info" structure.
+ *
+ * @a: Pointer to "struct ccs_path_info".
+ * @b: Pointer to "struct ccs_path_info".
+ *
+ * Returns true if @a != @b, false otherwise.
+ */
+static inline bool ccs_pathcmp(const struct ccs_path_info *a,
+			       const struct ccs_path_info *b)
+{
+	return a->hash != b->hash || strcmp(a->name, b->name);
+}
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
+
+/**
+ * ccs_read_lock - Take lock for protecting policy.
+ *
+ * Returns index number for ccs_read_unlock().
+ */
+static inline int ccs_read_lock(void)
+{
+	return srcu_read_lock(&ccs_ss);
+}
+
+/**
+ * ccs_read_unlock - Release lock for protecting policy.
+ *
+ * @idx: Index number returned by ccs_read_lock().
+ *
+ * Returns nothing.
+ */
+static inline void ccs_read_unlock(const int idx)
+{
+	srcu_read_unlock(&ccs_ss, idx);
+}
+
+#else
+
+int ccs_lock(void);
+void ccs_unlock(const int idx);
+
+/**
+ * ccs_read_lock - Take lock for protecting policy.
+ *
+ * Returns index number for ccs_read_unlock().
+ */
+static inline int ccs_read_lock(void)
+{
+	return ccs_lock();
+}
+
+/**
+ * ccs_read_unlock - Release lock for protecting policy.
+ *
+ * @idx: Index number returned by ccs_read_lock().
+ *
+ * Returns nothing.
+ */
+static inline void ccs_read_unlock(const int idx)
+{
+	ccs_unlock(idx);
+}
+
+#endif
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
+
+/**
+ * ccs_tasklist_lock - Take lock for reading list of "struct task_struct".
+ *
+ * Returns nothing.
+ */
+static inline void ccs_tasklist_lock(void)
+{
+	rcu_read_lock();
+}
+
+/**
+ * ccs_tasklist_unlock - Release lock for reading list of "struct task_struct".
+ *
+ * Returns nothing.
+ */
+static inline void ccs_tasklist_unlock(void)
+{
+	rcu_read_unlock();
+}
+
+#else
+
+/**
+ * ccs_tasklist_lock - Take lock for reading list of "struct task_struct".
+ *
+ * Returns nothing.
+ */
+static inline void ccs_tasklist_lock(void)
+{
+	read_lock(&tasklist_lock);
+}
+
+/**
+ * ccs_tasklist_unlock - Release lock for reading list of "struct task_struct".
+ *
+ * Returns nothing.
+ */
+static inline void ccs_tasklist_unlock(void)
+{
+	read_unlock(&tasklist_lock);
+}
+
+#endif
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
+
+/**
+ * ccs_sys_getppid - Copy of getppid().
+ *
+ * Returns parent process's PID.
+ *
+ * Alpha does not have getppid() defined. To be able to build this module on
+ * Alpha, I have to copy getppid() from kernel/timer.c.
+ */
+static inline pid_t ccs_sys_getppid(void)
+{
+	pid_t pid;
+	rcu_read_lock();
+	pid = task_tgid_vnr(rcu_dereference(current->real_parent));
+	rcu_read_unlock();
+	return pid;
+}
+
+#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
+
+/**
+ * ccs_sys_getppid - Copy of getppid().
+ *
+ * Returns parent process's PID.
+ *
+ * This function was rewritten to use RCU in 2.6.16.34. However, distributors
+ * which use earlier kernels (e.g. 2.6.8/2.6.9) did not backport the bugfix.
+ * Therefore, I'm using code for 2.6.16.34 for earlier kernels.
+ */
+static inline pid_t ccs_sys_getppid(void)
+{
+	pid_t pid;
+	rcu_read_lock();
+#if (defined(RHEL_MAJOR) && RHEL_MAJOR == 5) || (defined(AX_MAJOR) && AX_MAJOR == 3)
+	pid = rcu_dereference(current->parent)->tgid;
+#elif defined(CONFIG_UTRACE)
+	/*
+	 * RHEL 5.0 kernel does not have RHEL_MAJOR/RHEL_MINOR defined.
+	 * Assume RHEL 5.0 if CONFIG_UTRACE is defined.
+	 */
+	pid = rcu_dereference(current->parent)->tgid;
+#else
+	pid = rcu_dereference(current->real_parent)->tgid;
+#endif
+	rcu_read_unlock();
+	return pid;
+}
+
+#else
+
+/**
+ * ccs_sys_getppid - Copy of getppid().
+ *
+ * Returns parent process's PID.
+ *
+ * I can't use code for 2.6.16.34 for 2.4 kernels because 2.4 kernels does not
+ * have RCU. Therefore, I'm using pessimistic lock (i.e. tasklist_lock
+ * spinlock).
+ */
+static inline pid_t ccs_sys_getppid(void)
+{
+	pid_t pid;
+	read_lock(&tasklist_lock);
+#ifdef TASK_DEAD
+	pid = current->group_leader->real_parent->tgid;
+#else
+	pid = current->p_opptr->pid;
+#endif
+	read_unlock(&tasklist_lock);
+	return pid;
+}
+
+#endif
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
+
+/**
+ * ccs_sys_getpid - Copy of getpid().
+ *
+ * Returns current thread's PID.
+ *
+ * Alpha does not have getpid() defined. To be able to build this module on
+ * Alpha, I have to copy getpid() from kernel/timer.c.
+ */
+static inline pid_t ccs_sys_getpid(void)
+{
+	return task_tgid_vnr(current);
+}
+
+#else
+
+/**
+ * ccs_sys_getpid - Copy of getpid().
+ *
+ * Returns current thread's PID.
+ */
+static inline pid_t ccs_sys_getpid(void)
+{
+	return current->tgid;
+}
+
+#endif
+
+/**
+ * ccs_get_mode - Get mode for specified functionality.
+ *
+ * @profile: Profile number.
+ * @index:   Functionality number.
+ *
+ * Returns mode.
+ */
+static inline u8 ccs_get_mode(const u8 profile, const u8 index)
+{
+	return ccs_get_config(profile, index) & (CCS_CONFIG_MAX_MODE - 1);
+}
+
+#if defined(CONFIG_SLOB)
+
+/**
+ * ccs_round2 - Round up to power of 2 for calculating memory usage.
+ *
+ * @size: Size to be rounded up.
+ *
+ * Returns @size.
+ *
+ * Since SLOB does not round up, this function simply returns @size.
+ */
+static inline int ccs_round2(size_t size)
+{
+	return size;
+}
+
+#else
+
+/**
+ * ccs_round2 - Round up to power of 2 for calculating memory usage.
+ *
+ * @size: Size to be rounded up.
+ *
+ * Returns rounded size.
+ *
+ * Strictly speaking, SLAB may be able to allocate (e.g.) 96 bytes instead of
+ * (e.g.) 128 bytes.
+ */
+static inline int ccs_round2(size_t size)
+{
+#if PAGE_SIZE == 4096
+	size_t bsize = 32;
+#else
+	size_t bsize = 64;
+#endif
+	if (!size)
+		return 0;
+	while (size > bsize)
+		bsize <<= 1;
+	return bsize;
+}
+
+#endif
+
+/**
+ * ccs_put_condition - Drop reference on "struct ccs_condition".
+ *
+ * @cond: Pointer to "struct ccs_condition". Maybe NULL.
+ *
+ * Returns nothing.
+ */
+static inline void ccs_put_condition(struct ccs_condition *cond)
+{
+	if (cond)
+		atomic_dec(&cond->head.users);
+}
+
+/**
+ * ccs_put_group - Drop reference on "struct ccs_group".
+ *
+ * @group: Pointer to "struct ccs_group". Maybe NULL.
+ *
+ * Returns nothing.
+ */
+static inline void ccs_put_group(struct ccs_group *group)
+{
+	if (group)
+		atomic_dec(&group->head.users);
+}
+
+/**
+ * ccs_put_name - Drop reference on "struct ccs_name".
+ *
+ * @name: Pointer to "struct ccs_path_info". Maybe NULL.
+ *
+ * Returns nothing.
+ */
+static inline void ccs_put_name(const struct ccs_path_info *name)
+{
+	if (name)
+		atomic_dec(&container_of(name, struct ccs_name, entry)->
+			   head.users);
+}
+
+/* For importing variables and functions. */
+extern const struct ccsecurity_exports ccsecurity_exports;
+
+#ifdef CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY
+
+/*
+ * Structure for holding "struct ccs_domain_info *" and "struct ccs_execve *"
+ * and "u32 ccs_flags" for each "struct task_struct".
+ *
+ * "struct ccs_domain_info *" and "u32 ccs_flags" for each "struct task_struct"
+ * are maintained outside that "struct task_struct". Therefore, ccs_security
+ * != task_struct . This keeps KABI for distributor's prebuilt kernels but
+ * entails slow access.
+ *
+ * Memory for this structure is allocated when current thread tries to access
+ * it. Therefore, if memory allocation failed, current thread will be killed by
+ * SIGKILL. Note that if current->pid == 1, sending SIGKILL won't work.
+ */
+struct ccs_security {
+	struct list_head list;
+	const struct task_struct *task;
+	struct ccs_domain_info *ccs_domain_info;
+	u32 ccs_flags;
+	struct rcu_head rcu;
+};
+
+#define CCS_TASK_SECURITY_HASH_BITS 12
+#define CCS_MAX_TASK_SECURITY_HASH (1u << CCS_TASK_SECURITY_HASH_BITS)
+extern struct list_head ccs_task_security_list[CCS_MAX_TASK_SECURITY_HASH];
+
+struct ccs_security *ccs_find_task_security(const struct task_struct *task);
+
+/**
+ * ccs_current_security - Get "struct ccs_security" for current thread.
+ *
+ * Returns pointer to "struct ccs_security" for current thread.
+ */
+static inline struct ccs_security *ccs_current_security(void)
+{
+	return ccs_find_task_security(current);
+}
+
+/**
+ * ccs_task_domain - Get "struct ccs_domain_info" for specified thread.
+ *
+ * @task: Pointer to "struct task_struct".
+ *
+ * Returns pointer to "struct ccs_security" for specified thread.
+ */
+static inline struct ccs_domain_info *ccs_task_domain(struct task_struct *task)
+{
+	struct ccs_domain_info *domain;
+	rcu_read_lock();
+	domain = ccs_find_task_security(task)->ccs_domain_info;
+	rcu_read_unlock();
+	return domain;
+}
+
+/**
+ * ccs_current_domain - Get "struct ccs_domain_info" for current thread.
+ *
+ * Returns pointer to "struct ccs_domain_info" for current thread.
+ */
+static inline struct ccs_domain_info *ccs_current_domain(void)
+{
+	return ccs_find_task_security(current)->ccs_domain_info;
+}
+
+/**
+ * ccs_task_flags - Get flags for specified thread.
+ *
+ * @task: Pointer to "struct task_struct".
+ *
+ * Returns flags for specified thread.
+ */
+static inline u32 ccs_task_flags(struct task_struct *task)
+{
+	u32 ccs_flags;
+	rcu_read_lock();
+	ccs_flags = ccs_find_task_security(task)->ccs_flags;
+	rcu_read_unlock();
+	return ccs_flags;
+}
+
+/**
+ * ccs_current_flags - Get flags for current thread.
+ *
+ * Returns flags for current thread.
+ */
+static inline u32 ccs_current_flags(void)
+{
+	return ccs_find_task_security(current)->ccs_flags;
+}
+
+#else
+
+/*
+ * "struct ccs_domain_info *" and "u32 ccs_flags" for each "struct task_struct"
+ * are maintained inside that "struct task_struct". Therefore, ccs_security ==
+ * task_struct . This allows fast access but breaks KABI checks for
+ * distributor's prebuilt kernels due to changes in "struct task_struct".
+ */
+#define ccs_security task_struct
+
+/**
+ * ccs_find_task_security - Find "struct ccs_security" for given task.
+ *
+ * @task: Pointer to "struct task_struct".
+ *
+ * Returns pointer to "struct ccs_security".
+ */
+static inline struct ccs_security *ccs_find_task_security(struct task_struct *
+							  task)
+{
+	return task;
+}
+
+/**
+ * ccs_current_security - Get "struct ccs_security" for current thread.
+ *
+ * Returns pointer to "struct ccs_security" for current thread.
+ */
+static inline struct ccs_security *ccs_current_security(void)
+{
+	return ccs_find_task_security(current);
+}
+
+/**
+ * ccs_task_domain - Get "struct ccs_domain_info" for specified thread.
+ *
+ * @task: Pointer to "struct task_struct".
+ *
+ * Returns pointer to "struct ccs_security" for specified thread.
+ */
+static inline struct ccs_domain_info *ccs_task_domain(struct task_struct *task)
+{
+	struct ccs_domain_info *domain = task->ccs_domain_info;
+	return domain ? domain : &ccs_kernel_domain;
+}
+
+/**
+ * ccs_current_domain - Get "struct ccs_domain_info" for current thread.
+ *
+ * Returns pointer to "struct ccs_domain_info" for current thread.
+ *
+ * If current thread does not belong to a domain (which is true for initial
+ * init_task in order to hide ccs_kernel_domain from this module),
+ * current thread enters into ccs_kernel_domain.
+ */
+static inline struct ccs_domain_info *ccs_current_domain(void)
+{
+	struct task_struct *task = current;
+	if (!task->ccs_domain_info)
+		task->ccs_domain_info = &ccs_kernel_domain;
+	return task->ccs_domain_info;
+}
+
+/**
+ * ccs_task_flags - Get flags for specified thread.
+ *
+ * @task: Pointer to "struct task_struct".
+ *
+ * Returns flags for specified thread.
+ */
+static inline u32 ccs_task_flags(struct task_struct *task)
+{
+	return ccs_find_task_security(task)->ccs_flags;
+}
+
+/**
+ * ccs_current_flags - Get flags for current thread.
+ *
+ * Returns flags for current thread.
+ */
+static inline u32 ccs_current_flags(void)
+{
+	return ccs_find_task_security(current)->ccs_flags;
+}
+
+#endif
+
+/**
+ * ccs_current_namespace - Get "struct ccs_policy_namespace" for current thread.
+ *
+ * Returns pointer to "struct ccs_policy_namespace" for current thread.
+ */
+static inline struct ccs_policy_namespace *ccs_current_namespace(void)
+{
+	return ccs_current_domain()->ns;
+}
+
+#endif
diff --git a/security/ccsecurity/load_policy.c b/security/ccsecurity/load_policy.c
new file mode 100644
index 0000000..fc4ae30
--- /dev/null
+++ b/security/ccsecurity/load_policy.c
@@ -0,0 +1,352 @@
+/*
+ * security/ccsecurity/load_policy.c
+ *
+ * Copyright (C) 2005-2012  NTT DATA CORPORATION
+ *
+ * Version: 1.8.4   2015/05/05
+ */
+
+#include <linux/version.h>
+#include <linux/module.h>
+#include <linux/init.h>
+#include <linux/binfmts.h>
+#include <linux/sched.h>
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
+#include <linux/kmod.h>
+/*
+ * Regarding 2.4 kernels, we need to define __KERNEL_SYSCALLS__ in order to use
+ * waitpid() because call_usermodehelper() does not support UMH_WAIT_PROC.
+ */
+#define __KERNEL_SYSCALLS__
+#include <linux/unistd.h>
+#else
+#include <linux/fs.h>
+#include <linux/namei.h>
+#endif
+#ifndef LOOKUP_POSITIVE
+#define LOOKUP_POSITIVE 0
+#endif
+
+/*
+ * TOMOYO specific part start.
+ */
+
+#include <linux/ccsecurity.h>
+
+/**
+ * ccs_setup - Set enable/disable upon boot.
+ *
+ * @str: "off" to disable, "on" to enable.
+ *
+ * Returns 0.
+ */
+static int __init ccs_setup(char *str)
+{
+	if (!strcmp(str, "off"))
+		ccsecurity_ops.disabled = 1;
+	else if (!strcmp(str, "on"))
+		ccsecurity_ops.disabled = 0;
+	return 0;
+}
+
+__setup("ccsecurity=", ccs_setup);
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 0, 0)
+#include "lsm2ccsecurity.c"
+#endif
+
+#ifndef CONFIG_CCSECURITY_OMIT_USERSPACE_LOADER
+
+/* Path to the policy loader. (default = CONFIG_CCSECURITY_POLICY_LOADER) */
+static const char *ccs_loader;
+
+/**
+ * ccs_loader_setup - Set policy loader.
+ *
+ * @str: Program to use as a policy loader (e.g. /sbin/ccs-init ).
+ *
+ * Returns 0.
+ */
+static int __init ccs_loader_setup(char *str)
+{
+	ccs_loader = str;
+	return 0;
+}
+
+__setup("CCS_loader=", ccs_loader_setup);
+
+/**
+ * ccs_policy_loader_exists - Check whether /sbin/ccs-init exists.
+ *
+ * Returns true if /sbin/ccs-init exists, false otherwise.
+ */
+static _Bool ccs_policy_loader_exists(void)
+{
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
+	struct path path;
+	if (!ccs_loader)
+		ccs_loader = CONFIG_CCSECURITY_POLICY_LOADER;
+	if (kern_path(ccs_loader, LOOKUP_FOLLOW | LOOKUP_POSITIVE,
+		      &path) == 0) {
+		path_put(&path);
+		return 1;
+	}
+#else
+	struct nameidata nd;
+	if (!ccs_loader)
+		ccs_loader = CONFIG_CCSECURITY_POLICY_LOADER;
+	if (path_lookup(ccs_loader, LOOKUP_FOLLOW | LOOKUP_POSITIVE,
+			&nd) == 0) {
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
+		path_put(&nd.path);
+#else
+		path_release(&nd);
+#endif
+		return 1;
+	}
+#endif
+	printk(KERN_INFO "Not activating Mandatory Access Control "
+	       "as %s does not exist.\n", ccs_loader);
+	return 0;
+}
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
+
+/**
+ * ccs_run_loader - Start /sbin/ccs-init.
+ *
+ * @unused: Not used.
+ *
+ * Returns PID of /sbin/ccs-init on success, negative value otherwise.
+ */
+static int ccs_run_loader(void *unused)
+{
+	char *argv[2];
+	char *envp[3];
+	printk(KERN_INFO "Calling %s to load policy. Please wait.\n",
+	       ccs_loader);
+	argv[0] = (char *) ccs_loader;
+	argv[1] = NULL;
+	envp[0] = "HOME=/";
+	envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
+	envp[2] = NULL;
+	return exec_usermodehelper(argv[0], argv, envp);
+}
+
+#endif
+
+/* Path to the trigger. (default = CONFIG_CCSECURITY_ACTIVATION_TRIGGER) */
+static const char *ccs_trigger;
+
+/**
+ * ccs_trigger_setup - Set trigger for activation.
+ *
+ * @str: Program to use as an activation trigger (e.g. /sbin/init ).
+ *
+ * Returns 0.
+ */
+static int __init ccs_trigger_setup(char *str)
+{
+	ccs_trigger = str;
+	return 0;
+}
+
+__setup("CCS_trigger=", ccs_trigger_setup);
+
+/**
+ * ccs_load_policy - Run external policy loader to load policy.
+ *
+ * @filename: The program about to start.
+ *
+ * Returns nothing.
+ *
+ * This function checks whether @filename is /sbin/init, and if so
+ * invoke /sbin/ccs-init and wait for the termination of /sbin/ccs-init
+ * and then continues invocation of /sbin/init.
+ * /sbin/ccs-init reads policy files in /etc/ccs/ directory and
+ * writes to /proc/ccs/ interfaces.
+ */
+static void ccs_load_policy(const char *filename)
+{
+	static _Bool done;
+	if (ccsecurity_ops.disabled || done)
+		return;
+	if (!ccs_trigger)
+		ccs_trigger = CONFIG_CCSECURITY_ACTIVATION_TRIGGER;
+	if (strcmp(filename, ccs_trigger))
+		return;
+	if (!ccs_policy_loader_exists())
+		return;
+	done = 1;
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
+	{
+		char *argv[2];
+		char *envp[3];
+		printk(KERN_INFO "Calling %s to load policy. Please wait.\n",
+		       ccs_loader);
+		argv[0] = (char *) ccs_loader;
+		argv[1] = NULL;
+		envp[0] = "HOME=/";
+		envp[1] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
+		envp[2] = NULL;
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23) || defined(UMH_WAIT_PROC)
+		call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC);
+#else
+		call_usermodehelper(argv[0], argv, envp, 1);
+#endif
+	}
+#elif defined(TASK_DEAD)
+	{
+		/* Copied from kernel/kmod.c */
+		struct task_struct *task = current;
+		pid_t pid = kernel_thread(ccs_run_loader, NULL, 0);
+		sigset_t tmpsig;
+		spin_lock_irq(&task->sighand->siglock);
+		tmpsig = task->blocked;
+		siginitsetinv(&task->blocked,
+			      sigmask(SIGKILL) | sigmask(SIGSTOP));
+		recalc_sigpending();
+		spin_unlock_irq(&task->sighand->siglock);
+		if (pid >= 0)
+			waitpid(pid, NULL, __WCLONE);
+		spin_lock_irq(&task->sighand->siglock);
+		task->blocked = tmpsig;
+		recalc_sigpending();
+		spin_unlock_irq(&task->sighand->siglock);
+	}
+#else
+	{
+		/* Copied from kernel/kmod.c */
+		struct task_struct *task = current;
+		pid_t pid = kernel_thread(ccs_run_loader, NULL, 0);
+		sigset_t tmpsig;
+		spin_lock_irq(&task->sigmask_lock);
+		tmpsig = task->blocked;
+		siginitsetinv(&task->blocked,
+			      sigmask(SIGKILL) | sigmask(SIGSTOP));
+		recalc_sigpending(task);
+		spin_unlock_irq(&task->sigmask_lock);
+		if (pid >= 0)
+			waitpid(pid, NULL, __WCLONE);
+		spin_lock_irq(&task->sigmask_lock);
+		task->blocked = tmpsig;
+		recalc_sigpending(task);
+		spin_unlock_irq(&task->sigmask_lock);
+	}
+#endif
+	if (ccsecurity_ops.check_profile)
+		ccsecurity_ops.check_profile();
+	else
+		panic("Failed to load policy.");
+}
+
+#endif
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)
+
+/**
+ * __ccs_search_binary_handler - Load policy before calling search_binary_handler().
+ *
+ * @bprm: Pointer to "struct linux_binprm".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_search_binary_handler(struct linux_binprm *bprm)
+{
+#ifndef CONFIG_CCSECURITY_OMIT_USERSPACE_LOADER
+	ccs_load_policy(bprm->filename);
+#endif
+	/*
+	 * ccs_load_policy() executes /sbin/ccs-init if bprm->filename is
+	 * /sbin/init. /sbin/ccs-init executes /etc/ccs/ccs-load-module to
+	 * load loadable kernel module. The loadable kernel module modifies
+	 * "struct ccsecurity_ops". Thus, we need to transfer control to
+	 * __ccs_search_binary_handler() in security/ccsecurity/permission.c
+	 * if "struct ccsecurity_ops" was modified.
+	 */
+	if (ccsecurity_ops.search_binary_handler
+	    != __ccs_search_binary_handler)
+		return ccsecurity_ops.search_binary_handler(bprm);
+	return search_binary_handler(bprm);
+}
+
+#else
+
+/**
+ * __ccs_search_binary_handler - Load policy before calling search_binary_handler().
+ *
+ * @bprm: Pointer to "struct linux_binprm".
+ * @regs: Pointer to "struct pt_regs".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_search_binary_handler(struct linux_binprm *bprm,
+				       struct pt_regs *regs)
+{
+#ifndef CONFIG_CCSECURITY_OMIT_USERSPACE_LOADER
+	ccs_load_policy(bprm->filename);
+#endif
+	/*
+	 * ccs_load_policy() executes /sbin/ccs-init if bprm->filename is
+	 * /sbin/init. /sbin/ccs-init executes /etc/ccs/ccs-load-module to
+	 * load loadable kernel module. The loadable kernel module modifies
+	 * "struct ccsecurity_ops". Thus, we need to transfer control to
+	 * __ccs_search_binary_handler() in security/ccsecurity/permission.c
+	 * if "struct ccsecurity_ops" was modified.
+	 */
+	if (ccsecurity_ops.search_binary_handler
+	    != __ccs_search_binary_handler)
+		return ccsecurity_ops.search_binary_handler(bprm, regs);
+	return search_binary_handler(bprm, regs);
+}
+
+#endif
+
+/*
+ * Some exports for loadable kernel module part.
+ *
+ * Although scripts/checkpatch.pl complains about use of "extern" in C file,
+ * we don't put these into security/ccsecurity/internal.h because we want to
+ * split built-in part and loadable kernel module part.
+ */
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0) && LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 35)
+extern spinlock_t vfsmount_lock;
+#endif
+
+/* For exporting variables and functions. */
+const struct ccsecurity_exports ccsecurity_exports = {
+#ifndef CONFIG_CCSECURITY_OMIT_USERSPACE_LOADER
+	.load_policy = ccs_load_policy,
+#endif
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) && defined(CONFIG_SECURITY)
+	.add_hooks = ccs_add_hooks,
+#endif
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
+	.d_absolute_path = d_absolute_path,
+#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)
+	.__d_path = __d_path,
+#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
+	.vfsmount_lock = &vfsmount_lock,
+#endif
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
+	.find_task_by_vpid = find_task_by_vpid,
+	.find_task_by_pid_ns = find_task_by_pid_ns,
+#endif
+};
+#ifdef CONFIG_CCSECURITY_LKM
+/* Only ccsecurity module need to access this struct. */
+EXPORT_SYMBOL_GPL(ccsecurity_exports);
+#endif
+
+/* Members are updated by loadable kernel module. */
+struct ccsecurity_operations ccsecurity_ops = {
+	.search_binary_handler = __ccs_search_binary_handler,
+#ifdef CONFIG_CCSECURITY_DISABLE_BY_DEFAULT
+	.disabled = 1,
+#endif
+};
+/*
+ * Non-GPL modules might need to access this struct via inlined functions
+ * embedded into include/linux/security.h and include/net/ip.h
+ */
+EXPORT_SYMBOL(ccsecurity_ops);
diff --git a/security/ccsecurity/lsm2ccsecurity.c b/security/ccsecurity/lsm2ccsecurity.c
new file mode 100644
index 0000000..b81d8ed
--- /dev/null
+++ b/security/ccsecurity/lsm2ccsecurity.c
@@ -0,0 +1,192 @@
+/*
+ * security/ccsecurity/lsm2ccsecurity.c
+ *
+ * Copyright (C) 2005-2012  NTT DATA CORPORATION
+ *
+ * Version: 1.8.4   2015/07/11
+ */
+
+#include <linux/path.h>
+#include <linux/security.h>
+#include <linux/ccsecurity.h>
+
+int ccs_settime(const struct timespec *ts, const struct timezone *tz)
+{
+        return ccs_capable(CCS_SYS_SETTIME) ? 0 : -EPERM;
+}
+
+int ccs_sb_mount(const char *dev_name, struct path *path, const char *type,
+		 unsigned long flags, void *data)
+{
+        return ccs_mount_permission(dev_name, path, type, flags, data);
+}
+
+int ccs_sb_umount(struct vfsmount *mnt, int flags)
+{
+        return ccs_umount_permission(mnt, flags);
+}
+
+int ccs_sb_pivotroot(struct path *old_path, struct path *new_path)
+{
+        return ccs_pivot_root_permission(old_path, new_path);
+}
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(4, 1, 0)
+int ccs_inode_getattr(struct vfsmount *mnt, struct dentry *dentry)
+{
+        return ccs_getattr_permission(mnt, dentry);
+}
+#else
+int ccs_inode_getattr(const struct path *path)
+{
+        return ccs_getattr_permission(path->mnt, path->dentry);
+}
+#endif
+
+int ccs_file_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+        return ccs_ioctl_permission(file, cmd, arg);
+}
+
+int ccs_file_fcntl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+        return ccs_fcntl_permission(file, cmd, arg);
+}
+
+int ccs_file_open(struct file *file, const struct cred *cred)
+{
+        return ccs_open_permission(file);
+}
+
+int ccs_socket_create(int family, int type, int protocol, int kern)
+{
+        return ccs_socket_create_permission(family, type, protocol);
+}
+
+int ccs_socket_bind(struct socket *sock, struct sockaddr *address, int addrlen)
+{
+        return ccs_socket_bind_permission(sock, address, addrlen);
+}
+
+int ccs_socket_connect(struct socket *sock, struct sockaddr *address,
+		       int addrlen)
+{
+        return ccs_socket_connect_permission(sock, address, addrlen);
+}
+
+int ccs_socket_listen(struct socket *sock, int backlog)
+{
+        return ccs_socket_listen_permission(sock);
+}
+
+int ccs_socket_sendmsg(struct socket *sock, struct msghdr *msg, int size)
+{
+        return ccs_socket_sendmsg_permission(sock, msg, size);
+}
+
+int ccs_path_unlink(struct path *dir, struct dentry *dentry)
+{
+        return ccs_unlink_permission(dentry, dir->mnt);
+}
+
+int ccs_path_mkdir(struct path *dir, struct dentry *dentry, umode_t mode)
+{
+        return ccs_mkdir_permission(dentry, dir->mnt, mode);
+}
+
+int ccs_path_rmdir(struct path *dir, struct dentry *dentry)
+{
+        return ccs_rmdir_permission(dentry, dir->mnt);
+}
+
+int ccs_path_mknod(struct path *dir, struct dentry *dentry, umode_t mode,
+		   unsigned int dev)
+{
+        return ccs_mknod_permission(dentry, dir->mnt, mode, dev);
+}
+
+int ccs_path_truncate(struct path *path)
+{
+        return ccs_truncate_permission(path->dentry, path->mnt);
+}
+
+int ccs_path_symlink(struct path *dir, struct dentry *dentry,
+		     const char *old_name)
+{
+        return ccs_symlink_permission(dentry, dir->mnt, old_name);
+}
+
+int ccs_path_link(struct dentry *old_dentry, struct path *new_dir,
+		  struct dentry *new_dentry)
+{
+        return ccs_link_permission(old_dentry, new_dentry, new_dir->mnt);
+}
+
+int ccs_path_rename(struct path *old_dir, struct dentry *old_dentry,
+		    struct path *new_dir, struct dentry *new_dentry)
+{
+        return ccs_rename_permission(old_dentry, new_dentry, new_dir->mnt);
+}
+
+int ccs_path_chmod(struct path *path, umode_t mode)
+{
+        return ccs_chmod_permission(path->dentry, path->mnt, mode);
+}
+
+int ccs_path_chown(struct path *path, kuid_t uid, kgid_t gid)
+{
+        return ccs_chown_permission(path->dentry, path->mnt, uid, gid);
+}
+
+int ccs_path_chroot(struct path *path)
+{
+        return ccs_chroot_permission(path);
+}
+
+#if !defined(CONFIG_SECURITY_PATH)
+EXPORT_SYMBOL(ccs_path_mkdir);
+EXPORT_SYMBOL(ccs_path_mknod);
+EXPORT_SYMBOL(ccs_path_unlink);
+EXPORT_SYMBOL(ccs_path_rename);
+#endif
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) && defined(CONFIG_SECURITY)
+
+#include <linux/lsm_hooks.h>
+
+static struct security_hook_list ccsecurity_hooks[] = {
+	LSM_HOOK_INIT(settime, ccs_settime),
+	LSM_HOOK_INIT(sb_mount, ccs_sb_mount),
+	LSM_HOOK_INIT(sb_umount, ccs_sb_umount),
+	LSM_HOOK_INIT(sb_pivotroot, ccs_sb_pivotroot),
+	LSM_HOOK_INIT(inode_getattr, ccs_inode_getattr),
+	LSM_HOOK_INIT(file_ioctl, ccs_file_ioctl),
+	LSM_HOOK_INIT(file_fcntl, ccs_file_fcntl),
+	LSM_HOOK_INIT(file_open, ccs_file_open),
+#if defined(CONFIG_SECURITY_NETWORK)
+	LSM_HOOK_INIT(socket_create, ccs_socket_create),
+	LSM_HOOK_INIT(socket_bind, ccs_socket_bind),
+	LSM_HOOK_INIT(socket_connect, ccs_socket_connect),
+	LSM_HOOK_INIT(socket_listen, ccs_socket_listen),
+	LSM_HOOK_INIT(socket_sendmsg, ccs_socket_sendmsg),
+#endif
+#if defined(CONFIG_SECURITY_PATH)
+	LSM_HOOK_INIT(path_unlink, ccs_path_unlink),
+	LSM_HOOK_INIT(path_mkdir, ccs_path_mkdir),
+	LSM_HOOK_INIT(path_rmdir, ccs_path_rmdir),
+	LSM_HOOK_INIT(path_mknod, ccs_path_mknod),
+	LSM_HOOK_INIT(path_truncate, ccs_path_truncate),
+	LSM_HOOK_INIT(path_symlink, ccs_path_symlink),
+	LSM_HOOK_INIT(path_link, ccs_path_link),
+	LSM_HOOK_INIT(path_rename, ccs_path_rename),
+	LSM_HOOK_INIT(path_chmod, ccs_path_chmod),
+	LSM_HOOK_INIT(path_chown, ccs_path_chown),
+	LSM_HOOK_INIT(path_chroot, ccs_path_chroot),
+#endif
+};
+
+static void ccs_add_hooks(void)
+{
+	security_add_hooks(ccsecurity_hooks, ARRAY_SIZE(ccsecurity_hooks));
+}
+#endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) && defined(CONFIG_SECURITY) */
diff --git a/security/ccsecurity/memory.c b/security/ccsecurity/memory.c
new file mode 100644
index 0000000..6514a26
--- /dev/null
+++ b/security/ccsecurity/memory.c
@@ -0,0 +1,356 @@
+/*
+ * security/ccsecurity/memory.c
+ *
+ * Copyright (C) 2005-2012  NTT DATA CORPORATION
+ *
+ * Version: 1.8.4   2015/05/05
+ */
+
+#include "internal.h"
+
+/***** SECTION1: Constants definition *****/
+
+/***** SECTION2: Structure definition *****/
+
+/***** SECTION3: Prototype definition section *****/
+
+bool ccs_memory_ok(const void *ptr, const unsigned int size);
+const struct ccs_path_info *ccs_get_name(const char *name);
+#ifdef CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY
+struct ccs_security *ccs_find_task_security(const struct task_struct *task);
+#endif
+void *ccs_commit_ok(void *data, const unsigned int size);
+void __init ccs_mm_init(void);
+void ccs_warn_oom(const char *function);
+
+#ifdef CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY
+static int __ccs_alloc_task_security(const struct task_struct *task);
+static void __ccs_free_task_security(const struct task_struct *task);
+static void ccs_add_task_security(struct ccs_security *ptr,
+				  struct list_head *list);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8)
+static void ccs_rcu_free(struct rcu_head *rcu);
+#else
+static void ccs_rcu_free(void *arg);
+#endif
+#endif
+
+/***** SECTION4: Standalone functions section *****/
+
+/***** SECTION5: Variables definition section *****/
+
+/* Memoy currently used by policy/audit log/query. */
+unsigned int ccs_memory_used[CCS_MAX_MEMORY_STAT];
+
+/* Memory quota for "policy"/"audit log"/"query". */
+unsigned int ccs_memory_quota[CCS_MAX_MEMORY_STAT];
+
+/* The list for "struct ccs_name". */
+struct list_head ccs_name_list[CCS_MAX_HASH];
+
+#ifdef CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY
+
+/* Dummy security context for avoiding NULL pointer dereference. */
+static struct ccs_security ccs_oom_security = {
+	.ccs_domain_info = &ccs_kernel_domain
+};
+
+/* Dummy security context for avoiding NULL pointer dereference. */
+static struct ccs_security ccs_default_security = {
+	.ccs_domain_info = &ccs_kernel_domain
+};
+
+/* List of "struct ccs_security". */
+struct list_head ccs_task_security_list[CCS_MAX_TASK_SECURITY_HASH];
+/* Lock for protecting ccs_task_security_list[]. */
+static DEFINE_SPINLOCK(ccs_task_security_list_lock);
+
+#endif
+
+/***** SECTION6: Dependent functions section *****/
+
+/**
+ * ccs_warn_oom - Print out of memory warning message.
+ *
+ * @function: Function's name.
+ *
+ * Returns nothing.
+ */
+void ccs_warn_oom(const char *function)
+{
+	/* Reduce error messages. */
+	static pid_t ccs_last_pid;
+	const pid_t pid = current->pid;
+	if (ccs_last_pid != pid) {
+		printk(KERN_WARNING "ERROR: Out of memory at %s.\n",
+		       function);
+		ccs_last_pid = pid;
+	}
+	if (!ccs_policy_loaded)
+		panic("MAC Initialization failed.\n");
+}
+
+/**
+ * ccs_memory_ok - Check memory quota.
+ *
+ * @ptr:  Pointer to allocated memory. Maybe NULL.
+ * @size: Size in byte. Not used if @ptr is NULL.
+ *
+ * Returns true if @ptr is not NULL and quota not exceeded, false otherwise.
+ *
+ * Caller holds ccs_policy_lock mutex.
+ */
+bool ccs_memory_ok(const void *ptr, const unsigned int size)
+{
+	if (ptr) {
+		const size_t s = ccs_round2(size);
+		ccs_memory_used[CCS_MEMORY_POLICY] += s;
+		if (!ccs_memory_quota[CCS_MEMORY_POLICY] ||
+		    ccs_memory_used[CCS_MEMORY_POLICY] <=
+		    ccs_memory_quota[CCS_MEMORY_POLICY])
+			return true;
+		ccs_memory_used[CCS_MEMORY_POLICY] -= s;
+	}
+	ccs_warn_oom(__func__);
+	return false;
+}
+
+/**
+ * ccs_commit_ok - Allocate memory and check memory quota.
+ *
+ * @data: Data to copy from.
+ * @size: Size in byte.
+ *
+ * Returns pointer to allocated memory on success, NULL otherwise.
+ * @data is zero-cleared on success.
+ *
+ * Caller holds ccs_policy_lock mutex.
+ */
+void *ccs_commit_ok(void *data, const unsigned int size)
+{
+	void *ptr = kmalloc(size, CCS_GFP_FLAGS);
+	if (ccs_memory_ok(ptr, size)) {
+		memmove(ptr, data, size);
+		memset(data, 0, size);
+		return ptr;
+	}
+	kfree(ptr);
+	return NULL;
+}
+
+/**
+ * ccs_get_name - Allocate memory for string data.
+ *
+ * @name: The string to store into the permernent memory.
+ *
+ * Returns pointer to "struct ccs_path_info" on success, NULL otherwise.
+ */
+const struct ccs_path_info *ccs_get_name(const char *name)
+{
+	struct ccs_name *ptr;
+	unsigned int hash;
+	int len;
+	int allocated_len;
+	struct list_head *head;
+
+	if (!name)
+		return NULL;
+	len = strlen(name) + 1;
+	hash = full_name_hash((const unsigned char *) name, len - 1);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 0) || defined(RHEL_MAJOR)
+	head = &ccs_name_list[hash_long(hash, CCS_HASH_BITS)];
+#else
+	head = &ccs_name_list[hash % CCS_MAX_HASH];
+#endif
+	if (mutex_lock_interruptible(&ccs_policy_lock))
+		return NULL;
+	list_for_each_entry(ptr, head, head.list) {
+		if (hash != ptr->entry.hash || strcmp(name, ptr->entry.name) ||
+		    atomic_read(&ptr->head.users) == CCS_GC_IN_PROGRESS)
+			continue;
+		atomic_inc(&ptr->head.users);
+		goto out;
+	}
+	allocated_len = sizeof(*ptr) + len;
+	ptr = kzalloc(allocated_len, CCS_GFP_FLAGS);
+	if (ccs_memory_ok(ptr, allocated_len)) {
+		ptr->entry.name = ((char *) ptr) + sizeof(*ptr);
+		memmove((char *) ptr->entry.name, name, len);
+		atomic_set(&ptr->head.users, 1);
+		ccs_fill_path_info(&ptr->entry);
+		ptr->size = allocated_len;
+		list_add_tail(&ptr->head.list, head);
+	} else {
+		kfree(ptr);
+		ptr = NULL;
+	}
+out:
+	mutex_unlock(&ccs_policy_lock);
+	return ptr ? &ptr->entry : NULL;
+}
+
+#ifdef CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY
+
+/**
+ * ccs_add_task_security - Add "struct ccs_security" to list.
+ *
+ * @ptr:  Pointer to "struct ccs_security".
+ * @list: Pointer to "struct list_head".
+ *
+ * Returns nothing.
+ */
+static void ccs_add_task_security(struct ccs_security *ptr,
+				  struct list_head *list)
+{
+	unsigned long flags;
+	spin_lock_irqsave(&ccs_task_security_list_lock, flags);
+	list_add_rcu(&ptr->list, list);
+	spin_unlock_irqrestore(&ccs_task_security_list_lock, flags);
+}
+
+/**
+ * __ccs_alloc_task_security - Allocate memory for new tasks.
+ *
+ * @task: Pointer to "struct task_struct".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_alloc_task_security(const struct task_struct *task)
+{
+	struct ccs_security *old_security = ccs_current_security();
+	struct ccs_security *new_security = kzalloc(sizeof(*new_security),
+						    GFP_KERNEL);
+	struct list_head *list = &ccs_task_security_list
+		[hash_ptr((void *) task, CCS_TASK_SECURITY_HASH_BITS)];
+	if (!new_security)
+		return -ENOMEM;
+	new_security->task = task;
+	new_security->ccs_domain_info = old_security->ccs_domain_info;
+	new_security->ccs_flags = old_security->ccs_flags;
+	ccs_add_task_security(new_security, list);
+	return 0;
+}
+
+/**
+ * ccs_find_task_security - Find "struct ccs_security" for given task.
+ *
+ * @task: Pointer to "struct task_struct".
+ *
+ * Returns pointer to "struct ccs_security" on success, &ccs_oom_security on
+ * out of memory, &ccs_default_security otherwise.
+ *
+ * If @task is current thread and "struct ccs_security" for current thread was
+ * not found, I try to allocate it. But if allocation failed, current thread
+ * will be killed by SIGKILL. Note that if current->pid == 1, sending SIGKILL
+ * won't work.
+ */
+struct ccs_security *ccs_find_task_security(const struct task_struct *task)
+{
+	struct ccs_security *ptr;
+	struct list_head *list = &ccs_task_security_list
+		[hash_ptr((void *) task, CCS_TASK_SECURITY_HASH_BITS)];
+	/* Make sure INIT_LIST_HEAD() in ccs_mm_init() takes effect. */
+	while (!list->next);
+	rcu_read_lock();
+	list_for_each_entry_rcu(ptr, list, list) {
+		if (ptr->task != task)
+			continue;
+		rcu_read_unlock();
+		return ptr;
+	}
+	rcu_read_unlock();
+	if (task != current)
+		return &ccs_default_security;
+	/* Use GFP_ATOMIC because caller may have called rcu_read_lock(). */
+	ptr = kzalloc(sizeof(*ptr), GFP_ATOMIC);
+	if (!ptr) {
+		printk(KERN_WARNING "Unable to allocate memory for pid=%u\n",
+		       task->pid);
+		send_sig(SIGKILL, current, 0);
+		return &ccs_oom_security;
+	}
+	*ptr = ccs_default_security;
+	ptr->task = task;
+	ccs_add_task_security(ptr, list);
+	return ptr;
+}
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8)
+
+/**
+ * ccs_rcu_free - RCU callback for releasing "struct ccs_security".
+ *
+ * @rcu: Pointer to "struct rcu_head".
+ *
+ * Returns nothing.
+ */
+static void ccs_rcu_free(struct rcu_head *rcu)
+{
+	struct ccs_security *ptr = container_of(rcu, typeof(*ptr), rcu);
+	kfree(ptr);
+}
+
+#else
+
+/**
+ * ccs_rcu_free - RCU callback for releasing "struct ccs_security".
+ *
+ * @arg: Pointer to "void".
+ *
+ * Returns nothing.
+ */
+static void ccs_rcu_free(void *arg)
+{
+	struct ccs_security *ptr = arg;
+	kfree(ptr);
+}
+
+#endif
+
+/**
+ * __ccs_free_task_security - Release memory associated with "struct task_struct".
+ *
+ * @task: Pointer to "struct task_struct".
+ *
+ * Returns nothing.
+ */
+static void __ccs_free_task_security(const struct task_struct *task)
+{
+	unsigned long flags;
+	struct ccs_security *ptr = ccs_find_task_security(task);
+	if (ptr == &ccs_default_security || ptr == &ccs_oom_security)
+		return;
+	spin_lock_irqsave(&ccs_task_security_list_lock, flags);
+	list_del_rcu(&ptr->list);
+	spin_unlock_irqrestore(&ccs_task_security_list_lock, flags);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 8)
+	call_rcu(&ptr->rcu, ccs_rcu_free);
+#else
+	call_rcu(&ptr->rcu, ccs_rcu_free, ptr);
+#endif
+}
+
+#endif
+
+/**
+ * ccs_mm_init - Initialize mm related code.
+ *
+ * Returns nothing.
+ */
+void __init ccs_mm_init(void)
+{
+	int idx;
+	for (idx = 0; idx < CCS_MAX_HASH; idx++)
+		INIT_LIST_HEAD(&ccs_name_list[idx]);
+#ifdef CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY
+	for (idx = 0; idx < CCS_MAX_TASK_SECURITY_HASH; idx++)
+		INIT_LIST_HEAD(&ccs_task_security_list[idx]);
+#endif
+	smp_wmb(); /* Avoid out of order execution. */
+#ifdef CONFIG_CCSECURITY_USE_EXTERNAL_TASK_SECURITY
+	ccsecurity_ops.alloc_task_security = __ccs_alloc_task_security;
+	ccsecurity_ops.free_task_security = __ccs_free_task_security;
+#endif
+	ccs_kernel_domain.domainname = ccs_get_name("<kernel>");
+	list_add_tail_rcu(&ccs_kernel_domain.list, &ccs_domain_list);
+}
diff --git a/security/ccsecurity/permission.c b/security/ccsecurity/permission.c
new file mode 100644
index 0000000..d73c237
--- /dev/null
+++ b/security/ccsecurity/permission.c
@@ -0,0 +1,5025 @@
+/*
+ * security/ccsecurity/permission.c
+ *
+ * Copyright (C) 2005-2012  NTT DATA CORPORATION
+ *
+ * Version: 1.8.4   2015/05/05
+ */
+
+#include "internal.h"
+
+/***** SECTION1: Constants definition *****/
+
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 32)
+
+/*
+ * may_open() receives open flags modified by open_to_namei_flags() until
+ * 2.6.32. We stop here in case some distributions backported ACC_MODE changes,
+ * for we can't determine whether may_open() receives open flags modified by
+ * open_to_namei_flags() or not.
+ */
+#ifdef ACC_MODE
+#error ACC_MODE already defined.
+#endif
+#define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE])
+
+#if defined(RHEL_MAJOR) && RHEL_MAJOR == 6
+/* RHEL6 passes unmodified flags since 2.6.32-71.14.1.el6 . */
+#undef ACC_MODE
+#define ACC_MODE(x) ("\004\002\006"[(x)&O_ACCMODE])
+#endif
+
+#endif
+
+/* String table for special mount operations. */
+static const char * const ccs_mounts[CCS_MAX_SPECIAL_MOUNT] = {
+	[CCS_MOUNT_BIND]            = "--bind",
+	[CCS_MOUNT_MOVE]            = "--move",
+	[CCS_MOUNT_REMOUNT]         = "--remount",
+	[CCS_MOUNT_MAKE_UNBINDABLE] = "--make-unbindable",
+	[CCS_MOUNT_MAKE_PRIVATE]    = "--make-private",
+	[CCS_MOUNT_MAKE_SLAVE]      = "--make-slave",
+	[CCS_MOUNT_MAKE_SHARED]     = "--make-shared",
+};
+
+/* Mapping table from "enum ccs_path_acl_index" to "enum ccs_mac_index". */
+static const u8 ccs_p2mac[CCS_MAX_PATH_OPERATION] = {
+	[CCS_TYPE_EXECUTE]    = CCS_MAC_FILE_EXECUTE,
+	[CCS_TYPE_READ]       = CCS_MAC_FILE_OPEN,
+	[CCS_TYPE_WRITE]      = CCS_MAC_FILE_OPEN,
+	[CCS_TYPE_APPEND]     = CCS_MAC_FILE_OPEN,
+	[CCS_TYPE_UNLINK]     = CCS_MAC_FILE_UNLINK,
+#ifdef CONFIG_CCSECURITY_FILE_GETATTR
+	[CCS_TYPE_GETATTR]    = CCS_MAC_FILE_GETATTR,
+#endif
+	[CCS_TYPE_RMDIR]      = CCS_MAC_FILE_RMDIR,
+	[CCS_TYPE_TRUNCATE]   = CCS_MAC_FILE_TRUNCATE,
+	[CCS_TYPE_SYMLINK]    = CCS_MAC_FILE_SYMLINK,
+	[CCS_TYPE_CHROOT]     = CCS_MAC_FILE_CHROOT,
+	[CCS_TYPE_UMOUNT]     = CCS_MAC_FILE_UMOUNT,
+};
+
+/* Mapping table from "enum ccs_mkdev_acl_index" to "enum ccs_mac_index". */
+const u8 ccs_pnnn2mac[CCS_MAX_MKDEV_OPERATION] = {
+	[CCS_TYPE_MKBLOCK] = CCS_MAC_FILE_MKBLOCK,
+	[CCS_TYPE_MKCHAR]  = CCS_MAC_FILE_MKCHAR,
+};
+
+/* Mapping table from "enum ccs_path2_acl_index" to "enum ccs_mac_index". */
+const u8 ccs_pp2mac[CCS_MAX_PATH2_OPERATION] = {
+	[CCS_TYPE_LINK]       = CCS_MAC_FILE_LINK,
+	[CCS_TYPE_RENAME]     = CCS_MAC_FILE_RENAME,
+	[CCS_TYPE_PIVOT_ROOT] = CCS_MAC_FILE_PIVOT_ROOT,
+};
+
+/*
+ * Mapping table from "enum ccs_path_number_acl_index" to "enum ccs_mac_index".
+ */
+const u8 ccs_pn2mac[CCS_MAX_PATH_NUMBER_OPERATION] = {
+	[CCS_TYPE_CREATE] = CCS_MAC_FILE_CREATE,
+	[CCS_TYPE_MKDIR]  = CCS_MAC_FILE_MKDIR,
+	[CCS_TYPE_MKFIFO] = CCS_MAC_FILE_MKFIFO,
+	[CCS_TYPE_MKSOCK] = CCS_MAC_FILE_MKSOCK,
+	[CCS_TYPE_IOCTL]  = CCS_MAC_FILE_IOCTL,
+	[CCS_TYPE_CHMOD]  = CCS_MAC_FILE_CHMOD,
+	[CCS_TYPE_CHOWN]  = CCS_MAC_FILE_CHOWN,
+	[CCS_TYPE_CHGRP]  = CCS_MAC_FILE_CHGRP,
+};
+
+#ifdef CONFIG_CCSECURITY_NETWORK
+
+/*
+ * Mapping table from "enum ccs_network_acl_index" to "enum ccs_mac_index" for
+ * inet domain socket.
+ */
+static const u8 ccs_inet2mac[CCS_SOCK_MAX][CCS_MAX_NETWORK_OPERATION] = {
+	[SOCK_STREAM] = {
+		[CCS_NETWORK_BIND]    = CCS_MAC_NETWORK_INET_STREAM_BIND,
+		[CCS_NETWORK_LISTEN]  = CCS_MAC_NETWORK_INET_STREAM_LISTEN,
+		[CCS_NETWORK_CONNECT] = CCS_MAC_NETWORK_INET_STREAM_CONNECT,
+		[CCS_NETWORK_ACCEPT]  = CCS_MAC_NETWORK_INET_STREAM_ACCEPT,
+	},
+	[SOCK_DGRAM] = {
+		[CCS_NETWORK_BIND]    = CCS_MAC_NETWORK_INET_DGRAM_BIND,
+		[CCS_NETWORK_SEND]    = CCS_MAC_NETWORK_INET_DGRAM_SEND,
+#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG
+		[CCS_NETWORK_RECV]    = CCS_MAC_NETWORK_INET_DGRAM_RECV,
+#endif
+	},
+	[SOCK_RAW]    = {
+		[CCS_NETWORK_BIND]    = CCS_MAC_NETWORK_INET_RAW_BIND,
+		[CCS_NETWORK_SEND]    = CCS_MAC_NETWORK_INET_RAW_SEND,
+#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG
+		[CCS_NETWORK_RECV]    = CCS_MAC_NETWORK_INET_RAW_RECV,
+#endif
+	},
+};
+
+/*
+ * Mapping table from "enum ccs_network_acl_index" to "enum ccs_mac_index" for
+ * unix domain socket.
+ */
+static const u8 ccs_unix2mac[CCS_SOCK_MAX][CCS_MAX_NETWORK_OPERATION] = {
+	[SOCK_STREAM] = {
+		[CCS_NETWORK_BIND]    = CCS_MAC_NETWORK_UNIX_STREAM_BIND,
+		[CCS_NETWORK_LISTEN]  = CCS_MAC_NETWORK_UNIX_STREAM_LISTEN,
+		[CCS_NETWORK_CONNECT] = CCS_MAC_NETWORK_UNIX_STREAM_CONNECT,
+		[CCS_NETWORK_ACCEPT]  = CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT,
+	},
+	[SOCK_DGRAM] = {
+		[CCS_NETWORK_BIND]    = CCS_MAC_NETWORK_UNIX_DGRAM_BIND,
+		[CCS_NETWORK_SEND]    = CCS_MAC_NETWORK_UNIX_DGRAM_SEND,
+#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG
+		[CCS_NETWORK_RECV]    = CCS_MAC_NETWORK_UNIX_DGRAM_RECV,
+#endif
+	},
+	[SOCK_SEQPACKET] = {
+		[CCS_NETWORK_BIND]    = CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND,
+		[CCS_NETWORK_LISTEN]  = CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN,
+		[CCS_NETWORK_CONNECT] = CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT,
+		[CCS_NETWORK_ACCEPT]  = CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT,
+	},
+};
+
+#endif
+
+#ifdef CONFIG_CCSECURITY_CAPABILITY
+
+/*
+ * Mapping table from "enum ccs_capability_acl_index" to "enum ccs_mac_index".
+ */
+const u8 ccs_c2mac[CCS_MAX_CAPABILITY_INDEX] = {
+	[CCS_USE_ROUTE_SOCKET]  = CCS_MAC_CAPABILITY_USE_ROUTE_SOCKET,
+	[CCS_USE_PACKET_SOCKET] = CCS_MAC_CAPABILITY_USE_PACKET_SOCKET,
+	[CCS_SYS_REBOOT]        = CCS_MAC_CAPABILITY_SYS_REBOOT,
+	[CCS_SYS_VHANGUP]       = CCS_MAC_CAPABILITY_SYS_VHANGUP,
+	[CCS_SYS_SETTIME]       = CCS_MAC_CAPABILITY_SYS_SETTIME,
+	[CCS_SYS_NICE]          = CCS_MAC_CAPABILITY_SYS_NICE,
+	[CCS_SYS_SETHOSTNAME]   = CCS_MAC_CAPABILITY_SYS_SETHOSTNAME,
+	[CCS_USE_KERNEL_MODULE] = CCS_MAC_CAPABILITY_USE_KERNEL_MODULE,
+	[CCS_SYS_KEXEC_LOAD]    = CCS_MAC_CAPABILITY_SYS_KEXEC_LOAD,
+	[CCS_SYS_PTRACE]        = CCS_MAC_CAPABILITY_SYS_PTRACE,
+};
+
+#endif
+
+/***** SECTION2: Structure definition *****/
+
+/* Structure for holding inet domain socket's address. */
+struct ccs_inet_addr_info {
+	u16 port;           /* In network byte order. */
+	const u32 *address; /* In network byte order. */
+	bool is_ipv6;
+};
+
+/* Structure for holding unix domain socket's address. */
+struct ccs_unix_addr_info {
+	u8 *addr; /* This may not be '\0' terminated string. */
+	unsigned int addr_len;
+};
+
+/* Structure for holding socket address. */
+struct ccs_addr_info {
+	u8 protocol;
+	u8 operation;
+	struct ccs_inet_addr_info inet;
+	struct ccs_unix_addr_info unix0;
+};
+
+/***** SECTION3: Prototype definition section *****/
+
+bool ccs_dump_page(struct linux_binprm *bprm, unsigned long pos,
+		   struct ccs_page_dump *dump);
+void ccs_get_attributes(struct ccs_obj_info *obj);
+
+static bool ccs_alphabet_char(const char c);
+static bool ccs_argv(const unsigned int index, const char *arg_ptr,
+		     const int argc, const struct ccs_argv *argv, u8 *checked);
+static bool ccs_byte_range(const char *str);
+static bool ccs_check_entry(struct ccs_request_info *r,
+			    struct ccs_acl_info *ptr);
+static bool ccs_check_mkdev_acl(struct ccs_request_info *r,
+				const struct ccs_acl_info *ptr);
+static bool ccs_check_mount_acl(struct ccs_request_info *r,
+				const struct ccs_acl_info *ptr);
+static bool ccs_check_path2_acl(struct ccs_request_info *r,
+				const struct ccs_acl_info *ptr);
+static bool ccs_check_path_acl(struct ccs_request_info *r,
+			       const struct ccs_acl_info *ptr);
+static bool ccs_check_path_number_acl(struct ccs_request_info *r,
+				      const struct ccs_acl_info *ptr);
+static bool ccs_compare_number_union(const unsigned long value,
+				     const struct ccs_number_union *ptr);
+static bool ccs_condition(struct ccs_request_info *r,
+			  const struct ccs_condition *cond);
+static bool ccs_decimal(const char c);
+static bool ccs_envp(const char *env_name, const char *env_value,
+		     const int envc, const struct ccs_envp *envp, u8 *checked);
+static bool ccs_file_matches_pattern(const char *filename,
+				     const char *filename_end,
+				     const char *pattern,
+				     const char *pattern_end);
+static bool ccs_file_matches_pattern2(const char *filename,
+				      const char *filename_end,
+				      const char *pattern,
+				      const char *pattern_end);
+static bool ccs_get_realpath(struct ccs_path_info *buf, struct path *path);
+static bool ccs_hexadecimal(const char c);
+static bool ccs_number_matches_group(const unsigned long min,
+				     const unsigned long max,
+				     const struct ccs_group *group);
+static bool ccs_path_matches_pattern(const struct ccs_path_info *filename,
+				     const struct ccs_path_info *pattern);
+static bool ccs_path_matches_pattern2(const char *f, const char *p);
+static bool ccs_scan_bprm(struct ccs_execve *ee, const u16 argc,
+			  const struct ccs_argv *argv, const u16 envc,
+			  const struct ccs_envp *envp);
+static bool ccs_scan_exec_realpath(struct file *file,
+				   const struct ccs_name_union *ptr,
+				   const bool match);
+static bool ccs_scan_transition(const struct list_head *list,
+				const struct ccs_path_info *domainname,
+				const struct ccs_path_info *program,
+				const char *last_name,
+				const enum ccs_transition_type type);
+static const char *ccs_last_word(const char *name);
+static const struct ccs_path_info *ccs_compare_name_union
+(const struct ccs_path_info *name, const struct ccs_name_union *ptr);
+static const struct ccs_path_info *ccs_path_matches_group
+(const struct ccs_path_info *pathname, const struct ccs_group *group);
+static enum ccs_transition_type ccs_transition_type
+(const struct ccs_policy_namespace *ns, const struct ccs_path_info *domainname,
+ const struct ccs_path_info *program);
+static int __ccs_chmod_permission(struct dentry *dentry,
+				  struct vfsmount *vfsmnt, mode_t mode);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
+static int __ccs_chown_permission(struct dentry *dentry,
+				  struct vfsmount *vfsmnt, kuid_t user,
+				  kgid_t group);
+#else
+static int __ccs_chown_permission(struct dentry *dentry,
+				  struct vfsmount *vfsmnt, uid_t user,
+				  gid_t group);
+#endif
+static int __ccs_chroot_permission(struct path *path);
+static int __ccs_fcntl_permission(struct file *file, unsigned int cmd,
+				  unsigned long arg);
+static int __ccs_ioctl_permission(struct file *filp, unsigned int cmd,
+				  unsigned long arg);
+static int __ccs_link_permission(struct dentry *old_dentry,
+				 struct dentry *new_dentry,
+				 struct vfsmount *mnt);
+static int __ccs_mkdir_permission(struct dentry *dentry, struct vfsmount *mnt,
+				  unsigned int mode);
+static int __ccs_mknod_permission(struct dentry *dentry, struct vfsmount *mnt,
+				  const unsigned int mode, unsigned int dev);
+static int __ccs_mount_permission(const char *dev_name, struct path *path,
+				  const char *type, unsigned long flags,
+				  void *data_page);
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
+static int __ccs_open_exec_permission(struct dentry *dentry,
+				      struct vfsmount *mnt);
+#endif
+static int __ccs_open_permission(struct dentry *dentry, struct vfsmount *mnt,
+				 const int flag);
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18) || (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33) && defined(CONFIG_SYSCTL_SYSCALL))
+static int __ccs_parse_table(int __user *name, int nlen, void __user *oldval,
+			     void __user *newval, struct ctl_table *table);
+#endif
+static int __ccs_pivot_root_permission(struct path *old_path,
+				       struct path *new_path);
+static int __ccs_rename_permission(struct dentry *old_dentry,
+				   struct dentry *new_dentry,
+				   struct vfsmount *mnt);
+static int __ccs_rmdir_permission(struct dentry *dentry, struct vfsmount *mnt);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)
+static int __ccs_search_binary_handler(struct linux_binprm *bprm);
+#else
+static int __ccs_search_binary_handler(struct linux_binprm *bprm,
+				       struct pt_regs *regs);
+#endif
+static int __ccs_symlink_permission(struct dentry *dentry,
+				    struct vfsmount *mnt, const char *from);
+static int __ccs_truncate_permission(struct dentry *dentry,
+				     struct vfsmount *mnt);
+static int __ccs_umount_permission(struct vfsmount *mnt, int flags);
+static int __ccs_unlink_permission(struct dentry *dentry,
+				   struct vfsmount *mnt);
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
+static int __ccs_uselib_permission(struct dentry *dentry,
+				   struct vfsmount *mnt);
+#endif
+static int ccs_execute_permission(struct ccs_request_info *r,
+				  const struct ccs_path_info *filename);
+static int ccs_find_next_domain(struct ccs_execve *ee);
+static int ccs_get_path(const char *pathname, struct path *path);
+static int ccs_kern_path(const char *pathname, int flags, struct path *path);
+static int ccs_mkdev_perm(const u8 operation, struct dentry *dentry,
+			  struct vfsmount *mnt, const unsigned int mode,
+			  unsigned int dev);
+static int ccs_mount_acl(struct ccs_request_info *r, const char *dev_name,
+			 struct path *dir, const char *type,
+			 unsigned long flags);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33)
+static int ccs_new_open_permission(struct file *filp);
+#endif
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 24)
+static int ccs_old_chroot_permission(struct nameidata *nd);
+static int ccs_old_mount_permission(const char *dev_name, struct nameidata *nd,
+				    const char *type, unsigned long flags,
+				    void *data_page);
+static int ccs_old_pivot_root_permission(struct nameidata *old_nd,
+					 struct nameidata *new_nd);
+#endif
+static int ccs_path2_perm(const u8 operation, struct dentry *dentry1,
+			  struct vfsmount *mnt1, struct dentry *dentry2,
+			  struct vfsmount *mnt2);
+static int ccs_path_number_perm(const u8 type, struct dentry *dentry,
+				struct vfsmount *vfsmnt, unsigned long number);
+static int ccs_path_perm(const u8 operation, struct dentry *dentry,
+			 struct vfsmount *mnt, const char *target);
+static int ccs_path_permission(struct ccs_request_info *r, u8 operation,
+			       const struct ccs_path_info *filename);
+static int ccs_start_execve(struct linux_binprm *bprm,
+			    struct ccs_execve **eep);
+static int ccs_symlink_path(const char *pathname, struct ccs_path_info *name);
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 32)
+static void __ccs_clear_open_mode(void);
+static void __ccs_save_open_mode(int mode);
+#endif
+static void ccs_add_slash(struct ccs_path_info *buf);
+static void ccs_finish_execve(int retval, struct ccs_execve *ee);
+
+#ifdef CONFIG_CCSECURITY_MISC
+static bool ccs_check_env_acl(struct ccs_request_info *r,
+			      const struct ccs_acl_info *ptr);
+static int ccs_env_perm(struct ccs_request_info *r, const char *env);
+static int ccs_environ(struct ccs_execve *ee);
+#endif
+
+#ifdef CONFIG_CCSECURITY_CAPABILITY
+static bool __ccs_capable(const u8 operation);
+static bool ccs_check_capability_acl(struct ccs_request_info *r,
+				     const struct ccs_acl_info *ptr);
+static bool ccs_kernel_service(void);
+static int __ccs_ptrace_permission(long request, long pid);
+static int __ccs_socket_create_permission(int family, int type, int protocol);
+#endif
+
+#ifdef CONFIG_CCSECURITY_NETWORK
+static bool ccs_address_matches_group(const bool is_ipv6, const u32 *address,
+				      const struct ccs_group *group);
+static bool ccs_check_inet_acl(struct ccs_request_info *r,
+			       const struct ccs_acl_info *ptr);
+static bool ccs_check_unix_acl(struct ccs_request_info *r,
+			       const struct ccs_acl_info *ptr);
+static bool ccs_kernel_service(void);
+static int __ccs_socket_bind_permission(struct socket *sock,
+					struct sockaddr *addr, int addr_len);
+static int __ccs_socket_connect_permission(struct socket *sock,
+					   struct sockaddr *addr,
+					   int addr_len);
+static int __ccs_socket_listen_permission(struct socket *sock);
+static int __ccs_socket_post_accept_permission(struct socket *sock,
+					       struct socket *newsock);
+static int __ccs_socket_sendmsg_permission(struct socket *sock,
+					   struct msghdr *msg, int size);
+static int ccs_check_inet_address(const struct sockaddr *addr,
+				  const unsigned int addr_len, const u16 port,
+				  struct ccs_addr_info *address);
+static int ccs_check_unix_address(struct sockaddr *addr,
+				  const unsigned int addr_len,
+				  struct ccs_addr_info *address);
+static int ccs_inet_entry(const struct ccs_addr_info *address);
+static int ccs_unix_entry(const struct ccs_addr_info *address);
+static u8 ccs_sock_family(struct sock *sk);
+#endif
+
+#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG
+static int __ccs_socket_post_recvmsg_permission(struct sock *sk,
+						struct sk_buff *skb,
+						int flags);
+#endif
+
+#ifdef CONFIG_CCSECURITY_IPC
+static bool ccs_check_signal_acl(struct ccs_request_info *r,
+				 const struct ccs_acl_info *ptr);
+static int ccs_signal_acl(const int pid, const int sig);
+static int ccs_signal_acl0(pid_t tgid, pid_t pid, int sig);
+static int ccs_signal_acl2(const int sig, const int pid);
+#endif
+
+#ifdef CONFIG_CCSECURITY_FILE_GETATTR
+static int __ccs_getattr_permission(struct vfsmount *mnt,
+				    struct dentry *dentry);
+#endif
+
+#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER
+static bool ccs_find_execute_handler(struct ccs_execve *ee, const u8 type);
+static int ccs_try_alt_exec(struct ccs_execve *ee);
+static void ccs_unescape(unsigned char *dest);
+#endif
+
+#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION
+static bool ccs_check_task_acl(struct ccs_request_info *r,
+			       const struct ccs_acl_info *ptr);
+#endif
+
+/***** SECTION4: Standalone functions section *****/
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)
+
+/**
+ * ccs_copy_argv - Wrapper for copy_strings_kernel().
+ *
+ * @arg:  String to copy.
+ * @bprm: Pointer to "struct linux_binprm".
+ *
+ * Returns return value of copy_strings_kernel().
+ */
+static inline int ccs_copy_argv(const char *arg, struct linux_binprm *bprm)
+{
+	const int ret = copy_strings_kernel(1, &arg, bprm);
+	if (ret >= 0)
+		bprm->argc++;
+	return ret;
+}
+
+#else
+
+/**
+ * ccs_copy_argv - Wrapper for copy_strings_kernel().
+ *
+ * @arg:  String to copy.
+ * @bprm: Pointer to "struct linux_binprm".
+ *
+ * Returns return value of copy_strings_kernel().
+ */
+static inline int ccs_copy_argv(char *arg, struct linux_binprm *bprm)
+{
+	const int ret = copy_strings_kernel(1, &arg, bprm);
+	if (ret >= 0)
+		bprm->argc++;
+	return ret;
+}
+
+#endif
+
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 35)
+
+/**
+ * get_fs_root - Get reference on root directory.
+ *
+ * @fs:   Pointer to "struct fs_struct".
+ * @root: Pointer to "struct path".
+ *
+ * Returns nothing.
+ *
+ * This is for compatibility with older kernels.
+ */
+static inline void get_fs_root(struct fs_struct *fs, struct path *root)
+{
+	read_lock(&fs->lock);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
+	*root = fs->root;
+	path_get(root);
+#else
+	root->dentry = dget(fs->root);
+	root->mnt = mntget(fs->rootmnt);
+#endif
+	read_unlock(&fs->lock);
+}
+
+#endif
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
+
+/**
+ * module_put - Put a reference on module.
+ *
+ * @module: Pointer to "struct module". Maybe NULL.
+ *
+ * Returns nothing.
+ *
+ * This is for compatibility with older kernels.
+ */
+static inline void module_put(struct module *module)
+{
+	if (module)
+		__MOD_DEC_USE_COUNT(module);
+}
+
+#endif
+
+/**
+ * ccs_put_filesystem - Wrapper for put_filesystem().
+ *
+ * @fstype: Pointer to "struct file_system_type".
+ *
+ * Returns nothing.
+ *
+ * Since put_filesystem() is not exported, I embed put_filesystem() here.
+ */
+static inline void ccs_put_filesystem(struct file_system_type *fstype)
+{
+	module_put(fstype->owner);
+}
+
+#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 22)
+#if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5
+#if !defined(AX_MAJOR) || AX_MAJOR != 3
+
+/**
+ * ip_hdr - Get "struct iphdr".
+ *
+ * @skb: Pointer to "struct sk_buff".
+ *
+ * Returns pointer to "struct iphdr".
+ *
+ * This is for compatibility with older kernels.
+ */
+static inline struct iphdr *ip_hdr(const struct sk_buff *skb)
+{
+	return skb->nh.iph;
+}
+
+/**
+ * udp_hdr - Get "struct udphdr".
+ *
+ * @skb: Pointer to "struct sk_buff".
+ *
+ * Returns pointer to "struct udphdr".
+ *
+ * This is for compatibility with older kernels.
+ */
+static inline struct udphdr *udp_hdr(const struct sk_buff *skb)
+{
+	return skb->h.uh;
+}
+
+/**
+ * ipv6_hdr - Get "struct ipv6hdr".
+ *
+ * @skb: Pointer to "struct sk_buff".
+ *
+ * Returns pointer to "struct ipv6hdr".
+ *
+ * This is for compatibility with older kernels.
+ */
+static inline struct ipv6hdr *ipv6_hdr(const struct sk_buff *skb)
+{
+	return skb->nh.ipv6h;
+}
+
+#endif
+#endif
+#endif
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
+
+/**
+ * skb_kill_datagram - Kill a datagram forcibly.
+ *
+ * @sk:    Pointer to "struct sock".
+ * @skb:   Pointer to "struct sk_buff".
+ * @flags: Flags passed to skb_recv_datagram().
+ *
+ * Returns nothing.
+ */
+static inline void skb_kill_datagram(struct sock *sk, struct sk_buff *skb,
+				     int flags)
+{
+	/* Clear queue. */
+	if (flags & MSG_PEEK) {
+		int clear = 0;
+		spin_lock_irq(&sk->receive_queue.lock);
+		if (skb == skb_peek(&sk->receive_queue)) {
+			__skb_unlink(skb, &sk->receive_queue);
+			clear = 1;
+		}
+		spin_unlock_irq(&sk->receive_queue.lock);
+		if (clear)
+			kfree_skb(skb);
+	}
+	skb_free_datagram(sk, skb);
+}
+
+#elif LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 16)
+
+/**
+ * skb_kill_datagram - Kill a datagram forcibly.
+ *
+ * @sk:    Pointer to "struct sock".
+ * @skb:   Pointer to "struct sk_buff".
+ * @flags: Flags passed to skb_recv_datagram().
+ *
+ * Returns nothing.
+ */
+static inline void skb_kill_datagram(struct sock *sk, struct sk_buff *skb,
+				     int flags)
+{
+	/* Clear queue. */
+	if (flags & MSG_PEEK) {
+		int clear = 0;
+		spin_lock_bh(&sk->sk_receive_queue.lock);
+		if (skb == skb_peek(&sk->sk_receive_queue)) {
+			__skb_unlink(skb, &sk->sk_receive_queue);
+			clear = 1;
+		}
+		spin_unlock_bh(&sk->sk_receive_queue.lock);
+		if (clear)
+			kfree_skb(skb);
+	}
+	skb_free_datagram(sk, skb);
+}
+
+#endif
+
+#endif
+
+/***** SECTION5: Variables definition section *****/
+
+/* The initial domain. */
+struct ccs_domain_info ccs_kernel_domain;
+
+/* The list for "struct ccs_domain_info". */
+LIST_HEAD(ccs_domain_list);
+
+/***** SECTION6: Dependent functions section *****/
+
+/**
+ * ccs_path_matches_group - Check whether the given pathname matches members of the given pathname group.
+ *
+ * @pathname: The name of pathname.
+ * @group:    Pointer to "struct ccs_path_group".
+ *
+ * Returns matched member's pathname if @pathname matches pathnames in @group,
+ * NULL otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static const struct ccs_path_info *ccs_path_matches_group
+(const struct ccs_path_info *pathname, const struct ccs_group *group)
+{
+	struct ccs_path_group *member;
+	list_for_each_entry_srcu(member, &group->member_list, head.list,
+				 &ccs_ss) {
+		if (member->head.is_deleted)
+			continue;
+		if (!ccs_path_matches_pattern(pathname, member->member_name))
+			continue;
+		return member->member_name;
+	}
+	return NULL;
+}
+
+/**
+ * ccs_number_matches_group - Check whether the given number matches members of the given number group.
+ *
+ * @min:   Min number.
+ * @max:   Max number.
+ * @group: Pointer to "struct ccs_number_group".
+ *
+ * Returns true if @min and @max partially overlaps @group, false otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static bool ccs_number_matches_group(const unsigned long min,
+				     const unsigned long max,
+				     const struct ccs_group *group)
+{
+	struct ccs_number_group *member;
+	bool matched = false;
+	list_for_each_entry_srcu(member, &group->member_list, head.list,
+				 &ccs_ss) {
+		if (member->head.is_deleted)
+			continue;
+		if (min > member->number.values[1] ||
+		    max < member->number.values[0])
+			continue;
+		matched = true;
+		break;
+	}
+	return matched;
+}
+
+/**
+ * ccs_check_entry - Do permission check.
+ *
+ * @r:   Pointer to "struct ccs_request_info".
+ * @ptr: Pointer to "struct ccs_acl_info".
+ *
+ * Returns true on match, false otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static bool ccs_check_entry(struct ccs_request_info *r,
+			    struct ccs_acl_info *ptr)
+{
+	if (ptr->is_deleted || ptr->type != r->param_type)
+		return false;
+	switch (r->param_type) {
+	case CCS_TYPE_PATH_ACL:
+		return ccs_check_path_acl(r, ptr);
+	case CCS_TYPE_PATH2_ACL:
+		return ccs_check_path2_acl(r, ptr);
+	case CCS_TYPE_PATH_NUMBER_ACL:
+		return ccs_check_path_number_acl(r, ptr);
+	case CCS_TYPE_MKDEV_ACL:
+		return ccs_check_mkdev_acl(r, ptr);
+	case CCS_TYPE_MOUNT_ACL:
+		return ccs_check_mount_acl(r, ptr);
+#ifdef CONFIG_CCSECURITY_MISC
+	case CCS_TYPE_ENV_ACL:
+		return ccs_check_env_acl(r, ptr);
+#endif
+#ifdef CONFIG_CCSECURITY_CAPABILITY
+	case CCS_TYPE_CAPABILITY_ACL:
+		return ccs_check_capability_acl(r, ptr);
+#endif
+#ifdef CONFIG_CCSECURITY_NETWORK
+	case CCS_TYPE_INET_ACL:
+		return ccs_check_inet_acl(r, ptr);
+	case CCS_TYPE_UNIX_ACL:
+		return ccs_check_unix_acl(r, ptr);
+#endif
+#ifdef CONFIG_CCSECURITY_IPC
+	case CCS_TYPE_SIGNAL_ACL:
+		return ccs_check_signal_acl(r, ptr);
+#endif
+#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION
+	case CCS_TYPE_MANUAL_TASK_ACL:
+		return ccs_check_task_acl(r, ptr);
+#endif
+	}
+	return true;
+}
+
+/**
+ * ccs_check_acl - Do permission check.
+ *
+ * @r: Pointer to "struct ccs_request_info".
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+int ccs_check_acl(struct ccs_request_info *r)
+{
+	const struct ccs_domain_info *domain = ccs_current_domain();
+	int error;
+	do {
+		struct ccs_acl_info *ptr;
+		const struct list_head *list = &domain->acl_info_list;
+		u16 i = 0;
+retry:
+		list_for_each_entry_srcu(ptr, list, list, &ccs_ss) {
+			if (!ccs_check_entry(r, ptr))
+				continue;
+			if (!ccs_condition(r, ptr->cond))
+				continue;
+			r->matched_acl = ptr;
+			r->granted = true;
+			ccs_audit_log(r);
+			return 0;
+		}
+		for (; i < CCS_MAX_ACL_GROUPS; i++) {
+			if (!test_bit(i, domain->group))
+				continue;
+			list = &domain->ns->acl_group[i++];
+			goto retry;
+		}
+		r->granted = false;
+		error = ccs_audit_log(r);
+	} while (error == CCS_RETRY_REQUEST &&
+		 r->type != CCS_MAC_FILE_EXECUTE);
+	return error;
+}
+
+/**
+ * ccs_last_word - Get last component of a domainname.
+ *
+ * @name: Domainname to check.
+ *
+ * Returns the last word of @name.
+ */
+static const char *ccs_last_word(const char *name)
+{
+	const char *cp = strrchr(name, ' ');
+	if (cp)
+		return cp + 1;
+	return name;
+}
+
+/**
+ * ccs_scan_transition - Try to find specific domain transition type.
+ *
+ * @list:       Pointer to "struct list_head".
+ * @domainname: The name of current domain.
+ * @program:    The name of requested program.
+ * @last_name:  The last component of @domainname.
+ * @type:       One of values in "enum ccs_transition_type".
+ *
+ * Returns true if found one, false otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static bool ccs_scan_transition(const struct list_head *list,
+				const struct ccs_path_info *domainname,
+				const struct ccs_path_info *program,
+				const char *last_name,
+				const enum ccs_transition_type type)
+{
+	const struct ccs_transition_control *ptr;
+	list_for_each_entry_srcu(ptr, list, head.list, &ccs_ss) {
+		if (ptr->head.is_deleted || ptr->type != type)
+			continue;
+		if (ptr->domainname) {
+			if (!ptr->is_last_name) {
+				if (ptr->domainname != domainname)
+					continue;
+			} else {
+				/*
+				 * Use direct strcmp() since this is
+				 * unlikely used.
+				 */
+				if (strcmp(ptr->domainname->name, last_name))
+					continue;
+			}
+		}
+		if (ptr->program && ccs_pathcmp(ptr->program, program))
+			continue;
+		return true;
+	}
+	return false;
+}
+
+/**
+ * ccs_transition_type - Get domain transition type.
+ *
+ * @ns:         Pointer to "struct ccs_policy_namespace".
+ * @domainname: The name of current domain.
+ * @program:    The name of requested program.
+ *
+ * Returns CCS_TRANSITION_CONTROL_TRANSIT if executing @program causes domain
+ * transition across namespaces, CCS_TRANSITION_CONTROL_INITIALIZE if executing
+ * @program reinitializes domain transition within that namespace,
+ * CCS_TRANSITION_CONTROL_KEEP if executing @program stays at @domainname ,
+ * others otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static enum ccs_transition_type ccs_transition_type
+(const struct ccs_policy_namespace *ns, const struct ccs_path_info *domainname,
+ const struct ccs_path_info *program)
+{
+	const char *last_name = ccs_last_word(domainname->name);
+	enum ccs_transition_type type = CCS_TRANSITION_CONTROL_NO_RESET;
+	while (type < CCS_MAX_TRANSITION_TYPE) {
+		const struct list_head * const list =
+			&ns->policy_list[CCS_ID_TRANSITION_CONTROL];
+		if (!ccs_scan_transition(list, domainname, program, last_name,
+					 type)) {
+			type++;
+			continue;
+		}
+		if (type != CCS_TRANSITION_CONTROL_NO_RESET &&
+		    type != CCS_TRANSITION_CONTROL_NO_INITIALIZE)
+			break;
+		/*
+		 * Do not check for reset_domain if no_reset_domain matched.
+		 * Do not check for initialize_domain if no_initialize_domain
+		 * matched.
+		 */
+		type++;
+		type++;
+	}
+	return type;
+}
+
+/**
+ * ccs_find_next_domain - Find a domain.
+ *
+ * @ee: Pointer to "struct ccs_execve".
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static int ccs_find_next_domain(struct ccs_execve *ee)
+{
+	struct ccs_request_info *r = &ee->r;
+#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER
+	const struct ccs_path_info *handler = ee->handler;
+#endif
+	struct ccs_domain_info *domain = NULL;
+	struct ccs_domain_info * const old_domain = ccs_current_domain();
+	struct linux_binprm *bprm = ee->bprm;
+	struct ccs_security *task = ccs_current_security();
+	const struct ccs_path_info *candidate;
+	struct ccs_path_info exename;
+	int retval;
+	bool reject_on_transition_failure = false;
+
+	/* Get symlink's pathname of program. */
+	retval = ccs_symlink_path(bprm->filename, &exename);
+	if (retval < 0)
+		return retval;
+
+#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER
+	if (handler) {
+		/* No permission check for execute handler. */
+		candidate = &exename;
+		if (ccs_pathcmp(candidate, handler)) {
+			/* Failed to verify execute handler. */
+			static u8 counter = 20;
+			if (counter) {
+				counter--;
+				printk(KERN_WARNING "Failed to verify: %s\n",
+				       handler->name);
+			}
+			goto out;
+		}
+	} else
+#endif
+	{
+		struct ccs_aggregator *ptr;
+		struct list_head *list;
+retry:
+		/* Check 'aggregator' directive. */
+		candidate = &exename;
+		list = &old_domain->ns->policy_list[CCS_ID_AGGREGATOR];
+		list_for_each_entry_srcu(ptr, list, head.list, &ccs_ss) {
+			if (ptr->head.is_deleted ||
+			    !ccs_path_matches_pattern(candidate,
+						      ptr->original_name))
+				continue;
+			candidate = ptr->aggregated_name;
+			break;
+		}
+
+		/* Check execute permission. */
+		retval = ccs_execute_permission(r, candidate);
+		if (retval == CCS_RETRY_REQUEST)
+			goto retry;
+		if (retval < 0)
+			goto out;
+		/*
+		 * To be able to specify domainnames with wildcards, use the
+		 * pathname specified in the policy (which may contain
+		 * wildcard) rather than the pathname passed to execve()
+		 * (which never contains wildcard).
+		 */
+		if (r->param.path.matched_path)
+			candidate = r->param.path.matched_path;
+	}
+	/*
+	 * Check for domain transition preference if "file execute" matched.
+	 * If preference is given, make do_execve() fail if domain transition
+	 * has failed, for domain transition preference should be used with
+	 * destination domain defined.
+	 */
+	if (r->ee->transition) {
+		const char *domainname = r->ee->transition->name;
+		reject_on_transition_failure = true;
+		if (!strcmp(domainname, "keep"))
+			goto force_keep_domain;
+		if (!strcmp(domainname, "child"))
+			goto force_child_domain;
+		if (!strcmp(domainname, "reset"))
+			goto force_reset_domain;
+		if (!strcmp(domainname, "initialize"))
+			goto force_initialize_domain;
+		if (!strcmp(domainname, "parent")) {
+			char *cp;
+			strncpy(ee->tmp, old_domain->domainname->name,
+				CCS_EXEC_TMPSIZE - 1);
+			cp = strrchr(ee->tmp, ' ');
+			if (cp)
+				*cp = '\0';
+		} else if (*domainname == '<')
+			strncpy(ee->tmp, domainname, CCS_EXEC_TMPSIZE - 1);
+		else
+			snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s %s",
+				 old_domain->domainname->name, domainname);
+		goto force_jump_domain;
+	}
+	/*
+	 * No domain transition preference specified.
+	 * Calculate domain to transit to.
+	 */
+	switch (ccs_transition_type(old_domain->ns, old_domain->domainname,
+				    candidate)) {
+	case CCS_TRANSITION_CONTROL_RESET:
+force_reset_domain:
+		/* Transit to the root of specified namespace. */
+		snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "<%s>",
+			 candidate->name);
+		/*
+		 * Make do_execve() fail if domain transition across namespaces
+		 * has failed.
+		 */
+		reject_on_transition_failure = true;
+		break;
+	case CCS_TRANSITION_CONTROL_INITIALIZE:
+force_initialize_domain:
+		/* Transit to the child of current namespace's root. */
+		snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s %s",
+			 old_domain->ns->name, candidate->name);
+		break;
+	case CCS_TRANSITION_CONTROL_KEEP:
+force_keep_domain:
+		/* Keep current domain. */
+		domain = old_domain;
+		break;
+	default:
+		if (old_domain == &ccs_kernel_domain && !ccs_policy_loaded) {
+			/*
+			 * Needn't to transit from kernel domain before
+			 * starting /sbin/init. But transit from kernel domain
+			 * if executing initializers because they might start
+			 * before /sbin/init.
+			 */
+			domain = old_domain;
+			break;
+		}
+force_child_domain:
+		/* Normal domain transition. */
+		snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s %s",
+			 old_domain->domainname->name, candidate->name);
+		break;
+	}
+force_jump_domain:
+	/*
+	 * Tell GC that I started execve().
+	 * Also, tell open_exec() to check read permission.
+	 */
+	task->ccs_flags |= CCS_TASK_IS_IN_EXECVE;
+	/*
+	 * Make task->ccs_flags visible to GC before changing
+	 * task->ccs_domain_info.
+	 */
+	smp_wmb();
+	/*
+	 * Proceed to the next domain in order to allow reaching via PID.
+	 * It will be reverted if execve() failed. Reverting is not good.
+	 * But it is better than being unable to reach via PID in interactive
+	 * enforcing mode.
+	 */
+	if (!domain)
+		domain = ccs_assign_domain(ee->tmp, true);
+	if (domain)
+		retval = 0;
+	else if (reject_on_transition_failure) {
+		printk(KERN_WARNING
+		       "ERROR: Domain '%s' not ready.\n", ee->tmp);
+		retval = -ENOMEM;
+	} else if (r->mode == CCS_CONFIG_ENFORCING)
+		retval = -ENOMEM;
+	else {
+		retval = 0;
+		if (!old_domain->flags[CCS_DIF_TRANSITION_FAILED]) {
+			old_domain->flags[CCS_DIF_TRANSITION_FAILED] = true;
+			r->granted = false;
+			ccs_write_log(r, "%s",
+				      ccs_dif[CCS_DIF_TRANSITION_FAILED]);
+			printk(KERN_WARNING
+			       "ERROR: Domain '%s' not defined.\n", ee->tmp);
+		}
+	}
+out:
+	kfree(exename.name);
+	return retval;
+}
+
+#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER
+
+/**
+ * ccs_unescape - Unescape escaped string.
+ *
+ * @dest: String to unescape.
+ *
+ * Returns nothing.
+ */
+static void ccs_unescape(unsigned char *dest)
+{
+	unsigned char *src = dest;
+	unsigned char c;
+	unsigned char d;
+	unsigned char e;
+	while (1) {
+		c = *src++;
+		if (!c)
+			break;
+		if (c != '\\') {
+			*dest++ = c;
+			continue;
+		}
+		c = *src++;
+		if (c == '\\') {
+			*dest++ = c;
+			continue;
+		}
+		if (c < '0' || c > '3')
+			break;
+		d = *src++;
+		if (d < '0' || d > '7')
+			break;
+		e = *src++;
+		if (e < '0' || e > '7')
+			break;
+		*dest++ = ((c - '0') << 6) + ((d - '0') << 3) + (e - '0');
+	}
+	*dest = '\0';
+}
+
+/**
+ * ccs_try_alt_exec - Try to start execute handler.
+ *
+ * @ee: Pointer to "struct ccs_execve".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_try_alt_exec(struct ccs_execve *ee)
+{
+	/*
+	 * Contents of modified bprm.
+	 * The envp[] in original bprm is moved to argv[] so that
+	 * the alternatively executed program won't be affected by
+	 * some dangerous environment variables like LD_PRELOAD.
+	 *
+	 * modified bprm->argc
+	 *    = original bprm->argc + original bprm->envc + 7
+	 * modified bprm->envc
+	 *    = 0
+	 *
+	 * modified bprm->argv[0]
+	 *    = the program's name specified by *_execute_handler
+	 * modified bprm->argv[1]
+	 *    = ccs_current_domain()->domainname->name
+	 * modified bprm->argv[2]
+	 *    = the current process's name
+	 * modified bprm->argv[3]
+	 *    = the current process's information (e.g. uid/gid).
+	 * modified bprm->argv[4]
+	 *    = original bprm->filename
+	 * modified bprm->argv[5]
+	 *    = original bprm->argc in string expression
+	 * modified bprm->argv[6]
+	 *    = original bprm->envc in string expression
+	 * modified bprm->argv[7]
+	 *    = original bprm->argv[0]
+	 *  ...
+	 * modified bprm->argv[bprm->argc + 6]
+	 *     = original bprm->argv[bprm->argc - 1]
+	 * modified bprm->argv[bprm->argc + 7]
+	 *     = original bprm->envp[0]
+	 *  ...
+	 * modified bprm->argv[bprm->envc + bprm->argc + 6]
+	 *     = original bprm->envp[bprm->envc - 1]
+	 */
+	struct linux_binprm *bprm = ee->bprm;
+	struct file *filp;
+	int retval;
+	const int original_argc = bprm->argc;
+	const int original_envc = bprm->envc;
+
+	/* Close the requested program's dentry. */
+	ee->obj.path1.dentry = NULL;
+	ee->obj.path1.mnt = NULL;
+	ee->obj.stat_valid[CCS_PATH1] = false;
+	ee->obj.stat_valid[CCS_PATH1_PARENT] = false;
+	ee->obj.validate_done = false;
+	allow_write_access(bprm->file);
+	fput(bprm->file);
+	bprm->file = NULL;
+
+	/* Invalidate page dump cache. */
+	ee->dump.page = NULL;
+
+	/* Move envp[] to argv[] */
+	bprm->argc += bprm->envc;
+	bprm->envc = 0;
+
+	/* Set argv[6] */
+	{
+		snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%d", original_envc);
+		retval = ccs_copy_argv(ee->tmp, bprm);
+		if (retval < 0)
+			goto out;
+	}
+
+	/* Set argv[5] */
+	{
+		snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%d", original_argc);
+		retval = ccs_copy_argv(ee->tmp, bprm);
+		if (retval < 0)
+			goto out;
+	}
+
+	/* Set argv[4] */
+	{
+		retval = ccs_copy_argv(bprm->filename, bprm);
+		if (retval < 0)
+			goto out;
+	}
+
+	/* Set argv[3] */
+	{
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
+		/*
+		 * Pass uid/gid seen from current user namespace, for these
+		 * values are used by programs in current user namespace in
+		 * order to decide whether to execve() or not (rather than by
+		 * auditing daemon in init's user namespace).
+		 */
+		snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1,
+			 "pid=%d uid=%d gid=%d euid=%d egid=%d suid=%d "
+			 "sgid=%d fsuid=%d fsgid=%d", ccs_sys_getpid(),
+			 __kuid_val(current_uid()), __kgid_val(current_gid()),
+			 __kuid_val(current_euid()),
+			 __kgid_val(current_egid()),
+			 __kuid_val(current_suid()),
+			 __kgid_val(current_sgid()),
+			 __kuid_val(current_fsuid()),
+			 __kgid_val(current_fsgid()));
+#else
+		snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1,
+			 "pid=%d uid=%d gid=%d euid=%d egid=%d suid=%d "
+			 "sgid=%d fsuid=%d fsgid=%d", ccs_sys_getpid(),
+			 current_uid(), current_gid(), current_euid(),
+			 current_egid(), current_suid(), current_sgid(),
+			 current_fsuid(), current_fsgid());
+#endif
+		retval = ccs_copy_argv(ee->tmp, bprm);
+		if (retval < 0)
+			goto out;
+	}
+
+	/* Set argv[2] */
+	{
+		char *exe = (char *) ccs_get_exe();
+		if (exe) {
+			retval = ccs_copy_argv(exe, bprm);
+			kfree(exe);
+		} else {
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)
+			retval = ccs_copy_argv("<unknown>", bprm);
+#else
+			snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "<unknown>");
+			retval = ccs_copy_argv(ee->tmp, bprm);
+#endif
+		}
+		if (retval < 0)
+			goto out;
+	}
+
+	/* Set argv[1] */
+	{
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)
+		retval = ccs_copy_argv(ccs_current_domain()->domainname->name,
+				       bprm);
+#else
+		snprintf(ee->tmp, CCS_EXEC_TMPSIZE - 1, "%s",
+			 ccs_current_domain()->domainname->name);
+		retval = ccs_copy_argv(ee->tmp, bprm);
+#endif
+		if (retval < 0)
+			goto out;
+	}
+
+	/* Set argv[0] */
+	{
+		struct path root;
+		char *cp;
+		int root_len;
+		int handler_len;
+		get_fs_root(current->fs, &root);
+		cp = ccs_realpath(&root);
+		path_put(&root);
+		if (!cp) {
+			retval = -ENOMEM;
+			goto out;
+		}
+		root_len = strlen(cp);
+		retval = strncmp(ee->handler->name, cp, root_len);
+		root_len--;
+		kfree(cp);
+		if (retval) {
+			retval = -ENOENT;
+			goto out;
+		}
+		handler_len = ee->handler->total_len + 1;
+		cp = kmalloc(handler_len, CCS_GFP_FLAGS);
+		if (!cp) {
+			retval = -ENOMEM;
+			goto out;
+		}
+		/* ee->handler_path is released by ccs_finish_execve(). */
+		ee->handler_path = cp;
+		/* Adjust root directory for open_exec(). */
+		memmove(cp, ee->handler->name + root_len,
+			handler_len - root_len);
+		ccs_unescape(cp);
+		retval = -ENOENT;
+		if (*cp != '/')
+			goto out;
+		retval = ccs_copy_argv(cp, bprm);
+		if (retval < 0)
+			goto out;
+	}
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23)
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 24)
+	bprm->argv_len = bprm->exec - bprm->p;
+#endif
+#endif
+
+	/*
+	 * OK, now restart the process with execute handler program's dentry.
+	 */
+	filp = open_exec(ee->handler_path);
+	if (IS_ERR(filp)) {
+		retval = PTR_ERR(filp);
+		goto out;
+	}
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
+	ee->obj.path1 = filp->f_path;
+#else
+	ee->obj.path1.dentry = filp->f_dentry;
+	ee->obj.path1.mnt = filp->f_vfsmnt;
+#endif
+	bprm->file = filp;
+	bprm->filename = ee->handler_path;
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
+	bprm->interp = bprm->filename;
+#endif
+	retval = prepare_binprm(bprm);
+	if (retval < 0)
+		goto out;
+	ee->r.dont_sleep_on_enforce_error = true;
+	retval = ccs_find_next_domain(ee);
+	ee->r.dont_sleep_on_enforce_error = false;
+out:
+	return retval;
+}
+
+/**
+ * ccs_find_execute_handler - Find an execute handler.
+ *
+ * @ee:   Pointer to "struct ccs_execve".
+ * @type: Type of execute handler.
+ *
+ * Returns true if found, false otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static bool ccs_find_execute_handler(struct ccs_execve *ee, const u8 type)
+{
+	struct ccs_request_info *r = &ee->r;
+	/*
+	 * To avoid infinite execute handler loop, don't use execute handler
+	 * if the current process is marked as execute handler.
+	 */
+	if (ccs_current_flags() & CCS_TASK_IS_EXECUTE_HANDLER)
+		return false;
+	r->param_type = type;
+	ccs_check_acl(r);
+	if (!r->granted)
+		return false;
+	ee->handler = container_of(r->matched_acl, struct ccs_handler_acl,
+				   head)->handler;
+	ee->transition = r->matched_acl && r->matched_acl->cond &&
+		r->matched_acl->cond->exec_transit ?
+		r->matched_acl->cond->transit : NULL;
+	return true;
+}
+
+#endif
+
+#ifdef CONFIG_MMU
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23)
+#define CCS_BPRM_MMU
+#elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR >= 3
+#define CCS_BPRM_MMU
+#elif defined(AX_MAJOR) && AX_MAJOR == 3 && defined(AX_MINOR) && AX_MINOR >= 2
+#define CCS_BPRM_MMU
+#endif
+#endif
+
+/**
+ * ccs_dump_page - Dump a page to buffer.
+ *
+ * @bprm: Pointer to "struct linux_binprm".
+ * @pos:  Location to dump.
+ * @dump: Poiner to "struct ccs_page_dump".
+ *
+ * Returns true on success, false otherwise.
+ */
+bool ccs_dump_page(struct linux_binprm *bprm, unsigned long pos,
+		   struct ccs_page_dump *dump)
+{
+	struct page *page;
+	/* dump->data is released by ccs_start_execve(). */
+	if (!dump->data) {
+		dump->data = kzalloc(PAGE_SIZE, CCS_GFP_FLAGS);
+		if (!dump->data)
+			return false;
+	}
+	/* Same with get_arg_page(bprm, pos, 0) in fs/exec.c */
+#ifdef CCS_BPRM_MMU
+	if (get_user_pages(current, bprm->mm, pos, 1, 0, 1, &page, NULL) <= 0)
+		return false;
+#else
+	page = bprm->page[pos / PAGE_SIZE];
+#endif
+	if (page != dump->page) {
+		const unsigned int offset = pos % PAGE_SIZE;
+		/*
+		 * Maybe kmap()/kunmap() should be used here.
+		 * But remove_arg_zero() uses kmap_atomic()/kunmap_atomic().
+		 * So do I.
+		 */
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)
+		char *kaddr = kmap_atomic(page);
+#else
+		char *kaddr = kmap_atomic(page, KM_USER0);
+#endif
+		dump->page = page;
+		memcpy(dump->data + offset, kaddr + offset,
+		       PAGE_SIZE - offset);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)
+		kunmap_atomic(kaddr);
+#else
+		kunmap_atomic(kaddr, KM_USER0);
+#endif
+	}
+	/* Same with put_arg_page(page) in fs/exec.c */
+#ifdef CCS_BPRM_MMU
+	put_page(page);
+#endif
+	return true;
+}
+
+/**
+ * ccs_start_execve - Prepare for execve() operation.
+ *
+ * @bprm: Pointer to "struct linux_binprm".
+ * @eep:  Pointer to "struct ccs_execve *".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_start_execve(struct linux_binprm *bprm,
+			    struct ccs_execve **eep)
+{
+	int retval;
+	struct ccs_security *task = ccs_current_security();
+	struct ccs_execve *ee;
+	int idx;
+	*eep = NULL;
+	ee = kzalloc(sizeof(*ee), CCS_GFP_FLAGS);
+	if (!ee)
+		return -ENOMEM;
+	ee->tmp = kzalloc(CCS_EXEC_TMPSIZE, CCS_GFP_FLAGS);
+	if (!ee->tmp) {
+		kfree(ee);
+		return -ENOMEM;
+	}
+	idx = ccs_read_lock();
+	/* ee->dump->data is allocated by ccs_dump_page(). */
+	ee->previous_domain = task->ccs_domain_info;
+	/* Clear manager flag. */
+	task->ccs_flags &= ~CCS_TASK_IS_MANAGER;
+	*eep = ee;
+	ccs_init_request_info(&ee->r, CCS_MAC_FILE_EXECUTE);
+	ee->r.ee = ee;
+	ee->bprm = bprm;
+	ee->r.obj = &ee->obj;
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
+	ee->obj.path1 = bprm->file->f_path;
+#else
+	ee->obj.path1.dentry = bprm->file->f_dentry;
+	ee->obj.path1.mnt = bprm->file->f_vfsmnt;
+#endif
+#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER
+	/*
+	 * No need to call ccs_environ() for execute handler because envp[] is
+	 * moved to argv[].
+	 */
+	if (ccs_find_execute_handler(ee, CCS_TYPE_AUTO_EXECUTE_HANDLER)) {
+		retval = ccs_try_alt_exec(ee);
+		goto done;
+	}
+#endif
+	retval = ccs_find_next_domain(ee);
+#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER
+	if (retval == -EPERM &&
+	    ccs_find_execute_handler(ee, CCS_TYPE_DENIED_EXECUTE_HANDLER)) {
+		retval = ccs_try_alt_exec(ee);
+		goto done;
+	}
+#endif
+#ifdef CONFIG_CCSECURITY_MISC
+	if (!retval)
+		retval = ccs_environ(ee);
+#endif
+#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER
+done:
+#endif
+	ccs_read_unlock(idx);
+	kfree(ee->tmp);
+	ee->tmp = NULL;
+	kfree(ee->dump.data);
+	ee->dump.data = NULL;
+	return retval;
+}
+
+/**
+ * ccs_finish_execve - Clean up execve() operation.
+ *
+ * @retval: Return code of an execve() operation.
+ * @ee:     Pointer to "struct ccs_execve".
+ *
+ * Returns nothing.
+ */
+static void ccs_finish_execve(int retval, struct ccs_execve *ee)
+{
+	struct ccs_security *task = ccs_current_security();
+	if (!ee)
+		return;
+	if (retval < 0) {
+		task->ccs_domain_info = ee->previous_domain;
+		/*
+		 * Make task->ccs_domain_info visible to GC before changing
+		 * task->ccs_flags.
+		 */
+		smp_wmb();
+	} else {
+		/* Mark the current process as execute handler. */
+		if (ee->handler)
+			task->ccs_flags |= CCS_TASK_IS_EXECUTE_HANDLER;
+		/* Mark the current process as normal process. */
+		else
+			task->ccs_flags &= ~CCS_TASK_IS_EXECUTE_HANDLER;
+	}
+	/* Tell GC that I finished execve(). */
+	task->ccs_flags &= ~CCS_TASK_IS_IN_EXECVE;
+	kfree(ee->handler_path);
+	kfree(ee);
+}
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 8, 0)
+
+/**
+ * __ccs_search_binary_handler - Main routine for do_execve().
+ *
+ * @bprm: Pointer to "struct linux_binprm".
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * Performs permission checks for do_execve() and domain transition.
+ * Domain transition by "struct ccs_domain_transition_control" and
+ * "auto_domain_transition=" parameter of "struct ccs_condition" are reverted
+ * if do_execve() failed.
+ * Garbage collector does not remove "struct ccs_domain_info" from
+ * ccs_domain_list nor kfree("struct ccs_domain_info") if the current thread is
+ * marked as CCS_TASK_IS_IN_EXECVE.
+ */
+static int __ccs_search_binary_handler(struct linux_binprm *bprm)
+{
+	struct ccs_execve *ee;
+	int retval;
+#ifndef CONFIG_CCSECURITY_OMIT_USERSPACE_LOADER
+	if (!ccs_policy_loaded)
+		ccsecurity_exports.load_policy(bprm->filename);
+#endif
+	retval = ccs_start_execve(bprm, &ee);
+	if (!retval)
+		retval = search_binary_handler(bprm);
+	ccs_finish_execve(retval, ee);
+	return retval;
+}
+
+#else
+
+/**
+ * __ccs_search_binary_handler - Main routine for do_execve().
+ *
+ * @bprm: Pointer to "struct linux_binprm".
+ * @regs: Pointer to "struct pt_regs".
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * Performs permission checks for do_execve() and domain transition.
+ * Domain transition by "struct ccs_domain_transition_control" and
+ * "auto_domain_transition=" parameter of "struct ccs_condition" are reverted
+ * if do_execve() failed.
+ * Garbage collector does not remove "struct ccs_domain_info" from
+ * ccs_domain_list nor kfree("struct ccs_domain_info") if the current thread is
+ * marked as CCS_TASK_IS_IN_EXECVE.
+ */
+static int __ccs_search_binary_handler(struct linux_binprm *bprm,
+				       struct pt_regs *regs)
+{
+	struct ccs_execve *ee;
+	int retval;
+#ifndef CONFIG_CCSECURITY_OMIT_USERSPACE_LOADER
+	if (!ccs_policy_loaded)
+		ccsecurity_exports.load_policy(bprm->filename);
+#endif
+	retval = ccs_start_execve(bprm, &ee);
+	if (!retval)
+		retval = search_binary_handler(bprm, regs);
+	ccs_finish_execve(retval, ee);
+	return retval;
+}
+
+#endif
+
+/**
+ * ccs_permission_init - Register permission check hooks.
+ *
+ * Returns nothing.
+ */
+void __init ccs_permission_init(void)
+{
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 32)
+	ccsecurity_ops.save_open_mode = __ccs_save_open_mode;
+	ccsecurity_ops.clear_open_mode = __ccs_clear_open_mode;
+	ccsecurity_ops.open_permission = __ccs_open_permission;
+#else
+	ccsecurity_ops.open_permission = ccs_new_open_permission;
+#endif
+	ccsecurity_ops.fcntl_permission = __ccs_fcntl_permission;
+	ccsecurity_ops.ioctl_permission = __ccs_ioctl_permission;
+	ccsecurity_ops.chmod_permission = __ccs_chmod_permission;
+	ccsecurity_ops.chown_permission = __ccs_chown_permission;
+#ifdef CONFIG_CCSECURITY_FILE_GETATTR
+	ccsecurity_ops.getattr_permission = __ccs_getattr_permission;
+#endif
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
+	ccsecurity_ops.pivot_root_permission = __ccs_pivot_root_permission;
+	ccsecurity_ops.chroot_permission = __ccs_chroot_permission;
+#else
+	ccsecurity_ops.pivot_root_permission = ccs_old_pivot_root_permission;
+	ccsecurity_ops.chroot_permission = ccs_old_chroot_permission;
+#endif
+	ccsecurity_ops.umount_permission = __ccs_umount_permission;
+	ccsecurity_ops.mknod_permission = __ccs_mknod_permission;
+	ccsecurity_ops.mkdir_permission = __ccs_mkdir_permission;
+	ccsecurity_ops.rmdir_permission = __ccs_rmdir_permission;
+	ccsecurity_ops.unlink_permission = __ccs_unlink_permission;
+	ccsecurity_ops.symlink_permission = __ccs_symlink_permission;
+	ccsecurity_ops.truncate_permission = __ccs_truncate_permission;
+	ccsecurity_ops.rename_permission = __ccs_rename_permission;
+	ccsecurity_ops.link_permission = __ccs_link_permission;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
+	ccsecurity_ops.open_exec_permission = __ccs_open_exec_permission;
+	ccsecurity_ops.uselib_permission = __ccs_uselib_permission;
+#endif
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18) || (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33) && defined(CONFIG_SYSCTL_SYSCALL))
+	ccsecurity_ops.parse_table = __ccs_parse_table;
+#endif
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
+	ccsecurity_ops.mount_permission = __ccs_mount_permission;
+#else
+	ccsecurity_ops.mount_permission = ccs_old_mount_permission;
+#endif
+#ifdef CONFIG_CCSECURITY_CAPABILITY
+	ccsecurity_ops.socket_create_permission =
+		__ccs_socket_create_permission;
+#endif
+#ifdef CONFIG_CCSECURITY_NETWORK
+	ccsecurity_ops.socket_listen_permission =
+		__ccs_socket_listen_permission;
+	ccsecurity_ops.socket_connect_permission =
+		__ccs_socket_connect_permission;
+	ccsecurity_ops.socket_bind_permission = __ccs_socket_bind_permission;
+	ccsecurity_ops.socket_post_accept_permission =
+		__ccs_socket_post_accept_permission;
+	ccsecurity_ops.socket_sendmsg_permission =
+		__ccs_socket_sendmsg_permission;
+#endif
+#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG
+	ccsecurity_ops.socket_post_recvmsg_permission =
+		__ccs_socket_post_recvmsg_permission;
+#endif
+#ifdef CONFIG_CCSECURITY_IPC
+	ccsecurity_ops.kill_permission = ccs_signal_acl;
+	ccsecurity_ops.tgkill_permission = ccs_signal_acl0;
+	ccsecurity_ops.tkill_permission = ccs_signal_acl;
+	ccsecurity_ops.sigqueue_permission = ccs_signal_acl;
+	ccsecurity_ops.tgsigqueue_permission = ccs_signal_acl0;
+#endif
+#ifdef CONFIG_CCSECURITY_CAPABILITY
+	ccsecurity_ops.capable = __ccs_capable;
+	ccsecurity_ops.ptrace_permission = __ccs_ptrace_permission;
+#endif
+	ccsecurity_ops.search_binary_handler = __ccs_search_binary_handler;
+}
+
+/**
+ * ccs_kern_path - Wrapper for kern_path().
+ *
+ * @pathname: Pathname to resolve. Maybe NULL.
+ * @flags:    Lookup flags.
+ * @path:     Pointer to "struct path".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_kern_path(const char *pathname, int flags, struct path *path)
+{
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28)
+	if (!pathname || kern_path(pathname, flags, path))
+		return -ENOENT;
+#else
+	struct nameidata nd;
+	if (!pathname || path_lookup(pathname, flags, &nd))
+		return -ENOENT;
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
+	*path = nd.path;
+#else
+	path->dentry = nd.dentry;
+	path->mnt = nd.mnt;
+#endif
+#endif
+	return 0;
+}
+
+/**
+ * ccs_get_path - Get dentry/vfsmmount of a pathname.
+ *
+ * @pathname: The pathname to solve. Maybe NULL.
+ * @path:     Pointer to "struct path".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_get_path(const char *pathname, struct path *path)
+{
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
+	return ccs_kern_path(pathname, LOOKUP_FOLLOW, path);
+#else
+	return ccs_kern_path(pathname, LOOKUP_FOLLOW | LOOKUP_POSITIVE, path);
+#endif
+}
+
+/**
+ * ccs_symlink_path - Get symlink's pathname.
+ *
+ * @pathname: The pathname to solve. Maybe NULL.
+ * @name:     Pointer to "struct ccs_path_info".
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * This function uses kzalloc(), so caller must kfree() if this function
+ * didn't return NULL.
+ */
+static int ccs_symlink_path(const char *pathname, struct ccs_path_info *name)
+{
+	char *buf;
+	struct path path;
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
+	if (ccs_kern_path(pathname, 0, &path))
+		return -ENOENT;
+#else
+	if (ccs_kern_path(pathname, LOOKUP_POSITIVE, &path))
+		return -ENOENT;
+#endif
+	buf = ccs_realpath(&path);
+	path_put(&path);
+	if (buf) {
+		name->name = buf;
+		ccs_fill_path_info(name);
+		return 0;
+	}
+	return -ENOMEM;
+}
+
+/**
+ * ccs_check_mount_acl - Check permission for path path path number operation.
+ *
+ * @r:   Pointer to "struct ccs_request_info".
+ * @ptr: Pointer to "struct ccs_acl_info".
+ *
+ * Returns true if granted, false otherwise.
+ */
+static bool ccs_check_mount_acl(struct ccs_request_info *r,
+				const struct ccs_acl_info *ptr)
+{
+	const struct ccs_mount_acl *acl =
+		container_of(ptr, typeof(*acl), head);
+	return ccs_compare_number_union(r->param.mount.flags, &acl->flags) &&
+		ccs_compare_name_union(r->param.mount.type, &acl->fs_type) &&
+		ccs_compare_name_union(r->param.mount.dir, &acl->dir_name) &&
+		(!r->param.mount.need_dev ||
+		 ccs_compare_name_union(r->param.mount.dev, &acl->dev_name));
+}
+
+/**
+ * ccs_mount_acl - Check permission for mount() operation.
+ *
+ * @r:        Pointer to "struct ccs_request_info".
+ * @dev_name: Name of device file. Maybe NULL.
+ * @dir:      Pointer to "struct path".
+ * @type:     Name of filesystem type.
+ * @flags:    Mount options.
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static int ccs_mount_acl(struct ccs_request_info *r, const char *dev_name,
+			 struct path *dir, const char *type,
+			 unsigned long flags)
+{
+	struct ccs_obj_info obj = { };
+	struct file_system_type *fstype = NULL;
+	const char *requested_type = NULL;
+	const char *requested_dir_name = NULL;
+	const char *requested_dev_name = NULL;
+	struct ccs_path_info rtype;
+	struct ccs_path_info rdev;
+	struct ccs_path_info rdir;
+	int need_dev = 0;
+	int error = -ENOMEM;
+	r->obj = &obj;
+
+	/* Get fstype. */
+	requested_type = ccs_encode(type);
+	if (!requested_type)
+		goto out;
+	rtype.name = requested_type;
+	ccs_fill_path_info(&rtype);
+
+	/* Get mount point. */
+	obj.path2 = *dir;
+	requested_dir_name = ccs_realpath(dir);
+	if (!requested_dir_name) {
+		error = -ENOMEM;
+		goto out;
+	}
+	rdir.name = requested_dir_name;
+	ccs_fill_path_info(&rdir);
+
+	/* Compare fs name. */
+	if (type == ccs_mounts[CCS_MOUNT_REMOUNT]) {
+		/* dev_name is ignored. */
+	} else if (type == ccs_mounts[CCS_MOUNT_MAKE_UNBINDABLE] ||
+		   type == ccs_mounts[CCS_MOUNT_MAKE_PRIVATE] ||
+		   type == ccs_mounts[CCS_MOUNT_MAKE_SLAVE] ||
+		   type == ccs_mounts[CCS_MOUNT_MAKE_SHARED]) {
+		/* dev_name is ignored. */
+	} else if (type == ccs_mounts[CCS_MOUNT_BIND] ||
+		   type == ccs_mounts[CCS_MOUNT_MOVE]) {
+		need_dev = -1; /* dev_name is a directory */
+	} else {
+		fstype = get_fs_type(type);
+		if (!fstype) {
+			error = -ENODEV;
+			goto out;
+		}
+		if (fstype->fs_flags & FS_REQUIRES_DEV)
+			/* dev_name is a block device file. */
+			need_dev = 1;
+	}
+	if (need_dev) {
+		/* Get mount point or device file. */
+		if (ccs_get_path(dev_name, &obj.path1)) {
+			error = -ENOENT;
+			goto out;
+		}
+		requested_dev_name = ccs_realpath(&obj.path1);
+		if (!requested_dev_name) {
+			error = -ENOENT;
+			goto out;
+		}
+	} else {
+		/* Map dev_name to "<NULL>" if no dev_name given. */
+		if (!dev_name)
+			dev_name = "<NULL>";
+		requested_dev_name = ccs_encode(dev_name);
+		if (!requested_dev_name) {
+			error = -ENOMEM;
+			goto out;
+		}
+	}
+	rdev.name = requested_dev_name;
+	ccs_fill_path_info(&rdev);
+	r->param_type = CCS_TYPE_MOUNT_ACL;
+	r->param.mount.need_dev = need_dev;
+	r->param.mount.dev = &rdev;
+	r->param.mount.dir = &rdir;
+	r->param.mount.type = &rtype;
+	r->param.mount.flags = flags;
+	error = ccs_check_acl(r);
+out:
+	kfree(requested_dev_name);
+	kfree(requested_dir_name);
+	if (fstype)
+		ccs_put_filesystem(fstype);
+	kfree(requested_type);
+	/* Drop refcount obtained by ccs_get_path(). */
+	if (obj.path1.dentry)
+		path_put(&obj.path1);
+	return error;
+}
+
+/**
+ * __ccs_mount_permission - Check permission for mount() operation.
+ *
+ * @dev_name:  Name of device file. Maybe NULL.
+ * @path:      Pointer to "struct path".
+ * @type:      Name of filesystem type. Maybe NULL.
+ * @flags:     Mount options.
+ * @data_page: Optional data. Maybe NULL.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_mount_permission(const char *dev_name, struct path *path,
+				  const char *type, unsigned long flags,
+				  void *data_page)
+{
+	struct ccs_request_info r;
+	int error = 0;
+	int idx;
+	if ((flags & MS_MGC_MSK) == MS_MGC_VAL)
+		flags &= ~MS_MGC_MSK;
+	if (flags & MS_REMOUNT) {
+		type = ccs_mounts[CCS_MOUNT_REMOUNT];
+		flags &= ~MS_REMOUNT;
+	} else if (flags & MS_BIND) {
+		type = ccs_mounts[CCS_MOUNT_BIND];
+		flags &= ~MS_BIND;
+	} else if (flags & MS_SHARED) {
+		if (flags & (MS_PRIVATE | MS_SLAVE | MS_UNBINDABLE))
+			return -EINVAL;
+		type = ccs_mounts[CCS_MOUNT_MAKE_SHARED];
+		flags &= ~MS_SHARED;
+	} else if (flags & MS_PRIVATE) {
+		if (flags & (MS_SHARED | MS_SLAVE | MS_UNBINDABLE))
+			return -EINVAL;
+		type = ccs_mounts[CCS_MOUNT_MAKE_PRIVATE];
+		flags &= ~MS_PRIVATE;
+	} else if (flags & MS_SLAVE) {
+		if (flags & (MS_SHARED | MS_PRIVATE | MS_UNBINDABLE))
+			return -EINVAL;
+		type = ccs_mounts[CCS_MOUNT_MAKE_SLAVE];
+		flags &= ~MS_SLAVE;
+	} else if (flags & MS_UNBINDABLE) {
+		if (flags & (MS_SHARED | MS_PRIVATE | MS_SLAVE))
+			return -EINVAL;
+		type = ccs_mounts[CCS_MOUNT_MAKE_UNBINDABLE];
+		flags &= ~MS_UNBINDABLE;
+	} else if (flags & MS_MOVE) {
+		type = ccs_mounts[CCS_MOUNT_MOVE];
+		flags &= ~MS_MOVE;
+	}
+	if (!type)
+		type = "<NULL>";
+	idx = ccs_read_lock();
+	if (ccs_init_request_info(&r, CCS_MAC_FILE_MOUNT)
+	    != CCS_CONFIG_DISABLED)
+		error = ccs_mount_acl(&r, dev_name, path, type, flags);
+	ccs_read_unlock(idx);
+	return error;
+}
+
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 24)
+
+/**
+ * ccs_old_mount_permission - Check permission for mount() operation.
+ *
+ * @dev_name:  Name of device file.
+ * @nd:        Pointer to "struct nameidata".
+ * @type:      Name of filesystem type. Maybe NULL.
+ * @flags:     Mount options.
+ * @data_page: Optional data. Maybe NULL.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_old_mount_permission(const char *dev_name, struct nameidata *nd,
+				    const char *type, unsigned long flags,
+				    void *data_page)
+{
+	struct path path = { nd->mnt, nd->dentry };
+	return __ccs_mount_permission(dev_name, &path, type, flags, data_page);
+}
+
+#endif
+
+/**
+ * ccs_compare_number_union - Check whether a value matches "struct ccs_number_union" or not.
+ *
+ * @value: Number to check.
+ * @ptr:   Pointer to "struct ccs_number_union".
+ *
+ * Returns true if @value matches @ptr, false otherwise.
+ */
+static bool ccs_compare_number_union(const unsigned long value,
+				     const struct ccs_number_union *ptr)
+{
+	if (ptr->group)
+		return ccs_number_matches_group(value, value, ptr->group);
+	return value >= ptr->values[0] && value <= ptr->values[1];
+}
+
+/**
+ * ccs_compare_name_union - Check whether a name matches "struct ccs_name_union" or not.
+ *
+ * @name: Pointer to "struct ccs_path_info".
+ * @ptr:  Pointer to "struct ccs_name_union".
+ *
+ * Returns "struct ccs_path_info" if @name matches @ptr, NULL otherwise.
+ */
+static const struct ccs_path_info *ccs_compare_name_union
+(const struct ccs_path_info *name, const struct ccs_name_union *ptr)
+{
+	if (ptr->group)
+		return ccs_path_matches_group(name, ptr->group);
+	if (ccs_path_matches_pattern(name, ptr->filename))
+		return ptr->filename;
+	return NULL;
+}
+
+/**
+ * ccs_add_slash - Add trailing '/' if needed.
+ *
+ * @buf: Pointer to "struct ccs_path_info".
+ *
+ * Returns nothing.
+ *
+ * @buf must be generated by ccs_encode() because this function does not
+ * allocate memory for adding '/'.
+ */
+static void ccs_add_slash(struct ccs_path_info *buf)
+{
+	if (buf->is_dir)
+		return;
+	/* This is OK because ccs_encode() reserves space for appending "/". */
+	strcat((char *) buf->name, "/");
+	ccs_fill_path_info(buf);
+}
+
+/**
+ * ccs_get_realpath - Get realpath.
+ *
+ * @buf:  Pointer to "struct ccs_path_info".
+ * @path: Pointer to "struct path". @path->mnt may be NULL.
+ *
+ * Returns true on success, false otherwise.
+ */
+static bool ccs_get_realpath(struct ccs_path_info *buf, struct path *path)
+{
+	buf->name = ccs_realpath(path);
+	if (buf->name) {
+		ccs_fill_path_info(buf);
+		return true;
+	}
+	return false;
+}
+
+/**
+ * ccs_check_path_acl - Check permission for path operation.
+ *
+ * @r:   Pointer to "struct ccs_request_info".
+ * @ptr: Pointer to "struct ccs_acl_info".
+ *
+ * Returns true if granted, false otherwise.
+ *
+ * To be able to use wildcard for domain transition, this function sets
+ * matching entry on success. Since the caller holds ccs_read_lock(),
+ * it is safe to set matching entry.
+ */
+static bool ccs_check_path_acl(struct ccs_request_info *r,
+			       const struct ccs_acl_info *ptr)
+{
+	const struct ccs_path_acl *acl = container_of(ptr, typeof(*acl), head);
+	if (ptr->perm & (1 << r->param.path.operation)) {
+		r->param.path.matched_path =
+			ccs_compare_name_union(r->param.path.filename,
+					       &acl->name);
+		return r->param.path.matched_path != NULL;
+	}
+	return false;
+}
+
+/**
+ * ccs_check_path_number_acl - Check permission for path number operation.
+ *
+ * @r:   Pointer to "struct ccs_request_info".
+ * @ptr: Pointer to "struct ccs_acl_info".
+ *
+ * Returns true if granted, false otherwise.
+ */
+static bool ccs_check_path_number_acl(struct ccs_request_info *r,
+				      const struct ccs_acl_info *ptr)
+{
+	const struct ccs_path_number_acl *acl =
+		container_of(ptr, typeof(*acl), head);
+	return (ptr->perm & (1 << r->param.path_number.operation)) &&
+		ccs_compare_number_union(r->param.path_number.number,
+					 &acl->number) &&
+		ccs_compare_name_union(r->param.path_number.filename,
+				       &acl->name);
+}
+
+/**
+ * ccs_check_path2_acl - Check permission for path path operation.
+ *
+ * @r:   Pointer to "struct ccs_request_info".
+ * @ptr: Pointer to "struct ccs_acl_info".
+ *
+ * Returns true if granted, false otherwise.
+ */
+static bool ccs_check_path2_acl(struct ccs_request_info *r,
+				const struct ccs_acl_info *ptr)
+{
+	const struct ccs_path2_acl *acl =
+		container_of(ptr, typeof(*acl), head);
+	return (ptr->perm & (1 << r->param.path2.operation)) &&
+		ccs_compare_name_union(r->param.path2.filename1, &acl->name1)
+		&& ccs_compare_name_union(r->param.path2.filename2,
+					  &acl->name2);
+}
+
+/**
+ * ccs_check_mkdev_acl - Check permission for path number number number operation.
+ *
+ * @r:   Pointer to "struct ccs_request_info".
+ * @ptr: Pointer to "struct ccs_acl_info".
+ *
+ * Returns true if granted, false otherwise.
+ */
+static bool ccs_check_mkdev_acl(struct ccs_request_info *r,
+				const struct ccs_acl_info *ptr)
+{
+	const struct ccs_mkdev_acl *acl =
+		container_of(ptr, typeof(*acl), head);
+	return (ptr->perm & (1 << r->param.mkdev.operation)) &&
+		ccs_compare_number_union(r->param.mkdev.mode, &acl->mode) &&
+		ccs_compare_number_union(r->param.mkdev.major, &acl->major) &&
+		ccs_compare_number_union(r->param.mkdev.minor, &acl->minor) &&
+		ccs_compare_name_union(r->param.mkdev.filename, &acl->name);
+}
+
+/**
+ * ccs_path_permission - Check permission for path operation.
+ *
+ * @r:         Pointer to "struct ccs_request_info".
+ * @operation: Type of operation.
+ * @filename:  Filename to check.
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static int ccs_path_permission(struct ccs_request_info *r, u8 operation,
+			       const struct ccs_path_info *filename)
+{
+	r->type = ccs_p2mac[operation];
+	r->mode = ccs_get_mode(r->profile, r->type);
+	if (r->mode == CCS_CONFIG_DISABLED)
+		return 0;
+	r->param_type = CCS_TYPE_PATH_ACL;
+	r->param.path.filename = filename;
+	r->param.path.operation = operation;
+	return ccs_check_acl(r);
+}
+
+/**
+ * ccs_execute_permission - Check permission for execute operation.
+ *
+ * @r:         Pointer to "struct ccs_request_info".
+ * @filename:  Filename to check.
+ *
+ * Returns 0 on success, CCS_RETRY_REQUEST on retry, negative value otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static int ccs_execute_permission(struct ccs_request_info *r,
+				  const struct ccs_path_info *filename)
+{
+	int error;
+	/*
+	 * Unlike other permission checks, this check is done regardless of
+	 * profile mode settings in order to check for domain transition
+	 * preference.
+	 */
+	r->type = CCS_MAC_FILE_EXECUTE;
+	r->mode = ccs_get_mode(r->profile, r->type);
+	r->param_type = CCS_TYPE_PATH_ACL;
+	r->param.path.filename = filename;
+	r->param.path.operation = CCS_TYPE_EXECUTE;
+	error = ccs_check_acl(r);
+	r->ee->transition = r->matched_acl && r->matched_acl->cond &&
+		r->matched_acl->cond->exec_transit ?
+		r->matched_acl->cond->transit : NULL;
+	return error;
+}
+
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 32)
+
+/**
+ * __ccs_save_open_mode - Remember original flags passed to sys_open().
+ *
+ * @mode: Flags passed to sys_open().
+ *
+ * Returns nothing.
+ *
+ * TOMOYO does not check "file write" if open(path, O_TRUNC | O_RDONLY) was
+ * requested because write() is not permitted. Instead, TOMOYO checks
+ * "file truncate" if O_TRUNC is passed.
+ *
+ * TOMOYO does not check "file read" and "file write" if open(path, 3) was
+ * requested because read()/write() are not permitted. Instead, TOMOYO checks
+ * "file ioctl" when ioctl() is requested.
+ */
+static void __ccs_save_open_mode(int mode)
+{
+	if ((mode & 3) == 3)
+		ccs_current_security()->ccs_flags |= CCS_OPEN_FOR_IOCTL_ONLY;
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 14)
+	/* O_TRUNC passes MAY_WRITE to ccs_open_permission(). */
+	else if (!(mode & 3) && (mode & O_TRUNC))
+		ccs_current_security()->ccs_flags |=
+			CCS_OPEN_FOR_READ_TRUNCATE;
+#endif
+}
+
+/**
+ * __ccs_clear_open_mode - Forget original flags passed to sys_open().
+ *
+ * Returns nothing.
+ */
+static void __ccs_clear_open_mode(void)
+{
+	ccs_current_security()->ccs_flags &= ~(CCS_OPEN_FOR_IOCTL_ONLY |
+					       CCS_OPEN_FOR_READ_TRUNCATE);
+}
+
+#endif
+
+/**
+ * __ccs_open_permission - Check permission for "read" and "write".
+ *
+ * @dentry: Pointer to "struct dentry".
+ * @mnt:    Pointer to "struct vfsmount". Maybe NULL.
+ * @flag:   Flags for open().
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_open_permission(struct dentry *dentry, struct vfsmount *mnt,
+				 const int flag)
+{
+	struct ccs_request_info r;
+	struct ccs_obj_info obj = {
+		.path1.dentry = dentry,
+		.path1.mnt = mnt,
+	};
+	const u32 ccs_flags = ccs_current_flags();
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33)
+	const u8 acc_mode = (flag & 3) == 3 ? 0 : ACC_MODE(flag);
+#else
+	const u8 acc_mode = (ccs_flags & CCS_OPEN_FOR_IOCTL_ONLY) ? 0 :
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 14)
+		(ccs_flags & CCS_OPEN_FOR_READ_TRUNCATE) ? 4 :
+#endif
+		ACC_MODE(flag);
+#endif
+	int error = 0;
+	struct ccs_path_info buf;
+	int idx;
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)
+	if (current->in_execve && !(ccs_flags & CCS_TASK_IS_IN_EXECVE))
+		return 0;
+#endif
+#ifndef CONFIG_CCSECURITY_FILE_READDIR
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)
+	if (d_is_dir(dentry))
+		return 0;
+#else
+	if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
+		return 0;
+#endif
+#endif
+	buf.name = NULL;
+	r.mode = CCS_CONFIG_DISABLED;
+	idx = ccs_read_lock();
+	if (acc_mode && ccs_init_request_info(&r, CCS_MAC_FILE_OPEN)
+	    != CCS_CONFIG_DISABLED) {
+		if (!ccs_get_realpath(&buf, &obj.path1)) {
+			error = -ENOMEM;
+			goto out;
+		}
+		r.obj = &obj;
+		if (acc_mode & MAY_READ)
+			error = ccs_path_permission(&r, CCS_TYPE_READ, &buf);
+		if (!error && (acc_mode & MAY_WRITE))
+			error = ccs_path_permission(&r, (flag & O_APPEND) ?
+						    CCS_TYPE_APPEND :
+						    CCS_TYPE_WRITE, &buf);
+	}
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 32)
+	if (!error && (flag & O_TRUNC) &&
+	    ccs_init_request_info(&r, CCS_MAC_FILE_TRUNCATE)
+	    != CCS_CONFIG_DISABLED) {
+		if (!buf.name && !ccs_get_realpath(&buf, &obj.path1)) {
+			error = -ENOMEM;
+			goto out;
+		}
+		r.obj = &obj;
+		error = ccs_path_permission(&r, CCS_TYPE_TRUNCATE, &buf);
+	}
+#endif
+out:
+	kfree(buf.name);
+	ccs_read_unlock(idx);
+	if (r.mode != CCS_CONFIG_ENFORCING)
+		error = 0;
+	return error;
+}
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33)
+
+/**
+ * ccs_new_open_permission - Check permission for "read" and "write".
+ *
+ * @filp: Pointer to "struct file".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_new_open_permission(struct file *filp)
+{
+	return __ccs_open_permission(filp->f_path.dentry, filp->f_path.mnt,
+				     filp->f_flags);
+}
+
+#endif
+
+/**
+ * ccs_path_perm - Check permission for "unlink", "rmdir", "truncate", "symlink", "append", "getattr", "chroot" and "unmount".
+ *
+ * @operation: Type of operation.
+ * @dentry:    Pointer to "struct dentry".
+ * @mnt:       Pointer to "struct vfsmount". Maybe NULL.
+ * @target:    Symlink's target if @operation is CCS_TYPE_SYMLINK,
+ *             NULL otherwise.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_path_perm(const u8 operation, struct dentry *dentry,
+			 struct vfsmount *mnt, const char *target)
+{
+	struct ccs_request_info r;
+	struct ccs_obj_info obj = {
+		.path1.dentry = dentry,
+		.path1.mnt = mnt,
+	};
+	int error = 0;
+	struct ccs_path_info buf;
+	bool is_enforce = false;
+	struct ccs_path_info symlink_target;
+	int idx;
+	buf.name = NULL;
+	symlink_target.name = NULL;
+	idx = ccs_read_lock();
+	if (ccs_init_request_info(&r, ccs_p2mac[operation])
+	    == CCS_CONFIG_DISABLED)
+		goto out;
+	is_enforce = (r.mode == CCS_CONFIG_ENFORCING);
+	error = -ENOMEM;
+	if (!ccs_get_realpath(&buf, &obj.path1))
+		goto out;
+	r.obj = &obj;
+	switch (operation) {
+	case CCS_TYPE_RMDIR:
+	case CCS_TYPE_CHROOT:
+		ccs_add_slash(&buf);
+		break;
+	case CCS_TYPE_SYMLINK:
+		symlink_target.name = ccs_encode(target);
+		if (!symlink_target.name)
+			goto out;
+		ccs_fill_path_info(&symlink_target);
+		obj.symlink_target = &symlink_target;
+		break;
+	}
+	error = ccs_path_permission(&r, operation, &buf);
+	if (operation == CCS_TYPE_SYMLINK)
+		kfree(symlink_target.name);
+out:
+	kfree(buf.name);
+	ccs_read_unlock(idx);
+	if (!is_enforce)
+		error = 0;
+	return error;
+}
+
+/**
+ * ccs_mkdev_perm - Check permission for "mkblock" and "mkchar".
+ *
+ * @operation: Type of operation. (CCS_TYPE_MKCHAR or CCS_TYPE_MKBLOCK)
+ * @dentry:    Pointer to "struct dentry".
+ * @mnt:       Pointer to "struct vfsmount". Maybe NULL.
+ * @mode:      Create mode.
+ * @dev:       Device number.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_mkdev_perm(const u8 operation, struct dentry *dentry,
+			  struct vfsmount *mnt, const unsigned int mode,
+			  unsigned int dev)
+{
+	struct ccs_request_info r;
+	struct ccs_obj_info obj = {
+		.path1.dentry = dentry,
+		.path1.mnt = mnt,
+	};
+	int error = 0;
+	struct ccs_path_info buf;
+	bool is_enforce = false;
+	int idx;
+	idx = ccs_read_lock();
+	if (ccs_init_request_info(&r, ccs_pnnn2mac[operation])
+	    == CCS_CONFIG_DISABLED)
+		goto out;
+	is_enforce = (r.mode == CCS_CONFIG_ENFORCING);
+	error = -EPERM;
+	if (!capable(CAP_MKNOD))
+		goto out;
+	error = -ENOMEM;
+	if (!ccs_get_realpath(&buf, &obj.path1))
+		goto out;
+	r.obj = &obj;
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
+	dev = new_decode_dev(dev);
+#endif
+	r.param_type = CCS_TYPE_MKDEV_ACL;
+	r.param.mkdev.filename = &buf;
+	r.param.mkdev.operation = operation;
+	r.param.mkdev.mode = mode;
+	r.param.mkdev.major = MAJOR(dev);
+	r.param.mkdev.minor = MINOR(dev);
+	error = ccs_check_acl(&r);
+	kfree(buf.name);
+out:
+	ccs_read_unlock(idx);
+	if (!is_enforce)
+		error = 0;
+	return error;
+}
+
+/**
+ * ccs_path2_perm - Check permission for "rename", "link" and "pivot_root".
+ *
+ * @operation: Type of operation.
+ * @dentry1:   Pointer to "struct dentry".
+ * @mnt1:      Pointer to "struct vfsmount". Maybe NULL.
+ * @dentry2:   Pointer to "struct dentry".
+ * @mnt2:      Pointer to "struct vfsmount". Maybe NULL.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_path2_perm(const u8 operation, struct dentry *dentry1,
+			  struct vfsmount *mnt1, struct dentry *dentry2,
+			  struct vfsmount *mnt2)
+{
+	struct ccs_request_info r;
+	int error = 0;
+	struct ccs_path_info buf1;
+	struct ccs_path_info buf2;
+	bool is_enforce = false;
+	struct ccs_obj_info obj = {
+		.path1.dentry = dentry1,
+		.path1.mnt = mnt1,
+		.path2.dentry = dentry2,
+		.path2.mnt = mnt2,
+	};
+	int idx;
+	buf1.name = NULL;
+	buf2.name = NULL;
+	idx = ccs_read_lock();
+	if (ccs_init_request_info(&r, ccs_pp2mac[operation])
+	    == CCS_CONFIG_DISABLED)
+		goto out;
+	is_enforce = (r.mode == CCS_CONFIG_ENFORCING);
+	error = -ENOMEM;
+	if (!ccs_get_realpath(&buf1, &obj.path1) ||
+	    !ccs_get_realpath(&buf2, &obj.path2))
+		goto out;
+	switch (operation) {
+	case CCS_TYPE_RENAME:
+	case CCS_TYPE_LINK:
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)
+		if (!d_is_dir(dentry1))
+			break;
+#else
+		if (!dentry1->d_inode || !S_ISDIR(dentry1->d_inode->i_mode))
+			break;
+#endif
+		/* fall through */
+	case CCS_TYPE_PIVOT_ROOT:
+		ccs_add_slash(&buf1);
+		ccs_add_slash(&buf2);
+		break;
+	}
+	r.obj = &obj;
+	r.param_type = CCS_TYPE_PATH2_ACL;
+	r.param.path2.operation = operation;
+	r.param.path2.filename1 = &buf1;
+	r.param.path2.filename2 = &buf2;
+	error = ccs_check_acl(&r);
+out:
+	kfree(buf1.name);
+	kfree(buf2.name);
+	ccs_read_unlock(idx);
+	if (!is_enforce)
+		error = 0;
+	return error;
+}
+
+/**
+ * ccs_path_number_perm - Check permission for "create", "mkdir", "mkfifo", "mksock", "ioctl", "chmod", "chown", "chgrp".
+ *
+ * @type:   Type of operation.
+ * @dentry: Pointer to "struct dentry".
+ * @vfsmnt: Pointer to "struct vfsmount". Maybe NULL.
+ * @number: Number.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_path_number_perm(const u8 type, struct dentry *dentry,
+				struct vfsmount *vfsmnt, unsigned long number)
+{
+	struct ccs_request_info r;
+	struct ccs_obj_info obj = {
+		.path1.dentry = dentry,
+		.path1.mnt = vfsmnt,
+	};
+	int error = 0;
+	struct ccs_path_info buf;
+	int idx;
+	if (!dentry)
+		return 0;
+	idx = ccs_read_lock();
+	if (ccs_init_request_info(&r, ccs_pn2mac[type]) == CCS_CONFIG_DISABLED)
+		goto out;
+	error = -ENOMEM;
+	if (!ccs_get_realpath(&buf, &obj.path1))
+		goto out;
+	r.obj = &obj;
+	if (type == CCS_TYPE_MKDIR)
+		ccs_add_slash(&buf);
+	r.param_type = CCS_TYPE_PATH_NUMBER_ACL;
+	r.param.path_number.operation = type;
+	r.param.path_number.filename = &buf;
+	r.param.path_number.number = number;
+	error = ccs_check_acl(&r);
+	kfree(buf.name);
+out:
+	ccs_read_unlock(idx);
+	if (r.mode != CCS_CONFIG_ENFORCING)
+		error = 0;
+	return error;
+}
+
+/**
+ * __ccs_ioctl_permission - Check permission for "ioctl".
+ *
+ * @filp: Pointer to "struct file".
+ * @cmd:  Ioctl command number.
+ * @arg:  Param for @cmd.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_ioctl_permission(struct file *filp, unsigned int cmd,
+				  unsigned long arg)
+{
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
+	return ccs_path_number_perm(CCS_TYPE_IOCTL, filp->f_path.dentry,
+				    filp->f_path.mnt, cmd);
+#else
+	return ccs_path_number_perm(CCS_TYPE_IOCTL, filp->f_dentry,
+				    filp->f_vfsmnt, cmd);
+#endif
+}
+
+/**
+ * __ccs_chmod_permission - Check permission for "chmod".
+ *
+ * @dentry: Pointer to "struct dentry".
+ * @vfsmnt: Pointer to "struct vfsmount". Maybe NULL.
+ * @mode:   Mode.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_chmod_permission(struct dentry *dentry,
+				  struct vfsmount *vfsmnt, mode_t mode)
+{
+	return ccs_path_number_perm(CCS_TYPE_CHMOD, dentry, vfsmnt,
+				    mode & S_IALLUGO);
+}
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 5, 0)
+
+/**
+ * __ccs_chown_permission - Check permission for "chown/chgrp".
+ *
+ * @dentry: Pointer to "struct dentry".
+ * @vfsmnt: Pointer to "struct vfsmount". Maybe NULL.
+ * @user:   User ID.
+ * @group:  Group ID.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_chown_permission(struct dentry *dentry,
+				  struct vfsmount *vfsmnt, kuid_t user,
+				  kgid_t group)
+{
+	int error = 0;
+	if (uid_valid(user))
+		error = ccs_path_number_perm(CCS_TYPE_CHOWN, dentry, vfsmnt,
+					     from_kuid(&init_user_ns, user));
+	if (!error && gid_valid(group))
+		error = ccs_path_number_perm(CCS_TYPE_CHGRP, dentry, vfsmnt,
+					     from_kgid(&init_user_ns, group));
+	return error;
+}
+
+#else
+
+/**
+ * __ccs_chown_permission - Check permission for "chown/chgrp".
+ *
+ * @dentry: Pointer to "struct dentry".
+ * @vfsmnt: Pointer to "struct vfsmount". Maybe NULL.
+ * @user:   User ID.
+ * @group:  Group ID.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_chown_permission(struct dentry *dentry,
+				  struct vfsmount *vfsmnt, uid_t user,
+				  gid_t group)
+{
+	int error = 0;
+	if (user == (uid_t) -1 && group == (gid_t) -1)
+		return 0;
+	if (user != (uid_t) -1)
+		error = ccs_path_number_perm(CCS_TYPE_CHOWN, dentry, vfsmnt,
+					     user);
+	if (!error && group != (gid_t) -1)
+		error = ccs_path_number_perm(CCS_TYPE_CHGRP, dentry, vfsmnt,
+					     group);
+	return error;
+}
+
+#endif
+
+/**
+ * __ccs_fcntl_permission - Check permission for changing O_APPEND flag.
+ *
+ * @file: Pointer to "struct file".
+ * @cmd:  Command number.
+ * @arg:  Value for @cmd.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_fcntl_permission(struct file *file, unsigned int cmd,
+				  unsigned long arg)
+{
+	if (!(cmd == F_SETFL && ((arg ^ file->f_flags) & O_APPEND)))
+		return 0;
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 33)
+	return __ccs_open_permission(file->f_path.dentry, file->f_path.mnt,
+				     O_WRONLY | (arg & O_APPEND));
+#elif defined(RHEL_MAJOR) && RHEL_MAJOR == 6
+	return __ccs_open_permission(file->f_dentry, file->f_vfsmnt,
+				     O_WRONLY | (arg & O_APPEND));
+#else
+	return __ccs_open_permission(file->f_dentry, file->f_vfsmnt,
+				     (O_WRONLY + 1) | (arg & O_APPEND));
+#endif
+}
+
+/**
+ * __ccs_pivot_root_permission - Check permission for pivot_root().
+ *
+ * @old_path: Pointer to "struct path".
+ * @new_path: Pointer to "struct path".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_pivot_root_permission(struct path *old_path,
+				       struct path *new_path)
+{
+	return ccs_path2_perm(CCS_TYPE_PIVOT_ROOT, new_path->dentry,
+			      new_path->mnt, old_path->dentry, old_path->mnt);
+}
+
+/**
+ * __ccs_chroot_permission - Check permission for chroot().
+ *
+ * @path: Pointer to "struct path".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_chroot_permission(struct path *path)
+{
+	return ccs_path_perm(CCS_TYPE_CHROOT, path->dentry, path->mnt, NULL);
+}
+
+/**
+ * __ccs_umount_permission - Check permission for unmount.
+ *
+ * @mnt:   Pointer to "struct vfsmount".
+ * @flags: Unused.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_umount_permission(struct vfsmount *mnt, int flags)
+{
+	return ccs_path_perm(CCS_TYPE_UMOUNT, mnt->mnt_root, mnt, NULL);
+}
+
+/**
+ * __ccs_mknod_permission - Check permission for vfs_mknod().
+ *
+ * @dentry: Pointer to "struct dentry".
+ * @mnt:    Pointer to "struct vfsmount". Maybe NULL.
+ * @mode:   Device type and permission.
+ * @dev:    Device number for block or character device.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_mknod_permission(struct dentry *dentry, struct vfsmount *mnt,
+				  const unsigned int mode, unsigned int dev)
+{
+	int error = 0;
+	const unsigned int perm = mode & S_IALLUGO;
+	switch (mode & S_IFMT) {
+	case S_IFCHR:
+		error = ccs_mkdev_perm(CCS_TYPE_MKCHAR, dentry, mnt, perm,
+				       dev);
+		break;
+	case S_IFBLK:
+		error = ccs_mkdev_perm(CCS_TYPE_MKBLOCK, dentry, mnt, perm,
+				       dev);
+		break;
+	case S_IFIFO:
+		error = ccs_path_number_perm(CCS_TYPE_MKFIFO, dentry, mnt,
+					     perm);
+		break;
+	case S_IFSOCK:
+		error = ccs_path_number_perm(CCS_TYPE_MKSOCK, dentry, mnt,
+					     perm);
+		break;
+	case 0:
+	case S_IFREG:
+		error = ccs_path_number_perm(CCS_TYPE_CREATE, dentry, mnt,
+					     perm);
+		break;
+	}
+	return error;
+}
+
+/**
+ * __ccs_mkdir_permission - Check permission for vfs_mkdir().
+ *
+ * @dentry: Pointer to "struct dentry".
+ * @mnt:    Pointer to "struct vfsmount". Maybe NULL.
+ * @mode:   Create mode.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_mkdir_permission(struct dentry *dentry, struct vfsmount *mnt,
+				  unsigned int mode)
+{
+	return ccs_path_number_perm(CCS_TYPE_MKDIR, dentry, mnt, mode);
+}
+
+/**
+ * __ccs_rmdir_permission - Check permission for vfs_rmdir().
+ *
+ * @dentry: Pointer to "struct dentry".
+ * @mnt:    Pointer to "struct vfsmount". Maybe NULL.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_rmdir_permission(struct dentry *dentry, struct vfsmount *mnt)
+{
+	return ccs_path_perm(CCS_TYPE_RMDIR, dentry, mnt, NULL);
+}
+
+/**
+ * __ccs_unlink_permission - Check permission for vfs_unlink().
+ *
+ * @dentry: Pointer to "struct dentry".
+ * @mnt:    Pointer to "struct vfsmount". Maybe NULL.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_unlink_permission(struct dentry *dentry, struct vfsmount *mnt)
+{
+	return ccs_path_perm(CCS_TYPE_UNLINK, dentry, mnt, NULL);
+}
+
+#ifdef CONFIG_CCSECURITY_FILE_GETATTR
+
+/**
+ * __ccs_getattr_permission - Check permission for vfs_getattr().
+ *
+ * @mnt:    Pointer to "struct vfsmount". Maybe NULL.
+ * @dentry: Pointer to "struct dentry".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_getattr_permission(struct vfsmount *mnt,
+				    struct dentry *dentry)
+{
+	return ccs_path_perm(CCS_TYPE_GETATTR, dentry, mnt, NULL);
+}
+
+#endif
+
+/**
+ * __ccs_symlink_permission - Check permission for vfs_symlink().
+ *
+ * @dentry: Pointer to "struct dentry".
+ * @mnt:    Pointer to "struct vfsmount". Maybe NULL.
+ * @from:   Content of symlink.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_symlink_permission(struct dentry *dentry,
+				    struct vfsmount *mnt, const char *from)
+{
+	return ccs_path_perm(CCS_TYPE_SYMLINK, dentry, mnt, from);
+}
+
+/**
+ * __ccs_truncate_permission - Check permission for notify_change().
+ *
+ * @dentry: Pointer to "struct dentry".
+ * @mnt:    Pointer to "struct vfsmount". Maybe NULL.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_truncate_permission(struct dentry *dentry,
+				     struct vfsmount *mnt)
+{
+	return ccs_path_perm(CCS_TYPE_TRUNCATE, dentry, mnt, NULL);
+}
+
+/**
+ * __ccs_rename_permission - Check permission for vfs_rename().
+ *
+ * @old_dentry: Pointer to "struct dentry".
+ * @new_dentry: Pointer to "struct dentry".
+ * @mnt:        Pointer to "struct vfsmount". Maybe NULL.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_rename_permission(struct dentry *old_dentry,
+				   struct dentry *new_dentry,
+				   struct vfsmount *mnt)
+{
+	return ccs_path2_perm(CCS_TYPE_RENAME, old_dentry, mnt, new_dentry,
+			      mnt);
+}
+
+/**
+ * __ccs_link_permission - Check permission for vfs_link().
+ *
+ * @old_dentry: Pointer to "struct dentry".
+ * @new_dentry: Pointer to "struct dentry".
+ * @mnt:        Pointer to "struct vfsmount". Maybe NULL.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_link_permission(struct dentry *old_dentry,
+				 struct dentry *new_dentry,
+				 struct vfsmount *mnt)
+{
+	return ccs_path2_perm(CCS_TYPE_LINK, old_dentry, mnt, new_dentry, mnt);
+}
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
+
+/**
+ * __ccs_open_exec_permission - Check permission for open_exec().
+ *
+ * @dentry: Pointer to "struct dentry".
+ * @mnt:    Pointer to "struct vfsmount".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_open_exec_permission(struct dentry *dentry,
+				      struct vfsmount *mnt)
+{
+	return (ccs_current_flags() & CCS_TASK_IS_IN_EXECVE) ?
+		__ccs_open_permission(dentry, mnt, O_RDONLY + 1) : 0;
+}
+
+/**
+ * __ccs_uselib_permission - Check permission for sys_uselib().
+ *
+ * @dentry: Pointer to "struct dentry".
+ * @mnt:    Pointer to "struct vfsmount".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_uselib_permission(struct dentry *dentry, struct vfsmount *mnt)
+{
+	return __ccs_open_permission(dentry, mnt, O_RDONLY + 1);
+}
+
+#endif
+
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 18) || (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33) && defined(CONFIG_SYSCTL_SYSCALL))
+
+/**
+ * __ccs_parse_table - Check permission for parse_table().
+ *
+ * @name:   Pointer to "int __user".
+ * @nlen:   Number of elements in @name.
+ * @oldval: Pointer to "void __user".
+ * @newval: Pointer to "void __user".
+ * @table:  Pointer to "struct ctl_table".
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * Note that this function is racy because this function checks values in
+ * userspace memory which could be changed after permission check.
+ */
+static int __ccs_parse_table(int __user *name, int nlen, void __user *oldval,
+			     void __user *newval, struct ctl_table *table)
+{
+	int n;
+	int error = -ENOMEM;
+	int op = 0;
+	struct ccs_path_info buf;
+	char *buffer = NULL;
+	struct ccs_request_info r;
+	int idx;
+	if (oldval)
+		op |= 004;
+	if (newval)
+		op |= 002;
+	if (!op) /* Neither read nor write */
+		return 0;
+	idx = ccs_read_lock();
+	if (ccs_init_request_info(&r, CCS_MAC_FILE_OPEN)
+	    == CCS_CONFIG_DISABLED) {
+		error = 0;
+		goto out;
+	}
+	buffer = kmalloc(PAGE_SIZE, CCS_GFP_FLAGS);
+	if (!buffer)
+		goto out;
+	snprintf(buffer, PAGE_SIZE - 1, "proc:/sys");
+repeat:
+	if (!nlen) {
+		error = -ENOTDIR;
+		goto out;
+	}
+	if (get_user(n, name)) {
+		error = -EFAULT;
+		goto out;
+	}
+	for ( ; table->ctl_name
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 21)
+		      || table->procname
+#endif
+		      ; table++) {
+		int pos;
+		const char *cp;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 21)
+		if (n != table->ctl_name && table->ctl_name != CTL_ANY)
+			continue;
+#else
+		if (!n || n != table->ctl_name)
+			continue;
+#endif
+		pos = strlen(buffer);
+		cp = table->procname;
+		error = -ENOMEM;
+		if (cp) {
+			int len = strlen(cp);
+			if (len + 2 > PAGE_SIZE - 1)
+				goto out;
+			buffer[pos++] = '/';
+			memmove(buffer + pos, cp, len + 1);
+		} else {
+			/* Assume nobody assigns "=\$=" for procname. */
+			snprintf(buffer + pos, PAGE_SIZE - pos - 1,
+				 "/=%d=", table->ctl_name);
+			if (!memchr(buffer, '\0', PAGE_SIZE - 2))
+				goto out;
+		}
+		if (!table->child)
+			goto no_child;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 21)
+		if (!table->strategy)
+			goto no_strategy;
+		/* printk("sysctl='%s'\n", buffer); */
+		buf.name = ccs_encode(buffer);
+		if (!buf.name)
+			goto out;
+		ccs_fill_path_info(&buf);
+		if (op & MAY_READ)
+			error = ccs_path_permission(&r, CCS_TYPE_READ, &buf);
+		else
+			error = 0;
+		if (!error && (op & MAY_WRITE))
+			error = ccs_path_permission(&r, CCS_TYPE_WRITE, &buf);
+		kfree(buf.name);
+		if (error)
+			goto out;
+no_strategy:
+#endif
+		name++;
+		nlen--;
+		table = table->child;
+		goto repeat;
+no_child:
+		/* printk("sysctl='%s'\n", buffer); */
+		buf.name = ccs_encode(buffer);
+		if (!buf.name)
+			goto out;
+		ccs_fill_path_info(&buf);
+		if (op & MAY_READ)
+			error = ccs_path_permission(&r, CCS_TYPE_READ, &buf);
+		else
+			error = 0;
+		if (!error && (op & MAY_WRITE))
+			error = ccs_path_permission(&r, CCS_TYPE_WRITE, &buf);
+		kfree(buf.name);
+		goto out;
+	}
+	error = -ENOTDIR;
+out:
+	ccs_read_unlock(idx);
+	kfree(buffer);
+	return error;
+}
+
+#endif
+
+#if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 24)
+
+/**
+ * ccs_old_pivot_root_permission - Check permission for pivot_root().
+ *
+ * @old_nd: Pointer to "struct nameidata".
+ * @new_nd: Pointer to "struct nameidata".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_old_pivot_root_permission(struct nameidata *old_nd,
+					 struct nameidata *new_nd)
+{
+	struct path old_path = { old_nd->mnt, old_nd->dentry };
+	struct path new_path = { new_nd->mnt, new_nd->dentry };
+	return __ccs_pivot_root_permission(&old_path, &new_path);
+}
+
+/**
+ * ccs_old_chroot_permission - Check permission for chroot().
+ *
+ * @nd: Pointer to "struct nameidata".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_old_chroot_permission(struct nameidata *nd)
+{
+	struct path path = { nd->mnt, nd->dentry };
+	return __ccs_chroot_permission(&path);
+}
+
+#endif
+
+#ifdef CONFIG_CCSECURITY_NETWORK
+
+/**
+ * ccs_address_matches_group - Check whether the given address matches members of the given address group.
+ *
+ * @is_ipv6: True if @address is an IPv6 address.
+ * @address: An IPv4 or IPv6 address.
+ * @group:   Pointer to "struct ccs_address_group".
+ *
+ * Returns true if @address matches addresses in @group group, false otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static bool ccs_address_matches_group(const bool is_ipv6, const u32 *address,
+				      const struct ccs_group *group)
+{
+	struct ccs_address_group *member;
+	bool matched = false;
+	const u8 size = is_ipv6 ? 16 : 4;
+	list_for_each_entry_srcu(member, &group->member_list, head.list,
+				 &ccs_ss) {
+		if (member->head.is_deleted)
+			continue;
+		if (member->address.is_ipv6 != is_ipv6)
+			continue;
+		if (memcmp(&member->address.ip[0], address, size) > 0 ||
+		    memcmp(address, &member->address.ip[1], size) > 0)
+			continue;
+		matched = true;
+		break;
+	}
+	return matched;
+}
+
+/**
+ * ccs_check_inet_acl - Check permission for inet domain socket operation.
+ *
+ * @r:   Pointer to "struct ccs_request_info".
+ * @ptr: Pointer to "struct ccs_acl_info".
+ *
+ * Returns true if granted, false otherwise.
+ */
+static bool ccs_check_inet_acl(struct ccs_request_info *r,
+			       const struct ccs_acl_info *ptr)
+{
+	const struct ccs_inet_acl *acl = container_of(ptr, typeof(*acl), head);
+	const u8 size = r->param.inet_network.is_ipv6 ? 16 : 4;
+	if (!(ptr->perm & (1 << r->param.inet_network.operation)) ||
+	    !ccs_compare_number_union(r->param.inet_network.port, &acl->port))
+		return false;
+	if (acl->address.group)
+		return ccs_address_matches_group(r->param.inet_network.is_ipv6,
+						 r->param.inet_network.address,
+						 acl->address.group);
+	return acl->address.is_ipv6 == r->param.inet_network.is_ipv6 &&
+		memcmp(&acl->address.ip[0],
+		       r->param.inet_network.address, size) <= 0 &&
+		memcmp(r->param.inet_network.address,
+		       &acl->address.ip[1], size) <= 0;
+}
+
+/**
+ * ccs_check_unix_acl - Check permission for unix domain socket operation.
+ *
+ * @r:   Pointer to "struct ccs_request_info".
+ * @ptr: Pointer to "struct ccs_acl_info".
+ *
+ * Returns true if granted, false otherwise.
+ */
+static bool ccs_check_unix_acl(struct ccs_request_info *r,
+			       const struct ccs_acl_info *ptr)
+{
+	const struct ccs_unix_acl *acl = container_of(ptr, typeof(*acl), head);
+	return (ptr->perm & (1 << r->param.unix_network.operation)) &&
+		ccs_compare_name_union(r->param.unix_network.address,
+				       &acl->name);
+}
+
+/**
+ * ccs_inet_entry - Check permission for INET network operation.
+ *
+ * @address: Pointer to "struct ccs_addr_info".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_inet_entry(const struct ccs_addr_info *address)
+{
+	const int idx = ccs_read_lock();
+	struct ccs_request_info r;
+	int error = 0;
+	const u8 type = ccs_inet2mac[address->protocol][address->operation];
+	if (type && ccs_init_request_info(&r, type) != CCS_CONFIG_DISABLED) {
+		r.param_type = CCS_TYPE_INET_ACL;
+		r.param.inet_network.protocol = address->protocol;
+		r.param.inet_network.operation = address->operation;
+		r.param.inet_network.is_ipv6 = address->inet.is_ipv6;
+		r.param.inet_network.address = address->inet.address;
+		r.param.inet_network.port = ntohs(address->inet.port);
+		r.dont_sleep_on_enforce_error =
+			address->operation == CCS_NETWORK_ACCEPT
+#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG
+			|| address->operation == CCS_NETWORK_RECV
+#endif
+			;
+		error = ccs_check_acl(&r);
+	}
+	ccs_read_unlock(idx);
+	return error;
+}
+
+/**
+ * ccs_check_inet_address - Check permission for inet domain socket's operation.
+ *
+ * @addr:     Pointer to "struct sockaddr".
+ * @addr_len: Size of @addr.
+ * @port:     Port number.
+ * @address:  Pointer to "struct ccs_addr_info".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_check_inet_address(const struct sockaddr *addr,
+				  const unsigned int addr_len, const u16 port,
+				  struct ccs_addr_info *address)
+{
+	struct ccs_inet_addr_info *i = &address->inet;
+	switch (addr->sa_family) {
+	case AF_INET6:
+		if (addr_len < SIN6_LEN_RFC2133)
+			goto skip;
+		i->is_ipv6 = true;
+		i->address = (u32 *)
+			((struct sockaddr_in6 *) addr)->sin6_addr.s6_addr;
+		i->port = ((struct sockaddr_in6 *) addr)->sin6_port;
+		break;
+	case AF_INET:
+		if (addr_len < sizeof(struct sockaddr_in))
+			goto skip;
+		i->is_ipv6 = false;
+		i->address = (u32 *) &((struct sockaddr_in *) addr)->sin_addr;
+		i->port = ((struct sockaddr_in *) addr)->sin_port;
+		break;
+	default:
+		goto skip;
+	}
+	if (address->protocol == SOCK_RAW)
+		i->port = htons(port);
+	return ccs_inet_entry(address);
+skip:
+	return 0;
+}
+
+/**
+ * ccs_unix_entry - Check permission for UNIX network operation.
+ *
+ * @address: Pointer to "struct ccs_addr_info".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_unix_entry(const struct ccs_addr_info *address)
+{
+	const int idx = ccs_read_lock();
+	struct ccs_request_info r;
+	int error = 0;
+	const u8 type = ccs_unix2mac[address->protocol][address->operation];
+	if (type && ccs_init_request_info(&r, type) != CCS_CONFIG_DISABLED) {
+		char *buf = address->unix0.addr;
+		int len = address->unix0.addr_len - sizeof(sa_family_t);
+		if (len <= 0) {
+			buf = "anonymous";
+			len = 9;
+		} else if (buf[0]) {
+			len = strnlen(buf, len);
+		}
+		buf = ccs_encode2(buf, len);
+		if (buf) {
+			struct ccs_path_info addr;
+			addr.name = buf;
+			ccs_fill_path_info(&addr);
+			r.param_type = CCS_TYPE_UNIX_ACL;
+			r.param.unix_network.protocol = address->protocol;
+			r.param.unix_network.operation = address->operation;
+			r.param.unix_network.address = &addr;
+			r.dont_sleep_on_enforce_error =
+				address->operation == CCS_NETWORK_ACCEPT
+#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG
+				|| address->operation == CCS_NETWORK_RECV
+#endif
+				;
+			error = ccs_check_acl(&r);
+			kfree(buf);
+		} else
+			error = -ENOMEM;
+	}
+	ccs_read_unlock(idx);
+	return error;
+}
+
+/**
+ * ccs_check_unix_address - Check permission for unix domain socket's operation.
+ *
+ * @addr:     Pointer to "struct sockaddr".
+ * @addr_len: Size of @addr.
+ * @address:  Pointer to "struct ccs_addr_info".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_check_unix_address(struct sockaddr *addr,
+				  const unsigned int addr_len,
+				  struct ccs_addr_info *address)
+{
+	struct ccs_unix_addr_info *u = &address->unix0;
+	if (addr->sa_family != AF_UNIX)
+		return 0;
+	u->addr = ((struct sockaddr_un *) addr)->sun_path;
+	u->addr_len = addr_len;
+	return ccs_unix_entry(address);
+}
+
+/**
+ * ccs_sock_family - Get socket's family.
+ *
+ * @sk: Pointer to "struct sock".
+ *
+ * Returns one of PF_INET, PF_INET6, PF_UNIX or 0.
+ */
+static u8 ccs_sock_family(struct sock *sk)
+{
+	u8 family;
+	if (ccs_kernel_service())
+		return 0;
+	family = sk->sk_family;
+	switch (family) {
+	case PF_INET:
+	case PF_INET6:
+	case PF_UNIX:
+		return family;
+	default:
+		return 0;
+	}
+}
+
+/**
+ * __ccs_socket_listen_permission - Check permission for listening a socket.
+ *
+ * @sock: Pointer to "struct socket".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_socket_listen_permission(struct socket *sock)
+{
+	struct ccs_addr_info address;
+	const u8 family = ccs_sock_family(sock->sk);
+	const unsigned int type = sock->type;
+	struct sockaddr_storage addr;
+	int addr_len;
+	if (!family || (type != SOCK_STREAM && type != SOCK_SEQPACKET))
+		return 0;
+	{
+		const int error = sock->ops->getname(sock, (struct sockaddr *)
+						     &addr, &addr_len, 0);
+		if (error)
+			return error;
+	}
+	address.protocol = type;
+	address.operation = CCS_NETWORK_LISTEN;
+	if (family == PF_UNIX)
+		return ccs_check_unix_address((struct sockaddr *) &addr,
+					      addr_len, &address);
+	return ccs_check_inet_address((struct sockaddr *) &addr, addr_len, 0,
+				      &address);
+}
+
+/**
+ * __ccs_socket_connect_permission - Check permission for setting the remote address of a socket.
+ *
+ * @sock:     Pointer to "struct socket".
+ * @addr:     Pointer to "struct sockaddr".
+ * @addr_len: Size of @addr.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_socket_connect_permission(struct socket *sock,
+					   struct sockaddr *addr, int addr_len)
+{
+	struct ccs_addr_info address;
+	const u8 family = ccs_sock_family(sock->sk);
+	const unsigned int type = sock->type;
+	if (!family)
+		return 0;
+	address.protocol = type;
+	switch (type) {
+	case SOCK_DGRAM:
+	case SOCK_RAW:
+		address.operation = CCS_NETWORK_SEND;
+		break;
+	case SOCK_STREAM:
+	case SOCK_SEQPACKET:
+		address.operation = CCS_NETWORK_CONNECT;
+		break;
+	default:
+		return 0;
+	}
+	if (family == PF_UNIX)
+		return ccs_check_unix_address(addr, addr_len, &address);
+	return ccs_check_inet_address(addr, addr_len, sock->sk->sk_protocol,
+				      &address);
+}
+
+/**
+ * __ccs_socket_bind_permission - Check permission for setting the local address of a socket.
+ *
+ * @sock:     Pointer to "struct socket".
+ * @addr:     Pointer to "struct sockaddr".
+ * @addr_len: Size of @addr.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_socket_bind_permission(struct socket *sock,
+					struct sockaddr *addr, int addr_len)
+{
+	struct ccs_addr_info address;
+	const u8 family = ccs_sock_family(sock->sk);
+	const unsigned int type = sock->type;
+	if (!family)
+		return 0;
+	switch (type) {
+	case SOCK_STREAM:
+	case SOCK_DGRAM:
+	case SOCK_RAW:
+	case SOCK_SEQPACKET:
+		address.protocol = type;
+		address.operation = CCS_NETWORK_BIND;
+		break;
+	default:
+		return 0;
+	}
+	if (family == PF_UNIX)
+		return ccs_check_unix_address(addr, addr_len, &address);
+	return ccs_check_inet_address(addr, addr_len, sock->sk->sk_protocol,
+				      &address);
+}
+
+/**
+ * __ccs_socket_sendmsg_permission - Check permission for sending a datagram.
+ *
+ * @sock: Pointer to "struct socket".
+ * @msg:  Pointer to "struct msghdr".
+ * @size: Unused.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_socket_sendmsg_permission(struct socket *sock,
+					   struct msghdr *msg, int size)
+{
+	struct ccs_addr_info address;
+	const u8 family = ccs_sock_family(sock->sk);
+	const unsigned int type = sock->type;
+	if (!msg->msg_name || !family ||
+	    (type != SOCK_DGRAM && type != SOCK_RAW))
+		return 0;
+	address.protocol = type;
+	address.operation = CCS_NETWORK_SEND;
+	if (family == PF_UNIX)
+		return ccs_check_unix_address((struct sockaddr *)
+					      msg->msg_name, msg->msg_namelen,
+					      &address);
+	return ccs_check_inet_address((struct sockaddr *) msg->msg_name,
+				      msg->msg_namelen, sock->sk->sk_protocol,
+				      &address);
+}
+
+/**
+ * __ccs_socket_post_accept_permission - Check permission for accepting a socket.
+ *
+ * @sock:    Pointer to "struct socket".
+ * @newsock: Pointer to "struct socket".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_socket_post_accept_permission(struct socket *sock,
+					       struct socket *newsock)
+{
+	struct ccs_addr_info address;
+	const u8 family = ccs_sock_family(sock->sk);
+	const unsigned int type = sock->type;
+	struct sockaddr_storage addr;
+	int addr_len;
+	if (!family || (type != SOCK_STREAM && type != SOCK_SEQPACKET))
+		return 0;
+	{
+		const int error = newsock->ops->getname(newsock,
+							(struct sockaddr *)
+							&addr, &addr_len, 2);
+		if (error)
+			return error;
+	}
+	address.protocol = type;
+	address.operation = CCS_NETWORK_ACCEPT;
+	if (family == PF_UNIX)
+		return ccs_check_unix_address((struct sockaddr *) &addr,
+					      addr_len, &address);
+	return ccs_check_inet_address((struct sockaddr *) &addr, addr_len, 0,
+				      &address);
+}
+
+#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG
+
+/**
+ * __ccs_socket_post_recvmsg_permission - Check permission for receiving a datagram.
+ *
+ * @sk:    Pointer to "struct sock".
+ * @skb:   Pointer to "struct sk_buff".
+ * @flags: Flags passed to skb_recv_datagram().
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_socket_post_recvmsg_permission(struct sock *sk,
+						struct sk_buff *skb, int flags)
+{
+	struct ccs_addr_info address;
+	const u8 family = ccs_sock_family(sk);
+	const unsigned int type = sk->sk_type;
+	struct sockaddr_storage addr;
+	if (!family)
+		return 0;
+	switch (type) {
+	case SOCK_DGRAM:
+	case SOCK_RAW:
+		address.protocol = type;
+		break;
+	default:
+		return 0;
+	}
+	address.operation = CCS_NETWORK_RECV;
+	switch (family) {
+	case PF_INET6:
+		{
+			struct in6_addr *sin6 = (struct in6_addr *) &addr;
+			address.inet.is_ipv6 = true;
+			if (type == SOCK_DGRAM &&
+			    skb->protocol == htons(ETH_P_IP))
+				ipv6_addr_set(sin6, 0, 0, htonl(0xffff),
+					      ip_hdr(skb)->saddr);
+			else
+				*sin6 = ipv6_hdr(skb)->saddr;
+			break;
+		}
+	case PF_INET:
+		{
+			struct in_addr *sin4 = (struct in_addr *) &addr;
+			address.inet.is_ipv6 = false;
+			sin4->s_addr = ip_hdr(skb)->saddr;
+			break;
+		}
+	default: /* == PF_UNIX */
+		{
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
+			struct unix_address *u = unix_sk(skb->sk)->addr;
+#else
+			struct unix_address *u =
+				skb->sk->protinfo.af_unix.addr;
+#endif
+			unsigned int addr_len;
+			if (u && u->len <= sizeof(addr)) {
+				addr_len = u->len;
+				memcpy(&addr, u->name, addr_len);
+			} else {
+				addr_len = 0;
+				addr.ss_family = AF_UNIX;
+			}
+			if (ccs_check_unix_address((struct sockaddr *) &addr,
+						   addr_len, &address))
+				goto out;
+			return 0;
+		}
+	}
+	address.inet.address = (u32 *) &addr;
+	if (type == SOCK_DGRAM)
+		address.inet.port = udp_hdr(skb)->source;
+	else
+		address.inet.port = htons(sk->sk_protocol);
+	if (ccs_inet_entry(&address))
+		goto out;
+	return 0;
+out:
+	/*
+	 * Remove from queue if MSG_PEEK is used so that
+	 * the head message from unwanted source in receive queue will not
+	 * prevent the caller from picking up next message from wanted source
+	 * when the caller is using MSG_PEEK flag for picking up.
+	 */
+	{
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)
+		bool slow = false;
+		if (type == SOCK_DGRAM && family != PF_UNIX)
+			slow = lock_sock_fast(sk);
+#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
+		if (type == SOCK_DGRAM && family != PF_UNIX)
+			lock_sock(sk);
+#elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR >= 2
+		if (type == SOCK_DGRAM && family != PF_UNIX)
+			lock_sock(sk);
+#endif
+		skb_kill_datagram(sk, skb, flags);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35)
+		if (type == SOCK_DGRAM && family != PF_UNIX)
+			unlock_sock_fast(sk, slow);
+#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
+		if (type == SOCK_DGRAM && family != PF_UNIX)
+			release_sock(sk);
+#elif defined(RHEL_MAJOR) && RHEL_MAJOR == 5 && defined(RHEL_MINOR) && RHEL_MINOR >= 2
+		if (type == SOCK_DGRAM && family != PF_UNIX)
+			release_sock(sk);
+#endif
+	}
+	return -EPERM;
+}
+
+#endif
+
+#endif
+
+#if defined(CONFIG_CCSECURITY_CAPABILITY) || defined(CONFIG_CCSECURITY_NETWORK)
+
+/**
+ * ccs_kernel_service - Check whether I'm kernel service or not.
+ *
+ * Returns true if I'm kernel service, false otherwise.
+ */
+static bool ccs_kernel_service(void)
+{
+	/* Nothing to do if I am a kernel service. */
+	return segment_eq(get_fs(), KERNEL_DS);
+}
+
+#endif
+
+#ifdef CONFIG_CCSECURITY_CAPABILITY
+
+/**
+ * ccs_check_capability_acl - Check permission for capability operation.
+ *
+ * @r:   Pointer to "struct ccs_request_info".
+ * @ptr: Pointer to "struct ccs_acl_info".
+ *
+ * Returns true if granted, false otherwise.
+ */
+static bool ccs_check_capability_acl(struct ccs_request_info *r,
+				     const struct ccs_acl_info *ptr)
+{
+	const struct ccs_capability_acl *acl =
+		container_of(ptr, typeof(*acl), head);
+	return acl->operation == r->param.capability.operation;
+}
+
+/**
+ * ccs_capable - Check permission for capability.
+ *
+ * @operation: Type of operation.
+ *
+ * Returns true on success, false otherwise.
+ */
+static bool __ccs_capable(const u8 operation)
+{
+	struct ccs_request_info r;
+	int error = 0;
+	const int idx = ccs_read_lock();
+	if (ccs_init_request_info(&r, ccs_c2mac[operation])
+	    != CCS_CONFIG_DISABLED) {
+		r.param_type = CCS_TYPE_CAPABILITY_ACL;
+		r.param.capability.operation = operation;
+		error = ccs_check_acl(&r);
+	}
+	ccs_read_unlock(idx);
+	return !error;
+}
+
+/**
+ * __ccs_socket_create_permission - Check permission for creating a socket.
+ *
+ * @family:   Protocol family.
+ * @type:     Unused.
+ * @protocol: Unused.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __ccs_socket_create_permission(int family, int type, int protocol)
+{
+	if (ccs_kernel_service())
+		return 0;
+	if (family == PF_PACKET && !ccs_capable(CCS_USE_PACKET_SOCKET))
+		return -EPERM;
+	if (family == PF_ROUTE && !ccs_capable(CCS_USE_ROUTE_SOCKET))
+		return -EPERM;
+	return 0;
+}
+
+/**
+ * __ccs_ptrace_permission - Check permission for ptrace().
+ *
+ * @request: Unused.
+ * @pid:     Unused.
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * Since this function is called from location where it is permitted to sleep,
+ * it is racy to check target process's domainname anyway. Therefore, we don't
+ * use target process's domainname.
+ */
+static int __ccs_ptrace_permission(long request, long pid)
+{
+	return __ccs_capable(CCS_SYS_PTRACE) ? 0 : -EPERM;
+}
+
+#endif
+
+#ifdef CONFIG_CCSECURITY_IPC
+
+/**
+ * ccs_check_signal_acl - Check permission for signal operation.
+ *
+ * @r:   Pointer to "struct ccs_request_info".
+ * @ptr: Pointer to "struct ccs_acl_info".
+ *
+ * Returns true if granted, false otherwise.
+ */
+static bool ccs_check_signal_acl(struct ccs_request_info *r,
+				 const struct ccs_acl_info *ptr)
+{
+	const struct ccs_signal_acl *acl =
+		container_of(ptr, typeof(*acl), head);
+	if (ccs_compare_number_union(r->param.signal.sig, &acl->sig)) {
+		const int len = acl->domainname->total_len;
+		if (!strncmp(acl->domainname->name,
+			     r->param.signal.dest_pattern, len)) {
+			switch (r->param.signal.dest_pattern[len]) {
+			case ' ':
+			case '\0':
+				return true;
+			}
+		}
+	}
+	return false;
+}
+
+/**
+ * ccs_signal_acl2 - Check permission for signal.
+ *
+ * @sig: Signal number.
+ * @pid: Target's PID.
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static int ccs_signal_acl2(const int sig, const int pid)
+{
+	struct ccs_request_info r;
+	struct ccs_domain_info *dest = NULL;
+	const struct ccs_domain_info * const domain = ccs_current_domain();
+	if (ccs_init_request_info(&r, CCS_MAC_SIGNAL) == CCS_CONFIG_DISABLED)
+		return 0;
+	if (!sig)
+		return 0;                /* No check for NULL signal. */
+	r.param_type = CCS_TYPE_SIGNAL_ACL;
+	r.param.signal.sig = sig;
+	r.param.signal.dest_pattern = domain->domainname->name;
+	r.granted = true;
+	if (ccs_sys_getpid() == pid) {
+		ccs_audit_log(&r);
+		return 0;                /* No check for self process. */
+	}
+	{ /* Simplified checking. */
+		struct task_struct *p = NULL;
+		ccs_tasklist_lock();
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
+		if (pid > 0)
+			p = ccsecurity_exports.find_task_by_vpid((pid_t) pid);
+		else if (pid == 0)
+			p = current;
+		else if (pid == -1)
+			dest = &ccs_kernel_domain;
+		else
+			p = ccsecurity_exports.find_task_by_vpid((pid_t) -pid);
+#else
+		if (pid > 0)
+			p = find_task_by_pid((pid_t) pid);
+		else if (pid == 0)
+			p = current;
+		else if (pid == -1)
+			dest = &ccs_kernel_domain;
+		else
+			p = find_task_by_pid((pid_t) -pid);
+#endif
+		if (p)
+			dest = ccs_task_domain(p);
+		ccs_tasklist_unlock();
+	}
+	if (!dest)
+		return 0; /* I can't find destinatioin. */
+	if (domain == dest) {
+		ccs_audit_log(&r);
+		return 0;                /* No check for self domain. */
+	}
+	r.param.signal.dest_pattern = dest->domainname->name;
+	return ccs_check_acl(&r);
+}
+
+/**
+ * ccs_signal_acl - Check permission for signal.
+ *
+ * @pid: Target's PID.
+ * @sig: Signal number.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_signal_acl(const int pid, const int sig)
+{
+	int error;
+	if (!sig)
+		error = 0;
+	else {
+		const int idx = ccs_read_lock();
+		error = ccs_signal_acl2(sig, pid);
+		ccs_read_unlock(idx);
+	}
+	return error;
+}
+
+/**
+ * ccs_signal_acl0 - Permission check for signal().
+ *
+ * @tgid: Unused.
+ * @pid:  Target's PID.
+ * @sig:  Signal number.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_signal_acl0(pid_t tgid, pid_t pid, int sig)
+{
+	return ccs_signal_acl(pid, sig);
+}
+
+#endif
+
+#ifdef CONFIG_CCSECURITY_MISC
+
+/**
+ * ccs_check_env_acl - Check permission for environment variable's name.
+ *
+ * @r:   Pointer to "struct ccs_request_info".
+ * @ptr: Pointer to "struct ccs_acl_info".
+ *
+ * Returns true if granted, false otherwise.
+ */
+static bool ccs_check_env_acl(struct ccs_request_info *r,
+			      const struct ccs_acl_info *ptr)
+{
+	const struct ccs_env_acl *acl = container_of(ptr, typeof(*acl), head);
+	return ccs_path_matches_pattern(r->param.environ.name, acl->env);
+}
+
+/**
+ * ccs_env_perm - Check permission for environment variable's name.
+ *
+ * @r:   Pointer to "struct ccs_request_info".
+ * @env: The name of environment variable.
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static int ccs_env_perm(struct ccs_request_info *r, const char *env)
+{
+	struct ccs_path_info environ;
+	if (!env || !*env)
+		return 0;
+	environ.name = env;
+	ccs_fill_path_info(&environ);
+	r->param_type = CCS_TYPE_ENV_ACL;
+	r->param.environ.name = &environ;
+	return ccs_check_acl(r);
+}
+
+/**
+ * ccs_environ - Check permission for environment variable names.
+ *
+ * @ee: Pointer to "struct ccs_execve".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_environ(struct ccs_execve *ee)
+{
+	struct ccs_request_info *r = &ee->r;
+	struct linux_binprm *bprm = ee->bprm;
+	/* env_page.data is allocated by ccs_dump_page(). */
+	struct ccs_page_dump env_page = { };
+	char *arg_ptr; /* Size is CCS_EXEC_TMPSIZE bytes */
+	int arg_len = 0;
+	unsigned long pos = bprm->p;
+	int offset = pos % PAGE_SIZE;
+	int argv_count = bprm->argc;
+	int envp_count = bprm->envc;
+	/* printk(KERN_DEBUG "start %d %d\n", argv_count, envp_count); */
+	int error = -ENOMEM;
+	ee->r.type = CCS_MAC_ENVIRON;
+	ee->r.profile = ccs_current_domain()->profile;
+	ee->r.mode = ccs_get_mode(ee->r.profile, CCS_MAC_ENVIRON);
+	if (!r->mode || !envp_count)
+		return 0;
+	arg_ptr = kzalloc(CCS_EXEC_TMPSIZE, CCS_GFP_FLAGS);
+	if (!arg_ptr)
+		goto out;
+	while (error == -ENOMEM) {
+		if (!ccs_dump_page(bprm, pos, &env_page))
+			goto out;
+		pos += PAGE_SIZE - offset;
+		/* Read. */
+		while (argv_count && offset < PAGE_SIZE) {
+			if (!env_page.data[offset++])
+				argv_count--;
+		}
+		if (argv_count) {
+			offset = 0;
+			continue;
+		}
+		while (offset < PAGE_SIZE) {
+			const unsigned char c = env_page.data[offset++];
+			if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {
+				if (c == '=') {
+					arg_ptr[arg_len++] = '\0';
+				} else if (c == '\\') {
+					arg_ptr[arg_len++] = '\\';
+					arg_ptr[arg_len++] = '\\';
+				} else if (c > ' ' && c < 127) {
+					arg_ptr[arg_len++] = c;
+				} else {
+					arg_ptr[arg_len++] = '\\';
+					arg_ptr[arg_len++] = (c >> 6) + '0';
+					arg_ptr[arg_len++]
+						= ((c >> 3) & 7) + '0';
+					arg_ptr[arg_len++] = (c & 7) + '0';
+				}
+			} else {
+				arg_ptr[arg_len] = '\0';
+			}
+			if (c)
+				continue;
+			if (ccs_env_perm(r, arg_ptr)) {
+				error = -EPERM;
+				break;
+			}
+			if (!--envp_count) {
+				error = 0;
+				break;
+			}
+			arg_len = 0;
+		}
+		offset = 0;
+	}
+out:
+	if (r->mode != CCS_CONFIG_ENFORCING)
+		error = 0;
+	kfree(env_page.data);
+	kfree(arg_ptr);
+	return error;
+}
+
+#endif
+
+/**
+ * ccs_argv - Check argv[] in "struct linux_binbrm".
+ *
+ * @index:   Index number of @arg_ptr.
+ * @arg_ptr: Contents of argv[@index].
+ * @argc:    Length of @argv.
+ * @argv:    Pointer to "struct ccs_argv".
+ * @checked: Set to true if @argv[@index] was found.
+ *
+ * Returns true on success, false otherwise.
+ */
+static bool ccs_argv(const unsigned int index, const char *arg_ptr,
+		     const int argc, const struct ccs_argv *argv,
+		     u8 *checked)
+{
+	int i;
+	struct ccs_path_info arg;
+	arg.name = arg_ptr;
+	for (i = 0; i < argc; argv++, checked++, i++) {
+		bool result;
+		if (index != argv->index)
+			continue;
+		*checked = 1;
+		ccs_fill_path_info(&arg);
+		result = ccs_path_matches_pattern(&arg, argv->value);
+		if (argv->is_not)
+			result = !result;
+		if (!result)
+			return false;
+	}
+	return true;
+}
+
+/**
+ * ccs_envp - Check envp[] in "struct linux_binbrm".
+ *
+ * @env_name:  The name of environment variable.
+ * @env_value: The value of environment variable.
+ * @envc:      Length of @envp.
+ * @envp:      Pointer to "struct ccs_envp".
+ * @checked:   Set to true if @envp[@env_name] was found.
+ *
+ * Returns true on success, false otherwise.
+ */
+static bool ccs_envp(const char *env_name, const char *env_value,
+		     const int envc, const struct ccs_envp *envp,
+		     u8 *checked)
+{
+	int i;
+	struct ccs_path_info name;
+	struct ccs_path_info value;
+	name.name = env_name;
+	ccs_fill_path_info(&name);
+	value.name = env_value;
+	ccs_fill_path_info(&value);
+	for (i = 0; i < envc; envp++, checked++, i++) {
+		bool result;
+		if (!ccs_path_matches_pattern(&name, envp->name))
+			continue;
+		*checked = 1;
+		if (envp->value) {
+			result = ccs_path_matches_pattern(&value, envp->value);
+			if (envp->is_not)
+				result = !result;
+		} else {
+			result = true;
+			if (!envp->is_not)
+				result = !result;
+		}
+		if (!result)
+			return false;
+	}
+	return true;
+}
+
+/**
+ * ccs_scan_bprm - Scan "struct linux_binprm".
+ *
+ * @ee:   Pointer to "struct ccs_execve".
+ * @argc: Length of @argc.
+ * @argv: Pointer to "struct ccs_argv".
+ * @envc: Length of @envp.
+ * @envp: Poiner to "struct ccs_envp".
+ *
+ * Returns true on success, false otherwise.
+ */
+static bool ccs_scan_bprm(struct ccs_execve *ee,
+			  const u16 argc, const struct ccs_argv *argv,
+			  const u16 envc, const struct ccs_envp *envp)
+{
+	struct linux_binprm *bprm = ee->bprm;
+	struct ccs_page_dump *dump = &ee->dump;
+	char *arg_ptr = ee->tmp;
+	int arg_len = 0;
+	unsigned long pos = bprm->p;
+	int offset = pos % PAGE_SIZE;
+	int argv_count = bprm->argc;
+	int envp_count = bprm->envc;
+	bool result = true;
+	u8 local_checked[32];
+	u8 *checked;
+	if (argc + envc <= sizeof(local_checked)) {
+		checked = local_checked;
+		memset(local_checked, 0, sizeof(local_checked));
+	} else {
+		checked = kzalloc(argc + envc, CCS_GFP_FLAGS);
+		if (!checked)
+			return false;
+	}
+	while (argv_count || envp_count) {
+		if (!ccs_dump_page(bprm, pos, dump)) {
+			result = false;
+			goto out;
+		}
+		pos += PAGE_SIZE - offset;
+		while (offset < PAGE_SIZE) {
+			/* Read. */
+			const char *kaddr = dump->data;
+			const unsigned char c = kaddr[offset++];
+			if (c && arg_len < CCS_EXEC_TMPSIZE - 10) {
+				if (c == '\\') {
+					arg_ptr[arg_len++] = '\\';
+					arg_ptr[arg_len++] = '\\';
+				} else if (c > ' ' && c < 127) {
+					arg_ptr[arg_len++] = c;
+				} else {
+					arg_ptr[arg_len++] = '\\';
+					arg_ptr[arg_len++] = (c >> 6) + '0';
+					arg_ptr[arg_len++] =
+						((c >> 3) & 7) + '0';
+					arg_ptr[arg_len++] = (c & 7) + '0';
+				}
+			} else {
+				arg_ptr[arg_len] = '\0';
+			}
+			if (c)
+				continue;
+			/* Check. */
+			if (argv_count) {
+				if (!ccs_argv(bprm->argc - argv_count,
+					      arg_ptr, argc, argv,
+					      checked)) {
+					result = false;
+					break;
+				}
+				argv_count--;
+			} else if (envp_count) {
+				char *cp = strchr(arg_ptr, '=');
+				if (cp) {
+					*cp = '\0';
+					if (!ccs_envp(arg_ptr, cp + 1,
+						      envc, envp,
+						      checked + argc)) {
+						result = false;
+						break;
+					}
+				}
+				envp_count--;
+			} else {
+				break;
+			}
+			arg_len = 0;
+		}
+		offset = 0;
+		if (!result)
+			break;
+	}
+out:
+	if (result) {
+		int i;
+		/* Check not-yet-checked entries. */
+		for (i = 0; i < argc; i++) {
+			if (checked[i])
+				continue;
+			/*
+			 * Return true only if all unchecked indexes in
+			 * bprm->argv[] are not matched.
+			 */
+			if (argv[i].is_not)
+				continue;
+			result = false;
+			break;
+		}
+		for (i = 0; i < envc; envp++, i++) {
+			if (checked[argc + i])
+				continue;
+			/*
+			 * Return true only if all unchecked environ variables
+			 * in bprm->envp[] are either undefined or not matched.
+			 */
+			if ((!envp->value && !envp->is_not) ||
+			    (envp->value && envp->is_not))
+				continue;
+			result = false;
+			break;
+		}
+	}
+	if (checked != local_checked)
+		kfree(checked);
+	return result;
+}
+
+/**
+ * ccs_scan_exec_realpath - Check "exec.realpath" parameter of "struct ccs_condition".
+ *
+ * @file:  Pointer to "struct file".
+ * @ptr:   Pointer to "struct ccs_name_union".
+ * @match: True if "exec.realpath=", false if "exec.realpath!=".
+ *
+ * Returns true on success, false otherwise.
+ */
+static bool ccs_scan_exec_realpath(struct file *file,
+				   const struct ccs_name_union *ptr,
+				   const bool match)
+{
+	bool result;
+	struct ccs_path_info exe;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
+	struct path path;
+#endif
+	if (!file)
+		return false;
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
+	exe.name = ccs_realpath(&file->f_path);
+#else
+	path.mnt = file->f_vfsmnt;
+	path.dentry = file->f_dentry;
+	exe.name = ccs_realpath(&path);
+#endif
+	if (!exe.name)
+		return false;
+	ccs_fill_path_info(&exe);
+	result = ccs_compare_name_union(&exe, ptr);
+	kfree(exe.name);
+	return result == match;
+}
+
+/**
+ * ccs_get_attributes - Revalidate "struct inode".
+ *
+ * @obj: Pointer to "struct ccs_obj_info".
+ *
+ * Returns nothing.
+ */
+void ccs_get_attributes(struct ccs_obj_info *obj)
+{
+	u8 i;
+	struct dentry *dentry = NULL;
+
+	for (i = 0; i < CCS_MAX_PATH_STAT; i++) {
+		struct inode *inode;
+		switch (i) {
+		case CCS_PATH1:
+			dentry = obj->path1.dentry;
+			if (!dentry)
+				continue;
+			break;
+		case CCS_PATH2:
+			dentry = obj->path2.dentry;
+			if (!dentry)
+				continue;
+			break;
+		default:
+			if (!dentry)
+				continue;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
+			spin_lock(&dcache_lock);
+			dentry = dget(dentry->d_parent);
+			spin_unlock(&dcache_lock);
+#else
+			dentry = dget_parent(dentry);
+#endif
+			break;
+		}
+		inode = dentry->d_inode;
+		if (inode) {
+			struct ccs_mini_stat *stat = &obj->stat[i];
+			stat->uid  = inode->i_uid;
+			stat->gid  = inode->i_gid;
+			stat->ino  = inode->i_ino;
+			stat->mode = inode->i_mode;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
+			stat->dev  = inode->i_dev;
+#else
+			stat->dev  = inode->i_sb->s_dev;
+#endif
+			stat->rdev = inode->i_rdev;
+			obj->stat_valid[i] = true;
+		}
+		if (i & 1) /* i == CCS_PATH1_PARENT || i == CCS_PATH2_PARENT */
+			dput(dentry);
+	}
+}
+
+/**
+ * ccs_condition - Check condition part.
+ *
+ * @r:    Pointer to "struct ccs_request_info".
+ * @cond: Pointer to "struct ccs_condition". Maybe NULL.
+ *
+ * Returns true on success, false otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static bool ccs_condition(struct ccs_request_info *r,
+			  const struct ccs_condition *cond)
+{
+	const u32 ccs_flags = ccs_current_flags();
+	u32 i;
+	unsigned long min_v[2] = { 0, 0 };
+	unsigned long max_v[2] = { 0, 0 };
+	const struct ccs_condition_element *condp;
+	const struct ccs_number_union *numbers_p;
+	const struct ccs_name_union *names_p;
+	const struct ccs_argv *argv;
+	const struct ccs_envp *envp;
+	struct ccs_obj_info *obj;
+	u16 condc;
+	u16 argc;
+	u16 envc;
+	struct linux_binprm *bprm = NULL;
+	if (!cond)
+		return true;
+	condc = cond->condc;
+	argc = cond->argc;
+	envc = cond->envc;
+	obj = r->obj;
+	if (r->ee)
+		bprm = r->ee->bprm;
+	if (!bprm && (argc || envc))
+		return false;
+	condp = (struct ccs_condition_element *) (cond + 1);
+	numbers_p = (const struct ccs_number_union *) (condp + condc);
+	names_p = (const struct ccs_name_union *)
+		(numbers_p + cond->numbers_count);
+	argv = (const struct ccs_argv *) (names_p + cond->names_count);
+	envp = (const struct ccs_envp *) (argv + argc);
+	for (i = 0; i < condc; i++) {
+		const bool match = condp->equals;
+		const u8 left = condp->left;
+		const u8 right = condp->right;
+		bool is_bitop[2] = { false, false };
+		u8 j;
+		condp++;
+		/* Check argv[] and envp[] later. */
+		if (left == CCS_ARGV_ENTRY || left == CCS_ENVP_ENTRY)
+			continue;
+		/* Check string expressions. */
+		if (right == CCS_NAME_UNION) {
+			const struct ccs_name_union *ptr = names_p++;
+			switch (left) {
+				struct ccs_path_info *symlink;
+				struct ccs_execve *ee;
+				struct file *file;
+			case CCS_SYMLINK_TARGET:
+				symlink = obj ? obj->symlink_target : NULL;
+				if (!symlink ||
+				    !ccs_compare_name_union(symlink, ptr)
+				    == match)
+					goto out;
+				break;
+			case CCS_EXEC_REALPATH:
+				ee = r->ee;
+				file = ee ? ee->bprm->file : NULL;
+				if (!ccs_scan_exec_realpath(file, ptr, match))
+					goto out;
+				break;
+			}
+			continue;
+		}
+		/* Check numeric or bit-op expressions. */
+		for (j = 0; j < 2; j++) {
+			const u8 index = j ? right : left;
+			unsigned long value = 0;
+			switch (index) {
+			case CCS_TASK_UID:
+				value = from_kuid(&init_user_ns,
+						  current_uid());
+				break;
+			case CCS_TASK_EUID:
+				value = from_kuid(&init_user_ns,
+						  current_euid());
+				break;
+			case CCS_TASK_SUID:
+				value = from_kuid(&init_user_ns,
+						  current_suid());
+				break;
+			case CCS_TASK_FSUID:
+				value = from_kuid(&init_user_ns,
+						  current_fsuid());
+				break;
+			case CCS_TASK_GID:
+				value = from_kgid(&init_user_ns,
+						  current_gid());
+				break;
+			case CCS_TASK_EGID:
+				value = from_kgid(&init_user_ns,
+						  current_egid());
+				break;
+			case CCS_TASK_SGID:
+				value = from_kgid(&init_user_ns,
+						  current_sgid());
+				break;
+			case CCS_TASK_FSGID:
+				value = from_kgid(&init_user_ns,
+						  current_fsgid());
+				break;
+			case CCS_TASK_PID:
+				value = ccs_sys_getpid();
+				break;
+			case CCS_TASK_PPID:
+				value = ccs_sys_getppid();
+				break;
+			case CCS_TYPE_IS_SOCKET:
+				value = S_IFSOCK;
+				break;
+			case CCS_TYPE_IS_SYMLINK:
+				value = S_IFLNK;
+				break;
+			case CCS_TYPE_IS_FILE:
+				value = S_IFREG;
+				break;
+			case CCS_TYPE_IS_BLOCK_DEV:
+				value = S_IFBLK;
+				break;
+			case CCS_TYPE_IS_DIRECTORY:
+				value = S_IFDIR;
+				break;
+			case CCS_TYPE_IS_CHAR_DEV:
+				value = S_IFCHR;
+				break;
+			case CCS_TYPE_IS_FIFO:
+				value = S_IFIFO;
+				break;
+			case CCS_MODE_SETUID:
+				value = S_ISUID;
+				break;
+			case CCS_MODE_SETGID:
+				value = S_ISGID;
+				break;
+			case CCS_MODE_STICKY:
+				value = S_ISVTX;
+				break;
+			case CCS_MODE_OWNER_READ:
+				value = S_IRUSR;
+				break;
+			case CCS_MODE_OWNER_WRITE:
+				value = S_IWUSR;
+				break;
+			case CCS_MODE_OWNER_EXECUTE:
+				value = S_IXUSR;
+				break;
+			case CCS_MODE_GROUP_READ:
+				value = S_IRGRP;
+				break;
+			case CCS_MODE_GROUP_WRITE:
+				value = S_IWGRP;
+				break;
+			case CCS_MODE_GROUP_EXECUTE:
+				value = S_IXGRP;
+				break;
+			case CCS_MODE_OTHERS_READ:
+				value = S_IROTH;
+				break;
+			case CCS_MODE_OTHERS_WRITE:
+				value = S_IWOTH;
+				break;
+			case CCS_MODE_OTHERS_EXECUTE:
+				value = S_IXOTH;
+				break;
+			case CCS_EXEC_ARGC:
+				if (!bprm)
+					goto out;
+				value = bprm->argc;
+				break;
+			case CCS_EXEC_ENVC:
+				if (!bprm)
+					goto out;
+				value = bprm->envc;
+				break;
+			case CCS_TASK_TYPE:
+				value = ((u8) ccs_flags)
+					& CCS_TASK_IS_EXECUTE_HANDLER;
+				break;
+			case CCS_TASK_EXECUTE_HANDLER:
+				value = CCS_TASK_IS_EXECUTE_HANDLER;
+				break;
+			case CCS_NUMBER_UNION:
+				/* Fetch values later. */
+				break;
+			default:
+				if (!obj)
+					goto out;
+				if (!obj->validate_done) {
+					ccs_get_attributes(obj);
+					obj->validate_done = true;
+				}
+				{
+					u8 stat_index;
+					struct ccs_mini_stat *stat;
+					switch (index) {
+					case CCS_PATH1_UID:
+					case CCS_PATH1_GID:
+					case CCS_PATH1_INO:
+					case CCS_PATH1_MAJOR:
+					case CCS_PATH1_MINOR:
+					case CCS_PATH1_TYPE:
+					case CCS_PATH1_DEV_MAJOR:
+					case CCS_PATH1_DEV_MINOR:
+					case CCS_PATH1_PERM:
+						stat_index = CCS_PATH1;
+						break;
+					case CCS_PATH2_UID:
+					case CCS_PATH2_GID:
+					case CCS_PATH2_INO:
+					case CCS_PATH2_MAJOR:
+					case CCS_PATH2_MINOR:
+					case CCS_PATH2_TYPE:
+					case CCS_PATH2_DEV_MAJOR:
+					case CCS_PATH2_DEV_MINOR:
+					case CCS_PATH2_PERM:
+						stat_index = CCS_PATH2;
+						break;
+					case CCS_PATH1_PARENT_UID:
+					case CCS_PATH1_PARENT_GID:
+					case CCS_PATH1_PARENT_INO:
+					case CCS_PATH1_PARENT_PERM:
+						stat_index = CCS_PATH1_PARENT;
+						break;
+					case CCS_PATH2_PARENT_UID:
+					case CCS_PATH2_PARENT_GID:
+					case CCS_PATH2_PARENT_INO:
+					case CCS_PATH2_PARENT_PERM:
+						stat_index = CCS_PATH2_PARENT;
+						break;
+					default:
+						goto out;
+					}
+					if (!obj->stat_valid[stat_index])
+						goto out;
+					stat = &obj->stat[stat_index];
+					switch (index) {
+					case CCS_PATH1_UID:
+					case CCS_PATH2_UID:
+					case CCS_PATH1_PARENT_UID:
+					case CCS_PATH2_PARENT_UID:
+						value = from_kuid
+							(&init_user_ns,
+							 stat->uid);
+						break;
+					case CCS_PATH1_GID:
+					case CCS_PATH2_GID:
+					case CCS_PATH1_PARENT_GID:
+					case CCS_PATH2_PARENT_GID:
+						value = from_kgid
+							(&init_user_ns,
+							 stat->gid);
+						break;
+					case CCS_PATH1_INO:
+					case CCS_PATH2_INO:
+					case CCS_PATH1_PARENT_INO:
+					case CCS_PATH2_PARENT_INO:
+						value = stat->ino;
+						break;
+					case CCS_PATH1_MAJOR:
+					case CCS_PATH2_MAJOR:
+						value = MAJOR(stat->dev);
+						break;
+					case CCS_PATH1_MINOR:
+					case CCS_PATH2_MINOR:
+						value = MINOR(stat->dev);
+						break;
+					case CCS_PATH1_TYPE:
+					case CCS_PATH2_TYPE:
+						value = stat->mode & S_IFMT;
+						break;
+					case CCS_PATH1_DEV_MAJOR:
+					case CCS_PATH2_DEV_MAJOR:
+						value = MAJOR(stat->rdev);
+						break;
+					case CCS_PATH1_DEV_MINOR:
+					case CCS_PATH2_DEV_MINOR:
+						value = MINOR(stat->rdev);
+						break;
+					case CCS_PATH1_PERM:
+					case CCS_PATH2_PERM:
+					case CCS_PATH1_PARENT_PERM:
+					case CCS_PATH2_PARENT_PERM:
+						value = stat->mode & S_IALLUGO;
+						break;
+					}
+				}
+				break;
+			}
+			max_v[j] = value;
+			min_v[j] = value;
+			switch (index) {
+			case CCS_MODE_SETUID:
+			case CCS_MODE_SETGID:
+			case CCS_MODE_STICKY:
+			case CCS_MODE_OWNER_READ:
+			case CCS_MODE_OWNER_WRITE:
+			case CCS_MODE_OWNER_EXECUTE:
+			case CCS_MODE_GROUP_READ:
+			case CCS_MODE_GROUP_WRITE:
+			case CCS_MODE_GROUP_EXECUTE:
+			case CCS_MODE_OTHERS_READ:
+			case CCS_MODE_OTHERS_WRITE:
+			case CCS_MODE_OTHERS_EXECUTE:
+				is_bitop[j] = true;
+			}
+		}
+		if (left == CCS_NUMBER_UNION) {
+			/* Fetch values now. */
+			const struct ccs_number_union *ptr = numbers_p++;
+			min_v[0] = ptr->values[0];
+			max_v[0] = ptr->values[1];
+		}
+		if (right == CCS_NUMBER_UNION) {
+			/* Fetch values now. */
+			const struct ccs_number_union *ptr = numbers_p++;
+			if (ptr->group) {
+				if (ccs_number_matches_group(min_v[0],
+							     max_v[0],
+							     ptr->group)
+				    == match)
+					continue;
+			} else {
+				if ((min_v[0] <= ptr->values[1] &&
+				     max_v[0] >= ptr->values[0]) == match)
+					continue;
+			}
+			goto out;
+		}
+		/*
+		 * Bit operation is valid only when counterpart value
+		 * represents permission.
+		 */
+		if (is_bitop[0] && is_bitop[1]) {
+			goto out;
+		} else if (is_bitop[0]) {
+			switch (right) {
+			case CCS_PATH1_PERM:
+			case CCS_PATH1_PARENT_PERM:
+			case CCS_PATH2_PERM:
+			case CCS_PATH2_PARENT_PERM:
+				if (!(max_v[0] & max_v[1]) == !match)
+					continue;
+			}
+			goto out;
+		} else if (is_bitop[1]) {
+			switch (left) {
+			case CCS_PATH1_PERM:
+			case CCS_PATH1_PARENT_PERM:
+			case CCS_PATH2_PERM:
+			case CCS_PATH2_PARENT_PERM:
+				if (!(max_v[0] & max_v[1]) == !match)
+					continue;
+			}
+			goto out;
+		}
+		/* Normal value range comparison. */
+		if ((min_v[0] <= max_v[1] && max_v[0] >= min_v[1]) == match)
+			continue;
+out:
+		return false;
+	}
+	/* Check argv[] and envp[] now. */
+	if (r->ee && (argc || envc))
+		return ccs_scan_bprm(r->ee, argc, argv, envc, envp);
+	return true;
+}
+
+#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION
+
+/**
+ * ccs_check_task_acl - Check permission for task operation.
+ *
+ * @r:   Pointer to "struct ccs_request_info".
+ * @ptr: Pointer to "struct ccs_acl_info".
+ *
+ * Returns true if granted, false otherwise.
+ */
+static bool ccs_check_task_acl(struct ccs_request_info *r,
+			       const struct ccs_acl_info *ptr)
+{
+	const struct ccs_task_acl *acl = container_of(ptr, typeof(*acl), head);
+	return !ccs_pathcmp(r->param.task.domainname, acl->domainname);
+}
+
+#endif
+
+/**
+ * ccs_init_request_info - Initialize "struct ccs_request_info" members.
+ *
+ * @r:     Pointer to "struct ccs_request_info" to initialize.
+ * @index: Index number of functionality.
+ *
+ * Returns mode.
+ *
+ * "task auto_domain_transition" keyword is evaluated before returning mode for
+ * @index. If "task auto_domain_transition" keyword was specified and
+ * transition to that domain failed, the current thread will be killed by
+ * SIGKILL. Note that if current->pid == 1, sending SIGKILL won't work.
+ */
+int ccs_init_request_info(struct ccs_request_info *r, const u8 index)
+{
+#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION
+	u8 i;
+	const char *buf;
+	for (i = 0; i < 255; i++) {
+		const u8 profile = ccs_current_domain()->profile;
+		memset(r, 0, sizeof(*r));
+		r->profile = profile;
+		r->type = index;
+		r->mode = ccs_get_mode(profile, index);
+		r->param_type = CCS_TYPE_AUTO_TASK_ACL;
+		ccs_check_acl(r);
+		if (!r->granted)
+			return r->mode;
+		buf = container_of(r->matched_acl, typeof(struct ccs_task_acl),
+				   head)->domainname->name;
+		if (!ccs_assign_domain(buf, true))
+			break;
+	}
+	ccs_transition_failed(buf);
+	return CCS_CONFIG_DISABLED;
+#else
+	const u8 profile = ccs_current_domain()->profile;
+	memset(r, 0, sizeof(*r));
+	r->profile = profile;
+	r->type = index;
+	r->mode = ccs_get_mode(profile, index);
+	return r->mode;
+#endif
+}
+
+/**
+ * ccs_byte_range - Check whether the string is a \ooo style octal value.
+ *
+ * @str: Pointer to the string.
+ *
+ * Returns true if @str is a \ooo style octal value, false otherwise.
+ */
+static bool ccs_byte_range(const char *str)
+{
+	return *str >= '0' && *str++ <= '3' &&
+		*str >= '0' && *str++ <= '7' &&
+		*str >= '0' && *str <= '7';
+}
+
+/**
+ * ccs_decimal - Check whether the character is a decimal character.
+ *
+ * @c: The character to check.
+ *
+ * Returns true if @c is a decimal character, false otherwise.
+ */
+static bool ccs_decimal(const char c)
+{
+	return c >= '0' && c <= '9';
+}
+
+/**
+ * ccs_hexadecimal - Check whether the character is a hexadecimal character.
+ *
+ * @c: The character to check.
+ *
+ * Returns true if @c is a hexadecimal character, false otherwise.
+ */
+static bool ccs_hexadecimal(const char c)
+{
+	return (c >= '0' && c <= '9') ||
+		(c >= 'A' && c <= 'F') ||
+		(c >= 'a' && c <= 'f');
+}
+
+/**
+ * ccs_alphabet_char - Check whether the character is an alphabet.
+ *
+ * @c: The character to check.
+ *
+ * Returns true if @c is an alphabet character, false otherwise.
+ */
+static bool ccs_alphabet_char(const char c)
+{
+	return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z');
+}
+
+/**
+ * ccs_file_matches_pattern2 - Pattern matching without '/' character and "\-" pattern.
+ *
+ * @filename:     The start of string to check.
+ * @filename_end: The end of string to check.
+ * @pattern:      The start of pattern to compare.
+ * @pattern_end:  The end of pattern to compare.
+ *
+ * Returns true if @filename matches @pattern, false otherwise.
+ */
+static bool ccs_file_matches_pattern2(const char *filename,
+				      const char *filename_end,
+				      const char *pattern,
+				      const char *pattern_end)
+{
+	while (filename < filename_end && pattern < pattern_end) {
+		char c;
+		if (*pattern != '\\') {
+			if (*filename++ != *pattern++)
+				return false;
+			continue;
+		}
+		c = *filename;
+		pattern++;
+		switch (*pattern) {
+			int i;
+			int j;
+		case '?':
+			if (c == '/') {
+				return false;
+			} else if (c == '\\') {
+				if (filename[1] == '\\')
+					filename++;
+				else if (ccs_byte_range(filename + 1))
+					filename += 3;
+				else
+					return false;
+			}
+			break;
+		case '\\':
+			if (c != '\\')
+				return false;
+			if (*++filename != '\\')
+				return false;
+			break;
+		case '+':
+			if (!ccs_decimal(c))
+				return false;
+			break;
+		case 'x':
+			if (!ccs_hexadecimal(c))
+				return false;
+			break;
+		case 'a':
+			if (!ccs_alphabet_char(c))
+				return false;
+			break;
+		case '0':
+		case '1':
+		case '2':
+		case '3':
+			if (c == '\\' && ccs_byte_range(filename + 1)
+			    && !strncmp(filename + 1, pattern, 3)) {
+				filename += 3;
+				pattern += 2;
+				break;
+			}
+			return false; /* Not matched. */
+		case '*':
+		case '@':
+			for (i = 0; i <= filename_end - filename; i++) {
+				if (ccs_file_matches_pattern2(filename + i,
+							      filename_end,
+							      pattern + 1,
+							      pattern_end))
+					return true;
+				c = filename[i];
+				if (c == '.' && *pattern == '@')
+					break;
+				if (c != '\\')
+					continue;
+				if (filename[i + 1] == '\\')
+					i++;
+				else if (ccs_byte_range(filename + i + 1))
+					i += 3;
+				else
+					break; /* Bad pattern. */
+			}
+			return false; /* Not matched. */
+		default:
+			j = 0;
+			c = *pattern;
+			if (c == '$') {
+				while (ccs_decimal(filename[j]))
+					j++;
+			} else if (c == 'X') {
+				while (ccs_hexadecimal(filename[j]))
+					j++;
+			} else if (c == 'A') {
+				while (ccs_alphabet_char(filename[j]))
+					j++;
+			}
+			for (i = 1; i <= j; i++) {
+				if (ccs_file_matches_pattern2(filename + i,
+							      filename_end,
+							      pattern + 1,
+							      pattern_end))
+					return true;
+			}
+			return false; /* Not matched or bad pattern. */
+		}
+		filename++;
+		pattern++;
+	}
+	while (*pattern == '\\' &&
+	       (*(pattern + 1) == '*' || *(pattern + 1) == '@'))
+		pattern += 2;
+	return filename == filename_end && pattern == pattern_end;
+}
+
+/**
+ * ccs_file_matches_pattern - Pattern matching without '/' character.
+ *
+ * @filename:     The start of string to check.
+ * @filename_end: The end of string to check.
+ * @pattern:      The start of pattern to compare.
+ * @pattern_end:  The end of pattern to compare.
+ *
+ * Returns true if @filename matches @pattern, false otherwise.
+ */
+static bool ccs_file_matches_pattern(const char *filename,
+				     const char *filename_end,
+				     const char *pattern,
+				     const char *pattern_end)
+{
+	const char *pattern_start = pattern;
+	bool first = true;
+	bool result;
+	while (pattern < pattern_end - 1) {
+		/* Split at "\-" pattern. */
+		if (*pattern++ != '\\' || *pattern++ != '-')
+			continue;
+		result = ccs_file_matches_pattern2(filename, filename_end,
+						   pattern_start, pattern - 2);
+		if (first)
+			result = !result;
+		if (result)
+			return false;
+		first = false;
+		pattern_start = pattern;
+	}
+	result = ccs_file_matches_pattern2(filename, filename_end,
+					   pattern_start, pattern_end);
+	return first ? result : !result;
+}
+
+/**
+ * ccs_path_matches_pattern2 - Do pathname pattern matching.
+ *
+ * @f: The start of string to check.
+ * @p: The start of pattern to compare.
+ *
+ * Returns true if @f matches @p, false otherwise.
+ */
+static bool ccs_path_matches_pattern2(const char *f, const char *p)
+{
+	const char *f_delimiter;
+	const char *p_delimiter;
+	while (*f && *p) {
+		f_delimiter = strchr(f, '/');
+		if (!f_delimiter)
+			f_delimiter = f + strlen(f);
+		p_delimiter = strchr(p, '/');
+		if (!p_delimiter)
+			p_delimiter = p + strlen(p);
+		if (*p == '\\' && *(p + 1) == '{')
+			goto recursive;
+		if (!ccs_file_matches_pattern(f, f_delimiter, p, p_delimiter))
+			return false;
+		f = f_delimiter;
+		if (*f)
+			f++;
+		p = p_delimiter;
+		if (*p)
+			p++;
+	}
+	/* Ignore trailing "\*" and "\@" in @pattern. */
+	while (*p == '\\' &&
+	       (*(p + 1) == '*' || *(p + 1) == '@'))
+		p += 2;
+	return !*f && !*p;
+recursive:
+	/*
+	 * The "\{" pattern is permitted only after '/' character.
+	 * This guarantees that below "*(p - 1)" is safe.
+	 * Also, the "\}" pattern is permitted only before '/' character
+	 * so that "\{" + "\}" pair will not break the "\-" operator.
+	 */
+	if (*(p - 1) != '/' || p_delimiter <= p + 3 || *p_delimiter != '/' ||
+	    *(p_delimiter - 1) != '}' || *(p_delimiter - 2) != '\\')
+		return false; /* Bad pattern. */
+	do {
+		/* Compare current component with pattern. */
+		if (!ccs_file_matches_pattern(f, f_delimiter, p + 2,
+					      p_delimiter - 2))
+			break;
+		/* Proceed to next component. */
+		f = f_delimiter;
+		if (!*f)
+			break;
+		f++;
+		/* Continue comparison. */
+		if (ccs_path_matches_pattern2(f, p_delimiter + 1))
+			return true;
+		f_delimiter = strchr(f, '/');
+	} while (f_delimiter);
+	return false; /* Not matched. */
+}
+
+/**
+ * ccs_path_matches_pattern - Check whether the given filename matches the given pattern.
+ *
+ * @filename: The filename to check.
+ * @pattern:  The pattern to compare.
+ *
+ * Returns true if matches, false otherwise.
+ *
+ * The following patterns are available.
+ *   \\     \ itself.
+ *   \ooo   Octal representation of a byte.
+ *   \*     Zero or more repetitions of characters other than '/'.
+ *   \@     Zero or more repetitions of characters other than '/' or '.'.
+ *   \?     1 byte character other than '/'.
+ *   \$     One or more repetitions of decimal digits.
+ *   \+     1 decimal digit.
+ *   \X     One or more repetitions of hexadecimal digits.
+ *   \x     1 hexadecimal digit.
+ *   \A     One or more repetitions of alphabet characters.
+ *   \a     1 alphabet character.
+ *
+ *   \-     Subtraction operator.
+ *
+ *   /\{dir\}/   '/' + 'One or more repetitions of dir/' (e.g. /dir/ /dir/dir/
+ *               /dir/dir/dir/ ).
+ */
+static bool ccs_path_matches_pattern(const struct ccs_path_info *filename,
+				     const struct ccs_path_info *pattern)
+{
+	const char *f = filename->name;
+	const char *p = pattern->name;
+	const int len = pattern->const_len;
+	/* If @pattern doesn't contain pattern, I can use strcmp(). */
+	if (!pattern->is_patterned)
+		return !ccs_pathcmp(filename, pattern);
+	/* Don't compare directory and non-directory. */
+	if (filename->is_dir != pattern->is_dir)
+		return false;
+	/* Compare the initial length without patterns. */
+	if (strncmp(f, p, len))
+		return false;
+	f += len;
+	p += len;
+	return ccs_path_matches_pattern2(f, p);
+}
diff --git a/security/ccsecurity/policy_io.c b/security/ccsecurity/policy_io.c
new file mode 100644
index 0000000..67adb50
--- /dev/null
+++ b/security/ccsecurity/policy_io.c
@@ -0,0 +1,6484 @@
+/*
+ * security/ccsecurity/policy_io.c
+ *
+ * Copyright (C) 2005-2012  NTT DATA CORPORATION
+ *
+ * Version: 1.8.4   2015/05/05
+ */
+
+#include "internal.h"
+
+/***** SECTION1: Constants definition *****/
+
+/* Define this to enable debug mode. */
+/* #define DEBUG_CONDITION */
+
+#ifdef DEBUG_CONDITION
+#define dprintk printk
+#else
+#define dprintk(...) do { } while (0)
+#endif
+
+/* Mapping table from "enum ccs_mac_index" to "enum ccs_mac_category_index". */
+static const u8 ccs_index2category[CCS_MAX_MAC_INDEX] = {
+	/* CONFIG::file group */
+	[CCS_MAC_FILE_EXECUTE]    = CCS_MAC_CATEGORY_FILE,
+	[CCS_MAC_FILE_OPEN]       = CCS_MAC_CATEGORY_FILE,
+	[CCS_MAC_FILE_CREATE]     = CCS_MAC_CATEGORY_FILE,
+	[CCS_MAC_FILE_UNLINK]     = CCS_MAC_CATEGORY_FILE,
+#ifdef CONFIG_CCSECURITY_FILE_GETATTR
+	[CCS_MAC_FILE_GETATTR]    = CCS_MAC_CATEGORY_FILE,
+#endif
+	[CCS_MAC_FILE_MKDIR]      = CCS_MAC_CATEGORY_FILE,
+	[CCS_MAC_FILE_RMDIR]      = CCS_MAC_CATEGORY_FILE,
+	[CCS_MAC_FILE_MKFIFO]     = CCS_MAC_CATEGORY_FILE,
+	[CCS_MAC_FILE_MKSOCK]     = CCS_MAC_CATEGORY_FILE,
+	[CCS_MAC_FILE_TRUNCATE]   = CCS_MAC_CATEGORY_FILE,
+	[CCS_MAC_FILE_SYMLINK]    = CCS_MAC_CATEGORY_FILE,
+	[CCS_MAC_FILE_MKBLOCK]    = CCS_MAC_CATEGORY_FILE,
+	[CCS_MAC_FILE_MKCHAR]     = CCS_MAC_CATEGORY_FILE,
+	[CCS_MAC_FILE_LINK]       = CCS_MAC_CATEGORY_FILE,
+	[CCS_MAC_FILE_RENAME]     = CCS_MAC_CATEGORY_FILE,
+	[CCS_MAC_FILE_CHMOD]      = CCS_MAC_CATEGORY_FILE,
+	[CCS_MAC_FILE_CHOWN]      = CCS_MAC_CATEGORY_FILE,
+	[CCS_MAC_FILE_CHGRP]      = CCS_MAC_CATEGORY_FILE,
+	[CCS_MAC_FILE_IOCTL]      = CCS_MAC_CATEGORY_FILE,
+	[CCS_MAC_FILE_CHROOT]     = CCS_MAC_CATEGORY_FILE,
+	[CCS_MAC_FILE_MOUNT]      = CCS_MAC_CATEGORY_FILE,
+	[CCS_MAC_FILE_UMOUNT]     = CCS_MAC_CATEGORY_FILE,
+	[CCS_MAC_FILE_PIVOT_ROOT] = CCS_MAC_CATEGORY_FILE,
+#ifdef CONFIG_CCSECURITY_MISC
+	/* CONFIG::misc group */
+	[CCS_MAC_ENVIRON]         = CCS_MAC_CATEGORY_MISC,
+#endif
+#ifdef CONFIG_CCSECURITY_NETWORK
+	/* CONFIG::network group */
+	[CCS_MAC_NETWORK_INET_STREAM_BIND]       = CCS_MAC_CATEGORY_NETWORK,
+	[CCS_MAC_NETWORK_INET_STREAM_LISTEN]     = CCS_MAC_CATEGORY_NETWORK,
+	[CCS_MAC_NETWORK_INET_STREAM_CONNECT]    = CCS_MAC_CATEGORY_NETWORK,
+	[CCS_MAC_NETWORK_INET_STREAM_ACCEPT]     = CCS_MAC_CATEGORY_NETWORK,
+	[CCS_MAC_NETWORK_INET_DGRAM_BIND]        = CCS_MAC_CATEGORY_NETWORK,
+	[CCS_MAC_NETWORK_INET_DGRAM_SEND]        = CCS_MAC_CATEGORY_NETWORK,
+#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG
+	[CCS_MAC_NETWORK_INET_DGRAM_RECV]        = CCS_MAC_CATEGORY_NETWORK,
+#endif
+	[CCS_MAC_NETWORK_INET_RAW_BIND]          = CCS_MAC_CATEGORY_NETWORK,
+	[CCS_MAC_NETWORK_INET_RAW_SEND]          = CCS_MAC_CATEGORY_NETWORK,
+#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG
+	[CCS_MAC_NETWORK_INET_RAW_RECV]          = CCS_MAC_CATEGORY_NETWORK,
+#endif
+	[CCS_MAC_NETWORK_UNIX_STREAM_BIND]       = CCS_MAC_CATEGORY_NETWORK,
+	[CCS_MAC_NETWORK_UNIX_STREAM_LISTEN]     = CCS_MAC_CATEGORY_NETWORK,
+	[CCS_MAC_NETWORK_UNIX_STREAM_CONNECT]    = CCS_MAC_CATEGORY_NETWORK,
+	[CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT]     = CCS_MAC_CATEGORY_NETWORK,
+	[CCS_MAC_NETWORK_UNIX_DGRAM_BIND]        = CCS_MAC_CATEGORY_NETWORK,
+	[CCS_MAC_NETWORK_UNIX_DGRAM_SEND]        = CCS_MAC_CATEGORY_NETWORK,
+#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG
+	[CCS_MAC_NETWORK_UNIX_DGRAM_RECV]        = CCS_MAC_CATEGORY_NETWORK,
+#endif
+	[CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND]    = CCS_MAC_CATEGORY_NETWORK,
+	[CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN]  = CCS_MAC_CATEGORY_NETWORK,
+	[CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT] = CCS_MAC_CATEGORY_NETWORK,
+	[CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT]  = CCS_MAC_CATEGORY_NETWORK,
+#endif
+#ifdef CONFIG_CCSECURITY_IPC
+	/* CONFIG::ipc group */
+	[CCS_MAC_SIGNAL]          = CCS_MAC_CATEGORY_IPC,
+#endif
+#ifdef CONFIG_CCSECURITY_CAPABILITY
+	/* CONFIG::capability group */
+	[CCS_MAC_CAPABILITY_USE_ROUTE_SOCKET]  = CCS_MAC_CATEGORY_CAPABILITY,
+	[CCS_MAC_CAPABILITY_USE_PACKET_SOCKET] = CCS_MAC_CATEGORY_CAPABILITY,
+	[CCS_MAC_CAPABILITY_SYS_REBOOT]        = CCS_MAC_CATEGORY_CAPABILITY,
+	[CCS_MAC_CAPABILITY_SYS_VHANGUP]       = CCS_MAC_CATEGORY_CAPABILITY,
+	[CCS_MAC_CAPABILITY_SYS_SETTIME]       = CCS_MAC_CATEGORY_CAPABILITY,
+	[CCS_MAC_CAPABILITY_SYS_NICE]          = CCS_MAC_CATEGORY_CAPABILITY,
+	[CCS_MAC_CAPABILITY_SYS_SETHOSTNAME]   = CCS_MAC_CATEGORY_CAPABILITY,
+	[CCS_MAC_CAPABILITY_USE_KERNEL_MODULE] = CCS_MAC_CATEGORY_CAPABILITY,
+	[CCS_MAC_CAPABILITY_SYS_KEXEC_LOAD]    = CCS_MAC_CATEGORY_CAPABILITY,
+	[CCS_MAC_CAPABILITY_SYS_PTRACE]        = CCS_MAC_CATEGORY_CAPABILITY,
+#endif
+};
+
+/* String table for operation mode. */
+static const char * const ccs_mode[CCS_CONFIG_MAX_MODE] = {
+	[CCS_CONFIG_DISABLED]   = "disabled",
+	[CCS_CONFIG_LEARNING]   = "learning",
+	[CCS_CONFIG_PERMISSIVE] = "permissive",
+	[CCS_CONFIG_ENFORCING]  = "enforcing"
+};
+
+/* String table for /proc/ccs/profile interface. */
+static const char * const ccs_mac_keywords[CCS_MAX_MAC_INDEX
+					   + CCS_MAX_MAC_CATEGORY_INDEX] = {
+	/* CONFIG::file group */
+	[CCS_MAC_FILE_EXECUTE]    = "execute",
+	[CCS_MAC_FILE_OPEN]       = "open",
+	[CCS_MAC_FILE_CREATE]     = "create",
+	[CCS_MAC_FILE_UNLINK]     = "unlink",
+#ifdef CONFIG_CCSECURITY_FILE_GETATTR
+	[CCS_MAC_FILE_GETATTR]    = "getattr",
+#endif
+	[CCS_MAC_FILE_MKDIR]      = "mkdir",
+	[CCS_MAC_FILE_RMDIR]      = "rmdir",
+	[CCS_MAC_FILE_MKFIFO]     = "mkfifo",
+	[CCS_MAC_FILE_MKSOCK]     = "mksock",
+	[CCS_MAC_FILE_TRUNCATE]   = "truncate",
+	[CCS_MAC_FILE_SYMLINK]    = "symlink",
+	[CCS_MAC_FILE_MKBLOCK]    = "mkblock",
+	[CCS_MAC_FILE_MKCHAR]     = "mkchar",
+	[CCS_MAC_FILE_LINK]       = "link",
+	[CCS_MAC_FILE_RENAME]     = "rename",
+	[CCS_MAC_FILE_CHMOD]      = "chmod",
+	[CCS_MAC_FILE_CHOWN]      = "chown",
+	[CCS_MAC_FILE_CHGRP]      = "chgrp",
+	[CCS_MAC_FILE_IOCTL]      = "ioctl",
+	[CCS_MAC_FILE_CHROOT]     = "chroot",
+	[CCS_MAC_FILE_MOUNT]      = "mount",
+	[CCS_MAC_FILE_UMOUNT]     = "unmount",
+	[CCS_MAC_FILE_PIVOT_ROOT] = "pivot_root",
+#ifdef CONFIG_CCSECURITY_MISC
+	/* CONFIG::misc group */
+	[CCS_MAC_ENVIRON] = "env",
+#endif
+#ifdef CONFIG_CCSECURITY_NETWORK
+	/* CONFIG::network group */
+	[CCS_MAC_NETWORK_INET_STREAM_BIND]       = "inet_stream_bind",
+	[CCS_MAC_NETWORK_INET_STREAM_LISTEN]     = "inet_stream_listen",
+	[CCS_MAC_NETWORK_INET_STREAM_CONNECT]    = "inet_stream_connect",
+	[CCS_MAC_NETWORK_INET_STREAM_ACCEPT]     = "inet_stream_accept",
+	[CCS_MAC_NETWORK_INET_DGRAM_BIND]        = "inet_dgram_bind",
+	[CCS_MAC_NETWORK_INET_DGRAM_SEND]        = "inet_dgram_send",
+#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG
+	[CCS_MAC_NETWORK_INET_DGRAM_RECV]        = "inet_dgram_recv",
+#endif
+	[CCS_MAC_NETWORK_INET_RAW_BIND]          = "inet_raw_bind",
+	[CCS_MAC_NETWORK_INET_RAW_SEND]          = "inet_raw_send",
+#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG
+	[CCS_MAC_NETWORK_INET_RAW_RECV]          = "inet_raw_recv",
+#endif
+	[CCS_MAC_NETWORK_UNIX_STREAM_BIND]       = "unix_stream_bind",
+	[CCS_MAC_NETWORK_UNIX_STREAM_LISTEN]     = "unix_stream_listen",
+	[CCS_MAC_NETWORK_UNIX_STREAM_CONNECT]    = "unix_stream_connect",
+	[CCS_MAC_NETWORK_UNIX_STREAM_ACCEPT]     = "unix_stream_accept",
+	[CCS_MAC_NETWORK_UNIX_DGRAM_BIND]        = "unix_dgram_bind",
+	[CCS_MAC_NETWORK_UNIX_DGRAM_SEND]        = "unix_dgram_send",
+#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG
+	[CCS_MAC_NETWORK_UNIX_DGRAM_RECV]        = "unix_dgram_recv",
+#endif
+	[CCS_MAC_NETWORK_UNIX_SEQPACKET_BIND]    = "unix_seqpacket_bind",
+	[CCS_MAC_NETWORK_UNIX_SEQPACKET_LISTEN]  = "unix_seqpacket_listen",
+	[CCS_MAC_NETWORK_UNIX_SEQPACKET_CONNECT] = "unix_seqpacket_connect",
+	[CCS_MAC_NETWORK_UNIX_SEQPACKET_ACCEPT]  = "unix_seqpacket_accept",
+#endif
+#ifdef CONFIG_CCSECURITY_IPC
+	/* CONFIG::ipc group */
+	[CCS_MAC_SIGNAL] = "signal",
+#endif
+#ifdef CONFIG_CCSECURITY_CAPABILITY
+	/* CONFIG::capability group */
+	[CCS_MAC_CAPABILITY_USE_ROUTE_SOCKET]  = "use_route",
+	[CCS_MAC_CAPABILITY_USE_PACKET_SOCKET] = "use_packet",
+	[CCS_MAC_CAPABILITY_SYS_REBOOT]        = "SYS_REBOOT",
+	[CCS_MAC_CAPABILITY_SYS_VHANGUP]       = "SYS_VHANGUP",
+	[CCS_MAC_CAPABILITY_SYS_SETTIME]       = "SYS_TIME",
+	[CCS_MAC_CAPABILITY_SYS_NICE]          = "SYS_NICE",
+	[CCS_MAC_CAPABILITY_SYS_SETHOSTNAME]   = "SYS_SETHOSTNAME",
+	[CCS_MAC_CAPABILITY_USE_KERNEL_MODULE] = "use_kernel_module",
+	[CCS_MAC_CAPABILITY_SYS_KEXEC_LOAD]    = "SYS_KEXEC_LOAD",
+	[CCS_MAC_CAPABILITY_SYS_PTRACE]        = "SYS_PTRACE",
+#endif
+	/* CONFIG group */
+	[CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_FILE]       = "file",
+#ifdef CONFIG_CCSECURITY_NETWORK
+	[CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_NETWORK]    = "network",
+#endif
+#ifdef CONFIG_CCSECURITY_MISC
+	[CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_MISC]       = "misc",
+#endif
+#ifdef CONFIG_CCSECURITY_IPC
+	[CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_IPC]        = "ipc",
+#endif
+#ifdef CONFIG_CCSECURITY_CAPABILITY
+	[CCS_MAX_MAC_INDEX + CCS_MAC_CATEGORY_CAPABILITY] = "capability",
+#endif
+};
+
+/* String table for path operation. */
+static const char * const ccs_path_keyword[CCS_MAX_PATH_OPERATION] = {
+	[CCS_TYPE_EXECUTE]    = "execute",
+	[CCS_TYPE_READ]       = "read",
+	[CCS_TYPE_WRITE]      = "write",
+	[CCS_TYPE_APPEND]     = "append",
+	[CCS_TYPE_UNLINK]     = "unlink",
+#ifdef CONFIG_CCSECURITY_FILE_GETATTR
+	[CCS_TYPE_GETATTR]    = "getattr",
+#endif
+	[CCS_TYPE_RMDIR]      = "rmdir",
+	[CCS_TYPE_TRUNCATE]   = "truncate",
+	[CCS_TYPE_SYMLINK]    = "symlink",
+	[CCS_TYPE_CHROOT]     = "chroot",
+	[CCS_TYPE_UMOUNT]     = "unmount",
+};
+
+#ifdef CONFIG_CCSECURITY_NETWORK
+
+/* String table for socket's operation. */
+static const char * const ccs_socket_keyword[CCS_MAX_NETWORK_OPERATION] = {
+	[CCS_NETWORK_BIND]    = "bind",
+	[CCS_NETWORK_LISTEN]  = "listen",
+	[CCS_NETWORK_CONNECT] = "connect",
+	[CCS_NETWORK_ACCEPT]  = "accept",
+	[CCS_NETWORK_SEND]    = "send",
+#ifdef CONFIG_CCSECURITY_NETWORK_RECVMSG
+	[CCS_NETWORK_RECV]    = "recv",
+#endif
+};
+
+/* String table for socket's protocols. */
+static const char * const ccs_proto_keyword[CCS_SOCK_MAX] = {
+	[SOCK_STREAM]    = "stream",
+	[SOCK_DGRAM]     = "dgram",
+	[SOCK_RAW]       = "raw",
+	[SOCK_SEQPACKET] = "seqpacket",
+	[0] = " ", /* Dummy for avoiding NULL pointer dereference. */
+	[4] = " ", /* Dummy for avoiding NULL pointer dereference. */
+};
+
+#endif
+
+/* String table for categories. */
+static const char * const ccs_category_keywords[CCS_MAX_MAC_CATEGORY_INDEX] = {
+	[CCS_MAC_CATEGORY_FILE]       = "file",
+#ifdef CONFIG_CCSECURITY_NETWORK
+	[CCS_MAC_CATEGORY_NETWORK]    = "network",
+#endif
+#ifdef CONFIG_CCSECURITY_MISC
+	[CCS_MAC_CATEGORY_MISC]       = "misc",
+#endif
+#ifdef CONFIG_CCSECURITY_IPC
+	[CCS_MAC_CATEGORY_IPC]        = "ipc",
+#endif
+#ifdef CONFIG_CCSECURITY_CAPABILITY
+	[CCS_MAC_CATEGORY_CAPABILITY] = "capability",
+#endif
+};
+
+/* String table for conditions. */
+static const char * const ccs_condition_keyword[CCS_MAX_CONDITION_KEYWORD] = {
+	[CCS_TASK_UID]             = "task.uid",
+	[CCS_TASK_EUID]            = "task.euid",
+	[CCS_TASK_SUID]            = "task.suid",
+	[CCS_TASK_FSUID]           = "task.fsuid",
+	[CCS_TASK_GID]             = "task.gid",
+	[CCS_TASK_EGID]            = "task.egid",
+	[CCS_TASK_SGID]            = "task.sgid",
+	[CCS_TASK_FSGID]           = "task.fsgid",
+	[CCS_TASK_PID]             = "task.pid",
+	[CCS_TASK_PPID]            = "task.ppid",
+	[CCS_EXEC_ARGC]            = "exec.argc",
+	[CCS_EXEC_ENVC]            = "exec.envc",
+	[CCS_TYPE_IS_SOCKET]       = "socket",
+	[CCS_TYPE_IS_SYMLINK]      = "symlink",
+	[CCS_TYPE_IS_FILE]         = "file",
+	[CCS_TYPE_IS_BLOCK_DEV]    = "block",
+	[CCS_TYPE_IS_DIRECTORY]    = "directory",
+	[CCS_TYPE_IS_CHAR_DEV]     = "char",
+	[CCS_TYPE_IS_FIFO]         = "fifo",
+	[CCS_MODE_SETUID]          = "setuid",
+	[CCS_MODE_SETGID]          = "setgid",
+	[CCS_MODE_STICKY]          = "sticky",
+	[CCS_MODE_OWNER_READ]      = "owner_read",
+	[CCS_MODE_OWNER_WRITE]     = "owner_write",
+	[CCS_MODE_OWNER_EXECUTE]   = "owner_execute",
+	[CCS_MODE_GROUP_READ]      = "group_read",
+	[CCS_MODE_GROUP_WRITE]     = "group_write",
+	[CCS_MODE_GROUP_EXECUTE]   = "group_execute",
+	[CCS_MODE_OTHERS_READ]     = "others_read",
+	[CCS_MODE_OTHERS_WRITE]    = "others_write",
+	[CCS_MODE_OTHERS_EXECUTE]  = "others_execute",
+	[CCS_TASK_TYPE]            = "task.type",
+	[CCS_TASK_EXECUTE_HANDLER] = "execute_handler",
+	[CCS_EXEC_REALPATH]        = "exec.realpath",
+	[CCS_SYMLINK_TARGET]       = "symlink.target",
+	[CCS_PATH1_UID]            = "path1.uid",
+	[CCS_PATH1_GID]            = "path1.gid",
+	[CCS_PATH1_INO]            = "path1.ino",
+	[CCS_PATH1_MAJOR]          = "path1.major",
+	[CCS_PATH1_MINOR]          = "path1.minor",
+	[CCS_PATH1_PERM]           = "path1.perm",
+	[CCS_PATH1_TYPE]           = "path1.type",
+	[CCS_PATH1_DEV_MAJOR]      = "path1.dev_major",
+	[CCS_PATH1_DEV_MINOR]      = "path1.dev_minor",
+	[CCS_PATH2_UID]            = "path2.uid",
+	[CCS_PATH2_GID]            = "path2.gid",
+	[CCS_PATH2_INO]            = "path2.ino",
+	[CCS_PATH2_MAJOR]          = "path2.major",
+	[CCS_PATH2_MINOR]          = "path2.minor",
+	[CCS_PATH2_PERM]           = "path2.perm",
+	[CCS_PATH2_TYPE]           = "path2.type",
+	[CCS_PATH2_DEV_MAJOR]      = "path2.dev_major",
+	[CCS_PATH2_DEV_MINOR]      = "path2.dev_minor",
+	[CCS_PATH1_PARENT_UID]     = "path1.parent.uid",
+	[CCS_PATH1_PARENT_GID]     = "path1.parent.gid",
+	[CCS_PATH1_PARENT_INO]     = "path1.parent.ino",
+	[CCS_PATH1_PARENT_PERM]    = "path1.parent.perm",
+	[CCS_PATH2_PARENT_UID]     = "path2.parent.uid",
+	[CCS_PATH2_PARENT_GID]     = "path2.parent.gid",
+	[CCS_PATH2_PARENT_INO]     = "path2.parent.ino",
+	[CCS_PATH2_PARENT_PERM]    = "path2.parent.perm",
+};
+
+/* String table for PREFERENCE keyword. */
+static const char * const ccs_pref_keywords[CCS_MAX_PREF] = {
+	[CCS_PREF_MAX_AUDIT_LOG]      = "max_audit_log",
+	[CCS_PREF_MAX_LEARNING_ENTRY] = "max_learning_entry",
+	[CCS_PREF_ENFORCING_PENALTY]  = "enforcing_penalty",
+};
+
+/* String table for domain flags. */
+const char * const ccs_dif[CCS_MAX_DOMAIN_INFO_FLAGS] = {
+	[CCS_DIF_QUOTA_WARNED]      = "quota_exceeded\n",
+	[CCS_DIF_TRANSITION_FAILED] = "transition_failed\n",
+};
+
+/* String table for domain transition control keywords. */
+static const char * const ccs_transition_type[CCS_MAX_TRANSITION_TYPE] = {
+	[CCS_TRANSITION_CONTROL_NO_RESET]      = "no_reset_domain ",
+	[CCS_TRANSITION_CONTROL_RESET]         = "reset_domain ",
+	[CCS_TRANSITION_CONTROL_NO_INITIALIZE] = "no_initialize_domain ",
+	[CCS_TRANSITION_CONTROL_INITIALIZE]    = "initialize_domain ",
+	[CCS_TRANSITION_CONTROL_NO_KEEP]       = "no_keep_domain ",
+	[CCS_TRANSITION_CONTROL_KEEP]          = "keep_domain ",
+};
+
+/* String table for grouping keywords. */
+static const char * const ccs_group_name[CCS_MAX_GROUP] = {
+	[CCS_PATH_GROUP]    = "path_group ",
+	[CCS_NUMBER_GROUP]  = "number_group ",
+#ifdef CONFIG_CCSECURITY_NETWORK
+	[CCS_ADDRESS_GROUP] = "address_group ",
+#endif
+};
+
+/* String table for /proc/ccs/stat interface. */
+static const char * const ccs_policy_headers[CCS_MAX_POLICY_STAT] = {
+	[CCS_STAT_POLICY_UPDATES]    = "update:",
+	[CCS_STAT_POLICY_LEARNING]   = "violation in learning mode:",
+	[CCS_STAT_POLICY_PERMISSIVE] = "violation in permissive mode:",
+	[CCS_STAT_POLICY_ENFORCING]  = "violation in enforcing mode:",
+};
+
+/* String table for /proc/ccs/stat interface. */
+static const char * const ccs_memory_headers[CCS_MAX_MEMORY_STAT] = {
+	[CCS_MEMORY_POLICY]     = "policy:",
+	[CCS_MEMORY_AUDIT]      = "audit log:",
+	[CCS_MEMORY_QUERY]      = "query message:",
+};
+
+/***** SECTION2: Structure definition *****/
+
+struct iattr;
+
+/* Structure for query. */
+struct ccs_query {
+	struct list_head list;
+	struct ccs_domain_info *domain;
+	char *query;
+	size_t query_len;
+	unsigned int serial;
+	u8 timer;
+	u8 answer;
+	u8 retry;
+};
+
+/* Structure for audit log. */
+struct ccs_log {
+	struct list_head list;
+	char *log;
+	int size;
+};
+
+/***** SECTION3: Prototype definition section *****/
+
+int ccs_audit_log(struct ccs_request_info *r);
+struct ccs_domain_info *ccs_assign_domain(const char *domainname,
+					  const bool transit);
+u8 ccs_get_config(const u8 profile, const u8 index);
+void ccs_transition_failed(const char *domainname);
+void ccs_write_log(struct ccs_request_info *r, const char *fmt, ...);
+
+static bool ccs_correct_domain(const unsigned char *domainname);
+static bool ccs_correct_path(const char *filename);
+static bool ccs_correct_word(const char *string);
+static bool ccs_correct_word2(const char *string, size_t len);
+static bool ccs_domain_def(const unsigned char *buffer);
+static bool ccs_domain_quota_ok(struct ccs_request_info *r);
+static bool ccs_flush(struct ccs_io_buffer *head);
+static bool ccs_get_audit(const struct ccs_request_info *r);
+static bool ccs_has_more_namespace(struct ccs_io_buffer *head);
+static bool ccs_manager(void);
+static bool ccs_namespace_jump(const char *domainname);
+static bool ccs_parse_argv(char *left, char *right, struct ccs_argv *argv);
+static bool ccs_parse_envp(char *left, char *right, struct ccs_envp *envp);
+static bool ccs_parse_name_union(struct ccs_acl_param *param,
+				 struct ccs_name_union *ptr);
+static bool ccs_parse_name_union_quoted(struct ccs_acl_param *param,
+					struct ccs_name_union *ptr);
+static bool ccs_parse_number_union(struct ccs_acl_param *param,
+				   struct ccs_number_union *ptr);
+static bool ccs_permstr(const char *string, const char *keyword);
+static bool ccs_print_condition(struct ccs_io_buffer *head,
+				const struct ccs_condition *cond);
+static bool ccs_print_entry(struct ccs_io_buffer *head,
+			    const struct ccs_acl_info *acl);
+static bool ccs_print_group(struct ccs_io_buffer *head,
+			    const struct ccs_group *group);
+static bool ccs_read_acl(struct ccs_io_buffer *head, struct list_head *list);
+static bool ccs_read_group(struct ccs_io_buffer *head, const int idx);
+static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx);
+static bool ccs_same_condition(const struct ccs_condition *a,
+			       const struct ccs_condition *b);
+static bool ccs_select_domain(struct ccs_io_buffer *head, const char *data);
+static bool ccs_set_lf(struct ccs_io_buffer *head);
+static bool ccs_str_starts(char **src, const char *find);
+static char *ccs_get_transit_preference(struct ccs_acl_param *param,
+					struct ccs_condition *e);
+static char *ccs_init_log(struct ccs_request_info *r, int len, const char *fmt,
+			  va_list args);
+static char *ccs_print_bprm(struct linux_binprm *bprm,
+			    struct ccs_page_dump *dump);
+static char *ccs_print_header(struct ccs_request_info *r);
+static char *ccs_read_token(struct ccs_acl_param *param);
+static const char *ccs_yesno(const unsigned int value);
+static const struct ccs_path_info *ccs_get_domainname
+(struct ccs_acl_param *param);
+static const struct ccs_path_info *ccs_get_dqword(char *start);
+static int __init ccs_init_module(void);
+static int ccs_delete_domain(char *domainname);
+static int ccs_open(struct inode *inode, struct file *file);
+static int ccs_parse_policy(struct ccs_io_buffer *head, char *line);
+static int ccs_release(struct inode *inode, struct file *file);
+static int ccs_set_mode(char *name, const char *value,
+			struct ccs_profile *profile);
+static int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)
+	__printf(2, 3);
+static int ccs_truncate(char *str);
+static int ccs_update_acl(const int size, struct ccs_acl_param *param);
+static int ccs_update_manager_entry(const char *manager, const bool is_delete);
+static int ccs_update_policy(const int size, struct ccs_acl_param *param);
+static int ccs_write_acl(struct ccs_policy_namespace *ns,
+			 struct list_head *list, char *data,
+			 const bool is_delete);
+static int ccs_write_aggregator(struct ccs_acl_param *param);
+static int ccs_write_answer(struct ccs_io_buffer *head);
+static int ccs_write_domain(struct ccs_io_buffer *head);
+static int ccs_write_exception(struct ccs_io_buffer *head);
+static int ccs_write_file(struct ccs_acl_param *param);
+static int ccs_write_group(struct ccs_acl_param *param, const u8 type);
+static int ccs_write_manager(struct ccs_io_buffer *head);
+static int ccs_write_pid(struct ccs_io_buffer *head);
+static int ccs_write_profile(struct ccs_io_buffer *head);
+static int ccs_write_stat(struct ccs_io_buffer *head);
+static int ccs_write_task(struct ccs_acl_param *param);
+static int ccs_write_transition_control(struct ccs_acl_param *param,
+					const u8 type);
+static s8 ccs_find_yesno(const char *string, const char *find);
+static ssize_t ccs_read(struct file *file, char __user *buf, size_t count,
+			loff_t *ppos);
+static ssize_t ccs_read_self(struct file *file, char __user *buf, size_t count,
+			     loff_t *ppos);
+static ssize_t ccs_write(struct file *file, const char __user *buf,
+			 size_t count, loff_t *ppos);
+static struct ccs_condition *ccs_commit_condition(struct ccs_condition *entry);
+static struct ccs_condition *ccs_get_condition(struct ccs_acl_param *param);
+static struct ccs_domain_info *ccs_find_domain(const char *domainname);
+static struct ccs_domain_info *ccs_find_domain_by_qid(unsigned int serial);
+static struct ccs_group *ccs_get_group(struct ccs_acl_param *param,
+				       const u8 idx);
+static struct ccs_policy_namespace *ccs_assign_namespace
+(const char *domainname);
+static struct ccs_policy_namespace *ccs_find_namespace(const char *name,
+						       const unsigned int len);
+static struct ccs_profile *ccs_assign_profile(struct ccs_policy_namespace *ns,
+					      const unsigned int profile);
+static struct ccs_profile *ccs_profile(const u8 profile);
+static u8 ccs_condition_type(const char *word);
+static u8 ccs_make_byte(const u8 c1, const u8 c2, const u8 c3);
+static u8 ccs_parse_ulong(unsigned long *result, char **str);
+static unsigned int ccs_poll(struct file *file, poll_table *wait);
+static void __init ccs_create_entry(const char *name, const umode_t mode,
+				    struct proc_dir_entry *parent,
+				    const u8 key);
+static void __init ccs_load_builtin_policy(void);
+static void __init ccs_policy_io_init(void);
+static void __init ccs_proc_init(void);
+static void ccs_add_entry(char *header);
+static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
+	__printf(3, 4);
+static void ccs_addprintf(char *buffer, int len, const char *fmt, ...);
+static void ccs_check_profile(void);
+static void ccs_convert_time(time_t time, struct ccs_time *stamp);
+static void ccs_init_policy_namespace(struct ccs_policy_namespace *ns);
+static void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
+	__printf(2, 3);
+static void ccs_normalize_line(unsigned char *buffer);
+static void ccs_print_config(struct ccs_io_buffer *head, const u8 config);
+static void ccs_print_name_union(struct ccs_io_buffer *head,
+				 const struct ccs_name_union *ptr);
+static void ccs_print_name_union_quoted(struct ccs_io_buffer *head,
+					const struct ccs_name_union *ptr);
+static void ccs_print_namespace(struct ccs_io_buffer *head);
+static void ccs_print_number_union(struct ccs_io_buffer *head,
+				   const struct ccs_number_union *ptr);
+static void ccs_print_number_union_nospace(struct ccs_io_buffer *head,
+					   const struct ccs_number_union *ptr);
+static void ccs_read_domain(struct ccs_io_buffer *head);
+static void ccs_read_exception(struct ccs_io_buffer *head);
+static void ccs_read_log(struct ccs_io_buffer *head);
+static void ccs_read_manager(struct ccs_io_buffer *head);
+static void ccs_read_pid(struct ccs_io_buffer *head);
+static void ccs_read_profile(struct ccs_io_buffer *head);
+static void ccs_read_query(struct ccs_io_buffer *head);
+static void ccs_read_stat(struct ccs_io_buffer *head);
+static void ccs_read_version(struct ccs_io_buffer *head);
+static void ccs_set_group(struct ccs_io_buffer *head, const char *category);
+static void ccs_set_namespace_cursor(struct ccs_io_buffer *head);
+static void ccs_set_slash(struct ccs_io_buffer *head);
+static void ccs_set_space(struct ccs_io_buffer *head);
+static void ccs_set_string(struct ccs_io_buffer *head, const char *string);
+static void ccs_set_uint(unsigned int *i, const char *string,
+			 const char *find);
+static void ccs_update_stat(const u8 index);
+static void ccs_update_task_domain(struct ccs_request_info *r);
+static void ccs_write_log2(struct ccs_request_info *r, int len,
+			   const char *fmt, va_list args);
+
+#ifdef CONFIG_CCSECURITY_PORTRESERVE
+static bool __ccs_lport_reserved(const u16 port);
+static int ccs_write_reserved_port(struct ccs_acl_param *param);
+#endif
+
+#ifdef CONFIG_CCSECURITY_NETWORK
+static bool ccs_parse_ipaddr_union(struct ccs_acl_param *param,
+				   struct ccs_ipaddr_union *ptr);
+static int ccs_print_ipv4(char *buffer, const unsigned int buffer_len,
+			  const u32 *ip);
+static int ccs_print_ipv6(char *buffer, const unsigned int buffer_len,
+			  const struct in6_addr *ip);
+static int ccs_write_inet_network(struct ccs_acl_param *param);
+static int ccs_write_unix_network(struct ccs_acl_param *param);
+static void ccs_print_ip(char *buf, const unsigned int size,
+			 const struct ccs_ipaddr_union *ptr);
+#endif
+
+#ifdef CONFIG_CCSECURITY_CAPABILITY
+static int ccs_write_capability(struct ccs_acl_param *param);
+#endif
+
+#ifdef CONFIG_CCSECURITY_MISC
+static int ccs_write_misc(struct ccs_acl_param *param);
+#endif
+
+#ifdef CONFIG_CCSECURITY_IPC
+static int ccs_write_ipc(struct ccs_acl_param *param);
+#endif
+
+#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION
+static ssize_t ccs_write_self(struct file *file, const char __user *buf,
+			      size_t count, loff_t *ppos);
+#endif
+
+/***** SECTION4: Standalone functions section *****/
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 25)
+
+/**
+ * fatal_signal_pending - Check whether SIGKILL is pending or not.
+ *
+ * @p: Pointer to "struct task_struct".
+ *
+ * Returns true if SIGKILL is pending on @p, false otherwise.
+ *
+ * This is for compatibility with older kernels.
+ */
+#define fatal_signal_pending(p) (signal_pending(p) &&			\
+				 sigismember(&p->pending.signal, SIGKILL))
+
+#endif
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
+
+/**
+ * __wait_event_interruptible_timeout - Sleep until a condition gets true or a timeout elapses.
+ *
+ * @wq:        The waitqueue to wait on.
+ * @condition: A C expression for the event to wait for.
+ * @ret:       Timeout, in jiffies.
+ *
+ * Returns 0 if the @timeout elapsed, -ERESTARTSYS if it was interrupted by a
+ * signal, and the remaining jiffies otherwise if the condition evaluated to
+ * true before the timeout elapsed.
+ *
+ * This is for compatibility with older kernels.
+ */
+#define __wait_event_interruptible_timeout(wq, condition, ret)		\
+do {									\
+	wait_queue_t __wait;						\
+	init_waitqueue_entry(&__wait, current);				\
+									\
+	add_wait_queue(&wq, &__wait);					\
+	for (;;) {							\
+		set_current_state(TASK_INTERRUPTIBLE);			\
+		if (condition)						\
+			break;						\
+		if (!signal_pending(current)) {				\
+			ret = schedule_timeout(ret);			\
+			if (!ret)					\
+				break;					\
+			continue;					\
+		}							\
+		ret = -ERESTARTSYS;					\
+		break;							\
+	}								\
+	current->state = TASK_RUNNING;					\
+	remove_wait_queue(&wq, &__wait);				\
+} while (0)
+
+/**
+ * wait_event_interruptible_timeout - Sleep until a condition gets true or a timeout elapses.
+ *
+ * @wq:        The waitqueue to wait on.
+ * @condition: A C expression for the event to wait for.
+ * @timeout:   Timeout, in jiffies.
+ *
+ * Returns 0 if the @timeout elapsed, -ERESTARTSYS if it was interrupted by a
+ * signal, and the remaining jiffies otherwise if the condition evaluated to
+ * true before the timeout elapsed.
+ *
+ * This is for compatibility with older kernels.
+ */
+#define wait_event_interruptible_timeout(wq, condition, timeout)	\
+({									\
+	long __ret = timeout;						\
+	if (!(condition))						\
+		__wait_event_interruptible_timeout(wq, condition, __ret); \
+	__ret;								\
+})
+
+#endif
+
+/**
+ * ccs_convert_time - Convert time_t to YYYY/MM/DD hh/mm/ss.
+ *
+ * @time:  Seconds since 1970/01/01 00:00:00.
+ * @stamp: Pointer to "struct ccs_time".
+ *
+ * Returns nothing.
+ *
+ * This function does not handle Y2038 problem.
+ */
+static void ccs_convert_time(time_t time, struct ccs_time *stamp)
+{
+	static const u16 ccs_eom[2][12] = {
+		{ 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
+		{ 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
+	};
+	u16 y;
+	u8 m;
+	bool r;
+	stamp->sec = time % 60;
+	time /= 60;
+	stamp->min = time % 60;
+	time /= 60;
+	stamp->hour = time % 24;
+	time /= 24;
+	for (y = 1970; ; y++) {
+		const unsigned short days = (y & 3) ? 365 : 366;
+		if (time < days)
+			break;
+		time -= days;
+	}
+	r = (y & 3) == 0;
+	for (m = 0; m < 11 && time >= ccs_eom[r][m]; m++);
+	if (m)
+		time -= ccs_eom[r][m - 1];
+	stamp->year = y;
+	stamp->month = ++m;
+	stamp->day = ++time;
+}
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 4, 23)
+#if !defined(RHEL_VERSION) || RHEL_VERSION != 3
+
+/**
+ * PDE - Get "struct proc_dir_entry".
+ *
+ * @inode: Pointer to "struct inode".
+ *
+ * Returns pointer to "struct proc_dir_entry".
+ *
+ * This is for compatibility with older kernels.
+ */
+static inline struct proc_dir_entry *PDE(const struct inode *inode)
+{
+	return (struct proc_dir_entry *) inode->u.generic_ip;
+}
+
+#endif
+#endif
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
+
+/**
+ * proc_notify_change - Update inode's attributes and reflect to the dentry.
+ *
+ * @dentry: Pointer to "struct dentry".
+ * @iattr:  Pointer to "struct iattr".
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * The 2.4 kernels don't allow chmod()/chown() for files in /proc,
+ * while the 2.6 kernels allow.
+ * To permit management of /proc/ccs/ interface by non-root user,
+ * I modified to allow chmod()/chown() of /proc/ccs/ interface like 2.6 kernels
+ * by adding "struct inode_operations"->setattr hook.
+ */
+static int proc_notify_change(struct dentry *dentry, struct iattr *iattr)
+{
+	struct inode *inode = dentry->d_inode;
+	struct proc_dir_entry *de = PDE(inode);
+	int error;
+
+	error = inode_change_ok(inode, iattr);
+	if (error)
+		goto out;
+
+	error = inode_setattr(inode, iattr);
+	if (error)
+		goto out;
+
+	de->uid = inode->i_uid;
+	de->gid = inode->i_gid;
+	de->mode = inode->i_mode;
+out:
+	return error;
+}
+
+#endif
+
+#ifdef CONFIG_CCSECURITY_NETWORK
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19) && defined(CONFIG_NET)
+#define ccs_in4_pton in4_pton
+#define ccs_in6_pton in6_pton
+#else
+/*
+ * Routines for parsing IPv4 or IPv6 address.
+ * These are copied from lib/hexdump.c net/core/utils.c .
+ */
+#include <linux/ctype.h>
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 35)
+static int hex_to_bin(char ch)
+{
+	if ((ch >= '0') && (ch <= '9'))
+		return ch - '0';
+	ch = tolower(ch);
+	if ((ch >= 'a') && (ch <= 'f'))
+		return ch - 'a' + 10;
+	return -1;
+}
+#endif
+
+#define IN6PTON_XDIGIT		0x00010000
+#define IN6PTON_DIGIT		0x00020000
+#define IN6PTON_COLON_MASK	0x00700000
+#define IN6PTON_COLON_1		0x00100000	/* single : requested */
+#define IN6PTON_COLON_2		0x00200000	/* second : requested */
+#define IN6PTON_COLON_1_2	0x00400000	/* :: requested */
+#define IN6PTON_DOT		0x00800000	/* . */
+#define IN6PTON_DELIM		0x10000000
+#define IN6PTON_NULL		0x20000000	/* first/tail */
+#define IN6PTON_UNKNOWN		0x40000000
+
+static inline int xdigit2bin(char c, int delim)
+{
+	int val;
+
+	if (c == delim || c == '\0')
+		return IN6PTON_DELIM;
+	if (c == ':')
+		return IN6PTON_COLON_MASK;
+	if (c == '.')
+		return IN6PTON_DOT;
+
+	val = hex_to_bin(c);
+	if (val >= 0)
+		return val | IN6PTON_XDIGIT | (val < 10 ? IN6PTON_DIGIT : 0);
+
+	if (delim == -1)
+		return IN6PTON_DELIM;
+	return IN6PTON_UNKNOWN;
+}
+
+static int ccs_in4_pton(const char *src, int srclen, u8 *dst, int delim,
+			const char **end)
+{
+	const char *s;
+	u8 *d;
+	u8 dbuf[4];
+	int ret = 0;
+	int i;
+	int w = 0;
+
+	if (srclen < 0)
+		srclen = strlen(src);
+	s = src;
+	d = dbuf;
+	i = 0;
+	while (1) {
+		int c;
+		c = xdigit2bin(srclen > 0 ? *s : '\0', delim);
+		if (!(c & (IN6PTON_DIGIT | IN6PTON_DOT | IN6PTON_DELIM |
+			   IN6PTON_COLON_MASK)))
+			goto out;
+		if (c & (IN6PTON_DOT | IN6PTON_DELIM | IN6PTON_COLON_MASK)) {
+			if (w == 0)
+				goto out;
+			*d++ = w & 0xff;
+			w = 0;
+			i++;
+			if (c & (IN6PTON_DELIM | IN6PTON_COLON_MASK)) {
+				if (i != 4)
+					goto out;
+				break;
+			}
+			goto cont;
+		}
+		w = (w * 10) + c;
+		if ((w & 0xffff) > 255)
+			goto out;
+cont:
+		if (i >= 4)
+			goto out;
+		s++;
+		srclen--;
+	}
+	ret = 1;
+	memcpy(dst, dbuf, sizeof(dbuf));
+out:
+	if (end)
+		*end = s;
+	return ret;
+}
+
+static int ccs_in6_pton(const char *src, int srclen, u8 *dst, int delim,
+			const char **end)
+{
+	const char *s, *tok = NULL;
+	u8 *d, *dc = NULL;
+	u8 dbuf[16];
+	int ret = 0;
+	int i;
+	int state = IN6PTON_COLON_1_2 | IN6PTON_XDIGIT | IN6PTON_NULL;
+	int w = 0;
+
+	memset(dbuf, 0, sizeof(dbuf));
+
+	s = src;
+	d = dbuf;
+	if (srclen < 0)
+		srclen = strlen(src);
+
+	while (1) {
+		int c;
+
+		c = xdigit2bin(srclen > 0 ? *s : '\0', delim);
+		if (!(c & state))
+			goto out;
+		if (c & (IN6PTON_DELIM | IN6PTON_COLON_MASK)) {
+			/* process one 16-bit word */
+			if (!(state & IN6PTON_NULL)) {
+				*d++ = (w >> 8) & 0xff;
+				*d++ = w & 0xff;
+			}
+			w = 0;
+			if (c & IN6PTON_DELIM) {
+				/* We've processed last word */
+				break;
+			}
+			/*
+			 * COLON_1 => XDIGIT
+			 * COLON_2 => XDIGIT|DELIM
+			 * COLON_1_2 => COLON_2
+			 */
+			switch (state & IN6PTON_COLON_MASK) {
+			case IN6PTON_COLON_2:
+				dc = d;
+				state = IN6PTON_XDIGIT | IN6PTON_DELIM;
+				if (dc - dbuf >= sizeof(dbuf))
+					state |= IN6PTON_NULL;
+				break;
+			case IN6PTON_COLON_1|IN6PTON_COLON_1_2:
+				state = IN6PTON_XDIGIT | IN6PTON_COLON_2;
+				break;
+			case IN6PTON_COLON_1:
+				state = IN6PTON_XDIGIT;
+				break;
+			case IN6PTON_COLON_1_2:
+				state = IN6PTON_COLON_2;
+				break;
+			default:
+				state = 0;
+			}
+			tok = s + 1;
+			goto cont;
+		}
+
+		if (c & IN6PTON_DOT) {
+			ret = ccs_in4_pton(tok ? tok : s, srclen +
+					   (int)(s - tok), d, delim, &s);
+			if (ret > 0) {
+				d += 4;
+				break;
+			}
+			goto out;
+		}
+
+		w = (w << 4) | (0xff & c);
+		state = IN6PTON_COLON_1 | IN6PTON_DELIM;
+		if (!(w & 0xf000))
+			state |= IN6PTON_XDIGIT;
+		if (!dc && d + 2 < dbuf + sizeof(dbuf)) {
+			state |= IN6PTON_COLON_1_2;
+			state &= ~IN6PTON_DELIM;
+		}
+		if (d + 2 >= dbuf + sizeof(dbuf))
+			state &= ~(IN6PTON_COLON_1|IN6PTON_COLON_1_2);
+cont:
+		if ((dc && d + 4 < dbuf + sizeof(dbuf)) ||
+		    d + 4 == dbuf + sizeof(dbuf))
+			state |= IN6PTON_DOT;
+		if (d >= dbuf + sizeof(dbuf))
+			state &= ~(IN6PTON_XDIGIT|IN6PTON_COLON_MASK);
+		s++;
+		srclen--;
+	}
+
+	i = 15; d--;
+
+	if (dc) {
+		while (d >= dc)
+			dst[i--] = *d--;
+		while (i >= dc - dbuf)
+			dst[i--] = 0;
+		while (i >= 0)
+			dst[i--] = *d--;
+	} else
+		memcpy(dst, dbuf, sizeof(dbuf));
+
+	ret = 1;
+out:
+	if (end)
+		*end = s;
+	return ret;
+}
+#endif
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 32)
+
+/*
+ * Routines for printing IPv4 or IPv6 address.
+ * These are copied from include/linux/kernel.h include/net/ipv6.h
+ * include/net/addrconf.h lib/hexdump.c lib/vsprintf.c and simplified.
+ */
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
+#if !defined(RHEL_MAJOR) || RHEL_MAJOR != 5 || !defined(RHEL_MINOR) || RHEL_MINOR < 9
+static const char hex_asc[] = "0123456789abcdef";
+#define hex_asc_lo(x)   hex_asc[((x) & 0x0f)]
+#define hex_asc_hi(x)   hex_asc[((x) & 0xf0) >> 4]
+
+static inline char *pack_hex_byte(char *buf, u8 byte)
+{
+	*buf++ = hex_asc_hi(byte);
+	*buf++ = hex_asc_lo(byte);
+	return buf;
+}
+#endif
+#endif
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
+static inline int ipv6_addr_v4mapped(const struct in6_addr *a)
+{
+	return (a->s6_addr32[0] | a->s6_addr32[1] |
+		(a->s6_addr32[2] ^ htonl(0x0000ffff))) == 0;
+}
+#endif
+
+static inline int ipv6_addr_is_isatap(const struct in6_addr *addr)
+{
+	return (addr->s6_addr32[2] | htonl(0x02000000)) == htonl(0x02005EFE);
+}
+
+static char *ip4_string(char *p, const u8 *addr)
+{
+	/*
+	 * Since this function is called outside vsnprintf(), I can use
+	 * sprintf() here.
+	 */
+	return p +
+		sprintf(p, "%u.%u.%u.%u", addr[0], addr[1], addr[2], addr[3]);
+}
+
+static char *ip6_compressed_string(char *p, const char *addr)
+{
+	int i, j, range;
+	unsigned char zerolength[8];
+	int longest = 1;
+	int colonpos = -1;
+	u16 word;
+	u8 hi, lo;
+	bool needcolon = false;
+	bool useIPv4;
+	struct in6_addr in6;
+
+	memcpy(&in6, addr, sizeof(struct in6_addr));
+
+	useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
+
+	memset(zerolength, 0, sizeof(zerolength));
+
+	if (useIPv4)
+		range = 6;
+	else
+		range = 8;
+
+	/* find position of longest 0 run */
+	for (i = 0; i < range; i++) {
+		for (j = i; j < range; j++) {
+			if (in6.s6_addr16[j] != 0)
+				break;
+			zerolength[i]++;
+		}
+	}
+	for (i = 0; i < range; i++) {
+		if (zerolength[i] > longest) {
+			longest = zerolength[i];
+			colonpos = i;
+		}
+	}
+	if (longest == 1)		/* don't compress a single 0 */
+		colonpos = -1;
+
+	/* emit address */
+	for (i = 0; i < range; i++) {
+		if (i == colonpos) {
+			if (needcolon || i == 0)
+				*p++ = ':';
+			*p++ = ':';
+			needcolon = false;
+			i += longest - 1;
+			continue;
+		}
+		if (needcolon) {
+			*p++ = ':';
+			needcolon = false;
+		}
+		/* hex u16 without leading 0s */
+		word = ntohs(in6.s6_addr16[i]);
+		hi = word >> 8;
+		lo = word & 0xff;
+		if (hi) {
+			if (hi > 0x0f)
+				p = pack_hex_byte(p, hi);
+			else
+				*p++ = hex_asc_lo(hi);
+			p = pack_hex_byte(p, lo);
+		} else if (lo > 0x0f)
+			p = pack_hex_byte(p, lo);
+		else
+			*p++ = hex_asc_lo(lo);
+		needcolon = true;
+	}
+
+	if (useIPv4) {
+		if (needcolon)
+			*p++ = ':';
+		p = ip4_string(p, &in6.s6_addr[12]);
+	}
+	*p = '\0';
+
+	return p;
+}
+#endif
+
+/**
+ * ccs_print_ipv4 - Print an IPv4 address.
+ *
+ * @buffer:     Buffer to write to.
+ * @buffer_len: Size of @buffer.
+ * @ip:         Pointer to "u32 in network byte order".
+ *
+ * Returns written length.
+ */
+static int ccs_print_ipv4(char *buffer, const unsigned int buffer_len,
+			  const u32 *ip)
+{
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32)
+	return snprintf(buffer, buffer_len, "%pI4", ip);
+#else
+	char addr[sizeof("255.255.255.255")];
+	ip4_string(addr, (const u8 *) ip);
+	return snprintf(buffer, buffer_len, "%s", addr);
+#endif
+}
+
+/**
+ * ccs_print_ipv6 - Print an IPv6 address.
+ *
+ * @buffer:     Buffer to write to.
+ * @buffer_len: Size of @buffer.
+ * @ip:         Pointer to "struct in6_addr".
+ *
+ * Returns written length.
+ */
+static int ccs_print_ipv6(char *buffer, const unsigned int buffer_len,
+			  const struct in6_addr *ip)
+{
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 32)
+	return snprintf(buffer, buffer_len, "%pI6c", ip);
+#else
+	char addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
+	ip6_compressed_string(addr, (const u8 *) ip);
+	return snprintf(buffer, buffer_len, "%s", addr);
+#endif
+}
+
+/**
+ * ccs_print_ip - Print an IP address.
+ *
+ * @buf:  Buffer to write to.
+ * @size: Size of @buf.
+ * @ptr:  Pointer to "struct ipaddr_union".
+ *
+ * Returns nothing.
+ */
+static void ccs_print_ip(char *buf, const unsigned int size,
+			 const struct ccs_ipaddr_union *ptr)
+{
+	int len;
+	if (ptr->is_ipv6)
+		len = ccs_print_ipv6(buf, size, &ptr->ip[0]);
+	else
+		len = ccs_print_ipv4(buf, size, &ptr->ip[0].s6_addr32[0]);
+	if (!memcmp(&ptr->ip[0], &ptr->ip[1], 16) || len >= size / 2)
+		return;
+	buf[len++] = '-';
+	if (ptr->is_ipv6)
+		ccs_print_ipv6(buf + len, size - len, &ptr->ip[1]);
+	else
+		ccs_print_ipv4(buf + len, size - len,
+			       &ptr->ip[1].s6_addr32[0]);
+}
+
+#endif
+
+/***** SECTION5: Variables definition section *****/
+
+/* Permit policy management by non-root user? */
+static bool ccs_manage_by_non_root;
+
+/* Lock for protecting policy. */
+DEFINE_MUTEX(ccs_policy_lock);
+
+/* Has /sbin/init started? */
+bool ccs_policy_loaded;
+
+/* List of namespaces. */
+LIST_HEAD(ccs_namespace_list);
+/* True if namespace other than ccs_kernel_namespace is defined. */
+static bool ccs_namespace_enabled;
+
+/* Initial namespace.*/
+static struct ccs_policy_namespace ccs_kernel_namespace;
+
+/* List of "struct ccs_condition". */
+LIST_HEAD(ccs_condition_list);
+
+#ifdef CONFIG_CCSECURITY_PORTRESERVE
+/* Bitmap for reserved local port numbers.*/
+static u8 ccs_reserved_port_map[8192];
+#endif
+
+/* Wait queue for kernel -> userspace notification. */
+static DECLARE_WAIT_QUEUE_HEAD(ccs_query_wait);
+/* Wait queue for userspace -> kernel notification. */
+static DECLARE_WAIT_QUEUE_HEAD(ccs_answer_wait);
+
+/* The list for "struct ccs_query". */
+static LIST_HEAD(ccs_query_list);
+
+/* Lock for manipulating ccs_query_list. */
+static DEFINE_SPINLOCK(ccs_query_list_lock);
+
+/* Number of "struct file" referring /proc/ccs/query interface. */
+static atomic_t ccs_query_observers = ATOMIC_INIT(0);
+
+/* Wait queue for /proc/ccs/audit. */
+static DECLARE_WAIT_QUEUE_HEAD(ccs_log_wait);
+
+/* The list for "struct ccs_log". */
+static LIST_HEAD(ccs_log);
+
+/* Lock for "struct list_head ccs_log". */
+static DEFINE_SPINLOCK(ccs_log_lock);
+
+/* Length of "stuct list_head ccs_log". */
+static unsigned int ccs_log_count;
+
+/* Timestamp counter for last updated. */
+static unsigned int ccs_stat_updated[CCS_MAX_POLICY_STAT];
+
+/* Counter for number of updates. */
+static unsigned int ccs_stat_modified[CCS_MAX_POLICY_STAT];
+
+/* Operations for /proc/ccs/self_domain interface. */
+static
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
+const
+#endif
+struct file_operations ccs_self_operations = {
+#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION
+	.write = ccs_write_self,
+#endif
+	.read  = ccs_read_self,
+};
+
+/* Operations for /proc/ccs/ interface. */
+static
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 17)
+const
+#endif
+struct file_operations ccs_operations = {
+	.open    = ccs_open,
+	.release = ccs_release,
+	.poll    = ccs_poll,
+	.read    = ccs_read,
+	.write   = ccs_write,
+};
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
+
+/* The inode operations for /proc/ccs/ directory. */
+static struct inode_operations ccs_dir_inode_operations;
+
+/* The inode operations for files under /proc/ccs/ directory. */
+static struct inode_operations ccs_file_inode_operations;
+
+#endif
+
+/***** SECTION6: Dependent functions section *****/
+
+/**
+ * list_for_each_cookie - iterate over a list with cookie.
+ *
+ * @pos:  Pointer to "struct list_head".
+ * @head: Pointer to "struct list_head".
+ */
+#define list_for_each_cookie(pos, head)					\
+	for (pos = pos ? pos : srcu_dereference((head)->next, &ccs_ss); \
+	     pos != (head); pos = srcu_dereference(pos->next, &ccs_ss))
+
+/**
+ * ccs_read_token - Read a word from a line.
+ *
+ * @param: Pointer to "struct ccs_acl_param".
+ *
+ * Returns a word on success, "" otherwise.
+ *
+ * To allow the caller to skip NULL check, this function returns "" rather than
+ * NULL if there is no more words to read.
+ */
+static char *ccs_read_token(struct ccs_acl_param *param)
+{
+	char *pos = param->data;
+	char *del = strchr(pos, ' ');
+	if (del)
+		*del++ = '\0';
+	else
+		del = pos + strlen(pos);
+	param->data = del;
+	return pos;
+}
+
+/**
+ * ccs_make_byte - Make byte value from three octal characters.
+ *
+ * @c1: The first character.
+ * @c2: The second character.
+ * @c3: The third character.
+ *
+ * Returns byte value.
+ */
+static u8 ccs_make_byte(const u8 c1, const u8 c2, const u8 c3)
+{
+	return ((c1 - '0') << 6) + ((c2 - '0') << 3) + (c3 - '0');
+}
+
+/**
+ * ccs_correct_word2 - Check whether the given string follows the naming rules.
+ *
+ * @string: The byte sequence to check. Not '\0'-terminated.
+ * @len:    Length of @string.
+ *
+ * Returns true if @string follows the naming rules, false otherwise.
+ */
+static bool ccs_correct_word2(const char *string, size_t len)
+{
+	const char *const start = string;
+	bool in_repetition = false;
+	unsigned char c;
+	unsigned char d;
+	unsigned char e;
+	if (!len)
+		goto out;
+	while (len--) {
+		c = *string++;
+		if (c == '\\') {
+			if (!len--)
+				goto out;
+			c = *string++;
+			switch (c) {
+			case '\\':  /* "\\" */
+				continue;
+			case '$':   /* "\$" */
+			case '+':   /* "\+" */
+			case '?':   /* "\?" */
+			case '*':   /* "\*" */
+			case '@':   /* "\@" */
+			case 'x':   /* "\x" */
+			case 'X':   /* "\X" */
+			case 'a':   /* "\a" */
+			case 'A':   /* "\A" */
+			case '-':   /* "\-" */
+				continue;
+			case '{':   /* "/\{" */
+				if (string - 3 < start || *(string - 3) != '/')
+					break;
+				in_repetition = true;
+				continue;
+			case '}':   /* "\}/" */
+				if (*string != '/')
+					break;
+				if (!in_repetition)
+					break;
+				in_repetition = false;
+				continue;
+			case '0':   /* "\ooo" */
+			case '1':
+			case '2':
+			case '3':
+				if (!len-- || !len--)
+					break;
+				d = *string++;
+				e = *string++;
+				if (d < '0' || d > '7' || e < '0' || e > '7')
+					break;
+				c = ccs_make_byte(c, d, e);
+				if (c <= ' ' || c >= 127)
+					continue;
+			}
+			goto out;
+		} else if (in_repetition && c == '/') {
+			goto out;
+		} else if (c <= ' ' || c >= 127) {
+			goto out;
+		}
+	}
+	if (in_repetition)
+		goto out;
+	return true;
+out:
+	return false;
+}
+
+/**
+ * ccs_correct_word - Check whether the given string follows the naming rules.
+ *
+ * @string: The string to check.
+ *
+ * Returns true if @string follows the naming rules, false otherwise.
+ */
+static bool ccs_correct_word(const char *string)
+{
+	return ccs_correct_word2(string, strlen(string));
+}
+
+/**
+ * ccs_get_group - Allocate memory for "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group".
+ *
+ * @param: Pointer to "struct ccs_acl_param".
+ * @idx:   Index number.
+ *
+ * Returns pointer to "struct ccs_group" on success, NULL otherwise.
+ */
+static struct ccs_group *ccs_get_group(struct ccs_acl_param *param,
+				       const u8 idx)
+{
+	struct ccs_group e = { };
+	struct ccs_group *group = NULL;
+	struct list_head *list;
+	const char *group_name = ccs_read_token(param);
+	bool found = false;
+	if (!ccs_correct_word(group_name) || idx >= CCS_MAX_GROUP)
+		return NULL;
+	e.group_name = ccs_get_name(group_name);
+	if (!e.group_name)
+		return NULL;
+	if (mutex_lock_interruptible(&ccs_policy_lock))
+		goto out;
+	list = &param->ns->group_list[idx];
+	list_for_each_entry(group, list, head.list) {
+		if (e.group_name != group->group_name ||
+		    atomic_read(&group->head.users) == CCS_GC_IN_PROGRESS)
+			continue;
+		atomic_inc(&group->head.users);
+		found = true;
+		break;
+	}
+	if (!found) {
+		struct ccs_group *entry = ccs_commit_ok(&e, sizeof(e));
+		if (entry) {
+			INIT_LIST_HEAD(&entry->member_list);
+			atomic_set(&entry->head.users, 1);
+			list_add_tail_rcu(&entry->head.list, list);
+			group = entry;
+			found = true;
+		}
+	}
+	mutex_unlock(&ccs_policy_lock);
+out:
+	ccs_put_name(e.group_name);
+	return found ? group : NULL;
+}
+
+/**
+ * ccs_parse_name_union - Parse a ccs_name_union.
+ *
+ * @param: Pointer to "struct ccs_acl_param".
+ * @ptr:   Pointer to "struct ccs_name_union".
+ *
+ * Returns true on success, false otherwise.
+ */
+static bool ccs_parse_name_union(struct ccs_acl_param *param,
+				 struct ccs_name_union *ptr)
+{
+	char *filename;
+	if (param->data[0] == '@') {
+		param->data++;
+		ptr->group = ccs_get_group(param, CCS_PATH_GROUP);
+		return ptr->group != NULL;
+	}
+	filename = ccs_read_token(param);
+	if (!ccs_correct_word(filename))
+		return false;
+	ptr->filename = ccs_get_name(filename);
+	return ptr->filename != NULL;
+}
+
+/**
+ * ccs_parse_ulong - Parse an "unsigned long" value.
+ *
+ * @result: Pointer to "unsigned long".
+ * @str:    Pointer to string to parse.
+ *
+ * Returns one of values in "enum ccs_value_type".
+ *
+ * The @src is updated to point the first character after the value
+ * on success.
+ */
+static u8 ccs_parse_ulong(unsigned long *result, char **str)
+{
+	const char *cp = *str;
+	char *ep;
+	int base = 10;
+	if (*cp == '0') {
+		char c = *(cp + 1);
+		if (c == 'x' || c == 'X') {
+			base = 16;
+			cp += 2;
+		} else if (c >= '0' && c <= '7') {
+			base = 8;
+			cp++;
+		}
+	}
+	*result = simple_strtoul(cp, &ep, base);
+	if (cp == ep)
+		return CCS_VALUE_TYPE_INVALID;
+	*str = ep;
+	switch (base) {
+	case 16:
+		return CCS_VALUE_TYPE_HEXADECIMAL;
+	case 8:
+		return CCS_VALUE_TYPE_OCTAL;
+	default:
+		return CCS_VALUE_TYPE_DECIMAL;
+	}
+}
+
+/**
+ * ccs_parse_number_union - Parse a ccs_number_union.
+ *
+ * @param: Pointer to "struct ccs_acl_param".
+ * @ptr:   Pointer to "struct ccs_number_union".
+ *
+ * Returns true on success, false otherwise.
+ */
+static bool ccs_parse_number_union(struct ccs_acl_param *param,
+				   struct ccs_number_union *ptr)
+{
+	char *data;
+	u8 type;
+	unsigned long v;
+	memset(ptr, 0, sizeof(*ptr));
+	if (param->data[0] == '@') {
+		param->data++;
+		ptr->group = ccs_get_group(param, CCS_NUMBER_GROUP);
+		return ptr->group != NULL;
+	}
+	data = ccs_read_token(param);
+	type = ccs_parse_ulong(&v, &data);
+	if (type == CCS_VALUE_TYPE_INVALID)
+		return false;
+	ptr->values[0] = v;
+	ptr->value_type[0] = type;
+	if (!*data) {
+		ptr->values[1] = v;
+		ptr->value_type[1] = type;
+		return true;
+	}
+	if (*data++ != '-')
+		return false;
+	type = ccs_parse_ulong(&v, &data);
+	if (type == CCS_VALUE_TYPE_INVALID || *data || ptr->values[0] > v)
+		return false;
+	ptr->values[1] = v;
+	ptr->value_type[1] = type;
+	return true;
+}
+
+#ifdef CONFIG_CCSECURITY_NETWORK
+
+/**
+ * ccs_parse_ipaddr_union - Parse an IP address.
+ *
+ * @param: Pointer to "struct ccs_acl_param".
+ * @ptr:   Pointer to "struct ccs_ipaddr_union".
+ *
+ * Returns true on success, false otherwise.
+ */
+static bool ccs_parse_ipaddr_union(struct ccs_acl_param *param,
+				   struct ccs_ipaddr_union *ptr)
+{
+	u8 * const min = ptr->ip[0].in6_u.u6_addr8;
+	u8 * const max = ptr->ip[1].in6_u.u6_addr8;
+	char *address = ccs_read_token(param);
+	const char *end;
+	if (!strchr(address, ':') &&
+	    ccs_in4_pton(address, -1, min, '-', &end) > 0) {
+		ptr->is_ipv6 = false;
+		if (!*end)
+			ptr->ip[1].s6_addr32[0] = ptr->ip[0].s6_addr32[0];
+		else if (*end++ != '-' ||
+			 ccs_in4_pton(end, -1, max, '\0', &end) <= 0 || *end)
+			return false;
+		return true;
+	}
+	if (ccs_in6_pton(address, -1, min, '-', &end) > 0) {
+		ptr->is_ipv6 = true;
+		if (!*end)
+			memmove(max, min, sizeof(u16) * 8);
+		else if (*end++ != '-' ||
+			 ccs_in6_pton(end, -1, max, '\0', &end) <= 0 || *end)
+			return false;
+		return true;
+	}
+	return false;
+}
+
+#endif
+
+/**
+ * ccs_get_dqword - ccs_get_name() for a quoted string.
+ *
+ * @start: String to save.
+ *
+ * Returns pointer to "struct ccs_path_info" on success, NULL otherwise.
+ */
+static const struct ccs_path_info *ccs_get_dqword(char *start)
+{
+	char *cp = start + strlen(start) - 1;
+	if (cp == start || *start++ != '"' || *cp != '"')
+		return NULL;
+	*cp = '\0';
+	if (*start && !ccs_correct_word(start))
+		return NULL;
+	return ccs_get_name(start);
+}
+
+/**
+ * ccs_parse_name_union_quoted - Parse a quoted word.
+ *
+ * @param: Pointer to "struct ccs_acl_param".
+ * @ptr:   Pointer to "struct ccs_name_union".
+ *
+ * Returns true on success, false otherwise.
+ */
+static bool ccs_parse_name_union_quoted(struct ccs_acl_param *param,
+					struct ccs_name_union *ptr)
+{
+	char *filename = param->data;
+	if (*filename == '@')
+		return ccs_parse_name_union(param, ptr);
+	ptr->filename = ccs_get_dqword(filename);
+	return ptr->filename != NULL;
+}
+
+/**
+ * ccs_parse_argv - Parse an argv[] condition part.
+ *
+ * @left:  Lefthand value.
+ * @right: Righthand value.
+ * @argv:  Pointer to "struct ccs_argv".
+ *
+ * Returns true on success, false otherwise.
+ */
+static bool ccs_parse_argv(char *left, char *right, struct ccs_argv *argv)
+{
+	if (ccs_parse_ulong(&argv->index, &left) != CCS_VALUE_TYPE_DECIMAL ||
+	    *left++ != ']' || *left)
+		return false;
+	argv->value = ccs_get_dqword(right);
+	return argv->value != NULL;
+}
+
+/**
+ * ccs_parse_envp - Parse an envp[] condition part.
+ *
+ * @left:  Lefthand value.
+ * @right: Righthand value.
+ * @envp:  Pointer to "struct ccs_envp".
+ *
+ * Returns true on success, false otherwise.
+ */
+static bool ccs_parse_envp(char *left, char *right, struct ccs_envp *envp)
+{
+	const struct ccs_path_info *name;
+	const struct ccs_path_info *value;
+	char *cp = left + strlen(left) - 1;
+	if (*cp-- != ']' || *cp != '"')
+		goto out;
+	*cp = '\0';
+	if (!ccs_correct_word(left))
+		goto out;
+	name = ccs_get_name(left);
+	if (!name)
+		goto out;
+	if (!strcmp(right, "NULL")) {
+		value = NULL;
+	} else {
+		value = ccs_get_dqword(right);
+		if (!value) {
+			ccs_put_name(name);
+			goto out;
+		}
+	}
+	envp->name = name;
+	envp->value = value;
+	return true;
+out:
+	return false;
+}
+
+/**
+ * ccs_same_condition - Check for duplicated "struct ccs_condition" entry.
+ *
+ * @a: Pointer to "struct ccs_condition".
+ * @b: Pointer to "struct ccs_condition".
+ *
+ * Returns true if @a == @b, false otherwise.
+ */
+static bool ccs_same_condition(const struct ccs_condition *a,
+			       const struct ccs_condition *b)
+{
+	return a->size == b->size && a->condc == b->condc &&
+		a->numbers_count == b->numbers_count &&
+		a->names_count == b->names_count &&
+		a->argc == b->argc && a->envc == b->envc &&
+		a->grant_log == b->grant_log &&
+		a->exec_transit == b->exec_transit && a->transit == b->transit
+		&& !memcmp(a + 1, b + 1, a->size - sizeof(*a));
+}
+
+/**
+ * ccs_condition_type - Get condition type.
+ *
+ * @word: Keyword string.
+ *
+ * Returns one of values in "enum ccs_conditions_index" on success,
+ * CCS_MAX_CONDITION_KEYWORD otherwise.
+ */
+static u8 ccs_condition_type(const char *word)
+{
+	u8 i;
+	for (i = 0; i < CCS_MAX_CONDITION_KEYWORD; i++) {
+		if (!strcmp(word, ccs_condition_keyword[i]))
+			break;
+	}
+	return i;
+}
+
+/**
+ * ccs_commit_condition - Commit "struct ccs_condition".
+ *
+ * @entry: Pointer to "struct ccs_condition".
+ *
+ * Returns pointer to "struct ccs_condition" on success, NULL otherwise.
+ *
+ * This function merges duplicated entries. This function returns NULL if
+ * @entry is not duplicated but memory quota for policy has exceeded.
+ */
+static struct ccs_condition *ccs_commit_condition(struct ccs_condition *entry)
+{
+	struct ccs_condition *ptr;
+	bool found = false;
+	if (mutex_lock_interruptible(&ccs_policy_lock)) {
+		dprintk(KERN_WARNING "%u: %s failed\n", __LINE__, __func__);
+		ptr = NULL;
+		found = true;
+		goto out;
+	}
+	list_for_each_entry(ptr, &ccs_condition_list, head.list) {
+		if (!ccs_same_condition(ptr, entry) ||
+		    atomic_read(&ptr->head.users) == CCS_GC_IN_PROGRESS)
+			continue;
+		/* Same entry found. Share this entry. */
+		atomic_inc(&ptr->head.users);
+		found = true;
+		break;
+	}
+	if (!found) {
+		if (ccs_memory_ok(entry, entry->size)) {
+			atomic_set(&entry->head.users, 1);
+			list_add(&entry->head.list, &ccs_condition_list);
+		} else {
+			found = true;
+			ptr = NULL;
+		}
+	}
+	mutex_unlock(&ccs_policy_lock);
+out:
+	if (found) {
+		ccs_del_condition(&entry->head.list);
+		kfree(entry);
+		entry = ptr;
+	}
+	return entry;
+}
+
+/**
+ * ccs_correct_path - Check whether the given pathname follows the naming rules.
+ *
+ * @filename: The pathname to check.
+ *
+ * Returns true if @filename follows the naming rules, false otherwise.
+ */
+static bool ccs_correct_path(const char *filename)
+{
+	return *filename == '/' && ccs_correct_word(filename);
+}
+
+/**
+ * ccs_domain_def - Check whether the given token can be a domainname.
+ *
+ * @buffer: The token to check.
+ *
+ * Returns true if @buffer possibly be a domainname, false otherwise.
+ */
+static bool ccs_domain_def(const unsigned char *buffer)
+{
+	const unsigned char *cp;
+	int len;
+	if (*buffer != '<')
+		return false;
+	cp = strchr(buffer, ' ');
+	if (!cp)
+		len = strlen(buffer);
+	else
+		len = cp - buffer;
+	if (buffer[len - 1] != '>' || !ccs_correct_word2(buffer + 1, len - 2))
+		return false;
+	return true;
+}
+
+/**
+ * ccs_correct_domain - Check whether the given domainname follows the naming rules.
+ *
+ * @domainname: The domainname to check.
+ *
+ * Returns true if @domainname follows the naming rules, false otherwise.
+ */
+static bool ccs_correct_domain(const unsigned char *domainname)
+{
+	if (!domainname || !ccs_domain_def(domainname))
+		return false;
+	domainname = strchr(domainname, ' ');
+	if (!domainname++)
+		return true;
+	while (1) {
+		const unsigned char *cp = strchr(domainname, ' ');
+		if (!cp)
+			break;
+		if (*domainname != '/' ||
+		    !ccs_correct_word2(domainname, cp - domainname))
+			return false;
+		domainname = cp + 1;
+	}
+	return ccs_correct_path(domainname);
+}
+
+/**
+ * ccs_normalize_line - Format string.
+ *
+ * @buffer: The line to normalize.
+ *
+ * Returns nothing.
+ *
+ * Leading and trailing whitespaces are removed.
+ * Multiple whitespaces are packed into single space.
+ */
+static void ccs_normalize_line(unsigned char *buffer)
+{
+	unsigned char *sp = buffer;
+	unsigned char *dp = buffer;
+	bool first = true;
+	while (*sp && (*sp <= ' ' || *sp >= 127))
+		sp++;
+	while (*sp) {
+		if (!first)
+			*dp++ = ' ';
+		first = false;
+		while (*sp > ' ' && *sp < 127)
+			*dp++ = *sp++;
+		while (*sp && (*sp <= ' ' || *sp >= 127))
+			sp++;
+	}
+	*dp = '\0';
+}
+
+/**
+ * ccs_get_domainname - Read a domainname from a line.
+ *
+ * @param: Pointer to "struct ccs_acl_param".
+ *
+ * Returns a domainname on success, NULL otherwise.
+ */
+static const struct ccs_path_info *ccs_get_domainname
+(struct ccs_acl_param *param)
+{
+	char *start = param->data;
+	char *pos = start;
+	while (*pos) {
+		if (*pos++ != ' ' || *pos++ == '/')
+			continue;
+		pos -= 2;
+		*pos++ = '\0';
+		break;
+	}
+	param->data = pos;
+	if (ccs_correct_domain(start))
+		return ccs_get_name(start);
+	return NULL;
+}
+
+/**
+ * ccs_get_transit_preference - Parse domain transition preference for execve().
+ *
+ * @param: Pointer to "struct ccs_acl_param".
+ * @e:     Pointer to "struct ccs_condition".
+ *
+ * Returns the condition string part.
+ */
+static char *ccs_get_transit_preference(struct ccs_acl_param *param,
+					struct ccs_condition *e)
+{
+	char * const pos = param->data;
+	bool flag;
+	if (*pos == '<') {
+		e->transit = ccs_get_domainname(param);
+		goto done;
+	}
+	{
+		char *cp = strchr(pos, ' ');
+		if (cp)
+			*cp = '\0';
+		flag = ccs_correct_path(pos) || !strcmp(pos, "keep") ||
+			!strcmp(pos, "initialize") || !strcmp(pos, "reset") ||
+			!strcmp(pos, "child") || !strcmp(pos, "parent");
+		if (cp)
+			*cp = ' ';
+	}
+	if (!flag)
+		return pos;
+	e->transit = ccs_get_name(ccs_read_token(param));
+done:
+	if (e->transit) {
+		e->exec_transit = true;
+		return param->data;
+	}
+	/*
+	 * Return a bad read-only condition string that will let
+	 * ccs_get_condition() return NULL.
+	 */
+	return "/";
+}
+
+/**
+ * ccs_get_condition - Parse condition part.
+ *
+ * @param: Pointer to "struct ccs_acl_param".
+ *
+ * Returns pointer to "struct ccs_condition" on success, NULL otherwise.
+ */
+struct ccs_condition *ccs_get_condition(struct ccs_acl_param *param)
+{
+	struct ccs_condition *entry = NULL;
+	struct ccs_condition_element *condp = NULL;
+	struct ccs_number_union *numbers_p = NULL;
+	struct ccs_name_union *names_p = NULL;
+	struct ccs_argv *argv = NULL;
+	struct ccs_envp *envp = NULL;
+	struct ccs_condition e = { };
+	char * const start_of_string = ccs_get_transit_preference(param, &e);
+	char * const end_of_string = start_of_string + strlen(start_of_string);
+	char *pos;
+rerun:
+	pos = start_of_string;
+	while (1) {
+		u8 left = -1;
+		u8 right = -1;
+		char *left_word = pos;
+		char *cp;
+		char *right_word;
+		bool is_not;
+		if (!*left_word)
+			break;
+		/*
+		 * Since left-hand condition does not allow use of "path_group"
+		 * or "number_group" and environment variable's names do not
+		 * accept '=', it is guaranteed that the original line consists
+		 * of one or more repetition of $left$operator$right blocks
+		 * where "$left is free from '=' and ' '" and "$operator is
+		 * either '=' or '!='" and "$right is free from ' '".
+		 * Therefore, we can reconstruct the original line at the end
+		 * of dry run even if we overwrite $operator with '\0'.
+		 */
+		cp = strchr(pos, ' ');
+		if (cp) {
+			*cp = '\0'; /* Will restore later. */
+			pos = cp + 1;
+		} else {
+			pos = "";
+		}
+		right_word = strchr(left_word, '=');
+		if (!right_word || right_word == left_word)
+			goto out;
+		is_not = *(right_word - 1) == '!';
+		if (is_not)
+			*(right_word++ - 1) = '\0'; /* Will restore later. */
+		else if (*(right_word + 1) != '=')
+			*right_word++ = '\0'; /* Will restore later. */
+		else
+			goto out;
+		dprintk(KERN_WARNING "%u: <%s>%s=<%s>\n", __LINE__, left_word,
+			is_not ? "!" : "", right_word);
+		if (!strcmp(left_word, "grant_log")) {
+			if (entry) {
+				if (is_not ||
+				    entry->grant_log != CCS_GRANTLOG_AUTO)
+					goto out;
+				else if (!strcmp(right_word, "yes"))
+					entry->grant_log = CCS_GRANTLOG_YES;
+				else if (!strcmp(right_word, "no"))
+					entry->grant_log = CCS_GRANTLOG_NO;
+				else
+					goto out;
+			}
+			continue;
+		}
+		if (!strcmp(left_word, "auto_domain_transition")) {
+			if (entry) {
+				if (is_not || entry->transit)
+					goto out;
+				entry->transit = ccs_get_dqword(right_word);
+				if (!entry->transit ||
+				    (entry->transit->name[0] != '/' &&
+				     !ccs_domain_def(entry->transit->name)))
+					goto out;
+			}
+			continue;
+		}
+		if (!strncmp(left_word, "exec.argv[", 10)) {
+			if (!argv) {
+				e.argc++;
+				e.condc++;
+			} else {
+				e.argc--;
+				e.condc--;
+				left = CCS_ARGV_ENTRY;
+				argv->is_not = is_not;
+				if (!ccs_parse_argv(left_word + 10,
+						    right_word, argv++))
+					goto out;
+			}
+			goto store_value;
+		}
+		if (!strncmp(left_word, "exec.envp[\"", 11)) {
+			if (!envp) {
+				e.envc++;
+				e.condc++;
+			} else {
+				e.envc--;
+				e.condc--;
+				left = CCS_ENVP_ENTRY;
+				envp->is_not = is_not;
+				if (!ccs_parse_envp(left_word + 11,
+						    right_word, envp++))
+					goto out;
+			}
+			goto store_value;
+		}
+		left = ccs_condition_type(left_word);
+		dprintk(KERN_WARNING "%u: <%s> left=%u\n", __LINE__, left_word,
+			left);
+		if (left == CCS_MAX_CONDITION_KEYWORD) {
+			if (!numbers_p) {
+				e.numbers_count++;
+			} else {
+				e.numbers_count--;
+				left = CCS_NUMBER_UNION;
+				param->data = left_word;
+				if (*left_word == '@' ||
+				    !ccs_parse_number_union(param,
+							    numbers_p++))
+					goto out;
+			}
+		}
+		if (!condp)
+			e.condc++;
+		else
+			e.condc--;
+		if (left == CCS_EXEC_REALPATH || left == CCS_SYMLINK_TARGET) {
+			if (!names_p) {
+				e.names_count++;
+			} else {
+				e.names_count--;
+				right = CCS_NAME_UNION;
+				param->data = right_word;
+				if (!ccs_parse_name_union_quoted(param,
+								 names_p++))
+					goto out;
+			}
+			goto store_value;
+		}
+		right = ccs_condition_type(right_word);
+		if (right == CCS_MAX_CONDITION_KEYWORD) {
+			if (!numbers_p) {
+				e.numbers_count++;
+			} else {
+				e.numbers_count--;
+				right = CCS_NUMBER_UNION;
+				param->data = right_word;
+				if (!ccs_parse_number_union(param,
+							    numbers_p++))
+					goto out;
+			}
+		}
+store_value:
+		if (!condp) {
+			dprintk(KERN_WARNING "%u: dry_run left=%u right=%u "
+				"match=%u\n", __LINE__, left, right, !is_not);
+			continue;
+		}
+		condp->left = left;
+		condp->right = right;
+		condp->equals = !is_not;
+		dprintk(KERN_WARNING "%u: left=%u right=%u match=%u\n",
+			__LINE__, condp->left, condp->right,
+			condp->equals);
+		condp++;
+	}
+	dprintk(KERN_INFO "%u: cond=%u numbers=%u names=%u ac=%u ec=%u\n",
+		__LINE__, e.condc, e.numbers_count, e.names_count, e.argc,
+		e.envc);
+	if (entry) {
+		BUG_ON(e.names_count | e.numbers_count | e.argc | e.envc |
+		       e.condc);
+		return ccs_commit_condition(entry);
+	}
+	e.size = sizeof(*entry)
+		+ e.condc * sizeof(struct ccs_condition_element)
+		+ e.numbers_count * sizeof(struct ccs_number_union)
+		+ e.names_count * sizeof(struct ccs_name_union)
+		+ e.argc * sizeof(struct ccs_argv)
+		+ e.envc * sizeof(struct ccs_envp);
+	entry = kzalloc(e.size, CCS_GFP_FLAGS);
+	if (!entry)
+		goto out2;
+	*entry = e;
+	e.transit = NULL;
+	condp = (struct ccs_condition_element *) (entry + 1);
+	numbers_p = (struct ccs_number_union *) (condp + e.condc);
+	names_p = (struct ccs_name_union *) (numbers_p + e.numbers_count);
+	argv = (struct ccs_argv *) (names_p + e.names_count);
+	envp = (struct ccs_envp *) (argv + e.argc);
+	{
+		bool flag = false;
+		for (pos = start_of_string; pos < end_of_string; pos++) {
+			if (*pos)
+				continue;
+			if (flag) /* Restore " ". */
+				*pos = ' ';
+			else if (*(pos + 1) == '=') /* Restore "!=". */
+				*pos = '!';
+			else /* Restore "=". */
+				*pos = '=';
+			flag = !flag;
+		}
+	}
+	goto rerun;
+out:
+	dprintk(KERN_WARNING "%u: %s failed\n", __LINE__, __func__);
+	if (entry) {
+		ccs_del_condition(&entry->head.list);
+		kfree(entry);
+	}
+out2:
+	ccs_put_name(e.transit);
+	return NULL;
+}
+
+/**
+ * ccs_yesno - Return "yes" or "no".
+ *
+ * @value: Bool value.
+ *
+ * Returns "yes" if @value is not 0, "no" otherwise.
+ */
+static const char *ccs_yesno(const unsigned int value)
+{
+	return value ? "yes" : "no";
+}
+
+/**
+ * ccs_addprintf - strncat()-like-snprintf().
+ *
+ * @buffer: Buffer to write to. Must be '\0'-terminated.
+ * @len:    Size of @buffer.
+ * @fmt:    The printf()'s format string, followed by parameters.
+ *
+ * Returns nothing.
+ */
+static void ccs_addprintf(char *buffer, int len, const char *fmt, ...)
+{
+	va_list args;
+	const int pos = strlen(buffer);
+	va_start(args, fmt);
+	vsnprintf(buffer + pos, len - pos - 1, fmt, args);
+	va_end(args);
+}
+
+/**
+ * ccs_flush - Flush queued string to userspace's buffer.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ *
+ * Returns true if all data was flushed, false otherwise.
+ */
+static bool ccs_flush(struct ccs_io_buffer *head)
+{
+	while (head->r.w_pos) {
+		const char *w = head->r.w[0];
+		size_t len = strlen(w);
+		if (len) {
+			if (len > head->read_user_buf_avail)
+				len = head->read_user_buf_avail;
+			if (!len)
+				return false;
+			if (copy_to_user(head->read_user_buf, w, len))
+				return false;
+			head->read_user_buf_avail -= len;
+			head->read_user_buf += len;
+			w += len;
+		}
+		head->r.w[0] = w;
+		if (*w)
+			return false;
+		/* Add '\0' for audit logs and query. */
+		if (head->type == CCS_AUDIT || head->type == CCS_QUERY) {
+			if (!head->read_user_buf_avail ||
+			    copy_to_user(head->read_user_buf, "", 1))
+				return false;
+			head->read_user_buf_avail--;
+			head->read_user_buf++;
+		}
+		head->r.w_pos--;
+		for (len = 0; len < head->r.w_pos; len++)
+			head->r.w[len] = head->r.w[len + 1];
+	}
+	head->r.avail = 0;
+	return true;
+}
+
+/**
+ * ccs_set_string - Queue string to "struct ccs_io_buffer" structure.
+ *
+ * @head:   Pointer to "struct ccs_io_buffer".
+ * @string: String to print.
+ *
+ * Returns nothing.
+ *
+ * Note that @string has to be kept valid until @head is kfree()d.
+ * This means that char[] allocated on stack memory cannot be passed to
+ * this function. Use ccs_io_printf() for char[] allocated on stack memory.
+ */
+static void ccs_set_string(struct ccs_io_buffer *head, const char *string)
+{
+	if (head->r.w_pos < CCS_MAX_IO_READ_QUEUE) {
+		head->r.w[head->r.w_pos++] = string;
+		ccs_flush(head);
+	} else
+		printk(KERN_WARNING "Too many words in a line.\n");
+}
+
+/**
+ * ccs_io_printf - printf() to "struct ccs_io_buffer" structure.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ * @fmt:  The printf()'s format string, followed by parameters.
+ *
+ * Returns nothing.
+ */
+static void ccs_io_printf(struct ccs_io_buffer *head, const char *fmt, ...)
+{
+	va_list args;
+	size_t len;
+	size_t pos = head->r.avail;
+	int size = head->readbuf_size - pos;
+	if (size <= 0)
+		return;
+	va_start(args, fmt);
+	len = vsnprintf(head->read_buf + pos, size, fmt, args) + 1;
+	va_end(args);
+	if (pos + len >= head->readbuf_size) {
+		printk(KERN_WARNING "Too many words in a line.\n");
+		return;
+	}
+	head->r.avail += len;
+	ccs_set_string(head, head->read_buf + pos);
+}
+
+/**
+ * ccs_set_space - Put a space to "struct ccs_io_buffer" structure.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ *
+ * Returns nothing.
+ */
+static void ccs_set_space(struct ccs_io_buffer *head)
+{
+	ccs_set_string(head, " ");
+}
+
+/**
+ * ccs_set_lf - Put a line feed to "struct ccs_io_buffer" structure.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ *
+ * Returns true if all data was flushed, false otherwise.
+ */
+static bool ccs_set_lf(struct ccs_io_buffer *head)
+{
+	ccs_set_string(head, "\n");
+	return !head->r.w_pos;
+}
+
+/**
+ * ccs_set_slash - Put a shash to "struct ccs_io_buffer" structure.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ *
+ * Returns nothing.
+ */
+static void ccs_set_slash(struct ccs_io_buffer *head)
+{
+	ccs_set_string(head, "/");
+}
+
+/**
+ * ccs_init_policy_namespace - Initialize namespace.
+ *
+ * @ns: Pointer to "struct ccs_policy_namespace".
+ *
+ * Returns nothing.
+ */
+static void ccs_init_policy_namespace(struct ccs_policy_namespace *ns)
+{
+	unsigned int idx;
+	for (idx = 0; idx < CCS_MAX_ACL_GROUPS; idx++)
+		INIT_LIST_HEAD(&ns->acl_group[idx]);
+	for (idx = 0; idx < CCS_MAX_GROUP; idx++)
+		INIT_LIST_HEAD(&ns->group_list[idx]);
+	for (idx = 0; idx < CCS_MAX_POLICY; idx++)
+		INIT_LIST_HEAD(&ns->policy_list[idx]);
+	ns->profile_version = 20150505;
+	ccs_namespace_enabled = !list_empty(&ccs_namespace_list);
+	list_add_tail_rcu(&ns->namespace_list, &ccs_namespace_list);
+}
+
+/**
+ * ccs_print_namespace - Print namespace header.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ *
+ * Returns nothing.
+ */
+static void ccs_print_namespace(struct ccs_io_buffer *head)
+{
+	if (!ccs_namespace_enabled)
+		return;
+	ccs_set_string(head,
+		       container_of(head->r.ns, struct ccs_policy_namespace,
+				    namespace_list)->name);
+	ccs_set_space(head);
+}
+
+/**
+ * ccs_assign_profile - Create a new profile.
+ *
+ * @ns:      Pointer to "struct ccs_policy_namespace".
+ * @profile: Profile number to create.
+ *
+ * Returns pointer to "struct ccs_profile" on success, NULL otherwise.
+ */
+static struct ccs_profile *ccs_assign_profile(struct ccs_policy_namespace *ns,
+					      const unsigned int profile)
+{
+	struct ccs_profile *ptr;
+	struct ccs_profile *entry;
+	if (profile >= CCS_MAX_PROFILES)
+		return NULL;
+	ptr = ns->profile_ptr[profile];
+	if (ptr)
+		return ptr;
+	entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
+	if (mutex_lock_interruptible(&ccs_policy_lock))
+		goto out;
+	ptr = ns->profile_ptr[profile];
+	if (!ptr && ccs_memory_ok(entry, sizeof(*entry))) {
+		ptr = entry;
+		ptr->default_config = CCS_CONFIG_DISABLED |
+			CCS_CONFIG_WANT_GRANT_LOG | CCS_CONFIG_WANT_REJECT_LOG;
+		memset(ptr->config, CCS_CONFIG_USE_DEFAULT,
+		       sizeof(ptr->config));
+		ptr->pref[CCS_PREF_MAX_AUDIT_LOG] =
+			CONFIG_CCSECURITY_MAX_AUDIT_LOG;
+		ptr->pref[CCS_PREF_MAX_LEARNING_ENTRY] =
+			CONFIG_CCSECURITY_MAX_ACCEPT_ENTRY;
+		mb(); /* Avoid out-of-order execution. */
+		ns->profile_ptr[profile] = ptr;
+		entry = NULL;
+	}
+	mutex_unlock(&ccs_policy_lock);
+out:
+	kfree(entry);
+	return ptr;
+}
+
+/**
+ * ccs_check_profile - Check all profiles currently assigned to domains are defined.
+ *
+ * Returns nothing.
+ */
+static void ccs_check_profile(void)
+{
+	struct ccs_domain_info *domain;
+	const int idx = ccs_read_lock();
+	ccs_policy_loaded = true;
+	printk(KERN_INFO "CCSecurity: 1.8.4   2015/05/05\n");
+	list_for_each_entry_srcu(domain, &ccs_domain_list, list, &ccs_ss) {
+		const u8 profile = domain->profile;
+		struct ccs_policy_namespace *ns = domain->ns;
+		if (ns->profile_version == 20100903) {
+			static bool done;
+			if (!done)
+				printk(KERN_INFO "Converting profile version "
+				       "from %u to %u.\n", 20100903, 20150505);
+			done = true;
+			ns->profile_version = 20150505;
+		}
+		if (ns->profile_version != 20150505)
+			printk(KERN_ERR
+			       "Profile version %u is not supported.\n",
+			       ns->profile_version);
+		else if (!ns->profile_ptr[profile])
+			printk(KERN_ERR
+			       "Profile %u (used by '%s') is not defined.\n",
+			       profile, domain->domainname->name);
+		else
+			continue;
+		printk(KERN_ERR
+		       "Userland tools for TOMOYO 1.8 must be installed and "
+		       "policy must be initialized.\n");
+		printk(KERN_ERR "Please see http://tomoyo.osdn.jp/1.8/ "
+		       "for more information.\n");
+		panic("STOP!");
+	}
+	ccs_read_unlock(idx);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 2, 0) && defined(CONFIG_SECURITY)
+	ccsecurity_exports.add_hooks();
+#endif
+	printk(KERN_INFO "Mandatory Access Control activated.\n");
+}
+
+/**
+ * ccs_profile - Find a profile.
+ *
+ * @profile: Profile number to find.
+ *
+ * Returns pointer to "struct ccs_profile".
+ */
+static struct ccs_profile *ccs_profile(const u8 profile)
+{
+	static struct ccs_profile ccs_null_profile;
+	struct ccs_profile *ptr = ccs_current_namespace()->
+		profile_ptr[profile];
+	if (!ptr)
+		ptr = &ccs_null_profile;
+	return ptr;
+}
+
+/**
+ * ccs_get_config - Get config for specified profile's specified functionality.
+ *
+ * @profile: Profile number.
+ * @index:   Index number of functionality.
+ *
+ * Returns config.
+ *
+ * First, check for CONFIG::category::functionality.
+ * If CONFIG::category::functionality is set to use default, then check
+ * CONFIG::category. If CONFIG::category is set to use default, then use
+ * CONFIG. CONFIG cannot be set to use default.
+ */
+u8 ccs_get_config(const u8 profile, const u8 index)
+{
+	u8 config;
+	const struct ccs_profile *p;
+	if (!ccs_policy_loaded)
+		return CCS_CONFIG_DISABLED;
+	p = ccs_profile(profile);
+	config = p->config[index];
+	if (config == CCS_CONFIG_USE_DEFAULT)
+		config = p->config[ccs_index2category[index]
+				   + CCS_MAX_MAC_INDEX];
+	if (config == CCS_CONFIG_USE_DEFAULT)
+		config = p->default_config;
+	return config;
+}
+
+/**
+ * ccs_find_yesno - Find values for specified keyword.
+ *
+ * @string: String to check.
+ * @find:   Name of keyword.
+ *
+ * Returns 1 if "@find=yes" was found, 0 if "@find=no" was found, -1 otherwise.
+ */
+static s8 ccs_find_yesno(const char *string, const char *find)
+{
+	const char *cp = strstr(string, find);
+	if (cp) {
+		cp += strlen(find);
+		if (!strncmp(cp, "=yes", 4))
+			return 1;
+		else if (!strncmp(cp, "=no", 3))
+			return 0;
+	}
+	return -1;
+}
+
+/**
+ * ccs_set_uint - Set value for specified preference.
+ *
+ * @i:      Pointer to "unsigned int".
+ * @string: String to check.
+ * @find:   Name of keyword.
+ *
+ * Returns nothing.
+ */
+static void ccs_set_uint(unsigned int *i, const char *string, const char *find)
+{
+	const char *cp = strstr(string, find);
+	if (cp)
+		sscanf(cp + strlen(find), "=%u", i);
+}
+
+/**
+ * ccs_str_starts - Check whether the given string starts with the given keyword.
+ *
+ * @src:  Pointer to pointer to the string.
+ * @find: Pointer to the keyword.
+ *
+ * Returns true if @src starts with @find, false otherwise.
+ *
+ * The @src is updated to point the first character after the @find
+ * if @src starts with @find.
+ */
+static bool ccs_str_starts(char **src, const char *find)
+{
+	const int len = strlen(find);
+	char *tmp = *src;
+	if (strncmp(tmp, find, len))
+		return false;
+	tmp += len;
+	*src = tmp;
+	return true;
+}
+
+/**
+ * ccs_print_group - Print group's name.
+ *
+ * @head:  Pointer to "struct ccs_io_buffer".
+ * @group: Pointer to "struct ccsgroup". Maybe NULL.
+ *
+ * Returns true if @group is not NULL. false otherwise.
+ */
+static bool ccs_print_group(struct ccs_io_buffer *head,
+			    const struct ccs_group *group)
+{
+	if (group) {
+		ccs_set_string(head, "@");
+		ccs_set_string(head, group->group_name->name);
+		return true;
+	}
+	return false;
+}
+
+/**
+ * ccs_set_mode - Set mode for specified profile.
+ *
+ * @name:    Name of functionality.
+ * @value:   Mode for @name.
+ * @profile: Pointer to "struct ccs_profile".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_set_mode(char *name, const char *value,
+			struct ccs_profile *profile)
+{
+	u8 i;
+	u8 config;
+	if (!strcmp(name, "CONFIG")) {
+		i = CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX;
+		config = profile->default_config;
+	} else if (ccs_str_starts(&name, "CONFIG::")) {
+		config = 0;
+		for (i = 0; i < CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX;
+		     i++) {
+			int len = 0;
+			if (i < CCS_MAX_MAC_INDEX) {
+				const u8 c = ccs_index2category[i];
+				const char *category =
+					ccs_category_keywords[c];
+				len = strlen(category);
+				if (strncmp(name, category, len) ||
+				    name[len++] != ':' || name[len++] != ':')
+					continue;
+			}
+			if (strcmp(name + len, ccs_mac_keywords[i]))
+				continue;
+			config = profile->config[i];
+			break;
+		}
+		if (i == CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX)
+			return -EINVAL;
+	} else {
+		return -EINVAL;
+	}
+	if (strstr(value, "use_default")) {
+		config = CCS_CONFIG_USE_DEFAULT;
+	} else {
+		u8 mode;
+		for (mode = 0; mode < CCS_CONFIG_MAX_MODE; mode++)
+			if (strstr(value, ccs_mode[mode]))
+				/*
+				 * Update lower 3 bits in order to distinguish
+				 * 'config' from 'CCS_CONFIG_USE_DEAFULT'.
+				 */
+				config = (config & ~7) | mode;
+		if (config != CCS_CONFIG_USE_DEFAULT) {
+			switch (ccs_find_yesno(value, "grant_log")) {
+			case 1:
+				config |= CCS_CONFIG_WANT_GRANT_LOG;
+				break;
+			case 0:
+				config &= ~CCS_CONFIG_WANT_GRANT_LOG;
+				break;
+			}
+			switch (ccs_find_yesno(value, "reject_log")) {
+			case 1:
+				config |= CCS_CONFIG_WANT_REJECT_LOG;
+				break;
+			case 0:
+				config &= ~CCS_CONFIG_WANT_REJECT_LOG;
+				break;
+			}
+		}
+	}
+	if (i < CCS_MAX_MAC_INDEX + CCS_MAX_MAC_CATEGORY_INDEX)
+		profile->config[i] = config;
+	else if (config != CCS_CONFIG_USE_DEFAULT)
+		profile->default_config = config;
+	return 0;
+}
+
+/**
+ * ccs_write_profile - Write profile table.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_write_profile(struct ccs_io_buffer *head)
+{
+	char *data = head->write_buf;
+	unsigned int i;
+	char *cp;
+	struct ccs_profile *profile;
+	if (sscanf(data, "PROFILE_VERSION=%u", &head->w.ns->profile_version)
+	    == 1)
+		return 0;
+	i = simple_strtoul(data, &cp, 10);
+	if (*cp != '-')
+		return -EINVAL;
+	data = cp + 1;
+	profile = ccs_assign_profile(head->w.ns, i);
+	if (!profile)
+		return -EINVAL;
+	cp = strchr(data, '=');
+	if (!cp)
+		return -EINVAL;
+	*cp++ = '\0';
+	if (!strcmp(data, "COMMENT")) {
+		static DEFINE_SPINLOCK(lock);
+		const struct ccs_path_info *new_comment = ccs_get_name(cp);
+		const struct ccs_path_info *old_comment;
+		if (!new_comment)
+			return -ENOMEM;
+		spin_lock(&lock);
+		old_comment = profile->comment;
+		profile->comment = new_comment;
+		spin_unlock(&lock);
+		ccs_put_name(old_comment);
+		return 0;
+	}
+	if (!strcmp(data, "PREFERENCE")) {
+		for (i = 0; i < CCS_MAX_PREF; i++)
+			ccs_set_uint(&profile->pref[i], cp,
+				     ccs_pref_keywords[i]);
+		return 0;
+	}
+	return ccs_set_mode(data, cp, profile);
+}
+
+/**
+ * ccs_print_config - Print mode for specified functionality.
+ *
+ * @head:   Pointer to "struct ccs_io_buffer".
+ * @config: Mode for that functionality.
+ *
+ * Returns nothing.
+ *
+ * Caller prints functionality's name.
+ */
+static void ccs_print_config(struct ccs_io_buffer *head, const u8 config)
+{
+	ccs_io_printf(head, "={ mode=%s grant_log=%s reject_log=%s }\n",
+		      ccs_mode[config & 3],
+		      ccs_yesno(config & CCS_CONFIG_WANT_GRANT_LOG),
+		      ccs_yesno(config & CCS_CONFIG_WANT_REJECT_LOG));
+}
+
+/**
+ * ccs_read_profile - Read profile table.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ *
+ * Returns nothing.
+ */
+static void ccs_read_profile(struct ccs_io_buffer *head)
+{
+	u8 index;
+	struct ccs_policy_namespace *ns = container_of(head->r.ns, typeof(*ns),
+						       namespace_list);
+	const struct ccs_profile *profile;
+	if (head->r.eof)
+		return;
+next:
+	index = head->r.index;
+	profile = ns->profile_ptr[index];
+	switch (head->r.step) {
+	case 0:
+		ccs_print_namespace(head);
+		ccs_io_printf(head, "PROFILE_VERSION=%u\n",
+			      ns->profile_version);
+		head->r.step++;
+		break;
+	case 1:
+		for ( ; head->r.index < CCS_MAX_PROFILES; head->r.index++)
+			if (ns->profile_ptr[head->r.index])
+				break;
+		if (head->r.index == CCS_MAX_PROFILES) {
+			head->r.eof = true;
+			return;
+		}
+		head->r.step++;
+		break;
+	case 2:
+		{
+			u8 i;
+			const struct ccs_path_info *comment = profile->comment;
+			ccs_print_namespace(head);
+			ccs_io_printf(head, "%u-COMMENT=", index);
+			ccs_set_string(head, comment ? comment->name : "");
+			ccs_set_lf(head);
+			ccs_print_namespace(head);
+			ccs_io_printf(head, "%u-PREFERENCE={ ", index);
+			for (i = 0; i < CCS_MAX_PREF; i++)
+				ccs_io_printf(head, "%s=%u ",
+					      ccs_pref_keywords[i],
+					      profile->pref[i]);
+			ccs_set_string(head, "}\n");
+			head->r.step++;
+		}
+		break;
+	case 3:
+		{
+			ccs_print_namespace(head);
+			ccs_io_printf(head, "%u-%s", index, "CONFIG");
+			ccs_print_config(head, profile->default_config);
+			head->r.bit = 0;
+			head->r.step++;
+		}
+		break;
+	case 4:
+		for ( ; head->r.bit < CCS_MAX_MAC_INDEX
+			      + CCS_MAX_MAC_CATEGORY_INDEX; head->r.bit++) {
+			const u8 i = head->r.bit;
+			const u8 config = profile->config[i];
+			if (config == CCS_CONFIG_USE_DEFAULT)
+				continue;
+			ccs_print_namespace(head);
+			if (i < CCS_MAX_MAC_INDEX)
+				ccs_io_printf(head, "%u-CONFIG::%s::%s", index,
+					      ccs_category_keywords
+					      [ccs_index2category[i]],
+					      ccs_mac_keywords[i]);
+			else
+				ccs_io_printf(head, "%u-CONFIG::%s", index,
+					      ccs_mac_keywords[i]);
+			ccs_print_config(head, config);
+			head->r.bit++;
+			break;
+		}
+		if (head->r.bit == CCS_MAX_MAC_INDEX
+		    + CCS_MAX_MAC_CATEGORY_INDEX) {
+			head->r.index++;
+			head->r.step = 1;
+		}
+		break;
+	}
+	if (ccs_flush(head))
+		goto next;
+}
+
+/**
+ * ccs_update_policy - Update an entry for exception policy.
+ *
+ * @size:  Size of new entry in bytes.
+ * @param: Pointer to "struct ccs_acl_param".
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static int ccs_update_policy(const int size, struct ccs_acl_param *param)
+{
+	struct ccs_acl_head *new_entry = &param->e.acl_head;
+	int error = param->is_delete ? -ENOENT : -ENOMEM;
+	struct ccs_acl_head *entry;
+	struct list_head *list = param->list;
+	BUG_ON(size < sizeof(*entry));
+	if (mutex_lock_interruptible(&ccs_policy_lock))
+		return -ENOMEM;
+	list_for_each_entry_srcu(entry, list, list, &ccs_ss) {
+		if (entry->is_deleted == CCS_GC_IN_PROGRESS)
+			continue;
+		if (memcmp(entry + 1, new_entry + 1, size - sizeof(*entry)))
+			continue;
+		entry->is_deleted = param->is_delete;
+		error = 0;
+		break;
+	}
+	if (error && !param->is_delete) {
+		entry = ccs_commit_ok(new_entry, size);
+		if (entry) {
+			list_add_tail_rcu(&entry->list, list);
+			error = 0;
+		}
+	}
+	mutex_unlock(&ccs_policy_lock);
+	return error;
+}
+
+/**
+ * ccs_update_manager_entry - Add a manager entry.
+ *
+ * @manager:   The path to manager or the domainnamme.
+ * @is_delete: True if it is a delete request.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_update_manager_entry(const char *manager,
+				    const bool is_delete)
+{
+	struct ccs_acl_param param = {
+		/* .ns = &ccs_kernel_namespace, */
+		.is_delete = is_delete,
+		.list = &ccs_kernel_namespace.policy_list[CCS_ID_MANAGER],
+	};
+	struct ccs_manager *e = &param.e.manager;
+	int error = is_delete ? -ENOENT : -ENOMEM;
+	/* Forced zero clear for using memcmp() at ccs_update_policy(). */
+	memset(&param.e, 0, sizeof(param.e));
+	if (!ccs_correct_domain(manager) && !ccs_correct_word(manager))
+		return -EINVAL;
+	e->manager = ccs_get_name(manager);
+	if (e->manager) {
+		error = ccs_update_policy(sizeof(*e), &param);
+		ccs_put_name(e->manager);
+	}
+	return error;
+}
+
+/**
+ * ccs_write_manager - Write manager policy.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_write_manager(struct ccs_io_buffer *head)
+{
+	const char *data = head->write_buf;
+	if (!strcmp(data, "manage_by_non_root")) {
+		ccs_manage_by_non_root = !head->w.is_delete;
+		return 0;
+	}
+	return ccs_update_manager_entry(data, head->w.is_delete);
+}
+
+/**
+ * ccs_read_manager - Read manager policy.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ *
+ * Returns nothing.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static void ccs_read_manager(struct ccs_io_buffer *head)
+{
+	if (head->r.eof)
+		return;
+	list_for_each_cookie(head->r.acl, &ccs_kernel_namespace.
+			     policy_list[CCS_ID_MANAGER]) {
+		struct ccs_manager *ptr =
+			list_entry(head->r.acl, typeof(*ptr), head.list);
+		if (ptr->head.is_deleted)
+			continue;
+		if (!ccs_flush(head))
+			return;
+		ccs_set_string(head, ptr->manager->name);
+		ccs_set_lf(head);
+	}
+	head->r.eof = true;
+}
+
+/**
+ * ccs_manager - Check whether the current process is a policy manager.
+ *
+ * Returns true if the current process is permitted to modify policy
+ * via /proc/ccs/ interface.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static bool ccs_manager(void)
+{
+	struct ccs_manager *ptr;
+	struct ccs_path_info exe;
+	struct ccs_security *task = ccs_current_security();
+	const struct ccs_path_info *domainname
+		= ccs_current_domain()->domainname;
+	bool found = false;
+	if (!ccs_policy_loaded)
+		return true;
+	if (task->ccs_flags & CCS_TASK_IS_MANAGER)
+		return true;
+	if (!ccs_manage_by_non_root &&
+	    (!uid_eq(current_uid(), GLOBAL_ROOT_UID) ||
+	     !uid_eq(current_euid(), GLOBAL_ROOT_UID)))
+		return false;
+	exe.name = ccs_get_exe();
+	if (!exe.name)
+		return false;
+	ccs_fill_path_info(&exe);
+	list_for_each_entry_srcu(ptr, &ccs_kernel_namespace.
+				 policy_list[CCS_ID_MANAGER], head.list,
+				 &ccs_ss) {
+		if (ptr->head.is_deleted)
+			continue;
+		if (ccs_pathcmp(domainname, ptr->manager) &&
+		    ccs_pathcmp(&exe, ptr->manager))
+			continue;
+		/* Set manager flag. */
+		task->ccs_flags |= CCS_TASK_IS_MANAGER;
+		found = true;
+		break;
+	}
+	if (!found) { /* Reduce error messages. */
+		static pid_t ccs_last_pid;
+		const pid_t pid = current->pid;
+		if (ccs_last_pid != pid) {
+			printk(KERN_WARNING "%s ( %s ) is not permitted to "
+			       "update policies.\n", domainname->name,
+			       exe.name);
+			ccs_last_pid = pid;
+		}
+	}
+	kfree(exe.name);
+	return found;
+}
+
+/**
+ * ccs_find_domain - Find a domain by the given name.
+ *
+ * @domainname: The domainname to find.
+ *
+ * Returns pointer to "struct ccs_domain_info" if found, NULL otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static struct ccs_domain_info *ccs_find_domain(const char *domainname)
+{
+	struct ccs_domain_info *domain;
+	struct ccs_path_info name;
+	name.name = domainname;
+	ccs_fill_path_info(&name);
+	list_for_each_entry_srcu(domain, &ccs_domain_list, list, &ccs_ss) {
+		if (!domain->is_deleted &&
+		    !ccs_pathcmp(&name, domain->domainname))
+			return domain;
+	}
+	return NULL;
+}
+
+/**
+ * ccs_select_domain - Parse select command.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ * @data: String to parse.
+ *
+ * Returns true on success, false otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static bool ccs_select_domain(struct ccs_io_buffer *head, const char *data)
+{
+	unsigned int pid;
+	struct ccs_domain_info *domain = NULL;
+	bool global_pid = false;
+	if (strncmp(data, "select ", 7))
+		return false;
+	data += 7;
+	if (sscanf(data, "pid=%u", &pid) == 1 ||
+	    (global_pid = true, sscanf(data, "global-pid=%u", &pid) == 1)) {
+		struct task_struct *p;
+		ccs_tasklist_lock();
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
+		if (global_pid)
+			p = ccsecurity_exports.find_task_by_pid_ns(pid,
+							       &init_pid_ns);
+		else
+			p = ccsecurity_exports.find_task_by_vpid(pid);
+#else
+		p = find_task_by_pid(pid);
+#endif
+		if (p)
+			domain = ccs_task_domain(p);
+		ccs_tasklist_unlock();
+	} else if (!strncmp(data, "domain=", 7)) {
+		if (*(data + 7) == '<')
+			domain = ccs_find_domain(data + 7);
+	} else if (sscanf(data, "Q=%u", &pid) == 1) {
+		domain = ccs_find_domain_by_qid(pid);
+	} else
+		return false;
+	head->w.domain = domain;
+	/* Accessing read_buf is safe because head->io_sem is held. */
+	if (!head->read_buf)
+		return true; /* Do nothing if open(O_WRONLY). */
+	memset(&head->r, 0, sizeof(head->r));
+	head->r.print_this_domain_only = true;
+	if (domain)
+		head->r.domain = &domain->list;
+	else
+		head->r.eof = true;
+	ccs_io_printf(head, "# select %s\n", data);
+	if (domain && domain->is_deleted)
+		ccs_set_string(head, "# This is a deleted domain.\n");
+	return true;
+}
+
+/**
+ * ccs_update_acl - Update "struct ccs_acl_info" entry.
+ *
+ * @size:  Size of new entry in bytes.
+ * @param: Pointer to "struct ccs_acl_param".
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static int ccs_update_acl(const int size, struct ccs_acl_param *param)
+{
+	struct ccs_acl_info *new_entry = &param->e.acl_info;
+	const bool is_delete = param->is_delete;
+	int error = is_delete ? -ENOENT : -ENOMEM;
+	struct ccs_acl_info *entry;
+	struct list_head * const list = param->list;
+	BUG_ON(size < sizeof(*entry));
+	if (param->data[0]) {
+		new_entry->cond = ccs_get_condition(param);
+		if (!new_entry->cond)
+			return -EINVAL;
+		/*
+		 * Domain transition preference is allowed for only
+		 * "file execute"/"task auto_execute_handler"/
+		 * "task denied_auto_execute_handler" entries.
+		 */
+		if (new_entry->cond->exec_transit &&
+		    !(new_entry->type == CCS_TYPE_PATH_ACL &&
+		      new_entry->perm == 1 << CCS_TYPE_EXECUTE)
+#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER
+		    && new_entry->type != CCS_TYPE_AUTO_EXECUTE_HANDLER &&
+		    new_entry->type != CCS_TYPE_DENIED_EXECUTE_HANDLER
+#endif
+		    )
+			return -EINVAL;
+	}
+	if (mutex_lock_interruptible(&ccs_policy_lock))
+		return -ENOMEM;
+	list_for_each_entry_srcu(entry, list, list, &ccs_ss) {
+		if (entry->is_deleted == CCS_GC_IN_PROGRESS)
+			continue;
+		if (entry->type != new_entry->type ||
+		    entry->cond != new_entry->cond ||
+		    memcmp(entry + 1, new_entry + 1, size - sizeof(*entry)))
+			continue;
+		if (is_delete)
+			entry->perm &= ~new_entry->perm;
+		else
+			entry->perm |= new_entry->perm;
+		entry->is_deleted = !entry->perm;
+		error = 0;
+		break;
+	}
+	if (error && !is_delete) {
+		entry = ccs_commit_ok(new_entry, size);
+		if (entry) {
+			list_add_tail_rcu(&entry->list, list);
+			error = 0;
+		}
+	}
+	mutex_unlock(&ccs_policy_lock);
+	return error;
+}
+
+/**
+ * ccs_permstr - Find permission keywords.
+ *
+ * @string: String representation for permissions in foo/bar/buz format.
+ * @keyword: Keyword to find from @string/
+ *
+ * Returns ture if @keyword was found in @string, false otherwise.
+ *
+ * This function assumes that strncmp(w1, w2, strlen(w1)) != 0 if w1 != w2.
+ */
+static bool ccs_permstr(const char *string, const char *keyword)
+{
+	const char *cp = strstr(string, keyword);
+	if (cp)
+		return cp == string || *(cp - 1) == '/';
+	return false;
+}
+
+/**
+ * ccs_write_task - Update task related list.
+ *
+ * @param: Pointer to "struct ccs_acl_param".
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static int ccs_write_task(struct ccs_acl_param *param)
+{
+	int error;
+	const bool is_auto = ccs_str_starts(&param->data,
+					    "auto_domain_transition ");
+	if (!is_auto && !ccs_str_starts(&param->data,
+					"manual_domain_transition ")) {
+#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER
+		struct ccs_handler_acl *e = &param->e.handler_acl;
+		char *handler;
+		if (ccs_str_starts(&param->data, "auto_execute_handler "))
+			e->head.type = CCS_TYPE_AUTO_EXECUTE_HANDLER;
+		else if (ccs_str_starts(&param->data,
+					"denied_execute_handler "))
+			e->head.type = CCS_TYPE_DENIED_EXECUTE_HANDLER;
+		else
+			return -EINVAL;
+		handler = ccs_read_token(param);
+		if (!ccs_correct_path(handler))
+			return -EINVAL;
+		e->handler = ccs_get_name(handler);
+		if (!e->handler)
+			return -ENOMEM;
+		if (e->handler->is_patterned)
+			return -EINVAL; /* No patterns allowed. */
+		return ccs_update_acl(sizeof(*e), param);
+#else
+		error = -EINVAL;
+#endif
+	} else {
+#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION
+		struct ccs_task_acl *e = &param->e.task_acl;
+		e->head.type = is_auto ?
+			CCS_TYPE_AUTO_TASK_ACL : CCS_TYPE_MANUAL_TASK_ACL;
+		e->domainname = ccs_get_domainname(param);
+		if (!e->domainname)
+			return -EINVAL;
+		return ccs_update_acl(sizeof(*e), param);
+#else
+		error = -EINVAL;
+#endif
+	}
+	return error;
+}
+
+#ifdef CONFIG_CCSECURITY_NETWORK
+
+/**
+ * ccs_write_inet_network - Write "struct ccs_inet_acl" list.
+ *
+ * @param: Pointer to "struct ccs_acl_param".
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static int ccs_write_inet_network(struct ccs_acl_param *param)
+{
+	struct ccs_inet_acl *e = &param->e.inet_acl;
+	u8 type;
+	const char *protocol = ccs_read_token(param);
+	const char *operation = ccs_read_token(param);
+	e->head.type = CCS_TYPE_INET_ACL;
+	for (type = 0; type < CCS_SOCK_MAX; type++)
+		if (!strcmp(protocol, ccs_proto_keyword[type]))
+			break;
+	if (type == CCS_SOCK_MAX)
+		return -EINVAL;
+	e->protocol = type;
+	e->head.perm = 0;
+	for (type = 0; type < CCS_MAX_NETWORK_OPERATION; type++)
+		if (ccs_permstr(operation, ccs_socket_keyword[type]))
+			e->head.perm |= 1 << type;
+	if (!e->head.perm)
+		return -EINVAL;
+	if (param->data[0] == '@') {
+		param->data++;
+		e->address.group = ccs_get_group(param, CCS_ADDRESS_GROUP);
+		if (!e->address.group)
+			return -ENOMEM;
+	} else {
+		if (!ccs_parse_ipaddr_union(param, &e->address))
+			return -EINVAL;
+	}
+	if (!ccs_parse_number_union(param, &e->port) ||
+	    e->port.values[1] > 65535)
+		return -EINVAL;
+	return ccs_update_acl(sizeof(*e), param);
+}
+
+/**
+ * ccs_write_unix_network - Write "struct ccs_unix_acl" list.
+ *
+ * @param: Pointer to "struct ccs_acl_param".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_write_unix_network(struct ccs_acl_param *param)
+{
+	struct ccs_unix_acl *e = &param->e.unix_acl;
+	u8 type;
+	const char *protocol = ccs_read_token(param);
+	const char *operation = ccs_read_token(param);
+	e->head.type = CCS_TYPE_UNIX_ACL;
+	for (type = 0; type < CCS_SOCK_MAX; type++)
+		if (!strcmp(protocol, ccs_proto_keyword[type]))
+			break;
+	if (type == CCS_SOCK_MAX)
+		return -EINVAL;
+	e->protocol = type;
+	e->head.perm = 0;
+	for (type = 0; type < CCS_MAX_NETWORK_OPERATION; type++)
+		if (ccs_permstr(operation, ccs_socket_keyword[type]))
+			e->head.perm |= 1 << type;
+	if (!e->head.perm)
+		return -EINVAL;
+	if (!ccs_parse_name_union(param, &e->name))
+		return -EINVAL;
+	return ccs_update_acl(sizeof(*e), param);
+}
+
+#endif
+
+/**
+ * ccs_write_file - Update file related list.
+ *
+ * @param: Pointer to "struct ccs_acl_param".
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static int ccs_write_file(struct ccs_acl_param *param)
+{
+	u16 perm = 0;
+	u8 type;
+	const char *operation = ccs_read_token(param);
+	for (type = 0; type < CCS_MAX_PATH_OPERATION; type++)
+		if (ccs_permstr(operation, ccs_path_keyword[type]))
+			perm |= 1 << type;
+	if (perm) {
+		struct ccs_path_acl *e = &param->e.path_acl;
+		e->head.type = CCS_TYPE_PATH_ACL;
+		e->head.perm = perm;
+		if (!ccs_parse_name_union(param, &e->name))
+			return -EINVAL;
+		return ccs_update_acl(sizeof(*e), param);
+	}
+	for (type = 0; type < CCS_MAX_PATH2_OPERATION; type++)
+		if (ccs_permstr(operation, ccs_mac_keywords[ccs_pp2mac[type]]))
+			perm |= 1 << type;
+	if (perm) {
+		struct ccs_path2_acl *e = &param->e.path2_acl;
+		e->head.type = CCS_TYPE_PATH2_ACL;
+		e->head.perm = perm;
+		if (!ccs_parse_name_union(param, &e->name1) ||
+		    !ccs_parse_name_union(param, &e->name2))
+			return -EINVAL;
+		return ccs_update_acl(sizeof(*e), param);
+	}
+	for (type = 0; type < CCS_MAX_PATH_NUMBER_OPERATION; type++)
+		if (ccs_permstr(operation, ccs_mac_keywords[ccs_pn2mac[type]]))
+			perm |= 1 << type;
+	if (perm) {
+		struct ccs_path_number_acl *e = &param->e.path_number_acl;
+		e->head.type = CCS_TYPE_PATH_NUMBER_ACL;
+		e->head.perm = perm;
+		if (!ccs_parse_name_union(param, &e->name) ||
+		    !ccs_parse_number_union(param, &e->number))
+			return -EINVAL;
+		return ccs_update_acl(sizeof(*e), param);
+	}
+	for (type = 0; type < CCS_MAX_MKDEV_OPERATION; type++)
+		if (ccs_permstr(operation,
+				ccs_mac_keywords[ccs_pnnn2mac[type]]))
+			perm |= 1 << type;
+	if (perm) {
+		struct ccs_mkdev_acl *e = &param->e.mkdev_acl;
+		e->head.type = CCS_TYPE_MKDEV_ACL;
+		e->head.perm = perm;
+		if (!ccs_parse_name_union(param, &e->name) ||
+		    !ccs_parse_number_union(param, &e->mode) ||
+		    !ccs_parse_number_union(param, &e->major) ||
+		    !ccs_parse_number_union(param, &e->minor))
+			return -EINVAL;
+		return ccs_update_acl(sizeof(*e), param);
+	}
+	if (ccs_permstr(operation, ccs_mac_keywords[CCS_MAC_FILE_MOUNT])) {
+		struct ccs_mount_acl *e = &param->e.mount_acl;
+		e->head.type = CCS_TYPE_MOUNT_ACL;
+		if (!ccs_parse_name_union(param, &e->dev_name) ||
+		    !ccs_parse_name_union(param, &e->dir_name) ||
+		    !ccs_parse_name_union(param, &e->fs_type) ||
+		    !ccs_parse_number_union(param, &e->flags))
+			return -EINVAL;
+		return ccs_update_acl(sizeof(*e), param);
+	}
+	return -EINVAL;
+}
+
+#ifdef CONFIG_CCSECURITY_MISC
+
+/**
+ * ccs_write_misc - Update environment variable list.
+ *
+ * @param: Pointer to "struct ccs_acl_param".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_write_misc(struct ccs_acl_param *param)
+{
+	if (ccs_str_starts(&param->data, "env ")) {
+		struct ccs_env_acl *e = &param->e.env_acl;
+		const char *data = ccs_read_token(param);
+		e->head.type = CCS_TYPE_ENV_ACL;
+		if (!ccs_correct_word(data) || strchr(data, '='))
+			return -EINVAL;
+		e->env = ccs_get_name(data);
+		if (!e->env)
+			return -ENOMEM;
+		return ccs_update_acl(sizeof(*e), param);
+	}
+	return -EINVAL;
+}
+
+#endif
+
+#ifdef CONFIG_CCSECURITY_IPC
+
+/**
+ * ccs_write_ipc - Update "struct ccs_signal_acl" list.
+ *
+ * @param: Pointer to "struct ccs_acl_param".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_write_ipc(struct ccs_acl_param *param)
+{
+	struct ccs_signal_acl *e = &param->e.signal_acl;
+	e->head.type = CCS_TYPE_SIGNAL_ACL;
+	if (!ccs_parse_number_union(param, &e->sig))
+		return -EINVAL;
+	e->domainname = ccs_get_domainname(param);
+	if (!e->domainname)
+		return -EINVAL;
+	return ccs_update_acl(sizeof(*e), param);
+}
+
+#endif
+
+#ifdef CONFIG_CCSECURITY_CAPABILITY
+
+/**
+ * ccs_write_capability - Write "struct ccs_capability_acl" list.
+ *
+ * @param: Pointer to "struct ccs_acl_param".
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static int ccs_write_capability(struct ccs_acl_param *param)
+{
+	struct ccs_capability_acl *e = &param->e.capability_acl;
+	const char *operation = ccs_read_token(param);
+	u8 type;
+	e->head.type = CCS_TYPE_CAPABILITY_ACL;
+	for (type = 0; type < CCS_MAX_CAPABILITY_INDEX; type++) {
+		if (strcmp(operation, ccs_mac_keywords[ccs_c2mac[type]]))
+			continue;
+		e->operation = type;
+		return ccs_update_acl(sizeof(*e), param);
+	}
+	return -EINVAL;
+}
+
+#endif
+
+/**
+ * ccs_write_acl - Write "struct ccs_acl_info" list.
+ *
+ * @ns:        Pointer to "struct ccs_policy_namespace".
+ * @list:      Pointer to "struct list_head".
+ * @data:      Policy to be interpreted.
+ * @is_delete: True if it is a delete request.
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static int ccs_write_acl(struct ccs_policy_namespace *ns,
+			 struct list_head *list, char *data,
+			 const bool is_delete)
+{
+	struct ccs_acl_param param = {
+		.ns = ns,
+		.list = list,
+		.data = data,
+		.is_delete = is_delete,
+	};
+	static const struct {
+		const char *keyword;
+		int (*write) (struct ccs_acl_param *);
+	} ccs_callback[] = {
+		{ "file ", ccs_write_file },
+#ifdef CONFIG_CCSECURITY_NETWORK
+		{ "network inet ", ccs_write_inet_network },
+		{ "network unix ", ccs_write_unix_network },
+#endif
+#ifdef CONFIG_CCSECURITY_MISC
+		{ "misc ", ccs_write_misc },
+#endif
+#ifdef CONFIG_CCSECURITY_CAPABILITY
+		{ "capability ", ccs_write_capability },
+#endif
+#ifdef CONFIG_CCSECURITY_IPC
+		{ "ipc signal ", ccs_write_ipc },
+#endif
+		{ "task ", ccs_write_task },
+	};
+	u8 i;
+	/* Forced zero clear for using memcmp() at ccs_update_acl(). */
+	memset(&param.e, 0, sizeof(param.e));
+	param.e.acl_info.perm = 1;
+	for (i = 0; i < ARRAY_SIZE(ccs_callback); i++) {
+		int error;
+		if (!ccs_str_starts(&param.data, ccs_callback[i].keyword))
+			continue;
+		error = ccs_callback[i].write(&param);
+		ccs_del_acl(&param.e.acl_info.list);
+		return error;
+	}
+	return -EINVAL;
+}
+
+/**
+ * ccs_delete_domain - Delete a domain.
+ *
+ * @domainname: The name of domain.
+ *
+ * Returns 0.
+ */
+static int ccs_delete_domain(char *domainname)
+{
+	struct ccs_domain_info *domain;
+	struct ccs_path_info name;
+	name.name = domainname;
+	ccs_fill_path_info(&name);
+	if (mutex_lock_interruptible(&ccs_policy_lock))
+		return 0;
+	/* Is there an active domain? */
+	list_for_each_entry_srcu(domain, &ccs_domain_list, list, &ccs_ss) {
+		/* Never delete ccs_kernel_domain. */
+		if (domain == &ccs_kernel_domain)
+			continue;
+		if (domain->is_deleted ||
+		    ccs_pathcmp(domain->domainname, &name))
+			continue;
+		domain->is_deleted = true;
+		break;
+	}
+	mutex_unlock(&ccs_policy_lock);
+	return 0;
+}
+
+/**
+ * ccs_write_domain - Write domain policy.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static int ccs_write_domain(struct ccs_io_buffer *head)
+{
+	char *data = head->write_buf;
+	struct ccs_policy_namespace *ns;
+	struct ccs_domain_info *domain = head->w.domain;
+	const bool is_delete = head->w.is_delete;
+	const bool is_select = !is_delete && ccs_str_starts(&data, "select ");
+	unsigned int idx;
+	if (*data == '<') {
+		domain = NULL;
+		if (is_delete)
+			ccs_delete_domain(data);
+		else if (is_select)
+			domain = ccs_find_domain(data);
+		else
+			domain = ccs_assign_domain(data, false);
+		head->w.domain = domain;
+		return 0;
+	}
+	if (!domain)
+		return -EINVAL;
+	ns = domain->ns;
+	if (sscanf(data, "use_profile %u\n", &idx) == 1 &&
+	    idx < CCS_MAX_PROFILES) {
+		if (!ccs_policy_loaded || ns->profile_ptr[(u8) idx])
+			if (!is_delete)
+				domain->profile = (u8) idx;
+		return 0;
+	}
+	if (sscanf(data, "use_group %u\n", &idx) == 1 &&
+	    idx < CCS_MAX_ACL_GROUPS) {
+		if (!is_delete)
+			set_bit(idx, domain->group);
+		else
+			clear_bit(idx, domain->group);
+		return 0;
+	}
+	for (idx = 0; idx < CCS_MAX_DOMAIN_INFO_FLAGS; idx++) {
+		const char *cp = ccs_dif[idx];
+		if (strncmp(data, cp, strlen(cp) - 1))
+			continue;
+		domain->flags[idx] = !is_delete;
+		return 0;
+	}
+	return ccs_write_acl(ns, &domain->acl_info_list, data, is_delete);
+}
+
+/**
+ * ccs_print_name_union - Print a ccs_name_union.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ * @ptr:  Pointer to "struct ccs_name_union".
+ *
+ * Returns nothing.
+ */
+static void ccs_print_name_union(struct ccs_io_buffer *head,
+				 const struct ccs_name_union *ptr)
+{
+	ccs_set_space(head);
+	if (!ccs_print_group(head, ptr->group))
+		ccs_set_string(head, ptr->filename->name);
+}
+
+/**
+ * ccs_print_name_union_quoted - Print a ccs_name_union with a quote.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ * @ptr:  Pointer to "struct ccs_name_union".
+ *
+ * Returns nothing.
+ */
+static void ccs_print_name_union_quoted(struct ccs_io_buffer *head,
+					const struct ccs_name_union *ptr)
+{
+	if (!ccs_print_group(head, ptr->group)) {
+		ccs_set_string(head, "\"");
+		ccs_set_string(head, ptr->filename->name);
+		ccs_set_string(head, "\"");
+	}
+}
+
+/**
+ * ccs_print_number_union_nospace - Print a ccs_number_union without a space.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ * @ptr:  Pointer to "struct ccs_number_union".
+ *
+ * Returns nothing.
+ */
+static void ccs_print_number_union_nospace(struct ccs_io_buffer *head,
+					   const struct ccs_number_union *ptr)
+{
+	if (!ccs_print_group(head, ptr->group)) {
+		int i;
+		unsigned long min = ptr->values[0];
+		const unsigned long max = ptr->values[1];
+		u8 min_type = ptr->value_type[0];
+		const u8 max_type = ptr->value_type[1];
+		char buffer[128];
+		buffer[0] = '\0';
+		for (i = 0; i < 2; i++) {
+			switch (min_type) {
+			case CCS_VALUE_TYPE_HEXADECIMAL:
+				ccs_addprintf(buffer, sizeof(buffer), "0x%lX",
+					      min);
+				break;
+			case CCS_VALUE_TYPE_OCTAL:
+				ccs_addprintf(buffer, sizeof(buffer), "0%lo",
+					      min);
+				break;
+			default:
+				ccs_addprintf(buffer, sizeof(buffer), "%lu",
+					      min);
+				break;
+			}
+			if (min == max && min_type == max_type)
+				break;
+			ccs_addprintf(buffer, sizeof(buffer), "-");
+			min_type = max_type;
+			min = max;
+		}
+		ccs_io_printf(head, "%s", buffer);
+	}
+}
+
+/**
+ * ccs_print_number_union - Print a ccs_number_union.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ * @ptr:  Pointer to "struct ccs_number_union".
+ *
+ * Returns nothing.
+ */
+static void ccs_print_number_union(struct ccs_io_buffer *head,
+				   const struct ccs_number_union *ptr)
+{
+	ccs_set_space(head);
+	ccs_print_number_union_nospace(head, ptr);
+}
+
+/**
+ * ccs_print_condition - Print condition part.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ * @cond: Pointer to "struct ccs_condition".
+ *
+ * Returns true on success, false otherwise.
+ */
+static bool ccs_print_condition(struct ccs_io_buffer *head,
+				const struct ccs_condition *cond)
+{
+	switch (head->r.cond_step) {
+	case 0:
+		head->r.cond_index = 0;
+		head->r.cond_step++;
+		if (cond->transit && cond->exec_transit) {
+			ccs_set_space(head);
+			ccs_set_string(head, cond->transit->name);
+		}
+		/* fall through */
+	case 1:
+		{
+			const u16 condc = cond->condc;
+			const struct ccs_condition_element *condp =
+				(typeof(condp)) (cond + 1);
+			const struct ccs_number_union *numbers_p =
+				(typeof(numbers_p)) (condp + condc);
+			const struct ccs_name_union *names_p =
+				(typeof(names_p))
+				(numbers_p + cond->numbers_count);
+			const struct ccs_argv *argv =
+				(typeof(argv)) (names_p + cond->names_count);
+			const struct ccs_envp *envp =
+				(typeof(envp)) (argv + cond->argc);
+			u16 skip;
+			for (skip = 0; skip < head->r.cond_index; skip++) {
+				const u8 left = condp->left;
+				const u8 right = condp->right;
+				condp++;
+				switch (left) {
+				case CCS_ARGV_ENTRY:
+					argv++;
+					continue;
+				case CCS_ENVP_ENTRY:
+					envp++;
+					continue;
+				case CCS_NUMBER_UNION:
+					numbers_p++;
+					break;
+				}
+				switch (right) {
+				case CCS_NAME_UNION:
+					names_p++;
+					break;
+				case CCS_NUMBER_UNION:
+					numbers_p++;
+					break;
+				}
+			}
+			while (head->r.cond_index < condc) {
+				const u8 match = condp->equals;
+				const u8 left = condp->left;
+				const u8 right = condp->right;
+				if (!ccs_flush(head))
+					return false;
+				condp++;
+				head->r.cond_index++;
+				ccs_set_space(head);
+				switch (left) {
+				case CCS_ARGV_ENTRY:
+					ccs_io_printf(head,
+						      "exec.argv[%lu]%s=\"",
+						      argv->index,
+						      argv->is_not ? "!" : "");
+					ccs_set_string(head,
+						       argv->value->name);
+					ccs_set_string(head, "\"");
+					argv++;
+					continue;
+				case CCS_ENVP_ENTRY:
+					ccs_set_string(head, "exec.envp[\"");
+					ccs_set_string(head, envp->name->name);
+					ccs_io_printf(head, "\"]%s=",
+						      envp->is_not ? "!" : "");
+					if (envp->value) {
+						ccs_set_string(head, "\"");
+						ccs_set_string(head, envp->
+							       value->name);
+						ccs_set_string(head, "\"");
+					} else {
+						ccs_set_string(head, "NULL");
+					}
+					envp++;
+					continue;
+				case CCS_NUMBER_UNION:
+					ccs_print_number_union_nospace
+						(head, numbers_p++);
+					break;
+				default:
+					ccs_set_string(head,
+					       ccs_condition_keyword[left]);
+					break;
+				}
+				ccs_set_string(head, match ? "=" : "!=");
+				switch (right) {
+				case CCS_NAME_UNION:
+					ccs_print_name_union_quoted
+						(head, names_p++);
+					break;
+				case CCS_NUMBER_UNION:
+					ccs_print_number_union_nospace
+						(head, numbers_p++);
+					break;
+				default:
+					ccs_set_string(head,
+					       ccs_condition_keyword[right]);
+					break;
+				}
+			}
+		}
+		head->r.cond_step++;
+		/* fall through */
+	case 2:
+		if (!ccs_flush(head))
+			break;
+		head->r.cond_step++;
+		/* fall through */
+	case 3:
+		if (cond->grant_log != CCS_GRANTLOG_AUTO)
+			ccs_io_printf(head, " grant_log=%s",
+				      ccs_yesno(cond->grant_log ==
+						CCS_GRANTLOG_YES));
+		if (cond->transit && !cond->exec_transit) {
+			const char *name = cond->transit->name;
+			ccs_set_string(head, " auto_domain_transition=\"");
+			ccs_set_string(head, name);
+			ccs_set_string(head, "\"");
+		}
+		ccs_set_lf(head);
+		return true;
+	}
+	return false;
+}
+
+/**
+ * ccs_set_group - Print "acl_group " header keyword and category name.
+ *
+ * @head:     Pointer to "struct ccs_io_buffer".
+ * @category: Category name.
+ *
+ * Returns nothing.
+ */
+static void ccs_set_group(struct ccs_io_buffer *head, const char *category)
+{
+	if (head->type == CCS_EXCEPTION_POLICY) {
+		ccs_print_namespace(head);
+		ccs_io_printf(head, "acl_group %u ", head->r.acl_group_index);
+	}
+	ccs_set_string(head, category);
+}
+
+/**
+ * ccs_print_entry - Print an ACL entry.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ * @acl:  Pointer to an ACL entry.
+ *
+ * Returns true on success, false otherwise.
+ */
+static bool ccs_print_entry(struct ccs_io_buffer *head,
+			    const struct ccs_acl_info *acl)
+{
+	const u8 acl_type = acl->type;
+	const bool may_trigger_transition = acl->cond && acl->cond->transit;
+	bool first = true;
+	u8 bit;
+	if (head->r.print_cond_part)
+		goto print_cond_part;
+	if (acl->is_deleted)
+		return true;
+	if (!ccs_flush(head))
+		return false;
+	else if (acl_type == CCS_TYPE_PATH_ACL) {
+		struct ccs_path_acl *ptr
+			= container_of(acl, typeof(*ptr), head);
+		for (bit = 0; bit < CCS_MAX_PATH_OPERATION; bit++) {
+			if (!(acl->perm & (1 << bit)))
+				continue;
+			if (head->r.print_transition_related_only &&
+			    bit != CCS_TYPE_EXECUTE && !may_trigger_transition)
+				continue;
+			if (first) {
+				ccs_set_group(head, "file ");
+				first = false;
+			} else {
+				ccs_set_slash(head);
+			}
+			ccs_set_string(head, ccs_path_keyword[bit]);
+		}
+		if (first)
+			return true;
+		ccs_print_name_union(head, &ptr->name);
+#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER
+	} else if (acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER ||
+		   acl_type == CCS_TYPE_DENIED_EXECUTE_HANDLER) {
+		struct ccs_handler_acl *ptr
+			= container_of(acl, typeof(*ptr), head);
+		ccs_set_group(head, "task ");
+		ccs_set_string(head, acl_type == CCS_TYPE_AUTO_EXECUTE_HANDLER
+			       ? "auto_execute_handler " :
+			       "denied_execute_handler ");
+		ccs_set_string(head, ptr->handler->name);
+#endif
+#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION
+	} else if (acl_type == CCS_TYPE_AUTO_TASK_ACL ||
+		   acl_type == CCS_TYPE_MANUAL_TASK_ACL) {
+		struct ccs_task_acl *ptr =
+			container_of(acl, typeof(*ptr), head);
+		ccs_set_group(head, "task ");
+		ccs_set_string(head, acl_type == CCS_TYPE_AUTO_TASK_ACL ?
+			       "auto_domain_transition " :
+			       "manual_domain_transition ");
+		ccs_set_string(head, ptr->domainname->name);
+#endif
+	} else if (head->r.print_transition_related_only &&
+		   !may_trigger_transition) {
+		return true;
+	} else if (acl_type == CCS_TYPE_MKDEV_ACL) {
+		struct ccs_mkdev_acl *ptr =
+			container_of(acl, typeof(*ptr), head);
+		for (bit = 0; bit < CCS_MAX_MKDEV_OPERATION; bit++) {
+			if (!(acl->perm & (1 << bit)))
+				continue;
+			if (first) {
+				ccs_set_group(head, "file ");
+				first = false;
+			} else {
+				ccs_set_slash(head);
+			}
+			ccs_set_string(head, ccs_mac_keywords
+				       [ccs_pnnn2mac[bit]]);
+		}
+		if (first)
+			return true;
+		ccs_print_name_union(head, &ptr->name);
+		ccs_print_number_union(head, &ptr->mode);
+		ccs_print_number_union(head, &ptr->major);
+		ccs_print_number_union(head, &ptr->minor);
+	} else if (acl_type == CCS_TYPE_PATH2_ACL) {
+		struct ccs_path2_acl *ptr =
+			container_of(acl, typeof(*ptr), head);
+		for (bit = 0; bit < CCS_MAX_PATH2_OPERATION; bit++) {
+			if (!(acl->perm & (1 << bit)))
+				continue;
+			if (first) {
+				ccs_set_group(head, "file ");
+				first = false;
+			} else {
+				ccs_set_slash(head);
+			}
+			ccs_set_string(head, ccs_mac_keywords
+				       [ccs_pp2mac[bit]]);
+		}
+		if (first)
+			return true;
+		ccs_print_name_union(head, &ptr->name1);
+		ccs_print_name_union(head, &ptr->name2);
+	} else if (acl_type == CCS_TYPE_PATH_NUMBER_ACL) {
+		struct ccs_path_number_acl *ptr =
+			container_of(acl, typeof(*ptr), head);
+		for (bit = 0; bit < CCS_MAX_PATH_NUMBER_OPERATION; bit++) {
+			if (!(acl->perm & (1 << bit)))
+				continue;
+			if (first) {
+				ccs_set_group(head, "file ");
+				first = false;
+			} else {
+				ccs_set_slash(head);
+			}
+			ccs_set_string(head, ccs_mac_keywords
+				       [ccs_pn2mac[bit]]);
+		}
+		if (first)
+			return true;
+		ccs_print_name_union(head, &ptr->name);
+		ccs_print_number_union(head, &ptr->number);
+#ifdef CONFIG_CCSECURITY_MISC
+	} else if (acl_type == CCS_TYPE_ENV_ACL) {
+		struct ccs_env_acl *ptr =
+			container_of(acl, typeof(*ptr), head);
+		ccs_set_group(head, "misc env ");
+		ccs_set_string(head, ptr->env->name);
+#endif
+#ifdef CONFIG_CCSECURITY_CAPABILITY
+	} else if (acl_type == CCS_TYPE_CAPABILITY_ACL) {
+		struct ccs_capability_acl *ptr =
+			container_of(acl, typeof(*ptr), head);
+		ccs_set_group(head, "capability ");
+		ccs_set_string(head, ccs_mac_keywords
+			       [ccs_c2mac[ptr->operation]]);
+#endif
+#ifdef CONFIG_CCSECURITY_NETWORK
+	} else if (acl_type == CCS_TYPE_INET_ACL) {
+		struct ccs_inet_acl *ptr =
+			container_of(acl, typeof(*ptr), head);
+		for (bit = 0; bit < CCS_MAX_NETWORK_OPERATION; bit++) {
+			if (!(acl->perm & (1 << bit)))
+				continue;
+			if (first) {
+				ccs_set_group(head, "network inet ");
+				ccs_set_string(head, ccs_proto_keyword
+					       [ptr->protocol]);
+				ccs_set_space(head);
+				first = false;
+			} else {
+				ccs_set_slash(head);
+			}
+			ccs_set_string(head, ccs_socket_keyword[bit]);
+		}
+		if (first)
+			return true;
+		ccs_set_space(head);
+		if (!ccs_print_group(head, ptr->address.group)) {
+			char buf[128];
+			ccs_print_ip(buf, sizeof(buf), &ptr->address);
+			ccs_io_printf(head, "%s", buf);
+		}
+		ccs_print_number_union(head, &ptr->port);
+	} else if (acl_type == CCS_TYPE_UNIX_ACL) {
+		struct ccs_unix_acl *ptr =
+			container_of(acl, typeof(*ptr), head);
+		for (bit = 0; bit < CCS_MAX_NETWORK_OPERATION; bit++) {
+			if (!(acl->perm & (1 << bit)))
+				continue;
+			if (first) {
+				ccs_set_group(head, "network unix ");
+				ccs_set_string(head, ccs_proto_keyword
+					       [ptr->protocol]);
+				ccs_set_space(head);
+				first = false;
+			} else {
+				ccs_set_slash(head);
+			}
+			ccs_set_string(head, ccs_socket_keyword[bit]);
+		}
+		if (first)
+			return true;
+		ccs_print_name_union(head, &ptr->name);
+#endif
+#ifdef CONFIG_CCSECURITY_IPC
+	} else if (acl_type == CCS_TYPE_SIGNAL_ACL) {
+		struct ccs_signal_acl *ptr =
+			container_of(acl, typeof(*ptr), head);
+		ccs_set_group(head, "ipc signal ");
+		ccs_print_number_union_nospace(head, &ptr->sig);
+		ccs_set_space(head);
+		ccs_set_string(head, ptr->domainname->name);
+#endif
+	} else if (acl_type == CCS_TYPE_MOUNT_ACL) {
+		struct ccs_mount_acl *ptr =
+			container_of(acl, typeof(*ptr), head);
+		ccs_set_group(head, "file mount");
+		ccs_print_name_union(head, &ptr->dev_name);
+		ccs_print_name_union(head, &ptr->dir_name);
+		ccs_print_name_union(head, &ptr->fs_type);
+		ccs_print_number_union(head, &ptr->flags);
+	}
+	if (acl->cond) {
+		head->r.print_cond_part = true;
+		head->r.cond_step = 0;
+		if (!ccs_flush(head))
+			return false;
+print_cond_part:
+		if (!ccs_print_condition(head, acl->cond))
+			return false;
+		head->r.print_cond_part = false;
+	} else {
+		ccs_set_lf(head);
+	}
+	return true;
+}
+
+/**
+ * ccs_read_acl - Read "struct ccs_acl_info" list.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ * @list: Pointer to "struct list_head".
+ *
+ * Returns true on success, false otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static bool ccs_read_acl(struct ccs_io_buffer *head, struct list_head *list)
+{
+	list_for_each_cookie(head->r.acl, list) {
+		struct ccs_acl_info *ptr =
+			list_entry(head->r.acl, typeof(*ptr), list);
+		if (!ccs_print_entry(head, ptr))
+			return false;
+	}
+	head->r.acl = NULL;
+	return true;
+}
+
+/**
+ * ccs_read_domain - Read domain policy.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ *
+ * Returns nothing.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static void ccs_read_domain(struct ccs_io_buffer *head)
+{
+	if (head->r.eof)
+		return;
+	list_for_each_cookie(head->r.domain, &ccs_domain_list) {
+		struct ccs_domain_info *domain =
+			list_entry(head->r.domain, typeof(*domain), list);
+		switch (head->r.step) {
+			u8 i;
+		case 0:
+			if (domain->is_deleted &&
+			    !head->r.print_this_domain_only)
+				continue;
+			/* Print domainname and flags. */
+			ccs_set_string(head, domain->domainname->name);
+			ccs_set_lf(head);
+			ccs_io_printf(head, "use_profile %u\n",
+				      domain->profile);
+			for (i = 0; i < CCS_MAX_DOMAIN_INFO_FLAGS; i++)
+				if (domain->flags[i])
+					ccs_set_string(head, ccs_dif[i]);
+			head->r.index = 0;
+			head->r.step++;
+			/* fall through */
+		case 1:
+			while (head->r.index < CCS_MAX_ACL_GROUPS) {
+				i = head->r.index++;
+				if (!test_bit(i, domain->group))
+					continue;
+				ccs_io_printf(head, "use_group %u\n", i);
+				if (!ccs_flush(head))
+					return;
+			}
+			head->r.index = 0;
+			head->r.step++;
+			ccs_set_lf(head);
+			/* fall through */
+		case 2:
+			if (!ccs_read_acl(head, &domain->acl_info_list))
+				return;
+			head->r.step++;
+			if (!ccs_set_lf(head))
+				return;
+			/* fall through */
+		case 3:
+			head->r.step = 0;
+			if (head->r.print_this_domain_only)
+				goto done;
+		}
+	}
+done:
+	head->r.eof = true;
+}
+
+/**
+ * ccs_write_pid - Specify PID to obtain domainname.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ *
+ * Returns 0.
+ */
+static int ccs_write_pid(struct ccs_io_buffer *head)
+{
+	head->r.eof = false;
+	return 0;
+}
+
+/**
+ * ccs_read_pid - Read information of a process.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ *
+ * Returns the domainname which the specified PID is in or
+ * process information of the specified PID on success,
+ * empty string otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static void ccs_read_pid(struct ccs_io_buffer *head)
+{
+	char *buf = head->write_buf;
+	bool task_info = false;
+	bool global_pid = false;
+	unsigned int pid;
+	struct task_struct *p;
+	struct ccs_domain_info *domain = NULL;
+	u32 ccs_flags = 0;
+	/* Accessing write_buf is safe because head->io_sem is held. */
+	if (!buf) {
+		head->r.eof = true;
+		return; /* Do nothing if open(O_RDONLY). */
+	}
+	if (head->r.w_pos || head->r.eof)
+		return;
+	head->r.eof = true;
+	if (ccs_str_starts(&buf, "info "))
+		task_info = true;
+	if (ccs_str_starts(&buf, "global-pid "))
+		global_pid = true;
+	pid = (unsigned int) simple_strtoul(buf, NULL, 10);
+	ccs_tasklist_lock();
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 24)
+	if (global_pid)
+		p = ccsecurity_exports.find_task_by_pid_ns(pid, &init_pid_ns);
+	else
+		p = ccsecurity_exports.find_task_by_vpid(pid);
+#else
+	p = find_task_by_pid(pid);
+#endif
+	if (p) {
+		domain = ccs_task_domain(p);
+		ccs_flags = ccs_task_flags(p);
+	}
+	ccs_tasklist_unlock();
+	if (!domain)
+		return;
+	if (!task_info) {
+		ccs_io_printf(head, "%u %u ", pid, domain->profile);
+		ccs_set_string(head, domain->domainname->name);
+	} else {
+		ccs_io_printf(head, "%u manager=%s execute_handler=%s ", pid,
+			      ccs_yesno(ccs_flags &
+					CCS_TASK_IS_MANAGER),
+			      ccs_yesno(ccs_flags &
+					CCS_TASK_IS_EXECUTE_HANDLER));
+	}
+}
+
+/**
+ * ccs_write_group - Write "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
+ *
+ * @param: Pointer to "struct ccs_acl_param".
+ * @type:  Type of this group.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_write_group(struct ccs_acl_param *param, const u8 type)
+{
+	struct ccs_group *group = ccs_get_group(param, type);
+	int error = -EINVAL;
+	if (!group)
+		return -ENOMEM;
+	param->list = &group->member_list;
+	if (type == CCS_PATH_GROUP) {
+		struct ccs_path_group *e = &param->e.path_group;
+		e->member_name = ccs_get_name(ccs_read_token(param));
+		if (!e->member_name) {
+			error = -ENOMEM;
+			goto out;
+		}
+		error = ccs_update_policy(sizeof(*e), param);
+		ccs_put_name(e->member_name);
+	} else if (type == CCS_NUMBER_GROUP) {
+		struct ccs_number_group *e = &param->e.number_group;
+		if (param->data[0] == '@' ||
+		    !ccs_parse_number_union(param, &e->number))
+			goto out;
+		error = ccs_update_policy(sizeof(*e), param);
+#ifdef CONFIG_CCSECURITY_NETWORK
+	} else {
+		struct ccs_address_group *e = &param->e.address_group;
+		if (param->data[0] == '@' ||
+		    !ccs_parse_ipaddr_union(param, &e->address))
+			goto out;
+		error = ccs_update_policy(sizeof(*e), param);
+#endif
+	}
+out:
+	ccs_put_group(group);
+	return error;
+}
+
+#ifdef CONFIG_CCSECURITY_PORTRESERVE
+/**
+ * ccs_lport_reserved - Check whether local port is reserved or not.
+ *
+ * @port: Port number.
+ *
+ * Returns true if local port is reserved, false otherwise.
+ */
+static bool __ccs_lport_reserved(const u16 port)
+{
+	return ccs_reserved_port_map[port >> 3] & (1 << (port & 7))
+		? true : false;
+}
+
+/**
+ * ccs_write_reserved_port - Update "struct ccs_reserved" list.
+ *
+ * @param: Pointer to "struct ccs_acl_param".
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static int ccs_write_reserved_port(struct ccs_acl_param *param)
+{
+	struct ccs_reserved *e = &param->e.reserved;
+	struct ccs_policy_namespace *ns = param->ns;
+	int error;
+	u8 *tmp;
+	if (param->data[0] == '@' ||
+	    !ccs_parse_number_union(param, &e->port) ||
+	    e->port.values[1] > 65535 || param->data[0])
+		return -EINVAL;
+	param->list = &ns->policy_list[CCS_ID_RESERVEDPORT];
+	error = ccs_update_policy(sizeof(*e), param);
+	if (error)
+		return error;
+	tmp = kzalloc(sizeof(ccs_reserved_port_map), CCS_GFP_FLAGS);
+	if (!tmp)
+		return -ENOMEM;
+	list_for_each_entry_srcu(ns, &ccs_namespace_list, namespace_list,
+				 &ccs_ss) {
+		struct ccs_reserved *ptr;
+		struct list_head *list = &ns->policy_list[CCS_ID_RESERVEDPORT];
+		list_for_each_entry_srcu(ptr, list, head.list, &ccs_ss) {
+			unsigned int port;
+			if (ptr->head.is_deleted)
+				continue;
+			for (port = ptr->port.values[0];
+			     port <= ptr->port.values[1]; port++)
+				tmp[port >> 3] |= 1 << (port & 7);
+		}
+	}
+	memmove(ccs_reserved_port_map, tmp, sizeof(ccs_reserved_port_map));
+	kfree(tmp);
+	/*
+	 * Since this feature is no-op by default, we don't need to register
+	 * this callback hook unless the first entry is added.
+	 */
+	ccsecurity_ops.lport_reserved = __ccs_lport_reserved;
+	return 0;
+}
+#endif
+
+/**
+ * ccs_write_aggregator - Write "struct ccs_aggregator" list.
+ *
+ * @param: Pointer to "struct ccs_acl_param".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_write_aggregator(struct ccs_acl_param *param)
+{
+	struct ccs_aggregator *e = &param->e.aggregator;
+	int error = param->is_delete ? -ENOENT : -ENOMEM;
+	const char *original_name = ccs_read_token(param);
+	const char *aggregated_name = ccs_read_token(param);
+	if (!ccs_correct_word(original_name) ||
+	    !ccs_correct_path(aggregated_name))
+		return -EINVAL;
+	e->original_name = ccs_get_name(original_name);
+	e->aggregated_name = ccs_get_name(aggregated_name);
+	if (!e->original_name || !e->aggregated_name ||
+	    e->aggregated_name->is_patterned) /* No patterns allowed. */
+		goto out;
+	param->list = &param->ns->policy_list[CCS_ID_AGGREGATOR];
+	error = ccs_update_policy(sizeof(*e), param);
+out:
+	ccs_put_name(e->original_name);
+	ccs_put_name(e->aggregated_name);
+	return error;
+}
+
+/**
+ * ccs_write_transition_control - Write "struct ccs_transition_control" list.
+ *
+ * @param: Pointer to "struct ccs_acl_param".
+ * @type:  Type of this entry.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_write_transition_control(struct ccs_acl_param *param,
+					const u8 type)
+{
+	struct ccs_transition_control *e = &param->e.transition_control;
+	int error = param->is_delete ? -ENOENT : -ENOMEM;
+	char *program = param->data;
+	char *domainname = strstr(program, " from ");
+	e->type = type;
+	if (domainname) {
+		*domainname = '\0';
+		domainname += 6;
+	} else if (type == CCS_TRANSITION_CONTROL_NO_KEEP ||
+		   type == CCS_TRANSITION_CONTROL_KEEP) {
+		domainname = program;
+		program = NULL;
+	}
+	if (program && strcmp(program, "any")) {
+		if (!ccs_correct_path(program))
+			return -EINVAL;
+		e->program = ccs_get_name(program);
+		if (!e->program)
+			goto out;
+	}
+	if (domainname && strcmp(domainname, "any")) {
+		if (!ccs_correct_domain(domainname)) {
+			if (!ccs_correct_path(domainname))
+				goto out;
+			e->is_last_name = true;
+		}
+		e->domainname = ccs_get_name(domainname);
+		if (!e->domainname)
+			goto out;
+	}
+	param->list = &param->ns->policy_list[CCS_ID_TRANSITION_CONTROL];
+	error = ccs_update_policy(sizeof(*e), param);
+out:
+	ccs_put_name(e->domainname);
+	ccs_put_name(e->program);
+	return error;
+}
+
+/**
+ * ccs_write_exception - Write exception policy.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_write_exception(struct ccs_io_buffer *head)
+{
+	const bool is_delete = head->w.is_delete;
+	struct ccs_acl_param param = {
+		.ns = head->w.ns,
+		.is_delete = is_delete,
+		.data = head->write_buf,
+	};
+	u8 i;
+	/* Forced zero clear for using memcmp() at ccs_update_policy(). */
+	memset(&param.e, 0, sizeof(param.e));
+	if (ccs_str_starts(&param.data, "aggregator "))
+		return ccs_write_aggregator(&param);
+#ifdef CONFIG_CCSECURITY_PORTRESERVE
+	if (ccs_str_starts(&param.data, "deny_autobind "))
+		return ccs_write_reserved_port(&param);
+#endif
+	for (i = 0; i < CCS_MAX_TRANSITION_TYPE; i++)
+		if (ccs_str_starts(&param.data, ccs_transition_type[i]))
+			return ccs_write_transition_control(&param, i);
+	for (i = 0; i < CCS_MAX_GROUP; i++)
+		if (ccs_str_starts(&param.data, ccs_group_name[i]))
+			return ccs_write_group(&param, i);
+	if (ccs_str_starts(&param.data, "acl_group ")) {
+		unsigned int group;
+		char *data;
+		group = simple_strtoul(param.data, &data, 10);
+		if (group < CCS_MAX_ACL_GROUPS && *data++ == ' ')
+			return ccs_write_acl(head->w.ns,
+					     &head->w.ns->acl_group[group],
+					     data, is_delete);
+	}
+	return -EINVAL;
+}
+
+/**
+ * ccs_read_group - Read "struct ccs_path_group"/"struct ccs_number_group"/"struct ccs_address_group" list.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ * @idx:  Index number.
+ *
+ * Returns true on success, false otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static bool ccs_read_group(struct ccs_io_buffer *head, const int idx)
+{
+	struct ccs_policy_namespace *ns = container_of(head->r.ns, typeof(*ns),
+						       namespace_list);
+	struct list_head *list = &ns->group_list[idx];
+	list_for_each_cookie(head->r.group, list) {
+		struct ccs_group *group =
+			list_entry(head->r.group, typeof(*group), head.list);
+		list_for_each_cookie(head->r.acl, &group->member_list) {
+			struct ccs_acl_head *ptr =
+				list_entry(head->r.acl, typeof(*ptr), list);
+			if (ptr->is_deleted)
+				continue;
+			if (!ccs_flush(head))
+				return false;
+			ccs_print_namespace(head);
+			ccs_set_string(head, ccs_group_name[idx]);
+			ccs_set_string(head, group->group_name->name);
+			if (idx == CCS_PATH_GROUP) {
+				ccs_set_space(head);
+				ccs_set_string(head, container_of
+					       (ptr, struct ccs_path_group,
+						head)->member_name->name);
+			} else if (idx == CCS_NUMBER_GROUP) {
+				ccs_print_number_union(head, &container_of
+					       (ptr, struct ccs_number_group,
+						head)->number);
+#ifdef CONFIG_CCSECURITY_NETWORK
+			} else if (idx == CCS_ADDRESS_GROUP) {
+				char buffer[128];
+				struct ccs_address_group *member =
+					container_of(ptr, typeof(*member),
+						     head);
+				ccs_print_ip(buffer, sizeof(buffer),
+					     &member->address);
+				ccs_io_printf(head, " %s", buffer);
+#endif
+			}
+			ccs_set_lf(head);
+		}
+		head->r.acl = NULL;
+	}
+	head->r.group = NULL;
+	return true;
+}
+
+/**
+ * ccs_read_policy - Read "struct ccs_..._entry" list.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ * @idx:  Index number.
+ *
+ * Returns true on success, false otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static bool ccs_read_policy(struct ccs_io_buffer *head, const int idx)
+{
+	struct ccs_policy_namespace *ns = container_of(head->r.ns, typeof(*ns),
+						       namespace_list);
+	struct list_head *list = &ns->policy_list[idx];
+	list_for_each_cookie(head->r.acl, list) {
+		struct ccs_acl_head *acl =
+			container_of(head->r.acl, typeof(*acl), list);
+		if (acl->is_deleted)
+			continue;
+		if (head->r.print_transition_related_only &&
+		    idx != CCS_ID_TRANSITION_CONTROL)
+			continue;
+		if (!ccs_flush(head))
+			return false;
+		switch (idx) {
+		case CCS_ID_TRANSITION_CONTROL:
+			{
+				struct ccs_transition_control *ptr =
+					container_of(acl, typeof(*ptr), head);
+				ccs_print_namespace(head);
+				ccs_set_string(head,
+					       ccs_transition_type[ptr->type]);
+				ccs_set_string(head, ptr->program ?
+					       ptr->program->name : "any");
+				ccs_set_string(head, " from ");
+				ccs_set_string(head, ptr->domainname ?
+					       ptr->domainname->name : "any");
+			}
+			break;
+		case CCS_ID_AGGREGATOR:
+			{
+				struct ccs_aggregator *ptr =
+					container_of(acl, typeof(*ptr), head);
+				ccs_print_namespace(head);
+				ccs_set_string(head, "aggregator ");
+				ccs_set_string(head, ptr->original_name->name);
+				ccs_set_space(head);
+				ccs_set_string(head,
+					       ptr->aggregated_name->name);
+			}
+			break;
+#ifdef CONFIG_CCSECURITY_PORTRESERVE
+		case CCS_ID_RESERVEDPORT:
+			{
+				struct ccs_reserved *ptr =
+					container_of(acl, typeof(*ptr), head);
+				ccs_print_namespace(head);
+				ccs_set_string(head, "deny_autobind ");
+				ccs_print_number_union_nospace(head,
+							       &ptr->port);
+			}
+			break;
+#endif
+		default:
+			continue;
+		}
+		ccs_set_lf(head);
+	}
+	head->r.acl = NULL;
+	return true;
+}
+
+/**
+ * ccs_read_exception - Read exception policy.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ *
+ * Returns nothing.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static void ccs_read_exception(struct ccs_io_buffer *head)
+{
+	struct ccs_policy_namespace *ns = container_of(head->r.ns, typeof(*ns),
+						       namespace_list);
+	if (head->r.eof)
+		return;
+	while (head->r.step < CCS_MAX_POLICY &&
+	       ccs_read_policy(head, head->r.step))
+		head->r.step++;
+	if (head->r.step < CCS_MAX_POLICY)
+		return;
+	while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP &&
+	       ccs_read_group(head, head->r.step - CCS_MAX_POLICY))
+		head->r.step++;
+	if (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP)
+		return;
+	while (head->r.step < CCS_MAX_POLICY + CCS_MAX_GROUP
+	       + CCS_MAX_ACL_GROUPS) {
+		head->r.acl_group_index =
+			head->r.step - CCS_MAX_POLICY - CCS_MAX_GROUP;
+		if (!ccs_read_acl(head, &ns->acl_group
+				  [head->r.acl_group_index]))
+			return;
+		head->r.step++;
+	}
+	head->r.eof = true;
+}
+
+/**
+ * ccs_truncate - Truncate a line.
+ *
+ * @str: String to truncate.
+ *
+ * Returns length of truncated @str.
+ */
+static int ccs_truncate(char *str)
+{
+	char *start = str;
+	while (*(unsigned char *) str > (unsigned char) ' ')
+		str++;
+	*str = '\0';
+	return strlen(start) + 1;
+}
+
+/**
+ * ccs_add_entry - Add an ACL to current thread's domain. Used by learning mode.
+ *
+ * @header: Lines containing ACL.
+ *
+ * Returns nothing.
+ */
+static void ccs_add_entry(char *header)
+{
+	char *buffer;
+	char *realpath = NULL;
+	char *argv0 = NULL;
+	char *symlink = NULL;
+#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER
+	char *handler;
+#endif
+	char *cp = strchr(header, '\n');
+	int len;
+	if (!cp)
+		return;
+	cp = strchr(cp + 1, '\n');
+	if (!cp)
+		return;
+	*cp++ = '\0';
+	len = strlen(cp) + 1;
+	/* strstr() will return NULL if ordering is wrong. */
+	if (*cp == 'f') {
+		argv0 = strstr(header, " argv[]={ \"");
+		if (argv0) {
+			argv0 += 10;
+			len += ccs_truncate(argv0) + 14;
+		}
+		realpath = strstr(header, " exec={ realpath=\"");
+		if (realpath) {
+			realpath += 8;
+			len += ccs_truncate(realpath) + 6;
+		}
+		symlink = strstr(header, " symlink.target=\"");
+		if (symlink)
+			len += ccs_truncate(symlink + 1) + 1;
+	}
+#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER
+	handler = strstr(header, "type=execute_handler");
+	if (handler)
+		len += ccs_truncate(handler) + 6;
+#endif
+	buffer = kmalloc(len, CCS_GFP_FLAGS);
+	if (!buffer)
+		return;
+	snprintf(buffer, len - 1, "%s", cp);
+#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER
+	if (handler)
+		ccs_addprintf(buffer, len, " task.%s", handler);
+#endif
+	if (realpath)
+		ccs_addprintf(buffer, len, " exec.%s", realpath);
+	if (argv0)
+		ccs_addprintf(buffer, len, " exec.argv[0]=%s", argv0);
+	if (symlink)
+		ccs_addprintf(buffer, len, "%s", symlink);
+	ccs_normalize_line(buffer);
+	{
+		struct ccs_domain_info *domain = ccs_current_domain();
+		if (!ccs_write_acl(domain->ns, &domain->acl_info_list,
+				   buffer, false))
+			ccs_update_stat(CCS_STAT_POLICY_UPDATES);
+	}
+	kfree(buffer);
+}
+
+/**
+ * ccs_domain_quota_ok - Check for domain's quota.
+ *
+ * @r: Pointer to "struct ccs_request_info".
+ *
+ * Returns true if the domain is not exceeded quota, false otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static bool ccs_domain_quota_ok(struct ccs_request_info *r)
+{
+	unsigned int count = 0;
+	struct ccs_domain_info * const domain = ccs_current_domain();
+	struct ccs_acl_info *ptr;
+	if (r->mode != CCS_CONFIG_LEARNING)
+		return false;
+	if (!domain)
+		return true;
+	list_for_each_entry_srcu(ptr, &domain->acl_info_list, list, &ccs_ss) {
+		u16 perm;
+		u8 i;
+		if (ptr->is_deleted)
+			continue;
+		switch (ptr->type) {
+		case CCS_TYPE_PATH_ACL:
+		case CCS_TYPE_PATH2_ACL:
+		case CCS_TYPE_PATH_NUMBER_ACL:
+		case CCS_TYPE_MKDEV_ACL:
+#ifdef CONFIG_CCSECURITY_NETWORK
+		case CCS_TYPE_INET_ACL:
+		case CCS_TYPE_UNIX_ACL:
+#endif
+			perm = ptr->perm;
+			break;
+#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER
+		case CCS_TYPE_AUTO_EXECUTE_HANDLER:
+		case CCS_TYPE_DENIED_EXECUTE_HANDLER:
+#endif
+#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION
+		case CCS_TYPE_AUTO_TASK_ACL:
+		case CCS_TYPE_MANUAL_TASK_ACL:
+#endif
+			perm = 0;
+			break;
+		default:
+			perm = 1;
+		}
+		for (i = 0; i < 16; i++)
+			if (perm & (1 << i))
+				count++;
+	}
+	if (count < ccs_profile(r->profile)->pref[CCS_PREF_MAX_LEARNING_ENTRY])
+		return true;
+	if (!domain->flags[CCS_DIF_QUOTA_WARNED]) {
+		domain->flags[CCS_DIF_QUOTA_WARNED] = true;
+		/* r->granted = false; */
+		ccs_write_log(r, "%s", ccs_dif[CCS_DIF_QUOTA_WARNED]);
+		printk(KERN_WARNING "WARNING: "
+		       "Domain '%s' has too many ACLs to hold. "
+		       "Stopped learning mode.\n", domain->domainname->name);
+	}
+	return false;
+}
+
+/**
+ * ccs_supervisor - Ask for the supervisor's decision.
+ *
+ * @r:   Pointer to "struct ccs_request_info".
+ * @fmt: The printf()'s format string, followed by parameters.
+ *
+ * Returns 0 if the supervisor decided to permit the access request which
+ * violated the policy in enforcing mode, CCS_RETRY_REQUEST if the supervisor
+ * decided to retry the access request which violated the policy in enforcing
+ * mode, 0 if it is not in enforcing mode, -EPERM otherwise.
+ */
+static int ccs_supervisor(struct ccs_request_info *r, const char *fmt, ...)
+{
+	va_list args;
+	int error;
+	int len;
+	static unsigned int ccs_serial;
+	struct ccs_query entry = { };
+	bool quota_exceeded = false;
+	va_start(args, fmt);
+	len = vsnprintf((char *) &len, 1, fmt, args) + 1;
+	va_end(args);
+	/* Write /proc/ccs/audit. */
+	va_start(args, fmt);
+	ccs_write_log2(r, len, fmt, args);
+	va_end(args);
+	/* Nothing more to do if granted. */
+	if (r->granted)
+		return 0;
+	if (r->mode)
+		ccs_update_stat(r->mode);
+	switch (r->mode) {
+		int i;
+		struct ccs_profile *p;
+	case CCS_CONFIG_ENFORCING:
+		error = -EPERM;
+		if (atomic_read(&ccs_query_observers))
+			break;
+		if (r->dont_sleep_on_enforce_error)
+			goto out;
+		p = ccs_profile(r->profile);
+		/* Check enforcing_penalty parameter. */
+		for (i = 0; i < p->pref[CCS_PREF_ENFORCING_PENALTY]; i++) {
+			set_current_state(TASK_INTERRUPTIBLE);
+			schedule_timeout(HZ / 10);
+		}
+		goto out;
+	case CCS_CONFIG_LEARNING:
+		error = 0;
+		/* Check max_learning_entry parameter. */
+		if (ccs_domain_quota_ok(r))
+			break;
+		/* fall through */
+	default:
+		return 0;
+	}
+	/* Get message. */
+	va_start(args, fmt);
+	entry.query = ccs_init_log(r, len, fmt, args);
+	va_end(args);
+	if (!entry.query)
+		goto out;
+	entry.query_len = strlen(entry.query) + 1;
+	if (!error) {
+		ccs_add_entry(entry.query);
+		goto out;
+	}
+	len = ccs_round2(entry.query_len);
+	entry.domain = ccs_current_domain();
+	spin_lock(&ccs_query_list_lock);
+	if (ccs_memory_quota[CCS_MEMORY_QUERY] &&
+	    ccs_memory_used[CCS_MEMORY_QUERY] + len
+	    >= ccs_memory_quota[CCS_MEMORY_QUERY]) {
+		quota_exceeded = true;
+	} else {
+		entry.serial = ccs_serial++;
+		entry.retry = r->retry;
+		ccs_memory_used[CCS_MEMORY_QUERY] += len;
+		list_add_tail(&entry.list, &ccs_query_list);
+	}
+	spin_unlock(&ccs_query_list_lock);
+	if (quota_exceeded)
+		goto out;
+	/* Give 10 seconds for supervisor's opinion. */
+	while (entry.timer < 10) {
+		wake_up_all(&ccs_query_wait);
+		if (wait_event_interruptible_timeout
+		    (ccs_answer_wait, entry.answer ||
+		     !atomic_read(&ccs_query_observers), HZ))
+			break;
+		else
+			entry.timer++;
+	}
+	spin_lock(&ccs_query_list_lock);
+	list_del(&entry.list);
+	ccs_memory_used[CCS_MEMORY_QUERY] -= len;
+	spin_unlock(&ccs_query_list_lock);
+	switch (entry.answer) {
+	case 3: /* Asked to retry by administrator. */
+		error = CCS_RETRY_REQUEST;
+		r->retry++;
+		break;
+	case 1:
+		/* Granted by administrator. */
+		error = 0;
+		break;
+	default:
+		/* Timed out or rejected by administrator. */
+		break;
+	}
+out:
+	kfree(entry.query);
+	return error;
+}
+
+/**
+ * ccs_audit_log - Audit permission check log.
+ *
+ * @r: Pointer to "struct ccs_request_info".
+ *
+ * Returns return value of ccs_supervisor().
+ */
+int ccs_audit_log(struct ccs_request_info *r)
+{
+	switch (r->param_type) {
+		u8 type;
+		char buf[48];
+#ifdef CONFIG_CCSECURITY_NETWORK
+		const u32 *address;
+#endif
+	case CCS_TYPE_PATH_ACL:
+		return ccs_supervisor(r, "file %s %s\n", ccs_path_keyword
+				      [r->param.path.operation],
+				      r->param.path.filename->name);
+	case CCS_TYPE_PATH2_ACL:
+		return ccs_supervisor(r, "file %s %s %s\n", ccs_mac_keywords
+				      [ccs_pp2mac[r->param.path2.operation]],
+				      r->param.path2.filename1->name,
+				      r->param.path2.filename2->name);
+	case CCS_TYPE_PATH_NUMBER_ACL:
+		type = r->param.path_number.operation;
+		switch (type) {
+		case CCS_TYPE_CREATE:
+		case CCS_TYPE_MKDIR:
+		case CCS_TYPE_MKFIFO:
+		case CCS_TYPE_MKSOCK:
+		case CCS_TYPE_CHMOD:
+			snprintf(buf, sizeof(buf), "0%lo",
+				 r->param.path_number.number);
+			break;
+		case CCS_TYPE_IOCTL:
+			snprintf(buf, sizeof(buf), "0x%lX",
+				 r->param.path_number.number);
+			break;
+		default:
+			snprintf(buf, sizeof(buf), "%lu",
+				 r->param.path_number.number);
+			break;
+		}
+		return ccs_supervisor(r, "file %s %s %s\n", ccs_mac_keywords
+				      [ccs_pn2mac[type]],
+				      r->param.path_number.filename->name,
+				      buf);
+	case CCS_TYPE_MKDEV_ACL:
+		return ccs_supervisor(r, "file %s %s 0%o %u %u\n",
+				      ccs_mac_keywords
+				      [ccs_pnnn2mac[r->param.mkdev.operation]],
+				      r->param.mkdev.filename->name,
+				      r->param.mkdev.mode,
+				      r->param.mkdev.major,
+				      r->param.mkdev.minor);
+	case CCS_TYPE_MOUNT_ACL:
+		return ccs_supervisor(r, "file mount %s %s %s 0x%lX\n",
+				      r->param.mount.dev->name,
+				      r->param.mount.dir->name,
+				      r->param.mount.type->name,
+				      r->param.mount.flags);
+#ifdef CONFIG_CCSECURITY_MISC
+	case CCS_TYPE_ENV_ACL:
+		return ccs_supervisor(r, "misc env %s\n",
+				      r->param.environ.name->name);
+#endif
+#ifdef CONFIG_CCSECURITY_CAPABILITY
+	case CCS_TYPE_CAPABILITY_ACL:
+		return ccs_supervisor(r, "capability %s\n", ccs_mac_keywords
+				      [ccs_c2mac[r->param.capability.
+						 operation]]);
+#endif
+#ifdef CONFIG_CCSECURITY_NETWORK
+	case CCS_TYPE_INET_ACL:
+		address = r->param.inet_network.address;
+		if (r->param.inet_network.is_ipv6)
+			ccs_print_ipv6(buf, sizeof(buf),
+				       (const struct in6_addr *) address);
+		else
+			ccs_print_ipv4(buf, sizeof(buf), address);
+		return ccs_supervisor(r, "network inet %s %s %s %u\n",
+				      ccs_proto_keyword[r->param.inet_network.
+							protocol],
+				      ccs_socket_keyword[r->param.inet_network.
+							 operation],
+				      buf, r->param.inet_network.port);
+	case CCS_TYPE_UNIX_ACL:
+		return ccs_supervisor(r, "network unix %s %s %s\n",
+				      ccs_proto_keyword[r->param.
+							unix_network.protocol],
+				      ccs_socket_keyword[r->param.unix_network.
+							 operation],
+				      r->param.unix_network.address->name);
+#endif
+#ifdef CONFIG_CCSECURITY_IPC
+	case CCS_TYPE_SIGNAL_ACL:
+		return ccs_supervisor(r, "ipc signal %d %s\n",
+				      r->param.signal.sig,
+				      r->param.signal.dest_pattern);
+#endif
+	}
+	return 0;
+}
+
+/**
+ * ccs_find_domain_by_qid - Get domain by query id.
+ *
+ * @serial: Query ID assigned by ccs_supervisor().
+ *
+ * Returns pointer to "struct ccs_domain_info" if found, NULL otherwise.
+ */
+static struct ccs_domain_info *ccs_find_domain_by_qid(unsigned int serial)
+{
+	struct ccs_query *ptr;
+	struct ccs_domain_info *domain = NULL;
+	spin_lock(&ccs_query_list_lock);
+	list_for_each_entry(ptr, &ccs_query_list, list) {
+		if (ptr->serial != serial)
+			continue;
+		domain = ptr->domain;
+		break;
+	}
+	spin_unlock(&ccs_query_list_lock);
+	return domain;
+}
+
+/**
+ * ccs_read_query - Read access requests which violated policy in enforcing mode.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ *
+ * Returns nothing.
+ */
+static void ccs_read_query(struct ccs_io_buffer *head)
+{
+	struct list_head *tmp;
+	unsigned int pos = 0;
+	size_t len = 0;
+	char *buf;
+	if (head->r.w_pos)
+		return;
+	kfree(head->read_buf);
+	head->read_buf = NULL;
+	spin_lock(&ccs_query_list_lock);
+	list_for_each(tmp, &ccs_query_list) {
+		struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
+		if (pos++ != head->r.query_index)
+			continue;
+		len = ptr->query_len;
+		break;
+	}
+	spin_unlock(&ccs_query_list_lock);
+	if (!len) {
+		head->r.query_index = 0;
+		return;
+	}
+	buf = kzalloc(len + 32, CCS_GFP_FLAGS);
+	if (!buf)
+		return;
+	pos = 0;
+	spin_lock(&ccs_query_list_lock);
+	list_for_each(tmp, &ccs_query_list) {
+		struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
+		if (pos++ != head->r.query_index)
+			continue;
+		/*
+		 * Some query can be skipped because ccs_query_list
+		 * can change, but I don't care.
+		 */
+		if (len == ptr->query_len)
+			snprintf(buf, len + 31, "Q%u-%hu\n%s", ptr->serial,
+				 ptr->retry, ptr->query);
+		break;
+	}
+	spin_unlock(&ccs_query_list_lock);
+	if (buf[0]) {
+		head->read_buf = buf;
+		head->r.w[head->r.w_pos++] = buf;
+		head->r.query_index++;
+	} else {
+		kfree(buf);
+	}
+}
+
+/**
+ * ccs_write_answer - Write the supervisor's decision.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ *
+ * Returns 0 on success, -EINVAL otherwise.
+ */
+static int ccs_write_answer(struct ccs_io_buffer *head)
+{
+	char *data = head->write_buf;
+	struct list_head *tmp;
+	unsigned int serial;
+	unsigned int answer;
+	spin_lock(&ccs_query_list_lock);
+	list_for_each(tmp, &ccs_query_list) {
+		struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
+		ptr->timer = 0;
+	}
+	spin_unlock(&ccs_query_list_lock);
+	if (sscanf(data, "A%u=%u", &serial, &answer) != 2)
+		return -EINVAL;
+	spin_lock(&ccs_query_list_lock);
+	list_for_each(tmp, &ccs_query_list) {
+		struct ccs_query *ptr = list_entry(tmp, typeof(*ptr), list);
+		if (ptr->serial != serial)
+			continue;
+		ptr->answer = (u8) answer;
+		/* Remove from ccs_query_list. */
+		if (ptr->answer) {
+			list_del(&ptr->list);
+			INIT_LIST_HEAD(&ptr->list);
+		}
+		break;
+	}
+	spin_unlock(&ccs_query_list_lock);
+	wake_up_all(&ccs_answer_wait);
+	return 0;
+}
+
+/**
+ * ccs_read_version - Get version.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ *
+ * Returns nothing.
+ */
+static void ccs_read_version(struct ccs_io_buffer *head)
+{
+	if (head->r.eof)
+		return;
+	ccs_set_string(head, "1.8.4");
+	head->r.eof = true;
+}
+
+/**
+ * ccs_update_stat - Update statistic counters.
+ *
+ * @index: Index for policy type.
+ *
+ * Returns nothing.
+ */
+static void ccs_update_stat(const u8 index)
+{
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
+	struct timeval tv;
+	do_gettimeofday(&tv);
+	/*
+	 * I don't use atomic operations because race condition is not fatal.
+	 */
+	ccs_stat_updated[index]++;
+	ccs_stat_modified[index] = tv.tv_sec;
+#else
+	/*
+	 * I don't use atomic operations because race condition is not fatal.
+	 */
+	ccs_stat_updated[index]++;
+	ccs_stat_modified[index] = get_seconds();
+#endif
+}
+
+/**
+ * ccs_read_stat - Read statistic data.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ *
+ * Returns nothing.
+ */
+static void ccs_read_stat(struct ccs_io_buffer *head)
+{
+	u8 i;
+	unsigned int total = 0;
+	if (head->r.eof)
+		return;
+	for (i = 0; i < CCS_MAX_POLICY_STAT; i++) {
+		ccs_io_printf(head, "Policy %-30s %10u", ccs_policy_headers[i],
+			      ccs_stat_updated[i]);
+		if (ccs_stat_modified[i]) {
+			struct ccs_time stamp;
+			ccs_convert_time(ccs_stat_modified[i], &stamp);
+			ccs_io_printf(head, " (Last: %04u/%02u/%02u "
+				      "%02u:%02u:%02u)",
+				      stamp.year, stamp.month, stamp.day,
+				      stamp.hour, stamp.min, stamp.sec);
+		}
+		ccs_set_lf(head);
+	}
+	for (i = 0; i < CCS_MAX_MEMORY_STAT; i++) {
+		unsigned int used = ccs_memory_used[i];
+		total += used;
+		ccs_io_printf(head, "Memory used by %-22s %10u",
+			      ccs_memory_headers[i], used);
+		used = ccs_memory_quota[i];
+		if (used)
+			ccs_io_printf(head, " (Quota: %10u)", used);
+		ccs_set_lf(head);
+	}
+	ccs_io_printf(head, "Total memory used:                    %10u\n",
+		      total);
+	head->r.eof = true;
+}
+
+/**
+ * ccs_write_stat - Set memory quota.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ *
+ * Returns 0.
+ */
+static int ccs_write_stat(struct ccs_io_buffer *head)
+{
+	char *data = head->write_buf;
+	u8 i;
+	if (ccs_str_starts(&data, "Memory used by "))
+		for (i = 0; i < CCS_MAX_MEMORY_STAT; i++)
+			if (ccs_str_starts(&data, ccs_memory_headers[i])) {
+				if (*data == ' ')
+					data++;
+				ccs_memory_quota[i] =
+					simple_strtoul(data, NULL, 10);
+			}
+	return 0;
+}
+
+/**
+ * ccs_print_bprm - Print "struct linux_binprm" for auditing.
+ *
+ * @bprm: Pointer to "struct linux_binprm".
+ * @dump: Pointer to "struct ccs_page_dump".
+ *
+ * Returns the contents of @bprm on success, NULL otherwise.
+ *
+ * This function uses kzalloc(), so caller must kfree() if this function
+ * didn't return NULL.
+ */
+static char *ccs_print_bprm(struct linux_binprm *bprm,
+			    struct ccs_page_dump *dump)
+{
+	static const int ccs_buffer_len = 4096 * 2;
+	char *buffer = kzalloc(ccs_buffer_len, CCS_GFP_FLAGS);
+	char *cp;
+	char *last_start;
+	int len;
+	unsigned long pos = bprm->p;
+	int offset = pos % PAGE_SIZE;
+	int argv_count = bprm->argc;
+	int envp_count = bprm->envc;
+	bool truncated = false;
+	if (!buffer)
+		return NULL;
+	len = snprintf(buffer, ccs_buffer_len - 1, "argv[]={ ");
+	cp = buffer + len;
+	if (!argv_count) {
+		memmove(cp, "} envp[]={ ", 11);
+		cp += 11;
+	}
+	last_start = cp;
+	while (argv_count || envp_count) {
+		if (!ccs_dump_page(bprm, pos, dump))
+			goto out;
+		pos += PAGE_SIZE - offset;
+		/* Read. */
+		while (offset < PAGE_SIZE) {
+			const char *kaddr = dump->data;
+			const unsigned char c = kaddr[offset++];
+			if (cp == last_start)
+				*cp++ = '"';
+			if (cp >= buffer + ccs_buffer_len - 32) {
+				/* Reserve some room for "..." string. */
+				truncated = true;
+			} else if (c == '\\') {
+				*cp++ = '\\';
+				*cp++ = '\\';
+			} else if (c > ' ' && c < 127) {
+				*cp++ = c;
+			} else if (!c) {
+				*cp++ = '"';
+				*cp++ = ' ';
+				last_start = cp;
+			} else {
+				*cp++ = '\\';
+				*cp++ = (c >> 6) + '0';
+				*cp++ = ((c >> 3) & 7) + '0';
+				*cp++ = (c & 7) + '0';
+			}
+			if (c)
+				continue;
+			if (argv_count) {
+				if (--argv_count == 0) {
+					if (truncated) {
+						cp = last_start;
+						memmove(cp, "... ", 4);
+						cp += 4;
+					}
+					memmove(cp, "} envp[]={ ", 11);
+					cp += 11;
+					last_start = cp;
+					truncated = false;
+				}
+			} else if (envp_count) {
+				if (--envp_count == 0) {
+					if (truncated) {
+						cp = last_start;
+						memmove(cp, "... ", 4);
+						cp += 4;
+					}
+				}
+			}
+			if (!argv_count && !envp_count)
+				break;
+		}
+		offset = 0;
+	}
+	*cp++ = '}';
+	*cp = '\0';
+	return buffer;
+out:
+	snprintf(buffer, ccs_buffer_len - 1, "argv[]={ ... } envp[]= { ... }");
+	return buffer;
+}
+
+/**
+ * ccs_filetype - Get string representation of file type.
+ *
+ * @mode: Mode value for stat().
+ *
+ * Returns file type string.
+ */
+static inline const char *ccs_filetype(const umode_t mode)
+{
+	switch (mode & S_IFMT) {
+	case S_IFREG:
+	case 0:
+		return ccs_condition_keyword[CCS_TYPE_IS_FILE];
+	case S_IFDIR:
+		return ccs_condition_keyword[CCS_TYPE_IS_DIRECTORY];
+	case S_IFLNK:
+		return ccs_condition_keyword[CCS_TYPE_IS_SYMLINK];
+	case S_IFIFO:
+		return ccs_condition_keyword[CCS_TYPE_IS_FIFO];
+	case S_IFSOCK:
+		return ccs_condition_keyword[CCS_TYPE_IS_SOCKET];
+	case S_IFBLK:
+		return ccs_condition_keyword[CCS_TYPE_IS_BLOCK_DEV];
+	case S_IFCHR:
+		return ccs_condition_keyword[CCS_TYPE_IS_CHAR_DEV];
+	}
+	return "unknown"; /* This should not happen. */
+}
+
+/**
+ * ccs_print_header - Get header line of audit log.
+ *
+ * @r: Pointer to "struct ccs_request_info".
+ *
+ * Returns string representation.
+ *
+ * This function uses kmalloc(), so caller must kfree() if this function
+ * didn't return NULL.
+ */
+static char *ccs_print_header(struct ccs_request_info *r)
+{
+	struct ccs_time stamp;
+	struct ccs_obj_info *obj = r->obj;
+	const u32 ccs_flags = ccs_current_flags();
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 24)
+	const pid_t gpid = ccs_sys_getpid();
+#else
+	const pid_t gpid = task_pid_nr(current);
+#endif
+	static const int ccs_buffer_len = 4096;
+	char *buffer = kmalloc(ccs_buffer_len, CCS_GFP_FLAGS);
+	int pos;
+	u8 i;
+	if (!buffer)
+		return NULL;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
+	{
+		struct timeval tv;
+		do_gettimeofday(&tv);
+		ccs_convert_time(tv.tv_sec, &stamp);
+	}
+#else
+	ccs_convert_time(get_seconds(), &stamp);
+#endif
+	pos = snprintf(buffer, ccs_buffer_len - 1,
+		       "#%04u/%02u/%02u %02u:%02u:%02u# profile=%u mode=%s "
+		       "granted=%s (global-pid=%u) task={ pid=%u ppid=%u "
+		       "uid=%u gid=%u euid=%u egid=%u suid=%u sgid=%u "
+		       "fsuid=%u fsgid=%u type%s=execute_handler }",
+		       stamp.year, stamp.month, stamp.day, stamp.hour,
+		       stamp.min, stamp.sec, r->profile, ccs_mode[r->mode],
+		       ccs_yesno(r->granted), gpid, ccs_sys_getpid(),
+		       ccs_sys_getppid(),
+		       from_kuid(&init_user_ns, current_uid()),
+		       from_kgid(&init_user_ns, current_gid()),
+		       from_kuid(&init_user_ns, current_euid()),
+		       from_kgid(&init_user_ns, current_egid()),
+		       from_kuid(&init_user_ns, current_suid()),
+		       from_kgid(&init_user_ns, current_sgid()),
+		       from_kuid(&init_user_ns, current_fsuid()),
+		       from_kgid(&init_user_ns, current_fsgid()),
+		       ccs_flags & CCS_TASK_IS_EXECUTE_HANDLER ? "" : "!");
+	if (!obj)
+		goto no_obj_info;
+	if (!obj->validate_done) {
+		ccs_get_attributes(obj);
+		obj->validate_done = true;
+	}
+	for (i = 0; i < CCS_MAX_PATH_STAT; i++) {
+		struct ccs_mini_stat *stat;
+		unsigned int dev;
+		umode_t mode;
+		if (!obj->stat_valid[i])
+			continue;
+		stat = &obj->stat[i];
+		dev = stat->dev;
+		mode = stat->mode;
+		if (i & 1) {
+			pos += snprintf(buffer + pos, ccs_buffer_len - 1 - pos,
+					" path%u.parent={ uid=%u gid=%u "
+					"ino=%lu perm=0%o }", (i >> 1) + 1,
+					from_kuid(&init_user_ns, stat->uid),
+					from_kgid(&init_user_ns, stat->gid),
+					(unsigned long) stat->ino,
+					stat->mode & S_IALLUGO);
+			continue;
+		}
+		pos += snprintf(buffer + pos, ccs_buffer_len - 1 - pos,
+				" path%u={ uid=%u gid=%u ino=%lu major=%u"
+				" minor=%u perm=0%o type=%s", (i >> 1) + 1,
+				from_kuid(&init_user_ns, stat->uid),
+				from_kgid(&init_user_ns, stat->gid),
+				(unsigned long) stat->ino, MAJOR(dev),
+				MINOR(dev), mode & S_IALLUGO,
+				ccs_filetype(mode));
+		if (S_ISCHR(mode) || S_ISBLK(mode)) {
+			dev = stat->rdev;
+			pos += snprintf(buffer + pos, ccs_buffer_len - 1 - pos,
+					" dev_major=%u dev_minor=%u",
+					MAJOR(dev), MINOR(dev));
+		}
+		pos += snprintf(buffer + pos, ccs_buffer_len - 1 - pos, " }");
+	}
+no_obj_info:
+	if (pos < ccs_buffer_len - 1)
+		return buffer;
+	kfree(buffer);
+	return NULL;
+}
+
+/**
+ * ccs_init_log - Allocate buffer for audit logs.
+ *
+ * @r:    Pointer to "struct ccs_request_info".
+ * @len:  Buffer size needed for @fmt and @args.
+ * @fmt:  The printf()'s format string.
+ * @args: va_list structure for @fmt.
+ *
+ * Returns pointer to allocated memory.
+ *
+ * This function uses kzalloc(), so caller must kfree() if this function
+ * didn't return NULL.
+ */
+static char *ccs_init_log(struct ccs_request_info *r, int len, const char *fmt,
+			  va_list args)
+{
+	char *buf = NULL;
+	char *bprm_info = NULL;
+	char *realpath = NULL;
+	const char *symlink = NULL;
+	const char *header = NULL;
+	int pos;
+	const char *domainname = ccs_current_domain()->domainname->name;
+	header = ccs_print_header(r);
+	if (!header)
+		return NULL;
+	/* +10 is for '\n' etc. and '\0'. */
+	len += strlen(domainname) + strlen(header) + 10;
+	if (r->ee) {
+		struct file *file = r->ee->bprm->file;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
+		struct path path = { file->f_vfsmnt, file->f_dentry };
+		realpath = ccs_realpath(&path);
+#else
+		realpath = ccs_realpath(&file->f_path);
+#endif
+		bprm_info = ccs_print_bprm(r->ee->bprm, &r->ee->dump);
+		if (!realpath || !bprm_info)
+			goto out;
+		/* +80 is for " exec={ realpath=\"%s\" argc=%d envc=%d %s }" */
+		len += strlen(realpath) + 80 + strlen(bprm_info);
+	} else if (r->obj && r->obj->symlink_target) {
+		symlink = r->obj->symlink_target->name;
+		/* +18 is for " symlink.target=\"%s\"" */
+		len += 18 + strlen(symlink);
+	}
+	len = ccs_round2(len);
+	buf = kzalloc(len, CCS_GFP_FLAGS);
+	if (!buf)
+		goto out;
+	len--;
+	pos = snprintf(buf, len, "%s", header);
+	if (realpath) {
+		struct linux_binprm *bprm = r->ee->bprm;
+		pos += snprintf(buf + pos, len - pos,
+				" exec={ realpath=\"%s\" argc=%d envc=%d %s }",
+				realpath, bprm->argc, bprm->envc, bprm_info);
+	} else if (symlink)
+		pos += snprintf(buf + pos, len - pos, " symlink.target=\"%s\"",
+				symlink);
+	pos += snprintf(buf + pos, len - pos, "\n%s\n", domainname);
+	vsnprintf(buf + pos, len - pos, fmt, args);
+out:
+	kfree(realpath);
+	kfree(bprm_info);
+	kfree(header);
+	return buf;
+}
+
+/**
+ * ccs_transition_failed - Print waning message and send signal when domain transition failed.
+ *
+ * @domainname: Name of domain to transit.
+ *
+ * Returns nothing.
+ *
+ * Note that if current->pid == 1, sending SIGKILL won't work.
+ */
+void ccs_transition_failed(const char *domainname)
+{
+	printk(KERN_WARNING
+	       "ERROR: Unable to transit to '%s' domain.\n", domainname);
+	force_sig(SIGKILL, current);
+}
+
+/**
+ * ccs_update_task_domain - Update task's domain.
+ *
+ * @r: Pointer to "struct ccs_request_info".
+ *
+ * Returns nothing.
+ *
+ * The task will retry as hard as possible. But if domain transition failed,
+ * the task will be killed by SIGKILL.
+ */
+static void ccs_update_task_domain(struct ccs_request_info *r)
+{
+	char *buf;
+	const char *cp;
+	const struct ccs_acl_info *acl = r->matched_acl;
+	r->matched_acl = NULL;
+	if (!acl || !acl->cond || !acl->cond->transit ||
+	    acl->cond->exec_transit)
+		return;
+	while (1) {
+		buf = kzalloc(CCS_EXEC_TMPSIZE, CCS_GFP_FLAGS);
+		if (buf)
+			break;
+		ssleep(1);
+		if (fatal_signal_pending(current))
+			return;
+	}
+	cp = acl->cond->transit->name;
+	if (*cp == '/')
+		snprintf(buf, CCS_EXEC_TMPSIZE - 1, "%s %s",
+			 ccs_current_domain()->domainname->name, cp);
+	else
+		strncpy(buf, cp, CCS_EXEC_TMPSIZE - 1);
+	if (!ccs_assign_domain(buf, true))
+		ccs_transition_failed(buf);
+	kfree(buf);
+}
+
+/**
+ * ccs_get_audit - Get audit mode.
+ *
+ * @r: Pointer to "struct ccs_request_info".
+ *
+ * Returns true if this request should be audited, false otherwise.
+ */
+static bool ccs_get_audit(const struct ccs_request_info *r)
+{
+	const struct ccs_acl_info *matched_acl = r->matched_acl;
+	const u8 profile = r->profile;
+	const u8 index = r->type;
+	const bool is_granted = r->granted;
+	u8 mode;
+	struct ccs_profile *p;
+	if (!ccs_policy_loaded)
+		return false;
+	p = ccs_profile(profile);
+	if (ccs_log_count >= p->pref[CCS_PREF_MAX_AUDIT_LOG])
+		return false;
+	if (is_granted && matched_acl && matched_acl->cond &&
+	    matched_acl->cond->grant_log != CCS_GRANTLOG_AUTO)
+		return matched_acl->cond->grant_log == CCS_GRANTLOG_YES;
+	mode = p->config[index];
+	if (mode == CCS_CONFIG_USE_DEFAULT)
+		mode = p->config
+			[ccs_index2category[index] + CCS_MAX_MAC_INDEX];
+	if (mode == CCS_CONFIG_USE_DEFAULT)
+		mode = p->default_config;
+	if (is_granted)
+		return mode & CCS_CONFIG_WANT_GRANT_LOG;
+	return mode & CCS_CONFIG_WANT_REJECT_LOG;
+}
+
+/**
+ * ccs_write_log2 - Write an audit log.
+ *
+ * @r:    Pointer to "struct ccs_request_info".
+ * @len:  Buffer size needed for @fmt and @args.
+ * @fmt:  The printf()'s format string.
+ * @args: va_list structure for @fmt.
+ *
+ * Returns nothing.
+ */
+static void ccs_write_log2(struct ccs_request_info *r, int len,
+			   const char *fmt, va_list args)
+{
+	char *buf;
+	struct ccs_log *entry;
+	bool quota_exceeded = false;
+	if (!ccs_get_audit(r))
+		goto out;
+	buf = ccs_init_log(r, len, fmt, args);
+	if (!buf)
+		goto out;
+	entry = kzalloc(sizeof(*entry), CCS_GFP_FLAGS);
+	if (!entry) {
+		kfree(buf);
+		goto out;
+	}
+	entry->log = buf;
+	len = ccs_round2(strlen(buf) + 1);
+	/*
+	 * The entry->size is used for memory quota checks.
+	 * Don't go beyond strlen(entry->log).
+	 */
+	entry->size = len + ccs_round2(sizeof(*entry));
+	spin_lock(&ccs_log_lock);
+	if (ccs_memory_quota[CCS_MEMORY_AUDIT] &&
+	    ccs_memory_used[CCS_MEMORY_AUDIT] + entry->size >=
+	    ccs_memory_quota[CCS_MEMORY_AUDIT]) {
+		quota_exceeded = true;
+	} else {
+		ccs_memory_used[CCS_MEMORY_AUDIT] += entry->size;
+		list_add_tail(&entry->list, &ccs_log);
+		ccs_log_count++;
+	}
+	spin_unlock(&ccs_log_lock);
+	if (quota_exceeded) {
+		kfree(buf);
+		kfree(entry);
+		goto out;
+	}
+	wake_up(&ccs_log_wait);
+out:
+	ccs_update_task_domain(r);
+}
+
+/**
+ * ccs_write_log - Write an audit log.
+ *
+ * @r:   Pointer to "struct ccs_request_info".
+ * @fmt: The printf()'s format string, followed by parameters.
+ *
+ * Returns nothing.
+ */
+void ccs_write_log(struct ccs_request_info *r, const char *fmt, ...)
+{
+	va_list args;
+	int len;
+	va_start(args, fmt);
+	len = vsnprintf((char *) &len, 1, fmt, args) + 1;
+	va_end(args);
+	va_start(args, fmt);
+	ccs_write_log2(r, len, fmt, args);
+	va_end(args);
+}
+
+/**
+ * ccs_read_log - Read an audit log.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ *
+ * Returns nothing.
+ */
+static void ccs_read_log(struct ccs_io_buffer *head)
+{
+	struct ccs_log *ptr = NULL;
+	if (head->r.w_pos)
+		return;
+	kfree(head->read_buf);
+	head->read_buf = NULL;
+	spin_lock(&ccs_log_lock);
+	if (!list_empty(&ccs_log)) {
+		ptr = list_entry(ccs_log.next, typeof(*ptr), list);
+		list_del(&ptr->list);
+		ccs_log_count--;
+		ccs_memory_used[CCS_MEMORY_AUDIT] -= ptr->size;
+	}
+	spin_unlock(&ccs_log_lock);
+	if (ptr) {
+		head->read_buf = ptr->log;
+		head->r.w[head->r.w_pos++] = head->read_buf;
+		kfree(ptr);
+	}
+}
+
+/**
+ * ccs_set_namespace_cursor - Set namespace to read.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ *
+ * Returns nothing.
+ */
+static void ccs_set_namespace_cursor(struct ccs_io_buffer *head)
+{
+	struct list_head *ns;
+	if (head->type != CCS_EXCEPTION_POLICY && head->type != CCS_PROFILE)
+		return;
+	/*
+	 * If this is the first read, or reading previous namespace finished
+	 * and has more namespaces to read, update the namespace cursor.
+	 */
+	ns = head->r.ns;
+	if (!ns || (head->r.eof && ns->next != &ccs_namespace_list)) {
+		/* Clearing is OK because ccs_flush() returned true. */
+		memset(&head->r, 0, sizeof(head->r));
+		head->r.ns = ns ? ns->next : ccs_namespace_list.next;
+	}
+}
+
+/**
+ * ccs_has_more_namespace - Check for unread namespaces.
+ *
+ * @head: Pointer to "struct ccs_io_buffer".
+ *
+ * Returns true if we have more entries to print, false otherwise.
+ */
+static bool ccs_has_more_namespace(struct ccs_io_buffer *head)
+{
+	return (head->type == CCS_EXCEPTION_POLICY ||
+		head->type == CCS_PROFILE) && head->r.eof &&
+		head->r.ns->next != &ccs_namespace_list;
+}
+
+/**
+ * ccs_find_namespace - Find specified namespace.
+ *
+ * @name: Name of namespace to find.
+ * @len:  Length of @name.
+ *
+ * Returns pointer to "struct ccs_policy_namespace" if found, NULL otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static struct ccs_policy_namespace *ccs_find_namespace(const char *name,
+						       const unsigned int len)
+{
+	struct ccs_policy_namespace *ns;
+	list_for_each_entry_srcu(ns, &ccs_namespace_list, namespace_list,
+				 &ccs_ss) {
+		if (strncmp(name, ns->name, len) ||
+		    (name[len] && name[len] != ' '))
+			continue;
+		return ns;
+	}
+	return NULL;
+}
+
+/**
+ * ccs_assign_namespace - Create a new namespace.
+ *
+ * @domainname: Name of namespace to create.
+ *
+ * Returns pointer to "struct ccs_policy_namespace" on success, NULL otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static struct ccs_policy_namespace *ccs_assign_namespace
+(const char *domainname)
+{
+	struct ccs_policy_namespace *ptr;
+	struct ccs_policy_namespace *entry;
+	const char *cp = domainname;
+	unsigned int len = 0;
+	while (*cp && *cp++ != ' ')
+		len++;
+	ptr = ccs_find_namespace(domainname, len);
+	if (ptr)
+		return ptr;
+	if (len >= CCS_EXEC_TMPSIZE - 10 || !ccs_domain_def(domainname))
+		return NULL;
+	entry = kzalloc(sizeof(*entry) + len + 1, CCS_GFP_FLAGS);
+	if (!entry)
+		return NULL;
+	if (mutex_lock_interruptible(&ccs_policy_lock))
+		goto out;
+	ptr = ccs_find_namespace(domainname, len);
+	if (!ptr && ccs_memory_ok(entry, sizeof(*entry) + len + 1)) {
+		char *name = (char *) (entry + 1);
+		ptr = entry;
+		memmove(name, domainname, len);
+		name[len] = '\0';
+		entry->name = name;
+		ccs_init_policy_namespace(entry);
+		entry = NULL;
+	}
+	mutex_unlock(&ccs_policy_lock);
+out:
+	kfree(entry);
+	return ptr;
+}
+
+/**
+ * ccs_namespace_jump - Check for namespace jump.
+ *
+ * @domainname: Name of domain.
+ *
+ * Returns true if namespace differs, false otherwise.
+ */
+static bool ccs_namespace_jump(const char *domainname)
+{
+	const char *namespace = ccs_current_namespace()->name;
+	const int len = strlen(namespace);
+	return strncmp(domainname, namespace, len) ||
+		(domainname[len] && domainname[len] != ' ');
+}
+
+/**
+ * ccs_assign_domain - Create a domain or a namespace.
+ *
+ * @domainname: The name of domain.
+ * @transit:    True if transit to domain found or created.
+ *
+ * Returns pointer to "struct ccs_domain_info" on success, NULL otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+struct ccs_domain_info *ccs_assign_domain(const char *domainname,
+					  const bool transit)
+{
+	struct ccs_security *security = ccs_current_security();
+	struct ccs_domain_info e = { };
+	struct ccs_domain_info *entry = ccs_find_domain(domainname);
+	bool created = false;
+	if (entry) {
+		if (transit) {
+			/*
+			 * Since namespace is created at runtime, profiles may
+			 * not be created by the moment the process transits to
+			 * that domain. Do not perform domain transition if
+			 * profile for that domain is not yet created.
+			 */
+			if (ccs_policy_loaded &&
+			    !entry->ns->profile_ptr[entry->profile])
+				return NULL;
+			security->ccs_domain_info = entry;
+		}
+		return entry;
+	}
+	/* Requested domain does not exist. */
+	/* Don't create requested domain if domainname is invalid. */
+	if (strlen(domainname) >= CCS_EXEC_TMPSIZE - 10 ||
+	    !ccs_correct_domain(domainname))
+		return NULL;
+	/*
+	 * Since definition of profiles and acl_groups may differ across
+	 * namespaces, do not inherit "use_profile" and "use_group" settings
+	 * by automatically creating requested domain upon domain transition.
+	 */
+	if (transit && ccs_namespace_jump(domainname))
+		return NULL;
+	e.ns = ccs_assign_namespace(domainname);
+	if (!e.ns)
+		return NULL;
+	/*
+	 * "use_profile" and "use_group" settings for automatically created
+	 * domains are inherited from current domain. These are 0 for manually
+	 * created domains.
+	 */
+	if (transit) {
+		const struct ccs_domain_info *domain =
+			security->ccs_domain_info;
+		e.profile = domain->profile;
+		memcpy(e.group, domain->group, sizeof(e.group));
+	}
+	e.domainname = ccs_get_name(domainname);
+	if (!e.domainname)
+		return NULL;
+	if (mutex_lock_interruptible(&ccs_policy_lock))
+		goto out;
+	entry = ccs_find_domain(domainname);
+	if (!entry) {
+		entry = ccs_commit_ok(&e, sizeof(e));
+		if (entry) {
+			INIT_LIST_HEAD(&entry->acl_info_list);
+			list_add_tail_rcu(&entry->list, &ccs_domain_list);
+			created = true;
+		}
+	}
+	mutex_unlock(&ccs_policy_lock);
+out:
+	ccs_put_name(e.domainname);
+	if (entry && transit) {
+		security->ccs_domain_info = entry;
+		if (created) {
+			struct ccs_request_info r;
+			int i;
+			ccs_init_request_info(&r, CCS_MAC_FILE_EXECUTE);
+			r.granted = false;
+			ccs_write_log(&r, "use_profile %u\n", entry->profile);
+			for (i = 0; i < CCS_MAX_ACL_GROUPS; i++)
+				if (test_bit(i, entry->group))
+					ccs_write_log(&r, "use_group %u\n", i);
+			ccs_update_stat(CCS_STAT_POLICY_UPDATES);
+		}
+	}
+	return entry;
+}
+
+/**
+ * ccs_parse_policy - Parse a policy line.
+ *
+ * @head: Poiter to "struct ccs_io_buffer".
+ * @line: Line to parse.
+ *
+ * Returns 0 on success, negative value otherwise.
+ *
+ * Caller holds ccs_read_lock().
+ */
+static int ccs_parse_policy(struct ccs_io_buffer *head, char *line)
+{
+	/* Delete request? */
+	head->w.is_delete = !strncmp(line, "delete ", 7);
+	if (head->w.is_delete)
+		memmove(line, line + 7, strlen(line + 7) + 1);
+	/* Selecting namespace to update. */
+	if (head->type == CCS_EXCEPTION_POLICY || head->type == CCS_PROFILE) {
+		if (*line == '<') {
+			char *cp = strchr(line, ' ');
+			if (cp) {
+				*cp++ = '\0';
+				head->w.ns = ccs_assign_namespace(line);
+				memmove(line, cp, strlen(cp) + 1);
+			} else
+				head->w.ns = NULL;
+		} else
+			head->w.ns = &ccs_kernel_namespace;
+		/* Don't allow updating if namespace is invalid. */
+		if (!head->w.ns)
+			return -ENOENT;
+	}
+	/* Do the update. */
+	switch (head->type) {
+	case CCS_DOMAIN_POLICY:
+		return ccs_write_domain(head);
+	case CCS_EXCEPTION_POLICY:
+		return ccs_write_exception(head);
+#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER
+	case CCS_EXECUTE_HANDLER:
+#endif
+	case CCS_PROCESS_STATUS:
+		return ccs_write_pid(head);
+	case CCS_STAT:
+		return ccs_write_stat(head);
+	case CCS_PROFILE:
+		return ccs_write_profile(head);
+	case CCS_QUERY:
+		return ccs_write_answer(head);
+	case CCS_MANAGER:
+		return ccs_write_manager(head);
+	default:
+		return -ENOSYS;
+	}
+}
+
+/**
+ * ccs_policy_io_init - Register hooks for policy I/O.
+ *
+ * Returns nothing.
+ */
+static void __init ccs_policy_io_init(void)
+{
+	ccsecurity_ops.check_profile = ccs_check_profile;
+}
+
+/**
+ * ccs_load_builtin_policy - Load built-in policy.
+ *
+ * Returns nothing.
+ */
+static void __init ccs_load_builtin_policy(void)
+{
+	/*
+	 * This include file is manually created and contains built-in policy
+	 * named "ccs_builtin_profile", "ccs_builtin_exception_policy",
+	 * "ccs_builtin_domain_policy", "ccs_builtin_manager",
+	 * "ccs_builtin_stat" in the form of "static char [] __initdata".
+	 */
+#include "builtin-policy.h"
+	u8 i;
+	const int idx = ccs_read_lock();
+	for (i = 0; i < 5; i++) {
+		struct ccs_io_buffer head = { };
+		char *start = "";
+		switch (i) {
+		case 0:
+			start = ccs_builtin_profile;
+			head.type = CCS_PROFILE;
+			break;
+		case 1:
+			start = ccs_builtin_exception_policy;
+			head.type = CCS_EXCEPTION_POLICY;
+			break;
+		case 2:
+			start = ccs_builtin_domain_policy;
+			head.type = CCS_DOMAIN_POLICY;
+			break;
+		case 3:
+			start = ccs_builtin_manager;
+			head.type = CCS_MANAGER;
+			break;
+		case 4:
+			start = ccs_builtin_stat;
+			head.type = CCS_STAT;
+			break;
+		}
+		while (1) {
+			char *end = strchr(start, '\n');
+			if (!end)
+				break;
+			*end = '\0';
+			ccs_normalize_line(start);
+			head.write_buf = start;
+			ccs_parse_policy(&head, start);
+			start = end + 1;
+		}
+	}
+	ccs_read_unlock(idx);
+#ifdef CONFIG_CCSECURITY_OMIT_USERSPACE_LOADER
+	ccs_check_profile();
+#endif
+}
+
+/**
+ * ccs_read_self - read() for /proc/ccs/self_domain interface.
+ *
+ * @file:  Pointer to "struct file".
+ * @buf:   Domainname which current thread belongs to.
+ * @count: Size of @buf.
+ * @ppos:  Bytes read by now.
+ *
+ * Returns read size on success, negative value otherwise.
+ */
+static ssize_t ccs_read_self(struct file *file, char __user *buf, size_t count,
+			     loff_t *ppos)
+{
+	const char *domain = ccs_current_domain()->domainname->name;
+	loff_t len = strlen(domain);
+	loff_t pos = *ppos;
+	if (pos >= len || !count)
+		return 0;
+	len -= pos;
+	if (count < len)
+		len = count;
+	if (copy_to_user(buf, domain + pos, len))
+		return -EFAULT;
+	*ppos += len;
+	return len;
+}
+
+/**
+ * ccs_open - open() for /proc/ccs/ interface.
+ *
+ * @inode: Pointer to "struct inode".
+ * @file:  Pointer to "struct file".
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int ccs_open(struct inode *inode, struct file *file)
+{
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 10, 0)
+	const u8 type = (unsigned long) PDE_DATA(inode);
+#else
+	const u8 type = (unsigned long) PDE(inode)->data;
+#endif
+	struct ccs_io_buffer *head = kzalloc(sizeof(*head), CCS_GFP_FLAGS);
+	if (!head)
+		return -ENOMEM;
+	mutex_init(&head->io_sem);
+	head->type = type;
+#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER
+	if (type == CCS_EXECUTE_HANDLER) {
+		/* Allow execute_handler to read process's status. */
+		if (!(ccs_current_flags() & CCS_TASK_IS_EXECUTE_HANDLER)) {
+			kfree(head);
+			return -EPERM;
+		}
+	}
+#endif
+	if ((file->f_mode & FMODE_READ) && type != CCS_AUDIT &&
+	    type != CCS_QUERY) {
+		/* Don't allocate read_buf for poll() access. */
+		head->readbuf_size = 4096;
+		head->read_buf = kzalloc(head->readbuf_size, CCS_GFP_FLAGS);
+		if (!head->read_buf) {
+			kfree(head);
+			return -ENOMEM;
+		}
+	}
+	if (file->f_mode & FMODE_WRITE) {
+		head->writebuf_size = 4096;
+		head->write_buf = kzalloc(head->writebuf_size, CCS_GFP_FLAGS);
+		if (!head->write_buf) {
+			kfree(head->read_buf);
+			kfree(head);
+			return -ENOMEM;
+		}
+	}
+	/*
+	 * If the file is /proc/ccs/query, increment the observer counter.
+	 * The obserber counter is used by ccs_supervisor() to see if
+	 * there is some process monitoring /proc/ccs/query.
+	 */
+	if (type == CCS_QUERY)
+		atomic_inc(&ccs_query_observers);
+	file->private_data = head;
+	ccs_notify_gc(head, true);
+	return 0;
+}
+
+/**
+ * ccs_release - close() for /proc/ccs/ interface.
+ *
+ * @inode: Pointer to "struct inode".
+ * @file:  Pointer to "struct file".
+ *
+ * Returns 0.
+ */
+static int ccs_release(struct inode *inode, struct file *file)
+{
+	struct ccs_io_buffer *head = file->private_data;
+	/*
+	 * If the file is /proc/ccs/query, decrement the observer counter.
+	 */
+	if (head->type == CCS_QUERY &&
+	    atomic_dec_and_test(&ccs_query_observers))
+		wake_up_all(&ccs_answer_wait);
+	ccs_notify_gc(head, false);
+	return 0;
+}
+
+/**
+ * ccs_poll - poll() for /proc/ccs/ interface.
+ *
+ * @file: Pointer to "struct file".
+ * @wait: Pointer to "poll_table". Maybe NULL.
+ *
+ * Returns POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM if ready to read/write,
+ * POLLOUT | POLLWRNORM otherwise.
+ */
+static unsigned int ccs_poll(struct file *file, poll_table *wait)
+{
+	struct ccs_io_buffer *head = file->private_data;
+	if (head->type == CCS_AUDIT) {
+		if (!ccs_memory_used[CCS_MEMORY_AUDIT]) {
+			poll_wait(file, &ccs_log_wait, wait);
+			if (!ccs_memory_used[CCS_MEMORY_AUDIT])
+				return POLLOUT | POLLWRNORM;
+		}
+	} else if (head->type == CCS_QUERY) {
+		if (list_empty(&ccs_query_list)) {
+			poll_wait(file, &ccs_query_wait, wait);
+			if (list_empty(&ccs_query_list))
+				return POLLOUT | POLLWRNORM;
+		}
+	}
+	return POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM;
+}
+
+/**
+ * ccs_read - read() for /proc/ccs/ interface.
+ *
+ * @file:  Pointer to "struct file".
+ * @buf:   Pointer to buffer.
+ * @count: Size of @buf.
+ * @ppos:  Unused.
+ *
+ * Returns bytes read on success, negative value otherwise.
+ */
+static ssize_t ccs_read(struct file *file, char __user *buf, size_t count,
+			loff_t *ppos)
+{
+	struct ccs_io_buffer *head = file->private_data;
+	int len;
+	int idx;
+	if (mutex_lock_interruptible(&head->io_sem))
+		return -EINTR;
+	head->read_user_buf = buf;
+	head->read_user_buf_avail = count;
+	idx = ccs_read_lock();
+	if (ccs_flush(head))
+		/* Call the policy handler. */
+		do {
+			ccs_set_namespace_cursor(head);
+			switch (head->type) {
+			case CCS_DOMAIN_POLICY:
+				ccs_read_domain(head);
+				break;
+			case CCS_EXCEPTION_POLICY:
+				ccs_read_exception(head);
+				break;
+			case CCS_AUDIT:
+				ccs_read_log(head);
+				break;
+#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER
+			case CCS_EXECUTE_HANDLER:
+#endif
+			case CCS_PROCESS_STATUS:
+				ccs_read_pid(head);
+				break;
+			case CCS_VERSION:
+				ccs_read_version(head);
+				break;
+			case CCS_STAT:
+				ccs_read_stat(head);
+				break;
+			case CCS_PROFILE:
+				ccs_read_profile(head);
+				break;
+			case CCS_QUERY:
+				ccs_read_query(head);
+				break;
+			case CCS_MANAGER:
+				ccs_read_manager(head);
+				break;
+			}
+		} while (ccs_flush(head) && ccs_has_more_namespace(head));
+	ccs_read_unlock(idx);
+	len = head->read_user_buf - buf;
+	mutex_unlock(&head->io_sem);
+	return len;
+}
+
+#ifdef CONFIG_CCSECURITY_TASK_DOMAIN_TRANSITION
+
+/**
+ * ccs_write_self - write() for /proc/ccs/self_domain interface.
+ *
+ * @file:  Pointer to "struct file".
+ * @buf:   Domainname to transit to.
+ * @count: Size of @buf.
+ * @ppos:  Unused.
+ *
+ * Returns @count on success, negative value otherwise.
+ *
+ * If domain transition was permitted but the domain transition failed, this
+ * function returns error rather than terminating current thread with SIGKILL.
+ */
+static ssize_t ccs_write_self(struct file *file, const char __user *buf,
+			      size_t count, loff_t *ppos)
+{
+	char *data;
+	int error;
+	if (!count || count >= CCS_EXEC_TMPSIZE - 10)
+		return -ENOMEM;
+	data = kzalloc(count + 1, CCS_GFP_FLAGS);
+	if (!data)
+		return -ENOMEM;
+	if (copy_from_user(data, buf, count)) {
+		error = -EFAULT;
+		goto out;
+	}
+	ccs_normalize_line(data);
+	if (ccs_correct_domain(data)) {
+		const int idx = ccs_read_lock();
+		struct ccs_path_info name;
+		struct ccs_request_info r;
+		name.name = data;
+		ccs_fill_path_info(&name);
+		/* Check "task manual_domain_transition" permission. */
+		ccs_init_request_info(&r, CCS_MAC_FILE_EXECUTE);
+		r.param_type = CCS_TYPE_MANUAL_TASK_ACL;
+		r.param.task.domainname = &name;
+		ccs_check_acl(&r);
+		if (!r.granted)
+			error = -EPERM;
+		else
+			error = ccs_assign_domain(data, true) ? 0 : -ENOENT;
+		ccs_read_unlock(idx);
+	} else
+		error = -EINVAL;
+out:
+	kfree(data);
+	return error ? error : count;
+}
+
+#endif
+
+/**
+ * ccs_write - write() for /proc/ccs/ interface.
+ *
+ * @file:  Pointer to "struct file".
+ * @buf:   Pointer to buffer.
+ * @count: Size of @buf.
+ * @ppos:  Unused.
+ *
+ * Returns @count on success, negative value otherwise.
+ */
+static ssize_t ccs_write(struct file *file, const char __user *buf,
+			 size_t count, loff_t *ppos)
+{
+	struct ccs_io_buffer *head = file->private_data;
+	int error = count;
+	char *cp0 = head->write_buf;
+	int idx;
+	if (mutex_lock_interruptible(&head->io_sem))
+		return -EINTR;
+	head->read_user_buf_avail = 0;
+	idx = ccs_read_lock();
+	/* Read a line and dispatch it to the policy handler. */
+	while (count) {
+		char c;
+		if (head->w.avail >= head->writebuf_size - 1) {
+			const int len = head->writebuf_size * 2;
+			char *cp = kzalloc(len, CCS_GFP_FLAGS);
+			if (!cp) {
+				error = -ENOMEM;
+				break;
+			}
+			memmove(cp, cp0, head->w.avail);
+			kfree(cp0);
+			head->write_buf = cp;
+			cp0 = cp;
+			head->writebuf_size = len;
+		}
+		if (get_user(c, buf)) {
+			error = -EFAULT;
+			break;
+		}
+		buf++;
+		count--;
+		cp0[head->w.avail++] = c;
+		if (c != '\n')
+			continue;
+		cp0[head->w.avail - 1] = '\0';
+		head->w.avail = 0;
+		ccs_normalize_line(cp0);
+		if (!strcmp(cp0, "reset")) {
+			head->w.ns = &ccs_kernel_namespace;
+			head->w.domain = NULL;
+			memset(&head->r, 0, sizeof(head->r));
+			continue;
+		}
+		/* Don't allow updating policies by non manager programs. */
+		switch (head->type) {
+		case CCS_PROCESS_STATUS:
+			/* This does not write anything. */
+			break;
+		case CCS_DOMAIN_POLICY:
+			if (ccs_select_domain(head, cp0))
+				continue;
+			/* fall through */
+		case CCS_EXCEPTION_POLICY:
+			if (!strcmp(cp0, "select transition_only")) {
+				head->r.print_transition_related_only = true;
+				continue;
+			}
+			/* fall through */
+		default:
+			if (!ccs_manager()) {
+				error = -EPERM;
+				goto out;
+			}
+		}
+		switch (ccs_parse_policy(head, cp0)) {
+		case -EPERM:
+			error = -EPERM;
+			goto out;
+		case 0:
+			/* Update statistics. */
+			switch (head->type) {
+			case CCS_DOMAIN_POLICY:
+			case CCS_EXCEPTION_POLICY:
+			case CCS_STAT:
+			case CCS_PROFILE:
+			case CCS_MANAGER:
+				ccs_update_stat(CCS_STAT_POLICY_UPDATES);
+				break;
+			default:
+				break;
+			}
+			break;
+		}
+	}
+out:
+	ccs_read_unlock(idx);
+	mutex_unlock(&head->io_sem);
+	return error;
+}
+
+/**
+ * ccs_create_entry - Create interface files under /proc/ccs/ directory.
+ *
+ * @name:   The name of the interface file.
+ * @mode:   The permission of the interface file.
+ * @parent: The parent directory.
+ * @key:    Type of interface.
+ *
+ * Returns nothing.
+ */
+static void __init ccs_create_entry(const char *name, const umode_t mode,
+				    struct proc_dir_entry *parent,
+				    const u8 key)
+{
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)
+	proc_create_data(name, mode, parent, &ccs_operations,
+			 ((u8 *) NULL) + key);
+#else
+	struct proc_dir_entry *entry = create_proc_entry(name, mode, parent);
+	if (entry) {
+		entry->proc_fops = &ccs_operations;
+		entry->data = ((u8 *) NULL) + key;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
+		if (entry->proc_iops)
+			ccs_file_inode_operations = *entry->proc_iops;
+		if (!ccs_file_inode_operations.setattr)
+			ccs_file_inode_operations.setattr = proc_notify_change;
+		entry->proc_iops = &ccs_file_inode_operations;
+#endif
+	}
+#endif
+}
+
+/**
+ * ccs_proc_init - Initialize /proc/ccs/ interface.
+ *
+ * Returns nothing.
+ */
+static void __init ccs_proc_init(void)
+{
+	struct proc_dir_entry *ccs_dir = proc_mkdir("ccs", NULL);
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
+	if (ccs_dir->proc_iops)
+		ccs_dir_inode_operations = *ccs_dir->proc_iops;
+	if (!ccs_dir_inode_operations.setattr)
+		ccs_dir_inode_operations.setattr = proc_notify_change;
+	ccs_dir->proc_iops = &ccs_dir_inode_operations;
+#endif
+	ccs_create_entry("query",            0600, ccs_dir, CCS_QUERY);
+	ccs_create_entry("domain_policy",    0600, ccs_dir, CCS_DOMAIN_POLICY);
+	ccs_create_entry("exception_policy", 0600, ccs_dir,
+			 CCS_EXCEPTION_POLICY);
+	ccs_create_entry("audit",            0400, ccs_dir, CCS_AUDIT);
+	ccs_create_entry(".process_status",  0600, ccs_dir,
+			 CCS_PROCESS_STATUS);
+	ccs_create_entry("stat",             0644, ccs_dir, CCS_STAT);
+	ccs_create_entry("profile",          0600, ccs_dir, CCS_PROFILE);
+	ccs_create_entry("manager",          0600, ccs_dir, CCS_MANAGER);
+	ccs_create_entry("version",          0400, ccs_dir, CCS_VERSION);
+#ifdef CONFIG_CCSECURITY_TASK_EXECUTE_HANDLER
+	ccs_create_entry(".execute_handler", 0666, ccs_dir,
+			 CCS_EXECUTE_HANDLER);
+#endif
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)
+	proc_create("self_domain", 0666, ccs_dir, &ccs_self_operations);
+#else
+	{
+		struct proc_dir_entry *e = create_proc_entry("self_domain",
+							     0666, ccs_dir);
+		if (e)
+			e->proc_fops = &ccs_self_operations;
+	}
+#endif
+}
+
+/**
+ * ccs_init_module - Initialize this module.
+ *
+ * Returns 0 on success, negative value otherwise.
+ */
+static int __init ccs_init_module(void)
+{
+	if (ccsecurity_ops.disabled)
+		return -EINVAL;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 0)
+	MOD_INC_USE_COUNT;
+#endif
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19)
+	if (init_srcu_struct(&ccs_ss))
+		panic("Out of memory.");
+#endif
+	ccs_kernel_namespace.name = "<kernel>";
+	ccs_init_policy_namespace(&ccs_kernel_namespace);
+	ccs_kernel_domain.ns = &ccs_kernel_namespace;
+	INIT_LIST_HEAD(&ccs_kernel_domain.acl_info_list);
+	ccs_mm_init();
+	ccs_policy_io_init();
+	ccs_permission_init();
+	ccs_proc_init();
+	ccs_load_builtin_policy();
+	return 0;
+}
+
+MODULE_LICENSE("GPL");
+module_init(ccs_init_module);
diff --git a/security/ccsecurity/realpath.c b/security/ccsecurity/realpath.c
new file mode 100644
index 0000000..df821ed
--- /dev/null
+++ b/security/ccsecurity/realpath.c
@@ -0,0 +1,767 @@
+/*
+ * security/ccsecurity/realpath.c
+ *
+ * Copyright (C) 2005-2012  NTT DATA CORPORATION
+ *
+ * Version: 1.8.4   2015/05/05
+ */
+
+#include "internal.h"
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36) && LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0)
+#include <linux/nsproxy.h>
+#include <linux/mnt_namespace.h>
+#endif
+
+/***** SECTION1: Constants definition *****/
+
+#define SOCKFS_MAGIC 0x534F434B
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
+#define s_fs_info u.generic_sbp
+#endif
+
+/***** SECTION2: Structure definition *****/
+
+/***** SECTION3: Prototype definition section *****/
+
+char *ccs_encode(const char *str);
+char *ccs_encode2(const char *str, int str_len);
+char *ccs_realpath(const struct path *path);
+const char *ccs_get_exe(void);
+void ccs_fill_path_info(struct ccs_path_info *ptr);
+
+static char *ccs_get_absolute_path(const struct path *path,
+				   char * const buffer, const int buflen);
+static char *ccs_get_dentry_path(struct dentry *dentry, char * const buffer,
+				 const int buflen);
+static char *ccs_get_local_path(struct dentry *dentry, char * const buffer,
+				const int buflen);
+static char *ccs_get_socket_name(const struct path *path, char * const buffer,
+				 const int buflen);
+static int ccs_const_part_length(const char *filename);
+
+/***** SECTION4: Standalone functions section *****/
+
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 5, 0)
+
+/**
+ * SOCKET_I - Get "struct socket".
+ *
+ * @inode: Pointer to "struct inode".
+ *
+ * Returns pointer to "struct socket".
+ *
+ * This is for compatibility with older kernels.
+ */
+static inline struct socket *SOCKET_I(struct inode *inode)
+{
+	return inode->i_sock ? &inode->u.socket_i : NULL;
+}
+
+#endif
+
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 37)
+
+/**
+ * ccs_realpath_lock - Take locks for __d_path().
+ *
+ * Returns nothing.
+ */
+static inline void ccs_realpath_lock(void)
+{
+	/* dcache_lock is locked by __d_path(). */
+	/* vfsmount_lock is locked by __d_path(). */
+}
+
+/**
+ * ccs_realpath_unlock - Release locks for __d_path().
+ *
+ * Returns nothing.
+ */
+static inline void ccs_realpath_unlock(void)
+{
+	/* vfsmount_lock is unlocked by __d_path(). */
+	/* dcache_lock is unlocked by __d_path(). */
+}
+
+#elif LINUX_VERSION_CODE == KERNEL_VERSION(2, 6, 36)
+
+/**
+ * ccs_realpath_lock - Take locks for __d_path().
+ *
+ * Returns nothing.
+ */
+static inline void ccs_realpath_lock(void)
+{
+	spin_lock(&dcache_lock);
+	/* vfsmount_lock is locked by __d_path(). */
+}
+
+/**
+ * ccs_realpath_unlock - Release locks for __d_path().
+ *
+ * Returns nothing.
+ */
+static inline void ccs_realpath_unlock(void)
+{
+	/* vfsmount_lock is unlocked by __d_path(). */
+	spin_unlock(&dcache_lock);
+}
+
+#elif defined(D_PATH_DISCONNECT) && !defined(CONFIG_SUSE_KERNEL)
+
+/**
+ * ccs_realpath_lock - Take locks for __d_path().
+ *
+ * Returns nothing.
+ *
+ * Original unambiguous-__d_path.diff in patches.apparmor.tar.bz2 inversed the
+ * order of holding dcache_lock and vfsmount_lock. That patch was applied on
+ * (at least) SUSE 11.1 and Ubuntu 8.10 and Ubuntu 9.04 kernels.
+ *
+ * However, that patch was updated to use original order and the updated patch
+ * is applied to (as far as I know) only SUSE kernels.
+ *
+ * Therefore, I need to use original order for SUSE 11.1 kernels and inversed
+ * order for other kernels. I detect it by checking D_PATH_DISCONNECT and
+ * CONFIG_SUSE_KERNEL. I don't know whether other distributions are using the
+ * updated patch or not. If you got deadlock, check fs/dcache.c for locking
+ * order, and add " && 0" to this "#elif " block if fs/dcache.c uses original
+ * order.
+ */
+static inline void ccs_realpath_lock(void)
+{
+	spin_lock(ccsecurity_exports.vfsmount_lock);
+	spin_lock(&dcache_lock);
+}
+
+/**
+ * ccs_realpath_unlock - Release locks for __d_path().
+ *
+ * Returns nothing.
+ */
+static inline void ccs_realpath_unlock(void)
+{
+	spin_unlock(&dcache_lock);
+	spin_unlock(ccsecurity_exports.vfsmount_lock);
+}
+
+#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 0)
+
+/**
+ * ccs_realpath_lock - Take locks for __d_path().
+ *
+ * Returns nothing.
+ */
+static inline void ccs_realpath_lock(void)
+{
+	spin_lock(&dcache_lock);
+	spin_lock(ccsecurity_exports.vfsmount_lock);
+}
+
+/**
+ * ccs_realpath_unlock - Release locks for __d_path().
+ *
+ * Returns nothing.
+ */
+static inline void ccs_realpath_unlock(void)
+{
+	spin_unlock(ccsecurity_exports.vfsmount_lock);
+	spin_unlock(&dcache_lock);
+}
+
+#else
+
+/**
+ * ccs_realpath_lock - Take locks for __d_path().
+ *
+ * Returns nothing.
+ */
+static inline void ccs_realpath_lock(void)
+{
+	spin_lock(&dcache_lock);
+}
+
+/**
+ * ccs_realpath_unlock - Release locks for __d_path().
+ *
+ * Returns nothing.
+ */
+static inline void ccs_realpath_unlock(void)
+{
+	spin_unlock(&dcache_lock);
+}
+
+#endif
+
+/***** SECTION5: Variables definition section *****/
+
+/***** SECTION6: Dependent functions section *****/
+
+/**
+ * ccs_get_absolute_path - Get the path of a dentry but ignores chroot'ed root.
+ *
+ * @path:   Pointer to "struct path".
+ * @buffer: Pointer to buffer to return value in.
+ * @buflen: Sizeof @buffer.
+ *
+ * Returns the buffer on success, an error code otherwise.
+ *
+ * Caller holds the dcache_lock and vfsmount_lock.
+ * Based on __d_path() in fs/dcache.c
+ *
+ * If dentry is a directory, trailing '/' is appended.
+ */
+static char *ccs_get_absolute_path(const struct path *path,
+				   char * const buffer, const int buflen)
+{
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(3, 2, 0)
+	char *pos = ERR_PTR(-ENOMEM);
+	if (buflen >= 256) {
+		pos = ccsecurity_exports.d_absolute_path(path, buffer,
+							 buflen - 1);
+		if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
+			struct inode *inode = path->dentry->d_inode;
+			if (inode && S_ISDIR(inode->i_mode)) {
+				buffer[buflen - 2] = '/';
+				buffer[buflen - 1] = '\0';
+			}
+		}
+	}
+	return pos;
+#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 36)
+	/*
+	 * __d_path() will start returning NULL by backporting commit 02125a82
+	 * "fix apparmor dereferencing potentially freed dentry, sanitize
+	 * __d_path() API".
+	 *
+	 * Unfortunately, __d_path() after applying that commit always returns
+	 * NULL when root is empty. d_absolute_path() is provided for TOMOYO
+	 * 2.x and AppArmor but TOMOYO 1.x does not use it, for TOMOYO 1.x
+	 * might be built as a loadable kernel module and there is no warrantee
+	 * that TOMOYO 1.x is recompiled after applying that commit. Also,
+	 * I don't want to search /proc/kallsyms for d_absolute_path() because
+	 * I want to keep TOMOYO 1.x architecture independent. Thus, supply
+	 * non empty root like AppArmor's d_namespace_path() did.
+	 */
+	char *pos = ERR_PTR(-ENOMEM);
+	if (buflen >= 256) {
+		static bool ccs_no_empty;
+		if (!ccs_no_empty) {
+			struct path root = { };
+			pos = ccsecurity_exports.__d_path(path, &root, buffer,
+							  buflen - 1);
+		} else {
+			pos = NULL;
+		}
+		if (!pos) {
+			struct task_struct *task = current;
+			struct path root;
+			struct path tmp;
+			spin_lock(&task->fs->lock);
+			root.mnt = task->nsproxy->mnt_ns->root;
+			root.dentry = root.mnt->mnt_root;
+			path_get(&root);
+			spin_unlock(&task->fs->lock);
+			tmp = root;
+			pos = ccsecurity_exports.__d_path(path, &tmp, buffer,
+							  buflen - 1);
+			path_put(&root);
+			if (!pos)
+				return ERR_PTR(-EINVAL);
+			/* Remember if __d_path() needs non empty root. */
+			ccs_no_empty = true;
+		}
+		if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
+			struct inode *inode = path->dentry->d_inode;
+			if (inode && S_ISDIR(inode->i_mode)) {
+				buffer[buflen - 2] = '/';
+				buffer[buflen - 1] = '\0';
+			}
+		}
+	}
+	return pos;
+#else
+	char *pos = buffer + buflen - 1;
+	struct dentry *dentry = path->dentry;
+	struct vfsmount *vfsmnt = path->mnt;
+	const char *name;
+	int len;
+
+	if (buflen < 256)
+		goto out;
+
+	*pos = '\0';
+	if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
+		*--pos = '/';
+	for (;;) {
+		struct dentry *parent;
+		if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
+			if (vfsmnt->mnt_parent == vfsmnt)
+				break;
+			dentry = vfsmnt->mnt_mountpoint;
+			vfsmnt = vfsmnt->mnt_parent;
+			continue;
+		}
+		parent = dentry->d_parent;
+		name = dentry->d_name.name;
+		len = dentry->d_name.len;
+		pos -= len;
+		if (pos <= buffer)
+			goto out;
+		memmove(pos, name, len);
+		*--pos = '/';
+		dentry = parent;
+	}
+	if (*pos == '/')
+		pos++;
+	len = dentry->d_name.len;
+	pos -= len;
+	if (pos < buffer)
+		goto out;
+	memmove(pos, dentry->d_name.name, len);
+	return pos;
+out:
+	return ERR_PTR(-ENOMEM);
+#endif
+}
+
+/**
+ * ccs_get_dentry_path - Get the path of a dentry.
+ *
+ * @dentry: Pointer to "struct dentry".
+ * @buffer: Pointer to buffer to return value in.
+ * @buflen: Sizeof @buffer.
+ *
+ * Returns the buffer on success, an error code otherwise.
+ *
+ * Based on dentry_path() in fs/dcache.c
+ *
+ * If dentry is a directory, trailing '/' is appended.
+ */
+static char *ccs_get_dentry_path(struct dentry *dentry, char * const buffer,
+				 const int buflen)
+{
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 1, 0)
+	char *pos = ERR_PTR(-ENOMEM);
+	if (buflen >= 256) {
+		pos = dentry_path_raw(dentry, buffer, buflen - 1);
+		if (!IS_ERR(pos) && *pos == '/' && pos[1] &&
+		    d_is_dir(dentry)) {
+			buffer[buflen - 2] = '/';
+			buffer[buflen - 1] = '\0';
+		}
+	}
+	return pos;
+#elif LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 38)
+	char *pos = ERR_PTR(-ENOMEM);
+	if (buflen >= 256) {
+		/* rename_lock is locked/unlocked by dentry_path_raw(). */
+		pos = dentry_path_raw(dentry, buffer, buflen - 1);
+		if (!IS_ERR(pos) && *pos == '/' && pos[1]) {
+			struct inode *inode = dentry->d_inode;
+			if (inode && S_ISDIR(inode->i_mode)) {
+				buffer[buflen - 2] = '/';
+				buffer[buflen - 1] = '\0';
+			}
+		}
+	}
+	return pos;
+#else
+	char *pos = buffer + buflen - 1;
+	if (buflen < 256)
+		return ERR_PTR(-ENOMEM);
+	*pos = '\0';
+	if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
+		*--pos = '/';
+	spin_lock(&dcache_lock);
+	while (!IS_ROOT(dentry)) {
+		struct dentry *parent = dentry->d_parent;
+		const char *name = dentry->d_name.name;
+		const int len = dentry->d_name.len;
+		pos -= len;
+		if (pos <= buffer) {
+			pos = ERR_PTR(-ENOMEM);
+			break;
+		}
+		memmove(pos, name, len);
+		*--pos = '/';
+		dentry = parent;
+	}
+	spin_unlock(&dcache_lock);
+	return pos;
+#endif
+}
+
+/**
+ * ccs_get_local_path - Get the path of a dentry.
+ *
+ * @dentry: Pointer to "struct dentry".
+ * @buffer: Pointer to buffer to return value in.
+ * @buflen: Sizeof @buffer.
+ *
+ * Returns the buffer on success, an error code otherwise.
+ */
+static char *ccs_get_local_path(struct dentry *dentry, char * const buffer,
+				const int buflen)
+{
+	struct super_block *sb = dentry->d_sb;
+	char *pos = ccs_get_dentry_path(dentry, buffer, buflen);
+	if (IS_ERR(pos))
+		return pos;
+	/* Convert from $PID to self if $PID is current thread. */
+	if (sb->s_magic == PROC_SUPER_MAGIC && *pos == '/') {
+		char *ep;
+		const pid_t pid = (pid_t) simple_strtoul(pos + 1, &ep, 10);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25)
+		if (*ep == '/' && pid && pid ==
+		    task_tgid_nr_ns(current, sb->s_fs_info)) {
+			pos = ep - 5;
+			if (pos < buffer)
+				goto out;
+			memmove(pos, "/self", 5);
+		}
+#else
+		if (*ep == '/' && pid == ccs_sys_getpid()) {
+			pos = ep - 5;
+			if (pos < buffer)
+				goto out;
+			memmove(pos, "/self", 5);
+		}
+#endif
+		goto prepend_filesystem_name;
+	}
+	/* Use filesystem name for unnamed devices. */
+	if (!MAJOR(sb->s_dev))
+		goto prepend_filesystem_name;
+	{
+		struct inode *inode = sb->s_root->d_inode;
+		/*
+		 * Use filesystem name if filesystems does not support rename()
+		 * operation.
+		 */
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0)
+		if (inode->i_op && !inode->i_op->rename)
+			goto prepend_filesystem_name;
+#else
+		if (!inode->i_op->rename && !inode->i_op->rename2)
+			goto prepend_filesystem_name;
+#endif
+	}
+	/* Prepend device name. */
+	{
+		char name[64];
+		int name_len;
+		const dev_t dev = sb->s_dev;
+		name[sizeof(name) - 1] = '\0';
+		snprintf(name, sizeof(name) - 1, "dev(%u,%u):", MAJOR(dev),
+			 MINOR(dev));
+		name_len = strlen(name);
+		pos -= name_len;
+		if (pos < buffer)
+			goto out;
+		memmove(pos, name, name_len);
+		return pos;
+	}
+	/* Prepend filesystem name. */
+prepend_filesystem_name:
+	{
+		const char *name = sb->s_type->name;
+		const int name_len = strlen(name);
+		pos -= name_len + 1;
+		if (pos < buffer)
+			goto out;
+		memmove(pos, name, name_len);
+		pos[name_len] = ':';
+	}
+	return pos;
+out:
+	return ERR_PTR(-ENOMEM);
+}
+
+/**
+ * ccs_get_socket_name - Get the name of a socket.
+ *
+ * @path:   Pointer to "struct path".
+ * @buffer: Pointer to buffer to return value in.
+ * @buflen: Sizeof @buffer.
+ *
+ * Returns the buffer.
+ */
+static char *ccs_get_socket_name(const struct path *path, char * const buffer,
+				 const int buflen)
+{
+	struct inode *inode = path->dentry->d_inode;
+	struct socket *sock = inode ? SOCKET_I(inode) : NULL;
+	struct sock *sk = sock ? sock->sk : NULL;
+	if (sk) {
+		snprintf(buffer, buflen, "socket:[family=%u:type=%u:"
+			 "protocol=%u]", sk->sk_family, sk->sk_type,
+			 sk->sk_protocol);
+	} else {
+		snprintf(buffer, buflen, "socket:[unknown]");
+	}
+	return buffer;
+}
+
+#define SOCKFS_MAGIC 0x534F434B
+
+/**
+ * ccs_realpath - Returns realpath(3) of the given pathname but ignores chroot'ed root.
+ *
+ * @path: Pointer to "struct path".
+ *
+ * Returns the realpath of the given @path on success, NULL otherwise.
+ *
+ * This function uses kzalloc(), so caller must kfree() if this function
+ * didn't return NULL.
+ */
+char *ccs_realpath(const struct path *path)
+{
+	char *buf = NULL;
+	char *name = NULL;
+	unsigned int buf_len = PAGE_SIZE / 2;
+	struct dentry *dentry = path->dentry;
+	struct super_block *sb;
+	if (!dentry)
+		return NULL;
+	sb = dentry->d_sb;
+	while (1) {
+		char *pos;
+		struct inode *inode;
+		buf_len <<= 1;
+		kfree(buf);
+		buf = kmalloc(buf_len, CCS_GFP_FLAGS);
+		if (!buf)
+			break;
+		/* To make sure that pos is '\0' terminated. */
+		buf[buf_len - 1] = '\0';
+		/* Get better name for socket. */
+		if (sb->s_magic == SOCKFS_MAGIC) {
+			pos = ccs_get_socket_name(path, buf, buf_len - 1);
+			goto encode;
+		}
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 22)
+		/* For "pipe:[\$]". */
+		if (dentry->d_op && dentry->d_op->d_dname) {
+			pos = dentry->d_op->d_dname(dentry, buf, buf_len - 1);
+			goto encode;
+		}
+#endif
+		inode = sb->s_root->d_inode;
+		/*
+		 * Use local name for "filesystems without rename() operation"
+		 * or "path without vfsmount" or "absolute name is unavailable"
+		 * cases.
+		 */
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 17, 0)
+		if (!path->mnt || (inode->i_op && !inode->i_op->rename))
+			pos = ERR_PTR(-EINVAL);
+		else {
+			/* Get absolute name for the rest. */
+			ccs_realpath_lock();
+			pos = ccs_get_absolute_path(path, buf, buf_len - 1);
+			ccs_realpath_unlock();
+		}
+		if (pos == ERR_PTR(-EINVAL))
+			pos = ccs_get_local_path(path->dentry, buf,
+						 buf_len - 1);
+#else
+		if (!path->mnt ||
+		    (!inode->i_op->rename && !inode->i_op->rename2))
+			pos = ccs_get_local_path(path->dentry, buf,
+						 buf_len - 1);
+		else
+			pos = ccs_get_absolute_path(path, buf, buf_len - 1);
+#endif
+encode:
+		if (IS_ERR(pos))
+			continue;
+		name = ccs_encode(pos);
+		break;
+	}
+	kfree(buf);
+	if (!name)
+		ccs_warn_oom(__func__);
+	return name;
+}
+
+/**
+ * ccs_encode2 - Encode binary string to ascii string.
+ *
+ * @str:     String in binary format.
+ * @str_len: Size of @str in byte.
+ *
+ * Returns pointer to @str in ascii format on success, NULL otherwise.
+ *
+ * This function uses kzalloc(), so caller must kfree() if this function
+ * didn't return NULL.
+ */
+char *ccs_encode2(const char *str, int str_len)
+{
+	int i;
+	int len = 0;
+	const char *p = str;
+	char *cp;
+	char *cp0;
+	if (!p)
+		return NULL;
+	for (i = 0; i < str_len; i++) {
+		const unsigned char c = p[i];
+		if (c == '\\')
+			len += 2;
+		else if (c > ' ' && c < 127)
+			len++;
+		else
+			len += 4;
+	}
+	len++;
+	/* Reserve space for appending "/". */
+	cp = kzalloc(len + 10, CCS_GFP_FLAGS);
+	if (!cp)
+		return NULL;
+	cp0 = cp;
+	p = str;
+	for (i = 0; i < str_len; i++) {
+		const unsigned char c = p[i];
+		if (c == '\\') {
+			*cp++ = '\\';
+			*cp++ = '\\';
+		} else if (c > ' ' && c < 127) {
+			*cp++ = c;
+		} else {
+			*cp++ = '\\';
+			*cp++ = (c >> 6) + '0';
+			*cp++ = ((c >> 3) & 7) + '0';
+			*cp++ = (c & 7) + '0';
+		}
+	}
+	return cp0;
+}
+
+/**
+ * ccs_encode - Encode binary string to ascii string.
+ *
+ * @str: String in binary format.
+ *
+ * Returns pointer to @str in ascii format on success, NULL otherwise.
+ *
+ * This function uses kzalloc(), so caller must kfree() if this function
+ * didn't return NULL.
+ */
+char *ccs_encode(const char *str)
+{
+	return str ? ccs_encode2(str, strlen(str)) : NULL;
+}
+
+/**
+ * ccs_const_part_length - Evaluate the initial length without a pattern in a token.
+ *
+ * @filename: The string to evaluate.
+ *
+ * Returns the initial length without a pattern in @filename.
+ */
+static int ccs_const_part_length(const char *filename)
+{
+	char c;
+	int len = 0;
+	if (!filename)
+		return 0;
+	while (1) {
+		c = *filename++;
+		if (!c)
+			break;
+		if (c != '\\') {
+			len++;
+			continue;
+		}
+		c = *filename++;
+		switch (c) {
+		case '\\':  /* "\\" */
+			len += 2;
+			continue;
+		case '0':   /* "\ooo" */
+		case '1':
+		case '2':
+		case '3':
+			c = *filename++;
+			if (c < '0' || c > '7')
+				break;
+			c = *filename++;
+			if (c < '0' || c > '7')
+				break;
+			len += 4;
+			continue;
+		}
+		break;
+	}
+	return len;
+}
+
+/**
+ * ccs_fill_path_info - Fill in "struct ccs_path_info" members.
+ *
+ * @ptr: Pointer to "struct ccs_path_info" to fill in.
+ *
+ * The caller sets "struct ccs_path_info"->name.
+ */
+void ccs_fill_path_info(struct ccs_path_info *ptr)
+{
+	const char *name = ptr->name;
+	const int len = strlen(name);
+	ptr->total_len = len;
+	ptr->const_len = ccs_const_part_length(name);
+	ptr->is_dir = len && (name[len - 1] == '/');
+	ptr->is_patterned = (ptr->const_len < len);
+	ptr->hash = full_name_hash(name, len);
+}
+
+/**
+ * ccs_get_exe - Get ccs_realpath() of current process.
+ *
+ * Returns the ccs_realpath() of current process on success, NULL otherwise.
+ *
+ * This function uses kzalloc(), so the caller must kfree()
+ * if this function didn't return NULL.
+ */
+const char *ccs_get_exe(void)
+{
+	struct mm_struct *mm = current->mm;
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
+	struct vm_area_struct *vma;
+#endif
+#if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
+	struct path path;
+#endif
+	struct file *exe_file = NULL;
+	const char *cp;
+	if (!mm)
+		return NULL;
+	down_read(&mm->mmap_sem);
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)
+	/* Not using get_mm_exe_file() as it is not exported. */
+	exe_file = mm->exe_file;
+#else
+	for (vma = mm->mmap; vma; vma = vma->vm_next) {
+		if ((vma->vm_flags & VM_EXECUTABLE) && vma->vm_file) {
+			exe_file = vma->vm_file;
+			break;
+		}
+	}
+#endif
+	if (exe_file)
+		get_file(exe_file);
+	up_read(&mm->mmap_sem);
+	if (!exe_file)
+		return NULL;
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20)
+	cp = ccs_realpath(&exe_file->f_path);
+#else
+	path.mnt = exe_file->f_vfsmnt;
+	path.dentry = exe_file->f_dentry;
+	cp = ccs_realpath(&path);
+#endif
+	fput(exe_file);
+	return cp;
+}
-- 
1.9.1