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
|
/* Copyright (C) 2001-2004 Tresys Technology, LLC
* see file 'COPYING' for use and warranty information */
/*
* Author: mayerf@tresys.com
*/
/* skeleton and structure "borrowed" from SE Linux module\selinux_plug\ss\policy.parse.y */
/* nearly all the logic is new and specific to policy analysis */
/* Keeping track of policy version. We try to maintain some backwards
* comptability so that we can use this libaray with old and new policies.
* This is difficult since changes are occuring regular to the syntax!
* Below is our record of the versions we track, and what we think distinguishes them.
* This is not a complete version determiner; just key issues.
*
* Policy Version 18 (POL_VER_18)
* Added new netlink permission
*
* Policy Version 17 (POL_VER_17)
* Added support for IPv6
*
* Policy Version 16 (POL_VER_16):
* Added conditional policy syntax
* if-else statement
* bool declarations
*
* Policy Version 15 (POL_VER_15):
* Added FSUSEXATTR
*
* Pre Jul 2002 (POL_VER_11 & POL_VER_12):
*
* devfs_context syntax
* clone rule
* notify rule (though never used and we don't keep it around)
* fs_context without FSCON keyword
* port context without PORTCON keyword
* net if context without NETIFCON keyword
* node context without NODECON keyword
* type attributes wihtout ATTRIBUTE keyword
*
* As of Jul 2002:
*
* Type Attributes declared via ATTRIBUTE keyword
* clone rule not supported
* notify rule not supported
* added dontaudit rule
* fs_uses added
* gen_fs (GENFSCON) added; devfs_context removed
* PORTCON, FSCON, NETIFCON, and NODECON added, and previous
* formats removed
*
*/
%{
#include <stdio.h>
#include "queue.h"
#include "policy.h"
#include "cond.h"
#include <assert.h>
queue_t id_queue = 0;
unsigned int pass;
/* our GLOBAL policy structure */
policy_t *parse_policy = NULL;
/* from originial constraint.h */
/* Needed by apolicy_parse.y for "borrowed" code */
typedef struct constraint_expr {
#define CEXPR_NOT 1 /* not expr */
#define CEXPR_AND 2 /* expr and expr */
#define CEXPR_OR 3 /* expr or expr */
#define CEXPR_ATTR 4 /* attr op attr */
#define CEXPR_NAMES 5 /* attr op names */
__u32 expr_type; /* expression type */
#define CEXPR_USER 1 /* user */
#define CEXPR_ROLE 2 /* role */
#define CEXPR_TYPE 4 /* type */
#define CEXPR_TARGET 8 /* target if set, source otherwise */
__u32 attr; /* attribute */
#define CEXPR_EQ 1 /* == or eq */
#define CEXPR_NEQ 2 /* != */
#define CEXPR_DOM 3 /* dom */
#define CEXPR_DOMBY 4 /* domby */
#define CEXPR_INCOMP 5 /* incomp */
__u32 op; /* operator */
/* ebitmap_t names;*/ /* names */
struct constraint_expr *left;
struct constraint_expr *right;
__u32 count; /* reference count */
} constraint_expr_t;
/* end from constraint.h */
/* this is for passing around a rule (used in the conditional
* policy support.
*/
typedef struct rule_desc {
int rule_type;
int idx;
} rule_desc_t;
/* used to signify non-error but empty return */
static rule_desc_t dummy_rule_desc;
static cond_expr_t dummy_cond_expr;
extern char yytext[];
extern int yywarn(char *msg);
extern int yyerror(char *msg);
static char errormsg[255];
extern unsigned long policydb_lineno;
int yylex(void);
static int insert_separator(int push);
static int insert_id(char *id,int push);
static int define_class(void);
static int define_initial_sid(void);
static int define_common_perms(void);
static int define_av_perms(int inherits);
static int define_sens(void);
static int define_dominance(void);
static int define_category(void);
static int define_level(void);
static int define_common_base(void);
static int define_av_base(void);
static int define_attrib(void);
static int define_typeattribute(void);
static int define_typealias(void);
static int define_type(int alias);
static int define_compute_type(int rule_type);
static int define_te_clone(void);
static int define_te_avtab(int rule_type);
static int define_role_types(void);
/*static role_datum_t *merge_roles_dom(role_datum_t *r1,role_datum_t *r2);*/
static int define_role_dom(void);
static int define_role_trans(void);
static int define_role_allow(void);
static int define_constraint(void);
static constraint_expr_t *define_cexpr(__u32 expr_type, __u32 arg1, __u32 arg2);
static int define_user(void);
static security_con_t *parse_security_context(int dontsave);
static int define_initial_sid_context(void);
static int define_devfs_context(int has_type);
static int define_fs_context(int ver);
static int define_port_context(int ver);
static int define_netif_context(int ver);
static int define_ipv4_node_context(int ver);
static int define_ipv6_node_context(int ver);
static int define_fs_use(int behavior, int ver);
static int define_genfs_context(int has_type);
static int define_nfs_context(void);
static int define_bool(void);
static int define_conditional(cond_expr_t *expr, cond_rule_list_t *t_list, cond_rule_list_t *f_list);
static cond_expr_t *define_cond_expr(__u32 expr_type, void *arg1, void *arg2);
static cond_rule_list_t *define_cond_pol_list(cond_rule_list_t *list, rule_desc_t *rule);
static rule_desc_t *define_cond_compute_type(int rule_type);
static rule_desc_t *define_cond_te_avtab(int rule_type);
%}
%union {
int sval;
unsigned int val;
unsigned int *valptr;
void *ptr;
}
%type <ptr> cond_expr cond_expr_prim cond_pol_list
%type <ptr> cond_allow_def cond_auditallow_def cond_auditdeny_def cond_dontaudit_def
%type <ptr> cond_transition_def cond_te_avtab_def cond_rule_def
%type <sval> role_def roles
%type <sval> cexpr cexpr_prim op roleop
%type <val> ipv4_addr_def number
%token PATH
%token CLONE
%token COMMON
%token CLASS
%token CONSTRAIN
%token INHERITS
%token SID
%token ROLE
%token ROLES
%token TYPEATTRIBUTE
%token TYPEALIAS
%token TYPE
%token TYPES
%token ALIAS
%token ATTRIBUTE
%token BOOL
%token IF
%token ELSE
%token TYPE_TRANSITION
%token TYPE_MEMBER
%token TYPE_CHANGE
%token ROLE_TRANSITION
%token SENSITIVITY
%token DOMINANCE
%token DOM DOMBY INCOMP
%token CATEGORY
%token LEVEL
%token RANGES
%token USER
%token NEVERALLOW
%token ALLOW
%token AUDITALLOW
%token AUDITDENY
%token DONTAUDIT
%token SOURCE
%token TARGET
%token SAMEUSER
%token FSCON PORTCON NETIFCON NODECON
%token FSUSEPSID FSUSETASK FSUSETRANS FSUSEXATTR
%token GENFSCON
%token U1 U2 R1 R2 T1 T2
%token NOT AND OR XOR
%token CTRUE CFALSE
%token IDENTIFIER
%token NUMBER
%token EQUALS
%token NOTEQUAL
%token IPV6_ADDR
%left OR
%left XOR
%left AND
%right NOT
%left EQUALS NOTEQUAL
%%
policy : classes initial_sids access_vectors
{ /*do nothing */ }
opt_mls te_rbac users opt_constraints
{ /*do nothing */ }
initial_sid_contexts
{ /*do nothing*/}
policy_version_contexts
{ /* determine which policy version and
branch accordingly */ }
;
classes : class_def
| classes class_def
;
class_def : CLASS identifier
{if (define_class()) return -1;}
;
initial_sids : initial_sid_def
| initial_sids initial_sid_def
;
initial_sid_def : SID identifier
{if (define_initial_sid()) return -1;}
;
access_vectors : opt_common_perms av_perms
;
/* added Jul 2002 */
opt_common_perms : common_perms
|
;
common_perms : common_perms_def
| common_perms common_perms_def
;
common_perms_def : COMMON identifier '{' identifier_list '}'
{if (define_common_perms()) return -1;}
;
av_perms : av_perms_def
| av_perms av_perms_def
;
av_perms_def : CLASS identifier '{' identifier_list '}'
{if (define_av_perms(FALSE)) return -1;}
| CLASS identifier INHERITS identifier
{if (define_av_perms(TRUE)) return -1;}
| CLASS identifier INHERITS identifier '{' identifier_list '}'
{if (define_av_perms(TRUE)) return -1;}
;
opt_mls : mls
|
;
mls : sensitivities dominance opt_categories levels base_perms
;
sensitivities : sensitivity_def
| sensitivities sensitivity_def
;
sensitivity_def : SENSITIVITY identifier alias_def ';'
{if (define_sens()) return -1;}
| SENSITIVITY identifier ';'
{if (define_sens()) return -1;}
;
alias_def : ALIAS names
;
dominance : DOMINANCE identifier
{if (define_dominance()) return -1;}
| DOMINANCE '{' identifier_list '}'
{if (define_dominance()) return -1;}
;
/* added Jul 2002 */
opt_categories : categories
|
;
categories : category_def
| categories category_def
;
category_def : CATEGORY identifier alias_def ';'
{if (define_category()) return -1;}
| CATEGORY identifier ';'
{if (define_category()) return -1;}
;
levels : level_def
| levels level_def
;
level_def : LEVEL identifier ':' id_comma_list ';'
{if (define_level()) return -1;}
| LEVEL identifier ';'
{if (define_level()) return -1;}
;
base_perms : opt_common_base av_base
;
/* added Jul 2002 */
opt_common_base : common_base
|
;
common_base : common_base_def
| common_base common_base_def
;
common_base_def : COMMON identifier '{' perm_base_list '}'
{if (define_common_base()) return -1;}
;
av_base : av_base_def
| av_base av_base_def
;
av_base_def : CLASS identifier '{' perm_base_list '}'
{if (define_av_base()) return -1;}
| CLASS identifier
{if (define_av_base()) return -1;}
;
perm_base_list : perm_base
| perm_base_list perm_base
;
perm_base : identifier ':' identifier
{if (insert_separator(0)) return -1;}
| identifier ':' '{' identifier_list '}'
{if (insert_separator(0)) return -1;}
;
te_rbac : te_rbac_decl
| te_rbac te_rbac_decl
;
te_rbac_decl : te_decl
| rbac_decl
| ';'
;
rbac_decl : role_type_def
| role_dominance
| role_trans_def
| role_allow_def
;
/* added July 2002; we make optional for backwards compatability */
/* added optional conditional language stuff */
te_decl : attribute_def
| cond_def
| type_def
| typeattribute_def
| typealias_def
| transition_def
| te_avtab_def
/* removed July 2002; remain for backward compatability */
| te_clone_def
;
/* added July 2002 */
attribute_def : ATTRIBUTE identifier ';'
{ if (define_attrib()) return -1;}
;
/* support for conditional policy language extensions */
cond_def : bool_def
| cond_stmt_def
;
bool_def : BOOL identifier bool_val ';'
{if (define_bool()) return -1;}
;
bool_val : CTRUE
{ if (insert_id("T",0)) return -1; }
| CFALSE
{ if (insert_id("F",0)) return -1; }
;
cond_stmt_def : IF cond_expr '{' cond_pol_list '}'
{ if (define_conditional((cond_expr_t*)$2, (cond_rule_list_t*)$4, (cond_rule_list_t*)NULL) < 0) return -1; }
| IF cond_expr '{' cond_pol_list '}' ELSE '{' cond_pol_list '}'
{ if (define_conditional((cond_expr_t*)$2, (cond_rule_list_t*)$4, (cond_rule_list_t*)$8) < 0 ) return -1; }
;
cond_expr : '(' cond_expr ')'
{ $$ = $2;}
| NOT cond_expr
{ $$ = define_cond_expr(COND_NOT, $2, NULL);
if ($$ == NULL) return -1; }
| cond_expr AND cond_expr
{ $$ = define_cond_expr(COND_AND, $1, $3);
if ($$ == NULL) return -1; }
| cond_expr OR cond_expr
{ $$ = define_cond_expr(COND_OR, $1, $3);
if ($$ == NULL) return -1; }
| cond_expr XOR cond_expr
{ $$ = define_cond_expr(COND_XOR, $1, $3);
if ($$ == NULL) return -1; }
| cond_expr EQUALS cond_expr
{ $$ = define_cond_expr(COND_EQ, $1, $3);
if ($$ == NULL) return -1; }
| cond_expr NOTEQUAL cond_expr
{ $$ = define_cond_expr(COND_NEQ, $1, $3);
if ($$ == NULL) return -1; }
| cond_expr_prim
{ $$ = $1; }
;
cond_expr_prim : identifier
{ $$ = define_cond_expr(COND_BOOL, NULL, NULL);
if ($$ == NULL) return -1; }
;
cond_pol_list : /* empty */
{ $$ = (cond_rule_list_t*)NULL; }
| cond_pol_list cond_rule_def
{ $$ = define_cond_pol_list((cond_rule_list_t*)$1, (rule_desc_t*)$2);
if ($$ == 0) return -1; }
;
cond_rule_def : cond_transition_def
{ $$ = $1;
if ($$ == NULL) return -1; }
| cond_te_avtab_def
{ $$ = $1;
if ($$ == NULL) return -1; }
;
cond_transition_def : TYPE_TRANSITION names names ':' names identifier ';'
{ $$ = define_cond_compute_type(RULE_TE_TRANS) ;
if ($$ == 0) return -1;}
| TYPE_MEMBER names names ':' names identifier ';'
{ $$ = define_cond_compute_type(RULE_TE_MEMBER) ;
if ($$ == 0) return -1;}
| TYPE_CHANGE names names ':' names identifier ';'
{ $$ = define_cond_compute_type(RULE_TE_CHANGE) ;
if ($$ == 0) return -1;}
;
cond_te_avtab_def : cond_allow_def
{ $$ = $1; }
| cond_auditallow_def
{ $$ = $1; }
| cond_auditdeny_def
{ $$ = $1; }
| cond_dontaudit_def
{ $$ = $1; }
;
cond_allow_def : ALLOW names names ':' names names ';'
{ $$ = define_cond_te_avtab(RULE_TE_ALLOW) ;
if ($$ == 0) return -1; }
;
cond_auditallow_def : AUDITALLOW names names ':' names names ';'
{ $$ = define_cond_te_avtab(RULE_AUDITALLOW) ;
if ($$ == 0) return -1; }
;
cond_auditdeny_def : AUDITDENY names names ':' names names ';'
{ $$ = define_cond_te_avtab(RULE_AUDITDENY) ;
if ($$ == 0) return -1; }
;
cond_dontaudit_def : DONTAUDIT names names ':' names names ';'
{ $$ = define_cond_te_avtab(RULE_DONTAUDIT);
if ($$ == 0) return -1; }
;
/* removed July 2002; remain for backward compatability */
te_clone_def : CLONE identifier identifier ';'
{if (define_te_clone()) return -1;}
;
type_def : TYPE identifier alias_def opt_attr_list ';'
{if (define_type(1)) return -1;}
| TYPE identifier opt_attr_list ';'
{if (define_type(0)) return -1;}
;
/* added jan 2005 */
typeattribute_def : TYPEATTRIBUTE identifier id_comma_list ';'
{if (define_typeattribute()) return -1; }
/* added feb 2004 */
typealias_def : TYPEALIAS identifier alias_def ';'
{if (define_typealias()) return -1;}
;
opt_attr_list : ',' id_comma_list
|
;
transition_def : TYPE_TRANSITION names names ':' names identifier ';'
{if (define_compute_type(RULE_TE_TRANS)) return -1;}
| TYPE_MEMBER names names ':' names identifier ';'
{if (define_compute_type(RULE_TE_MEMBER)) return -1;}
| TYPE_CHANGE names names ':' names identifier ';'
{if (define_compute_type(RULE_TE_CHANGE)) return -1;}
;
te_avtab_def : allow_def
| auditallow_def
| auditdeny_def
/* Jul 2002, removed notify and added dontaudit */
| dontaudit_def
| neverallow_def
;
allow_def : ALLOW names names ':' names names ';'
{if (define_te_avtab(RULE_TE_ALLOW)) return -1; }
;
auditallow_def : AUDITALLOW names names ':' names names ';'
{if (define_te_avtab(RULE_AUDITALLOW)) return -1; }
;
auditdeny_def : AUDITDENY names names ':' names names ';'
{if (define_te_avtab(RULE_AUDITDENY)) return -1; }
;
/* Jul 2002, removed notify and added dontaudit */
dontaudit_def : DONTAUDIT names names ':' names names ';'
{if (define_te_avtab(RULE_DONTAUDIT)) return -1; }
;
neverallow_def : NEVERALLOW names names ':' names names ';'
{if (define_te_avtab(RULE_NEVERALLOW)) return -1; }
;
role_type_def : ROLE identifier TYPES names ';'
{if (define_role_types()) return -1;}
;
role_dominance : DOMINANCE '{' roles '}'
;
role_trans_def : ROLE_TRANSITION names names identifier ';'
{if (define_role_trans()) return -1; }
;
role_allow_def : ALLOW names names ';'
{if (define_role_allow()) return -1; }
;
roles : role_def
{ $$ = $1; }
| roles role_def
{ /* do nothing */}
;
role_def : ROLE identifier_push ';'
{$$ = define_role_dom(); if ($$ == 0) return -1;}
| ROLE identifier_push '{' roles '}'
{$$ = define_role_dom(); if ($$ == 0) return -1;}
;
/* added July 2002; made constraints optional */
opt_constraints : constraints
|
;
constraints : constraint_def
| constraints constraint_def
;
constraint_def : CONSTRAIN names names cexpr ';'
{ if (define_constraint()) return -1; }
;
cexpr : '(' cexpr ')'
{ $$ = $2; }
| NOT cexpr
{ $$ = (int) define_cexpr(CEXPR_NOT, $2, 0);
if ($$ == 0) return -1; }
| cexpr AND cexpr
{ $$ = (int) define_cexpr(CEXPR_AND, $1, $3);
if ($$ == 0) return -1; }
| cexpr OR cexpr
{ $$ = (int) define_cexpr(CEXPR_OR, $1, $3);
if ($$ == 0) return -1; }
| cexpr_prim
{ $$ = $1; }
;
cexpr_prim : U1 op U2
{ $$ = (int) define_cexpr(CEXPR_ATTR, CEXPR_USER, $2);
if ($$ == 0) return -1; }
| R1 roleop R2
{ $$ = (int) define_cexpr(CEXPR_ATTR, CEXPR_ROLE, $2);
if ($$ == 0) return -1; }
| T1 op T2
{ $$ = (int) define_cexpr(CEXPR_ATTR, CEXPR_TYPE, $2);
if ($$ == 0) return -1; }
| U1 op { if (insert_separator(1)) return -1; } names_push
{ $$ = (int) define_cexpr(CEXPR_NAMES, CEXPR_USER, $2);
if ($$ == 0) return -1; }
| U2 op { if (insert_separator(1)) return -1; } names_push
{ $$ = (int) define_cexpr(CEXPR_NAMES, CEXPR_USER | CEXPR_TARGET, $2);
if ($$ == 0) return -1; }
| R1 op { if (insert_separator(1)) return -1; } names_push
{ $$ = (int) define_cexpr(CEXPR_NAMES, CEXPR_ROLE, $2);
if ($$ == 0) return -1; }
| R2 op { if (insert_separator(1)) return -1; } names_push
{ $$ = (int) define_cexpr(CEXPR_NAMES, CEXPR_ROLE | CEXPR_TARGET, $2);
if ($$ == 0) return -1; }
| T1 op { if (insert_separator(1)) return -1; } names_push
{ $$ = (int) define_cexpr(CEXPR_NAMES, CEXPR_TYPE, $2);
if ($$ == 0) return -1; }
| T2 op { if (insert_separator(1)) return -1; } names_push
{ $$ = (int) define_cexpr(CEXPR_NAMES, CEXPR_TYPE | CEXPR_TARGET, $2);
if ($$ == 0) return -1; }
| SAMEUSER
{ $$ = (int) define_cexpr(CEXPR_ATTR, CEXPR_USER, CEXPR_EQ);
if ($$ == 0) return -1; }
| SOURCE ROLE { if (insert_separator(1)) return -1; } names_push
{ $$ = (int) define_cexpr(CEXPR_NAMES, CEXPR_ROLE, CEXPR_EQ);
if ($$ == 0) return -1; }
| TARGET ROLE { if (insert_separator(1)) return -1; } names_push
{ $$ = (int) define_cexpr(CEXPR_NAMES, CEXPR_ROLE | CEXPR_TARGET, CEXPR_EQ);
if ($$ == 0) return -1; }
| ROLE roleop
{ $$ = (int) define_cexpr(CEXPR_ATTR, CEXPR_ROLE, $2);
if ($$ == 0) return -1; }
| SOURCE TYPE { if (insert_separator(1)) return -1; } names_push
{ $$ = (int) define_cexpr(CEXPR_NAMES, CEXPR_TYPE, CEXPR_EQ);
if ($$ == 0) return -1; }
| TARGET TYPE { if (insert_separator(1)) return -1; } names_push
{ $$ = (int) define_cexpr(CEXPR_NAMES, CEXPR_TYPE | CEXPR_TARGET, CEXPR_EQ);
if ($$ == 0) return -1; }
;
op : EQUALS
{ $$ = CEXPR_EQ; }
| NOTEQUAL
{ $$ = CEXPR_NEQ; }
;
roleop : op
{ $$ = $1; }
| DOM
{ $$ = CEXPR_DOM; }
| DOMBY
{ $$ = CEXPR_DOMBY; }
| INCOMP
{ $$ = CEXPR_INCOMP; }
;
users : user_def
| users user_def
;
user_id : identifier
;
user_def : USER user_id ROLES names opt_user_ranges ';'
{if (define_user()) return -1;}
;
opt_user_ranges : RANGES user_ranges
|
;
user_ranges : mls_range_def
| '{' user_range_def_list '}'
;
user_range_def_list : mls_range_def
| user_range_def_list mls_range_def
;
initial_sid_contexts : initial_sid_context_def
| initial_sid_contexts initial_sid_context_def
;
initial_sid_context_def : SID identifier security_context_def
{if (define_initial_sid_context()) return -1;}
;
/* added Jul 2002 to maintain backwards compatabililty with previous
* policy versions
*
* current distinctions are:
* pre_jul_2002 policy versions prior to version 11
* jul_2002 policy version 11 or later
*/
policy_version_contexts : version_jul_2002
| versions_pre_jul_2002
;
version_jul_2002 : opt_fs_contexts_11 fs_uses opt_genfs_contexts
net_contexts_11
;
versions_pre_jul_2002 : fs_contexts_pre11
opt_devfs_contexts { /* this is a new policy component added in the
May 2002 release of SE Linux. We are making this
optional so that apolicy will (for now at least)
work wih older policies as well as newer ones */ }
net_contexts_pre11
;
/* added Jul 2002 */
fs_uses : fs_use_def
| fs_uses fs_use_def
;
/* added Jul 2002 */
/* changed Jul 2003; added FSUSEXATTR */
fs_use_def : FSUSEPSID identifier ';'
{if (define_fs_use(0, POL_VER_JUL2002)) return -1;}
| FSUSEXATTR identifier security_context_def ';'
{if (define_fs_use(1, POL_VER_15)) return -1;}
| FSUSETASK identifier security_context_def ';'
{if (define_fs_use(1, POL_VER_JUL2002)) return -1;}
| FSUSETRANS identifier security_context_def ';'
{if (define_fs_use(1, POL_VER_JUL2002)) return -1;}
;
/* added Jul 2002 */
opt_genfs_contexts : genfs_contexts
|
;
/* added Jul 2002 */
genfs_contexts : genfs_context_def
| genfs_contexts genfs_context_def
;
/* added Jul 2002 */
genfs_context_def : GENFSCON identifier path '-' identifier security_context_def
{if (define_genfs_context(1)) return -1;}
| GENFSCON identifier path '-' '-' {insert_id("-", 0);} security_context_def
{if (define_genfs_context(1)) return -1;}
| GENFSCON identifier path security_context_def
{if (define_genfs_context(0)) return -1;}
;
/* added Jul 2002 */
opt_fs_contexts_11 : fs_contexts_11
|
;
fs_contexts_11 : fs_context_def_11
| fs_contexts_11 fs_context_def_11
;
fs_contexts_pre11 : fs_context_def_pre11
| fs_contexts_pre11 fs_context_def_pre11
;
/* changed Jul 2002, added FSCON keyword */
fs_context_def_11 : FSCON number number security_context_def security_context_def
{if (define_fs_context(POL_VER_JUL2002)) return -1;}
;
/* removed Jul 2002, keep for backward compatability */
fs_context_def_pre11 : number number security_context_def security_context_def
{if (define_fs_context(POL_VER_PREJUL2002)) return -1;}
;
/* net_context versions */
net_contexts_11 : opt_port_contexts_11 opt_netif_contexts_11 opt_node_contexts_11
;
net_contexts_pre11 : port_contexts_pre11 netif_contexts_pre11 node_contexts_pre11 opt_nfs_contexts
;
/* added Jul 2002 */
opt_port_contexts_11 : port_contexts_11
|
;
port_contexts_11 : port_context_def_11
| port_contexts_11 port_context_def_11
;
/* changed Jul 2002 to add PORTCON keyword */
port_context_def_11 : PORTCON identifier number security_context_def
{if (define_port_context(POL_VER_JUL2002)) return -1;}
| PORTCON identifier number '-' number security_context_def
{if (define_port_context(POL_VER_JUL2002)) return -1;}
;
/* removed in Jul 2002; keep to allow old form for backwards compatability */
port_contexts_pre11 : port_context_def_pre11
| port_contexts_pre11 port_context_def_pre11
;
port_context_def_pre11 : identifier number security_context_def
{if (define_port_context(POL_VER_PREJUL2002)) return -1;}
| identifier number '-' number security_context_def
{if (define_port_context(POL_VER_PREJUL2002)) return -1;}
;
/* added Jul 2002 */
opt_netif_contexts_11 : netif_contexts_11
|
;
/* changed Jul 2002 to add NETIFCON keyword */
netif_contexts_11 : netif_context_def_11
| netif_contexts_11 netif_context_def_11
;
netif_context_def_11 : NETIFCON identifier security_context_def security_context_def
{if (define_netif_context(POL_VER_JUL2002)) return -1;}
;
/* removed in Jul 2002; keep to allow old form for backwards compatability */
netif_contexts_pre11 : netif_context_def_pre11
| netif_contexts_pre11 netif_context_def_pre11
;
netif_context_def_pre11 : identifier security_context_def security_context_def
{if (define_netif_context(POL_VER_PREJUL2002)) return -1;}
;
/* added Jul 2002 */
opt_node_contexts_11 : node_contexts_11
|
;
node_contexts_11 : node_context_def_11
| node_contexts_11 node_context_def_11
;
/* changed Jul 2002 to add NODECON keyword (v 11) or later version changes (e.g., 17 w/ IPv6) */
node_context_def_11 : NODECON ipv4_addr_def ipv4_addr_def security_context_def
{if (define_ipv4_node_context(POL_VER_JUL2002)) return -1;}
| NODECON ipv6_addr ipv6_addr security_context_def
{if (define_ipv6_node_context(POL_VER_17)) return -1;}
;
/* Jul 2002, allow for old form for backwards compatability */
node_contexts_pre11 : node_context_def_pre11
| node_contexts_pre11 node_context_def_pre11
;
/* removed in Jul 2002; keep to allow old form for backwards compatability */
node_context_def_pre11 : ipv4_addr_def ipv4_addr_def security_context_def
{if (define_ipv4_node_context(POL_VER_PREJUL2002)) return -1;}
;
/* all NFS remove Jul 2002 */
opt_nfs_contexts : nfs_contexts
|
;
nfs_contexts : nfs_context_def
| nfs_contexts nfs_context_def
;
nfs_context_def : ipv4_addr_def ipv4_addr_def security_context_def security_context_def
{if (define_nfs_context()) return -1;}
;
/* removed Jul 2002 */
opt_devfs_contexts : devfs_contexts
|
;
/* removed Jul 2002 */
devfs_contexts : devfs_context_def
| devfs_contexts devfs_context_def
;
/* removed Jul 2002 */
devfs_context_def : path '-' identifier security_context_def
{if (define_devfs_context(1)) return -1;}
| path security_context_def
{if (define_devfs_context(0)) return -1;}
;
ipv4_addr_def : number '.' number '.' number '.' number
{
/*do nothing*/
}
;
ipv6_addr : IPV6_ADDR
{ if (insert_id(yytext,0)) return -1; }
;
security_context_def : user_id ':' identifier ':' identifier opt_mls_range_def
;
opt_mls_range_def : ':' mls_range_def
|
;
mls_range_def : mls_level_def '-' mls_level_def
{if (insert_separator(0)) return -1;}
| mls_level_def
{if (insert_separator(0)) return -1;}
;
mls_level_def : identifier ':' id_comma_list
{if (insert_separator(0)) return -1;}
| identifier
{if (insert_separator(0)) return -1;}
;
id_comma_list : identifier
| id_comma_list ',' identifier
;
tilde : '~'
;
asterisk : '*'
;
exclude : '-'
/* Jul 2002 nesting logic changed slightly */ ;
names : identifier
{ if (insert_separator(0)) return -1; }
| nested_id_set
{ if (insert_separator(0)) return -1; }
| asterisk
{ if (insert_id("*", 0)) return -1;
if (insert_separator(0)) return -1; }
| tilde identifier
{ if (insert_id("~", 0)) return -1;
if (insert_separator(0)) return -1; }
| identifier exclude { if (insert_id("-", 0)) return -1; } identifier
{ if (insert_separator(0)) return -1; }
| tilde nested_id_set
{ if (insert_id("~", 0)) return -1;
if (insert_separator(0)) return -1; }
;
tilde_push : tilde
{ if (insert_id("~", 1)) return -1; }
;
asterisk_push : asterisk
{ if (insert_id("*", 1)) return -1; }
;
names_push : identifier_push
| '{' identifier_list_push '}'
| asterisk_push
| tilde_push identifier_push
| tilde_push '{' identifier_list_push '}'
;
identifier_list_push : identifier_push
| identifier_list_push identifier_push
;
identifier_push : IDENTIFIER
{ if (insert_id(yytext, 1)) return -1; }
;
identifier_list : identifier
| identifier_list identifier
;
/* added Jul 2002*/
nested_id_set : '{' nested_id_list '}'
;
nested_id_list : nested_id_element | nested_id_list nested_id_element
;
nested_id_element : identifier | '-' { if (insert_id("-", 0)) return -1; } identifier | nested_id_set
;
/* end add */
identifier : IDENTIFIER
{ if (insert_id(yytext,0)) return -1; }
;
path : PATH
{ if (insert_id(yytext,0)) return -1; }
;
number : NUMBER
{ $$ = strtoul(yytext,NULL,0); }
;
%%
static int insert_separator(int push)
{
int error;
if (push)
error = queue_push(id_queue, 0);
else
error = queue_insert(id_queue, 0);
if (error) {
yyerror("queue overflow");
return -1;
}
return 0;
}
static int insert_id(char *id, int push)
{
char *newid = 0;
int error;
newid = (char *) malloc(strlen(id) + 1);
if (!newid) {
yyerror("out of memory");
return -1;
}
strcpy(newid, id);
if (push)
error = queue_push(id_queue, (queue_element_t) newid);
else
error = queue_insert(id_queue, (queue_element_t) newid);
if (error) {
yyerror("queue overflow");
free(newid);
return -1;
}
return 0;
}
/* added with Jul 2002 policy changes; allows for explicit declarations of type attributes */
static int define_attrib(void)
{
char *id;
int rt;
rt = set_policy_version(POL_VER_JUL2002, parse_policy);
if(rt != 0) {
yyerror("error setting policy version");
return -1;
}
if (pass == 2 ||(pass == 1 && !(parse_policy->opts & POLOPT_TYPES))) {
free(queue_remove(id_queue));
return 0;
}
id = queue_remove(id_queue);
/* check whether already exists */
rt = get_attrib_idx(id, parse_policy);
if(rt >=0){
snprintf(errormsg,sizeof(errormsg), "duplicate class decalaration (%s)\n", id);
yyerror(errormsg);
return -1;
}
if(!is_valid_str_sz(id)) {
snprintf(errormsg,sizeof(errormsg), "string \"%s\" exceeds APOL_SZ_SIZE", id);
yyerror(errormsg);
return -1;
}
rt = add_attrib(FALSE, 0, parse_policy, id);
if(rt == -1) {
yyerror("Error adding attribute via ATTRIBUTE keyword");
return -1;
}
free(id);
return 0;
}
static int define_type(int alias)
{
char *id;
int idx;
if (pass == 2 ||(pass == 1 && !(parse_policy->opts & POLOPT_TYPES))) {
while ((id = queue_remove(id_queue)))
free(id);
/* change in 2002031409 version for alias syntax */
if (alias) {
while ((id = queue_remove(id_queue)))
free(id);
}
return 0;
}
/* add the new type */
id = (char *) queue_remove(id_queue);
if (!id) {
yyerror("no type name for type definition?");
return -1;
}
if(!is_valid_str_sz(id)) {
snprintf(errormsg, sizeof(errormsg), "string \"%s\" exceeds APOL_SZ_SIZE", id);
yyerror(errormsg);
return -1;
}
idx = add_type(id, parse_policy);
if(idx == -2) {
snprintf(errormsg, sizeof(errormsg), "duplicate type decalaration (%s)\n", id);
yyerror(errormsg);
return -1;
}
else if(idx < 0)
return -1;
/* aliases */
if (alias) {
while ((id = queue_remove(id_queue))) {
if(!is_valid_str_sz(id)) {
snprintf(errormsg, sizeof(errormsg), "string \"%s\" exceeds APOL_SZ_SIZE", id);
yyerror(errormsg);
return -1;
}
if(add_alias(idx, id, parse_policy) != 0) {
snprintf(errormsg, sizeof(errormsg), "failed add_name for alias %s\n", id);
yyerror(errormsg);
return -1;
}
free(id); /* add_alias() makes its owm copy of the string */
}
}
/* attribs */
while ((id = queue_remove(id_queue))) {
if(!is_valid_str_sz(id)) {
snprintf(errormsg, sizeof(errormsg), "string \"%s\" exceeds APOL_SZ_SIZE", id);
yyerror(errormsg);
return -1;
}
if(add_attrib_to_type(idx, id, parse_policy) != 0)
return -1;
}
return 0;
}
static int define_typeattribute(void)
{
char *id;
int rt, idx, idx_type;
if (pass == 2 || (pass == 1 && !(parse_policy->opts & POLOPT_TYPES))) {
while ((id = queue_remove(id_queue)))
free(id);
return 0;
}
rt = set_policy_version(POL_VER_18, parse_policy);
if(rt != 0) {
yyerror("error setting policy version");
return -1;
}
id = (char*)queue_remove(id_queue);
if (!id) {
yyerror("type name required for typeattribute declaration");
return -1;
}
idx = get_type_or_attrib_idx(id, &idx_type, parse_policy);
if (idx < 0) {
snprintf(errormsg, sizeof(errormsg), "unknown type %s in typeattribute definitition.", id);
yyerror(errormsg);
return -1;
}
if (idx_type != IDX_TYPE) {
snprintf(errormsg, sizeof(errormsg), "%s is not a type. Illegal typeattribute definitition.", id);
yyerror(errormsg);
return -1;
}
while ((id = queue_remove(id_queue))) {
if(!is_valid_str_sz(id)) {
snprintf(errormsg, sizeof(errormsg), "string \"%s\" exceeds APOL_SZ_SIZE", id);
yyerror(errormsg);
return -1;
}
if(add_attrib_to_type(idx, id, parse_policy) != 0)
return -1;
}
return 0;
}
static int define_typealias(void)
{
char *id;
int idx, idx_type;
if (pass == 2 || (pass == 1 && !(parse_policy->opts & POLOPT_TYPES))) {
while ((id = queue_remove(id_queue)))
free(id);
return 0;
}
id = (char*)queue_remove(id_queue);
if (!id) {
yyerror("type name required for typealias declaration");
return -1;
}
idx = get_type_or_attrib_idx(id, &idx_type, parse_policy);
if (idx < 0) {
snprintf(errormsg, sizeof(errormsg), "unknown type %s in typealias definitition.", id);
yyerror(errormsg);
return -1;
}
if (idx_type != IDX_TYPE) {
snprintf(errormsg, sizeof(errormsg), "%s is not a type. Illegal typealias definitition.", id);
yyerror(errormsg);
return -1;
}
while ((id = queue_remove(id_queue))) {
if(!is_valid_str_sz(id)) {
snprintf(errormsg, sizeof(errormsg), "string \"%s\" exceeds APOL_SZ_SIZE", id);
yyerror(errormsg);
return -1;
}
if(add_alias(idx, id, parse_policy) != 0) {
snprintf(errormsg, sizeof(errormsg), "failed add_alias for alias %s\n", id);
yyerror(errormsg);
return -1;
}
}
return 0;
}
/* add a rule to the provided av rule list */
static int add_avrule(int type, av_item_t **rlist, int *list_num, bool_t enabled) {
int idx, idx_type;
char *id;
av_item_t *item;
ta_item_t *titem;
bool_t subtract;
item = add_new_av_rule(type, parse_policy);
if(item == NULL) {
yyerror("Problem adding AV rule to policy database");
return -1;
}
item->type = type;
item->lineno = policydb_lineno;
item->enabled = enabled;
/* source (domain) types/attribs */
subtract = FALSE;
while ((id = queue_remove(id_queue))) {
if(strcmp(id, "*") == 0) {
item->flags |= AVFLAG_SRC_STAR;
free(id);
continue;
}
if(strcmp(id, "-") == 0) {
subtract = TRUE;
free(id);
continue;
}
if(strcmp(id, "~") == 0) {
item->flags |= AVFLAG_SRC_TILDA;
free(id);
continue;
}
idx = get_type_or_attrib_idx(id, &idx_type, parse_policy);
if(idx < 0) {
snprintf(errormsg, sizeof(errormsg), "%s is neither a type nor type attribute", id);
yyerror(errormsg);
return -1;
}
titem = (ta_item_t *)malloc(sizeof(ta_item_t));
if(titem == NULL) {
yyerror("out of memory");
return -1;
}
titem->type = idx_type;
if (subtract) {
titem->type |= IDX_SUBTRACT;
subtract = FALSE;
}
titem->idx = idx;
if(insert_ta_item(titem, &(item->src_types)) != 0) {
snprintf(errormsg, sizeof(errormsg), "failed ta_item insetion for source type id %s", id);
yyerror(errormsg);
return -1;
}
free(id);
}
/* target object types/attribs */
subtract = FALSE;
while ((id = queue_remove(id_queue))) {
if(strcmp(id, "*") == 0) {
item->flags |= AVFLAG_TGT_STAR;
free(id);
continue;
}
if(strcmp(id, "-") == 0) {
subtract = TRUE;
free(id);
continue;
}
if(strcmp(id, "~") == 0) {
item->flags |= AVFLAG_TGT_TILDA;
free(id);
continue;
}
idx = get_type_or_attrib_idx(id, &idx_type, parse_policy);
if(idx < 0) {
snprintf(errormsg, sizeof(errormsg), "%s is neither a type or type attribute", id);
yyerror(errormsg);
return -1;
}
titem = (ta_item_t *)malloc(sizeof(ta_item_t));
if(titem == NULL) {
yyerror("out of memory");
return -1;
}
titem->type = idx_type;
if (subtract) {
titem->type |= IDX_SUBTRACT;
subtract = FALSE;
}
titem->idx = idx;
if(insert_ta_item(titem, &(item->tgt_types)) != 0) {
snprintf(errormsg, sizeof(errormsg), "failed ta_item insetion for target type id %s", id);
yyerror(errormsg);
return -1;
}
free(id);
}
/* object classes */
while ((id = queue_remove(id_queue))) {
if(strcmp(id, "*") == 0) {
yyerror("'*' operator cannot be applied to object classes");
return -1;
}
if(strcmp(id, "~") == 0) {
yyerror("'~' operator cannot be applied to object classes");
return -1;
}
idx = get_obj_class_idx(id, parse_policy);
if(idx < 0) {
snprintf(errormsg, sizeof(errormsg), "%s is not a valid object class name", id);
yyerror(errormsg);
return -1;
}
titem = (ta_item_t *)malloc(sizeof(ta_item_t));
if(titem == NULL) {
yyerror("out of memory");
return -1;
}
titem->type = IDX_OBJ_CLASS;
titem->idx = idx;
if(insert_ta_item(titem, &(item->classes)) != 0) {
snprintf(errormsg, sizeof(errormsg), "failed ta_item insetion for classes id %s", id);
yyerror(errormsg);
return -1;
}
free(id);
}
/* permissions */
while ((id = queue_remove(id_queue))) {
if(strcmp(id, "*") == 0) {
item->flags |= AVFLAG_PERM_STAR;
free(id);
continue;
}
if(strcmp(id, "~") == 0) {
item->flags |= AVFLAG_PERM_TILDA;
free(id);
continue;
}
idx = get_perm_idx(id, parse_policy);
if(idx < 0) {
snprintf(errormsg, sizeof(errormsg), "%s is not a valid permission name", id);
yyerror(errormsg);
return -1;
}
titem = (ta_item_t *)malloc(sizeof(ta_item_t));
if(titem == NULL) {
yyerror("out of memory");
return -1;
}
titem->type = IDX_PERM;
titem->idx = idx;
if(insert_ta_item(titem, &(item->perms)) != 0) {
snprintf(errormsg, sizeof(errormsg), "failed ta_item insetion for classes id %s", id);
yyerror(errormsg);
return -1;
}
free(id);
}
return *list_num - 1;
}
/* store av rules */
static int define_te_avtab(int rule_type)
{
int rt;
char *id;
if (pass == 1) {
goto skip_avtab_rule;
}
switch(rule_type) {
case RULE_TE_ALLOW:
if(!(parse_policy->opts & POLOPT_TE_ALLOW))
goto skip_avtab_rule;
rt = add_avrule(rule_type, &(parse_policy->av_access), &(parse_policy->num_av_access), TRUE);
break;
case RULE_NEVERALLOW:
if(!(parse_policy->opts & POLOPT_TE_NEVERALLOW))
goto skip_avtab_rule;
rt = add_avrule(rule_type, &(parse_policy->av_access), &(parse_policy->num_av_access), TRUE);
break;
/* Jul 2002, added RULE_DONTAUDIT, which replaces RULE_NOTIFY */
case RULE_DONTAUDIT:
rt = set_policy_version(POL_VER_JUL2002, parse_policy);
if(rt != 0) {
yyerror("error setting policy version");
return -1;
}
/* fall thru */
case RULE_AUDITDENY:
if(!(parse_policy->opts & POLOPT_TE_DONTAUDIT))
goto skip_avtab_rule;
rt = add_avrule(rule_type, &(parse_policy->av_audit), &(parse_policy->num_av_audit), TRUE);
break;
case RULE_AUDITALLOW:
if(!(parse_policy->opts & POLOPT_TE_AUDITALLOW))
goto skip_avtab_rule;
rt = add_avrule(rule_type, &(parse_policy->av_audit), &(parse_policy->num_av_audit), TRUE);
break;
default:
snprintf(errormsg, sizeof(errormsg), "Invalid AV type (%d)", rule_type);
yyerror(errormsg);
return -1;
}
if(rt < 0)
return rt;
return 0;
skip_avtab_rule:
while ((id = queue_remove(id_queue)))
free(id);
while ((id = queue_remove(id_queue)))
free(id);
while ((id = queue_remove(id_queue)))
free(id);
while ((id = queue_remove(id_queue)))
free(id);
return 0;
}
/* add a new type transition rule */
static int add_ttrule(int rule_type, bool_t enabled)
{
int idx, idx_type;
char *id;
tt_item_t *item;
ta_item_t *titem;
bool_t subtract;
item = add_new_tt_rule(rule_type, parse_policy);
if(item == NULL) {
yyerror("Problem adding TT rule to policy database");
return -1;
}
item->type = rule_type;
item->cond_expr = -1;
item->lineno = policydb_lineno;
item->enabled = enabled;
/* source (domain) types/attribs */
subtract = FALSE;
while ((id = queue_remove(id_queue))) {
if(strcmp(id, "*") == 0) {
item->flags |= AVFLAG_SRC_STAR;
free(id);
continue;
}
if (strcmp(id, "-") == 0) {
subtract = TRUE;
free(id);
continue;
}
if(strcmp(id, "~") == 0) {
item->flags |= AVFLAG_SRC_TILDA;
free(id);
continue;
}
idx = get_type_or_attrib_idx(id, &idx_type, parse_policy);
if(idx < 0) {
snprintf(errormsg, sizeof(errormsg), "%s is neither a type or type attribute", id);
yyerror(errormsg);
return -1;
}
titem = (ta_item_t *)malloc(sizeof(ta_item_t));
if(titem == NULL) {
yyerror("out of memory");
return -1;
}
titem->type = idx_type;
if (subtract) {
titem->type |= IDX_SUBTRACT;
subtract = FALSE;
}
titem->idx = idx;
if(insert_ta_item(titem, &(item->src_types)) != 0) {
snprintf(errormsg, sizeof(errormsg), "failed ta_item insetion for source type id %s\n", id);
yyerror(errormsg);
return -1;
}
free(id);
}
/* target object types/attribs */
subtract = FALSE;
while ((id = queue_remove(id_queue))) {
if(strcmp(id, "*") == 0) {
item->flags |= AVFLAG_TGT_STAR;
free(id);
continue;
}
if(strcmp(id, "~") == 0) {
item->flags |= AVFLAG_TGT_TILDA;
free(id);
continue;
}
if (strcmp(id, "-") == 0) {
subtract = TRUE;
free(id);
continue;
}
idx = get_type_or_attrib_idx(id, &idx_type, parse_policy);
if(idx < 0) {
snprintf(errormsg, sizeof(errormsg), "%s is neither a type or type attribute", id);
yyerror(errormsg);
return -1;
}
titem = (ta_item_t *)malloc(sizeof(ta_item_t));
if(titem == NULL) {
yyerror("out of memory");
return -1;
}
titem->type = idx_type;
if (subtract) {
titem->type |= IDX_SUBTRACT;
subtract = FALSE;
}
titem->idx = idx;
if(insert_ta_item(titem, &(item->tgt_types)) != 0) {
snprintf(errormsg, sizeof(errormsg), "failed ta_item insetion for target type id %s\n", id);
yyerror(errormsg);
return -1;
}
free(id);
}
/* object classes */
while ((id = queue_remove(id_queue))) {
if(strcmp(id, "*") == 0) {
yyerror("'*' operator cannot be applied to object classes");
return -1;
}
if(strcmp(id, "~") == 0) {
yyerror("'~' operator cannot be applied to object classes");
return -1;
}
idx = get_obj_class_idx(id, parse_policy);
if(idx < 0) {
snprintf(errormsg, sizeof(errormsg), "%s is not a valid object class name", id);
yyerror(errormsg);
return -1;
}
titem = (ta_item_t *)malloc(sizeof(ta_item_t));
if(titem == NULL) {
yyerror("out of memory");
return -1;
}
titem->type = IDX_OBJ_CLASS;
titem->idx = idx;
if(insert_ta_item(titem, &(item->classes)) != 0) {
snprintf(errormsg, sizeof(errormsg), "failed ta_item insetion for classes id %s", id);
yyerror(errormsg);
return -1;
}
free(id);
}
/* default type */
id = queue_remove(id_queue);
idx = get_type_or_attrib_idx(id, &idx_type, parse_policy);
if(idx < 0 || idx_type != IDX_TYPE) {
snprintf(errormsg, sizeof(errormsg), "default type %s is NOT a defined type.", id);
yyerror(errormsg);
return -1;
}
item->dflt_type.type = idx_type;
item->dflt_type.idx = idx;
free(id);
return parse_policy->num_te_trans - 1;
}
/* put type transition (change, member) rules in policy object */
static int define_compute_type(int rule_type)
{
char *id;
int rt;
if (pass == 1) {
goto skip_tt_rule;
}
switch(rule_type) {
case RULE_TE_TRANS:
if(!(parse_policy->opts & POLOPT_TE_TRANS))
goto skip_tt_rule;
break;
case RULE_TE_MEMBER:
if(!(parse_policy->opts & POLOPT_TE_MEMBER))
goto skip_tt_rule;
break;
case RULE_TE_CHANGE:
if(!(parse_policy->opts & POLOPT_TE_CHANGE))
goto skip_tt_rule;
break;
default:
snprintf(errormsg, sizeof(errormsg), "Invalid type transition|member|change rule type (%d)", rule_type);
yyerror(errormsg);
return -1;
}
rt = add_ttrule(rule_type, TRUE);
if(rt < 0)
return rt;
return 0;
skip_tt_rule:
while ((id = queue_remove(id_queue)))
free(id);
while ((id = queue_remove(id_queue)))
free(id);
while ((id = queue_remove(id_queue)))
free(id);
id = queue_remove(id_queue);
free(id);
return 0;
}
/* capture clone rule; we won't actually clone rules but keep the actual CLONE statements
* and resolve clones when needed */
/* pre Jul 2002 only */
static int define_te_clone(void)
{
char *id;
int src, tgt, rt;
if (pass == 1) {
id = queue_remove(id_queue);
free(id);
id = queue_remove(id_queue);
free(id);
return 0;
}
id = queue_remove(id_queue);
src = get_type_idx(id, parse_policy);
if(src < 0) {
snprintf(errormsg, sizeof(errormsg), "Invalid source type (%s)", id);
yyerror(errormsg);
return -1;
}
free(id);
id = queue_remove(id_queue);
tgt = get_type_idx(id, parse_policy);
if(tgt < 0) {
snprintf(errormsg, sizeof(errormsg), "Invalid target type (%s)", id);
yyerror(errormsg);
return -1;
}
free(id);
rt = add_clone_rule(src, tgt, policydb_lineno, parse_policy);
if(rt != 0) return rt;
(parse_policy->rule_cnt[RULE_CLONE])++;
rt= set_policy_version(POL_VER_PREJUL2002, parse_policy);
if (rt != 0) {
yyerror("error setting policy version");
return -1;
}
return 0;
}
static int define_role_types(void)
{
char *id, *or_name;
int role_idx, idx, idx_type, rt;
if (pass == 1 || (pass == 2 && !(parse_policy->opts & POLOPT_ROLES))) {
while ((id = queue_remove(id_queue)))
free(id);
return 0;
}
id = (char *) queue_remove(id_queue);
if (!id) {
yyerror("no role name for role definition?");
return -1;
}
if(!is_valid_str_sz(id) ) {
snprintf(errormsg, sizeof(errormsg), "string \"%s\" is too large", id);
yyerror(errormsg);
return -1;
}
/* If this is the first role to be added, then add the hard-coded
* default object role "object_r" as it will not show up in the policy */
if(parse_policy->num_roles < 1) {
or_name = (char *)malloc(strlen(OBJECT_R_NAME) + 1);
strcpy(or_name, OBJECT_R_NAME);
role_idx = add_role(or_name, parse_policy);
if(role_idx < 0) {
yyerror("Problem adding object role object_r to policy");
free(id);
return -1;
}
assert(role_idx == 0);
}
/* if the role already exists, we'll just add the new type, otherwise
* we create new role */
role_idx = get_role_idx(id, parse_policy);
if(role_idx < 0) {
role_idx = add_role(id, parse_policy);
/* don't free id if we added it; the add_role type uses the memory */
if(role_idx < 0) {
snprintf(errormsg, sizeof(errormsg), "Problem adding role %s to policy", id);
yyerror(errormsg);
free(id);
return -1;
}
}
else {
/* get rid of the role name if it alrady exists */
free(id);
}
/* add the types or attributes */
while ((id = queue_remove(id_queue))) {
idx = get_type_or_attrib_idx(id, &idx_type, parse_policy);
if(idx < 0) {
snprintf(errormsg, sizeof(errormsg), "Invalid type name (%s) in role definition", id);
yyerror(errormsg);
return -1;
}
rt = add_type_to_role(idx, role_idx, parse_policy);
if(rt != 0)
return rt;
free(id);
}
return 0;
}
static int define_role_allow(void)
{
char *id;
int idx;
ta_item_t *role = NULL;
role_allow_t *rule = NULL;
if(pass == 1 || (pass == 2 && !(parse_policy-> opts & POLOPT_ROLE_RULES))) {
while ((id = queue_remove(id_queue)))
free(id);
while ((id = queue_remove(id_queue)))
free(id);
return 0;
}
if(parse_policy->num_role_allow >= parse_policy->list_sz[POL_LIST_ROLE_ALLOW]) {
/* grow the dynamic array */
role_allow_t * ptr;
ptr = (role_allow_t *)realloc(parse_policy->role_allow, (LIST_SZ+parse_policy->list_sz[POL_LIST_ROLE_ALLOW]) * sizeof(role_allow_t));
if(ptr == NULL) {
yyerror("out of memory\n");
return -1;
}
parse_policy->role_allow = ptr;
parse_policy->list_sz[POL_LIST_ROLE_ALLOW] += LIST_SZ;
}
rule = &(parse_policy->role_allow[parse_policy->num_role_allow]);
memset(rule, 0, sizeof(role_allow_t));
rule->lineno = policydb_lineno;
/* source roles*/
while ((id = queue_remove(id_queue))) {
if(strcmp(id, "*") == 0) {
rule->flags |= AVFLAG_SRC_STAR;
free(id);
continue;
}
if(strcmp(id, "~") == 0) {
rule->flags |= AVFLAG_SRC_TILDA;
free(id);
continue;
}
idx = get_role_idx(id, parse_policy);
if(idx < 0) {
snprintf(errormsg, sizeof(errormsg), "%s is an invalid role attribute", id);
yyerror(errormsg);
free(id);
return -1;
}
role = (ta_item_t *)malloc(sizeof(ta_item_t));
if(role == NULL) {
yyerror("out of memory");
free(id);
return -1;
}
role->type = IDX_ROLE;
role->idx = idx;
if(insert_ta_item(role, &(rule->src_roles)) != 0) {
snprintf(errormsg, sizeof(errormsg), "failed ta_item insetion for source rule id %s\n", id);
yyerror(errormsg);
free(id);
free(role);
return -1;
}
free(id);
}
/* target role */
while ((id = queue_remove(id_queue))) {
if(strcmp(id, "*") == 0) {
rule->flags |= AVFLAG_TGT_STAR;
free(id);
free(role);
continue;
}
if(strcmp(id, "~") == 0) {
rule->flags |= AVFLAG_TGT_TILDA;
free(id);
continue;
}
idx = get_role_idx(id, parse_policy);
if(idx < 0) {
snprintf(errormsg, sizeof(errormsg), "%s is not a valid role", id);
yyerror(errormsg);
free(id);
return -1;
}
role = (ta_item_t *)malloc(sizeof(ta_item_t));
if(role == NULL) {
yyerror("out of memory");
free(id);
return -1;
}
role->type = IDX_ROLE;
role->idx = idx;
if(insert_ta_item(role, &(rule->tgt_roles)) != 0) {
snprintf(errormsg, sizeof(errormsg), "failed ta_item insetion for target role id %s\n", id);
yyerror(errormsg);
free(id);
free(role);
return -1;
}
free(id);
}
(parse_policy->num_role_allow)++;
(parse_policy->rule_cnt[RULE_ROLE_ALLOW])++;
return 0;
}
static int define_role_trans(void)
{
char *id;
int idx, idx_type;
rt_item_t *rule;
ta_item_t *role = NULL, *type = NULL;
if(pass == 1 || (pass == 2 && !(parse_policy-> opts & POLOPT_ROLE_RULES))) {
while ((id = queue_remove(id_queue)))
free(id); /* src roles */
while ((id = queue_remove(id_queue)))
free(id); /* tgt types */
id = queue_remove(id_queue);
free(id); /* trans. role */
return 0;
}
if(parse_policy->num_role_trans >= parse_policy->list_sz[POL_LIST_ROLE_TRANS]) {
/* grow the dynamic array */
rt_item_t *ptr;
ptr = (rt_item_t *)realloc(parse_policy->role_trans,
(LIST_SZ+parse_policy->list_sz[POL_LIST_ROLE_TRANS]) * sizeof(rt_item_t));
if(ptr == NULL) {
yyerror("out of memory\n");
return -1;
}
parse_policy->role_trans = ptr;
parse_policy->list_sz[POL_LIST_ROLE_TRANS] += LIST_SZ;
}
rule = &(parse_policy->role_trans[parse_policy->num_role_trans]);
memset(rule, 0, sizeof(rt_item_t));
rule->lineno = policydb_lineno;
/* source ROLES*/
while ((id = queue_remove(id_queue))) {
if(strcmp(id, "*") == 0) {
rule->flags |= AVFLAG_SRC_STAR;
free(id);
continue;
}
if(strcmp(id, "~") == 0) {
rule->flags |= AVFLAG_SRC_TILDA;
free(id);
continue;
}
idx = get_role_idx(id, parse_policy);
if(idx < 0) {
snprintf(errormsg, sizeof(errormsg), "%s is an invalid role name", id);
yyerror(errormsg);
free(id);
return -1;
}
role = (ta_item_t *)malloc(sizeof(ta_item_t));
if(role == NULL) {
yyerror("out of memory");
free(id);
return -1;
}
role->type = IDX_ROLE;
role->idx = idx;
if(insert_ta_item(role, &(rule->src_roles)) != 0) {
snprintf(errormsg, sizeof(errormsg), "failed ta_item insetion for source rule id %s\n", id);
yyerror(errormsg);
free(role);
free(id);
return -1;
}
free(id);
}
/* target TYPES/ATTRIBS */
while ((id = queue_remove(id_queue))) {
if(strcmp(id, "*") == 0) {
rule->flags |= AVFLAG_TGT_STAR;
free(id);
continue;
}
if(strcmp(id, "~") == 0) {
rule->flags |= AVFLAG_TGT_TILDA;
free(id);
continue;
}
idx = get_type_or_attrib_idx(id, &idx_type, parse_policy);
if(idx < 0) {
snprintf(errormsg, sizeof(errormsg), "%s is neither a type or type attribute", id);
yyerror(errormsg);
free(id);
free(role);
return -1;
}
type = (ta_item_t *)malloc(sizeof(ta_item_t));
if(type == NULL) {
yyerror("out of memory");
free(id);
free(role);
return -1;
}
type->type = idx_type;
type->idx = idx;
if(insert_ta_item(type, &(rule->tgt_types)) != 0) {
snprintf(errormsg, sizeof(errormsg), "failed ta_item insetion for target type %s\n", id);
yyerror(errormsg);
free(id);
free(role);
free(type);
return -1;
}
free(id);
}
/* transition role */
id = queue_remove(id_queue);
rule->trans_role.idx = get_role_idx(id, parse_policy);
rule->trans_role.type = IDX_ROLE;
if(rule->trans_role.idx < 0) {
snprintf(errormsg, sizeof(errormsg), "%s is an invalid role name", id);
yyerror(errormsg);
free(role);
free(type);
free(id);
return -1;
}
free(id);
(parse_policy->num_role_trans)++;
(parse_policy->rule_cnt[RULE_ROLE_TRANS])++;
return 0;
}
/* users list */
static int define_user(void)
{
char *id;
int idx, ridx, rt;
bool_t existing;
if(pass == 1 || (pass == 2 && !(parse_policy-> opts & POLOPT_USERS))) {
while ((id = queue_remove(id_queue)))
free(id);
return 0;
}
id = (char *) queue_remove(id_queue);
if (!id) {
yyerror("no user name for user definition?");
return -1;
}
if(!is_valid_str_sz(id)) {
snprintf(errormsg, sizeof(errormsg), "string \"%s\" exceeds APOL_SZ_SIZE", id);
yyerror(errormsg);
return -1;
}
/* check if existing user; if so treat as union of roles */
idx = get_user_idx(id, parse_policy);
if(idx >= 0) {
/* existing; ptr now points to existing user record */
existing = TRUE;
free(id);
}
else {
/* new user */
existing = FALSE;
idx = add_user(id, parse_policy);
if(idx < 0) {
snprintf(errormsg, sizeof(errormsg), "problem adding user \"%s\" to policy", id);
yyerror(errormsg);
return -1;
}
/* don't free id (user name)*/
}
while((id = queue_remove(id_queue))) {
ridx = get_role_idx(id, parse_policy);
if(ridx < 0) {
snprintf(errormsg, sizeof(errormsg), "%s is an invalid role name", id);
yyerror(errormsg);
free(id);
return -1;
}
rt = add_role_to_user(ridx, idx, parse_policy);
if(rt != 0) {
yyerror("problem inserting role in user");
return -1;
}
free(id);
}
return 0;
}
/* class definitions */
static int define_class(void)
{
char *id = 0;
int idx;
if(pass == 2 || (pass == 1 && !(parse_policy->opts & POLOPT_CLASSES))) {
id = queue_remove(id_queue);
free(id);
return 0;
}
/* add class name */
id = queue_remove(id_queue);
if(!id) {
yyerror("no class name for class definitions\n");
return -1;
}
if(!is_valid_str_sz(id)) {
snprintf(errormsg, sizeof(errormsg), "string \"%s\" exceeds APOL_SZ_SIZE", id);
yyerror(errormsg);
return -1;
}
idx = add_class(id, parse_policy);
if(idx == -2) {
snprintf(errormsg, sizeof(errormsg), "duplicate class decalaration (%s)\n", id);
yyerror(errormsg);
return -1;
}
else if(idx < 0)
return -1;
return 0;
}
/* object permissions permissions */
static int define_av_perms(int inherits)
{
char *id;
int cls_idx, cp_idx, p_idx, rt;
char *tname = NULL;
if(pass == 2 || (pass == 1 && !(parse_policy->opts & POLOPT_CLASSES)) ||
(pass == 1 && !(parse_policy->opts & POLOPT_PERMS))) {
while ((id = queue_remove(id_queue)))
free(id);
return 0;
}
id = queue_remove(id_queue);
if(!id) {
yyerror("no class name for permission definition");
return -1;
}
cls_idx = get_obj_class_idx(id, parse_policy);
if(cls_idx < 0) {
snprintf(errormsg, sizeof(errormsg), "%s is not a valid object class name", id);
yyerror(errormsg);
return -1;
}
free(id);
if (inherits) {
id = (char *) queue_remove(id_queue);
if (!id) {
yyerror("no inherits name for access vector definition?");
return -1;
}
cp_idx = get_common_perm_idx(id, parse_policy);
if(cp_idx < 0) {
snprintf(errormsg, sizeof(errormsg), "%s is not a valid object class name", id);
yyerror(errormsg);
return -1;
}
tname = id; /* temporarily keep around for check below */
rt = add_common_perm_to_class(cls_idx, cp_idx, parse_policy);
if( rt < 0) {
yyerror("problem adding common perm idx to class");
return -1;
}
}
/* class-specific permissions */
while ((id = queue_remove(id_queue))) {
p_idx = add_perm(id, parse_policy);
if(p_idx < 0) {
snprintf(errormsg, sizeof(errormsg), "problem adding permisions %s", id);
yyerror(errormsg);
return -1;
}
/* this check straight from checkpolicy; not sure necessary ??? */
if (inherits) {
/*
* Class-specific permissions and
* common permissions exist in the same
* name space.
*/
if(strcasecmp(id, tname) == 0) {
snprintf(errormsg, sizeof(errormsg), "class-specific permission (%s) conflicts with common permission", id);
yyerror(errormsg);
return -1;
}
}
if(!is_valid_str_sz(id)) {
snprintf(errormsg, sizeof(errormsg), "string \"%s\" exceeds APOL_SZ_SIZE", id);
yyerror(errormsg);
return -1;
}
rt = add_perm_to_class(cls_idx, p_idx, parse_policy);
if(rt != 0) {
snprintf(errormsg, sizeof(errormsg), "problem adding permission (%s) to object class", id);
yyerror(errormsg);
return -1;
}
free(id);
}
if(inherits)
free(tname); /* no longer need common name */
return 0;
}
/* common permissions */
static int define_common_perms(void)
{
char *id = 0;
int idx, permidx, rt;
if (pass == 2 || (pass == 1 && !(parse_policy->opts & POLOPT_PERMS))) {
while ((id = queue_remove(id_queue)))
free(id);
return 0;
}
id = (char *) queue_remove(id_queue);
if (!id) {
yyerror("no common name for common perm definition?");
return -1;
}
/* add new common perm */
if(!is_valid_str_sz(id)) {
snprintf(errormsg, sizeof(errormsg), "string \"%s\" exceeds APOL_SZ_SIZE", id);
yyerror(errormsg);
return -1;
}
idx = add_common_perm(id, parse_policy);
if(idx == -2) {
snprintf(errormsg, sizeof(errormsg), "Duplicate common permission name (%s)", id);
yyerror(errormsg);
return -1;
}
else if(idx < 0)
return -1; /* other err */
/* add permissions to common perm */
while ((id = queue_remove(id_queue))) {
if(!is_valid_str_sz(id)) {
snprintf(errormsg, sizeof(errormsg), "string \"%s\" exceeds APOL_SZ_SIZE", id);
yyerror(errormsg);
return -1;
}
permidx = add_perm(id, parse_policy);
if(permidx < 0) {
free(id); /* add_perm() allocates its own memory */
snprintf(errormsg, sizeof(errormsg), "Problem adding permission name (%s)", id);
yyerror(errormsg);
return -1;
}
free(id); /* add_perm() allocates its own memory */
rt = add_perm_to_common(idx, permidx, parse_policy);
/* Note: a -2 return (already exists) is fine here */
if(rt == -1 ) {
yyerror("Problem adding permission idx to common permission");
return -1;
}
}
return 0;
}
/* TODO: Temporary fix to allow role names defined by role domainance to exist
* in policy name space. Still need to handle their definitions.
*/
static int
define_role_dom(void)
{
char *id;
int idx;
id = queue_remove(id_queue);
if (!id) {
yyerror("no role name for role dominance?");
return -1;
}
if (pass == 1 || (pass == 2 && !(parse_policy->opts & POLOPT_ROLES))) {
free(id);
return 1;
}
/* pass 2 */
/* if the role already exists, we'll just add the new type, otherwise
* we create new role */
idx = get_role_idx(id, parse_policy);
if(idx < 0) {
/* new role identifier; for now just create the role...nothing
* else will be done with it but user statements can use it!
* TODO: Finish this!
*/
if(!is_valid_str_sz(id) ) {
snprintf(errormsg, sizeof(errormsg), "string \"%s\" exceeds APOL_SZ_SIZE", id);
yyerror(errormsg);
return -1;
}
idx = add_role(id, parse_policy);
if(idx < 0)
return -1;
}
else {
/* already exists; we're done for now
* TODO: handle dominance semantics
*/
free(id);
}
return 1;
}
static int define_bool(void)
{
char *id, *name;
bool_t state;
int rt;
rt = set_policy_version(POL_VER_COND, parse_policy);
if(rt != 0) {
yyerror("error setting policy version");
return -1;
}
if (!(parse_policy->opts & POLOPT_COND_BOOLS) || pass == 2) {
while ((id = (char*)queue_remove(id_queue)))
free(id);
return 0;
}
name = (char*)queue_remove(id_queue);
if (!name) {
yyerror("No name for boolean declaration");
return -1;
}
id = (char*)queue_remove(id_queue);
if (!id) {
yyerror("No value for boolean declaration");
return -1;
}
if (strcmp(id, "T") == 0)
state = TRUE;
else
state = FALSE;
free(id);
rt = add_cond_bool(name, state, parse_policy);
if (rt == -2) {
snprintf(errormsg, sizeof(errormsg), "Boolean %s already exists", name);
yyerror(errormsg);
return -1;
} else if (rt < 0) {
yyerror("Error adding boolean");
return -1;
}
return 0;
}
/* search through the policy for a matching conditional expression
* returns -1 if non or found
* returns the index of the cond_expr_item_t if found.
*/
static int find_matching_cond_expr(cond_expr_t *expr, policy_t *policy)
{
int i;
for (i = 0; i < policy->num_cond_exprs; i++) {
if (cond_exprs_equal(expr, policy->cond_exprs[i].expr))
return i;
}
return -1;
}
static int cond_rule_add_helper(int **list, int *list_len, int *add, int add_len)
{
int i;
if (!add)
return 0;
if (!*list) {
/* Copy the pointer of the incoming true|false list into our cond_expr_item_t list member */
*list = add;
return 0;
}
for (i = 0; i < add_len; i++) {
/* we probably don't need to do this checking, but it is better to be safe */
if (find_int_in_array(add[i], *list, *list_len) == -1) {
if (add_i_to_a(add[i], list_len, list) == -1)
return -1;
}
}
return 0;
}
static int define_conditional(cond_expr_t *expr, cond_rule_list_t *t_list, cond_rule_list_t *f_list)
{
int idx, rt;
cond_expr_item_t *cur = NULL;
rt = set_policy_version(POL_VER_COND, parse_policy);
if(rt != 0) {
yyerror("error setting policy version");
return -1;
}
if (pass == 1)
return 0;
if (expr == &dummy_cond_expr) {
yyerror("Received invalid expression in define_conditional");
return -1;
}
idx = find_matching_cond_expr(expr, parse_policy);
/* found matching expressions - add the rules to this expression. */
if (idx != -1) {
cond_free_expr(expr);
cur = &parse_policy->cond_exprs[idx];
if (t_list) {
if (!cur->true_list) {
cur->true_list = t_list;
} else {
if (cond_rule_add_helper(&cur->true_list->av_access, &cur->true_list->num_av_access,
t_list->av_access, t_list->num_av_access) == -1)
return -1;
if (cond_rule_add_helper(&cur->true_list->av_audit, &cur->true_list->num_av_audit,
t_list->av_audit, t_list->num_av_audit) == -1)
return -1;
if (cond_rule_add_helper(&cur->true_list->te_trans, &cur->true_list->num_te_trans,
t_list->te_trans, t_list->num_te_trans) == -1)
return -1;
}
add_cond_expr_item_helper(idx, TRUE, t_list, parse_policy);
}
if (f_list) {
if (!cur->false_list) {
cur->false_list = f_list;
} else {
if (cond_rule_add_helper(&cur->false_list->av_access, &cur->false_list->num_av_access,
f_list->av_access, f_list->num_av_access) == -1)
return -1;
if (cond_rule_add_helper(&cur->false_list->av_audit, &cur->false_list->num_av_audit,
f_list->av_audit, f_list->num_av_audit) == -1)
return -1;
if (cond_rule_add_helper(&cur->false_list->te_trans, &cur->false_list->num_te_trans,
f_list->te_trans, f_list->num_te_trans) == -1)
return -1;
}
add_cond_expr_item_helper(idx, FALSE, f_list, parse_policy);
}
} else {
if (add_cond_expr_item(expr, t_list, f_list, parse_policy) < 0) {
yyerror("Error adding conditional expression item to the policy");
return -1;
}
}
if (update_cond_expr_items(parse_policy) != 0)
return -1;
return 0;
}
static cond_expr_t *define_cond_expr(__u32 expr_type, void *arg1, void *arg2)
{
char *id;
cond_expr_t *expr, *e1 = NULL, *e2;
int bool_var;
if (pass == 1) {
if (expr_type == COND_BOOL) {
while ((id = queue_remove(id_queue)))
free(id);
}
return &dummy_cond_expr;
}
if (!(parse_policy->opts & POLOPT_COND_BOOLS)) {
return &dummy_cond_expr;
}
/* create a new expression struct */
expr = malloc(sizeof(struct cond_expr));
if (!expr) {
yyerror("out of memory");
return NULL;
}
memset(expr, 0, sizeof(cond_expr_t));
expr->expr_type = expr_type;
/* create the type asked for */
switch (expr_type) {
case COND_NOT:
e1 = NULL;
e2 = (struct cond_expr *) arg1;
while (e2) {
e1 = e2;
e2 = e2->next;
}
if (!e1 || e1->next) {
yyerror("illegal conditional NOT expression");
free(expr);
return NULL;
}
e1->next = expr;
return (struct cond_expr *) arg1;
case COND_AND:
case COND_OR:
case COND_XOR:
case COND_EQ:
case COND_NEQ:
e1 = NULL;
e2 = (struct cond_expr *) arg1;
while (e2) {
e1 = e2;
e2 = e2->next;
}
if (!e1 || e1->next) {
yyerror("illegal left side of conditional binary op expression");
free(expr);
return NULL;
}
e1->next = (struct cond_expr *) arg2;
e1 = NULL;
e2 = (struct cond_expr *) arg2;
while (e2) {
e1 = e2;
e2 = e2->next;
}
if (!e1 || e1->next) {
yyerror("illegal right side of conditional binary op expression");
free(expr);
return NULL ;
}
e1->next = expr;
return (struct cond_expr *) arg1;
case COND_BOOL:
id = (char *) queue_remove(id_queue) ;
if (!id) {
yyerror("bad conditional; expected boolean id");
free(id);
free(expr);
return NULL;
}
bool_var = get_cond_bool_idx(id, parse_policy);
if (bool_var < 0) {
snprintf(errormsg, sizeof(errormsg), "unknown boolean %s in conditional expression", id);
yyerror(errormsg);
free(expr);
free(id);
return NULL ;
}
expr->bool = bool_var;
free(id);
return expr;
default:
yyerror("illegal conditional expression");
return NULL;
}
}
/* collect the type lists */
static cond_rule_list_t *define_cond_pol_list(cond_rule_list_t *list, rule_desc_t *rule)
{
cond_rule_list_t *rl;
if (pass == 1)
return (cond_rule_list_t*)1;
if (!list) {
rl = (cond_rule_list_t*)malloc(sizeof(cond_rule_list_t));
if (!rl) {
yyerror("Memory error");
free(rule);
return NULL;
}
memset(rl, 0, sizeof(cond_rule_list_t));
} else {
rl = list;
}
if (!rule || rule == &dummy_rule_desc)
return rl;
switch (rule->rule_type) {
case RULE_TE_ALLOW:
case RULE_NEVERALLOW:
if (add_i_to_a(rule->idx, &rl->num_av_access, &rl->av_access) != 0) {
yyerror("Memory error");
free(rule);
return NULL;
}
break;
case RULE_DONTAUDIT:
case RULE_AUDITDENY:
case RULE_AUDITALLOW:
if (add_i_to_a(rule->idx, &rl->num_av_audit, &rl->av_audit) != 0) {
yyerror("Memory error");
free(rule);
return NULL;
}
break;
case RULE_TE_TRANS:
case RULE_TE_MEMBER:
case RULE_TE_CHANGE:
if (add_i_to_a(rule->idx, &rl->num_te_trans, &rl->te_trans) != 0) {
yyerror("Memory error");
free(rule);
return NULL;
}
break;
default:
yyerror("Internal error: invalid type description.");
free(rule);
return NULL;
}
free(rule);
return rl;
}
static rule_desc_t *define_cond_compute_type(int rule_type)
{
char *id;
int rt;
rule_desc_t *rule;
if (pass == 1)
goto skip_tt_rule;
if (!(parse_policy->opts & POLOPT_COND_TE_RULES))
goto skip_tt_rule;
rule = (rule_desc_t*)malloc(sizeof(rule_desc_t));
if (!rule) {
yyerror("Memory error");
return NULL;
}
memset(rule, 0, sizeof(rule_desc_t));
switch(rule_type) {
case RULE_TE_TRANS:
case RULE_TE_MEMBER:
case RULE_TE_CHANGE:
break;
default:
snprintf(errormsg, sizeof(errormsg), "Invalid type transition|member|change rule type (%d)", rule_type);
yyerror(errormsg);
return NULL;
}
rt = add_ttrule(rule_type, TRUE);
if(rt < 0)
return NULL;
rule->rule_type = rule_type;
rule->idx = rt;
return rule;
skip_tt_rule:
/* TODO: Currently an empty stub */
while ((id = queue_remove(id_queue)))
free(id);
while ((id = queue_remove(id_queue)))
free(id);
while ((id = queue_remove(id_queue)))
free(id);
id = queue_remove(id_queue);
free(id);
return &dummy_rule_desc;
}
static rule_desc_t *define_cond_te_avtab(int rule_type)
{
char *id;
int rt;
rule_desc_t *rule;
if (pass == 1) {
goto skip_avtab_rule;
}
if(!(parse_policy->opts & POLOPT_COND_TE_RULES))
goto skip_avtab_rule;
rule = (rule_desc_t*)malloc(sizeof(rule_desc_t));
if (!rule) {
yyerror("Memory error");
return NULL;
}
memset(rule, 0, sizeof(rule_desc_t));
switch(rule_type) {
case RULE_TE_ALLOW:
rt = add_avrule(rule_type, &(parse_policy->av_access), &(parse_policy->num_av_access), TRUE);
break;
case RULE_NEVERALLOW:
rt = add_avrule(rule_type, &(parse_policy->av_access), &(parse_policy->num_av_access), TRUE);
break;
/* Jul 2002, added RULE_DONTAUDIT, which replaces RULE_NOTIFY */
case RULE_DONTAUDIT:
rt = set_policy_version(POL_VER_JUL2002, parse_policy);
if(rt != 0) {
yyerror("error setting policy version");
return NULL;
}
/* fall thru */
case RULE_AUDITDENY:
rt = add_avrule(rule_type, &(parse_policy->av_audit), &(parse_policy->num_av_audit), TRUE);
break;
case RULE_AUDITALLOW:
rt = add_avrule(rule_type, &(parse_policy->av_audit), &(parse_policy->num_av_audit), TRUE);
break;
default:
snprintf(errormsg, sizeof(errormsg), "Invalid AV type (%d)", rule_type);
yyerror(errormsg);
return NULL;
}
if (rt < 0)
return NULL;
rule->rule_type = rule_type;
rule->idx = rt;
return rule;
skip_avtab_rule:
while ((id = queue_remove(id_queue)))
free(id);
while ((id = queue_remove(id_queue)))
free(id);
while ((id = queue_remove(id_queue)))
free(id);
while ((id = queue_remove(id_queue)))
free(id);
return &dummy_rule_desc; /* 0 (i.e., NULL) is fail */
}
static int define_initial_sid(void)
{
char *id = 0;
int idx;
__u32 sid;
if (pass == 2 ||(pass == 1 && !(parse_policy->opts & POLOPT_INITIAL_SIDS))) {
id = queue_remove(id_queue);
free(id);
return 0;
}
/* add initial SID name; context will be added in define_initial_sid_context() */
id = (char *) queue_remove(id_queue);
if (!id) {
yyerror("no name for SID definition?");
free(id);
return -1;
}
if(!is_valid_str_sz(id)) {
snprintf(errormsg, sizeof(errormsg), "string \"%s\" exceeds APOL_SZ_SIZE", id);
yyerror(errormsg);
free(id);
return -1;
}
sid = num_initial_sids(parse_policy)+1; /* from the src parse, the enxt SID # is always the
* number of SIDs plus one (SIDs start from 1) */
idx = add_initial_sid2(id, sid, parse_policy);
/*idx = add_initial_sid(id, parse_policy);*/
if(idx == -2) {
snprintf(errormsg, sizeof(errormsg), "duplicate initial SID decalaration (%s)\n", id);
yyerror(errormsg);
return -1;
}
else if(idx < 0)
return -1;
return 0;
}
/* If dontsave, then just clear the queue and return NULL (the return
* should be ignored in this case). Otherwise, allocate a context
* structure and return it, or NULL for error
*/
static security_con_t *parse_security_context(int dontsave)
{
char *id;
int rt;
security_con_t *scontext;
if (pass == 1 || dontsave) {
id = queue_remove(id_queue); /* user */
free(id);
id = queue_remove(id_queue); /* role */
free(id);
id = queue_remove(id_queue); /* type */
free(id);
#ifdef CONFIG_SECURITY_SELINUX_MLS
{
int l;
id = queue_remove(id_queue); free(id);
for (l = 0; l < 2; l++) {
while ((id = queue_remove(id_queue))) {
free(id);
}
}
}
#endif
return NULL; /* In this case this is not an error */
}
scontext = (security_con_t *)malloc(sizeof(security_con_t));
if(scontext == NULL) {
yyerror("out of memory");
return NULL;
}
/* user */
id = queue_remove(id_queue);
if (!id) {
yyerror("Security context missing user?");
free(scontext);
return NULL;
}
rt = get_user_idx(id, parse_policy);
if(rt < 0) {
snprintf(errormsg, sizeof(errormsg), "User %s is not defined in policy.", id);
yyerror(errormsg);
free(id);
free(scontext);
return NULL;
}
free(id);
scontext->user = rt;
/* role */
id = queue_remove(id_queue);
if (!id) {
yyerror("Security context missing role?");
free(scontext);
return NULL;
}
rt = get_role_idx(id, parse_policy);
if(rt < 0) {
snprintf(errormsg, sizeof(errormsg), "Role %s is not defined in policy.", id);
yyerror(errormsg);
free(id);
free(scontext);
return NULL;
}
free(id);
scontext->role = rt;
/* type */
id = queue_remove(id_queue);
if (!id) {
yyerror("Security context missing type?");
free(scontext);
return NULL;
}
rt = get_type_idx(id, parse_policy);
if(rt < 0) {
snprintf(errormsg, sizeof(errormsg), "Type %s is not defined in policy.", id);
yyerror(errormsg);
free(id);
free(scontext);
return NULL;
}
free(id);
scontext->type = rt;
#ifdef CONFIG_SECURITY_SELINUX_MLS
{
int l;
id = queue_remove(id_queue); free(id);
for (l = 0; l < 2; l++) {
while ((id = queue_remove(id_queue))) {
free(id);
}
}
}
#endif
return scontext;
}
static int define_initial_sid_context(void)
{
char *id;
int idx;
security_con_t *scontext;
if (pass == 1 || (pass == 2 && !(parse_policy->opts & POLOPT_INITIAL_SIDS))){
id = (char *) queue_remove(id_queue);
parse_security_context(1);
free(id);
return 0;
}
id = (char *) queue_remove(id_queue);
if (!id) {
yyerror("no sid name for SID context definition?");
return -1;
}
if(!is_valid_str_sz(id)) {
snprintf(errormsg, sizeof(errormsg), "string \"%s\" exceeds APOL_SZ_SIZE", id);
yyerror(errormsg);
free(id);
return -1;
}
idx = get_initial_sid_idx(id, parse_policy);
if(idx < 0) {
snprintf(errormsg, sizeof(errormsg), "%s is not a valid initial SID name", id);
yyerror(errormsg);
free(id);
return -1;
}
free(id);
scontext = parse_security_context(0);
if(scontext == NULL)
return -1;
if(add_initial_sid_context(idx, scontext, parse_policy) != 0) {
yyerror("problem adding security context to Initial SID");
return -1;
}
return 0;
}
/************************************************************************
*
* Until we decide to include these additional statments in the analysis
* policy database, all we do is free the various ids.
*
************************************************************************/
static int define_sens(void)
{
#ifdef CONFIG_SECURITY_SELINUX_MLS
char *id;
while ((id = queue_remove(id_queue)))
free(id);
return 0;
#else
yyerror("sensitivity definition in non-MLS configuration");
return -1;
#endif
}
static int define_dominance(void)
{
#ifdef CONFIG_SECURITY_SELINUX_MLS
char *id;
while ((id = queue_remove(id_queue)))
free(id);
return 0;
#else
yyerror("dominance definition in non-MLS configuration");
return -1;
#endif
}
static int define_category(void)
{
#ifdef CONFIG_SECURITY_SELINUX_MLS
char *id;
while ((id = queue_remove(id_queue)))
free(id);
return 0;
#else
yyerror("category definition in non-MLS configuration");
return -1;
#endif
}
static int define_level(void)
{
#ifdef CONFIG_SECURITY_SELINUX_MLS
char *id;
while ((id = queue_remove(id_queue)))
free(id);
return 0;
#else
yyerror("level definition in non-MLS configuration");
return -1;
#endif
}
static int define_common_base(void)
{
#ifdef CONFIG_SECURITY_SELINUX_MLS
char *id;
id = queue_remove(id_queue); free(id);
while ((id = queue_remove(id_queue))) {
free(id);
while ((id = queue_remove(id_queue))) {
free(id);
}
}
return 0;
#else
yyerror("MLS base permission definition in non-MLS configuration");
return -1;
#endif
}
/* #ifdef CONFIG_SECURITY_SELINUX_MLS*/
#if 0
static int common_base_set(hashtab_key_t key, hashtab_datum_t datum, void *p)
{
return 0;
}
#endif
static int define_av_base(void)
{
#ifdef CONFIG_SECURITY_SELINUX_MLS
char *id;
id = queue_remove(id_queue); free(id);
while ((id = queue_remove(id_queue))) {
free(id);
while ((id = queue_remove(id_queue))) {
free(id);
}
}
return 0;
#else
yyerror("MLS base permission definition in non-MLS configuration");
return -1;
#endif
}
static int define_constraint(void)
{
char *id;
while ((id = queue_remove(id_queue)))
free(id);
while ((id = queue_remove(id_queue)))
free(id);
return 0;
}
static constraint_expr_t *
define_cexpr(__u32 expr_type, __u32 arg1, __u32 arg2)
{
char *id;
if (expr_type == CEXPR_NAMES) {
while ((id = queue_remove(id_queue)))
free(id);
}
return (constraint_expr_t *)1; /* any non-NULL value */
}
static int define_fs_context(int ver)
{
int rt;
rt = set_policy_version(ver, parse_policy);
if(rt != 0) {
yyerror("error setting policy version");
return -1;
}
parse_security_context(1);
parse_security_context(1);
return 0;
}
static int define_port_context(int ver)
{
char *id;
int rt;
rt = set_policy_version(ver, parse_policy);
if(rt != 0) {
yyerror("error setting policy version");
return -1;
}
id = (char *) queue_remove(id_queue);
free(id);
parse_security_context(1);
return 0;
}
static int define_netif_context(int ver)
{
int rt;
rt = set_policy_version(ver, parse_policy);
if(rt != 0) {
yyerror("error setting policy version");
return -1;
}
free(queue_remove(id_queue));
parse_security_context(1);
parse_security_context(1);
return 0;
}
static int define_ipv4_node_context(int ver)
{
int rt;
rt = set_policy_version(ver, parse_policy);
if(rt != 0) {
yyerror("error setting policy version");
return -1;
}
parse_security_context(1);
return 0;
}
static int define_ipv6_node_context(int ver)
{
int rt;
rt = set_policy_version(ver, parse_policy);
if(rt != 0) {
yyerror("error setting policy version");
return -1;
}
free(queue_remove(id_queue));
free(queue_remove(id_queue));
parse_security_context(1);
return 0;
}
/* removed Jul 2002 */
static int define_devfs_context(int has_type)
{
int rt;
rt = set_policy_version(POL_VER_PREJUL2002, parse_policy);
if(rt != 0) {
yyerror("error setting policy version");
return -1;
}
free(queue_remove(id_queue));
if (has_type)
free(queue_remove(id_queue));
parse_security_context(1);
return 0;
}
/* removed Jul 2002 */
static int define_nfs_context(void)
{
int rt;
rt = set_policy_version(POL_VER_PREJUL2002, parse_policy);
if(rt != 0) {
yyerror("error setting policy version");
return -1;
}
parse_security_context(1);
return 0;
}
/* added Jul 2002 */
/* changed Jul 2003; added FSUSEXATTR */
static int define_fs_use(int behavior, int ver)
{
int rt;
rt = set_policy_version(ver, parse_policy);
if(rt != 0) {
yyerror("error setting policy version)");
return -1;
}
free(queue_remove(id_queue));
if(behavior != 0)
parse_security_context(1);
return 0;
}
/* added Jul 2002 */
static int define_genfs_context_helper(char *fstype, int has_type)
{
int rt;
rt = set_policy_version(POL_VER_JUL2002, parse_policy);
if(rt != 0) {
yyerror("error setting policy version");
return -1;
}
free(fstype);
free(queue_remove(id_queue));
if (has_type)
free(queue_remove(id_queue));
parse_security_context(1);
return 0;
}
/* added Jul 2002 */
static int define_genfs_context(int has_type)
{
return define_genfs_context_helper(queue_remove(id_queue), has_type);
}
|