Loading...
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 | /*
* Modifications for Lustre
*
* Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
*
* Copyright (c) 2011, 2012, Intel Corporation.
*
* Author: Eric Mei <ericm@clusterfs.com>
*/
/*
* linux/net/sunrpc/auth_gss.c
*
* RPCSEC_GSS client authentication.
*
* Copyright (c) 2000 The Regents of the University of Michigan.
* All rights reserved.
*
* Dug Song <dugsong@monkey.org>
* Andy Adamson <andros@umich.edu>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#define DEBUG_SUBSYSTEM S_SEC
#include <linux/init.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/dcache.h>
#include <linux/fs.h>
#include <linux/mutex.h>
#include <asm/atomic.h>
#include <obd.h>
#include <obd_class.h>
#include <obd_support.h>
#include <obd_cksum.h>
#include <lustre/lustre_idl.h>
#include <lustre_net.h>
#include <lustre_import.h>
#include <lustre_sec.h>
#include "gss_err.h"
#include "gss_internal.h"
#include "gss_api.h"
#include <linux/crypto.h>
#include <linux/crc32.h>
/*
* early reply have fixed size, respectively in privacy and integrity mode.
* so we calculate them only once.
*/
static int gss_at_reply_off_integ;
static int gss_at_reply_off_priv;
static inline int msg_last_segidx(struct lustre_msg *msg)
{
LASSERT(msg->lm_bufcount > 0);
return msg->lm_bufcount - 1;
}
static inline int msg_last_seglen(struct lustre_msg *msg)
{
return msg->lm_buflens[msg_last_segidx(msg)];
}
/********************************************
* wire data swabber *
********************************************/
static
void gss_header_swabber(struct gss_header *ghdr)
{
__swab32s(&ghdr->gh_flags);
__swab32s(&ghdr->gh_proc);
__swab32s(&ghdr->gh_seq);
__swab32s(&ghdr->gh_svc);
__swab32s(&ghdr->gh_pad1);
__swab32s(&ghdr->gh_handle.len);
}
struct gss_header *gss_swab_header(struct lustre_msg *msg, int segment,
int swabbed)
{
struct gss_header *ghdr;
ghdr = lustre_msg_buf(msg, segment, sizeof(*ghdr));
if (ghdr == NULL)
return NULL;
if (swabbed)
gss_header_swabber(ghdr);
if (sizeof(*ghdr) + ghdr->gh_handle.len > msg->lm_buflens[segment]) {
CERROR("gss header has length %d, now %u received\n",
(int) sizeof(*ghdr) + ghdr->gh_handle.len,
msg->lm_buflens[segment]);
return NULL;
}
return ghdr;
}
#if 0
static
void gss_netobj_swabber(netobj_t *obj)
{
__swab32s(&obj->len);
}
netobj_t *gss_swab_netobj(struct lustre_msg *msg, int segment)
{
netobj_t *obj;
obj = lustre_swab_buf(msg, segment, sizeof(*obj), gss_netobj_swabber);
if (obj && sizeof(*obj) + obj->len > msg->lm_buflens[segment]) {
CERROR("netobj require length %u but only %u received\n",
(unsigned int) sizeof(*obj) + obj->len,
msg->lm_buflens[segment]);
return NULL;
}
return obj;
}
#endif
/*
* payload should be obtained from mechanism. but currently since we
* only support kerberos, we could simply use fixed value.
* krb5 "meta" data:
* - krb5 header: 16
* - krb5 checksum: 20
*
* for privacy mode, payload also include the cipher text which has the same
* size as plain text, plus possible confounder, padding both at maximum cipher
* block size.
*/
#define GSS_KRB5_INTEG_MAX_PAYLOAD (40)
static inline
int gss_mech_payload(struct gss_ctx *mechctx, int msgsize, int privacy)
{
if (privacy)
return GSS_KRB5_INTEG_MAX_PAYLOAD + 16 + 16 + 16 + msgsize;
else
return GSS_KRB5_INTEG_MAX_PAYLOAD;
}
/*
* return signature size, otherwise < 0 to indicate error
*/
static int gss_sign_msg(struct lustre_msg *msg,
struct gss_ctx *mechctx,
enum lustre_sec_part sp,
__u32 flags, __u32 proc, __u32 seq, __u32 svc,
rawobj_t *handle)
{
struct gss_header *ghdr;
rawobj_t text[4], mic;
int textcnt, max_textcnt, mic_idx;
__u32 major;
LASSERT(msg->lm_bufcount >= 2);
/* gss hdr */
LASSERT(msg->lm_buflens[0] >=
sizeof(*ghdr) + (handle ? handle->len : 0));
ghdr = lustre_msg_buf(msg, 0, 0);
ghdr->gh_version = PTLRPC_GSS_VERSION;
ghdr->gh_sp = (__u8) sp;
ghdr->gh_flags = flags;
ghdr->gh_proc = proc;
ghdr->gh_seq = seq;
ghdr->gh_svc = svc;
if (!handle) {
/* fill in a fake one */
ghdr->gh_handle.len = 0;
} else {
ghdr->gh_handle.len = handle->len;
memcpy(ghdr->gh_handle.data, handle->data, handle->len);
}
/* no actual signature for null mode */
if (svc == SPTLRPC_SVC_NULL)
return lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
/* MIC */
mic_idx = msg_last_segidx(msg);
max_textcnt = (svc == SPTLRPC_SVC_AUTH) ? 1 : mic_idx;
for (textcnt = 0; textcnt < max_textcnt; textcnt++) {
text[textcnt].len = msg->lm_buflens[textcnt];
text[textcnt].data = lustre_msg_buf(msg, textcnt, 0);
}
mic.len = msg->lm_buflens[mic_idx];
mic.data = lustre_msg_buf(msg, mic_idx, 0);
major = lgss_get_mic(mechctx, textcnt, text, 0, NULL, &mic);
if (major != GSS_S_COMPLETE) {
CERROR("fail to generate MIC: %08x\n", major);
return -EPERM;
}
LASSERT(mic.len <= msg->lm_buflens[mic_idx]);
return lustre_shrink_msg(msg, mic_idx, mic.len, 0);
}
/*
* return gss error
*/
static
__u32 gss_verify_msg(struct lustre_msg *msg,
struct gss_ctx *mechctx,
__u32 svc)
{
rawobj_t text[4], mic;
int textcnt, max_textcnt;
int mic_idx;
__u32 major;
LASSERT(msg->lm_bufcount >= 2);
if (svc == SPTLRPC_SVC_NULL)
return GSS_S_COMPLETE;
mic_idx = msg_last_segidx(msg);
max_textcnt = (svc == SPTLRPC_SVC_AUTH) ? 1 : mic_idx;
for (textcnt = 0; textcnt < max_textcnt; textcnt++) {
text[textcnt].len = msg->lm_buflens[textcnt];
text[textcnt].data = lustre_msg_buf(msg, textcnt, 0);
}
mic.len = msg->lm_buflens[mic_idx];
mic.data = lustre_msg_buf(msg, mic_idx, 0);
major = lgss_verify_mic(mechctx, textcnt, text, 0, NULL, &mic);
if (major != GSS_S_COMPLETE)
CERROR("mic verify error: %08x\n", major);
return major;
}
/*
* return gss error code
*/
static
__u32 gss_unseal_msg(struct gss_ctx *mechctx,
struct lustre_msg *msgbuf,
int *msg_len, int msgbuf_len)
{
rawobj_t clear_obj, hdrobj, token;
__u8 *clear_buf;
int clear_buflen;
__u32 major;
if (msgbuf->lm_bufcount != 2) {
CERROR("invalid bufcount %d\n", msgbuf->lm_bufcount);
return GSS_S_FAILURE;
}
/* allocate a temporary clear text buffer, same sized as token,
* we assume the final clear text size <= token size */
clear_buflen = lustre_msg_buflen(msgbuf, 1);
OBD_ALLOC_LARGE(clear_buf, clear_buflen);
if (!clear_buf)
return GSS_S_FAILURE;
/* buffer objects */
hdrobj.len = lustre_msg_buflen(msgbuf, 0);
hdrobj.data = lustre_msg_buf(msgbuf, 0, 0);
token.len = lustre_msg_buflen(msgbuf, 1);
token.data = lustre_msg_buf(msgbuf, 1, 0);
clear_obj.len = clear_buflen;
clear_obj.data = clear_buf;
major = lgss_unwrap(mechctx, &hdrobj, &token, &clear_obj);
if (major != GSS_S_COMPLETE) {
CERROR("unwrap message error: %08x\n", major);
GOTO(out_free, major = GSS_S_FAILURE);
}
LASSERT(clear_obj.len <= clear_buflen);
LASSERT(clear_obj.len <= msgbuf_len);
/* now the decrypted message */
memcpy(msgbuf, clear_obj.data, clear_obj.len);
*msg_len = clear_obj.len;
major = GSS_S_COMPLETE;
out_free:
OBD_FREE_LARGE(clear_buf, clear_buflen);
return major;
}
/********************************************
* gss client context manipulation helpers *
********************************************/
int cli_ctx_expire(struct ptlrpc_cli_ctx *ctx)
{
LASSERT(atomic_read(&ctx->cc_refcount));
if (!test_and_set_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags)) {
if (!ctx->cc_early_expire)
clear_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
CWARN("ctx %p(%u->%s) get expired: %lu(%+lds)\n",
ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec),
ctx->cc_expire,
ctx->cc_expire == 0 ? 0 :
cfs_time_sub(ctx->cc_expire, cfs_time_current_sec()));
sptlrpc_cli_ctx_wakeup(ctx);
return 1;
}
return 0;
}
/*
* return 1 if the context is dead.
*/
int cli_ctx_check_death(struct ptlrpc_cli_ctx *ctx)
{
if (unlikely(cli_ctx_is_dead(ctx)))
return 1;
/* expire is 0 means never expire. a newly created gss context
* which during upcall may has 0 expiration */
if (ctx->cc_expire == 0)
return 0;
/* check real expiration */
if (cfs_time_after(ctx->cc_expire, cfs_time_current_sec()))
return 0;
cli_ctx_expire(ctx);
return 1;
}
void gss_cli_ctx_uptodate(struct gss_cli_ctx *gctx)
{
struct ptlrpc_cli_ctx *ctx = &gctx->gc_base;
unsigned long ctx_expiry;
if (lgss_inquire_context(gctx->gc_mechctx, &ctx_expiry)) {
CERROR("ctx %p(%u): unable to inquire, expire it now\n",
gctx, ctx->cc_vcred.vc_uid);
ctx_expiry = 1; /* make it expired now */
}
ctx->cc_expire = gss_round_ctx_expiry(ctx_expiry,
ctx->cc_sec->ps_flvr.sf_flags);
/* At this point this ctx might have been marked as dead by
* someone else, in which case nobody will make further use
* of it. we don't care, and mark it UPTODATE will help
* destroying server side context when it be destroied. */
set_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
if (sec_is_reverse(ctx->cc_sec)) {
CWARN("server installed reverse ctx %p idx "LPX64", "
"expiry %lu(%+lds)\n", ctx,
gss_handle_to_u64(&gctx->gc_handle),
ctx->cc_expire, ctx->cc_expire - cfs_time_current_sec());
} else {
CWARN("client refreshed ctx %p idx "LPX64" (%u->%s), "
"expiry %lu(%+lds)\n", ctx,
gss_handle_to_u64(&gctx->gc_handle),
ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec),
ctx->cc_expire, ctx->cc_expire - cfs_time_current_sec());
/* install reverse svc ctx for root context */
if (ctx->cc_vcred.vc_uid == 0)
gss_sec_install_rctx(ctx->cc_sec->ps_import,
ctx->cc_sec, ctx);
}
sptlrpc_cli_ctx_wakeup(ctx);
}
static void gss_cli_ctx_finalize(struct gss_cli_ctx *gctx)
{
LASSERT(gctx->gc_base.cc_sec);
if (gctx->gc_mechctx) {
lgss_delete_sec_context(&gctx->gc_mechctx);
gctx->gc_mechctx = NULL;
}
if (!rawobj_empty(&gctx->gc_svc_handle)) {
/* forward ctx: mark buddy reverse svcctx soon-expire. */
if (!sec_is_reverse(gctx->gc_base.cc_sec) &&
!rawobj_empty(&gctx->gc_svc_handle))
gss_svc_upcall_expire_rvs_ctx(&gctx->gc_svc_handle);
rawobj_free(&gctx->gc_svc_handle);
}
rawobj_free(&gctx->gc_handle);
}
/*
* Based on sequence number algorithm as specified in RFC 2203.
*
* modified for our own problem: arriving request has valid sequence number,
* but unwrapping request might cost a long time, after that its sequence
* are not valid anymore (fall behind the window). It rarely happen, mostly
* under extreme load.
*
* note we should not check sequence before verify the integrity of incoming
* request, because just one attacking request with high sequence number might
* cause all following request be dropped.
*
* so here we use a multi-phase approach: prepare 2 sequence windows,
* "main window" for normal sequence and "back window" for fall behind sequence.
* and 3-phase checking mechanism:
* 0 - before integrity verification, perform a initial sequence checking in
* main window, which only try and don't actually set any bits. if the
* sequence is high above the window or fit in the window and the bit
* is 0, then accept and proceed to integrity verification. otherwise
* reject this sequence.
* 1 - after integrity verification, check in main window again. if this
* sequence is high above the window or fit in the window and the bit
* is 0, then set the bit and accept; if it fit in the window but bit
* already set, then reject; if it fall behind the window, then proceed
* to phase 2.
* 2 - check in back window. if it is high above the window or fit in the
* window and the bit is 0, then set the bit and accept. otherwise reject.
*
* return value:
* 1: looks like a replay
* 0: is ok
* -1: is a replay
*
* note phase 0 is necessary, because otherwise replay attacking request of
* sequence which between the 2 windows can't be detected.
*
* this mechanism can't totally solve the problem, but could help much less
* number of valid requests be dropped.
*/
static
int gss_do_check_seq(unsigned long *window, __u32 win_size, __u32 *max_seq,
__u32 seq_num, int phase)
{
LASSERT(phase >= 0 && phase <= 2);
if (seq_num > *max_seq) {
/*
* 1. high above the window
*/
if (phase == 0)
return 0;
if (seq_num >= *max_seq + win_size) {
memset(window, 0, win_size / 8);
*max_seq = seq_num;
} else {
while(*max_seq < seq_num) {
(*max_seq)++;
__clear_bit((*max_seq) % win_size, window);
}
}
__set_bit(seq_num % win_size, window);
} else if (seq_num + win_size <= *max_seq) {
/*
* 2. low behind the window
*/
if (phase == 0 || phase == 2)
goto replay;
CWARN("seq %u is %u behind (size %d), check backup window\n",
seq_num, *max_seq - win_size - seq_num, win_size);
return 1;
} else {
/*
* 3. fit into the window
*/
switch (phase) {
case 0:
if (test_bit(seq_num % win_size, window))
goto replay;
break;
case 1:
case 2:
if (__test_and_set_bit(seq_num % win_size, window))
goto replay;
break;
}
}
return 0;
replay:
CERROR("seq %u (%s %s window) is a replay: max %u, winsize %d\n",
seq_num,
seq_num + win_size > *max_seq ? "in" : "behind",
phase == 2 ? "backup " : "main",
*max_seq, win_size);
return -1;
}
/*
* Based on sequence number algorithm as specified in RFC 2203.
*
* if @set == 0: initial check, don't set any bit in window
* if @sec == 1: final check, set bit in window
*/
int gss_check_seq_num(struct gss_svc_seq_data *ssd, __u32 seq_num, int set)
{
int rc = 0;
spin_lock(&ssd->ssd_lock);
if (set == 0) {
/*
* phase 0 testing
*/
rc = gss_do_check_seq(ssd->ssd_win_main, GSS_SEQ_WIN_MAIN,
&ssd->ssd_max_main, seq_num, 0);
if (unlikely(rc))
gss_stat_oos_record_svc(0, 1);
} else {
/*
* phase 1 checking main window
*/
rc = gss_do_check_seq(ssd->ssd_win_main, GSS_SEQ_WIN_MAIN,
&ssd->ssd_max_main, seq_num, 1);
switch (rc) {
case -1:
gss_stat_oos_record_svc(1, 1);
/* fall through */
case 0:
goto exit;
}
/*
* phase 2 checking back window
*/
rc = gss_do_check_seq(ssd->ssd_win_back, GSS_SEQ_WIN_BACK,
&ssd->ssd_max_back, seq_num, 2);
if (rc)
gss_stat_oos_record_svc(2, 1);
else
gss_stat_oos_record_svc(2, 0);
}
exit:
spin_unlock(&ssd->ssd_lock);
return rc;
}
/***************************************
* cred APIs *
***************************************/
static inline int gss_cli_payload(struct ptlrpc_cli_ctx *ctx,
int msgsize, int privacy)
{
return gss_mech_payload(NULL, msgsize, privacy);
}
static int gss_cli_bulk_payload(struct ptlrpc_cli_ctx *ctx,
struct sptlrpc_flavor *flvr,
int reply, int read)
{
int payload = sizeof(struct ptlrpc_bulk_sec_desc);
LASSERT(SPTLRPC_FLVR_BULK_TYPE(flvr->sf_rpc) == SPTLRPC_BULK_DEFAULT);
if ((!reply && !read) || (reply && read)) {
switch (SPTLRPC_FLVR_BULK_SVC(flvr->sf_rpc)) {
case SPTLRPC_BULK_SVC_NULL:
break;
case SPTLRPC_BULK_SVC_INTG:
payload += gss_cli_payload(ctx, 0, 0);
break;
case SPTLRPC_BULK_SVC_PRIV:
payload += gss_cli_payload(ctx, 0, 1);
break;
case SPTLRPC_BULK_SVC_AUTH:
default:
LBUG();
}
}
return payload;
}
int gss_cli_ctx_match(struct ptlrpc_cli_ctx *ctx, struct vfs_cred *vcred)
{
return (ctx->cc_vcred.vc_uid == vcred->vc_uid);
}
void gss_cli_ctx_flags2str(unsigned long flags, char *buf, int bufsize)
{
buf[0] = '\0';
if (flags & PTLRPC_CTX_NEW)
strncat(buf, "new,", bufsize);
if (flags & PTLRPC_CTX_UPTODATE)
strncat(buf, "uptodate,", bufsize);
if (flags & PTLRPC_CTX_DEAD)
strncat(buf, "dead,", bufsize);
if (flags & PTLRPC_CTX_ERROR)
strncat(buf, "error,", bufsize);
if (flags & PTLRPC_CTX_CACHED)
strncat(buf, "cached,", bufsize);
if (flags & PTLRPC_CTX_ETERNAL)
strncat(buf, "eternal,", bufsize);
if (buf[0] == '\0')
strncat(buf, "-,", bufsize);
buf[strlen(buf) - 1] = '\0';
}
int gss_cli_ctx_sign(struct ptlrpc_cli_ctx *ctx,
struct ptlrpc_request *req)
{
struct gss_cli_ctx *gctx = ctx2gctx(ctx);
__u32 flags = 0, seq, svc;
int rc;
LASSERT(req->rq_reqbuf);
LASSERT(req->rq_reqbuf->lm_bufcount >= 2);
LASSERT(req->rq_cli_ctx == ctx);
/* nothing to do for context negotiation RPCs */
if (req->rq_ctx_init)
return 0;
svc = SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc);
if (req->rq_pack_bulk)
flags |= LUSTRE_GSS_PACK_BULK;
if (req->rq_pack_udesc)
flags |= LUSTRE_GSS_PACK_USER;
redo:
seq = atomic_inc_return(&gctx->gc_seq);
rc = gss_sign_msg(req->rq_reqbuf, gctx->gc_mechctx,
ctx->cc_sec->ps_part,
flags, gctx->gc_proc, seq, svc,
&gctx->gc_handle);
if (rc < 0)
return rc;
/* gss_sign_msg() msg might take long time to finish, in which period
* more rpcs could be wrapped up and sent out. if we found too many
* of them we should repack this rpc, because sent it too late might
* lead to the sequence number fall behind the window on server and
* be dropped. also applies to gss_cli_ctx_seal().
*
* Note: null mode dosen't check sequence number. */
if (svc != SPTLRPC_SVC_NULL &&
atomic_read(&gctx->gc_seq) - seq > GSS_SEQ_REPACK_THRESHOLD) {
int behind = atomic_read(&gctx->gc_seq) - seq;
gss_stat_oos_record_cli(behind);
CWARN("req %p: %u behind, retry signing\n", req, behind);
goto redo;
}
req->rq_reqdata_len = rc;
return 0;
}
static
int gss_cli_ctx_handle_err_notify(struct ptlrpc_cli_ctx *ctx,
struct ptlrpc_request *req,
struct gss_header *ghdr)
{
struct gss_err_header *errhdr;
int rc;
LASSERT(ghdr->gh_proc == PTLRPC_GSS_PROC_ERR);
errhdr = (struct gss_err_header *) ghdr;
CWARN("req x"LPU64"/t"LPU64", ctx %p idx "LPX64"(%u->%s): "
"%sserver respond (%08x/%08x)\n",
req->rq_xid, req->rq_transno, ctx,
gss_handle_to_u64(&ctx2gctx(ctx)->gc_handle),
ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec),
sec_is_reverse(ctx->cc_sec) ? "reverse" : "",
errhdr->gh_major, errhdr->gh_minor);
/* context fini rpc, let it failed */
if (req->rq_ctx_fini) {
CWARN("context fini rpc failed\n");
return -EINVAL;
}
/* reverse sec, just return error, don't expire this ctx because it's
* crucial to callback rpcs. note if the callback rpc failed because
* of bit flip during network transfer, the client will be evicted
* directly. so more gracefully we probably want let it retry for
* number of times. */
if (sec_is_reverse(ctx->cc_sec))
return -EINVAL;
if (errhdr->gh_major != GSS_S_NO_CONTEXT &&
errhdr->gh_major != GSS_S_BAD_SIG)
return -EACCES;
/* server return NO_CONTEXT might be caused by context expire
* or server reboot/failover. we try to refresh a new ctx which
* be transparent to upper layer.
*
* In some cases, our gss handle is possible to be incidentally
* identical to another handle since the handle itself is not
* fully random. In krb5 case, the GSS_S_BAD_SIG will be
* returned, maybe other gss error for other mechanism.
*
* if we add new mechanism, make sure the correct error are
* returned in this case. */
CWARN("%s: server might lost the context, retrying\n",
errhdr->gh_major == GSS_S_NO_CONTEXT ? "NO_CONTEXT" : "BAD_SIG");
sptlrpc_cli_ctx_expire(ctx);
/* we need replace the ctx right here, otherwise during
* resent we'll hit the logic in sptlrpc_req_refresh_ctx()
* which keep the ctx with RESEND flag, thus we'll never
* get rid of this ctx. */
rc = sptlrpc_req_replace_dead_ctx(req);
if (rc == 0)
req->rq_resend = 1;
return rc;
}
int gss_cli_ctx_verify(struct ptlrpc_cli_ctx *ctx,
struct ptlrpc_request *req)
{
struct gss_cli_ctx *gctx;
struct gss_header *ghdr, *reqhdr;
struct lustre_msg *msg = req->rq_repdata;
__u32 major;
int pack_bulk, swabbed, rc = 0;
LASSERT(req->rq_cli_ctx == ctx);
LASSERT(msg);
gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
/* special case for context negotiation, rq_repmsg/rq_replen actually
* are not used currently. but early reply always be treated normally */
if (req->rq_ctx_init && !req->rq_early) {
req->rq_repmsg = lustre_msg_buf(msg, 1, 0);
req->rq_replen = msg->lm_buflens[1];
return 0;
}
if (msg->lm_bufcount < 2 || msg->lm_bufcount > 4) {
CERROR("unexpected bufcount %u\n", msg->lm_bufcount);
return -EPROTO;
}
swabbed = ptlrpc_rep_need_swab(req);
ghdr = gss_swab_header(msg, 0, swabbed);
if (ghdr == NULL) {
CERROR("can't decode gss header\n");
return -EPROTO;
}
/* sanity checks */
reqhdr = lustre_msg_buf(msg, 0, sizeof(*reqhdr));
LASSERT(reqhdr);
if (ghdr->gh_version != reqhdr->gh_version) {
CERROR("gss version %u mismatch, expect %u\n",
ghdr->gh_version, reqhdr->gh_version);
return -EPROTO;
}
switch (ghdr->gh_proc) {
case PTLRPC_GSS_PROC_DATA:
pack_bulk = ghdr->gh_flags & LUSTRE_GSS_PACK_BULK;
if (!req->rq_early && !equi(req->rq_pack_bulk == 1, pack_bulk)){
CERROR("%s bulk flag in reply\n",
req->rq_pack_bulk ? "missing" : "unexpected");
return -EPROTO;
}
if (ghdr->gh_seq != reqhdr->gh_seq) {
CERROR("seqnum %u mismatch, expect %u\n",
ghdr->gh_seq, reqhdr->gh_seq);
return -EPROTO;
}
if (ghdr->gh_svc != reqhdr->gh_svc) {
CERROR("svc %u mismatch, expect %u\n",
ghdr->gh_svc, reqhdr->gh_svc);
return -EPROTO;
}
if (swabbed)
gss_header_swabber(ghdr);
major = gss_verify_msg(msg, gctx->gc_mechctx, reqhdr->gh_svc);
if (major != GSS_S_COMPLETE) {
CERROR("failed to verify reply: %x\n", major);
return -EPERM;
}
if (req->rq_early && reqhdr->gh_svc == SPTLRPC_SVC_NULL) {
__u32 cksum;
cksum = crc32_le(!(__u32) 0,
lustre_msg_buf(msg, 1, 0),
lustre_msg_buflen(msg, 1));
if (cksum != msg->lm_cksum) {
CWARN("early reply checksum mismatch: "
"%08x != %08x\n", cksum, msg->lm_cksum);
return -EPROTO;
}
}
if (pack_bulk) {
/* bulk checksum is right after the lustre msg */
if (msg->lm_bufcount < 3) {
CERROR("Invalid reply bufcount %u\n",
msg->lm_bufcount);
return -EPROTO;
}
rc = bulk_sec_desc_unpack(msg, 2, swabbed);
if (rc) {
CERROR("unpack bulk desc: %d\n", rc);
return rc;
}
}
req->rq_repmsg = lustre_msg_buf(msg, 1, 0);
req->rq_replen = msg->lm_buflens[1];
break;
case PTLRPC_GSS_PROC_ERR:
if (req->rq_early) {
CERROR("server return error with early reply\n");
rc = -EPROTO;
} else {
rc = gss_cli_ctx_handle_err_notify(ctx, req, ghdr);
}
break;
default:
CERROR("unknown gss proc %d\n", ghdr->gh_proc);
rc = -EPROTO;
}
return rc;
}
int gss_cli_ctx_seal(struct ptlrpc_cli_ctx *ctx,
struct ptlrpc_request *req)
{
struct gss_cli_ctx *gctx;
rawobj_t hdrobj, msgobj, token;
struct gss_header *ghdr;
__u32 buflens[2], major;
int wiresize, rc;
LASSERT(req->rq_clrbuf);
LASSERT(req->rq_cli_ctx == ctx);
LASSERT(req->rq_reqlen);
gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
/* final clear data length */
req->rq_clrdata_len = lustre_msg_size_v2(req->rq_clrbuf->lm_bufcount,
req->rq_clrbuf->lm_buflens);
/* calculate wire data length */
buflens[0] = PTLRPC_GSS_HEADER_SIZE;
buflens[1] = gss_cli_payload(&gctx->gc_base, req->rq_clrdata_len, 1);
wiresize = lustre_msg_size_v2(2, buflens);
/* allocate wire buffer */
if (req->rq_pool) {
/* pre-allocated */
LASSERT(req->rq_reqbuf);
LASSERT(req->rq_reqbuf != req->rq_clrbuf);
LASSERT(req->rq_reqbuf_len >= wiresize);
} else {
OBD_ALLOC_LARGE(req->rq_reqbuf, wiresize);
if (!req->rq_reqbuf)
return -ENOMEM;
req->rq_reqbuf_len = wiresize;
}
lustre_init_msg_v2(req->rq_reqbuf, 2, buflens, NULL);
req->rq_reqbuf->lm_secflvr = req->rq_flvr.sf_rpc;
/* gss header */
ghdr = lustre_msg_buf(req->rq_reqbuf, 0, 0);
ghdr->gh_version = PTLRPC_GSS_VERSION;
ghdr->gh_sp = (__u8) ctx->cc_sec->ps_part;
ghdr->gh_flags = 0;
ghdr->gh_proc = gctx->gc_proc;
ghdr->gh_svc = SPTLRPC_SVC_PRIV;
ghdr->gh_handle.len = gctx->gc_handle.len;
memcpy(ghdr->gh_handle.data, gctx->gc_handle.data, gctx->gc_handle.len);
if (req->rq_pack_bulk)
ghdr->gh_flags |= LUSTRE_GSS_PACK_BULK;
if (req->rq_pack_udesc)
ghdr->gh_flags |= LUSTRE_GSS_PACK_USER;
redo:
ghdr->gh_seq = atomic_inc_return(&gctx->gc_seq);
/* buffer objects */
hdrobj.len = PTLRPC_GSS_HEADER_SIZE;
hdrobj.data = (__u8 *) ghdr;
msgobj.len = req->rq_clrdata_len;
msgobj.data = (__u8 *) req->rq_clrbuf;
token.len = lustre_msg_buflen(req->rq_reqbuf, 1);
token.data = lustre_msg_buf(req->rq_reqbuf, 1, 0);
major = lgss_wrap(gctx->gc_mechctx, &hdrobj, &msgobj,
req->rq_clrbuf_len, &token);
if (major != GSS_S_COMPLETE) {
CERROR("priv: wrap message error: %08x\n", major);
GOTO(err_free, rc = -EPERM);
}
LASSERT(token.len <= buflens[1]);
/* see explain in gss_cli_ctx_sign() */
if (unlikely(atomic_read(&gctx->gc_seq) - ghdr->gh_seq >
GSS_SEQ_REPACK_THRESHOLD)) {
int behind = atomic_read(&gctx->gc_seq) - ghdr->gh_seq;
gss_stat_oos_record_cli(behind);
CWARN("req %p: %u behind, retry sealing\n", req, behind);
ghdr->gh_seq = atomic_inc_return(&gctx->gc_seq);
goto redo;
}
/* now set the final wire data length */
req->rq_reqdata_len = lustre_shrink_msg(req->rq_reqbuf, 1, token.len,0);
return 0;
err_free:
if (!req->rq_pool) {
OBD_FREE_LARGE(req->rq_reqbuf, req->rq_reqbuf_len);
req->rq_reqbuf = NULL;
req->rq_reqbuf_len = 0;
}
return rc;
}
int gss_cli_ctx_unseal(struct ptlrpc_cli_ctx *ctx,
struct ptlrpc_request *req)
{
struct gss_cli_ctx *gctx;
struct gss_header *ghdr;
struct lustre_msg *msg = req->rq_repdata;
int msglen, pack_bulk, swabbed, rc;
__u32 major;
LASSERT(req->rq_cli_ctx == ctx);
LASSERT(req->rq_ctx_init == 0);
LASSERT(msg);
gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
swabbed = ptlrpc_rep_need_swab(req);
ghdr = gss_swab_header(msg, 0, swabbed);
if (ghdr == NULL) {
CERROR("can't decode gss header\n");
return -EPROTO;
}
/* sanity checks */
if (ghdr->gh_version != PTLRPC_GSS_VERSION) {
CERROR("gss version %u mismatch, expect %u\n",
ghdr->gh_version, PTLRPC_GSS_VERSION);
return -EPROTO;
}
switch (ghdr->gh_proc) {
case PTLRPC_GSS_PROC_DATA:
pack_bulk = ghdr->gh_flags & LUSTRE_GSS_PACK_BULK;
if (!req->rq_early && !equi(req->rq_pack_bulk == 1, pack_bulk)){
CERROR("%s bulk flag in reply\n",
req->rq_pack_bulk ? "missing" : "unexpected");
return -EPROTO;
}
if (swabbed)
gss_header_swabber(ghdr);
/* use rq_repdata_len as buffer size, which assume unseal
* doesn't need extra memory space. for precise control, we'd
* better calculate out actual buffer size as
* (repbuf_len - offset - repdata_len) */
major = gss_unseal_msg(gctx->gc_mechctx, msg,
&msglen, req->rq_repdata_len);
if (major != GSS_S_COMPLETE) {
CERROR("failed to unwrap reply: %x\n", major);
rc = -EPERM;
break;
}
swabbed = __lustre_unpack_msg(msg, msglen);
if (swabbed < 0) {
CERROR("Failed to unpack after decryption\n");
return -EPROTO;
}
if (msg->lm_bufcount < 1) {
CERROR("Invalid reply buffer: empty\n");
return -EPROTO;
}
if (pack_bulk) {
if (msg->lm_bufcount < 2) {
CERROR("bufcount %u: missing bulk sec desc\n",
msg->lm_bufcount);
return -EPROTO;
}
/* bulk checksum is the last segment */
if (bulk_sec_desc_unpack(msg, msg->lm_bufcount - 1,
swabbed))
return -EPROTO;
}
req->rq_repmsg = lustre_msg_buf(msg, 0, 0);
req->rq_replen = msg->lm_buflens[0];
rc = 0;
break;
case PTLRPC_GSS_PROC_ERR:
if (req->rq_early) {
CERROR("server return error with early reply\n");
rc = -EPROTO;
} else {
rc = gss_cli_ctx_handle_err_notify(ctx, req, ghdr);
}
break;
default:
CERROR("unexpected proc %d\n", ghdr->gh_proc);
rc = -EPERM;
}
return rc;
}
/*********************************************
* reverse context installation *
*********************************************/
static inline
int gss_install_rvs_svc_ctx(struct obd_import *imp,
struct gss_sec *gsec,
struct gss_cli_ctx *gctx)
{
return gss_svc_upcall_install_rvs_ctx(imp, gsec, gctx);
}
/*********************************************
* GSS security APIs *
*********************************************/
int gss_sec_create_common(struct gss_sec *gsec,
struct ptlrpc_sec_policy *policy,
struct obd_import *imp,
struct ptlrpc_svc_ctx *svcctx,
struct sptlrpc_flavor *sf)
{
struct ptlrpc_sec *sec;
LASSERT(imp);
LASSERT(SPTLRPC_FLVR_POLICY(sf->sf_rpc) == SPTLRPC_POLICY_GSS);
gsec->gs_mech = lgss_subflavor_to_mech(
SPTLRPC_FLVR_BASE_SUB(sf->sf_rpc));
if (!gsec->gs_mech) {
CERROR("gss backend 0x%x not found\n",
SPTLRPC_FLVR_BASE_SUB(sf->sf_rpc));
return -EOPNOTSUPP;
}
spin_lock_init(&gsec->gs_lock);
gsec->gs_rvs_hdl = 0ULL;
/* initialize upper ptlrpc_sec */
sec = &gsec->gs_base;
sec->ps_policy = policy;
atomic_set(&sec->ps_refcount, 0);
atomic_set(&sec->ps_nctx, 0);
sec->ps_id = sptlrpc_get_next_secid();
sec->ps_flvr = *sf;
sec->ps_import = class_import_get(imp);
spin_lock_init(&sec->ps_lock);
INIT_LIST_HEAD(&sec->ps_gc_list);
if (!svcctx) {
sec->ps_gc_interval = GSS_GC_INTERVAL;
} else {
LASSERT(sec_is_reverse(sec));
/* never do gc on reverse sec */
sec->ps_gc_interval = 0;
}
if (SPTLRPC_FLVR_BULK_SVC(sec->ps_flvr.sf_rpc) == SPTLRPC_BULK_SVC_PRIV)
sptlrpc_enc_pool_add_user();
CDEBUG(D_SEC, "create %s%s@%p\n", (svcctx ? "reverse " : ""),
policy->sp_name, gsec);
return 0;
}
void gss_sec_destroy_common(struct gss_sec *gsec)
{
struct ptlrpc_sec *sec = &gsec->gs_base;
LASSERT(sec->ps_import);
LASSERT(atomic_read(&sec->ps_refcount) == 0);
LASSERT(atomic_read(&sec->ps_nctx) == 0);
if (gsec->gs_mech) {
lgss_mech_put(gsec->gs_mech);
gsec->gs_mech = NULL;
}
class_import_put(sec->ps_import);
if (SPTLRPC_FLVR_BULK_SVC(sec->ps_flvr.sf_rpc) == SPTLRPC_BULK_SVC_PRIV)
sptlrpc_enc_pool_del_user();
}
void gss_sec_kill(struct ptlrpc_sec *sec)
{
sec->ps_dying = 1;
}
int gss_cli_ctx_init_common(struct ptlrpc_sec *sec,
struct ptlrpc_cli_ctx *ctx,
struct ptlrpc_ctx_ops *ctxops,
struct vfs_cred *vcred)
{
struct gss_cli_ctx *gctx = ctx2gctx(ctx);
gctx->gc_win = 0;
atomic_set(&gctx->gc_seq, 0);
INIT_HLIST_NODE(&ctx->cc_cache);
atomic_set(&ctx->cc_refcount, 0);
ctx->cc_sec = sec;
ctx->cc_ops = ctxops;
ctx->cc_expire = 0;
ctx->cc_flags = PTLRPC_CTX_NEW;
ctx->cc_vcred = *vcred;
spin_lock_init(&ctx->cc_lock);
INIT_LIST_HEAD(&ctx->cc_req_list);
INIT_LIST_HEAD(&ctx->cc_gc_chain);
/* take a ref on belonging sec, balanced in ctx destroying */
atomic_inc(&sec->ps_refcount);
/* statistic only */
atomic_inc(&sec->ps_nctx);
CDEBUG(D_SEC, "%s@%p: create ctx %p(%u->%s)\n",
sec->ps_policy->sp_name, ctx->cc_sec,
ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
return 0;
}
/*
* return value:
* 1: the context has been taken care of by someone else
* 0: proceed to really destroy the context locally
*/
int gss_cli_ctx_fini_common(struct ptlrpc_sec *sec,
struct ptlrpc_cli_ctx *ctx)
{
struct gss_cli_ctx *gctx = ctx2gctx(ctx);
LASSERT(atomic_read(&sec->ps_nctx) > 0);
LASSERT(atomic_read(&ctx->cc_refcount) == 0);
LASSERT(ctx->cc_sec == sec);
/*
* remove UPTODATE flag of reverse ctx thus we won't send fini rpc,
* this is to avoid potential problems of client side reverse svc ctx
* be mis-destroyed in various recovery senarios. anyway client can
* manage its reverse ctx well by associating it with its buddy ctx.
*/
if (sec_is_reverse(sec))
ctx->cc_flags &= ~PTLRPC_CTX_UPTODATE;
if (gctx->gc_mechctx) {
/* the final context fini rpc will use this ctx too, and it's
* asynchronous which finished by request_out_callback(). so
* we add refcount, whoever drop finally drop the refcount to
* 0 should responsible for the rest of destroy. */
atomic_inc(&ctx->cc_refcount);
gss_do_ctx_fini_rpc(gctx);
gss_cli_ctx_finalize(gctx);
if (!atomic_dec_and_test(&ctx->cc_refcount))
return 1;
}
if (sec_is_reverse(sec))
CWARN("reverse sec %p: destroy ctx %p\n",
ctx->cc_sec, ctx);
else
CWARN("%s@%p: destroy ctx %p(%u->%s)\n",
sec->ps_policy->sp_name, ctx->cc_sec,
ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
return 0;
}
static
int gss_alloc_reqbuf_intg(struct ptlrpc_sec *sec,
struct ptlrpc_request *req,
int svc, int msgsize)
{
int bufsize, txtsize;
int bufcnt = 2;
__u32 buflens[5];
/*
* on-wire data layout:
* - gss header
* - lustre message
* - user descriptor (optional)
* - bulk sec descriptor (optional)
* - signature (optional)
* - svc == NULL: NULL
* - svc == AUTH: signature of gss header
* - svc == INTG: signature of all above
*
* if this is context negotiation, reserver fixed space
* at the last (signature) segment regardless of svc mode.
*/
buflens[0] = PTLRPC_GSS_HEADER_SIZE;
txtsize = buflens[0];
buflens[1] = msgsize;
if (svc == SPTLRPC_SVC_INTG)
txtsize += buflens[1];
if (req->rq_pack_udesc) {
buflens[bufcnt] = sptlrpc_current_user_desc_size();
if (svc == SPTLRPC_SVC_INTG)
txtsize += buflens[bufcnt];
bufcnt++;
}
if (req->rq_pack_bulk) {
buflens[bufcnt] = gss_cli_bulk_payload(req->rq_cli_ctx,
&req->rq_flvr,
0, req->rq_bulk_read);
if (svc == SPTLRPC_SVC_INTG)
txtsize += buflens[bufcnt];
bufcnt++;
}
if (req->rq_ctx_init)
buflens[bufcnt++] = GSS_CTX_INIT_MAX_LEN;
else if (svc != SPTLRPC_SVC_NULL)
buflens[bufcnt++] = gss_cli_payload(req->rq_cli_ctx, txtsize,0);
bufsize = lustre_msg_size_v2(bufcnt, buflens);
if (!req->rq_reqbuf) {
bufsize = size_roundup_power2(bufsize);
OBD_ALLOC_LARGE(req->rq_reqbuf, bufsize);
if (!req->rq_reqbuf)
return -ENOMEM;
req->rq_reqbuf_len = bufsize;
} else {
LASSERT(req->rq_pool);
LASSERT(req->rq_reqbuf_len >= bufsize);
memset(req->rq_reqbuf, 0, bufsize);
}
lustre_init_msg_v2(req->rq_reqbuf, bufcnt, buflens, NULL);
req->rq_reqbuf->lm_secflvr = req->rq_flvr.sf_rpc;
req->rq_reqmsg = lustre_msg_buf(req->rq_reqbuf, 1, msgsize);
LASSERT(req->rq_reqmsg);
/* pack user desc here, later we might leave current user's process */
if (req->rq_pack_udesc)
sptlrpc_pack_user_desc(req->rq_reqbuf, 2);
return 0;
}
static
int gss_alloc_reqbuf_priv(struct ptlrpc_sec *sec,
struct ptlrpc_request *req,
int msgsize)
{
__u32 ibuflens[3], wbuflens[2];
int ibufcnt;
int clearsize, wiresize;
LASSERT(req->rq_clrbuf == NULL);
LASSERT(req->rq_clrbuf_len == 0);
/* Inner (clear) buffers
* - lustre message
* - user descriptor (optional)
* - bulk checksum (optional)
*/
ibufcnt = 1;
ibuflens[0] = msgsize;
if (req->rq_pack_udesc)
ibuflens[ibufcnt++] = sptlrpc_current_user_desc_size();
if (req->rq_pack_bulk)
ibuflens[ibufcnt++] = gss_cli_bulk_payload(req->rq_cli_ctx,
&req->rq_flvr, 0,
req->rq_bulk_read);
clearsize = lustre_msg_size_v2(ibufcnt, ibuflens);
/* to allow append padding during encryption */
clearsize += GSS_MAX_CIPHER_BLOCK;
/* Wrapper (wire) buffers
* - gss header
* - cipher text
*/
wbuflens[0] = PTLRPC_GSS_HEADER_SIZE;
wbuflens[1] = gss_cli_payload(req->rq_cli_ctx, clearsize, 1);
wiresize = lustre_msg_size_v2(2, wbuflens);
if (req->rq_pool) {
/* rq_reqbuf is preallocated */
LASSERT(req->rq_reqbuf);
LASSERT(req->rq_reqbuf_len >= wiresize);
memset(req->rq_reqbuf, 0, req->rq_reqbuf_len);
/* if the pre-allocated buffer is big enough, we just pack
* both clear buf & request buf in it, to avoid more alloc. */
if (clearsize + wiresize <= req->rq_reqbuf_len) {
req->rq_clrbuf =
(void *) (((char *) req->rq_reqbuf) + wiresize);
} else {
CWARN("pre-allocated buf size %d is not enough for "
"both clear (%d) and cipher (%d) text, proceed "
"with extra allocation\n", req->rq_reqbuf_len,
clearsize, wiresize);
}
}
if (!req->rq_clrbuf) {
clearsize = size_roundup_power2(clearsize);
OBD_ALLOC_LARGE(req->rq_clrbuf, clearsize);
if (!req->rq_clrbuf)
return -ENOMEM;
}
req->rq_clrbuf_len = clearsize;
lustre_init_msg_v2(req->rq_clrbuf, ibufcnt, ibuflens, NULL);
req->rq_reqmsg = lustre_msg_buf(req->rq_clrbuf, 0, msgsize);
if (req->rq_pack_udesc)
sptlrpc_pack_user_desc(req->rq_clrbuf, 1);
return 0;
}
/*
* NOTE: any change of request buffer allocation should also consider
* changing enlarge_reqbuf() series functions.
*/
int gss_alloc_reqbuf(struct ptlrpc_sec *sec,
struct ptlrpc_request *req,
int msgsize)
{
int svc = SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc);
LASSERT(!req->rq_pack_bulk ||
(req->rq_bulk_read || req->rq_bulk_write));
switch (svc) {
case SPTLRPC_SVC_NULL:
case SPTLRPC_SVC_AUTH:
case SPTLRPC_SVC_INTG:
return gss_alloc_reqbuf_intg(sec, req, svc, msgsize);
case SPTLRPC_SVC_PRIV:
return gss_alloc_reqbuf_priv(sec, req, msgsize);
default:
LASSERTF(0, "bad rpc flavor %x\n", req->rq_flvr.sf_rpc);
return 0;
}
}
void gss_free_reqbuf(struct ptlrpc_sec *sec,
struct ptlrpc_request *req)
{
int privacy;
LASSERT(!req->rq_pool || req->rq_reqbuf);
privacy = SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc) == SPTLRPC_SVC_PRIV;
if (!req->rq_clrbuf)
goto release_reqbuf;
/* release clear buffer */
LASSERT(privacy);
LASSERT(req->rq_clrbuf_len);
if (req->rq_pool == NULL ||
req->rq_clrbuf < req->rq_reqbuf ||
(char *) req->rq_clrbuf >=
(char *) req->rq_reqbuf + req->rq_reqbuf_len)
OBD_FREE_LARGE(req->rq_clrbuf, req->rq_clrbuf_len);
req->rq_clrbuf = NULL;
req->rq_clrbuf_len = 0;
release_reqbuf:
if (!req->rq_pool && req->rq_reqbuf) {
LASSERT(req->rq_reqbuf_len);
OBD_FREE_LARGE(req->rq_reqbuf, req->rq_reqbuf_len);
req->rq_reqbuf = NULL;
req->rq_reqbuf_len = 0;
}
}
static int do_alloc_repbuf(struct ptlrpc_request *req, int bufsize)
{
bufsize = size_roundup_power2(bufsize);
OBD_ALLOC_LARGE(req->rq_repbuf, bufsize);
if (!req->rq_repbuf)
return -ENOMEM;
req->rq_repbuf_len = bufsize;
return 0;
}
static
int gss_alloc_repbuf_intg(struct ptlrpc_sec *sec,
struct ptlrpc_request *req,
int svc, int msgsize)
{
int txtsize;
__u32 buflens[4];
int bufcnt = 2;
int alloc_size;
/*
* on-wire data layout:
* - gss header
* - lustre message
* - bulk sec descriptor (optional)
* - signature (optional)
* - svc == NULL: NULL
* - svc == AUTH: signature of gss header
* - svc == INTG: signature of all above
*
* if this is context negotiation, reserver fixed space
* at the last (signature) segment regardless of svc mode.
*/
buflens[0] = PTLRPC_GSS_HEADER_SIZE;
txtsize = buflens[0];
buflens[1] = msgsize;
if (svc == SPTLRPC_SVC_INTG)
txtsize += buflens[1];
if (req->rq_pack_bulk) {
buflens[bufcnt] = gss_cli_bulk_payload(req->rq_cli_ctx,
&req->rq_flvr,
1, req->rq_bulk_read);
if (svc == SPTLRPC_SVC_INTG)
txtsize += buflens[bufcnt];
bufcnt++;
}
if (req->rq_ctx_init)
buflens[bufcnt++] = GSS_CTX_INIT_MAX_LEN;
else if (svc != SPTLRPC_SVC_NULL)
buflens[bufcnt++] = gss_cli_payload(req->rq_cli_ctx, txtsize,0);
alloc_size = lustre_msg_size_v2(bufcnt, buflens);
/* add space for early reply */
alloc_size += gss_at_reply_off_integ;
return do_alloc_repbuf(req, alloc_size);
}
static
int gss_alloc_repbuf_priv(struct ptlrpc_sec *sec,
struct ptlrpc_request *req,
int msgsize)
{
int txtsize;
__u32 buflens[2];
int bufcnt;
int alloc_size;
/* inner buffers */
bufcnt = 1;
buflens[0] = msgsize;
if (req->rq_pack_bulk)
buflens[bufcnt++] = gss_cli_bulk_payload(req->rq_cli_ctx,
&req->rq_flvr,
1, req->rq_bulk_read);
txtsize = lustre_msg_size_v2(bufcnt, buflens);
txtsize += GSS_MAX_CIPHER_BLOCK;
/* wrapper buffers */
bufcnt = 2;
buflens[0] = PTLRPC_GSS_HEADER_SIZE;
buflens[1] = gss_cli_payload(req->rq_cli_ctx, txtsize, 1);
alloc_size = lustre_msg_size_v2(bufcnt, buflens);
/* add space for early reply */
alloc_size += gss_at_reply_off_priv;
return do_alloc_repbuf(req, alloc_size);
}
int gss_alloc_repbuf(struct ptlrpc_sec *sec,
struct ptlrpc_request *req,
int msgsize)
{
int svc = SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc);
LASSERT(!req->rq_pack_bulk ||
(req->rq_bulk_read || req->rq_bulk_write));
switch (svc) {
case SPTLRPC_SVC_NULL:
case SPTLRPC_SVC_AUTH:
case SPTLRPC_SVC_INTG:
return gss_alloc_repbuf_intg(sec, req, svc, msgsize);
case SPTLRPC_SVC_PRIV:
return gss_alloc_repbuf_priv(sec, req, msgsize);
default:
LASSERTF(0, "bad rpc flavor %x\n", req->rq_flvr.sf_rpc);
return 0;
}
}
void gss_free_repbuf(struct ptlrpc_sec *sec,
struct ptlrpc_request *req)
{
OBD_FREE_LARGE(req->rq_repbuf, req->rq_repbuf_len);
req->rq_repbuf = NULL;
req->rq_repbuf_len = 0;
req->rq_repdata = NULL;
req->rq_repdata_len = 0;
}
static int get_enlarged_msgsize(struct lustre_msg *msg,
int segment, int newsize)
{
int save, newmsg_size;
LASSERT(newsize >= msg->lm_buflens[segment]);
save = msg->lm_buflens[segment];
msg->lm_buflens[segment] = newsize;
newmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
msg->lm_buflens[segment] = save;
return newmsg_size;
}
static int get_enlarged_msgsize2(struct lustre_msg *msg,
int segment1, int newsize1,
int segment2, int newsize2)
{
int save1, save2, newmsg_size;
LASSERT(newsize1 >= msg->lm_buflens[segment1]);
LASSERT(newsize2 >= msg->lm_buflens[segment2]);
save1 = msg->lm_buflens[segment1];
save2 = msg->lm_buflens[segment2];
msg->lm_buflens[segment1] = newsize1;
msg->lm_buflens[segment2] = newsize2;
newmsg_size = lustre_msg_size_v2(msg->lm_bufcount, msg->lm_buflens);
msg->lm_buflens[segment1] = save1;
msg->lm_buflens[segment2] = save2;
return newmsg_size;
}
static
int gss_enlarge_reqbuf_intg(struct ptlrpc_sec *sec,
struct ptlrpc_request *req,
int svc,
int segment, int newsize)
{
struct lustre_msg *newbuf;
int txtsize, sigsize = 0, i;
int newmsg_size, newbuf_size;
/*
* gss header is at seg 0;
* embedded msg is at seg 1;
* signature (if any) is at the last seg
*/
LASSERT(req->rq_reqbuf);
LASSERT(req->rq_reqbuf_len > req->rq_reqlen);
LASSERT(req->rq_reqbuf->lm_bufcount >= 2);
LASSERT(lustre_msg_buf(req->rq_reqbuf, 1, 0) == req->rq_reqmsg);
/* 1. compute new embedded msg size */
newmsg_size = get_enlarged_msgsize(req->rq_reqmsg, segment, newsize);
LASSERT(newmsg_size >= req->rq_reqbuf->lm_buflens[1]);
/* 2. compute new wrapper msg size */
if (svc == SPTLRPC_SVC_NULL) {
/* no signature, get size directly */
newbuf_size = get_enlarged_msgsize(req->rq_reqbuf,
1, newmsg_size);
} else {
txtsize = req->rq_reqbuf->lm_buflens[0];
if (svc == SPTLRPC_SVC_INTG) {
for (i = 1; i < req->rq_reqbuf->lm_bufcount; i++)
txtsize += req->rq_reqbuf->lm_buflens[i];
txtsize += newmsg_size - req->rq_reqbuf->lm_buflens[1];
}
sigsize = gss_cli_payload(req->rq_cli_ctx, txtsize, 0);
LASSERT(sigsize >= msg_last_seglen(req->rq_reqbuf));
newbuf_size = get_enlarged_msgsize2(
req->rq_reqbuf,
1, newmsg_size,
msg_last_segidx(req->rq_reqbuf),
sigsize);
}
/* request from pool should always have enough buffer */
LASSERT(!req->rq_pool || req->rq_reqbuf_len >= newbuf_size);
if (req->rq_reqbuf_len < newbuf_size) {
newbuf_size = size_roundup_power2(newbuf_size);
OBD_ALLOC_LARGE(newbuf, newbuf_size);
if (newbuf == NULL)
return -ENOMEM;
memcpy(newbuf, req->rq_reqbuf, req->rq_reqbuf_len);
OBD_FREE_LARGE(req->rq_reqbuf, req->rq_reqbuf_len);
req->rq_reqbuf = newbuf;
req->rq_reqbuf_len = newbuf_size;
req->rq_reqmsg = lustre_msg_buf(req->rq_reqbuf, 1, 0);
}
/* do enlargement, from wrapper to embedded, from end to begin */
if (svc != SPTLRPC_SVC_NULL)
_sptlrpc_enlarge_msg_inplace(req->rq_reqbuf,
msg_last_segidx(req->rq_reqbuf),
sigsize);
_sptlrpc_enlarge_msg_inplace(req->rq_reqbuf, 1, newmsg_size);
_sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
req->rq_reqlen = newmsg_size;
return 0;
}
static
int gss_enlarge_reqbuf_priv(struct ptlrpc_sec *sec,
struct ptlrpc_request *req,
int segment, int newsize)
{
struct lustre_msg *newclrbuf;
int newmsg_size, newclrbuf_size, newcipbuf_size;
__u32 buflens[3];
/*
* embedded msg is at seg 0 of clear buffer;
* cipher text is at seg 2 of cipher buffer;
*/
LASSERT(req->rq_pool ||
(req->rq_reqbuf == NULL && req->rq_reqbuf_len == 0));
LASSERT(req->rq_reqbuf == NULL ||
(req->rq_pool && req->rq_reqbuf->lm_bufcount == 3));
LASSERT(req->rq_clrbuf);
LASSERT(req->rq_clrbuf_len > req->rq_reqlen);
LASSERT(lustre_msg_buf(req->rq_clrbuf, 0, 0) == req->rq_reqmsg);
/* compute new embedded msg size */
newmsg_size = get_enlarged_msgsize(req->rq_reqmsg, segment, newsize);
/* compute new clear buffer size */
newclrbuf_size = get_enlarged_msgsize(req->rq_clrbuf, 0, newmsg_size);
newclrbuf_size += GSS_MAX_CIPHER_BLOCK;
/* compute new cipher buffer size */
buflens[0] = PTLRPC_GSS_HEADER_SIZE;
buflens[1] = gss_cli_payload(req->rq_cli_ctx, buflens[0], 0);
buflens[2] = gss_cli_payload(req->rq_cli_ctx, newclrbuf_size, 1);
newcipbuf_size = lustre_msg_size_v2(3, buflens);
/* handle the case that we put both clear buf and cipher buf into
* pre-allocated single buffer. */
if (unlikely(req->rq_pool) &&
req->rq_clrbuf >= req->rq_reqbuf &&
(char *) req->rq_clrbuf <
(char *) req->rq_reqbuf + req->rq_reqbuf_len) {
/* it couldn't be better we still fit into the
* pre-allocated buffer. */
if (newclrbuf_size + newcipbuf_size <= req->rq_reqbuf_len) {
void *src, *dst;
/* move clear text backward. */
src = req->rq_clrbuf;
dst = (char *) req->rq_reqbuf + newcipbuf_size;
memmove(dst, src, req->rq_clrbuf_len);
req->rq_clrbuf = (struct lustre_msg *) dst;
req->rq_clrbuf_len = newclrbuf_size;
req->rq_reqmsg = lustre_msg_buf(req->rq_clrbuf, 0, 0);
} else {
/* sadly we have to split out the clear buffer */
LASSERT(req->rq_reqbuf_len >= newcipbuf_size);
LASSERT(req->rq_clrbuf_len < newclrbuf_size);
}
}
if (req->rq_clrbuf_len < newclrbuf_size) {
newclrbuf_size = size_roundup_power2(newclrbuf_size);
OBD_ALLOC_LARGE(newclrbuf, newclrbuf_size);
if (newclrbuf == NULL)
return -ENOMEM;
memcpy(newclrbuf, req->rq_clrbuf, req->rq_clrbuf_len);
if (req->rq_reqbuf == NULL ||
req->rq_clrbuf < req->rq_reqbuf ||
(char *) req->rq_clrbuf >=
(char *) req->rq_reqbuf + req->rq_reqbuf_len) {
OBD_FREE_LARGE(req->rq_clrbuf, req->rq_clrbuf_len);
}
req->rq_clrbuf = newclrbuf;
req->rq_clrbuf_len = newclrbuf_size;
req->rq_reqmsg = lustre_msg_buf(req->rq_clrbuf, 0, 0);
}
_sptlrpc_enlarge_msg_inplace(req->rq_clrbuf, 0, newmsg_size);
_sptlrpc_enlarge_msg_inplace(req->rq_reqmsg, segment, newsize);
req->rq_reqlen = newmsg_size;
return 0;
}
int gss_enlarge_reqbuf(struct ptlrpc_sec *sec,
struct ptlrpc_request *req,
int segment, int newsize)
{
int svc = SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc);
LASSERT(!req->rq_ctx_init && !req->rq_ctx_fini);
switch (svc) {
case SPTLRPC_SVC_NULL:
case SPTLRPC_SVC_AUTH:
case SPTLRPC_SVC_INTG:
return gss_enlarge_reqbuf_intg(sec, req, svc, segment, newsize);
case SPTLRPC_SVC_PRIV:
return gss_enlarge_reqbuf_priv(sec, req, segment, newsize);
default:
LASSERTF(0, "bad rpc flavor %x\n", req->rq_flvr.sf_rpc);
return 0;
}
}
int gss_sec_install_rctx(struct obd_import *imp,
struct ptlrpc_sec *sec,
struct ptlrpc_cli_ctx *ctx)
{
struct gss_sec *gsec;
struct gss_cli_ctx *gctx;
int rc;
gsec = container_of(sec, struct gss_sec, gs_base);
gctx = container_of(ctx, struct gss_cli_ctx, gc_base);
rc = gss_install_rvs_svc_ctx(imp, gsec, gctx);
return rc;
}
/********************************************
* server side API *
********************************************/
static inline
int gss_svc_reqctx_is_special(struct gss_svc_reqctx *grctx)
{
LASSERT(grctx);
return (grctx->src_init || grctx->src_init_continue ||
grctx->src_err_notify);
}
static
void gss_svc_reqctx_free(struct gss_svc_reqctx *grctx)
{
if (grctx->src_ctx)
gss_svc_upcall_put_ctx(grctx->src_ctx);
sptlrpc_policy_put(grctx->src_base.sc_policy);
OBD_FREE_PTR(grctx);
}
static inline
void gss_svc_reqctx_addref(struct gss_svc_reqctx *grctx)
{
LASSERT(atomic_read(&grctx->src_base.sc_refcount) > 0);
atomic_inc(&grctx->src_base.sc_refcount);
}
static inline
void gss_svc_reqctx_decref(struct gss_svc_reqctx *grctx)
{
LASSERT(atomic_read(&grctx->src_base.sc_refcount) > 0);
if (atomic_dec_and_test(&grctx->src_base.sc_refcount))
gss_svc_reqctx_free(grctx);
}
static
int gss_svc_sign(struct ptlrpc_request *req,
struct ptlrpc_reply_state *rs,
struct gss_svc_reqctx *grctx,
__u32 svc)
{
__u32 flags = 0;
int rc;
LASSERT(rs->rs_msg == lustre_msg_buf(rs->rs_repbuf, 1, 0));
/* embedded lustre_msg might have been shrinked */
if (req->rq_replen != rs->rs_repbuf->lm_buflens[1])
lustre_shrink_msg(rs->rs_repbuf, 1, req->rq_replen, 1);
if (req->rq_pack_bulk)
flags |= LUSTRE_GSS_PACK_BULK;
rc = gss_sign_msg(rs->rs_repbuf, grctx->src_ctx->gsc_mechctx,
LUSTRE_SP_ANY, flags, PTLRPC_GSS_PROC_DATA,
grctx->src_wirectx.gw_seq, svc, NULL);
if (rc < 0)
return rc;
rs->rs_repdata_len = rc;
if (likely(req->rq_packed_final)) {
if (lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT)
req->rq_reply_off = gss_at_reply_off_integ;
else
req->rq_reply_off = 0;
} else {
if (svc == SPTLRPC_SVC_NULL)
rs->rs_repbuf->lm_cksum = crc32_le(!(__u32) 0,
lustre_msg_buf(rs->rs_repbuf, 1, 0),
lustre_msg_buflen(rs->rs_repbuf, 1));
req->rq_reply_off = 0;
}
return 0;
}
int gss_pack_err_notify(struct ptlrpc_request *req, __u32 major, __u32 minor)
{
struct gss_svc_reqctx *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
struct ptlrpc_reply_state *rs;
struct gss_err_header *ghdr;
int replen = sizeof(struct ptlrpc_body);
int rc;
//if (OBD_FAIL_CHECK_ORSET(OBD_FAIL_SVCGSS_ERR_NOTIFY, OBD_FAIL_ONCE))
// return -EINVAL;
grctx->src_err_notify = 1;
grctx->src_reserve_len = 0;
rc = lustre_pack_reply_v2(req, 1, &replen, NULL, 0);
if (rc) {
CERROR("could not pack reply, err %d\n", rc);
return rc;
}
/* gss hdr */
rs = req->rq_reply_state;
LASSERT(rs->rs_repbuf->lm_buflens[1] >= sizeof(*ghdr));
ghdr = lustre_msg_buf(rs->rs_repbuf, 0, 0);
ghdr->gh_version = PTLRPC_GSS_VERSION;
ghdr->gh_flags = 0;
ghdr->gh_proc = PTLRPC_GSS_PROC_ERR;
ghdr->gh_major = major;
ghdr->gh_minor = minor;
ghdr->gh_handle.len = 0; /* fake context handle */
rs->rs_repdata_len = lustre_msg_size_v2(rs->rs_repbuf->lm_bufcount,
rs->rs_repbuf->lm_buflens);
CDEBUG(D_SEC, "prepare gss error notify(0x%x/0x%x) to %s\n",
major, minor, libcfs_nid2str(req->rq_peer.nid));
return 0;
}
static
int gss_svc_handle_init(struct ptlrpc_request *req,
struct gss_wire_ctx *gw)
{
struct gss_svc_reqctx *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
struct lustre_msg *reqbuf = req->rq_reqbuf;
struct obd_uuid *uuid;
struct obd_device *target;
rawobj_t uuid_obj, rvs_hdl, in_token;
__u32 lustre_svc;
__u32 *secdata, seclen;
int swabbed, rc;
CDEBUG(D_SEC, "processing gss init(%d) request from %s\n", gw->gw_proc,
libcfs_nid2str(req->rq_peer.nid));
req->rq_ctx_init = 1;
if (gw->gw_flags & LUSTRE_GSS_PACK_BULK) {
CERROR("unexpected bulk flag\n");
return SECSVC_DROP;
}
if (gw->gw_proc == PTLRPC_GSS_PROC_INIT && gw->gw_handle.len != 0) {
CERROR("proc %u: invalid handle length %u\n",
gw->gw_proc, gw->gw_handle.len);
return SECSVC_DROP;
}
if (reqbuf->lm_bufcount < 3 || reqbuf->lm_bufcount > 4){
CERROR("Invalid bufcount %d\n", reqbuf->lm_bufcount);
return SECSVC_DROP;
}
swabbed = ptlrpc_req_need_swab(req);
/* ctx initiate payload is in last segment */
secdata = lustre_msg_buf(reqbuf, reqbuf->lm_bufcount - 1, 0);
seclen = reqbuf->lm_buflens[reqbuf->lm_bufcount - 1];
if (seclen < 4 + 4) {
CERROR("sec size %d too small\n", seclen);
return SECSVC_DROP;
}
/* lustre svc type */
lustre_svc = le32_to_cpu(*secdata++);
seclen -= 4;
/* extract target uuid, note this code is somewhat fragile
* because touched internal structure of obd_uuid */
if (rawobj_extract(&uuid_obj, &secdata, &seclen)) {
CERROR("failed to extract target uuid\n");
return SECSVC_DROP;
}
uuid_obj.data[uuid_obj.len - 1] = '\0';
uuid = (struct obd_uuid *) uuid_obj.data;
target = class_uuid2obd(uuid);
if (!target || target->obd_stopping || !target->obd_set_up) {
CERROR("target '%s' is not available for context init (%s)\n",
uuid->uuid, target == NULL ? "no target" :
(target->obd_stopping ? "stopping" : "not set up"));
return SECSVC_DROP;
}
/* extract reverse handle */
if (rawobj_extract(&rvs_hdl, &secdata, &seclen)) {
CERROR("failed extract reverse handle\n");
return SECSVC_DROP;
}
/* extract token */
if (rawobj_extract(&in_token, &secdata, &seclen)) {
CERROR("can't extract token\n");
return SECSVC_DROP;
}
rc = gss_svc_upcall_handle_init(req, grctx, gw, target, lustre_svc,
&rvs_hdl, &in_token);
if (rc != SECSVC_OK)
return rc;
if (grctx->src_ctx->gsc_usr_mds || grctx->src_ctx->gsc_usr_oss ||
grctx->src_ctx->gsc_usr_root)
CWARN("create svc ctx %p: user from %s authenticated as %s\n",
grctx->src_ctx, libcfs_nid2str(req->rq_peer.nid),
grctx->src_ctx->gsc_usr_mds ? "mds" :
(grctx->src_ctx->gsc_usr_oss ? "oss" : "root"));
else
CWARN("create svc ctx %p: accept user %u from %s\n",
grctx->src_ctx, grctx->src_ctx->gsc_uid,
libcfs_nid2str(req->rq_peer.nid));
if (gw->gw_flags & LUSTRE_GSS_PACK_USER) {
if (reqbuf->lm_bufcount < 4) {
CERROR("missing user descriptor\n");
return SECSVC_DROP;
}
if (sptlrpc_unpack_user_desc(reqbuf, 2, swabbed)) {
CERROR("Mal-formed user descriptor\n");
return SECSVC_DROP;
}
req->rq_pack_udesc = 1;
req->rq_user_desc = lustre_msg_buf(reqbuf, 2, 0);
}
req->rq_reqmsg = lustre_msg_buf(reqbuf, 1, 0);
req->rq_reqlen = lustre_msg_buflen(reqbuf, 1);
return rc;
}
/*
* last segment must be the gss signature.
*/
static
int gss_svc_verify_request(struct ptlrpc_request *req,
struct gss_svc_reqctx *grctx,
struct gss_wire_ctx *gw,
__u32 *major)
{
struct gss_svc_ctx *gctx = grctx->src_ctx;
struct lustre_msg *msg = req->rq_reqbuf;
int offset = 2;
int swabbed;
*major = GSS_S_COMPLETE;
if (msg->lm_bufcount < 2) {
CERROR("Too few segments (%u) in request\n", msg->lm_bufcount);
return -EINVAL;
}
if (gw->gw_svc == SPTLRPC_SVC_NULL)
goto verified;
if (gss_check_seq_num(&gctx->gsc_seqdata, gw->gw_seq, 0)) {
CERROR("phase 0: discard replayed req: seq %u\n", gw->gw_seq);
*major = GSS_S_DUPLICATE_TOKEN;
return -EACCES;
}
*major = gss_verify_msg(msg, gctx->gsc_mechctx, gw->gw_svc);
if (*major != GSS_S_COMPLETE) {
CERROR("failed to verify request: %x\n", *major);
return -EACCES;
}
if (gctx->gsc_reverse == 0 &&
gss_check_seq_num(&gctx->gsc_seqdata, gw->gw_seq, 1)) {
CERROR("phase 1+: discard replayed req: seq %u\n", gw->gw_seq);
*major = GSS_S_DUPLICATE_TOKEN;
return -EACCES;
}
verified:
swabbed = ptlrpc_req_need_swab(req);
/* user descriptor */
if (gw->gw_flags & LUSTRE_GSS_PACK_USER) {
if (msg->lm_bufcount < (offset + 1)) {
CERROR("no user desc included\n");
return -EINVAL;
}
if (sptlrpc_unpack_user_desc(msg, offset, swabbed)) {
CERROR("Mal-formed user descriptor\n");
return -EINVAL;
}
req->rq_pack_udesc = 1;
req->rq_user_desc = lustre_msg_buf(msg, offset, 0);
offset++;
}
/* check bulk_sec_desc data */
if (gw->gw_flags & LUSTRE_GSS_PACK_BULK) {
if (msg->lm_bufcount < (offset + 1)) {
CERROR("missing bulk sec descriptor\n");
return -EINVAL;
}
if (bulk_sec_desc_unpack(msg, offset, swabbed))
return -EINVAL;
req->rq_pack_bulk = 1;
grctx->src_reqbsd = lustre_msg_buf(msg, offset, 0);
grctx->src_reqbsd_size = lustre_msg_buflen(msg, offset);
}
req->rq_reqmsg = lustre_msg_buf(msg, 1, 0);
req->rq_reqlen = msg->lm_buflens[1];
return 0;
}
static
int gss_svc_unseal_request(struct ptlrpc_request *req,
struct gss_svc_reqctx *grctx,
struct gss_wire_ctx *gw,
__u32 *major)
{
struct gss_svc_ctx *gctx = grctx->src_ctx;
struct lustre_msg *msg = req->rq_reqbuf;
int swabbed, msglen, offset = 1;
if (gss_check_seq_num(&gctx->gsc_seqdata, gw->gw_seq, 0)) {
CERROR("phase 0: discard replayed req: seq %u\n", gw->gw_seq);
*major = GSS_S_DUPLICATE_TOKEN;
return -EACCES;
}
*major = gss_unseal_msg(gctx->gsc_mechctx, msg,
&msglen, req->rq_reqdata_len);
if (*major != GSS_S_COMPLETE) {
CERROR("failed to unwrap request: %x\n", *major);
return -EACCES;
}
if (gss_check_seq_num(&gctx->gsc_seqdata, gw->gw_seq, 1)) {
CERROR("phase 1+: discard replayed req: seq %u\n", gw->gw_seq);
*major = GSS_S_DUPLICATE_TOKEN;
return -EACCES;
}
swabbed = __lustre_unpack_msg(msg, msglen);
if (swabbed < 0) {
CERROR("Failed to unpack after decryption\n");
return -EINVAL;
}
req->rq_reqdata_len = msglen;
if (msg->lm_bufcount < 1) {
CERROR("Invalid buffer: is empty\n");
return -EINVAL;
}
if (gw->gw_flags & LUSTRE_GSS_PACK_USER) {
if (msg->lm_bufcount < offset + 1) {
CERROR("no user descriptor included\n");
return -EINVAL;
}
if (sptlrpc_unpack_user_desc(msg, offset, swabbed)) {
CERROR("Mal-formed user descriptor\n");
return -EINVAL;
}
req->rq_pack_udesc = 1;
req->rq_user_desc = lustre_msg_buf(msg, offset, 0);
offset++;
}
if (gw->gw_flags & LUSTRE_GSS_PACK_BULK) {
if (msg->lm_bufcount < offset + 1) {
CERROR("no bulk checksum included\n");
return -EINVAL;
}
if (bulk_sec_desc_unpack(msg, offset, swabbed))
return -EINVAL;
req->rq_pack_bulk = 1;
grctx->src_reqbsd = lustre_msg_buf(msg, offset, 0);
grctx->src_reqbsd_size = lustre_msg_buflen(msg, offset);
}
req->rq_reqmsg = lustre_msg_buf(req->rq_reqbuf, 0, 0);
req->rq_reqlen = req->rq_reqbuf->lm_buflens[0];
return 0;
}
static
int gss_svc_handle_data(struct ptlrpc_request *req,
struct gss_wire_ctx *gw)
{
struct gss_svc_reqctx *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
__u32 major = 0;
int rc = 0;
grctx->src_ctx = gss_svc_upcall_get_ctx(req, gw);
if (!grctx->src_ctx) {
major = GSS_S_NO_CONTEXT;
goto error;
}
switch (gw->gw_svc) {
case SPTLRPC_SVC_NULL:
case SPTLRPC_SVC_AUTH:
case SPTLRPC_SVC_INTG:
rc = gss_svc_verify_request(req, grctx, gw, &major);
break;
case SPTLRPC_SVC_PRIV:
rc = gss_svc_unseal_request(req, grctx, gw, &major);
break;
default:
CERROR("unsupported gss service %d\n", gw->gw_svc);
rc = -EINVAL;
}
if (rc == 0)
return SECSVC_OK;
CERROR("svc %u failed: major 0x%08x: req xid "LPU64" ctx %p idx "
LPX64"(%u->%s)\n", gw->gw_svc, major, req->rq_xid,
grctx->src_ctx, gss_handle_to_u64(&gw->gw_handle),
grctx->src_ctx->gsc_uid, libcfs_nid2str(req->rq_peer.nid));
error:
/* we only notify client in case of NO_CONTEXT/BAD_SIG, which
* might happen after server reboot, to allow recovery. */
if ((major == GSS_S_NO_CONTEXT || major == GSS_S_BAD_SIG) &&
gss_pack_err_notify(req, major, 0) == 0)
return SECSVC_COMPLETE;
return SECSVC_DROP;
}
static
int gss_svc_handle_destroy(struct ptlrpc_request *req,
struct gss_wire_ctx *gw)
{
struct gss_svc_reqctx *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
__u32 major;
req->rq_ctx_fini = 1;
req->rq_no_reply = 1;
grctx->src_ctx = gss_svc_upcall_get_ctx(req, gw);
if (!grctx->src_ctx) {
CDEBUG(D_SEC, "invalid gss context handle for destroy.\n");
return SECSVC_DROP;
}
if (gw->gw_svc != SPTLRPC_SVC_INTG) {
CERROR("svc %u is not supported in destroy.\n", gw->gw_svc);
return SECSVC_DROP;
}
if (gss_svc_verify_request(req, grctx, gw, &major))
return SECSVC_DROP;
CWARN("destroy svc ctx %p idx "LPX64" (%u->%s)\n",
grctx->src_ctx, gss_handle_to_u64(&gw->gw_handle),
grctx->src_ctx->gsc_uid, libcfs_nid2str(req->rq_peer.nid));
gss_svc_upcall_destroy_ctx(grctx->src_ctx);
if (gw->gw_flags & LUSTRE_GSS_PACK_USER) {
if (req->rq_reqbuf->lm_bufcount < 4) {
CERROR("missing user descriptor, ignore it\n");
return SECSVC_OK;
}
if (sptlrpc_unpack_user_desc(req->rq_reqbuf, 2,
ptlrpc_req_need_swab(req))) {
CERROR("Mal-formed user descriptor, ignore it\n");
return SECSVC_OK;
}
req->rq_pack_udesc = 1;
req->rq_user_desc = lustre_msg_buf(req->rq_reqbuf, 2, 0);
}
return SECSVC_OK;
}
int gss_svc_accept(struct ptlrpc_sec_policy *policy, struct ptlrpc_request *req)
{
struct gss_header *ghdr;
struct gss_svc_reqctx *grctx;
struct gss_wire_ctx *gw;
int swabbed, rc;
LASSERT(req->rq_reqbuf);
LASSERT(req->rq_svc_ctx == NULL);
if (req->rq_reqbuf->lm_bufcount < 2) {
CERROR("buf count only %d\n", req->rq_reqbuf->lm_bufcount);
return SECSVC_DROP;
}
swabbed = ptlrpc_req_need_swab(req);
ghdr = gss_swab_header(req->rq_reqbuf, 0, swabbed);
if (ghdr == NULL) {
CERROR("can't decode gss header\n");
return SECSVC_DROP;
}
/* sanity checks */
if (ghdr->gh_version != PTLRPC_GSS_VERSION) {
CERROR("gss version %u, expect %u\n", ghdr->gh_version,
PTLRPC_GSS_VERSION);
return SECSVC_DROP;
}
req->rq_sp_from = ghdr->gh_sp;
/* alloc grctx data */
OBD_ALLOC_PTR(grctx);
if (!grctx)
return SECSVC_DROP;
grctx->src_base.sc_policy = sptlrpc_policy_get(policy);
atomic_set(&grctx->src_base.sc_refcount, 1);
req->rq_svc_ctx = &grctx->src_base;
gw = &grctx->src_wirectx;
/* save wire context */
gw->gw_flags = ghdr->gh_flags;
gw->gw_proc = ghdr->gh_proc;
gw->gw_seq = ghdr->gh_seq;
gw->gw_svc = ghdr->gh_svc;
rawobj_from_netobj(&gw->gw_handle, &ghdr->gh_handle);
/* keep original wire header which subject to checksum verification */
if (swabbed)
gss_header_swabber(ghdr);
switch(ghdr->gh_proc) {
case PTLRPC_GSS_PROC_INIT:
case PTLRPC_GSS_PROC_CONTINUE_INIT:
rc = gss_svc_handle_init(req, gw);
break;
case PTLRPC_GSS_PROC_DATA:
rc = gss_svc_handle_data(req, gw);
break;
case PTLRPC_GSS_PROC_DESTROY:
rc = gss_svc_handle_destroy(req, gw);
break;
default:
CERROR("unknown proc %u\n", gw->gw_proc);
rc = SECSVC_DROP;
break;
}
switch (rc) {
case SECSVC_OK:
LASSERT (grctx->src_ctx);
req->rq_auth_gss = 1;
req->rq_auth_remote = grctx->src_ctx->gsc_remote;
req->rq_auth_usr_mdt = grctx->src_ctx->gsc_usr_mds;
req->rq_auth_usr_ost = grctx->src_ctx->gsc_usr_oss;
req->rq_auth_usr_root = grctx->src_ctx->gsc_usr_root;
req->rq_auth_uid = grctx->src_ctx->gsc_uid;
req->rq_auth_mapped_uid = grctx->src_ctx->gsc_mapped_uid;
break;
case SECSVC_COMPLETE:
break;
case SECSVC_DROP:
gss_svc_reqctx_free(grctx);
req->rq_svc_ctx = NULL;
break;
}
return rc;
}
void gss_svc_invalidate_ctx(struct ptlrpc_svc_ctx *svc_ctx)
{
struct gss_svc_reqctx *grctx;
if (svc_ctx == NULL) {
return;
}
grctx = gss_svc_ctx2reqctx(svc_ctx);
CWARN("gss svc invalidate ctx %p(%u)\n",
grctx->src_ctx, grctx->src_ctx->gsc_uid);
gss_svc_upcall_destroy_ctx(grctx->src_ctx);
}
static inline
int gss_svc_payload(struct gss_svc_reqctx *grctx, int early,
int msgsize, int privacy)
{
/* we should treat early reply normally, but which is actually sharing
* the same ctx with original request, so in this case we should
* ignore the special ctx's special flags */
if (early == 0 && gss_svc_reqctx_is_special(grctx))
return grctx->src_reserve_len;
return gss_mech_payload(NULL, msgsize, privacy);
}
static int gss_svc_bulk_payload(struct gss_svc_ctx *gctx,
struct sptlrpc_flavor *flvr,
int read)
{
int payload = sizeof(struct ptlrpc_bulk_sec_desc);
if (read) {
switch (SPTLRPC_FLVR_BULK_SVC(flvr->sf_rpc)) {
case SPTLRPC_BULK_SVC_NULL:
break;
case SPTLRPC_BULK_SVC_INTG:
payload += gss_mech_payload(NULL, 0, 0);
break;
case SPTLRPC_BULK_SVC_PRIV:
payload += gss_mech_payload(NULL, 0, 1);
break;
case SPTLRPC_BULK_SVC_AUTH:
default:
LBUG();
}
}
return payload;
}
int gss_svc_alloc_rs(struct ptlrpc_request *req, int msglen)
{
struct gss_svc_reqctx *grctx;
struct ptlrpc_reply_state *rs;
int early, privacy, svc, bsd_off = 0;
__u32 ibuflens[2], buflens[4];
int ibufcnt = 0, bufcnt;
int txtsize, wmsg_size, rs_size;
LASSERT(msglen % 8 == 0);
if (req->rq_pack_bulk && !req->rq_bulk_read && !req->rq_bulk_write) {
CERROR("client request bulk sec on non-bulk rpc\n");
return -EPROTO;
}
svc = SPTLRPC_FLVR_SVC(req->rq_flvr.sf_rpc);
early = (req->rq_packed_final == 0);
grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
if (!early && gss_svc_reqctx_is_special(grctx))
privacy = 0;
else
privacy = (svc == SPTLRPC_SVC_PRIV);
if (privacy) {
/* inner clear buffers */
ibufcnt = 1;
ibuflens[0] = msglen;
if (req->rq_pack_bulk) {
LASSERT(grctx->src_reqbsd);
bsd_off = ibufcnt;
ibuflens[ibufcnt++] = gss_svc_bulk_payload(
grctx->src_ctx,
&req->rq_flvr,
req->rq_bulk_read);
}
txtsize = lustre_msg_size_v2(ibufcnt, ibuflens);
txtsize += GSS_MAX_CIPHER_BLOCK;
/* wrapper buffer */
bufcnt = 2;
buflens[0] = PTLRPC_GSS_HEADER_SIZE;
buflens[1] = gss_svc_payload(grctx, early, txtsize, 1);
} else {
bufcnt = 2;
buflens[0] = PTLRPC_GSS_HEADER_SIZE;
buflens[1] = msglen;
txtsize = buflens[0];
if (svc == SPTLRPC_SVC_INTG)
txtsize += buflens[1];
if (req->rq_pack_bulk) {
LASSERT(grctx->src_reqbsd);
bsd_off = bufcnt;
buflens[bufcnt] = gss_svc_bulk_payload(
grctx->src_ctx,
&req->rq_flvr,
req->rq_bulk_read);
if (svc == SPTLRPC_SVC_INTG)
txtsize += buflens[bufcnt];
bufcnt++;
}
if ((!early && gss_svc_reqctx_is_special(grctx)) ||
svc != SPTLRPC_SVC_NULL)
buflens[bufcnt++] = gss_svc_payload(grctx, early,
txtsize, 0);
}
wmsg_size = lustre_msg_size_v2(bufcnt, buflens);
rs_size = sizeof(*rs) + wmsg_size;
rs = req->rq_reply_state;
if (rs) {
/* pre-allocated */
LASSERT(rs->rs_size >= rs_size);
} else {
OBD_ALLOC_LARGE(rs, rs_size);
if (rs == NULL)
return -ENOMEM;
rs->rs_size = rs_size;
}
rs->rs_repbuf = (struct lustre_msg *) (rs + 1);
rs->rs_repbuf_len = wmsg_size;
/* initialize the buffer */
if (privacy) {
lustre_init_msg_v2(rs->rs_repbuf, ibufcnt, ibuflens, NULL);
rs->rs_msg = lustre_msg_buf(rs->rs_repbuf, 0, msglen);
} else {
lustre_init_msg_v2(rs->rs_repbuf, bufcnt, buflens, NULL);
rs->rs_repbuf->lm_secflvr = req->rq_flvr.sf_rpc;
rs->rs_msg = lustre_msg_buf(rs->rs_repbuf, 1, 0);
}
if (bsd_off) {
grctx->src_repbsd = lustre_msg_buf(rs->rs_repbuf, bsd_off, 0);
grctx->src_repbsd_size = lustre_msg_buflen(rs->rs_repbuf,
bsd_off);
}
gss_svc_reqctx_addref(grctx);
rs->rs_svc_ctx = req->rq_svc_ctx;
LASSERT(rs->rs_msg);
req->rq_reply_state = rs;
return 0;
}
static int gss_svc_seal(struct ptlrpc_request *req,
struct ptlrpc_reply_state *rs,
struct gss_svc_reqctx *grctx)
{
struct gss_svc_ctx *gctx = grctx->src_ctx;
rawobj_t hdrobj, msgobj, token;
struct gss_header *ghdr;
__u8 *token_buf;
int token_buflen;
__u32 buflens[2], major;
int msglen, rc;
/* get clear data length. note embedded lustre_msg might
* have been shrinked */
if (req->rq_replen != lustre_msg_buflen(rs->rs_repbuf, 0))
msglen = lustre_shrink_msg(rs->rs_repbuf, 0, req->rq_replen, 1);
else
msglen = lustre_msg_size_v2(rs->rs_repbuf->lm_bufcount,
rs->rs_repbuf->lm_buflens);
/* temporarily use tail of buffer to hold gss header data */
LASSERT(msglen + PTLRPC_GSS_HEADER_SIZE <= rs->rs_repbuf_len);
ghdr = (struct gss_header *) ((char *) rs->rs_repbuf +
rs->rs_repbuf_len - PTLRPC_GSS_HEADER_SIZE);
ghdr->gh_version = PTLRPC_GSS_VERSION;
ghdr->gh_sp = LUSTRE_SP_ANY;
ghdr->gh_flags = 0;
ghdr->gh_proc = PTLRPC_GSS_PROC_DATA;
ghdr->gh_seq = grctx->src_wirectx.gw_seq;
ghdr->gh_svc = SPTLRPC_SVC_PRIV;
ghdr->gh_handle.len = 0;
if (req->rq_pack_bulk)
ghdr->gh_flags |= LUSTRE_GSS_PACK_BULK;
/* allocate temporary cipher buffer */
token_buflen = gss_mech_payload(gctx->gsc_mechctx, msglen, 1);
OBD_ALLOC_LARGE(token_buf, token_buflen);
if (token_buf == NULL)
return -ENOMEM;
hdrobj.len = PTLRPC_GSS_HEADER_SIZE;
hdrobj.data = (__u8 *) ghdr;
msgobj.len = msglen;
msgobj.data = (__u8 *) rs->rs_repbuf;
token.len = token_buflen;
token.data = token_buf;
major = lgss_wrap(gctx->gsc_mechctx, &hdrobj, &msgobj,
rs->rs_repbuf_len - PTLRPC_GSS_HEADER_SIZE, &token);
if (major != GSS_S_COMPLETE) {
CERROR("wrap message error: %08x\n", major);
GOTO(out_free, rc = -EPERM);
}
LASSERT(token.len <= token_buflen);
/* we are about to override data at rs->rs_repbuf, nullify pointers
* to which to catch further illegal usage. */
if (req->rq_pack_bulk) {
grctx->src_repbsd = NULL;
grctx->src_repbsd_size = 0;
}
/* now fill the actual wire data
* - gss header
* - gss token
*/
buflens[0] = PTLRPC_GSS_HEADER_SIZE;
buflens[1] = token.len;
rs->rs_repdata_len = lustre_msg_size_v2(2, buflens);
LASSERT(rs->rs_repdata_len <= rs->rs_repbuf_len);
lustre_init_msg_v2(rs->rs_repbuf, 2, buflens, NULL);
rs->rs_repbuf->lm_secflvr = req->rq_flvr.sf_rpc;
memcpy(lustre_msg_buf(rs->rs_repbuf, 0, 0), ghdr,
PTLRPC_GSS_HEADER_SIZE);
memcpy(lustre_msg_buf(rs->rs_repbuf, 1, 0), token.data, token.len);
/* reply offset */
if (req->rq_packed_final &&
(lustre_msghdr_get_flags(req->rq_reqmsg) & MSGHDR_AT_SUPPORT))
req->rq_reply_off = gss_at_reply_off_priv;
else
req->rq_reply_off = 0;
/* to catch upper layer's further access */
rs->rs_msg = NULL;
req->rq_repmsg = NULL;
req->rq_replen = 0;
rc = 0;
out_free:
OBD_FREE_LARGE(token_buf, token_buflen);
return rc;
}
int gss_svc_authorize(struct ptlrpc_request *req)
{
struct ptlrpc_reply_state *rs = req->rq_reply_state;
struct gss_svc_reqctx *grctx = gss_svc_ctx2reqctx(req->rq_svc_ctx);
struct gss_wire_ctx *gw = &grctx->src_wirectx;
int early, rc;
early = (req->rq_packed_final == 0);
if (!early && gss_svc_reqctx_is_special(grctx)) {
LASSERT(rs->rs_repdata_len != 0);
req->rq_reply_off = gss_at_reply_off_integ;
return 0;
}
/* early reply could happen in many cases */
if (!early &&
gw->gw_proc != PTLRPC_GSS_PROC_DATA &&
gw->gw_proc != PTLRPC_GSS_PROC_DESTROY) {
CERROR("proc %d not support\n", gw->gw_proc);
return -EINVAL;
}
LASSERT(grctx->src_ctx);
switch (gw->gw_svc) {
case SPTLRPC_SVC_NULL:
case SPTLRPC_SVC_AUTH:
case SPTLRPC_SVC_INTG:
rc = gss_svc_sign(req, rs, grctx, gw->gw_svc);
break;
case SPTLRPC_SVC_PRIV:
rc = gss_svc_seal(req, rs, grctx);
break;
default:
CERROR("Unknown service %d\n", gw->gw_svc);
GOTO(out, rc = -EINVAL);
}
rc = 0;
out:
return rc;
}
void gss_svc_free_rs(struct ptlrpc_reply_state *rs)
{
struct gss_svc_reqctx *grctx;
LASSERT(rs->rs_svc_ctx);
grctx = container_of(rs->rs_svc_ctx, struct gss_svc_reqctx, src_base);
gss_svc_reqctx_decref(grctx);
rs->rs_svc_ctx = NULL;
if (!rs->rs_prealloc)
OBD_FREE_LARGE(rs, rs->rs_size);
}
void gss_svc_free_ctx(struct ptlrpc_svc_ctx *ctx)
{
LASSERT(atomic_read(&ctx->sc_refcount) == 0);
gss_svc_reqctx_free(gss_svc_ctx2reqctx(ctx));
}
int gss_copy_rvc_cli_ctx(struct ptlrpc_cli_ctx *cli_ctx,
struct ptlrpc_svc_ctx *svc_ctx)
{
struct gss_cli_ctx *cli_gctx = ctx2gctx(cli_ctx);
struct gss_svc_ctx *svc_gctx = gss_svc_ctx2gssctx(svc_ctx);
struct gss_ctx *mechctx = NULL;
LASSERT(cli_gctx);
LASSERT(svc_gctx && svc_gctx->gsc_mechctx);
cli_gctx->gc_proc = PTLRPC_GSS_PROC_DATA;
cli_gctx->gc_win = GSS_SEQ_WIN;
/* The problem is the reverse ctx might get lost in some recovery
* situations, and the same svc_ctx will be used to re-create it.
* if there's callback be sentout before that, new reverse ctx start
* with sequence 0 will lead to future callback rpc be treated as
* replay.
*
* each reverse root ctx will record its latest sequence number on its
* buddy svcctx before be destroied, so here we continue use it.
*/
atomic_set(&cli_gctx->gc_seq, svc_gctx->gsc_rvs_seq);
if (gss_svc_upcall_dup_handle(&cli_gctx->gc_svc_handle, svc_gctx)) {
CERROR("failed to dup svc handle\n");
goto err_out;
}
if (lgss_copy_reverse_context(svc_gctx->gsc_mechctx, &mechctx) !=
GSS_S_COMPLETE) {
CERROR("failed to copy mech context\n");
goto err_svc_handle;
}
if (rawobj_dup(&cli_gctx->gc_handle, &svc_gctx->gsc_rvs_hdl)) {
CERROR("failed to dup reverse handle\n");
goto err_ctx;
}
cli_gctx->gc_mechctx = mechctx;
gss_cli_ctx_uptodate(cli_gctx);
return 0;
err_ctx:
lgss_delete_sec_context(&mechctx);
err_svc_handle:
rawobj_free(&cli_gctx->gc_svc_handle);
err_out:
return -ENOMEM;
}
static void gss_init_at_reply_offset(void)
{
__u32 buflens[3];
int clearsize;
buflens[0] = PTLRPC_GSS_HEADER_SIZE;
buflens[1] = lustre_msg_early_size();
buflens[2] = gss_cli_payload(NULL, buflens[1], 0);
gss_at_reply_off_integ = lustre_msg_size_v2(3, buflens);
buflens[0] = lustre_msg_early_size();
clearsize = lustre_msg_size_v2(1, buflens);
buflens[0] = PTLRPC_GSS_HEADER_SIZE;
buflens[1] = gss_cli_payload(NULL, clearsize, 0);
buflens[2] = gss_cli_payload(NULL, clearsize, 1);
gss_at_reply_off_priv = lustre_msg_size_v2(3, buflens);
}
int __init sptlrpc_gss_init(void)
{
int rc;
rc = gss_init_lproc();
if (rc)
return rc;
rc = gss_init_cli_upcall();
if (rc)
goto out_lproc;
rc = gss_init_svc_upcall();
if (rc)
goto out_cli_upcall;
rc = init_kerberos_module();
if (rc)
goto out_svc_upcall;
/* register policy after all other stuff be intialized, because it
* might be in used immediately after the registration. */
rc = gss_init_keyring();
if (rc)
goto out_kerberos;
#ifdef HAVE_GSS_PIPEFS
rc = gss_init_pipefs();
if (rc)
goto out_keyring;
#endif
gss_init_at_reply_offset();
return 0;
#ifdef HAVE_GSS_PIPEFS
out_keyring:
gss_exit_keyring();
#endif
out_kerberos:
cleanup_kerberos_module();
out_svc_upcall:
gss_exit_svc_upcall();
out_cli_upcall:
gss_exit_cli_upcall();
out_lproc:
gss_exit_lproc();
return rc;
}
static void __exit sptlrpc_gss_exit(void)
{
gss_exit_keyring();
#ifdef HAVE_GSS_PIPEFS
gss_exit_pipefs();
#endif
cleanup_kerberos_module();
gss_exit_svc_upcall();
gss_exit_cli_upcall();
gss_exit_lproc();
}
MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
MODULE_DESCRIPTION("GSS security policy for Lustre");
MODULE_LICENSE("GPL");
module_init(sptlrpc_gss_init);
module_exit(sptlrpc_gss_exit);
|